datetimesettings.cpp 6.48 KB
Newer Older
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
/*
 * Copyright (C) 2013 Jolla Ltd. <pekka.vuorela@jollamobile.com>
 *
 * You may use this file under the terms of the BSD license as follows:
 *
 * "Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Nemo Mobile nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 */

#include "datetimesettings.h"
33

34 35
#include <timed-qt5/interface>
#include <timed-qt5/wallclock>
36 37
#include <QDebug>

38

39
DateTimeSettings::DateTimeSettings(QObject *parent)
40 41 42 43 44 45 46
    : QObject(parent)
    , m_timed()
    , m_timezone()
    , m_autoSystemTime(false)
    , m_autoTimezone(false)
    , m_timedInfoValid(false)
    , m_timedInfo()
47
{
48 49 50 51 52 53
    if (!m_timed.settings_changed_connect(this, SLOT(onTimedSignal(const Maemo::Timed::WallClock::Info &, bool)))) {
        qWarning("Connection to timed signal failed: '%s'", Maemo::Timed::bus().lastError().message().toStdString().c_str());
    }

    // Request the first update of the wall clock info
    updateTimedInfo();
54 55 56 57 58 59
}

DateTimeSettings::~DateTimeSettings()
{
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
void DateTimeSettings::updateTimedInfo()
{
    QDBusPendingCall call = m_timed.get_wall_clock_info_async();
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);

    QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher *)),
                     this, SLOT(onGetWallClockInfoFinished(QDBusPendingCallWatcher *)));
}

void DateTimeSettings::onGetWallClockInfoFinished(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<Maemo::Timed::WallClock::Info> reply = *watcher;

    if (reply.isError()) {
        qWarning("Could not retrieve wall clock info: '%s'", reply.error().message().toStdString().c_str());
    } else {
        onTimedSignal(reply.value(), false);
    }

    watcher->deleteLater();
}

82 83 84 85 86
bool DateTimeSettings::ready() const
{
    return m_timedInfoValid;
}

87 88 89 90 91
void DateTimeSettings::setTime(int hour, int minute)
{
    QDate currentDate = QDate::currentDate();
    QTime time(hour, minute);
    QDateTime newTime(currentDate, time);
92
    setTime(newTime.toTime_t());
93 94 95 96 97 98 99
}


void DateTimeSettings::setDate(const QDate &date)
{
    QDateTime newTime = QDateTime::currentDateTime();
    newTime.setDate(date);
100
    setTime(newTime.toTime_t());
101 102 103 104
}

bool DateTimeSettings::automaticTimeUpdate()
{
105
    return m_autoSystemTime;
106 107 108 109
}

void DateTimeSettings::setAutomaticTimeUpdate(bool enable)
{
110 111 112 113 114 115 116 117 118 119 120
    if (enable != m_autoSystemTime) {
        Maemo::Timed::WallClock::Settings s;

        if (enable) {
            s.setTimeNitz();
        } else {
            s.setTimeManual();
        }

        setSettings(s);
    }
121 122 123 124
}

bool DateTimeSettings::automaticTimezoneUpdate()
{
125
    return m_autoTimezone;
126 127 128 129
}

void DateTimeSettings::setAutomaticTimezoneUpdate(bool enable)
{
130 131 132 133 134 135 136 137
    if (enable != m_autoTimezone) {
        Maemo::Timed::WallClock::Settings s;

        if (enable) {
            s.setTimezoneCellular();
        } else {
            s.setTimezoneManual("");
        }
138

139 140
        setSettings(s);
    }
141 142 143 144 145 146 147 148 149
}

QString DateTimeSettings::timezone() const
{
    return m_timezone;
}

void DateTimeSettings::setTimezone(const QString &tz)
{
150
    if (tz == m_timezone) {
151
        return;
152
    }
153

154 155 156
    Maemo::Timed::WallClock::Settings s;
    s.setTimezoneManual(tz);
    setSettings(s);
157 158
}

159 160
void DateTimeSettings::setHourMode(DateTimeSettings::HourMode mode)
{
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    Maemo::Timed::WallClock::Settings s;
    s.setFlag24(mode == TwentyFourHours);
    setSettings(s);
}

void DateTimeSettings::onWallClockSettingsFinished(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<bool> reply = *watcher;

    if (reply.isError()) {
        qWarning("Could not set wall clock settings: '%s'", reply.error().message().toStdString().c_str());
    } else if (!reply.value()) {
        qWarning("Could not set wall clock settings");
    }

    watcher->deleteLater();
}

bool DateTimeSettings::setSettings(Maemo::Timed::WallClock::Settings &s)
{
    if (!s.check()) {
        return false;
    }

    QDBusPendingCall call = m_timed.wall_clock_settings_async(s);
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);

    QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher *)),
                     this, SLOT(onWallClockSettingsFinished(QDBusPendingCallWatcher *)));

    return true;
}

bool DateTimeSettings::setTime(time_t time)
{
    Maemo::Timed::WallClock::Settings s;
    s.setTimeManual(time);
    return setSettings(s);
199 200
}

201
void DateTimeSettings::onTimedSignal(const Maemo::Timed::WallClock::Info &info, bool time_changed)
202
{
203 204
    const bool prevReady = ready();

205 206 207 208
    m_timedInfo = info;
    m_timedInfoValid = true;

    if (time_changed) {
209 210
        emit timeChanged();
    }
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

    bool newAutoSystemTime = info.flagTimeNitz();
    if (newAutoSystemTime != m_autoSystemTime) {
        m_autoSystemTime = newAutoSystemTime;
        emit automaticTimeUpdateChanged();
    }

    bool newAutoTimezone = info.flagLocalCellular();
    if (newAutoTimezone != m_autoTimezone) {
        m_autoTimezone = newAutoTimezone;
        emit automaticTimezoneUpdateChanged();
    }

    QString newTimezone = info.humanReadableTz();
    if (newTimezone != m_timezone) {
        m_timezone = newTimezone;
        emit timezoneChanged();
228
    }
229 230 231 232

    if (prevReady != ready()) {
        emit readyChanged();
    }
233
}