emailuis/nmhswidget/src/nmhswidget.cpp
changeset 20 ecc8def7944a
parent 18 578830873419
child 23 2dc6caa42ec3
equal deleted inserted replaced
18:578830873419 20:ecc8def7944a
     1 /*
     1 /*
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3  * All rights reserved.
     3  * All rights reserved.
     4  * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5  * under the terms of "Eclipse Public License v1.0"
     5  * under the terms of "Eclipse Public License v1.0"
     6  * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    16  */
    16  */
    17 #include <QDebug>
    17 #include <QDebug>
    18 #include <QtGui>
    18 #include <QtGui>
    19 #include <QGraphicsLinearLayout>
    19 #include <QGraphicsLinearLayout>
    20 #include <hbcolorscheme.h>
    20 #include <hbcolorscheme.h>
       
    21 #include "nmcommon.h"
    21 #include "nmhswidget.h"
    22 #include "nmhswidget.h"
    22 #include "nmhswidgetemailengine.h"
    23 #include "nmhswidgetemailengine.h"
    23 #include "nmmessageenvelope.h"
    24 #include "nmmessageenvelope.h"
    24 #include "nmhswidgettitlerow.h"
    25 #include "nmhswidgettitlerow.h"
    25 #include "nmhswidgetemailrow.h"
    26 #include "nmhswidgetemailrow.h"
    26 #include "nmhswidgetconsts.h"
    27 #include "nmhswidgetconsts.h"
    27 
    28 
    28 NmHsWidget::NmHsWidget(QGraphicsItem *parent, Qt::WindowFlags flags)
    29 NmHsWidget::NmHsWidget(QGraphicsItem *parent, Qt::WindowFlags flags)
    29     : HbWidget(parent, flags), 
    30     : HbWidget(parent, flags), 
    30       mEngine( new NmHsWidgetEmailEngine() ),
    31       mEngine(0),
    31       mRowLayout(0),
    32       mRowLayout(0),
    32       mTitleRow( new NmHsWidgetTitleRow() )
    33       mTitleRow( new NmHsWidgetTitleRow() ),
       
    34       mAccountId(0)
    33 {
    35 {
    34     qDebug() << "NmHsWidget::NmHsWidget IN -->>";
    36     qDebug() << "NmHsWidget::NmHsWidget IN -->>";
    35    
    37 
    36     this->setContentsMargins( KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin,
    38     this->setContentsMargins( KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin,
    37             KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin);
    39             KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin);
    38     
       
    39     //Setup layout
    40     //Setup layout
    40     mRowLayout = new QGraphicsLinearLayout(Qt::Vertical);
    41     mRowLayout = new QGraphicsLinearLayout(Qt::Vertical);
    41     mRowLayout->setContentsMargins(KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin,
    42     mRowLayout->setContentsMargins(KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin,
    42             KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin);
    43             KNmHsWidgetContentsMargin, KNmHsWidgetContentsMargin);
    43     mRowLayout->setSpacing(KNmHsWidgetContentsMargin);
    44     mRowLayout->setSpacing(KNmHsWidgetContentsMargin);
    44     mRowLayout->addItem(mTitleRow);
    45     mRowLayout->addItem(mTitleRow);
    45     setLayout(mRowLayout);
    46     setLayout(mRowLayout);
    46     
    47     
    47     //Do the initial data setting
    48     qDebug() << "NmHsWidget::NmHsWidget OUT <<--";
    48     updateAccountName( mEngine->accountName() );
    49 }
       
    50 
       
    51 /*!
       
    52     Destructor
       
    53 */
       
    54 NmHsWidget::~NmHsWidget()
       
    55 {
       
    56     qDebug() << "NmHsWidget::~NmHsWidget IN -->>";
       
    57     if(mEngine)
       
    58         {
       
    59         delete mEngine;
       
    60         mEngine = NULL;
       
    61         }
       
    62     qDebug() << "NmHsWidget::~NmHsWidget OUT <<--";
       
    63 }
       
    64 
       
    65 /*!
       
    66     \fn void NmHsWidget::onShow()
       
    67 
       
    68     Shows the widget
       
    69 */
       
    70 void NmHsWidget::onShow()
       
    71 {
       
    72     qDebug() << "NmHsWidget::onShow IN -->>";
       
    73     if (mEngine)
       
    74         {
       
    75         mEngine->activate();
       
    76         }
       
    77     qDebug() << "NmHsWidget::onShow OUT <<--";
       
    78 }
       
    79 
       
    80 
       
    81 /*!
       
    82     \fn void NmHsWidget::onHide()
       
    83 
       
    84     Hides the widget
       
    85 */
       
    86 void NmHsWidget::onHide()
       
    87 {
       
    88     qDebug() << "NmHsWidget::onHide IN -->>";
       
    89     if (mEngine)
       
    90         {
       
    91         mEngine->suspend();
       
    92         }
       
    93     qDebug() << "NmHsWidget::onHide OUT <<--";
       
    94 }
       
    95 
       
    96 
       
    97 /*!
       
    98     Initializes the widget.
       
    99 */
       
   100 void NmHsWidget::onInitialize()
       
   101 {
       
   102     qDebug() << "NmHsWidget::onInitialize IN -->>";
       
   103 
       
   104     mEngine = new NmHsWidgetEmailEngine( mAccountId ); 
       
   105     mTitleRow->updateAccountName(mEngine->accountName());
    49     updateMailData();
   106     updateMailData();
    50     updateUnreadCount( mEngine->unreadCount() );
   107     mTitleRow->updateUnreadCount(mEngine->unreadCount());
    51     
   108     
    52     //Get signals about changes in mail data
   109     //Get signals about changes in mail data
    53     connect(mEngine, SIGNAL( mailDataChanged() )
   110     connect(mEngine, SIGNAL( mailDataChanged() )
    54             ,this, SLOT( updateMailData() ) );
   111             ,this, SLOT( updateMailData() ) );
    55     
   112     
    56     //Get Signals about changes in unread count
   113     //Get Signals about changes in unread count
    57     connect(mEngine, SIGNAL( unreadCountChanged(const int&) )
   114     connect(mEngine, SIGNAL( unreadCountChanged(const int&) )
    58             ,this, SLOT( updateUnreadCount(const int&) ) );
   115             ,mTitleRow, SLOT( updateUnreadCount(const int&) ) );
    59     
   116     
    60     //Get signals about account name changes
   117     //Get signals about account name changes
    61     connect(mEngine, SIGNAL( accountNameChanged(const QString&) )
   118     connect(mEngine, SIGNAL( accountNameChanged(const QString&) )
    62             ,this, SLOT( updateAccountName(const QString&) ) );
   119             ,mTitleRow, SLOT( updateAccountName(const QString&) ) );
    63     
   120 
    64     qDebug() << "NmHsWidget::NmHsWidget OUT <<--";
       
    65 }
       
    66 
       
    67 /*!
       
    68     Destructor
       
    69 */
       
    70 NmHsWidget::~NmHsWidget()
       
    71 {
       
    72     qDebug() << "NmHsWidget::~NmHsWidget IN -->>";
       
    73     if(mEngine)
       
    74         {
       
    75         delete mEngine;
       
    76         mEngine = NULL;
       
    77         }
       
    78     qDebug() << "NmHsWidget::~NmHsWidget OUT <<--";
       
    79 }
       
    80 
       
    81 /*!
       
    82     Initializes the widget.
       
    83 */
       
    84 void NmHsWidget::onInitialize()
       
    85 {
       
    86     qDebug() << "NmHsWidget::onInitialize IN -->>";
       
    87     
       
    88     qDebug() << "NmHsWidget::onInitialize OUT <<--";  
   121     qDebug() << "NmHsWidget::onInitialize OUT <<--";  
    89 }
   122 }
    90 
   123 
    91 /*!
   124 /*!
    92     Uninitializes the widget.
   125     Uninitializes the widget.
   117             KNmHsWidgetShoulderRadius);
   150             KNmHsWidgetShoulderRadius);
   118     painter->setOpacity(1); //set opacity back to default
   151     painter->setOpacity(1); //set opacity back to default
   119 }
   152 }
   120 
   153 
   121 /*!
   154 /*!
   122     updateAccountName slot 
       
   123 */
       
   124 void NmHsWidget::updateAccountName(const QString& accountName)
       
   125 {
       
   126     qDebug() << "NmHsWidget::updateAccountName IN -->>";
       
   127     qDebug() << "New account name is: " << accountName;
       
   128     
       
   129     mTitleRow->updateAccountName(accountName);
       
   130         
       
   131     qDebug() << "NmHsWidget::updateAccountName OUT <<--"; 
       
   132 }
       
   133 
       
   134 /*!
       
   135     updateUnreadCount slot
       
   136 */
       
   137 void NmHsWidget::updateUnreadCount(const int& unreadCount )
       
   138 {
       
   139     qDebug() << "NmHsWidget::updateUnreadCount IN -->>";
       
   140     qDebug() << "New unread count is: " << QString::number(unreadCount);
       
   141    
       
   142     mTitleRow->updateUnreadCount(unreadCount);
       
   143     
       
   144     qDebug() << "NmHsWidget::updateUnreadCount OUT <<--"; 
       
   145 }
       
   146 
       
   147 /*!
       
   148     updateMailData slot
   155     updateMailData slot
   149 */
   156 */
   150 void NmHsWidget::updateMailData()
   157 void NmHsWidget::updateMailData()
   151 {
   158 {
   152     qDebug() << "NmHsWidget::updateData IN -->>";
   159     qDebug() << "NmHsWidget::updateData IN -->>";
   168         }
   175         }
   169     qDebug() << "NmHsWidget::updateData OUT <<--"; 
   176     qDebug() << "NmHsWidget::updateData OUT <<--"; 
   170 }
   177 }
   171 
   178 
   172 /*!
   179 /*!
       
   180     Sets monitored account id from given string
       
   181     Needed for home screen framework thich supports only QString type properties
       
   182 */
       
   183 void NmHsWidget::setAccountId(const QString &text)
       
   184 {
       
   185     qDebug() << "NmHsWidget::setAccountId IN -->>"; 
       
   186     bool ok;
       
   187     quint64 id = text.toULongLong(&ok);
       
   188     if (!ok)
       
   189         {
       
   190         // TODO: assert here if conversion failed?
       
   191         qDebug() << "NmHsWidget::setAccountId: invalid account ID data!!!"; 
       
   192         mAccountId.setId(0);
       
   193         }
       
   194     else
       
   195         {
       
   196         mAccountId.setId(id);
       
   197         }
       
   198     qDebug() << "NmHsWidget::setAccountId OUT <<--"; 
       
   199 }
       
   200 
       
   201 /*!
       
   202     Returns monitored account id as a string
       
   203     Needed for home screen framework which supports only QString type properties
       
   204 */
       
   205 QString NmHsWidget::accountId() const
       
   206 {
       
   207     qDebug() << "NmHsWidget::accountId()"; 
       
   208     return QString::number(mAccountId.id());
       
   209 }
       
   210 
       
   211 /*!
   173     Updates list to include correct amount of mail row widgets
   212     Updates list to include correct amount of mail row widgets
   174 */
   213 */
   175 void NmHsWidget::updateMailRowsList(int mailCount)
   214 void NmHsWidget::updateMailRowsList(int mailCount)
   176 {
   215 {
   177     qDebug() << "NmHsWidget::updateMailRowsList IN -->>";
   216     qDebug() << "NmHsWidget::updateMailRowsList IN -->>";
       
   217     qDebug() << "NmHsWidget - mMailRows.count() == " <<  mMailRows.count();
       
   218     qDebug() << "NmHsWidget - ordered count == " <<  mailCount;
   178     while (mMailRows.count() != mailCount)
   219     while (mMailRows.count() != mailCount)
   179         {
   220         {
   180         //more mails to show than rows
   221         //more mails to show than rows
   181         if (mMailRows.count() < mailCount)
   222         if (mMailRows.count() < mailCount)
   182             {
   223             {
       
   224             qDebug() << "NmHsWidget - add new mail row";
   183             NmHsWidgetEmailRow *row = new NmHsWidgetEmailRow();
   225             NmHsWidgetEmailRow *row = new NmHsWidgetEmailRow();
   184             mMailRows.append(row);
   226             mMailRows.append(row);
   185             mRowLayout->addItem(row);            
   227             mRowLayout->addItem(row);            
   186             }
   228             }
   187         //too many rows
   229         //too many rows
   188         else if (mMailRows.count() > mailCount)
   230         else if (mMailRows.count() > mailCount)
   189             {
   231             {
       
   232             qDebug() << "NmHsWidget - remove mail row";
   190             mRowLayout->removeItem(mMailRows.last());
   233             mRowLayout->removeItem(mMailRows.last());
   191             delete mMailRows.takeLast();
   234             delete mMailRows.takeLast();
   192             }
   235             }
   193         }
   236         }
   194     __ASSERT_ALWAYS( mMailRows.count() == mailCount, User::Panic(_L("Invalid"), 500) );
   237     __ASSERT_ALWAYS( mMailRows.count() == mailCount, User::Panic(_L("Invalid"), 500) );
   195     qDebug() << "NmHsWidget::updateMailRowsList OUT <<--";
   238     qDebug() << "NmHsWidget::updateMailRowsList OUT <<--";
   196 }
   239 }
       
   240 
       
   241 /*!
       
   242     mousePressEvent(QGraphicsSceneMouseEvent *event)
       
   243 */
       
   244 void NmHsWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
       
   245 {
       
   246     qDebug() << "NmHsWidget::mousePressEvent IN -->>";
       
   247 
       
   248     if (mTitleRow->rect().contains(event->pos()))
       
   249         {
       
   250         mEngine->launchMailAppInboxView();
       
   251         }
       
   252     else
       
   253         {
       
   254         for (int i=0; i < mMailRows.count(); i++)
       
   255             {
       
   256             QRectF tmpRect = mMailRows.at(i)->geometry(); // rect();
       
   257             if (tmpRect.contains(event->pos()))
       
   258                 {
       
   259                 mEngine->launchMailAppMailViewer(mMailRows.at(i)->messageId());
       
   260                 break;
       
   261                 }
       
   262             }
       
   263         }
       
   264     qDebug() << "NmHsWidget::mousePressEvent OUT <<--";
       
   265 }