27 #include "nmdataplugininterface.h" |
26 #include "nmdataplugininterface.h" |
28 #include "nmmailbox.h" |
27 #include "nmmailbox.h" |
29 #include "nmfolder.h" |
28 #include "nmfolder.h" |
30 #include "nmdatapluginfactory.h" |
29 #include "nmdatapluginfactory.h" |
31 #include "nmhswidgetconsts.h" |
30 #include "nmhswidgetconsts.h" |
|
31 #include "emailtrace.h" |
32 |
32 |
33 /*! |
33 /*! |
34 Constructor |
34 Constructor |
35 */ |
35 */ |
36 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine(const NmId& monitoredMailboxId) : |
36 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine(const NmId& monitoredMailboxId) : |
37 mMailboxId(monitoredMailboxId), |
37 mMailboxId(monitoredMailboxId), |
38 mFolderId(0), |
38 mFolderId(0), |
39 mAccountName(0), |
39 mAccountName(), |
40 mUnreadCount(-1), |
40 mUnreadCount(-1), |
41 mEmailInterface(0), |
41 mEmailInterface(0), |
42 mFactory(0), |
42 mFactory(0), |
43 mAccountEventReceivedWhenSuspended(false), |
43 mAccountEventReceivedWhenSuspended(false), |
44 mMessageEventReceivedWhenSuspended(false), |
44 mMessageEventReceivedWhenSuspended(false), |
45 mSuspended(false), |
45 mSuspended(false), |
46 mUpdateTimer(0) |
46 mUpdateTimer(0) |
47 { |
47 { |
48 qDebug() << "NmHsWidgetEmailEngine() -- START"; |
48 NM_FUNCTION; |
49 |
|
50 qDebug() << "NmHsWidgetEmailEngine() -- END"; |
|
51 } |
49 } |
52 |
50 |
53 /*! |
51 /*! |
54 initilaize - Initializes the engine |
52 initilaize - Initializes the engine |
55 Works as a second phase constructor. Must be called before any other operations. |
53 Works as a second phase constructor. Must be called before any other operations. |
56 Client should connect to exceptionOccured signal before calling this method. |
54 Client should connect to exceptionOccured signal before calling this method. |
57 /return true if initialize succeedes otherwise false. If false the object is unusable. |
55 /return true if initialize succeedes otherwise false. If false the object is unusable. |
58 */ |
56 */ |
59 bool NmHsWidgetEmailEngine::initialize() |
57 bool NmHsWidgetEmailEngine::initialize() |
60 { |
58 { |
61 qDebug() << "initialize() -- START"; |
59 NM_FUNCTION; |
62 |
60 |
63 if (!constructNmPlugin()) { |
61 if (!constructNmPlugin()) { |
64 //if plugin connection fails, there's no reason to proceed |
62 //if plugin connection fails, there's no reason to proceed |
65 return false; |
63 return false; |
66 } |
64 } |
69 |
67 |
70 mUpdateTimer = new QTimer(this); |
68 mUpdateTimer = new QTimer(this); |
71 mUpdateTimer->setInterval(NmHsWidgetEmailEngineUpdateTimerValue); |
69 mUpdateTimer->setInterval(NmHsWidgetEmailEngineUpdateTimerValue); |
72 connect(mUpdateTimer, SIGNAL(timeout()), this, SLOT(handleUpdateTimeout()) ); |
70 connect(mUpdateTimer, SIGNAL(timeout()), this, SLOT(handleUpdateTimeout()) ); |
73 |
71 |
74 qDebug() << "initialize() -- END"; |
|
75 return true; |
72 return true; |
76 } |
73 } |
77 |
74 |
78 /*! |
75 /*! |
79 constructNmPlugin |
76 constructNmPlugin |
80 /post Constructs a connection to an instance of NmDataPluginInterface |
77 /post Constructs a connection to an instance of NmDataPluginInterface |
81 /return true if construction succeeded, otherwise false. |
78 /return true if construction succeeded, otherwise false. |
82 */ |
79 */ |
83 bool NmHsWidgetEmailEngine::constructNmPlugin() |
80 bool NmHsWidgetEmailEngine::constructNmPlugin() |
84 { |
81 { |
85 qDebug() << "NmHsWidgetEmailEngine::constructNmPlugin() -- START"; |
82 NM_FUNCTION; |
86 |
83 |
87 QObject* pluginInstance(0); |
84 QObject* pluginInstance(0); |
88 //Get data plugin factory instance |
85 //Get data plugin factory instance |
89 mFactory = NmDataPluginFactory::instance(); |
86 mFactory = NmDataPluginFactory::instance(); |
90 if (!mFactory) { |
87 if (!mFactory) { |
91 qDebug() << "NmHsWidgetEmailEngine::constructNmPlugin() -- mFactory FAILED"; |
88 NM_ERROR(1,"NmHsWidgetEmailEngine::constructNmPlugin() -- mFactory FAILED"); |
92 return false; |
89 return false; |
93 } |
90 } |
94 |
91 |
95 //Get plugin instance |
92 //Get plugin instance |
96 pluginInstance = mFactory->pluginInstance(mMailboxId); |
93 pluginInstance = mFactory->pluginInstance(mMailboxId); |
97 if (!pluginInstance) { |
94 if (!pluginInstance) { |
98 qDebug() << "NmHsWidgetEmailEngine::constructNmPlugin() -- pluginInstance FAILED"; |
95 NM_ERROR(1,"NmHsWidgetEmailEngine::constructNmPlugin() -- pluginInstance FAILED"); |
99 return false; |
96 return false; |
100 } |
97 } |
101 |
98 |
102 //get interface instance |
99 //get interface instance |
103 mEmailInterface = mFactory->interfaceInstance(pluginInstance); |
100 mEmailInterface = mFactory->interfaceInstance(pluginInstance); |
104 |
101 |
105 if (!mEmailInterface) { |
102 if (!mEmailInterface) { |
106 qDebug() << "NmHsWidgetEmailEngine::constructNmPlugin() -- mEmailInterface FAILED"; |
103 NM_ERROR(1,"NmHsWidgetEmailEngine::constructNmPlugin() -- mEmailInterface FAILED"); |
107 return false; |
104 return false; |
108 } |
105 } |
109 //Verify that the mailbox we are interested actually exists. |
106 //Verify that the mailbox we are interested actually exists. |
110 //Otherwise emit account deleted event (instead of just returning) |
107 //Otherwise emit account deleted event (instead of just returning) |
111 QList<NmId> ids; |
108 QList<NmId> ids; |
112 mEmailInterface->listMailboxIds(ids); |
109 mEmailInterface->listMailboxIds(ids); |
113 if(!ids.contains(mMailboxId)){ |
110 if(!ids.contains(mMailboxId)){ |
114 qDebug() << "NmHsWidgetEmailEngine::constructNmPlugin() -- !ids.contains(mMailboxId) FAILED"; |
111 NM_ERROR(1,"NmHsWidgetEmailEngine::constructNmPlugin() -- !ids.contains(mMailboxId) FAILED"); |
115 emit exceptionOccured(NmEngineExcAccountDeleted); |
112 emit exceptionOccured(NmEngineExcAccountDeleted); |
116 return false; |
113 return false; |
117 } |
114 } |
118 |
115 |
119 //retrieve folderID for this mailbox's inbox |
116 //retrieve folderID for this mailbox's inbox |
120 //If NmId equals zero, we don't have the Initial sync done for the account |
117 //If NmId equals zero, we don't have the Initial sync done for the account |
121 //This is valid at least for IMAP accounts. |
118 //This is valid at least for IMAP accounts. |
122 //Folder ID is then retrieved later when first message event is received |
119 //Folder ID is then retrieved later when first message event is received |
123 mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox); |
120 mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox); |
124 qDebug() << "NmHsWidgetEmailEngine::constructNmPlugin() -- mFolderId==" << mFolderId.id(); |
|
125 |
121 |
126 //Subscription is needed - otherwise the signals will not be received |
122 //Subscription is needed - otherwise the signals will not be received |
127 mEmailInterface->subscribeMailboxEvents(mMailboxId); |
123 mEmailInterface->subscribeMailboxEvents(mMailboxId); |
128 |
124 |
129 //Connect to events |
125 //Connect to events |
133 const QList<NmId>&, const NmId&) )); |
129 const QList<NmId>&, const NmId&) )); |
134 |
130 |
135 connect(pluginInstance, SIGNAL( mailboxEvent(NmMailboxEvent, const QList<NmId>& ) ), this, |
131 connect(pluginInstance, SIGNAL( mailboxEvent(NmMailboxEvent, const QList<NmId>& ) ), this, |
136 SLOT( handleMailboxEvent(NmMailboxEvent, const QList<NmId>&) )); |
132 SLOT( handleMailboxEvent(NmMailboxEvent, const QList<NmId>&) )); |
137 |
133 |
138 qDebug() << "NmHsWidgetEmailEngine::constructNmPlugin() -- OK"; |
|
139 |
|
140 return true; |
134 return true; |
141 } |
135 } |
142 |
136 |
143 /*! |
137 /*! |
144 Reset envelope list |
138 Reset envelope list |
145 \post mEnvelopeList.isEmpty() == true && all contained objects are deleted |
139 \post mEnvelopeList.isEmpty() == true && all contained objects are deleted |
146 */ |
140 */ |
147 void NmHsWidgetEmailEngine::resetEnvelopeList() |
141 void NmHsWidgetEmailEngine::resetEnvelopeList() |
148 { |
142 { |
149 qDebug() << "NmHsWidgetEmailEngine::resetEnvelopeList() -- START"; |
143 NM_FUNCTION; |
150 |
144 |
151 while (!mEnvelopeList.isEmpty()) { |
145 while (!mEnvelopeList.isEmpty()) { |
152 delete mEnvelopeList.takeFirst(); |
146 delete mEnvelopeList.takeFirst(); |
153 } |
147 } |
154 |
|
155 qDebug() << "NmHsWidgetEmailEngine::resetEnvelopeList() -- END"; |
|
156 } |
148 } |
157 |
149 |
158 /*! |
150 /*! |
159 Destructor |
151 Destructor |
160 */ |
152 */ |
161 NmHsWidgetEmailEngine::~NmHsWidgetEmailEngine() |
153 NmHsWidgetEmailEngine::~NmHsWidgetEmailEngine() |
162 { |
154 { |
163 qDebug() << "~NmHsWidgetEmailEngine -- START"; |
155 NM_FUNCTION; |
164 |
156 |
165 resetEnvelopeList(); |
157 resetEnvelopeList(); |
166 if (mFactory) { |
158 if (mFactory) { |
167 NmDataPluginFactory::releaseInstance(mFactory); |
159 NmDataPluginFactory::releaseInstance(mFactory); |
168 } |
160 } |
169 |
161 |
170 if (mUpdateTimer){ |
162 if (mUpdateTimer){ |
171 mUpdateTimer->stop(); |
163 mUpdateTimer->stop(); |
172 delete mUpdateTimer; |
164 delete mUpdateTimer; |
173 } |
165 } |
174 |
|
175 qDebug() << "~NmHsWidgetEmailEngine -- END"; |
|
176 } |
166 } |
177 |
167 |
178 /*! |
168 /*! |
179 getEnvelopes() provides message envelopes as a list of stack objects |
169 getEnvelopes() provides message envelopes as a list of stack objects |
180 Amount of message envelopes in the list parameter is the smallest of the following factors: |
170 Amount of message envelopes in the list parameter is the smallest of the following factors: |
184 \param maxEnvelopeAmount Client side limit for amount of message envelope count. |
174 \param maxEnvelopeAmount Client side limit for amount of message envelope count. |
185 \return count of envelopes added to list |
175 \return count of envelopes added to list |
186 */ |
176 */ |
187 int NmHsWidgetEmailEngine::getEnvelopes(QList<NmMessageEnvelope> &list, int maxEnvelopeAmount) |
177 int NmHsWidgetEmailEngine::getEnvelopes(QList<NmMessageEnvelope> &list, int maxEnvelopeAmount) |
188 { |
178 { |
189 qDebug() << "NmHsWidgetEmailEngine::getEnvelopes()"; |
179 NM_FUNCTION; |
190 list.clear(); //Reset the parameter list to avoid side effects |
180 list.clear(); //Reset the parameter list to avoid side effects |
191 int i = 0; |
181 int i = 0; |
192 for (; i < mEnvelopeList.count() && i < maxEnvelopeAmount; i++) { |
182 for (; i < mEnvelopeList.count() && i < maxEnvelopeAmount; i++) { |
193 NmMessageEnvelope env(*mEnvelopeList[i]); |
183 NmMessageEnvelope env(*mEnvelopeList[i]); |
194 list.append(env); |
184 list.append(env); |
229 |
219 |
230 \return true if everything succeeded, otherwise false |
220 \return true if everything succeeded, otherwise false |
231 */ |
221 */ |
232 bool NmHsWidgetEmailEngine::updateData() |
222 bool NmHsWidgetEmailEngine::updateData() |
233 { |
223 { |
234 qDebug() << "NmHsWidgetEmailEngine::updateData() -- START"; |
224 NM_FUNCTION; |
235 if (!mEmailInterface) { |
225 if (!mEmailInterface) { |
236 qDebug() << "NmHsWidgetEmailEngine::updateData() -- Interface missing"; |
226 NM_ERROR(1,"NmHsWidgetEmailEngine::updateData() -- Interface missing"); |
237 emit exceptionOccured(NmEngineExcFailure); //fatal error |
227 emit exceptionOccured(NmEngineExcFailure); //fatal error |
238 return false; //if interface is missing there's nothing to do |
228 return false; //if interface is missing there's nothing to do |
239 } |
229 } |
240 |
230 |
241 //reset envelope list before retrieving new items |
231 //reset envelope list before retrieving new items |
288 NmMessageEvent event, |
276 NmMessageEvent event, |
289 const NmId &folderId, |
277 const NmId &folderId, |
290 const QList<NmId> &messageIds, |
278 const QList<NmId> &messageIds, |
291 const NmId& mailboxId) |
279 const NmId& mailboxId) |
292 { |
280 { |
293 qDebug() << "NmHsWidgetEmailEngine::handleMessageEvent() -- START"; |
281 NM_FUNCTION; |
294 Q_UNUSED(event); |
282 Q_UNUSED(event); |
295 Q_UNUSED(messageIds); |
283 Q_UNUSED(messageIds); |
296 |
284 |
297 if (!mEmailInterface) { |
285 if (!mEmailInterface) { |
298 qDebug() << "NmHsWidgetEmailEngine::handleMessageEvent() -- Interface missing"; |
286 NM_ERROR(1,"NmHsWidgetEmailEngine::handleMessageEvent() -- Interface missing"); |
299 emit exceptionOccured(NmEngineExcFailure); //fatal error |
287 emit exceptionOccured(NmEngineExcFailure); //fatal error |
300 return; //if interface is missing there's nothing to do |
288 return; //if interface is missing there's nothing to do |
301 } |
289 } |
302 |
290 |
303 if (mFolderId == NmId(0)) { |
291 if (mFolderId == NmId(0)) { |
313 else { |
301 else { |
314 //start or restart the timer. Update is started when timer expires |
302 //start or restart the timer. Update is started when timer expires |
315 mUpdateTimer->start(); |
303 mUpdateTimer->start(); |
316 } |
304 } |
317 } |
305 } |
318 qDebug() << "NmHsWidgetEmailEngine::handleMessageEvent() -- END"; |
|
319 } |
306 } |
320 |
307 |
321 /*! |
308 /*! |
322 handleMailboxEvent slot. |
309 handleMailboxEvent slot. |
323 */ |
310 */ |
324 void NmHsWidgetEmailEngine::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds) |
311 void NmHsWidgetEmailEngine::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds) |
325 { |
312 { |
326 qDebug() << "NmHsWidgetEmailEngine::handleMailboxEvent() -- START"; |
313 NM_FUNCTION; |
327 if (mailboxIds.contains(mMailboxId)) { |
314 if (mailboxIds.contains(mMailboxId)) { |
328 switch (event) { |
315 switch (event) { |
329 case (NmMailboxChanged): { |
316 case (NmMailboxChanged): { |
330 if (mSuspended) { |
317 if (mSuspended) { |
331 mAccountEventReceivedWhenSuspended = true; |
318 mAccountEventReceivedWhenSuspended = true; |
342 default: |
329 default: |
343 //ignored |
330 //ignored |
344 break; |
331 break; |
345 } |
332 } |
346 } |
333 } |
347 qDebug() << "NmHsWidgetEmailEngine::handleMailboxEvent() -- END"; |
|
348 } |
334 } |
349 |
335 |
350 /*! |
336 /*! |
351 * handleUpdateTimeout slot |
337 * handleUpdateTimeout slot |
352 */ |
338 */ |
353 void NmHsWidgetEmailEngine::handleUpdateTimeout() |
339 void NmHsWidgetEmailEngine::handleUpdateTimeout() |
354 { |
340 { |
355 qDebug() << "NmHsWidgetEmailEngine::handleUpdateTimeout() -- START"; |
341 NM_FUNCTION; |
356 if (mUpdateTimer){ |
342 if (mUpdateTimer){ |
357 mUpdateTimer->stop(); |
343 mUpdateTimer->stop(); |
358 } |
344 } |
359 updateData(); |
345 updateData(); |
360 qDebug() << "NmHsWidgetEmailEngine::updateAccount() -- END"; |
|
361 } |
346 } |
362 |
347 |
363 /*! |
348 /*! |
364 Update Account data |
349 Update Account data |
365 \post if mEmailInterface exists, the mAccountName is refreshed from adapter |
350 \post if mEmailInterface exists, the mAccountName is refreshed from adapter |
366 and accountNameChanged signal is emitted. |
351 and accountNameChanged signal is emitted. |
367 */ |
352 */ |
368 bool NmHsWidgetEmailEngine::updateAccount() |
353 bool NmHsWidgetEmailEngine::updateAccount() |
369 { |
354 { |
370 qDebug() << "NmHsWidgetEmailEngine::updateAccount() -- START"; |
355 NM_FUNCTION; |
371 |
356 |
372 NmMailbox* box = NULL; |
357 NmMailbox* box = NULL; |
373 if (mEmailInterface) { |
358 if (mEmailInterface) { |
374 int err = mEmailInterface->getMailboxById(mMailboxId, box); |
359 int err = mEmailInterface->getMailboxById(mMailboxId, box); |
375 if (err) { |
360 if (err) { |
381 mAccountName = box->name(); |
366 mAccountName = box->name(); |
382 delete box; |
367 delete box; |
383 box = NULL; |
368 box = NULL; |
384 emit accountNameChanged(mAccountName); |
369 emit accountNameChanged(mAccountName); |
385 } |
370 } |
386 qDebug() << "NmHsWidgetEmailEngine::updateAccount() -- END"; |
|
387 return true; |
371 return true; |
388 } |
372 } |
389 |
373 |
390 /*! |
374 /*! |
391 suspend slot. |
375 suspend slot. |
392 \post engine will not emit signals or refresh its data during suspension. |
376 \post engine will not emit signals or refresh its data during suspension. |
393 */ |
377 */ |
394 void NmHsWidgetEmailEngine::suspend() |
378 void NmHsWidgetEmailEngine::suspend() |
395 { |
379 { |
396 qDebug() << "NmHsWidgetEmailEngine::suspend() -- START"; |
380 NM_FUNCTION; |
397 mSuspended = true; |
381 mSuspended = true; |
398 qDebug() << "NmHsWidgetEmailEngine::suspend() -- END"; |
|
399 } |
382 } |
400 |
383 |
401 /*! |
384 /*! |
402 activate slot. |
385 activate slot. |
403 \post Engine will immediately refresh all the data that has been announced to |
386 \post Engine will immediately refresh all the data that has been announced to |
404 have changed during the suspension. Events are enabled. |
387 have changed during the suspension. Events are enabled. |
405 */ |
388 */ |
406 void NmHsWidgetEmailEngine::activate() |
389 void NmHsWidgetEmailEngine::activate() |
407 { |
390 { |
408 qDebug() << "NmHsWidgetEmailEngine::activate() -- START"; |
391 NM_FUNCTION; |
409 mSuspended = false; |
392 mSuspended = false; |
410 if (mAccountEventReceivedWhenSuspended) { |
393 if (mAccountEventReceivedWhenSuspended) { |
411 mAccountEventReceivedWhenSuspended = false; |
394 mAccountEventReceivedWhenSuspended = false; |
412 updateAccount(); |
395 updateAccount(); |
413 } |
396 } |
414 if (mMessageEventReceivedWhenSuspended) { |
397 if (mMessageEventReceivedWhenSuspended) { |
415 mMessageEventReceivedWhenSuspended = false; |
398 mMessageEventReceivedWhenSuspended = false; |
416 updateData(); |
399 updateData(); |
417 } |
400 } |
418 qDebug() << "NmHsWidgetEmailEngine::activate() -- END"; |
|
419 } |
401 } |
420 |
402 |
421 /*! |
403 /*! |
422 launchMailAppInboxView slot. |
404 launchMailAppInboxView slot. |
423 \post Mail application is launched to inbox view corresponding widget's mailbox id |
405 \post Mail application is launched to inbox view corresponding widget's mailbox id |
424 */ |
406 */ |
425 void NmHsWidgetEmailEngine::launchMailAppInboxView() |
407 void NmHsWidgetEmailEngine::launchMailAppInboxView() |
426 { |
408 { |
427 qDebug() << "NmHsWidgetEmailEngine::launchMailAppInboxView() -- START"; |
409 NM_FUNCTION; |
428 |
410 |
429 XQServiceRequest request( |
411 XQServiceRequest request( |
430 emailFullServiceNameMailbox, |
412 emailFullServiceNameMailbox, |
431 emailOperationViewInbox, |
413 emailOperationViewInbox, |
432 false); |
414 false); |
434 QList<QVariant> list; |
416 QList<QVariant> list; |
435 list.append(QVariant(mMailboxId.id())); |
417 list.append(QVariant(mMailboxId.id())); |
436 |
418 |
437 request.setArguments(list); |
419 request.setArguments(list); |
438 request.send(); |
420 request.send(); |
439 |
|
440 qDebug() << "NmHsWidgetEmailEngine::launchMailAppInboxView() -- END"; |
|
441 } |
421 } |
442 |
422 |
443 /*! |
423 /*! |
444 launchMailAppMailViewer slot. |
424 launchMailAppMailViewer slot. |
445 \param messageId Defines the message opened to viewer |
425 \param messageId Defines the message opened to viewer |
446 \post Mail application is launched and viewing mail specified by |
426 \post Mail application is launched and viewing mail specified by |
447 */ |
427 */ |
448 void NmHsWidgetEmailEngine::launchMailAppMailViewer(const NmId &messageId) |
428 void NmHsWidgetEmailEngine::launchMailAppMailViewer(const NmId &messageId) |
449 { |
429 { |
450 qDebug() << "NmHsWidgetEmailEngine::launchMailAppMailViewer() -- START"; |
430 NM_FUNCTION; |
451 |
431 |
452 XQServiceRequest request( |
432 XQServiceRequest request( |
453 emailFullServiceNameMessage, |
433 emailFullServiceNameMessage, |
454 emailOperationViewMessage, |
434 emailOperationViewMessage, |
455 false); |
435 false); |