examples/ForumNokia/Symbian_OS_End-to-End_Sockets_API_Example/SocketTaskManager_S60/src/TaskManagerAppUi.cpp

00001 /*
00002 * ============================================================================
00003 *  Name     : CTaskManagerAppUi from TaskManagerAppUi.cpp
00004 *  Part of  : TaskManager
00005 *  Created  : 08/31/2005 by Forum Nokia
00006 *  Version  : 1.01
00007 *  Copyright: Nokia Corporation
00008 * ============================================================================
00009 */
00010 
00011 // INCLUDE FILES
00012 #include "TaskManager.pan"
00013 #include "TaskManagerAppUi.h"
00014 #include "TaskManagerAppView.h"
00015 #include "TaskManager.hrh"
00016 #include "TaskManagerEngine.h"
00017 #include "TaskManagerConnForm.h"
00018 #include "TaskManagerConnInfo.h"
00019 
00020 #include <avkon.rsg>
00021 #include <eikapp.h>
00022 
00023 // CONSTANTS
00024 _LIT(KSettingsFile, "socketsettings.txt");
00025 
00026 
00027 // ================= MEMBER FUNCTIONS =======================
00028 
00029 // ----------------------------------------------------------
00030 // CTaskManagerAppUi::ConstructL()
00031 // 
00032 // ----------------------------------------------------------
00033 //
00034 void CTaskManagerAppUi::ConstructL()
00035         {
00036         // load the menu, etc. configuration from the resources
00037     BaseConstructL(EAknEnableSkin);
00038 
00039     // create the AppView - the control that will be able to draw on the screen
00040     iAppView = CTaskManagerAppView::NewL(ClientRect(), *this);    
00041     
00042     // required e.g. to display scroll buttons
00043     iAppView->SetMopParent(this);
00044 
00045     // add the control to the control stack - it will receive key press events
00046     AddToStackL(iAppView);
00047     
00048     // create an engine that handles socket transactions and SMS notifications.
00049     iEngine = CTaskManagerEngine::NewL(*iAppView);
00050 
00051 #ifdef __SERIES60_30__    
00052         // Only in 3rd Edition
00053     User::LeaveIfError( iEikonEnv->FsSession().CreatePrivatePath( EDriveC ) );
00054         User::LeaveIfError( iEikonEnv->FsSession().SetSessionToPrivate( EDriveC ) );
00055 
00056 #else
00057         // Only in 2nd Edition
00058         #ifndef __WINS__  // don't save settings to z-drive in emulator
00059         TFileName appFullName = Application()->AppFullName();
00060         TParsePtr appPath(appFullName);
00061         iSettingsFile = appPath.DriveAndPath();
00062         #endif //__WINS__
00063 #endif
00064 
00065     iSettingsFile.Append(KSettingsFile);
00066 
00067         // read settings    
00068     InternalizeConnInfoL();
00069 
00070         // Here we open the connections dialog. We needn't worry about the return
00071         // value because the dialog class has internal code that saves changes
00072         // to iConnectionInfo, if the user presses ok
00073     if (CTaskManagerConnForm::RunDlgLD( iConnectionInfo ))
00074         {
00075         iEngine->SetConnectionSettings(iConnectionInfo.ServerAddress(),
00076                                                                         iConnectionInfo.Port(),
00077                                                                         iConnectionInfo.Username(), 
00078                                                                         iConnectionInfo.Password());
00079         TUint32 iap;
00080 #ifdef __SERIES60_30__    
00081                         // Set an empty IAP, the device itself handles the correct IAP
00082                 iConnectionInfo.SetIap(iap);
00083                 iEngine->SetIap(iap);
00084                 iEngine->FetchTasksL();
00085 
00086 #else
00087                 // In 2nd Edition we need to do this manually
00088         
00089                 // query the IAP to be used.
00090                 if (iAppView->QueryIapL(iap, iConnectionInfo.Iap()))
00091                         {
00092                         iConnectionInfo.SetIap(iap);
00093                         iEngine->SetIap(iap);
00094                         iEngine->FetchTasksL();
00095                         }
00096 #endif
00097                 
00098         // save settings to file
00099         ExternalizeConnInfoL();
00100         }
00101         iEngine->SetAutomaticUpdateL(ETrue);
00102         }
00103 
00104 // ----------------------------------------------------
00105 // CTaskManagerAppUi::~CTaskManagerAppUi()
00106 // Destructor
00107 // Frees reserved resources
00108 // ----------------------------------------------------
00109 //
00110 CTaskManagerAppUi::~CTaskManagerAppUi()
00111         {
00112     if (iAppView)
00113             {
00114         RemoveFromStack(iAppView);
00115         delete iAppView;
00116         iAppView = NULL;
00117             }
00118     delete iEngine;
00119         }
00120 
00121 // ----------------------------------------------------
00122 // CTaskManagerAppUi::HandleCommandL()
00123 // takes care of command handling
00124 // ----------------------------------------------------
00125 //
00126 void CTaskManagerAppUi::HandleCommandL(TInt aCommand)
00127         {
00128     switch(aCommand)
00129                 {
00130         case EEikCmdExit:
00131         case EAknSoftkeyExit:
00132             Exit();
00133             break;
00134 
00135         case ETaskManagerConnectionSettingsCommand:
00136             {
00137             iUiBusy = ETrue;
00138             iEngine->SetAutomaticUpdateL(EFalse);
00139             
00140                         // Open the connections dialog
00141                         if (CTaskManagerConnForm::RunDlgLD( iConnectionInfo ))
00142                                 {
00143                                 TUint32 iap;
00144                                 // if IAP not yet selected, query the user.
00145                                 if (!iEngine->IapSet())
00146                                         {
00147                                         if (iAppView->QueryIapL(iap, iConnectionInfo.Iap()))
00148                                                 {
00149                                                 iConnectionInfo.SetIap(iap);
00150                                                 iEngine->SetIap(iap);
00151                                                 }
00152                                         }
00153                                 // update the connection settings.
00154                                 iEngine->SetConnectionSettings(iConnectionInfo.ServerAddress(),
00155                                                                                                 iConnectionInfo.Port(),
00156                                                                                                 iConnectionInfo.Username(), 
00157                                                                                                 iConnectionInfo.Password());
00158                                 
00159                                 // save the connection settings to a file.
00160                                 ExternalizeConnInfoL();
00161                                 }
00162                                 
00163                         iUiBusy = EFalse;
00164                         SetAutomaticUpdateL();
00165                 }
00166                 break;
00167         
00168                 case ETaskManagerLoadTasksCommand:
00169                         iEngine->FetchTasksL();
00170                         break;
00171                         
00172                 case EAknSoftkeyCancel:
00173                         iEngine->CancelTransaction();
00174                         break;
00175 
00176         default:
00177             Panic(ETaskManagerBasicUi);
00178             break;
00179         }
00180         }
00181 
00182 // ----------------------------------------------------
00183 // CTaskManagerAppUi::HandleForegroundEventL()
00184 // Called when an application switches to, or from, the 
00185 // foreground.
00186 // ----------------------------------------------------
00187 //              
00188 void CTaskManagerAppUi::HandleForegroundEventL(TBool aForeground)
00189         {
00190         CAknAppUi::HandleForegroundEventL(aForeground);
00191 
00192         // when coming in to foreground, unpause the engine and load 
00193         // tasks if an update SMS has arrived while we were in the background
00194         if (aForeground)
00195                 {
00196                 SetAutomaticUpdateL();
00197                 }
00198         // when going in to background, pause engine so that tasks are not 
00199         // downloaded automatically if an update SMS arrives.
00200         else
00201                 {
00202                 iEngine->SetAutomaticUpdateL(EFalse);
00203                 }
00204         }
00205 
00206 // ----------------------------------------------------
00207 // CTaskManagerAppUi::Model()
00208 // Returns a reference to the engine.
00209 // ----------------------------------------------------
00210 //      
00211 CTaskManagerEngine& CTaskManagerAppUi::Model()
00212         {
00213         return *iEngine;
00214         }
00215 
00216 // ----------------------------------------------------
00217 // CTaskManagerAppUi::ShowConnectingCbaL()
00218 // While a transaction is running, show only a 
00219 // cancel button.
00220 // ----------------------------------------------------
00221 //      
00222 void CTaskManagerAppUi::ShowConnectingCbaL(const TBool& aShow)
00223         {
00224         if (aShow)
00225                 {
00226                 Cba()->SetCommandSetL(R_AVKON_SOFTKEYS_CANCEL);
00227                 }
00228         else
00229                 {
00230                 Cba()->SetCommandSetL(R_AVKON_SOFTKEYS_OPTIONS_EXIT);
00231                 }
00232         Cba()->DrawNow();
00233         }
00234 
00235 // ----------------------------------------------------
00236 // CTaskManagerAppUi::InternalizeConnInfoL()
00237 // Reads connection settings from a settings file.
00238 // ----------------------------------------------------
00239 //
00240 void CTaskManagerAppUi::InternalizeConnInfoL()
00241         {
00242         RFs& fs = CCoeEnv::Static()->FsSession();
00243     RFileReadStream readStream;
00244     TInt error = readStream.Open(fs, iSettingsFile, EFileRead);
00245     readStream.PushL();
00246     TInt internalizationError = KErrNone;
00247     // if settings file existed, try to read settings.
00248     if (error == KErrNone)
00249         {
00250         TRAP(internalizationError, iConnectionInfo.InternalizeL(readStream);)
00251         }
00252     readStream.Pop();
00253     readStream.Release();
00254 
00255         // reading failed, settings file might be corrupted.    
00256     if (internalizationError != KErrNone)
00257         {
00258                 User::LeaveIfError(fs.Delete(iSettingsFile));
00259         }
00260 
00261         }
00262 
00263 // ----------------------------------------------------
00264 // CTaskManagerAppUi::ExternalizeConnInfoL()
00265 // Saves connection settings to a settings file.
00266 // ----------------------------------------------------
00267 //      
00268 void CTaskManagerAppUi::ExternalizeConnInfoL()
00269         {
00270         RFs& fs = CCoeEnv::Static()->FsSession();
00271         RFileWriteStream writeStream;
00272         TInt error = writeStream.Open(fs, iSettingsFile, EFileWrite);
00273         // setting file did not exist, create one.
00274         if (error != KErrNone)
00275                 {
00276                 User::LeaveIfError(writeStream.Create(fs, iSettingsFile, EFileWrite));
00277                 }
00278         writeStream.PushL();
00279         writeStream << iConnectionInfo;
00280         writeStream.CommitL();
00281         writeStream.Pop();
00282         writeStream.Release();
00283         }
00284 
00285 // ----------------------------------------------------
00286 // CTaskManagerAppUi::SetViewBusyL()
00287 // AppView informs the AppUi that it is busy. While 
00288 // AppView is busy, the engine does not update tasks.
00289 // ----------------------------------------------------
00290 //      
00291 void CTaskManagerAppUi::SetViewBusyL(const TBool& aBusy)
00292         {
00293         iViewBusy = aBusy;
00294         if (aBusy)
00295                 {
00296                 iEngine->SetAutomaticUpdateL(EFalse);
00297                 }
00298         else
00299                 {
00300                 SetAutomaticUpdateL();
00301                 }
00302         }
00303 
00304 // ----------------------------------------------------
00305 // CTaskManagerAppUi::SetAutomaticUpdateL()
00306 // If view and ui aren't busy, tasks can be loaded 
00307 // automatically.
00308 // ----------------------------------------------------
00309 //              
00310 void CTaskManagerAppUi::SetAutomaticUpdateL()
00311         {
00312         if (!iViewBusy && !iUiBusy)
00313                 {
00314                 iEngine->SetAutomaticUpdateL(ETrue);
00315                 }
00316         }
00317         
00318 // End of file

Generated by  doxygen 1.6.2