File: cdbirthdaycontroller.cpp

package info (click to toggle)
contactsd 1.4.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,392 kB
  • sloc: cpp: 8,862; ansic: 3,793; sh: 361; xml: 75; python: 22; makefile: 13
file content (343 lines) | stat: -rw-r--r-- 12,698 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/** This file is part of Contacts daemon
 **
 ** Copyright (c) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
 ** Copyright (c) 2012 - 2019 Jolla Ltd.
 ** Copyright (c) 2020 Open Mobile Platform LLC.
 **
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser General Public License
 ** version 2.1 as published by the Free Software Foundation and appearing in the
 ** file LICENSE.LGPL included in the packaging of this file.  Please review the
 ** following information to ensure the GNU Lesser General Public License version
 ** 2.1 requirements will be met:
 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional rights.
 ** These rights are described in the Nokia Qt LGPL Exception version 1.1, included
 ** in the file LGPL_EXCEPTION.txt in this package.
 **
 ** Other Usage
 ** Alternatively, this file may be used in accordance with the terms and
 ** conditions contained in a signed written agreement between you and Nokia.
 **/

#include "cdbirthdaycontroller.h"
#include "cdbirthdaycalendar.h"
#include "cdbirthdayplugin.h"
#include "debug.h"

#include <QDir>
#include <QFile>

#include <QContactBirthday>
#include <QContactDetailFilter>
#include <QContactFetchRequest>
#include <QContactIdFilter>
#include <QContactDisplayLabel>
#include <QContactCollectionFilter>

#include <qtcontacts-extensions.h>
#include <qtcontacts-extensions_impl.h>

QTCONTACTS_USE_NAMESPACE

using namespace Contactsd;

namespace {

// version number for current type of birthday events. Increase number when changing event details.
const int CURRENT_BIRTHDAY_VERSION = 1;
const int UPDATE_TIMEOUT = 1000; // ms

template<typename DetailType>
QContactDetailFilter detailFilter(int field = -1)
{
    QContactDetailFilter filter;
    filter.setDetailType(DetailType::Type, field);
    return filter;
}

QMap<QString, QString> managerParameters()
{
    // We don't need to handle presence changes, so report them separately and ignore them
    QMap<QString, QString> parameters;
    parameters.insert(QString::fromLatin1("mergePresenceChanges"), QString::fromLatin1("false"));
    return parameters;
}

}

CDBirthdayController::CDBirthdayController(QObject *parent)
    : QObject(parent)
    , mCalendar(stampFileUpToDate() ? CDBirthdayCalendar::KeepOldDB : CDBirthdayCalendar::DropOldDB)
    , mManager(QStringLiteral("org.nemomobile.contacts.sqlite"), managerParameters())
    , mRequest(new QContactFetchRequest)
    , mSyncMode(Incremental)
    , mUpdateAllPending(false)
{
    connect(&mManager, &QContactManager::contactsAdded,
            this, &CDBirthdayController::contactsChanged);
    connect(&mManager, &QContactManager::contactsChanged,
            this, &CDBirthdayController::contactsChanged);
    connect(&mManager, &QContactManager::contactsRemoved,
            this, &CDBirthdayController::contactsRemoved);

    connect(&mManager, SIGNAL(dataChanged()), SLOT(updateAllBirthdays()));

    updateAllBirthdays();

    mUpdateTimer.setInterval(UPDATE_TIMEOUT);
    mUpdateTimer.setSingleShot(true);
    connect(&mUpdateTimer, SIGNAL(timeout()), SLOT(onUpdateQueueTimeout()));
}

CDBirthdayController::~CDBirthdayController()
{
}

void CDBirthdayController::contactsChanged(const QList<QContactId>& contacts)
{
    foreach (const QContactId &id, contacts)
        mUpdatedContacts.insert(id);

    // Just restart the timer - if it doesn't expire, we can afford to wait
    mUpdateTimer.start();
}

