~phablet-team/history-service/optimize_dbus_traffic

« back to all changes in this revision

Viewing changes to Ubuntu/History/historymodel.cpp

  • Committer: Gustavo Pichorim Boiko
  • Date: 2017-02-16 20:11:04 UTC
  • Revision ID: gustavo.boiko@canonical.com-20170216201104-1rycc1a203ei5941
Move the mark{Event,Threads}AsRead to HistoryModel so that all models can use it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
HistoryModel::HistoryModel(QObject *parent) :
36
36
    QAbstractListModel(parent), mFilter(0), mSort(new HistoryQmlSort(this)),
37
 
    mType(EventTypeText), mMatchContacts(false), mUpdateTimer(0), mWaitingForQml(false)
 
37
    mType(EventTypeText), mMatchContacts(false), mUpdateTimer(0), mEventWritingTimer(0), mWaitingForQml(false)
38
38
{
39
39
    // configure the roles
40
40
    mRoles[AccountIdRole] = "accountId";
406
406
 
407
407
void HistoryModel::timerEvent(QTimerEvent *event)
408
408
{
409
 
    if (event->timerId() == mUpdateTimer && !mWaitingForQml) {
410
 
        killTimer(mUpdateTimer);
411
 
        mUpdateTimer = 0;
412
 
        updateQuery();
 
409
    if (event->timerId() == mUpdateTimer) {
 
410
        if (!mWaitingForQml) {
 
411
            killTimer(mUpdateTimer);
 
412
            mUpdateTimer = 0;
 
413
            updateQuery();
 
414
        }
 
415
    } else if (event->timerId() == mEventWritingTimer) {
 
416
        killTimer(mEventWritingTimer);
 
417
        mEventWritingTimer = 0;
 
418
 
 
419
        if (mEventWritingQueue.isEmpty()) {
 
420
            return;
 
421
        }
 
422
 
 
423
        qDebug() << "Goint to update" << mEventWritingQueue.count() << "events.";
 
424
        if (History::Manager::instance()->writeEvents(mEventWritingQueue)) {
 
425
            qDebug() << "... succeeded!";
 
426
            mEventWritingQueue.clear();
 
427
        }
413
428
    }
414
429
}
415
430
 
478
493
    return data;
479
494
}
480
495
 
 
496
bool HistoryModel::markEventAsRead(const QString &accountId, const QString &threadId, const QString &eventId, int eventType)
 
497
{
 
498
    History::Event event = History::Manager::instance()->getSingleEvent((History::EventType)eventType, accountId, threadId, eventId);
 
499
    event.setNewEvent(false);
 
500
    if (event.type() == History::EventTypeText) {
 
501
        History::TextEvent textEvent = event;
 
502
        textEvent.setReadTimestamp(QDateTime::currentDateTime());
 
503
        event = textEvent;
 
504
    }
 
505
    mEventWritingQueue << event;
 
506
    if (mEventWritingTimer != 0) {
 
507
        killTimer(mEventWritingTimer);
 
508
    }
 
509
    mEventWritingTimer  = startTimer(500);
 
510
    return true;
 
511
}
 
512
 
 
513
void HistoryModel::markThreadsAsRead(const QVariantList &threadsProperties)
 
514
{
 
515
    History::Threads threads;
 
516
    Q_FOREACH(const QVariant &entry, threadsProperties) {
 
517
        QVariantMap threadProperties = entry.toMap();
 
518
        History::Thread thread = History::Thread::fromProperties(threadProperties);
 
519
 
 
520
        if (!thread.isNull()) {
 
521
            threads << thread;
 
522
        }
 
523
    }
 
524
 
 
525
    if (threads.isEmpty()) {
 
526
        return;
 
527
    }
 
528
 
 
529
    History::Manager::instance()->markThreadsAsRead(threads);
 
530
}
 
531
 
481
532
void HistoryModel::classBegin()
482
533
{
483
534
    mWaitingForQml = true;