1 | /*
|
---|
2 | * historydlg.cpp - a dialog to show event 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 library; 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"historydlg.h"
|
---|
22 |
|
---|
23 | #include<qpopupmenu.h>
|
---|
24 | #include<qheader.h>
|
---|
25 | #include<qlayout.h>
|
---|
26 | #include<qlabel.h>
|
---|
27 | #include<qlineedit.h>
|
---|
28 | #include<qpushbutton.h>
|
---|
29 | #include<qfiledialog.h>
|
---|
30 | #include<qmessagebox.h>
|
---|
31 | #include<qframe.h>
|
---|
32 | #include<qapplication.h>
|
---|
33 | #include<qclipboard.h>
|
---|
34 | #include"psiaccount.h"
|
---|
35 | #include"psievent.h"
|
---|
36 | #include"common.h"
|
---|
37 | #include"busywidget.h"
|
---|
38 | #include"eventdb.h"
|
---|
39 | #include"userlist.h"
|
---|
40 |
|
---|
41 | static QString getNext(QString *str)
|
---|
42 | {
|
---|
43 | // are we in space?
|
---|
44 | int n = 0;
|
---|
45 | if(str->at(n).isSpace()) {
|
---|
46 | // get out of it
|
---|
47 | while(n < (int)str->length() && str->at(n).isSpace())
|
---|
48 | ++n;
|
---|
49 | if(n == (int)str->length())
|
---|
50 | return QString::null;
|
---|
51 | }
|
---|
52 | // find end or next space
|
---|
53 | while(n < (int)str->length() && !str->at(n).isSpace())
|
---|
54 | ++n;
|
---|
55 | QString result = str->mid(0, n);
|
---|
56 | *str = str->mid(n);
|
---|
57 | return result;
|
---|
58 | }
|
---|
59 |
|
---|
60 | // wraps a string against a fixed width
|
---|
61 | static QStringList wrapString(const QString &str, int wid)
|
---|
62 | {
|
---|
63 | QStringList lines;
|
---|
64 | QString cur;
|
---|
65 | QString tmp = str;
|
---|
66 | //printf("parsing: [%s]\n", tmp.latin1());
|
---|
67 | while(1) {
|
---|
68 | QString word = getNext(&tmp);
|
---|
69 | if(word == QString::null) {
|
---|
70 | lines += cur;
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | //printf("word:[%s]\n", word.latin1());
|
---|
74 | if(!cur.isEmpty()) {
|
---|
75 | if((int)cur.length() + (int)word.length() > wid) {
|
---|
76 | lines += cur;
|
---|
77 | cur = "";
|
---|
78 | }
|
---|
79 | }
|
---|
80 | if(cur.isEmpty()) {
|
---|
81 | // trim the whitespace in front
|
---|
82 | for(int n = 0; n < (int)word.length(); ++n) {
|
---|
83 | if(!word.at(n).isSpace()) {
|
---|
84 | if(n > 0)
|
---|
85 | word = word.mid(n);
|
---|
86 | break;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | cur += word;
|
---|
91 | }
|
---|
92 | return lines;
|
---|
93 | }
|
---|
94 |
|
---|
95 | //----------------------------------------------------------------------------
|
---|
96 | // HistoryView
|
---|
97 | //----------------------------------------------------------------------------
|
---|
98 | class HistoryDlg::Private
|
---|
99 | {
|
---|
100 | public:
|
---|
101 | Private() {}
|
---|
102 |
|
---|
103 | Jid jid;
|
---|
104 | PsiAccount *pa;
|
---|
105 | HistoryView *lv;
|
---|
106 | //BusyWidget *busy;
|
---|
107 | QPushButton *pb_prev, *pb_next, *pb_refresh, *pb_find;
|
---|
108 | QLineEdit *le_find;
|
---|
109 |
|
---|
110 | QString id_prev, id_begin, id_end, id_next;
|
---|
111 | int reqtype;
|
---|
112 | QString findStr;
|
---|
113 |
|
---|
114 | EDBHandle *h, *exp;
|
---|
115 | };
|
---|
116 |
|
---|
117 | HistoryDlg::HistoryDlg(const Jid &jid, PsiAccount *pa)
|
---|
118 | :QWidget(0, 0, WDestructiveClose)
|
---|
119 | {
|
---|
120 | d = new Private;
|
---|
121 | d->pa = pa;
|
---|
122 | d->jid = jid;
|
---|
123 | d->pa->dialogRegister(this, d->jid);
|
---|
124 | d->exp = 0;
|
---|
125 |
|
---|
126 | setCaption(d->jid.full());
|
---|
127 | #ifndef Q_WS_MAC
|
---|
128 | setIcon(IconsetFactory::icon("psi/history"));
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | d->h = new EDBHandle(d->pa->edb());
|
---|
132 | connect(d->h, SIGNAL(finished()), SLOT(edb_finished()));
|
---|
133 |
|
---|
134 | QVBoxLayout *vb1 = new QVBoxLayout(this, 8);
|
---|
135 | d->lv = new HistoryView(this);
|
---|
136 | d->lv->setVScrollBarMode(QScrollView::AlwaysOn);
|
---|
137 | connect(d->lv, SIGNAL(aOpenEvent(PsiEvent *)), SLOT(actionOpenEvent(PsiEvent *)));
|
---|
138 | QSizePolicy sp = d->lv->sizePolicy();
|
---|
139 | sp.setVerStretch(1);
|
---|
140 | d->lv->setSizePolicy(sp);
|
---|
141 | vb1->addWidget(d->lv);
|
---|
142 |
|
---|
143 | QHBoxLayout *hb1 = new QHBoxLayout(vb1);
|
---|
144 |
|
---|
145 | QVBoxLayout *vb2 = new QVBoxLayout(hb1);
|
---|
146 | QHBoxLayout *hb2 = new QHBoxLayout(vb2);
|
---|
147 |
|
---|
148 | //d->busy = new BusyWidget(this);
|
---|
149 | //hb1->addWidget(d->busy);
|
---|
150 |
|
---|
151 | d->pb_refresh = new QPushButton(tr("&Latest"), this);
|
---|
152 | d->pb_refresh->setMinimumWidth(80);
|
---|
153 | connect(d->pb_refresh, SIGNAL(clicked()), SLOT(doLatest()));
|
---|
154 | hb2->addWidget(d->pb_refresh);
|
---|
155 |
|
---|
156 | d->pb_prev = new QPushButton(tr("&Previous"), this);
|
---|
157 | d->pb_prev->setMinimumWidth(80);
|
---|
158 | connect(d->pb_prev, SIGNAL(clicked()), SLOT(doPrev()));
|
---|
159 | hb2->addWidget(d->pb_prev);
|
---|
160 |
|
---|
161 | d->pb_next = new QPushButton(tr("&Next"), this);
|
---|
162 | d->pb_next->setMinimumWidth(80);
|
---|
163 | connect(d->pb_next, SIGNAL(clicked()), SLOT(doNext()));
|
---|
164 | hb2->addWidget(d->pb_next);
|
---|
165 |
|
---|
166 | QHBoxLayout *hb3 = new QHBoxLayout(vb2);
|
---|
167 |
|
---|
168 | d->le_find = new QLineEdit(this);
|
---|
169 | connect(d->le_find, SIGNAL(textChanged(const QString &)), SLOT(le_textChanged(const QString &)));
|
---|
170 | connect(d->le_find, SIGNAL(returnPressed()), SLOT(doFind()));
|
---|
171 | hb3->addWidget(d->le_find);
|
---|
172 | d->pb_find = new QPushButton(tr("Find"), this);
|
---|
173 | connect(d->pb_find, SIGNAL(clicked()), SLOT(doFind()));
|
---|
174 | d->pb_find->setEnabled(false);
|
---|
175 | hb3->addWidget(d->pb_find);
|
---|
176 |
|
---|
177 | QFrame *sep;
|
---|
178 | sep = new QFrame(this);
|
---|
179 | sep->setFrameShape(QFrame::VLine);
|
---|
180 | hb1->addWidget(sep);
|
---|
181 |
|
---|
182 | QVBoxLayout *vb3 = new QVBoxLayout(hb1);
|
---|
183 | QPushButton *pb_save = new QPushButton(tr("&Export..."), this);
|
---|
184 | connect(pb_save, SIGNAL(clicked()), SLOT(doSave()));
|
---|
185 | vb3->addWidget(pb_save);
|
---|
186 | QPushButton *pb_erase = new QPushButton(tr("Er&ase All"), this);
|
---|
187 | connect(pb_erase, SIGNAL(clicked()), SLOT(doErase()));
|
---|
188 | vb3->addWidget(pb_erase);
|
---|
189 |
|
---|
190 | sep = new QFrame(this);
|
---|
191 | sep->setFrameShape(QFrame::VLine);
|
---|
192 | hb1->addWidget(sep);
|
---|
193 |
|
---|
194 | hb1->addStretch(1);
|
---|
195 |
|
---|
196 | QVBoxLayout *vb4 = new QVBoxLayout(hb1);
|
---|
197 | vb4->addStretch(1);
|
---|
198 |
|
---|
199 | QPushButton *pb_close = new QPushButton(tr("&Close"), this);
|
---|
200 | pb_close->setMinimumWidth(80);
|
---|
201 | connect(pb_close, SIGNAL(clicked()), SLOT(close()));
|
---|
202 | vb4->addWidget(pb_close);
|
---|
203 |
|
---|
204 | resize(520,320);
|
---|
205 |
|
---|
206 | X11WM_CLASS("history");
|
---|
207 |
|
---|
208 | d->le_find->setFocus();
|
---|
209 |
|
---|
210 | setButtons();
|
---|
211 | doLatest();
|
---|
212 | }
|
---|
213 |
|
---|
214 | HistoryDlg::~HistoryDlg()
|
---|
215 | {
|
---|
216 | delete d->exp;
|
---|
217 | d->pa->dialogUnregister(this);
|
---|
218 | delete d;
|
---|
219 | }
|
---|
220 |
|
---|
221 | void HistoryDlg::keyPressEvent(QKeyEvent *e)
|
---|
222 | {
|
---|
223 | if(e->key() == Key_Escape)
|
---|
224 | close();
|
---|
225 | else {
|
---|
226 | e->ignore();
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | void HistoryDlg::closeEvent(QCloseEvent *e)
|
---|
231 | {
|
---|
232 | if(d->exp)
|
---|
233 | return;
|
---|
234 |
|
---|
235 | e->accept();
|
---|
236 | }
|
---|
237 |
|
---|
238 | void HistoryDlg::show()
|
---|
239 | {
|
---|
240 | QWidget::show();
|
---|
241 | d->lv->doResize();
|
---|
242 | }
|
---|
243 |
|
---|
244 | void HistoryDlg::le_textChanged(const QString &s)
|
---|
245 | {
|
---|
246 | if(s.isEmpty() && d->pb_find->isEnabled())
|
---|
247 | d->pb_find->setEnabled(false);
|
---|
248 | else if(!s.isEmpty() && !d->pb_find->isEnabled())
|
---|
249 | d->pb_find->setEnabled(true);
|
---|
250 | }
|
---|
251 |
|
---|
252 | void HistoryDlg::setButtons()
|
---|
253 | {
|
---|
254 | d->pb_prev->setEnabled(!d->id_prev.isEmpty());
|
---|
255 | d->pb_next->setEnabled(!d->id_next.isEmpty());
|
---|
256 | }
|
---|
257 |
|
---|
258 | void HistoryDlg::doLatest()
|
---|
259 | {
|
---|
260 | loadPage(0);
|
---|
261 | }
|
---|
262 |
|
---|
263 | void HistoryDlg::doPrev()
|
---|
264 | {
|
---|
265 | loadPage(1);
|
---|
266 | }
|
---|
267 |
|
---|
268 | void HistoryDlg::doNext()
|
---|
269 | {
|
---|
270 | loadPage(2);
|
---|
271 | }
|
---|
272 |
|
---|
273 | void HistoryDlg::doSave()
|
---|
274 | {
|
---|
275 | if(option.lastSavePath.isEmpty())
|
---|
276 | option.lastSavePath = QDir::homeDirPath();
|
---|
277 |
|
---|
278 | UserListItem *u = d->pa->findFirstRelavent(d->jid);
|
---|
279 | QString them = jidnick(u->jid().full(), u->name());
|
---|
280 | QString s = qstrlower(jidEncode(them));
|
---|
281 |
|
---|
282 | QString str = option.lastSavePath + "/" + s + ".txt";
|
---|
283 | while(1) {
|
---|
284 | str = QFileDialog::getSaveFileName(str, tr("Text files (*.txt);;All files (*.*)"), this, 0, tr("Export message history"));
|
---|
285 | if(str.isEmpty())
|
---|
286 | break;
|
---|
287 |
|
---|
288 | QFileInfo fi(str);
|
---|
289 | if(fi.exists()) {
|
---|
290 | int x = QMessageBox::information(this, tr("Confirm overwrite"), tr("File already exists, overwrite?"), tr("&Yes"), tr("&No"));
|
---|
291 | if(x != 0)
|
---|
292 | continue;
|
---|
293 | }
|
---|
294 |
|
---|
295 | option.lastSavePath = fi.dirPath();
|
---|
296 | exportHistory(str);
|
---|
297 | break;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | void HistoryDlg::doErase()
|
---|
302 | {
|
---|
303 | int x = QMessageBox::information(this, tr("Confirm erase all"), tr("This will erase all message history for this contact!\nAre you sure you want to do this?"), tr("&Yes"), tr("&No"), QString::null, 1);
|
---|
304 | if(x == 0) {
|
---|
305 | QString fname = getHistoryDir() + "/" + qstrlower(jidEncode(d->jid.userHost())) + ".history";
|
---|
306 | QFileInfo fi(fname);
|
---|
307 | if(fi.exists()) {
|
---|
308 | QDir dir = fi.dir();
|
---|
309 | dir.remove(fi.fileName());
|
---|
310 | }
|
---|
311 | d->lv->clear();
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | void HistoryDlg::loadPage(int type)
|
---|
316 | {
|
---|
317 | d->reqtype = type;
|
---|
318 | if(type == 0) {
|
---|
319 | d->pb_refresh->setEnabled(false);
|
---|
320 | d->h->getLatest(d->jid, 50);
|
---|
321 | //printf("EDB: requesting latest 50 events\n");
|
---|
322 | }
|
---|
323 | else if(type == 1) {
|
---|
324 | d->pb_prev->setEnabled(false);
|
---|
325 | d->h->get(d->jid, d->id_prev, EDB::Backward, 50);
|
---|
326 | //printf("EDB: requesting 50 events backward, starting at %s\n", d->id_prev.latin1());
|
---|
327 | }
|
---|
328 | else if(type == 2) {
|
---|
329 | d->pb_next->setEnabled(false);
|
---|
330 | d->h->get(d->jid, d->id_next, EDB::Forward, 50);
|
---|
331 | //printf("EDB: requesting 50 events forward, starting at %s\n", d->id_next.latin1());
|
---|
332 | }
|
---|
333 |
|
---|
334 | //d->busy->start();
|
---|
335 | }
|
---|
336 |
|
---|
337 | void HistoryDlg::displayResult(const EDBResult *r, int direction, int max)
|
---|
338 | {
|
---|
339 | //d->lv->setUpdatesEnabled(false);
|
---|
340 | d->lv->clear();
|
---|
341 | QPtrListIterator<EDBItem> it(*r);
|
---|
342 | if(direction == EDB::Forward)
|
---|
343 | it.toLast();
|
---|
344 | int at = 0;
|
---|
345 | for(EDBItem *i; (i = it.current()) && (max == -1 ? true : at < max);) {
|
---|
346 | PsiEvent *e = i->event();
|
---|
347 | /*printf(" id=%s", i->id().latin1());
|
---|
348 | if(i->prevId())
|
---|
349 | printf(", prevId=%s", i->prevId().latin1());
|
---|
350 | if(i->nextId())
|
---|
351 | printf(", nextId=%s", i->nextId().latin1());
|
---|
352 | printf("\n");
|
---|
353 | if(e->type() == PsiEvent::Message) {
|
---|
354 | MessageEvent *me = (MessageEvent *)e;
|
---|
355 | printf(" body: [%s]\n", me->message().body().latin1());
|
---|
356 | }
|
---|
357 | else if(e->type() == PsiEvent::Auth) {
|
---|
358 | AuthEvent *ae = (AuthEvent *)e;
|
---|
359 | printf(" authType: [%s]\n", ae->authType().latin1());
|
---|
360 | }
|
---|
361 | else {
|
---|
362 | printf(" unknown event type\n");
|
---|
363 | }
|
---|
364 | printf("\n");*/
|
---|
365 |
|
---|
366 | d->lv->addEvent(e, i->prevId());
|
---|
367 | ++at;
|
---|
368 | if(direction == EDB::Backward)
|
---|
369 | ++it;
|
---|
370 | else
|
---|
371 | --it;
|
---|
372 | }
|
---|
373 | //d->lv->setUpdatesEnabled(true);
|
---|
374 | //d->lv->repaint(true);
|
---|
375 | }
|
---|
376 |
|
---|
377 | void HistoryDlg::edb_finished()
|
---|
378 | {
|
---|
379 | const EDBResult *r = d->h->result();
|
---|
380 | if(r) {
|
---|
381 | //printf("EDB: retrieved %d events:\n", r->count());
|
---|
382 | if(r->count() > 0) {
|
---|
383 | QPtrListIterator<EDBItem> it(*r);
|
---|
384 | if(d->reqtype == 0 || d->reqtype == 1) {
|
---|
385 | // events are in backward order
|
---|
386 | // first entry is the end event
|
---|
387 | it.toFirst();
|
---|
388 | d->id_end = it.current()->id();
|
---|
389 | d->id_next = it.current()->nextId();
|
---|
390 | // last entry is the begin event
|
---|
391 | it.toLast();
|
---|
392 | d->id_begin = it.current()->id();
|
---|
393 | d->id_prev = it.current()->prevId();
|
---|
394 | displayResult(r, EDB::Backward);
|
---|
395 | //printf("[%s],[%s],[%s],[%s]\n", d->id_prev.latin1(), d->id_begin.latin1(), d->id_end.latin1(), d->id_next.latin1());
|
---|
396 | }
|
---|
397 | else if(d->reqtype == 2) {
|
---|
398 | // events are in forward order
|
---|
399 | // last entry is the end event
|
---|
400 | it.toLast();
|
---|
401 | d->id_end = it.current()->id();
|
---|
402 | d->id_next = it.current()->nextId();
|
---|
403 | // first entry is the begin event
|
---|
404 | it.toFirst();
|
---|
405 | d->id_begin = it.current()->id();
|
---|
406 | d->id_prev = it.current()->prevId();
|
---|
407 | displayResult(r, EDB::Forward);
|
---|
408 | }
|
---|
409 | else if(d->reqtype == 3) {
|
---|
410 | // should only be one entry
|
---|
411 | EDBItem *ei = it.current();
|
---|
412 | d->reqtype = 1;
|
---|
413 | d->h->get(d->jid, ei->id(), EDB::Backward, 50);
|
---|
414 | //printf("EDB: requesting 50 events backward, starting at %s\n", d->id_prev.latin1());
|
---|
415 | return;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | else {
|
---|
419 | if(d->reqtype == 3) {
|
---|
420 | QMessageBox::information(this, tr("Find"), tr("Search string '%1' not found.").arg(d->findStr));
|
---|
421 | return;
|
---|
422 | }
|
---|
423 | }
|
---|
424 | }
|
---|
425 | else {
|
---|
426 | //printf("EDB: error\n");
|
---|
427 | }
|
---|
428 |
|
---|
429 | if(d->lv->firstChild())
|
---|
430 | d->lv->setSelected(d->lv->firstChild(), true);
|
---|
431 |
|
---|
432 | //d->busy->stop();
|
---|
433 | d->pb_refresh->setEnabled(true);
|
---|
434 | setButtons();
|
---|
435 | }
|
---|
436 |
|
---|
437 | void HistoryDlg::actionOpenEvent(PsiEvent *e)
|
---|
438 | {
|
---|
439 | openEvent(e);
|
---|
440 | }
|
---|
441 |
|
---|
442 | void HistoryDlg::doFind()
|
---|
443 | {
|
---|
444 | QString str = d->le_find->text();
|
---|
445 | if(str.isEmpty())
|
---|
446 | return;
|
---|
447 |
|
---|
448 | if(d->lv->childCount() < 1)
|
---|
449 | return;
|
---|
450 |
|
---|
451 | HistoryViewItem *i = (HistoryViewItem *)d->lv->selectedItem();
|
---|
452 | if(!i)
|
---|
453 | i = (HistoryViewItem *)d->lv->firstChild();
|
---|
454 | QString id = i->eventId;
|
---|
455 | if(id.isEmpty()) {
|
---|
456 | QMessageBox::information(this, tr("Find"), tr("Already at beginning of message history."));
|
---|
457 | return;
|
---|
458 | }
|
---|
459 |
|
---|
460 | //printf("searching for: [%s], starting at id=[%s]\n", str.latin1(), id.latin1());
|
---|
461 | d->reqtype = 3;
|
---|
462 | d->findStr = str;
|
---|
463 | d->h->find(str, d->jid, id, EDB::Backward);
|
---|
464 | }
|
---|
465 |
|
---|
466 | void HistoryDlg::exportHistory(const QString &fname)
|
---|
467 | {
|
---|
468 | QFile f(fname);
|
---|
469 | if(!f.open(IO_WriteOnly | IO_Truncate | IO_Translate)) {
|
---|
470 | QMessageBox::information(this, tr("Error"), tr("Error writing to file."));
|
---|
471 | return;
|
---|
472 | }
|
---|
473 | QTextStream stream(&f);
|
---|
474 |
|
---|
475 | QString us = d->pa->nick();
|
---|
476 | UserListItem *u = d->pa->findFirstRelavent(d->jid);
|
---|
477 | QString them = jidnick(u->jid().full(), u->name());
|
---|
478 |
|
---|
479 | d->exp = new EDBHandle(d->pa->edb());
|
---|
480 | QString id;
|
---|
481 | while(1) {
|
---|
482 | if(id.isEmpty())
|
---|
483 | d->exp->getOldest(d->jid, 1000);
|
---|
484 | else
|
---|
485 | d->exp->get(d->jid, id, EDB::Forward, 1000);
|
---|
486 | while(d->exp->busy())
|
---|
487 | qApp->processEvents();
|
---|
488 |
|
---|
489 | const EDBResult *r = d->exp->result();
|
---|
490 | if(!r)
|
---|
491 | break;
|
---|
492 | if(r->count() <= 0)
|
---|
493 | break;
|
---|
494 |
|
---|
495 | // events are in forward order
|
---|
496 | QPtrListIterator<EDBItem> it(*r);
|
---|
497 | for(EDBItem *i; (i = it.current()); ++it) {
|
---|
498 | id = it.current()->nextId();
|
---|
499 | PsiEvent *e = i->event();
|
---|
500 | QString txt;
|
---|
501 |
|
---|
502 | QDateTime dt = e->timeStamp();
|
---|
503 | QString ts;
|
---|
504 | //ts.sprintf("%04d/%02d/%02d %02d:%02d:%02d", dt.date().year(), dt.date().month(), dt.date().day(), dt.time().hour(), dt.time().minute(), dt.time().second());
|
---|
505 | ts = dt.toString(LocalDate);
|
---|
506 |
|
---|
507 | QString nick;
|
---|
508 | if(e->originLocal())
|
---|
509 | nick = us;
|
---|
510 | else
|
---|
511 | nick = them;
|
---|
512 |
|
---|
513 | QString heading = QString("(%1) ").arg(ts) + nick + ": ";
|
---|
514 | if(e->type() == PsiEvent::Message) {
|
---|
515 | MessageEvent *me = (MessageEvent *)e;
|
---|
516 | stream << heading << endl;
|
---|
517 |
|
---|
518 | QStringList lines = QStringList::split('\n', me->message().body(), true);
|
---|
519 | for(QStringList::ConstIterator lit = lines.begin(); lit != lines.end(); ++lit) {
|
---|
520 | QStringList sub = wrapString(*lit, 72);
|
---|
521 | for(QStringList::ConstIterator lit2 = sub.begin(); lit2 != sub.end(); ++lit2)
|
---|
522 | txt += QString(" ") + *lit2 + '\n';
|
---|
523 | }
|
---|
524 | }
|
---|
525 | else
|
---|
526 | continue;
|
---|
527 |
|
---|
528 | stream << txt << endl;
|
---|
529 | }
|
---|
530 |
|
---|
531 | // done!
|
---|
532 | if(id.isEmpty())
|
---|
533 | break;
|
---|
534 | }
|
---|
535 | delete d->exp;
|
---|
536 | d->exp = 0;
|
---|
537 | f.close();
|
---|
538 | }
|
---|
539 |
|
---|
540 | //----------------------------------------------------------------------------
|
---|
541 | // HistoryView
|
---|
542 | //----------------------------------------------------------------------------
|
---|
543 | HistoryView::HistoryView(QWidget *parent, const char *name)
|
---|
544 | :QListView(parent, name)
|
---|
545 | {
|
---|
546 | at_id = 0;
|
---|
547 | connect(this, SIGNAL(doubleClicked(QListViewItem *)), SLOT(qlv_doubleclick(QListViewItem *)));
|
---|
548 | connect(this, SIGNAL(rightButtonPressed(QListViewItem *, const QPoint &, int)), SLOT(qlv_contextPopup(QListViewItem *, const QPoint &, int)));
|
---|
549 |
|
---|
550 | setAllColumnsShowFocus(true);
|
---|
551 | addColumn(tr("Type"));
|
---|
552 | addColumn(tr("Origin"));
|
---|
553 | addColumn(tr("Date"));
|
---|
554 | addColumn(tr("Text"));
|
---|
555 | setSorting(2);
|
---|
556 | setResizeMode(QListView::LastColumn);
|
---|
557 | setShowToolTips(false);
|
---|
558 | header()->setClickEnabled(false);
|
---|
559 | header()->setMovingEnabled(false);
|
---|
560 | header()->setResizeEnabled(false);
|
---|
561 | }
|
---|
562 |
|
---|
563 | void HistoryView::resizeEvent(QResizeEvent *e)
|
---|
564 | {
|
---|
565 | QListView::resizeEvent(e);
|
---|
566 |
|
---|
567 | if(e->oldSize().width() != e->size().width())
|
---|
568 | doResize();
|
---|
569 | }
|
---|
570 |
|
---|
571 | void HistoryView::keyPressEvent(QKeyEvent *e)
|
---|
572 | {
|
---|
573 | if(e->key() == Key_Enter || e->key() == Key_Return)
|
---|
574 | doOpenEvent();
|
---|
575 | else
|
---|
576 | QListView::keyPressEvent(e);
|
---|
577 | }
|
---|
578 |
|
---|
579 | void HistoryView::doResize()
|
---|
580 | {
|
---|
581 | QListViewItemIterator it(this);
|
---|
582 | HistoryViewItem *item;
|
---|
583 | for(; it.current() ; ++it) {
|
---|
584 | item = (HistoryViewItem *)it.current();
|
---|
585 | item->setup();
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | void HistoryView::addEvent(PsiEvent *e, const QString &eid)
|
---|
590 | {
|
---|
591 | new HistoryViewItem(e, eid, at_id++, this);
|
---|
592 | }
|
---|
593 |
|
---|
594 | void HistoryView::doOpenEvent()
|
---|
595 | {
|
---|
596 | HistoryViewItem *i = (HistoryViewItem *)selectedItem();
|
---|
597 | if(!i)
|
---|
598 | return;
|
---|
599 | aOpenEvent(i->e);
|
---|
600 | }
|
---|
601 |
|
---|
602 | void HistoryView::qlv_doubleclick(QListViewItem *xi)
|
---|
603 | {
|
---|
604 | HistoryViewItem *i = (HistoryViewItem *)xi;
|
---|
605 |
|
---|
606 | setSelected(i, true);
|
---|
607 | doOpenEvent();
|
---|
608 | }
|
---|
609 |
|
---|
610 | void HistoryView::qlv_contextPopup(QListViewItem *ix, const QPoint &pos, int)
|
---|
611 | {
|
---|
612 | HistoryViewItem *i = (HistoryViewItem *)ix;
|
---|
613 | if(!i)
|
---|
614 | return;
|
---|
615 |
|
---|
616 | QPopupMenu popup;
|
---|
617 | popup.insertItem(tr("Open"), 1);
|
---|
618 | popup.insertSeparator();
|
---|
619 | popup.insertItem(tr("Copy"), 2);
|
---|
620 |
|
---|
621 | if(i->e->type() != PsiEvent::Message)
|
---|
622 | popup.setItemEnabled(2, false);
|
---|
623 |
|
---|
624 | int x = popup.exec(pos);
|
---|
625 |
|
---|
626 | if(x == 1)
|
---|
627 | doOpenEvent();
|
---|
628 | else if(x == 2) {
|
---|
629 | HistoryViewItem *i = (HistoryViewItem *)selectedItem();
|
---|
630 | if(!i)
|
---|
631 | return;
|
---|
632 |
|
---|
633 | MessageEvent *me = (MessageEvent *)i->e;
|
---|
634 | QApplication::clipboard()->setText(me->message().body(), QClipboard::Clipboard);
|
---|
635 | if(QApplication::clipboard()->supportsSelection())
|
---|
636 | QApplication::clipboard()->setText(me->message().body(), QClipboard::Selection);
|
---|
637 | }
|
---|
638 | }
|
---|
639 |
|
---|
640 |
|
---|
641 | //----------------------------------------------------------------------------
|
---|
642 | // HistoryViewItem
|
---|
643 | //----------------------------------------------------------------------------
|
---|
644 | HistoryViewItem::HistoryViewItem(PsiEvent *_e, const QString &eid, int xid, QListView *parent)
|
---|
645 | :QListViewItem(parent)
|
---|
646 | {
|
---|
647 | rt = 0;
|
---|
648 | id = xid;
|
---|
649 | eventId = eid;
|
---|
650 |
|
---|
651 | if(_e->type() == PsiEvent::Message) {
|
---|
652 | MessageEvent *me = (MessageEvent *)_e;
|
---|
653 | e = new MessageEvent(*me);
|
---|
654 | }
|
---|
655 | else if(_e->type() == PsiEvent::Auth) {
|
---|
656 | AuthEvent *ae = (AuthEvent *)_e;
|
---|
657 | e = new AuthEvent(*ae);
|
---|
658 | }
|
---|
659 |
|
---|
660 | Icon *a = is->event2icon(e);
|
---|
661 | if(e->type() == PsiEvent::Message) {
|
---|
662 | MessageEvent *me = (MessageEvent *)e;
|
---|
663 | const Message &m = me->message();
|
---|
664 | text = plain2rich(m.body());
|
---|
665 |
|
---|
666 | if(!m.urlList().isEmpty())
|
---|
667 | setPixmap(0, IconsetFactory::icon("psi/www"));
|
---|
668 | else if(e->originLocal())
|
---|
669 | setPixmap(0, IconsetFactory::icon("psi/sendMessage"));
|
---|
670 | else
|
---|
671 | setPixmap(0, a->impix());
|
---|
672 | }
|
---|
673 | else if(e->type() == PsiEvent::Auth) {
|
---|
674 | AuthEvent *ae = (AuthEvent *)e;
|
---|
675 | text = ae->authType();
|
---|
676 | setPixmap(0, a->impix());
|
---|
677 | }
|
---|
678 |
|
---|
679 | if(e->originLocal())
|
---|
680 | setText(1, HistoryView::tr("To"));
|
---|
681 | else
|
---|
682 | setText(1, HistoryView::tr("From"));
|
---|
683 |
|
---|
684 | QString date;
|
---|
685 | const QDateTime &ts = e->timeStamp();
|
---|
686 | /*date.sprintf("%02d/%02d/%02d %02d:%02d:%02d",
|
---|
687 | ts.date().month(),
|
---|
688 | ts.date().day(),
|
---|
689 | ts.date().year(),
|
---|
690 | ts.time().hour(),
|
---|
691 | ts.time().minute(),
|
---|
692 | ts.time().second());*/
|
---|
693 | date = ts.toString(LocalDate);
|
---|
694 |
|
---|
695 | setText(2, date);
|
---|
696 |
|
---|
697 | rt = new QSimpleRichText(text, listView()->font());
|
---|
698 | }
|
---|
699 |
|
---|
700 | HistoryViewItem::~HistoryViewItem()
|
---|
701 | {
|
---|
702 | delete rt;
|
---|
703 | delete e;
|
---|
704 | }
|
---|
705 |
|
---|
706 | // reimplemented from QListViewItem. setup() and paintCell() are tricky stuff
|
---|
707 | void HistoryViewItem::setup()
|
---|
708 | {
|
---|
709 | widthChanged();
|
---|
710 |
|
---|
711 | QListView *lv = listView();
|
---|
712 |
|
---|
713 | if(rt) {
|
---|
714 | int w = lv->columnWidth(3);
|
---|
715 | rt->setWidth(w);
|
---|
716 | }
|
---|
717 |
|
---|
718 | int y;
|
---|
719 | //y = lv->fontMetrics().size(AlignVCenter, displayStr).height();
|
---|
720 | if(!rt)
|
---|
721 | y = 22;
|
---|
722 | else
|
---|
723 | y = rt->height();
|
---|
724 |
|
---|
725 | y += lv->itemMargin() * 2;
|
---|
726 |
|
---|
727 | // ensure an even number
|
---|
728 | if(y & 1)
|
---|
729 | ++y;
|
---|
730 |
|
---|
731 | setHeight(y);
|
---|
732 | }
|
---|
733 |
|
---|
734 | void HistoryViewItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int alignment)
|
---|
735 | {
|
---|
736 | QColorGroup mycg = cg;
|
---|
737 | if(e->originLocal())
|
---|
738 | mycg.setColor(QColorGroup::Text, Qt::red);
|
---|
739 | else
|
---|
740 | mycg.setColor(QColorGroup::Text, Qt::blue);
|
---|
741 |
|
---|
742 | if(column == 3) {
|
---|
743 | QBrush br;
|
---|
744 | if(isSelected()) {
|
---|
745 | mycg.setColor(QColorGroup::Text, mycg.highlightedText());
|
---|
746 | br = cg.brush(QColorGroup::Highlight);
|
---|
747 | }
|
---|
748 | else {
|
---|
749 | br = cg.brush(QColorGroup::Base);
|
---|
750 | }
|
---|
751 |
|
---|
752 | int h = height();
|
---|
753 | if(rt) {
|
---|
754 | QSimpleRichText tmp(QString("<qt><font color=\"%1\">" + text + "</font></qt>").arg(mycg.text().name()), listView()->font());
|
---|
755 | tmp.setWidth(rt->width());
|
---|
756 | tmp.draw(p, 0, 0, QRect(0, 0, width, h), mycg, &br);
|
---|
757 | }
|
---|
758 | }
|
---|
759 | else {
|
---|
760 | alignment = AlignTop;
|
---|
761 |
|
---|
762 | QListViewItem::paintCell(p, mycg, column, width, alignment);
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | int HistoryViewItem::compare(QListViewItem *xi, int, bool) const
|
---|
767 | {
|
---|
768 | HistoryViewItem *i = (HistoryViewItem *)xi;
|
---|
769 | return id - i->id;
|
---|
770 | }
|
---|
771 |
|
---|
772 | int HistoryViewItem::rtti() const
|
---|
773 | {
|
---|
774 | return 7105;
|
---|
775 | }
|
---|
776 |
|
---|