~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to src/eventdb.cpp

  • Committer: Package Import Robot
  • Author(s): Jan Niehusmann
  • Date: 2014-07-01 21:49:34 UTC
  • mfrom: (6.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140701214934-gt4dkgm94byi4vnn
Tags: 0.15-1
* New upstream version
* set debhelper compat level to 9
* set Standards-Version to 3.9.5 (no further changes)
* add lintian override regarding license-problem-non-free-RFC
* use qconf to regenerate configure script
* implement hardening using buildflags instead of hardening-wrapper

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 *
15
15
 * You should have received a copy of the GNU General Public License
16
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
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 *
19
19
 */
20
20
 
25
25
#include <QVector>
26
26
#include <QFileInfo>
27
27
#include <QDir>
28
 
#include <qtimer.h>
29
 
#include <qtextstream.h>
 
28
#include <QTimer>
 
29
#include <QTextStream>
 
30
#include <QDateTime>
30
31
 
31
32
#include "common.h"
32
33
#include "applicationinfo.h"
81
82
        Private() {}
82
83
 
83
84
        EDB *edb;
84
 
        EDBResult *r;
 
85
        EDBResult r;
85
86
        bool busy;
86
87
        bool writeSuccess;
87
88
        int listeningFor;
93
94
{
94
95
        d = new Private;
95
96
        d->edb = edb;
96
 
        d->r = 0;
97
97
        d->busy = false;
98
98
        d->writeSuccess = false;
99
99
        d->listeningFor = -1;
106
106
{
107
107
        d->edb->unreg(this);
108
108
 
109
 
        delete d->r;
110
109
        delete d;
111
110
}
112
111
 
131
130
        d->listeningFor = d->edb->op_get(j, id, direction, len);
132
131
}
133
132
 
 
133
void EDBHandle::getByDate(const Jid &j, QDateTime first, QDateTime last)
 
134
{
 
135
        d->busy = true;
 
136
        d->lastRequestType = Read;
 
137
        d->listeningFor = d->edb->op_getByDate(j, first, last);
 
138
}
 
139
 
134
140
void EDBHandle::find(const QString &str, const Jid &j, const QString &id, int direction)
135
141
{
136
142
        d->busy = true;
157
163
        return d->busy;
158
164
}
159
165
 
160
 
const EDBResult *EDBHandle::result() const
 
166
const EDBResult EDBHandle::result() const
161
167
{
162
168
        return d->r;
163
169
}
167
173
        return d->writeSuccess;
168
174
}
169
175
 
170
 
void EDBHandle::edb_resultReady(EDBResult *r)
 
176
void EDBHandle::edb_resultReady(EDBResult r)
171
177
{
172
178
        d->busy = false;
173
 
        if(r == 0) {
174
 
                delete d->r;
175
 
                d->r = 0;
176
 
        }
177
 
        else {
178
 
                d->r = r;
179
 
        }
 
179
        d->r = r;
180
180
        d->listeningFor = -1;
181
181
        finished();
182
182
}
255
255
        return get(jid, id, direction, len);
256
256
}
257
257
 
 
258
int EDB::op_getByDate(const Jid &j, QDateTime first, QDateTime last)
 
259
{
 
260
        return getByDate(j, first, last);
 
261
}
 
262
 
258
263
int EDB::op_find(const QString &str, const Jid &j, const QString &id, int direction)
259
264
{
260
265
        return find(str, j, id, direction);
270
275
        return erase(j);
271
276
}
272
277
 
273
 
void EDB::resultReady(int req, EDBResult *r)
 
278
void EDB::resultReady(int req, EDBResult r)
274
279
{
275
280
        // deliver
276
281
        foreach(EDBHandle* h, d->list) {
279
284
                        return;
280
285
                }
281
286
        }
282
 
        delete r;
283
287
}
284
288
 
285
289
void EDB::writeFinished(int req, bool b)
308
312
        QString findStr;
309
313
        PsiEvent *event;
310
314
 
 
315
        QDateTime first, last;
311
316
        enum Type {
312
317
                Type_getLatest = 0,
313
318
                Type_getOldest,
314
319
                Type_get,
315
320
                Type_append,
316
321
                Type_find,
 
322
                Type_getByDate,
317
323
                Type_erase
318
324
        };
319
325
};
383
389
        return r->id;
384
390
}
385
391
 
 
392
 
 
393
int EDBFlatFile::getByDate(const XMPP::Jid &jid, QDateTime first, QDateTime last)
 
