|
1 /* |
|
2 * |
|
3 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 * All rights reserved. |
|
5 * |
|
6 * This program is free software: you can redistribute it and/or modify |
|
7 * it under the terms of the GNU Lesser General Public License as published by |
|
8 * the Free Software Foundation, version 2.1 of the License. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public License |
|
16 * along with this program. If not, |
|
17 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/". |
|
18 * |
|
19 * Description: |
|
20 * |
|
21 */ |
|
22 |
|
23 #include <QObject> |
|
24 #include <qglobal.h> |
|
25 #include <QApplication> |
|
26 #include <QLatin1String> |
|
27 #include <QLocale> |
|
28 #include <QIcon> |
|
29 #include <QFileInfo> |
|
30 |
|
31 #include <xqaiwdecl.h> |
|
32 #include <xqservicerequest.h> |
|
33 #include "xqrequestutil.h" |
|
34 #include "xqservicelog.h" |
|
35 #include "xqaiwutils.h" |
|
36 #include "xqaiwservicedriver.h" |
|
37 |
|
38 // Constants |
|
39 |
|
40 XQAiwServiceDriver::XQAiwServiceDriver(const XQAiwInterfaceDescriptor& descriptor, const QString &operation) |
|
41 : XQAiwRequestDriver(), |
|
42 currentRequest(NULL), |
|
43 completeSignalConnected(false), |
|
44 errorSignalConnected(false) |
|
45 { |
|
46 mErrorMsg = ""; |
|
47 mDescr = descriptor; |
|
48 mOperation = operation; |
|
49 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::XQAiwServiceDriver: %s %s,%x", |
|
50 qPrintable(mDescr.interfaceName()), |
|
51 qPrintable(mOperation), |
|
52 mDescr.property(XQAiwInterfaceDescriptor::ImplementationId).toInt()); |
|
53 |
|
54 // Allocate QtHighway request |
|
55 currentRequest = new XQServiceRequest(mDescr, mOperation); |
|
56 } |
|
57 |
|
58 XQAiwServiceDriver::~XQAiwServiceDriver() |
|
59 { |
|
60 XQSERVICE_DEBUG_PRINT("~XQAiwServiceDriver::XQAiwServiceDriver"); |
|
61 |
|
62 // Disconnect signals |
|
63 if (completeSignalConnected) |
|
64 { |
|
65 disconnect(currentRequest, SIGNAL(requestCompleted(const QVariant&)), this, SLOT(handleAsyncResponse(const QVariant&))); |
|
66 } |
|
67 if (errorSignalConnected) |
|
68 { |
|
69 disconnect(currentRequest, SIGNAL(requestError(int)), this, SLOT(handleAsyncError(int))); |
|
70 } |
|
71 |
|
72 delete currentRequest; // Destructor cancels the async request |
|
73 |
|
74 XQSERVICE_DEBUG_PRINT("~XQAiwServiceDriver::XQAiwServiceDriver 2"); |
|
75 removeTranslator(); |
|
76 currentRequest = NULL; |
|
77 } |
|
78 |
|
79 |
|
80 |
|
81 QAction *XQAiwServiceDriver::createAction() |
|
82 { |
|
83 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::createAction"); |
|
84 |
|
85 if (!qApp) |
|
86 return NULL; // Not supported in non-GUI environments |
|
87 |
|
88 installTranslator(); |
|
89 |
|
90 QString textId = mDescr.customProperty(XQCUSTOM_PROP_AIW_TEXT); |
|
91 if (textId.isEmpty()) |
|
92 { |
|
93 // Applye the key name and make it visible to client as indication of |
|
94 // missing property .. |
|
95 textId = "#missing " + XQCUSTOM_PROP_AIW_TEXT; |
|
96 } |
|
97 |
|
98 QByteArray ba = textId.toLatin1(); |
|
99 const char *textPtr = ba.data(); |
|
100 QString text = qtTrId(textPtr); // translate |
|
101 XQSERVICE_DEBUG_PRINT("Translated text %s", qPrintable(text)); |
|
102 |
|
103 QAction *action=0; |
|
104 QIcon icon; |
|
105 QString iconFile = mDescr.customProperty(XQCUSTOM_PROP_AIW_ICON); |
|
106 if (!iconFile.isEmpty()) |
|
107 { |
|
108 XQSERVICE_DEBUG_PRINT("QAction with icon and text"); |
|
109 icon.addFile(iconFile); |
|
110 action = new QAction(icon, text, 0); |
|
111 } |
|
112 else |
|
113 { |
|
114 XQSERVICE_DEBUG_PRINT("QAction with text"); |
|
115 action = new QAction(text,0); |
|
116 } |
|
117 |
|
118 return action; |
|
119 } |
|
120 |
|
121 void XQAiwServiceDriver::setArguments(const QList<QVariant> &arguments) |
|
122 { |
|
123 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setArguments"); |
|
124 currentRequest->setArguments(arguments); |
|
125 } |
|
126 |
|
127 |
|
128 bool XQAiwServiceDriver::send(QVariant& retValue) |
|
129 { |
|
130 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::send>>>"); |
|
131 |
|
132 // Update info (these ones can be given via XQAiwRequest function count) |
|
133 XQRequestInfo opt = info(); |
|
134 QVariant emb = opt.info(XQServiceUtils::OptEmbedded); |
|
135 QVariant bg = opt.info(XQServiceUtils::OptBackground); |
|
136 if (!emb.isValid()) |
|
137 { |
|
138 // Not set via setInfo |
|
139 opt.setEmbedded(mEmbedded); |
|
140 } |
|
141 if (!bg.isValid()) |
|
142 { |
|
143 // Not set via setInfo |
|
144 opt.setBackground(mBackground); |
|
145 } |
|
146 currentRequest->setInfo(opt); |
|
147 |
|
148 QStringList list; |
|
149 bool res = true; |
|
150 if (!currentRequest->isSynchronous() && !completeSignalConnected) |
|
151 { |
|
152 // Async request. Connect signal only once |
|
153 XQSERVICE_DEBUG_PRINT("request::async send"); |
|
154 connect(currentRequest, SIGNAL(requestCompleted(const QVariant&)), this, SLOT(handleAsyncResponse(const QVariant&))); |
|
155 completeSignalConnected = true; |
|
156 } |
|
157 if (!errorSignalConnected) |
|
158 { |
|
159 // Connect error signal only once |
|
160 connect(currentRequest, SIGNAL(requestError(int)), this, SLOT(handleAsyncError(int))); |
|
161 errorSignalConnected = true; |
|
162 } |
|
163 |
|
164 XQSERVICE_DEBUG_PRINT("request::send>>>"); |
|
165 res = currentRequest->send(retValue); // Result is valid for sync request only |
|
166 XQSERVICE_DEBUG_PRINT("request::send: %d<<<", res); |
|
167 |
|
168 mErrorCode = XQRequestUtil::mapError(currentRequest->latestError()); |
|
169 if (res && !mErrorCode) |
|
170 { |
|
171 mErrorMsg = ""; |
|
172 } |
|
173 else |
|
174 { |
|
175 // This is for debugging/trace purposes only, no need to localize |
|
176 mErrorMsg = XQAiwUtils::createErrorMessage(mErrorCode, "XQAiwServiceDriver", "sync send"); |
|
177 } |
|
178 |
|
179 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::send: %d<<<", res); |
|
180 |
|
181 return res; |
|
182 |
|
183 } |
|
184 |
|
185 const QString& XQAiwServiceDriver::lastErrorMessage() const |
|
186 { |
|
187 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::lastErrorMessage: %s", qPrintable(mErrorMsg)); |
|
188 return mErrorMsg; |
|
189 } |
|
190 |
|
191 int XQAiwServiceDriver::lastError() const |
|
192 { |
|
193 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::lastError %d", mErrorCode); |
|
194 return mErrorCode; |
|
195 } |
|
196 |
|
197 const XQAiwInterfaceDescriptor &XQAiwServiceDriver::descriptor() const |
|
198 { |
|
199 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::descriptor"); |
|
200 return mDescr; |
|
201 } |
|
202 |
|
203 void XQAiwServiceDriver::setEmbedded(bool embedded) |
|
204 { |
|
205 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setEmbedded"); |
|
206 // Embedded is option |
|
207 mEmbedded = embedded; |
|
208 } |
|
209 bool XQAiwServiceDriver::isEmbedded() const |
|
210 { |
|
211 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::isEmbedded"); |
|
212 // Embedded is option |
|
213 return mEmbedded; |
|
214 } |
|
215 |
|
216 |
|
217 void XQAiwServiceDriver::setBackground(bool background ) |
|
218 { |
|
219 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setBackground"); |
|
220 mBackground = background; |
|
221 } |
|
222 bool XQAiwServiceDriver::isBackground() const |
|
223 { |
|
224 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::isBackground"); |
|
225 return mBackground; |
|
226 } |
|
227 |
|
228 |
|
229 void XQAiwServiceDriver::setOperation(const QString &operation) |
|
230 { |
|
231 XQSERVICE_DEBUG_PRINT("XQAiwRequest::setOperation"); |
|
232 mOperation = operation; |
|
233 currentRequest->setMessage(operation); |
|
234 } |
|
235 const QString &XQAiwServiceDriver::operation() const |
|
236 { |
|
237 XQSERVICE_DEBUG_PRINT("XQAiwRequest::operation"); |
|
238 return mOperation; |
|
239 } |
|
240 |
|
241 void XQAiwServiceDriver::setSynchronous(bool synchronous) |
|
242 { |
|
243 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setSynchronous"); |
|
244 currentRequest->setSynchronous(synchronous); |
|
245 } |
|
246 bool XQAiwServiceDriver::isSynchronous() const |
|
247 { |
|
248 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::isSynchronous"); |
|
249 |
|
250 return currentRequest->isSynchronous(); |
|
251 } |
|
252 |
|
253 void XQAiwServiceDriver::setInfo(const XQRequestInfo &info) |
|
254 { |
|
255 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setInfo"); |
|
256 currentRequest->setInfo(info); |
|
257 } |
|
258 |
|
259 XQRequestInfo XQAiwServiceDriver::info() const |
|
260 { |
|
261 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::info"); |
|
262 return currentRequest->info(); |
|
263 } |
|
264 |
|
265 void XQAiwServiceDriver::handleAsyncResponse(const QVariant& value) |
|
266 { |
|
267 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::handleAsyncResponse"); |
|
268 emit requestOk(value); |
|
269 } |
|
270 |
|
271 void XQAiwServiceDriver::handleAsyncError(int err) |
|
272 { |
|
273 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::handleAsyncError"); |
|
274 |
|
275 mErrorCode = XQRequestUtil::mapError(err); |
|
276 mErrorMsg = XQAiwUtils::createErrorMessage(mErrorCode, "XQAiwServiceDriver", "async send"); |
|
277 emit requestError(mErrorCode, mErrorMsg); |
|
278 |
|
279 } |
|
280 |
|
281 |
|
282 bool XQAiwServiceDriver::installTranslator() |
|
283 { |
|
284 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::installTranslator"); |
|
285 |
|
286 if (!qApp) |
|
287 { |
|
288 XQSERVICE_DEBUG_PRINT("\tNo application context"); |
|
289 return false; |
|
290 } |
|
291 |
|
292 // The the name of the resource file containing the text |
|
293 QString textFile = mDescr.customProperty(XQCUSTOM_PROP_AIW_TEXT_FILE); |
|
294 if (textFile.isEmpty()) |
|
295 { |
|
296 XQSERVICE_DEBUG_PRINT("\tCustom property missing for text file"); |
|
297 return false; |
|
298 } |
|
299 |
|
300 // Check if locale has changed since last request |
|
301 QString lang = QLocale::system().name(); |
|
302 XQSERVICE_DEBUG_PRINT("\tLanguage now is%s", qPrintable(lang)); |
|
303 if (!this->lastLang.isEmpty() && (this->lastLang != lang)) |
|
304 { |
|
305 // Language has changed since last time |
|
306 // Remove previous translator, if any |
|
307 removeTranslator(); |
|
308 } |
|
309 |
|
310 textFile = makeFileName(textFile); |
|
311 |
|
312 bool res=false; |
|
313 if (this->translator.isEmpty()) |
|
314 { |
|
315 // Construct the full name of the localized resource |
|
316 XQSERVICE_DEBUG_PRINT("textFile name is %s", qPrintable(textFile)); |
|
317 res = this->translator.load(textFile); |
|
318 if (res) |
|
319 { |
|
320 qApp->installTranslator(&this->translator); |
|
321 this->lastLang = lang; // Remember the current language |
|
322 XQSERVICE_DEBUG_PRINT("Translator installed %s", qPrintable(lang)); |
|
323 } |
|
324 } |
|
325 |
|
326 return res; |
|
327 |
|
328 } |
|
329 |
|
330 void XQAiwServiceDriver::removeTranslator() |
|
331 { |
|
332 if (qApp && !this->translator.isEmpty()) |
|
333 { |
|
334 qApp->removeTranslator(&this->translator); |
|
335 } |
|
336 } |
|
337 |
|
338 QString XQAiwServiceDriver::makeFileName(const QString &textFile) const |
|
339 { |
|
340 QFileInfo info(textFile); |
|
341 QString ret = textFile; |
|
342 |
|
343 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::makeFileName %s", qPrintable(textFile)); |
|
344 |
|
345 if (info.baseName() == info.filePath()) |
|
346 { |
|
347 // No path, apply the default path |
|
348 // Drive is where app is loaded, like "C:" or "Z:" |
|
349 ret = qApp->applicationFilePath().left(2) + "/resource/qt/translations/" + textFile; |
|
350 } |
|
351 |
|
352 // Add current language |
|
353 QString lang = QLocale::system().name(); |
|
354 ret += "_"; |
|
355 ret += lang; |
|
356 |
|
357 XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::makeFileName ret=%s", qPrintable(ret)); |
|
358 |
|
359 return ret; |
|
360 |
|
361 } |