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