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"
|
|
|
|
|
2018-04-16 15:44:35 +08:00
|
|
|
#include <DDBusSender>
|
2016-07-13 10:08:38 +08:00
|
|
|
#include <QLabel>
|
2018-03-06 15:18:53 +08:00
|
|
|
#include <QDebug>
|
2016-07-13 10:08:38 +08:00
|
|
|
|
2018-11-20 14:45:35 +08:00
|
|
|
#define PLUGIN_STATE_KEY "enable"
|
|
|
|
#define TIME_FORMAT_KEY "24HourFormat"
|
|
|
|
|
2016-06-15 17:44:38 +08:00
|
|
|
DatetimePlugin::DatetimePlugin(QObject *parent)
|
2016-06-16 17:48:19 +08:00
|
|
|
: QObject(parent),
|
2016-07-01 11:07:20 +08:00
|
|
|
|
2018-07-04 16:17:01 +08:00
|
|
|
m_dateTipsLabel(new TipsWidget),
|
2016-07-01 11:07:20 +08:00
|
|
|
|
2018-11-08 11:39:26 +08:00
|
|
|
m_refershTimer(new QTimer(this))
|
2016-06-15 17:44:38 +08:00
|
|
|
{
|
2016-07-28 10:59:21 +08:00
|
|
|
m_dateTipsLabel->setObjectName("datetime");
|
2016-07-01 11:07:20 +08:00
|
|
|
|
2016-06-16 17:48:19 +08:00
|
|
|
m_refershTimer->setInterval(1000);
|
|
|
|
m_refershTimer->start();
|
|
|
|
|
2017-02-06 22:25:47 +08:00
|
|
|
m_centralWidget = new DatetimeWidget;
|
2016-06-23 10:24:51 +08:00
|
|
|
|
2019-01-07 09:14:37 +08:00
|
|
|
connect(m_centralWidget, &DatetimeWidget::requestUpdateGeometry, [this] { m_proxyInter->itemUpdate(this, pluginName()); });
|
2016-09-21 10:24:42 +08:00
|
|
|
|
2016-06-27 15:34:54 +08:00
|
|
|
connect(m_refershTimer, &QTimer::timeout, this, &DatetimePlugin::updateCurrentTimeString);
|
2016-06-16 17:48:19 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2018-11-20 14:45:35 +08:00
|
|
|
m_centralWidget->set24HourFormat(m_proxyInter->getValue(this, TIME_FORMAT_KEY, true).toBool());
|
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();
|
|
|
|
const QString key = QString("pos_%1").arg(mode);
|
|
|
|
proxyInter->saveValue(this, key, settings.value(key, mode == Dock::DisplayMode::Fashion ? 5 : -1));
|
|
|
|
QFile::remove(settings.fileName());
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:45:35 +08:00
|
|
|
if (!pluginIsDisable())
|
2019-01-07 09:14:37 +08:00
|
|
|
m_proxyInter->itemAdded(this, pluginName());
|
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);
|
|
|
|
|
2018-09-06 10:42:27 +08:00
|
|
|
Dock::DisplayMode mode = displayMode();
|
|
|
|
const QString key = QString("pos_%1").arg(mode);
|
|
|
|
|
|
|
|
if (mode == Dock::DisplayMode::Fashion) {
|
2018-11-21 15:37:49 +08:00
|
|
|
return m_proxyInter->getValue(this, key, 3).toInt();
|
2018-09-06 10:42:27 +08:00
|
|
|
} else {
|
2018-11-08 11:39:26 +08:00
|
|
|
return m_proxyInter->getValue(this, key, -1).toInt();
|
2018-09-06 10:42:27 +08:00
|
|
|
}
|
2018-03-06 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DatetimePlugin::setSortKey(const QString &itemKey, const int order)
|
|
|
|
{
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
|
|
const QString key = QString("pos_%1").arg(displayMode());
|
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);
|
|
|
|
|
2017-02-06 22:25:47 +08:00
|
|
|
return m_centralWidget;
|
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);
|
|
|
|
|
2016-07-13 10:08:38 +08:00
|
|
|
return m_dateTipsLabel;
|
|
|
|
}
|
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);
|
|
|
|
|
2017-10-18 13:16:04 +08:00
|
|
|
return "dbus-send --print-reply --dest=com.deepin.Calendar /com/deepin/Calendar com.deepin.Calendar.RaiseWindow";
|
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())
|
|
|
|
settings["itemText"] = tr("12 Hour Time");
|
|
|
|
else
|
|
|
|
settings["itemText"] = tr("24 Hour Time");
|
|
|
|
settings["isActive"] = true;
|
|
|
|
items.push_back(settings);
|
|
|
|
|
2016-09-21 10:24:42 +08:00
|
|
|
QMap<QString, QVariant> open;
|
|
|
|
open["itemId"] = "open";
|
|
|
|
open["itemText"] = tr("Time Settings");
|
|
|
|
open["isActive"] = true;
|
|
|
|
items.push_back(open);
|
|
|
|
|
|
|
|
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") {
|
|
|
|
DDBusSender()
|
|
|
|
.service("com.deepin.dde.ControlCenter")
|
|
|
|
.interface("com.deepin.dde.ControlCenter")
|
|
|
|
.path("/com/deepin/dde/ControlCenter")
|
|
|
|
.method(QString("ShowModule"))
|
|
|
|
.arg(QString("datetime"))
|
|
|
|
.call();
|
|
|
|
} else {
|
2018-11-20 14:45:35 +08:00
|
|
|
const bool value = m_proxyInter->getValue(this, TIME_FORMAT_KEY, true).toBool();
|
|
|
|
m_proxyInter->saveValue(this, TIME_FORMAT_KEY, !value);
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
m_centralWidget->set24HourFormat(m_proxyInter->getValue(this, TIME_FORMAT_KEY, true).toBool());
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
const QString currentString = currentDateTime.toString("mm");
|
2016-06-27 15:34:54 +08:00
|
|
|
|
|
|
|
if (currentString == m_currentTimeString)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_currentTimeString = currentString;
|
2017-02-06 22:25:47 +08:00
|
|
|
m_centralWidget->update();
|
2016-06-27 15:34:54 +08:00
|
|
|
}
|
2019-01-30 18:00:46 +08:00
|
|
|
|
|
|
|
void DatetimePlugin::refreshPluginItemsVisible()
|
|
|
|
{
|
|
|
|
if (!pluginIsDisable())
|
|
|
|
m_proxyInter->itemAdded(this, pluginName());
|
|
|
|
else
|
|
|
|
m_proxyInter->itemRemoved(this, pluginName());
|
|
|
|
}
|