~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to libs/plasma/datacontainer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License version 2 as
 
6
 *   published by the Free Software Foundation
 
7
 *
 
8
 *   This program is distributed in the hope that it will be useful,
 
9
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *   GNU General Public License for more details
 
12
 *
 
13
 *   You should have received a copy of the GNU Library General Public
 
14
 *   License along with this program; if not, write to the
 
15
 *   Free Software Foundation, Inc.,
 
16
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "datacontainer.h"
 
20
 
 
21
#include <QVariant>
 
22
 
 
23
#include <KDebug>
 
24
 
 
25
namespace Plasma
 
26
{
 
27
 
 
28
class DataContainer::Private
 
29
{
 
30
    public:
 
31
        Private()
 
32
            : dirty(false)
 
33
        {}
 
34
 
 
35
        DataEngine::Data data;
 
36
        int connectCount;
 
37
        bool dirty : 1;
 
38
};
 
39
 
 
40
DataContainer::DataContainer(QObject* parent)
 
41
    : QObject(parent),
 
42
      d(new Private())
 
43
{
 
44
}
 
45
 
 
46
DataContainer::~DataContainer()
 
47
{
 
48
    delete d;
 
49
}
 
50
 
 
51
const DataEngine::Data DataContainer::data() const
 
52
{
 
53
    return d->data;
 
54
}
 
55
 
 
56
void DataContainer::setData(const QString& key, const QVariant& value)
 
57
{
 
58
    if (value.isNull() || !value.isValid()) {
 
59
        if (!d->data.contains(key)) {
 
60
            return;
 
61
        }
 
62
 
 
63
        d->data.remove(key);
 
64
    } else {
 
65
        d->data[key] = value;
 
66
    }
 
67
 
 
68
    d->dirty = true;
 
69
}
 
70
 
 
71
void DataContainer::clearData()
 
72
{
 
73
    if (d->data.count() < 1) {
 
74
        // avoid an update if we don't have any data anyways
 
75
        return;
 
76
    }
 
77
 
 
78
    d->data.clear();
 
79
    d->dirty = true;
 
80
}
 
81
 
 
82
void DataContainer::checkForUpdate()
 
83
{
 
84
    if (d->dirty) {
 
85
        emit updated(objectName(), d->data);
 
86
        d->dirty = false;
 
87
    }
 
88
}
 
89
 
 
90
void DataContainer::connectNotify(const char *signal)
 
91
{
 
92
    if (QLatin1String(signal) == QMetaObject::normalizedSignature(SIGNAL(updated(QString, Plasma::DataEngine::Data))).constData()) {
 
93
        ++d->connectCount;
 
94
    }
 
95
}
 
96
 
 
97
void DataContainer::disconnectNotify(const char *signal)
 
98
{
 
99
    if (QLatin1String(signal) == QMetaObject::normalizedSignature(SIGNAL(updated(QString, Plasma::DataEngine::Data))).constData()) {
 
100
        if (d->connectCount > 0) {
 
101
            --d->connectCount;
 
102
        }
 
103
 
 
104
        if (d->connectCount < 1) {
 
105
            // DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
 
106
            emit unused(objectName());
 
107
        }
 
108
    }
 
109
}
 
110
 
 
111
} // Plasma namespace
 
112
 
 
113
#include "datacontainer.moc"
 
114