You Are Here:

Community: Blogs

Paul Todd's Forum Nokia Blog

Use and abuse of Symbian idioms

Paul.Todd | 02 April, 2007 23:18

I saw a very interesting snipped of code on the newsgroups the other day. It showed how people can so easily misuse Symbian programming idioms if they are not careful

    RArray<TInt>* array = new (ELeave) RArray<TInt>;
    array->Append(1);
    array->Append(2);
    delete array;

This is not the original class, the original class used another R class but the problem is still the same.
To all intents and purposes this look like safe c++ code but the coder failed to realize that an RClose MUST be closed and cannot be deleted.

I wonder how many other people have been bitten by this issue?

Final part of using RSendAs with an email address

Paul.Todd | 02 April, 2007 23:02

This concludes the previous post on how to use RSendAsMessage to send an email.

With the first post, doing it the simple way causes lots of problems and
has many corner cases that it turned out the only way to do it successfully is to
do it properly - like everything else in Symbian.

One of the more annoying things is that the SMTP MTM seems to report it cannot
send an email, and only when you force it does it actually load the mtm and query the capabilties,
that it can in fact send emails. So this makes the AvailableAccountsL function
effectivly useless to get all the mtm accounts, so we need to dig deeper and use
the raw messaging API's to get the true list of accounts (uugh!)

So from a simple implementation which was supposed to utilize the RSendAs API's to
hide the complexity of the messaging server we end up having to write a complex
method using the the raw messaging API's....

First we create a structure to hold the name of the mtm and its account id as
well as an array to hold this data
struct TAccountInfo
    {
    TInt32        iId;
    TBuf<32>    iAccountName;
    };
typedef RArray<TAccountInfo>    RAccountInfo;

The next is the uid of the email technology group. The MTM
subsystem utilizes different technology groups to define how the
MTM is to appear in the messaging server and how it is generally expected to
perform.
const TUid KUidMsvTechnologyGroupEmail    = {0x10001671};

Finally we need a dummy class to absorb the events from the messaging
server. Possibly we should check from errors here and store them
in case any callback fails, but for now we just ignore after all what
could possibly go wrong :)
class TDummyObserver: public MMsvSessionObserver
    {
public:
    void HandleSessionEventL(TMsvSessionEvent, TAny*, TAny*, TAny*) {};
    };


Now we have intialized the variables and classes, we can get the account information
// Open a session to the messaging server
    TDummyObserver obs;
    CMsvSession* session = CMsvSession::OpenSyncL(obs);
    CleanupStack::PushL(session);

// Now get the list of Client MTM's installed on the device
// by asking the mtm session for the list.
    CClientMtmRegistry* registry = CClientMtmRegistry::NewL(*session);
    CleanupStack::PushL(registry);

// Enumerate the client mtm's and check its an email technology mtm and it has at least one account.
// Also remember to ensure the mtm can send a message!! very important!
    const TInt count = registry->NumRegisteredMtmDlls();
    for (TInt i=0; i < count; i++)
        {
        const TUid uid = registry->MtmTypeUid(i);

// Check to see the mtm is an email mtm
        if (registry->TechnologyTypeUid(uid) == KUidMsvTechnologyGroupEmail)
            {
// Create a client side MTM for the specified UID
// this is so we can get whether or not the MTM
// is one that can send a message
            CBaseMtm* mtm = registry->NewMtmL(uid);
            CleanupStack::PushL(mtm);

// Now check to see if the MTM can send a message
            TInt response = KErrNone;
            if (mtm->QueryCapability(KUidMtmQueryCanSendMsg, response) == KErrNone)
                {
// As its an email mtm that can send message add all the accounts associated with the mtm
// We get the root entry for the MTM and then get all children under the root
// that match the mtm
                const TMsvSelectionOrdering order(KMsvNoGrouping, EMsvSortByDetails, ETrue);
                CMsvEntry* entry = CMsvEntry::NewL(*session, KMsvRootIndexEntryId, order);
                CleanupStack::PushL(entry);
               
                entry->SetEntryL(KMsvRootIndexEntryId);
                CMsvEntrySelection* selection = entry->ChildrenWithMtmL(uid);
                CleanupStack::PushL(selection);

// Now we can get the details for the accounts in the mtm
                const TInt entryCount = selection->Count();
                for (TInt j=0; j < entryCount; j++)
                    {
                    entry->SetEntryL(selection->At(j));

// Save the id of the account in the messaging server
// and its associated name
                    TAccountInfo info;
                    info.iId = entry->EntryId();
                    info.iAccountName = entry->Entry().iDetails.Left(info.iAccountName.MaxLength());
                    User::LeaveIfError(aAccountInfo.Append(info));
                    }

                CleanupStack::PopAndDestroy(2, entry); // entry and selection
                }
           
            CleanupStack::PopAndDestroy(mtm);
            }
        }

    CleanupStack::PopAndDestroy(2, session); // registry, session

