aboutsummaryrefslogtreecommitdiff
path: root/libcontextprovider/src/propertyprivate.cpp
blob: 80c480f6b4bf5795bcb744b4ccc156f04abc58ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "propertyprivate.h"
#include "servicebackend.h"
#include "logging.h"
#include "sconnect.h"
#include "loggingfeatures.h"
#include <time.h>
#include <math.h>

namespace ContextProvider {

QHash<QPair<ServiceBackend*,QString>, PropertyPrivate*> PropertyPrivate::propertyPrivateMap;

PropertyPrivate::PropertyPrivate(ServiceBackend* serviceBackend, const QString &key, QObject *parent)
    : QObject(parent), refCount(0), serviceBackend(serviceBackend),
      key(key), value(QVariant()),  timestamp(currentTimestamp()), subscribed(false),
      emittedValue(value), emittedTimestamp(timestamp), overheard(false)
{
}

PropertyPrivate::~PropertyPrivate()
{
}

/// Increase the reference count by one. Property calls this.
void PropertyPrivate::ref()
{
    refCount++;
    if (refCount == 1) {
        // Increase the reference count of serviceBackend to ensure it
        // stays alive
        serviceBackend->ref();
        // Associate the property to the service backend
        serviceBackend->addProperty(key, this);
    }
}

/// Decrease the reference count by one. Property calls this. If the
/// reference count goes to zero, schedule the PropertyPrivate
/// instance to be deleted.
void PropertyPrivate::unref()
{
    refCount--;

    if (refCount == 0) {
        // Remove the property from the service backend
        serviceBackend->removeProperty(key);
        // Now serviceBackend can be deleted if nobody else needs it.
        serviceBackend->unref();

        // For now, remove the PropertyPrivate from the instance
        // map. If this is changed, we need to check carefully what
        // happens if a new ServiceBackend is created with the same
        // memory address as a previous ServiceBackend (which was
        // destructed), and having the old PropertyPrivate store the
        // ServiceBackend pointer to that memory address.
        QPair<ServiceBackend*, QString> key = propertyPrivateMap.key(this);
        if (key.second != "")
            propertyPrivateMap.remove(key);
        else
            contextCritical() << "PropertyPrivate couldn't find itself in the instance store";
        deleteLater(); // "delete this" would be probably unsafe
    }
}

void PropertyPrivate::setValue(const QVariant& v)
{
    contextDebug() << F_PROPERTY << "Setting key:" << key << "to type:" << v.typeName();

    // Always store the intention of the provider
    timestamp = currentTimestamp();
    value = v;

    // The provider is setting a different value than it has previously.
    if (value != emittedValue || value.isNull() != emittedValue.isNull()) {
        emitValue();
        return;
    }

    // The provider is setting the same value again. But it has
    // overheard a different value after the emission of
    // emittedValue. Thus, emit again.
    if (overheard) {
        emitValue();
        return;
    }
}

void PropertyPrivate::emitValue()
{
    // No difference between intention and emitted value, nothing happens
    if (emittedTimestamp == timestamp && emittedValue == value
        && emittedValue.isNull() == value.isNull())
        return;

    emittedValue = value;
    emittedTimestamp = timestamp;
    overheard = false;

    // If nobody is subscribed to us, no D-Bus signal needs to be
    // emitted. Note: The bookkeeping above must be done, though.
    if (!subscribed) {
        return;
    }

    QVariantList values;
    if (value.isNull() == false) {
        values << value;
    }
    emit valueChanged(values, timestamp);
}

void PropertyPrivate::setSubscribed()
{
    if (subscribed == false) {
        subscribed = true;
        emit firstSubscriberAppeared(key);
    }
}

void PropertyPrivate::setUnsubscribed()
{
    if (subscribed == true) {
      subscribed = false;
      emit lastSubscriberDisappeared(key);
    }
}

void PropertyPrivate::updateOverheardValue(const QVariantList& v, const quint64& t)
{
    contextDebug() << "Updating overheard value" << v << t;

    QVariant overheardValue;
    // Update the "overheard" value
    if (v.size() == 0) {
        overheardValue = QVariant();
    }
    else {
        overheardValue = v[0];
    }
    if (t > emittedTimestamp &&  overheardValue != emittedValue){
        // record that a different and more recent value than the one
        // emitted has been overheard
        overheard = true;
        // emit the value because provider might have a more recent
        // timestamp than t
        emitValue();
    }
}

quint64 PropertyPrivate::currentTimestamp()
{
    struct timespec time;
    clock_gettime(CLOCK_MONOTONIC, &time);
    quint64 toReturn = time.tv_nsec;
    toReturn += ((quint64)pow(10,9) * time.tv_sec);
    return toReturn;
}


} // end namespace