00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00055
00056
00057
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
00072
00073
00074
00075
00076 CContactsModelAppUi::~CContactsModelAppUi()
00077 {
00078 if (iAppContainer)
00079 {
00080 RemoveFromStack( iAppContainer );
00081 delete iAppContainer;
00082 }
00083 }
00084
00085
00086
00087
00088
00089
00090
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
00107
00108
00109
00110
00111 TKeyResponse CContactsModelAppUi::HandleKeyEventL(
00112 const TKeyEvent& ,TEventCode )
00113 {
00114 return EKeyWasNotConsumed;
00115 }
00116
00117
00118
00119
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
00137 if(AknCommonDialogs::RunSelectDlgLD(fileName, R_FILE_SELECTION_DIALOG))
00138 {
00139
00140 ImportL(fileName);
00141
00142 HandleModelChangeL();
00143 }
00144
00145 break;
00146 }
00147 case EContactsModelCmdAppExport:
00148 {
00149
00150 TFileName defaultFileName(KDefaultFileName);
00151 if(AknCommonDialogs::RunSaveDlgLD(defaultFileName, R_MEMORY_SELECTION_DIALOG))
00152 {
00153
00154 ExportL(defaultFileName, iAppContainer->GetSelectedItem());
00155 }
00156
00157 break;
00158 }
00159
00160 default:
00161 break;
00162 }
00163 }
00164
00165
00166
00167
00168
00169
00170 void CContactsModelAppUi::ImportL(const TDesC& aFileName)
00171 {
00172
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();
00183 return;
00184 }
00185 CleanupClosePushL(file);
00186
00187
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
00199 CleanupStack::PopAndDestroy();
00200
00201 CleanupStack::PopAndDestroy();
00202 CleanupStack::PopAndDestroy();
00203
00204
00205 ((CContactsModelDocument*)iDocument)->UpdateContactsL();
00206 }
00207
00208
00209
00210
00211
00212
00213 void CContactsModelAppUi::ExportL(const TDesC& aFileName, TInt aItemIndex)
00214 {
00215
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();
00226 return;
00227 }
00228 CleanupClosePushL(file);
00229
00230
00231 RFileWriteStream outputFileStream(file);
00232 CleanupClosePushL(outputFileStream);
00233
00234 ((CContactsModelDocument*)iDocument)->ExportL(outputFileStream, aItemIndex);
00235
00236
00237 CleanupStack::PopAndDestroy();
00238
00239 CleanupStack::PopAndDestroy();
00240 CleanupStack::PopAndDestroy();
00241 }
00242
00243 void CContactsModelAppUi::HandleModelChangeL()
00244 {
00245
00246 iAppContainer->UpdateL();
00247
00248 iAppContainer->DrawNow();
00249 }
00250
00251
00252
00253
00254
00255
00256 void CContactsModelAppUi::HandleResourceChangeL(TInt aType)
00257 {
00258 CAknAppUi::HandleResourceChangeL(aType);
00259
00260
00261
00262
00263
00264 if ( aType == 0x101F8121 )
00265 {
00266 if(iAppContainer)
00267 iAppContainer->SetRect( ClientRect() );
00268 }
00269 }
00270
00271
00272