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