emailuis/nmhswidget/src/nmhswidgetemailrow.cpp
changeset 18 578830873419
child 20 ecc8def7944a
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     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 <QDebug>
       
    19 #include <QGraphicsLinearLayout>
       
    20 #include <hbdocumentloader.h>
       
    21 #include <hblabel.h>
       
    22 #include <hbextendedlocale.h>
       
    23 #include "nmicons.h"
       
    24 #include "nmcommon.h"
       
    25 #include "nmhswidgetemailrow.h"
       
    26 #include "nmhswidgetconsts.h"
       
    27 #include "nmmessageenvelope.h"
       
    28 
       
    29 NmHsWidgetEmailRow::NmHsWidgetEmailRow(QGraphicsItem *parent, Qt::WindowFlags flags)
       
    30     : HbWidget(parent, flags),
       
    31       mSenderLabel(0),
       
    32       mSubjectLabel(0),
       
    33       mTimeLabel(0),
       
    34       mNewMailIcon(0)
       
    35 {
       
    36     qDebug() << "NmHsWidgetEmailRow::NmHsWidgetEmailRow IN -->>";
       
    37     
       
    38     loadDocML();
       
    39     
       
    40     qDebug() << "NmHsWidgetEmailRow::NmHsWidgetEmailRow OUT <<--";
       
    41 }
       
    42 
       
    43 /*!
       
    44     Destructor
       
    45 */
       
    46 NmHsWidgetEmailRow::~NmHsWidgetEmailRow()
       
    47 {
       
    48     qDebug() << "NmHsWidgetEmailRow::~NmHsWidgetEmailRow IN -->>";
       
    49 
       
    50     qDebug() << "NmHsWidgetEmailRow::~NmHsWidgetEmailRow OUT <<--";
       
    51 }
       
    52     
       
    53 /*!
       
    54     Loads layout data and child items from docml file
       
    55 */
       
    56 void NmHsWidgetEmailRow::loadDocML()
       
    57 {
       
    58     qDebug() << "NmHsWidgetEmailRow::loadDocML IN -->>";
       
    59     
       
    60     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
       
    61     layout->setContentsMargins(KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin,
       
    62             KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin);
       
    63     layout->setSpacing(KNmHsWidgetContentsMargin);        
       
    64     
       
    65     // Use document loader to load the contents
       
    66     HbDocumentLoader loader;
       
    67     bool ok = false;
       
    68     loader.load( KNmHsWidgetMailRowDocML, &ok);
       
    69     Q_ASSERT_X(ok, "nmhswidget", "invalid email docml file");
       
    70     
       
    71     //find container widget
       
    72     QGraphicsWidget *container = loader.findWidget(KNmHsWidgetMailRowContainer);
       
    73     Q_ASSERT_X((container != 0), "nmhswidget", "email container not found!");
       
    74     layout->addItem(container);
       
    75 
       
    76     //child items possible to update
       
    77     mSenderLabel = static_cast<HbLabel*>(loader.findWidget(KNmHsWidgetMailRowSenderLabel));
       
    78     mSubjectLabel = static_cast<HbLabel*>(loader.findWidget(KNmHsWidgetMailRowSubjectLabel));
       
    79     mTimeLabel = static_cast<HbLabel*>(loader.findWidget(KNmHsWidgetMailRowTimeLabel));
       
    80 
       
    81     //Icons
       
    82     mNewMailIcon = static_cast<HbLabel*>(loader.findWidget(KNmHsWidgetMailRowNewMailIcon));
       
    83     // KNmHsWidgetMailRowLeftIcon is not yet used, because followup information is not shown in client side
       
    84     // and thus it is not wanted to be shown in widget side
       
    85     mStatusIcons.append( static_cast<HbLabel*>(loader.findWidget(KNmHsWidgetMailRowRightIcon)));
       
    86     mStatusIcons.append( static_cast<HbLabel*>(loader.findWidget(KNmHsWidgetMailRowMiddleIcon)));
       
    87     mStatusIcons.append( static_cast<HbLabel*>(loader.findWidget(KNmHsWidgetMailRowLeftIcon)));
       
    88 
       
    89     //hide all the icons first to avoid blinking
       
    90     hideIcons();
       
    91     
       
    92     setLayout(layout);
       
    93     
       
    94     qDebug() << "NmHsWidgetEmailRow::loadDocML OUT <<--";
       
    95 }
       
    96 
       
    97 /*!
       
    98     Sets the data provided as a parameter to the UI components
       
    99     \param envelope message envelope representing an email
       
   100 */
       
   101 void NmHsWidgetEmailRow::updateMailData( const NmMessageEnvelope& envelope )
       
   102     {
       
   103     qDebug() << "NmHsWidgetEmailRow::updateMailData IN -->>";
       
   104     //hide all icons, so no previous data is messing with the new
       
   105     hideIcons();
       
   106     
       
   107     //Show sender name if it is available, otherwise show email address
       
   108     QString senderDisplayName = envelope.sender().displayName();
       
   109     if ( !senderDisplayName.isNull() && !senderDisplayName.isEmpty()  )
       
   110         {
       
   111         mSenderLabel->setPlainText( senderDisplayName );
       
   112         }
       
   113     else
       
   114         {
       
   115         mSenderLabel->setPlainText ( envelope.sender().address() );
       
   116         }
       
   117     
       
   118     //Set subject
       
   119     //Todo: what about empty subject?
       
   120     mSubjectLabel->setPlainText( envelope.subject() );
       
   121     
       
   122     //Set Date with locale support
       
   123     //Time shown if message is sent today, otherwise show date
       
   124     HbExtendedLocale locale = HbExtendedLocale::system();
       
   125     QDateTime now = QDateTime::currentDateTime();
       
   126     QDateTime time = envelope.sentTime(); 
       
   127     if ( time.date() == now.date() )
       
   128         {
       
   129         //time format specification
       
   130         QString timeSpec = r_qtn_time_usual;
       
   131         mTimeLabel->setPlainText( locale.format(time.time(), timeSpec) );
       
   132         }
       
   133     else
       
   134         {
       
   135         QString dateSpec = r_qtn_date_without_year;
       
   136         mTimeLabel->setPlainText( locale.format(time.date(), dateSpec) );
       
   137         }
       
   138     
       
   139     //set new icons to widget based on the data
       
   140     setIconsToWidget( envelope );
       
   141     qDebug() << "NmHsWidgetEmailRow::updateMailData OUT <<--";
       
   142     }
       
   143 
       
   144 /*!
       
   145     hide icons from widget
       
   146 */
       
   147 void NmHsWidgetEmailRow::hideIcons()
       
   148     {
       
   149     qDebug() << "NmHsWidgetEmailRow::hideIcons IN -->>";
       
   150     for( int i = 0; i<mStatusIcons.count();i++)
       
   151         {
       
   152         mStatusIcons[i]->hide();
       
   153         }
       
   154     mNewMailIcon->hide();
       
   155     qDebug() << "NmHsWidgetEmailRow::hideIcons OUT <<--";
       
   156     }
       
   157 
       
   158 
       
   159 /*!
       
   160     Set icons to widget
       
   161 */
       
   162 void NmHsWidgetEmailRow::setIconsToWidget( const NmMessageEnvelope& envelope )
       
   163 {
       
   164     qDebug() << "NmHsWidgetEmailRow::setIconsToWidget IN -->>";
       
   165 
       
   166     bool unreadMail = !envelope.isRead();
       
   167     bool attachment = envelope.hasAttachments();
       
   168     int priority = envelope.priority();
       
   169     
       
   170     if(unreadMail){
       
   171         //Todo: set unread icon when available
       
   172         mNewMailIcon->show();
       
   173     }
       
   174     
       
   175     // Here we have list for priority and attachment icons. Later it is easy to add
       
   176     // followup icon or something else if needed.
       
   177     QList<HbIcon> iconList;
       
   178     
       
   179     // Priority icon is added to list first thus it is always shown most right.
       
   180     switch (priority){
       
   181         case NmMessagePriorityLow:
       
   182             iconList.append(NmIcons::getIcon(NmIcons::NmIconPriorityLow));
       
   183             break;
       
   184         case NmMessagePriorityHigh:
       
   185             iconList.append(NmIcons::getIcon(NmIcons::NmIconPriorityHigh));
       
   186             break;
       
   187         case NmMessagePriorityNormal:
       
   188         default:
       
   189             // Normal priority has no icon
       
   190             break;
       
   191         }
       
   192     
       
   193     // Attachment icon is always shown on the left side of attachment icon if it
       
   194     // exists. Otherwise in the most right.
       
   195     if(attachment){
       
   196         iconList.append(NmIcons::getIcon(NmIcons::NmIconAttachment));
       
   197     }
       
   198 
       
   199     // Here we show icons added to the iconList in the order they have been added.
       
   200     for(int count = 0; count<iconList.count(); count++){
       
   201         mStatusIcons[count]->setIcon(iconList[count]);
       
   202         mStatusIcons[count]->setAlignment(Qt::AlignRight);
       
   203         mStatusIcons[count]->show();
       
   204     }
       
   205     
       
   206     qDebug() << "NmHsWidgetEmailRow::setIconsToWidget OUT <<--";
       
   207 }
       
   208