394
{
 
395
        item_file_req *r = new item_file_req;
 
396
        r->j = jid;
 
397
        r->type = item_file_req::Type_getByDate;
 
398
        r->len = 1;
 
399
        r->id = genUniqueId();
 
400
        d->rlist.append(r);
 
401
        r->first = first;
 
402
        r->last = last;
 
403
 
 
404
        QTimer::singleShot(FAKEDELAY, this, SLOT(performRequests()));
 
405
        return r->id;
 
406
}
 
407
 
386
408
int EDBFlatFile::find(const QString &str, const Jid &j, const QString &id, int direction)
387
409
{
388
410
        item_file_req *r = new item_file_req;
517
539
                                len = r->len;
518
540
                }
519
541
 
520
 
                EDBResult *result = new EDBResult;
521
 
                result->setAutoDelete(true);
 
542
                EDBResult result;
522
543
                for(int n = 0; n < len; ++n) {
523
544
                        PsiEvent *e = f->get(id);
524
545
                        if(e) {
527
548
                                        prevId = QString::number(id-1);
528
549
                                if(id < f->total()-1)
529
550
                                        nextId = QString::number(id+1);
530
 
                                EDBItem *ei = new EDBItem(e, QString::number(id), prevId, nextId);
531
 
                                result->append(ei);
 
551
                                EDBItemPtr ei = EDBItemPtr(new EDBItem(e, QString::number(id), prevId, nextId));
 
552
                                result.append(ei);
532
553
                        }
533
554
 
534
555
                        if(direction == Forward)
544
565
        }
545
566
        else if(type == item_file_req::Type_find) {
546
567
                int id = r->eventId;
547
 
                EDBResult *result = new EDBResult;
548
 
                result->setAutoDelete(true);
 
568
                EDBResult result;
549
569
                while(1) {
550
570
                        PsiEvent *e = f->get(id);
551
571
                        if(!e)
557
577
                        if(id < f->total()-1)
558
578
                                nextId = QString::number(id+1);
559
579
 
 
580
                        bool matched = false;
560
581
                        if(e->type() == PsiEvent::Message) {
561
582
                                MessageEvent *me = (MessageEvent *)e;
562
583
                                const Message &m = me->message();
563
584
                                if(m.body().indexOf(r->findStr, 0, Qt::CaseInsensitive) != -1) {
564
 
                                        EDBItem *ei = new EDBItem(e, QString::number(id), prevId, nextId);
565
 
                                        result->append(ei);
566
 
                                        break;
 
585
                                        EDBItemPtr ei = EDBItemPtr(new EDBItem(e, QString::number(id), prevId, nextId));
 
586
                                        result.append(ei);
 
587
                                        matched = true;
 
588
                                        //commented line below to return ALL(instead of just first) messages that contain findStr
 
589
                                        //break;
567
590
                                }
568
591
                        }
 
592
                        if (!matched)
 
593
                                delete e;
569
594
 
570
595
                        if(r->dir == Forward)
571
596
                                ++id;
574
599
                }
575
600
                resultReady(r->id, result);
576
601
        }
 
602
        else if(type == item_file_req::Type_getByDate ) {
 
603
                int id = 0;
 
604
                EDBResult result;
 
605
                for (int i=0; i < f->total(); ++i) {
 
606
                        PsiEvent *e = f->get(id);
 
607
                        if(!e)
 
608
                                continue;
 
609
 
 
610
                        QString prevId, nextId;
 
611
                        if(id > 0)
 
612
                                prevId = QString::number(id-1);
 
613
                        if(id < f->total()-1)
 
614
                                nextId = QString::number(id+1);
 
615
 
 
616
                        bool matched = false;
 
617
                        if(e->type() == PsiEvent::Message) {
 
618
                                MessageEvent *me = (MessageEvent *)e;
 
619
                                const Message &m = me->message();
 
620
                                if(m.timeStamp() > r->first && m.timeStamp() < r->last ) {
 
621
                                        EDBItemPtr ei = EDBItemPtr(new EDBItem(e, QString::number(id), prevId, nextId));
 
622
                                        result.append(ei);
 
623
                                        matched = true;
 
624
                                }
 
625
                        }
 
626
                        if (!matched)
 
627
                                delete e;
 
628
 
 
629
                        ++id;
 
630
                }
 
631
                resultReady(r->id, result);
 
632
        }
 
633
 
577
634
        else if(type == item_file_req::Type_erase) {
578
635
                writeFinished(r->id, deleteFile(f->j));
579
636
        }
700
757
                return 0;
701
758
 
702
759
        ensureIndex();
703
 
        if(id < 0 || id > (int)d->index.size())
 
760
        if(id < 0 || id >= (int)d->index.size())
704
761
                return 0;
705
762
 
706
763
        f.seek(d->index[id]);