- fix loading status message and presence from tracker
[qtcontacts-tracker:qtcontacts-tracker.git] / qtrackercontactasyncrequest.cpp
1 /* * This file is part of qtcontacts-tracker
2  * Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
3  * Contact: Aleksandar Stojiljkovic <aleksandar.stojiljkovic@nokia.com>
4  * This software, including documentation, is protected by copyright controlled by
5  * Nokia Corporation. All rights are reserved. Copying, including reproducing, storing,
6  * adapting or translating, any or all of this material requires the prior written consent
7  * of Nokia Corporation. This material also contains confidential information which may
8  * not be disclosed to others without the prior written consent of Nokia.
9  */
10
11 #include "qcontactabstractrequest.h"
12 #include "qcontactmanagerengine.h"
13
14
15 #include <QtTracker/ontologies/nie.h>
16 #include <QtTracker/ontologies/nco.h>
17
18 #include "qcontact.h"
19 #include "qcontactdetails.h"
20 #include "qcontactrequests.h"
21 #include "qtrackercontactasyncrequest.h"
22 using namespace SopranoLive;
23
24 QTrackerContactAsyncRequest::QTrackerContactAsyncRequest(
25         QContactAbstractRequest* request, QContactManagerEngine* parent) :
26     QObject(parent)
27 {
28     req = request;
29     switch (req->type())
30     {
31         case QContactAbstractRequest::ContactIdFetch:
32         {
33             QContactIdFetchRequest* r =
34                     static_cast<QContactIdFetchRequest*> (req);
35             QContactFilter filter = r->filter();
36             QList<QContactSortOrder> sorting = r->sorting();
37             Q_UNUSED(filter)
38             Q_UNUSED(sorting)
39
40             RDFVariable RDFContact =
41                     RDFVariable::fromType<nco::PersonContact>();
42             RDFSelect quer;
43
44             quer.addColumn("contact_uri", RDFContact);
45             quer.addColumn("contactId",
46                     RDFContact.property<nco::contactUID> ());
47             query = ::tracker()->modelQuery(quer);
48             // need to store LiveNodes in order to receive notification from model
49             QObject::connect(query.model(), SIGNAL(modelUpdated()), this,
50                     SLOT(modelUpdated()));
51
52             break;
53         }
54         case QContactAbstractRequest::ContactFetch:
55         {
56             QContactFetchRequest* r =
57                     static_cast<QContactFetchRequest*> (req);
58             QContactFilter filter = r->filter();
59             QList<QContactSortOrder> sorting = r->sorting();
60             QStringList fetchOnlyDetails = r->definitionRestrictions();
61             Q_UNUSED(filter)
62             Q_UNUSED(sorting)
63             Q_UNUSED(fetchOnlyDetails)
64
65             QList<QUniqueId> ids;
66             RDFVariable RDFContact =
67                     RDFVariable::fromType<nco::PersonContact>();
68             RDFSelect quer;
69
70
71             quer.addColumn("contact_uri", RDFContact);
72             quer.addColumn("contactId",
73                     RDFContact.property<nco::contactUID> ());
74             quer.addColumn("firstname",
75                     RDFContact.property<nco::nameGiven> ());
76             quer.addColumn("secondname",
77                     RDFContact.property<nco::nameFamily> ());
78             quer.addColumn("photo",
79                     RDFContact.property<nco::photo> ());
80
81             query = ::tracker()->modelQuery(quer);
82             // need to store LiveNodes in order to receive notification from model
83             QObject::connect(query.model(), SIGNAL(modelUpdated()), this,
84                     SLOT(contactsReady()));
85
86             break;
87         }
88
89             // implement the rest
90         default:
91             break;
92     }
93 }
94
95 QTrackerContactAsyncRequest::~QTrackerContactAsyncRequest()
96 {
97
98 }
99
100 void QTrackerContactAsyncRequest::modelUpdated()
101 {
102     // fastest way to get this working. refactor
103     QContactManagerEngine *engine = qobject_cast<QContactManagerEngine *> (
104             parent());
105     QList<QUniqueId> result;
106     // for now this only serves get all contacts
107     for(int i = 0; i < query->rowCount(); i++)
108     {
109         result.append(query->index(i, 1).data().toUInt());
110     }
111
112     if (engine)
113         engine->updateRequest(req, result, QContactManager::NoError, QList<
114                 QContactManager::Error> (), QContactAbstractRequest::Finished,
115                 false);
116 }
117
118 void QTrackerContactAsyncRequest::contactsReady()
119 {
120     // fastest way to get this working. refactor
121     QContactManagerEngine *engine = qobject_cast<QContactManagerEngine *> (
122             parent());
123     QList<QContact> result;
124     for(int i = 0; i < query->rowCount(); i++)
125     {
126         QContact contact;
127         contact.setId(query->index(i, 1).data().toUInt());
128         QContactName name;
129         name.setFirst(query->index(i, 2).data().toString());
130         name.setLast(query->index(i, 3).data().toString());
131         contact.saveDetail(&name);
132
133         QContactAvatar avatar;
134         avatar.setAvatar(query->index(i, 4).data().toString());
135         contact.saveDetail(&avatar);
136
137         result.append(contact);
138     }
139
140     if (engine)
141         engine->updateRequest(req, result, QContactManager::NoError, QList<
142                 QContactManager::Error> (), QContactAbstractRequest::Finished,
143                 false);
144 }