~ubuntu-branches/ubuntu/feisty/psi/feisty

« back to all changes in this revision

Viewing changes to src/historydlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2002-04-19 02:28:44 UTC
  • Revision ID: james.westby@ubuntu.com-20020419022844-za7xgai5qyfd9xv6
Tags: upstream-0.8.5
ImportĀ upstreamĀ versionĀ 0.8.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
** historydlg.cpp - a dialog to show message history
 
3
** Copyright (C) 2001, 2002  Justin Karneges
 
4
**
 
5
** This program is free software; you can redistribute it and/or
 
6
** modify it under the terms of the GNU General Public License
 
7
** as published by the Free Software Foundation; either version 2
 
8
** of the License, or (at your option) any later version.
 
9
**
 
10
** This program is distributed in the hope that it will be useful,
 
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
** GNU General Public License for more details.
 
14
**
 
15
** You should have received a copy of the GNU General Public License
 
16
** along with this program; if not, write to the Free Software
 
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
 
18
**
 
19
****************************************************************************/
 
20
 
 
21
#include<qlayout.h>
 
22
#include<qlabel.h>
 
23
#include<qlineedit.h>
 
24
#include<qpushbutton.h>
 
25
#include<qheader.h>
 
26
#include<qpopupmenu.h>
 
27
 
 
28
#include<qapplication.h>
 
29
#include<qclipboard.h>
 
30
 
 
31
#include"historydlg.h"
 
32
 
 
33
 
 
34
/****************************************************************************
 
35
  HistoryDlg - a dialog window for history
 
36
****************************************************************************/
 
37
HistoryDlg::HistoryDlg(UserListItem *item, QWidget *parent, const char *name)
 
38
:QWidget(parent, name), UniqueWindow(TRUE, "HistoryDlg", this, cleanJid(item->jid))
 
39
{
 
40
        v_jid = item->jid;
 
41
 
 
42
        setWFlags(getWFlags() | WDestructiveClose);
 
43
 
 
44
        setCaption(item->jid);
 
45
        setIcon(*pix_history);
 
46
 
 
47
        QVBoxLayout *vb1 = new QVBoxLayout(this, 8);
 
48
        lv = new HistoryView(this);
 
49
        connect(lv, SIGNAL(aOpenMessage(const Message &)), SLOT(actionOpenMessage(const Message &)));
 
50
        vb1->addWidget(lv);
 
51
 
 
52
        QHBoxLayout *hb1 = new QHBoxLayout(vb1);
 
53
 
 
54
        QPushButton *pb = new QPushButton(tr("&Refresh"), this);
 
55
        pb->setMinimumWidth(80);
 
56
        connect(pb, SIGNAL(clicked()), SLOT(doRefresh()));
 
57
        hb1->addWidget(pb);
 
58
 
 
59
        QLabel *lb = new QLabel(tr("Search:"), this);
 
60
        hb1->addWidget(lb);
 
61
        le_search = new QLineEdit(this);
 
62
        connect(le_search, SIGNAL(textChanged(const QString &)), SLOT(searchTextChanged(const QString &)));
 
63
        connect(le_search, SIGNAL(returnPressed()), SLOT(doSearch()));
 
64
        hb1->addWidget(le_search);
 
65
        pb_go = new QPushButton(tr("&Go"), this);
 
66
        //pb->setMinimumWidth(80);
 
67
        pb_go->setEnabled(FALSE);
 
68
        connect(pb_go, SIGNAL(clicked()), SLOT(doSearch()));
 
69
        hb1->addWidget(pb_go);
 
70
 
 
71
        QComboBox *cb = new QComboBox(this);
 
72
        cb->insertItem(tr("Show Last 50"));
 
73
        cb->insertItem(tr("Show Last 100"));
 
74
        cb->insertItem(tr("Show Last 500"));
 
75
        cb->insertItem(tr("Show all"));
 
76
        connect(cb, SIGNAL(activated(int)), SLOT(comboChanged(int)));
 
77
        hb1->addWidget(cb);
 
78
 
 
79
        hb1->addStretch(1);
 
80
 
 
81
        pb = new QPushButton(tr("&Close"), this);
 
82
        pb->setMinimumWidth(80);
 
83
        connect(pb, SIGNAL(clicked()), SLOT(close()));
 
84
        hb1->addWidget(pb);
 
85
 
 
86
 
 
87
        lv->setAllColumnsShowFocus(TRUE);
 
88
        lv->addColumn(tr("Type"));
 
89
        lv->addColumn(tr("Origin"));
 
90
        lv->addColumn(tr("Date"));
 
91
        lv->addColumn(tr("Text"));
 
92
        lv->setSorting(2);
 
93
        lv->setResizeMode(QListView::LastColumn);
 
94
        lv->setShowToolTips(FALSE);
 
95
        lv->header()->setClickEnabled(FALSE);
 
96
        lv->header()->setMovingEnabled(FALSE);
 
97
        lv->header()->setResizeEnabled(FALSE);
 
98
 
 
99
        comboChanged(0);
 
100
 
 
101
        resize(520,280);
 
102
}
 
