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