[e8cdcd]: plugins / gui-error-log / errorlog.cpp Maximize Restore History

Download this file

errorlog.cpp    235 lines (185 with data), 7.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/***************************************************************************
errorlog.cpp - description
-------------------
begin : Sa Sep 13 2003
copyright : (C) 2003 by Martin Witte
email : emw-kradio@nocabal.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "errorlog.h"
#include <QtGui/QFrame>
#include <QtCore/QDateTime>
#include <QtGui/QLayout>
#include <QtCore/QTextCodec>
#include <ktextedit.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kfiledialog.h>
#include <kurl.h>
#include <ktemporaryfile.h>
#include <kio/netaccess.h>
///////////////////////////////////////////////////////////////////////
PLUGIN_LIBRARY_FUNCTIONS(ErrorLog, PROJECT_NAME, i18n("Error Logging Window"));
/////////////////////////////////////////////////////////////////////////////
ErrorLog::ErrorLog(const QString &instanceID, const QString &name)
: KPageDialog(),
WidgetPluginBase(this, instanceID, name, i18n("Error Logger")),
m_debug(NULL),
m_infos(NULL),
m_warnings(NULL),
m_errors(NULL),
init_done(false)
{
setFaceType (KPageDialog::List);
setCaption (i18n("KRadio Logger"));
setObjectName (name);
setModal (false);
setButtons (KDialog::Close | KDialog::User1);
setDefaultButton (KDialog::Close);
showButtonSeparator(true);
setButtonGuiItem(KDialog::User1, KGuiItem(i18n("Save &as"), KIcon("document-save-as")));
setWindowTitle(i18n("KRadio Logger"));
m_teInfos = createTextEditPage(i18n("Information"), KIcon("dialog-information"), m_infos);
logInfo(i18n("KRadio4 Version %1 logging started", QString(KRADIO_VERSION)));
m_teWarnings = createTextEditPage(i18n("Warnings"), KIcon("dialog-warning"), m_warnings);
logWarning(i18n("KRadio4 Version %1 logging started", QString(KRADIO_VERSION)));
m_teErrors = createTextEditPage(i18n("Errors"), KIcon("dialog-error"), m_errors);
logError(i18n("KRadio4 Version %1 logging started", QString(KRADIO_VERSION)));
m_teDebug = createTextEditPage(i18n("Debugging"), KIcon("system-search"), m_debug);
logDebug(i18n("KRadio4 Version %1 logging started", QString(KRADIO_VERSION)));
QObject::connect(this, SIGNAL(user1Clicked()), this, SLOT(slotUser1()));
init_done = true;
}
ErrorLog::~ErrorLog()
{
}
KTextEdit *ErrorLog::createTextEditPage(const QString &title, const KIcon &icon, KPageWidgetItem *&item)
{
QFrame *frame = new QFrame(this);
QGridLayout *linfo = new QGridLayout(frame);
KTextEdit *edit = new KTextEdit(frame);
linfo->setSpacing( 5 );
linfo->setMargin ( 0 );
linfo->addWidget(edit, 0, 0);
edit->setReadOnly(true);
item = addPage(frame, title);
item->setHeader(title);
item->setIcon(icon);
return edit;
}
bool ErrorLog::connectI (Interface *i)
{
bool a = IErrorLog::connectI(i);
bool b = PluginBase::connectI(i);
return a || b;
}
bool ErrorLog::disconnectI (Interface *i)
{
bool a = IErrorLog::disconnectI(i);
bool b = PluginBase::disconnectI(i);
return a || b;
}
void ErrorLog::restoreState (const KConfigGroup &config)
{
WidgetPluginBase::restoreState(config, false);
}
void ErrorLog::saveState (KConfigGroup &config) const
{
WidgetPluginBase::saveState(config);
}
void ErrorLog::setVisible(bool v)
{
pSetVisible(v);
KPageDialog::setVisible(v);
}
void ErrorLog::showEvent(QShowEvent *e)
{
KPageDialog::showEvent(e);
WidgetPluginBase::pShowEvent(e);
}
void ErrorLog::hideEvent(QHideEvent *e)
{
KPageDialog::hideEvent(e);
WidgetPluginBase::pHideEvent(e);
}
// IErrorLog
bool ErrorLog::logError (const QString &s)
{
QMutexLocker lock(&m_sequentializer);
m_teErrors->append("<i>" + QDateTime::currentDateTime().toString(Qt::ISODate) + "</i> " + s + "\n");
if (init_done) {
setCurrentPage(m_errors);
show();
}
return true;
}
bool ErrorLog::logWarning(const QString &s)
{
QMutexLocker lock(&m_sequentializer);
m_teWarnings->append("<i>" + QDateTime::currentDateTime().toString(Qt::ISODate) + "</i> " + s + "\n");
return true;
}
bool ErrorLog::logInfo (const QString &s)
{
QMutexLocker lock(&m_sequentializer);
m_teInfos->append("<i>" + QDateTime::currentDateTime().toString(Qt::ISODate) + "</i> " + s + "\n");
return true;
}
bool ErrorLog::logDebug (const QString &s)
{
QMutexLocker lock(&m_sequentializer);
m_teDebug->append("<i>" + QDateTime::currentDateTime().toString(Qt::ISODate) + "</i> " + s + "\n");
return true;
}
// KDialogBase
// store Log Data
void ErrorLog::slotUser1()
{
KFileDialog fd(KUrl(),
("*.log|" + i18n("Log Files") + "( *.log )"),
this);
fd.setModal(true);
fd.setMode(KFile::File);
fd.setOperationMode(KFileDialog::Saving);
fd.setWindowTitle (i18n("Save KRadio Logging Data"));
if (fd.exec() == QDialog::Accepted) {
KUrl url = fd.selectedUrl();
KTemporaryFile tmpFile;
tmpFile.setAutoRemove(true);
if (tmpFile.open()) {
QString filename = tmpFile.fileName();
QTextStream outs(&tmpFile);
outs.setCodec(QTextCodec::QTextCodec::codecForName("UTF-8"));
if (currentPage() == m_errors) {
outs << m_teErrors->toPlainText();
} else if (currentPage() == m_warnings) {
outs << m_teWarnings->toPlainText();
} else if (currentPage() == m_infos) {
outs << m_teInfos->toPlainText();
} else if (currentPage() == m_debug) {
outs << m_teDebug->toPlainText();
}
if (tmpFile.error()) {
logError("ErrorLogger: " +
i18n("error writing to temporary file %1", filename));
return;
}
// close hopefully flushes buffers ;)
tmpFile.close();
if (!KIO::NetAccess::upload(filename, url, this)) {
logError("ErrorLogger: " +
i18n("error uploading preset file %1", url.pathOrUrl()));
}
}
}
// FIXME: no idea how to port
//setIconListAllVisible(true);
}
#include "errorlog.moc"