00001
00002
00003
00004
00005
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
00030
00031
00032
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
00047
00048
00049
00050
00051 CContactsModelAppUi::~CContactsModelAppUi()
00052 {
00053 if (iAppContainer)
00054 {
00055 RemoveFromStack( iAppContainer );
00056 delete iAppContainer;
00057 }
00058 }
00059
00060
00061
00062
00063
00064
00065
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
00082
00083
00084
00085
00086 TKeyResponse CContactsModelAppUi::HandleKeyEventL(
00087 const TKeyEvent& ,TEventCode )
00088 {
00089 return EKeyWasNotConsumed;
00090 }
00091
00092
00093
00094
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
00112 if(AknCommonDialogs::RunSelectDlgLD(fileName, R_FILE_SELECTION_DIALOG))
00113 {
00114
00115 ImportL(fileName);
00116
00117 HandleModelChangeL();
00118 }
00119
00120 break;
00121 }
00122 case EContactsModelCmdAppExport:
00123 {
00124
00125 TFileName defaultFileName(KDefaultFileName);
00126 if(AknCommonDialogs::RunSaveDlgLD(defaultFileName, R_MEMORY_SELECTION_DIALOG))
00127 {
00128
00129 ExportL(defaultFileName, iAppContainer->GetSelectedItem());
00130 }
00131
00132 break;
00133 }
00134
00135 default:
00136 break;
00137 }
00138 }
00139
00140
00141
00142
00143
00144
00145 void CContactsModelAppUi::ImportL(const TDesC& aFileName)
00146 {
00147
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();
00158 return;
00159 }
00160 CleanupClosePushL(file);
00161
00162
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
00174 CleanupStack::PopAndDestroy();
00175
00176 CleanupStack::PopAndDestroy();
00177 CleanupStack::PopAndDestroy();
00178
00179
00180 ((CContactsModelDocument*)iDocument)->UpdateContactsL();
00181 }
00182
00183
00184
00185
00186
00187
00188 void CContactsModelAppUi::ExportL(const TDesC& aFileName, TInt aItemIndex)
00189 {
00190
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();
00201 return;
00202 }
00203 CleanupClosePushL(file);
00204
00205
00206 RFileWriteStream outputFileStream(file);
00207 CleanupClosePushL(outputFileStream);
00208
00209 ((CContactsModelDocument*)iDocument)->ExportL(outputFileStream, aItemIndex);
00210
00211
00212 CleanupStack::PopAndDestroy();
00213
00214 CleanupStack::PopAndDestroy();
00215 CleanupStack::PopAndDestroy();
00216 }
00217
00218 void CContactsModelAppUi::HandleModelChangeL()
00219 {
00220
00221 iAppContainer->UpdateL();
00222
00223 iAppContainer->DrawNow();
00224 }
00225
00226
00227
00228
00229
00230
00231 void CContactsModelAppUi::HandleResourceChangeL(TInt aType)
00232 {
00233 CAknAppUi::HandleResourceChangeL(aType);
00234
00235
00236
00237
00238
00239 if ( aType == 0x101F8121 )
00240 {
00241 if(iAppContainer)
00242 iAppContainer->SetRect( ClientRect() );
00243 }
00244 }
00245
00246
00247