summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/wince/qcontactwincebackend.cpp
blob: dd25e8211f473e88a00a6912ff11ebdcea88d02c (plain)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QMutex>
#include <QDebug>
#include "qcontactmanager.h"
#include "qcontactchangeset.h"
#include "qtcontacts.h"

#define INITGUID
#include "qcontactwincebackend_p.h"

/*
 * This file is most of the engine plumbing - conversion to/from POOM is in
 * the contactconversions.cpp file.
 */

/*
 * TODOS
 *
 * - refactor (much needed)
 *  - single properties should have common code
 *  - error reporting when we run out of room
 * - use QScopedPointer
 * - pronunciation (needs schema update)
 * - any XXX comments
 * - store extra metadata
 *  - Voice subtype for phone, don't always assume
 * - Address formatting - PIMPR_HOME_ADDRESS seems to be read only
 */


QContactWinCEEngine::QContactWinCEEngine(ContactWinceFactory* factory, const QString& engineName, const QMap<QString, QString>& , QContactManager::Error* error)
    : d(new QContactWinCEEngineData)
{
    *error = QContactManager::NoError;
    
    buildHashForContactDetailToPoomPropId();
    d->m_engineName = engineName;
    d->m_factory = factory;
    if (SUCCEEDED(d->m_cominit.hr())) {
        if (SUCCEEDED(CoCreateInstance(CLSID_Application, NULL,
                                       CLSCTX_INPROC_SERVER, IID_IPOutlookApp2,
                                       reinterpret_cast<void **>(&d->m_app)))) {
            if(FAILED(d->m_app->Logon(NULL))) {
                qWarning() << "Failed to log on";
                d->m_app = 0;
            } else {
                if(SUCCEEDED(d->m_app->GetDefaultFolder(olFolderContacts, &d->m_folder))) {
                    if(SUCCEEDED(d->m_folder->get_Items(&d->m_collection))) {
                        // Register/retrieve our custom ids
                        LPCWSTR customIds[4] = { L"QTCONTACTS_PHONE_META", L"QTCONTACTS_EMAIL_META", L"QTCONTACTS_AVATAR_IMAGE_META",  L"QTCONTACTS_AVATAR_VIDEO_META" };
                        CEPROPID outIds[4];

                        if (SUCCEEDED(d->m_app->GetIDsFromNames(4, customIds, PIM_CREATE | CEVT_LPWSTR, outIds))) {
                            d->m_phonemeta = outIds[0];
                            d->m_emailmeta = outIds[1];
                            d->m_avatarImageMeta = outIds[2];
                            d->m_avatarVideoMeta = outIds[3];
                        }

                        // get an IPOLItems2 pointer for the collection, too
                        if (SUCCEEDED(d->m_collection->QueryInterface<IPOlItems2>(&d->m_items2))) {
                            d->m_ids = convertP2QIdList(d->m_collection);
                        }
                    } else {
                        qWarning() << "Failed to get items";
                        d->m_collection = 0;
                    }
                } else {
                    qWarning() << "Failed to get contacts folder";
                    d->m_folder = 0;
                }
            }
        }
    }
    d->m_requestWorker.start();
}

QContactWinCEEngine::QContactWinCEEngine(const QContactWinCEEngine& other)
    : QContactManagerEngine(), d(other.d)
{

}

QContactWinCEEngine& QContactWinCEEngine::operator=(const QContactWinCEEngine& other)
{
    // assign
    d = other.d;

    return *this;
}

QContactWinCEEngine::~QContactWinCEEngine()
{
    if (d->m_app) {
        d->m_app->Logoff();
    }
    d->m_factory->resetEngine();
}

QString QContactWinCEEngine::managerName() const
{
    return d->m_engineName;
}

QContact QContactWinCEEngine::contact(const QContactLocalId& contactId, const QtMobility::QContactFetchHint& hint, QContactManager::Error* error) const
{
    // TODO: implementation for definitionRestrictions!
    Q_UNUSED(hint);
    QContact ret;

    // id == 0 gives a bad argument error from POOM, so don't even try it
    if (contactId != 0) {
        // Fetch!
        SimpleComPointer<IItem> item = 0;
        HRESULT hr = d->m_app->GetItemFromOidEx(contactId, 0, &item);
        if (SUCCEEDED(hr)) {
            if (item) {
                *error = QContactManager::NoError;
                ret = convertToQContact(item);
            } else {
                *error = QContactManager::DoesNotExistError;
            }
        } else {
            if (HRESULT_CODE(hr) == ERROR_NOT_FOUND) {
                *error = QContactManager::DoesNotExistError;
            } else {
                qWarning() << "Failed to retrieve contact:" << HRESULT_CODE(hr);
                *error = QContactManager::UnspecifiedError;
            }
        }
    } else {
        *error = QContactManager::DoesNotExistError;
    }
    return ret;
}

