|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Offers file share services. |
|
15 * |
|
16 */ |
|
17 |
|
18 // SYSTEM INCLUDES |
|
19 #include <QVariant> |
|
20 #include <QDir> |
|
21 #include <QRegExp> |
|
22 #include <QFile> |
|
23 #include <QStringList> |
|
24 #include <QGraphicsLinearLayout> |
|
25 |
|
26 #include <HbAction> |
|
27 #include <HbDialog> |
|
28 #include <HbLabel> |
|
29 #include <HbListView> |
|
30 #include <HbListViewItem> |
|
31 #include <HbNotificationDialog> |
|
32 #include <HbIconItem> |
|
33 #include <HbTextItem> |
|
34 #include <HbColorScheme> |
|
35 #include <xqserviceglobal.h> |
|
36 #include <xqaiwrequest.h> |
|
37 #include <xqaiwinterfacedescriptor.h> |
|
38 |
|
39 // USER INCLUDES |
|
40 #include "shareuiprivate.h" |
|
41 |
|
42 // Localized Constants |
|
43 //#define LOC_SEND_SELECTED_ITEM hbTrId("txt_shareui_send_selected_item") |
|
44 //#define LOC_BUTTON_CANCEL hbTrId("txt_shareui_button_cancel") |
|
45 //#define LOC_NO_SERVICES hbTrId("txt_shareui_no_services") |
|
46 //#define LOC_NO_FILES hbTrId("txt_shareui_no_files") |
|
47 //#define LOC_SERVICE_ERROR hbTrId("txt_shareui_service_error") |
|
48 //#define LOC_PROTECTED_CONTENT hbTrId("txt_shareui_protected_content") |
|
49 |
|
50 #define LOC_SEND_SELECTED_ITEM hbTrId("Send selected item") |
|
51 #define LOC_BUTTON_CANCEL hbTrId("Cancel") |
|
52 #define LOC_NO_SERVICES hbTrId("No services found.") |
|
53 #define LOC_NO_FILES hbTrId("No files found.") |
|
54 #define LOC_SERVICE_ERROR hbTrId("Service error.") |
|
55 #define LOC_PROTECTED_CONTENT hbTrId("Protected content.") |
|
56 |
|
57 const QString LIST_ITEM_TITLE("qtc_list_item_title_normal"); |
|
58 |
|
59 /** |
|
60 * Constructor. |
|
61 */ |
|
62 ShareUiPrivate::ShareUiPrivate() |
|
63 { |
|
64 mContentItemModel = 0; |
|
65 mContentListView = 0; |
|
66 mSharePopup = 0; |
|
67 } |
|
68 |
|
69 /** |
|
70 * Destructor. |
|
71 */ |
|
72 ShareUiPrivate::~ShareUiPrivate() |
|
73 { |
|
74 } |
|
75 |
|
76 /** |
|
77 * Clear the internal data structures |
|
78 */ |
|
79 void ShareUiPrivate::reset() |
|
80 { |
|
81 mFileList.clear(); |
|
82 |
|
83 mIndexActionMap.clear(); |
|
84 foreach(XQAiwRequest* request,mAiwRequestList) |
|
85 { |
|
86 delete request; |
|
87 } |
|
88 mAiwRequestList.clear(); |
|
89 } |
|
90 |
|
91 /** |
|
92 * Initialize and start the dialog |
|
93 * |
|
94 * @param fileList list of tokens that would allow file manipulation. |
|
95 * @return bool error false if dialog initialization failed, true otherwise. |
|
96 * |
|
97 */ |
|
98 bool ShareUiPrivate::init(QStringList& fileList, bool embedded) |
|
99 { |
|
100 reset(); |
|
101 mIsEmbedded = embedded; |
|
102 // No input files |
|
103 if (fileList.count() == 0) { |
|
104 showNote(LOC_NO_FILES); |
|
105 return true; |
|
106 } |
|
107 |
|
108 #ifdef __SHAREUI_MIME_HANDLING__ |
|
109 // Get the file attributes (MIME and forward lock) |
|
110 QStringList mimeTypeList; |
|
111 QStringList forwardStatusList; |
|
112 getFileAttributes(fileList, mimeTypeList, forwardStatusList); |
|
113 |
|
114 // Ignore protected files |
|
115 QStringList filteredFileList; |
|
116 QStringList filteredMimeList; |
|
117 for (int i = 0; i < forwardStatusList.count(); i++) { |
|
118 if (forwardStatusList[i].toInt()) { |
|
119 filteredFileList << fileList[i]; |
|
120 filteredMimeList << mimeTypeList[i]; |
|
121 } |
|
122 } |
|
123 filteredMimeList.removeDuplicates(); |
|
124 |
|
125 // Some protected content |
|
126 if (fileList.count() != filteredFileList.count()) { |
|
127 showNote(LOC_PROTECTED_CONTENT); |
|
128 return true; |
|
129 } |
|
130 |
|
131 // Only protected content |
|
132 if (filteredFileList.count() == 0) { |
|
133 showNote(LOC_NO_FILES); |
|
134 return true; |
|
135 } |
|
136 #endif |
|
137 |
|
138 #ifdef __SHAREUI_MIME_HANDLING__ |
|
139 for (int i = 0; i < filteredFileList.count(); i++) { |
|
140 mFileList.append(QDir::toNativeSeparators(filteredFileList[i])); |
|
141 } |
|
142 #else |
|
143 for ( int i = 0; i < fileList.count(); i++ ) |
|
144 { |
|
145 mFileList.append(QDir::toNativeSeparators(fileList[i])); |
|
146 } |
|
147 #endif |
|
148 |
|
149 QList<XQAiwInterfaceDescriptor> serviceDescriptorList = mAppManager.list(SERVICE_INTERFACE, |
|
150 SHARE_OP); |
|
151 |
|
152 if (serviceDescriptorList.size() > 0) { |
|
153 initializeUi(); |
|
154 //sorting the services based on service names, |
|
155 QMap<QString,XQAiwInterfaceDescriptor > serviceInterfaceMap; |
|
156 for (int i = 0; i < serviceDescriptorList.count(); i++) { |
|
157 serviceInterfaceMap.insert(serviceDescriptorList[i].serviceName(), |
|
158 serviceDescriptorList[i]); |
|
159 } |
|
160 QStringList serviceNames = serviceInterfaceMap.keys(); |
|
161 serviceDescriptorList.clear(); |
|
162 |
|
163 for (int i = 0; i < serviceNames.count(); i++) { |
|
164 #ifdef __SHAREUI_MIME_HANDLING__ |
|
165 // Filter services based on content type |
|
166 QString allowedTypes = serviceInterfaceMap[serviceNames.at(i)].customProperty(QString( |
|
167 "allowed_mime_types")); |
|
168 QString blockedTypes = serviceInterfaceMap[serviceNames.at(i)].customProperty(QString( |
|
169 "blocked_mime_types")); |
|
170 |
|
171 // Check against MIME filters |
|
172 if (!isContentAllowed(filteredMimeList, allowedTypes)) |
|
173 continue; |
|
174 |
|
175 if (isContentBlocked(filteredMimeList, blockedTypes)) |
|
176 continue; |
|
177 #endif |
|
178 HbAction* action = fetchServiceAction(serviceInterfaceMap[serviceNames.at(i)]); |
|
179 QString iconName = serviceInterfaceMap[serviceNames.at(i)].customProperty(QString("aiw_action_icon")); |
|
180 if (action) { |
|
181 updateShareUiDialogList(action, iconName); |
|
182 } |
|
183 } |
|
184 serviceInterfaceMap.clear(); |
|
185 if (mContentItemModel->rowCount() == 0) { |
|
186 showNote(LOC_NO_SERVICES); |
|
187 return true; |
|
188 } |
|
189 |
|
190 mSharePopup->show(); |
|
191 } |
|
192 else { |
|
193 showNote(LOC_NO_SERVICES); |
|
194 } |
|
195 |
|
196 return true; |
|
197 } |
|
198 |
|
199 /** |
|
200 * Creates the view for the share ui. |
|
201 */ |
|
202 void ShareUiPrivate::initializeUi() |
|
203 { |
|
204 // Dialog |
|
205 mSharePopup = new HbDialog(); |
|
206 // make it delete itself on close |
|
207 mSharePopup->setAttribute( Qt::WA_DeleteOnClose, true ); |
|
208 HbTextItem* heading = new HbTextItem(LOC_SEND_SELECTED_ITEM, mSharePopup); |
|
209 QColor color = HbColorScheme::color( LIST_ITEM_TITLE ); |
|
210 heading->setTextColor( color ); |
|
211 heading->setAlignment(Qt::AlignCenter); |
|
212 mSharePopup->setDismissPolicy(HbDialog::TapAnywhere); |
|
213 mSharePopup->setHeadingWidget(heading); |
|
214 mSharePopup->setFrameType(HbDialog::Strong); |
|
215 connect(mSharePopup, SIGNAL(aboutToClose()), this, SLOT(reset())); |
|
216 |
|
217 // Content widget |
|
218 mContentListView = new HbListView(mSharePopup); |
|
219 ShareListItem *prototype = new ShareListItem(mSharePopup); |
|
220 mContentListView->setItemPrototype(prototype); |
|
221 |
|
222 mContentItemModel = new QStandardItemModel(mSharePopup); |
|
223 mContentListView->setModel(mContentItemModel); |
|
224 mContentListView->setUniformItemSizes(true); |
|
225 mSharePopup->setContentWidget(mContentListView); |
|
226 connect(mContentListView, SIGNAL(activated(QModelIndex)), |
|
227 this, SLOT(itemActivated(QModelIndex))); |
|
228 |
|
229 HbAction* cancelAction = new HbAction(LOC_BUTTON_CANCEL,mSharePopup); |
|
230 mSharePopup->addAction(cancelAction); |
|
231 } |
|
232 |
|
233 /** |
|
234 * fetchServiceAction fetches the action associated with a specified interface descriptor. |
|
235 * This is used after the fetchServiceDescriptors is called, and |
|
236 */ |
|
237 HbAction* ShareUiPrivate::fetchServiceAction |
|
238 (XQAiwInterfaceDescriptor interfaceDescriptor) |
|
239 { |
|
240 XQAiwRequest* request = mAppManager.create(interfaceDescriptor, |
|
241 SHARE_OP, mIsEmbedded); |
|
242 mAiwRequestList.append(request); |
|
243 |
|
244 HbAction* action = convertAction(request->createAction()); |
|
245 if ( action ) |
|
246 { |
|
247 connect(request, SIGNAL(triggered()), this, SLOT(onTriggered())); |
|
248 connect(request, SIGNAL(requestOk(const QVariant&)), this, |
|
249 SLOT(handleOk(const QVariant&))); |
|
250 connect(request, SIGNAL(requestError(int,const QString&)), this, |
|
251 SLOT(handleError(int,const QString&))); |
|
252 } |
|
253 return action; |
|
254 } |
|
255 |
|
256 |
|
257 /** |
|
258 * Update the SendUi Dialog (and associated list items) once the required |
|
259 * information has been fetched. |
|
260 */ |
|
261 void ShareUiPrivate::updateShareUiDialogList(HbAction* action, QString iconName) |
|
262 { |
|
263 QStandardItem* item = new QStandardItem(); |
|
264 item->setData(action->text(), ProviderNameRole); |
|
265 item->setData(iconName, ProviderIconRole); |
|
266 mContentItemModel->appendRow(item); |
|
267 QModelIndex index = item->index(); |
|
268 mIndexActionMap[index] = action; |
|
269 } |
|
270 |
|
271 /** |
|
272 * Slot for handling send service selection on the UI. |
|
273 */ |
|
274 void ShareUiPrivate::onTriggered(void) |
|
275 { |
|
276 |
|
277 XQAiwRequest* request = 0; |
|
278 request = qobject_cast<XQAiwRequest*>(sender()); |
|
279 if(request) |
|
280 { |
|
281 QList<QVariant> args; |
|
282 QVariant fileList(mFileList); |
|
283 args << fileList; |
|
284 request->setEmbedded(mIsEmbedded); |
|
285 request->setArguments(args); |
|
286 } |
|
287 } |
|
288 |
|
289 /** |
|
290 * Slot for handling valid returns from the framework. |
|
291 */ |
|
292 void ShareUiPrivate::handleOk(const QVariant& result) |
|
293 { |
|
294 Q_UNUSED(result) |
|
295 |
|
296 XQAiwRequest* request = 0; |
|
297 request = qobject_cast<XQAiwRequest*>(sender()); |
|
298 if(request) |
|
299 { |
|
300 disconnect(request, |
|
301 SIGNAL(requestError(int,const QString&)), |
|
302 this, |
|
303 SLOT(handleError(int,const QString&))); |
|
304 } |
|
305 } |
|
306 |
|
307 /** |
|
308 * Slot for handling errors from the framework. |
|
309 */ |
|
310 void ShareUiPrivate::handleError(int errorCode, const QString& errorMessage) |
|
311 { |
|
312 Q_UNUSED(errorCode) |
|
313 Q_UNUSED(errorMessage) |
|
314 |
|
315 showNote(LOC_SERVICE_ERROR); |
|
316 } |
|
317 |
|
318 /** |
|
319 * When a list box item is pressed, this slot should be invoked. The action |
|
320 * corresponding to the clicked list item should be invoked with the stored lis |
|
321 * of files. |
|
322 * |
|
323 * @param index QModelIndex |
|
324 */ |
|
325 |
|
326 void ShareUiPrivate::itemActivated(QModelIndex index) |
|
327 { |
|
328 HbAction* action = mIndexActionMap[index]; |
|
329 |
|
330 if ( action ) |
|
331 { |
|
332 action->setEnabled(true); |
|
333 action->activate(HbAction::Trigger); |
|
334 } |
|
335 |
|
336 mSharePopup->close(); |
|
337 } |
|
338 |
|
339 |
|
340 /** |
|
341 * Convert a qaction to hbaction. |
|
342 * |
|
343 * @param action QAction* |
|
344 * @return HbAction* |
|
345 */ |
|
346 HbAction* ShareUiPrivate::convertAction(QAction *action) |
|
347 { |
|
348 HbAction *actionHb = 0; |
|
349 if (action) |
|
350 { |
|
351 actionHb = new HbAction(HbIcon(action->icon()), action->text()); |
|
352 actionHb->setVisible(action->isVisible()); |
|
353 actionHb->setCheckable(action->isCheckable()); |
|
354 actionHb->setEnabled(action->isEnabled()); |
|
355 |
|
356 connect(actionHb, SIGNAL(triggered()), action, SIGNAL(triggered())); |
|
357 connect(actionHb, SIGNAL(changed()), action, SIGNAL(changed())); |
|
358 connect(actionHb, SIGNAL(hovered()), action, SIGNAL(hovered())); |
|
359 connect(actionHb, SIGNAL(toggled(bool)), action, SIGNAL(toggled(bool))); |
|
360 |
|
361 action->setParent(actionHb); |
|
362 } |
|
363 |
|
364 return actionHb; |
|
365 } |
|
366 |
|
367 #ifdef __SHAREUI_MIME_HANDLING__ |
|
368 /** |
|
369 * Checks if the specified content type is allowed as per the |
|
370 * specified MIME pattern strings |
|
371 */ |
|
372 bool ShareUiPrivate::isContentAllowed( QStringList mimeType, |
|
373 QString mimePattern ) |
|
374 { |
|
375 if ( mimePattern.trimmed().length() == 0 ) |
|
376 return true; |
|
377 |
|
378 QStringList patterns = mimePattern.split(","); |
|
379 |
|
380 for ( QStringList::Iterator it = mimeType.begin(); |
|
381 it != mimeType.end(); ++it ) |
|
382 { |
|
383 bool match = false; |
|
384 for ( QStringList::Iterator it1 = patterns.begin(); |
|
385 it1 != patterns.end(); ++it1 ) |
|
386 { |
|
387 QString pattern = (*it1).trimmed(); |
|
388 QRegExp rx(pattern, Qt::CaseInsensitive, QRegExp::Wildcard); |
|
389 // Not case sensitive, wild cards allowed |
|
390 if ( rx.exactMatch( *it ) ) |
|
391 { |
|
392 match = true; |
|
393 break; |
|
394 } |
|
395 } |
|
396 |
|
397 if ( ! match ) |
|
398 { |
|
399 return false; |
|
400 } |
|
401 } |
|
402 |
|
403 return true; |
|
404 } |
|
405 |
|
406 /** |
|
407 * Checks if the specified content type is allowed as per the |
|
408 * specified MIME pattern strings |
|
409 */ |
|
410 bool ShareUiPrivate::isContentBlocked( QStringList mimeType, |
|
411 QString mimePattern ) |
|
412 { |
|
413 if ( mimePattern.trimmed().length() == 0 ) |
|
414 return false; |
|
415 |
|
416 QStringList patterns = mimePattern.split(","); |
|
417 |
|
418 for ( QStringList::Iterator it = mimeType.begin(); |
|
419 it != mimeType.end(); ++it ) |
|
420 { |
|
421 bool match = false; |
|
422 for ( QStringList::Iterator it1 = patterns.begin(); |
|
423 it1 != patterns.end(); ++it1 ) |
|
424 { |
|
425 QString pattern = (*it1).trimmed(); |
|
426 QRegExp rx(pattern, Qt::CaseInsensitive, QRegExp::Wildcard); |
|
427 // Not case sensitive, wild cards allowed |
|
428 if ( rx.exactMatch( *it ) ) |
|
429 { |
|
430 match = true; |
|
431 break; |
|
432 } |
|
433 } |
|
434 |
|
435 if ( match ) |
|
436 { |
|
437 return true; |
|
438 } |
|
439 } |
|
440 |
|
441 return false; |
|
442 } |
|
443 |
|
444 /** |
|
445 * Get the file MIME types and forward status |
|
446 */ |
|
447 void ShareUiPrivate::getFileAttributes |
|
448 (QStringList& fileList, QStringList& mimeTypeList, QStringList& forwardStatusList) |
|
449 { |
|
450 // Attributes to read |
|
451 QList<int> attrNames; |
|
452 attrNames.append(XQApplicationManager::MimeType); |
|
453 attrNames.append(XQApplicationManager::IsForwardable); |
|
454 |
|
455 for ( QStringList::Iterator it = fileList.begin(); |
|
456 it != fileList.end(); ++it ) |
|
457 { |
|
458 QFile file(*it); |
|
459 QVariantList attrValues; |
|
460 bool ok = mAppManager.getDrmAttributes(file, attrNames, attrValues); |
|
461 if ( !ok ) |
|
462 { |
|
463 mimeTypeList << QString(); |
|
464 forwardStatusList << QString("1"); // Forward ok. |
|
465 continue; |
|
466 } |
|
467 |
|
468 int i = 0; |
|
469 foreach (QVariant v, attrValues) |
|
470 { |
|
471 if ( attrNames[i] == XQApplicationManager::MimeType ) |
|
472 mimeTypeList << v.toString(); |
|
473 if ( attrNames[i] == XQApplicationManager::IsForwardable ) |
|
474 forwardStatusList << v.toString(); |
|
475 i++; |
|
476 } |
|
477 } |
|
478 } |
|
479 #endif |
|
480 |
|
481 /** |
|
482 * Show notes |
|
483 */ |
|
484 void ShareUiPrivate::showNote(QString text) |
|
485 { |
|
486 HbNotificationDialog* dlg = new HbNotificationDialog(); |
|
487 dlg->setFocusPolicy(Qt::NoFocus); |
|
488 dlg->setAttribute(Qt::WA_DeleteOnClose, true); |
|
489 dlg->setTitle(text); |
|
490 dlg->show(); |
|
491 } |
|
492 |
|
493 /** |
|
494 * List item prototype constructor. |
|
495 */ |
|
496 ShareListItem::ShareListItem(QGraphicsItem* parent) : |
|
497 HbListViewItem(parent), |
|
498 mProviderName(NULL), |
|
499 mProviderIcon(NULL) |
|
500 { |
|
501 } |
|
502 |
|
503 /** |
|
504 * @see HbListViewItem |
|
505 */ |
|
506 HbAbstractViewItem* ShareListItem::createItem() |
|
507 { |
|
508 return new ShareListItem(*this); |
|
509 } |
|
510 |
|
511 /** |
|
512 * @see HbListViewItem |
|
513 */ |
|
514 void ShareListItem::updateChildItems() |
|
515 { |
|
516 QGraphicsLinearLayout *layout = |
|
517 new QGraphicsLinearLayout(Qt::Horizontal, this); |
|
518 |
|
519 if (!mProviderIcon) |
|
520 { |
|
521 QVariant iconName = modelIndex().data(ProviderIconRole); |
|
522 mProviderIcon = new HbIconItem(HbIcon(iconName.toString()), this); |
|
523 } |
|
524 |
|
525 if (!mProviderName) |
|
526 { |
|
527 QVariant name = modelIndex().data(ProviderNameRole); |
|
528 mProviderName = new HbTextItem(name.toString(), this); |
|
529 } |
|
530 |
|
531 layout->addItem(mProviderIcon); |
|
532 layout->addItem(mProviderName); |
|
533 |
|
534 HbListViewItem::updateChildItems(); |
|
535 } |
|
536 |
|
537 // End of file |
|
538 |