phonebookui/phonebookservices/src/cntservices.cpp
changeset 50 77bc263e1626
child 53 e6aff7b69165
equal deleted inserted replaced
49:74b30151afd6 50:77bc263e1626
       
     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 "cntservices.h"
       
    19 #include "cntglobal.h"
       
    20 #include "cntdebug.h"
       
    21 #include <cntabstractviewmanager.h>
       
    22 #include "cntimageutility.h"
       
    23 #include "cntserviceviewparams.h"
       
    24 
       
    25 #include <hbview.h>
       
    26 #include <hblistview.h>
       
    27 #include <qversitreader.h>
       
    28 #include <qversitcontactimporter.h>
       
    29 #include <qversitdocument.h>
       
    30 #include <xqserviceprovider.h>
       
    31 
       
    32 #include <QApplication>
       
    33 #include <QPixmap>
       
    34 #include <QFile>
       
    35 #include <QUrl>
       
    36 
       
    37 CntServices::CntServices() :
       
    38 mViewManager(NULL),
       
    39 mCurrentProvider(NULL),
       
    40 mIsQuitable(true)
       
    41 {
       
    42     CNT_ENTRY
       
    43 
       
    44     // Definition names supported in symbian back-end
       
    45     m_definitionNames << QContactName::DefinitionName;
       
    46     m_definitionNames << QContactNickname::DefinitionName;
       
    47     m_definitionNames << QContactPhoneNumber::DefinitionName;
       
    48     m_definitionNames << QContactEmailAddress::DefinitionName;
       
    49     m_definitionNames << QContactAddress::DefinitionName;
       
    50     m_definitionNames << QContactUrl::DefinitionName;
       
    51     m_definitionNames << QContactBirthday::DefinitionName;
       
    52     m_definitionNames << QContactOrganization::DefinitionName;
       
    53     m_definitionNames << QContactSyncTarget::DefinitionName;
       
    54     m_definitionNames << QContactNote::DefinitionName;
       
    55     m_definitionNames << QContactFamily::DefinitionName;
       
    56     m_definitionNames << QContactAvatar::DefinitionName;
       
    57     m_definitionNames << QContactAnniversary::DefinitionName;
       
    58     m_definitionNames << QContactGeoLocation::DefinitionName;
       
    59     m_definitionNames << QContactGender::DefinitionName;
       
    60     m_definitionNames << QContactOnlineAccount::DefinitionName;
       
    61     
       
    62     CNT_EXIT
       
    63 }
       
    64 
       
    65 
       
    66 CntServices::~CntServices()
       
    67 {
       
    68     CNT_LOG
       
    69 }
       
    70 
       
    71 
       
    72 void CntServices::setViewManager( CntAbstractViewManager& aViewManager )
       
    73 {
       
    74     CNT_LOG
       
    75     mViewManager = &aViewManager;
       
    76 }
       
    77 
       
    78 
       
    79 void CntServices::singleFetch(
       
    80     const QString &title, const QString &action,
       
    81     CntAbstractServiceProvider& aServiceProvider )
       
    82 {
       
    83     CNT_ENTRY
       
    84     mCurrentProvider = &aServiceProvider;
       
    85 
       
    86     CntViewParameters params;
       
    87     params.insert(EViewId, serviceContactFetchView);
       
    88     params.insert(ESelectedAction, action);
       
    89     params.insert(KCntServiceViewParamTitle, title);
       
    90     // SINGLE fetch
       
    91     params.insert(ESelectionMode, HbAbstractItemView::SingleSelection);
       
    92 
       
    93     mViewManager->changeView(params);
       
    94     CNT_EXIT
       
    95 }
       
    96 
       
    97 
       
    98 void CntServices::multiFetch(
       
    99     const QString &title, const QString &action,
       
   100     CntAbstractServiceProvider& aServiceProvider )
       
   101 {
       
   102     CNT_ENTRY
       
   103     mCurrentProvider = &aServiceProvider;
       
   104 
       
   105     CntViewParameters params;
       
   106     params.insert(EViewId, serviceContactFetchView);
       
   107     params.insert(ESelectedAction, action);
       
   108     params.insert(KCntServiceViewParamTitle, title);
       
   109     // MULTI fetch
       
   110     params.insert(ESelectionMode, HbAbstractItemView::MultiSelection);
       
   111     mViewManager->changeView(params);
       
   112     CNT_EXIT
       
   113 }
       
   114 
       
   115 
       
   116 void CntServices::editCreateNew(const QString &definitionName, const QString &value,
       
   117     CntAbstractServiceProvider& aServiceProvider)
       
   118 {
       
   119     CNT_ENTRY
       
   120     mCurrentProvider = &aServiceProvider;
       
   121 
       
   122     QContact contact;
       
   123     
       
   124     if (definitionName == QContactPhoneNumber::DefinitionName)
       
   125         {
       
   126         QContactPhoneNumber phoneNumber;
       
   127         phoneNumber.setNumber(value);
       
   128         phoneNumber.setSubTypes(QContactPhoneNumber::SubTypeMobile);
       
   129         contact.saveDetail(&phoneNumber);
       
   130         }
       
   131     else if (definitionName == QContactEmailAddress::DefinitionName)
       
   132         {
       
   133         QContactEmailAddress email;
       
   134         email.setEmailAddress(value);
       
   135         contact.saveDetail(&email);
       
   136         }
       
   137     else if (definitionName == QContactOnlineAccount::DefinitionName)
       
   138         {
       
   139         QContactOnlineAccount account;
       
   140         account.setAccountUri(value);
       
   141         account.setSubTypes(QContactOnlineAccount::SubTypeSipVoip);
       
   142         contact.saveDetail(&account);
       
   143         }    
       
   144 
       
   145     // Launch editor now
       
   146     CntViewParameters params;
       
   147     params.insert(EViewId, serviceEditView);
       
   148     QVariant var;
       
   149     var.setValue(contact);
       
   150     params.insert(ESelectedContact, var);
       
   151     mViewManager->changeView(params);
       
   152     CNT_EXIT
       
   153 }
       
   154 
       
   155 
       
   156 void CntServices::editCreateNewFromVCard(const QString &vCardFile,
       
   157     CntAbstractServiceProvider& aServiceProvider )
       
   158 {
       
   159     CNT_ENTRY
       
   160     mCurrentProvider = &aServiceProvider;
       
   161 
       
   162     CntImageUtility imageUtility;
       
   163     QContact contact;
       
   164     QVersitReader reader;
       
   165     QFile file(vCardFile);
       
   166     if (!file.open(QIODevice::ReadOnly))
       
   167         return;
       
   168     reader.setDevice(&file);
       
   169     
       
   170     reader.startReading();
       
   171     reader.waitForFinished();
       
   172     // Use the resulting document(s)...
       
   173     QVersitContactImporter importer;
       
   174     QList<QVersitDocument> versitDocuments = reader.results();
       
   175     if(versitDocuments.count() > 0 
       
   176        && importer.importDocuments(versitDocuments))
       
   177         {
       
   178         QList<QContact> contacts = importer.contacts();
       
   179         if(contacts.count() > 0)
       
   180             contact = contacts.first();
       
   181         }
       
   182     file.close();
       
   183     
       
   184     // Save thumbnail images
       
   185     QList<QContactThumbnail> details = contact.details<QContactThumbnail>();
       
   186     for (int i = 0;i < details.count();i++)
       
   187         {
       
   188         if (!details.at(i).thumbnail().isNull())
       
   189             {
       
   190             QString imagepath;
       
   191             QPixmap pixmap = QPixmap::fromImage(details.at(i).thumbnail());
       
   192             if(imageUtility.createImage(pixmap,imagepath))
       
   193                 {
       
   194                 // Update contact detail
       
   195                 QContactAvatar detail=details.at(i);
       
   196                 detail.setImageUrl(imagepath);
       
   197                 contact.saveDetail(&detail);
       
   198                 }
       
   199             }
       
   200         }
       
   201     
       
   202     removeNotSupportedDetails(contact);
       
   203     removeNotSupportedFields(contact);
       
   204 
       
   205     // Launch editor now
       
   206     CntViewParameters params;
       
   207     params.insert(EViewId, serviceEditView);
       
   208     QVariant var;
       
   209     var.setValue(contact);
       
   210     params.insert(ESelectedContact, var);
       
   211     mViewManager->changeView(params);
       
   212     CNT_EXIT
       
   213 }
       
   214 
       
   215 
       
   216 void CntServices::editUpdateExisting(const QString &definitionName, const QString &value,
       
   217     CntAbstractServiceProvider& aServiceProvider )
       
   218 {
       
   219     CNT_ENTRY
       
   220     mCurrentProvider = &aServiceProvider;
       
   221 
       
   222     QContactDetail detail;
       
   223     
       
   224     if (definitionName == QContactPhoneNumber::DefinitionName)
       
   225     {
       
   226         QContactPhoneNumber phoneNumber;
       
   227         phoneNumber.setNumber(value);
       
   228         phoneNumber.setSubTypes(QContactPhoneNumber::SubTypeMobile);
       
   229         detail = phoneNumber;
       
   230     }
       
   231     else if (definitionName == QContactEmailAddress::DefinitionName)
       
   232     {
       
   233         QContactEmailAddress email;
       
   234         email.setEmailAddress(value);
       
   235         detail = email;
       
   236     }
       
   237     else if (definitionName == QContactOnlineAccount::DefinitionName)
       
   238     {
       
   239         QContactOnlineAccount account;
       
   240         account.setAccountUri(value);
       
   241         account.setSubTypes(QContactOnlineAccount::SubTypeSipVoip);
       
   242         detail = account;
       
   243     }
       
   244 
       
   245     CntViewParameters params;
       
   246     params.insert(EViewId, serviceContactSelectionView);
       
   247     QVariant var;
       
   248     var.setValue(detail);
       
   249     params.insert(ESelectedDetail, var);
       
   250     mViewManager->changeView(params);
       
   251     CNT_EXIT
       
   252 }
       
   253 
       
   254 void CntServices::editExisting(int contactId,
       
   255 CntAbstractServiceProvider& aServiceProvider)
       
   256 {
       
   257     CNT_ENTRY
       
   258     mCurrentProvider = &aServiceProvider;
       
   259 
       
   260     // Give parameters for launching editor.
       
   261     CntViewParameters params;
       
   262     params.insert(EViewId, serviceEditView);
       
   263 
       
   264     QContact contact = mViewManager->contactManager(SYMBIAN_BACKEND)->contact(contactId);
       
   265     QVariant varContact;
       
   266     varContact.setValue(contact);
       
   267     params.insert(ESelectedContact, varContact);
       
   268     mViewManager->changeView(params);
       
   269     CNT_EXIT
       
   270 }
       
   271 
       
   272 
       
   273 void CntServices::launchContactCard(int aContactId,
       
   274     CntAbstractServiceProvider& aServiceProvider )
       
   275 {
       
   276     CNT_ENTRY
       
   277     mCurrentProvider = &aServiceProvider;
       
   278 
       
   279     QContactManager manager("symbian");
       
   280     QContact contact = manager.contact(aContactId);
       
   281 
       
   282     // Launch Contact Card view
       
   283     CntViewParameters params;
       
   284     params.insert(EViewId, serviceContactCardView);
       
   285     QVariant var;
       
   286     var.setValue(contact);
       
   287     params.insert(ESelectedContact, var);
       
   288     mViewManager->changeView( params );
       
   289     CNT_EXIT
       
   290 }
       
   291 
       
   292 
       
   293 void CntServices::launchTemporaryContactCard(const QString &definitionName, const QString &value,
       
   294     CntAbstractServiceProvider& aServiceProvider )
       
   295 {
       
   296     CNT_ENTRY
       
   297     mCurrentProvider = &aServiceProvider;
       
   298 
       
   299     QContact contact;
       
   300     QContactDetail detail;
       
   301     
       
   302     if (definitionName == QContactPhoneNumber::DefinitionName)
       
   303     {
       
   304         QContactPhoneNumber phoneNumber;
       
   305         phoneNumber.setNumber(value);
       
   306         phoneNumber.setSubTypes(QContactPhoneNumber::SubTypeMobile);
       
   307         contact.saveDetail(&phoneNumber);
       
   308         detail = phoneNumber;
       
   309     }
       
   310     else if (definitionName == QContactEmailAddress::DefinitionName)
       
   311     {
       
   312         QContactEmailAddress email;
       
   313         email.setEmailAddress(value);
       
   314         contact.saveDetail(&email);
       
   315         detail = email;
       
   316     }
       
   317     else if (definitionName == QContactOnlineAccount::DefinitionName)
       
   318     {
       
   319         QContactOnlineAccount account;
       
   320         account.setAccountUri(value);
       
   321         account.setSubTypes(QContactOnlineAccount::SubTypeSipVoip);
       
   322         contact.saveDetail(&account);
       
   323         detail = account;
       
   324     }
       
   325 
       
   326     // Launch the view
       
   327     CntViewParameters params;
       
   328     params.insert(EViewId, serviceAssignContactCardView);
       
   329     QVariant var;
       
   330     var.setValue(contact);
       
   331     params.insert(ESelectedContact, var);
       
   332     QVariant varDetail;
       
   333     varDetail.setValue(detail);
       
   334     params.insert(ESelectedDetail, varDetail);
       
   335     mViewManager->changeView(params);
       
   336     CNT_EXIT
       
   337 }
       
   338 
       
   339 
       
   340 void CntServices::removeNotSupportedDetails(QContact& contact)
       
   341 {
       
   342     CNT_ENTRY
       
   343     // Check definition map, remove details not supported
       
   344     for (int i=0; i < contact.details().count(); i++)
       
   345     {
       
   346         if (!m_definitionNames.contains(contact.details().at(i).definitionName()))
       
   347         {
       
   348             QContactDetail detail= contact.details().at(i);
       
   349             contact.removeDetail(&detail);
       
   350         }
       
   351     }
       
   352     CNT_EXIT
       
   353 }
       
   354 
       
   355 
       
   356 
       
   357 void CntServices::removeNotSupportedFields(QContact& contact)
       
   358 {
       
   359     CNT_ENTRY
       
   360     QContactManager cm("symbian");
       
   361     QList<QContactDetail>   removeList;
       
   362     
       
   363     // Not all fields are supported in symbian back-end
       
   364     // Remove not supported fields
       
   365     for (int i=0; i < contact.details().count(); i++)
       
   366     {
       
   367         QContactDetail detail = contact.details().at(i);
       
   368         if(detail.definitionName() == QContactAddress::DefinitionName)
       
   369         {
       
   370             QContactAddress addrDetail = static_cast<QContactAddress>(detail);
       
   371             
       
   372             // Sub-types not supported
       
   373             if(detail.removeValue(QContactAddress::FieldSubTypes))
       
   374                 contact.saveDetail(&detail);
       
   375         }
       
   376         else if(detail.definitionName() == QContactAnniversary::DefinitionName)
       
   377         {
       
   378             QContactAnniversary annivDetail = static_cast<QContactAnniversary>(detail);
       
   379             
       
   380             // Sub-types not supported
       
   381             if(detail.removeValue(QContactAnniversary::FieldSubType))
       
   382                 contact.saveDetail(&detail);
       
   383             
       
   384             // Context not supported
       
   385             if(detail.removeValue(QContactDetail::FieldContext))
       
   386                 contact.saveDetail(&detail);
       
   387         }
       
   388         else if(detail.definitionName() == QContactPhoneNumber::DefinitionName)
       
   389         {
       
   390             QContactPhoneNumber phoneDetail = static_cast<QContactPhoneNumber>(detail);
       
   391             
       
   392             if(phoneDetail.subTypes().size() > 0 )
       
   393             {
       
   394                 // Sub-types not supported
       
   395                 if(phoneDetail.subTypes().contains(QContactPhoneNumber::SubTypeVoice)
       
   396                    || phoneDetail.subTypes().contains(QContactPhoneNumber::SubTypeMessagingCapable))
       
   397                 {
       
   398                     detail.removeValue(QContactPhoneNumber::FieldSubTypes);
       
   399                     contact.saveDetail(&detail);
       
   400                 }
       
   401             }
       
   402         }
       
   403         else if(detail.definitionName() == QContactUrl::DefinitionName)
       
   404         {
       
   405             QContactUrl urlDetail = static_cast<QContactUrl>(detail);
       
   406             
       
   407             QString stype=QContactUrl::SubTypeHomePage;
       
   408             if(urlDetail.subType().compare(stype) != 0)
       
   409             {
       
   410                 detail.removeValue(QContactUrl::FieldSubType);
       
   411                 contact.saveDetail(&detail);
       
   412             }
       
   413         }
       
   414     }
       
   415     CNT_EXIT
       
   416 }
       
   417 
       
   418 // This method is inherited from CntAbstractServiceProvider
       
   419 void CntServices::CompleteServiceAndCloseApp(const QVariant& retValue)
       
   420 {
       
   421     CNT_ENTRY
       
   422     if (  mCurrentProvider )
       
   423         {
       
   424         mCurrentProvider->CompleteServiceAndCloseApp( retValue );
       
   425         }
       
   426     CNT_EXIT
       
   427 }
       
   428 
       
   429 void CntServices::setQuitable(bool quitable)
       
   430 {
       
   431     mIsQuitable = quitable;
       
   432 }
       
   433 
       
   434 void CntServices::quitApp()
       
   435 {
       
   436     CNT_ENTRY
       
   437    
       
   438     // Only embedded applications should be exited once a client
       
   439     // disconnects. At the moments QtHighWay has unresolved issues
       
   440     // when closing non-embedded applications. Error ou1cimx1#472852
       
   441     // has more info
       
   442     if ( mIsQuitable ) 
       
   443     {
       
   444        qApp->quit();
       
   445     }
       
   446    
       
   447     CNT_EXIT
       
   448 }
       
   449 
       
   450 Q_IMPLEMENT_USER_METATYPE(CntServicesContact)
       
   451 Q_IMPLEMENT_USER_METATYPE_NO_OPERATORS(CntServicesContactList)