]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/joblog/joblog.cpp
Backport from BEE
[bacula/bacula] / bacula / src / qt-console / joblog / joblog.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 many
7    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    Bacula® is a registered trademark of Kern Sibbald.
15 */
16
17 /*
18  *   Version $Id$
19  *
20  *  JobLog Class
21  *
22  *   Dirk Bartley, March 2007
23  *
24  */
25
26 #include "bat.h"
27 #include "joblog.h"
28
29 JobLog::JobLog(QString &jobId, QTreeWidgetItem *parentTreeWidgetItem) : Pages()
30 {
31    setupUi(this);
32    pgInitialize(tr("JobLog"), parentTreeWidgetItem);
33    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
34    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/joblog.png")));
35    m_cursor = new QTextCursor(textEdit->document());
36
37    m_jobId = jobId;
38    getFont();
39    populateText();
40
41    dockPage();
42    setCurrent();
43 }
44
45 void JobLog::getFont()
46 {
47    QFont font = textEdit->font();
48
49    QString dirname;
50    m_console->getDirResName(dirname);
51    QSettings settings(dirname, "bat");
52    settings.beginGroup("Console");
53    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
54    font.setPointSize(settings.value("consolePointSize", 10).toInt());
55    font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
56    settings.endGroup();
57    textEdit->setFont(font);
58 }
59
60 /*
61  * Populate the text in the window
62  */
63 void JobLog::populateText()
64 {
65    QString query;
66    query = "SELECT Time, LogText FROM Log WHERE JobId='" + m_jobId + "' order by Time";
67
68    /* This could be a log item */
69    if (mainWin->m_sqlDebug) {
70       Pmsg1(000, "Log query cmd : %s\n", query.toUtf8().data());
71    }
72
73    QStringList results;
74    if (m_console->sql_cmd(query, results)) {
75
76       if (!results.size()) {
77          QMessageBox::warning(this, tr("Bat"),
78             tr("There were no results!\n"
79                "It is possible you may need to add \"catalog = all\" "
80                "to the Messages resource for this job.\n"), QMessageBox::Ok);
81          return;
82       }
83
84       QString jobstr("JobId "); /* FIXME: should this be translated ? */
85       jobstr += m_jobId;
86
87       QString htmlbuf("<html><body><b>" + tr("Log records for job %1").arg(m_jobId) );
88       htmlbuf += "</b><table>";
89
90       /* Iterate through the lines of results. */
91       QString field;
92       QStringList fieldlist;
93       QString lastTime;
94       QString lastSvc;
95       foreach (QString resultline, results) {
96          fieldlist = resultline.split("\t");
97         
98          if (fieldlist.size() < 2)
99             continue;
100
101          htmlbuf +="<tr>";
102
103          QString curTime = fieldlist[0].trimmed();
104
105          field = fieldlist[1].trimmed();
106          int colon = field.indexOf(":");
107          if (colon > 0) {
108             /* string is like <service> <jobId xxxx>: ..."
109              * we split at ':' then remove the jobId xxxx string (always the same) */
110             QString curSvc(field.left(colon).replace(jobstr,"").trimmed());
111             if (curSvc == lastSvc  && curTime == lastTime) {
112                curTime.clear();
113                curSvc.clear();
114             } else {
115                lastTime = curTime;
116                lastSvc = curSvc;
117             }
118             htmlbuf += "<td>" + curTime + "</td>";
119             htmlbuf += "<td><p>" + curSvc + "</p></td>";
120
121             /* rest of string is marked as pre-formatted (here trimming should
122              * be avoided, to preserve original formatting) */
123             QString msg(field.mid(colon+2));
124             if (msg.startsWith( tr("Error:")) ) { /* FIXME: should really be translated ? */
125                /* error msg, use a specific class */
126                htmlbuf += "<td><pre class=err>" + msg + "</pre></td>";
127             } else {
128                htmlbuf += "<td><pre>" + msg + "</pre></td>";
129             }
130          } else {
131             /* non standard string, place as-is */
132             if (curTime == lastTime) {
133                curTime.clear();
134             } else {
135                lastTime = curTime;
136             }
137             htmlbuf += "<td>" + curTime + "</td>";
138             htmlbuf += "<td><pre>" + field + "</pre></td>";
139          }
140
141          htmlbuf += "</tr>";
142
143       } /* foreach resultline */
144
145       htmlbuf += "</table></body></html>";
146
147       /* full text ready. Here a custom sheet is used to align columns */
148       QString logSheet("p,pre,.err {margin-left: 10px} .err {color:#FF0000;}");
149       textEdit->document()->setDefaultStyleSheet(logSheet);
150       textEdit->document()->setHtml(htmlbuf);
151       textEdit->moveCursor(QTextCursor::Start);
152
153    } /* if results from query */
154
155 }
156