~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to src/plugins/contacts/jsondb/qcontactjsondbengine.cpp

  • Committer: chris.gagnon
  • Date: 2013-12-10 23:09:37 UTC
  • Revision ID: chris.gagnon@canonical.com-20131210230937-2akf1ft1edcttk87
first post

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtContacts module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
#include <QVariantMap>
 
42
#include <QEventLoop>
 
43
 
 
44
#include "qcontactjsondbengine.h"
 
45
#include "qcontactjsondbconverter.h"
 
46
#include "qcontactjsondbglobal.h"
 
47
#include "qcontactjsondbstring.h"
 
48
#include "qcontactjsondbrequesthandler.h"
 
49
 
 
50
#include <QDebug>
 
51
#include <QEventLoop>
 
52
 
 
53
QT_BEGIN_NAMESPACE_CONTACTS
 
54
 
 
55
/*
 
56
  class QContactJsonDbEngine
 
57
  \brief The QContactJsonDbEngine class provides an implementation of
 
58
  QContactManagerEngine whose functions always return an error.
 
59
 
 
60
  The JsonDb engine.
 
61
 */
 
62
 
 
63
QContactJsonDbEngine::QContactJsonDbEngine(const QMap<QString, QString>& parameters)
 
64
{
 
65
    Q_UNUSED(parameters);
 
66
    m_requestHandler = new QContactJsonDbRequestHandler();
 
67
    qRegisterMetaType<QContactAbstractRequest::State>("QContactAbstractRequest::State");
 
68
    qRegisterMetaType<QList<QContactId> >("QList<QContactId>");
 
69
    qRegisterMetaType<QContactId>("QContactId");
 
70
    m_thread = new QThread();
 
71
    m_thread->start();
 
72
    connect(this, SIGNAL(requestReceived(QContactAbstractRequest*)),
 
73
            m_requestHandler, SLOT(handleRequest(QContactAbstractRequest*)));
 
74
    m_requestHandler->moveToThread(m_thread);
 
75
    QMetaObject::invokeMethod(m_requestHandler,"init",Qt::BlockingQueuedConnection);
 
76
    m_requestHandler->setEngine(this);
 
77
}
 
78
 
 
79
 
 
80
 
 
81
 
 
82
 
 
83
QContactJsonDbEngine::~QContactJsonDbEngine()
 
84
{
 
85
    if (m_requestHandler)
 
86
        m_requestHandler->deleteLater();
 
87
    if (m_thread) {
 
88
        m_thread->quit();
 
89
        m_thread->wait();
 
90
        delete m_thread;
 
91
    }
 
92
 
 
93
}
 
94
 
 
95
 
 
96
 
 
97
 
 
98
 
 
99
 
 
100
bool QContactJsonDbEngine::startRequest(QContactAbstractRequest* req){
 
101
    QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::ActiveState);
 
102
    connect(req, SIGNAL(destroyed(QObject*)), m_requestHandler, SLOT(removeDestroyed(QObject*)),Qt::QueuedConnection);
 
103
    emit requestReceived(req);
 
104
    return true;
 
105
}
 
106
 
 
107
 
 
108
 
 
109
 
 
110
 
 
111
/* \reimp */
 
112
QString QContactJsonDbEngine::managerName() const
 
113
{
 
114
    return QContactJsonDbStr::contactJsonDbEngineName();
 
115
}
 
116
 
 
117
QList<QContactDetail::DetailType> QContactJsonDbEngine::supportedContactDetailTypes() const
 
118
{
 
119
    QList<QContactDetail::DetailType> supportedDetails;
 
120
    supportedDetails << QContactAddress::Type
 
121
                     << QContactAvatar::Type
 
122
                     << QContactBirthday::Type
 
123
                     << QContactDisplayLabel::Type
 
124
                     << QContactEmailAddress::Type
 
125
                     << QContactExtendedDetail::Type
 
126
                     << QContactGender::Type
 
127
                     << QContactGuid::Type
 
128
                     << QContactName::Type
 
129
                     << QContactNickname::Type
 
130
                     << QContactNote::Type
 
131
                     << QContactOrganization::Type
 
132
                     << QContactPhoneNumber::Type
 
133
                     << QContactRingtone::Type
 
134
                     << QContactSyncTarget::Type
 
135
                     << QContactType::Type
 
136
                     << QContactUrl::Type
 
137
                     << QContactVersion::Type;
 
138
    return supportedDetails;
 
139
}
 
140
 
 
141
 
 
142
 
 
143
bool QContactJsonDbEngine::validateContact(const QContact& contact, QContactManager::Error* error) const
 
