omadm/omadmextensions/adapters/email/src/nsmldmemailadapter.cpp
branchRCL_3
changeset 58 5b858729772b
equal deleted inserted replaced
57:6757f1e2efd2 58:5b858729772b
       
     1 /*
       
     2 * Copyright (c) 2004 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:    DM Email Settings Adapter
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <implementationproxy.h> // For TImplementationProxy definition
       
    22 #include <utf.h>
       
    23 #include <iapprefs.h>
       
    24 #include <smtpset.h>
       
    25 #include <pop3set.h>
       
    26 #include <imapset.h>
       
    27 #include <sysutil.h>
       
    28 #include <etelmm.h>
       
    29 
       
    30 #include <cemailaccounts.h>
       
    31 
       
    32 #include "nsmldmimpluids.h"
       
    33 #include "nsmldmemailadapter.h"
       
    34 #include "nsmldebug.h"
       
    35 #include "nsmlconstants.h"
       
    36 #include "nsmldmconst.h"
       
    37 #include "nsmldmiapmatcher.h"
       
    38 
       
    39 #ifndef __WINS__
       
    40 // This lowers the unnecessary compiler warning (armv5) to remark.
       
    41 // "Warning:  #174-D: expression has no effect..." is caused by 
       
    42 // DBG_ARGS8 macro in no-debug builds.
       
    43 #pragma diag_remark 174
       
    44 #endif
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CNSmlDmEmailAdapter* CNSmlDmEmailAdapter::NewL( )
       
    48 // -----------------------------------------------------------------------------
       
    49 CNSmlDmEmailAdapter* CNSmlDmEmailAdapter::NewL(MSmlDmCallback* aDmCallback )
       
    50     {
       
    51     _DBG_FILE("CNSmlDmEmailAdapter::NewL(): begin");
       
    52     CNSmlDmEmailAdapter* self = new (ELeave) CNSmlDmEmailAdapter(aDmCallback);
       
    53     CleanupStack::PushL(self);
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop();
       
    56     _DBG_FILE("CNSmlDmEmailAdapter::NewL(): end");
       
    57     return self;
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CNSmlDmEmailAdapter::CNSmlDmEmailAdapter()
       
    62 // -----------------------------------------------------------------------------
       
    63 
       
    64 CNSmlDmEmailAdapter::CNSmlDmEmailAdapter( MSmlDmCallback* aDmCallback )
       
    65     : CSmlDmAdapter(aDmCallback)
       
    66     {
       
    67     _DBG_FILE("CNSmlDmEmailAdapter::CNSmlDmEmailAdapter(): begin");
       
    68     iMsvSession = NULL;
       
    69     _DBG_FILE("CNSmlDmEmailAdapter::CNSmlDmEmailAdapter(): end");
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CNSmlDmEmailAdapter::ConstructL()
       
    74 // -----------------------------------------------------------------------------
       
    75 void CNSmlDmEmailAdapter::ConstructL()
       
    76     {
       
    77     iBuffer = new (ELeave) CArrayFixFlat <TNSmlEmailSettingsElement> 
       
    78         (KNSmlDmEmailGranularity);
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CNSmlDmEmailAdapter::~CNSmlDmEmailAdapter()
       
    83 // -----------------------------------------------------------------------------
       
    84 CNSmlDmEmailAdapter::~CNSmlDmEmailAdapter()
       
    85     {
       
    86     _DBG_FILE("CNSmlDmEmailAdapter::~CNSmlDmEmailAdapter(): begin");
       
    87     delete iMsvSession;
       
    88     if(iBuffer)
       
    89         {
       
    90         for(TInt i=0;i<iBuffer->Count();i++)
       
    91             {
       
    92             delete iBuffer->At(i).iMappingName;
       
    93             delete iBuffer->At(i).iName;
       
    94             delete iBuffer->At(i).iSmtpSettings;
       
    95             delete iBuffer->At(i).iPop3Settings;
       
    96             delete iBuffer->At(i).iImap4Settings;
       
    97             delete iBuffer->At(i).iIapPref;
       
    98             delete iBuffer->At(i).iSmtpIapPref;
       
    99 
       
   100             if(iBuffer->At(i).iNodeBuf)
       
   101                 {
       
   102                 for(TInt j=0;j<iBuffer->At(i).iNodeBuf->Count();j++)
       
   103                     {
       
   104                     delete iBuffer->At(i).iNodeBuf->At(j).iLastUriSeg;
       
   105                     delete iBuffer->At(i).iNodeBuf->At(j).iData;
       
   106                     }
       
   107                 iBuffer->At(i).iNodeBuf->Reset();
       
   108                 delete iBuffer->At(i).iNodeBuf;
       
   109                 }
       
   110             }
       
   111         iBuffer->Reset();
       
   112         delete iBuffer;
       
   113         }
       
   114 
       
   115     _DBG_FILE("CNSmlDmEmailAdapter::~CNSmlDmEmailAdapter(): end");
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 //  CNSmlDmEmailAdapter::DDFVersionL()
       
   120 // -----------------------------------------------------------------------------
       
   121 void CNSmlDmEmailAdapter::DDFVersionL(CBufBase& aDDFVersion)
       
   122     {
       
   123     _DBG_FILE("CNSmlDmEmailAdapter::DDFVersionL(TDes& aDDFVersion): begin");
       
   124     aDDFVersion.InsertL(0,KNSmlDMEmailDDFVersion);
       
   125     _DBG_FILE("CNSmlDmEmailAdapter::DDFVersionL(TDes& aDDFVersion): end");
       
   126     }
       
   127 
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 //  CNSmlDmEmailAdapter::DDFStructureL()
       
   131 //
       
   132 // -----------------------------------------------------------------------------
       
   133 void CNSmlDmEmailAdapter::DDFStructureL(MSmlDmDDFObject& aDDF)
       
   134     {
       
   135     _DBG_FILE("CNSmlDmEmailAdapter::DDFStructureL(): begin");
       
   136 
       
   137     TSmlDmAccessTypes accessTypesNode;
       
   138 
       
   139     accessTypesNode.SetGet();
       
   140 
       
   141     // Email
       
   142     MSmlDmDDFObject& email = aDDF.AddChildObjectL(KNSmlDMEmailNodeName);
       
   143     FillNodeInfoL(email,accessTypesNode,MSmlDmDDFObject::EOne,
       
   144                   MSmlDmDDFObject::EPermanent,
       
   145                   MSmlDmDDFObject::ENode,KNSmlDMEmailDescription);
       
   146 
       
   147     accessTypesNode.SetAdd();
       
   148     accessTypesNode.SetDelete();
       
   149     accessTypesNode.SetReplace();
       
   150     MSmlDmDDFObject& rtNode = email.AddChildObjectGroupL();
       
   151 
       
   152 
       
   153     FillNodeInfoL(rtNode,accessTypesNode,MSmlDmDDFObject::EZeroOrMore,
       
   154                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::ENode,
       
   155                   KNSmlDMDynamicNodeDescription);
       
   156 
       
   157     TSmlDmAccessTypes accessTypes;
       
   158     accessTypes.SetGet();
       
   159     accessTypes.SetAdd();
       
   160     accessTypes.SetReplace();
       
   161     //displayable account name
       
   162     MSmlDmDDFObject& name = rtNode.AddChildObjectL(KNSmlDMEmailName);
       
   163     FillNodeInfoL(name,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   164                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   165                   KNSmlDMNameDescription);
       
   166 
       
   167     //user id for email account
       
   168     MSmlDmDDFObject& uid = rtNode.AddChildObjectL(KNSmlDMEmailUID);
       
   169     FillNodeInfoL(uid,accessTypes,MSmlDmDDFObject::EOne,
       
   170                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   171                   KNSmlDMUIDDescription);
       
   172 
       
   173     //password for email account
       
   174     TSmlDmAccessTypes accessTypesPw;
       
   175     accessTypesPw.SetAdd();
       
   176     accessTypesPw.SetReplace();
       
   177     MSmlDmDDFObject& pw = rtNode.AddChildObjectL(KNSmlDMEmailPW);
       
   178     FillNodeInfoL(pw,accessTypesPw,MSmlDmDDFObject::EZeroOrOne,
       
   179                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   180                   KNSmlDMPWDescription);
       
   181 
       
   182     //user's email address
       
   183     MSmlDmDDFObject& uaddr = rtNode.AddChildObjectL(KNSmlDMEmailUAddr);
       
   184     FillNodeInfoL(uaddr,accessTypes,MSmlDmDDFObject::EOne,
       
   185                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   186                   KNSmlDMUAddrDescription);
       
   187 
       
   188     //Displayable name for the user
       
   189     MSmlDmDDFObject& uname = rtNode.AddChildObjectL(KNSmlDMEmailUName);
       
   190     FillNodeInfoL(uname,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   191                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   192                   KNSmlDMUNameDescription);
       
   193 
       
   194     //Host name for the receiving server
       
   195     MSmlDmDDFObject& mrcv = rtNode.AddChildObjectL(KNSmlDMEmailMrcv);
       
   196     FillNodeInfoL(mrcv,accessTypes,MSmlDmDDFObject::EOne,
       
   197                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   198                   KNSmlDMMrcvDescription);
       
   199 
       
   200     //Host name for the sending server
       
   201     MSmlDmDDFObject& msnd = rtNode.AddChildObjectL(KNSmlDMEmailMsnd);
       
   202     FillNodeInfoL(msnd,accessTypes,MSmlDmDDFObject::EOne,
       
   203                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   204                   KNSmlDMMsndDescription);
       
   205 
       
   206     //Remote mailbox protocol
       
   207     MSmlDmDDFObject& mpro = rtNode.AddChildObjectL(KNSmlDMEmailMpro);
       
   208     FillNodeInfoL(mpro,accessTypes,MSmlDmDDFObject::EOne,
       
   209                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   210                   KNSmlDMMproDescription);
       
   211 
       
   212     //Use secure connection
       
   213     MSmlDmDDFObject& useSecCon = rtNode.AddChildObjectL(KNSmlDMEmailUseSecCon);
       
   214     FillNodeInfoL(useSecCon,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   215                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EBool,
       
   216                   KNSmlDMUseSecConDescription);
       
   217 
       
   218     //Use APop
       
   219     MSmlDmDDFObject& useAPOP = rtNode.AddChildObjectL(KNSmlDMEmailUseAPOP);
       
   220     FillNodeInfoL(useAPOP, accessTypes, MSmlDmDDFObject::EZeroOrOne,
       
   221                   MSmlDmDDFObject::EDynamic, MSmlDmDDFObject::EBool,
       
   222                   KNSmlDMUseAPOPDescription );
       
   223 
       
   224     //Use smtp authentication
       
   225     MSmlDmDDFObject& useSauth = rtNode.AddChildObjectL(KNSmlDMEmailUseSauth);
       
   226     FillNodeInfoL(useSauth,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   227                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EBool,
       
   228                   KNSmlDMUseSauthDescription);
       
   229 
       
   230     //smtp authentication user id
       
   231     MSmlDmDDFObject& sauthUid = rtNode.AddChildObjectL(KNSmlDMEmailSauthUID);
       
   232     FillNodeInfoL(sauthUid,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   233                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   234                   KNSmlDMSauthUIDDescription);
       
   235 
       
   236     //Use smtp authentication
       
   237     MSmlDmDDFObject& sauthPw = rtNode.AddChildObjectL(KNSmlDMEmailSauthPW);
       
   238     FillNodeInfoL(sauthPw,accessTypesPw,MSmlDmDDFObject::EZeroOrOne,
       
   239                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   240                   KNSmlDMSauthPWDescription);
       
   241 
       
   242     //Use smtp authentication
       
   243     MSmlDmDDFObject& ptxtSAuth = rtNode.AddChildObjectL(KNSmlDMEmailPtxtSAuth);
       
   244     FillNodeInfoL(ptxtSAuth,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   245                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EBool,
       
   246                   KNSmlDMPtxtSAuthDescription);
       
   247 
       
   248     //Link to IAP
       
   249     MSmlDmDDFObject& toNapId = rtNode.AddChildObjectL(KNSmlDMEmailToNapID);
       
   250     FillNodeInfoL(toNapId,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   251                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   252                   KNSmlDMToNapIDDescription);
       
   253 
       
   254     //Link to IAP for SMTP
       
   255     MSmlDmDDFObject& sToNapId = rtNode.AddChildObjectL(KNSmlDMEmailSToNapID);
       
   256     FillNodeInfoL(sToNapId,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   257                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   258                   KNSmlDMSToNapIDDescription);
       
   259 
       
   260     //SSL/TLS used in SMTP
       
   261     MSmlDmDDFObject& sUseSecCon = rtNode.
       
   262         AddChildObjectL(KNSmlDMEmailSUseSecCon);
       
   263     FillNodeInfoL(sUseSecCon,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   264                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EBool,
       
   265                   KNSmlDMSUseSecConDescription);
       
   266 
       
   267     //StartTLS used in IMAP/POP
       
   268     MSmlDmDDFObject& useStartTls = rtNode.
       
   269         AddChildObjectL(KNSmlDMEmailUseStartTLS);
       
   270     FillNodeInfoL(useStartTls,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   271                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EBool,
       
   272                   KNSmlDMUseStartTLSDescription);
       
   273 
       
   274     //StartTLS used in SMTP
       
   275     MSmlDmDDFObject& sUseStartTls = rtNode.
       
   276         AddChildObjectL(KNSmlDMEmailSUseStartTLS);
       
   277     FillNodeInfoL(sUseStartTls,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   278                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EBool,
       
   279                   KNSmlDMSUseStartTLSDescription);
       
   280 
       
   281     //port of IMAP/POP server
       
   282     MSmlDmDDFObject& mrcvPort = rtNode.AddChildObjectL(KNSmlDMEmailMrcvPort);
       
   283     FillNodeInfoL(mrcvPort,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   284                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EInt,
       
   285                   KNSmlDMMrcvPortDescription);
       
   286 
       
   287     //port of SMTP server
       
   288     MSmlDmDDFObject& msndPort = rtNode.AddChildObjectL(KNSmlDMEmailMsndPort);
       
   289     FillNodeInfoL(msndPort,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   290                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EInt,
       
   291                   KNSmlDMMsndPortDescription);
       
   292 
       
   293     //IMAP folder path
       
   294     MSmlDmDDFObject& folderPath = rtNode.
       
   295         AddChildObjectL(KNSmlDMEmailFolderPath);
       
   296     FillNodeInfoL(folderPath,accessTypes,MSmlDmDDFObject::EZeroOrOne,
       
   297                   MSmlDmDDFObject::EDynamic,MSmlDmDDFObject::EChr,
       
   298                   KNSmlDMFolderPathDescription);
       
   299 
       
   300     _DBG_FILE("CNSmlDmEmailAdapter::DDFStructureL(): end");
       
   301     }
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 //  CNSmlDmEmailAdapter::UpdateLeafObjectL()
       
   305 // -----------------------------------------------------------------------------
       
   306 void CNSmlDmEmailAdapter::UpdateLeafObjectL( const TDesC8& aURI, 
       
   307                                              const TDesC8& aLUID,
       
   308                                              const TDesC8& aObject,
       
   309                                              const TDesC8& /*aType*/,
       
   310                                              const TInt aStatusRef )
       
   311     {
       
   312     _DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(): begin");
       
   313 
       
   314     TInt index = -1;
       
   315     TBool firstAdd = EFalse;
       
   316 
       
   317     for(TInt i = 0; i<iBuffer->Count(); i++)
       
   318         {
       
   319         TPtrC8 parentUri = RemoveLastURISeg(aURI);
       
   320         //Check Node from buffer if match
       
   321         if(iBuffer->At(i).iMappingName->Compare(parentUri) == 0 )
       
   322             {
       
   323             firstAdd = ETrue;
       
   324             index = i;
       
   325             break;
       
   326             }
       
   327         }
       
   328  	
       
   329  	TInt luid = DesToInt(aLUID);
       
   330 			
       
   331     if(aLUID.Length()==0 && index<0)
       
   332         {
       
   333         	luid = ConstructTreeL(aURI);
       
   334         	if(luid == -1)        	
       
   335         	{
       
   336         		Callback().SetStatusL(aStatusRef, CSmlDmAdapter::ENotFound);
       
   337         		_DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(): end");
       
   338         		return;
       
   339         	}
       
   340         }
       
   341 
       
   342     if( OpenSessionL() != KErrNone )
       
   343         {
       
   344         Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
   345         _DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(): end");
       
   346         return;
       
   347         }
       
   348 
       
   349     if(!firstAdd)
       
   350         {
       
   351 
       
   352         if(LastURISeg(aURI).Compare(KNSmlDMEmailMpro)==0)
       
   353             {
       
   354             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
   355             _DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(): end");
       
   356             return;
       
   357             }
       
   358         index = -1;
       
   359         for(TInt i=0;i<iBuffer->Count();i++)
       
   360             {
       
   361             if(iBuffer->At(i).iLuid == luid)
       
   362                 {
       
   363                 index = i;
       
   364                 break;
       
   365                 }
       
   366             }
       
   367 
       
   368         if(index<0)
       
   369             {
       
   370             TNSmlEmailSettingsElement newNode;
       
   371             newNode.iImap4Settings = NULL;
       
   372             newNode.iPop3Settings = NULL;
       
   373             newNode.iSmtpSettings = NULL;
       
   374             newNode.iIapPref = NULL;
       
   375             newNode.iSmtpIapPref = NULL;
       
   376             newNode.iLuid = luid;
       
   377             newNode.iMappingName = aURI.AllocLC();
       
   378             newNode.iName = HBufC::NewLC(0);
       
   379             newNode.iNodeBuf = new (ELeave) CArrayFixFlat 
       
   380                 <TNSmlEmailCommandElement> (KNSmlDmEmailGranularity);
       
   381             newNode.iExecuted = ETrue;
       
   382             GetIdByLuid(newNode.iAccType, newNode.iAccId, luid); 
       
   383             iBuffer->AppendL(newNode);
       
   384             index = iBuffer->Count() - 1;
       
   385             TInt err = DoRestoreSettingsL(index);
       
   386             if(err!=KErrNone)
       
   387                 {
       
   388                 CleanupStack::PopAndDestroy(2); //newNode.iMappingName,newNode.iName
       
   389                 newNode.iMappingName = NULL;
       
   390                 newNode.iName = NULL;
       
   391                 iBuffer->At(index).iNodeBuf->Reset();
       
   392                 delete iBuffer->At(index).iNodeBuf;
       
   393                 iBuffer->Delete(index);
       
   394                 }
       
   395             if(err==KErrNotFound)
       
   396                 {
       
   397                 Callback().SetStatusL(aStatusRef, CSmlDmAdapter::ENotFound);
       
   398                 _DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(): end");
       
   399                 return;
       
   400                 }
       
   401             else if(err!=KErrNone)
       
   402                 {
       
   403                 Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
   404                 _DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(): end");
       
   405                 return;
       
   406                 }
       
   407             CleanupStack::Pop(2); //newNode.iMappingName,newNode.iName
       
   408             }
       
   409         TNSmlEmailCommandElement newCommand;
       
   410         newCommand.iLastUriSeg = LastURISeg(aURI).AllocLC();
       
   411         newCommand.iData = NULL;
       
   412         newCommand.iStatusRef = aStatusRef;
       
   413         newCommand.iLeaf = ETrue;
       
   414         newCommand.iStatus = CSmlDmAdapter::EOk;
       
   415         iBuffer->At(index).iNodeBuf->AppendL(newCommand);
       
   416         CleanupStack::Pop(); //newCommand.iLastUriSeg
       
   417         iBuffer->At(index).iJustFetch=EFalse;
       
   418         DoAddLeafObjectL(newCommand.iLastUriSeg->Des(),aObject,index,
       
   419                          iBuffer->At(index).iNodeBuf->Count()-1);
       
   420         }
       
   421     else
       
   422         {
       
   423         FirstAddLeafL(aURI,aObject,aStatusRef,index);
       
   424         }
       
   425 
       
   426     _DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(): end");
       
   427     }
       
   428 
       
   429 // -----------------------------------------------------------------------------
       
   430 // CNSmlDmEmailAdapter::DeleteObjectL( const TDesC8& aURI,
       
   431 //                                         const TDesC8& aLUID,
       
   432 //                                         const TInt aStatusRef )
       
   433 // -----------------------------------------------------------------------------
       
   434 void CNSmlDmEmailAdapter::DeleteObjectL( const TDesC8& aURI,
       
   435                                          const TDesC8& aLUID,
       
   436                                          const TInt aStatusRef )
       
   437     {
       
   438     _DBG_FILE("CNSmlDmEmailAdapter::DeleteLeafObjectL( ): begin");
       
   439     if(NumOfURISegs(aURI)==2)
       
   440         {
       
   441         if(aLUID.Length()==0)
       
   442             {
       
   443             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::ENotFound);
       
   444             _DBG_FILE("CNSmlDmEmailAdapter::DeleteLeafObjectL( ): ENotFound end");
       
   445             return;
       
   446             }
       
   447 
       
   448         TInt luid = DesToInt(aLUID);
       
   449         TInt err = OpenSessionL();
       
   450         if(err!=KErrNone)
       
   451             {
       
   452             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
   453             _DBG_FILE("CNSmlDmEmailAdapter::DeleteLeafObjectL( ): EError end");
       
   454             return;
       
   455             }
       
   456         CEmailAccounts* emailAccs = CEmailAccounts::NewLC();
       
   457         TNSmlEmailAccountType accType;
       
   458         TInt id;
       
   459         GetIdByLuid(accType, id, luid );
       
   460         if( AccountExistsL( emailAccs, accType, id, -1 ) )
       
   461             {
       
   462             switch ( accType )
       
   463                 {
       
   464             case EImap:
       
   465                 {
       
   466                 RArray<TImapAccount> imap4Accs;
       
   467                 CleanupClosePushL( imap4Accs );
       
   468                 emailAccs->GetImapAccountsL( imap4Accs );
       
   469                 
       
   470                 for (TInt i(0); i < imap4Accs.Count(); i++)
       
   471                     {
       
   472                     if ( imap4Accs[i].iImapAccountId == id  ) 
       
   473                         {
       
   474                         // Delete also related Smtp account
       
   475                         TSmtpAccount relatedSmtp;
       
   476                         emailAccs->GetSmtpAccountL( imap4Accs[i].iSmtpService, relatedSmtp );
       
   477                         emailAccs->DeleteImapAccountL( imap4Accs[i] );
       
   478                         emailAccs->DeleteSmtpAccountL( relatedSmtp );                       
       
   479                         //The only email a/c in phone gettiong deleted , so delete the default sntp a/c                     
       
   480                         if( imap4Accs.Count() == 1 )
       
   481 	                        {
       
   482 	                        TInt deferr = KErrNone;
       
   483                         	TRAP(deferr, emailAccs->DeleteDefaultSmtpAccountL());
       
   484                         	DBG_ARGS(_S16("CNSmlDmEmailAdapter::DeleteLeafObjectL( ): Delete default smtp account Error code %d"),deferr);
       
   485     	                    }
       
   486                         break;   
       
   487                         }
       
   488                     }
       
   489                 
       
   490                 CleanupStack::PopAndDestroy(&imap4Accs); //imap4Accs
       
   491                 
       
   492                 // If the default account was just deleted,
       
   493                 // new one needs to be set.
       
   494                 // 
       
   495 	            TSmtpAccount defaultSmtp;
       
   496 	            TInt retVal = emailAccs->DefaultSmtpAccountL( defaultSmtp );
       
   497 	            if (  retVal != KErrNone )
       
   498 	                {
       
   499                     RArray<TImapAccount> imapAccs;
       
   500                     CleanupClosePushL( imapAccs );
       
   501                     emailAccs->GetImapAccountsL( imapAccs );
       
   502                     if ( imapAccs.Count() > 0 )
       
   503                         {
       
   504                         TSmtpAccount relatedSmtp;
       
   505                         emailAccs->GetSmtpAccountL( imapAccs[0].iSmtpService, relatedSmtp );
       
   506 	                    emailAccs->SetDefaultSmtpAccountL( relatedSmtp );
       
   507                         }
       
   508                     CleanupStack::PopAndDestroy( &imapAccs );
       
   509                     RArray<TPopAccount> popAccounts;
       
   510                     CleanupClosePushL( popAccounts );
       
   511                     emailAccs->GetPopAccountsL( popAccounts );
       
   512                     if ( popAccounts.Count() > 0 )
       
   513                         {
       
   514                         TSmtpAccount relatedSmtp;
       
   515                         emailAccs->GetSmtpAccountL( popAccounts[0].iSmtpService, relatedSmtp );
       
   516 	                    emailAccs->SetDefaultSmtpAccountL( relatedSmtp );
       
   517                         }
       
   518                     CleanupStack::PopAndDestroy( &popAccounts );
       
   519 	                }	                
       
   520                 break;    
       
   521                 }
       
   522                 
       
   523             case EPop:
       
   524                 {
       
   525                 RArray<TPopAccount> popAccs;
       
   526                 CleanupClosePushL( popAccs );
       
   527                 emailAccs->GetPopAccountsL( popAccs );
       
   528                 
       
   529                 for (TInt i(0); i < popAccs.Count(); i++)
       
   530                     {
       
   531                     if ( popAccs[i].iPopAccountId == id  ) 
       
   532                         {
       
   533                         // Delete also related Smtp account
       
   534                         TSmtpAccount relatedSmtp;
       
   535                         emailAccs->GetSmtpAccountL( popAccs[i].iSmtpService, relatedSmtp );
       
   536                         emailAccs->DeletePopAccountL( popAccs[i] );    
       
   537                         emailAccs->DeleteSmtpAccountL( relatedSmtp );
       
   538                         if( popAccs.Count() == 1 )
       
   539                         	{
       
   540                         	TInt deferr = KErrNone;
       
   541                         	TRAP(deferr, emailAccs->DeleteDefaultSmtpAccountL());
       
   542                         	DBG_ARGS(_S16("CNSmlDmEmailAdapter::DeleteLeafObjectL( ): Delete default smtp account Error code %d "),deferr);
       
   543                         	}
       
   544                         break;                                             
       
   545                         }
       
   546                     }
       
   547                 
       
   548                 CleanupStack::PopAndDestroy(); //popAccs
       
   549 
       
   550                 // If the default account was just deleted,
       
   551                 // new one needs to be set.
       
   552                 // 
       
   553 	            TSmtpAccount defSmtp;
       
   554 	            TInt retVal = emailAccs->DefaultSmtpAccountL( defSmtp );
       
   555 	            if (  retVal != KErrNone )
       
   556 	                {
       
   557                     RArray<TPopAccount> popAccounts;
       
   558                     CleanupClosePushL( popAccounts );
       
   559                     emailAccs->GetPopAccountsL( popAccounts );
       
   560                     if ( popAccounts.Count() > 0 )
       
   561                         {
       
   562                         TSmtpAccount relatedSmtp;
       
   563                         emailAccs->GetSmtpAccountL( popAccounts[0].iSmtpService, relatedSmtp );
       
   564 	                    emailAccs->SetDefaultSmtpAccountL( relatedSmtp );
       
   565                         }
       
   566                     CleanupStack::PopAndDestroy( &popAccounts );
       
   567                     RArray<TImapAccount> imapAccs;
       
   568                     CleanupClosePushL( imapAccs );
       
   569                     emailAccs->GetImapAccountsL( imapAccs );
       
   570                     if ( imapAccs.Count() > 0 )
       
   571                         {
       
   572                         TSmtpAccount relatedSmtp;
       
   573                         emailAccs->GetSmtpAccountL( imapAccs[0].iSmtpService, relatedSmtp );
       
   574 	                    emailAccs->SetDefaultSmtpAccountL( relatedSmtp );
       
   575                         }
       
   576                     CleanupStack::PopAndDestroy( &imapAccs );                    
       
   577 	                }	                
       
   578                 break;
       
   579                 }                
       
   580                 
       
   581             default:
       
   582                 Callback().SetStatusL(aStatusRef, CSmlDmAdapter::ENotFound);
       
   583                 break;
       
   584                 }
       
   585 
       
   586             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EOk);
       
   587             }
       
   588         else
       
   589             {
       
   590             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::ENotFound);
       
   591             }
       
   592         CleanupStack::PopAndDestroy(); //emailAccs
       
   593         }
       
   594     else
       
   595         {
       
   596         Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
   597         }
       
   598 
       
   599     _DBG_FILE("CNSmlDmEmailAdapter::DeleteLeafObjectL( ): end");
       
   600     }
       
   601 
       
   602 // -----------------------------------------------------------------------------
       
   603 //  CNSmlDmEmailAdapter::FetchLeafObjectL( const TDesC8& aURI,
       
   604 //                                            const TDesC8& aLUID,
       
   605 //                                            const TDesC8& aType,
       
   606 //                                            const TInt aResultsRef,
       
   607 //                                            const TInt aStatusRef )
       
   608 //  
       
   609 // -----------------------------------------------------------------------------
       
   610 void CNSmlDmEmailAdapter::FetchLeafObjectL( const TDesC8& aURI,
       
   611                                             const TDesC8& aLUID,
       
   612                                             const TDesC8& aType,
       
   613                                             const TInt aResultsRef,
       
   614                                             const TInt aStatusRef )
       
   615     {
       
   616     _DBG_FILE("CNSmlDmEmailAdapter::FetchLeafObjectL(): begin");
       
   617 		
       
   618 	TInt luid = DesToInt(aLUID);	
       
   619 			
       
   620     if(aLUID.Length()==0)
       
   621         {
       
   622         	luid = ConstructTreeL(aURI);
       
   623         	if(luid == -1)        	
       
   624         	{
       
   625         		Callback().SetStatusL(aStatusRef, CSmlDmAdapter::ENotFound);
       
   626         		_DBG_FILE("CNSmlDmEmailAdapter::FetchLeafObjectL(): ENotFound end");
       
   627         		return;
       
   628         	}
       
   629         }
       
   630     if( OpenSessionL() != KErrNone )
       
   631         {
       
   632         Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
   633         _DBG_FILE("CNSmlDmEmailAdapter::FetchLeafObjectL(): EError end");
       
   634         return;
       
   635         }
       
   636 
       
   637     TInt index  = -1;
       
   638     for(TInt i=0;i<iBuffer->Count();i++)
       
   639         {
       
   640         if(iBuffer->At(i).iLuid == luid)
       
   641             {
       
   642             index = i;
       
   643             break;
       
   644             }
       
   645         }
       
   646 
       
   647     if(index<0)
       
   648         {
       
   649         TNSmlEmailSettingsElement newNode;
       
   650         newNode.iImap4Settings = NULL;
       
   651         newNode.iPop3Settings = NULL;
       
   652         newNode.iSmtpSettings = NULL;
       
   653         newNode.iIapPref = NULL;
       
   654         newNode.iSmtpIapPref = NULL;
       
   655         newNode.iLuid = luid;
       
   656         newNode.iMappingName = aURI.AllocLC();
       
   657         newNode.iName = HBufC::NewLC(0);
       
   658         newNode.iNodeBuf = new (ELeave)
       
   659             CArrayFixFlat <TNSmlEmailCommandElement> (KNSmlDmEmailGranularity);
       
   660         newNode.iExecuted = ETrue;
       
   661         newNode.iJustFetch = ETrue;
       
   662         GetIdByLuid(newNode.iAccType, newNode.iAccId, luid);
       
   663         iBuffer->AppendL(newNode);
       
   664         index = iBuffer->Count() - 1;
       
   665         if( DoRestoreSettingsL(index) != KErrNone )
       
   666             {
       
   667             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
   668             CleanupStack::Pop(2); //newNode.iMappingName,newNode.iName
       
   669             _DBG_FILE("CNSmlDmEmailAdapter::FetchLeafObjectL(): EError end");
       
   670             return;
       
   671             }
       
   672         CleanupStack::Pop(2); //newNode.iMappingName,newNode.iName
       
   673         }
       
   674 
       
   675     TPtrC8 lastUriSeg = LastURISeg(aURI);
       
   676     CBufBase* object = CBufFlat::NewL(32);
       
   677     CleanupStack::PushL(object);
       
   678     CSmlDmAdapter::TError status = CSmlDmAdapter::EOk;
       
   679 
       
   680     if(lastUriSeg.Compare(KNSmlDMEmailName)==0)
       
   681         {
       
   682         HBufC8 *data = HBufC8::NewLC(iBuffer->At(index).iName->Length());
       
   683         TPtr8 dataPtr = data->Des();
       
   684         CnvUtfConverter::ConvertFromUnicodeToUtf8( 
       
   685             dataPtr,iBuffer->At(index).iName->Des() );
       
   686         object->InsertL(0,dataPtr);
       
   687         CleanupStack::PopAndDestroy(); //data
       
   688         }
       
   689 
       
   690     else if(lastUriSeg.Compare(KNSmlDMEmailUID)==0)
       
   691         {
       
   692         HBufC *data = NULL;
       
   693         if(iBuffer->At(index).iPop3Settings!= NULL)
       
   694             {
       
   695             data = HBufC::NewLC(iBuffer->At(index).iPop3Settings->LoginName().Length());
       
   696             data->Des().Copy(iBuffer->At(index).iPop3Settings->LoginName());
       
   697             }
       
   698         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
   699             {
       
   700             data = HBufC::NewLC(iBuffer->At(index).iImap4Settings->LoginName().Length());
       
   701             data->Des().Copy(iBuffer->At(index).iImap4Settings->LoginName());
       
   702             }
       
   703         if(data)
       
   704             {
       
   705             HBufC8 *data8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*data);
       
   706             CleanupStack::PushL(data8);
       
   707             object->InsertL(0,*data8);
       
   708             CleanupStack::PopAndDestroy(2);//data, data8
       
   709             }
       
   710         }
       
   711 
       
   712     else if(lastUriSeg.Compare(KNSmlDMEmailPW)==0)
       
   713         {
       
   714         HBufC *data = NULL;
       
   715         if(iBuffer->At(index).iPop3Settings!= NULL)
       
   716             {
       
   717             data = HBufC::NewLC(iBuffer->At(index).iPop3Settings->Password().Length());
       
   718             data->Des().Copy(iBuffer->At(index).iPop3Settings->Password());
       
   719             }
       
   720         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
   721             {
       
   722             data = HBufC::NewLC(iBuffer->At(index).iImap4Settings->Password().Length());
       
   723             data->Des().Copy(iBuffer->At(index).iImap4Settings->Password());
       
   724             }
       
   725         if(data)
       
   726             {
       
   727             HBufC8 *data8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*data);
       
   728             CleanupStack::PushL(data8);
       
   729             object->InsertL(0,*data8);
       
   730             CleanupStack::PopAndDestroy(2);//data, data8
       
   731             }
       
   732         }
       
   733 
       
   734     else if(lastUriSeg.Compare(KNSmlDMEmailUAddr)==0)
       
   735         {
       
   736         HBufC *data = NULL;
       
   737         data = iBuffer->At(index).iSmtpSettings->EmailAddress().AllocLC();
       
   738         HBufC8 *data8 = HBufC8::NewLC(data->Size());
       
   739         TPtr8 dataPtr8 = data8->Des();
       
   740         CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr8, data->Des() );
       
   741 
       
   742         object->InsertL(0,dataPtr8);
       
   743         CleanupStack::PopAndDestroy(2); //data, data8
       
   744         }
       
   745 
       
   746     else if(lastUriSeg.Compare(KNSmlDMEmailUName)==0)
       
   747         {
       
   748 
       
   749         HBufC *data = NULL;
       
   750         data = iBuffer->At(index).iSmtpSettings->EmailAlias().AllocLC();
       
   751         HBufC8 *data8 = HBufC8::NewLC(data->Size());
       
   752         TPtr8 dataPtr8 = data8->Des();
       
   753         CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr8, data->Des() );
       
   754 
       
   755         object->InsertL(0,dataPtr8);
       
   756         CleanupStack::PopAndDestroy(2); //data, data8
       
   757         }
       
   758 
       
   759     else if(lastUriSeg.Compare( KNSmlDMEmailMrcv)==0)
       
   760         {
       
   761         HBufC *data = NULL;
       
   762         if(iBuffer->At(index).iPop3Settings!= NULL)
       
   763             {
       
   764             data = iBuffer->At(index).iPop3Settings->ServerAddress().AllocLC();
       
   765             }
       
   766         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
   767             {
       
   768             data = iBuffer->At(index).iImap4Settings->ServerAddress().AllocLC();
       
   769             }
       
   770         else
       
   771         	{
       
   772           	status = CSmlDmAdapter::ENotFound;
       
   773         	}
       
   774         if(data)
       
   775         	{	    
       
   776         		HBufC8 *data8 = HBufC8::NewLC(data->Size());
       
   777         		TPtr8 dataPtr8 = data8->Des();
       
   778         		CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr8, data->Des() );
       
   779 
       
   780         		object->InsertL(0,dataPtr8);
       
   781         		CleanupStack::PopAndDestroy(2); //data, data8
       
   782         	}
       
   783         }
       
   784 
       
   785     else if(lastUriSeg.Compare(KNSmlDMEmailMsnd)==0)
       
   786         {
       
   787         HBufC *data = NULL;
       
   788         data = iBuffer->At(index).iSmtpSettings->ServerAddress().AllocLC();
       
   789         HBufC8 *data8 = HBufC8::NewLC(data->Size());
       
   790         TPtr8 dataPtr8 = data8->Des();
       
   791         CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr8, data->Des() );
       
   792 
       
   793         object->InsertL(0,dataPtr8);
       
   794         CleanupStack::PopAndDestroy(2); //data, data8
       
   795         }
       
   796 
       
   797     else if(lastUriSeg.Compare(KNSmlDMEmailMpro)==0)
       
   798         {
       
   799         if(iBuffer->At(index).iPop3Settings!= NULL)
       
   800             {
       
   801             object->InsertL(0,KNSmlDMEmailMproPOP);
       
   802             }
       
   803         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
   804             {
       
   805             object->InsertL(0,KNSmlDMEmailMproIMAP);
       
   806             }
       
   807         }
       
   808 
       
   809     else if(lastUriSeg.Compare(KNSmlDMEmailUseSecCon)==0)
       
   810         {
       
   811         FetchSecurityValueL(*object, &iBuffer->At(index), EUseSecCon );
       
   812         }
       
   813 
       
   814     else if(lastUriSeg.Compare(KNSmlDMEmailUseSauth)==0)
       
   815         {
       
   816         if(iBuffer->At(index).iSmtpSettings->SMTPAuth())
       
   817             {
       
   818             object->InsertL(0,KNSmlDMEmailTrue);
       
   819             }
       
   820         else
       
   821             {
       
   822             object->InsertL(0,KNSmlDMEmailFalse);
       
   823             }
       
   824         }
       
   825 
       
   826     else if(lastUriSeg.Compare(KNSmlDMEmailSauthUID)==0)
       
   827         {
       
   828         HBufC *data = HBufC::NewLC(iBuffer->At(index).iSmtpSettings->LoginName().Length());
       
   829         data->Des().Copy(iBuffer->At(index).iSmtpSettings->LoginName());
       
   830         HBufC8 *data8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*data);
       
   831         CleanupStack::PushL(data8);
       
   832         object->InsertL(0,*data8);
       
   833         CleanupStack::PopAndDestroy(2);//data, data8
       
   834         }
       
   835 
       
   836     else if(lastUriSeg.Compare(KNSmlDMEmailSauthPW)==0)
       
   837         {
       
   838         HBufC *data = HBufC::NewLC(iBuffer->At(index).iSmtpSettings->Password().Length());
       
   839         data->Des().Copy(iBuffer->At(index).iSmtpSettings->Password());
       
   840         HBufC8 *data8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*data);
       
   841         CleanupStack::PushL(data8);
       
   842         object->InsertL(0,*data8);
       
   843         CleanupStack::PopAndDestroy(2);//data, data8
       
   844         }
       
   845 
       
   846     else if(lastUriSeg.Compare(KNSmlDMEmailPtxtSAuth)==0)
       
   847         {
       
   848         if(iBuffer->At(index).iSmtpSettings->InboxLoginDetails())
       
   849             {
       
   850             object->InsertL(0,KNSmlDMEmailTrue);
       
   851             }
       
   852         else
       
   853             {
       
   854             object->InsertL(0,KNSmlDMEmailFalse);
       
   855             }
       
   856         }
       
   857 
       
   858     else if (lastUriSeg.Compare(KNSmlDMEmailSUseSecCon)==0)
       
   859         {
       
   860         FetchSecurityValueL(*object, &iBuffer->At(index), ESuseSecCon );
       
   861         }
       
   862 
       
   863     else if (lastUriSeg.Compare(KNSmlDMEmailUseStartTLS)==0)
       
   864         {
       
   865         FetchSecurityValueL(*object, &iBuffer->At(index), EUseStartTls );
       
   866         }
       
   867 
       
   868     else if (lastUriSeg.Compare(KNSmlDMEmailSUseStartTLS)==0)
       
   869         {
       
   870         FetchSecurityValueL(*object, &iBuffer->At(index), ESuseStartTls );
       
   871         }
       
   872 
       
   873     else if(lastUriSeg.Compare(KNSmlDMEmailMrcvPort)==0)
       
   874         {
       
   875         TUint port;
       
   876         if(iBuffer->At(index).iPop3Settings!= NULL)
       
   877             {
       
   878             port = iBuffer->At(index).iPop3Settings->Port();
       
   879             }
       
   880         else 
       
   881             {
       
   882             port = iBuffer->At(index).iImap4Settings->Port();
       
   883             }
       
   884         HBufC8 *data = IntToDes8LC(port);
       
   885         object->InsertL(0,data->Des());
       
   886         CleanupStack::PopAndDestroy(); //data
       
   887         }
       
   888 
       
   889     else if(lastUriSeg.Compare(KNSmlDMEmailMsndPort)==0)
       
   890         {
       
   891         TUint port = iBuffer->At(index).iSmtpSettings->Port();
       
   892         HBufC8 *data = IntToDes8LC(port);
       
   893         object->InsertL(0,data->Des());
       
   894         CleanupStack::PopAndDestroy(); //data
       
   895         }
       
   896 
       
   897     else if(lastUriSeg.Compare(KNSmlDMEmailUseAPOP)==0)
       
   898         {
       
   899         if(iBuffer->At(index).iPop3Settings!= NULL)
       
   900             {
       
   901             if(iBuffer->At(index).iPop3Settings->Apop())
       
   902                 {
       
   903                 object->InsertL(0,KNSmlDMEmailTrue);
       
   904                 }
       
   905             else
       
   906                 {
       
   907                 object->InsertL(0,KNSmlDMEmailFalse);
       
   908                 }
       
   909             }
       
   910         else
       
   911             {
       
   912             status = CSmlDmAdapter::ENotFound;
       
   913             }
       
   914         }
       
   915 
       
   916     else if (lastUriSeg.Compare(KNSmlDMEmailFolderPath)==0)
       
   917         {
       
   918         if(iBuffer->At(index).iImap4Settings)
       
   919             {
       
   920             object->InsertL(0, iBuffer->At(index).iImap4Settings->FolderPath() );
       
   921             }
       
   922         else
       
   923             {
       
   924             status = CSmlDmAdapter::ENotFound;
       
   925             }
       
   926         }
       
   927 
       
   928     else if(lastUriSeg.Compare(KNSmlDMEmailToNapID)==0)
       
   929         {
       
   930 
       
   931         CNSmlDMIAPMatcher* iapmatch = CNSmlDMIAPMatcher::NewL( &Callback() );
       
   932         CleanupStack::PushL(iapmatch);
       
   933         HBufC8* uri8 = NULL;
       
   934         TInt iapCount = iBuffer->At(index).iIapPref->NumberOfIAPs();
       
   935         if ( iapCount )
       
   936             {
       
   937             TImIAPChoice localIAP = iBuffer->At(index).iIapPref->IAPPreference(0);
       
   938             uri8 = iapmatch->URIFromIAPIdL( localIAP.iIAP );
       
   939             }
       
   940 
       
   941         if( uri8 )
       
   942             {
       
   943             CleanupStack::PushL(uri8);
       
   944             object->InsertL(0,uri8->Des());
       
   945             status = CSmlDmAdapter::EOk;
       
   946             CleanupStack::PopAndDestroy(); // uri8
       
   947             }
       
   948         else
       
   949             {
       
   950             status = CSmlDmAdapter::ENotFound;
       
   951             }
       
   952 
       
   953         CleanupStack::PopAndDestroy(); // iapMatch
       
   954         }
       
   955     else if(lastUriSeg.Compare(KNSmlDMEmailSToNapID)==0)
       
   956         {
       
   957 
       
   958         // SToNapID exists only for SMTP
       
   959         if (iBuffer->At(index).iSmtpSettings!= NULL)
       
   960             {
       
   961             CNSmlDMIAPMatcher* iapmatch = CNSmlDMIAPMatcher::NewL( &Callback() );
       
   962             CleanupStack::PushL(iapmatch);
       
   963             HBufC8* uri8 = NULL;
       
   964             TInt iapCount = iBuffer->At(index).iIapPref->NumberOfIAPs();
       
   965             if ( iapCount )
       
   966                 {
       
   967                 TImIAPChoice localIAP = iBuffer->At(index).iSmtpIapPref->IAPPreference(0);
       
   968                 uri8 = iapmatch->URIFromIAPIdL( localIAP.iIAP );
       
   969                 }
       
   970 
       
   971             if( uri8 )
       
   972                 {
       
   973                 CleanupStack::PushL(uri8);
       
   974                 object->InsertL(0,uri8->Des());
       
   975                 status = CSmlDmAdapter::EOk;
       
   976                 CleanupStack::PopAndDestroy(); // uri8
       
   977                 }
       
   978             else
       
   979                 {
       
   980                 status = CSmlDmAdapter::ENotFound;
       
   981                 }
       
   982 
       
   983             CleanupStack::PopAndDestroy(); // iapMatch
       
   984             }
       
   985         else
       
   986             {
       
   987             status = CSmlDmAdapter::ENotFound;
       
   988             }
       
   989         }
       
   990 
       
   991     if(status == CSmlDmAdapter::EOk)
       
   992         {
       
   993         Callback().SetResultsL(aResultsRef,*object,aType);
       
   994         }
       
   995     Callback().SetStatusL(aStatusRef, status);
       
   996     CleanupStack::PopAndDestroy(object); //object
       
   997 
       
   998     _DBG_FILE("CNSmlDmEmailAdapter::FetchLeafObjectL(): end");
       
   999     }
       
  1000 
       
  1001 // -----------------------------------------------------------------------------
       
  1002 //  CNSmlDmEmailAdapter::ChildURIListL( const TDesC8& aURI,
       
  1003 //                                          const TDesC8& aParentLUID,
       
  1004 //                                          const CArrayFix<TSmlDmMappingInfo>& 
       
  1005 //                                          aPreviousURISegmentList,
       
  1006 //                                          const TInt aResultsRef,
       
  1007 //                                          const TInt aStatusRef  )
       
  1008 // -----------------------------------------------------------------------------
       
  1009 void CNSmlDmEmailAdapter::ChildURIListL( const TDesC8& aURI,
       
  1010                                          const TDesC8& aParentLUID,
       
  1011                                          const CArrayFix<TSmlDmMappingInfo>& 
       
  1012                                          aPreviousURISegmentList,
       
  1013                                          const TInt aResultsRef,
       
  1014                                          const TInt aStatusRef  )
       
  1015     {
       
  1016     _DBG_FILE("CNSmlDmEmailAdapter::ChildURIListL(): begin");
       
  1017 
       
  1018     CSmlDmAdapter::TError ret = CSmlDmAdapter::EError;
       
  1019 
       
  1020     if( OpenSessionL() != KErrNone )
       
  1021         {
       
  1022         Callback().SetStatusL(aStatusRef, ret);
       
  1023         _DBG_FILE("CNSmlDmEmailAdapter::ChildURIListL(): end");
       
  1024         return;
       
  1025         }
       
  1026     CBufBase *currentList = CBufFlat::NewL(128);
       
  1027     CleanupStack::PushL(currentList);
       
  1028 
       
  1029     CEmailAccounts* emailAccs = CEmailAccounts::NewLC();
       
  1030 
       
  1031     if(aURI.Compare(KNSmlDMEmailNodeName)==0)
       
  1032         {
       
  1033         RArray<TImapAccount> imap4Accounts;
       
  1034         CleanupClosePushL( imap4Accounts );
       
  1035         emailAccs->GetImapAccountsL(imap4Accounts);
       
  1036 
       
  1037         // First, IMAP accounts
       
  1038         //
       
  1039         TInt luid;
       
  1040         TPtrC8 name;
       
  1041         // cycle through every found account
       
  1042         for(TInt count = 0; count < imap4Accounts.Count(); count++)
       
  1043             {
       
  1044             SetLuidById( EImap, imap4Accounts[count].iImapAccountId, luid );
       
  1045 
       
  1046             TBool found = EFalse;
       
  1047             // cycle through every known account
       
  1048             for(TInt i = 0; i<aPreviousURISegmentList.Count();i++)
       
  1049                 {
       
  1050                 // compare if found is known
       
  1051                 if( luid == DesToInt(aPreviousURISegmentList.At(i).iURISegLUID))
       
  1052                     {
       
  1053                     found = ETrue;
       
  1054                     name.Set(aPreviousURISegmentList.At(i).iURISeg);
       
  1055                     break;
       
  1056                     }
       
  1057                 }
       
  1058             // if found != known add mapping to it
       
  1059             if(!found)
       
  1060                 {
       
  1061                 HBufC8 *uri = HBufC8::NewLC(aURI.Length() + 
       
  1062                                             KNSmlDMEmailMproIMAP().Length() + 6);
       
  1063                                             
       
  1064                 TPtr8 uriPtr = uri->Des();
       
  1065                 uriPtr.Format(aURI);
       
  1066                 uriPtr.Append(KNSmlDMEmailSeparator8);
       
  1067                 uriPtr.Append(KNSmlDMEmailMproIMAP);
       
  1068                 TUint16 crc;
       
  1069                	Mem::Crc ( crc, &imap4Accounts[count],sizeof(TImapAccount) );
       
  1070                 HBufC8 *crcBuf = IntToDes8LC(crc);
       
  1071                 uriPtr.Append(*crcBuf);
       
  1072                 HBufC8 *luidBuf = IntToDes8LC(luid);
       
  1073                 Callback().SetMappingL(uriPtr,luidBuf->Des());
       
  1074                 currentList->InsertL( currentList->Size(),LastURISeg(uriPtr));
       
  1075                 CleanupStack::PopAndDestroy(3); //uri,luidBuf, crcBuf
       
  1076                 }
       
  1077             else
       
  1078                 {
       
  1079                 currentList->InsertL( currentList->Size(),name );
       
  1080                 }
       
  1081             // in all cases: add it to results.
       
  1082             if(count < imap4Accounts.Count()-1 )
       
  1083                 {
       
  1084                 currentList->InsertL(currentList->Size(),KNSmlDMEmailSeparator8);
       
  1085                 }
       
  1086             }
       
  1087         CleanupStack::PopAndDestroy(); //imap4Accounts
       
  1088 
       
  1089         // Then, POP accounts
       
  1090         //
       
  1091         RArray<TPopAccount> pop3Accounts;
       
  1092         CleanupClosePushL( pop3Accounts );
       
  1093         emailAccs->GetPopAccountsL( pop3Accounts );
       
  1094         if (pop3Accounts.Count()&&currentList->Size()>0)
       
  1095             {
       
  1096             // there is at least one account; insert separator
       
  1097             currentList->InsertL(currentList->Size(),KNSmlDMEmailSeparator8);
       
  1098             }
       
  1099         // cycle through every found account
       
  1100         for(TInt count = 0; count < pop3Accounts.Count(); count++)
       
  1101             {
       
  1102             SetLuidById( EPop, pop3Accounts[count].iPopAccountId, luid );
       
  1103 
       
  1104             TBool found = EFalse;
       
  1105             // cycle through every known account
       
  1106             for(TInt i = 0; i<aPreviousURISegmentList.Count();i++)
       
  1107                 {
       
  1108                 // compare if found is known
       
  1109                 if( luid == DesToInt(aPreviousURISegmentList.At(i).iURISegLUID))
       
  1110 
       
  1111                     {
       
  1112                     found = ETrue;
       
  1113                     name.Set(aPreviousURISegmentList.At(i).iURISeg);
       
  1114                     break;
       
  1115                     }
       
  1116                 }
       
  1117             // if found != known add mapping to it
       
  1118             if(!found)
       
  1119                 {
       
  1120                                          
       
  1121                 HBufC8 *uri = HBufC8::NewLC(aURI.Length() + 
       
  1122                                             KNSmlDMEmailMproPOP().Length() + 5);
       
  1123                 TPtr8 uriPtr = uri->Des();
       
  1124                 uriPtr.Format(aURI);
       
  1125                 uriPtr.Append(KNSmlDMEmailSeparator8);
       
  1126                 uriPtr.Append(KNSmlDMEmailMproPOP);
       
  1127                 TUint16 crc;
       
  1128                	Mem::Crc ( crc, &pop3Accounts[count],sizeof(TPopAccount) );
       
  1129                 HBufC8 *crcBuf = IntToDes8LC(crc);
       
  1130                 uriPtr.Append(*crcBuf);
       
  1131                 HBufC8 *luidBuf = IntToDes8LC(luid);
       
  1132                 Callback().SetMappingL(uriPtr,luidBuf->Des());
       
  1133                 currentList->InsertL( currentList->Size(),LastURISeg(uriPtr));
       
  1134                 CleanupStack::PopAndDestroy(3); //uri,luidBuf,crcBuf
       
  1135                 }
       
  1136             else
       
  1137                 {
       
  1138                 currentList->InsertL( currentList->Size(),name );
       
  1139                 }
       
  1140             // in all cases: add it to results.
       
  1141             if(count < pop3Accounts.Count()-1)
       
  1142                 {
       
  1143                 currentList->InsertL(currentList->Size(),KNSmlDMEmailSeparator8);
       
  1144                 }
       
  1145             }
       
  1146         CleanupStack::PopAndDestroy(); //pop3Accounts
       
  1147         ret = CSmlDmAdapter::EOk;
       
  1148         }
       
  1149     else
       
  1150         {
       
  1151         if(aParentLUID.Length() > 0)
       
  1152             {
       
  1153             TInt err = KErrNotFound;
       
  1154             TInt luid = DesToInt(aParentLUID);
       
  1155             TNSmlEmailAccountType accType;
       
  1156             TInt id;
       
  1157             GetIdByLuid( accType, id, luid );
       
  1158             switch ( accType )
       
  1159                 {
       
  1160             case EImap:
       
  1161                 {
       
  1162                 RArray<TImapAccount> imap4Accounts;
       
  1163                 CleanupClosePushL( imap4Accounts );
       
  1164                 emailAccs->GetImapAccountsL( imap4Accounts ); 
       
  1165                 for ( TInt i=0; i<imap4Accounts.Count(); i++ )
       
  1166                     {
       
  1167                     if ( imap4Accounts[i].iImapAccountId == id )
       
  1168                         {
       
  1169                         err = KErrNone;
       
  1170                         break;
       
  1171                         }
       
  1172                     }
       
  1173                 CleanupStack::PopAndDestroy(); //imap4Accounts    
       
  1174                 break;                
       
  1175                 }
       
  1176             case EPop:
       
  1177                 {
       
  1178                 RArray<TPopAccount> pop3Accounts;
       
  1179                 CleanupClosePushL( pop3Accounts );
       
  1180                 emailAccs->GetPopAccountsL(pop3Accounts); 
       
  1181                 for ( TInt i=0; i<pop3Accounts.Count(); i++ )
       
  1182                     {
       
  1183                     if ( pop3Accounts[i].iPopAccountId == id )
       
  1184                         {
       
  1185                         err = KErrNone;
       
  1186                         break;
       
  1187                         }
       
  1188                     }
       
  1189                 CleanupStack::PopAndDestroy(); //pop3Accounts       
       
  1190                 break;                
       
  1191                 }
       
  1192             default:
       
  1193                 ret = CSmlDmAdapter::EError;
       
  1194                 break;
       
  1195                 }
       
  1196             if( err == KErrNotFound )
       
  1197                 {
       
  1198                 ret = CSmlDmAdapter::ENotFound;
       
  1199                 }
       
  1200             
       
  1201             if ( err == KErrNone )
       
  1202                 {
       
  1203                 ret = CSmlDmAdapter::EOk;
       
  1204                 currentList->InsertL(0,KNSmlDMEmailListOfLeafs);
       
  1205                 }
       
  1206             }
       
  1207         else
       
  1208             {
       
  1209             ret = CSmlDmAdapter::ENotFound;
       
  1210             }
       
  1211         }
       
  1212     // Common part
       
  1213     Callback().SetStatusL(aStatusRef, ret);
       
  1214     if( ret == CSmlDmAdapter::EOk )
       
  1215         {
       
  1216         Callback().SetResultsL(aResultsRef,*currentList,KNullDesC8);
       
  1217         }
       
  1218 
       
  1219     CleanupStack::PopAndDestroy(emailAccs); //emailAccs
       
  1220     CleanupStack::PopAndDestroy(currentList); //currentList
       
  1221     _DBG_FILE("CNSmlDmEmailAdapter::ChildURIListL(): end");
       
  1222     }
       
  1223 
       
  1224 // -----------------------------------------------------------------------------
       
  1225 // CNSmlDmEmailAdapter::AddNodeObjectL( const TDesC8& aURI, 
       
  1226 // const TDesC8& aParentLUID, const TInt aStatusRef )
       
  1227 // -----------------------------------------------------------------------------
       
  1228 void CNSmlDmEmailAdapter::AddNodeObjectL( const TDesC8& aURI,
       
  1229                                           const TDesC8& aParentLUID,
       
  1230                                           const TInt aStatusRef )
       
  1231     {
       
  1232     _DBG_FILE("CNSmlDmEmailAdapter::AddNodeObjectL(): begin");
       
  1233 
       
  1234     if(aParentLUID.Length()>0)
       
  1235         {
       
  1236         TInt luid = DesToInt(aParentLUID);
       
  1237         TInt accId;
       
  1238         TNSmlEmailAccountType accType;
       
  1239         GetIdByLuid( accType, accId, luid );
       
  1240         TInt err = OpenSessionL();
       
  1241         if(err!=KErrNone)
       
  1242             {
       
  1243             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
  1244             _DBG_FILE("CNSmlDmEmailAdapter::AddNodeObjectL(): EError end");
       
  1245             return;
       
  1246             }
       
  1247         CEmailAccounts* emailAccs = CEmailAccounts::NewLC();
       
  1248         TBool accEx = AccountExistsL( emailAccs, accType, accId, -1 );
       
  1249         CleanupStack::PopAndDestroy( emailAccs );
       
  1250         if( accEx )
       
  1251             {
       
  1252             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EAlreadyExists);
       
  1253             _DBG_FILE("CNSmlDmEmailAdapter::AddNodeObjectL(): EAlreadyExists end");
       
  1254             return;
       
  1255             }
       
  1256         else
       
  1257             {
       
  1258             TInt ret = Callback().RemoveMappingL( KNSmlDMEmailAdapterImplUid, 
       
  1259 						GetDynamicEmailNodeUri(aURI), ETrue );
       
  1260 			if(ret != KErrNone)
       
  1261 				{
       
  1262             	Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
  1263 	            _DBG_FILE("CNSmlDmEmailAdapter::AddNodeObjectL(): EError end");
       
  1264             	return;
       
  1265 				}
       
  1266             }
       
  1267         Callback().SetMappingL(aURI,KNullDesC8);
       
  1268         }
       
  1269 
       
  1270     if( aURI.Find(KNSmlDMEmailNodeName)>=0 && NumOfURISegs(aURI)==2 )
       
  1271         {
       
  1272         if( OpenSessionL() != KErrNone )
       
  1273             {
       
  1274             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
  1275             _DBG_FILE("CNSmlDmEmailAdapter::AddNodeObjectL(): EError end");
       
  1276             return;
       
  1277             }
       
  1278 
       
  1279         RFs fs;
       
  1280         User::LeaveIfError( fs.Connect() );
       
  1281         CleanupClosePushL(fs);
       
  1282         //2files and 2 folders are needed for settings
       
  1283         if (SysUtil::FFSSpaceBelowCriticalLevelL(&fs,
       
  1284                                                  KNSmlDmFatMinimumFileSize*4))
       
  1285             {
       
  1286             CleanupStack::PopAndDestroy(); //fs
       
  1287             Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EDiskFull);
       
  1288             _DBG_FILE("CNSmlDmEmailAdapter::AddNodeObjectL(): EDiskFull end");
       
  1289             return;
       
  1290             // do not write
       
  1291             }
       
  1292         CleanupStack::PopAndDestroy(); //fs
       
  1293 
       
  1294 
       
  1295         TNSmlEmailSettingsElement newNode;
       
  1296         newNode.iMappingName = aURI.AllocLC();
       
  1297         newNode.iName = HBufC::NewLC(0);
       
  1298         newNode.iNodeBuf = new (ELeave)
       
  1299             CArrayFixFlat <TNSmlEmailCommandElement> (KNSmlDmEmailGranularity);
       
  1300         newNode.iExecuted = EFalse;
       
  1301         newNode.iImap4Settings = NULL;
       
  1302         newNode.iPop3Settings = NULL;
       
  1303         newNode.iSmtpSettings = NULL;
       
  1304         newNode.iIapPref = NULL;
       
  1305         newNode.iSmtpIapPref = NULL;
       
  1306         newNode.iLuid = 0;
       
  1307         newNode.iJustFetch=EFalse;
       
  1308         iBuffer->AppendL(newNode);
       
  1309         CleanupStack::Pop(2); //newNode.iMappingName,newNode.iName
       
  1310         TNSmlEmailCommandElement newCommand;
       
  1311         newCommand.iLastUriSeg = LastURISeg(aURI).AllocLC(); //aURI.AllocLC();
       
  1312         newCommand.iData = NULL;
       
  1313         newCommand.iStatusRef = aStatusRef;
       
  1314         newCommand.iLeaf = EFalse;
       
  1315         newCommand.iStatus = CSmlDmAdapter::EOk;
       
  1316         TInt index = iBuffer->Count() - 1;
       
  1317         iBuffer->At(index).iNodeBuf->AppendL(newCommand);
       
  1318         CleanupStack::Pop(); //newCommand.iLastUriSeg
       
  1319 
       
  1320 
       
  1321         }
       
  1322     _DBG_FILE("CNSmlDmEmailAdapter::AddNodeObjectL(): end");
       
  1323     }
       
  1324 // -----------------------------------------------------------------------------
       
  1325 // CNSmlDmEmailAdapter::UpdateLeafObjectL( const TDesC8& aURI,
       
  1326 // const TDesC8& aLUID, RWriteStream*& aStream, const TDesC8& aType,
       
  1327 // const TInt aStatusRef )
       
  1328 // This should update a stream object, but in case of Email settings streaming 
       
  1329 // is not supported.
       
  1330 // -----------------------------------------------------------------------------
       
  1331 void CNSmlDmEmailAdapter::UpdateLeafObjectL( const TDesC8& /*aURI*/,
       
  1332                                              const TDesC8& /*aLUID*/,
       
  1333                                              RWriteStream*& /*aStream*/,
       
  1334                                              const TDesC8& /*aType*/,
       
  1335                                              const TInt aStatusRef )
       
  1336     {
       
  1337     _DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(stream): begin");
       
  1338     Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
  1339     _DBG_FILE("CNSmlDmEmailAdapter::UpdateLeafObjectL(stream): end");
       
  1340     }
       
  1341 // -----------------------------------------------------------------------------
       
  1342 // void CNSmlDmEmailAdapter::FetchLeafObjectSizeL( const TDesC8& aURI,
       
  1343 // const TDesC8& aLUID, const TDesC8& aType, const TInt aResultsRef,
       
  1344 // const TInt aStatusRef )
       
  1345 // Fetches data object and returns its size.
       
  1346 // -----------------------------------------------------------------------------
       
  1347 void CNSmlDmEmailAdapter::FetchLeafObjectSizeL( const TDesC8& aURI,
       
  1348                                                 const TDesC8& aLUID,
       
  1349                                                 const TDesC8& aType,
       
  1350                                                 const TInt aResultsRef,
       
  1351                                                 const TInt aStatusRef )
       
  1352     {
       
  1353     _DBG_FILE("CNSmlDmEmailAdapter::FetchLeafObjectSizeL(): begin");
       
  1354 
       
  1355     CBufBase *result = CBufFlat::NewL(32);
       
  1356     CleanupStack::PushL(result);
       
  1357     CSmlDmAdapter::TError status = FetchObjectL(aURI, aLUID, *result);
       
  1358     if( status == CSmlDmAdapter::EOk )
       
  1359         {
       
  1360         TInt objSizeInBytes = result->Size();
       
  1361         TBuf8<4> stringObjSizeInBytes;
       
  1362         stringObjSizeInBytes.Num( objSizeInBytes );
       
  1363         result->Reset();
       
  1364         result->InsertL( 0, stringObjSizeInBytes );
       
  1365         Callback().SetResultsL(aResultsRef, *result, aType);
       
  1366         }
       
  1367     Callback().SetStatusL(aStatusRef, status);
       
  1368     CleanupStack::PopAndDestroy(); //result
       
  1369     _DBG_FILE("CNSmlDmEmailAdapter::FetchLeafObjectSizeL(): end");
       
  1370     }
       
  1371 // -----------------------------------------------------------------------------
       
  1372 // CNSmlDmEmailAdapter::ExecuteCommandL()
       
  1373 // -----------------------------------------------------------------------------
       
  1374 void CNSmlDmEmailAdapter::ExecuteCommandL( const TDesC8& /*aURI*/,
       
  1375                                            const TDesC8& /*aLUID*/,
       
  1376                                            const TDesC8& /*aArgument*/,
       
  1377                                            const TDesC8& /*aType*/,
       
  1378                                            const TInt aStatusRef )
       
  1379     {
       
  1380     //Not supported
       
  1381     _DBG_FILE("CNSmlDmEmailAdapter::ExecuteCommandL(): begin");
       
  1382     Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
  1383     _DBG_FILE("CNSmlDmEmailAdapter::ExecuteCommandL(): end");
       
  1384     }
       
  1385 
       
  1386 // -----------------------------------------------------------------------------
       
  1387 // CNSmlDmEmailAdapter::ExecuteCommandL()
       
  1388 // -----------------------------------------------------------------------------
       
  1389 void CNSmlDmEmailAdapter::ExecuteCommandL( const TDesC8& /*aURI*/,
       
  1390                                            const TDesC8& /*aLUID*/,
       
  1391                                            RWriteStream*& /*aStream*/,
       
  1392                                            const TDesC8& /*aType*/,
       
  1393                                            const TInt aStatusRef )
       
  1394     {
       
  1395     //Not supported
       
  1396     _DBG_FILE("CNSmlDmEmailAdapter::ExecuteCommandL(): begin");
       
  1397     Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
  1398     _DBG_FILE("CNSmlDmEmailAdapter::ExecuteCommandL(): end");
       
  1399     }
       
  1400 
       
  1401 // -----------------------------------------------------------------------------
       
  1402 // CNSmlDmEmailAdapter::CopyCommandL()
       
  1403 // -----------------------------------------------------------------------------
       
  1404 void CNSmlDmEmailAdapter::CopyCommandL( const TDesC8& /*aTargetURI*/,
       
  1405                                         const TDesC8& /*aTargetLUID*/,
       
  1406                                         const TDesC8& /*aSourceURI*/,
       
  1407                                         const TDesC8& /*aSourceLUID*/,
       
  1408                                         const TDesC8& /*aType*/,
       
  1409                                         TInt aStatusRef )
       
  1410     {
       
  1411     //Not supported
       
  1412     _DBG_FILE("CNSmlDmEmailAdapter::CopyCommandL(): begin");
       
  1413     Callback().SetStatusL(aStatusRef, CSmlDmAdapter::EError);
       
  1414     _DBG_FILE("CNSmlDmEmailAdapter::CopyCommandL(): end");
       
  1415     }
       
  1416 
       
  1417 // -----------------------------------------------------------------------------
       
  1418 // CNSmlDmEmailAdapter::StartAtomicL()
       
  1419 // -----------------------------------------------------------------------------
       
  1420 void CNSmlDmEmailAdapter::StartAtomicL()
       
  1421     {
       
  1422     //Not supported
       
  1423     _DBG_FILE("CNSmlDmEmailAdapter::StartAtomicL(): begin");
       
  1424     _DBG_FILE("CNSmlDmEmailAdapter::StartAtomicL(): end");
       
  1425     }
       
  1426 
       
  1427 // -----------------------------------------------------------------------------
       
  1428 // CNSmlDmEmailAdapter::CommitAtomicL()
       
  1429 // -----------------------------------------------------------------------------
       
  1430 void CNSmlDmEmailAdapter::CommitAtomicL()
       
  1431     {
       
  1432     //Not supported
       
  1433     _DBG_FILE("CNSmlDmEmailAdapter::CommitAtomicL(): begin");
       
  1434     _DBG_FILE("CNSmlDmEmailAdapter::CommitAtomicL(): end");
       
  1435     }
       
  1436 
       
  1437 // -----------------------------------------------------------------------------
       
  1438 // CNSmlDmEmailAdapter::RollbackAtomicL()
       
  1439 // -----------------------------------------------------------------------------
       
  1440 void CNSmlDmEmailAdapter::RollbackAtomicL()
       
  1441     {
       
  1442     //Not supported
       
  1443     _DBG_FILE("CNSmlDmEmailAdapter::RollbackAtomicL(): begin");
       
  1444     _DBG_FILE("CNSmlDmEmailAdapter::RollbackAtomicL(): end");
       
  1445     }
       
  1446 
       
  1447 // -----------------------------------------------------------------------------
       
  1448 // CNSmlDmEmailAdapter::StreamingSupport()
       
  1449 // -----------------------------------------------------------------------------
       
  1450 TBool CNSmlDmEmailAdapter::StreamingSupport( TInt& /*aItemSize*/ )
       
  1451     {
       
  1452     _DBG_FILE("CNSmlDmEmailAdapter::StreamingSupport(): begin");
       
  1453     _DBG_FILE("CNSmlDmEmailAdapter::StreamingSupport(): end");
       
  1454     return EFalse;
       
  1455     }
       
  1456 
       
  1457 // -----------------------------------------------------------------------------
       
  1458 // CNSmlDmEmailAdapter::StreamCommittedL()
       
  1459 // -----------------------------------------------------------------------------
       
  1460 void CNSmlDmEmailAdapter::StreamCommittedL()
       
  1461     {
       
  1462     //Not supported
       
  1463     _DBG_FILE("CNSmlDmEmailAdapter::StreamCommittedL(): begin");
       
  1464     _DBG_FILE("CNSmlDmEmailAdapter::StreamCommittedL(): end");
       
  1465     }
       
  1466 // -----------------------------------------------------------------------------
       
  1467 // CNSmlDmEmailAdapter::CompleteOutstandingCmdsL()
       
  1468 // Indication of message ending. The buffered commands must be executed
       
  1469 // before return of this function.
       
  1470 // -----------------------------------------------------------------------------
       
  1471 void CNSmlDmEmailAdapter::CompleteOutstandingCmdsL()
       
  1472     {
       
  1473     _DBG_FILE("CNSmlDmEmailAdapter::CompleteOutstandingCmdsL(): begin");
       
  1474 
       
  1475     for(TInt i=0;i<iBuffer->Count();i++)
       
  1476         {
       
  1477 
       
  1478         //Set buffered port values
       
  1479         for(TInt cmd=0;cmd<iBuffer->At(i).iNodeBuf->Count();cmd++)
       
  1480             {
       
  1481             
       
  1482             TNSmlEmailSettingsElement& bufElem = iBuffer->At(i);
       
  1483             TNSmlEmailCommandElement& elem = bufElem.iNodeBuf->At(cmd);
       
  1484 
       
  1485             if(elem.iLastUriSeg->Compare(KNSmlDMEmailMrcvPort)==0)
       
  1486                 {
       
  1487                 TUint port = DesToInt(*elem.iData);
       
  1488                 
       
  1489                 if(port>0 && port <= KNSmlDMEmailMaxPortNbr)
       
  1490                     {
       
  1491                     if(bufElem.iPop3Settings!= NULL)
       
  1492                         {
       
  1493                         bufElem.iPop3Settings->SetPort(port);
       
  1494                         }
       
  1495                     else if(bufElem.iImap4Settings!= NULL)
       
  1496                         {
       
  1497                         bufElem.iImap4Settings->SetPort(port);
       
  1498                         }
       
  1499                     }
       
  1500                 else
       
  1501                     {
       
  1502                     elem.iStatus = CSmlDmAdapter::EInvalidObject;
       
  1503                     }
       
  1504                 }
       
  1505             else if(elem.iLastUriSeg->Compare(KNSmlDMEmailMsndPort)==0)
       
  1506                 {
       
  1507                 TUint port = DesToInt(*elem.iData);
       
  1508                 
       
  1509                 if(port>0 && port <= KNSmlDMEmailMaxPortNbr)
       
  1510                     {
       
  1511                     bufElem.iSmtpSettings->SetPort(port);
       
  1512                     }
       
  1513                 else
       
  1514                     {
       
  1515                     elem.iStatus = CSmlDmAdapter::EInvalidObject;
       
  1516                     }
       
  1517                 }
       
  1518                 if((iBuffer->At(i).iSmtpSettings->LoginName()==KNullDesC8) && elem.iLastUriSeg->Compare(KNSmlDMEmailUID)==0)
       
  1519               	{
       
  1520                 	elem.iStatus = CSmlDmAdapter::EError;
       
  1521         	      }
       
  1522            }
       
  1523         
       
  1524 
       
  1525         CSmlDmAdapter::TError stat = CSmlDmAdapter::EOk;
       
  1526         TInt luid=-1;
       
  1527         
       
  1528         if(iBuffer->At(i).iExecuted)
       
  1529             {
       
  1530             TRAPD( leaveCode,luid = ValidateAndStoreSettingsL(i) );
       
  1531             if(leaveCode!=KErrNone)
       
  1532                 {
       
  1533                 stat = CSmlDmAdapter::EError;
       
  1534                 }
       
  1535             }
       
  1536         else
       
  1537             {
       
  1538             stat = CSmlDmAdapter::EError;
       
  1539             }
       
  1540 
       
  1541         for(TInt cmd=0;cmd<iBuffer->At(i).iNodeBuf->Count();cmd++)
       
  1542             {
       
  1543 
       
  1544             if(iBuffer->At(i).iNodeBuf->At(cmd).iStatus==CSmlDmAdapter::EOk)
       
  1545                 {
       
  1546                 Callback().SetStatusL(iBuffer->At(i).iNodeBuf->At(cmd).iStatusRef,
       
  1547                                       stat);
       
  1548                 if(!iBuffer->At(i).iNodeBuf->At(cmd).iLeaf)
       
  1549                     {
       
  1550                     if(stat==CSmlDmAdapter::EOk)
       
  1551                         {
       
  1552                         HBufC8* luidDes = IntToDes8LC(luid);
       
  1553                         Callback().SetMappingL( iBuffer->At(i).iMappingName->Des(),
       
  1554                                                 luidDes->Des() );
       
  1555                         CleanupStack::PopAndDestroy(); //luidDes
       
  1556                         }
       
  1557                     }
       
  1558                 }
       
  1559             else
       
  1560                 {
       
  1561                 Callback().SetStatusL(iBuffer->At(i).iNodeBuf->At(cmd).iStatusRef,
       
  1562                                       iBuffer->At(i).iNodeBuf->At(cmd).iStatus);
       
  1563                 }
       
  1564 
       
  1565             delete iBuffer->At(i).iNodeBuf->At(cmd).iLastUriSeg;
       
  1566             iBuffer->At(i).iNodeBuf->At(cmd).iLastUriSeg= NULL;
       
  1567             delete iBuffer->At(i).iNodeBuf->At(cmd).iData;
       
  1568             iBuffer->At(i).iNodeBuf->At(cmd).iData= NULL;
       
  1569 
       
  1570             }
       
  1571         iBuffer->At(i).iNodeBuf->Reset();
       
  1572         delete iBuffer->At(i).iNodeBuf;
       
  1573         iBuffer->At(i).iNodeBuf = NULL;
       
  1574         delete iBuffer->At(i).iMappingName;
       
  1575         iBuffer->At(i).iMappingName= NULL;
       
  1576         delete iBuffer->At(i).iName;
       
  1577         iBuffer->At(i).iName= NULL;
       
  1578         delete iBuffer->At(i).iSmtpSettings;
       
  1579         iBuffer->At(i).iSmtpSettings= NULL;
       
  1580         delete iBuffer->At(i).iPop3Settings;
       
  1581         iBuffer->At(i).iPop3Settings= NULL;
       
  1582         delete iBuffer->At(i).iImap4Settings;
       
  1583         iBuffer->At(i).iImap4Settings= NULL;
       
  1584         delete iBuffer->At(i).iIapPref;
       
  1585         iBuffer->At(i).iIapPref= NULL;
       
  1586         delete iBuffer->At(i).iSmtpIapPref;
       
  1587         iBuffer->At(i).iSmtpIapPref= NULL;
       
  1588 
       
  1589         } // end for
       
  1590     iBuffer->Reset();
       
  1591 
       
  1592     _DBG_FILE("CNSmlDmEmailAdapter::CompleteOutstandingCmdsL(): end");
       
  1593     }
       
  1594 
       
  1595 // -----------------------------------------------------------------------------
       
  1596 //
       
  1597 // -----------------------------------------------------------------------------
       
  1598 
       
  1599 #ifndef IMPLEMENTATION_PROXY_ENTRY
       
  1600 #define IMPLEMENTATION_PROXY_ENTRY(aUid, aFuncPtr)  {{aUid},(aFuncPtr)}
       
  1601 #endif
       
  1602 
       
  1603 const TImplementationProxy ImplementationTable[] =
       
  1604     {
       
  1605     IMPLEMENTATION_PROXY_ENTRY(KNSmlDMEmailAdapterImplUid, 
       
  1606                                CNSmlDmEmailAdapter::NewL)
       
  1607     };
       
  1608 
       
  1609 // -----------------------------------------------------------------------------
       
  1610 //
       
  1611 // -----------------------------------------------------------------------------
       
  1612 EXPORT_C const TImplementationProxy* ImplementationGroupProxy( TInt& aTableCount )
       
  1613     {
       
  1614     _DBG_FILE("ImplementationGroupProxy() for CNSmlDmEmailAdapter: begin");
       
  1615     aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
  1616     _DBG_FILE("ImplementationGroupProxy() for CNSmlDmEmailAdapter: end");
       
  1617     return ImplementationTable;
       
  1618     }
       
  1619 
       
  1620 
       
  1621 
       
  1622 // -----------------------------------------------------------------------------
       
  1623 // TPtrC8 CNSmlDmEmailAdapter::LastURISeg(const TDesC8& aURI)
       
  1624 // Returns only the last uri segemnt
       
  1625 // -----------------------------------------------------------------------------
       
  1626 TPtrC8 CNSmlDmEmailAdapter::LastURISeg(const TDesC8& aURI) const
       
  1627     {
       
  1628     _DBG_FILE("CNSmlDmEmailAdapter::LastURISeg() : begin");
       
  1629     TInt i;
       
  1630     for(i=aURI.Length()-1;i>=0;i--)
       
  1631         {
       
  1632         if(aURI[i]=='/')
       
  1633             {
       
  1634             break;
       
  1635             }
       
  1636         }
       
  1637     _DBG_FILE("CNSmlDmEmailAdapter::LastURISeg() : end");
       
  1638     if(i==0)
       
  1639         {
       
  1640         return aURI;
       
  1641         }
       
  1642     else
       
  1643         {
       
  1644         return aURI.Mid(i+1);
       
  1645         }
       
  1646     }
       
  1647 
       
  1648 // -----------------------------------------------------------------------------
       
  1649 // TPtrC8 CNSmlDmEmailAdapter::RemoveLastURISeg(const TDesC8& aURI)
       
  1650 // returns parent uri, i.e. removes last uri segment
       
  1651 // -----------------------------------------------------------------------------
       
  1652 TPtrC8 CNSmlDmEmailAdapter::RemoveLastURISeg(const TDesC8& aURI) const
       
  1653     {
       
  1654     TInt i;
       
  1655     for(i=aURI.Length()-1;i>=0;i--)
       
  1656         {
       
  1657         if(aURI[i]=='/')
       
  1658             {
       
  1659             break;
       
  1660             }
       
  1661         }
       
  1662     return aURI.Left(i);
       
  1663     }
       
  1664 
       
  1665 
       
  1666 
       
  1667 // -----------------------------------------------------------------------------
       
  1668 // TInt CNSmlDmEmailAdapter::NumOfURISegs(const TDesC8& aURI)
       
  1669 // Returns the num of uri segs
       
  1670 // -----------------------------------------------------------------------------
       
  1671 TInt CNSmlDmEmailAdapter::NumOfURISegs(const TDesC8& aURI) const
       
  1672     {
       
  1673     TInt numOfURISegs = 1;
       
  1674     for(TInt i=0;i<aURI.Length();i++)
       
  1675         {
       
  1676         if(aURI[i]=='/')
       
  1677             {
       
  1678             numOfURISegs++;
       
  1679             }
       
  1680         }
       
  1681     return numOfURISegs;
       
  1682     }
       
  1683 
       
  1684 
       
  1685 
       
  1686 // -----------------------------------------------------------------------------
       
  1687 // void CNSmlDmEmailAdapter::HandleSessionEventL( TMsvSessionEvent /*aEvent*/,
       
  1688 //                                               TAny* /*aArg1*/,
       
  1689 //                                               TAny* /*aArg2*/,
       
  1690 //                                               TAny* /*aArg3*/ )
       
  1691 // -----------------------------------------------------------------------------
       
  1692 void CNSmlDmEmailAdapter::HandleSessionEventL( TMsvSessionEvent /*aEvent*/,
       
  1693                                                TAny* /*aArg1*/,
       
  1694                                                TAny* /*aArg2*/,
       
  1695                                                TAny* /*aArg3*/ )
       
  1696     {
       
  1697     _DBG_FILE("CNSmlDmEmailAdapter::HandleSessionEventL(): begin");
       
  1698     _DBG_FILE("CNSmlDmEmailAdapter::HandleSessionEventL(): end");
       
  1699     }
       
  1700 
       
  1701 
       
  1702 // -----------------------------------------------------------------------------
       
  1703 // TInt CNSmlDmEmailAdapter::DesToInt(const TDesC& aLuid) const
       
  1704 // Converts 16bit descriptor to int
       
  1705 // -----------------------------------------------------------------------------
       
  1706 TInt CNSmlDmEmailAdapter::DesToInt(const TDesC& aLuid) const
       
  1707     {
       
  1708     TLex16 lex(aLuid);
       
  1709     TInt value = 0;
       
  1710     lex.Val(value);
       
  1711     return value;
       
  1712     }
       
  1713 
       
  1714 // -----------------------------------------------------------------------------
       
  1715 // TInt CNSmlDmEmailAdapter::DesToInt(const TDesC8& aLuid) const
       
  1716 // Converts 8bit descriptor to int
       
  1717 // -----------------------------------------------------------------------------
       
  1718 TInt CNSmlDmEmailAdapter::DesToInt(const TDesC8& aLuid) const
       
  1719     {
       
  1720     TLex8 lex(aLuid);
       
  1721     TInt value = 0;
       
  1722     lex.Val(value);
       
  1723     return value;
       
  1724     }
       
  1725 
       
  1726 // -----------------------------------------------------------------------------
       
  1727 // CNSmlDmEmailAdapter::FirstAddLeafL()
       
  1728 // -----------------------------------------------------------------------------
       
  1729 TInt CNSmlDmEmailAdapter::FirstAddLeafL(const TDesC8& aURI, 
       
  1730                                         const TDesC8& aObject,
       
  1731                                         TInt aStatusRef,
       
  1732                                         TInt aIndex)
       
  1733     {
       
  1734     _DBG_FILE("CNSmlDmEmailAdapter::FirstAddLeafL() :: begin");
       
  1735 
       
  1736     TPtrC8 lastSeg = LastURISeg(aURI);
       
  1737 
       
  1738     TNSmlEmailCommandElement newCommand;
       
  1739     newCommand.iLastUriSeg = lastSeg.AllocLC();
       
  1740     newCommand.iData = aObject.AllocLC();
       
  1741     newCommand.iStatusRef = aStatusRef;
       
  1742     newCommand.iLeaf = ETrue;
       
  1743     newCommand.iStatus = CSmlDmAdapter::EOk;
       
  1744     iBuffer->At(aIndex).iNodeBuf->AppendL(newCommand);
       
  1745     CleanupStack::Pop(2); //newCommand.iLastUriSeg, newCommand.iData
       
  1746     if(!iBuffer->At(aIndex).iExecuted)
       
  1747         {
       
  1748         if(lastSeg.Compare(KNSmlDMEmailMpro)==0)
       
  1749             {
       
  1750             TBool ok = EFalse;
       
  1751             HBufC8* object = aObject.AllocLC();
       
  1752             TPtr8 objectPtr = object->Des();
       
  1753             objectPtr.UpperCase();
       
  1754 
       
  1755             if(objectPtr.Find(KNSmlDMEmailMproPOP) >= 0)
       
  1756                 {
       
  1757                 iBuffer->At(aIndex).iPop3Settings = new(ELeave) CImPop3Settings;
       
  1758                 ok=ETrue;
       
  1759                 }
       
  1760 
       
  1761             else if(objectPtr.Find(KNSmlDMEmailMproIMAP) >= 0)
       
  1762                 {
       
  1763                 iBuffer->At(aIndex).iImap4Settings = new(ELeave) CImImap4Settings;
       
  1764                 ok=ETrue;
       
  1765                 }
       
  1766             if(ok)
       
  1767                 {
       
  1768                 iBuffer->At(aIndex).iSmtpSettings = new(ELeave) CImSmtpSettings;
       
  1769                 iBuffer->At(aIndex).iIapPref = CImIAPPreferences::NewLC();
       
  1770                 iBuffer->At(aIndex).iSmtpIapPref = CImIAPPreferences::NewLC();
       
  1771                 CleanupStack::Pop(); //iBuffer->At(index).iIapPref
       
  1772                 CleanupStack::Pop(); //iBuffer->At(index).iSmtpIapPref
       
  1773                 SetDefaultSettingsL(aIndex);
       
  1774 
       
  1775                 for(TInt i = 0; i<iBuffer->At(aIndex).iNodeBuf->Count();i++)
       
  1776                     {
       
  1777                     if(iBuffer->At(aIndex).iNodeBuf->At(i).iLeaf)
       
  1778                         {
       
  1779                         DoAddLeafObjectL(iBuffer->At(aIndex).iNodeBuf->At(i).iLastUriSeg->Des(),
       
  1780                                          iBuffer->At(aIndex).iNodeBuf->At(i).iData->Des(),
       
  1781                                          aIndex, i);
       
  1782                         }
       
  1783                     }
       
  1784 
       
  1785                 iBuffer->At(aIndex).iExecuted = ETrue;
       
  1786                 }
       
  1787             CleanupStack::PopAndDestroy(); //object
       
  1788             }
       
  1789         }
       
  1790     else
       
  1791         {
       
  1792         DoAddLeafObjectL(lastSeg,aObject,aIndex,
       
  1793                          iBuffer->At(aIndex).iNodeBuf->Count()-1);
       
  1794         }
       
  1795 
       
  1796     _DBG_FILE("CNSmlDmEmailAdapter::FirstAddLeafL() :: end");
       
  1797     return KErrNone;
       
  1798     }
       
  1799 
       
  1800 
       
  1801 // -----------------------------------------------------------------------------
       
  1802 // CNSmlDmEmailAdapter::DoAddLeafObjectL
       
  1803 // -----------------------------------------------------------------------------
       
  1804 TInt CNSmlDmEmailAdapter::DoAddLeafObjectL(const TDesC8& aLastUriSeg,
       
  1805                                            const TDesC8& aObject,
       
  1806                                            TInt aIndex,
       
  1807                                            TInt aCommand)
       
  1808     {
       
  1809     _DBG_FILE("CNSmlDmEmailAdapter::DoAddLeafObjectL() :: begin");
       
  1810     if(aLastUriSeg.Compare(KNSmlDMEmailName)==0)
       
  1811         {
       
  1812         if ( aObject.Size() < KEmailAccountNameSize )
       
  1813             {
       
  1814             delete iBuffer->At(aIndex).iName;
       
  1815             iBuffer->At(aIndex).iName = NULL;
       
  1816             iBuffer->At(aIndex).iName = HBufC::NewL(aObject.Size());
       
  1817             TPtr namePtr = iBuffer->At(aIndex).iName->Des();
       
  1818             CnvUtfConverter::ConvertToUnicodeFromUtf8( namePtr, aObject );
       
  1819             }
       
  1820         else 
       
  1821             {
       
  1822             iBuffer->At(aIndex).iNodeBuf->At(aCommand).iStatus = 
       
  1823                 CSmlDmAdapter::ETooLargeObject;
       
  1824             }
       
  1825         }
       
  1826 
       
  1827     else if(aLastUriSeg.Compare(KNSmlDMEmailUID)==0)
       
  1828         {
       
  1829         if(iBuffer->At(aIndex).iPop3Settings!= NULL)
       
  1830             {
       
  1831             iBuffer->At(aIndex).iPop3Settings->SetLoginNameL(aObject);
       
  1832             }
       
  1833         else if(iBuffer->At(aIndex).iImap4Settings!= NULL)
       
  1834             {
       
  1835             iBuffer->At(aIndex).iImap4Settings->SetLoginNameL(aObject);
       
  1836             }
       
  1837         if(iBuffer->At(aIndex).iSmtpSettings->LoginName()==KNullDesC8)
       
  1838             {
       
  1839             iBuffer->At(aIndex).iSmtpSettings->SetLoginNameL(aObject);
       
  1840             }
       
  1841         }
       
  1842 
       
  1843     else if(aLastUriSeg.Compare(KNSmlDMEmailPW)==0)
       
  1844         {
       
  1845         if(iBuffer->At(aIndex).iPop3Settings!= NULL)
       
  1846             {
       
  1847             iBuffer->At(aIndex).iPop3Settings->SetPasswordL(aObject);
       
  1848             }
       
  1849         else if(iBuffer->At(aIndex).iImap4Settings!= NULL)
       
  1850             {
       
  1851             iBuffer->At(aIndex).iImap4Settings->SetPasswordL(aObject);
       
  1852             }
       
  1853         if(iBuffer->At(aIndex).iSmtpSettings->Password()==KNullDesC8)
       
  1854             {
       
  1855             iBuffer->At(aIndex).iSmtpSettings->SetPasswordL(aObject);
       
  1856             }
       
  1857         }
       
  1858 
       
  1859     else if(aLastUriSeg.Compare(KNSmlDMEmailUAddr)==0)
       
  1860         {
       
  1861         HBufC *object = HBufC::NewLC(aObject.Size());
       
  1862         TPtr objectPtr = object->Des();
       
  1863         CnvUtfConverter::ConvertToUnicodeFromUtf8( objectPtr, aObject );
       
  1864         if(iBuffer->At(aIndex).iSmtpSettings!= NULL)
       
  1865             {
       
  1866             iBuffer->At(aIndex).iSmtpSettings->SetEmailAddressL(objectPtr);
       
  1867             iBuffer->At(aIndex).iSmtpSettings->SetReplyToAddressL(objectPtr);
       
  1868             iBuffer->At(aIndex).iSmtpSettings->SetReceiptAddressL(objectPtr);
       
  1869             }
       
  1870 
       
  1871         CleanupStack::PopAndDestroy(); //object
       
  1872         }
       
  1873 
       
  1874     else if(aLastUriSeg.Compare(KNSmlDMEmailUName)==0)
       
  1875         {
       
  1876         HBufC *object = HBufC::NewLC(aObject.Size());
       
  1877         TPtr objectPtr = object->Des();
       
  1878         CnvUtfConverter::ConvertToUnicodeFromUtf8( objectPtr, aObject );
       
  1879         if(iBuffer->At(aIndex).iSmtpSettings!= NULL)
       
  1880             {
       
  1881             iBuffer->At(aIndex).iSmtpSettings->SetEmailAliasL(objectPtr);
       
  1882             }
       
  1883         CleanupStack::PopAndDestroy(); //object
       
  1884         }
       
  1885 
       
  1886     else if(aLastUriSeg.Compare( KNSmlDMEmailMrcv)==0)
       
  1887         {
       
  1888         HBufC *object = HBufC::NewLC(aObject.Size());
       
  1889         TPtr objectPtr = object->Des();
       
  1890         CnvUtfConverter::ConvertToUnicodeFromUtf8( objectPtr, aObject );
       
  1891         if(iBuffer->At(aIndex).iPop3Settings!= NULL)
       
  1892             {
       
  1893             iBuffer->At(aIndex).iPop3Settings->SetServerAddressL(objectPtr);
       
  1894             }
       
  1895         else if(iBuffer->At(aIndex).iImap4Settings!= NULL)
       
  1896             {
       
  1897             iBuffer->At(aIndex).iImap4Settings->SetServerAddressL(objectPtr);
       
  1898             }
       
  1899         CleanupStack::PopAndDestroy(); //object
       
  1900         }
       
  1901 
       
  1902     else if(aLastUriSeg.Compare(KNSmlDMEmailMsnd)==0)
       
  1903         {
       
  1904         HBufC *object = HBufC::NewLC(aObject.Size());
       
  1905         TPtr objectPtr = object->Des();
       
  1906         CnvUtfConverter::ConvertToUnicodeFromUtf8( objectPtr, aObject );
       
  1907         if(iBuffer->At(aIndex).iSmtpSettings!= NULL)
       
  1908             {
       
  1909             iBuffer->At(aIndex).iSmtpSettings->SetServerAddressL(objectPtr);
       
  1910             }
       
  1911         CleanupStack::PopAndDestroy(); //object
       
  1912         }
       
  1913 
       
  1914     else if(aLastUriSeg.Compare(KNSmlDMEmailMpro)==0)
       
  1915         {
       
  1916         iBuffer->At(aIndex).iNodeBuf->At(aCommand).iStatus = CSmlDmAdapter::EOk;
       
  1917         }
       
  1918 
       
  1919     else if(aLastUriSeg.Compare(KNSmlDMEmailUseSecCon)==0)
       
  1920         {
       
  1921         SetDefaultSecurityL(aObject, &iBuffer->At(aIndex), EUseSecCon );
       
  1922         }
       
  1923 
       
  1924     else if(aLastUriSeg.Compare(KNSmlDMEmailUseSauth)==0)
       
  1925         {
       
  1926         HBufC8 *data = aObject.AllocLC();
       
  1927         TPtr8 dataPtr = data->Des();
       
  1928         dataPtr.Capitalize();
       
  1929         TBool object=EFalse;
       
  1930         if(dataPtr.Find(KNSmlDMEmailTrue)>=0)
       
  1931             {
       
  1932             object = ETrue;
       
  1933             }
       
  1934         if(iBuffer->At(aIndex).iSmtpSettings!= NULL)
       
  1935             {
       
  1936             iBuffer->At(aIndex).iSmtpSettings->SetSMTPAuth(object);
       
  1937             }
       
  1938         CleanupStack::PopAndDestroy(); //data
       
  1939         }
       
  1940 
       
  1941     else if(aLastUriSeg.Compare(KNSmlDMEmailSauthUID)==0)
       
  1942         {
       
  1943         if( iBuffer->At(aIndex).iSmtpSettings )
       
  1944             {
       
  1945             iBuffer->At(aIndex).iSmtpSettings->SetLoginNameL(aObject);
       
  1946             }
       
  1947         }
       
  1948 
       
  1949     else if(aLastUriSeg.Compare(KNSmlDMEmailSauthPW)==0)
       
  1950         {
       
  1951         if( iBuffer->At(aIndex).iSmtpSettings )
       
  1952             {
       
  1953             iBuffer->At(aIndex).iSmtpSettings->SetPasswordL( aObject );
       
  1954             }
       
  1955         }
       
  1956 
       
  1957     else if(aLastUriSeg.Compare(KNSmlDMEmailPtxtSAuth)==0)
       
  1958         {
       
  1959         HBufC8 *data = aObject.AllocLC();
       
  1960         TPtr8 dataPtr = data->Des();
       
  1961         dataPtr.Capitalize();
       
  1962         TBool object=EFalse;
       
  1963         if(dataPtr.Find(KNSmlDMEmailTrue)>=0)
       
  1964             {
       
  1965             object = ETrue;
       
  1966             }
       
  1967         if(iBuffer->At(aIndex).iSmtpSettings!= NULL)
       
  1968             {
       
  1969             iBuffer->At(aIndex).iSmtpSettings->SetInboxLoginDetails(object);
       
  1970             }
       
  1971         CleanupStack::PopAndDestroy(); //data
       
  1972         }
       
  1973 
       
  1974     else if(aLastUriSeg.Compare(KNSmlDMEmailSUseSecCon)==0)
       
  1975         {
       
  1976         SetDefaultSecurityL(aObject, &iBuffer->At(aIndex), ESuseSecCon );
       
  1977         }
       
  1978 
       
  1979     else if(aLastUriSeg.Compare(KNSmlDMEmailUseStartTLS)==0)
       
  1980         {
       
  1981         SetDefaultSecurityL(aObject, &iBuffer->At(aIndex), EUseStartTls );
       
  1982         }
       
  1983 
       
  1984     else if(aLastUriSeg.Compare(KNSmlDMEmailSUseStartTLS)==0)
       
  1985         {
       
  1986         SetDefaultSecurityL(aObject, &iBuffer->At(aIndex), ESuseStartTls );
       
  1987         }
       
  1988 
       
  1989     else if(aLastUriSeg.Compare(KNSmlDMEmailMrcvPort)==0)
       
  1990         {
       
  1991         TNSmlEmailSettingsElement& bufElem = iBuffer->At(aIndex);
       
  1992         TNSmlEmailCommandElement& elem = bufElem.iNodeBuf->At(aCommand);
       
  1993         elem.iData = aObject.AllocL();
       
  1994         }
       
  1995 
       
  1996     else if(aLastUriSeg.Compare(KNSmlDMEmailMsndPort)==0)
       
  1997         {
       
  1998         TNSmlEmailSettingsElement& bufElem = iBuffer->At(aIndex);
       
  1999         TNSmlEmailCommandElement& elem = bufElem.iNodeBuf->At(aCommand);
       
  2000         elem.iData = aObject.AllocL();
       
  2001         }
       
  2002 
       
  2003     else if(aLastUriSeg.Compare(KNSmlDMEmailUseAPOP)==0)
       
  2004         {
       
  2005         TBool object=EFalse;
       
  2006         if(aObject.Find(_L8("True"))>=0 || aObject.Find(_L8("true"))>=0)
       
  2007             {
       
  2008             object = ETrue;
       
  2009             }
       
  2010         if(iBuffer->At(aIndex).iPop3Settings!= NULL)
       
  2011             {
       
  2012             iBuffer->At(aIndex).iPop3Settings->SetApop(object);
       
  2013             }
       
  2014         }
       
  2015 
       
  2016     else if(aLastUriSeg.Compare(KNSmlDMEmailFolderPath)==0)
       
  2017         {
       
  2018         if(iBuffer->At(aIndex).iImap4Settings!= NULL)
       
  2019             {
       
  2020             iBuffer->At(aIndex).iImap4Settings->SetFolderPathL(aObject);
       
  2021             }
       
  2022         }
       
  2023 
       
  2024     else if(aLastUriSeg.Compare(KNSmlDMEmailToNapID)==0)
       
  2025         {
       
  2026         CNSmlDMIAPMatcher* iapmatch = CNSmlDMIAPMatcher::NewL( &Callback() );
       
  2027         CleanupStack::PushL(iapmatch);
       
  2028 
       
  2029         TInt iap = iapmatch->IAPIdFromURIL( aObject );
       
  2030 
       
  2031         if ( iap != KErrNotFound )
       
  2032             {
       
  2033             TImIAPChoice localIAP;
       
  2034             localIAP.iIAP = iap;
       
  2035             DBG_ARGS(_S("IAP = %d"), localIAP.iIAP);
       
  2036             localIAP.iDialogPref = ECommDbDialogPrefDoNotPrompt;
       
  2037 
       
  2038             if ( iBuffer->At(aIndex).iSmtpIapPref->NumberOfIAPs() )
       
  2039                 {
       
  2040                 iBuffer->At(aIndex).iIapPref->RemoveIAPL(0);
       
  2041                 }
       
  2042             iBuffer->At(aIndex).iIapPref->AddIAPL(localIAP,0);
       
  2043             if (iBuffer->At(aIndex).iSmtpSettings!= NULL)
       
  2044                 {
       
  2045                 if ( iBuffer->At(aIndex).iSmtpIapPref->NumberOfIAPs() )
       
  2046                     {
       
  2047                     iBuffer->At(aIndex).iSmtpIapPref->RemoveIAPL(0);
       
  2048                     }
       
  2049                 iBuffer->At(aIndex).iSmtpIapPref->AddIAPL(localIAP,0);
       
  2050                 }
       
  2051             }
       
  2052         else
       
  2053             {
       
  2054             iBuffer->At(aIndex).iNodeBuf->At(aCommand).iStatus = CSmlDmAdapter::ENotFound;
       
  2055             }
       
  2056 
       
  2057         CleanupStack::PopAndDestroy(); // iapmatch
       
  2058         }
       
  2059 
       
  2060     else if(aLastUriSeg.Compare(KNSmlDMEmailSToNapID)==0)
       
  2061         {
       
  2062         CNSmlDMIAPMatcher* iapmatch = CNSmlDMIAPMatcher::NewL( &Callback() );
       
  2063         CleanupStack::PushL(iapmatch);
       
  2064 
       
  2065         TInt iap = iapmatch->IAPIdFromURIL( aObject );
       
  2066 
       
  2067         if ( iap != KErrNotFound )
       
  2068             {
       
  2069             TImIAPChoice localIAP;
       
  2070             localIAP.iIAP = iap;
       
  2071             DBG_ARGS(_S("SMTP IAP = %d"), localIAP.iIAP);
       
  2072             localIAP.iDialogPref = ECommDbDialogPrefDoNotPrompt;
       
  2073             if ( iBuffer->At(aIndex).iSmtpSettings!= NULL )
       
  2074                 {
       
  2075                 if ( iBuffer->At(aIndex).iSmtpIapPref->NumberOfIAPs() )
       
  2076                     {
       
  2077                     iBuffer->At(aIndex).iSmtpIapPref->RemoveIAPL(0);
       
  2078                     }
       
  2079                 iBuffer->At(aIndex).iSmtpIapPref->AddIAPL(localIAP,0);
       
  2080                 }
       
  2081             else
       
  2082                 {
       
  2083                 iBuffer->At(aIndex).iNodeBuf->At(aCommand).iStatus =
       
  2084                     CSmlDmAdapter::ENotFound;
       
  2085                 }
       
  2086             }
       
  2087         else
       
  2088             {
       
  2089             iBuffer->At(aIndex).iNodeBuf->At(aCommand).iStatus =
       
  2090                 CSmlDmAdapter::ENotFound;
       
  2091             }
       
  2092 
       
  2093         CleanupStack::PopAndDestroy(); // iapmatch
       
  2094         }
       
  2095     _DBG_FILE("CNSmlDmEmailAdapter::DoAddLeafObjectL() :: end");
       
  2096     return KErrNone;
       
  2097     }
       
  2098 
       
  2099 
       
  2100 // -----------------------------------------------------------------------------
       
  2101 // void CNSmlDmEmailAdapter::SetDefaultSettingsL(TInt aIndex)
       
  2102 // Set defaults for used protocol
       
  2103 // -----------------------------------------------------------------------------
       
  2104 void CNSmlDmEmailAdapter::SetDefaultSettingsL(TInt aIndex)
       
  2105     {
       
  2106     _DBG_FILE("CNSmlDmEmailAdapter::SetDefaultSettingsL() :: begin");
       
  2107     TNSmlEmailSettingsElement& thisElem = iBuffer->At(aIndex);
       
  2108     CEmailAccounts* emailAccs = CEmailAccounts::NewLC();
       
  2109     if( thisElem.iPop3Settings )
       
  2110         {
       
  2111           
       
  2112         emailAccs->PopulateDefaultPopSettingsL(*thisElem.iPop3Settings,
       
  2113                                                *thisElem.iIapPref
       
  2114                                                );
       
  2115         emailAccs->PopulateDefaultSmtpSettingsL(*thisElem.iSmtpSettings,
       
  2116                                                 *thisElem.iSmtpIapPref 
       
  2117                                                );
       
  2118                                                
       
  2119         thisElem.iPop3Settings->SetDeleteEmailsWhenDisconnecting( EFalse ); 
       
  2120         thisElem.iPop3Settings->SetInboxSynchronisationLimit( KNSmlDMEmailDefaultHeaderCount );
       
  2121         thisElem.iPop3Settings->SetSecureSockets( EFalse );
       
  2122         thisElem.iPop3Settings->SetAutoSendOnConnect( ETrue );
       
  2123         thisElem.iPop3Settings->SetDisconnectedUserMode( ETrue );
       
  2124         thisElem.iPop3Settings->SetAcknowledgeReceipts( EFalse );                                
       
  2125         }
       
  2126     else if( thisElem.iImap4Settings )
       
  2127         {
       
  2128         
       
  2129         emailAccs->PopulateDefaultImapSettingsL(*thisElem.iImap4Settings,
       
  2130                                                 *thisElem.iIapPref 
       
  2131                                                 );
       
  2132 
       
  2133         emailAccs->PopulateDefaultSmtpSettingsL(*thisElem.iSmtpSettings,
       
  2134                                                 *thisElem.iSmtpIapPref 
       
  2135                                                );
       
  2136                                                 
       
  2137         thisElem.iImap4Settings->SetDeleteEmailsWhenDisconnecting( EFalse );
       
  2138         thisElem.iImap4Settings->SetInboxSynchronisationLimit( KNSmlDMEmailDefaultHeaderCount );
       
  2139         thisElem.iImap4Settings->SetMailboxSynchronisationLimit( KNSmlDMEmailDefaultHeaderCount );
       
  2140         thisElem.iImap4Settings->SetSecureSockets( EFalse );
       
  2141         thisElem.iImap4Settings->SetAutoSendOnConnect( ETrue );
       
  2142         thisElem.iImap4Settings->SetDisconnectedUserMode( ETrue );
       
  2143         thisElem.iImap4Settings->SetImapIdle( ETrue );
       
  2144         thisElem.iImap4Settings->SetUpdatingSeenFlags( ETrue );
       
  2145         }
       
  2146     thisElem.iSmtpSettings->SetLoginNameL(KNullDesC8);
       
  2147     thisElem.iSmtpSettings->SetPasswordL(KNullDesC8);
       
  2148     // Set IAP id to 0 and ask from user if ToNapID/SToNapID not sent
       
  2149     TImIAPChoice tmpIap;
       
  2150     tmpIap.iIAP = KNSmlDmEmailAlwaysAskIap;
       
  2151     tmpIap.iDialogPref = ECommDbDialogPrefPrompt;
       
  2152     thisElem.iIapPref->AddIAPL( tmpIap, 0 );
       
  2153     thisElem.iSmtpIapPref->AddIAPL( tmpIap, 0 );
       
  2154     CleanupStack::PopAndDestroy(); //emailAccs
       
  2155     _DBG_FILE("CNSmlDmEmailAdapter::SetDefaultSettingsL() :: end");
       
  2156     }
       
  2157 
       
  2158 // -----------------------------------------------------------------------------
       
  2159 // HBufC* CNSmlDmEmailAdapter::IntToDesLC(const TInt aLuid) const
       
  2160 // Converts integer to 16bit descriptor
       
  2161 // -----------------------------------------------------------------------------
       
  2162 HBufC* CNSmlDmEmailAdapter::IntToDesLC(const TInt aLuid) const
       
  2163     {
       
  2164     HBufC* buf = HBufC::NewLC(10); //10 = max length of 32bit integer
       
  2165     TPtr ptrBuf = buf->Des();
       
  2166     ptrBuf.Num(aLuid);
       
  2167     return buf;
       
  2168     }
       
  2169 
       
  2170 // -----------------------------------------------------------------------------
       
  2171 // HBufC8* CNSmlDmEmailAdapter::IntToDes8LC(const TInt aLuid) const
       
  2172 // Converts integer to 8bit descriptor
       
  2173 // -----------------------------------------------------------------------------
       
  2174 HBufC8* CNSmlDmEmailAdapter::IntToDes8LC(const TInt aLuid) const
       
  2175     {
       
  2176     HBufC8* buf = HBufC8::NewLC(10); //10 = max length of 32bit integer
       
  2177     TPtr8 ptrBuf = buf->Des();
       
  2178     ptrBuf.Num(aLuid);
       
  2179     return buf;
       
  2180     }
       
  2181    
       
  2182 // -----------------------------------------------------------------------------
       
  2183 //      CNSmlDmEmailAdapter::ConvertTo8LC()
       
  2184 //      Converts string value to 8-bit
       
  2185 //      
       
  2186 // -----------------------------------------------------------------------------
       
  2187 HBufC8* CNSmlDmEmailAdapter::ConvertTo8LC( const TDesC& aSource ) const
       
  2188     {
       
  2189     HBufC8* buf = HBufC8::NewLC( aSource.Length()*2);
       
  2190     TPtr8 bufPtr = buf->Des();
       
  2191     CnvUtfConverter::ConvertFromUnicodeToUtf8( bufPtr, aSource );
       
  2192 
       
  2193     return buf;
       
  2194     }
       
  2195 
       
  2196 // -----------------------------------------------------------------------------
       
  2197 // TInt CNSmlDmEmailAdapter::ValidateAndStoreSettingsL(TInt aIndex
       
  2198 // Transfers the email settings from memory to phones permanent data store
       
  2199 // -----------------------------------------------------------------------------
       
  2200 TInt CNSmlDmEmailAdapter::ValidateAndStoreSettingsL(TInt aIndex)
       
  2201     {
       
  2202     _DBG_FILE("CNSmlDmEmailAdapter::ValidateAndStoreSettingsL() : begin");
       
  2203     TInt luid = 0;
       
  2204 
       
  2205     TNSmlEmailSettingsElement& thisElem = iBuffer->At(aIndex);
       
  2206 
       
  2207     if( iBuffer->At( aIndex ).iJustFetch )
       
  2208         {
       
  2209         _DBG_FILE("CNSmlDmEmailAdapter::ValidateAndStoreSettingsL() : end");
       
  2210         return luid;
       
  2211         }
       
  2212     CEmailAccounts* emailAccs = CEmailAccounts::NewLC();
       
  2213     if( iBuffer->At( aIndex ).iLuid != 0 )
       
  2214         {
       
  2215         switch ( thisElem.iAccType )
       
  2216             {
       
  2217         case EPop:
       
  2218             {
       
  2219             TPopAccount accId;
       
  2220             emailAccs->GetPopAccountL( thisElem.iServiceId, accId ); 
       
  2221             //Copy name field
       
  2222             accId.iPopAccountName.Copy(thisElem.iName->Des());
       
  2223             emailAccs->SavePopSettingsL( accId, *thisElem.iPop3Settings );
       
  2224 	        emailAccs->SavePopIapSettingsL( accId, *thisElem.iIapPref );            
       
  2225             TSmtpAccount smtpAcc;
       
  2226 	        emailAccs->GetSmtpAccountL( accId.iSmtpService, smtpAcc ); 
       
  2227 	        //Copy name field
       
  2228 	        smtpAcc.iSmtpAccountName.Copy(thisElem.iName->Des());
       
  2229 	        emailAccs->SaveSmtpSettingsL( smtpAcc, *thisElem.iSmtpSettings );
       
  2230 	        emailAccs->SaveSmtpIapSettingsL( smtpAcc, *thisElem.iSmtpIapPref );
       
  2231             break;            
       
  2232             }
       
  2233         case EImap:
       
  2234             {
       
  2235             TImapAccount accId2;
       
  2236     	    emailAccs->GetImapAccountL( thisElem.iServiceId, accId2 );
       
  2237     	    //Copy name field
       
  2238     	    accId2.iImapAccountName.Copy(thisElem.iName->Des());
       
  2239             emailAccs->SaveImapSettingsL( accId2, *thisElem.iImap4Settings );
       
  2240 	        emailAccs->SaveImapIapSettingsL( accId2, *thisElem.iIapPref );
       
  2241             TSmtpAccount smtpAccForImap;
       
  2242 	        emailAccs->GetSmtpAccountL( accId2.iSmtpService, smtpAccForImap );
       
  2243 	        //Copy name field
       
  2244 	        smtpAccForImap.iSmtpAccountName.Copy(thisElem.iName->Des()); 
       
  2245 	        emailAccs->SaveSmtpSettingsL( smtpAccForImap, *thisElem.iSmtpSettings );
       
  2246 	        emailAccs->SaveSmtpIapSettingsL( smtpAccForImap, *thisElem.iSmtpIapPref );
       
  2247             break;            
       
  2248             }
       
  2249         default:
       
  2250 
       
  2251             break;
       
  2252             }
       
  2253         }
       
  2254     else // if(iBuffer->At(aIndex).iLuid != 0)
       
  2255         {
       
  2256         RTelServer telServer;
       
  2257         User::LeaveIfError( telServer.Connect() );
       
  2258         CleanupClosePushL(telServer);
       
  2259 
       
  2260         TInt numPhones = 0;
       
  2261         User::LeaveIfError( telServer.EnumeratePhones( numPhones ) );
       
  2262         if ( numPhones < 1 )
       
  2263             {
       
  2264             User::Leave( KErrNotFound );
       
  2265             }
       
  2266 
       
  2267         RTelServer::TPhoneInfo info;
       
  2268         User::LeaveIfError( telServer.GetPhoneInfo( 0, info ) );
       
  2269         RMobilePhone mobilePhone;
       
  2270         User::LeaveIfError( mobilePhone.Open( telServer, info.iName ) );
       
  2271         CleanupClosePushL( mobilePhone );
       
  2272         TRequestStatus status;
       
  2273         RMobilePhone::TMobilePhoneIdentityV1 mobilePhoneIdentity;
       
  2274         mobilePhone.GetPhoneId( status, mobilePhoneIdentity );
       
  2275         User::WaitForRequest( status );
       
  2276         User::LeaveIfError( status.Int() );
       
  2277         TBuf<50> imei;            
       
  2278         imei.Copy( mobilePhoneIdentity.iSerialNumber );
       
  2279         
       
  2280         CleanupStack::PopAndDestroy(&mobilePhone);
       
  2281         CleanupStack::PopAndDestroy(&telServer);
       
  2282         if ( thisElem.iPop3Settings )
       
  2283             {
       
  2284             TPopAccount accId;
       
  2285             accId = emailAccs->CreatePopAccountL( *thisElem.iName, 
       
  2286                                                   *thisElem.iPop3Settings, 
       
  2287                                                   *thisElem.iIapPref, 
       
  2288                                                   EFalse );
       
  2289 
       
  2290     	    TSmtpAccount smtpAcc = emailAccs->CreateSmtpAccountL( accId, 
       
  2291     	                                                          *thisElem.iSmtpSettings, 
       
  2292                                                                   *thisElem.iSmtpIapPref, 
       
  2293                                                                   EFalse );
       
  2294             TMsvEntry smtpEntry;
       
  2295             TMsvEntry popEntry;
       
  2296             CMsvEntry* entry = iMsvSession->GetEntryL(smtpAcc.iSmtpService);
       
  2297             CleanupStack::PushL(entry);
       
  2298             smtpEntry = entry->Entry();
       
  2299             smtpEntry.iMtmData2 =  smtpAcc.iSmtpAccountId;
       
  2300             smtpEntry.iDescription.Set( imei );
       
  2301 	        entry->ChangeL(smtpEntry);
       
  2302             CleanupStack::PopAndDestroy(entry);
       
  2303 
       
  2304             entry = iMsvSession->GetEntryL(smtpAcc.iRelatedService);
       
  2305             CleanupStack::PushL(entry);
       
  2306             popEntry = entry->Entry();
       
  2307             popEntry.iMtmData2 =  accId.iPopAccountId;          
       
  2308             popEntry.iDescription.Set( imei );
       
  2309 	        entry->ChangeL(popEntry);
       
  2310             CleanupStack::PopAndDestroy(entry);
       
  2311 
       
  2312             if ( accId.iSmtpService != smtpAcc.iSmtpService || smtpAcc.iRelatedService != accId.iPopService )
       
  2313                 {
       
  2314                 DBG_ARGS8(_S8("ERROR!!! PopAccount creation: iSmtpService=%d, smtpAcc.iSmtpService=%d, iRelatedService=%d, iPopService=%d"),
       
  2315                                 accId.iSmtpService, smtpAcc.iSmtpService, smtpAcc.iRelatedService, accId.iPopService );
       
  2316                 }                                                               
       
  2317             
       
  2318             thisElem.iAccType = EPop;
       
  2319             thisElem.iAccId = accId.iPopAccountId;
       
  2320             SetLuidById( thisElem.iAccType, thisElem.iAccId, thisElem.iLuid );
       
  2321 
       
  2322 			  RArray<TPopAccount> popAccs;
       
  2323               CleanupClosePushL( popAccs );
       
  2324               emailAccs->GetPopAccountsL( popAccs );
       
  2325               
       
  2326 			  RArray<TImapAccount> imapAccs;
       
  2327               CleanupClosePushL( imapAccs );
       
  2328               emailAccs->GetImapAccountsL( imapAccs );
       
  2329               
       
  2330               if ( popAccs.Count() == 1 && !imapAccs.Count())
       
  2331                  {
       
  2332                   emailAccs->SetDefaultSmtpAccountL( smtpAcc );
       
  2333                  }
       
  2334               
       
  2335               CleanupStack::PopAndDestroy( &imapAccs );          
       
  2336 			  CleanupStack::PopAndDestroy( &popAccs );          
       
  2337             // Check and set the default account
       
  2338 	        TSmtpAccount defSmtp;
       
  2339 	        TInt retVal = emailAccs->DefaultSmtpAccountL( defSmtp );
       
  2340 	        if (  retVal != KErrNone )
       
  2341 	            {
       
  2342                 RArray<TPopAccount> popAccounts;
       
  2343                 CleanupClosePushL( popAccounts );
       
  2344                 emailAccs->GetPopAccountsL( popAccounts );
       
  2345                 if ( popAccounts.Count() > 0 )
       
  2346                     {
       
  2347                     TSmtpAccount relatedSmtp;
       
  2348                     emailAccs->GetSmtpAccountL( popAccounts[0].iSmtpService, relatedSmtp );
       
  2349 	                emailAccs->SetDefaultSmtpAccountL( relatedSmtp );
       
  2350                     }
       
  2351                 CleanupStack::PopAndDestroy( &popAccounts );
       
  2352 	            }	                
       
  2353             }
       
  2354         else if ( thisElem.iImap4Settings )
       
  2355             {
       
  2356             TImapAccount accId2;
       
  2357             accId2 = emailAccs->CreateImapAccountL(*thisElem.iName, 
       
  2358                                                    *thisElem.iImap4Settings, 
       
  2359                                                    *thisElem.iIapPref,
       
  2360                                                    EFalse);
       
  2361 
       
  2362     	    TSmtpAccount smtpAcc = emailAccs->CreateSmtpAccountL( accId2, 
       
  2363     	                                                          *thisElem.iSmtpSettings, 
       
  2364                                                                   *thisElem.iSmtpIapPref, 
       
  2365                                                                   EFalse );
       
  2366             
       
  2367             TMsvEntry smtpEntry;
       
  2368             TMsvEntry imapEntry;
       
  2369             CMsvEntry* entry = iMsvSession->GetEntryL(smtpAcc.iSmtpService);
       
  2370             CleanupStack::PushL(entry);
       
  2371             smtpEntry = entry->Entry();
       
  2372             smtpEntry.iMtmData2 =  smtpAcc.iSmtpAccountId;          
       
  2373             smtpEntry.iDescription.Set( imei );
       
  2374 	        entry->ChangeL(smtpEntry);
       
  2375             CleanupStack::PopAndDestroy(entry);
       
  2376 
       
  2377             entry = iMsvSession->GetEntryL(smtpAcc.iRelatedService);
       
  2378             CleanupStack::PushL(entry);
       
  2379             imapEntry = entry->Entry();
       
  2380             imapEntry.iMtmData2 =  accId2.iImapAccountId;          
       
  2381             imapEntry.iDescription.Set( imei );
       
  2382 	        entry->ChangeL(imapEntry);
       
  2383             CleanupStack::PopAndDestroy(entry);
       
  2384 
       
  2385             if ( accId2.iSmtpService != smtpAcc.iSmtpService || smtpAcc.iRelatedService != accId2.iImapService )
       
  2386                 {
       
  2387                 DBG_ARGS8(_S8("ERROR!!! ImapAccount creation: iSmtpService=%d, smtpAcc.iSmtpService=%d, iRelatedService=%d, iImapService=%d"),
       
  2388                                 accId2.iSmtpService, smtpAcc.iSmtpService, smtpAcc.iRelatedService, accId2.iImapService );
       
  2389                 }
       
  2390                                                                   
       
  2391                                                    
       
  2392             thisElem.iAccType = EImap;
       
  2393             thisElem.iAccId = accId2.iImapAccountId;
       
  2394             SetLuidById( thisElem.iAccType, thisElem.iAccId, thisElem.iLuid );
       
  2395 
       
  2396             // Check and set the default account
       
  2397             RArray<TPopAccount> popAccs;
       
  2398             CleanupClosePushL( popAccs );
       
  2399             emailAccs->GetPopAccountsL( popAccs );
       
  2400               
       
  2401 			RArray<TImapAccount> imapAccs;
       
  2402             CleanupClosePushL( imapAccs );
       
  2403             emailAccs->GetImapAccountsL( imapAccs );
       
  2404               
       
  2405             if ( imapAccs.Count() == 1 && !popAccs.Count())
       
  2406             	{
       
  2407                 emailAccs->SetDefaultSmtpAccountL( smtpAcc );
       
  2408                 }
       
  2409               
       
  2410             CleanupStack::PopAndDestroy( &imapAccs );          
       
  2411 			CleanupStack::PopAndDestroy( &popAccs );
       
  2412 	        TSmtpAccount defaultSmtp;
       
  2413 	        TInt retVal = emailAccs->DefaultSmtpAccountL( defaultSmtp );
       
  2414 	        if (  retVal != KErrNone )
       
  2415 	            {
       
  2416                 RArray<TImapAccount> imapAccs;
       
  2417                 CleanupClosePushL( imapAccs );
       
  2418                 emailAccs->GetImapAccountsL( imapAccs );
       
  2419                 if ( imapAccs.Count() > 0 )
       
  2420                     {
       
  2421                     TSmtpAccount relatedSmtp;
       
  2422                     emailAccs->GetSmtpAccountL( imapAccs[0].iSmtpService, relatedSmtp );
       
  2423 	                emailAccs->SetDefaultSmtpAccountL( relatedSmtp );
       
  2424                     }
       
  2425                 CleanupStack::PopAndDestroy( &imapAccs );
       
  2426 	            }	                
       
  2427             }
       
  2428 
       
  2429         }
       
  2430     luid = thisElem.iLuid;
       
  2431     CleanupStack::PopAndDestroy(); //emailAccs
       
  2432     _DBG_FILE("CNSmlDmEmailAdapter::ValidateAndStoreSettingsL() : end");
       
  2433     return luid;
       
  2434     }
       
  2435 // -----------------------------------------------------------------------------
       
  2436 // void CNSmlDmEmailAdapter::GetIdByLuid( TNSmlEmailAccountType& aAccType,
       
  2437 //                                           TInt& aAccId, 
       
  2438 //                                       const TInt aLuid ) const
       
  2439 // Calculates the account type and id on basis of luid
       
  2440 // -----------------------------------------------------------------------------
       
  2441 void CNSmlDmEmailAdapter::GetIdByLuid( TNSmlEmailAccountType& aAccType,
       
  2442                                        TInt& aAccId, 
       
  2443                                        const TInt aLuid ) const
       
  2444     {
       
  2445     if ( ( aLuid > KNSmlDmEmailImapLuidLow ) && 
       
  2446          ( aLuid < KNSmlDmEmailImapLuidHigh ) )
       
  2447         {
       
  2448         aAccType = EImap;
       
  2449         aAccId = aLuid - KNSmlDmEmailImapLuidLow;
       
  2450         }
       
  2451     else if (( aLuid > KNSmlDmEmailImapLuidHigh ) && 
       
  2452              ( aLuid < KNSmlDmEmailPopLuidHigh ))
       
  2453         {
       
  2454         aAccType = EPop;
       
  2455         aAccId = aLuid - KNSmlDmEmailImapLuidHigh;
       
  2456         }
       
  2457     else
       
  2458         {
       
  2459         aAccType = EUnknown;
       
  2460         }
       
  2461 
       
  2462     }
       
  2463 
       
  2464 
       
  2465 // -----------------------------------------------------------------------------
       
  2466 // void CNSmlDmEmailAdapter::SetLuidById( const TNSmlEmailAccountType& aAccType,
       
  2467 // const TInt& aAccId, TInt& aLuid ) const
       
  2468 // Sets the luid on basis of account type and id
       
  2469 // -----------------------------------------------------------------------------
       
  2470 void CNSmlDmEmailAdapter::SetLuidById( const TNSmlEmailAccountType& aAccType,
       
  2471                                        const TInt& aAccId, TInt& aLuid ) const
       
  2472     {
       
  2473     switch (aAccType)
       
  2474         {
       
  2475     case EImap :
       
  2476         aLuid = KNSmlDmEmailImapLuidLow + aAccId;
       
  2477         break;
       
  2478 
       
  2479     case EPop :
       
  2480         aLuid = KNSmlDmEmailImapLuidHigh + aAccId;
       
  2481         break;
       
  2482 
       
  2483     default :
       
  2484         aLuid = -1;
       
  2485         break;
       
  2486         }
       
  2487     }
       
  2488 
       
  2489 // -----------------------------------------------------------------------------
       
  2490 // TBool CNSmlDmEmailAdapter::AccountExistsL( const CEmailAccounts* aAccs, 
       
  2491 //                                          const TNSmlEmailAccountType aAccType, 
       
  2492 //                                          const TInt aId,
       
  2493 //                                          TInt aIndex )
       
  2494 // Checks if the accout matching the given parameters is found. 
       
  2495 // returns True if found.
       
  2496 // -----------------------------------------------------------------------------
       
  2497 TBool CNSmlDmEmailAdapter::AccountExistsL( CEmailAccounts* aAccs, 
       
  2498                                            const TNSmlEmailAccountType aAccType, 
       
  2499                                            const TInt aId,
       
  2500                                            TInt aIndex )
       
  2501     {
       
  2502     RArray<TImapAccount> imap4Accs;
       
  2503     CleanupClosePushL( imap4Accs );
       
  2504     RArray<TPopAccount> pop3Accs;
       
  2505     CleanupClosePushL( pop3Accs );
       
  2506     switch ( aAccType )
       
  2507         {
       
  2508     case EImap:
       
  2509         aAccs->GetImapAccountsL(imap4Accs);
       
  2510         for(TInt i=0;i<imap4Accs.Count();i++)
       
  2511             {
       
  2512             if ( imap4Accs[i].iImapAccountId == aId )
       
  2513                 {
       
  2514                 
       
  2515                 if ( aIndex >= 0 )
       
  2516                     {
       
  2517                     iBuffer->At(aIndex).iServiceId = imap4Accs[i].iImapService;
       
  2518                     delete iBuffer->At(aIndex).iName;
       
  2519                     iBuffer->At(aIndex).iName = NULL;
       
  2520                     iBuffer->At(aIndex).iName = HBufC::NewL(imap4Accs[i].iImapAccountName.Size());
       
  2521                     *iBuffer->At(aIndex).iName = imap4Accs[i].iImapAccountName;
       
  2522                     }
       
  2523                 CleanupStack::PopAndDestroy(2); //imap4Accs, pop3Accs
       
  2524                 return ETrue;
       
  2525                 }
       
  2526             }
       
  2527         break;
       
  2528         
       
  2529     case EPop:
       
  2530         aAccs->GetPopAccountsL(pop3Accs);
       
  2531         for(TInt i=0;i<pop3Accs.Count();i++)
       
  2532             {
       
  2533             if ( pop3Accs[i].iPopAccountId == aId )
       
  2534                 {
       
  2535                 if ( aIndex >= 0 )
       
  2536                     {
       
  2537                     iBuffer->At(aIndex).iServiceId = pop3Accs[i].iPopService;                    
       
  2538                     delete iBuffer->At(aIndex).iName;
       
  2539                     iBuffer->At(aIndex).iName = NULL;
       
  2540                     iBuffer->At(aIndex).iName = HBufC::NewL(pop3Accs[i].iPopAccountName.Size());
       
  2541                     *iBuffer->At(aIndex).iName = pop3Accs[i].iPopAccountName;
       
  2542                     }
       
  2543                 CleanupStack::PopAndDestroy(2); //imap4Accs, pop3Accs
       
  2544                 return ETrue;
       
  2545                 }
       
  2546             }
       
  2547         break;
       
  2548     default:
       
  2549         // Returns EFalse in this case.
       
  2550         break;
       
  2551         }
       
  2552     CleanupStack::PopAndDestroy(2); //imap4Accs, pop3Accs
       
  2553     return EFalse;
       
  2554     }
       
  2555 
       
  2556 // -----------------------------------------------------------------------------
       
  2557 // TInt CNSmlDmEmailAdapter::DoRestoreSettingsL(TInt aIndex)
       
  2558 // Restores the settings to the adapter from settings store
       
  2559 // -----------------------------------------------------------------------------
       
  2560 TInt CNSmlDmEmailAdapter::DoRestoreSettingsL(TInt aIndex)
       
  2561     {
       
  2562     _DBG_FILE("CNSmlDmEmailAdapter::DoRestoreSettingsL() : begin");
       
  2563     TNSmlEmailSettingsElement& thisElem = iBuffer->At(aIndex);
       
  2564 
       
  2565     CEmailAccounts* emailAccs = CEmailAccounts::NewLC();
       
  2566     TInt err = KErrNone;
       
  2567 
       
  2568     // These are destroyed latest at destructor
       
  2569     thisElem.iSmtpSettings = new (ELeave) CImSmtpSettings;
       
  2570     thisElem.iIapPref = CImIAPPreferences::NewLC();
       
  2571     thisElem.iSmtpIapPref = CImIAPPreferences::NewLC();
       
  2572 
       
  2573     switch( thisElem.iAccType )
       
  2574         {
       
  2575     case EImap:
       
  2576         if (AccountExistsL(emailAccs, thisElem.iAccType, thisElem.iAccId, aIndex ))
       
  2577             {
       
  2578             TImapAccount imapAccId;
       
  2579             
       
  2580             thisElem.iImap4Settings = new(ELeave) CImImap4Settings;
       
  2581             
       
  2582             //Get IMAP account by iAccId id
       
  2583             emailAccs->GetImapAccountL(thisElem.iServiceId, imapAccId);
       
  2584             //Get IMAP settings by account id
       
  2585             emailAccs->LoadImapSettingsL( imapAccId, *thisElem.iImap4Settings );
       
  2586             //Get IMAP settings IAP prefs
       
  2587             emailAccs->LoadImapIapSettingsL( imapAccId, *thisElem.iIapPref );
       
  2588             
       
  2589             TSmtpAccount smtpAccount;
       
  2590             //Get smtp account by related IMAP id
       
  2591             emailAccs->GetSmtpAccountL( imapAccId.iSmtpService, smtpAccount );  
       
  2592             //Get smtp settings by IMAP id                                        
       
  2593             emailAccs->LoadSmtpSettingsL( smtpAccount, *thisElem.iSmtpSettings );
       
  2594             //Get Smtp settings IAP prefs
       
  2595             emailAccs->LoadSmtpIapSettingsL( smtpAccount, *thisElem.iSmtpIapPref );
       
  2596             }
       
  2597         else
       
  2598             {
       
  2599             err = KErrNotFound;
       
  2600             }
       
  2601         break;
       
  2602 
       
  2603     case EPop:
       
  2604         if (AccountExistsL(emailAccs, thisElem.iAccType, thisElem.iAccId, aIndex ))
       
  2605             {
       
  2606             TPopAccount popAccId;
       
  2607             
       
  2608             thisElem.iPop3Settings = new(ELeave) CImPop3Settings;
       
  2609             
       
  2610             //Get POP3 account by iAccId id
       
  2611             emailAccs->GetPopAccountL( thisElem.iServiceId, popAccId );
       
  2612             //Get POP3 settings by account id
       
  2613             emailAccs->LoadPopSettingsL( popAccId, *thisElem.iPop3Settings );
       
  2614             //Get POP3 setting IAP prefs
       
  2615             emailAccs->LoadPopIapSettingsL( popAccId, *thisElem.iIapPref );
       
  2616                         
       
  2617             TSmtpAccount smtpAccount;
       
  2618             //Get smtp account by related POP id
       
  2619             emailAccs->GetSmtpAccountL( popAccId.iSmtpService, smtpAccount );  
       
  2620             //Get smtp settings                                        
       
  2621             emailAccs->LoadSmtpSettingsL( smtpAccount, *thisElem.iSmtpSettings );
       
  2622             //Get Smtp setting IAP prefs
       
  2623             emailAccs->LoadSmtpIapSettingsL( smtpAccount, *thisElem.iSmtpIapPref );
       
  2624             }
       
  2625         else
       
  2626             {
       
  2627             err = KErrNotFound;
       
  2628             }
       
  2629         break;
       
  2630 
       
  2631     default:
       
  2632         err = KErrNotFound;
       
  2633         break;
       
  2634         }
       
  2635 
       
  2636     if ( err )
       
  2637         {
       
  2638         _DBG_FILE("CNSmlDmEmailAdapter::DoRestoreSettingsL() : \
       
  2639          KErrNotFound end");
       
  2640         CleanupStack::PopAndDestroy(2); // iIapPref, iSmtpIapPref
       
  2641         CleanupStack::PopAndDestroy(emailAccs); //emailAccs
       
  2642         return KErrNotFound;
       
  2643         }
       
  2644 
       
  2645     CleanupStack::Pop(2); // iIapPref, iSmtpIapPref
       
  2646     CleanupStack::PopAndDestroy(emailAccs); //emailAccs
       
  2647     _DBG_FILE("CNSmlDmEmailAdapter::DoRestoreSettingsL() : end");
       
  2648     return KErrNone;
       
  2649     }
       
  2650 
       
  2651 // -----------------------------------------------------------------------------
       
  2652 // CNSmlDmEmailAdapter::OpenSessionL()
       
  2653 // Opens the CMsvSession if it is not open already
       
  2654 // -----------------------------------------------------------------------------
       
  2655 TInt CNSmlDmEmailAdapter::OpenSessionL()
       
  2656     {
       
  2657     _DBG_FILE("CNSmlDmEmailAdapter::OpenSessionL() : begin");
       
  2658     if(!iMsvSession)
       
  2659         {
       
  2660         TRAPD(err, iMsvSession = CMsvSession::OpenSyncL(*this));
       
  2661         _DBG_FILE("CNSmlDmEmailAdapter::OpenSessionL() : end");
       
  2662         return err;
       
  2663         }
       
  2664 
       
  2665     _DBG_FILE("CNSmlDmEmailAdapter::OpenSessionL() : end");
       
  2666     return KErrNone;
       
  2667     }
       
  2668 
       
  2669 // -----------------------------------------------------------------------------
       
  2670 // CNSmlDmEmailAdapter::FillNodeInfoL()
       
  2671 // Fills the node info in ddf structure
       
  2672 // -----------------------------------------------------------------------------
       
  2673 void CNSmlDmEmailAdapter::FillNodeInfoL( MSmlDmDDFObject& aNode,
       
  2674                                          const TSmlDmAccessTypes& aAccTypes,
       
  2675                                          MSmlDmDDFObject::TOccurence aOccurrence,
       
  2676                                          MSmlDmDDFObject::TScope aScope,
       
  2677                                          MSmlDmDDFObject::TDFFormat aFormat,
       
  2678                                          const TDesC8& aDescription) const
       
  2679     {
       
  2680     aNode.SetAccessTypesL(aAccTypes);
       
  2681     aNode.SetOccurenceL(aOccurrence);
       
  2682     aNode.SetScopeL(aScope);
       
  2683     aNode.SetDFFormatL(aFormat);
       
  2684     if(aFormat!=MSmlDmDDFObject::ENode)
       
  2685         {
       
  2686         aNode.AddDFTypeMimeTypeL(KNSmlDMEmailTextPlain);
       
  2687         }
       
  2688     aNode.SetDescriptionL(aDescription);
       
  2689 
       
  2690     }
       
  2691 
       
  2692 // -----------------------------------------------------------------------------
       
  2693 // TPtrC8 CNSmlDmEmailAdapter::RemoveDotSlash(const TDesC8& aURI)
       
  2694 // return uri without dot and slash in start
       
  2695 // -----------------------------------------------------------------------------
       
  2696 TPtrC8 CNSmlDmEmailAdapter::RemoveDotSlash(const TDesC8& aURI) const
       
  2697     {
       
  2698     if(aURI.Find(KNSmlEmailUriDotSlash)==0)
       
  2699         {
       
  2700         return aURI.Right(aURI.Length()-KNSmlEmailUriDotSlash().Length());
       
  2701         }
       
  2702     else
       
  2703         {
       
  2704         return aURI;
       
  2705         }
       
  2706     }
       
  2707 // -----------------------------------------------------------------------------
       
  2708 // CSmlDmAdapter::TError CNSmlDmEmailAdapter::FetchObjectL(const TDesC8& aURI,
       
  2709 // const TDesC8& aLUID, CBufBase& aObject)
       
  2710 // Fetches object acording to URI & LUID. Retur codes acording to 
       
  2711 // CSmlDmAdapter::TError.
       
  2712 // -----------------------------------------------------------------------------
       
  2713 CSmlDmAdapter::TError CNSmlDmEmailAdapter::FetchObjectL(const TDesC8& aURI,
       
  2714                                                         const TDesC8& aLUID,
       
  2715                                                         CBufBase& aObject)
       
  2716     {
       
  2717     _DBG_FILE("CNSmlDmEmailAdapter::FetchObjectL() : begin");
       
  2718     CSmlDmAdapter::TError status = EOk;
       
  2719     if(aLUID.Length()==0)
       
  2720         {
       
  2721         _DBG_FILE("CNSmlDmEmailAdapter::FetchObjectL() : ENotFound end");
       
  2722         return CSmlDmAdapter::ENotFound;
       
  2723         }
       
  2724     if( OpenSessionL() != KErrNone )
       
  2725         {
       
  2726         _DBG_FILE("CNSmlDmEmailAdapter::FetchObjectL() : EError end");
       
  2727         return CSmlDmAdapter::EError;
       
  2728         }
       
  2729     TInt luid = DesToInt(aLUID);
       
  2730     TInt index  = -1;
       
  2731     for(TInt i=0;i<iBuffer->Count();i++)
       
  2732         {
       
  2733         if(iBuffer->At(i).iLuid == luid)
       
  2734             {
       
  2735             index = i;
       
  2736             break;
       
  2737             }
       
  2738         }
       
  2739 
       
  2740     if(index<0)
       
  2741         {
       
  2742         TNSmlEmailSettingsElement newNode;
       
  2743         newNode.iImap4Settings = NULL;
       
  2744         newNode.iPop3Settings = NULL;
       
  2745         newNode.iSmtpSettings = NULL;
       
  2746         newNode.iIapPref = NULL;
       
  2747         newNode.iLuid = luid;
       
  2748         newNode.iMappingName = aURI.AllocLC();
       
  2749         newNode.iName = HBufC::NewLC(0);
       
  2750         newNode.iNodeBuf = new (ELeave)
       
  2751             CArrayFixFlat <TNSmlEmailCommandElement> (KNSmlDmEmailGranularity);
       
  2752         newNode.iExecuted = ETrue;
       
  2753         newNode.iJustFetch = ETrue;
       
  2754         GetIdByLuid(newNode.iAccType, newNode.iAccId, luid);
       
  2755         iBuffer->AppendL(newNode);
       
  2756         index = iBuffer->Count() - 1;
       
  2757         if(DoRestoreSettingsL(index)!=KErrNone)
       
  2758             {
       
  2759             CleanupStack::Pop(2); //newNode.iMappingName,newNode.iName
       
  2760             _DBG_FILE("CNSmlDmEmailAdapter::FetchObjectL() : EError end");
       
  2761             return CSmlDmAdapter::EError;
       
  2762             }
       
  2763         CleanupStack::Pop(2); //newNode.iMappingName,newNode.iName
       
  2764         }
       
  2765 
       
  2766     TPtrC8 lastUriSeg = LastURISeg(aURI);
       
  2767 
       
  2768     if(lastUriSeg.Compare(KNSmlDMEmailName)==0)
       
  2769         {
       
  2770         HBufC8 *data = HBufC8::NewLC(iBuffer->At(index).iName->Length());
       
  2771         TPtr8 dataPtr = data->Des();
       
  2772         CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr, iBuffer->At(index).iName->Des() );
       
  2773         aObject.InsertL(0,dataPtr);
       
  2774         CleanupStack::PopAndDestroy(); //data
       
  2775         }
       
  2776     else if(lastUriSeg.Compare(KNSmlDMEmailUID)==0)
       
  2777         {
       
  2778         HBufC *data = NULL;
       
  2779         if(iBuffer->At(index).iPop3Settings!= NULL)
       
  2780             {
       
  2781             data = HBufC::NewLC(iBuffer->At(index).iPop3Settings->LoginName().Length());
       
  2782             data->Des().Copy(iBuffer->At(index).iPop3Settings->LoginName());
       
  2783             }
       
  2784         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
  2785             {
       
  2786             data = HBufC::NewLC(iBuffer->At(index).iImap4Settings->LoginName().Length());
       
  2787             data->Des().Copy(iBuffer->At(index).iImap4Settings->LoginName());
       
  2788             }
       
  2789         if(data)
       
  2790             {
       
  2791             HBufC8 *data8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*data);
       
  2792             CleanupStack::PushL(data8);
       
  2793             aObject.InsertL(0,*data8);
       
  2794             CleanupStack::PopAndDestroy(2);//data, data8
       
  2795             }
       
  2796         }
       
  2797     else if(lastUriSeg.Compare(KNSmlDMEmailPW)==0)
       
  2798         {
       
  2799         HBufC *data = NULL;
       
  2800         if(iBuffer->At(index).iPop3Settings!= NULL)
       
  2801             {
       
  2802             data = HBufC::NewLC(iBuffer->At(index).iPop3Settings->Password().Length());
       
  2803             data->Des().Copy(iBuffer->At(index).iPop3Settings->Password());
       
  2804             }
       
  2805         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
  2806             {
       
  2807             data = HBufC::NewLC(iBuffer->At(index).iImap4Settings->Password().Length());
       
  2808             data->Des().Copy(iBuffer->At(index).iImap4Settings->Password());
       
  2809             }
       
  2810         if(data)
       
  2811             {
       
  2812             HBufC8 *data8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*data);
       
  2813             CleanupStack::PushL(data8);
       
  2814             aObject.InsertL(0,*data8);
       
  2815             CleanupStack::PopAndDestroy(2);//data, data8
       
  2816             }
       
  2817         }
       
  2818     else if(lastUriSeg.Compare(KNSmlDMEmailUAddr)==0)
       
  2819         {
       
  2820         HBufC *data = NULL;
       
  2821         data = iBuffer->At(index).iSmtpSettings->EmailAddress().AllocLC();
       
  2822         HBufC8 *data8 = HBufC8::NewLC(data->Size());
       
  2823         TPtr8 dataPtr8 = data8->Des();
       
  2824         CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr8, data->Des() );
       
  2825 
       
  2826         aObject.InsertL(0,dataPtr8);
       
  2827         CleanupStack::PopAndDestroy(2); //data, data8
       
  2828         }
       
  2829     else if(lastUriSeg.Compare(KNSmlDMEmailUName)==0)
       
  2830         {
       
  2831 
       
  2832         HBufC *data = NULL;
       
  2833         data = iBuffer->At(index).iSmtpSettings->EmailAlias().AllocLC();
       
  2834         HBufC8 *data8 = HBufC8::NewLC(data->Size());
       
  2835         TPtr8 dataPtr8 = data8->Des();
       
  2836         CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr8, data->Des() );
       
  2837 
       
  2838         aObject.InsertL(0,dataPtr8);
       
  2839         CleanupStack::PopAndDestroy(2); //data, data8
       
  2840         }
       
  2841 
       
  2842     else if(lastUriSeg.Compare( KNSmlDMEmailMrcv)==0)
       
  2843         {
       
  2844         HBufC *data = NULL;
       
  2845         if(iBuffer->At(index).iPop3Settings!= NULL)
       
  2846             {
       
  2847             data = iBuffer->At(index).iPop3Settings->ServerAddress().AllocLC();
       
  2848             }
       
  2849         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
  2850             {
       
  2851             data = iBuffer->At(index).iImap4Settings->ServerAddress().AllocLC();
       
  2852             }
       
  2853         else
       
  2854         	{
       
  2855           	status = CSmlDmAdapter::ENotFound;
       
  2856           	return status;            
       
  2857         	}
       
  2858         HBufC8 *data8 = HBufC8::NewLC(data->Size());
       
  2859         TPtr8 dataPtr8 = data8->Des();
       
  2860         CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr8, data->Des() );
       
  2861 
       
  2862         aObject.InsertL(0,dataPtr8);
       
  2863         CleanupStack::PopAndDestroy(2); //data, data8
       
  2864         }
       
  2865     else if(lastUriSeg.Compare(KNSmlDMEmailMsnd)==0)
       
  2866         {
       
  2867         HBufC *data = NULL;
       
  2868         data = iBuffer->At(index).iSmtpSettings->ServerAddress().AllocLC();
       
  2869         HBufC8 *data8 = HBufC8::NewLC(data->Size());
       
  2870         TPtr8 dataPtr8 = data8->Des();
       
  2871         CnvUtfConverter::ConvertFromUnicodeToUtf8( dataPtr8, data->Des() );
       
  2872 
       
  2873         aObject.InsertL(0,dataPtr8);
       
  2874         CleanupStack::PopAndDestroy(2); //data, data8
       
  2875         }
       
  2876     else if(lastUriSeg.Compare(KNSmlDMEmailMpro)==0)
       
  2877         {
       
  2878         if(iBuffer->At(index).iPop3Settings!= NULL)
       
  2879             {
       
  2880             aObject.InsertL(0,KNSmlDMEmailMproPOP);
       
  2881             }
       
  2882         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
  2883             {
       
  2884             aObject.InsertL(0,KNSmlDMEmailMproIMAP);
       
  2885             }
       
  2886         }
       
  2887     else if(lastUriSeg.Compare(KNSmlDMEmailUseSecCon)==0)
       
  2888         {
       
  2889         FetchSecurityValueL(aObject, &iBuffer->At(index), EUseSecCon );
       
  2890         }
       
  2891 
       
  2892     else if(lastUriSeg.Compare(KNSmlDMEmailUseSauth)==0)
       
  2893         {
       
  2894         if(iBuffer->At(index).iSmtpSettings->SMTPAuth())
       
  2895             {
       
  2896             aObject.InsertL(0,KNSmlDMEmailTrue);
       
  2897             }
       
  2898         else
       
  2899             {
       
  2900             aObject.InsertL(0,KNSmlDMEmailFalse);
       
  2901             }
       
  2902         }
       
  2903     else if(lastUriSeg.Compare(KNSmlDMEmailSauthUID)==0)
       
  2904         {
       
  2905         HBufC *data = HBufC::NewLC(iBuffer->At(index).iSmtpSettings->LoginName().Length());
       
  2906         data->Des().Copy(iBuffer->At(index).iSmtpSettings->LoginName());
       
  2907         HBufC8 *data8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*data);
       
  2908         CleanupStack::PushL(data8);
       
  2909         aObject.InsertL(0,*data8);
       
  2910         CleanupStack::PopAndDestroy(2);//data, data8
       
  2911         }
       
  2912     else if(lastUriSeg.Compare(KNSmlDMEmailSauthPW)==0)
       
  2913         {
       
  2914         HBufC *data = HBufC::NewLC(iBuffer->At(index).iSmtpSettings->Password().Length());
       
  2915         data->Des().Copy(iBuffer->At(index).iSmtpSettings->Password());
       
  2916         HBufC8 *data8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*data);
       
  2917         CleanupStack::PushL(data8);
       
  2918         aObject.InsertL(0,*data8);
       
  2919         CleanupStack::PopAndDestroy(2);//data, data8
       
  2920         }
       
  2921     else if(lastUriSeg.Compare(KNSmlDMEmailPtxtSAuth)==0)
       
  2922         {
       
  2923         if(iBuffer->At(index).iSmtpSettings->InboxLoginDetails())
       
  2924             {
       
  2925             aObject.InsertL(0,KNSmlDMEmailTrue);
       
  2926             }
       
  2927         else
       
  2928             {
       
  2929             aObject.InsertL(0,KNSmlDMEmailFalse);
       
  2930             }
       
  2931         }
       
  2932 
       
  2933     else if (lastUriSeg.Compare(KNSmlDMEmailSUseSecCon)==0)
       
  2934         {
       
  2935         FetchSecurityValueL(aObject, &iBuffer->At(index), ESuseSecCon );
       
  2936         }
       
  2937 
       
  2938     else if (lastUriSeg.Compare(KNSmlDMEmailUseStartTLS)==0)
       
  2939         {
       
  2940         FetchSecurityValueL(aObject, &iBuffer->At(index), EUseStartTls );
       
  2941         }
       
  2942 
       
  2943     else if (lastUriSeg.Compare(KNSmlDMEmailSUseStartTLS)==0)
       
  2944         {
       
  2945         FetchSecurityValueL(aObject, &iBuffer->At(index), ESuseStartTls );
       
  2946         }
       
  2947 
       
  2948     else if(lastUriSeg.Compare(KNSmlDMEmailMrcvPort)==0)
       
  2949         {
       
  2950         TUint port;
       
  2951         if(iBuffer->At(index).iPop3Settings!= NULL)
       
  2952             {
       
  2953             port = iBuffer->At(index).iPop3Settings->Port();
       
  2954             }
       
  2955         else 
       
  2956             {
       
  2957             port = iBuffer->At(index).iImap4Settings->Port();
       
  2958             }
       
  2959         HBufC8 *data = IntToDes8LC(port);
       
  2960         aObject.InsertL(0,data->Des());
       
  2961         CleanupStack::PopAndDestroy(); //data
       
  2962         }
       
  2963 
       
  2964     else if(lastUriSeg.Compare(KNSmlDMEmailMsndPort)==0)
       
  2965         {
       
  2966         TUint port = iBuffer->At(index).iSmtpSettings->Port();
       
  2967         HBufC8 *data = IntToDes8LC(port);
       
  2968         aObject.InsertL(0,data->Des());
       
  2969         CleanupStack::PopAndDestroy(); //data
       
  2970         }
       
  2971 
       
  2972     else if(lastUriSeg.Compare(KNSmlDMEmailUseAPOP)==0)
       
  2973         {
       
  2974         if(iBuffer->At(index).iPop3Settings!= NULL)
       
  2975             {
       
  2976             if(iBuffer->At(index).iPop3Settings->Apop())
       
  2977                 {
       
  2978                 aObject.InsertL(0,KNSmlDMEmailTrue);
       
  2979                 }
       
  2980             else
       
  2981                 {
       
  2982                 aObject.InsertL(0,KNSmlDMEmailFalse);
       
  2983                 }
       
  2984             }
       
  2985         else if(iBuffer->At(index).iImap4Settings!= NULL)
       
  2986             {
       
  2987             status = CSmlDmAdapter::ENotFound;
       
  2988             }
       
  2989         }
       
  2990 
       
  2991     else if (lastUriSeg.Compare(KNSmlDMEmailFolderPath)==0)
       
  2992         {
       
  2993         if( iBuffer->At(index).iImap4Settings )
       
  2994             {
       
  2995             aObject.InsertL(0, iBuffer->At(index).iImap4Settings->FolderPath() );
       
  2996             }
       
  2997         else
       
  2998             {
       
  2999             status = CSmlDmAdapter::ENotFound;
       
  3000             }
       
  3001         }
       
  3002 
       
  3003     else if(lastUriSeg.Compare(KNSmlDMEmailToNapID)==0)
       
  3004         {
       
  3005         CNSmlDMIAPMatcher* iapmatch=CNSmlDMIAPMatcher::NewL( &Callback() );
       
  3006         CleanupStack::PushL(iapmatch);
       
  3007         HBufC8* uri8 = NULL;
       
  3008         TInt iapCount = iBuffer->At(index).iIapPref->NumberOfIAPs();
       
  3009         if ( iapCount )
       
  3010             {
       
  3011             TImIAPChoice localIAP = iBuffer->At(index).iIapPref->IAPPreference(0);
       
  3012             uri8 = iapmatch->URIFromIAPIdL( localIAP.iIAP );
       
  3013             }
       
  3014 
       
  3015         if( uri8 )
       
  3016             {
       
  3017             CleanupStack::PushL(uri8);
       
  3018             aObject.InsertL(0,uri8->Des());
       
  3019             status = CSmlDmAdapter::EOk;
       
  3020             CleanupStack::PopAndDestroy(); // uri8
       
  3021             }
       
  3022         else
       
  3023             {
       
  3024             status = CSmlDmAdapter::ENotFound;
       
  3025             }
       
  3026 
       
  3027         CleanupStack::PopAndDestroy(); // iapMatch
       
  3028         }
       
  3029     else if(lastUriSeg.Compare(KNSmlDMEmailSToNapID)==0)
       
  3030         {
       
  3031         // SToNapID exists only for SMTP
       
  3032         if (iBuffer->At(index).iSmtpSettings!= NULL)
       
  3033             {
       
  3034             CNSmlDMIAPMatcher* iapmatch=CNSmlDMIAPMatcher::NewL( &Callback() );
       
  3035             CleanupStack::PushL(iapmatch);
       
  3036             HBufC8* uri8 = NULL;
       
  3037             TInt iapCount = iBuffer->At(index).iIapPref->NumberOfIAPs();
       
  3038             if ( iapCount )
       
  3039                 {
       
  3040                 TImIAPChoice localIAP = iBuffer->At(index).iSmtpIapPref->IAPPreference(0);
       
  3041                 uri8 = iapmatch->URIFromIAPIdL( localIAP.iIAP );
       
  3042                 }
       
  3043 
       
  3044             if( uri8 )
       
  3045                 {
       
  3046                 CleanupStack::PushL(uri8);
       
  3047                 aObject.InsertL(0,uri8->Des());
       
  3048                 status = CSmlDmAdapter::EOk;
       
  3049                 CleanupStack::PopAndDestroy(); // uri8
       
  3050                 }
       
  3051             else
       
  3052                 {
       
  3053                 status = CSmlDmAdapter::ENotFound;
       
  3054                 }
       
  3055 
       
  3056             CleanupStack::PopAndDestroy(); // iapMatch
       
  3057             }
       
  3058         else
       
  3059             {
       
  3060             status = CSmlDmAdapter::ENotFound;
       
  3061             }
       
  3062         }
       
  3063     _DBG_FILE("CNSmlDmEmailAdapter::FetchObjectL() : end");
       
  3064     return status;
       
  3065     }
       
  3066 
       
  3067 // -----------------------------------------------------------------------------
       
  3068 // void CNSmlDmEmailAdapter::SetDefaultSecurityL(const TDesC8& aObject,
       
  3069 // const TNSmlEmailSettingsElement* thisElem,
       
  3070 // const TNSmlEmailSecuritySetting aSetting )
       
  3071 // Sets (on/off) the security modes: SSL/TLS, StartTLS
       
  3072 // -----------------------------------------------------------------------------
       
  3073 void CNSmlDmEmailAdapter::SetDefaultSecurityL(const TDesC8& aObject,
       
  3074                                               const TNSmlEmailSettingsElement* aThisElem,
       
  3075                                               const TNSmlEmailSecuritySetting aSetting
       
  3076     ) const
       
  3077     {
       
  3078     _DBG_FILE("CNSmlDmEmailAdapter::SetDefaultSecurityL(): begin");
       
  3079     HBufC8 *data = aObject.AllocLC();
       
  3080     TPtr8 dataPtr = data->Des();
       
  3081     dataPtr.Capitalize();
       
  3082     TBool object=EFalse;
       
  3083     if(dataPtr.Find(KNSmlDMEmailTrue)>=0)
       
  3084         {
       
  3085         object = ETrue;
       
  3086         }
       
  3087     switch ( aSetting )
       
  3088         {
       
  3089     case EUseSecCon :
       
  3090         if ( object )
       
  3091             {
       
  3092             if ( aThisElem->iImap4Settings )
       
  3093                 {
       
  3094                 aThisElem->iImap4Settings->SetSecureSockets(EFalse);
       
  3095                 aThisElem->iImap4Settings->SetSSLWrapper(ETrue);
       
  3096                 aThisElem->iImap4Settings->SetPort( KNSmlDMEmailSslWrappedImap4Port );
       
  3097                 }
       
  3098             else if ( aThisElem->iPop3Settings )
       
  3099                 {
       
  3100                 aThisElem->iPop3Settings->SetSecureSockets(EFalse);
       
  3101                 aThisElem->iPop3Settings->SetSSLWrapper(ETrue);
       
  3102                 aThisElem->iPop3Settings->SetPort( KNSmlDMEmailSslWrappedPop3Port );
       
  3103                 }
       
  3104             }
       
  3105         else
       
  3106             {
       
  3107             if ( aThisElem->iImap4Settings )
       
  3108                 {
       
  3109                 aThisElem->iImap4Settings->SetPort( KNSmlDMEmailNormalImap4Port );
       
  3110                 aThisElem->iImap4Settings->SetSSLWrapper(EFalse);
       
  3111                 }
       
  3112             else if ( aThisElem->iPop3Settings )
       
  3113                 {
       
  3114                 aThisElem->iPop3Settings->SetPort( KNSmlDMEmailNormalPop3Port );
       
  3115                 aThisElem->iPop3Settings->SetSSLWrapper(EFalse);
       
  3116                 }
       
  3117             }
       
  3118         break;
       
  3119     case EUseStartTls :
       
  3120          if ( object )
       
  3121             {
       
  3122            if ( aThisElem->iImap4Settings )
       
  3123                 {
       
  3124                 aThisElem->iImap4Settings->SetSecureSockets(ETrue);
       
  3125                 aThisElem->iImap4Settings->SetSSLWrapper(EFalse);
       
  3126                 aThisElem->iImap4Settings->SetPort( KNSmlDMEmailNormalImap4Port );
       
  3127                 }
       
  3128             else if ( aThisElem->iPop3Settings )
       
  3129                 {
       
  3130                 aThisElem->iPop3Settings->SetSecureSockets(ETrue);
       
  3131                 aThisElem->iPop3Settings->SetSSLWrapper(EFalse);
       
  3132                 aThisElem->iPop3Settings->SetPort( KNSmlDMEmailNormalPop3Port );
       
  3133                 }
       
  3134             }
       
  3135         else
       
  3136             {
       
  3137             if ( aThisElem->iImap4Settings )
       
  3138                 {
       
  3139                 aThisElem->iImap4Settings->SetSecureSockets(EFalse);
       
  3140                 }
       
  3141             else if ( aThisElem->iPop3Settings )
       
  3142                 {
       
  3143                 aThisElem->iPop3Settings->SetSecureSockets(EFalse);
       
  3144                 }
       
  3145             }
       
  3146         break;
       
  3147     case ESuseSecCon :
       
  3148         if ( object )
       
  3149             {
       
  3150             // The port is same in all cases
       
  3151             aThisElem->iSmtpSettings->SetSecureSockets(EFalse);
       
  3152             aThisElem->iSmtpSettings->SetSSLWrapper(ETrue);
       
  3153             aThisElem->iSmtpSettings->SetPort( KNSmlDMEmailSslSmtpPort );
       
  3154             }
       
  3155         else
       
  3156             {
       
  3157             aThisElem->iSmtpSettings->SetPort( KNSmlDMEmailNormalSmtpPort );
       
  3158             aThisElem->iSmtpSettings->SetSSLWrapper( EFalse );
       
  3159             }
       
  3160         break;
       
  3161     case ESuseStartTls :
       
  3162         if ( object )
       
  3163             {
       
  3164             // The port is same in all cases
       
  3165             aThisElem->iSmtpSettings->SetSecureSockets(ETrue);
       
  3166             aThisElem->iSmtpSettings->SetSSLWrapper(EFalse);
       
  3167             aThisElem->iSmtpSettings->SetPort( KNSmlDMEmailNormalSmtpPort );
       
  3168             }
       
  3169         else
       
  3170             {
       
  3171             aThisElem->iSmtpSettings->SetSecureSockets( EFalse );
       
  3172             }
       
  3173 
       
  3174         break;
       
  3175     default :
       
  3176         break;
       
  3177         }
       
  3178     CleanupStack::PopAndDestroy(); // object
       
  3179     _DBG_FILE("CNSmlDmEmailAdapter::SetDefaultSecurityL(): end");
       
  3180     }
       
  3181 
       
  3182 // -----------------------------------------------------------------------------
       
  3183 // CSmlDmAdapter::TError CNSmlDmEmailAdapter::FetchSecurityValueL(
       
  3184 //                               CBufBase& aObject,
       
  3185 //                               const TNSmlEmailSettingsElement* aElem,
       
  3186 //                               const TNSmlEmailSecuritySetting aSetting )
       
  3187 // Gets the status(on/off) of security modes  SSL/TLS or StartTLS
       
  3188 // -----------------------------------------------------------------------------
       
  3189 
       
  3190 CSmlDmAdapter::TError CNSmlDmEmailAdapter::FetchSecurityValueL(
       
  3191     CBufBase& aObject,
       
  3192     const TNSmlEmailSettingsElement* aElem,
       
  3193     const TNSmlEmailSecuritySetting aSetting ) const
       
  3194     {
       
  3195     _DBG_FILE("CNSmlDmEmailAdapter::FetchSecurityValueL(): start");
       
  3196     TBool result=EFalse;
       
  3197     CSmlDmAdapter::TError status = CSmlDmAdapter::EOk;
       
  3198     switch ( aSetting )
       
  3199         {
       
  3200 
       
  3201         case EUseStartTls :
       
  3202                 if ( aElem->iImap4Settings)
       
  3203                     {
       
  3204                     result = aElem->iImap4Settings->SecureSockets();
       
  3205                     }
       
  3206                 else if(aElem->iPop3Settings)
       
  3207                     {
       
  3208                     result = aElem->iPop3Settings->SecureSockets();
       
  3209                     }
       
  3210                 else
       
  3211                     {
       
  3212                     status = CSmlDmAdapter::EError;
       
  3213                     }
       
  3214                 break;
       
  3215         case EUseSecCon :
       
  3216                 if ( aElem->iImap4Settings)
       
  3217                     {
       
  3218                     result = aElem->iImap4Settings->SSLWrapper();
       
  3219                     }
       
  3220                 else if(aElem->iPop3Settings)
       
  3221                     {
       
  3222                     result = aElem->iPop3Settings->SSLWrapper();
       
  3223                     }
       
  3224                 else
       
  3225                     {
       
  3226                     status = CSmlDmAdapter::EError;
       
  3227                     }
       
  3228                 break;
       
  3229         case ESuseStartTls :
       
  3230                 if(aElem->iSmtpSettings)
       
  3231                     {
       
  3232                     result = aElem->iSmtpSettings->SecureSockets();
       
  3233                     }
       
  3234                 else
       
  3235                     {
       
  3236                     status = CSmlDmAdapter::EError;
       
  3237                     }
       
  3238                 break;
       
  3239         case ESuseSecCon :
       
  3240                 if(aElem->iSmtpSettings)
       
  3241                     {
       
  3242                     result = aElem->iSmtpSettings->SSLWrapper();
       
  3243                     }
       
  3244                 else
       
  3245                     {
       
  3246                     status = CSmlDmAdapter::EError;
       
  3247                     }
       
  3248                 break;
       
  3249         default:
       
  3250             status = CSmlDmAdapter::EError;
       
  3251             break;
       
  3252         }
       
  3253         
       
  3254     if ( result )
       
  3255         {
       
  3256         aObject.InsertL(0,KNSmlDMEmailTrue);
       
  3257         }
       
  3258     else 
       
  3259         {
       
  3260         aObject.InsertL(0,KNSmlDMEmailFalse);
       
  3261         }
       
  3262     _DBG_FILE("CNSmlDmEmailAdapter::FetchSecurityValueL(): end");
       
  3263     return status;
       
  3264     }
       
  3265 
       
  3266 //--------------------------------------------------------------------
       
  3267 //TInt CNSmlDmEmailAdapter::ConstructTreeL(const TDesC8& aURI)
       
  3268 //
       
  3269 //-------------------------------------------------------------------
       
  3270 
       
  3271 TInt CNSmlDmEmailAdapter::ConstructTreeL(const TDesC8& aURI)
       
  3272 {
       
  3273 	TInt ret = KErrNotFound;
       
  3274 	CEmailAccounts* emailAccs = CEmailAccounts::NewLC();
       
  3275 	
       
  3276     if(aURI.Find(KNSmlDMEmailNodeName)!= KErrNotFound)
       
  3277         {
       
  3278         RArray<TImapAccount> imap4Accounts;
       
  3279         CleanupClosePushL( imap4Accounts );
       
  3280         emailAccs->GetImapAccountsL(imap4Accounts);
       
  3281 
       
  3282         // First, IMAP accounts
       
  3283         //
       
  3284         TInt luid;
       
  3285         TPtrC8 name;
       
  3286         // cycle through every found account
       
  3287         for(TInt count = 0; count < imap4Accounts.Count(); count++)
       
  3288             {
       
  3289             
       
  3290             	SetLuidById( EImap, imap4Accounts[count].iImapAccountId, luid );
       
  3291             	HBufC8 *uri = HBufC8::NewLC(KNSmlDMEmailNodeName().Length() + 
       
  3292                                             KNSmlDMEmailMproIMAP().Length() + 6);
       
  3293                                             
       
  3294                 TPtr8 uriPtr = uri->Des();
       
  3295                 uriPtr.Append(KNSmlDMEmailNodeName);
       
  3296                 uriPtr.Append(KNSmlDMEmailSeparator8);
       
  3297                 uriPtr.Append(KNSmlDMEmailMproIMAP);
       
  3298                 uriPtr.AppendNumFixedWidth(count,EDecimal,3); 
       
  3299                 HBufC8 *luidBuf = IntToDes8LC(luid); 
       
  3300                 HBufC8* mapInfo = Callback().GetLuidAllocL(uriPtr);					
       
  3301 				if (mapInfo->Length() == 0)
       
  3302 				{                             
       
  3303                 	Callback().SetMappingL(uriPtr, luidBuf->Des());
       
  3304                 	if( aURI.Find(uriPtr) >= 0 )
       
  3305         			{ 
       
  3306          				ret = luid;
       
  3307         			}  
       
  3308 				}
       
  3309 				else
       
  3310 				{
       
  3311 					delete mapInfo;
       
  3312 				}
       
  3313 
       
  3314                 CleanupStack::PopAndDestroy(2); //uri,luidBuf   
       
  3315             }
       
  3316         CleanupStack::PopAndDestroy(); //imap4Accounts
       
  3317 
       
  3318         // Then, POP accounts
       
  3319         //
       
  3320         RArray<TPopAccount> pop3Accounts;
       
  3321         CleanupClosePushL( pop3Accounts );
       
  3322         emailAccs->GetPopAccountsL( pop3Accounts );        
       
  3323         // cycle through every found account
       
  3324         for(TInt count = 0; count < pop3Accounts.Count(); count++)
       
  3325             {
       
  3326             	SetLuidById( EPop, pop3Accounts[count].iPopAccountId, luid );
       
  3327                                      
       
  3328                 HBufC8 *uri = HBufC8::NewLC(KNSmlDMEmailNodeName().Length() + 
       
  3329                                             KNSmlDMEmailMproPOP().Length() + 5);
       
  3330                 TPtr8 uriPtr = uri->Des();
       
  3331                 uriPtr.Append(KNSmlDMEmailNodeName);
       
  3332                 uriPtr.Append(KNSmlDMEmailSeparator8);
       
  3333                 uriPtr.Append(KNSmlDMEmailMproPOP);
       
  3334 				uriPtr.AppendNumFixedWidth(count,EDecimal,3); 
       
  3335                 HBufC8 *luidBuf = IntToDes8LC(luid);
       
  3336                 HBufC8* mapInfo = Callback().GetLuidAllocL(uriPtr);					
       
  3337 				if (mapInfo->Length() == 0)
       
  3338 				{ 
       
  3339                 	Callback().SetMappingL(uriPtr,luidBuf->Des());
       
  3340                 	if( aURI.Find(uriPtr) >= 0 )
       
  3341         			{ 
       
  3342          				ret = luid;
       
  3343         			}  
       
  3344 				}
       
  3345 				else
       
  3346 				{
       
  3347 					delete mapInfo;
       
  3348 				}
       
  3349 
       
  3350                 CleanupStack::PopAndDestroy(2); //uri,luidBuf
       
  3351             }
       
  3352         CleanupStack::PopAndDestroy(); //pop3Accounts
       
  3353         }
       
  3354         CleanupStack::PopAndDestroy(); //emailAccs
       
  3355 		return ret;
       
  3356 }
       
  3357 
       
  3358 //------------------------------------------------------------------------------
       
  3359 // TPtrC8 CNSmlDmEmailAdapter::GetDynamicEmailNodeUri( const TDesC8& aURI )
       
  3360 // returns Email/xxx URI
       
  3361 //------------------------------------------------------------------------------
       
  3362 TPtrC8 CNSmlDmEmailAdapter::GetDynamicEmailNodeUri( const TDesC8& aURI )
       
  3363     {
       
  3364     DBG_ARGS8(_S8("CNSmlDmEmailAdapter::GetDynamicEmailNodeUri() - <%S> "), &aURI);
       
  3365     TInt i= 0;
       
  3366 	TBuf8<50> EmailAccRoot(KNSmlDMEmailNodeName);
       
  3367     for ( i = aURI.Find( KNSmlDMEmailNodeName ) + EmailAccRoot.Length() + 1; 
       
  3368 						i < aURI.Length(); i++ )
       
  3369         {
       
  3370         if( aURI[i] == '/' )
       
  3371             {
       
  3372             break;
       
  3373             }
       
  3374         }        
       
  3375     _DBG_FILE("CNSmlDmEmailAdapter::GetDynamicEmailNodeUri(): end");
       
  3376     return aURI.Left( i );
       
  3377     }
       
  3378 
       
  3379 
       
  3380 // End of File