103
 
 
104
void HistoryDlg::keyPressEvent(QKeyEvent *e)
 
105
{
 
106
        if(e->key() == Key_Escape)
 
107
                close();
 
108
        else {
 
109
                e->ignore();
 
110
        }
 
111
}
 
112
 
 
113
void HistoryDlg::show()
 
114
{
 
115
        QWidget::show();
 
116
 
 
117
        lv->doResize();
 
118
}
 
119
 
 
120
void HistoryDlg::doRefresh()
 
121
{
 
122
        le_search->setText("");
 
123
 
 
124
        refresh();
 
125
}
 
126
 
 
127
void HistoryDlg::refresh()
 
128
{
 
129
        lv->clear();
 
130
        MessageHistory log(v_jid, HISTORY_READ);
 
131
        Message *msg;
 
132
 
 
133
        bool useSearch = !searchStr.isEmpty();
 
134
 
 
135
        for(int n = 0; n != amountToShow; ++n) {
 
136
                msg = log.readEntry();
 
137
                if(!msg)
 
138
                        break;
 
139
                if(useSearch) {
 
140
                        if(msg->text.find(searchStr, 0, FALSE) == -1)
 
141
                                continue;
 
142
                }
 
143
                msg->from = v_jid;
 
144
                lv->addMessage(msg);
 
145
        }
 
146
 
 
147
        if(lv->firstChild())
 
148
                lv->setSelected(lv->firstChild(), TRUE);
 
149
}
 
150
 
 
151
void HistoryDlg::comboChanged(int x)
 
152
{
 
153
        if(x == 0)
 
154
                amountToShow = 50;
 
155
        else if(x == 1)
 
156
                amountToShow = 100;
 
157
        else if(x == 2)
 
158
                amountToShow = 500;
 
159
        else
 
160
                amountToShow = -1;
 
161
 
 
162
        refresh();
 
163
}
 
164
 
 
165
void HistoryDlg::searchTextChanged(const QString &str)
 
166
{
 
167
        searchStr = str;
 
168
        pb_go->setEnabled(!searchStr.isEmpty());
 
169
}
 
170
 
 
171
void HistoryDlg::doSearch()
 
172
{
 
173
        if(searchStr.isEmpty())
 
174
                return;
 
175
 
 
176
        refresh();
 
177
}
 
178
 
 
179
void HistoryDlg::actionOpenMessage(const Message &msg)
 
180
{
 
181
        openMessage(msg);
 
182
}
 
183
 
 
184
/*static*/ HistoryDlg * HistoryDlg::find(const QString &xjid)
 
185
{
 
186
        return (HistoryDlg *)UniqueWindowBank::find("HistoryDlg", cleanJid(xjid));
 
187
}
 
188
 
 
189
 
 
190
/****************************************************************************
 
191
  HistoryView - a list view for history items
 
192
****************************************************************************/
 
