emailuis/nmhswidget/src/nmhswidgetemailrow.cpp
changeset 75 47d84de1c893
parent 72 64e38f08e49c
equal deleted inserted replaced
72:64e38f08e49c 75:47d84de1c893
     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 <QGraphicsLinearLayout>
       
    19 #include <hbdocumentloader.h>
       
    20 #include <hblabel.h>
       
    21 #include <hbextendedlocale.h>
       
    22 #include <hbframedrawer.h>
       
    23 #include <hbframeitem.h>
       
    24 #include <hbcolorscheme.h>
       
    25 #include <hbevent.h>
       
    26 #include <hbtapgesture.h>
       
    27 #include <hbinstantfeedback.h>
       
    28 #include "nmicons.h"
       
    29 #include "nmcommon.h"
       
    30 #include "nmhswidgetemailrow.h"
       
    31 #include "nmhswidgetconsts.h"
       
    32 #include "emailtrace.h"
       
    33 
       
    34 NmHsWidgetEmailRow::NmHsWidgetEmailRow(QGraphicsItem *parent, Qt::WindowFlags flags) :
       
    35     HbWidget(parent, flags), 
       
    36     mSenderLabel(0), 
       
    37     mSubjectLabel(0), 
       
    38     mTimeLabel(0), 
       
    39     mNewMailIcon(0),
       
    40     mSeparatorIcon(0), 
       
    41     mMessageId(0),
       
    42     mBackgroundLayoutItem(0)
       
    43 {
       
    44     NM_FUNCTION;
       
    45     grabGesture(Qt::TapGesture);
       
    46 }
       
    47 
       
    48 /*!
       
    49  Destructor
       
    50  */
       
    51 NmHsWidgetEmailRow::~NmHsWidgetEmailRow()
       
    52 {
       
    53     NM_FUNCTION;
       
    54 }
       
    55 
       
    56 /*!
       
    57  Returns id of message shown
       
    58  */
       
    59 NmId NmHsWidgetEmailRow::messageId()
       
    60 {
       
    61     NM_FUNCTION;
       
    62     return mMessageId;
       
    63 
       
    64 }
       
    65 
       
    66 /*
       
    67  Setup email row ui
       
    68   Must be called after constructor.
       
    69    /return true if loading succeeded, otherwise false. False indicates that object is unusable.
       
    70  */
       
    71 bool NmHsWidgetEmailRow::setupUI()
       
    72     {
       
    73     NM_FUNCTION;
       
    74     
       
    75     if(!loadDocML() || !setupGraphics()){
       
    76         return false;
       
    77     }
       
    78     return true;
       
    79     }
       
    80 
       
    81 
       
    82 
       
    83 /*!
       
    84  Loads layout data and child items from docml file 
       
    85   /return true if loading succeeded, otherwise false. False indicates that object is unusable.
       
    86  */
       
    87 bool NmHsWidgetEmailRow::loadDocML()
       
    88 {
       
    89     NM_FUNCTION;
       
    90 
       
    91     QT_TRY{   
       
    92         // Use document loader to load the contents
       
    93         HbDocumentLoader loader;
       
    94         bool ok(false);
       
    95         loader.load(KNmHsWidgetMailRowDocML, &ok);
       
    96         if (!ok) {
       
    97             NM_ERROR(1,"NmHsWidgetEmailRow::loadDocML fail @ loader");
       
    98             return false;
       
    99         }
       
   100     
       
   101         QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
       
   102     
       
   103         //Do the layout stuff and Set layout before next return to avoid memoryleak
       
   104         layout->setContentsMargins(KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin,
       
   105             KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin);
       
   106         layout->setSpacing(KNmHsWidgetContentsMargin);
       
   107         setLayout(layout);
       
   108     
       
   109         //find container widget
       
   110         QGraphicsWidget *container = loader.findWidget(KNmHsWidgetMailRowContainer);
       
   111         if (!container) {
       
   112             NM_ERROR(1,"NmHsWidgetEmailRow::loadDocML fail @ container");
       
   113             return false;
       
   114         }
       
   115         layout->addItem(container);
       
   116     
       
   117         //child items possible to update
       
   118         //separator
       
   119         mSeparatorIcon = static_cast<HbLabel*> (loader.findWidget(KNmHsWidgetMailSeparatorIcon));
       
   120     
       
   121         //labels
       
   122         mSenderLabel = static_cast<HbLabel*> (loader.findWidget(KNmHsWidgetMailRowSenderLabel));
       
   123         mSubjectLabel = static_cast<HbLabel*> (loader.findWidget(KNmHsWidgetMailRowSubjectLabel));
       
   124         mTimeLabel = static_cast<HbLabel*> (loader.findWidget(KNmHsWidgetMailRowTimeLabel));
       
   125     
       
   126         //icons
       
   127         mNewMailIcon = static_cast<HbWidget*> (loader.findWidget(KNmHsWidgetMailRowNewMailIcon));
       
   128         // KNmHsWidgetMailRowLeftIcon is not yet used, because followup information is not shown in client side
       
   129         // and thus it is not wanted to be shown in widget side
       
   130         mStatusIcons.append(static_cast<HbLabel*> (loader.findWidget(KNmHsWidgetMailRowRightIcon)));
       
   131         mStatusIcons.append(static_cast<HbLabel*> (loader.findWidget(KNmHsWidgetMailRowMiddleIcon)));
       
   132         mStatusIcons.append(static_cast<HbLabel*> (loader.findWidget(KNmHsWidgetMailRowLeftIcon)));
       
   133     
       
   134         //Verify that items are valid
       
   135         if (!mSenderLabel || !mSubjectLabel || !mTimeLabel || !mNewMailIcon || !mSeparatorIcon) {
       
   136             NM_ERROR(1,"NmHsWidgetEmailRow::loadDocML fail @ labels & icons");
       
   137             return false;
       
   138         }
       
   139         //Verify all mStatusIcons
       
   140         for (int i = 0; i < mStatusIcons.length(); i++) {
       
   141             if (!mStatusIcons.at(i)) {
       
   142                 return false;
       
   143             }
       
   144         }
       
   145         
       
   146         return true;
       
   147     }
       
   148     QT_CATCH(...){
       
   149         return false;
       
   150     }
       
   151 }
       
   152 
       
   153 /*
       
   154  Setup graphics that cannot be loaded from docml.
       
   155   /return true if loading succeeded, otherwise false. False indicates that object is unusable.
       
   156  */
       
   157 bool NmHsWidgetEmailRow::setupGraphics()
       
   158     {
       
   159     NM_FUNCTION;
       
   160     
       
   161     HbFrameDrawer* newMailIconFrameDrawer = 0;
       
   162     HbFrameItem* newMailIconFrameItem = 0;
       
   163     HbFrameDrawer* backgroundFrameDrawer = 0;
       
   164     QT_TRY{ 
       
   165         //separator icon
       
   166         HbIcon separatorIcon("qtg_graf_divider_h_thin");
       
   167         mSeparatorIcon->setIcon(separatorIcon);
       
   168         
       
   169         //new email icon
       
   170         newMailIconFrameDrawer = new HbFrameDrawer("qtg_fr_list_new_item",
       
   171             HbFrameDrawer::ThreePiecesVertical);
       
   172         newMailIconFrameItem = new HbFrameItem(newMailIconFrameDrawer);
       
   173         mNewMailIcon->setBackgroundItem(newMailIconFrameItem);
       
   174         
       
   175         //hide all the icons first to avoid blinking
       
   176         hideIcons();
       
   177     
       
   178         //pressed background
       
   179         backgroundFrameDrawer = new HbFrameDrawer("qtg_fr_hsitems_pressed", HbFrameDrawer::NinePieces);
       
   180         mBackgroundLayoutItem = new HbFrameItem( backgroundFrameDrawer );
       
   181         setBackgroundItem( mBackgroundLayoutItem );
       
   182         mBackgroundLayoutItem->hide();
       
   183             
       
   184         return true;
       
   185     }
       
   186     QT_CATCH(...){
       
   187         if(!newMailIconFrameItem && newMailIconFrameDrawer){
       
   188             delete newMailIconFrameDrawer;
       
   189             newMailIconFrameDrawer = NULL;
       
   190         }
       
   191         if(!mBackgroundLayoutItem && backgroundFrameDrawer){
       
   192             delete backgroundFrameDrawer;
       
   193             backgroundFrameDrawer = NULL;
       
   194         }
       
   195         return false;
       
   196     }
       
   197 }
       
   198 
       
   199 /*!
       
   200  Sets the data provided as a parameter to the UI components
       
   201  \param envelope message envelope representing an email
       
   202  */
       
   203 void NmHsWidgetEmailRow::updateMailData(const NmMessageEnvelope& envelope)
       
   204 {
       
   205     NM_FUNCTION;
       
   206 
       
   207     mEnvelope = NmMessageEnvelope(envelope);
       
   208     
       
   209     //hide all icons, so no previous data is messing with the new
       
   210     hideIcons();
       
   211 
       
   212     mMessageId = envelope.messageId();
       
   213     //Show sender name if it is available, otherwise show email address
       
   214     QString senderDisplayName = envelope.sender().displayName();
       
   215     if (!senderDisplayName.isNull() && !senderDisplayName.isEmpty()) {
       
   216         mSenderLabel->setPlainText(senderDisplayName);
       
   217     }
       
   218     else {
       
   219         mSenderLabel->setPlainText(envelope.sender().address());
       
   220     }
       
   221 
       
   222     //Set subject
       
   223     mSubjectLabel->setPlainText(envelope.subject());
       
   224     
       
   225     mMessageSentTime = envelope.sentTime();
       
   226     updateDateTime();
       
   227     
       
   228     //set new icons to widget based on the data
       
   229     setIconsToWidget( envelope );
       
   230     
       
   231     //set fonts color and size
       
   232     setFontsSize(mEnvelope.isRead());
       
   233     setHighlighedFontsColor(false);
       
   234     }
       
   235 
       
   236 /*!
       
   237     updateDateTime to label using correct locale
       
   238 */
       
   239 void NmHsWidgetEmailRow::updateDateTime()
       
   240     {
       
   241     NM_FUNCTION;
       
   242     //Set Date with locale support
       
   243     //Time shown if message is sent today, otherwise show date
       
   244     HbExtendedLocale locale = HbExtendedLocale::system();
       
   245     QDateTime now = QDateTime::currentDateTime();
       
   246     //change time to locale time. mMessageSentTime is always in GMT
       
   247     QDateTime localTime = mMessageSentTime.addSecs(locale.universalTimeOffset());
       
   248     if ( localTime.date() == now.date() )
       
   249         {
       
   250         //time format specification
       
   251         QString timeSpec = r_qtn_time_usual;
       
   252         mTimeLabel->setPlainText( locale.format(localTime.time(), timeSpec) );
       
   253         }
       
   254     else
       
   255         {
       
   256         QString dateSpec = r_qtn_date_without_year;
       
   257         mTimeLabel->setPlainText( locale.format(localTime.date(), dateSpec) );
       
   258         }
       
   259     }
       
   260 
       
   261 /*!
       
   262  hide icons from widget
       
   263  */
       
   264 void NmHsWidgetEmailRow::hideIcons()
       
   265 {
       
   266     NM_FUNCTION;
       
   267     for (int i = 0; i < mStatusIcons.count(); i++) {
       
   268         mStatusIcons.at(i)->hide();
       
   269     }
       
   270     mNewMailIcon->hide();
       
   271 }
       
   272 
       
   273 /*!
       
   274  Set icons to widget
       
   275  */
       
   276 void NmHsWidgetEmailRow::setIconsToWidget(const NmMessageEnvelope& envelope)
       
   277 {
       
   278     NM_FUNCTION;
       
   279 
       
   280     bool unreadMail = !envelope.isRead();
       
   281     bool attachment = envelope.hasAttachments();
       
   282     int priority = envelope.priority();
       
   283 
       
   284     if (unreadMail) {
       
   285         mNewMailIcon->show();
       
   286     }
       
   287 
       
   288     // Here we have list for priority and attachment icons. Later it is easy to add
       
   289     // followup icon or something else if needed.
       
   290     QList<HbIcon> iconList;
       
   291 
       
   292     // Priority icon is added to list first thus it is always shown most right.
       
   293     switch (priority) {
       
   294         case NmMessagePriorityLow:
       
   295             iconList.append(NmIcons::getIcon(NmIcons::NmIconPriorityLow));
       
   296             break;
       
   297         case NmMessagePriorityHigh:
       
   298             iconList.append(NmIcons::getIcon(NmIcons::NmIconPriorityHigh));
       
   299             break;
       
   300         case NmMessagePriorityNormal:
       
   301         default:
       
   302             // Normal priority has no icon
       
   303             break;
       
   304     }
       
   305 
       
   306     // Attachment icon is always shown on the left side of attachment icon if it
       
   307     // exists. Otherwise in the most right.
       
   308     if (attachment) {
       
   309         iconList.append(NmIcons::getIcon(NmIcons::NmIconAttachment));
       
   310     }
       
   311 
       
   312     // Here we show icons added to the iconList in the order they have been added.
       
   313     for (int count = 0; count < iconList.count(); count++) {
       
   314         mStatusIcons.at(count)->setIcon(iconList.at(count));
       
   315         mStatusIcons.at(count)->show();
       
   316     }
       
   317 }
       
   318 
       
   319 
       
   320 /*!
       
   321     sets fonts size. Unread and read mails are shown differently
       
   322 */
       
   323 void NmHsWidgetEmailRow::setFontsSize( bool read )
       
   324     {
       
   325     NM_FUNCTION;
       
   326     HbFontSpec fontSpec;
       
   327     
       
   328     if(!read){
       
   329         fontSpec.setRole(HbFontSpec::Primary);
       
   330         mTimeLabel->fontSpec().setRole(HbFontSpec::Primary);
       
   331     }
       
   332     else{
       
   333         fontSpec.setRole(HbFontSpec::Secondary);
       
   334         mTimeLabel->fontSpec().setRole(HbFontSpec::Secondary);
       
   335     }  
       
   336     
       
   337     qreal size;
       
   338     bool found = style()->parameter(QString("hb-param-text-height-secondary"), size );
       
   339     if (found) {
       
   340         fontSpec.setTextHeight(size);
       
   341     }
       
   342     
       
   343     mSenderLabel->setFontSpec(fontSpec);
       
   344     mSubjectLabel->setFontSpec(fontSpec);
       
   345     }
       
   346 
       
   347 /*!
       
   348     sets fonts color.
       
   349     /param bool pressed indicates if row is pressed down or not
       
   350 */
       
   351 void NmHsWidgetEmailRow::setHighlighedFontsColor( bool pressed )
       
   352     {
       
   353     NM_FUNCTION;
       
   354     QColor newFontColor;
       
   355     
       
   356     if(pressed){
       
   357         newFontColor = HbColorScheme::color("qtc_hs_list_item_pressed");
       
   358     }
       
   359     else if(mEnvelope.isRead()){
       
   360         newFontColor = HbColorScheme::color("qtc_hs_list_item_content_normal");
       
   361     }
       
   362     else{
       
   363         newFontColor = HbColorScheme::color("qtc_hs_list_item_title_normal");
       
   364     }
       
   365  
       
   366     mSenderLabel->setTextColor(newFontColor);
       
   367     mSubjectLabel->setTextColor(newFontColor);
       
   368     mTimeLabel->setTextColor(newFontColor);
       
   369     }
       
   370 
       
   371 /*!
       
   372     change background highlight
       
   373     /param bool show if true then shown, false hide
       
   374 */
       
   375 void NmHsWidgetEmailRow::showHighlight( bool show )
       
   376     {
       
   377     NM_FUNCTION;
       
   378     
       
   379     if(show){
       
   380         mBackgroundLayoutItem->show();
       
   381     }
       
   382     else{
       
   383         mBackgroundLayoutItem->hide();
       
   384     }
       
   385     }
       
   386 
       
   387 /*
       
   388  * NmHsWidgetEmailRow::gestureEvent(QGestureEvent *event)
       
   389  */
       
   390 void NmHsWidgetEmailRow::gestureEvent(QGestureEvent *event)
       
   391 {
       
   392     NM_FUNCTION;
       
   393     if(!event){
       
   394         return;
       
   395     }
       
   396     HbTapGesture *gesture = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture));
       
   397     if(!gesture){
       
   398         return;
       
   399     }
       
   400     switch (gesture->state()) {
       
   401         case Qt::GestureStarted:
       
   402             setHighlighedFontsColor(true);
       
   403             showHighlight(true);
       
   404         break;
       
   405         case Qt::GestureCanceled:
       
   406             setHighlighedFontsColor(false);
       
   407             showHighlight(false);
       
   408         break;
       
   409         case Qt::GestureFinished:
       
   410             setHighlighedFontsColor(false);
       
   411             showHighlight(false);
       
   412             if (gesture->tapStyleHint() == HbTapGesture::Tap) {
       
   413                 HbInstantFeedback::play(HbFeedback::BasicItem);
       
   414                 emit mailViewerLaunchTriggered(mMessageId);
       
   415             }
       
   416         break;
       
   417     default: 
       
   418         break;
       
   419     }
       
   420 }
       
   421 
       
   422 /*
       
   423  * NmHsWidgetEmailRow::event()
       
   424  */
       
   425 bool NmHsWidgetEmailRow::event( QEvent *event )
       
   426 {
       
   427     NM_FUNCTION;
       
   428     QEvent::Type eventType = event->type();
       
   429     if( eventType == HbEvent::ThemeChanged ){
       
   430         setHighlighedFontsColor(false);
       
   431         return true;
       
   432     }
       
   433     return HbWidget::event(event);
       
   434 }