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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
**/
#include "datetimeinterface.h"
#include "universalinterface.h"
#include <libintl.h>
#include <polkit-qt5-1/polkitqt1-authority.h>
DatetimeInterface::DatetimeInterface()
{
timeDateInterface = new QDBusInterface("org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
QDBusConnection::systemBus());
QDBusConnection::systemBus().connect("com.control.center.qt.systemdbus",
"/",
"com.control.center.interface",
"changed",
this,
SLOT(timezoneChanged(QString)));
}
DatetimeInterface::~DatetimeInterface()
{
}
void DatetimeInterface::timezoneChanged(QString key)
{
if (key == "timezone") {
Q_EMIT changed("timezone");
}
}
QStringList DatetimeInterface::getTimezone()
{
QStringList list = {};
QDBusInterface interface("org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
QDBusConnection::systemBus());
QString timezoneT = interface.property("Timezone").toString(); // 系统配置时区
QDBusReply<QString> reply = UniversalInterface::self()->ukccSystemDbus()->call("getShowTimezone"); //显示的时区 ./etc/kytimezone
QString showTimezone = reply.value();
if (!reply.isValid() || reply.value() != timezoneT) {
// showTimezone 异常,重新设置
if (!LINK_SHANGHAI_TIMEZONE.contains(showTimezone) || showTimezone.isEmpty()) {
showTimezone = timezoneT;
UniversalInterface::self()->ukccSystemDbus()->call("setShowTimezone", showTimezone);
}
}
list.append(timezoneT);
QStringList zone = timezoneName(showTimezone);
if (zone.size() > 1) {
list.append(zone.at(1));
} else {
list.append(timezoneT);
}
list.append(showTimezone);
return list;
}
QStringList DatetimeInterface::timezoneName(QString timezone)
{
QStringList result = {};
if (LINK_SHANGHAI_TIMEZONE.contains(timezone)) {
result.append(QString("Asia/Shanghai"));
} else {
result.append(timezone);
}
const char kTimezoneDomain[] = "installer-timezones";
const QString locale = QLocale::system().name();
(void) setlocale(LC_ALL, QString(locale + ".UTF-8").toStdString().c_str());
const QString local_name(dgettext(kTimezoneDomain,
timezone.toStdString().c_str()));
int index = local_name.lastIndexOf('/');
if (index == -1) {
// Some translations of locale name contains non-standard char.
index = local_name.lastIndexOf("∕");
}
// Reset locale.
(void) setlocale(LC_ALL, "en_US.UTF-8");
result.append((index > -1) ? local_name.mid(index + 1) : local_name);
return result;
}
QString DatetimeInterface::getTimeMode()
{
bool syncFlag = timeDateInterface->property("NTP").toBool();
return syncFlag ? QString("automatic") : QString ("manual");
}
void DatetimeInterface::setTimezone(QString timezone)
{
QString setTimezone = timezone;
if (LINK_SHANGHAI_TIMEZONE.contains(timezone)) {
setTimezone = "Asia/Shanghai";
}
QDBusMessage retDBus = timeDateInterface->call("SetTimezone", setTimezone, true);
if (retDBus.type() == QDBusMessage::ErrorMessage) {
return;
}
UniversalInterface::self()->ukccSystemDbus()->call("setShowTimezone", timezone);
}
void DatetimeInterface::setTime(qint64 timeData)
{
timeDateInterface->call("SetNTP", false, true);
timeDateInterface->call("SetTime", timeData, false, true);
}
bool DatetimeInterface::setTimeMode(QString mode)
{
QDBusMessage retDBus;
if (mode == "manual") {
retDBus = timeDateInterface->call("SetNTP", false, true);
} else {
retDBus = timeDateInterface->call("SetNTP", true, true);
}
if (retDBus.type() == QDBusMessage::ErrorMessage) {
return false;
}
Q_EMIT changed("timeMode");
return true;
}
QStringList DatetimeInterface::getNtpServers()
{
return QStringList() <<
QString("pool.ntp.org") <<
QString("cn.ntp.org.cn") <<
QString("cn.pool.ntp.org") <<
QString("ntp.aliyun.com") <<
QString("0.debian.pool.ntp.org") <<
QString("1.debian.pool.ntp.org") <<
QString("0.arch.pool.ntp.org") <<
QString("1.arch.pool.ntp.org") <<
QString("0.fedora.pool.ntp.org") <<
QString("1.fedora.pool.ntp.org");
}
QString DatetimeInterface::getNtpServer()
{
QString ntpServer = "";
const QString ntpFileName = "/etc/systemd/timesyncd.conf.d/kylin.conf";
QFile ntpFile(ntpFileName);
if (!ntpFile.exists()) { // 系统默认
ntpServer = "";
} else {
QSettings readFile(ntpFileName, QSettings::IniFormat);
ntpServer = readFile.value("Time/NTP").toString();
}
return ntpServer;
}
bool DatetimeInterface::setNtpServer(QString server)
{
PolkitQt1::Authority::Result result;
result = PolkitQt1::Authority::instance()->checkAuthorizationSync(
"org.control.center.qt.systemdbus.action.ntp",
PolkitQt1::UnixProcessSubject(QCoreApplication::applicationPid()),
PolkitQt1::Authority::AllowUserInteraction);
if (result == PolkitQt1::Authority::Yes) {
QDBusReply<bool> reply = UniversalInterface::self()->ukccSystemDbus()->call("setNtpSerAddress", server);
if (reply.value()) {
Q_EMIT changed("ntpServer");
return true;
}
}
return false;
}
QStringList DatetimeInterface::getOtherTimezones()
{
if (UniversalInterface::self()->ukccPanelGsettings()->keys().contains(TIMEZONES_KEY)) {
return UniversalInterface::self()->ukccPanelGsettings()->get(TIMEZONES_KEY).toStringList();
} else {
return QStringList();
}
}
bool DatetimeInterface::setOtherTimezones(QStringList list)
{
if (UniversalInterface::self()->ukccPanelGsettings()->keys().contains(TIMEZONES_KEY)) {
UniversalInterface::self()->ukccPanelGsettings()->set(TIMEZONES_KEY, list);
Q_EMIT changed("otherTimezones");
return true;
} else {
return false;
}
}
|