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