summaryrefslogtreecommitdiffstats
path: root/kradio3/plugins/gui-error-log/errorlog.cpp
blob: e593192f61acd129bd399578bfd6e98071f15f7c (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/***************************************************************************
                          errorlog.cpp  -  description
                             -------------------
    begin                : Sa Sep 13 2003
    copyright            : (C) 2003 by Martin Witte
    email                : witte@kawo1.rwth-aachen.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 <tqframe.h>
#include <tqdatetime.h>
#include <tqlayout.h>

#include <klocale.h>
#include <kiconloader.h>
#include <tqtextedit.h>
#include <kfiledialog.h>
#include <kurl.h>
#include <ktempfile.h>
#include <kio/netaccess.h>

#define PAGE_ID_INFO  0
#define PAGE_ID_WARN  1
#define PAGE_ID_ERROR 2
#define PAGE_ID_DEBUG 3

///////////////////////////////////////////////////////////////////////

PLUGIN_LIBRARY_FUNCTIONS(ErrorLog, "kradio-gui-error-log", i18n("Error Logging Window for KRadio"));

/////////////////////////////////////////////////////////////////////////////

ErrorLog::ErrorLog(const TQString &name)
    :  KDialogBase(KDialogBase::IconList,
                   "",
                   KDialogBase::Close|KDialogBase::User1,
                   KDialogBase::Close,
                   NULL,
                   name.ascii(),
                   false,
                   false,
                   KGuiItem(i18n("Save &as"), "filesaveas")
                   ),
       WidgetPluginBase(name, i18n("Error Logger")),
       init_done(false)
{
    TQWidget::setCaption(i18n("KRadio Logger"));
    TQFrame *info = addPage(i18n("Information"), i18n("Information"),
                           KGlobal::instance()->iconLoader()->loadIcon(
                              "messagebox_info", KIcon::NoGroup, KIcon::SizeMedium
                           )
                          );

    TQGridLayout *linfo = new TQGridLayout(info);
    linfo->setSpacing( 5 );
    linfo->setMargin ( 0 );
    m_teInfos = new TQTextEdit(info);
    linfo->addWidget(m_teInfos, 0, 0);
    m_teInfos->setReadOnly(true);
    logInfo(i18n("logging started"));


    TQFrame *warn = addPage(i18n("Warnings"),    i18n("Warnings"),
                           KGlobal::instance()->iconLoader()->loadIcon(
                              "messagebox_warning", KIcon::NoGroup, KIcon::SizeMedium
                           )
                          );
    TQGridLayout *lwarn = new TQGridLayout(warn);
    lwarn->setSpacing( 5 );
    lwarn->setMargin ( 0 );
    m_teWarnings = new TQTextEdit(warn);
    lwarn->addWidget(m_teWarnings, 0, 0);
    m_teWarnings->setReadOnly(true);
    logWarning(i18n("logging started"));



    TQFrame *err  = addPage(i18n("Errors"),      i18n("Errors"),
                           KGlobal::instance()->iconLoader()->loadIcon(
                              "messagebox_critical", KIcon::NoGroup, KIcon::SizeMedium
                           )
                          );
    TQGridLayout *lerr = new TQGridLayout(err);
    lerr->setSpacing( 5 );
    lerr->setMargin ( 0 );
    m_teErrors = new TQTextEdit(err);
    lerr->addWidget(m_teErrors, 0, 0);
    m_teErrors->setReadOnly(true);
    logError(i18n("logging started"));

    TQFrame *debug = addPage(i18n("Debugging"), i18n("Debugging"),
                           KGlobal::instance()->iconLoader()->loadIcon(
                              "find", KIcon::NoGroup, KIcon::SizeMedium
                           )
                          );

    TQGridLayout *ldebug = new TQGridLayout(debug);
    ldebug->setSpacing( 5 );
    ldebug->setMargin ( 0 );
    m_teDebug = new TQTextEdit(debug);
    ldebug->addWidget(m_teDebug, 0, 0);
    m_teDebug->setReadOnly(true);
    logDebug(i18n("logging started"));

    init_done = true;
}


ErrorLog::~ErrorLog()
{
}

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 (KConfig *config)
{
    config->setGroup(TQString("errorlog-") + WidgetPluginBase::name());
    WidgetPluginBase::restoreState(config, false);
}


void ErrorLog::saveState (KConfig *config) const
{
    config->setGroup(TQString("errorlog-") + WidgetPluginBase::name());
    WidgetPluginBase::saveState(config);
}


void ErrorLog::show()
{
    WidgetPluginBase::pShow();
    KDialogBase::show();
}

void ErrorLog::showOnOrgDesktop()
{
    WidgetPluginBase::pShowOnOrgDesktop();
    //KDialogBase::show();
}

void ErrorLog::hide()
{
    logDebug(TQString("%1, ErrorLog::hide: all: %2, desktop: %3, visible:%4, anywherevisible:%5, cachevalid: %6").tqarg(name()).tqarg(m_saveSticky).tqarg(m_saveDesktop).tqarg(isReallyVisible()).tqarg(isAnywhereVisible()).tqarg(m_geoCacheValid));
    WidgetPluginBase::pHide();
    KDialogBase::hide();
}

void    ErrorLog::showEvent(TQShowEvent *e)
{
    KDialogBase::showEvent(e);
    WidgetPluginBase::pShowEvent(e);
}

void    ErrorLog::hideEvent(TQHideEvent *e)
{
    KDialogBase::hideEvent(e);
    WidgetPluginBase::pHideEvent(e);
}

// IErrorLog

bool ErrorLog::logError  (const TQString &s)
{
    m_teErrors->append("<i>" + TQDateTime::currentDateTime().toString(Qt::ISODate) + "</i> " + s + "\n");
    if (init_done) {
        showPage(PAGE_ID_ERROR);
        show();
    }
    return true;
}

bool ErrorLog::logWarning(const TQString &s)
{
    m_teWarnings->append("<i>" + TQDateTime::currentDateTime().toString(Qt::ISODate) + "</i> " + s + "\n");
    return true;
}

bool ErrorLog::logInfo   (const TQString &s)
{
    m_teInfos->append("<i>" + TQDateTime::currentDateTime().toString(Qt::ISODate) + "</i> " + s + "\n");
    return true;
}

bool ErrorLog::logDebug   (const TQString &s)
{
    m_teDebug->append("<i>" + TQDateTime::currentDateTime().toString(Qt::ISODate) + "</i> " + s + "\n");
    return true;
}

// KDialogBase


// store Log Data
void ErrorLog::slotUser1()
{
    KFileDialog fd("",
                   ("*.log|" + i18n("Log Files") + "( *.log )").ascii(),
                   this,
                   i18n("Select Log File").ascii(),
                   true);
    fd.setMode(KFile::File);
    fd.setOperationMode(KFileDialog::Saving);
    fd.setCaption (i18n("Save KRadio Logging Data as ..."));

    if (fd.exec() == TQDialog::Accepted) {
        KURL url = fd.selectedURL();

        KTempFile tmpFile;
        tmpFile.setAutoDelete(true);
        TQFile *outf = tmpFile.file();

        TQTextStream outs(outf);
        outs.setEncoding(TQTextStream::UnicodeUTF8);

        switch (activePageIndex()) {
            case PAGE_ID_INFO:  outs << m_teInfos->text();    break;
            case PAGE_ID_WARN:  outs << m_teWarnings->text(); break;
            case PAGE_ID_ERROR: outs << m_teErrors->text();   break;
            case PAGE_ID_DEBUG: outs << m_teDebug->text();    break;
            default: break;
        }

        if (outf->status() != IO_Ok) {
            logError("ErrorLogger: " +
                     i18n("error writing to tempfile %1").tqarg(tmpFile.name()));
            return;
        }

        // close hopefully flushes buffers ;)
        outf->close();

        if (!KIO::NetAccess::upload(tmpFile.name(), url, this)) {
            logError("ErrorLogger: " +
                     i18n("error uploading preset file %1").tqarg(url.url()));
        }
    }
    setIconListAllVisible(true);
}


#include "errorlog.moc"