logsui/logsengine/src/logscontact.cpp
changeset 0 4a5361db8937
child 8 6c9acdc6adc0
equal deleted inserted replaced
-1:000000000000 0:4a5361db8937
       
     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 //USER
       
    19 #include "logscontact.h"
       
    20 #include "logsmodel.h"
       
    21 #include "logsevent.h"
       
    22 #include "logslogger.h"
       
    23 #include "logseventdata.h"
       
    24 #include "logsdbconnector.h"
       
    25 #include "logscommondata.h"
       
    26 
       
    27 //SYSTEM
       
    28 #include <QVariant>
       
    29 #include <xqservicerequest.h>
       
    30 #include <qcontactmanager.h>
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 LogsContact::LogsContact(LogsEvent& event, LogsDbConnector& dbConnector)
       
    37   :  QObject(), 
       
    38      mDbConnector(dbConnector),
       
    39      mService(0),
       
    40      mCurrentRequest(TypeLogsContactSave),
       
    41      mContactId(0),
       
    42      mSaveAsOnlineAccount(false)
       
    43 {
       
    44     mNumber = event.getNumberForCalling();
       
    45     mContactId = event.contactLocalId();
       
    46     mContact = contact();
       
    47     if ( event.eventType() == LogsEvent::TypeVoIPCall &&
       
    48          event.logsEventData() && 
       
    49          !event.logsEventData()->remoteUrl().isEmpty() ) {
       
    50          mSaveAsOnlineAccount = true;
       
    51     }
       
    52 }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 LogsContact::LogsContact(const QString& number, 
       
    59                          LogsDbConnector& dbConnector,
       
    60                          unsigned int contactId)
       
    61   :  QObject(), 
       
    62      mDbConnector(dbConnector),
       
    63      mService(0),
       
    64      mCurrentRequest(TypeLogsContactSave),
       
    65      mNumber(number),
       
    66      mContactId(contactId),
       
    67      mSaveAsOnlineAccount(false)
       
    68 {
       
    69     mContact = contact();
       
    70 }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 LogsContact::~LogsContact()
       
    77 {
       
    78     LOGS_QDEBUG( "logs [ENG] <-> LogsContact::~LogsContact()" )
       
    79     delete mService;
       
    80 }
       
    81     
       
    82 // ----------------------------------------------------------------------------
       
    83 //
       
    84 // ----------------------------------------------------------------------------
       
    85 //
       
    86 LogsContact::RequestType LogsContact::allowedRequestType()
       
    87 {
       
    88     LOGS_QDEBUG( "logs [ENG] -> LogsContact::allowedRequestType()" )
       
    89     LogsContact::RequestType type = TypeLogsContactSave;
       
    90     
       
    91     if ( isContactInPhonebook() ) {
       
    92         type = TypeLogsContactOpen;
       
    93     }
       
    94     
       
    95     LOGS_QDEBUG_2( "logs [ENG] <- LogsContact::allowedRequestType(): ", type )
       
    96     return type;
       
    97 }
       
    98 
       
    99 // ----------------------------------------------------------------------------
       
   100 // 
       
   101 // ----------------------------------------------------------------------------
       
   102 //
       
   103 bool LogsContact::isContactRequestAllowed()
       
   104 {
       
   105     return ( isContactInPhonebook() || !mNumber.isEmpty() );
       
   106 }
       
   107 
       
   108 // ----------------------------------------------------------------------------
       
   109 //
       
   110 // ----------------------------------------------------------------------------
       
   111 //
       
   112 bool LogsContact::open()
       
   113 {
       
   114     LOGS_QDEBUG( "logs [ENG] -> LogsContact::open()")
       
   115     bool ret = false;
       
   116     if ( allowedRequestType() == TypeLogsContactOpen ) {
       
   117         mCurrentRequest = TypeLogsContactOpen;
       
   118 
       
   119         QList<QVariant> arguments;
       
   120         arguments.append( QVariant(mContactId) );
       
   121         ret = requestFetchService( "open(int)", arguments );
       
   122     }
       
   123     
       
   124     LOGS_QDEBUG_2( "logs [ENG] <- LogsContact::open(): ", ret )
       
   125     return ret;
       
   126 }
       
   127 
       
   128 // ----------------------------------------------------------------------------
       
   129 //
       
   130 // ----------------------------------------------------------------------------
       
   131 //
       
   132 bool LogsContact::addNew()
       
   133 {
       
   134     LOGS_QDEBUG( "logs [ENG] -> LogsContact::save()" )
       
   135             
       
   136     bool ret = save("editCreateNew(QString,QString)");
       
   137     
       
   138     LOGS_QDEBUG_2( "logs [ENG] <- LogsContact::save(): ", ret )
       
   139     return ret;
       
   140 }
       
   141 
       
   142 // ----------------------------------------------------------------------------
       
   143 //
       
   144 // ----------------------------------------------------------------------------
       
   145 //
       
   146 bool LogsContact::updateExisting()
       
   147 {
       
   148     LOGS_QDEBUG( "logs [ENG] -> LogsContact::updateExisting()" )
       
   149     
       
   150     bool ret = save("editUpdateExisting(QString,QString)");
       
   151     
       
   152     LOGS_QDEBUG( "logs [ENG] <- LogsContact::updateExisting()" )
       
   153     return ret;
       
   154 }
       
   155 
       
   156 // ----------------------------------------------------------------------------
       
   157 //
       
   158 // ----------------------------------------------------------------------------
       
   159 //
       
   160 bool LogsContact::save(QString message)
       
   161 {
       
   162     QList<QVariant> arguments;
       
   163  
       
   164     if ( !mNumber.isEmpty() ) {
       
   165         if ( mSaveAsOnlineAccount ){
       
   166             QString type = QContactOnlineAccount::DefinitionName;
       
   167             arguments.append( QVariant(type) );
       
   168         } else {
       
   169             QString type = QContactPhoneNumber::DefinitionName;
       
   170             arguments.append( QVariant(type) );
       
   171         }     
       
   172         arguments.append( QVariant(mNumber) );
       
   173     }
       
   174     
       
   175     bool ret(false);
       
   176     
       
   177     if ( arguments.count() == 2 ) {
       
   178         mCurrentRequest = TypeLogsContactSave;
       
   179         ret = requestFetchService( message, arguments );
       
   180     } else {
       
   181         LOGS_QDEBUG( "logs [ENG]  !No Caller ID, not saving the contact..")
       
   182     }
       
   183     
       
   184     return ret;
       
   185 }
       
   186 
       
   187 // ----------------------------------------------------------------------------
       
   188 //
       
   189 // ----------------------------------------------------------------------------
       
   190 //
       
   191 bool LogsContact::requestFetchService( QString message, 
       
   192         const QList<QVariant> &arguments, bool sync )
       
   193 {
       
   194     QString service("com.nokia.services.phonebookservices.Fetch");  
       
   195 
       
   196     delete mService;
       
   197     mService = 0;
       
   198     mService = new XQServiceRequest(service, message, sync);
       
   199     connect(mService, SIGNAL(requestCompleted(QVariant)), this, 
       
   200             SLOT(handleRequestCompleted(QVariant)));
       
   201 
       
   202     mService->setArguments(arguments);
       
   203 
       
   204     QVariant retValue;
       
   205     return mService->send(retValue);
       
   206 }
       
   207 
       
   208 // ----------------------------------------------------------------------------
       
   209 // Phonebookservices define following return values:
       
   210 // - contact wasn't modified (-2)
       
   211 // - was deleted (-1)
       
   212 // - was saved (1)
       
   213 // - saving failed (0)
       
   214 // ----------------------------------------------------------------------------
       
   215 //
       
   216 void LogsContact::handleRequestCompleted(const QVariant& result)
       
   217 {
       
   218     bool retValOk = false;
       
   219     int serviceRetVal = result.toInt(&retValOk);
       
   220     LOGS_QDEBUG_3( "logs [ENG] -> LogsContact::handleRequestCompleted(), (retval, is_ok)", 
       
   221                    serviceRetVal, retValOk )
       
   222     
       
   223     bool modified = ( retValOk && serviceRetVal == 1 );
       
   224     
       
   225     //If existing contact was modified, cached match for the contact should be 
       
   226     //cleaned up, since remote party info might have been changed.
       
   227     //However, if remote party info is taken from symbian DB, it won't be 
       
   228     //updated
       
   229     bool clearCached = ( mCurrentRequest == TypeLogsContactOpen );
       
   230     if ( modified ) {
       
   231         mContact = contact();
       
   232         mDbConnector.updateDetails(clearCached);
       
   233     }
       
   234     
       
   235     if ( mCurrentRequest == TypeLogsContactOpen ) {
       
   236         emit openCompleted(modified);
       
   237     } else if ( mCurrentRequest == TypeLogsContactSave ) {
       
   238         emit saveCompleted(modified);
       
   239     }
       
   240 }
       
   241 
       
   242 // ----------------------------------------------------------------------------
       
   243 //
       
   244 // ----------------------------------------------------------------------------
       
   245 //
       
   246 QContact LogsContact::contact()
       
   247 {
       
   248     if ( mContactId ) {     
       
   249         return LogsCommonData::getInstance().contactManager().contact( mContactId ); 
       
   250     } else {
       
   251         return QContact();
       
   252     }
       
   253 }
       
   254 
       
   255 // ----------------------------------------------------------------------------
       
   256 //
       
   257 // ----------------------------------------------------------------------------
       
   258 //
       
   259 bool LogsContact::isContactInPhonebook()
       
   260 {
       
   261     QContactLocalId localId = mContactId;    
       
   262     return ( localId && localId == mContact.localId() );
       
   263 }
       
   264 
       
   265 // End of file
       
   266