<commit_before>/** This file is part of Contacts daemon
 **
 ** Copyright (c) 2010-2012 Nokia Corporation and/or its subsidiary(-ies).
 **
 ** Contact:  Nokia Corporation (<EMAIL>)
 **
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser General Public License
 ** version 2.1 as published by the Free Software Foundation and appearing in the
 ** file LICENSE.LGPL included in the packaging of this file.  Please review the
 ** following information to ensure the GNU Lesser General Public License version
 ** 2.1 requirements will be met:
 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional rights.
 ** These rights are described in the Nokia Qt LGPL Exception version 1.1, included
 ** in the file LGPL_EXCEPTION.txt in this package.
 **
 ** Other Usage
 ** Alternatively, this file may be used in accordance with the terms and
 ** conditions contained in a signed written agreement between you and Nokia.
 **/

#include "base-plugin.h"
#include "debug.h"

#include "qmheartbeat.h"

namespace Contactsd {

using MeeGo::QmHeartbeat;

class HotFixesPlugin : public BasePlugin
{
    Q_OBJECT

public:
    HotFixesPlugin()
        : m_heartbeat(0)
        , m_timer(0)
    {
    }

    ~HotFixesPlugin()
    {
        if (m_heartbeat) {
            m_heartbeat->close();
        }
    }

    void init()
    {
        if (m_heartbeat) {
            m_heartbeat->close();
            m_heartbeat->deleteLater();
            m_heartbeat = 0;
        }

        m_heartbeat = new QmHeartbeat(this);

        if (m_heartbeat->open(QmHeartbeat::SignalNeeded)) {
            connect(m_heartbeat, SIGNAL(wakeUp(QTime)), this, SLOT(onWakeUp()));
        } else {
            warning() << "hotfixes - Hearthbeat service not available, using fallback mechanism.";
            m_heartbeat->deleteLater();
            m_heartbeat = 0;

            m_timer = new QTimer(this);
            m_timer->setSingleShot(true);
            connect(m_timer, SIGNAL(timeout()), this, SLOT(onWakeUp()));
        }

        scheduleWakeUp(0);
    }

    QMap<QString, QVariant> metaData()
    {
        MetaData data;
        data[metaDataKeyName]    = QVariant(QString::fromLatin1("contacts-hotfixes"));
        data[metaDataKeyVersion] = QVariant(QString::fromLatin1("1"));
        data[metaDataKeyComment] = QVariant(QString::fromLatin1("Hotfixes for the contacts framework"));
        return data;
    }

private:
    void scheduleWakeUp(unsigned short wakeupSlot = QmHeartbeat::WAKEUP_SLOT_10_HOURS)
    {
        debug() << "hotfixes - scheduling wakeup within" << wakeupSlot << "seconds";

        if (m_heartbeat) {
            m_heartbeat->wait(wakeupSlot,
                              QmHeartbeat::WAKEUP_SLOT_10_HOURS,
                              QmHeartbeat::DoNotWaitHeartbeat);
        } else if (m_timer) {
            // seems "wakeup slots" are just a delay in seconds
            m_timer->setInterval(wakeupSlot * 1000);
            m_timer->start();
        }
    }

    bool runQuery(const QSparqlQuery &query, const char *slot)
    {
        QSparqlQueryOptions options;
        options.setPriority(QSparqlQueryOptions::LowPriority);
        QSparqlResult *const result = sparqlConnection().exec(query);

        if (result->hasError()) {
            warning() << "hotfixes - query failed:" << result->lastError().message();
            result->deleteLater();
            return false;
        }

        connect(result, SIGNAL(finished()), this, slot);
        return true;
    }

