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

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

Generated by  doxygen 1.6.2