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