|
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: NMail viewer view header widget implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "nmuiheaders.h" |
|
20 |
|
21 static const qreal NmHeaderLineOpacity = 0.4; |
|
22 static const int NmTextWrapWordOrAnywhere = 4; |
|
23 |
|
24 /*! |
|
25 \class NmViewerHeader |
|
26 \brief Mail viewer header area class |
|
27 */ |
|
28 |
|
29 /*! |
|
30 Constructor |
|
31 */ |
|
32 NmViewerHeader::NmViewerHeader(QGraphicsItem *parent) : |
|
33 HbWidget(parent), |
|
34 mMessage(NULL), |
|
35 mSubject(NULL), |
|
36 mSent(NULL), |
|
37 mPrioIcon(NULL), |
|
38 mHeaderBox(NULL), |
|
39 mRecipientsBox(NULL), |
|
40 mViewerView(NULL) |
|
41 { |
|
42 NM_FUNCTION; |
|
43 |
|
44 loadWidgets(); |
|
45 setFlag(QGraphicsItem::ItemHasNoContents, false); |
|
46 } |
|
47 |
|
48 /*! |
|
49 Destructor |
|
50 */ |
|
51 NmViewerHeader::~NmViewerHeader() |
|
52 { |
|
53 NM_FUNCTION; |
|
54 } |
|
55 |
|
56 /*! |
|
57 setter for nmviewer view |
|
58 */ |
|
59 void NmViewerHeader::setView(NmViewerView* view) |
|
60 { |
|
61 NM_FUNCTION; |
|
62 |
|
63 mViewerView = view; |
|
64 } |
|
65 |
|
66 /*! |
|
67 Load widgets from XML for the header |
|
68 */ |
|
69 void NmViewerHeader::loadWidgets() |
|
70 { |
|
71 NM_FUNCTION; |
|
72 |
|
73 // Scale header widget to screen width |
|
74 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding); |
|
75 |
|
76 // Add header box |
|
77 if (!mHeaderBox) { |
|
78 mHeaderBox = new HbGroupBox(this); |
|
79 HbStyle::setItemName(mHeaderBox, "headergroupbox"); |
|
80 mHeaderBox->setObjectName("ViewerHeaderRecipients"); |
|
81 mHeaderBox->setFontSpec(HbFontSpec(HbFontSpec::Secondary)); |
|
82 mHeaderBox->setCollapsable(true); |
|
83 } |
|
84 |
|
85 // Add sent time |
|
86 if (!mSent){ |
|
87 mSent = new HbTextItem(this); |
|
88 HbStyle::setItemName(mSent, "sent"); |
|
89 mSent->setObjectName("ViewerHeaderSent"); |
|
90 } |
|
91 |
|
92 // Add priority icon |
|
93 if (!mPrioIcon){ |
|
94 mPrioIcon = new HbIconItem(this); |
|
95 mSent->setObjectName("ViewerHeaderPrioIcon"); |
|
96 HbStyle::setItemName(mPrioIcon, "prioicon"); |
|
97 } |
|
98 |
|
99 // Add subject |
|
100 if (!mSubject){ |
|
101 mSubject = new HbTextItem(this); |
|
102 HbStyle::setItemName(mSubject, "subject"); |
|
103 mSubject->setObjectName("ViewerHeaderSubject"); |
|
104 mSubject->setTextWrapping(Hb::TextWordWrap); |
|
105 } |
|
106 } |
|
107 |
|
108 |
|
109 /*! |
|
110 Reimplementation to do some extra painting |
|
111 */ |
|
112 void NmViewerHeader::paint( |
|
113 QPainter *painter, |
|
114 const QStyleOptionGraphicsItem *option, |
|
115 QWidget *widget) |
|
116 { |
|
117 NM_FUNCTION; |
|
118 |
|
119 Q_UNUSED(option); |
|
120 Q_UNUSED(widget); |
|
121 if (painter) { |
|
122 painter->save(); |
|
123 painter->setOpacity(NmHeaderLineOpacity); |
|
124 QLineF line1( rect().topLeft().x(), rect().bottomRight().y(), |
|
125 rect().bottomRight().x(), rect().bottomRight().y()); |
|
126 painter->drawLine(line1); |
|
127 if (mHeaderBox){ |
|
128 QRectF headerBoxGeometry = mHeaderBox->geometry(); |
|
129 QLineF line2( headerBoxGeometry.topLeft().x(), headerBoxGeometry.bottomRight().y(), |
|
130 headerBoxGeometry.bottomRight().x(), headerBoxGeometry.bottomRight().y()); |
|
131 painter->drawLine(line2); |
|
132 } |
|
133 painter->restore(); |
|
134 } |
|
135 } |
|
136 |
|
137 /*! |
|
138 Setter for message object |
|
139 */ |
|
140 void NmViewerHeader::setMessage(NmMessage* message) |
|
141 { |
|
142 NM_FUNCTION; |
|
143 |
|
144 mMessage=message; |
|
145 setHeaderData(); |
|
146 } |
|
147 |
|
148 |
|
149 /*! |
|
150 Function updates data in already created objects. New message pointer |
|
151 comes from viewer view, ownership is not transferred. |
|
152 This function gets called when message body is fetched and |
|
153 to/cc/bcc data needs to be updated |
|
154 */ |
|
155 void NmViewerHeader::updateMessageData(NmMessage* message) |
|
156 { |
|
157 NM_FUNCTION; |
|
158 |
|
159 if (message){ |
|
160 mMessage=message; |
|
161 // Set recipients to text edit field as html |
|
162 NmAddress sender = mMessage->envelope().sender(); |
|
163 if (mRecipientsBox){ |
|
164 mRecipientsBox->setHtml(formatRecipientList(addressToDisplay(sender), |
|
165 mMessage->envelope().toRecipients(), |
|
166 mMessage->envelope().ccRecipients())); |
|
167 } |
|
168 } |
|
169 } |
|
170 |
|
171 /*! |
|
172 Function sets data to header area based on message data |
|
173 */ |
|
174 void NmViewerHeader::setHeaderData() |
|
175 { |
|
176 NM_FUNCTION; |
|
177 |
|
178 if (mMessage) { |
|
179 // Background is all white always, so force text color to black |
|
180 QColor textColor(Qt::black); |
|
181 const NmMessageEnvelope &envelope = mMessage->envelope(); |
|
182 const QString dispName = envelope.sender().displayName(); |
|
183 if (dispName.length()>0){ |
|
184 mSenderName = NmUtilities::cleanupDisplayName(dispName); |
|
185 } |
|
186 else { |
|
187 mSenderName = envelope.sender().address(); |
|
188 } |
|
189 if (mHeaderBox) { |
|
190 mHeaderBox->setHeading(mSenderName); |
|
191 } |
|
192 // Set subject text |
|
193 if (mSubject){ |
|
194 QString subjectText = envelope.subject(); |
|
195 if (subjectText.length()){ |
|
196 mSubject->setText(subjectText); |
|
197 } |
|
198 else{ |
|
199 mSubject->setText(hbTrId("txt_mail_dblist_val_no_subject")); |
|
200 } |
|
201 mSubject->setTextColor(textColor); |
|
202 } |
|
203 if (mSent){ |
|
204 HbExtendedLocale locale = HbExtendedLocale::system(); |
|
205 QDateTime localTime = envelope.sentTime().addSecs(locale.universalTimeOffset()); |
|
206 QTime time = localTime.time(); |
|
207 QDate sentLocalDate = localTime.date(); |
|
208 QString shortDateSpec = r_qtn_date_without_year; |
|
209 QString shortTimeSpec = r_qtn_time_usual; |
|
210 QString text = locale.format(sentLocalDate, shortDateSpec); |
|
211 text += " "; |
|
212 text += locale.format(time, shortTimeSpec); |
|
213 mSent->setText(text); |
|
214 mSent->setTextColor(textColor); |
|
215 } |
|
216 if (mPrioIcon){ |
|
217 switch (envelope.priority()){ |
|
218 case NmMessagePriorityLow: |
|
219 mPrioIcon->setObjectName("ViewerHeaderPriorityLow"); |
|
220 mPrioIcon->setIcon(NmIcons::getIcon(NmIcons::NmIconPriorityLow)); |
|
221 mPrioIcon->show(); |
|
222 break; |
|
223 case NmMessagePriorityHigh: |
|
224 mPrioIcon->setObjectName("ViewerHeaderPriorityHigh"); |
|
225 mPrioIcon->setIcon(NmIcons::getIcon(NmIcons::NmIconPriorityHigh)); |
|
226 mPrioIcon->show(); |
|
227 break; |
|
228 case NmMessagePriorityNormal: |
|
229 default: |
|
230 // Normal priority has no icon so hide the label hide. |
|
231 mPrioIcon->setObjectName("ViewerHeaderPriorityNormal"); |
|
232 break; |
|
233 } |
|
234 } |
|
235 } |
|
236 createExpandableHeader(); |
|
237 } |
|
238 |
|
239 |
|
240 /*! |
|
241 Function can be used to rescale the header area. |
|
242 */ |
|
243 void NmViewerHeader::rescaleHeader(const QSizeF layoutReso) |
|
244 { |
|
245 NM_FUNCTION; |
|
246 |
|
247 setMinimumWidth(layoutReso.width()); |
|
248 setMaximumWidth(layoutReso.width()); |
|
249 } |
|
250 |
|
251 /*! |
|
252 Function creates expandable header area with group box |
|
253 */ |
|
254 void NmViewerHeader::createExpandableHeader() |
|
255 { |
|
256 NM_FUNCTION; |
|
257 |
|
258 if (mHeaderBox) { // Initialize recipient box |
|
259 if (!mRecipientsBox){ |
|
260 mRecipientsBox = new HbLabel(); |
|
261 HbStyle::setItemName(mRecipientsBox, "recipients"); |
|
262 mRecipientsBox->setFontSpec(HbFontSpec(HbFontSpec::Secondary)); |
|
263 mRecipientsBox->setTextWrapping((Hb::TextWrapping)NmTextWrapWordOrAnywhere); |
|
264 } |
|
265 |
|
266 // Set recipients to text edit field as html |
|
267 NmAddress sender = mMessage->envelope().sender(); |
|
268 if (mMessage) { |
|
269 mRecipientsBox->setHtml(formatRecipientList(addressToDisplay(sender), |
|
270 mMessage->envelope().toRecipients(), |
|
271 mMessage->envelope().ccRecipients())); |
|
272 } |
|
273 mHeaderBox->setContentWidget(mRecipientsBox); |
|
274 // Set box collapsed as default |
|
275 mHeaderBox->setCollapsed(true); |
|
276 } |
|
277 } |
|
278 |
|
279 /*! |
|
280 Function formats recipient list to be displayed in HTML format. |
|
281 */ |
|
282 QString NmViewerHeader::formatRecipientList(const QString &sender, |
|
283 const QList<NmAddress> &to, |
|
284 const QList<NmAddress> &cc) |
|
285 { |
|
286 NM_FUNCTION; |
|
287 |
|
288 QString result; |
|
289 result.append("<html><body link=\"blue\" topmargin=\"0\" leftmargin=\"0\" marginheight=\"0\""); |
|
290 result.append("marginwidth=\"0\">"); |
|
291 result.append("<font color=\"black\" face=\""); |
|
292 result.append("Nokia Sans"); |
|
293 result.append("\"size=\"3\">"); |
|
294 // Set text in HTML format based on layout direction |
|
295 if (qApp->layoutDirection()==Qt::RightToLeft){ |
|
296 result.append("<p style=\"margin-right: '0'; margin-left: '0'\" dir=\"rtl\"><b>"); |
|
297 result.append(hbTrId("txt_mail_list_from")); |
|
298 result.append(" </b>"); |
|
299 result.append(sender); |
|
300 result.append("<br><b>"); |
|
301 result.append(hbTrId("txt_mail_list_to")); |
|
302 result.append(" </b>"); |
|
303 int reciCount = to.count(); |
|
304 for (int i=0; i < reciCount; i++) { |
|
305 result.append(addressToDisplay(to[i])); |
|
306 if (i!=reciCount-1) { |
|
307 result.append(" "); |
|
308 } |
|
309 } |
|
310 reciCount = cc.count(); |
|
311 if (reciCount) { |
|
312 result.append("<br><b>"); |
|
313 result.append(hbTrId("txt_mail_list_cc")); |
|
314 result.append(" </b>"); |
|
315 for (int i=0; i < reciCount; i++) { |
|
316 result.append(addressToDisplay(cc[i])); |
|
317 if (i!=reciCount-1) { |
|
318 result.append(" "); |
|
319 } |
|
320 } |
|
321 } |
|
322 } |
|
323 else{ |
|
324 result.append("<p style=\"margin-right: '0'; margin-left: '0'\" dir=\"ltr\"><b>"); |
|
325 result.append(hbTrId("txt_mail_list_from")); |
|
326 result.append(" </b>"); |
|
327 result.append(sender); |
|
328 result.append("<br><b>"); |
|
329 result.append(hbTrId("txt_mail_list_to")); |
|
330 result.append(" </b>"); |
|
331 int reciCount = to.count(); |
|
332 for (int i=0; i < reciCount; i++) { |
|
333 result.append(addressToDisplay(to[i])); |
|
334 if (i!=reciCount-1) { |
|
335 result.append("; "); |
|
336 } |
|
337 } |
|
338 reciCount = cc.count(); |
|
339 if (reciCount) { |
|
340 result.append("<br><b>"); |
|
341 result.append(hbTrId("txt_mail_list_cc")); |
|
342 result.append(" </b>"); |
|
343 for (int i=0; i < reciCount; i++) { |
|
344 result.append(addressToDisplay(cc[i])); |
|
345 if (i!=reciCount-1) { |
|
346 result.append("; "); |
|
347 } |
|
348 } |
|
349 } |
|
350 } |
|
351 result.append("</p></font></body></html>"); |
|
352 return result; |
|
353 } |
|
354 |
|
355 /*! |
|
356 Function retursn address string from NmAddress to |
|
357 be displayed in HTML format |
|
358 */ |
|
359 QString NmViewerHeader::addressToDisplay(const NmAddress &addr) |
|
360 { |
|
361 NM_FUNCTION; |
|
362 |
|
363 QString dispName; |
|
364 if (addr.displayName().length()!=0){ |
|
365 dispName.append(NmUtilities::cleanupDisplayName(addr.displayName())); |
|
366 } |
|
367 else{ |
|
368 dispName.append(addr.address()); |
|
369 } |
|
370 QString ret="<a href=mailto:"; |
|
371 ret.append(addr.address()); |
|
372 ret.append(">"); |
|
373 ret.append(dispName); |
|
374 ret.append("</a>"); |
|
375 return ret; |
|
376 } |
|
377 |