~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:29:48 UTC
  • Revision ID: gustavo.boiko@canonical.com-20170216202948-vyp1uhtqo5y41k38
Buffer marking threads as read and get rid of duplicate calls.
Also, change the markEventAsRead to not fetch the event from the daemon.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include "textevent.h"
29
29
#include "manager.h"
30
30
#include "utils_p.h"
 
31
#include "voiceevent.h"
31
32
#include <QTimerEvent>
32
33
#include <QCryptographicHash>
33
34
#include <QDebug>
34
35
 
35
36
HistoryModel::HistoryModel(QObject *parent) :
36
37
    QAbstractListModel(parent), mFilter(0), mSort(new HistoryQmlSort(this)),
37
 
    mType(EventTypeText), mMatchContacts(false), mUpdateTimer(0), mEventWritingTimer(0), mWaitingForQml(false)
 
38
    mType(EventTypeText), mMatchContacts(false), mUpdateTimer(0), mEventWritingTimer(0), mThreadWritingTimer(0), mWaitingForQml(false)
38
39
{
39
40
    // configure the roles
40
41
    mRoles[AccountIdRole] = "accountId";
425
426
            qDebug() << "... succeeded!";
426
427
            mEventWritingQueue.clear();
427
428
        }
 
429
    } else if (event->timerId() == mThreadWritingTimer) {
 
430
        killTimer(mThreadWritingTimer);
 
431
        mThreadWritingTimer = 0;
 
432
 
 
433
        if (mThreadWritingQueue.isEmpty()) {
 
434
            return;
 
435
        }
 
436
 
 
437
        History::Manager::instance()->markThreadsAsRead(mThreadWritingQueue);
 
438
        mThreadWritingQueue.clear();
428
439
    }
429
440
}
430
441
 
493
504
    return data;
494
505
}
495
506
 
496
 
bool HistoryModel::markEventAsRead(const QString &accountId, const QString &threadId, const QString &eventId, int eventType)
 
507
bool HistoryModel::markEventAsRead(const QVariantMap &eventProperties)
497
508
{
498
 
    History::Event event = History::Manager::instance()->getSingleEvent((History::EventType)eventType, accountId, threadId, eventId);
 
509
    History::Event event;
 
510
    History::EventType type = (History::EventType) eventProperties[History::FieldType].toInt();
 
511
    switch (type) {
 
512
    case History::EventTypeText:
 
513
        event = History::TextEvent::fromProperties(eventProperties);
 
514
        break;
 
515
    case History::EventTypeVoice:
 
516
        event = History::VoiceEvent::fromProperties(eventProperties);
 
517
        break;
 
518
    }
 
519
 
499
520
    event.setNewEvent(false);
500
521
    if (event.type() == History::EventTypeText) {
501
522
        History::TextEvent textEvent = event;
502
523
        textEvent.setReadTimestamp(QDateTime::currentDateTime());
503
524
        event = textEvent;
504
525
    }
 
526
    // for repeated events, keep the last called one only
 
527
    if (mEventWritingQueue.contains(event)) {
 
528
        mEventWritingQueue.removeOne(event);
 
529
    }
505
530
    mEventWritingQueue << event;
506
531
    if (mEventWritingTimer != 0) {
507
532
        killTimer(mEventWritingTimer);
508
533
    }
509
 
    mEventWritingTimer  = startTimer(500);
 
534
    mEventWritingTimer = startTimer(500);
510
535
    return true;
511
536
}
512
537
 
513
538
void HistoryModel::markThreadsAsRead(const QVariantList &threadsProperties)
514
539
{
515
 
    History::Threads threads;
516
540
    Q_FOREACH(const QVariant &entry, threadsProperties) {
517
541
        QVariantMap threadProperties = entry.toMap();
518
542
        History::Thread thread = History::Thread::fromProperties(threadProperties);
519
543
 
520
544
        if (!thread.isNull()) {
521
 
            threads << thread;
 
545
            if (mThreadWritingQueue.contains(thread)) {
 
546
                mThreadWritingQueue.removeOne(thread);
 
547
            }
 
548
            mThreadWritingQueue << thread;
522
549
        }
523
550
    }
524
551
 
525
 
    if (threads.isEmpty()) {
526
 
        return;
 
552
    if (mThreadWritingTimer != 0) {
 
553
        killTimer(mThreadWritingTimer);
527
554
    }
528
 
 
529
 
    History::Manager::instance()->markThreadsAsRead(threads);
 
555
    mThreadWritingTimer = startTimer(500);
530
556
}
531
557
 
532
558
void HistoryModel::classBegin()