src/hbservers/hbinputserver/hbinputserversettings.cpp
author hgs
Mon, 18 Oct 2010 18:23:13 +0300
changeset 34 ed14f46c0e55
permissions -rw-r--r--
201041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34
hgs
parents:
diff changeset
     1
/****************************************************************************
hgs
parents:
diff changeset
     2
**
hgs
parents:
diff changeset
     3
** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     4
** All rights reserved.
hgs
parents:
diff changeset
     5
** Contact: Nokia Corporation (developer.feedback@nokia.com)
hgs
parents:
diff changeset
     6
**
hgs
parents:
diff changeset
     7
** This file is part of the HbServers module of the UI Extensions for Mobile.
hgs
parents:
diff changeset
     8
**
hgs
parents:
diff changeset
     9
** GNU Lesser General Public License Usage
hgs
parents:
diff changeset
    10
** This file may be used under the terms of the GNU Lesser General Public
hgs
parents:
diff changeset
    11
** License version 2.1 as published by the Free Software Foundation and
hgs
parents:
diff changeset
    12
** appearing in the file LICENSE.LGPL included in the packaging of this file.
hgs
parents:
diff changeset
    13
** Please review the following information to ensure the GNU Lesser General
hgs
parents:
diff changeset
    14
** Public License version 2.1 requirements will be met:
hgs
parents:
diff changeset
    15
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hgs
parents:
diff changeset
    16
**
hgs
parents:
diff changeset
    17
** In addition, as a special exception, Nokia gives you certain additional
hgs
parents:
diff changeset
    18
** rights.  These rights are described in the Nokia Qt LGPL Exception
hgs
parents:
diff changeset
    19
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
hgs
parents:
diff changeset
    20
**
hgs
parents:
diff changeset
    21
** If you have questions regarding the use of this file, please contact
hgs
parents:
diff changeset
    22
** Nokia at developer.feedback@nokia.com.
hgs
parents:
diff changeset
    23
**
hgs
parents:
diff changeset
    24
****************************************************************************/
hgs
parents:
diff changeset
    25
hgs
parents:
diff changeset
    26
#include "hbinputserversettings_p.h"
hgs
parents:
diff changeset
    27
hgs
parents:
diff changeset
    28
#include <QFile>
hgs
parents:
diff changeset
    29
#include <QDir>
hgs
parents:
diff changeset
    30
#include <hbinputsettingproxy.h>
hgs
parents:
diff changeset
    31
#include <hbinputsettingproxy_p.h>
hgs
parents:
diff changeset
    32
hgs
parents:
diff changeset
    33
HbInputServerSettings::HbInputServerSettings(HbInputServer *server)
hgs
parents:
diff changeset
    34
    : mServer(server),
hgs
parents:
diff changeset
    35
      mSettings(0)
hgs
parents:
diff changeset
    36
{
hgs
parents:
diff changeset
    37
}
hgs
parents:
diff changeset
    38
hgs
parents:
diff changeset
    39
HbInputServerSettings::~HbInputServerSettings()
hgs
parents:
diff changeset
    40
{
hgs
parents:
diff changeset
    41
    if (mSettings) {
hgs
parents:
diff changeset
    42
        mSettings->lock();
hgs
parents:
diff changeset
    43
        saveToDisk();
hgs
parents:
diff changeset
    44
        mSettings->unlock();
hgs
parents:
diff changeset
    45
        delete mSettings;
hgs
parents:
diff changeset
    46
    }
hgs
parents:
diff changeset
    47
}
hgs
parents:
diff changeset
    48
hgs
parents:
diff changeset
    49
/*
hgs
parents:
diff changeset
    50
Initializes input settings shared memory. If the shared memory already exists,
hgs
parents:
diff changeset
    51
attaches to it and refreshes the settings from disk.
hgs
parents:
diff changeset
    52
hgs
parents:
diff changeset
    53
Returns true on success.
hgs
parents:
diff changeset
    54
*/
hgs
parents:
diff changeset
    55