bool QContactWinCEEngine::saveContact(QContact* contact, QContactManager::Error* error)
{
    if (contact == 0) {
        *error = QContactManager::BadArgumentError;
        return false;
    }

    QContactChangeSet cs;

    // ensure that the contact's details conform to their definitions
    if (!validateContact(*contact, error)) {
        *error = QContactManager::InvalidDetailError;
        return false;
    }

    SimpleComPointer<IItem> icontact;
    bool wasOld = false;
    // Figure out if this is a new or old contact
    if (d->m_ids.contains(contact->localId())) {
        // update existing contact
        HRESULT hr = d->m_app->GetItemFromOidEx(contact->localId(), 0, &icontact);
        if (SUCCEEDED(hr)) {
            wasOld = true;
        } else {
            if (HRESULT_CODE(hr) == ERROR_NOT_FOUND) {
                // Well, doesn't exist any more
                *error = QContactManager::DoesNotExistError;
                d->m_ids.removeAll(contact->localId());
            } else {
                qWarning() << "Didn't get old contact" << HRESULT_CODE(hr);
                *error = QContactManager::UnspecifiedError;
            }
        }
    } else if (contact->localId() == 0) {
        // new contact!
        SimpleComPointer<IDispatch> idisp = 0;
        HRESULT hr = d->m_collection->Add(&idisp);
        if (SUCCEEDED(hr)) {
            // now get an actual item out of it (was IContact, which is not IItem)
            hr = idisp->QueryInterface<IItem>(&icontact);

            if (SUCCEEDED(hr)) {
            } else {
                qWarning() << "Failed to query interface" << HRESULT_CODE(hr);
                *error = QContactManager::UnspecifiedError;
            }
        } else {
            qWarning() << "Failed to create contact: "<< HRESULT_CODE(hr);
            *error = QContactManager::OutOfMemoryError;
        }
    } else {
        // Saving a contact with a non zero id, but that doesn't exist
        *error = QContactManager::DoesNotExistError;
    }

    if (icontact) {
        // Convert our QContact to the Icontact (via setProps)
        if (convertFromQContact(*contact, icontact, *error)) {
            HRESULT hr = icontact->Save();
            if (SUCCEEDED(hr)) {
                // yay! we also need to set the new contact id
                long oid = 0;
                hr = icontact->get_Oid(&oid);
                if (SUCCEEDED(hr)) {
                    *error = QContactManager::NoError;
                    QContact c = this->contact((QContactLocalId)oid, QContactFetchHint(), error);
                    
                    if (*error == QContactManager::NoError) {
                        *contact = c;
                        if (wasOld) {
                            cs.insertChangedContact(contact->localId());
                        } else {
                            cs.insertAddedContact(contact->localId());
                            d->m_ids.append(contact->localId());
                        }
                    }

                    cs.emitSignals(this);
                    return true;
                }
                qWarning() << "Saved contact, but couldn't retrieve id again??" << HRESULT_CODE(hr);
                // Blargh.
                *error = QContactManager::UnspecifiedError;
            } else {
                qWarning() << "Failed to save contact" << HRESULT_CODE(hr);
            }
        } else {
            qWarning() << "Failed to convert contact";
        }
    }

    // error should have been set.
    return false;
}

bool QContactWinCEEngine::removeContact(const QContactLocalId& contactId, QContactManager::Error* error)
{
    // Fetch an IItem* for this
    if (contactId != 0) {
        SimpleComPointer<IItem> item ;
        QContactChangeSet cs;

        HRESULT hr = d->m_app->GetItemFromOidEx(contactId, 0, &item);
        if (SUCCEEDED(hr)) {
            hr = item->Delete();
            if (SUCCEEDED(hr)) {
                *error = QContactManager::NoError;
                d->m_ids.removeAll(contactId);
                cs.insertRemovedContact(contactId);
                cs.emitSignals(this);
                return true;
            }
            qWarning() << "Failed to delete:" << HRESULT_CODE(hr);
            *error = QContactManager::UnspecifiedError;
        } else {
            if (HRESULT_CODE(hr) == ERROR_NOT_FOUND) {
                *error = QContactManager::DoesNotExistError;
            } else {
                qWarning() << "Failed to retrieve item pointer in delete" << HRESULT_CODE(hr);
                *error = QContactManager::UnspecifiedError;
            }
        }
    } else {
        // Id 0 does not exist
        *error = QContactManager::DoesNotExistError;
    }
    return false;
}

