examples/ForumNokia/ContactsModel/src/ContactsModelAppUi.cpp

00001 /*
00002  * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 // INCLUDE FILES
00031 #include "ContactsModelAppUi.h"
00032 #include "ContactsModelContainer.h"
00033 #include "ContactsModelDocument.h"
00034 #include <ContactsModel.rsg>
00035 #include "ContactsModel.hrh"
00036 
00037 #include <AknCommonDialogs.h>
00038 #include <CAknFileSelectionDialog.h>
00039 #include <CAknFileNamePromptDialog.h>
00040 #include <aknnotewrappers.h>
00041 #include <eikmenup.h>
00042 #include <f32file.h>
00043 #include <s32file.h>
00044 #include <avkon.hrh>
00045 #include <maknfileselectionobserver.h>
00046 
00047 _LIT(KDefaultFileName, "vcard");
00048 _LIT(KFailedToOpen, "Failed to open file for import");
00049 _LIT(KFailedToImport, "Failed to import");
00050 _LIT(KFailedToCreateFile, "Failed to create file for export");
00051 
00052 const TInt KBufferSize = 256;
00053 
00054 // ================= MEMBER FUNCTIONS =======================
00055 //
00056 // ----------------------------------------------------------
00057 // CContactsModelAppUi::ConstructL()
00058 // ----------------------------------------------------------
00059 //
00060 void CContactsModelAppUi::ConstructL()
00061     {
00062     BaseConstructL(EAknEnableSkin);
00063 
00064     iAppContainer = new (ELeave) CContactsModelContainer;
00065     iAppContainer->SetMopParent( this );
00066     iAppContainer->ConstructL( ClientRect(), (CContactsModelDocument*)(iDocument) );
00067     AddToStackL( iAppContainer );
00068     }
00069 
00070 // ----------------------------------------------------
00071 // CContactsModelAppUi::~CContactsModelAppUi()
00072 // Destructor
00073 // Frees reserved resources
00074 // ----------------------------------------------------
00075 //
00076 CContactsModelAppUi::~CContactsModelAppUi()
00077     {
00078     if (iAppContainer)
00079         {
00080         RemoveFromStack( iAppContainer );
00081         delete iAppContainer;
00082         }
00083    }
00084 
00085 // ------------------------------------------------------------------------------
00086 // CContactsModelAppUi::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
00087 //  This function is called by the EIKON framework just before it displays
00088 //  a menu pane. Its default implementation is empty, and by overriding it,
00089 //  the application can set the state of menu items dynamically according
00090 //  to the state of application data.
00091 // ------------------------------------------------------------------------------
00092 //
00093 void CContactsModelAppUi::DynInitMenuPaneL(
00094     TInt aResourceId,CEikMenuPane* aMenuPane)
00095     {
00096         if (aResourceId == R_CONTACTSMODEL_MENU)
00097         {
00098         if(iAppContainer->ItemCount() < 1)
00099                 {
00100                 aMenuPane->SetItemDimmed(EContactsModelCmdAppExport, ETrue);
00101                 }
00102         }
00103     }
00104 
00105 // ----------------------------------------------------
00106 // CContactsModelAppUi::HandleKeyEventL(
00107 //     const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
00108 // takes care of key event handling
00109 // ----------------------------------------------------
00110 //
00111 TKeyResponse CContactsModelAppUi::HandleKeyEventL(
00112     const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
00113     {
00114     return EKeyWasNotConsumed;
00115     }
00116 
00117 // ----------------------------------------------------
00118 // CContactsModelAppUi::HandleCommandL(TInt aCommand)
00119 // takes care of command handling
00120 // ----------------------------------------------------
00121 //
00122 void CContactsModelAppUi::HandleCommandL(TInt aCommand)
00123     {
00124     switch ( aCommand )
00125         {
00126         case EAknSoftkeyExit:
00127         case EEikCmdExit:
00128             {
00129             Exit();
00130             break;
00131             }
00132         case EContactsModelCmdAppImport:
00133             {
00134 
00135                         TBuf<KBufferSize> fileName;
00136                         //select file to import from
00137                         if(AknCommonDialogs::RunSelectDlgLD(fileName, R_FILE_SELECTION_DIALOG))
00138                         {
00139                                 //import to the default contact database
00140                                 ImportL(fileName);
00141                                 //update listbox to display imported contacts
00142                                 HandleModelChangeL();
00143                         }
00144 
00145             break;
00146             }
00147         case EContactsModelCmdAppExport:
00148             {
00149                         //get file to export to
00150                         TFileName defaultFileName(KDefaultFileName);
00151                         if(AknCommonDialogs::RunSaveDlgLD(defaultFileName, R_MEMORY_SELECTION_DIALOG))
00152                         {
00153                                 //and export selected item
00154                                 ExportL(defaultFileName, iAppContainer->GetSelectedItem());
00155                         }
00156 
00157             break;
00158             }
00159 
00160         default:
00161             break;
00162         }
00163     }
00164 
00165 // ----------------------------------------------------
00166 // CContactsModelAppUi::ImportL(TDesC& aFileName)
00167 // Import new contact information (VCard)
00168 // ----------------------------------------------------
00169 //
00170 void CContactsModelAppUi::ImportL(const TDesC& aFileName)
00171 {
00172         // Create a file to read contacts
00173         RFs fileSession;
00174         User::LeaveIfError(fileSession.Connect());
00175         CleanupClosePushL(fileSession);
00176 
00177         RFile file;
00178         if (file.Open(fileSession, aFileName, EFileRead) != KErrNone)
00179         {
00180                 CAknInformationNote* informationNote = new (ELeave)CAknInformationNote;
00181                 informationNote->ExecuteLD(KFailedToOpen);
00182                 CleanupStack::PopAndDestroy(); // Close fileSession
00183                 return;
00184         }
00185         CleanupClosePushL(file);
00186 
00187         //open file
00188     RFileReadStream inputFileStream(file);
00189     CleanupClosePushL(inputFileStream);
00190 
00191         TInt result = ((CContactsModelDocument*)iDocument)->ImportL(inputFileStream);
00192         if(result!=KErrNone)
00193         {
00194                 CAknInformationNote* informationNote = new (ELeave)CAknInformationNote;
00195                 informationNote->ExecuteLD(KFailedToImport);
00196         }
00197 
00198     // Clean up
00199     CleanupStack::PopAndDestroy(); // Close inputFileStream
00200 
00201     CleanupStack::PopAndDestroy(); // Close file
00202     CleanupStack::PopAndDestroy(); // Close fileSession
00203 
00204         // read from the default database to display new items
00205         ((CContactsModelDocument*)iDocument)->UpdateContactsL();
00206 }
00207 
00208 // ----------------------------------------------------
00209 // CContactsModelAppUi::ExportL(TDesC& aFileName, TInt aItemIndex)
00210 // Export contact information
00211 // ----------------------------------------------------
00212 //
00213 void CContactsModelAppUi::ExportL(const TDesC& aFileName, TInt aItemIndex)
00214 {
00215         // Create a file to write contacts
00216         RFs fileSession;
00217         User::LeaveIfError(fileSession.Connect());
00218         CleanupClosePushL(fileSession);
00219 
00220         RFile file;
00221         if (file.Replace(fileSession, aFileName, EFileWrite) != KErrNone)
00222         {
00223                 CAknInformationNote* informationNote = new (ELeave)CAknInformationNote;
00224                 informationNote->ExecuteLD(KFailedToCreateFile);
00225                 CleanupStack::PopAndDestroy(); // Close fileSession
00226                 return;
00227         }
00228         CleanupClosePushL(file);
00229 
00230         //open file
00231     RFileWriteStream outputFileStream(file);
00232     CleanupClosePushL(outputFileStream);
00233 
00234         ((CContactsModelDocument*)iDocument)->ExportL(outputFileStream, aItemIndex);
00235 
00236     // Clean up
00237     CleanupStack::PopAndDestroy(); // Close outputFileStream
00238 
00239     CleanupStack::PopAndDestroy(); // Close file
00240     CleanupStack::PopAndDestroy(); // Close fileSession
00241 }
00242 
00243 void CContactsModelAppUi::HandleModelChangeL()
00244     {
00245         //read from the document to the listbox model
00246     iAppContainer->UpdateL();
00247         //and redraw
00248         iAppContainer->DrawNow();
00249     }
00250 
00251 // ---------------------------------------------------------
00252 // CDBMSAppUi::HandleResourceChangeL()
00253 // Called by framework when resource is changed.
00254 // ---------------------------------------------------------
00255 //
00256 void CContactsModelAppUi::HandleResourceChangeL(TInt aType)
00257         {
00258         CAknAppUi::HandleResourceChangeL(aType); //call to upper class
00259 
00260     // ADDED FOR SCALABLE UI SUPPORT
00261     // *****************************
00262         //if ( aType == KEikDynamicLayoutVariantSwitch )
00263         //hard coded constant so it can be compiled with first edition
00264         if ( aType == 0x101F8121 )
00265                 {
00266         if(iAppContainer)
00267                 iAppContainer->SetRect( ClientRect() );
00268             }
00269         }
00270 
00271 // End of File
00272 

Generated by  doxygen 1.6.2