bool HbInputServerSettings::initialize()
hgs
parents:
diff changeset
    56
{
hgs
parents:
diff changeset
    57
    mServer->debugPrint("Initializing settings");
hgs
parents:
diff changeset
    58
    mSettings = new QSharedMemory(HbInputSettingsSharedMemoryKey);
hgs
parents:
diff changeset
    59
hgs
parents:
diff changeset
    60
    if (!mSettings->create(sizeof(HbSettingProxyInternalData))) {
hgs
parents:
diff changeset
    61
        mServer->debugPrint("Settings shared memory already exists!");
hgs
parents:
diff changeset
    62
        mSettings->attach();
hgs
parents:
diff changeset
    63
    }
hgs
parents:
diff changeset
    64
hgs
parents:
diff changeset
    65
    mSettings->lock();
hgs
parents:
diff changeset
    66
    if (!initializeFromDisk()) {
hgs
parents:
diff changeset
    67
        initializeWithDefaults();
hgs
parents:
diff changeset
    68
    }
hgs
parents:
diff changeset
    69
    mSettings->unlock();
hgs
parents:
diff changeset
    70
    mServer->debugPrint("Settings initialized");
hgs
parents:
diff changeset
    71
    return true;
hgs
parents:
diff changeset
    72
}
hgs
parents:
diff changeset
    73
hgs
parents:
diff changeset
    74
/*
hgs
parents:
diff changeset
    75
Reads input settings from disk. Returns true if successful,
hgs
parents:
diff changeset
    76
false if the file cannot be read.
hgs
parents:
diff changeset
    77
*/
hgs
parents:
diff changeset
    78
bool HbInputServerSettings::initializeFromDisk()
hgs
parents:
diff changeset
    79
{
hgs
parents:
diff changeset
    80
    mServer->debugPrint("Reading input settings from disk");
hgs
parents:
diff changeset
    81
hgs
parents:
diff changeset
    82
    if (HbInputSettingProxyPrivate::load(settingsData())) {
hgs
parents:
diff changeset
    83
        mServer->debugPrint("Input settings successfully read from disk");
hgs
parents:
diff changeset
    84
        return true;
hgs
parents:
diff changeset
    85
    } else {
hgs
parents:
diff changeset
    86
        mServer->debugPrint("Input settings could not be read from disk");
hgs
parents:
diff changeset
    87
        return false;
hgs
parents:
diff changeset
    88
    }
hgs
parents:
diff changeset
    89
}
hgs
parents:
diff changeset
    90
hgs
parents:
diff changeset
    91
/*
hgs
parents:
diff changeset
    92
Saves input settings to disk.
hgs
parents:
diff changeset
    93
*/
hgs
parents:
diff changeset
    94
void HbInputServerSettings::saveToDisk()
hgs
parents:
diff changeset
    95
{
hgs
parents:
diff changeset
    96
    mServer->debugPrint("Saving input settings to disk");
hgs
parents:
diff changeset
    97
    HbInputSettingProxyPrivate::save(settingsData());
hgs
parents:
diff changeset
    98
    mServer->debugPrint("Input settings saved");
hgs
parents:
diff changeset
    99
}
hgs
parents:
diff changeset
   100
hgs
parents:
diff changeset
   101
/*
hgs
parents:
diff changeset
   102
Fills shared data with default settings values.
hgs
parents:
diff changeset
   103
*/
hgs
parents:
diff changeset
   104
void HbInputServerSettings::initializeWithDefaults()
hgs
parents:
diff changeset
   105
{
hgs
parents:
diff changeset
   106
    HbSettingProxyInternalData *data = settingsData();
hgs
parents:
diff changeset
   107
    if (data) {
hgs
parents:
diff changeset
   108
        HbInputSettingProxyPrivate::writeDefaultValuesToData(data);
hgs
parents:
diff changeset
   109
        mServer->debugPrint("Input settings initialized with defaults");
hgs
parents:
diff changeset
   110
    }
hgs
parents:
diff changeset
   111
}
hgs
parents:
diff changeset
   112
hgs
parents:
diff changeset
   113
/*
hgs
parents:
diff changeset
   114
Returns pointer to the settings data inside shared memory.
hgs
parents:
diff changeset
   115
*/
hgs
parents:
diff changeset
   116
HbSettingProxyInternalData *HbInputServerSettings::settingsData() const
hgs
parents:
diff changeset
   117
{
hgs
parents:
diff changeset
   118
    return static_cast<HbSettingProxyInternalData *>(mSettings->data());
hgs
parents:
diff changeset
   119
}