qtmobileextensions/src/settingsmanager/xqsettingsmanager_p.cpp
branchRCL_3
changeset 10 cd2778e5acfe
parent 9 5d007b20cfd0
child 11 19a54be74e5e
equal deleted inserted replaced
9:5d007b20cfd0 10:cd2778e5acfe
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 #include "xqsettingsmanager_p.h"
       
    23 #include <QVariant>
       
    24 #include "settingshandlerbase.h"
       
    25 #include "ccentralrepositoryhandler.h"
       
    26 #include "cpublishandsubscribehandler.h"
       
    27 
       
    28 XQSettingsManagerPrivate::XQSettingsManagerPrivate(XQSettingsManager* settingsManager) 
       
    29     : q(settingsManager)
       
    30 {   
       
    31 }
       
    32 
       
    33 XQSettingsManagerPrivate::~XQSettingsManagerPrivate()
       
    34 {
       
    35     foreach (SettingsHandlerBase* handler, m_centralRepositoryHandlers)
       
    36         delete handler;
       
    37     foreach (SettingsHandlerBase* handler, m_publishAndSubscribeHandlers)
       
    38         delete handler;
       
    39 }
       
    40 
       
    41 QVariant XQSettingsManagerPrivate::readItemValue(const XQSettingsKey& key, XQSettingsManager::Type type)
       
    42 {
       
    43     SettingsHandlerBase* handler = handlerInstance(key);
       
    44     if (handler)
       
    45     {
       
    46         return handler->handleReadItemValue(key, type, iError);
       
    47     }
       
    48     return QVariant();
       
    49 }
       
    50 
       
    51 bool XQSettingsManagerPrivate::writeItemValue(const XQSettingsKey& key, const QVariant& value)
       
    52 {
       
    53     SettingsHandlerBase* handler = handlerInstance(key);
       
    54     if (handler)
       
    55     {
       
    56         return handler->handleWriteItemValue(key, value, iError);
       
    57     }
       
    58     return false;
       
    59 }
       
    60 
       
    61 bool XQSettingsManagerPrivate::startMonitoring(const XQSettingsKey& key, XQSettingsManager::Type type)
       
    62 {
       
    63     SettingsHandlerBase* handler = handlerInstance(key);
       
    64     if (handler)
       
    65     {
       
    66         return handler->handleStartMonitoring(key, type, *this, iError);
       
    67     }
       
    68     return false;
       
    69 }
       
    70 
       
    71 bool XQSettingsManagerPrivate::stopMonitoring(const XQSettingsKey& key)
       
    72 {
       
    73     SettingsHandlerBase* handler = NULL;
       
    74     const long int uid = key.uid();
       
    75     switch (key.target())
       
    76     {
       
    77         case XQSettingsKey::TargetCentralRepository:
       
    78             if (m_centralRepositoryHandlers.contains(uid))
       
    79             {
       
    80                 handler = m_centralRepositoryHandlers.value(uid);
       
    81             }
       
    82             break;
       
    83         case XQSettingsKey::TargetPublishAndSubscribe:
       
    84             if (m_publishAndSubscribeHandlers.contains(uid))
       
    85             {
       
    86                 handler = m_publishAndSubscribeHandlers.value(uid);
       
    87             }
       
    88             break;
       
    89         default:
       
    90             break;
       
    91     }
       
    92     if (handler)
       
    93     {
       
    94         return handler->handleStopMonitoring(key, iError);
       
    95     }
       
    96     iError = KErrNotFound;
       
    97     return false;
       
    98 }
       
    99 
       
   100 XQSettingsManager::Error XQSettingsManagerPrivate::error() const
       
   101 {
       
   102     switch (iError) 
       
   103     {
       
   104         case KErrNone:
       
   105             return XQSettingsManager::NoError;
       
   106         case KErrNoMemory:
       
   107             return XQSettingsManager::OutOfMemoryError;
       
   108         case KErrNotFound:
       
   109             return XQSettingsManager::NotFoundError;
       
   110         case KErrAlreadyExists:
       
   111             return XQSettingsManager::AlreadyExistsError;
       
   112         case KErrPermissionDenied:
       
   113             return XQSettingsManager::PermissionDeniedError;
       
   114         case KErrArgument:
       
   115             return XQSettingsManager::BadTypeError;
       
   116         case KErrNotSupported:
       
   117             return XQSettingsManager::NotSupportedError;
       
   118         default:
       
   119             qDebug("XQSettingsManagerPrivate::error() iError = %d", iError);
       
   120             return XQSettingsManager::UnknownError;
       
   121     }    
       
   122 }
       
   123 
       
   124 CCentralRepositoryHandler* XQSettingsManagerPrivate::cenRepHandlerInstance(long int repositoryUid)
       
   125 {
       
   126     CCentralRepositoryHandler* handler = NULL;
       
   127     if (m_centralRepositoryHandlers.contains(repositoryUid))
       
   128     {
       
   129         handler = m_centralRepositoryHandlers.value(repositoryUid);
       
   130     }
       
   131     else
       
   132     {
       
   133         TUid uid;
       
   134         uid.iUid = repositoryUid;
       
   135         TRAP(iError, handler = CCentralRepositoryHandler::NewL(uid);)
       
   136         if (handler)
       
   137         {
       
   138             static_cast<SettingsHandlerBase*>(handler)->setObserver(this);
       
   139             m_centralRepositoryHandlers[repositoryUid] = handler;
       
   140         }
       
   141     }
       
   142     return handler;
       
   143 }
       
   144 
       
   145 CPublishAndSubscribeHandler* XQSettingsManagerPrivate::pubSubHandlerInstance(long int categoryUid)
       
   146 {
       
   147     CPublishAndSubscribeHandler* handler = NULL;
       
   148     if (m_publishAndSubscribeHandlers.contains(categoryUid))
       
   149     {
       
   150         handler = m_publishAndSubscribeHandlers.value(categoryUid);
       
   151     }
       
   152     else
       
   153     {
       
   154         TUid uid;
       
   155         uid.iUid = categoryUid;
       
   156         TRAP(iError, handler = CPublishAndSubscribeHandler::NewL(uid);)
       
   157         if (handler)
       
   158         {
       
   159             static_cast<SettingsHandlerBase*>(handler)->setObserver(this);
       
   160             m_publishAndSubscribeHandlers[categoryUid] = handler;
       
   161         }
       
   162     }
       
   163     return handler;
       
   164 }
       
   165 
       
   166 SettingsHandlerBase* XQSettingsManagerPrivate::handlerInstance(const XQSettingsKey& key)
       
   167 {
       
   168     SettingsHandlerBase* handler = NULL;
       
   169     const XQSettingsKey::Target target = key.target();
       
   170     switch (target)
       
   171     {
       
   172         case XQSettingsKey::TargetCentralRepository:
       
   173             handler = cenRepHandlerInstance(key.uid());
       
   174             break;
       
   175         case XQSettingsKey::TargetPublishAndSubscribe:
       
   176             handler = pubSubHandlerInstance(key.uid());
       
   177             break;
       
   178         default:
       
   179             break;
       
   180     }
       
   181     return handler;
       
   182 }
       
   183 
       
   184 void XQSettingsManagerPrivate::valueChanged(const XQSettingsKey& key, XQSettingsManager::Type type)
       
   185 {
       
   186     emit q->valueChanged(key, readItemValue(key, type));
       
   187 }
       
   188 
       
   189 void XQSettingsManagerPrivate::itemDeleted(const XQSettingsKey& key)
       
   190 {
       
   191     XQSettingsKey deletedKey(key);
       
   192     stopMonitoring(deletedKey);
       
   193     emit q->itemDeleted(deletedKey);
       
   194 }
       
   195 // End of file