193
HistoryView::HistoryView(QWidget *parent, const char *name)
 
194
:QListView(parent, name)
 
195
{
 
196
        at_id = 0;
 
197
        connect(this, SIGNAL(doubleClicked(QListViewItem *)), SLOT(qlv_doubleclick(QListViewItem *)));
 
198
        connect(this, SIGNAL(rightButtonPressed(QListViewItem *, const QPoint &, int)), SLOT(qlv_contextPopup(QListViewItem *, const QPoint &, int)));
 
199
}
 
200
 
 
201
void HistoryView::resizeEvent(QResizeEvent *e)
 
202
{
 
203
        QListView::resizeEvent(e);
 
204
 
 
205
        if(e->oldSize().width() != e->size().width())
 
206
                doResize();
 
207
}
 
208
 
 
209
void HistoryView::keyPressEvent(QKeyEvent *e)
 
210
{
 
211
        if(e->key() == Key_Enter || e->key() == Key_Return)
 
212
                doOpenMessage();
 
213
        else
 
214
                QListView::keyPressEvent(e);
 
215
}
 
216
 
 
217
void HistoryView::doResize()
 
218
{
 
219
        QListViewItemIterator it(this);
 
220
        HistoryViewItem *item;
 
221
        for(; it.current() ; ++it) {
 
222
                item = (HistoryViewItem *)it.current();
 
223
                item->setup();
 
224
        }
 
225
}
 
226
 
 
227
void HistoryView::addMessage(Message *msg)
 
228
{
 
229
        HistoryViewItem *lvi;
 
230
        lvi = new HistoryViewItem(msg, at_id++, this);
 
231
}
 
232
 
 
233
void HistoryView::doOpenMessage()
 
234
{
 
235
        HistoryViewItem *i = (HistoryViewItem *)selectedItem();
 
236
        if(!i)
 
237
                return;
 
238
        aOpenMessage(*i->msg);
 
239
}
 
240
 
 
241
void HistoryView::qlv_doubleclick(QListViewItem *xi)
 
242
{
 
243
        HistoryViewItem *i = (HistoryViewItem *)xi;
 
244
 
 
245
        setSelected(i, TRUE);
 
246
        doOpenMessage();
 
247
}
 
248
 
 
249
void HistoryView::qlv_contextPopup(QListViewItem *ix, const QPoint &pos, int)
 
250
{
 
251
        HistoryViewItem *i = (HistoryViewItem *)ix;
 
252
        if(!i)
 
253
                return;
 
254
 
 
255
        QPopupMenu popup;
 
256
        popup.insertItem(tr("Open"), 1);
 
257
        popup.insertSeparator();
 
258
        popup.insertItem(tr("Copy"), 2);
 
259
 
 
260
        int x = popup.exec(pos);
 
261
 
 
262
        if(x == 1)
 
263
                doOpenMessage();
 
264
        else if(x == 2) {
 
265
                HistoryViewItem *i = (HistoryViewItem *)selectedItem();
 
266
                if(!i)
 
267
                        return;
 
268
                QApplication::clipboard()->setText(i->msg->text);
 
269
        }
 
270
}
 
271
 
 
272
 
 
273
/****************************************************************************
 
274
  HistoryViewItem - a history item
 
275
****************************************************************************/
 
276
HistoryViewItem::HistoryViewItem(Message *message, int xid, QListView *parent)
 
277
:QListViewItem(parent)
 
