camerauis/cameraxui/cxengine/src/cxesettingscenrepstore.cpp
branchRCL_3
changeset 54 bac7acad7cb3
parent 53 61bc0f252b2b
child 57 2c87b2808fd7
equal deleted inserted replaced
53:61bc0f252b2b 54:bac7acad7cb3
     1 /*
       
     2 * Copyright (c) 2009-2010 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:
       
    15 *
       
    16 */
       
    17 /*
       
    18 * Symbian specific settings handling
       
    19 */
       
    20 
       
    21 #include <QVariant>
       
    22 #include <QList>
       
    23 #include <QMultiMap>
       
    24 #include <QMetaType>
       
    25 #include <QString>
       
    26 #include <QStringList>
       
    27 #include <QVariant>
       
    28 #ifdef Q_OS_SYMBIAN
       
    29 #include <ProfileEngineSDKCRKeys.h>
       
    30 #endif
       
    31 
       
    32 #include "xqsettingsmanager.h" // Settings Manager API
       
    33 #include "xqsettingskey.h"
       
    34 #include "cxesettingscenrepstore.h"
       
    35 #include "cxenamespace.h"
       
    36 #include "cxutils.h"
       
    37 #include "cxenamespace.h"
       
    38 #include "cxeerror.h"
       
    39 #include "cxecenrepkeys.h"
       
    40 
       
    41 #include "OstTraceDefinitions.h"
       
    42 #ifdef OST_TRACE_COMPILER_IN_USE
       
    43 #include "cxesettingscenrepstoreTraces.h"
       
    44 #endif
       
    45 
       
    46 
       
    47 using namespace CxeSettingIds;
       
    48 
       
    49 
       
    50 /*!
       
    51 * CxeSettingsCenRepStore::CxeSettingsCenRepStore
       
    52 */
       
    53 CxeSettingsCenRepStore::CxeSettingsCenRepStore()
       
    54 {
       
    55     CX_DEBUG_ENTER_FUNCTION();
       
    56     OstTrace0(camerax_performance, CXESETTINGSCENREPSTORE_1, "msg: e_CX_SETTINGSSTORE_CREATE 1");
       
    57 
       
    58     // map keys of type "string" to cenrep ids
       
    59     mapKeys();
       
    60 
       
    61 	// we create settings handle, scope user::scope
       
    62     mSettingsManager = new XQSettingsManager(this);
       
    63 
       
    64     bool ok = connect(mSettingsManager, SIGNAL(valueChanged(XQSettingsKey, QVariant)),
       
    65                       this, SLOT(handleValueChanged(XQSettingsKey, QVariant)));
       
    66     CX_ASSERT_ALWAYS(ok);
       
    67 
       
    68     CX_DEBUG(("CxeSettingsCenRepStore - mSettingsManager ptr = %d", mSettingsManager));
       
    69 
       
    70     OstTrace0(camerax_performance, CXESETTINGSCENREPSTORE_2, "msg: e_CX_SETTINGSSTORE_CREATE 0");
       
    71 	CX_DEBUG_EXIT_FUNCTION();
       
    72 }
       
    73 
       
    74 
       
    75 
       
    76 /*
       
    77 * CxeSettingsCenRepStore::~CxeSettingsCenRepStore()
       
    78 */
       
    79 CxeSettingsCenRepStore::~CxeSettingsCenRepStore()
       
    80 {
       
    81     CX_DEBUG_ENTER_FUNCTION();
       
    82     delete mSettingsManager;
       
    83     CX_DEBUG_EXIT_FUNCTION();
       
    84 }
       
    85 
       
    86 
       
    87 /*!
       
    88 * Generates XQSettingsKey from given setting/runtime key
       
    89 * \param key Name of the setting from which to generate the XQSettingsKey
       
    90 * \param[out] error Error code. CxeError::None if everything went fine.
       
    91 */
       
    92 XQSettingsKey
       
    93 CxeSettingsCenRepStore::generateXQSettingsKey(const QString& key, CxeError::Id& error)
       
    94 {
       
    95     CX_DEBUG_ENTER_FUNCTION();
       
    96 
       
    97     error = CxeError::None;
       
    98 	long int uid = 0;
       
    99 	unsigned long int keyId = 0;
       
   100 
       
   101 	if(mKeyMapping.contains(key)) {
       
   102 	    uid = mKeyMapping[key].mRepositoryUid;
       
   103 		keyId = mKeyMapping[key].mKeyId;
       
   104 	} else {
       
   105 	    error = CxeError::NotFound;
       
   106 	}
       
   107 
       
   108 	CX_DEBUG(( "CxeSettingsCenRepStore::generateXQSettingsKey uid = %x keyId = %d", uid, keyId ));
       
   109 	XQSettingsKey newKey(XQSettingsKey::TargetCentralRepository, uid, keyId);
       
   110 
       
   111 	CX_DEBUG_EXIT_FUNCTION();
       
   112 
       
   113 	return newKey;
       
   114 }
       
   115 
       
   116 
       
   117 
       
   118 /*!
       
   119 * Reads/loads all run-time variation settings values from cenrep
       
   120 * @param QList<QString> contains list of all variation key ids which we use to load values from cenrep.
       
   121 * returns: QHash container, "contains" values associated with each key that are read from cenrep
       
   122 * NOTE: loading variation settings should be done only ONCE at start-up.
       
   123 */
       
   124 QHash<QString, QVariantList> CxeSettingsCenRepStore::loadVariationSettings(QList<QString>& variationKeys)
       
   125 {
       
   126     CX_DEBUG_ENTER_FUNCTION();
       
   127     OstTrace0(camerax_performance, CXESETTINGSCENREPSTORE_LOADRUNTIME_1, "msg: e_CX_SETTINGSSTORE_LOAD_RUNTIME 1");
       
   128 
       
   129     QHash<QString, QVariantList> settings;
       
   130 	CxeError::Id err = CxeError::None;
       
   131 	QVariantList list;
       
   132 	QVariant data;
       
   133 
       
   134     // parsing through the list of run-time variation keys and reading values from cenrep.
       
   135     foreach (QString key, variationKeys) {
       
   136 
       
   137         // read the data from cenrep
       
   138         err = get(key, data);
       
   139 
       
   140         // clear the list
       
   141         list.clear();
       
   142 
       
   143         if (CxeError::None == err) {
       
   144 			if (data.type() == QMetaType::QString ) {
       
   145                 QString str = data.toString();
       
   146                 QVariant strListVariant = qVariantFromValue(str.split(","));
       
   147                 //generate qvariantlist from strListVariant
       
   148                 list = strListVariant.toList();
       
   149             } else {
       
   150                 // if its of any other type, then just append to the list
       
   151                 list.append(data);
       
   152             }
       
   153         }
       
   154         // append the values associated with the key to the list.
       
   155         settings.insert(key, list);
       
   156 
       
   157     } // end for
       
   158 
       
   159     CX_DEBUG_EXIT_FUNCTION();
       
   160 
       
   161     OstTrace0(camerax_performance, CXESETTINGSCENREPSTORE_LOADRUNTIME_2, "msg: e_CX_SETTINGSSTORE_LOAD_RUNTIME 0");
       
   162     return settings;
       
   163 }
       
   164 
       
   165 
       
   166 
       
   167 /*!
       
   168 * Reads a value from cenrep
       
   169 * @param key   - setting key
       
   170 * @param value - setting value read from cenrep
       
   171 */
       
   172 
       
   173 CxeError::Id CxeSettingsCenRepStore::get(const QString& key, QVariant &value)
       
   174 {
       
   175     CX_DEBUG_ENTER_FUNCTION();
       
   176     CX_DEBUG_ASSERT(mSettingsManager);
       
   177 
       
   178     CX_DEBUG(("CxeSettingsCenRepStore::get - key: %s", key.toAscii().constData()));
       
   179 
       
   180     CxeError::Id err = CxeError::None;
       
   181 	XQSettingsKey mappedSettingKey = generateXQSettingsKey(key, err);
       
   182 
       
   183     if (CxeError::None == err) {
       
   184         CX_DEBUG(("reading values from XQSettingsManager.."));
       
   185         value = mSettingsManager->readItemValue(mappedSettingKey, mKeyMapping[key].mDataType);
       
   186         // checking if reading data from cenrep was successful
       
   187         if (mSettingsManager->error() != XQSettingsManager::NoError) {
       
   188             CX_DEBUG(("Error reading value from XQSettingsManager.."));
       
   189             err = CxeError::General;
       
   190         }
       
   191     }
       
   192 
       
   193     CX_DEBUG_EXIT_FUNCTION();
       
   194 
       
   195     return err;
       
   196 }
       
   197 
       
   198 
       
   199 
       
   200 /*!
       
   201 * Reads a value from cenrep and starts the value change monitoring.
       
   202 * @param key   - setting key
       
   203 * @param uid   - setting UID of the component that owns the setting key
       
   204 * @param type  - the type of key cr key or P&S key
       
   205 * @param value - setting value read from cenrep
       
   206 *
       
   207 * @sa handleValueChanged()
       
   208 */
       
   209 void CxeSettingsCenRepStore::startMonitoring(long int uid,
       
   210                                  unsigned long int key,
       
   211                                  Cxe::SettingKeyType type,
       
   212                                  QVariant &value)
       
   213 {
       
   214     CX_DEBUG_ENTER_FUNCTION();
       
   215     CX_DEBUG_ASSERT(mSettingsManager);
       
   216 
       
   217     XQSettingsKey::Target keyType;
       
   218 
       
   219     if (type == Cxe::PublishAndSubscribe) {
       
   220         keyType = XQSettingsKey::TargetPublishAndSubscribe;
       
   221     } else {
       
   222         keyType = XQSettingsKey::TargetCentralRepository;
       
   223     }
       
   224 
       
   225 	XQSettingsKey settingsKey(keyType, uid, key);
       
   226     CX_DEBUG(("reading value from XQSettingsManager.."));
       
   227     value = mSettingsManager->readItemValue(settingsKey);
       
   228 
       
   229     // start monitoring changes for the key
       
   230     // both P&S and Repository keys are monitored
       
   231     bool ok = mSettingsManager->startMonitoring(settingsKey);
       
   232     if (!ok) {
       
   233         XQSettingsManager::Error error = mSettingsManager->error();
       
   234         CX_DEBUG(("CxeSettingsCenRepStore - got error %d trying to start listening", error));
       
   235         // If we try to start listening one key more than once,
       
   236         // we get this error. We can safely ignore it.
       
   237         ok = (XQSettingsManager::AlreadyExistsError == error);
       
   238     }
       
   239     CX_ASSERT_ALWAYS(ok);
       
   240 
       
   241     CX_DEBUG_EXIT_FUNCTION();
       
   242 }
       
   243 
       
   244 
       
   245 
       
   246 /*!
       
   247 * Sets a new value to cenrep
       
   248 * @param key   - setting key
       
   249 * @param newValue - new value set to the key in cenrep
       
   250 */
       
   251 CxeError::Id CxeSettingsCenRepStore::set(const QString& key, const QVariant newValue)
       
   252 {
       
   253     CX_DEBUG_ENTER_FUNCTION();
       
   254     CX_DEBUG_ASSERT(mSettingsManager);
       
   255 
       
   256 	CxeError::Id err = CxeError::None;
       
   257     // generating key for xqsettingsmanager
       
   258     XQSettingsKey mappedSettingKey = generateXQSettingsKey(key, err);
       
   259 
       
   260     if (CxeError::None == err) {
       
   261         // key found
       
   262         // check if the key can be set any value or read-only
       
   263         if(mKeyMapping[key].mReadOnly) {
       
   264            err = CxeError::NotFound;
       
   265         } else {
       
   266             mSettingsManager->writeItemValue(mappedSettingKey, newValue);
       
   267             // check from xqsettingsmanager if "Set" operation was successful
       
   268             if (mSettingsManager->error() != XQSettingsManager::NoError) {
       
   269                 CX_DEBUG(("Error writing value to XQSettingsManager.."));
       
   270                 err = CxeError::General;
       
   271             }
       
   272         }
       
   273     }
       
   274 
       
   275     CX_DEBUG_EXIT_FUNCTION();
       
   276 
       
   277     return err;
       
   278 }
       
   279 
       
   280 
       
   281 
       
   282 /*!
       
   283 * resets the cenrep store
       
   284 */
       
   285 void CxeSettingsCenRepStore::reset()
       
   286 {
       
   287     CX_DEBUG_ENTER_FUNCTION();
       
   288     CX_DEBUG_ASSERT(mSettingsManager);
       
   289     //!@Todo: How to reset the repository.
       
   290     CX_DEBUG_EXIT_FUNCTION();
       
   291 }
       
   292 
       
   293 
       
   294 /*!
       
   295 * Maps CxeSettingIds to cenrep key ids that XQSettingsManager understands
       
   296 */
       
   297 void CxeSettingsCenRepStore::mapKeys()
       
   298 {
       
   299     CX_DEBUG_ENTER_FUNCTION();
       
   300     OstTrace0(camerax_performance, CXESETTINGSCENREPSTORE_MAPKEYS_1, "msg: e_CX_SETTINGSSTORE_INIT_MAPPING 1");
       
   301 
       
   302     mKeyMapping.clear();
       
   303 
       
   304     // mapping setting keys
       
   305     addKeyMapping(CxeSettingIds::FNAME_FOLDER_SUFFIX,
       
   306                   FileNameSuffixCr,
       
   307                   XQSettingsManager::TypeString);
       
   308 
       
   309     addKeyMapping(CxeSettingIds::FNAME_MONTH_FOLDER,
       
   310                   FolderNameCr,
       
   311                   XQSettingsManager::TypeString);
       
   312 
       
   313     addKeyMapping(CxeSettingIds::FNAME_IMAGE_COUNTER,
       
   314                   FileNameImageCounterCr,
       
   315                   XQSettingsManager::TypeInt);
       
   316 
       
   317     addKeyMapping(CxeSettingIds::FNAME_VIDEO_COUNTER,
       
   318                   FileNameVideoCounterCr,
       
   319                   XQSettingsManager::TypeInt);
       
   320 
       
   321     addKeyMapping(CxeSettingIds::STILL_SHOWCAPTURED,
       
   322                   StillShowCapturedCr,
       
   323                   XQSettingsManager::TypeInt);
       
   324 
       
   325     addKeyMapping(CxeSettingIds::VIDEO_SHOWCAPTURED,
       
   326                   VideoShowCapturedCr,
       
   327                   XQSettingsManager::TypeInt);
       
   328 
       
   329     addKeyMapping(CxeSettingIds::IMAGE_QUALITY,
       
   330                   ImageQualityCr,
       
   331                   XQSettingsManager::TypeInt);
       
   332 
       
   333     addKeyMapping(CxeSettingIds::VIDEO_QUALITY,
       
   334                   VideoQualityCr,
       
   335                   XQSettingsManager::TypeInt);
       
   336 
       
   337     addKeyMapping(CxeSettingIds::VIDEO_MUTE_SETTING,
       
   338                   AudioMuteCr,
       
   339                   XQSettingsManager::TypeInt);
       
   340 
       
   341     addKeyMapping(CxeSettingIds::GEOTAGGING,
       
   342                   GeoTaggingCr,
       
   343                   XQSettingsManager::TypeInt);
       
   344 
       
   345     addKeyMapping(CxeSettingIds::FACE_TRACKING,
       
   346                   FacetrackingCr,
       
   347                   XQSettingsManager::TypeInt);
       
   348 
       
   349     addKeyMapping(CxeSettingIds::IMAGE_SCENE,
       
   350                   SceneModeStillCr,
       
   351                   XQSettingsManager::TypeString);
       
   352 
       
   353     addKeyMapping(CxeSettingIds::VIDEO_SCENE,
       
   354                   SceneModeVideoCr,
       
   355                   XQSettingsManager::TypeString);
       
   356 
       
   357     addKeyMapping(CxeSettingIds::FLASH_MODE,
       
   358                   FlashModeStillCr,
       
   359                   XQSettingsManager::TypeInt);
       
   360 
       
   361     addKeyMapping(CxeSettingIds::CAPTURE_SOUND_ALWAYS_ON,
       
   362                   CaptureSoundAlwaysOnCr,
       
   363                   XQSettingsManager::TypeInt);
       
   364 
       
   365     addKeyMapping(CxeSettingIds::CAMERA_MODE,
       
   366                   CameraModeCr,
       
   367                   XQSettingsManager::TypeInt);
       
   368 
       
   369     addKeyMapping(CxeSettingIds::GEOTAGGING_DISCLAIMER,
       
   370                   GeoTaggingDisclaimerCr,
       
   371                   XQSettingsManager::TypeInt);
       
   372 
       
   373     // mapping run-time variation keys
       
   374     addKeyMapping(CxeVariationKeys::FREE_MEMORY_LEVELS,
       
   375                   FreeMemoryLevelsCr,
       
   376                   XQSettingsManager::TypeString,
       
   377                   true);
       
   378 
       
   379     addKeyMapping(CxeVariationKeys::STILL_MAX_ZOOM_LIMITS,
       
   380                   StillMaxZoomLimitsCr,
       
   381                   XQSettingsManager::TypeString,
       
   382                   true);
       
   383 
       
   384     addKeyMapping(CxeVariationKeys::VIDEO_MAX_ZOOM_LIMITS,
       
   385                   VideoMaxZoomLimitsCr,
       
   386                   XQSettingsManager::TypeString,
       
   387                   true);
       
   388 
       
   389     OstTrace0(camerax_performance, CXESETTINGSCENREPSTORE_MAPKEYS_2, "msg: e_CX_SETTINGSSTORE_INIT_MAPPING 0");
       
   390 	CX_DEBUG_EXIT_FUNCTION();
       
   391 }
       
   392 
       
   393 /*!
       
   394 * helper method to construct key mappings for each setting key
       
   395 */
       
   396 void CxeSettingsCenRepStore::addKeyMapping(QString key,
       
   397                                            unsigned long int keyid,
       
   398                                            XQSettingsManager::Type type,
       
   399                                            bool readOnly)
       
   400 {
       
   401     CX_DEBUG_ENTER_FUNCTION();
       
   402 
       
   403     CxeCenRepDefinition cenrepDef;
       
   404 
       
   405     long int uid;
       
   406     if (readOnly) {
       
   407         uid = CxRuntimeFeaturesCrUid;
       
   408     } else {
       
   409         uid = CxSettingsCrUid;
       
   410     }
       
   411     cenrepDef.mRepositoryUid = uid;
       
   412     cenrepDef.mKeyId         = keyid;
       
   413     cenrepDef.mReadOnly      = readOnly;
       
   414     cenrepDef.mDataType      = type;
       
   415 
       
   416     // insert the key specific mapping to the hash
       
   417     mKeyMapping.insert(key, cenrepDef);
       
   418 
       
   419     CX_DEBUG_EXIT_FUNCTION();
       
   420 }
       
   421 
       
   422 
       
   423 
       
   424 /*!
       
   425 *  Handles value changed signal from the XQSettingsManager.
       
   426 *  Emits signals corresponding the changed setting.
       
   427 *  \sa warningTonesChanged(), settingValueChanged()
       
   428 */
       
   429 void CxeSettingsCenRepStore::handleValueChanged(XQSettingsKey key, QVariant value)
       
   430 {
       
   431     CX_DEBUG_ENTER_FUNCTION();
       
   432     emit settingValueChanged(key.uid(), key.key(), value);
       
   433     CX_DEBUG_EXIT_FUNCTION();
       
   434 }
       
   435 
       
   436 const QHash<QString, CxeSettingsCenRepStore::CxeCenRepDefinition>& CxeSettingsCenRepStore::keyMapping() const
       
   437 {
       
   438     return mKeyMapping;
       
   439 }
       
   440 
       
   441 CxeSettingsLocalStore::CxeSettingsLocalStore()
       
   442 {
       
   443     CX_DEBUG_ENTER_FUNCTION();
       
   444     // load values from cenrep
       
   445     CX_DEBUG(("Loading keys from cenrep..."));
       
   446     foreach(QString key, keyMapping().keys()) {
       
   447 
       
   448         QVariant value;
       
   449         CxeError::Id err = CxeSettingsCenRepStore::get(key, value);
       
   450         if (!err) {
       
   451             CX_DEBUG(("Adding key %s", key.toAscii().constData()));
       
   452             mSettings[key] = value;
       
   453         } else {
       
   454             CX_DEBUG(("Key %s not found in cenrep", key.toAscii().constData()));
       
   455         }
       
   456     }
       
   457     CX_DEBUG(("Done"));
       
   458     CX_DEBUG_EXIT_FUNCTION();
       
   459 }
       
   460 CxeSettingsLocalStore::~CxeSettingsLocalStore()
       
   461 {
       
   462     CX_DEBUG_ENTER_FUNCTION();
       
   463     CX_DEBUG_EXIT_FUNCTION();
       
   464 }
       
   465 
       
   466 
       
   467 CxeError::Id CxeSettingsLocalStore::get(const QString& key, QVariant &value)
       
   468 {
       
   469     CX_DEBUG_ENTER_FUNCTION();
       
   470 
       
   471     if (useValueFromCenrep(key)) {
       
   472         CX_DEBUG(("Reading key %s directly from cenrep", key.toAscii().constData()));
       
   473         return CxeSettingsCenRepStore::get(key, value);
       
   474     }
       
   475 
       
   476     value = mSettings.value(key);
       
   477     if (value.isNull()) {
       
   478         CX_DEBUG(("Key %s not found", key.toAscii().constData()));
       
   479         return CxeError::NotFound;
       
   480     }
       
   481 
       
   482     CX_DEBUG(("Key %s, value %s", key.toAscii().constData(), value.toString().toAscii().constData()));
       
   483 
       
   484     CX_DEBUG_EXIT_FUNCTION();
       
   485     return CxeError::None;
       
   486 }
       
   487 
       
   488 
       
   489 CxeError::Id CxeSettingsLocalStore::set(const QString& key, const QVariant newValue)
       
   490 {
       
   491     CX_DEBUG_ENTER_FUNCTION();
       
   492 
       
   493     if (useValueFromCenrep(key)) {
       
   494         CX_DEBUG(("Writing key %s directly to cenrep", key.toAscii().constData()));
       
   495         return CxeSettingsCenRepStore::set(key, newValue);
       
   496     }
       
   497     if (mSettings.value(key).isNull()) {
       
   498         // only allow to set keys that are mapped in CxeSettingsCenRepStore
       
   499         CX_DEBUG(("Key %s not found", key.toAscii().constData()));
       
   500         return CxeError::NotFound;
       
   501     }
       
   502 
       
   503     mSettings[key] = newValue;
       
   504 
       
   505     CX_DEBUG_EXIT_FUNCTION();
       
   506     return CxeError::None;
       
   507 }
       
   508 
       
   509 /*!
       
   510  * Returns true for keys that should be read/written from cenrep instead
       
   511  * of using cached copies. This is for some settings that need to be shared
       
   512  * between all instances of camera.
       
   513  *
       
   514  * @param key name
       
   515  * @return boolean to indicate wheter to load use cenrep value or not
       
   516  */
       
   517 bool CxeSettingsLocalStore::useValueFromCenrep(const QString &key) const
       
   518 {
       
   519     if (key == CxeSettingIds::FNAME_MONTH_FOLDER ||
       
   520         key == CxeSettingIds::FNAME_IMAGE_COUNTER ||
       
   521         key == CxeSettingIds::FNAME_VIDEO_COUNTER) {
       
   522         return true;
       
   523     }
       
   524 
       
   525     return false;
       
   526 
       
   527 }
       
   528 
       
   529 
       
   530 // end of file