144
{
 
145
    QContactManagerEngine::validateContact(contact, error);
 
146
    if ((*error == QContactManager::InvalidContactTypeError) || (*error == QContactManager::DoesNotExistError))
 
147
        return false;
 
148
 
 
149
    QList<QContactDetail> contactDetailList = contact.details();
 
150
 
 
151
    for (int i=0; i<contactDetailList.count(); i++)
 
152
    {
 
153
        QContactDetail currentDetail = contactDetailList.value(i);
 
154
        if (!supportedContactDetailTypes().contains(currentDetail.type()))
 
155
        {
 
156
            *error = QContactManager::InvalidDetailError;
 
157
            return false;
 
158
        }
 
159
    }
 
160
 
 
161
    *error = QContactManager::NoError;
 
162
    return true;
 
163
}
 
164
 
 
165
 
 
166
QContactId QContactJsonDbEngine::selfContactId(QContactManager::Error* error) const
 
167
{
 
168
    // TODO: THE IDENTIFICATION FIELD DOES NOT EXIST YET IN JSON SCHEMA!
 
169
    // Just return "NotSupported" error
 
170
    *error = QContactManager::NotSupportedError;
 
171
    return QContactId();
 
172
}
 
173
 
 
174
 
 
175
QList<QContactId> QContactJsonDbEngine::contactIds(const QContactFilter& filter, const QList<QContactSortOrder>& sortOrders, QContactManager::Error* error) const
 
176
{
 
177
    QContactJsonDbConverter converter;
 
178
    QList<QContactId> contactIds;
 
179
    QVariantMap map;
 
180
    QContactFetchRequest request;
 
181
    request.setFilter(filter);
 
182
    request.setSorting(sortOrders);
 
183
    *error = QContactManager::NoError;
 
184
    //QString query = converter.queryFromRequest(&request);
 
185
    doSyncRequest(&request, 5000);
 
186
    *error = request.error();
 
187
    if (*error != QContactManager::NoError) {
 
188
        if (qt_debug_jsondb_contacts())
 
189
            qDebug() << "[QContactJsonDb] Error at " << Q_FUNC_INFO << ":" << *error;
 
190
        return QList<QContactId>();
 
191
    }
 
192
    QList<QContact> queryResults = (QList<QContact>)request.contacts();
 
193
    // found any results?
 
194
    if(queryResults.size() == 0) {
 
195
        *error = QContactManager::DoesNotExistError;
 
196
        qDebug() << "Error by function contactIds: no contacts found (DoesNotExistError)";
 
197
        return QList<QContactId>();
 
198
    }
 
199
    // Convert results for needed format
 
200
    QList<QContactId> results;
 
201
 
 
202
    foreach (const QContact &contact, queryResults)
 
203
        results.append(contact.id());
 
204
 
 
205
    return results;
 
206
}
 
207
 
 
208
 
 
209
 
 
210
 
 
211
 
 
212
QList<QContact> QContactJsonDbEngine::contacts(const QContactFilter & filter, const QList<QContactSortOrder> & sortOrders, const QContactFetchHint & fetchHint, QContactManager::Error* error ) const
 
213
{
 
214
  // TODO: ERROR HANDLING  (?)
 
215
  QList<QContact> contacts;
 
216
  QContactJsonDbConverter converter;
 
217
  QContactFetchRequest fetchReq;
 
218
  fetchReq.setFilter(filter);
 
219
  fetchReq.setSorting(sortOrders);
 
220
  fetchReq.setFetchHint(fetchHint);
 
221
  *error = QContactManager::NoError;
 
222
  //QString query = converter.queryFromRequest(&fetchReq);
 
223
  doSyncRequest(&fetchReq, 5000);
 
224
  *error = fetchReq.error();
 
225
  if (*error != QContactManager::NoError) {
 
226
      if (qt_debug_jsondb_contacts())
 
227
          qDebug() << "[QContactJsonDb] Error at " << Q_FUNC_INFO << ":" << *error;
 
228
      return QList<QContact>();
 
229
  }
 
230
  QList<QContact> queryResults = (QList<QContact>)fetchReq.contacts();
 
231
  // found any results?
 
232
  if(queryResults.size() == 0) {
 
233
      *error = QContactManager::DoesNotExistError;
 
234
      qDebug() << "Error by function contacts: no contacts found (DoesNotExistError)";
 
235
      return QList<QContact>();
 
236
  }
 
237
  /*
 
238
  else {
 
239
        converter.toQContacts(jsonDbObjectList, contacts, *this, *error);
 
240
  }
 
241
  */
 
242
  return queryResults;
 
243
}
 
244
 
 
245
 
 
246
 
 
247
 
 
248
QContact QContactJsonDbEngine::contact(const QContactId& contactId, const QContactFetchHint& fetchHint, QContactManager::Error* error) const
 
