~ubuntu-branches/ubuntu/vivid/youker-assistant/vivid

« back to all changes in this revision

Viewing changes to src/settings.cpp

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2014-03-24 15:52:37 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140324155237-3kod0m3wr2a2ag39
Tags: 1.0.1-0ubuntu1
* New upstream release (LP: #1294936).
* Modify display mode of weather forecast and system settings.
* Add file manager and font style settings.
* Improve system settings and restoring default settings.
* Cache user account (not password).
* Change the position of window control buttons.
* Add configuration for Kingsoft KuaiPan cloud client.
* Add the instruction of Youker Assistant.
* Open related folders when scanning items got double-clicked.
* Notify users when operating the Kingsoft disk cloud configuration.
* Modify Dbus starting method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
**
3
 
** Copyright (C) 2010 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 examples of the Qt Toolkit.
8
 
**
9
 
** You may use this file under the terms of the BSD license as follows:
10
 
**
11
 
** "Redistribution and use in source and binary forms, with or without
12
 
** modification, are permitted provided that the following conditions are
13
 
** met:
14
 
**   * Redistributions of source code must retain the above copyright
15
 
**     notice, this list of conditions and the following disclaimer.
16
 
**   * Redistributions in binary form must reproduce the above copyright
17
 
**     notice, this list of conditions and the following disclaimer in
18
 
**     the documentation and/or other materials provided with the
19
 
**     distribution.
20
 
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21
 
**     the names of its contributors may be used to endorse or promote
22
 
**     products derived from this software without specific prior written
23
 
**     permission.
24
 
**
25
 
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
 
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
 
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
 
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
 
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
 
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
 
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
 
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
 
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36
 
** $QT_END_LICENSE$
37
 
**
38
 
****************************************************************************/
39
 
 
40
 
#include "settings.h"
41
 
 
42
 
Settings::Settings(QObject *parent)
43
 
    : QObject(parent)
44
 
    , initialPropertyCount(metaObject()->propertyCount())
45
 
    , timerId(0)
46
 
    , propertyChanges(0)
47
 
    , isComplete(false)
48
 
{
49
 
}
50
 
 
51
 
Settings::~Settings()
52
 
{
53
 
}
54
 
 
55
 
void Settings::componentComplete()
56
 
{
57
 
    load();
58
 
    isComplete = true;
59
 
}
60
 
 
61
 
void Settings::load()
62
 
{
63
 
    const QMetaObject *mo = metaObject();
64
 
    int propertyCount = mo->propertyCount();
65
 
    for (int i = initialPropertyCount; i < propertyCount; ++i) {
66
 
        const QMetaProperty &property = mo->property(i);
67
 
        const QString name(property.name());
68
 
 
69
 
        QVariant previousValue = property.read(this);
70
 
        QVariant currentValue = settings.value(name, previousValue);
71
 
 
72
 
        if (!currentValue.isNull()
73
 
                && currentValue.canConvert(previousValue.type())
74
 
                && previousValue != currentValue)
75
 
            property.write(this, currentValue);
76
 
 
77
 
        // Setup change notifications on first load
78
 
        if (!isComplete && property.hasNotifySignal()) {
79
 
            static int pcSlot = metaObject()->indexOfSlot("propertyChanged()");
80
 
            Q_ASSERT(pcSlot != -1);
81
 
 
82
 
            bool ok = QMetaObject::connect(this, property.notifySignalIndex(), this, pcSlot);
83
 
            Q_UNUSED(ok) Q_ASSERT(ok);
84
 
        }
85
 
    }
86
 
}
87
 
 
88
 
void Settings::store()
89
 
{
90
 
    const QMetaObject *mo = metaObject();
91
 
    int propertyCount = mo->propertyCount();
92
 
    for (int i = initialPropertyCount; i < propertyCount; ++i) {
93
 
        const QMetaProperty &property = mo->property(i);
94
 
        settings.setValue(property.name(), property.read(this));
95
 
    }
96
 
 
97
 
    propertyChanges = 0;
98
 
}
99
 
 
100
 
void Settings::propertyChanged()
101
 
{
102
 
    ++propertyChanges;
103
 
    if (timerId == 0)
104
 
        timerId = startTimer(500);
105
 
}
106
 
 
107
 
void Settings::timerEvent(QTimerEvent *)
108
 
{
109
 
    if (propertyChanges == 0) {
110
 
        killTimer(timerId);
111
 
        timerId = 0;
112
 
        return;
113
 
    }
114
 
 
115
 
    store();
116
 
}
117
 
 
118
 
QString Settings::group() const
119
 
{
120
 
    return settings.group();
121
 
}
122
 
 
123
 
void Settings::setGroup(const QString &name)
124
 
{
125
 
    if (propertyChanges)
126
 
        store();
127
 
    if (!settings.group().isEmpty())
128
 
        settings.endGroup();
129
 
 
130
 
    if (!name.isEmpty())
131
 
        settings.beginGroup(name);
132
 
    if (isComplete)
133
 
        load();
134
 
}