]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mediainfo/mediainfo.cpp
Backport from BEE
[bacula/bacula] / bacula / src / qt-console / mediainfo / mediainfo.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16
17 #include "bat.h"
18 #include <QAbstractEventDispatcher>
19 #include <QTableWidgetItem>
20 #include <QMessageBox>
21 #include "mediaedit/mediaedit.h"
22 #include "relabel/relabel.h"
23 #include "run/run.h"
24 #include "mediainfo.h"
25 #include "util/fmtwidgetitem.h"
26 #include "job/job.h"
27
28 /*
29  * A constructor
30  */
31 MediaInfo::MediaInfo(QTreeWidgetItem *parentWidget, QString &mediaName)
32   : Pages()
33 {
34    setupUi(this);
35    pgInitialize(tr("Media Info"), parentWidget);
36    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
37    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge-edit.png")));
38    m_mediaName = mediaName;
39    connect(pbPrune, SIGNAL(clicked()), this, SLOT(pruneVol()));
40    connect(pbPurge, SIGNAL(clicked()), this, SLOT(purgeVol()));
41    connect(pbDelete, SIGNAL(clicked()), this, SLOT(deleteVol()));
42    connect(pbEdit, SIGNAL(clicked()), this, SLOT(editVol()));
43    connect(tableJob, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(showInfoForJob(QTableWidgetItem *)));
44
45    dockPage();
46    setCurrent();
47    populateForm();
48 }
49
50 /*
51  * Subroutine to call class to show the log in the database from that job
52  */
53 void MediaInfo::showInfoForJob(QTableWidgetItem * item)
54 {
55    QTreeWidgetItem* pageSelectorTreeWidgetItem = mainWin->getFromHash(this);
56    int row = item->row();
57    QString jobid = tableJob->item(row, 0)->text();
58    new Job(jobid, pageSelectorTreeWidgetItem);
59 //   connect(j, SIGNAL(destroyed()), this, SLOT(populateTree()));
60 }
61
62 void MediaInfo::pruneVol()
63 {
64    new prunePage(m_mediaName, "");
65 //   connect(prune, SIGNAL(destroyed()), this, SLOT(populateTree()));
66 }
67
68 // TODO: use same functions as in medialist.cpp
69 void MediaInfo::purgeVol()
70 {
71    if (QMessageBox::warning(this, "Bat",
72       tr("Are you sure you want to purge ??  !!!.\n"
73 "The Purge command will delete associated Catalog database records from Jobs and"
74 " Volumes without considering the retention period. Purge  works only on the"
75 " Catalog database and does not affect data written to Volumes. This command can"
76 " be dangerous because you can delete catalog records associated with current"
77 " backups of files, and we recommend that you do not use it unless you know what"
78 " you are doing.\n"
79       "Press OK to proceed with the purge operation?"),
80       QMessageBox::Ok | QMessageBox::Cancel)
81       == QMessageBox::Cancel) { return; }
82
83    QString cmd("purge volume=");
84    cmd += m_mediaName;
85    consoleCommand(cmd);
86 }
87
88 void MediaInfo::deleteVol()
89 {
90    if (QMessageBox::warning(this, "Bat",
91       tr("Are you sure you want to delete??  !!!.\n"
92 "This delete command is used to delete a Volume record and all associated catalog"
93 " records that were created. This command operates only on the Catalog"
94 " database and has no effect on the actual data written to a Volume. This"
95 " command can be dangerous and we strongly recommend that you do not use"
96 " it unless you know what you are doing.  All Jobs and all associated"
97 " records (File and JobMedia) will be deleted from the catalog."
98       "Press OK to proceed with delete operation.?"),
99       QMessageBox::Ok | QMessageBox::Cancel)
100       == QMessageBox::Cancel) { return; }
101
102    QString cmd("delete volume=");
103    cmd += m_mediaName;
104    consoleCommand(cmd);
105 }
106
107 void MediaInfo::editVol()
108 {
109    new MediaEdit(mainWin->getFromHash(this), m_mediaId);
110 //   connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
111 }
112
113 /*
114  * Populate the text in the window
115  */
116 void MediaInfo::populateForm()
117 {
118    utime_t t;
119    time_t ttime;
120
121    QString stat, LastWritten;
122    struct tm tm;
123    char buf[256];
124    QString query =
125       "SELECT MediaId, VolumeName, Pool.Name, MediaType, FirstWritten,"
126       "LastWritten, VolMounts, VolBytes, Media.Enabled,"
127       "Location.Location, VolStatus, RecyclePool.Name, Media.Recycle, "
128       "VolReadTime/1000000, VolWriteTime/1000000, Media.VolUseDuration, "
129       "Media.MaxVolJobs, Media.MaxVolFiles, Media.MaxVolBytes, "
130       "Media.VolRetention,InChanger,Slot "
131       "FROM Media JOIN Pool USING (PoolId) LEFT JOIN Pool AS RecyclePool "
132       "ON (Media.RecyclePoolId=RecyclePool.PoolId) "
133       "LEFT JOIN Location ON (Media.LocationId=Location.LocationId) "
134       "WHERE Media.VolumeName='" + m_mediaName + "'";
135
136    if (mainWin->m_sqlDebug) {
137       Pmsg1(000, "MediaInfo query cmd : %s\n",query.toUtf8().data());
138    }
139    QStringList results;
140    if (m_console->sql_cmd(query, results)) {
141       QString resultline;
142       QStringList fieldlist;
143
144       foreach (resultline, results) { // should have only one result
145          fieldlist = resultline.split("\t");
146          QStringListIterator fld(fieldlist);
147          m_mediaId = fld.next();
148
149          label_VolumeName->setText(fld.next());
150          label_Pool->setText(fld.next());
151          label_MediaType->setText(fld.next());
152          label_FirstWritten->setText(fld.next());
153          LastWritten = fld.next();
154          label_LastWritten->setText(LastWritten);
155 //         label_VolFiles->setText(fld.next());
156          label_VolMounts->setText(fld.next());
157          label_VolBytes->setText(convertBytesSI(fld.next().toULongLong()));
158          label_Enabled->setPixmap(QPixmap(":/images/inflag" + fld.next() + ".png"));
159          label_Location->setText(fld.next());
160          label_VolStatus->setText(fld.next());
161          label_RecyclePool->setText(fld.next());
162          chkbox_Recycle->setCheckState(fld.next().toInt()?Qt::Checked:Qt::Unchecked);
163          edit_utime(fld.next().toULongLong(), buf, sizeof(buf));
164          label_VolReadTime->setText(QString(buf));
165
166          edit_utime(fld.next().toULongLong(), buf, sizeof(buf));
167          label_VolWriteTime->setText(QString(buf));
168
169          edit_utime(fld.next().toULongLong(), buf, sizeof(buf));
170          label_VolUseDuration->setText(QString(buf));
171
172          label_MaxVolJobs->setText(fld.next());
173          label_MaxVolFiles->setText(fld.next());
174          label_MaxVolBytes->setText(fld.next());
175
176          stat = fld.next();
177          edit_utime(stat.toULongLong(), buf, sizeof(buf));
178          label_VolRetention->setText(QString(buf));
179
180          if (LastWritten != "") {
181             t = str_to_utime(LastWritten.toAscii().data());
182             t = t + stat.toULongLong();
183             ttime = t;
184             localtime_r(&ttime, &tm);
185             strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
186             label_Expire->setText(QString(buf));
187          }
188          label_Online->setPixmap(QPixmap(":/images/inflag"+fld.next()+".png"));
189 //         label_VolFiles->setText(fld.next());
190 //         label_VolErrors->setText(fld.next());
191
192 //         stat=fld.next();
193
194 //         jobstatus_to_ascii_gui(stat[0].toAscii(), buf, sizeof(buf));
195 //         stat = buf;
196 //
197       }
198    }
199
200    query =
201       "SELECT DISTINCT JobId, Name, StartTime, Type, Level, JobFiles,"
202       "JobBytes,JobStatus "
203       "FROM Job JOIN JobMedia USING (JobId) JOIN Media USING (MediaId) "
204       "WHERE Media.VolumeName = '" + m_mediaName + "'";
205
206    if (mainWin->m_sqlDebug) {
207       Pmsg1(000, "MediaInfo query cmd : %s\n",query.toUtf8().data());
208    }
209    results.clear();
210    if (m_console->sql_cmd(query, results)) {
211       QString resultline;
212       QStringList fieldlist;
213       int row = 0;
214       tableJob->setRowCount(results.size());
215       foreach (resultline, results) {
216          fieldlist = resultline.split("\t");
217          QStringListIterator fld(fieldlist);
218          int index=0;
219          TableItemFormatter jobitem(*tableJob, row);
220
221          /* JobId */
222          jobitem.setNumericFld(index++, fld.next());
223
224          /* job name */
225          jobitem.setTextFld(index++, fld.next());
226
227          /* job starttime */
228          jobitem.setTextFld(index++, fld.next(), true);
229
230          /* job type */
231          jobitem.setJobTypeFld(index++, fld.next());
232
233          /* job level */
234          jobitem.setJobLevelFld(index++, fld.next());
235
236          /* job files */
237          jobitem.setNumericFld(index++, fld.next());
238
239          /* job bytes */
240          jobitem.setBytesFld(index++, fld.next());
241
242          /* job status */
243          jobitem.setJobStatusFld(index++, fld.next());
244          row++;
245       }
246    }
247
248    tableJob->resizeColumnsToContents();
249    tableJob->resizeRowsToContents();
250    tableJob->verticalHeader()->hide();
251
252    /* make read only */
253    tableJob->setEditTriggers(QAbstractItemView::NoEditTriggers);
254 }