There we have it, a way to select an email account to send a message with using the RSendAs API

Finally we can send the message, aAccountId is on of the iId field of the accounts collected above.

// Open a session
    RSendAs session;
    User::LeaveIfError(session.Connect());
    CleanupClosePushL(session);

// Now create a message, using the send as API
    RSendAsMessage message;
    message.CreateL(session, aAccountId);
    CleanupClosePushL(message);
    message.SetBodyTextL(_L("Body"));
    message.SetSubjectL(_L("Subject"));
    message.AddRecipientL(_L("fred.smith@nowhere.com"), _L(""), RSendAsMessage::ESendAsRecipientTo);
   
// Now launch the editor and close the message
// and session.
    message.LaunchEditorAndCloseL();

    CleanupStack::PopAndDestroy(2, &session);
 
 

Rate This

 
 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxItE20allE20startedE20innocentlyE20enoughE2eE20TheE20notebookE20didnE27tE20fitE20inE20myE20pocketE2cE20andE20myE20N93E20wasE20alreadyE20thereE2eE20ItE92sE20beenE20aE20greatE20coupleE20ofE20weeksE2cE20butE20IE92mE20gladE20toE20beE20backE2eE20NareshE20E2cE20ToteE20andE20E52onE20haveE20alreadyE20commentedE20onE20theE20ChampionE20E44ayE2eE20IE20echoE20theirE20thoughtsE20thatE20itE20isE20wonderfulE20toE20meetE20inE203E44E2dlandE20thoseE20peopleE20whoE20IE20workE20withE20andE20workE20forE2cE20butE20rarelyE20actuallyE20seeE2eE20Ea0E20AfterE2eE2eE2eE20E45ventE2cE20GeneralE20IE92veE20beenE20workingE20onE20myE20ToE44oE20listE20thisE20weekE2eE20WellE2cE20workingE20mightE20beE20aE20stretchE2eE20ItE92sE20beeE2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fblogsE2eforumE2enokiaE2ecomE2fblogE2ftastyE2dmultimediaE2djournalsE2dforumE2dnokiaE2dblogE2fgeneralX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxTastyE20MultimediaE20JournalE27sE20ForumE20NokiaE20BlogE20E7cE20GeneralX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZBlogContentQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxItE20allE20startedE20innocentlyE20enoughE2eE20TheE20notebookE20didnE27tE20fitE20inE20myE20pocketE2cE20andE20myE20N93E20wasE20alreadyE20thereE2eE20ItE92sE20beenE20aE20greatE20coupleE20ofE20weeksE2cE20butE20IE92mE20gladE20toE20beE20backE2eE20NareshE20E2cE20ToteE20andE20E52onE20haveE20alreadyE20commentedE20onE20theE20ChampionE20E44ayE2eE20IE20echoE20theirE20thoughtsE20thatE20itE20isE20wonderfulE20toE20meetE20inE203E44E2dlandE20thoseE20peopleE20whoE20IE20workE20withE20andE20workE20forE2cE20butE20rarelyE20actuallyE20seeE2eE20Ea0E20AfterE2eE2eE2eE20E45ventE2cE20GeneralE20IE92veE20beenE20workingE20onE20myE20ToE44oE20listE20thisE20weekE2eE20WellE2cE20workingE20mightE20beE20aE20stretchE2eE20ItE92sE20beeE2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fblogsE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfntypeZBlogContentQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZupdatedQDx2008E2d10E2d03X qmarsZdescriptionQSxItE20allE20startedE20innocentlyE20enoughE2eE20TheE20notebookE20didnE27tE20fitE20inE20myE20pocketE2cE20andE20myE20N93E20wasE20alreadyE20thereE2eE20ItE92sE20beenE20aE20greatE20coupleE20ofE20weeksE2cE20butE20IE92mE20gladE20toE20beE20backE2eE20NareshE20E2cE20ToteE20andE20E52onE20haveE20alreadyE20commentedE20onE20theE20ChampionE20E44ayE2eE20IE20echoE20theirE20thoughtsE20thatE20itE20isE20wonderfulE20toE20meetE20inE203E44E2dlandE20thoseE20peopleE20whoE20IE20workE20withE20andE20workE20forE2cE20butE20rarelyE20actuallyE20seeE2eE20Ea0E20AfterE2eE2eE2eE20E45ventE2cE20GeneralE20IE92veE20beenE20workingE20onE20myE20ToE44oE20listE20thisE20weekE2eE20WellE2cE20workingE20mightE20beE20aE20stretchE2eE20ItE92sE20beeE2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZBlogContentQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