249
{
 
250
    QContact contact;
 
251
    QContactJsonDbConverter converter;
 
252
    QContactFetchRequest request;
 
253
    QList<QContactId> filterIds;
 
254
    QContactIdFilter idFilter;
 
255
    QString query;
 
256
    QVariantList results;
 
257
 
 
258
    filterIds.append(contactId);
 
259
    idFilter.setIds(filterIds);
 
260
    request.setFilter(idFilter);
 
261
    request.setFetchHint(fetchHint);
 
262
    *error = QContactManager::NoError;
 
263
 
 
264
    //query = converter.queryFromRequest(&request);
 
265
    doSyncRequest(&request, 5000);
 
266
    *error = request.error();
 
267
    if (*error != QContactManager::NoError) {
 
268
        if (qt_debug_jsondb_contacts())
 
269
            qDebug() << "[QContactJsonDb] Error at " << Q_FUNC_INFO << ":" << *error;
 
270
        return QContact();
 
271
    }
 
272
    QList<QContact> queryResults = (QList<QContact>)request.contacts();
 
273
    // Check if query returned a value and it can be converted
 
274
    if(queryResults.size() == 0) {
 
275
        *error = QContactManager::DoesNotExistError;
 
276
        qDebug() << "Error by function contact: no contact found (DoesNotExistError)";
 
277
        return QContact();
 
278
    }
 
279
 
 
280
    // Extract the desired results
 
281
    foreach (QContact curr, queryResults) {
 
282
        if (curr.id() == contactId) contact = curr;  ;
 
283
    }
 
284
    return contact;
 
285
}
 
