~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/eventdb.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *
19
19
 */
20
20
 
21
 
#include"eventdb.h"
 
21
#include "eventdb.h"
22
22
 
23
23
#define FAKEDELAY 0
24
24
 
25
 
#include<qfile.h>
26
 
#include<qptrlist.h>
27
 
#include<qtimer.h>
28
 
#include<qtextstream.h>
29
 
#include<qvaluevector.h>
30
 
#include"common.h"
31
 
#include"profiles.h"
32
 
 
 
25
#include <QVector>
 
26
#include <QFileInfo>
 
27
#include <QDir>
 
28
#include <q3ptrlist.h>
 
29
#include <qtimer.h>
 
30
#include <qtextstream.h>
 
31
 
 
32
#include "profiles.h"
 
33
#include "applicationinfo.h"
 
34
#include "psievent.h"
 
35
#include "jidutil.h"
 
36
 
 
37
using namespace XMPP;
33
38
 
34
39
//----------------------------------------------------------------------------
35
40
// EDBItem
141
146
        d->listeningFor = d->edb->op_append(j, e);
142
147
}
143
148
 
 
149
void EDBHandle::erase(const Jid &j)
 
150
{
 
151
        d->busy = true;
 
152
        d->lastRequestType = Erase;
 
153
        d->listeningFor = d->edb->op_erase(j);
 
154
}
 
155
 
144
156
bool EDBHandle::busy() const
145
157
{
146
158
        return d->busy;
197
209
public:
198
210
        Private() {}
199
211
 
200
 
        QPtrList<EDBHandle> list;
 
212
        Q3PtrList<EDBHandle> list;
201
213
        int reqid_base;
202
214
};
203
215
 
254
266
        return append(j, e);
255
267
}
256
268
 
 
269
int EDB::op_erase(const Jid &j)
 
270
{
 
271
        return erase(j);
 
272
}
 
273
 
257
274
void EDB::resultReady(int req, EDBResult *r)
258
275
{
259
276
        // deliver
260
 
        QPtrListIterator<EDBHandle> it(d->list);
 
277
        Q3PtrListIterator<EDBHandle> it(d->list);
261
278
        for(EDBHandle *h; (h = it.current()); ++it) {
262
279
                if(h->listeningFor() == req) {
263
280
                        h->edb_resultReady(r);
270
287
void EDB::writeFinished(int req, bool b)
271
288
{
272
289
        // deliver
273
 
        QPtrListIterator<EDBHandle> it(d->list);
 
290
        Q3PtrListIterator<EDBHandle> it(d->list);
274
291
        for(EDBHandle *h; (h = it.current()); ++it) {
275
292
                if(h->listeningFor() == req) {
276
293
                        h->edb_writeFinished(b);
299
316
                Type_getOldest,
300
317
                Type_get,
301
318
                Type_append,
302
 
                Type_find
 
319
                Type_find,
 
320
                Type_erase
303
321
        };
304
322
};
305
323
 
308
326
public:
309
327
        Private() {}
310
328
 
311
 
        QPtrList<File> flist;
312
 
        QPtrList<item_file_req> rlist;
 
329
        Q3PtrList<File> flist;
 
330
        Q3PtrList<item_file_req> rlist;
313
331
};
314
332
 
315
333
EDBFlatFile::EDBFlatFile()
402
420
        return r->id;
403
421
}
404
422
 
 
423
int EDBFlatFile::erase(const Jid &j)
 
424
{
 
425
        item_file_req *r = new item_file_req;
 
426
        r->j = j;
 
427
        r->type = item_file_req::Type_erase;
 
428
        r->event = 0;
 
429
        r->id = genUniqueId();
 
430
        d->rlist.append(r);
 
431
 
 
432
        QTimer::singleShot(FAKEDELAY, this, SLOT(performRequests()));
 
433
        return r->id;
 
434
}
 
435
 
405
436
EDBFlatFile::File *EDBFlatFile::findFile(const Jid &j) const
406
437
{
407
 
        QPtrListIterator<File> it(d->flist);
 
438
        Q3PtrListIterator<File> it(d->flist);
408
439
        for(File *i; (i = it.current()); ++it) {
409
440
                if(i->j.compare(j, false))
410
441
                        return i;
423
454
        return i;
424
455
}
425
456
 
 
457
bool EDBFlatFile::deleteFile(const Jid &j)
 
458
{
 
459
        File *i = findFile(j);
 
460
 
 
461
        QString fname;
 
462
 
 
463
        if (i) {
 
464
                fname = i->fname;
 
465
                d->flist.remove(i);
 
466
                delete i;
 
467
        }
 
468
        else {
 
469
                fname = File::jidToFileName(j);
 
470
        }
 
471
 
 
472
        QFileInfo fi(fname);
 
473
        if(fi.exists()) {
 
474
                QDir dir = fi.dir();
 
475
                return dir.remove(fi.fileName());
 
476
        }
 
477
        else
 
478
                return true;
 
479
}
 
480
 
426
481
void EDBFlatFile::performRequests()
427
482
{
428
483
        if(d->rlist.isEmpty())
524
579
                }
525
580
                resultReady(r->id, result);
526
581
        }
 
582
        else if(type == item_file_req::Type_erase) {
 
583
                writeFinished(r->id, deleteFile(f->j));
 
584
        }
527
585
 
528
586
        delete r;
529
587
}
544
602
public:
545
603
        Private() {}
546
604
 
547
 
        QValueVector<int> index;
 
605
        QVector<quint64> index;
548
606
        bool indexed;
549
607
};
550
608
 
