ipsservices/ipssossettings/src/ipssetutils.cpp
branchRCL_3
changeset 25 3533d4323edc
parent 0 8466d47a6819
equal deleted inserted replaced
24:d189ee25cf9d 25:3533d4323edc
       
     1 /*
       
     2 * Copyright (c) 2007 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: This file implements class IsMailMtm.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "emailtrace.h"
       
    21 #include <miuthdr.h>                // TMsvEmailEntry
       
    22 #include <SendUiConsts.h>
       
    23 #include <smtpset.h>
       
    24 
       
    25 #include "ipssetutilsconsts.h"
       
    26 #include "ipssetutils.h"
       
    27 #include "ipssetdata.h"
       
    28 
       
    29 _LIT( KIpsSetUtilsRfc2822Specials,"()<>@,;:\\\"[]");
       
    30 _LIT( KIpsSetUtilsImapInboxName,"INBOX");
       
    31 const TInt KIpsSetUtilsCharQuote = '\"';
       
    32 const TInt KIpsSetUtilsCharBackSlash = '\\';
       
    33 const TInt KIpsSetUtilsCharDot = '.';
       
    34 const TInt KIpsSetUtilsCharSpace = ' ';
       
    35 const TInt KIpsSetUtilsCharDel = 127;
       
    36 const TInt KIpsSetUtilsCharAt = '@';
       
    37 const TInt KIpsSetUtilsSpecialCharStrLen = 12;
       
    38 
       
    39 
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ===============================
       
    42 
       
    43 // ----------------------------------------------------------------------------
       
    44 // IpsSetUtils::IsMailbox()
       
    45 // ----------------------------------------------------------------------------
       
    46 //
       
    47 TBool IpsSetUtils::IsMailbox(
       
    48     const TMsvEntry& aEntry )
       
    49     {
       
    50     FUNC_LOG;
       
    51     return aEntry.iType.iUid == KUidMsvServiceEntryValue &&
       
    52         IsMailMtm( aEntry.iMtm );
       
    53     }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // IpsSetUtils::IsMailMtm()
       
    57 // ----------------------------------------------------------------------------
       
    58 //
       
    59 TBool IpsSetUtils::IsMailMtm( const TUid& aMtm )
       
    60     {
       
    61     FUNC_LOG;
       
    62     // Basic mail mtm's
       
    63     return
       
    64         ( aMtm == KSenduiMtmSmtpUid ) ||
       
    65         ( aMtm == KSenduiMtmPop3Uid ) ||
       
    66         ( aMtm == KSenduiMtmImap4Uid );
       
    67     }
       
    68 
       
    69 // ----------------------------------------------------------------------------
       
    70 // IpsSetUtils::GetMailboxEntryL()
       
    71 // ----------------------------------------------------------------------------
       
    72 //
       
    73 TMsvEntry IpsSetUtils::GetMailboxEntryL(
       
    74     CMsvSession& aMsvSession,
       
    75     const TMsvId aMailboxId )
       
    76     {
       
    77     FUNC_LOG;
       
    78     // Get the entry of the mailbox
       
    79     TMsvId serviceId;
       
    80     TMsvEntry mailbox;
       
    81     User::LeaveIfError(
       
    82         aMsvSession.GetEntry( aMailboxId, serviceId, mailbox ) );
       
    83 
       
    84     return mailbox;
       
    85     }
       
    86 
       
    87 // ----------------------------------------------------------------------------
       
    88 // IpsSetUtils::GetMailboxEntry()
       
    89 // ----------------------------------------------------------------------------
       
    90 //
       
    91 TInt IpsSetUtils::GetMailboxEntry(
       
    92     CMsvSession& aMsvSession,
       
    93     const TMsvId aMailboxId,
       
    94     TMsvEntry& aMailbox )
       
    95     {
       
    96     FUNC_LOG;
       
    97     // Get the entry of the mailbox
       
    98     TMsvId serviceId;
       
    99     return aMsvSession.GetEntry( aMailboxId, serviceId, aMailbox );
       
   100     }
       
   101 
       
   102 // ----------------------------------------------------------------------------
       
   103 // IpsSetUtils::GetMailboxEntriesL()
       
   104 // ----------------------------------------------------------------------------
       
   105 //
       
   106 TUid IpsSetUtils::GetMailboxEntriesL(
       
   107     const TMsvId aMailboxId,
       
   108     CMsvSession& aMsvSession,
       
   109     TMsvEntry& aSmtpEntry,
       
   110     TMsvEntry& aRelatedEntry )
       
   111     {
       
   112     FUNC_LOG;
       
   113     // Find the entry
       
   114     TMsvEntry entry = IpsSetUtils::GetMailboxEntryL(
       
   115         aMsvSession, aMailboxId );
       
   116 
       
   117     return GetMailboxEntriesL( entry, aMsvSession, aSmtpEntry, aRelatedEntry );
       
   118     }
       
   119 
       
   120 // ----------------------------------------------------------------------------
       
   121 // IpsSetUtils::GetMailboxEntriesL()
       
   122 // ----------------------------------------------------------------------------
       
   123 //
       
   124 TUid IpsSetUtils::GetMailboxEntriesL(
       
   125     const TMsvEntry& aEntry,
       
   126     CMsvSession& aMsvSession,
       
   127     TMsvEntry& aSmtpEntry,
       
   128     TMsvEntry& aRelatedEntry )
       
   129     {
       
   130     FUNC_LOG;
       
   131     // Make sure the entry is mailbox
       
   132     if ( !IpsSetUtils::IsMailbox( aEntry ) )
       
   133         {
       
   134         User::Leave( KErrUnknown );
       
   135         }
       
   136 
       
   137     // Get the related entry
       
   138     TMsvEntry entry = IpsSetUtils::GetMailboxEntryL(
       
   139         aMsvSession, aEntry.iRelatedId );
       
   140 
       
   141     // Check if the main entry is smtp, and set the entries correctly
       
   142     if ( aEntry.iMtm == KSenduiMtmSmtpUid )
       
   143         {
       
   144         aSmtpEntry = aEntry;
       
   145         aRelatedEntry = entry;
       
   146         }
       
   147     else
       
   148         {
       
   149         aRelatedEntry = aEntry;
       
   150         aSmtpEntry = entry;
       
   151         }
       
   152 
       
   153     // return the mtm id
       
   154     return aRelatedEntry.iMtm;
       
   155     }
       
   156 
       
   157 // ----------------------------------------------------------------------------
       
   158 // IpsSetUtils::GetSmtpAccountIdL()
       
   159 // ----------------------------------------------------------------------------
       
   160 //
       
   161 TSmtpAccount IpsSetUtils::GetSmtpAccountIdL(
       
   162     CMsvSession& aMsvSession,
       
   163     const TMsvId aMailboxId )
       
   164     {
       
   165     FUNC_LOG;
       
   166     TMsvEntry mailbox =
       
   167         IpsSetUtils::GetMailboxEntryL( aMsvSession, aMailboxId );
       
   168     return GetSmtpAccountIdL( aMsvSession, mailbox );
       
   169     }
       
   170 
       
   171 // ----------------------------------------------------------------------------
       
   172 // IpsSetUtils::GetImapAccountIdL()
       
   173 // ----------------------------------------------------------------------------
       
   174 //
       
   175 TImapAccount IpsSetUtils::GetImapAccountIdL(
       
   176     CMsvSession& aMsvSession,
       
   177     const TMsvId aMailboxId )
       
   178     {
       
   179     FUNC_LOG;
       
   180     TMsvEntry mailbox =
       
   181         IpsSetUtils::GetMailboxEntryL( aMsvSession, aMailboxId );
       
   182     return GetImapAccountIdL( aMsvSession, mailbox );
       
   183     }
       
   184 
       
   185 // ----------------------------------------------------------------------------
       
   186 // IpsSetUtils::GetPopAccountIdL()
       
   187 // ----------------------------------------------------------------------------
       
   188 //
       
   189 TPopAccount IpsSetUtils::GetPopAccountIdL(
       
   190     CMsvSession& aMsvSession,
       
   191     const TMsvId aMailboxId )
       
   192     {
       
   193     FUNC_LOG;
       
   194     TMsvEntry mailbox =
       
   195         IpsSetUtils::GetMailboxEntryL( aMsvSession, aMailboxId );
       
   196     return GetPopAccountIdL( aMsvSession, mailbox );
       
   197     }
       
   198 
       
   199 // ----------------------------------------------------------------------------
       
   200 // IpsSetUtils::GetSmtpAccountIdL()
       
   201 // ----------------------------------------------------------------------------
       
   202 //
       
   203 TSmtpAccount IpsSetUtils::GetSmtpAccountIdL(
       
   204     CMsvSession& aMsvSession,
       
   205     const TMsvEntry& aEntry )
       
   206     {
       
   207     FUNC_LOG;
       
   208     TMsvEntry smtpMailbox = aEntry;
       
   209 
       
   210     // Make sure the entry belongs to smtp mailbox
       
   211     if ( aEntry.iMtm != KSenduiMtmSmtpUid )
       
   212         {
       
   213         // Get the related mailbox
       
   214         smtpMailbox = IpsSetUtils::GetMailboxEntryL(
       
   215             aMsvSession, aEntry.iRelatedId );
       
   216         }
       
   217 
       
   218     return IpsSetUtils::GetSmtpAccountIdL( smtpMailbox.Id() );
       
   219     }
       
   220 
       
   221 // ----------------------------------------------------------------------------
       
   222 // IpsSetUtils::GetImapAccountIdL()
       
   223 // ----------------------------------------------------------------------------
       
   224 //
       
   225 TImapAccount IpsSetUtils::GetImapAccountIdL(
       
   226     CMsvSession& aMsvSession,
       
   227     const TMsvEntry& aEntry )
       
   228     {
       
   229     FUNC_LOG;
       
   230     TMsvEntry imapMailbox = aEntry;
       
   231 
       
   232     switch ( aEntry.iMtm.iUid )
       
   233         {
       
   234         // Pop cannot transform to imap
       
   235         case KSenduiMtmPop3UidValue:
       
   236             User::Leave( KErrNotSupported );
       
   237             break;
       
   238 
       
   239         // Smtp need to be changed to related one
       
   240         case KSenduiMtmSmtpUidValue:
       
   241             imapMailbox = IpsSetUtils::GetMailboxEntryL(
       
   242                 aMsvSession, aEntry.iRelatedId );
       
   243             break;
       
   244 
       
   245         // Accept the imap mailbox straight away
       
   246         default:
       
   247         case KSenduiMtmImap4UidValue:
       
   248             break;
       
   249         }
       
   250 
       
   251     // Make sure the entry belongs to imap mailbox
       
   252     if ( aEntry.iMtm.iUid != KSenduiMtmImap4UidValue )
       
   253         {
       
   254         User::Leave( KErrNotSupported );
       
   255         }
       
   256 
       
   257     return IpsSetUtils::GetImapAccountIdL( imapMailbox.Id() );
       
   258     }
       
   259 
       
   260 // ----------------------------------------------------------------------------
       
   261 // IpsSetUtils::GetPopAccountIdL()
       
   262 // ----------------------------------------------------------------------------
       
   263 //
       
   264 TPopAccount IpsSetUtils::GetPopAccountIdL(
       
   265     CMsvSession& aMsvSession,
       
   266     const TMsvEntry& aEntry )
       
   267     {
       
   268     FUNC_LOG;
       
   269     TMsvEntry popMailbox = aEntry;
       
   270 
       
   271     switch ( aEntry.iMtm.iUid )
       
   272         {
       
   273         // Imap cannot transform to pop
       
   274         case KSenduiMtmImap4UidValue:
       
   275             User::Leave( KErrNotSupported );
       
   276             break;
       
   277 
       
   278         // Smtp need to be changed to related one
       
   279         case KSenduiMtmSmtpUidValue:
       
   280             popMailbox = IpsSetUtils::GetMailboxEntryL(
       
   281                 aMsvSession, aEntry.iRelatedId );
       
   282             break;
       
   283 
       
   284         // Accept the pop mailbox straight away
       
   285         default:
       
   286         case KSenduiMtmPop3UidValue:
       
   287             break;
       
   288         }
       
   289 
       
   290     // Make sure the entry belongs to pop mailbox
       
   291     if ( aEntry.iMtm.iUid != KSenduiMtmPop3UidValue )
       
   292         {
       
   293         User::Leave( KErrNotSupported );
       
   294         }
       
   295 
       
   296     return IpsSetUtils::GetPopAccountIdL( popMailbox.Id() );
       
   297     }
       
   298 
       
   299 // ----------------------------------------------------------------------------
       
   300 // IpsSetUtils::GetPopAccountIdL()
       
   301 // ----------------------------------------------------------------------------
       
   302 //
       
   303 TPopAccount IpsSetUtils::GetPopAccountIdL(
       
   304     const TMsvId aPopMailboxId )
       
   305     {
       
   306     FUNC_LOG;
       
   307     CEmailAccounts* account = CEmailAccounts::NewLC();
       
   308 
       
   309     TPopAccount popAccountId;
       
   310     account->GetPopAccountL( aPopMailboxId, popAccountId );
       
   311 
       
   312     CleanupStack::PopAndDestroy( account );
       
   313 
       
   314     return popAccountId;
       
   315     }
       
   316 
       
   317 // ----------------------------------------------------------------------------
       
   318 // IpsSetUtils::GetImapAccountIdL()
       
   319 // ----------------------------------------------------------------------------
       
   320 //
       
   321 TImapAccount IpsSetUtils::GetImapAccountIdL(
       
   322     const TMsvId aImapMailboxId )
       
   323     {
       
   324     FUNC_LOG;
       
   325     CEmailAccounts* account = CEmailAccounts::NewLC();
       
   326 
       
   327     TImapAccount imapAccountId;
       
   328     account->GetImapAccountL( aImapMailboxId, imapAccountId );
       
   329 
       
   330     CleanupStack::PopAndDestroy( account );
       
   331 
       
   332     return imapAccountId;
       
   333     }
       
   334 
       
   335 // ----------------------------------------------------------------------------
       
   336 // IpsSetUtils::GetSmtpAccountIdL()
       
   337 // ----------------------------------------------------------------------------
       
   338 //
       
   339 TSmtpAccount IpsSetUtils::GetSmtpAccountIdL(
       
   340     const TMsvId aSmtpMailboxId )
       
   341     {
       
   342     FUNC_LOG;
       
   343     CEmailAccounts* account = CEmailAccounts::NewLC();
       
   344 
       
   345     TSmtpAccount smtpAccountId;
       
   346     account->GetSmtpAccountL( aSmtpMailboxId, smtpAccountId );
       
   347 
       
   348     CleanupStack::PopAndDestroy( account );
       
   349 
       
   350     return smtpAccountId;
       
   351     }
       
   352 
       
   353 // ----------------------------------------------------------------------------
       
   354 // IpsSetUtils::GetMailboxServiceId()
       
   355 // ----------------------------------------------------------------------------
       
   356 //
       
   357 TInt IpsSetUtils::GetMailboxServiceId(
       
   358     CMsvSession& aMsvSession,
       
   359     TMsvEntry& aMailbox,
       
   360     const TMsvId aMailboxId,
       
   361     const TBool aGetSendingService )
       
   362     {
       
   363     FUNC_LOG;
       
   364     // Get the entry based on mailbox id
       
   365     TInt error = IpsSetUtils::GetMailboxEntry(
       
   366         aMsvSession, aMailboxId, aMailbox );
       
   367     TInt32 mtm = aMailbox.iMtm.iUid;
       
   368 
       
   369     // Continue handling, if successful
       
   370     if ( error == KErrNone && IpsSetUtils::IsMailbox( aMailbox ) )
       
   371         {
       
   372         // Get related entry, if:
       
   373         // Mailbox type is smtp and pop3 or imap4 is wanted OR
       
   374         // Mailbox type is pop3 or imap4 and smtp is wanted
       
   375         if ( aGetSendingService && mtm != KSenduiMtmSmtpUidValue ||
       
   376             !aGetSendingService && mtm == KSenduiMtmSmtpUidValue )
       
   377             {
       
   378             error = IpsSetUtils::GetMailboxEntry(
       
   379                 aMsvSession, aMailbox.iRelatedId, aMailbox );
       
   380             }
       
   381         }
       
   382 
       
   383     return error;
       
   384     }
       
   385 
       
   386 // ----------------------------------------------------------------------------
       
   387 // IpsSetUtils::CountSubscribedFoldersL()
       
   388 // ----------------------------------------------------------------------------
       
   389 //
       
   390 TInt IpsSetUtils::CountSubscribedFoldersL(
       
   391     CMsvSession& aMsvSession,
       
   392     const TMsvId aMailboxId )
       
   393     {
       
   394     FUNC_LOG;
       
   395     TInt count = 0;
       
   396 
       
   397     DoCountSubscribedFoldersL( aMsvSession, aMailboxId, count );
       
   398 
       
   399     return count;
       
   400     }
       
   401 
       
   402 // ----------------------------------------------------------------------------
       
   403 // IpsSetUtils::DoCountSubscribedFoldersL()
       
   404 // ----------------------------------------------------------------------------
       
   405 //
       
   406 void IpsSetUtils::DoCountSubscribedFoldersL(
       
   407     CMsvSession& aMsvSession,
       
   408     const TMsvId aFolderId,
       
   409     TInt& aCount )
       
   410     {
       
   411     FUNC_LOG;
       
   412     // Get the folder entry
       
   413     CMsvEntry* folderEntry = aMsvSession.GetEntryL( aFolderId );
       
   414     CleanupStack::PushL( folderEntry );
       
   415 
       
   416     // Subscribed folders are hidden entries before the first sync
       
   417     TMsvSelectionOrdering order = folderEntry->SortType();
       
   418     order.SetShowInvisibleEntries( ETrue );
       
   419     folderEntry->SetSortTypeL( order );
       
   420 
       
   421     // Search through all the folders and locate the subscribed ones
       
   422     const TInt count = folderEntry->Count();
       
   423     for ( TInt loop = 0; loop < count; loop++ )
       
   424         {
       
   425         TMsvEmailEntry mailEntry = ( *folderEntry )[ loop ];
       
   426         if ( mailEntry.iType.iUid == KUidMsvFolderEntryValue )
       
   427             {
       
   428             if ( mailEntry.LocalSubscription() &&
       
   429                 !IsInbox( aMsvSession, mailEntry) )
       
   430                 {
       
   431                 aCount++;
       
   432                 }
       
   433 
       
   434             // Go into the folder to search the new entry
       
   435             DoCountSubscribedFoldersL( aMsvSession, mailEntry.Id(), aCount );
       
   436             }
       
   437         }
       
   438 
       
   439     CleanupStack::PopAndDestroy( folderEntry );
       
   440     }
       
   441 
       
   442 // ----------------------------------------------------------------------------
       
   443 // IpsSetUtils::HasSubscribedFoldersL()
       
   444 // ----------------------------------------------------------------------------
       
   445 //
       
   446 TBool IpsSetUtils::HasSubscribedFoldersL(
       
   447     CMsvSession& aMsvSession,
       
   448     const TMsvId aFolderId )
       
   449     {
       
   450     FUNC_LOG;
       
   451     // Get the folder entry
       
   452     CMsvEntry* folderEntry = aMsvSession.GetEntryL( aFolderId );
       
   453     CleanupStack::PushL( folderEntry );
       
   454 
       
   455     // Subscribed folders are hidden entries before the first sync
       
   456     TMsvSelectionOrdering order = folderEntry->SortType();
       
   457     order.SetShowInvisibleEntries( ETrue );
       
   458     folderEntry->SetSortTypeL( order );
       
   459 
       
   460     TBool found( EFalse );
       
   461 
       
   462     // Search through all the folders and locate the subscribed ones
       
   463     const TInt count = folderEntry->Count();
       
   464     for ( TInt loop = 0; !found && loop < count; loop++ )
       
   465         {
       
   466         TMsvEmailEntry mailEntry = ( *folderEntry )[ loop ];
       
   467         if ( mailEntry.iType.iUid == KUidMsvFolderEntryValue )
       
   468             {
       
   469             if ( mailEntry.LocalSubscription() &&
       
   470                 !IsInbox( aMsvSession, mailEntry ) )
       
   471                 {
       
   472                 found = ETrue;
       
   473                 }
       
   474             else
       
   475                 {
       
   476                 // Go into the folder to search the new entry
       
   477                 found = HasSubscribedFoldersL( aMsvSession, mailEntry.Id() );
       
   478                 }
       
   479             }
       
   480         }
       
   481 
       
   482     CleanupStack::PopAndDestroy( folderEntry );
       
   483 
       
   484     return found;
       
   485     }
       
   486 
       
   487 
       
   488 // ----------------------------------------------------------------------------
       
   489 // IpsSetUtils::IsInbox()
       
   490 // ----------------------------------------------------------------------------
       
   491 //
       
   492 TBool IpsSetUtils::IsInbox(
       
   493     CMsvSession& aMsvSession,
       
   494     const TMsvEntry& aFolderEntry )
       
   495     {
       
   496     FUNC_LOG;
       
   497     TBool isInbox( EFalse );
       
   498     if ( aFolderEntry.iDetails.CompareF( KIpsSetUtilsImapInboxName ) == 0 )
       
   499         {
       
   500         // If parent is Mailbox service, then this is IMAP4 protocol inbox,
       
   501         // otherwise it is something else...
       
   502         TMsvId serviceId;
       
   503         TMsvEntry entry;
       
   504         TInt ret = aMsvSession.GetEntry(
       
   505             aFolderEntry.Parent(), serviceId, entry );
       
   506         if ( ( ret == KErrNone ) && ( entry.Id() == serviceId ) )
       
   507             {
       
   508             isInbox = ETrue;
       
   509             }
       
   510         }
       
   511     return isInbox;
       
   512     }
       
   513 
       
   514 
       
   515 // ----------------------------------------------------------------------------
       
   516 // IpsSetUtils::IsValidEmailAddressL
       
   517 // ----------------------------------------------------------------------------
       
   518 EXPORT_C TBool IpsSetUtils::IsValidEmailAddressL( const TDesC& aAddress )
       
   519     {
       
   520     FUNC_LOG;
       
   521     TInt c;
       
   522     TInt length = aAddress.Length ();
       
   523     TBufC<KIpsSetUtilsSpecialCharStrLen>
       
   524         rfc822Specials ( KIpsSetUtilsRfc2822Specials );
       
   525 
       
   526     // first we validate the name portion (name@domain)
       
   527     if ( length && aAddress[0] == KIpsSetUtilsCharDot )
       
   528         {
       
   529         return EFalse;
       
   530         }
       
   531     for ( c = 0 ; c < length ; c++ )
       
   532         {
       
   533         if ( aAddress[c] == KIpsSetUtilsCharQuote && ( c == 0 ||
       
   534             aAddress[c-1] == KIpsSetUtilsCharDot ||
       
   535             aAddress[c-1] == KIpsSetUtilsCharQuote ) )
       
   536             {
       
   537             while ( ++c < length )
       
   538                 {
       
   539                 if ( aAddress[c] == KIpsSetUtilsCharQuote )
       
   540                     {
       
   541                     break;
       
   542                     }
       
   543                 if ( aAddress[c] == KIpsSetUtilsCharBackSlash &&
       
   544                     ( aAddress[++c] == KIpsSetUtilsCharSpace) )
       
   545                     {
       
   546                     continue;
       
   547                     }
       
   548                 if ( aAddress[c] <= KIpsSetUtilsCharSpace ||
       
   549                     aAddress[c] >= KIpsSetUtilsCharDel )
       
   550                     {
       
   551                     return EFalse;
       
   552                     }
       
   553                 }
       
   554             if ( c++ == length )
       
   555                 {
       
   556                 return EFalse;
       
   557                 }
       
   558             if ( aAddress[c] == KIpsSetUtilsCharAt )
       
   559                 {
       
   560                 break;
       
   561                 }
       
   562             if ( aAddress[c] != KIpsSetUtilsCharDel )
       
   563                 {
       
   564                 return EFalse;
       
   565                 }
       
   566             continue;
       
   567             }
       
   568         if ( aAddress[c] == KIpsSetUtilsCharAt )
       
   569             {
       
   570             break;
       
   571             }
       
   572         if ( aAddress[c] <= KIpsSetUtilsCharSpace || aAddress[c] >= KIpsSetUtilsCharDel )
       
   573             {
       
   574             return EFalse;
       
   575             }
       
   576         if ( rfc822Specials.Locate ( aAddress[c] ) != KErrNotFound )
       
   577             {
       
   578             return EFalse;
       
   579             }
       
   580         }
       
   581     if ( c == 0 || aAddress[c-1] == KIpsSetUtilsCharDot )
       
   582         {
       
   583         return EFalse;
       
   584         }
       
   585 
       
   586     // next we validate the domain portion (name@domain)
       
   587     if ( c == length )
       
   588         {
       
   589         return EFalse;
       
   590         }
       
   591     else
       
   592         {
       
   593         c++;
       
   594         return IsValidDomainL ( aAddress.Mid ( ( c ) , length-c ) );
       
   595         }
       
   596     }
       
   597 
       
   598 // -----------------------------------------------------------------------------
       
   599 // IpsSetUtils::IsValidDomainL
       
   600 // -----------------------------------------------------------------------------
       
   601 TBool IpsSetUtils::IsValidDomainL ( const TDesC& aDomain )
       
   602     {
       
   603     FUNC_LOG;
       
   604     TInt c = 0;
       
   605     TInt count = 0;
       
   606     TInt length = aDomain.Length ();
       
   607     TBufC<KIpsSetUtilsSpecialCharStrLen>
       
   608         rfc2822Specials( KIpsSetUtilsRfc2822Specials );
       
   609 
       
   610     if ( length == 0 )
       
   611         {
       
   612         return EFalse;
       
   613         }
       
   614 
       
   615     do
       
   616         {
       
   617         if ( aDomain[c] == KIpsSetUtilsCharDot )
       
   618             {
       
   619             if ( c == 0 || aDomain[c-1] == KIpsSetUtilsCharDot )
       
   620                 {
       
   621                 return EFalse;
       
   622                 }
       
   623             count++;
       
   624             }
       
   625         if ( aDomain[c] <= KIpsSetUtilsCharSpace ||
       
   626              aDomain[c] >= KIpsSetUtilsCharDel )
       
   627             {
       
   628             return EFalse;
       
   629             }
       
   630         if ( rfc2822Specials.Locate( aDomain[c] ) != KErrNotFound )
       
   631             {
       
   632             return EFalse;
       
   633             }
       
   634         }
       
   635     while ( ++c < length );
       
   636 
       
   637     return ( ( count >= 1 ) && ( aDomain[length-1] != '.' ) );
       
   638     }
       
   639 
       
   640 // ----------------------------------------------------------------------------
       
   641 // IpsSetUtils::GetRelatedMailboxId()
       
   642 // ----------------------------------------------------------------------------
       
   643 //
       
   644 TInt IpsSetUtils::GetRelatedMailboxId(
       
   645     CMsvSession& aMsvSession,
       
   646     TMsvId& aMailboxId )
       
   647     {
       
   648     FUNC_LOG;
       
   649     // Get the entry based on mailbox id
       
   650     TMsvEntry entry;
       
   651     TMsvId serviceId;
       
   652     TInt error = aMsvSession.GetEntry( aMailboxId, serviceId, entry );
       
   653 
       
   654     // Get the related id
       
   655     aMailboxId = ( error == KErrNone ) ? entry.iRelatedId : 0;
       
   656 
       
   657     return error;
       
   658     }
       
   659 
       
   660 // ----------------------------------------------------------------------------
       
   661 // CIpsSetUiDialogCtrl::GetDefaultSecurityPort()
       
   662 // ----------------------------------------------------------------------------
       
   663 //
       
   664 TUint32 IpsSetUtils::GetDefaultSecurityPort(
       
   665     const TInt aSecurity,
       
   666     const TBool aIncoming,
       
   667     const TBool aIsImap4 )
       
   668     {
       
   669     FUNC_LOG;
       
   670     TUint32 port( 0 );
       
   671 
       
   672     switch( aSecurity )
       
   673         {
       
   674         case CIpsSetData::EStartTls:
       
   675         case CIpsSetData::ESecurityOff:
       
   676             {
       
   677             if ( aIncoming )
       
   678                 {
       
   679                 port = aIsImap4 ? KIpsSetDataDefaultPortImap4 :
       
   680                     KIpsSetDataDefaultPortPop3;
       
   681                 }
       
   682             else
       
   683                 {
       
   684                 port = KIpsSetDataDefaultPortSmtp;
       
   685                 }
       
   686             }
       
   687             break;
       
   688         case CIpsSetData::ESslTls:
       
   689             {
       
   690             if ( aIncoming )
       
   691                 {
       
   692                 port = aIsImap4 ? KIpsSetDataDefaultSecurityPortImap4 :
       
   693                     KIpsSetDataDefaultSecurityPortPop3;
       
   694                 }
       
   695             else
       
   696                 {
       
   697                 port = KIpsSetDataDefaultSecurityPortSmtp;
       
   698                 }
       
   699             }
       
   700             break;
       
   701         default:
       
   702             break;
       
   703         }
       
   704     return port;
       
   705     }
       
   706 
       
   707 // ----------------------------------------------------------------------------
       
   708 // CIpsSetUiDialogCtrl::GetServiceIdFromAccountId()
       
   709 // ----------------------------------------------------------------------------
       
   710 //
       
   711 void IpsSetUtils::GetServiceIdFromAccountIdL(
       
   712     const TUint32 aAccountId,
       
   713     const TUid aMtmId,
       
   714     TMsvId& aServiceId,
       
   715     TMsvId& aSmtpId )
       
   716     {
       
   717     FUNC_LOG;
       
   718     __ASSERT_DEBUG( (aMtmId == KSenduiMtmPop3Uid || 
       
   719             aMtmId == KSenduiMtmImap4Uid ), 
       
   720             User::Panic(KIpsSetGenPanicLit, KErrArgument) );
       
   721     
       
   722     TMsvId retService = KErrNotFound;
       
   723     TMsvId retSmtp = KErrNotFound;
       
   724     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   725     if ( aMtmId == KSenduiMtmPop3Uid )
       
   726         {
       
   727         RArray<TPopAccount> popAccounts;
       
   728         accounts->GetPopAccountsL( popAccounts );
       
   729         for ( TInt i = 0; i < popAccounts.Count() ; i++ )
       
   730             {
       
   731             if ( popAccounts[i].iPopAccountId == aAccountId )
       
   732                 {
       
   733                 retService = popAccounts[i].iPopService;
       
   734                 retSmtp = popAccounts[i].iSmtpService;
       
   735                 }
       
   736             }
       
   737         popAccounts.Close();
       
   738         }
       
   739     else if ( aMtmId == KSenduiMtmImap4Uid )
       
   740         {
       
   741         RArray<TImapAccount> imapAccounts;
       
   742         accounts->GetImapAccountsL( imapAccounts );
       
   743         for ( TInt i = 0; i < imapAccounts.Count() ; i++ )
       
   744             {
       
   745             if ( imapAccounts[i].iImapAccountId == aAccountId )
       
   746                 {
       
   747                 retService = imapAccounts[i].iImapService;
       
   748                 retSmtp = imapAccounts[i].iSmtpService;
       
   749                 }
       
   750             }
       
   751         imapAccounts.Close();
       
   752         }
       
   753     else
       
   754         {
       
   755         User::Leave( KErrArgument );
       
   756         }
       
   757     
       
   758     if ( retService != KErrNotFound )
       
   759         {
       
   760         aServiceId = retService;
       
   761         aSmtpId = retSmtp;
       
   762         }
       
   763     else
       
   764         {
       
   765         User::Leave( KErrNotFound );
       
   766         }
       
   767     CleanupStack::PopAndDestroy( accounts );
       
   768     }
       
   769 
       
   770 // ----------------------------------------------------------------------------
       
   771 // CIpsSetUiDialogCtrl::GetSmtpSettingsFromAccountIdL()
       
   772 // ----------------------------------------------------------------------------
       
   773 //
       
   774 CImSmtpSettings* IpsSetUtils::GetSmtpSettingsFromAccountIdL(
       
   775     const TUint32 aAccountId,
       
   776     const TUid aMtmId )
       
   777     {
       
   778     FUNC_LOG;
       
   779     TMsvId serviceId;
       
   780     TMsvId smtpId;
       
   781     IpsSetUtils::GetServiceIdFromAccountIdL(
       
   782         aAccountId, aMtmId, serviceId, smtpId );
       
   783     
       
   784     CImSmtpSettings* smtpSettings = new ( ELeave ) CImSmtpSettings();
       
   785     CleanupStack::PushL( smtpSettings );
       
   786     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   787     TSmtpAccount smtpAcc;
       
   788     accounts->GetSmtpAccountL( smtpId, smtpAcc );
       
   789     accounts->LoadSmtpSettingsL( smtpAcc, *smtpSettings );
       
   790     CleanupStack::PopAndDestroy( accounts );
       
   791     CleanupStack::Pop( smtpSettings );
       
   792     return smtpSettings;
       
   793     }
       
   794 
       
   795 
       
   796 // End of File
       
   797