examples/ForumNokia/TZLocalizer/src/TZLocalizerAppUi.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 <AknQueryDialog.h>
00032 #include <badesca.h>
00033 #include <aknlists.h> 
00034 #include <eikmenup.h>
00035 #include <aknnotewrappers.h>
00036 #include "TZLocalizerAppUi.h"
00037 #include "TZLocalizerDialog.h" 
00038 #include "TZLocalizerEngine.h"
00039 #include <TZLocalizer.rsg>
00040 #include "tzlocalizer.hrh"
00041 
00042 
00043 #include <avkon.hrh>
00044 
00045 // constants
00046 
00047 const TInt KBufSize = 30;
00048 const TInt KTextBufferLength = 128;
00049 const TInt KArrayGranularity = 5;
00050 
00051 // literals for the ui
00052 _LIT( KDeleteCity , "Delete city:" );
00053 _LIT( KNoMatchingCity, "No matching city found!");
00054 _LIT( KCityName , "City name:" );
00055 _LIT( KErrorAddingCity, "Error while adding new city!");
00056 _LIT( KCityAdded, "City added.");
00057 _LIT( KCityRemoved, "City removed.");
00058 _LIT( KNewCityName , "New city name:" );
00059 _LIT( KChooseCityGroupID , "Choose city group ID"); 
00060 _LIT( KChooseTimeZoneID,"Choose Time Zone ID");
00061 _LIT( KCityLocalTime, "\tCity local time:\t" );
00062 _LIT( KCityGroupID, "\tCity group ID:\t");
00063 _LIT( KCityTimeZoneID, "\tCity time zone ID:\t");
00064 _LIT( KCityName2,"\tCity name:\t" );
00065 
00066 
00067 // ---------------------------------------------------------
00068 //  CTZLocalizerAppUi::ConstructL()
00069 //  
00070 // ---------------------------------------------------------
00071 void CTZLocalizerAppUi::ConstructL()
00072     {
00073     BaseConstructL(EAknEnableSkin);
00074 
00075     // Create engine
00076     iTzEngine = CTZLocalizerEngine::NewL();
00077 
00078     // Start dialog
00079     iAppDialog = new (ELeave) CTZLocalizerDialog;
00080     iAppDialog->SetMopParent( this );
00081     iAppDialog->ExecuteLD( R_TZLOCALIZER_DIALOG );
00082     }
00083 
00084 // ----------------------------------------------------
00085 // CTZLocalizerAppUi::~CTZLocalizerAppUi()
00086 // Destructor
00087 // Frees reserved resources
00088 // ----------------------------------------------------
00089 CTZLocalizerAppUi::~CTZLocalizerAppUi()
00090     {    
00091     if (iAppDialog)
00092         {
00093         delete iAppDialog;
00094         }
00095     if(iTzEngine)
00096         {
00097         delete iTzEngine;
00098         }
00099     }
00100 // ----------------------------------------------------
00101 //  CTZLocalizerAppUi::DynInitMenuPaneL()
00102 //  This function is called by the EIKON framework just before
00103 //  it displays a menu pane. Its default implementation is empty, 
00104 //  and by overriding it, the application can set the state of menu 
00105 //  items dynamically according to the state of application data.
00106 // ----------------------------------------------------
00107 void CTZLocalizerAppUi::DynInitMenuPaneL( TInt /*aResourceId*/,CEikMenuPane* /*aMenuPane*/)
00108     {
00109     }
00110 
00111 // ----------------------------------------------------
00112 // CTZLocalizerAppUi::HandleKeyEventL()
00113 // Standard key press information forwasion.
00114 // ----------------------------------------------------
00115 TKeyResponse CTZLocalizerAppUi::HandleKeyEventL( const TKeyEvent& aKeyEvent, 
00116                                               TEventCode aType )
00117     {
00118     if( aType!=EEventKey )
00119         {
00120         return EKeyWasNotConsumed;
00121         }
00122 
00123     switch( aKeyEvent.iCode )
00124         {
00125         case EKeyUpArrow:
00126         case EKeyDownArrow:
00127             {   
00128             if( iAppDialog != NULL )
00129                 {
00130                 TKeyResponse result = iAppDialog->OfferKeyEventL( aKeyEvent,
00131                     aType ); 
00132                 return result;
00133                 }
00134             } 
00135             break;
00136 
00137         default:
00138             break;
00139         }
00140     return EKeyWasNotConsumed;
00141     }
00142 
00143 // ----------------------------------------------------
00144 // CTZLocalizerAppUi::ShowInfoL()
00145 // Briefly shows small information note which contain
00146 // aText as message.
00147 // ----------------------------------------------------
00148 void CTZLocalizerAppUi::ShowInfoL( const TPtrC aText )
00149     {
00150     CAknInformationNote* infoNote = new (ELeave) CAknInformationNote();
00151     infoNote->ExecuteLD( aText );
00152     }
00153 
00154 // ----------------------------------------------------
00155 // CTZLocalizerAppUi::AskPopupChoiseL()
00156 // Shows a popupmenu list containing aList information
00157 // On return to aReturn is copied the chosen text and
00158 // if aIndex != NULL the index # of the chosen item
00159 // is copied to.
00160 // ----------------------------------------------------
00161 TBool CTZLocalizerAppUi::AskPopupChoiseL( const TPtrC aPrompt, 
00162                         CDesC16ArrayFlat* aList, TDes& aReturn )
00163     {
00164     TBool   rVal(EFalse);
00165     
00166     CEikTextListBox* list = new(ELeave) CAknSinglePopupMenuStyleListBox;
00167     CleanupStack::PushL(list);
00168 
00169    // Create popup list and PUSH it.
00170    CAknPopupList* popupList = CAknPopupList::NewL(
00171                                        list, R_AVKON_SOFTKEYS_OK_BACK,
00172                                        AknPopupLayouts::EMenuWindow);
00173    CleanupStack::PushL(popupList);
00174 
00175    // initialize listbox.
00176    
00177    list->ConstructL(popupList, CEikListBox::ELeftDownInViewRect);
00178    list->CreateScrollBarFrameL(ETrue);
00179    list->ScrollBarFrame()->SetScrollBarVisibilityL(
00180                                CEikScrollBarFrame::EOff,
00181                                CEikScrollBarFrame::EAuto);
00182 
00183     list->Model()->SetItemTextArray( aList );
00184     popupList->SetTitleL(aPrompt);
00185 
00186     //If the user enters ok on a selection
00187     if (popupList->ExecuteLD())
00188         {
00189         aReturn.Copy( (*aList)[list->CurrentItemIndex()] );
00190         
00191         rVal = ETrue;
00192     }
00193     CleanupStack::Pop(popupList);
00194     CleanupStack::PopAndDestroy(list);
00195 
00196     return rVal;
00197     }
00198 
00199 // ----------------------------------------------------
00200 // CTZLocalizerAppUi::QueryTextL()
00201 // Shows a dialog with aPrompt note and waits for text 
00202 // input which is then copied to aReturn 
00203 // ----------------------------------------------------
00204 TBool CTZLocalizerAppUi::QueryTextL( const TPtrC aPrompt, TDes& aReturn )
00205     {
00206     CAknTextQueryDialog* textQuery = CAknTextQueryDialog::NewL( aReturn );
00207     CleanupStack::PushL( textQuery );
00208 
00209     textQuery->SetPromptL( aPrompt );
00210     CleanupStack::Pop( textQuery );
00211 
00212     return textQuery->ExecuteLD( R_DATAQUERY_DATA_QUERY );
00213     }
00214 
00215 // ----------------------------------------------------
00216 // CTZLocalizerAppUi::HandleCommandL()
00217 // Normal menu command handling
00218 // ----------------------------------------------------
00219 void CTZLocalizerAppUi::HandleCommandL( TInt aCommand )
00220     {
00221     switch ( aCommand )
00222         {
00223         case EAknSoftkeyExit:
00224         case EEikCmdExit:
00225             {
00226             Exit();
00227             break;
00228             }
00229     // ------------------------------------------------
00230     // Ask for (partial) city name and show the city
00231     // information in the lisbox.
00232         case ETZLocalizerCmdSearchCities:
00233             SearchCitiesL();
00234             break;
00235 
00236     // ------------------------------------------------
00237     // Add new city to world server database.
00238         case ETZLocalizerCmdAddCity:
00239             CreateNewCityL();
00240             break;
00241     // ------------------------------------------------
00242     // remove city from world server database
00243         case ETZLocalizerCmdRemoveCity:
00244             DeleteCityL();
00245             break;
00246             
00247     // Search Cities In Groups
00248         case ETZLocalizerCmdSearchCitiesInGroup:
00249             SearchCitiesInGroupsL();
00250             break;       
00251     // ------------------------------------------------
00252         default:
00253             break;      
00254         }
00255     }
00256 
00257 // ---------------------------------------------------------
00258 //  CTZLocalizerAppUi::FillListBoxL()
00259 //  Fill main dialog listbox with city information
00260 // ---------------------------------------------------------
00261 void CTZLocalizerAppUi::FillListBoxL( CTzLocalizedCity* aLocalizedCity )
00262     {
00263     TBuf<KTextBufferLength>   formatBuffer;
00264     
00265     CDesC16ArrayFlat* cityInfo = 
00266         new (ELeave)CDesC16ArrayFlat( KArrayGranularity );
00267     CleanupStack::PushL( cityInfo );
00268 
00269     cityInfo->Reset();
00270 
00271     formatBuffer.Copy( KCityName2 );
00272     formatBuffer.Append( iTzEngine->GetCityName(aLocalizedCity) );
00273     cityInfo->AppendL( formatBuffer );
00274 
00275     formatBuffer.Copy( KCityTimeZoneID );
00276     TBuf<KBufSize> temp;
00277     temp.Num( iTzEngine->GetCityTimeZoneId(aLocalizedCity) );
00278     formatBuffer.Append( temp );
00279     cityInfo->AppendL( formatBuffer );
00280 
00281     formatBuffer.Copy( KCityGroupID );
00282     temp.Num( iTzEngine->GetCityGroupId(aLocalizedCity) );
00283     formatBuffer.Append( temp );
00284     cityInfo->AppendL( formatBuffer );
00285       
00286     formatBuffer.Copy( KCityLocalTime );
00287     HBufC* time;
00288     time = iTzEngine->GetCityLocalTimeL(aLocalizedCity);
00289     formatBuffer.Append( *time );
00290     
00291     cityInfo->AppendL( formatBuffer );
00292 
00293     if (iAppDialog)
00294         {
00295         iAppDialog->SetListBoxTextL( cityInfo );
00296         }
00297     
00298     delete time;
00299     CleanupStack::PopAndDestroy( cityInfo );
00300     }
00301 // ---------------------------------------------------------
00302 //  CTZLocalizerAppUi::FillListBoxL()
00303 //  Fill main dialog listbox with city information
00304 // ---------------------------------------------------------
00305 void CTZLocalizerAppUi::FillListBoxL( CDesC16ArrayFlat* aArray )
00306     {
00307     if (iAppDialog)
00308         {
00309         iAppDialog->SetListBoxTextL( aArray );
00310         }
00311     }
00312     
00313 // ---------------------------------------------------------
00314 //  CTZLocalizerAppUi::ClearListBoxL()
00315 //  Clear main dialog listbox
00316 // ---------------------------------------------------------
00317 void CTZLocalizerAppUi::ClearListBoxL()
00318     {
00319     CDesC16ArrayFlat* nothing = new (ELeave)CDesC16ArrayFlat(1);
00320     CleanupStack::PushL( nothing );    
00321     if (iAppDialog)
00322         {
00323         iAppDialog->SetListBoxTextL( nothing );
00324         }
00325     CleanupStack::PopAndDestroy(); // nothing       
00326     }
00327 
00328 // ---------------------------------------------------------
00329 //  CTZLocalizerAppUi::CreateCityUIL()
00330 //  Ask all the information about new city and create it.
00331 // ---------------------------------------------------------
00332 void CTZLocalizerAppUi::CreateNewCityL()
00333     {
00334     TBuf<KBufSize> newCity;
00335     TBuf<KBufSize> timeZoneID;
00336     TBuf<KBufSize> groupID;
00337     TPtrC    text;
00338 
00339     // ask a new city name
00340     text.Set(KNewCityName);
00341     if( QueryTextL( text, newCity) == EFalse )
00342         {
00343         return;
00344         }
00345     
00346     CDesC16ArrayFlat* timeZones = iTzEngine->GetAllTimeZonesL();
00347     
00348     // choose time zone ID
00349     text.Set(KChooseTimeZoneID);
00350     if( AskPopupChoiseL( text, timeZones, timeZoneID ) == EFalse )
00351         {
00352         return;
00353         }
00354         
00355     CDesC16ArrayFlat* groupdIDs = iTzEngine->GetAllGroupIDL();
00356         
00357     // choose group zone ID
00358     text.Set(KChooseCityGroupID);
00359     if( AskPopupChoiseL( text, groupdIDs, groupID ) == EFalse )
00360         {
00361         return;
00362         }
00363 
00364     TInt tzID;
00365     TLex lexTimeZone(timeZoneID); 
00366     lexTimeZone.Val(tzID); 
00367     
00368     TInt grID;
00369     TLex lexGroup(groupID); 
00370     lexGroup.Val(grID); 
00371 
00372     CTzLocalizedCity* localizedCity = iTzEngine->AddCityL(tzID, newCity, grID);
00373 
00374     if( !localizedCity )
00375         {
00376         TPtrC msg(KErrorAddingCity);
00377         ShowInfoL( msg );
00378         }
00379     else
00380         {
00381         TPtrC msg(KCityAdded);
00382         ShowInfoL( msg );
00383         FillListBoxL( localizedCity );
00384         }
00385     delete localizedCity;
00386     }
00387 
00388 // ---------------------------------------------------------
00389 //  CTZLocalizerAppUi::SearchCitiesL()
00390 //  Search a city and show it in the listbox
00391 // ---------------------------------------------------------
00392 void CTZLocalizerAppUi::SearchCitiesL()
00393     {
00394     
00395     TBuf<KTextBufferLength>    placeName;
00396               
00397     CTzLocalizedCity* localizedCity;  
00398     // ask for the city name
00399     TPtrC msg(KCityName);
00400     if( QueryTextL( msg, placeName) == false )
00401         {
00402         return;
00403         }
00404         
00405     // get the city matching the search string
00406     localizedCity = iTzEngine->FindCityL( placeName );
00407         
00408     if( !localizedCity )
00409         {
00410         TPtrC msg(KNoMatchingCity);
00411         ShowInfoL( msg );
00412         }
00413     else
00414         {
00415         FillListBoxL( localizedCity );
00416         }   
00417     delete localizedCity;
00418     }
00419 
00420 
00421 // ---------------------------------------------------------
00422 //  CTZLocalizerAppUi::DeleteCityL()
00423 //  Delete city from world server database
00424 // ---------------------------------------------------------
00425 void CTZLocalizerAppUi::DeleteCityL()
00426     {
00427     CDesC16ArrayFlat* citiesList = iTzEngine->GetAllCitiesL();
00428     TBuf<KTextBufferLength> cityName;
00429 
00430     // ask for the city name with the list of all cities
00431     TPtrC msg(KDeleteCity);
00432     
00433     if( AskPopupChoiseL( msg, citiesList, cityName ) == EFalse )
00434         {
00435         return;
00436         }
00437         
00438     iTzEngine->RemoveCityL(cityName);
00439     
00440     // clear display and show "city removed" message
00441     ClearListBoxL();
00442     TPtrC msgRemoved(KCityRemoved);
00443     ShowInfoL( msgRemoved );
00444     }
00445     
00446 // ---------------------------------------------------------
00447 //  CTZLocalizerAppUi::SearchCitiesInGroupsL()
00448 //  Search a city and show it in the listbox
00449 // ---------------------------------------------------------
00450 void CTZLocalizerAppUi::SearchCitiesInGroupsL()
00451     {
00452     TBuf<KBufSize> groupID;
00453     TPtrC    text;
00454     
00455     CDesC16ArrayFlat* groupIDs = iTzEngine->GetAllGroupIDL();
00456     
00457     // choose group zone ID
00458     text.Set(KChooseCityGroupID);
00459     if( AskPopupChoiseL( text, groupIDs, groupID ) == EFalse )
00460         {
00461         return;
00462         }
00463     
00464     TInt grID;
00465     TLex lexGroup(groupID); 
00466     lexGroup.Val(grID); 
00467     
00468     CDesC16ArrayFlat* citiest = iTzEngine->FindCitiesInGroupL(grID);
00469     FillListBoxL(citiest);
00470     
00471     citiest->Reset();
00472     delete citiest;
00473     }
00474 
00475 void CTZLocalizerAppUi::HandleResourceChangeL( TInt aType )
00476     {
00477     CAknAppUi::HandleResourceChangeL(aType);
00478     
00479     if ( aType==KEikDynamicLayoutVariantSwitch )
00480         {
00481         if (iAppDialog)
00482             {
00483             TRect rect;
00484             AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane,rect);
00485             iAppDialog->SetRect(rect);
00486             // NOTE: Remember to send KEikDynamicLayoutVariantSwitch also to dialog
00487             iAppDialog->HandleResourceChange( aType );
00488             }
00489         }   
00490     }
00491     
00492 // End of File  

Generated by  doxygen 1.6.2