qtmobility/src/publishsubscribe/gconflayer_linux.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "gconflayer_linux_p.h"
       
    43 #include <QVariant>
       
    44 #include <GConfItem>
       
    45 
       
    46 #include <QDebug>
       
    47 
       
    48 QTM_BEGIN_NAMESPACE
       
    49 
       
    50 Q_GLOBAL_STATIC(GConfLayer, gConfLayer);
       
    51 QVALUESPACE_AUTO_INSTALL_LAYER(GConfLayer);
       
    52 
       
    53 GConfLayer::GConfLayer()
       
    54 {
       
    55 }
       
    56 
       
    57 GConfLayer::~GConfLayer()
       
    58 {
       
    59     QMutableHashIterator<QString, GConfHandle *> i(m_handles);
       
    60     while (i.hasNext()) {
       
    61         i.next();
       
    62 
       
    63         removeHandle(Handle(i.value()));
       
    64     }
       
    65 }
       
    66 
       
    67 QString GConfLayer::name()
       
    68 {
       
    69     return QLatin1String("GConf Layer");
       
    70 }
       
    71 
       
    72 QUuid GConfLayer::id()
       
    73 {
       
    74     return QVALUESPACE_GCONF_LAYER;
       
    75 }
       
    76 
       
    77 unsigned int GConfLayer::order()
       
    78 {
       
    79     return 0;
       
    80 }
       
    81 
       
    82 QValueSpace::LayerOptions GConfLayer::layerOptions() const
       
    83 {
       
    84     return QValueSpace::PermanentLayer |
       
    85         QValueSpace::WritableLayer;
       
    86 }
       
    87 
       
    88 GConfLayer *GConfLayer::instance()
       
    89 {
       
    90     return gConfLayer();
       
    91 }
       
    92 
       
    93 bool GConfLayer::startup(Type /*type*/)
       
    94 {
       
    95     return true;
       
    96 }
       
    97 
       
    98 bool GConfLayer::value(Handle handle, QVariant *data)
       
    99 {
       
   100     GConfHandle *sh = gConfHandle(handle);
       
   101     if (!sh)
       
   102         return false;
       
   103 
       
   104     return value(InvalidHandle, sh->path, data);
       
   105 }
       
   106 
       
   107 bool GConfLayer::value(Handle handle, const QString &subPath, QVariant *data)
       
   108 {
       
   109     if (handle != InvalidHandle && !gConfHandle(handle))
       
   110         return false;
       
   111 
       
   112     QString path(subPath);
       
   113     while (path.endsWith(QLatin1Char('/')))
       
   114         path.chop(1);
       
   115     if (handle != InvalidHandle)
       
   116         while (path.startsWith(QLatin1Char('/')))
       
   117             path = path.mid(1);
       
   118     int index = path.lastIndexOf(QLatin1Char('/'), -1);
       
   119 
       
   120     bool createdHandle = false;
       
   121 
       
   122     QString value;
       
   123     if (index == -1) {
       
   124         value = path;
       
   125     } else {
       
   126         // want a value that is in a sub path under handle
       
   127         value = path.mid(index + 1);
       
   128         path.truncate(index);
       
   129 
       
   130         handle = item(handle, path);
       
   131         createdHandle = true;
       
   132     }
       
   133 
       
   134     GConfHandle *sh = gConfHandle(handle);
       
   135     if (!sh)
       
   136         return false;
       
   137 
       
   138     QString fullPath(sh->path);
       
   139     if (fullPath != QLatin1String("/"))
       
   140         fullPath.append(QLatin1Char('/'));
       
   141 
       
   142     fullPath.append(value);
       
   143 
       
   144     qDebug() << "Read value from" << fullPath;
       
   145     GConfItem gconfItem(fullPath);
       
   146     *data = gconfItem.value();
       
   147     qDebug() << "*data = " << *data;
       
   148 
       
   149     if (createdHandle)
       
   150         removeHandle(handle);
       
   151 
       
   152     return data->isValid();
       
   153 }
       
   154 
       
   155 QSet<QString> GConfLayer::children(Handle handle)
       
   156 {
       
   157     GConfHandle *sh = gConfHandle(handle);
       
   158     if (!sh)
       
   159         return QSet<QString>();
       
   160 
       
   161     qDebug() << "Get children from" << sh->path;
       
   162     GConfItem gconfItem(sh->path);
       
   163 
       
   164     QSet<QString> ret;
       
   165     foreach (QString child, gconfItem.listEntries() + gconfItem.listDirs()) {
       
   166         int index = child.lastIndexOf(QLatin1Char('/'), -1);
       
   167         ret += child.mid(index + 1);
       
   168         qDebug() << "found" << child.mid(index + 1);
       
   169     }
       
   170 
       
   171     return ret;
       
   172 }
       
   173 
       
   174 QAbstractValueSpaceLayer::Handle GConfLayer::item(Handle parent, const QString &path)
       
   175 {
       
   176     QString fullPath;
       
   177 
       
   178     // Fail on invalid path.
       
   179     if (path.isEmpty() || path.contains(QLatin1String("//")))
       
   180         return InvalidHandle;
       
   181 
       
   182     if (parent == InvalidHandle) {
       
   183         fullPath = path;
       
   184     } else {
       
   185         GConfHandle *sh = gConfHandle(parent);
       
   186         if (!sh)
       
   187             return InvalidHandle;
       
   188 
       
   189         if (path == QLatin1String("/")) {
       
   190             fullPath = sh->path;
       
   191         } else if (sh->path.endsWith(QLatin1Char('/')) && path.startsWith(QLatin1Char('/')))
       
   192             fullPath = sh->path + path.mid(1);
       
   193         else if (!sh->path.endsWith(QLatin1Char('/')) && !path.startsWith(QLatin1Char('/')))
       
   194             fullPath = sh->path + QLatin1Char('/') + path;
       
   195         else
       
   196             fullPath = sh->path + path;
       
   197     }
       
   198 
       
   199     if (m_handles.contains(fullPath)) {
       
   200         GConfHandle *sh = m_handles.value(fullPath);
       
   201         ++sh->refCount;
       
   202         return Handle(sh);
       
   203     }
       
   204 
       
   205     // Create a new handle for path
       
   206     GConfHandle *sh = new GConfHandle(fullPath);
       
   207     m_handles.insert(fullPath, sh);
       
   208 
       
   209     return Handle(sh);
       
   210 }
       
   211 
       
   212 void GConfLayer::setProperty(Handle handle, Properties properties)
       
   213 {
       
   214     GConfHandle *sh = gConfHandle(handle);
       
   215     if (!sh)
       
   216         return;
       
   217 
       
   218     if (properties & QAbstractValueSpaceLayer::Publish) {
       
   219         qDebug() << "TODO: Start monitoring (child) items from" << sh->path;
       
   220     } else {
       
   221         qDebug() << "TODO: Stop monitoring (child) items from" << sh->path;
       
   222     }
       
   223 
       
   224 }
       
   225 
       
   226 void GConfLayer::removeHandle(Handle handle)
       
   227 {
       
   228     GConfHandle *sh = gConfHandle(handle);
       
   229     if (!sh)
       
   230         return;
       
   231 
       
   232     if (--sh->refCount)
       
   233         return;
       
   234 
       
   235     m_handles.remove(sh->path);
       
   236 
       
   237     delete sh;
       
   238 }
       
   239 
       
   240 bool GConfLayer::setValue(QValueSpacePublisher */*creator*/,
       
   241                                     Handle handle,
       
   242                                     const QString &subPath,
       
   243                                     const QVariant &data)
       
   244 {
       
   245     GConfHandle *sh = gConfHandle(handle);
       
   246     if (!sh)
       
   247         return false;
       
   248 
       
   249     QString path(subPath);
       
   250     while (path.endsWith(QLatin1Char('/')))
       
   251         path.chop(1);
       
   252 
       
   253     int index = path.lastIndexOf(QLatin1Char('/'), -1);
       
   254 
       
   255     bool createdHandle = false;
       
   256 
       
   257     QString value;
       
   258     if (index == -1) {
       
   259         value = path;
       
   260     } else {
       
   261         // want a value that is in a sub path under handle
       
   262         value = path.mid(index + 1);
       
   263         path.truncate(index);
       
   264 
       
   265         if (path.isEmpty())
       
   266             path.append(QLatin1Char('/'));
       
   267 
       
   268         sh = gConfHandle(item(Handle(sh), path));
       
   269         createdHandle = true;
       
   270     }
       
   271 
       
   272     QString fullPath(sh->path);
       
   273     if (fullPath != QLatin1String("/"))
       
   274         fullPath.append(QLatin1Char('/'));
       
   275 
       
   276     fullPath.append(value);
       
   277 
       
   278 
       
   279     qDebug() << "Write value" << data << "to" << fullPath;
       
   280     GConfItem gconfItem(fullPath);
       
   281     gconfItem.set(data);
       
   282 
       
   283     if (createdHandle)
       
   284         removeHandle(Handle(sh));
       
   285     return true;    //TODO: How to check whether set was ok?
       
   286 }
       
   287 
       
   288 void GConfLayer::sync()
       
   289 {
       
   290     //Not needed
       
   291 }
       
   292 
       
   293 bool GConfLayer::removeSubTree(QValueSpacePublisher * /*creator*/, Handle /*handle*/)
       
   294 {
       
   295     //Not needed
       
   296     return false;
       
   297 }
       
   298 
       
   299 bool GConfLayer::removeValue(QValueSpacePublisher */*creator*/,
       
   300     Handle handle,
       
   301     const QString &subPath)
       
   302 {
       
   303     GConfHandle *sh = gConfHandle(handle);
       
   304     if (!sh)
       
   305         return false;
       
   306 
       
   307     QString path(subPath);
       
   308     while (path.endsWith(QLatin1Char('/')))
       
   309         path.chop(1);
       
   310 
       
   311     int index = path.lastIndexOf(QLatin1Char('/'), -1);
       
   312 
       
   313     bool createdHandle = false;
       
   314 
       
   315     QString value;
       
   316     if (index == -1) {
       
   317         value = path;
       
   318     } else {
       
   319         // want a value that is in a sub path under handle
       
   320         value = path.mid(index + 1);
       
   321         path.truncate(index);
       
   322 
       
   323         if (path.isEmpty())
       
   324             path.append(QLatin1Char('/'));
       
   325 
       
   326         sh = gConfHandle(item(Handle(sh), path));
       
   327         createdHandle = true;
       
   328     }
       
   329 
       
   330     QString fullPath(sh->path);
       
   331     if (fullPath != QLatin1String("/"))
       
   332         fullPath.append(QLatin1Char('/'));
       
   333 
       
   334     fullPath.append(value);
       
   335 
       
   336     qDebug() << "TODO: Remove value from" << fullPath;
       
   337     GConfItem gconfItem(fullPath);
       
   338     gconfItem.unset();
       
   339 
       
   340     if (createdHandle)
       
   341         removeHandle(Handle(sh));
       
   342 
       
   343     return true;    //TODO: How to check whether unset was ok?
       
   344 }
       
   345 
       
   346 void GConfLayer::addWatch(QValueSpacePublisher *, Handle)
       
   347 {
       
   348     //Not needed
       
   349 }
       
   350 
       
   351 void GConfLayer::removeWatches(QValueSpacePublisher *, Handle)
       
   352 {
       
   353     //Not needed
       
   354 }
       
   355 
       
   356 bool GConfLayer::supportsInterestNotification() const
       
   357 {
       
   358     return false;
       
   359 }
       
   360 
       
   361 bool GConfLayer::notifyInterest(Handle, bool)
       
   362 {
       
   363     //Not needed
       
   364     return false;
       
   365 }
       
   366 
       
   367 #include "moc_gconflayer_linux_p.cpp"
       
   368 
       
   369 QTM_END_NAMESPACE