emailservices/nmclientapi/src/nmapisettingsmanager_p.cpp
changeset 56 15bc1d5d6267
child 74 6c59112cfd31
equal deleted inserted replaced
51:d845db10c0d4 56:15bc1d5d6267
       
     1 /*
       
     2  * Copyright (c) 2010 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 <QVariant>
       
    19 #include <QList>
       
    20 
       
    21 #include <xqconversions.h>
       
    22 #include <xqsettingsmanager.h>
       
    23 #include <xqcentralrepositoryutils.h>
       
    24 #include <xqcentralrepositorysearchcriteria.h>
       
    25 
       
    26 #include <pop3set.h>
       
    27 #include <imapset.h>
       
    28 #include <smtpset.h>
       
    29 #include <iapprefs.h>
       
    30 
       
    31 #include "ipssettingkeys.h"
       
    32 #include "nmapiheaders.h"
       
    33 #include <nmapisettingsmanager.h>
       
    34 #include "nmapisettingsmanager_p.h"
       
    35 
       
    36 namespace EmailClientApi
       
    37 {
       
    38 
       
    39 NmApiSettingsManagerPrivate::NmApiSettingsManagerPrivate(const quint64 mailboxId)
       
    40     : mAccount(0),
       
    41     mPop3Settings(0),
       
    42     mImap4Settings(0),
       
    43     mSmtpSettings(0),
       
    44     mMailboxOffset(-1),
       
    45     mMailboxId(0)
       
    46 {
       
    47     QT_TRAP_THROWING(mAccount = CEmailAccounts::NewL());
       
    48     mMailboxId = (quint32)mailboxId;
       
    49 
       
    50     mQSettingsManager = new XQSettingsManager();
       
    51     Q_CHECK_PTR(mQSettingsManager);
       
    52 
       
    53     checkAccountType();
       
    54     QT_TRAP_THROWING(initAccountL());
       
    55     calculateMailboxOffset();
       
    56 
       
    57 }
       
    58 
       
    59 NmApiSettingsManagerPrivate::~NmApiSettingsManagerPrivate()
       
    60 {
       
    61     delete mPop3Settings;
       
    62     delete mImap4Settings;
       
    63     delete mSmtpSettings;
       
    64     delete mQSettingsManager;
       
    65     delete mAccount;
       
    66 }
       
    67 
       
    68 /*!
       
    69  Loads QMail specific settings.
       
    70  \param mailboxId which settings are returned
       
    71  \param data consists of keys and values of settings.
       
    72  \return bool <true> when the setting items were found otherwise <false>.
       
    73  */
       
    74 bool NmApiSettingsManagerPrivate::load(quint64 mailboxId, NmApiMailboxSettingsData &data)
       
    75 {
       
    76 	mMailboxId = mailboxId;
       
    77     return (readSetting(data) && readCenRepSetting(data));
       
    78 }
       
    79 
       
    80 /*!
       
    81  Saves QMail specific settings.
       
    82  \param data consists of keys and values of settings.
       
    83  \return bool <true> when the setting items were successfully saved otherwise <false>.
       
    84  */
       
    85 bool NmApiSettingsManagerPrivate::save(const NmApiMailboxSettingsData &data)
       
    86 {
       
    87     TInt err(KErrNone);
       
    88     TRAP(err, writeSettingL(data));
       
    89     return (!err && writeSettingToCenRep(data));
       
    90 }
       
    91 
       
    92 /*!
       
    93  Finds and returns the SMTP specific setting.
       
    94  \param settingItem SettingItem enum of the setting to return
       
    95  \param QVariant SettingValue of the found setting value.
       
    96  \return bool <true> when the setting item was found otherwise <false>.
       
    97  */
       
    98 bool NmApiSettingsManagerPrivate::readSetting(NmApiMailboxSettingsData &data)
       
    99 {
       
   100     if (mMailboxType == NmApiMailboxTypePop) {
       
   101         data.setValue(IncomingLoginName, XQConversions::s60Desc8ToQString(mPop3Settings->LoginName()));
       
   102         data.setValue(IncomingPassword, XQConversions::s60Desc8ToQString(mPop3Settings->Password()));
       
   103         data.setValue(MailboxName, XQConversions::s60DescToQString(mPop3Account.iPopAccountName));
       
   104         data.setValue(IncomingMailServer, XQConversions::s60DescToQString(mPop3Settings->ServerAddress()));
       
   105 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)
       
   106         data.setValue(IncomingMailUsesAuthentication, mPop3Settings->POP3Auth());
       
   107 #endif
       
   108         data.setValue(IncomingPort, mPop3Settings->Port());
       
   109     }
       
   110     else if (mMailboxType  == NmApiMailboxTypeImap) {
       
   111         data.setValue(IncomingLoginName, XQConversions::s60Desc8ToQString(mImap4Settings->LoginName()));
       
   112         data.setValue(IncomingPassword, XQConversions::s60Desc8ToQString(mImap4Settings->Password()));
       
   113         data.setValue(MailboxName, XQConversions::s60DescToQString(
       
   114                 mImap4Account.iImapAccountName));
       
   115         data.setValue(IncomingMailServer, XQConversions::s60DescToQString(
       
   116                 mImap4Settings->ServerAddress()));
       
   117 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)
       
   118         data.setValue(IncomingMailUsesAuthentication, mImap4Settings->IAMP4Auth());
       
   119 #endif
       
   120         data.setValue(IncomingPort, mImap4Settings->Port());
       
   121         data.setValue(FolderPath, XQConversions::s60Desc8ToQString(mImap4Settings->FolderPath()));
       
   122     }
       
   123 
       
   124     data.setValue(IncomingMailSecurityType, security());
       
   125     data.setValue(MyName, XQConversions::s60DescToQString(mSmtpSettings->EmailAlias()));
       
   126     data.setValue(EmailAlias, XQConversions::s60DescToQString(mSmtpSettings->EmailAlias()));
       
   127     data.setValue(EmailAddress, XQConversions::s60DescToQString(mSmtpSettings->EmailAddress()));
       
   128     data.setValue(ReplyAddress, XQConversions::s60DescToQString(mSmtpSettings->ReplyToAddress()));
       
   129     data.setValue(OutgoingMailServer, XQConversions::s60DescToQString(mSmtpSettings->ServerAddress()));
       
   130     data.setValue(OutgoingPort, mSmtpSettings->Port());
       
   131     data.setValue(OutgoingLoginName, XQConversions::s60Desc8ToQString(mSmtpSettings->LoginName()));
       
   132     data.setValue(OutgoingPassword, XQConversions::s60Desc8ToQString(mSmtpSettings->Password()));
       
   133     data.setValue(UseOutgoingAuthentication, mSmtpSettings->SMTPAuth());
       
   134     data.setValue(OutgoingMailUsesAuthentication, mSmtpSettings->SMTPAuth());
       
   135     data.setValue(OutgoingSecureSockets, mSmtpSettings->SecureSockets());
       
   136     data.setValue(OutgoingSSLWrapper, mSmtpSettings->SSLWrapper());
       
   137     data.setValue(OutgoingMailSecurityType, XQConversions::s60Desc8ToQString(mSmtpSettings->TlsSslDomain()));
       
   138     return true;
       
   139 }
       
   140 
       
   141 bool NmApiSettingsManagerPrivate::readCenRepSetting(NmApiMailboxSettingsData &data)
       
   142 {
       
   143     bool ret = true;
       
   144     QVariant tmp;
       
   145     data.setValue(DownloadPictures, readFromCenRep(IpsServices::EmailKeyPreferenceDownloadPictures));
       
   146     data.setValue(MessageDivider, readFromCenRep(IpsServices::EmailKeyPreferenceMessageDivider));
       
   147 
       
   148     tmp = readFromCenRep(IpsServices::EmailKeyReceptionActiveProfile);
       
   149     switch(tmp.toInt()){
       
   150         case 0:
       
   151             data.setValue(ReceptionActiveProfile, EmailClientApi::EmailProfileOffsetKUTD);
       
   152             break;
       
   153         case 1:
       
   154             data.setValue(ReceptionActiveProfile, EmailClientApi::EmailProfileOffsetSE);
       
   155             break;
       
   156         case 2:
       
   157             data.setValue(ReceptionActiveProfile, EmailClientApi::EmailProfileOffsetMF);
       
   158             break;
       
   159         case 3:
       
   160             data.setValue(ReceptionActiveProfile, EmailClientApi::EmailProfileOffsetUD);
       
   161             break;
       
   162         default:
       
   163             data.setValue(ReceptionActiveProfile, EmailClientApi::EmailProfileOffsetKUTD);
       
   164             break;
       
   165     };
       
   166 
       
   167     data.setValue(ReceptionUserDefinedProfile, readFromCenRep(IpsServices::EmailKeyReceptionUserDefinedProfile));
       
   168     data.setValue(ReceptionInboxSyncWindow, readFromCenRep(mActiveProfileOffset
       
   169                         + IpsServices::EmailKeyReceptionInboxSyncWindow));
       
   170     data.setValue(ReceptionGenericSyncWindowInMessages, readFromCenRep(mActiveProfileOffset
       
   171                         + IpsServices::EmailKeyReceptionGenericSyncWindowInMessages));
       
   172     data.setValue(ReceptionWeekDays, readFromCenRep(mActiveProfileOffset
       
   173                         + IpsServices::EmailKeyReceptionWeekDays));
       
   174     data.setValue(ReceptionDayStartTime, readFromCenRep(mActiveProfileOffset
       
   175                         + IpsServices::EmailKeyReceptionDayStartTime));
       
   176     data.setValue(ReceptionDayEndTime, readFromCenRep(mActiveProfileOffset
       
   177                         + IpsServices::EmailKeyReceptionDayEndTime));
       
   178     data.setValue(ReceptionRefreshPeriodDayTime, readFromCenRep(mActiveProfileOffset
       
   179                         + IpsServices::EmailKeyReceptionRefreshPeriodDayTime));
       
   180     data.setValue(ReceptionRefreshPeriodOther, readFromCenRep(mActiveProfileOffset
       
   181                         + IpsServices::EmailKeyReceptionRefreshPeriodOther));
       
   182     data.setValue(UserNameHidden, readFromCenRep(IpsServices::EmailKeyUserNameHidden));
       
   183     data.setValue(EmailNotificationState, readFromCenRep(IpsServices::EmailKeyEMNState));
       
   184     data.setValue(FirstEmnReceived, readFromCenRep(IpsServices::EmailKeyFirstEmnReceived));
       
   185     data.setValue(EmnReceivedNotSynced, readFromCenRep(IpsServices::EmailKeyEmnReceivedNotSynced));
       
   186 
       
   187     QString aolState = alwaysOnlineState();
       
   188     if(aolState.length()>0)
       
   189         data.setValue(AlwaysOnlineState, aolState);
       
   190 
       
   191     data.setValue(AoLastSuccessfulUpdate, readFromCenRep(IpsServices::EmailKeyAoLastSuccessfulUpdateL));
       
   192     data.setValue(AoLastUpdateFailed, readFromCenRep(IpsServices::EmailKeyAoLastUpdateFailed));
       
   193     data.setValue(AoUpdateSuccessfulWithCurSettings, readFromCenRep(IpsServices::EmailKeyAoUpdateSuccessfulWithCurSettings));
       
   194     return ret;
       
   195 }
       
   196 
       
   197 /*!
       
   198  Writes POP3/IMAP4 specific settings.
       
   199  \param data consists of keys and values of settings.
       
   200  */
       
   201 bool NmApiSettingsManagerPrivate::writeSettingL(const NmApiMailboxSettingsData &data)
       
   202 {
       
   203     HBufC *tmp = 0;
       
   204     HBufC8 *tmp8 = 0;
       
   205     bool ret(false);
       
   206     int key;
       
   207     QVariant settingValue;
       
   208     QList<int> keylist;
       
   209     bool aliasSet = false;
       
   210 
       
   211     keylist = data.listSettings();
       
   212     for (int i = 0; i < keylist.size(); i++) {
       
   213         key = keylist[i];
       
   214         data.getValue(key, settingValue);
       
   215         switch (key) {
       
   216             case IncomingLoginName: {
       
   217                 tmp8 = XQConversions::qStringToS60Desc8(settingValue.toString());
       
   218                 if (mMailboxType == NmApiMailboxTypePop) {
       
   219                     mPop3Settings->SetLoginNameL(*tmp8);
       
   220                 }
       
   221                 else if (mMailboxType == NmApiMailboxTypeImap) {
       
   222                     mImap4Settings->SetLoginNameL(*tmp8);
       
   223                     }
       
   224                 delete tmp8;
       
   225                 break;
       
   226             }
       
   227             case IncomingPassword: {
       
   228                 tmp8 = XQConversions::qStringToS60Desc8(settingValue.toString());
       
   229                 if (mMailboxType == NmApiMailboxTypePop) {
       
   230                     mPop3Settings->SetPasswordL(*tmp8);
       
   231                 }
       
   232                 else if (mMailboxType == NmApiMailboxTypeImap) {
       
   233                     mImap4Settings->SetPasswordL(*tmp8);
       
   234                     }
       
   235                 delete tmp8;
       
   236                 break;
       
   237             }
       
   238             case MailboxName: {
       
   239                 tmp = XQConversions::qStringToS60Desc(settingValue.toString());
       
   240                 if (mMailboxType == NmApiMailboxTypePop) {
       
   241                     mPop3Account.iPopAccountName.Copy(*tmp);
       
   242                 }
       
   243                 else if (mMailboxType == NmApiMailboxTypeImap) {
       
   244                     mImap4Account.iImapAccountName.Copy(*tmp);
       
   245                     }
       
   246                 delete tmp;
       
   247                 break;
       
   248             }
       
   249             case IncomingMailServer: {
       
   250                 tmp = XQConversions::qStringToS60Desc(settingValue.toString());
       
   251 
       
   252                 if (mMailboxType == NmApiMailboxTypePop) {
       
   253                     mPop3Settings->SetServerAddressL(*tmp);
       
   254                 }
       
   255                 else if (mMailboxType == NmApiMailboxTypeImap) {
       
   256                     mImap4Settings->SetServerAddressL(*tmp);
       
   257                     }
       
   258                 delete tmp;
       
   259                 break;
       
   260             }
       
   261             case IncomingMailUsesAuthentication: {
       
   262 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)
       
   263 
       
   264                 if (mMailboxType == NmApiMailboxTypePop) {
       
   265                     mPop3Settings->SetPOP3Auth(settingValue.toBool());
       
   266                 }
       
   267                 else if (mMailboxType == NmApiMailboxTypeImap) {
       
   268                     mImap4Settings->SetIAMP4Auth(settingValue.toBool());
       
   269                 }
       
   270 #endif
       
   271                 break;
       
   272             }
       
   273             case IncomingMailSecurityType: {
       
   274                 setSecurity(settingValue.toString());
       
   275                 break;
       
   276             }
       
   277             case IncomingPort: {
       
   278                 if (mMailboxType == NmApiMailboxTypePop) {
       
   279                 mPop3Settings->SetPort(settingValue.toInt());
       
   280                 }
       
   281                 else if (mMailboxType == NmApiMailboxTypeImap) {
       
   282                     mImap4Settings->SetPort(settingValue.toInt());
       
   283                 }
       
   284                 break;
       
   285             }
       
   286             case OutgoingPort: {
       
   287                 mSmtpSettings->SetPort(settingValue.toInt());
       
   288                 break;
       
   289             }
       
   290             case IncomingSecureSockets: {
       
   291                 if (mMailboxType == NmApiMailboxTypePop) {
       
   292                     mPop3Settings->SetSecureSockets(settingValue.toBool());
       
   293                  }
       
   294                  else if (mMailboxType == NmApiMailboxTypeImap) {
       
   295                     mImap4Settings->SetSecureSockets(settingValue.toBool());
       
   296                      }
       
   297                 break;
       
   298             }
       
   299             case IncomingSSLWrapper: {
       
   300                 if (mMailboxType == NmApiMailboxTypePop) {
       
   301                     mPop3Settings->SetSSLWrapper(settingValue.toBool());
       
   302                 }
       
   303                 else if (mMailboxType == NmApiMailboxTypeImap) {
       
   304                     mImap4Settings->SetSSLWrapper(settingValue.toBool());
       
   305                     }
       
   306                 break;
       
   307             }
       
   308             case EmailAddress: {
       
   309                 tmp = XQConversions::qStringToS60Desc(settingValue.toString());
       
   310                 mSmtpSettings->SetEmailAddressL(*tmp);
       
   311                 break;
       
   312             }
       
   313             case ReplyAddress: {
       
   314                 tmp = XQConversions::qStringToS60Desc(settingValue.toString());
       
   315                 mSmtpSettings->SetReplyToAddressL(*tmp);
       
   316                 break;
       
   317             }
       
   318             case EmailAlias: {
       
   319                 QVariant tmpName;
       
   320                 if(data.getValue(MyName,tmpName)) {
       
   321                     if(tmpName.toString()
       
   322                         ==XQConversions::s60DescToQString(mSmtpSettings->EmailAlias())) {
       
   323                         aliasSet = true;
       
   324                     }
       
   325                 }
       
   326                 tmp = XQConversions::qStringToS60Desc(settingValue.toString());
       
   327                 mSmtpSettings->SetEmailAliasL(*tmp);
       
   328                 break;
       
   329             }
       
   330             case MyName: {
       
   331                 if(!aliasSet) {
       
   332                 tmp = XQConversions::qStringToS60Desc(settingValue.toString());
       
   333                 mSmtpSettings->SetEmailAliasL(*tmp);
       
   334                 }
       
   335                 break;
       
   336             }
       
   337             case OutgoingMailServer: {
       
   338                 tmp = XQConversions::qStringToS60Desc(settingValue.toString());
       
   339                 mSmtpSettings->SetServerAddressL(*tmp);
       
   340                 break;
       
   341             }
       
   342             case OutgoingLoginName: {
       
   343                 tmp8 = XQConversions::qStringToS60Desc8(settingValue.toString());
       
   344                 mSmtpSettings->SetLoginNameL(*tmp8);
       
   345                 break;
       
   346             }
       
   347             case OutgoingPassword: {
       
   348                 tmp8 = XQConversions::qStringToS60Desc8(settingValue.toString());
       
   349                 mSmtpSettings->SetPasswordL(*tmp8);
       
   350                 break;
       
   351             }
       
   352             case UseOutgoingAuthentication:
       
   353             case OutgoingMailUsesAuthentication: {
       
   354                 mSmtpSettings->SetSMTPAuth(settingValue.toBool());
       
   355                 break;
       
   356             }
       
   357             case OutgoingSecureSockets: {
       
   358                 mSmtpSettings->SetSecureSockets(settingValue.toBool());
       
   359                 break;
       
   360             }
       
   361             case OutgoingSSLWrapper: {
       
   362                 mSmtpSettings->SetSSLWrapper(settingValue.toBool());
       
   363                 break;
       
   364             }
       
   365             case OutgoingMailSecurityType: {
       
   366                 tmp8 = XQConversions::qStringToS60Desc8(settingValue.toString());
       
   367                 mSmtpSettings->SetTlsSslDomainL(*tmp8);
       
   368                 break;
       
   369             }
       
   370             case FolderPath: {
       
   371                 tmp8 = XQConversions::qStringToS60Desc8(settingValue.toString());
       
   372                 mImap4Settings->SetFolderPathL(*tmp8);
       
   373                 break;
       
   374             }
       
   375             default: {
       
   376 				break;
       
   377             }
       
   378         };
       
   379     }
       
   380     ret = saveSettings();
       
   381     return ret;
       
   382 }
       
   383 
       
   384 /*!
       
   385 Writes settings to central repository.
       
   386 \param data consists of keys and values of settings.
       
   387  */
       
   388 bool NmApiSettingsManagerPrivate::writeSettingToCenRep(
       
   389     const NmApiMailboxSettingsData &data)
       
   390 {
       
   391     int key;
       
   392     QVariant settingValue;
       
   393     QList<int> keylist;
       
   394 
       
   395     keylist = data.listSettings();
       
   396     bool ret = false;
       
   397     if(keylist.contains(ReceptionActiveProfile)) {
       
   398         data.getValue(ReceptionActiveProfile,settingValue);
       
   399         QVariant profileOffset = 0;
       
   400         if(settingValue.toString() == EmailClientApi::EmailProfileOffsetKUTD) {
       
   401             profileOffset=0;
       
   402         } else if(settingValue.toString() == EmailClientApi::EmailProfileOffsetSE){
       
   403             profileOffset = 1;
       
   404         } else if (settingValue.toString() == EmailClientApi::EmailProfileOffsetMF){
       
   405                     profileOffset = 2;
       
   406         } else if (settingValue.toString() == EmailClientApi::EmailProfileOffsetUD) {
       
   407             profileOffset = 3;
       
   408         }
       
   409         ret = writeToCenRep(IpsServices::EmailKeyReceptionActiveProfile, profileOffset);
       
   410         if(ret) {
       
   411             calculateActiveProfileOffset();
       
   412         }
       
   413     }
       
   414     if (ret) {
       
   415         for (int i = 0; i < keylist.size(); i++) {
       
   416             key = keylist[i];
       
   417             data.getValue(key, settingValue);
       
   418             switch (key) {
       
   419                 case DownloadPictures: {
       
   420                     ret = writeToCenRep(IpsServices::EmailKeyPreferenceDownloadPictures,
       
   421                         settingValue);
       
   422                     break;
       
   423                 }
       
   424                 case MessageDivider: {
       
   425                     ret = writeToCenRep(IpsServices::EmailKeyPreferenceMessageDivider, settingValue);
       
   426                     break;
       
   427                 }
       
   428                 case ReceptionUserDefinedProfile: {
       
   429                     ret = writeToCenRep(IpsServices::EmailKeyReceptionUserDefinedProfile,
       
   430                         settingValue);
       
   431                     break;
       
   432                 }
       
   433                 case ReceptionInboxSyncWindow: {
       
   434                     ret = writeToCenRep(mActiveProfileOffset
       
   435                         + IpsServices::EmailKeyReceptionInboxSyncWindow, settingValue);
       
   436                     break;
       
   437                 }
       
   438                 case ReceptionGenericSyncWindowInMessages: {
       
   439                     ret = writeToCenRep(mActiveProfileOffset
       
   440                         + IpsServices::EmailKeyReceptionGenericSyncWindowInMessages, settingValue);
       
   441                     break;
       
   442                 }
       
   443                 case ReceptionWeekDays: {
       
   444                     ret = writeToCenRep(mActiveProfileOffset + IpsServices::EmailKeyReceptionWeekDays,
       
   445                         settingValue);
       
   446                     break;
       
   447                 }
       
   448                 case ReceptionDayStartTime: {
       
   449                     ret = writeToCenRep(mActiveProfileOffset + IpsServices::EmailKeyReceptionDayStartTime,
       
   450                         settingValue);
       
   451                     break;
       
   452                 }
       
   453                 case ReceptionDayEndTime: {
       
   454                     ret = writeToCenRep(mActiveProfileOffset + IpsServices::EmailKeyReceptionDayEndTime,
       
   455                         settingValue);
       
   456                     break;
       
   457                 }
       
   458                 case ReceptionRefreshPeriodDayTime: {
       
   459                     ret = writeToCenRep(mActiveProfileOffset
       
   460                         + IpsServices::EmailKeyReceptionRefreshPeriodDayTime, settingValue);
       
   461                     break;
       
   462                 }
       
   463                 case ReceptionRefreshPeriodOther: {
       
   464                     ret = writeToCenRep(mActiveProfileOffset
       
   465                         + IpsServices::EmailKeyReceptionRefreshPeriodOther, settingValue);
       
   466                     break;
       
   467                 }
       
   468                 case UserNameHidden: {
       
   469                     ret = writeToCenRep(IpsServices::EmailKeyUserNameHidden, settingValue);
       
   470                     break;
       
   471                 }
       
   472                 case EmailNotificationState: {
       
   473                     ret = writeToCenRep(IpsServices::EmailKeyEMNState, settingValue);
       
   474                     break;
       
   475                 }
       
   476                 case FirstEmnReceived: {
       
   477                     ret = writeToCenRep(IpsServices::EmailKeyFirstEmnReceived, settingValue);
       
   478                     break;
       
   479                 }
       
   480                 case EmnReceivedNotSynced: {
       
   481                     ret = writeToCenRep(IpsServices::EmailKeyEmnReceivedNotSynced, settingValue);
       
   482                     break;
       
   483                 }
       
   484                 case AlwaysOnlineState: {
       
   485                     setAlwaysOnlineState(settingValue);
       
   486                     break;
       
   487                 }
       
   488                 case AoLastSuccessfulUpdate: {
       
   489                     ret = writeToCenRep(IpsServices::EmailKeyAoLastSuccessfulUpdateL, settingValue);
       
   490                     break;
       
   491                 }
       
   492                 case AoLastUpdateFailed: {
       
   493                     ret = writeToCenRep(IpsServices::EmailKeyAoLastUpdateFailed, settingValue);
       
   494                     break;
       
   495                 }
       
   496                 case AoUpdateSuccessfulWithCurSettings: {
       
   497                     ret = writeToCenRep(IpsServices::EmailKeyAoUpdateSuccessfulWithCurSettings,
       
   498                         settingValue);
       
   499                     break;
       
   500                 }
       
   501                 case ReceptionActiveProfile:
       
   502                 default: {
       
   503                     break;
       
   504                 }
       
   505             };
       
   506         }
       
   507     }
       
   508     return ret;
       
   509 }
       
   510 /*!
       
   511  Stores the POP3/IMAP4 specific settings.
       
   512  \return bool <true> when the settings were succesfully written, otherwise <false>.
       
   513  */
       
   514 bool NmApiSettingsManagerPrivate::saveSettings()
       
   515 {
       
   516     bool ret(false);
       
   517     if (mMailboxType == NmApiMailboxTypePop) {
       
   518         TRAPD(err, mAccount->SavePopSettingsL(mPop3Account, *mPop3Settings));
       
   519         if (err == KErrNone) {
       
   520             ret = true;
       
   521         }
       
   522     }
       
   523     else if (mMailboxType == NmApiMailboxTypeImap) {
       
   524         TRAPD(err, mAccount->SaveImapSettingsL(mImap4Account, *mImap4Settings));
       
   525         if (err == KErrNone) {
       
   526             ret = true;
       
   527         }
       
   528     }
       
   529     return ret;
       
   530 }
       
   531 
       
   532 /*!
       
   533  Reads a key value from the Central Repository.
       
   534  \param key Key identifier.
       
   535  \return the settings value for the given key.
       
   536  */
       
   537 QVariant NmApiSettingsManagerPrivate::readFromCenRep(quint32 key) const
       
   538 {
       
   539     XQCentralRepositorySettingsKey settingKey(IpsServices::EmailMailboxSettingRepository,
       
   540         mMailboxOffset + key);
       
   541     return mQSettingsManager->readItemValue(settingKey);
       
   542 }
       
   543 
       
   544 /*!
       
   545  Writes a key value to the Central Repository.
       
   546  \param key Key identifier.
       
   547  \param value The settings value for the given key.
       
   548  \return Returns <true> if the value was succesfully written, <false> if not.
       
   549  */
       
   550 bool NmApiSettingsManagerPrivate::writeToCenRep(quint32 key, const QVariant &value) const
       
   551 {
       
   552     XQCentralRepositorySettingsKey settingKey(IpsServices::EmailMailboxSettingRepository,
       
   553         mMailboxOffset + key);
       
   554     return mQSettingsManager->writeItemValue(settingKey, value);
       
   555 }
       
   556 
       
   557 void NmApiSettingsManagerPrivate::setSecurity(QString securityType)
       
   558 {
       
   559     if (securityType == NmApiStartTls) {
       
   560         if (mMailboxType == NmApiMailboxTypePop) {
       
   561             mPop3Settings->SetSecureSockets(ETrue);
       
   562             mPop3Settings->SetSSLWrapper(EFalse);
       
   563         }
       
   564         else {
       
   565             mImap4Settings->SetSecureSockets(ETrue);
       
   566             mImap4Settings->SetSSLWrapper(EFalse);
       
   567         }
       
   568     }
       
   569     else if (securityType == NmApiSSLTls) {
       
   570         if (mMailboxType == NmApiMailboxTypePop) {
       
   571             mPop3Settings->SetSecureSockets(EFalse);
       
   572             mPop3Settings->SetSSLWrapper(ETrue);
       
   573         }
       
   574         else {
       
   575             mImap4Settings->SetSecureSockets(EFalse);
       
   576             mImap4Settings->SetSSLWrapper(ETrue);
       
   577         }
       
   578     }
       
   579     else {
       
   580         if (mMailboxType == NmApiMailboxTypePop) {
       
   581             mPop3Settings->SetSecureSockets(EFalse);
       
   582             mPop3Settings->SetSSLWrapper(EFalse);
       
   583         }
       
   584         else {
       
   585             mImap4Settings->SetSecureSockets(EFalse);
       
   586             mImap4Settings->SetSSLWrapper(EFalse);
       
   587         }
       
   588     }
       
   589 }
       
   590 
       
   591 QString NmApiSettingsManagerPrivate::security() const
       
   592 {
       
   593     bool ss(false);
       
   594     bool sslw(false);
       
   595     QString securityType("");
       
   596 
       
   597     if (mMailboxType == NmApiMailboxTypePop) {
       
   598         ss = mPop3Settings->SecureSockets();
       
   599         sslw = mPop3Settings->SSLWrapper();
       
   600     }
       
   601     else {
       
   602         ss = mImap4Settings->SecureSockets();
       
   603         sslw = mImap4Settings->SSLWrapper();
       
   604     }
       
   605 
       
   606     if (ss == true && sslw == false) {
       
   607         securityType = NmApiStartTls;
       
   608     }
       
   609     else if (ss == false && sslw == true) {
       
   610         securityType = NmApiSSLTls;
       
   611     }
       
   612     else {
       
   613         securityType = NmApiSecurityOff;
       
   614     }
       
   615     return securityType;
       
   616 }
       
   617 
       
   618 /*!
       
   619 
       
   620  */
       
   621 QString NmApiSettingsManagerPrivate::alwaysOnlineState() const
       
   622 {
       
   623     TInt profile = IpsServices::EmailSyncProfileManualFetch;
       
   624     QString ret = NmApiOff;
       
   625 
       
   626     QVariant state;
       
   627     TInt settingValue = -1;
       
   628     state = readFromCenRep(IpsServices::EmailKeyReceptionActiveProfile);
       
   629     settingValue = state.toInt();
       
   630 
       
   631     if (settingValue >= 0) {
       
   632         profile = settingValue;
       
   633         if (profile != IpsServices::EmailSyncProfileManualFetch) {
       
   634             ret = NmApiAlways;
       
   635         }
       
   636     }
       
   637     return ret;
       
   638 }
       
   639 
       
   640 /*!
       
   641 
       
   642  */
       
   643 void NmApiSettingsManagerPrivate::setAlwaysOnlineState(const QVariant state)
       
   644 {
       
   645     //only allowed to switch state off, not on.
       
   646     if (state.toString() == NmApiOff) {
       
   647         writeToCenRep(IpsServices::EmailKeyReceptionActiveProfile,
       
   648             IpsServices::EmailSyncProfileManualFetch);
       
   649     }
       
   650 }
       
   651 
       
   652 void NmApiSettingsManagerPrivate::calculateMailboxOffset()
       
   653 {
       
   654     // Find all the keys that match the criteria 0xZZZZZ000, where Z=don't care.
       
   655     // This will give us all the keys that hold the mailbox ids, e.g. 0x00001000, 0x00002000, etc.
       
   656     quint32 partialKey(0x00000000);
       
   657     quint32 bitMask(0x00000FFF);
       
   658     XQCentralRepositorySearchCriteria criteria(IpsServices::EmailMailboxSettingRepository,
       
   659         partialKey, bitMask);
       
   660     // Set the mailbox id is value criteria for the search.
       
   661     criteria.setValueCriteria((int) mMailboxId);
       
   662 
       
   663     // Find the keys.
       
   664     XQCentralRepositoryUtils utils(*mQSettingsManager);
       
   665     QList<XQCentralRepositorySettingsKey> foundKeys = utils.findKeys(criteria);
       
   666 
       
   667     // We should only get one key as a result.
       
   668     if (foundKeys.count() == 1) {
       
   669         mMailboxOffset = foundKeys[0].key();
       
   670         calculateActiveProfileOffset();
       
   671     }
       
   672     else {
       
   673         mActiveProfileOffset = -1;
       
   674         mMailboxOffset = -1;
       
   675     }
       
   676 }
       
   677 
       
   678 /*!
       
   679  Calculates the active reception schedule profile offset.
       
   680  */
       
   681 void NmApiSettingsManagerPrivate::calculateActiveProfileOffset()
       
   682 {
       
   683     QVariant activeProfile = readFromCenRep(IpsServices::EmailKeyReceptionActiveProfile);
       
   684     mActiveProfileOffset = convertToProfileOffset(activeProfile.toInt());
       
   685 }
       
   686 
       
   687 /*!
       
   688 
       
   689  */
       
   690 qint32 NmApiSettingsManagerPrivate::convertToProfileOffset(int profile) const
       
   691 {
       
   692     quint32 profileOffset(0);
       
   693     // Possible values are defined in ipssettingkeys.h
       
   694     switch (profile) {
       
   695         case 0: {
       
   696             profileOffset = IpsServices::EmailProfileOffsetKUTD;
       
   697             break;
       
   698         }
       
   699         case 1: {
       
   700             profileOffset = IpsServices::EmailProfileOffsetSE;
       
   701             break;
       
   702         }
       
   703         case 2: {
       
   704             profileOffset = IpsServices::EmailProfileOffsetMF;
       
   705             break;
       
   706         }
       
   707         case 3: {
       
   708             profileOffset = IpsServices::EmailProfileOffsetUD;
       
   709             break;
       
   710         }
       
   711         default: {
       
   712             break;
       
   713         }
       
   714     };
       
   715 
       
   716     return profileOffset;
       
   717 }
       
   718 
       
   719 bool NmApiSettingsManagerPrivate::checkAccountType()
       
   720 {
       
   721     bool identified = false;
       
   722 
       
   723     RArray<TImapAccount> imapAccounts;
       
   724     CleanupClosePushL(imapAccounts);
       
   725     TRAPD(err, mAccount->GetImapAccountsL(imapAccounts));
       
   726     if (err == KErrNone) {
       
   727         for (int i=0; i < imapAccounts.Count(); ++i) {
       
   728             TImapAccount account = imapAccounts[i];
       
   729             if (account.iImapService == mMailboxId) {
       
   730                 mImap4Account = account;
       
   731                 mMailboxType = NmApiMailboxTypeImap;
       
   732                 identified = true;
       
   733                 break;
       
   734             }
       
   735         }
       
   736     }
       
   737     CleanupStack::PopAndDestroy(&imapAccounts);
       
   738 
       
   739     if (!identified) {
       
   740         RArray<TPopAccount> popAccounts;
       
   741         CleanupClosePushL(popAccounts);
       
   742         TRAPD(err, mAccount->GetPopAccountsL(popAccounts));
       
   743         if (err == KErrNone) {
       
   744             for (int i = 0; i < popAccounts.Count(); ++i) {
       
   745                 TPopAccount account = popAccounts[i];
       
   746                 if (popAccounts[i].iPopService == mMailboxId) {
       
   747                     mPop3Account = account;
       
   748                     mMailboxType = NmApiMailboxTypePop;
       
   749                     identified = true;
       
   750                     break;
       
   751                 }
       
   752             }
       
   753         }
       
   754         CleanupStack::PopAndDestroy(&popAccounts);
       
   755     }
       
   756 
       
   757     return identified;
       
   758 }
       
   759 
       
   760 void NmApiSettingsManagerPrivate::initAccountL()
       
   761 {
       
   762     if (mMailboxType == NmApiMailboxTypePop) {
       
   763         mPop3Settings = new(ELeave) CImPop3Settings();
       
   764         mAccount->LoadPopSettingsL(mPop3Account, *mPop3Settings);
       
   765     }
       
   766     else if (mMailboxType == NmApiMailboxTypeImap) {
       
   767         mImap4Settings = new(ELeave) CImImap4Settings();
       
   768         mAccount->LoadImapSettingsL(mImap4Account, *mImap4Settings);
       
   769     }
       
   770 
       
   771     mSmtpSettings = new(ELeave) CImSmtpSettings();
       
   772 
       
   773     if (mMailboxType == NmApiMailboxTypePop) {
       
   774         mAccount->GetSmtpAccountL(mPop3Account.iSmtpService, mSmtpAccount);
       
   775     }
       
   776     else if (mMailboxType == NmApiMailboxTypeImap) {
       
   777         mAccount->GetSmtpAccountL(mImap4Account.iSmtpService, mSmtpAccount);
       
   778     }
       
   779     mAccount->LoadSmtpSettingsL(mSmtpAccount, *mSmtpSettings);
       
   780 }
       
   781 
       
   782 } // end namespace