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