|
1 /* |
|
2 * Copyright (c) 2010 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "nmuiheaders.h" |
|
19 |
|
20 static const char *NMUI_MAILBOX_SELECTION_DIALOG_XML = ":/docml/nmmailboxselectiondialog.docml"; |
|
21 static const char *NMUI_MAILBOX_LIST_VIEW = "mailboxListView"; |
|
22 static const char *NMUI_MAILBOX_SELECTION_DIALOG = "mailboxSelectionDialog"; |
|
23 |
|
24 |
|
25 /*! |
|
26 \class NmMailboxSelectionDialog |
|
27 \brief Selection dialog for the mailboxes. Displays a dialog from which the |
|
28 user can select a mailbox. The dialog can be cancelled by pressing |
|
29 the cancel button. |
|
30 */ |
|
31 |
|
32 |
|
33 /*! |
|
34 Class constructor. |
|
35 */ |
|
36 NmMailboxSelectionDialog::NmMailboxSelectionDialog( |
|
37 NmMailboxListModel &mailboxListModel, QGraphicsItem *parent /* = 0 */) |
|
38 : mMailboxListView(NULL), |
|
39 mMailboxSelectionDialog(NULL), |
|
40 mContentItemModel(NULL), |
|
41 mMailboxListModel(mailboxListModel), |
|
42 mParent(parent), |
|
43 mMailboxId(0) |
|
44 { |
|
45 NM_FUNCTION; |
|
46 } |
|
47 |
|
48 |
|
49 /*! |
|
50 Class destructor. |
|
51 */ |
|
52 NmMailboxSelectionDialog::~NmMailboxSelectionDialog() |
|
53 { |
|
54 NM_FUNCTION; |
|
55 |
|
56 delete mContentItemModel; |
|
57 delete mMailboxListView; |
|
58 delete mMailboxSelectionDialog; |
|
59 } |
|
60 |
|
61 |
|
62 /*! |
|
63 Initializes and displays the dialog |
|
64 Signal selectionDialogClosed will be emitted when the dialog is closed |
|
65 */ |
|
66 void NmMailboxSelectionDialog::open() |
|
67 { |
|
68 NM_FUNCTION; |
|
69 |
|
70 mMailboxId = 0; |
|
71 |
|
72 // Initialize the UI and fetch the mailbox items into the list. |
|
73 if (initializeUi() && populateListItems()) { |
|
74 mMailboxSelectionDialog->setModal(true); |
|
75 mMailboxSelectionDialog->open(this,SLOT(dialogClosed(HbAction*))); |
|
76 mMailboxSelectionDialog->show(); |
|
77 } |
|
78 } |
|
79 |
|
80 /*! |
|
81 Slot that is called when dialog is closed |
|
82 */ |
|
83 void NmMailboxSelectionDialog::dialogClosed(HbAction *action) |
|
84 { |
|
85 NM_FUNCTION; |
|
86 |
|
87 Q_UNUSED(action); |
|
88 |
|
89 // Store the ID of the selected mailbox into the given argument. |
|
90 NM_COMMENT(QString("NmMailboxSelectionDialog::dialogClosed() return %1"). |
|
91 arg(mMailboxId.id())); |
|
92 |
|
93 emit selectionDialogClosed(mMailboxId); |
|
94 } |
|
95 |
|
96 /*! |
|
97 Creates the view for the mailbox selection dialog. |
|
98 |
|
99 \return True if the widgets were loaded and set up successfully, false |
|
100 otherwise. |
|
101 */ |
|
102 bool NmMailboxSelectionDialog::initializeUi() |
|
103 { |
|
104 NM_FUNCTION; |
|
105 |
|
106 // Use the document loader to load the widgets. |
|
107 HbDocumentLoader documentLoader; |
|
108 bool documentLoaded(false); |
|
109 |
|
110 QObjectList objectList; |
|
111 objectList.append(this); |
|
112 |
|
113 documentLoader.setObjectTree(objectList); |
|
114 mWidgetList = |
|
115 documentLoader.load(NMUI_MAILBOX_SELECTION_DIALOG_XML, &documentLoaded); |
|
116 |
|
117 if (documentLoaded && mWidgetList.count()) { |
|
118 // Get the dialog and the list view widgets. |
|
119 mMailboxSelectionDialog = |
|
120 qobject_cast<HbDialog*>(documentLoader.findWidget(NMUI_MAILBOX_SELECTION_DIALOG)); |
|
121 mMailboxListView = |
|
122 qobject_cast<HbListView*>(documentLoader.findWidget(NMUI_MAILBOX_LIST_VIEW)); |
|
123 } |
|
124 |
|
125 if (!mMailboxSelectionDialog || !mMailboxListView) { |
|
126 NM_ERROR(1,"NmMailboxSelectionDialog::initializeUi(): Failed to load widgets!"); |
|
127 return false; |
|
128 } |
|
129 |
|
130 // Set up the loaded widgets. |
|
131 mMailboxSelectionDialog->setParentItem(mParent); |
|
132 |
|
133 // Disable timeout. |
|
134 mMailboxSelectionDialog->setTimeout(HbDialog::NoTimeout); |
|
135 |
|
136 // Connect the list view to the slot. When the user selects an item |
|
137 // from the list, the slot should be called. |
|
138 connect(mMailboxListView, SIGNAL(activated(QModelIndex)), |
|
139 this, SLOT(itemActivated(QModelIndex))); |
|
140 |
|
141 // Item model initialization, data will be populated later. |
|
142 mContentItemModel = new QStandardItemModel(this); |
|
143 |
|
144 // Set the model for the list view. Use default prototype for now. |
|
145 mMailboxListView->setModel(mContentItemModel); |
|
146 |
|
147 // Set the item prototype. |
|
148 mMailboxSelectionDialog->setContentWidget(mMailboxListView); |
|
149 |
|
150 return true; |
|
151 } |
|
152 |
|
153 |
|
154 /*! |
|
155 Populates the list items (mailboxes). |
|
156 |
|
157 \return True if success, false otherwise. |
|
158 */ |
|
159 bool NmMailboxSelectionDialog::populateListItems() |
|
160 { |
|
161 NM_FUNCTION; |
|
162 |
|
163 const int count = mMailboxListModel.rowCount(); |
|
164 |
|
165 if (!mContentItemModel || count == 0) { |
|
166 // The UI skeleton was not set up successfully or no mailboxes exist! |
|
167 return false; |
|
168 } |
|
169 |
|
170 NmMailboxMetaData *metaData = NULL; |
|
171 QStandardItem *item = NULL; |
|
172 |
|
173 EmailMailboxInfo mailboxInfo; |
|
174 for (int i = 0; i < count; ++i) { |
|
175 metaData = mailboxMetaData(i); |
|
176 |
|
177 if (metaData) { |
|
178 QString domainName = metaData->address(); |
|
179 QString iconName = mailboxInfo.mailboxIcon(domainName); |
|
180 HbIcon mailboxIcon( iconName ); |
|
181 |
|
182 // Construct the item and append it into the list. |
|
183 item = new QStandardItem(mailboxIcon.qicon(), metaData->name()); |
|
184 mContentItemModel->appendRow(item); |
|
185 } |
|
186 } |
|
187 |
|
188 return true; |
|
189 } |
|
190 |
|
191 |
|
192 /*! |
|
193 Retrieve the metadata for the given mailbox. |
|
194 |
|
195 \param index The index of the mailbox in the list model. |
|
196 \return A mailbox meta data instance. |
|
197 */ |
|
198 NmMailboxMetaData *NmMailboxSelectionDialog::mailboxMetaData(int index) const |
|
199 { |
|
200 NM_FUNCTION; |
|
201 |
|
202 QVariant mailbox = mMailboxListModel.data(mMailboxListModel.index(index, 0)); |
|
203 NmMailboxMetaData *mailboxMetaData = mailbox.value<NmMailboxMetaData*>(); |
|
204 return mailboxMetaData; |
|
205 } |
|
206 |
|
207 |
|
208 /*! |
|
209 This slot is invoked when a list item is selected. |
|
210 |
|
211 \param index The index of the selected item. |
|
212 */ |
|
213 void NmMailboxSelectionDialog::itemActivated(QModelIndex index) |
|
214 { |
|
215 NM_FUNCTION; |
|
216 |
|
217 const int rowIndex = index.row(); |
|
218 NmMailboxMetaData *metaData = mailboxMetaData(rowIndex); |
|
219 |
|
220 if (metaData && mMailboxSelectionDialog) { |
|
221 mMailboxId = metaData->id(); |
|
222 mMailboxSelectionDialog->close(); |
|
223 } |
|
224 } |
|
225 |
|
226 |
|
227 // End of file. |