void CDBirthdayController::contactsRemoved(const QList<QContactId>& contacts)
{
    foreach (const QContactId &id, contacts)
        mCalendar.deleteBirthday(id);
    mCalendar.save();
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Full sync logic
///////////////////////////////////////////////////////////////////////////////////////////////////

bool
CDBirthdayController::stampFileUpToDate()
{
    QFile cacheFile(stampFilePath());
    if (!cacheFile.exists()) {
        return false;
    }

    if (!cacheFile.open(QIODevice::ReadOnly)) {
        return false;
    }

    QByteArray content = cacheFile.read(100);
    bool ok;
    int value = content.toInt(&ok);
    return (ok && value == CURRENT_BIRTHDAY_VERSION);
}

void
CDBirthdayController::createStampFile()
{
    QFile cacheFile(stampFilePath());

    if (not cacheFile.open(QIODevice::WriteOnly)) {
        qCWarning(lcContactsd) << Q_FUNC_INFO << "Unable to create birthday plugin stamp file "
                  << cacheFile.fileName() << " with error " << cacheFile.errorString();
    }

    cacheFile.write(QByteArray::number(CURRENT_BIRTHDAY_VERSION));
}

QString
CDBirthdayController::stampFilePath()
{
    return BasePlugin::cacheFileName(QLatin1String("calendar.stamp"));
}

void
CDBirthdayController::updateAllBirthdays()
{
    if (mRequest->isActive()) {
        mUpdateAllPending = true;
    } else {
        // Fetch every contact with a birthday.
        fetchContacts(detailFilter<QContactBirthday>(), FullSync);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Incremental sync logic
///////////////////////////////////////////////////////////////////////////////////////////////////

void
CDBirthdayController::onUpdateQueueTimeout()
{
    if (mRequest->isActive()) {
        // The timer will be restarted by completion of the active request
        return;
    }

    QList<QContactId> contactIds(mUpdatedContacts.toList());

    // If we request too many contact IDs, we will exceed the SQLite bound variable limit
    const int batchSize = 200;
    if (contactIds.count() > batchSize) {
        mUpdatedContacts = contactIds.mid(batchSize).toSet();
        contactIds = contactIds.mid(0, batchSize);
    } else {
        mUpdatedContacts.clear();
    }

    QContactIdFilter fetchFilter;
    fetchFilter.setIds(contactIds);

    fetchContacts(fetchFilter, Incremental);
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Common sync logic
///////////////////////////////////////////////////////////////////////////////////////////////////

void
CDBirthdayController::fetchContacts(const QContactFilter &filter, SyncMode mode)
{
    // Set up the fetch request object
    mRequest->setManager(&mManager);

    QContactFetchHint fetchHint;
    fetchHint.setDetailTypesHint(QList<QContactDetail::DetailType>() << QContactBirthday::Type << QContactDisplayLabel::Type);
    fetchHint.setOptimizationHints(QContactFetchHint::NoRelationships |
                                   QContactFetchHint::NoActionPreferences |
                                   QContactFetchHint::NoBinaryBlobs);
    mRequest->setFetchHint(fetchHint);

    // Only fetch aggregate contacts
    QContactCollectionFilter aggregateFilter;
    aggregateFilter.setCollectionId(QtContactsSqliteExtensions::aggregateCollectionId(mManager.managerUri()));
    mRequest->setFilter(filter & aggregateFilter);

    connect(mRequest.data(), SIGNAL(stateChanged(QContactAbstractRequest::State)),
            SLOT(onRequestStateChanged(QContactAbstractRequest::State)));

    if (!mRequest->start()) {
        qCWarning(lcContactsd) << Q_FUNC_INFO << "Unable to start birthday contact fetch request";
    } else {
        qCDebug(lcContactsd) << "Birthday contacts fetch request started";
        mSyncMode = mode;
    }
}

void
CDBirthdayController::onRequestStateChanged(QContactAbstractRequest::State newState)
{
    if (newState == QContactAbstractRequest::FinishedState) {
        qCDebug(lcContactsd) << "Birthday contacts fetch request finished";

        if (mRequest->error() != QContactManager::NoError) {
            qCWarning(lcContactsd) << Q_FUNC_INFO << "Error during birthday contact fetch request, code:" << mRequest->error();
        } else {
            if (mSyncMode == FullSync) {
                syncBirthdays(mRequest->contacts());

                // Create the stamp file only after a successful full sync.
                createStampFile();
            } else {
                updateBirthdays(mRequest->contacts());
            }
        }

        // We're finished with this request, clear it out to drop any contact data
        // (although don't delete it directly, as we're currently handling a signal from it)
        mRequest.take()->deleteLater();
        mRequest.reset(new QContactFetchRequest);
    } else if (newState == QContactAbstractRequest::CanceledState) {
        qCDebug(lcContactsd) << "Birthday contacts fetch request canceled";
    } else {
        // Request still in progress
        return;
    }

    // Save the calendar in any case (success or not), since this "save" call
    // also applies for the deleteBirthday() calls in processNotificationQueues()
    mCalendar.save();

    if (mUpdateAllPending) {
        // We need to update all birthdays
        mUpdateAllPending = false;
        updateAllBirthdays();
    } else if (!mUpdatedContacts.isEmpty() && !mUpdateTimer.isActive()) {
        // If some updated contacts weren't requested, we need to go again
        mUpdateTimer.start();
    }
}

void
CDBirthdayController::updateBirthdays(const QList<QContact> &changedBirthdays)
{
    foreach (const QContact &contact, changedBirthdays) {
        const QContactBirthday contactBirthday = contact.detail<QContactBirthday>();
        const QString contactDisplayLabel = contact.detail<QContactDisplayLabel>().label();
        const CalendarBirthday calendarBirthday = mCalendar.birthday(contact.id());

        // Display label or birthdate was removed from the contact, so delete it from the calendar.
        if (contactDisplayLabel.isEmpty() || contactBirthday.date().isNull()) {
            if (!calendarBirthday.date().isNull()) {
                qCDebug(lcContactsd) << "Contact: " << contact.id() << " removed birthday or displayLabel, so delete the calendar event";

                mCalendar.deleteBirthday(contact.id());
            }
        // Display label or birthdate was changed on the contact, so update the calendar.
        } else if ((contactDisplayLabel != calendarBirthday.summary()) ||
                   (contactBirthday.date() != calendarBirthday.date())) {
            qCDebug(lcContactsd) << "Contact with calendar birthday: " << calendarBirthday.date()
                    << " and calendar displayLabel: " << calendarBirthday.summary()
                    << " changed details to: " << contactBirthday.date() << contactDisplayLabel
                    << ", so update the calendar event";

            mCalendar.updateBirthday(contact);
        }
    }
}

void
CDBirthdayController::syncBirthdays(const QList<QContact> &birthdayContacts)
{
    QHash<QContactId, CalendarBirthday> oldBirthdays = mCalendar.birthdays();

    // Check all birthdays from the contacts if the stored calendar item is up-to-date
    foreach (const QContact &contact, birthdayContacts) {
        const QString contactDisplayLabel = contact.detail<QContactDisplayLabel>().label();
        if (contactDisplayLabel.isEmpty()) {
            qCDebug(lcContactsd) << "Contact: " << contact << " has no displayLabel, so not syncing to calendar";
            continue;
        }

        QHash<QContactId, CalendarBirthday>::Iterator it = oldBirthdays.find(contact.id());

        if (oldBirthdays.end() != it) {
            const QContactBirthday contactBirthday = contact.detail<QContactBirthday>();
            const CalendarBirthday &calendarBirthday = *it;

            // Display label or birthdate was changed on the contact, so update the calendar.
            if ((contactDisplayLabel != calendarBirthday.summary()) ||
                (contactBirthday.date() != calendarBirthday.date())) {
                qCDebug(lcContactsd) << "Contact with calendar birthday: " << contactBirthday.date()
                        << " and calendar displayLabel: " << calendarBirthday.summary()
                        << " changed details to: " << contact << ", so update the calendar event";

                mCalendar.updateBirthday(contact);
            }

            // Birthday exists, so not a garbage one
            oldBirthdays.erase(it);
        } else {
            // Create new birthday
            mCalendar.updateBirthday(contact);
        }
    }

    // Remaining old birthdays in the calendar db do not did not match any contact, so remove them.
    foreach (const QContactId &id, oldBirthdays.keys()) {
        qCDebug(lcContactsd) << "Birthday with contact id" << id << "no longer has a matching contact, trashing it";
        mCalendar.deleteBirthday(id);
    }
}