<commit_before>/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * 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), serviceBackend(serviceBackend),
      key(key), value(QVariant()),  timestamp(currentTimestamp()), subscribed(false), 
      emittedValue(value), emittedTimestamp(timestamp), overheard(false)
{
}

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()
{
    if (!subscribed) {
        return;
    }
    // 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;

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

void PropertyPrivate::setSubscribed()
{<commit_msg>libcontextprovider, protocol change: Fix: do the bookkeeping always, not only if somebody is subscribed.<commit_after>/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * 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), serviceBackend(serviceBackend),
      key(key), value(QVariant()),  timestamp(currentTimestamp()), subscribed(false),
      emittedValue(value), emittedTimestamp(timestamp), overheard(false)
{
}

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()
{
{'fraction_non_alphanumeric': 0.10368875022939988, 'fraction_numerical': 0.007707836300238576, 'mean_word_length': 3.6225614927905005, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2482281', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1445, 'n_words': 550}