278
{
 
279
        rt = 0;
 
280
        id = xid;
 
281
        msg = message;
 
282
 
 
283
        if(msg->type == MESSAGE_NORM) {
 
284
                text = plain2rich(msg->text);
 
285
 
 
286
                if(!msg->url.isEmpty())
 
287
                        setPixmap(0, *pix_url);
 
288
                else if(msg->originLocal)
 
289
                        setPixmap(0, *pix_send);
 
290
                else
 
291
                        setPixmap(0, anim_message->base());
 
292
        }
 
293
        else if(msg->type == MESSAGE_CHAT) {
 
294
                text = plain2rich(msg->text);
 
295
                setPixmap(0, anim_chat->base());
 
296
        }
 
297
        else if(msg->type == MESSAGE_HEADLINE) {
 
298
                text = plain2rich(msg->text);
 
299
                setPixmap(0, anim_headline->base());
 
300
        }
 
301
        else {
 
302
                text = msg->text;
 
303
                setPixmap(0, anim_system->base());
 
304
        }
 
305
 
 
306
        if(msg->originLocal)
 
307
                setText(1, QObject::tr("To"));
 
308
        else
 
309
                setText(1, QObject::tr("From"));
 
310
 
 
311
        QString date;
 
312
        date.sprintf("%02d/%02d/%02d %02d:%02d:%02d",
 
313
          msg->timeStamp.date().month(),
 
314
          msg->timeStamp.date().day(),
 
315
          msg->timeStamp.date().year(),
 
316
          msg->timeStamp.time().hour(),
 
317
          msg->timeStamp.time().minute(),
 
318
          msg->timeStamp.time().second());
 
319
 
 
320
        setText(2, date);
 
321
 
 
322
        rt = new QSimpleRichText(text, listView()->font());
 
323
}
 
324
 
 
325
HistoryViewItem::~HistoryViewItem()
 
326
{
 
327
        if(rt) {
 
328
                delete rt;
 
329
                rt = 0;
 
330
        }
 
331
        delete msg;
 
332
}
 
333
 
 
334
// reimplemented from QListViewItem.  setup() and paintCell() are tricky stuff
 
335
void HistoryViewItem::setup()
 
336
{
 
337
        widthChanged();
 
338
 
 
339
        QListView *lv = listView();
 
340
 
 
341
        if(rt) {
 
342
                int w = lv->columnWidth(3);
 
343
                rt->setWidth(w);
 
344
        }
 
345
 
 
346
        int y;
 
347
        //y = lv->fontMetrics().size(AlignVCenter, displayStr).height();
 
348
        if(!rt)
 
349
                y = 22;
 
350
        else
 
351
                y = rt->height();
 
352
 
 
353
        y += lv->itemMargin() * 2;
 
354
 
 
355
        // ensure an even number
 
356
        if(y & 1)
 
357
                ++y;
 
358
 
 
359
        setHeight(y);
 
360
}
 
361
 
 
362
void HistoryViewItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int alignment)
 
363
{
 
364
        QColorGroup mycg = cg;
 
365
        if(msg->originLocal)
 
366
                mycg.setColor(QColorGroup::Text, Qt::red);
 
367
        else
 
368
                mycg.setColor(QColorGroup::Text, Qt::blue);
 
369
 
 
370
        if(column == 3) {
 
371
                QBrush br;
 
372
                if(isSelected()) {
 
373
                        mycg.setColor(QColorGroup::Text, Qt::white);
 
374
 
 
375
                        br = cg.brush(QColorGroup::Highlight);
 
376
                }
 
377
                else {
 
378
                        br = cg.brush(QColorGroup::Base);
 
379
                }
 
380
 
 
381
                if(rt) {
 
382
                        QSimpleRichText tmp(QString("<qt><font color=\"%1\">" + text + "</font></qt>").arg(mycg.text().name()), listView()->font());
 
383
                        tmp.setWidth(rt->width());
 
384
                        tmp.draw(p, 0, 0, QRegion(0, 0, width/* - (a * 2)*/, height()), mycg, &br);
 
385
                }
 
386
        }
 
387
        else {
 
388
                alignment = AlignTop;
 
389
 
 
390
                QListViewItem::paintCell(p, mycg, column, width, alignment);
 
391
        }
 
392
}
 
393
 
 
394
int HistoryViewItem::compare(QListViewItem *xi, int, bool) const
 
395
{
 
396
        HistoryViewItem *i = (HistoryViewItem *)xi;
 
397
 
 
398
        return id - i->id;
 
399
}