QMap<QString, QContactDetailDefinition> QContactWinCEEngine::detailDefinitions(const QString& contactType, QContactManager::Error* error) const
{
    *error = QContactManager::NoError;
    QMap<QString, QMap<QString, QContactDetailDefinition> > defns = QContactManagerEngine::schemaDefinitions();

    // Remove the details we don't support
    defns[contactType].remove(QContactSyncTarget::DefinitionName);
    defns[contactType].remove(QContactGeoLocation::DefinitionName);
    defns[contactType].remove(QContactTimestamp::DefinitionName);
    defns[contactType].remove(QContactGuid::DefinitionName);
    defns[contactType].remove(QContactGender::DefinitionName); // ? Surprising

    QMap<QString, QContactDetailFieldDefinition> fields = defns[contactType][QContactAnniversary::DefinitionName].fields();
    // Simple anniversarys
    fields.remove(QContactAnniversary::FieldCalendarId);
    fields.remove(QContactAnniversary::FieldEvent);
    fields.remove(QContactAnniversary::FieldSubType);
    defns[contactType][QContactAnniversary::DefinitionName].setFields(fields);

    // No logo for organisation
    fields = defns[contactType][QContactOrganization::DefinitionName].fields();
    fields.remove(QContactOrganization::FieldLogoUrl);
    defns[contactType][QContactOrganization::DefinitionName].setFields(fields);

    // No subtypes for these details
    fields = defns[contactType][QContactUrl::DefinitionName].fields();
    fields.remove(QContactUrl::FieldSubType);
    defns[contactType][QContactUrl::DefinitionName].setFields(fields);

    // No contexts for these details
    fields =  defns[contactType][QContactAvatar::DefinitionName].fields();
    fields.remove(QContactDetail::FieldContext);
    defns[contactType][QContactAvatar::DefinitionName].setFields(fields);

    // Simple phone number types (non multiple)
    // defns[QContactPhoneNumber::DefinitionName].fields()[QContactPhoneNumber::FieldSubTypes].dataType = QVariant::String; // XXX doesn't work
    fields = defns[contactType][QContactPhoneNumber::DefinitionName].fields();
    fields[QContactPhoneNumber::FieldSubTypes].allowableValues().removeAll(QString(QLatin1String(QContactPhoneNumber::SubTypeBulletinBoardSystem)));
    fields[QContactPhoneNumber::FieldSubTypes].allowableValues().removeAll(QString(QLatin1String(QContactPhoneNumber::SubTypeLandline)));
    fields[QContactPhoneNumber::FieldSubTypes].allowableValues().removeAll(QString(QLatin1String(QContactPhoneNumber::SubTypeMessagingCapable)));
    fields[QContactPhoneNumber::FieldSubTypes].allowableValues().removeAll(QString(QLatin1String(QContactPhoneNumber::SubTypeModem)));
    fields[QContactPhoneNumber::FieldSubTypes].allowableValues().removeAll(QString(QLatin1String(QContactPhoneNumber::SubTypeVideo)));
    defns[contactType][QContactPhoneNumber::DefinitionName].setFields(fields);
    
    // XXX temporary definitions that we should support but don't yet.
    defns[contactType].remove(QContactOnlineAccount::DefinitionName);

    return defns[contactType];
}


/*! \reimp */
void QContactWinCEEngine::requestDestroyed(QContactAbstractRequest* req)
{
    d->m_requestWorker.removeRequest(req);
}

/*! \reimp */
bool QContactWinCEEngine::startRequest(QContactAbstractRequest* req)
{
    return d->m_requestWorker.addRequest(req);
}

/*! \reimp */
bool QContactWinCEEngine::cancelRequest(QContactAbstractRequest* req)
{
    return  d->m_requestWorker.cancelRequest(req);
}


/*! \reimp */
bool QContactWinCEEngine::waitForRequestFinished(QContactAbstractRequest* req, int msecs)
{
    return d->m_requestWorker.waitRequest(req, msecs) && req->isFinished();
}

/*! \reimp */
bool QContactWinCEEngine::hasFeature(QContactManager::ManagerFeature feature, const QString& contactType) const
{
    Q_UNUSED(contactType);
    // The Windows CE backend is an "isolated" backend
    if (feature == QContactManager::Anonymous)
        return true;

    // Windows CE backend does not support Mutable Definitions, Relationships or Action Preferences
    return false;
}

