2017-09-18 14:33:44 +08:00
|
|
|
|
/*
|
2018-02-07 11:52:47 +08:00
|
|
|
|
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
2017-09-18 14:33:44 +08:00
|
|
|
|
*
|
|
|
|
|
* Author: sbw <sbw@sbw.so>
|
|
|
|
|
*
|
|
|
|
|
* Maintainer: sbw <sbw@sbw.so>
|
|
|
|
|
*
|
|
|
|
|
* 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 of the License, or
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-15 17:44:38 +08:00
|
|
|
|
#include "datetimeplugin.h"
|
2020-06-29 15:35:51 +08:00
|
|
|
|
#include "../../widgets/tipswidget.h"
|
2020-08-19 14:13:35 +08:00
|
|
|
|
#include "../../frame/util/utils.h"
|
2016-06-15 17:44:38 +08:00
|
|
|
|
|
2018-04-16 15:44:35 +08:00
|
|
|
|
#include <DDBusSender>
|
2021-05-25 13:05:45 +08:00
|
|
|
|
|
2018-03-06 15:18:53 +08:00
|
|
|
|
#include <QDebug>
|
2019-09-20 17:57:33 +08:00
|
|
|
|
#include <QDBusConnectionInterface>
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2016-07-13 10:08:38 +08:00
|
|
|
|
|
2018-11-20 14:45:35 +08:00
|
|
|
|
#define PLUGIN_STATE_KEY "enable"
|
2019-09-03 09:37:59 +08:00
|
|
|
|
#define TIME_FORMAT_KEY "Use24HourFormat"
|
2020-06-29 15:35:51 +08:00
|
|
|
|
using namespace Dock;
|
2016-06-15 17:44:38 +08:00
|
|
|
|
DatetimePlugin::DatetimePlugin(QObject *parent)
|
2019-06-28 16:58:31 +08:00
|
|
|
|
: QObject(parent)
|
2021-05-25 13:05:45 +08:00
|
|
|
|
, m_centralWidget(nullptr)
|
|
|
|
|
, m_dateTipsLabel(nullptr)
|
|
|
|
|
, m_refershTimer(nullptr)
|
2019-09-20 17:57:33 +08:00
|
|
|
|
, m_interface(nullptr)
|
2020-06-12 09:51:42 +08:00
|
|
|
|
, m_pluginLoaded(false)
|
2016-06-15 17:44:38 +08:00
|
|
|
|
{
|
2019-09-03 09:37:59 +08:00
|
|
|
|
QDBusConnection sessionBus = QDBusConnection::sessionBus();
|
2022-09-16 20:12:52 +00:00
|
|
|
|
sessionBus.connect("org.deepin.daemon.Timedate1", "/org/deepin/daemon/Timedate1", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(propertiesChanged()));
|
2016-06-16 17:48:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 17:52:08 +08:00
|
|
|
|
PluginsItemInterface::PluginSizePolicy DatetimePlugin::pluginSizePolicy() const
|
|
|
|
|
{
|
|
|
|
|
return PluginsItemInterface::Custom;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-28 14:23:30 +08:00
|
|
|
|
const QString DatetimePlugin::pluginName() const
|
2016-06-15 17:44:38 +08:00
|
|
|
|
{
|
2016-06-16 16:56:21 +08:00
|
|
|
|
return "datetime";
|
2016-07-01 11:07:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-23 13:36:16 +08:00
|
|
|
|
const QString DatetimePlugin::pluginDisplayName() const
|
|
|
|
|
{
|
|
|
|
|
return tr("Datetime");
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 11:32:25 +08:00
|
|
|
|
void DatetimePlugin::init(PluginProxyInterface *proxyInter)
|
|
|
|
|
{
|
|
|
|
|
m_proxyInter = proxyInter;
|
2017-04-20 17:22:19 +08:00
|
|
|
|
|
2018-11-08 11:39:26 +08:00
|
|
|
|
// transfer config
|
|
|
|
|
QSettings settings("deepin", "dde-dock-datetime");
|
|
|
|
|
if (QFile::exists(settings.fileName())) {
|
|
|
|
|
Dock::DisplayMode mode = displayMode();
|
2020-07-22 20:38:24 +08:00
|
|
|
|
const QString key = QString("pos_%1_%2").arg(pluginName()).arg(mode);
|
2020-07-23 14:00:44 +08:00
|
|
|
|
proxyInter->saveValue(this, key, settings.value(key, mode == Dock::DisplayMode::Fashion ? 6 : -1));
|
2018-11-08 11:39:26 +08:00
|
|
|
|
QFile::remove(settings.fileName());
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 16:58:31 +08:00
|
|
|
|
if (pluginIsDisable()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 11:07:02 +08:00
|
|
|
|
loadPlugin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DatetimePlugin::loadPlugin()
|
|
|
|
|
{
|
|
|
|
|
if (m_pluginLoaded)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_pluginLoaded = true;
|
2021-05-25 13:05:45 +08:00
|
|
|
|
m_dateTipsLabel.reset(new TipsWidget);
|
2019-06-28 16:58:31 +08:00
|
|
|
|
m_refershTimer = new QTimer(this);
|
|
|
|
|
m_dateTipsLabel->setObjectName("datetime");
|
|
|
|
|
|
|
|
|
|
m_refershTimer->setInterval(1000);
|
|
|
|
|
m_refershTimer->start();
|
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
m_centralWidget.reset(new DatetimeWidget);
|
2019-06-28 16:58:31 +08:00
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
connect(m_centralWidget.data(), &DatetimeWidget::requestUpdateGeometry, [this] { m_proxyInter->itemUpdate(this, pluginName()); });
|
2019-06-28 16:58:31 +08:00
|
|
|
|
connect(m_refershTimer, &QTimer::timeout, this, &DatetimePlugin::updateCurrentTimeString);
|
2019-09-04 11:07:02 +08:00
|
|
|
|
|
2019-06-28 16:58:31 +08:00
|
|
|
|
m_proxyInter->itemAdded(this, pluginName());
|
2019-09-10 15:57:40 +08:00
|
|
|
|
|
|
|
|
|
pluginSettingsChanged();
|
2017-10-23 13:36:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DatetimePlugin::pluginStateSwitched()
|
|
|
|
|
{
|
2018-11-20 14:45:35 +08:00
|
|
|
|
m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, pluginIsDisable());
|
2017-10-23 13:36:16 +08:00
|
|
|
|
|
2019-01-30 18:00:46 +08:00
|
|
|
|
refreshPluginItemsVisible();
|
2017-10-23 13:36:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DatetimePlugin::pluginIsDisable()
|
|
|
|
|
{
|
2018-11-20 14:45:35 +08:00
|
|
|
|
return !(m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool());
|
2016-06-24 11:32:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 20:14:45 +08:00
|
|
|
|
int DatetimePlugin::itemSortKey(const QString &itemKey)
|
2016-06-28 10:06:04 +08:00
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2020-07-22 20:38:24 +08:00
|
|
|
|
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
|
2020-07-23 14:00:44 +08:00
|
|
|
|
return m_proxyInter->getValue(this, key, 6).toInt();
|
2018-03-06 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DatetimePlugin::setSortKey(const QString &itemKey, const int order)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2020-07-22 20:38:24 +08:00
|
|
|
|
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
|
2018-11-08 11:39:26 +08:00
|
|
|
|
m_proxyInter->saveValue(this, key, order);
|
2016-06-28 10:06:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 11:32:25 +08:00
|
|
|
|
QWidget *DatetimePlugin::itemWidget(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
return m_centralWidget.data();
|
2016-06-16 17:48:19 +08:00
|
|
|
|
}
|
2016-06-27 15:34:54 +08:00
|
|
|
|
|
2016-07-01 11:07:20 +08:00
|
|
|
|
QWidget *DatetimePlugin::itemTipsWidget(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
return m_dateTipsLabel.data();
|
2016-07-13 10:08:38 +08:00
|
|
|
|
}
|
2016-07-01 11:20:26 +08:00
|
|
|
|
|
2016-07-13 10:08:38 +08:00
|
|
|
|
const QString DatetimePlugin::itemCommand(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2022-07-15 14:30:03 +08:00
|
|
|
|
return "dbus-send --print-reply --dest=org.deepin.dde.Widgets / org.deepin.dde.Widgets.Toggle";
|
2016-07-01 11:07:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 10:24:42 +08:00
|
|
|
|
const QString DatetimePlugin::itemContextMenu(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
|
|
|
|
QList<QVariant> items;
|
|
|
|
|
items.reserve(1);
|
|
|
|
|
|
2017-03-30 16:48:34 +08:00
|
|
|
|
QMap<QString, QVariant> settings;
|
|
|
|
|
settings["itemId"] = "settings";
|
|
|
|
|
if (m_centralWidget->is24HourFormat())
|
2020-05-27 12:24:01 +08:00
|
|
|
|
settings["itemText"] = tr("12-hour time");
|
2017-03-30 16:48:34 +08:00
|
|
|
|
else
|
2020-05-27 12:24:01 +08:00
|
|
|
|
settings["itemText"] = tr("24-hour time");
|
2017-03-30 16:48:34 +08:00
|
|
|
|
settings["isActive"] = true;
|
|
|
|
|
items.push_back(settings);
|
|
|
|
|
|
2020-08-19 14:13:35 +08:00
|
|
|
|
if (!QFile::exists(ICBC_CONF_FILE)) {
|
|
|
|
|
QMap<QString, QVariant> open;
|
|
|
|
|
open["itemId"] = "open";
|
|
|
|
|
open["itemText"] = tr("Time settings");
|
|
|
|
|
open["isActive"] = true;
|
|
|
|
|
items.push_back(open);
|
|
|
|
|
}
|
2016-09-21 10:24:42 +08:00
|
|
|
|
|
|
|
|
|
QMap<QString, QVariant> menu;
|
|
|
|
|
menu["items"] = items;
|
|
|
|
|
menu["checkableMenu"] = false;
|
|
|
|
|
menu["singleCheck"] = false;
|
|
|
|
|
|
|
|
|
|
return QJsonDocument::fromVariant(menu).toJson();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DatetimePlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey)
|
|
|
|
|
Q_UNUSED(checked)
|
|
|
|
|
|
2018-04-16 15:44:35 +08:00
|
|
|
|
if (menuId == "open") {
|
2022-08-16 10:29:10 +00:00
|
|
|
|
#ifdef USE_AM
|
2018-04-16 15:44:35 +08:00
|
|
|
|
DDBusSender()
|
2022-08-16 10:29:10 +00:00
|
|
|
|
.service("org.deepin.dde.ControlCenter1")
|
|
|
|
|
.interface("org.deepin.dde.ControlCenter1")
|
|
|
|
|
.path("/org/deepin/dde/ControlCenter1")
|
2022-06-14 12:57:16 +00:00
|
|
|
|
.method(QString("ShowPage"))
|
2021-05-25 13:05:45 +08:00
|
|
|
|
.arg(QString("datetime"))
|
|
|
|
|
.call();
|
2022-08-16 10:29:10 +00:00
|
|
|
|
#else
|
|
|
|
|
DDBusSender()
|
|
|
|
|
.service("com.deepin.dde.ControlCenter")
|
|
|
|
|
.interface("com.deepin.dde.ControlCenter")
|
|
|
|
|
.path("/com/deepin/dde/ControlCenter")
|
|
|
|
|
.method(QString("ShowPage"))
|
|
|
|
|
.arg(QString("datetime"))
|
|
|
|
|
.call();
|
|
|
|
|
#endif
|
2018-04-16 15:44:35 +08:00
|
|
|
|
} else {
|
2019-09-20 17:57:33 +08:00
|
|
|
|
const bool value = timedateInterface()->property(TIME_FORMAT_KEY).toBool();
|
|
|
|
|
timedateInterface()->setProperty(TIME_FORMAT_KEY, !value);
|
2018-11-20 14:45:35 +08:00
|
|
|
|
m_centralWidget->set24HourFormat(!value);
|
2018-04-16 15:44:35 +08:00
|
|
|
|
}
|
2016-09-21 10:24:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 18:00:46 +08:00
|
|
|
|
void DatetimePlugin::pluginSettingsChanged()
|
|
|
|
|
{
|
2019-09-20 17:57:33 +08:00
|
|
|
|
if (!m_pluginLoaded)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const bool value = timedateInterface()->property(TIME_FORMAT_KEY).toBool();
|
2019-09-03 09:37:59 +08:00
|
|
|
|
|
|
|
|
|
m_proxyInter->saveValue(this, TIME_FORMAT_KEY, value);
|
|
|
|
|
m_centralWidget->set24HourFormat(value);
|
2019-01-30 18:00:46 +08:00
|
|
|
|
|
|
|
|
|
refreshPluginItemsVisible();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-27 15:34:54 +08:00
|
|
|
|
void DatetimePlugin::updateCurrentTimeString()
|
|
|
|
|
{
|
2016-07-13 10:08:38 +08:00
|
|
|
|
const QDateTime currentDateTime = QDateTime::currentDateTime();
|
|
|
|
|
|
2017-03-31 16:48:49 +08:00
|
|
|
|
if (m_centralWidget->is24HourFormat())
|
|
|
|
|
m_dateTipsLabel->setText(currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(" HH:mm:ss"));
|
|
|
|
|
else
|
|
|
|
|
m_dateTipsLabel->setText(currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(" hh:mm:ss A"));
|
2016-07-13 10:08:38 +08:00
|
|
|
|
|
2019-09-29 17:23:33 +08:00
|
|
|
|
const QString currentString = currentDateTime.toString("yyyy/MM/dd hh:mm");
|
2016-06-27 15:34:54 +08:00
|
|
|
|
|
|
|
|
|
if (currentString == m_currentTimeString)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_currentTimeString = currentString;
|
2021-05-29 17:23:34 +08:00
|
|
|
|
//当时间显示格式为12小时制且格式为0:00时,当从9:59变到10:00时,插件宽度需要变化
|
|
|
|
|
m_centralWidget->requestUpdateGeometry();
|
2016-06-27 15:34:54 +08:00
|
|
|
|
}
|
2019-01-30 18:00:46 +08:00
|
|
|
|
|
|
|
|
|
void DatetimePlugin::refreshPluginItemsVisible()
|
|
|
|
|
{
|
2019-09-04 11:07:02 +08:00
|
|
|
|
if (!pluginIsDisable()) {
|
|
|
|
|
|
|
|
|
|
if (!m_pluginLoaded) {
|
|
|
|
|
loadPlugin();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-01-30 18:00:46 +08:00
|
|
|
|
m_proxyInter->itemAdded(this, pluginName());
|
2019-09-04 11:07:02 +08:00
|
|
|
|
} else {
|
2019-01-30 18:00:46 +08:00
|
|
|
|
m_proxyInter->itemRemoved(this, pluginName());
|
2019-09-04 11:07:02 +08:00
|
|
|
|
}
|
2019-01-30 18:00:46 +08:00
|
|
|
|
}
|
2019-09-03 09:37:59 +08:00
|
|
|
|
|
|
|
|
|
void DatetimePlugin::propertiesChanged()
|
|
|
|
|
{
|
|
|
|
|
pluginSettingsChanged();
|
|
|
|
|
}
|
2019-09-20 17:57:33 +08:00
|
|
|
|
|
|
|
|
|
QDBusInterface* DatetimePlugin::timedateInterface()
|
|
|
|
|
{
|
|
|
|
|
if (!m_interface) {
|
2022-09-16 20:12:52 +00:00
|
|
|
|
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.deepin.daemon.Timedate1")) {
|
|
|
|
|
m_interface = new QDBusInterface("org.deepin.daemon.Timedate1", "/org/deepin/daemon/Timedate1", "org.deepin.daemon.Timedate1", QDBusConnection::sessionBus(), this);
|
2019-09-20 17:57:33 +08:00
|
|
|
|
} else {
|
2022-09-16 20:12:52 +00:00
|
|
|
|
const QString path = QString("/org/deepin/daemon/Accounts/User%1").arg(QString::number(getuid()));
|
|
|
|
|
QDBusInterface * systemInterface = new QDBusInterface("org.deepin.daemon.Accounts1", path, "org.deepin.daemon.Accounts.User",
|
2021-05-25 13:05:45 +08:00
|
|
|
|
QDBusConnection::systemBus(), this);
|
2019-09-20 17:57:33 +08:00
|
|
|
|
return systemInterface;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_interface;
|
|
|
|
|
}
|