messagingapp/msgnotifications/msgnotifier/src/msgnotifier_p.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: Listen to conversation server events and parse the 
       
    15  * events. These parse infomation passed to notifications and indications.
       
    16  *
       
    17  */
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include <msvapi.h>
       
    21 #include <ccsclientconversation.h>
       
    22 #include <ccsrequesthandler.h>
       
    23 #include <ccsconversationentry.h>
       
    24 #include <xqservicerequest.h>
       
    25 #include <QString>
       
    26 #include <w32std.h>
       
    27 #include <APGTASK.H> 
       
    28 #include <XQSettingsManager>
       
    29 #include <xqpublishandsubscribeutils.h>
       
    30 
       
    31 //USER INCLUDES
       
    32 #include "msgnotifier.h"
       
    33 #include "msgnotifier_p.h"
       
    34 #include "s60qconversions.h"
       
    35 #include "msgstorehandler.h"
       
    36 #include "msginfodefs.h"
       
    37 #include "conversationidpsconsts.h"
       
    38 #include "debugtraces.h"
       
    39 
       
    40 
       
    41 // ----------------------------------------------------------------------------
       
    42 // MsgNotifierPrivate::MsgNotifierPrivate
       
    43 // @see MsgNotifierPrivate.h
       
    44 // ----------------------------------------------------------------------------
       
    45 MsgNotifierPrivate::MsgNotifierPrivate(MsgNotifier* MsgNotifier) :
       
    46     q_ptr(MsgNotifier), mCvServer(NULL), iMsgStoreHandler(NULL),
       
    47     mSettingsManager(NULL),mPSUtils(NULL)
       
    48 {
       
    49     QDEBUG_WRITE("MsgNotifierPrivate::MsgNotifierPrivate : Enter")
       
    50 
       
    51     TRAP_IGNORE(initL());
       
    52     QDEBUG_WRITE("MsgNotifierPrivate::MsgNotifierPrivate : Exit")
       
    53 }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // MsgNotifierPrivate::~MsgNotifierPrivate
       
    57 // @see MsgNotifierPrivate.h
       
    58 // ----------------------------------------------------------------------------
       
    59 MsgNotifierPrivate::~MsgNotifierPrivate()
       
    60 {
       
    61     QDEBUG_WRITE("MsgNotifierPrivate::~MsgNotifierPrivate : Enter")
       
    62     if (mCvServer) {
       
    63         mCvServer->RemoveConversationListChangeEventL(this);
       
    64         delete mCvServer;
       
    65         mCvServer = NULL;
       
    66     }
       
    67 
       
    68     if (iMsgStoreHandler) {
       
    69         delete iMsgStoreHandler;
       
    70         iMsgStoreHandler = NULL;
       
    71     }
       
    72     
       
    73     if(mPSUtils)
       
    74         {
       
    75         delete mPSUtils;
       
    76         }
       
    77     
       
    78     if(mSettingsManager)
       
    79         {
       
    80         delete mSettingsManager;
       
    81         }
       
    82     
       
    83     QDEBUG_WRITE("MsgNotifierPrivate::~MsgNotifierPrivate : Exit")
       
    84 }
       
    85 
       
    86 // ----------------------------------------------------------------------------
       
    87 // MsgNotifierPrivate::initL
       
    88 // @see MsgNotifierPrivate.h
       
    89 // ----------------------------------------------------------------------------
       
    90 void MsgNotifierPrivate::initL()
       
    91 {
       
    92     QDEBUG_WRITE("MsgNotifierPrivate::initL : Enter")
       
    93 
       
    94     mCvServer = CCSRequestHandler::NewL();
       
    95     mCvServer->RequestConversationListChangeEventL(this);
       
    96     iMsgStoreHandler = new MsgStoreHandler(this, mCvServer);
       
    97     updateUnreadIndications(true); 
       
    98     updateOutboxIndications();
       
    99 
       
   100     mSettingsManager = new XQSettingsManager();
       
   101     
       
   102     // define property
       
   103     mPSUtils = new XQPublishAndSubscribeUtils(*mSettingsManager);
       
   104     XQPublishAndSubscribeSettingsKey convIdKey(
       
   105             KMsgCVIdProperty, KMsgCVIdKey);
       
   106     bool success = mPSUtils->defineProperty(convIdKey, 
       
   107                             XQSettingsManager::TypeInt);
       
   108     
       
   109     QDEBUG_WRITE_FORMAT("MsgNotifierPrivate::initL "
       
   110                         "property creation ret value",success)
       
   111     
       
   112     // write -1 initially 
       
   113     success = mSettingsManager->writeItemValue(convIdKey,-1);
       
   114     
       
   115     QDEBUG_WRITE_FORMAT("MsgNotifierPrivate::initL "
       
   116                            "writing ret value",success)
       
   117     
       
   118     QDEBUG_WRITE("MsgNotifierPrivate::initL : Exit")
       
   119 }
       
   120 
       
   121 // ----------------------------------------------------------------------------
       
   122 // MsgNotifierPrivate::AddConversationList
       
   123 // @see MsgNotifierPrivate.h
       
   124 // ----------------------------------------------------------------------------
       
   125 void MsgNotifierPrivate::AddConversationList(
       
   126                   const CCsClientConversation& aClientConversation)
       
   127 {
       
   128     QDEBUG_WRITE("MsgNotifierPrivate::AddConversationList : Enter")
       
   129 
       
   130     processListEntry(aClientConversation);
       
   131     updateUnreadIndications();
       
   132 
       
   133     QDEBUG_WRITE("MsgNotifierPrivate::AddConversationList : Exit")
       
   134 }
       
   135 
       
   136 // ----------------------------------------------------------------------------
       
   137 // MsgNotifierPrivate::DeleteConversationList
       
   138 // @see MsgNotifierPrivate.h
       
   139 // ----------------------------------------------------------------------------
       
   140 void MsgNotifierPrivate::DeleteConversationList( 
       
   141 	                const CCsClientConversation& /*aClientConversation*/)
       
   142 {
       
   143     QDEBUG_WRITE("MsgNotifierPrivate::DeleteConversationList : Enter")
       
   144     updateUnreadIndications();
       
   145     QDEBUG_WRITE("MsgNotifierPrivate::DeleteConversationList : Exit")
       
   146 }
       
   147 
       
   148 // ----------------------------------------------------------------------------
       
   149 // MsgNotifierPrivate::ModifyConversationList
       
   150 // @see MsgNotifierPrivate.h
       
   151 // ----------------------------------------------------------------------------
       
   152 void MsgNotifierPrivate::ModifyConversationList(const CCsClientConversation& aClientConversation)
       
   153 {
       
   154     QDEBUG_WRITE("MsgNotifierPrivate::ModifyConversationList : Enter")
       
   155 
       
   156     processListEntry(aClientConversation);
       
   157     updateUnreadIndications();
       
   158 
       
   159     QDEBUG_WRITE("MsgNotifierPrivate::ModifyConversationList : Exit")
       
   160 }
       
   161 
       
   162 // ----------------------------------------------------------------------------
       
   163 // MsgNotifierPrivate::RefreshConversationList
       
   164 // @see MsgNotifierPrivate.h
       
   165 // ----------------------------------------------------------------------------
       
   166 void MsgNotifierPrivate::RefreshConversationList()
       
   167 {
       
   168 
       
   169 }
       
   170 
       
   171 // ----------------------------------------------------------------------------
       
   172 // MsgNotifierPrivate::processListEntry
       
   173 // @see MsgNotifierPrivate.h
       
   174 // ----------------------------------------------------------------------------
       
   175 void MsgNotifierPrivate::processListEntry(
       
   176         const CCsClientConversation& aClientConversation)
       
   177     {
       
   178     QDEBUG_WRITE("MsgNotifierPrivate::processListEntry : Enter")
       
   179     
       
   180     CCsConversationEntry* convEntry = 
       
   181                                  aClientConversation.GetConversationEntry();
       
   182     
       
   183     QDEBUG_WRITE_FORMAT("MsgNotifierPrivate::processListEntry Direction",
       
   184                                                 convEntry->ConversationDir())
       
   185     QDEBUG_WRITE_FORMAT("MsgNotifierPrivate::processListEntry New Entry Attribute",
       
   186                                                 convEntry->IsAttributeSet(ECsAttributeNewEntryAdded))
       
   187     QDEBUG_WRITE_FORMAT("MsgNotifierPrivate::processListEntry Unread Attribute",
       
   188                                                 convEntry->IsAttributeSet(ECsAttributeUnread))
       
   189    
       
   190     if((convEntry->ConversationDir() == ECsDirectionIncoming)
       
   191             && convEntry->IsAttributeSet(ECsAttributeNewEntryAdded) 
       
   192             && convEntry->IsAttributeSet(ECsAttributeUnread))
       
   193         {
       
   194         QDEBUG_WRITE("processListEntry : Processing data for Notification")
       
   195         
       
   196         NotificationData notifData;
       
   197         notifData.msvEntryId = convEntry->EntryId(); 
       
   198         notifData.mConversationId = aClientConversation.GetConversationEntryId();
       
   199         notifData.mMsgType = static_cast<int>(convEntry->GetType());
       
   200         
       
   201         HBufC* displayName = aClientConversation.GetDisplayName();
       
   202         HBufC* number =  convEntry->Contact();
       
   203         HBufC* descrp =  convEntry->Description();
       
   204         
       
   205         if(displayName)
       
   206             {
       
   207             notifData.mDisplayName = 
       
   208                                 S60QConversions::s60DescToQString(*displayName);
       
   209             }        
       
   210         if(number)
       
   211             {
       
   212             notifData.mContactNum =  S60QConversions::s60DescToQString(*number);
       
   213             }
       
   214         if(descrp)
       
   215             {
       
   216             notifData.mDescription = S60QConversions::s60DescToQString(*descrp);
       
   217             }
       
   218         
       
   219         // check whether opened cv id and received 
       
   220         // cv id are same and show notification
       
   221         if( showNotification(notifData.mConversationId ))
       
   222             {
       
   223              q_ptr->displayNewMessageNotification(notifData);
       
   224              QDEBUG_WRITE("processListEntry : Notification display called")
       
   225             }
       
   226        
       
   227         }
       
   228     
       
   229     QDEBUG_WRITE("MsgNotifierPrivate::processListEntry : Exit")
       
   230     }
       
   231 
       
   232 // ----------------------------------------------------------------------------
       
   233 // MsgNotifierPrivate::updateUnreadIndications
       
   234 // @see MsgNotifierPrivate.h
       
   235 // ----------------------------------------------------------------------------
       
   236 void MsgNotifierPrivate::updateUnreadIndications(bool bootup)
       
   237 {
       
   238     QDEBUG_WRITE("MsgNotifierPrivate::updateIndicationsL : Enter")
       
   239 
       
   240     int unreadCount(0);
       
   241    if(bootup)
       
   242    {
       
   243        TRAP_IGNORE(unreadCount = iMsgStoreHandler->GetUnreadMessageCountL());
       
   244    }
       
   245    else
       
   246    {
       
   247        TRAP_IGNORE(unreadCount = mCvServer->GetTotalUnreadCountL());
       
   248    }
       
   249 
       
   250     QDEBUG_WRITE_FORMAT("MsgNotifierPrivate::"
       
   251         "updateIndications unreadCount = ",unreadCount );
       
   252 
       
   253     //activate or deactivate indications based on unread count
       
   254     q_ptr->updateUnreadIndications(unreadCount);
       
   255 
       
   256     QDEBUG_WRITE("MsgNotifierPrivate::updateIndicationsL : Exit")
       
   257 }
       
   258 
       
   259 // ----------------------------------------------------------------------------
       
   260 // MsgNotifierPrivate::updateOutboxIndications
       
   261 // @see MsgNotifierPrivate.h
       
   262 // ----------------------------------------------------------------------------
       
   263 void MsgNotifierPrivate::updateOutboxIndications()
       
   264 {
       
   265 
       
   266     MsgInfo failedIndicatorData;
       
   267     MsgInfo pendingIndicatorData;
       
   268     TInt err = KErrNone;
       
   269     
       
   270     TRAP(err, iMsgStoreHandler->GetOutboxEntriesL(failedIndicatorData,pendingIndicatorData));
       
   271 
       
   272     q_ptr->updateOutboxIndications(failedIndicatorData);
       
   273     q_ptr->updateOutboxIndications(pendingIndicatorData);
       
   274 }
       
   275 
       
   276 
       
   277 // ----------------------------------------------------------------------------
       
   278 // MsgNotifierPrivate::displayOutboxIndications
       
   279 // @see MsgNotifierPrivate.h
       
   280 // ----------------------------------------------------------------------------
       
   281 
       
   282 void MsgNotifierPrivate::displayOutboxIndications(MsgInfo data)
       
   283 	{
       
   284 		q_ptr->updateOutboxIndications(data);
       
   285 	}
       
   286 
       
   287 // ----------------------------------------------------------------------------
       
   288 // MsgNotifierPrivate::displayFailedNote
       
   289 // @see MsgNotifierPrivate.h
       
   290 // ----------------------------------------------------------------------------
       
   291 void MsgNotifierPrivate::displayFailedNote(MsgInfo info)
       
   292 {
       
   293     // TODO: use XQAiwRequest
       
   294     QDEBUG_WRITE("[MsgNotifierPrivate::handleFailedState] : entered")
       
   295     // change to com.nokia.symbian.messaging (servicename), IMsgErrorNotifier
       
   296     // as the service name.
       
   297     XQServiceRequest snd("messaging.com.nokia.symbian.MsgErrorNotifier",
       
   298         "displayErrorNote(QVariantList)", false);
       
   299 
       
   300     QVariantList args;
       
   301     info.mDisplayName.removeDuplicates();
       
   302     info.mDisplayName.sort();
       
   303     
       
   304     QString nameString;
       
   305     
       
   306     nameString.append(info.mDisplayName.at(0));
       
   307     for(int i = 1; i < info.mDisplayName.count(); ++i){
       
   308         nameString.append(", ");
       
   309         nameString.append(info.mDisplayName.at(i));
       
   310     }
       
   311       
       
   312     //Even if name string is empty we shall add name into args
       
   313     QVariant nameV(nameString);
       
   314     args << nameV;
       
   315 
       
   316     QDEBUG_WRITE("[MsgNotifierPrivate::handleFailedState] : name and contactnumber")
       
   317 
       
   318     args << info.mConversationId;
       
   319     args << info.mMessageType;
       
   320     snd << args;
       
   321     snd.send();
       
   322     QDEBUG_WRITE("[MsgNotifierPrivate::handleFailedState] : left")
       
   323 
       
   324 }
       
   325 
       
   326 // ----------------------------------------------------------------------------
       
   327 // MsgNotifierPrivate::showNotification
       
   328 // @see MsgNotifierPrivate.h
       
   329 // ----------------------------------------------------------------------------
       
   330 bool MsgNotifierPrivate::showNotification(int receivedMsgConvId)
       
   331 {
       
   332     bool showNotification = true;
       
   333     
       
   334     RWsSession wsSession ;
       
   335     wsSession.Connect();
       
   336 
       
   337     TApaTaskList taskList( wsSession );
       
   338     TApaTask task = taskList.FindApp(KMsgAppUid); // find msgapp is running
       
   339 
       
   340     if(task.Exists())
       
   341         {
       
   342         TApaTask foregndtask =   taskList.FindByPos(0) ;  // foreground app
       
   343         // compare  window group id  
       
   344         // if application is in foregorund, then check the currently
       
   345         // opened conversation is same as received one.
       
   346         if(task.WgId() == foregndtask.WgId() )
       
   347             {
       
   348             // get the current conversation ID
       
   349             XQPublishAndSubscribeSettingsKey convIdKey( KMsgCVIdProperty, 
       
   350                     KMsgCVIdKey);
       
   351             QVariant value = mSettingsManager->readItemValue(convIdKey, 
       
   352                     XQSettingsManager::TypeInt);
       
   353 
       
   354             int openedConvId  = value.toInt();
       
   355             if( openedConvId == receivedMsgConvId)
       
   356                 {
       
   357                 showNotification = false;
       
   358                 QDEBUG_WRITE("processListEntry : Notification not shown")
       
   359                 }
       
   360             }
       
   361         }
       
   362 
       
   363     wsSession.Close();
       
   364     return showNotification;
       
   365 }
       
   366 //EOF