locationlandmarksrefappfors60/Src/LandmarksEditDialog.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2004-2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implements the CLandmarksEditDialog class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <eikmenub.h>
       
    20 #include <akntitle.h>
       
    21 #include <aknnotewrappers.h>
       
    22 #include <aknappui.h>
       
    23 #include <e32math.h>
       
    24 #include <eikcapc.h>
       
    25 #include <aknselectionlist.h>
       
    26 #include <aknlocationed.h>
       
    27 #include <AknWaitDialog.h> 
       
    28 #include <avkon.rsg>
       
    29 
       
    30 #include <lbsposition.h>
       
    31 #include <lbspositioninfo.h>
       
    32 
       
    33 #include <EPos_CPosLandmark.h>
       
    34 #include <EPos_CPosLandmarkCategory.h>
       
    35 
       
    36 #include "LmRefApp.hrh"
       
    37 #include <lmrefapp.rsg>
       
    38 
       
    39 #include "LandmarksUtils.h"
       
    40 #include "LandmarksCommonData.h"
       
    41 #include "LandmarksEditDialog.h"
       
    42 #include "LandmarksCategoriesDialog.h"
       
    43 #include "LandmarksPositionRequest.h"
       
    44 #include "LandmarksApplicationEngine.h"
       
    45 #include "LandmarksAppUi.h"
       
    46 #include "LandmarksLmCategoriesModel.h"
       
    47 
       
    48 // ============================ LOCAL FUNCTIONS ===============================
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 //  Gets value from EDWIN and puts to cleanup stack
       
    52 //  @param aEdwin must be EDWIN control
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 HBufC* FetchTextLC( CCoeControl* aEdwin )
       
    56     {
       
    57     CEikEdwin* editor = static_cast <CEikEdwin*> ( aEdwin );
       
    58     HBufC* text = editor->GetTextInHBufL();
       
    59     if ( text )
       
    60         {
       
    61         CleanupStack::PushL( text );
       
    62         }
       
    63     else
       
    64         {
       
    65         // create empty text
       
    66         text = HBufC::NewLC( 0 );
       
    67         }
       
    68     return text;
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 //  Retrieves a long float from am EDWIN editor.
       
    73 //  @param aDouble double float that is retrieved from editor.
       
    74 //  @param aEdwin must be EDWIN control
       
    75 //  @return ETrue if successful, EFalse otherwise
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 TBool FetchDoubleL( TReal& aDouble, CCoeControl* aEdwin )
       
    79     {
       
    80     TBool success = EFalse;
       
    81     TLocale locale;
       
    82 
       
    83     HBufC* text = FetchTextLC( aEdwin );
       
    84 
       
    85     if ( text->Length() )
       
    86         {
       
    87         // Try to extract value
       
    88         TLex fieldLex( *text );
       
    89         success = ( KErrNone == fieldLex.Val( aDouble, locale.DecimalSeparator() ) );
       
    90         }
       
    91     else
       
    92         {
       
    93         // User has indicated that he/she wants this value to be undefined.
       
    94         TRealX nan;
       
    95         nan.SetNaN();
       
    96         aDouble = nan;
       
    97         success = ETrue;
       
    98         }
       
    99 
       
   100     CleanupStack::PopAndDestroy( text );
       
   101     return success;
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 //  Retrieves a float from am EDWIN editor.
       
   106 //  @param aFloat float that is retrieved from editor.
       
   107 //  @param aEdwin must be EDWIN control
       
   108 //  @return ETrue if successful, EFalse otherwise
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 TBool FetchFloatL( TReal32& aFloat, CCoeControl* aEdwin )
       
   112     {
       
   113     TBool success = EFalse;
       
   114     TLocale locale;
       
   115 
       
   116     HBufC* text = FetchTextLC( aEdwin );
       
   117 
       
   118     if ( text->Length() )
       
   119         {
       
   120         // Try to extract value
       
   121         TLex fieldLex( *text );
       
   122         success = ( KErrNone == fieldLex.Val( aFloat, locale.DecimalSeparator() ) );
       
   123         }
       
   124     else
       
   125         {
       
   126         // User has indicated that he/she wants this value to be undefined.
       
   127         TRealX nan;
       
   128         nan.SetNaN();
       
   129         aFloat = nan;
       
   130         success = ETrue;
       
   131         }
       
   132 
       
   133     CleanupStack::PopAndDestroy( text );
       
   134     return success;
       
   135     }
       
   136 
       
   137 // ============================ MEMBER FUNCTIONS ===============================
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 CLandmarksEditDialog::CLandmarksEditDialog(
       
   143     CPosLandmark& aLandmark,
       
   144     TBool aUseCurrentLocation,
       
   145     TBool& aEdited,
       
   146     CLandmarksApplicationEngine& aEngine,
       
   147     TInt aInitialFocusFieldId )
       
   148 :   iOriginalLandmark( aLandmark ),
       
   149     iUseCurrentLocation( aUseCurrentLocation ),
       
   150     iEdited( aEdited ),
       
   151     iEngine( aEngine ),
       
   152     iInitialFocusFieldId( aInitialFocusFieldId )
       
   153     {
       
   154     iEdited = EFalse;
       
   155 
       
   156     // Configure real format
       
   157     const TChar KDecimalChar = '.';
       
   158     iRealFormat.iPlaces = KNrOfDecimals;
       
   159     iRealFormat.iPoint = KDecimalChar;
       
   160     iRealFormat.iTriLen = 0;
       
   161     iRealFormat.iWidth = KPosLmMaxTextFieldLength;
       
   162     iRealFormat.iType = KRealFormatFixed;
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void CLandmarksEditDialog::ConstructL()
       
   169     {
       
   170     CAknForm::ConstructL( R_LMREFAPP_EDIT_MENU_BAR );
       
   171 
       
   172     // Save original title
       
   173     CEikStatusPane* statusPane = iAvkonAppUi->StatusPane();
       
   174     iTitlePane = ( CAknTitlePane* ) statusPane->ControlL(
       
   175         TUid::Uid( EEikStatusPaneUidTitle ) );
       
   176     iOriginalTitle = iTitlePane->Text()->AllocL();
       
   177 
       
   178     // Copy data of original landmark
       
   179     iLandmark = CPosLandmark::NewL( iOriginalLandmark );
       
   180 
       
   181     // Create iPositionRequest
       
   182     HBufC* appName = iCoeEnv->AllocReadResourceAsDes16LC( R_LMREFAPP_APP_TITLE );
       
   183     iPositionRequest = CLandmarksPositionRequest::NewL( *appName, this );
       
   184     CleanupStack::PopAndDestroy( appName );
       
   185 
       
   186     // Initialize iCategoryIds
       
   187     iLandmark->GetCategoriesL( iCategoryIds );
       
   188 
       
   189     iLmCategoriesModel =
       
   190             CLandmarksLmCategoriesModel::NewL( iEngine, iCategoryIds );
       
   191 
       
   192     // Initialise states of real value editors
       
   193     for ( TInt i = ELatitudeEditor; i <= ERadiusEditor; i++ )
       
   194         {
       
   195         User::LeaveIfError( iIsRealValueEdited.Append( EFalse ) );
       
   196         }
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 CLandmarksEditDialog* CLandmarksEditDialog::NewLC(
       
   203     CPosLandmark& aLandmark,
       
   204     TBool aUseCurrentLocation,
       
   205     TBool& aEdited,
       
   206     CLandmarksApplicationEngine& aEngine,
       
   207     TInt aInitialFocusFieldId )
       
   208     {
       
   209     CLandmarksEditDialog* self = new ( ELeave ) CLandmarksEditDialog(
       
   210         aLandmark, aUseCurrentLocation, aEdited, aEngine, aInitialFocusFieldId );
       
   211     CleanupStack::PushL( self );
       
   212     self->ConstructL();
       
   213     return self;
       
   214     }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 CLandmarksEditDialog::~CLandmarksEditDialog()
       
   220     {
       
   221     CloseWaitDialog();
       
   222 
       
   223     delete iLandmark;
       
   224     delete iAppName;
       
   225     delete iPositionRequest;
       
   226     iCategoryIds.Close();
       
   227     iIsRealValueEdited.Close();
       
   228     delete iLmCategoriesModel;
       
   229 
       
   230     // Restore title pane. Ownership of iTitlePane transferred.
       
   231     if ( iTitlePane && iOriginalTitle )
       
   232         {
       
   233         iTitlePane->SetText( iOriginalTitle );
       
   234         }
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void CLandmarksEditDialog::DynInitMenuPaneL(
       
   241     TInt aResourceId,
       
   242     CEikMenuPane* aMenuPane )
       
   243     {
       
   244     CAknForm::DynInitMenuPaneL( aResourceId, aMenuPane );
       
   245 
       
   246     if ( aResourceId == R_AVKON_FORM_MENUPANE )
       
   247         {
       
   248         // We do not want to allow the user to modify our Form therefore
       
   249         // Disable the Label, Add, Delete, and Save Field
       
   250         aMenuPane->SetItemDimmed( EAknFormCmdLabel, ETrue );
       
   251         aMenuPane->SetItemDimmed( EAknFormCmdAdd, ETrue );
       
   252         aMenuPane->SetItemDimmed( EAknFormCmdDelete, ETrue );
       
   253         aMenuPane->SetItemDimmed( EAknFormCmdSave, ETrue );
       
   254 
       
   255         aMenuPane->SetItemDimmed( ELandmarksUpdateToCurrentLocation,
       
   256             Control( ELandmarkCategoryField )->IsFocused() );
       
   257         aMenuPane->SetItemDimmed( ELandmarksEditCategories,
       
   258             !Control( ELandmarkCategoryField )->IsFocused() );
       
   259         }
       
   260     }
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CLandmarksEditDialog::ProcessCommandL( TInt aCommandId )
       
   266 	{
       
   267 	CAknForm::ProcessCommandL( aCommandId );
       
   268 
       
   269         switch ( aCommandId )
       
   270             {
       
   271             case ELandmarksUpdateToCurrentLocation:
       
   272                 UpdateToCurrentLocationL();
       
   273                 break;
       
   274 
       
   275             case ELandmarksResetCoordinates:
       
   276                 ResetCoordinatesL();
       
   277                 break;
       
   278 
       
   279             case ELandmarksEditCategories:
       
   280                 EditCategoriesL();
       
   281                 break;
       
   282 
       
   283             default:
       
   284                 break;
       
   285             }
       
   286 	}
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 void CLandmarksEditDialog::NotifyOperationProgressL(
       
   292     TOperation aOperation,
       
   293     TInt /*aProgress*/,
       
   294     TInt aErrorCode )
       
   295     {
       
   296     if ( aErrorCode == KErrNone &&
       
   297         aOperation == EAcquiringLocation )
       
   298         {
       
   299         // Update location fields with new location
       
   300         TPosition position;
       
   301         iPositionInfo.GetPosition( position );
       
   302         ConfigureLocationFieldsL( position );
       
   303         SetChangesPending( ETrue );
       
   304 
       
   305         // Indicate that editors have been updated
       
   306         for ( TInt i = ELatitudeEditor; i <= EVerticalAccEditor; i++ )
       
   307             {
       
   308             iIsRealValueEdited[i] = ETrue;
       
   309             }
       
   310         }
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 TBool CLandmarksEditDialog::OkToExitL( TInt aButtonId )
       
   317 	{
       
   318     if ( aButtonId == EAknSoftkeyDone )
       
   319         {
       
   320         // exit from CAknForm's "edit" mode
       
   321         return CAknForm::OkToExitL( EAknSoftkeyBack );
       
   322         }
       
   323     else
       
   324         {
       
   325     	return CAknForm::OkToExitL( aButtonId );
       
   326         }
       
   327 	}
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // -----------------------------------------------------------------------------
       
   331 //
       
   332 void CLandmarksEditDialog::PreLayoutDynInitL()
       
   333     {
       
   334     SetEditableL( ETrue );
       
   335     ConfigureFieldsL();
       
   336     ConfigureCategoryFieldL();
       
   337     }
       
   338 
       
   339 // -----------------------------------------------------------------------------
       
   340 // -----------------------------------------------------------------------------
       
   341 //
       
   342 void CLandmarksEditDialog::PostLayoutDynInitL()
       
   343     {
       
   344     SetChangesPending( ETrue );
       
   345 
       
   346     UpdateTitleBarL();
       
   347     CAknForm::PostLayoutDynInitL();
       
   348 
       
   349     if ( iUseCurrentLocation )
       
   350         {
       
   351         UpdateToCurrentLocationL();
       
   352         }
       
   353     }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // -----------------------------------------------------------------------------
       
   357 //
       
   358 void CLandmarksEditDialog::SetInitialCurrentLine()
       
   359     {
       
   360     CAknForm::SetInitialCurrentLine();
       
   361 
       
   362     if ( iInitialFocusFieldId >= ELandmarkNameField &&
       
   363         iInitialFocusFieldId <= ELandmarkCountryField )
       
   364         {
       
   365         TInt err = KErrNone;
       
   366         TRAP( err, TryChangeFocusToL( iInitialFocusFieldId ) );
       
   367         }
       
   368     }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CLandmarksEditDialog::HandleControlEventL(
       
   374     CCoeControl* aControl,
       
   375     TCoeEvent aEventType )
       
   376 	{
       
   377     CAknForm::HandleControlEventL( aControl, aEventType );
       
   378 
       
   379     if ( aEventType == EEventStateChanged )
       
   380         {
       
   381         if ( aControl == ControlOrNull( ELandmarkLatitudeField ) )
       
   382             {
       
   383             iIsRealValueEdited[ELatitudeEditor] = ETrue;
       
   384             }
       
   385         else if ( aControl == ControlOrNull( ELandmarkLongitudeField ) )
       
   386             {
       
   387             iIsRealValueEdited[ELongitudeEditor] = ETrue;
       
   388             }
       
   389         else if ( aControl == ControlOrNull( ELandmarkAltitudeField ) )
       
   390             {
       
   391             iIsRealValueEdited[EAltitudeEditor] = ETrue;
       
   392             }
       
   393         else if ( aControl == ControlOrNull( ELandmarkHorAccField ) )
       
   394             {
       
   395             iIsRealValueEdited[EHorizontalAccEditor] = ETrue;
       
   396             }
       
   397         else if ( aControl == ControlOrNull( ELandmarkVerAccField ) )
       
   398             {
       
   399             iIsRealValueEdited[EVerticalAccEditor] = ETrue;
       
   400             }
       
   401         else if ( aControl == ControlOrNull( ELandmarkRadiusField ) )
       
   402             {
       
   403             iIsRealValueEdited[ERadiusEditor] = ETrue;
       
   404             }
       
   405         else if ( aControl == ControlOrNull( ELandmarkNameField ) )
       
   406             {
       
   407             UpdateTitleBarL( ETrue ); // get it from name field
       
   408             }
       
   409         }
       
   410 	}
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // -----------------------------------------------------------------------------
       
   414 //
       
   415 void CLandmarksEditDialog::HandleResourceChange( TInt aType )
       
   416 	{
       
   417 	if ( aType == KEikDynamicLayoutVariantSwitch )
       
   418 		{
       
   419 		CAknForm::HandleResourceChange( aType );
       
   420 		}
       
   421 	}
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // -----------------------------------------------------------------------------
       
   425 //
       
   426 TBool CLandmarksEditDialog::SaveFormDataL()
       
   427     {
       
   428     TErrorInForm error = FetchAndSaveFormDataL();
       
   429     if ( error )
       
   430         {
       
   431         ProcessErrorsInFormL( error );
       
   432         // Leave to interrupt saving of data. Other error codes than KErrNone
       
   433         // result in an additional error dialog.
       
   434         User::Leave( KErrNone );
       
   435         }
       
   436     else
       
   437         {
       
   438         // input is valid, save everything to original landmark
       
   439         TPtrC text;
       
   440 
       
   441         iLandmark->GetLandmarkName( text );
       
   442         iOriginalLandmark.SetLandmarkNameL( text );
       
   443 
       
   444         iLandmark->GetLandmarkDescription( text );
       
   445         iOriginalLandmark.SetLandmarkDescriptionL( text );
       
   446 
       
   447         iOriginalLandmark.RemoveLandmarkAttributes( CPosLandmark::ECategoryInfo );
       
   448         for ( TInt i = 0; i < iCategoryIds.Count(); i++ )
       
   449             {
       
   450             iOriginalLandmark.AddCategoryL( iCategoryIds[i] );
       
   451             }
       
   452 
       
   453         CopyCoordinateDataL( *iLandmark, iOriginalLandmark );
       
   454         CopyAddressDataL( *iLandmark, iOriginalLandmark );
       
   455         }
       
   456 
       
   457     iEdited = ETrue;
       
   458     return ETrue;
       
   459     }
       
   460 
       
   461 // -----------------------------------------------------------------------------
       
   462 // -----------------------------------------------------------------------------
       
   463 //
       
   464 CLandmarksEditDialog::TErrorInForm CLandmarksEditDialog::FetchAndSaveFormDataL()
       
   465     {
       
   466     HBufC* text = NULL;
       
   467 
       
   468     // name
       
   469     text = FetchTextLC( Control( ELandmarkNameField ) );
       
   470     if ( text->Length() )
       
   471         {
       
   472         iLandmark->SetLandmarkNameL( *text );
       
   473         }
       
   474     else
       
   475         {
       
   476         return EEmptyLandmarkName;
       
   477         }
       
   478     CleanupStack::PopAndDestroy( text );
       
   479 
       
   480     // description
       
   481     text = FetchTextLC( Control( ELandmarkDescField ) );
       
   482     iLandmark->SetLandmarkDescriptionL( *text );
       
   483     CleanupStack::PopAndDestroy( text );
       
   484 
       
   485     // categories
       
   486     iLandmark->RemoveLandmarkAttributes( CPosLandmark::ECategoryInfo );
       
   487     for ( TInt i = 0; i < iCategoryIds.Count(); i++ )
       
   488         {
       
   489         iLandmark->AddCategoryL( iCategoryIds[i] );
       
   490         }
       
   491 
       
   492     // Address fields
       
   493     SaveAddressDataL();
       
   494 
       
   495     // coordinate fields
       
   496     TErrorInForm error = SaveCoordinateDataL();
       
   497     if ( error )
       
   498         {
       
   499         return error;
       
   500         }
       
   501 
       
   502     return ENoErrors;
       
   503     }
       
   504 
       
   505 // -----------------------------------------------------------------------------
       
   506 // -----------------------------------------------------------------------------
       
   507 //
       
   508 void CLandmarksEditDialog::SaveAddressDataL()
       
   509     {
       
   510     HBufC* text = NULL;
       
   511 
       
   512     iLandmark->RemovePositionField( EPositionFieldStreet );
       
   513     iLandmark->RemovePositionField( EPositionFieldPostalCode );
       
   514     iLandmark->RemovePositionField( EPositionFieldCity );
       
   515     iLandmark->RemovePositionField( EPositionFieldCountry );
       
   516 
       
   517     // street
       
   518     text = FetchTextLC( Control( ELandmarkStreetField ) );
       
   519     if ( text->Length() )
       
   520         {
       
   521         iLandmark->SetPositionFieldL( EPositionFieldStreet, *text );
       
   522         }
       
   523     CleanupStack::PopAndDestroy( text );
       
   524 
       
   525     // postal code
       
   526     text = FetchTextLC( Control( ELandmarkPostalField ) );
       
   527     if ( text->Length() )
       
   528         {
       
   529         iLandmark->SetPositionFieldL( EPositionFieldPostalCode, *text );
       
   530         }
       
   531     CleanupStack::PopAndDestroy( text );
       
   532 
       
   533     // city
       
   534     text = FetchTextLC( Control( ELandmarkCityField ) );
       
   535     if ( text->Length() )
       
   536         {
       
   537         iLandmark->SetPositionFieldL( EPositionFieldCity, *text );
       
   538         }
       
   539     CleanupStack::PopAndDestroy( text );
       
   540 
       
   541     // country
       
   542     text = FetchTextLC( Control( ELandmarkCountryField ) );
       
   543     if ( text->Length() )
       
   544         {
       
   545         iLandmark->SetPositionFieldL( EPositionFieldCountry, *text );
       
   546         }
       
   547     CleanupStack::PopAndDestroy( text );
       
   548     }
       
   549 
       
   550 // -----------------------------------------------------------------------------
       
   551 // -----------------------------------------------------------------------------
       
   552 //
       
   553 CLandmarksEditDialog::TErrorInForm CLandmarksEditDialog::SaveCoordinateDataL()
       
   554     {
       
   555     TLocality existingLocation;
       
   556     TBool locationExists = ( iLandmark->GetPosition( existingLocation ) == KErrNone );
       
   557 
       
   558     TReal latitude = existingLocation.Latitude();
       
   559     TReal longitude = existingLocation.Longitude();
       
   560     TReal32 altitude = existingLocation.Altitude();
       
   561     TReal32 horAcc = existingLocation.HorizontalAccuracy();
       
   562     TReal32 verAcc = existingLocation.VerticalAccuracy();
       
   563 
       
   564     TPosition tempPos;
       
   565 
       
   566     // latitude
       
   567     if ( iIsRealValueEdited[ELatitudeEditor] || !locationExists )
       
   568         {
       
   569         CAknLocationEditor* editor = static_cast<CAknLocationEditor*>( Control ( ELandmarkLatitudeField ) );
       
   570     	editor->Get( tempPos );
       
   571     	latitude = tempPos.Latitude();
       
   572         }
       
   573 
       
   574     // longitude
       
   575     if ( iIsRealValueEdited[ELongitudeEditor] || !locationExists )
       
   576         {
       
   577         CAknLocationEditor* editor = static_cast<CAknLocationEditor*>( Control ( ELandmarkLongitudeField ) );
       
   578     	editor->Get( tempPos );
       
   579     	longitude = tempPos.Longitude();
       
   580         }
       
   581 
       
   582     // altitude
       
   583     if ( iIsRealValueEdited[EAltitudeEditor] || !locationExists )
       
   584         {
       
   585         if ( !FetchFloatL( altitude, Control( ELandmarkAltitudeField ) ) )
       
   586             {
       
   587             return EWrongAltitude;
       
   588             }
       
   589         }
       
   590 
       
   591     // horizontal accuracy
       
   592     if ( iIsRealValueEdited[EHorizontalAccEditor] || !locationExists )
       
   593         {
       
   594         if ( !FetchFloatL( horAcc, Control( ELandmarkHorAccField ) ) )
       
   595             {
       
   596             return EWrongHorAcc;
       
   597             }
       
   598         }
       
   599 
       
   600     // vertical accuracy
       
   601     if ( iIsRealValueEdited[EVerticalAccEditor] || !locationExists )
       
   602         {
       
   603         if ( !FetchFloatL( verAcc, Control( ELandmarkVerAccField ) ) )
       
   604             {
       
   605             return EWrongVerAcc;
       
   606             }
       
   607         }
       
   608 
       
   609     // Update location. There are some rules specified by TLocality and
       
   610     // TCoordinate how to update a location. These rules are taken into
       
   611     // account below when validating the location data.
       
   612     if ( Math::IsNaN( latitude ) && Math::IsNaN( longitude ) )
       
   613         {
       
   614         // If lat long are undefined, then alt and hAcc
       
   615         // must be undefined
       
   616         if ( !Math::IsNaN( horAcc ) )
       
   617             {
       
   618             return ENoLatLonButHorAcc;
       
   619             }
       
   620         else if ( !Math::IsNaN( altitude ) )
       
   621             {
       
   622             return ENoLatLonButAltitude;
       
   623             }
       
   624         else if ( !Math::IsNaN( verAcc ) )
       
   625             {
       
   626             return ENoAltButVerAcc;
       
   627             }
       
   628         else
       
   629             {
       
   630             // None position fields are set, Remove position info.
       
   631             iLandmark->RemoveLandmarkAttributes( CPosLandmark::EPosition );
       
   632             }
       
   633         }
       
   634     else if ( !Math::IsNaN( latitude ) && !Math::IsNaN( longitude ) )
       
   635         {
       
   636         if ( !Math::IsNaN( horAcc ) && horAcc < 0 )
       
   637             {
       
   638             // If lat long are defined, hAcc must be positive if defined
       
   639             return EWrongHorAcc;
       
   640             }
       
   641         else if ( Math::IsNaN( altitude ) && !Math::IsNaN( verAcc ) )
       
   642             {
       
   643             // if altitude is undefined, then vertical accuracy
       
   644             // must be undefined
       
   645             return ENoAltButVerAcc;
       
   646             }
       
   647         else if ( !Math::IsNaN( verAcc ) && verAcc < 0 )
       
   648             {
       
   649             // vertical accuracy must be positive if defined
       
   650             return EWrongVerAcc;
       
   651             }
       
   652         else
       
   653             {
       
   654             // It is allowed to set the new locality
       
   655             TCoordinate coordinate( latitude, longitude, altitude );
       
   656             TLocality newLocation( coordinate, horAcc, verAcc );
       
   657             iLandmark->SetPositionL( newLocation );
       
   658             }
       
   659         }
       
   660     else
       
   661         {
       
   662         // Inform user that no valid lat/long has been entered
       
   663         return EOnlyLatOrLon;
       
   664         }
       
   665 
       
   666     // coverage radius
       
   667     TReal32 radius;
       
   668     TInt radiusExists = ( iLandmark->GetCoverageRadius( radius ) == KErrNone );
       
   669     if ( iIsRealValueEdited[ERadiusEditor] || !radiusExists )
       
   670         {
       
   671         if ( !FetchFloatL( radius, Control( ELandmarkRadiusField ) ) )
       
   672             {
       
   673             return EWrongRadius;
       
   674             }
       
   675         }
       
   676     if ( !Math::IsNaN( radius ) && radius < 0 )
       
   677         {
       
   678         return EWrongRadius;
       
   679         }
       
   680     iLandmark->SetCoverageRadius( radius );
       
   681 
       
   682     return ENoErrors;
       
   683     }
       
   684 
       
   685 // -----------------------------------------------------------------------------
       
   686 // -----------------------------------------------------------------------------
       
   687 //
       
   688 void CLandmarksEditDialog::ConfigureFieldsL()
       
   689     {
       
   690     TPtrC textFieldValue;
       
   691 
       
   692     // Configure landmark name
       
   693     textFieldValue.Set( KNullDesC );
       
   694     iLandmark->GetLandmarkName( textFieldValue );
       
   695 
       
   696     SetTextFieldL( ELandmarkNameField, textFieldValue );
       
   697 
       
   698     // Configure description
       
   699     textFieldValue.Set( KNullDesC );
       
   700     iLandmark->GetLandmarkDescription( textFieldValue );
       
   701 
       
   702     SetTextFieldL( ELandmarkDescField, textFieldValue );
       
   703 
       
   704     // all address fields
       
   705     ConfigureAddressFieldsL();
       
   706 
       
   707     // Configure location fields
       
   708     TLocality location;
       
   709     iLandmark->GetPosition( location );
       
   710     ConfigureLocationFieldsL( location );
       
   711 
       
   712     // Configure coverage Radius
       
   713     TReal32 coverageRadius;
       
   714     TBuf<KPosLmMaxTextFieldLength> radiusBuf;
       
   715     TInt res = iLandmark->GetCoverageRadius( coverageRadius );
       
   716     if ( res != KErrNotFound )
       
   717         {
       
   718         radiusBuf.AppendNum( coverageRadius, iRealFormat );
       
   719         }
       
   720 
       
   721     SetTextFieldL( ELandmarkRadiusField, radiusBuf );
       
   722     }
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // -----------------------------------------------------------------------------
       
   726 //
       
   727 void CLandmarksEditDialog::ConfigureAddressFieldsL()
       
   728     {
       
   729     TPtrC textFieldValue;
       
   730 
       
   731     // street
       
   732     textFieldValue.Set( KNullDesC );
       
   733     iLandmark->GetPositionField( EPositionFieldStreet, textFieldValue );
       
   734 
       
   735     SetTextFieldL( ELandmarkStreetField, textFieldValue );
       
   736 
       
   737     // postalCode
       
   738     textFieldValue.Set( KNullDesC );
       
   739     iLandmark->GetPositionField( EPositionFieldPostalCode, textFieldValue );
       
   740 
       
   741     SetTextFieldL( ELandmarkPostalField, textFieldValue );
       
   742 
       
   743     // city
       
   744     textFieldValue.Set( KNullDesC );
       
   745     iLandmark->GetPositionField( EPositionFieldCity, textFieldValue );
       
   746 
       
   747     SetTextFieldL( ELandmarkCityField, textFieldValue );
       
   748 
       
   749     // country
       
   750     textFieldValue.Set( KNullDesC );
       
   751     iLandmark->GetPositionField( EPositionFieldCountry, textFieldValue );
       
   752 
       
   753     SetTextFieldL( ELandmarkCountryField, textFieldValue );
       
   754     }
       
   755 
       
   756 // -----------------------------------------------------------------------------
       
   757 // -----------------------------------------------------------------------------
       
   758 //
       
   759 void CLandmarksEditDialog::ConfigureLocationFieldsL( TLocality& aLocation )
       
   760     {
       
   761     TBuf<KPosLmMaxTextFieldLength> textFieldValue;
       
   762 
       
   763     TPosition pos( aLocation, TTime(0) );
       
   764     // Configure latitude in float format
       
   765     SetLocationFieldL( ELandmarkLatitudeField, pos );
       
   766 
       
   767     // Configure longitude in float format
       
   768     SetLocationFieldL( ELandmarkLongitudeField, pos );
       
   769 
       
   770     // Configure altitude
       
   771     LandmarksUtils::FloatToDes( aLocation.Altitude(), textFieldValue, iRealFormat );
       
   772     SetTextFieldL( ELandmarkAltitudeField, textFieldValue );
       
   773 
       
   774     // Configure horizontal accuracy
       
   775     LandmarksUtils::FloatToDes( aLocation.HorizontalAccuracy(), textFieldValue, iRealFormat );
       
   776     SetTextFieldL( ELandmarkHorAccField, textFieldValue );
       
   777 
       
   778     // Configure vertical accuracy
       
   779     LandmarksUtils::FloatToDes( aLocation.VerticalAccuracy(), textFieldValue, iRealFormat );
       
   780     SetTextFieldL( ELandmarkVerAccField, textFieldValue );
       
   781     }
       
   782 
       
   783 // -----------------------------------------------------------------------------
       
   784 // -----------------------------------------------------------------------------
       
   785 //
       
   786 void CLandmarksEditDialog::ConfigureCategoryFieldL()
       
   787     {
       
   788     TInt nrOfCategories = iCategoryIds.Count();
       
   789     const TInt KExtraChars = 2; // CRLF
       
   790 
       
   791     HBufC* lmCategoriesBuf = HBufC::NewLC(
       
   792         ( KPosLmMaxTextFieldLength + KExtraChars ) * nrOfCategories );
       
   793 
       
   794     TPtr lmCategories = lmCategoriesBuf->Des();
       
   795     for ( TInt i = 0; i < nrOfCategories; i++ )
       
   796         {
       
   797         if ( i > 0 )
       
   798             {
       
   799             lmCategories.Append( CEditableText::ELineBreak );
       
   800             }
       
   801 
       
   802         CPosLandmarkCategory* category = iEngine.CategoryLC( iCategoryIds[i] );
       
   803         TPtrC categoryName;
       
   804         category->GetCategoryName( categoryName );
       
   805         lmCategories.Append( categoryName );
       
   806         CleanupStack::PopAndDestroy( category );
       
   807         }
       
   808 
       
   809     SetTextFieldL( ELandmarkCategoryField, *lmCategoriesBuf );
       
   810 
       
   811     CleanupStack::PopAndDestroy( lmCategoriesBuf );
       
   812     }
       
   813 
       
   814 // -----------------------------------------------------------------------------
       
   815 // -----------------------------------------------------------------------------
       
   816 //
       
   817 void CLandmarksEditDialog::SetTextFieldL( TInt aId, const TDesC& aText )
       
   818     {
       
   819     CEikEdwin* editor = static_cast<CEikEdwin*>( Control( aId ) );
       
   820 	editor->SetTextL( &aText );
       
   821     editor->DrawDeferred();
       
   822     }
       
   823 
       
   824 // -----------------------------------------------------------------------------
       
   825 // -----------------------------------------------------------------------------
       
   826 //
       
   827 void CLandmarksEditDialog::SetLocationFieldL( TInt aId, const TPosition& aPosition )
       
   828     {
       
   829     CAknLocationEditor* editor = static_cast<CAknLocationEditor*>( Control( aId ) );
       
   830 	editor->Set( aPosition );
       
   831     editor->DrawDeferred();
       
   832     }
       
   833 
       
   834 // -----------------------------------------------------------------------------
       
   835 // -----------------------------------------------------------------------------
       
   836 //
       
   837 void CLandmarksEditDialog::UpdateTitleBarL( TBool aFromField )
       
   838     {
       
   839     if ( aFromField )
       
   840         {
       
   841         TBuf<KPosLmMaxTextFieldLength> name;
       
   842 
       
   843         CEikEdwin* nameEditor =
       
   844             static_cast<CEikEdwin*>( Control( ELandmarkNameField ) );
       
   845         nameEditor->GetText( name );
       
   846         iTitlePane->SetTextL( name );
       
   847         }
       
   848     else
       
   849         {
       
   850         TPtrC ptr;
       
   851         iLandmark->GetLandmarkName( ptr );
       
   852         iTitlePane->SetTextL( ptr );
       
   853         }
       
   854     }
       
   855 
       
   856 // -----------------------------------------------------------------------------
       
   857 // -----------------------------------------------------------------------------
       
   858 //
       
   859 void CLandmarksEditDialog::ProcessErrorsInFormL(
       
   860     CLandmarksEditDialog::TErrorInForm aErrorId )
       
   861     {
       
   862     switch ( aErrorId )
       
   863         {
       
   864         case ENoErrors:
       
   865             break;
       
   866         case EWrongLatitude:
       
   867             NotifyErrorToUserL( R_LMREFAPP_LATITUDE_ERROR );
       
   868             break;
       
   869         case EWrongLongitude:
       
   870             NotifyErrorToUserL( R_LMREFAPP_LONGITUDE_ERROR );
       
   871             break;
       
   872         case EWrongAltitude:
       
   873             NotifyErrorToUserL( R_LMREFAPP_ALTITUDE_ERROR );
       
   874             break;
       
   875         case EWrongHorAcc:
       
   876             NotifyErrorToUserL( R_LMREFAPP_HOR_ACC_ERROR );
       
   877             break;
       
   878         case EWrongVerAcc:
       
   879             NotifyErrorToUserL( R_LMREFAPP_VER_ACC_ERROR );
       
   880             break;
       
   881         case EWrongRadius:
       
   882             NotifyErrorToUserL( R_LMREFAPP_RADIUS_ERROR );
       
   883             break;
       
   884         case EOnlyLatOrLon:
       
   885             NotifyErrorToUserL( R_LMREFAPP_LAT_LONG_ERROR );
       
   886             break;
       
   887         case ENoLatLonButHorAcc:
       
   888             NotifyErrorToUserL( R_LMREFAPP_HACC_LAT_LONG_ERROR );
       
   889             break;
       
   890         case ENoLatLonButAltitude:
       
   891             NotifyErrorToUserL( R_LMREFAPP_ALT_LAT_LONG_ERROR );
       
   892             break;
       
   893         case ENoAltButVerAcc:
       
   894             NotifyErrorToUserL( R_LMREFAPP_VACC_ALT_ERROR );
       
   895             break;
       
   896         case EEmptyLandmarkName:
       
   897             NotifyErrorToUserL( R_LMREFAPP_EMPTY_LANDMARK_NAME_ERROR );
       
   898             break;
       
   899         default:
       
   900             LandmarksUtils::Panic( KErrGeneral );
       
   901             break;
       
   902         }
       
   903     }
       
   904 
       
   905 // -----------------------------------------------------------------------------
       
   906 // -----------------------------------------------------------------------------
       
   907 //
       
   908 void CLandmarksEditDialog::NotifyErrorToUserL( TInt aResourceId )
       
   909     {
       
   910     HBufC* msg = iCoeEnv->AllocReadResourceLC( aResourceId );
       
   911     LandmarksUtils::ErrorNoteL( *msg );
       
   912     CleanupStack::PopAndDestroy( msg );
       
   913     }
       
   914 
       
   915 // -----------------------------------------------------------------------------
       
   916 // -----------------------------------------------------------------------------
       
   917 //
       
   918 void CLandmarksEditDialog::EditCategoriesL()
       
   919     {
       
   920     TInt res = CLandmarksCategoriesDialog::RunDialogL( *iLmCategoriesModel );
       
   921     if ( res )
       
   922         {
       
   923         ConfigureCategoryFieldL();
       
   924         DrawDeferred();
       
   925         SetChangesPending( ETrue );
       
   926         }
       
   927     }
       
   928 
       
   929 // -----------------------------------------------------------------------------
       
   930 // -----------------------------------------------------------------------------
       
   931 //
       
   932 void CLandmarksEditDialog::UpdateToCurrentLocationL()
       
   933     {
       
   934     // Update location fields to current location
       
   935     HBufC* title =
       
   936         iCoeEnv->AllocReadResourceAsDes16LC( R_LMREFAPP_ACQUIRING_LOC );
       
   937     iPositionRequest->FetchPositionInfoL( *title, iPositionInfo );
       
   938     CleanupStack::PopAndDestroy( title );
       
   939     }
       
   940 
       
   941 // ---------------------------------------------------------------------------
       
   942 //  Copies address information ( address and building position fields ) from
       
   943 //  source landmark to target.
       
   944 // ---------------------------------------------------------------------------
       
   945 //
       
   946 void CLandmarksEditDialog::CopyAddressDataL( CPosLandmark& aSrc, CPosLandmark& aTrg )
       
   947     {
       
   948     // clean address fields
       
   949     for ( TPositionFieldId fieldId = EPositionFieldAddressCapabilitiesBegin + 1;
       
   950           fieldId < EPositionFieldNMEACapabilitiesBegin;
       
   951           fieldId++ )
       
   952         {
       
   953         aTrg.RemovePositionField( fieldId );
       
   954         }
       
   955 
       
   956     // copy fields
       
   957     TPositionFieldId fieldId = aSrc.FirstPositionFieldId();
       
   958     while ( fieldId != EPositionFieldNone )
       
   959         {
       
   960         if ( fieldId > EPositionFieldAddressCapabilitiesBegin &&
       
   961              fieldId < EPositionFieldNMEACapabilitiesBegin )
       
   962             {
       
   963             TPtrC field;
       
   964             if ( KErrNone == aSrc.GetPositionField( fieldId, field ) )
       
   965                 {
       
   966                 aTrg.SetPositionFieldL( fieldId, field );
       
   967                 }
       
   968             }
       
   969         fieldId = aSrc.NextPositionFieldId( fieldId );
       
   970         }
       
   971     }
       
   972 
       
   973 // ---------------------------------------------------------------------------
       
   974 //  Copies coordinate information ( locality and coverage ) from
       
   975 //  source landmark to target. If some fields are empty in source,
       
   976 //  they will also be emptied in result.
       
   977 // ---------------------------------------------------------------------------
       
   978 //
       
   979 void CLandmarksEditDialog::CopyCoordinateDataL( CPosLandmark& aSrc, CPosLandmark& aTrg )
       
   980     {
       
   981     TLocality loc;
       
   982     if ( KErrNone == aSrc.GetPosition( loc ) )
       
   983         {
       
   984         aTrg.SetPositionL( loc );
       
   985         }
       
   986     else
       
   987         {
       
   988         aTrg.RemoveLandmarkAttributes( CPosLandmark::EPosition );
       
   989         }
       
   990 
       
   991     TRealX nan;
       
   992     nan.SetNaN();
       
   993     TReal32 coverage( nan );
       
   994 
       
   995     aSrc.GetCoverageRadius( coverage );
       
   996     aTrg.SetCoverageRadius( coverage );
       
   997     }
       
   998 
       
   999 // -----------------------------------------------------------------------------
       
  1000 // -----------------------------------------------------------------------------
       
  1001 //
       
  1002 TBool CLandmarksEditDialog::HasAddressData()
       
  1003     {
       
  1004     // check only those fields, which are shown to user
       
  1005     const TPositionFieldId fieldIds[] =
       
  1006         {
       
  1007         EPositionFieldCountry,
       
  1008         EPositionFieldCity,
       
  1009         EPositionFieldStreet,
       
  1010         EPositionFieldPostalCode,
       
  1011         };
       
  1012     const TInt fields = 4;
       
  1013 
       
  1014     for ( TInt i = 0; i < fields; i++ )
       
  1015         {
       
  1016         TPtrC field;
       
  1017         TInt err = iLandmark->GetPositionField( fieldIds[i], field );
       
  1018         if ( !err && field.Length() )
       
  1019             {
       
  1020             return ETrue;
       
  1021             }
       
  1022         }
       
  1023 
       
  1024     return EFalse;
       
  1025     }
       
  1026 
       
  1027 // -----------------------------------------------------------------------------
       
  1028 // -----------------------------------------------------------------------------
       
  1029 //
       
  1030 TBool CLandmarksEditDialog::HasCoordinateData()
       
  1031     {
       
  1032     TLocality loc;
       
  1033     return ( KErrNone == iLandmark->GetPosition( loc ) );
       
  1034     }
       
  1035 
       
  1036 // -----------------------------------------------------------------------------
       
  1037 // -----------------------------------------------------------------------------
       
  1038 //
       
  1039 void CLandmarksEditDialog::StartWaitDialogL( TInt aResId )
       
  1040     {
       
  1041     if ( iWaitDialog )
       
  1042         {
       
  1043         User::Leave( KErrInUse );
       
  1044         }
       
  1045         
       
  1046     iWaitDialog = new (ELeave) CAknWaitDialog( 
       
  1047         ( reinterpret_cast<CEikDialog**>( &iWaitDialog ) ) ); 
       
  1048         
       
  1049     iWaitDialog->SetTone( CAknNoteDialog::EConfirmationTone ); 
       
  1050     iWaitDialog->ExecuteLD( aResId );
       
  1051     }
       
  1052 
       
  1053 // -----------------------------------------------------------------------------
       
  1054 // -----------------------------------------------------------------------------
       
  1055 //
       
  1056 void CLandmarksEditDialog::CloseWaitDialog()
       
  1057     {
       
  1058     if ( iWaitDialog )
       
  1059         {
       
  1060         TRAP_IGNORE( iWaitDialog->ProcessFinishedL() );
       
  1061         iWaitDialog = NULL;
       
  1062         }
       
  1063     }
       
  1064 
       
  1065 // -----------------------------------------------------------------------------
       
  1066 // -----------------------------------------------------------------------------
       
  1067 //
       
  1068 void CLandmarksEditDialog::ResetCoordinatesL()
       
  1069     {
       
  1070     TPosition position;
       
  1071     ConfigureLocationFieldsL( position );
       
  1072 
       
  1073     SetChangesPending( ETrue );
       
  1074 
       
  1075     // Indicate that editors have been updated
       
  1076     for ( TInt i = ELatitudeEditor; i <= EVerticalAccEditor; i++ )
       
  1077         {
       
  1078         iIsRealValueEdited[i] = ETrue;
       
  1079         }
       
  1080     }
       
  1081