|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #ifndef MALICIOUSPLUGINTARGET |
|
43 #define MALICIOUSPLUGINTARGET contacts_maliciousplugin |
|
44 #endif |
|
45 |
|
46 #ifndef MALICIOUSPLUGINNAME |
|
47 #define MALICIOUSPLUGINNAME maliciousplugin |
|
48 #endif |
|
49 |
|
50 #define makestr(x) (#x) |
|
51 #define makename(x) makestr(x) |
|
52 |
|
53 #include "maliciousplugin_p.h" |
|
54 #include "qcontactmanager.h" |
|
55 |
|
56 #include <QThread> |
|
57 #include <QMutex> |
|
58 #include <QSet> |
|
59 #include <QDebug> |
|
60 |
|
61 class MaliciousThreadObject : public QObject |
|
62 { |
|
63 Q_OBJECT |
|
64 public: |
|
65 MaliciousThreadObject() {} |
|
66 |
|
67 public slots: |
|
68 void activateRequest(QContactAbstractRequest* req) |
|
69 { |
|
70 mutex.lock(); |
|
71 if (activeRequests.contains(req)) { |
|
72 QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::ActiveState); |
|
73 } |
|
74 mutex.unlock(); |
|
75 } |
|
76 |
|
77 void finishRequest(QContactAbstractRequest* req) |
|
78 { |
|
79 QContactManager::Error errorResult = QContactManager::NoError; |
|
80 QMap<int, QContactManager::Error> errorsResult; |
|
81 QList<QContactLocalId> idResult; |
|
82 QList<QContact> contactResult; |
|
83 QList<QContactDetailDefinition> defResult; |
|
84 QMap<QString, QContactDetailDefinition> defMapResult; |
|
85 QList<QContactRelationship> relResult; |
|
86 |
|
87 mutex.lock(); |
|
88 if (activeRequests.contains(req)) { |
|
89 switch(req->type()) { |
|
90 case QContactAbstractRequest::ContactLocalIdFetchRequest: |
|
91 QContactManagerEngine::updateContactLocalIdFetchRequest(static_cast<QContactLocalIdFetchRequest*>(req), idResult, errorResult, QContactAbstractRequest::FinishedState); |
|
92 break; |
|
93 |
|
94 case QContactAbstractRequest::ContactFetchRequest: |
|
95 QContactManagerEngine::updateContactFetchRequest(static_cast<QContactFetchRequest*>(req), contactResult, errorResult, QContactAbstractRequest::FinishedState); |
|
96 break; |
|
97 |
|
98 case QContactAbstractRequest::DetailDefinitionSaveRequest: |
|
99 QContactManagerEngine::updateDefinitionSaveRequest(static_cast<QContactDetailDefinitionSaveRequest*>(req), defResult, errorResult, errorsResult, QContactAbstractRequest::FinishedState); |
|
100 break; |
|
101 |
|
102 case QContactAbstractRequest::DetailDefinitionFetchRequest: |
|
103 QContactManagerEngine::updateDefinitionFetchRequest(static_cast<QContactDetailDefinitionFetchRequest*>(req), defMapResult, errorResult, errorsResult, QContactAbstractRequest::FinishedState); |
|
104 break; |
|
105 |
|
106 case QContactAbstractRequest::DetailDefinitionRemoveRequest: |
|
107 QContactManagerEngine::updateDefinitionRemoveRequest(static_cast<QContactDetailDefinitionRemoveRequest*>(req), errorResult, errorsResult, QContactAbstractRequest::FinishedState); |
|
108 break; |
|
109 default: |
|
110 QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState); |
|
111 break; |
|
112 } |
|
113 } |
|
114 activeRequests.remove(req); |
|
115 mutex.unlock(); |
|
116 } |
|
117 public: |
|
118 QMutex mutex; |
|
119 |
|
120 QSet<QContactAbstractRequest*> activeRequests; |
|
121 }; |
|
122 |
|
123 class MaliciousThread : public QThread |
|
124 { |
|
125 Q_OBJECT; |
|
126 public: |
|
127 MaliciousThread(); |
|
128 ~MaliciousThread(); |
|
129 QObject* eventLoopQuitHack; |
|
130 }; |
|
131 |
|
132 MaliciousThread::MaliciousThread() |
|
133 { |
|
134 eventLoopQuitHack = new QObject; |
|
135 eventLoopQuitHack->moveToThread(this); |
|
136 connect(eventLoopQuitHack, SIGNAL(destroyed(QObject*)), SLOT(quit()), Qt::DirectConnection); |
|
137 } |
|
138 |
|
139 MaliciousThread::~MaliciousThread() |
|
140 { |
|
141 eventLoopQuitHack->deleteLater(); |
|
142 wait(); |
|
143 } |
|
144 |
|
145 MaliciousAsyncManagerEngine::MaliciousAsyncManagerEngine() |
|
146 : QContactManagerEngine() |
|
147 { |
|
148 thread = new MaliciousThread(); |
|
149 threadObject = new MaliciousThreadObject(); |
|
150 threadObject->moveToThread(thread); |
|
151 |
|
152 connect(this, SIGNAL(doStartRequest(QContactAbstractRequest*)), threadObject, SLOT(activateRequest(QContactAbstractRequest*))); |
|
153 connect(this, SIGNAL(doFinishRequest(QContactAbstractRequest*)), threadObject, SLOT(finishRequest(QContactAbstractRequest*))); |
|
154 |
|
155 thread->start(); |
|
156 } |
|
157 |
|
158 MaliciousAsyncManagerEngine::~MaliciousAsyncManagerEngine() |
|
159 { |
|
160 delete thread; |
|
161 delete threadObject; |
|
162 } |
|
163 |
|
164 QString MaliciousAsyncManagerEngine::synthesizedDisplayLabel(const QContact& contact, QContactManager::Error* error) const |
|
165 { |
|
166 Q_UNUSED(contact); |
|
167 *error = QContactManager::NotSupportedError; |
|
168 return QString(); |
|
169 } |
|
170 |
|
171 QString MaliciousAsyncManagerEngine::managerName() const |
|
172 { |
|
173 return QString(makename(MALICIOUSPLUGINNAME)); |
|
174 } |
|
175 |
|
176 bool MaliciousAsyncManagerEngine::startRequest(QContactAbstractRequest* req) |
|
177 { |
|
178 threadObject->mutex.lock(); |
|
179 threadObject->activeRequests.insert(req); |
|
180 threadObject->mutex.unlock(); |
|
181 |
|
182 // Spawn a thread to do stuff on another thread |
|
183 emit doStartRequest(req); |
|
184 emit doFinishRequest(req); |
|
185 |
|
186 return true; |
|
187 } |
|
188 |
|
189 bool MaliciousAsyncManagerEngine::cancelRequest(QContactAbstractRequest *req) |
|
190 { |
|
191 updateRequestState(req, QContactAbstractRequest::CanceledState); |
|
192 QContactManagerEngine::cancelRequest(req); |
|
193 return true; |
|
194 } |
|
195 |
|
196 void MaliciousAsyncManagerEngine::requestDestroyed(QContactAbstractRequest *req) |
|
197 { |
|
198 threadObject->mutex.lock(); |
|
199 threadObject->activeRequests.remove(req); |
|
200 threadObject->mutex.unlock(); |
|
201 QContactManagerEngine::requestDestroyed(req); |
|
202 } |
|
203 |
|
204 QString MaliciousEngineFactory::managerName() const |
|
205 { |
|
206 return QString(makename(MALICIOUSPLUGINNAME)); |
|
207 } |
|
208 Q_EXPORT_PLUGIN2(MALICIOUSPLUGINTARGET, MaliciousEngineFactory); |
|
209 |
|
210 QContactManagerEngine* MaliciousEngineFactory::engine(const QMap<QString, QString>& parameters, QContactManager::Error* error) |
|
211 { |
|
212 Q_UNUSED(parameters); |
|
213 *error = QContactManager::NoError; |
|
214 return new MaliciousAsyncManagerEngine(); |
|
215 } |
|
216 |
|
217 #include "maliciousplugin.moc" |