dde-dock/plugins/datetime/datetimeplugin.cpp
donghualin c78f101e84 fix: 修复从任务栏无法打开控制中心问题
由于控制中心适配了v23的接口,所以需要在任务栏增加USE_AM宏的控制,未使用USE_AM宏的调用com.deepin.dde.ControlCenter接口,使用USE_AM宏的使用org.deepin.dde.ControlCenter1接口。

Log: 优化任务栏打开控制中心的问题
Influence: 任务栏-任务栏设置-打开控制中心 任务栏-蓝牙-蓝牙设置 任务栏-日期-日期时间设置 任务栏-电源-电源设置 任务栏-关机-关机设置 任务栏-声音-声音设置 观察是否正常打开控制中心
Bug: https://pms.uniontech.com/bug-view-149189.html
Change-Id: I3373fc641c35e7589730a7cd595f2f55929ad2ec
2022-08-16 10:29:26 +00:00

283 lines
8.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* 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/>.
*/
#include "datetimeplugin.h"
#include "../../widgets/tipswidget.h"
#include "../../frame/util/utils.h"
#include <DDBusSender>
#include <QDebug>
#include <QDBusConnectionInterface>
#include <unistd.h>
#define PLUGIN_STATE_KEY "enable"
#define TIME_FORMAT_KEY "Use24HourFormat"
using namespace Dock;
DatetimePlugin::DatetimePlugin(QObject *parent)
: QObject(parent)
, m_centralWidget(nullptr)
, m_dateTipsLabel(nullptr)
, m_refershTimer(nullptr)
, m_interface(nullptr)
, m_pluginLoaded(false)
{
QDBusConnection sessionBus = QDBusConnection::sessionBus();
sessionBus.connect("com.deepin.daemon.Timedate", "/com/deepin/daemon/Timedate", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(propertiesChanged()));
}
PluginsItemInterface::PluginSizePolicy DatetimePlugin::pluginSizePolicy() const
{
return PluginsItemInterface::Custom;
}
const QString DatetimePlugin::pluginName() const
{
return "datetime";
}
const QString DatetimePlugin::pluginDisplayName() const
{
return tr("Datetime");
}
void DatetimePlugin::init(PluginProxyInterface *proxyInter)
{
m_proxyInter = proxyInter;
// transfer config
QSettings settings("deepin", "dde-dock-datetime");
if (QFile::exists(settings.fileName())) {
Dock::DisplayMode mode = displayMode();
const QString key = QString("pos_%1_%2").arg(pluginName()).arg(mode);
proxyInter->saveValue(this, key, settings.value(key, mode == Dock::DisplayMode::Fashion ? 6 : -1));
QFile::remove(settings.fileName());
}
if (pluginIsDisable()) {
return;
}
loadPlugin();
}
void DatetimePlugin::loadPlugin()
{
if (m_pluginLoaded)
return;
m_pluginLoaded = true;
m_dateTipsLabel.reset(new TipsWidget);
m_refershTimer = new QTimer(this);
m_dateTipsLabel->setObjectName("datetime");
m_refershTimer->setInterval(1000);
m_refershTimer->start();
m_centralWidget.reset(new DatetimeWidget);
connect(m_centralWidget.data(), &DatetimeWidget::requestUpdateGeometry, [this] { m_proxyInter->itemUpdate(this, pluginName()); });
connect(m_refershTimer, &QTimer::timeout, this, &DatetimePlugin::updateCurrentTimeString);
m_proxyInter->itemAdded(this, pluginName());
pluginSettingsChanged();
}
void DatetimePlugin::pluginStateSwitched()
{
m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, pluginIsDisable());
refreshPluginItemsVisible();
}
bool DatetimePlugin::pluginIsDisable()
{
return !(m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool());
}
int DatetimePlugin::itemSortKey(const QString &itemKey)
{
Q_UNUSED(itemKey);
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
return m_proxyInter->getValue(this, key, 6).toInt();
}
void DatetimePlugin::setSortKey(const QString &itemKey, const int order)
{
Q_UNUSED(itemKey);
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
m_proxyInter->saveValue(this, key, order);
}
QWidget *DatetimePlugin::itemWidget(const QString &itemKey)
{
Q_UNUSED(itemKey);
return m_centralWidget.data();
}
QWidget *DatetimePlugin::itemTipsWidget(const QString &itemKey)
{
Q_UNUSED(itemKey);
return m_dateTipsLabel.data();
}
const QString DatetimePlugin::itemCommand(const QString &itemKey)
{
Q_UNUSED(itemKey);
return "dbus-send --print-reply --dest=org.deepin.dde.Widgets / org.deepin.dde.Widgets.Toggle";
}
const QString DatetimePlugin::itemContextMenu(const QString &itemKey)
{
Q_UNUSED(itemKey);
QList<QVariant> items;
items.reserve(1);
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);
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);
}
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)
if (menuId == "open") {
#ifdef USE_AM
DDBusSender()
.service("org.deepin.dde.ControlCenter1")
.interface("org.deepin.dde.ControlCenter1")
.path("/org/deepin/dde/ControlCenter1")
.method(QString("ShowPage"))
.arg(QString("datetime"))
.call();
#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
} else {
const bool value = timedateInterface()->property(TIME_FORMAT_KEY).toBool();
timedateInterface()->setProperty(TIME_FORMAT_KEY, !value);
m_centralWidget->set24HourFormat(!value);
}
}
void DatetimePlugin::pluginSettingsChanged()
{
if (!m_pluginLoaded)
return;
const bool value = timedateInterface()->property(TIME_FORMAT_KEY).toBool();
m_proxyInter->saveValue(this, TIME_FORMAT_KEY, value);
m_centralWidget->set24HourFormat(value);
refreshPluginItemsVisible();
}
void DatetimePlugin::updateCurrentTimeString()
{
const QDateTime currentDateTime = QDateTime::currentDateTime();
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"));
const QString currentString = currentDateTime.toString("yyyy/MM/dd hh:mm");
if (currentString == m_currentTimeString)
return;
m_currentTimeString = currentString;
//当时间显示格式为12小时制且格式为0:00时当从9:59变到10:00时插件宽度需要变化
m_centralWidget->requestUpdateGeometry();
}
void DatetimePlugin::refreshPluginItemsVisible()
{
if (!pluginIsDisable()) {
if (!m_pluginLoaded) {
loadPlugin();
return;
}
m_proxyInter->itemAdded(this, pluginName());
} else {
m_proxyInter->itemRemoved(this, pluginName());
}
}
void DatetimePlugin::propertiesChanged()
{
pluginSettingsChanged();
}
QDBusInterface* DatetimePlugin::timedateInterface()
{
if (!m_interface) {
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("com.deepin.daemon.Timedate")) {
m_interface = new QDBusInterface("com.deepin.daemon.Timedate", "/com/deepin/daemon/Timedate", "com.deepin.daemon.Timedate", QDBusConnection::sessionBus(), this);
} else {
const QString path = QString("/com/deepin/daemon/Accounts/User%1").arg(QString::number(getuid()));
QDBusInterface * systemInterface = new QDBusInterface("com.deepin.daemon.Accounts", path, "com.deepin.daemon.Accounts.User",
QDBusConnection::systemBus(), this);
return systemInterface;
}
}
return m_interface;
}