examples/ForumNokia/ContactsModel/src/ContactsModelAppUi.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include "ContactsModelAppUi.h"
00007 #include "ContactsModelContainer.h"
00008 #include "ContactsModelDocument.h"
00009 #include <ContactsModel.rsg>
00010 #include "ContactsModel.hrh"
00011 
00012 #include <AknCommonDialogs.h>
00013 #include <CAknFileSelectionDialog.h>
00014 #include <CAknFileNamePromptDialog.h>
00015 #include <aknnotewrappers.h>
00016 #include <eikmenup.h>
00017 #include <f32file.h>
00018 #include <s32file.h>
00019 #include <avkon.hrh>
00020 #include <maknfileselectionobserver.h>
00021 
00022 _LIT(KDefaultFileName, "vcard");
00023 _LIT(KFailedToOpen, "Failed to open file for import");
00024 _LIT(KFailedToImport, "Failed to import");
00025 _LIT(KFailedToCreateFile, "Failed to create file for export");
00026 
00027 const TInt KBufferSize = 256;
00028 
00029 // ================= MEMBER FUNCTIONS =======================
00030 //
00031 // ----------------------------------------------------------
00032 // CContactsModelAppUi::ConstructL()
00033 // ----------------------------------------------------------
00034 //
00035 void CContactsModelAppUi::ConstructL()
00036     {
00037     BaseConstructL(EAknEnableSkin);
00038 
00039     iAppContainer = new (ELeave) CContactsModelContainer;
00040     iAppContainer->SetMopParent( this );
00041     iAppContainer->ConstructL( ClientRect(), (CContactsModelDocument*)(iDocument) );
00042     AddToStackL( iAppContainer );
00043     }
00044 
00045 // ----------------------------------------------------
00046 // CContactsModelAppUi::~CContactsModelAppUi()
00047 // Destructor
00048 // Frees reserved resources
00049 // ----------------------------------------------------
00050 //
00051 CContactsModelAppUi::~CContactsModelAppUi()
00052     {
00053     if (iAppContainer)
00054         {
00055         RemoveFromStack( iAppContainer );
00056         delete iAppContainer;
00057         }
00058    }
00059 
00060 // ------------------------------------------------------------------------------
00061 // CContactsModelAppUi::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
00062 //  This function is called by the EIKON framework just before it displays
00063 //  a menu pane. Its default implementation is empty, and by overriding it,
00064 //  the application can set the state of menu items dynamically according
00065 //  to the state of application data.
00066 // ------------------------------------------------------------------------------
00067 //
00068 void CContactsModelAppUi::DynInitMenuPaneL(
00069     TInt aResourceId,CEikMenuPane* aMenuPane)
00070     {
00071         if (aResourceId == R_CONTACTSMODEL_MENU)
00072         {
00073         if(iAppContainer->ItemCount() < 1)
00074                 {
00075                 aMenuPane->SetItemDimmed(EContactsModelCmdAppExport, ETrue);
00076                 }
00077         }
00078     }
00079 
00080 // ----------------------------------------------------
00081 // CContactsModelAppUi::HandleKeyEventL(
00082 //     const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
00083 // takes care of key event handling
00084 // ----------------------------------------------------
00085 //
00086 TKeyResponse CContactsModelAppUi::HandleKeyEventL(
00087     const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
00088     {
00089     return EKeyWasNotConsumed;
00090     }
00091 
00092 // ----------------------------------------------------
00093 // CContactsModelAppUi::HandleCommandL(TInt aCommand)
00094 // takes care of command handling
00095 // ----------------------------------------------------
00096 //
00097 void CContactsModelAppUi::HandleCommandL(TInt aCommand)
00098     {
00099     switch ( aCommand )
00100         {
00101         case EAknSoftkeyExit:
00102         case EEikCmdExit:
00103             {
00104             Exit();
00105             break;
00106             }
00107         case EContactsModelCmdAppImport:
00108             {
00109 
00110                         TBuf<KBufferSize> fileName;
00111                         //select file to import from
00112                         if(AknCommonDialogs::RunSelectDlgLD(fileName, R_FILE_SELECTION_DIALOG))
00113                         {
00114                                 //import to the default contact database
00115                                 ImportL(fileName);
00116                                 //update listbox to display imported contacts
00117                                 HandleModelChangeL();
00118                         }
00119 
00120             break;
00121             }
00122         case EContactsModelCmdAppExport:
00123             {
00124                         //get file to export to
00125                         TFileName defaultFileName(KDefaultFileName);
00126                         if(AknCommonDialogs::RunSaveDlgLD(defaultFileName, R_MEMORY_SELECTION_DIALOG))
00127                         {
00128                                 //and export selected item
00129                                 ExportL(defaultFileName, iAppContainer->GetSelectedItem());
00130                         }
00131 
00132             break;
00133             }
00134 
00135         default:
00136             break;
00137         }
00138     }
00139 
00140 // ----------------------------------------------------
00141 // CContactsModelAppUi::ImportL(TDesC& aFileName)
00142 // Import new contact information (VCard)
00143 // ----------------------------------------------------
00144 //
00145 void CContactsModelAppUi::ImportL(const TDesC& aFileName)
00146 {
00147         // Create a file to read contacts
00148         RFs fileSession;
00149         User::LeaveIfError(fileSession.Connect());
00150         CleanupClosePushL(fileSession);
00151 
00152         RFile file;
00153         if (file.Open(fileSession, aFileName, EFileRead) != KErrNone)
00154         {
00155                 CAknInformationNote* informationNote = new (ELeave)CAknInformationNote;
00156                 informationNote->ExecuteLD(KFailedToOpen);
00157                 CleanupStack::PopAndDestroy(); // Close fileSession
00158                 return;
00159         }
00160         CleanupClosePushL(file);
00161 
00162         //open file
00163     RFileReadStream inputFileStream(file);
00164     CleanupClosePushL(inputFileStream);
00165 
00166         TInt result = ((CContactsModelDocument*)iDocument)->ImportL(inputFileStream);
00167         if(result!=KErrNone)
00168         {
00169                 CAknInformationNote* informationNote = new (ELeave)CAknInformationNote;
00170                 informationNote->ExecuteLD(KFailedToImport);
00171         }
00172 
00173     // Clean up
00174     CleanupStack::PopAndDestroy(); // Close inputFileStream
00175 
00176     CleanupStack::PopAndDestroy(); // Close file
00177     CleanupStack::PopAndDestroy(); // Close fileSession
00178 
00179         // read from the default database to display new items
00180         ((CContactsModelDocument*)iDocument)->UpdateContactsL();
00181 }
00182 
00183 // ----------------------------------------------------
00184 // CContactsModelAppUi::ExportL(TDesC& aFileName, TInt aItemIndex)
00185 // Export contact information
00186 // ----------------------------------------------------
00187 //
00188 void CContactsModelAppUi::ExportL(const TDesC& aFileName, TInt aItemIndex)
00189 {
00190         // Create a file to write contacts
00191         RFs fileSession;
00192         User::LeaveIfError(fileSession.Connect());
00193         CleanupClosePushL(fileSession);
00194 
00195         RFile file;
00196         if (file.Replace(fileSession, aFileName, EFileWrite) != KErrNone)
00197         {
00198                 CAknInformationNote* informationNote = new (ELeave)CAknInformationNote;
00199                 informationNote->ExecuteLD(KFailedToCreateFile);
00200                 CleanupStack::PopAndDestroy(); // Close fileSession
00201                 return;
00202         }
00203         CleanupClosePushL(file);
00204 
00205         //open file
00206     RFileWriteStream outputFileStream(file);
00207     CleanupClosePushL(outputFileStream);
00208 
00209         ((CContactsModelDocument*)iDocument)->ExportL(outputFileStream, aItemIndex);
00210 
00211     // Clean up
00212     CleanupStack::PopAndDestroy(); // Close outputFileStream
00213 
00214     CleanupStack::PopAndDestroy(); // Close file
00215     CleanupStack::PopAndDestroy(); // Close fileSession
00216 }
00217 
00218 void CContactsModelAppUi::HandleModelChangeL()
00219     {
00220         //read from the document to the listbox model
00221     iAppContainer->UpdateL();
00222         //and redraw
00223         iAppContainer->DrawNow();
00224     }
00225 
00226 // ---------------------------------------------------------
00227 // CDBMSAppUi::HandleResourceChangeL()
00228 // Called by framework when resource is changed.
00229 // ---------------------------------------------------------
00230 //
00231 void CContactsModelAppUi::HandleResourceChangeL(TInt aType)
00232         {
00233         CAknAppUi::HandleResourceChangeL(aType); //call to upper class
00234 
00235     // ADDED FOR SCALABLE UI SUPPORT
00236     // *****************************
00237         //if ( aType == KEikDynamicLayoutVariantSwitch )
00238         //hard coded constant so it can be compiled with first edition
00239         if ( aType == 0x101F8121 )
00240                 {
00241         if(iAppContainer)
00242                 iAppContainer->SetRect( ClientRect() );
00243             }
00244         }
00245 
00246 // End of File
00247 

Generated by  doxygen 1.6.2