/* Synthesise the display label of a contact */
QString QContactWinCEEngine::synthesizedDisplayLabel(const QContact& contact, QContactManager::Error* error) const
{
    Q_UNUSED(error)
    // The POOM API (well, lack thereof) makes this a bit strange.
    // It's basically just "Last, First" or "Company", if "FileAs" is not set.
    QContactName name = contact.detail<QContactName>();
    QContactOrganization org = contact.detail<QContactOrganization>();

    // Basically we ignore any existing labels for this contact, since we're being
    // asked what the synthesized label would be

    // XXX For greatest accuracy we might be better off converting this contact to
    // a real item (but don't save it), and then retrieve it...
    if (!name.customLabel().isEmpty()) {
        return name.customLabel();
    }
    else if (!name.lastName().isEmpty()) {
        if (!name.firstName().isEmpty()) {
            return QString(QLatin1String("%1, %2")).arg(name.lastName()).arg(name.firstName());
        } else {
            // Just last
            return name.lastName();
        }
    } else if (!name.firstName().isEmpty()) {
        return name.firstName();
    } else if (!org.name().isEmpty()) {
        return org.name();
    } else {
        // XXX grargh.
        return QLatin1String("Unnamed");
    }
}



PROPID QContactWinCEEngine::metaAvatarImage() const
{
    return d->m_avatarImageMeta;
}

PROPID QContactWinCEEngine::metaAvatarVideo() const
{
    return d->m_avatarVideoMeta;
}

PROPID QContactWinCEEngine::metaEmail() const
{
    return d->m_emailmeta;
}

PROPID QContactWinCEEngine::metaPhone() const
{
    return d->m_phonemeta;
}

/*! \reimp */
bool QContactWinCEEngine::isFilterSupported(const QContactFilter& filter) const
{
    switch (filter.type()) {
        case QContactFilter::InvalidFilter:
        case QContactFilter::DefaultFilter:
        case QContactFilter::LocalIdFilter:
        case QContactFilter::ContactDetailFilter:
        case QContactFilter::ContactDetailRangeFilter:
        case QContactFilter::ActionFilter:
        case QContactFilter::IntersectionFilter:
        case QContactFilter::UnionFilter:
            return true;
    }
    return false;
}

/*! \reimp */
int QContactWinCEEngine::managerVersion() const
{
    return QTCONTACTS_VERSION;
}

/*! \reimp */
QList<QContact> QContactWinCEEngine::contacts(const QContactFilter& filter, const QList<QContactSortOrder>& sortOrders, const QContactFetchHint& fetchHint, QContactManager::Error* error) const
{
    QList<QContactLocalId> ids = contactIds(filter, sortOrders, error);
    QList<QContact> cs;
    if (*error == QContactManager::NoError) {
        foreach (const QContactLocalId& id, ids) {
            cs << contact(id, fetchHint, error);
        }
    }
    return cs;
}

/*! \reimp */
bool QContactWinCEEngine::saveContacts(QList<QContact>* contacts, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
{
    bool ret = true;

    for (int j = 0; j < contacts->size(); j++) {
        if (!saveContact(&((*contacts)[j]), error)) {
            ret = false;
        }
        if (*error != QContactManager::NoError) {
            errorMap->insert(j, *error);
        }
    }
    return ret;
}

/*! \reimp */
bool QContactWinCEEngine::removeContacts(const QList<QContactLocalId>& contactIds, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
{
    bool ret = true;

    for (int j = 0; j < contactIds.size(); j++) {
        if (!removeContact(contactIds[j], error)) {
            ret = false;
        }

        if (*error != QContactManager::NoError) {
            errorMap->insert(j, *error);
        }
    }
    return ret;
}

/*!
 * Returns the list of data types supported by the WinCE engine
 */
QList<QVariant::Type> QContactWinCEEngine::supportedDataTypes() const
{
    QList<QVariant::Type> st;
    st.append(QVariant::String);
    st.append(QVariant::Int);
    st.append(QVariant::UInt);
    st.append(QVariant::Double);
    st.append(QVariant::Date);
    st.append(QVariant::DateTime);

    return st;
}

/* The default constructor of wince contact manager engine factory */
ContactWinceFactory::ContactWinceFactory()
:m_engine(0)
{
}

/* Factory lives here in the basement */
QContactManagerEngine* ContactWinceFactory::engine(const QMap<QString, QString>& parameters, QContactManager::Error* error)
{
    QMutexLocker locker(&m_mutex);
    if (!m_engine) {
        m_engine = new QContactWinCEEngine(this, managerName(), parameters, error);
    }
    return m_engine;
}

QString ContactWinceFactory::managerName() const
{
    return QString("wince");
}

 void ContactWinceFactory::resetEngine()
 {
     QMutexLocker locker(&m_mutex);
     m_engine = 0;
 }
Q_EXPORT_PLUGIN2(qtcontacts_wince, ContactWinceFactory);