    bool runLookupQuery()<commit_msg>Fixes: Ensure first hotfix run happens within 2.5 minutes<commit_after>/** This file is part of Contacts daemon
 **
 ** Copyright (c) 2010-2012 Nokia Corporation and/or its subsidiary(-ies).
 **
 ** Contact:  Nokia Corporation (<EMAIL>)
 **
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser General Public License
 ** version 2.1 as published by the Free Software Foundation and appearing in the
 ** file LICENSE.LGPL included in the packaging of this file.  Please review the
 ** following information to ensure the GNU Lesser General Public License version
 ** 2.1 requirements will be met:
 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional rights.
 ** These rights are described in the Nokia Qt LGPL Exception version 1.1, included
 ** in the file LGPL_EXCEPTION.txt in this package.
 **
 ** Other Usage
 ** Alternatively, this file may be used in accordance with the terms and
 ** conditions contained in a signed written agreement between you and Nokia.
 **/

#include "base-plugin.h"
#include "debug.h"

#include "qmheartbeat.h"

namespace Contactsd {

using MeeGo::QmHeartbeat;

class HotFixesPlugin : public BasePlugin
{
    Q_OBJECT

public:
    HotFixesPlugin()
        : m_heartbeat(0)
        , m_timer(0)
    {
    }

    ~HotFixesPlugin()
    {
        if (m_heartbeat) {
            m_heartbeat->close();
        }
    }

    void init()
    {
        if (m_heartbeat) {
            m_heartbeat->close();
            m_heartbeat->deleteLater();
            m_heartbeat = 0;
        }

        m_heartbeat = new QmHeartbeat(this);

        if (m_heartbeat->open(QmHeartbeat::SignalNeeded)) {
            connect(m_heartbeat, SIGNAL(wakeUp(QTime)), this, SLOT(onWakeUp()));
        } else {
            warning() << "hotfixes - Hearthbeat service not available, using fallback mechanism.";
            m_heartbeat->deleteLater();
            m_heartbeat = 0;

            m_timer = new QTimer(this);
            m_timer->setSingleShot(true);
            connect(m_timer, SIGNAL(timeout()), this, SLOT(onWakeUp()));
        }

        scheduleWakeUp(0, QmHeartbeat::WAKEUP_SLOT_2_5_MINS);
    }

    QMap<QString, QVariant> metaData()
    {
        MetaData data;
        data[metaDataKeyName]    = QVariant(QString::fromLatin1("contacts-hotfixes"));
        data[metaDataKeyVersion] = QVariant(QString::fromLatin1("1"));
        data[metaDataKeyComment] = QVariant(QString::fromLatin1("Hotfixes for the contacts framework"));
        return data;
    }

private:
    void scheduleWakeUp(unsigned short minimumDelay = QmHeartbeat::WAKEUP_SLOT_10_HOURS,
                        unsigned short maximumDelay = QmHeartbeat::WAKEUP_SLOT_10_HOURS)
    {
        debug() << "hotfixes - scheduling wakeup within" << minimumDelay << "seconds";

        if (m_heartbeat) {
            m_heartbeat->wait(minimumDelay, maximumDelay,
                              QmHeartbeat::DoNotWaitHeartbeat);
        } else if (m_timer) {
            // seems "wakeup slots" are just a delay in seconds
            m_timer->setInterval((minimumDelay + maximumDelay) * 1000 / 2);
            m_timer->start();
        }
    }

    bool runQuery(const QSparqlQuery &query, const char *slot)
    {
        QSparqlQueryOptions options;
        options.setPriority(QSparqlQueryOptions::LowPriority);
        QSparqlResult *const result = sparqlConnection().exec(query);

        if (result->hasError()) {
            warning() << "hotfixes - query failed:" << result->lastError().message();
            result->deleteLater();
            return false;
        }

        connect(result, SIGNAL(finished()), this, slot);
        return true;
    }

    bool runLookupQuery()
{'fraction_non_alphanumeric': 0.10745908237188087, 'fraction_numerical': 0.009525087201502549, 'mean_word_length': 2.610169491525424, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2199761', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2132, 'n_words': 582}