|
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: Drafts List View for displaying drafts messages. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "draftslistview.h" |
|
19 |
|
20 // SYSTEM INCLUDES |
|
21 #include <HbMenu> |
|
22 #include <HbAction> |
|
23 #include <HbToolBar> |
|
24 #include <HbToolBarExtension> |
|
25 #include <HbListView> |
|
26 #include <HbListViewItem> |
|
27 #include <HbListWidget> |
|
28 #include <HbListWidgetItem> |
|
29 #include <HbStyleLoader> |
|
30 #include <HbGroupBox> |
|
31 #include <HbFrameBackground> |
|
32 #include <HbMessageBox> |
|
33 #include <HbMainWindow> |
|
34 |
|
35 #include <QAbstractItemModel> |
|
36 #include <QSortFilterProxyModel> |
|
37 #include <QGraphicsLinearLayout> |
|
38 |
|
39 // USER INCLUDES |
|
40 #include "conversationsengine.h" |
|
41 #include "conversationsenginedefines.h" |
|
42 #include "convergedmessage.h" |
|
43 #include "convergedmessageid.h" |
|
44 |
|
45 // LOCAL CONSTANTS |
|
46 const QString POPUP_LIST_FRAME("qtg_fr_popup_list_normal"); |
|
47 const QString NEW_MESSAGE_ICON("qtg_mono_create_message"); |
|
48 const QString SORT_ICON("qtg_mono_sort"); |
|
49 |
|
50 // LOCALIZATION CONSTANTS |
|
51 |
|
52 // Long Tap |
|
53 #define LOC_COMMON_OPEN hbTrId("txt_common_menu_open") |
|
54 #define LOC_COMMON_DELETE hbTrId("txt_common_menu_delete") |
|
55 |
|
56 // View heading |
|
57 #define LOC_DLV_HEADING hbTrId("txt_messaging_title_drafts") |
|
58 |
|
59 // Menu items |
|
60 #define LOC_MENU_DELETE_ALL hbTrId("txt_messaging_opt_delete_all") |
|
61 |
|
62 // Toolbar & toolbar exension |
|
63 #define LOC_TB_EXTN_DRAFTS hbTrId("txt_messaging_button_drafts") |
|
64 #define LOC_TB_EXTN_CONVERSATIONS hbTrId("txt_messaging_button_conversations") |
|
65 |
|
66 // Confirmation note |
|
67 #define LOC_DELETE_MESSAGE hbTrId("txt_messaging_dialog_delete_message") |
|
68 #define LOC_DELETE_ALL_DRAFTS hbTrId("txt_messaging_dialog_delete_all_drafts") |
|
69 |
|
70 //--------------------------------------------------------------- |
|
71 // DraftsListView::DraftsListView |
|
72 // @see header |
|
73 //--------------------------------------------------------------- |
|
74 DraftsListView::DraftsListView(QGraphicsItem *parent) : |
|
75 MsgBaseView(parent), |
|
76 mListView(0), |
|
77 mViewExtnList(0), |
|
78 mToolBar(0) |
|
79 { |
|
80 // Delayed loading. |
|
81 connect(this->mainWindow(), SIGNAL(viewReady()), this, SLOT(doDelayedLoading())); |
|
82 } |
|
83 |
|
84 //--------------------------------------------------------------- |
|
85 // DraftsListView::~DraftsListView |
|
86 // @see header |
|
87 //--------------------------------------------------------------- |
|
88 DraftsListView::~DraftsListView() |
|
89 { |
|
90 } |
|
91 |
|
92 //--------------------------------------------------------------- |
|
93 // DraftsListView::setupMenu |
|
94 // @see header |
|
95 //--------------------------------------------------------------- |
|
96 void DraftsListView::setupMenu() |
|
97 { |
|
98 QAbstractItemModel *model = mListView->model(); |
|
99 |
|
100 // Menu items are added/removed based on the item count. |
|
101 connect(mListView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, |
|
102 SLOT(handleModelChanged()), Qt::UniqueConnection); |
|
103 connect(mListView->model(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this, |
|
104 SLOT(handleModelChanged()), Qt::UniqueConnection); |
|
105 } |
|
106 |
|
107 //--------------------------------------------------------------- |
|
108 // DraftsListView::setupToolbar |
|
109 // @see header |
|
110 //--------------------------------------------------------------- |
|
111 void DraftsListView::setupToolbar() |
|
112 { |
|
113 if (!mToolBar) { |
|
114 mToolBar = this->toolBar(); |
|
115 mToolBar->setOrientation(Qt::Horizontal); |
|
116 |
|
117 // Create & setup ToolBar Extension |
|
118 HbToolBarExtension *viewExtn = new HbToolBarExtension(); |
|
119 HbAction *viewAction = mToolBar->addExtension(viewExtn); |
|
120 viewAction->setIcon(HbIcon(SORT_ICON)); |
|
121 |
|
122 mViewExtnList = new HbListWidget(); |
|
123 mViewExtnList->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed); |
|
124 mViewExtnList->addItem(LOC_TB_EXTN_DRAFTS); |
|
125 mViewExtnList->addItem(LOC_TB_EXTN_CONVERSATIONS); |
|
126 |
|
127 HbListViewItem *prototype = mViewExtnList->listItemPrototype(); |
|
128 HbFrameBackground frame(POPUP_LIST_FRAME, HbFrameDrawer::NinePieces); |
|
129 prototype->setDefaultFrame(frame); |
|
130 |
|
131 connect(mViewExtnList, SIGNAL(activated(HbListWidgetItem*)), this, |
|
132 SLOT(handleViewExtnActivated(HbListWidgetItem*))); |
|
133 connect(mViewExtnList, SIGNAL(released(HbListWidgetItem*)), viewExtn, SLOT(close())); |
|
134 |
|
135 viewExtn->setContentWidget(mViewExtnList); |
|
136 |
|
137 // Create & setup 2nd ToolBar button. |
|
138 mToolBar->addAction(HbIcon(NEW_MESSAGE_ICON), "", this, SLOT(createNewMessage())); |
|
139 } |
|
140 } |
|
141 |
|
142 //--------------------------------------------------------------- |
|
143 // DraftsListView::setupListView |
|
144 // @see header |
|
145 //--------------------------------------------------------------- |
|
146 void DraftsListView::setupListView() |
|
147 { |
|
148 if (!mListView) { |
|
149 // Create parent layout. |
|
150 QGraphicsLinearLayout *mainLayout = new QGraphicsLinearLayout(Qt::Vertical); |
|
151 mainLayout->setContentsMargins(0, 0, 0, 0); |
|
152 mainLayout->setSpacing(0); |
|
153 |
|
154 // Create view heading. |
|
155 HbGroupBox *viewHeading = new HbGroupBox(); |
|
156 viewHeading->setHeading(LOC_DLV_HEADING); |
|
157 |
|
158 // Create List View. |
|
159 mListView = new HbListView(this); |
|
160 |
|
161 mListView->setScrollingStyle(HbScrollArea::PanOrFlick); |
|
162 mListView->setItemRecycling(true); |
|
163 mListView->setUniformItemSizes(true); |
|
164 mListView->setClampingStyle(HbScrollArea::BounceBackClamping); |
|
165 |
|
166 // Register the custorm css path. |
|
167 HbStyleLoader::registerFilePath(":/dlv"); |
|
168 // mListView->setLayoutName("custom"); |
|
169 |
|
170 // Set list item properties. |
|
171 HbListViewItem *prototype = mListView->listItemPrototype(); |
|
172 prototype->setGraphicsSize(HbListViewItem::SmallIcon); |
|
173 prototype->setStretchingStyle(HbListViewItem::StretchLandscape); |
|
174 prototype->setSecondaryTextRowCount(1, 1); |
|
175 |
|
176 // Create and set model |
|
177 QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this); |
|
178 proxyModel->setDynamicSortFilter(true); |
|
179 proxyModel->setSourceModel(ConversationsEngine::instance()->getDraftsModel()); |
|
180 proxyModel->setSortRole(TimeStamp); |
|
181 proxyModel->sort(0, Qt::DescendingOrder); |
|
182 mListView->setModel(proxyModel); |
|
183 |
|
184 // Short & Long Taps |
|
185 connect(mListView, SIGNAL(activated(QModelIndex)), this, |
|
186 SLOT(openDraftMessage(QModelIndex))); |
|
187 connect(mListView, SIGNAL(longPressed(HbAbstractViewItem*,QPointF)), this, |
|
188 SLOT(handleLongPressed(HbAbstractViewItem*,QPointF))); |
|
189 |
|
190 // Add all widgets to main layout. |
|
191 mainLayout->addItem(viewHeading); |
|
192 mainLayout->addItem(mListView); |
|
193 |
|
194 this->setLayout(mainLayout); |
|
195 } |
|
196 } |
|
197 |
|
198 //------------------------------------------------------------------------------ |
|
199 // DraftsListView::doDelayedLoading |
|
200 // @see header |
|
201 //------------------------------------------------------------------------------ |
|
202 void DraftsListView::doDelayedLoading() |
|
203 { |
|
204 setupToolbar(); |
|
205 setupListView(); |
|
206 setupMenu(); |
|
207 disconnect(this->mainWindow(), SIGNAL(viewReady()), this, SLOT(doDelayedLoading())); |
|
208 } |
|
209 |
|
210 //------------------------------------------------------------------------------ |
|
211 // DraftsListView::openDraftMessage |
|
212 // @see header |
|
213 //------------------------------------------------------------------------------ |
|
214 void DraftsListView::openDraftMessage() |
|
215 { |
|
216 openDraftMessage(mListView->currentIndex()); |
|
217 } |
|
218 |
|
219 //------------------------------------------------------------------------------ |
|
220 // DraftsListView::deleteDraftMessage |
|
221 // @see header |
|
222 //------------------------------------------------------------------------------ |
|
223 void DraftsListView::deleteDraftMessage() |
|
224 { |
|
225 QModelIndex index = mListView->currentIndex(); |
|
226 |
|
227 if (!index.isValid()) { |
|
228 return; |
|
229 } |
|
230 |
|
231 HbMessageBox::question(LOC_DELETE_MESSAGE, |
|
232 this,SLOT(onDialogDeleteMsg(HbAction*)), |
|
233 HbMessageBox::Delete | HbMessageBox::Cancel); |
|
234 |
|
235 } |
|
236 |
|
237 //------------------------------------------------------------------------------ |
|
238 // DraftsListView::deleteAllDraftMessage |
|
239 // @see header |
|
240 //------------------------------------------------------------------------------ |
|
241 void DraftsListView::deleteAllDraftMessage() |
|
242 { |
|
243 HbMessageBox::question(LOC_DELETE_ALL_DRAFTS, |
|
244 this,SLOT(onDialogDeleteAllMessages(HbAction*)), |
|
245 HbMessageBox::Delete | HbMessageBox::Cancel); |
|
246 } |
|
247 |
|
248 //------------------------------------------------------------------------------ |
|
249 // DraftsListView::createNewMessage |
|
250 // @see header |
|
251 //------------------------------------------------------------------------------ |
|
252 void DraftsListView::createNewMessage() |
|
253 { |
|
254 QVariantList param; |
|
255 param << MsgBaseView::UNIEDITOR; // target view |
|
256 param << MsgBaseView::DLV; // source view |
|
257 |
|
258 emit switchView(param); |
|
259 } |
|
260 |
|
261 //------------------------------------------------------------------------------ |
|
262 // DraftsListView::openDraftMessage |
|
263 // @see header |
|
264 //------------------------------------------------------------------------------ |
|
265 void DraftsListView::openDraftMessage(const QModelIndex &index) |
|
266 { |
|
267 QVariant msgId = index.data(ConvergedMsgId); |
|
268 QVariant msgType = index.data(MessageType); |
|
269 ConvergedMessageId convergedMsgId = ConvergedMessageId(msgId.toInt()); |
|
270 ConvergedMessage message; |
|
271 message.setMessageType((ConvergedMessage::MessageType) msgType.toInt()); |
|
272 message.setMessageId(convergedMsgId); |
|
273 |
|
274 // Launch uni-editor view |
|
275 QByteArray dataArray; |
|
276 QDataStream messageStream(&dataArray, QIODevice::WriteOnly | QIODevice::Append); |
|
277 message.serialize(messageStream); |
|
278 |
|
279 QVariantList params; |
|
280 params << MsgBaseView::UNIEDITOR; // target view |
|
281 params << MsgBaseView::DLV; // source view |
|
282 |
|
283 params << dataArray; |
|
284 emit switchView(params); |
|
285 } |
|
286 |
|
287 //------------------------------------------------------------------------------ |
|
288 // DraftsListView::handleLongPressed |
|
289 // @see header |
|
290 //------------------------------------------------------------------------------ |
|
291 void DraftsListView::handleLongPressed(HbAbstractViewItem *item, const QPointF &coords) |
|
292 { |
|
293 if (this->isVisible()) { |
|
294 |
|
295 // Set the current index as tapped items index. |
|
296 mListView->setCurrentIndex(item->modelIndex(), QItemSelectionModel::Select); |
|
297 |
|
298 HbMenu *contextMenu = new HbMenu(); |
|
299 contextMenu->setAttribute(Qt::WA_DeleteOnClose); |
|
300 // Open |
|
301 HbAction* openAction = contextMenu->addAction(LOC_COMMON_OPEN); |
|
302 connect(openAction, SIGNAL(triggered()), this, SLOT(openDraftMessage())); |
|
303 |
|
304 // Delete |
|
305 HbAction *deletAction = contextMenu->addAction(LOC_COMMON_DELETE); |
|
306 connect(deletAction, SIGNAL(triggered()), this, SLOT(deleteDraftMessage())); |
|
307 |
|
308 contextMenu->setPreferredPos(coords); |
|
309 contextMenu->show(); |
|
310 } |
|
311 } |
|
312 |
|
313 //------------------------------------------------------------------------------ |
|
314 // DraftsListView::handleViewExtnActivated |
|
315 // @see header |
|
316 //------------------------------------------------------------------------------ |
|
317 void DraftsListView::handleViewExtnActivated(HbListWidgetItem *item) |
|
318 { |
|
319 int row = mViewExtnList->row(item); |
|
320 if (CONVERSATIONS_EXTN == row) { |
|
321 QVariantList param; |
|
322 param << MsgBaseView::CLV; // target view |
|
323 param << MsgBaseView::DLV; // source view |
|
324 |
|
325 emit switchView(param); |
|
326 } |
|
327 } |
|
328 |
|
329 //------------------------------------------------------------------------------ |
|
330 // DraftsListView::handleModelChanged |
|
331 // @see header |
|
332 //------------------------------------------------------------------------------ |
|
333 void DraftsListView::handleModelChanged() |
|
334 { |
|
335 // If there are no items in list view, delete the menu item. |
|
336 HbMenu *mainMenu = this->menu(); |
|
337 if (!mListView->model()->rowCount()) { |
|
338 mainMenu->clearActions(); |
|
339 } |
|
340 else { |
|
341 if (this->menu()->isEmpty()) { |
|
342 mainMenu->addAction(LOC_MENU_DELETE_ALL, this, SLOT(deleteAllDraftMessage())); |
|
343 } |
|
344 } |
|
345 } |
|
346 |
|
347 //------------------------------------------------------------------------------ |
|
348 // DraftsListView::onDialogDeleteMsg |
|
349 // @see header |
|
350 //------------------------------------------------------------------------------ |
|
351 void DraftsListView::onDialogDeleteMsg(HbAction* action) |
|
352 { |
|
353 HbMessageBox *dlg = qobject_cast<HbMessageBox*> (sender()); |
|
354 if (action == dlg->actions().at(0)) { |
|
355 QModelIndex index = mListView->currentIndex(); |
|
356 if (index.isValid()) { |
|
357 int msgId = index.data(ConvergedMsgId).toInt(); |
|
358 QList<int> msgIdList; |
|
359 msgIdList.append(msgId); |
|
360 ConversationsEngine::instance()->deleteMessages(msgIdList); |
|
361 } |
|
362 |
|
363 } |
|
364 } |
|
365 |
|
366 //------------------------------------------------------------------------------ |
|
367 // DraftsListView::onDialogDeleteMsg |
|
368 // @see header |
|
369 //------------------------------------------------------------------------------ |
|
370 void DraftsListView::onDialogDeleteAllMessages(HbAction* action) |
|
371 { |
|
372 HbMessageBox *dlg = qobject_cast<HbMessageBox*> (sender()); |
|
373 if (action == dlg->actions().at(0)) { |
|
374 ConversationsEngine::instance()->deleteAllDraftMessages(); |
|
375 } |
|
376 } |
|
377 |
|
378 // EOF |