]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/job/job.cpp
bat: Go to the media info panel when double-click on job page or media list
[bacula/bacula] / bacula / src / qt-console / job / job.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 "job.h"
31 #include "util/fmtwidgetitem.h"
32 #include "mediainfo/mediainfo.h"
33
34 Job::Job(QString &jobId, QTreeWidgetItem *parentTreeWidgetItem)
35 {
36    setupUi(this);
37    m_closeable = true;
38    pgInitialize(tr("Job"), parentTreeWidgetItem);
39    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
40    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/joblog.png")));
41    m_cursor = new QTextCursor(textJobLog->document());
42
43    m_jobId = jobId;
44    getFont();
45
46    connect(pbRefresh, SIGNAL(clicked()), this, SLOT(populateAll()));
47    connect(pbDelete, SIGNAL(clicked()), this, SLOT(deleteJob()));
48    connect(list_Volume, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(showInfoVolume(QListWidgetItem *)));
49
50    populateAll();
51    dockPage();
52    setCurrent();
53 }
54
55 void Job::showInfoVolume(QListWidgetItem *item)
56 {
57    QString s= item->text();
58    QTreeWidgetItem* pageSelectorTreeWidgetItem = mainWin->getFromHash(this);
59
60    MediaInfo *m = new MediaInfo(pageSelectorTreeWidgetItem, s);
61    connect(m, SIGNAL(destroyed()), this, SLOT(populateTree()));
62 }
63
64 void Job::deleteJob()
65 {
66    if (QMessageBox::warning(this, "Bat",
67       tr("Are you sure you want to delete??  !!!.\n"
68 "This delete command is used to delete a Job record and all associated catalog"
69 " records that were created. This command operates only on the Catalog"
70 " database and has no effect on the actual data written to a Volume. This"
71 " command can be dangerous and we strongly recommend that you do not use"
72 " it unless you know what you are doing.  The Job and all its associated"
73 " records (File and JobMedia) will be deleted from the catalog."
74       "Press OK to proceed with delete operation.?"),
75       QMessageBox::Ok | QMessageBox::Cancel)
76       == QMessageBox::Cancel) { return; }
77
78    QString cmd("delete job jobid=");
79    cmd += m_jobId;
80    consoleCommand(cmd, false);
81    closeStackPage();
82 }
83
84 void Job::getFont()
85 {
86    QFont font = textJobLog->font();
87
88    QString dirname;
89    m_console->getDirResName(dirname);
90    QSettings settings(dirname, "bat");
91    settings.beginGroup("Console");
92    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
93    font.setPointSize(settings.value("consolePointSize", 10).toInt());
94    font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
95    settings.endGroup();
96    textJobLog->setFont(font);
97 }
98
99 void Job::populateAll()
100 {
101    Pmsg0(0, "populateAll()\n");
102    populateText();
103    populateForm();
104    populateVolumes();
105 }
106
107 /*
108  * Populate the text in the window
109  */
110 void Job::populateText()
111 {
112    textJobLog->clear();
113    QString query;
114    query = "SELECT Time, LogText FROM Log WHERE JobId='" + m_jobId + "' order by Time";
115
116    /* This could be a log item */
117    if (mainWin->m_sqlDebug) {
118       Pmsg1(000, "Log query cmd : %s\n", query.toUtf8().data());
119    }
120   
121    QStringList results;
122    if (m_console->sql_cmd(query, results)) {
123
124       if (!results.size()) {
125          QMessageBox::warning(this, tr("Bat"),
126             tr("There were no results!\n"
127                "It is possible you may need to add \"catalog = all\" "
128                "to the Messages resource for this job.\n"), QMessageBox::Ok);
129          return;
130       } 
131
132       QString jobstr("JobId "); /* FIXME: should this be translated ? */
133       jobstr += m_jobId;
134
135       QString htmlbuf("<html><body><pre>");
136   
137       /* Iterate through the lines of results. */
138       QString field;
139       QStringList fieldlist;
140       QString lastTime;
141       QString lastSvc;
142       foreach (QString resultline, results) {
143          fieldlist = resultline.split("\t");
144          
145          if (fieldlist.size() < 2)
146             continue;
147
148          QString curTime = fieldlist[0].trimmed();
149
150          field = fieldlist[1].trimmed();
151          int colon = field.indexOf(":");
152          if (colon > 0) {
153             /* string is like <service> <jobId xxxx>: ..." 
154              * we split at ':' then remove the jobId xxxx string (always the same) */ 
155             QString curSvc(field.left(colon).replace(jobstr,"").trimmed());
156             if (curSvc == lastSvc  && curTime == lastTime) {
157                curTime.clear();
158                curSvc.clear(); 
159             } else {
160                lastTime = curTime;
161                lastSvc = curSvc;
162             }
163 //          htmlbuf += "<td>" + curTime + "</td>";
164             htmlbuf += "\n" + curSvc + " ";
165
166             /* rest of string is marked as pre-formatted (here trimming should
167              * be avoided, to preserve original formatting) */
168             QString msg(field.mid(colon+2));
169             if (msg.startsWith( tr("Error:")) ) { /* FIXME: should really be translated ? */
170                /* error msg, use a specific class */
171                htmlbuf += "</pre><pre class=err>" + msg + "</pre><pre>";
172             } else {
173                htmlbuf += msg ;
174             }
175          } else {
176             /* non standard string, place as-is */
177             if (curTime == lastTime) {
178                curTime.clear();
179             } else {
180                lastTime = curTime;
181             }
182 //          htmlbuf += "<td>" + curTime + "</td>";
183             htmlbuf += "\n" + field ;
184          }
185   
186       } /* foreach resultline */
187
188       htmlbuf += "</pre></body></html>";
189
190       /* full text ready. Here a custom sheet is used to align columns */
191       QString logSheet(".err {color:#FF0000;}");
192       textJobLog->document()->setDefaultStyleSheet(logSheet);
193       textJobLog->document()->setHtml(htmlbuf); 
194       textJobLog->moveCursor(QTextCursor::Start);
195
196    } /* if results from query */
197   
198 }
199
200 // Need to use the fmtwidgetitem helper instead
201 QString convertBytesSI(qint64 qfld)
202 {
203    static const qint64 KB = Q_INT64_C(1000);
204    static const qint64 MB = (KB * KB);
205    static const qint64 GB = (MB * KB);
206    static const qint64 TB = (GB * KB);
207    static const qint64 PB = (TB * KB);
208    static const qint64 EB = (PB * KB);
209
210    /* note: division is integer, so to have some decimals we divide for a
211       smaller unit (e.g. GB for a TB number and so on) */
212    char suffix;
213    if (qfld >= EB) {
214       qfld /= PB; 
215       suffix = 'E';
216    }
217    else if (qfld >= PB) {
218       qfld /= TB; 
219       suffix = 'P';
220    }
221    else if (qfld >= TB) {
222       qfld /= GB; 
223       suffix = 'T';
224    }
225    else if (qfld >= GB) {
226       qfld /= MB;
227       suffix = 'G';
228    }
229    else if (qfld >= MB) {
230       qfld /= KB;
231       suffix = 'M';
232    }
233    else if (qfld >= KB) {
234       suffix = 'k'; /* SI uses lowercase k */
235    }
236    else  {
237       /* plain bytes, no need to reformat */
238       return QString("%1 B").arg(qfld); 
239    }
240
241    /* having divided for a smaller unit, now we can safely convert to double and
242       use the extra room for decimals */
243    return QString("%1 %2B").arg(qfld / 1000.0, 0, 'f', 2).arg(suffix);
244 }
245
246 /*
247  * Populate the text in the window
248  */
249 void Job::populateForm()
250 {
251    QString stat;
252    char buf[256];
253    QString query = 
254       "SELECT JobId, Job.Name, Level, Client.Name, Pool.Name, FileSet, SchedTime, StartTime, EndTime, "
255       "EndTime - StartTime AS Duration, JobBytes, JobFiles, JobErrors, JobStatus, PurgedFiles "
256       "FROM Job JOIN Client USING (ClientId) LEFT JOIN Pool USING (PoolId) "
257       "LEFT JOIN FileSet USING (FileSetId)"
258       "WHERE JobId=" + m_jobId; 
259    QStringList results;
260    if (m_console->sql_cmd(query, results)) {
261       QString resultline;
262       QStringList fieldlist;
263
264       foreach (resultline, results) { // should have only one result
265          fieldlist = resultline.split("\t");
266          QStringListIterator fld(fieldlist);
267          label_JobId->setText(fld.next());
268          label_Name->setText(fld.next());
269          
270          label_Level->setText(job_level_to_str(fld.next()[0].toAscii()));
271
272          label_Client->setText(fld.next());
273          label_Pool->setText(fld.next());
274          label_FileSet->setText(fld.next());
275          label_SchedTime->setText(fld.next());
276          label_StartTime->setText(fld.next());
277          label_EndTime->setText(fld.next());
278          label_Duration->setText(fld.next());
279
280          label_JobBytes->setText(convertBytesSI(fld.next().toULongLong()));
281          label_JobFiles->setText(fld.next());
282          label_JobErrors->setText(fld.next());
283
284          stat=fld.next();
285          label_JobStatus->setPixmap(QPixmap(":/images/" + stat + ".png"));
286          jobstatus_to_ascii_gui(stat[0].toAscii(), buf, sizeof(buf));
287          stat = buf;
288          label_JobStatus->setToolTip(stat);
289
290          chkbox_PurgedFiles->setCheckState(fld.next().toInt()?Qt::Checked:Qt::Unchecked);
291       }
292    }
293 }
294   
295 void Job::populateVolumes()
296 {
297
298    QString query = 
299       "SELECT DISTINCT VolumeName, InChanger, Slot "
300       "FROM Job JOIN JobMedia USING (JobId) JOIN Media USING (MediaId) "
301       "WHERE JobId=" + m_jobId + " ORDER BY VolumeName "; 
302    Pmsg1(000, "Query cmd : %s\n",query.toUtf8().data());
303          
304
305    QStringList results;
306    if (m_console->sql_cmd(query, results)) {
307       QString resultline;
308       QStringList fieldlist;
309       list_Volume->clear();
310       foreach (resultline, results) { // should have only one result
311          fieldlist = resultline.split("\t");
312          QStringListIterator fld(fieldlist);
313 //         QListWidgetItem(QIcon(":/images/inchanger" + fld.next() + ".png"), 
314 //                         fld.next(), list_Volume);
315          list_Volume->addItem(fld.next());
316       }
317    }
318 }
319
320 //QListWidgetItem ( const QIcon & icon, const QString & text, QListWidget * parent = 0, int type = Type )