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