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