286
 
 
287
 
 
288
 
 
289
 
 
290
bool QContactJsonDbEngine::saveContacts(QList<QContact>* contacts, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
 
291
{
 
292
    QContactSaveRequest saveReq;
 
293
 
 
294
    saveReq.setContacts(*contacts);
 
295
    doSyncRequest(&saveReq, 5000);
 
296
    *error = saveReq.error();
 
297
    if (*error != QContactManager::NoError) {
 
298
        if (qt_debug_jsondb_contacts())
 
299
            qDebug() << "[QContactJsonDb] Error at " << Q_FUNC_INFO << ":" << *error;
 
300
    }
 
301
 
 
302
    for (int i = 0; i < saveReq.contacts().size(); i++)
 
303
        contacts->replace(i, saveReq.contacts()[i]);
 
304
    *errorMap = saveReq.errorMap();
 
305
    return *error == QContactManager::NoError;  // No problem detected, return NoError
 
306
}
 
307
 
 
308
 
 
309
 
 
310
 
 
311
 
 
312
bool QContactJsonDbEngine::removeContacts(const QList<QContactId>& ids, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
 
313
{
 
314
    QContactRemoveRequest removeReq;
 
315
    removeReq.setContactIds(ids);
 
316
    doSyncRequest(&removeReq, 5000);
 
317
    *error = removeReq.error();
 
318
    *errorMap = removeReq.errorMap();
 
319
    if (*error != QContactManager::NoError) {
 
320
        qWarning() << "Error at function removeContacts:" << *error;
 
321
        return false;
 
322
    } else {
 
323
        return true;
 
324
    }
 
325
}
 
326
 
 
327
bool QContactJsonDbEngine::saveContact(QContact* contact, QContactManager::Error* error)
 
328
{
 
329
   QContactSaveRequest saveReq;
 
330
   *error = QContactManager::NoError;
 
331
 
 
332
   saveReq.setContact(*contact);
 
333
   doSyncRequest(&saveReq, 5000);
 
334
   *error = saveReq.error();
 
335
   if (*error != QContactManager::NoError) {
 
336
       if (qt_debug_jsondb_contacts())
 
337
           qDebug() << "[QContactJsonDb] Error at " << Q_FUNC_INFO << ":" << *error;
 
338
       return false;
 
339
   }
 
340
   *contact = saveReq.contacts().first();  // Check if this is the desired behavior !!!
 
341
   return *error == QContactManager::NoError;  // No problem detected, return NoError
 
342
}
 
343
 
 
344
 
 
345
 
 
346
bool QContactJsonDbEngine::removeContact(const QContactId& contactId, QContactManager::Error* error)
 
347
{
 
348
    Q_UNUSED(contactId)
 
349
    Q_UNUSED(error)
 
350
 
 
351
    QContactRemoveRequest removeReq;
 
352
    *error = QContactManager::NoError;
 
353
    removeReq.setContactId(contactId);
 
354
    doSyncRequest(&removeReq, 5000);
 
355
    *error = removeReq.error();
 
356
    if (*error != QContactManager::NoError) {
 
357
        if (qt_debug_jsondb_contacts())
 
358
            qDebug() << "[QContactJsonDb] Error at " << Q_FUNC_INFO << ":" << *error;
 
359
        return false;
 
360
    }
 
361
    else return true;
 
362
}
 
363
 
 
364
bool QContactJsonDbEngine::isFilterSupported(const QContactFilter& filter) const
 
365
{
 
366
    switch (filter.type()) {
 
367
    case QContactFilter::ContactDetailFilter: {
 
368
        QContactDetailFilter detailFilter = static_cast<QContactDetailFilter>(filter);
 
369
        int field = detailFilter.detailField();
 
370
        if (field < 0)
 
371
            return false;
 
372
        switch (detailFilter.detailType()) {
 
373
        case QContactDetail::TypeEmailAddress:
 
374
            if (field != QContactEmailAddress::FieldEmailAddress)
 
375
                return false;
 
376
        case QContactDetail::TypePhoneNumber:
 
377
            if (field != QContactPhoneNumber::FieldNumber)
 
378
                return false;
 
379
        case QContactDetail::TypeUrl:
 
380
            if (field != QContactUrl::FieldUrl)
 
381
                return false;
 
382
        case QContactDetail::TypeName:
 
383
            return true;
 
384
        default:
 
385
            return false;
 
386
        };
 
387
        return false;
 
388
    }
 
389
    case QContactFilter::InvalidFilter:
 
390
    case QContactFilter::DefaultFilter:
 
391
    case QContactFilter::IdFilter:
 
392
    case QContactFilter::IntersectionFilter:
 
393
      return true;
 
394
    default:
 
395
      return false;
 
396
  }
 
397
}
 
398
 
 
399
QList<QVariant::Type> QContactJsonDbEngine::supportedDataTypes() const {
 
400
  QList<QVariant::Type> st;
 
401
  st.append(QVariant::String);
 
402
  st.append(QVariant::Int);
 
403
  st.append(QVariant::UInt);
 
404
  st.append(QVariant::Double);
 
405
  st.append(QVariant::Date);
 
406
  st.append(QVariant::DateTime);
 
407
  st.append(QVariant::Bool);
 
408
  st.append(QVariant::Url);
 
409
  return st;
 
410
}
 
411
 
 
412
void QContactJsonDbEngine::requestDestroyed(QContactAbstractRequest* req){
 
413
   //We inform the handler that this request is about to be destroyed so as to
 
414
   //avoid that the worker handler thread will start handling request objects during
 
415
   //their destruction.
 
416
   QMetaObject::invokeMethod(m_requestHandler,"removeDestroyed",Qt::BlockingQueuedConnection,Q_ARG(QObject*, req));
 
417
   return QContactManagerEngine::requestDestroyed(req);
 
418
}
 
419
 
 
420
bool QContactJsonDbEngine::cancelRequest(QContactAbstractRequest* req){
 
421
    /*
 
422
        TODO
 
423
 
 
424
        Cancel an in progress async request.  If not possible, return false from here.
 
425
    */
 
426
    return QContactManagerEngine::cancelRequest(req);
 
427
}
 
428
 
 
429
bool QContactJsonDbEngine::waitForRequestProgress(QContactAbstractRequest* req, int msecs){
 
430
  Q_UNUSED(msecs);
 
431
  Q_UNUSED(req);
 
432
  //TODO: can we get progress info from jsondb??
 
433
 
 
434
  return true;
 
435
}
 
436
 
 
437
bool QContactJsonDbEngine::waitForRequestFinished(QContactAbstractRequest* req, int msecs){
 
438
    bool result = false;
 
439
    result = m_requestHandler->waitForRequestFinished(req, msecs);
 
440
    return result;
 
441
}
 
442
 
 
443
bool QContactJsonDbEngine::doSyncRequest(QContactAbstractRequest* req, int msecs) const  {
 
444
    Q_UNUSED(msecs); // TODO
 
445
    //if (req->ContactFetchRequest)
 
446
    const_cast<QContactJsonDbEngine*>(this)->startRequest(req);
 
447
    const_cast<QContactJsonDbEngine*>(this)->waitForRequestFinished(req, 0);
 
448
    //if (req->FinishedState)
 
449
    if (req->isFinished() == true)
 
450
        return true;
 
451
    else
 
452
        return false;
 
453
}
 
454
 
 
455
 
 
456
/* Internal, for debugging */
 
457
bool qt_debug_jsondb_contacts()
 
458
{
 
459
    static int debug_env = -1;
 
460
    if (debug_env == -1)
 
461
       debug_env = QT_PREPEND_NAMESPACE(qgetenv)("QT_DEBUG_JSONDB_CONTACTS").toInt();
 
462
 
 
463
    return debug_env != 0;
 
464
}
 
465
 
 
466
#include "moc_qcontactjsondbengine.cpp"
 
467
 
 
468
QT_END_NAMESPACE_CONTACTS