messagingapp/msgnotifications/msgindicatorplugin/src/msgindicator.cpp
changeset 31 ebfee66fde93
child 34 84197e66a4bd
equal deleted inserted replaced
30:6a20128ce557 31:ebfee66fde93
       
     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 #include "msgindicator.h" 
       
    19 #include "msgindicator_p.h"   
       
    20 #include "msginfodefs.h"
       
    21 #include "debugtraces.h"
       
    22 
       
    23 #include <QThreadPool>
       
    24 #include <QRunnable>
       
    25 #include <QByteArray>
       
    26 #include <QProcess>
       
    27 #include <QFileInfo>
       
    28 #include <qvariant.h>
       
    29 #include <xqaiwrequest.h>
       
    30 #include <xqappmgr.h>
       
    31 #include <ccsdefs.h>
       
    32 
       
    33 
       
    34 //Localized Constants
       
    35 #define LOC_UNREAD_MESSAGES hbTrId("txt_messaging_list_ln")
       
    36 #define LOC_NEW_MESSAGES hbTrId("txt_common_opt_ln_new_messages")
       
    37 #define LOC_RECEIVED_FILES hbTrId("Received files")
       
    38 #define LOC_UNREAD_SINGLE_MESSAGE hbTrId("Unread Message")
       
    39 #define LOC_UNREAD_MULTIPLE_MESSAGES hbTrId("Unread Messages")
       
    40 #define LOC_FAILED_SINGLE_MESSAGE hbTrId("Failed Message")
       
    41 #define LOC_FAILED_MULTIPLE_MESSAGES hbTrId("Failed Messages")
       
    42 #define LOC_OUTGOING_SINGLE_MESSAGE hbTrId("Outgoing Message")
       
    43 #define LOC_OUTGOING_MULTIPLE_MESSAGES hbTrId("Outgoing Messages")
       
    44 
       
    45 /**
       
    46  * The number of indicators.
       
    47  */
       
    48 static const int MsgIndicatorCount = 4;
       
    49 
       
    50 /**
       
    51  * The attibutes of an indicator.
       
    52  */
       
    53 struct MsgIndicatorInfo
       
    54 {
       
    55     QString icon;
       
    56     QString primaryText;
       
    57     QString secondaryText;
       
    58 };
       
    59 
       
    60 /**
       
    61  * Array of Indicator information elements
       
    62  */
       
    63 
       
    64 //TODO: Localize
       
    65 
       
    66 
       
    67 // TODO: get regular icons, thes are placeholders.hanlde later
       
    68 static const MsgIndicatorInfo IndicatorInfo[MsgIndicatorCount] = { 
       
    69     { "", "", "" }, 
       
    70     { "qtg_large_new_message", "newindicatorplugin", "%1 unread messages" }, 
       
    71     { "qtg_small_fail", "failedindicatorplugin", "%1 Failed message%2" }, 
       
    72     { "qtg_small_outbox","pendingindicatorplugin", "%1 Outgoing message%2" }
       
    73 };
       
    74 
       
    75 // ----------------------------------------------------------------------------
       
    76 // ServiceRequestSenderTask::ServiceRequestSenderTask
       
    77 // @see msgindicator.h
       
    78 // ----------------------------------------------------------------------------   
       
    79 ServiceRequestSenderTask::ServiceRequestSenderTask(qint64 conversationId):
       
    80 mConvId(conversationId)
       
    81      {     
       
    82      }
       
    83 
       
    84 // ----------------------------------------------------------------------------
       
    85 // ServiceRequestSenderTask::~ServiceRequestSenderTask
       
    86 // @see msgindicator.h
       
    87 // ----------------------------------------------------------------------------   
       
    88 ServiceRequestSenderTask::~ServiceRequestSenderTask()
       
    89      {     
       
    90      }
       
    91 
       
    92 // ----------------------------------------------------------------------------
       
    93 // ServiceRequestSenderTask::run
       
    94 // @see msgindicator.h
       
    95 // ----------------------------------------------------------------------------   
       
    96 void ServiceRequestSenderTask::run()
       
    97      {
       
    98      QList<QVariant> args;
       
    99      QString serviceName("com.nokia.services.hbserviceprovider");
       
   100      QString operation("open(qint64)");
       
   101      XQAiwRequest* request;
       
   102      XQApplicationManager appManager;
       
   103      request = appManager.create(serviceName, "conversationview", operation, false); // embedded
       
   104      if ( request == NULL )
       
   105          {
       
   106          return;       
       
   107          }
       
   108      args << QVariant(mConvId);
       
   109      request->setArguments(args);
       
   110      request->send();
       
   111      delete request;
       
   112      }
       
   113 
       
   114 // ----------------------------------------------------------------------------
       
   115 // MsgIndicator::MsgIndicator
       
   116 // @see msgindicator.h
       
   117 // ----------------------------------------------------------------------------
       
   118 MsgIndicator::MsgIndicator(const QString &indicatorType) :
       
   119     HbIndicatorInterface(indicatorType, HbIndicatorInterface::NotificationCategory,
       
   120         InteractionActivated),
       
   121 		mIndicatorType(NULL)
       
   122 {
       
   123     d_ptr = new MsgIndicatorPrivate(this);
       
   124 }
       
   125 
       
   126 // ----------------------------------------------------------------------------
       
   127 // MsgIndicator::~MsgIndicator
       
   128 // @see msgindicator.h
       
   129 // ----------------------------------------------------------------------------
       
   130 MsgIndicator::~MsgIndicator()
       
   131 {
       
   132     delete d_ptr;
       
   133 }
       
   134 
       
   135 // ----------------------------------------------------------------------------
       
   136 // MsgIndicator::handleInteraction
       
   137 // @see msgindicator.h
       
   138 // ----------------------------------------------------------------------------
       
   139 bool MsgIndicator::handleInteraction(InteractionType type)
       
   140   {
       
   141   bool handled = false;
       
   142   if (type == InteractionActivated)
       
   143     {
       
   144     QThreadPool::globalInstance()->start(new ServiceRequestSenderTask(
       
   145         mConversationId));
       
   146     handled = true;
       
   147     }
       
   148     return handled;
       
   149 }
       
   150 
       
   151 // ----------------------------------------------------------------------------
       
   152 // MsgIndicator::indicatorData
       
   153 // @see msgindicator.h
       
   154 // ----------------------------------------------------------------------------
       
   155 QVariant MsgIndicator::indicatorData(int role) const
       
   156 {
       
   157     switch (role) {
       
   158     case PrimaryTextRole:
       
   159     {
       
   160         return mPrimaryText;
       
   161     }
       
   162     case SecondaryTextRole:
       
   163     {
       
   164         return mSecondaryText;
       
   165     }
       
   166     case DecorationNameRole:
       
   167     {
       
   168         return IndicatorInfo[mIndicatorType].icon;        
       
   169     }
       
   170     case MonoDecorationNameRole:
       
   171     {
       
   172         if (NewIndicatorPlugin == mIndicatorType) {
       
   173             return IndicatorInfo[mIndicatorType].icon;
       
   174         }
       
   175         else {
       
   176             // Don't show status-bar icons for indications other 
       
   177 			// than new-message
       
   178             return QVariant();
       
   179         }
       
   180     }
       
   181     default:
       
   182         return QVariant();
       
   183     }
       
   184 }
       
   185 
       
   186 // ----------------------------------------------------------------------------
       
   187 // MsgIndicator::handleClientRequest
       
   188 // @see msgindicator.h
       
   189 // ----------------------------------------------------------------------------
       
   190 bool MsgIndicator::handleClientRequest(RequestType type, const QVariant &parameter)
       
   191 {
       
   192 
       
   193     bool handled(false);
       
   194     switch (type) {
       
   195     case RequestActivate:
       
   196     {
       
   197         QByteArray dataArray = parameter.toByteArray();
       
   198         QDataStream messageStream(&dataArray, QIODevice::ReadOnly);
       
   199         MsgInfo info;
       
   200         info.deserialize(messageStream);
       
   201 
       
   202         mCount = info.mMsgCount;
       
   203         mIndicatorType = info.mIndicatorType;
       
   204         mPrimaryText = getPrimaryText(info);
       
   205         mSecondaryText = getSecondaryText(info);
       
   206         mConversationId = info.mConversationId;
       
   207         emit
       
   208         dataChanged();
       
   209         handled = true;
       
   210     }
       
   211         break;
       
   212 
       
   213     case RequestDeactivate:
       
   214     {
       
   215         // reset data 
       
   216         mConversationId = -100;
       
   217         mBodyText = QString();
       
   218         emit deactivate();
       
   219     }
       
   220         break;
       
   221     default:
       
   222         break;
       
   223     }
       
   224 
       
   225     return handled;
       
   226 }
       
   227 
       
   228 // ----------------------------------------------------------------------------
       
   229 // MsgIndicator::prepareDisplayName
       
   230 // @see msgindicator.h
       
   231 // ----------------------------------------------------------------------------
       
   232 QString MsgIndicator::prepareDisplayName(MsgInfo& indicatorData) const
       
   233 {
       
   234     //Set the Contact Name/Number
       
   235 
       
   236     QString displayName;
       
   237 
       
   238     if (ECsBlueTooth == indicatorData.mMessageType) {
       
   239         displayName = LOC_RECEIVED_FILES;
       
   240     }
       
   241     else {
       
   242         indicatorData.mDisplayName.removeDuplicates();
       
   243         indicatorData.mDisplayName.sort();
       
   244         displayName = indicatorData.mDisplayName.at(0);
       
   245 
       
   246         for (int i = 1; i < indicatorData.mDisplayName.count(); ++i) {
       
   247             displayName += QString(", ") + indicatorData.mDisplayName.at(i);
       
   248         }
       
   249     }
       
   250     return displayName;
       
   251 }
       
   252 
       
   253 // ----------------------------------------------------------------------------
       
   254 // MsgIndicator::getPrimaryText
       
   255 // @see msgindicator.h
       
   256 // ----------------------------------------------------------------------------
       
   257 QString MsgIndicator::getPrimaryText(MsgInfo& data)
       
   258 {
       
   259     if (data.mIndicatorType == NewIndicatorPlugin) {
       
   260         return getUnreadPrimaryText(data);
       
   261     }
       
   262     else {
       
   263         QString primaryText;
       
   264         if (mCount >= 1) {
       
   265             if (data.mIndicatorType == FailedIndicatorPlugin) {
       
   266                 primaryText += QString("%1 ").arg(mCount);
       
   267                 primaryText += (mCount > 1)? 
       
   268                     LOC_FAILED_MULTIPLE_MESSAGES : LOC_FAILED_SINGLE_MESSAGE;
       
   269             }
       
   270             else {
       
   271                 primaryText += QString("%1 ").arg(mCount);
       
   272                 primaryText += (mCount > 1)? 
       
   273                     LOC_OUTGOING_MULTIPLE_MESSAGES : LOC_OUTGOING_SINGLE_MESSAGE;
       
   274             }
       
   275         }
       
   276         return primaryText;
       
   277     }
       
   278 }
       
   279 
       
   280 // ----------------------------------------------------------------------------
       
   281 // MsgIndicator::getUnreadPrimaryText
       
   282 // @see msgindicator.h
       
   283 // ----------------------------------------------------------------------------
       
   284 QString MsgIndicator::getUnreadPrimaryText(MsgInfo& data)
       
   285 {
       
   286     mConversationId = -100;
       
   287     
       
   288     d_ptr->getIndicatorInfo(data);
       
   289 
       
   290     mConversationFromSingleContact = data.mFromSingle;
       
   291     mBodyText = data.mDescription;
       
   292 
       
   293     if (mConversationFromSingleContact) {
       
   294         mConversationId = (int) data.mConversationId;
       
   295         QString name = data.mDisplayName.at(0);
       
   296         name.append(QString(" (%1)").arg(data.mMsgCount));
       
   297         return name;
       
   298     }
       
   299     else {
       
   300         QString text = QString("%1 ").arg(mCount) + LOC_UNREAD_MULTIPLE_MESSAGES;
       
   301         return text;
       
   302     }
       
   303 
       
   304 }
       
   305 
       
   306 // ----------------------------------------------------------------------------
       
   307 // MsgIndicator::getSecondaryText
       
   308 // @see msgindicator.h
       
   309 // ----------------------------------------------------------------------------
       
   310 QString MsgIndicator::getSecondaryText(MsgInfo& info)
       
   311 {
       
   312     QString secondaryText("");
       
   313 
       
   314     if (info.mIndicatorType == NewIndicatorPlugin) {
       
   315         secondaryText = info.mDescription;
       
   316 
       
   317         if (info.mFromSingle) {
       
   318             if (ECsBlueTooth == info.mMessageType) {
       
   319                 QFileInfo fname(secondaryText);
       
   320                 secondaryText = fname.fileName();
       
   321             }
       
   322         }
       
   323         else {
       
   324             info.mConversationId = -100;
       
   325         }
       
   326     }
       
   327     else {
       
   328         info.mDisplayName.removeDuplicates();
       
   329         info.mDisplayName.sort();
       
   330         int count = info.mDisplayName.count();
       
   331         if (count > 1) {
       
   332             info.mConversationId = -100;
       
   333         }
       
   334 
       
   335         secondaryText.append(info.mDisplayName.at(0));
       
   336         for (int i = 1; i < info.mDisplayName.count(); ++i) {
       
   337             secondaryText.append(", ");
       
   338             secondaryText.append(info.mDisplayName.at(i));
       
   339         }
       
   340     }
       
   341     return secondaryText;
       
   342 }