|
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: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QTimer> |
|
19 |
|
20 #include "nmhswidgetemailengine.h" |
|
21 #include "nmmessageenvelope.h" |
|
22 |
|
23 #include "nmframeworkadapter_stub.h" //Stub implementation for Sprint 2 |
|
24 |
|
25 |
|
26 /*! |
|
27 Constructor |
|
28 */ |
|
29 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine() : |
|
30 mAccountName(0), |
|
31 mUnreadCount(0), |
|
32 m_stub_adapter(0) |
|
33 { |
|
34 m_stub_adapter = new NmFrameworkAdapter_stub(); |
|
35 |
|
36 updateData(); |
|
37 updateAccount(); |
|
38 |
|
39 //TODO: To be removed in Sprint 3 |
|
40 |
|
41 connect( m_stub_adapter, SIGNAL( messageEvent() ) , this, SLOT( updateData() ) ); |
|
42 connect( m_stub_adapter, SIGNAL( mailboxEvent() ) , this, SLOT( updateAccount() ) ); |
|
43 |
|
44 //END |
|
45 |
|
46 } |
|
47 |
|
48 |
|
49 |
|
50 /*! |
|
51 Reset envelope list |
|
52 \post mEnvelopeList.isEmpty() == true && all contained objects are deleted |
|
53 */ |
|
54 void NmHsWidgetEmailEngine::resetEnvelopeList() |
|
55 { |
|
56 while (!mEnvelopeList.isEmpty()) |
|
57 { |
|
58 delete mEnvelopeList.takeFirst(); |
|
59 } |
|
60 |
|
61 } |
|
62 |
|
63 /*! |
|
64 Destructor |
|
65 */ |
|
66 NmHsWidgetEmailEngine::~NmHsWidgetEmailEngine() |
|
67 { |
|
68 resetEnvelopeList(); |
|
69 delete m_stub_adapter; |
|
70 } |
|
71 |
|
72 |
|
73 /*! |
|
74 Shrink envelope list |
|
75 \post mEnvelopeList.count() <= KMaxNumberOfEnvelopesProvided, |
|
76 other objects are removed and freed. |
|
77 */ |
|
78 void NmHsWidgetEmailEngine::shrinkEnvelopeList() |
|
79 { |
|
80 //remove & delete the last object from the list |
|
81 //until we have shrinked the list to be small enough |
|
82 while ( mEnvelopeList.count() > KMaxNumberOfEnvelopesProvided ) |
|
83 { |
|
84 delete mEnvelopeList.takeLast(); |
|
85 } |
|
86 } |
|
87 |
|
88 |
|
89 /*! |
|
90 getEnvelopes() provides message envelopes. |
|
91 Amount of message envelopes returned is the smallest of the following factors: |
|
92 'KMaxNumberOfEnvelopesProvided', 'maxEnvelopeAmount', 'amount of available envelopes'. |
|
93 |
|
94 \param list list to be filled with message envelopes |
|
95 \param maxEnvelopeAmount Client side limit for amount of message envelope count. |
|
96 \return count of envelopes added to list |
|
97 */ |
|
98 int NmHsWidgetEmailEngine::getEnvelopes(QList<NmMessageEnvelope> &list, int maxEnvelopeAmount) |
|
99 { |
|
100 int i = 0; |
|
101 for (; i<mEnvelopeList.count() && i<maxEnvelopeAmount ; i++ ) |
|
102 { |
|
103 NmMessageEnvelope env( *mEnvelopeList[i] ); |
|
104 list.append(env); |
|
105 } |
|
106 return i; |
|
107 } |
|
108 |
|
109 /*! |
|
110 UnreadCount |
|
111 |
|
112 \return count of unread mails |
|
113 */ |
|
114 int NmHsWidgetEmailEngine::unreadCount() |
|
115 { |
|
116 return mUnreadCount; |
|
117 } |
|
118 |
|
119 /*! |
|
120 accountName |
|
121 |
|
122 \return name of the monitored account |
|
123 */ |
|
124 QString NmHsWidgetEmailEngine::accountName() |
|
125 { |
|
126 return mAccountName; |
|
127 } |
|
128 |
|
129 /*! |
|
130 Refresh email data. |
|
131 \post mData is refreshed with valid content so that it has |
|
132 valid data with maximum of KMaxNumberOfEnvelopesProvided envelopes |
|
133 |
|
134 NOTE: THIS IS STUB IMPLEMENTATION UNTIL SPRINT 3 |
|
135 */ |
|
136 void NmHsWidgetEmailEngine::updateData() |
|
137 { |
|
138 resetEnvelopeList(); |
|
139 //TODO Sprint 3: Get the list from the server |
|
140 /* TEST DATA */ |
|
141 |
|
142 resetEnvelopeList(); |
|
143 m_stub_adapter->listMessages(mEnvelopeList); |
|
144 |
|
145 emit mailDataChanged(); |
|
146 mUnreadCount = calculateUnreadCount( mEnvelopeList ); |
|
147 emit unreadCountChanged( mUnreadCount ); |
|
148 /* TEST DATA END*/ |
|
149 |
|
150 //TODO: Sprint3 at this point shrink the list to KMaxNumberOfEnvelopesProvided |
|
151 shrinkEnvelopeList(); |
|
152 } |
|
153 |
|
154 /*! |
|
155 Update Account data |
|
156 \post mAccountName is valid |
|
157 |
|
158 NOTE: THIS IS STUB IMPLEMENTATION UNTIL SPRINT 3 |
|
159 */ |
|
160 void NmHsWidgetEmailEngine::updateAccount() |
|
161 { |
|
162 mAccountName = m_stub_adapter->mailboxName(); |
|
163 emit accountNameChanged (mAccountName); |
|
164 } |
|
165 |
|
166 /*! |
|
167 Calculate unread count from envelope list. |
|
168 |
|
169 \param envelopeList List of envelopes from which unread envelopes are to be count. |
|
170 \return count of unreads in list |
|
171 if unread count is < KMaxUnreadCount, otherwise KMaxUnreadCount. |
|
172 */ |
|
173 int NmHsWidgetEmailEngine::calculateUnreadCount(QList<NmMessageEnvelope*> envelopeList) |
|
174 { |
|
175 int unreadCount = 0; |
|
176 |
|
177 QList<NmMessageEnvelope*>::const_iterator itEnd(envelopeList.constEnd()); |
|
178 QList<NmMessageEnvelope*>::const_iterator it = envelopeList.constBegin(); |
|
179 |
|
180 for ( ; (it != itEnd)&&(unreadCount < KMaxUnreadCount); ++it) |
|
181 { |
|
182 if ( !(*it)->isRead() ) |
|
183 { |
|
184 unreadCount++; |
|
185 } |
|
186 } |
|
187 return unreadCount; |
|
188 } |