559
617
        connect(t, SIGNAL(timeout()), SLOT(timer_timeout()));
560
618
 
561
619
        //printf("[EDB opening -- %s]\n", j.full().latin1());
562
 
        QString s = j.userHost();
563
 
        fname = getHistoryDir() + "/" + qstrlower(jidEncode(s)) + ".history";
 
620
        fname = jidToFileName(_j);
564
621
        f.setName(fname);
565
 
        valid = f.open(IO_ReadWrite);
 
622
        valid = f.open(QIODevice::ReadWrite);
566
623
 
567
624
        touch();
568
625
}
576
633
        delete d;
577
634
}
578
635
 
 
636
QString EDBFlatFile::File::jidToFileName(const XMPP::Jid &j)
 
637
{
 
638
        return ApplicationInfo::historyDir() + "/" + JIDUtil::encode(j.userHost()).toLower() + ".history";
 
639
}
 
640
 
579
641
void EDBFlatFile::File::ensureIndex()
580
642
{
581
643
        if ( valid && !d->indexed ) {
 
644
                if (f.isSequential()) {
 
645
                        qWarning("EDBFlatFile::File::ensureIndex(): Can't index sequential files.");
 
646
                        return;
 
647
                }
 
648
 
582
649
                f.reset(); // go to beginning
583
650
                d->index.clear();
584
651
 
585
652
                //printf(" file: %s\n", fname.latin1());
586
653
                // build index
587
654
                while(1) {
588
 
                        int at = f.at();
 
655
                        quint64 at = f.pos();
589
656
 
590
657
                        // locate a newline
591
658
                        bool found = false;
592
 
                        do {
593
 
                                int c = f.getch();
594
 
                                if(c == '\n') {
 
659
                        char c;
 
660
                        while (f.getChar(&c)) {
 
661
                                if (c == '\n') {
595
662
                                        found = true;
596
663
                                        break;
597
664
                                }
598
 
                        } while(!f.atEnd());
 
665
                        }
599
666
 
600
667
                        if(!found)
601
668
                                break;
641
708
        if(id < 0 || id > (int)d->index.size())
642
709
                return 0;
643
710
 
644
 
        f.at(d->index[id]);
 
711
        f.seek(d->index[id]);
645
712
 
646
713
        QTextStream t;
647
714
        t.setDevice(&f);
648
715
        t.setEncoding(QTextStream::UnicodeUTF8);
649
716
        QString line = t.readLine();
650
 
        t.unsetDevice();
651
717
 
652
718
        return lineToEvent(line);
653
719
}
663
729
        if(line.isEmpty())
664
730
                return false;
665
731
 
666
 
        f.at(f.size());
667
 
        int at = f.at();
 
732
        f.seek(f.size());
 
733
        quint64 at = f.pos();
668
734
 
669
735
        QTextStream t;
670
736
        t.setDevice(&f);
671
737
        t.setEncoding(QTextStream::UnicodeUTF8);
672
738
        t << line << endl;
673
 
        t.unsetDevice();
674
739
        f.flush();
675
740
 
676
741
        if ( d->indexed ) {
707
772
 
708
773
        // check for extra fields
709
774
        if(sFlags[1] != '-') {
710
 
                int subflags = hexChar2int(sFlags[1].latin1());
 
775
                int subflags = QString(sFlags[1]).toInt(NULL,16);
711
776
 
712
777
                // have subject?
713
778
                if(subflags & 1) {
785
850
                ae->setTimeStamp(QDateTime::fromString(sTime, Qt::ISODate));
786
851
                return ae;
787
852
        }
788
 
        else
789
 
                return 0;
 
853
 
 
854
        return NULL;
790
855
}
791
856
 
792
857
QString EDBFlatFile::File::eventToLine(PsiEvent *e)
817
882
                sFlags = "N---";
818
883
 
819
884
                if(subflags != 0)
820
 
                        sFlags[1] = int2hexChar(subflags);
 
885
                        sFlags[1] = QString::number(subflags,16)[0];
821
886
 
822
887
                //  | date | type | To/from | flags | text
823
888
                QString line = "|" + sTime + "|" + sType + "|" + sOrigin + "|" + sFlags + "|";
857
922
 
858
923
                return line;
859
924
        }
860
 
        else
861
 
                return "";
 
925
        
 
926
        return "";
862
927
}