]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/job/job.cpp
Merge branch 'master' into basejobv3
[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 /*
201  * Populate the text in the window
202  */
203 void Job::populateForm()
204 {
205    QString stat;
206    char buf[256];
207    QString query = 
208       "SELECT JobId, Job.Name, Level, Client.Name, Pool.Name, FileSet, SchedTime, StartTime, EndTime, "
209       "EndTime - StartTime AS Duration, JobBytes, JobFiles, JobErrors, JobStatus, PurgedFiles "
210       "FROM Job JOIN Client USING (ClientId) LEFT JOIN Pool USING (PoolId) "
211       "LEFT JOIN FileSet USING (FileSetId)"
212       "WHERE JobId=" + m_jobId; 
213    QStringList results;
214    if (m_console->sql_cmd(query, results)) {
215       QString resultline;
216       QStringList fieldlist;
217
218       foreach (resultline, results) { // should have only one result
219          fieldlist = resultline.split("\t");
220          QStringListIterator fld(fieldlist);
221          label_JobId->setText(fld.next());
222          label_Name->setText(fld.next());
223          
224          label_Level->setText(job_level_to_str(fld.next()[0].toAscii()));
225
226          label_Client->setText(fld.next());
227          label_Pool->setText(fld.next());
228          label_FileSet->setText(fld.next());
229          label_SchedTime->setText(fld.next());
230          label_StartTime->setText(fld.next());
231          label_EndTime->setText(fld.next());
232          label_Duration->setText(fld.next());
233
234          label_JobBytes->setText(convertBytesSI(fld.next().toULongLong()));
235          label_JobFiles->setText(fld.next());
236          label_JobErrors->setText(fld.next());
237
238          stat=fld.next();
239          label_JobStatus->setPixmap(QPixmap(":/images/" + stat + ".png"));
240          jobstatus_to_ascii_gui(stat[0].toAscii(), buf, sizeof(buf));
241          stat = buf;
242          label_JobStatus->setToolTip(stat);
243
244          chkbox_PurgedFiles->setCheckState(fld.next().toInt()?Qt::Checked:Qt::Unchecked);
245       }
246    }
247 }
248   
249 void Job::populateVolumes()
250 {
251
252    QString query = 
253       "SELECT DISTINCT VolumeName, InChanger, Slot "
254       "FROM Job JOIN JobMedia USING (JobId) JOIN Media USING (MediaId) "
255       "WHERE JobId=" + m_jobId + " ORDER BY VolumeName "; 
256    Pmsg1(000, "Query cmd : %s\n",query.toUtf8().data());
257          
258
259    QStringList results;
260    if (m_console->sql_cmd(query, results)) {
261       QString resultline;
262       QStringList fieldlist;
263       list_Volume->clear();
264       foreach (resultline, results) { // should have only one result
265          fieldlist = resultline.split("\t");
266          QStringListIterator fld(fieldlist);
267 //         QListWidgetItem(QIcon(":/images/inchanger" + fld.next() + ".png"), 
268 //                         fld.next(), list_Volume);
269          list_Volume->addItem(fld.next());
270       }
271    }
272 }
273
274 //QListWidgetItem ( const QIcon & icon, const QString & text, QListWidget * parent = 0, int type = Type )