~ci-train-bot/history-service/history-service-ubuntu-yakkety-landing-052

« back to all changes in this revision

Viewing changes to plugins/sqlite/sqlitehistoryplugin.cpp

Request contact information for all known participants on history-daemon initialization, and use this cached information on the models.
Approved by: Tiago Salem Herrmann, PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include "intersectionfilter.h"
29
29
#include "unionfilter.h"
30
30
#include "thread.h"
 
31
#include "contactmatcher_p.h"
31
32
#include "utils_p.h"
32
33
#include <QDateTime>
33
34
#include <QDebug>
50
51
{
51
52
    // just trigger the database creation or update
52
53
    SQLiteDatabase::instance();
53
 
 
54
 
    updateGroupedThreadsCache();
55
 
 
56
 
    mInitialised = true;
 
54
}
 
55
 
 
56
bool SQLiteHistoryPlugin::initialised()
 
57
{
 
58
    return mInitialised;
57
59
}
58
60
 
59
61
void SQLiteHistoryPlugin::updateGroupedThreadsCache()
106
108
            const QString &conversationKey = it.key();
107
109
            History::Threads groupedThreads = it.value();
108
110
            Q_FOREACH(const History::Thread &groupedThread, groupedThreads) {
109
 
                found = History::Utils::compareNormalizedParticipants(thread.participants(), groupedThread.participants(), History::MatchPhoneNumber);
 
111
                found = History::Utils::compareNormalizedParticipants(thread.participants().identifiers(), groupedThread.participants().identifiers(), History::MatchPhoneNumber);
110
112
                if (found) {
111
113
                    Q_FOREACH(const History::Thread &groupedThread, groupedThreads) {
112
114
                        mConversationsCacheKeys.remove(generateThreadMapKey(groupedThread));
212
214
    }
213
215
}
214
216
 
 
217
/**
 
218
 * @brief Generates the cache containing contact data for all known participants.
 
219
 *
 
220
 * FIXME: this should probably be done outside of the plugin, but it requires a
 
221
 * refactory of \ref HistoryDaemon itself.
 
222
 */
 
223
void SQLiteHistoryPlugin::generateContactCache()
 
224
{
 
225
    QTime time;
 
226
    time.start();
 
227
    qDebug() << "---- HistoryService: start generating cached content";
 
228
    QSqlQuery query(SQLiteDatabase::instance()->database());
 
229
    if (!query.exec("SELECT DISTINCT accountId, normalizedId FROM thread_participants")) {
 
230
        qWarning() << "Failed to generate contact cache:" << query.lastError().text();
 
231
        return;
 
232
    }
 
233
 
 
234
    while (query.next()) {
 
235
        QString accountId = query.value(0).toString();
 
236
        QString participantId = query.value(1).toString();
 
237
        // we don't care about the results, as long as the contact data is present in the cache for
 
238
        // future usage.
 
239
        ContactMatcher::instance()->contactInfo(accountId, participantId, true);
 
240
    }
 
241
 
 
242
    updateGroupedThreadsCache();
 
243
 
 
244
    qDebug() << "---- HistoryService: finished generating contact cache. elapsed time:" << time.elapsed() << "ms";
 
245
 
 
246
    mInitialised = true;
 
247
}
 
248
 
215
249
// Reader
216
250
History::PluginThreadView *SQLiteHistoryPlugin::queryThreads(History::EventType type,
217
251
                                                             const History::Sort &sort,
444
478
    thread[History::FieldAccountId] = accountId;
445
479
    thread[History::FieldThreadId] = threadId;
446
480
    thread[History::FieldType] = (int) type;
447
 
    thread[History::FieldParticipants] = participants;
 
481
    thread[History::FieldParticipants] = ContactMatcher::instance()->contactInfo(accountId, participants, true);
448
482
    thread[History::FieldCount] = 0;
449
483
    thread[History::FieldUnreadCount] = 0;
450
484
 
726
760
    }
727
761
    while (query.next()) {
728
762
        QVariantMap thread;
729
 
        QString accountId = query.value(0).toString();                   
 
763
        QString accountId = query.value(0).toString();
730
764
        QString threadId = query.value(1).toString();
731
765
        if (threadId.trimmed().isEmpty()) {
732
766
            continue;
746
780
                    groupedThreads << thread.properties();
747
781
                }
748
782
            }
749
 
            thread[History::FieldGroupedThreads] = QVariant::fromValue(groupedThreads);
 
783
            thread[History::FieldGroupedThreads] = QVariant::fromValue(groupedThreads);
750
784
        }
751
785
 
752
786
        thread[History::FieldEventId] = query.value(2);
753
787
        thread[History::FieldCount] = query.value(3);
754
788
        thread[History::FieldUnreadCount] = query.value(4);
755
 
        thread[History::FieldParticipants] = query.value(5).toString().split("|,|");
 
789
        QStringList participants = query.value(5).toString().split("|,|");
 
790
        thread[History::FieldParticipants] = ContactMatcher::instance()->contactInfo(accountId, participants, true);
756
791
 
757
792
        // the generic event fields
758
793
        thread[History::FieldSenderId] = query.value(6);
796
831
        case History::EventTypeVoice:
797
832
            thread[History::FieldMissed] = query.value(10);
798
833
            thread[History::FieldDuration] = query.value(9);
799
 
            thread[History::FieldRemoteParticipant] = query.value(11);
 
834
            thread[History::FieldRemoteParticipant] = ContactMatcher::instance()->contactInfo(accountId, query.value(11).toString(), true);
800
835
            break;
801
836
        }
802
837
        threads << thread;
841
876
        QString accountId = query.value(0).toString();
842
877
        QString threadId = query.value(1).toString();
843
878
        QString eventId = query.value(2).toString();
 
879
 
 
880
        // ignore events that don't have a threadId or an eventId
 
881
        if (threadId.trimmed().isEmpty() || eventId.trimmed().isEmpty()) {
 
882
            continue;
 
883
        }
 
884
 
844
885
        event[History::FieldType] = (int) type;
845
886
        event[History::FieldAccountId] = accountId;
846
887
        event[History::FieldThreadId] = threadId;
848
889
        event[History::FieldSenderId] = query.value(3);
849
890
        event[History::FieldTimestamp] = toLocalTimeString(query.value(4).toDateTime());
850
891
        event[History::FieldNewEvent] = query.value(5);
851
 
        event[History::FieldParticipants] = query.value(6).toString().split("|,|");
852
 
 
 
892
        QStringList participants = query.value(6).toString().split("|,|");
 
893
        event[History::FieldParticipants] = ContactMatcher::instance()->contactInfo(accountId, participants, true);
853
894
 
854
895
        switch (type) {
855
896
        case History::EventTypeText:
889
930
        case History::EventTypeVoice:
890
931
            event[History::FieldDuration] = query.value(7).toInt();
891
932
            event[History::FieldMissed] = query.value(8);
892
 
            event[History::FieldRemoteParticipant] = query.value(9);
 
933
            event[History::FieldRemoteParticipant] = query.value(9).toString();
893
934
            break;
894
935
        }
895
936