summaryrefslogtreecommitdiffstats
path: root/kget/logwindow.cpp
blob: ecf3598682ff4ca58f353ed550036ba342189e70 (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
/***************************************************************************
*                                logwindow.cpp
*                             -------------------
*
*    Revision     : $Id$
*    begin        : Tue Jan 29 2002
*    copyright    : (C) 2002 by Patrick Charbonnier
*                 : Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
*    email        : pch@freeshell.org
*
****************************************************************************/

/**************************************************************************
 *
 *   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.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 ***************************************************************************/


#include <tqlayout.h>
#include <tqlistview.h>

#include <klocale.h>
#include <kdialog.h>
#include <tdeaction.h>

#include "transfer.h"
#include "tdemainwidget.h"
#include "logwindow.h"

#include <tdeapplication.h>
#include <tqtextedit.h>

// // Replace regular space with nbsp
// TQString replaceSpaces(const TQString &str) {
//   TQString res = str;
//   res.simplifyWhiteSpace();

//   int pos;
//   while ( (pos = res.find(' ')) != -1) {
//     res.replace(pos, 1, new TQChar( 0x00a0 ), 1);
//   }

//   return res;
// }


static TQString removeHTML(const TQString & str)
{
    TQString res = str;
    int pos;

    // replace <br/> with a newline
    while ((pos = res.find("<br/>")) != -1) {
        res.replace(pos, 4, "\n");
    }

    // remove all tags
    while ((pos = res.find('<')) != -1) {
        int pos2 = res.find('>', pos);

        if (pos2 == -1) {
            pos2 = res.length() + 1;
        }
        res.remove(pos, pos2 - pos + 1);
    }

    return res;
}


SeparatedLog::SeparatedLog(TQWidget * parent):TQWidget(parent)
{
    idSelected = 0;

    TQGridLayout *topGridLayout = new TQGridLayout(this, 1, 2, 0, KDialog::spacingHint());

    topGridLayout->setRowStretch(0, 5);

    topGridLayout->setColStretch(0, 3);
    topGridLayout->setColStretch(1, 10);

    lv_log = new TQListView(this);
    lv_log->setMultiSelection(false);
    lv_log->setAllColumnsShowFocus(true);
    lv_log->setSorting(-1);

    lv_log->addColumn(i18n("Id"), 40);
    lv_log->addColumn(i18n("Name"), 100);

    topGridLayout->addWidget(lv_log, 0, 0);

    connect(lv_log, TQT_SIGNAL(selectionChanged(TQListViewItem *)), TQT_SLOT(transferSelected(TQListViewItem *)));

    ml_log = new TQTextEdit(this);
    ml_log->setTextFormat(LogText);
    ml_log->setMinimumSize(300, 200);
    ml_log->setVScrollBarMode(TQScrollView::Auto);
    ml_log->setWordWrap(TQTextEdit::NoWrap);

    topGridLayout->addWidget(ml_log, 0, 1);
}


void SeparatedLog::addLog(uint id, const TQString & filename, const TQString & message)
{
    if (!trMap.contains(id)) {
        trMap.insert(id, message);
        TQListViewItem *last=lv_log->lastItem();
        new TQListViewItem(lv_log, last);
        last=lv_log->lastItem();
        last->setText(0, TQString::number(id));
        last->setText(1, filename);
        // if I don't do this, ID#10 gets sorted between ID#1 and ID#2, ugly.      
    } else {
        trMap[id] += ("\n"+message);
    }

    if (idSelected == id) {
        refresh();
    }
}


void SeparatedLog::transferSelected(TQListViewItem * item)
{
    if (item) {
        idSelected = item->text(0).toUInt();
        // kapp->lock();
        ml_log->setText(trMap[idSelected]);
        // kapp->unlock();
    }
}


void SeparatedLog::refresh()
{
    if (idSelected > 0) {
        // kapp->lock();
        ml_log->setText(trMap[idSelected]);
        // kapp->unlock();
    }
}


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




LogWindow::LogWindow():KDialogBase(Tabbed, i18n("Log Window"), Close, Close, 0, "", false)
{

    // add pages
    TQFrame *page = addPage(i18n("Mixed"));
    TQVBoxLayout *topLayout = new TQVBoxLayout(page, 0, spacingHint());

    mixed_log = new TQTextEdit(page);
    mixed_log->setTextFormat(LogText);
    mixed_log->setVScrollBarMode(TQScrollView::Auto);
    mixed_log->setWordWrap(TQTextEdit::NoWrap);
    topLayout->addWidget(mixed_log);

    page = addPage(i18n("Separated"));
    topLayout = new TQVBoxLayout(page, 0, spacingHint());
    sep_log = new SeparatedLog(page);
    topLayout->addWidget(sep_log);

    connect(this, TQT_SIGNAL(closeClicked()), this, TQT_SLOT(close()));

    // resize( 500, 300 );
}


void LogWindow::closeEvent(TQCloseEvent *e)
{
    tdemain->m_paShowLog->setChecked(false);
    tdemain->b_viewLogWindow = false;
    KDialogBase::closeEvent( e );
}


void LogWindow::logGeneral(const TQString & message)
{
    TQString tmps = "<font color=\"blue\">" + TQTime::currentTime().toString() + "</font> : <b>" + message + "</b>";

    mixed_log->append(tmps);
}


void LogWindow::logTransfer(uint id, const TQString & filename, const TQString & message)
{
    TQString mixed_msg, single_msg, job_id;

    job_id = TQString("Job[<font color=\"red\">%1</font>] : ").arg(id);
    mixed_msg = "<font color=\"blue\">" + TQTime::currentTime().toString() + "</font> : " + job_id + message;

    single_msg = "<font color=\"blue\">" + TQTime::currentTime().toString() + "</font> : " + message;

    mixed_log->append(mixed_msg);
    sep_log->addLog(id, filename, single_msg);
}


TQString LogWindow::getText() const
{
    return removeHTML(mixed_log->text());
}

#include "logwindow.moc"