mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-07 07:33:52 +02:00
Subscribe to StatusNotifierHostRegistered signal
This commit is contained in:
parent
6f89598a7b
commit
093fcc3821
4 changed files with 113 additions and 43 deletions
|
@ -39,6 +39,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include <QtDBus/QDBusReply>
|
#include <QtDBus/QDBusReply>
|
||||||
#include <QtDBus/QDBusError>
|
#include <QtDBus/QDBusError>
|
||||||
#include <QtDBus/QDBusMetaType>
|
#include <QtDBus/QDBusMetaType>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#undef signals
|
||||||
|
#include <gio/gio.h>
|
||||||
|
#define signals public
|
||||||
|
} // extern "C"
|
||||||
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
@ -51,10 +57,13 @@ constexpr auto kForcePanelIcon = "TDESKTOP_FORCE_PANEL_ICON"_cs;
|
||||||
constexpr auto kPanelTrayIconName = "telegram-panel"_cs;
|
constexpr auto kPanelTrayIconName = "telegram-panel"_cs;
|
||||||
constexpr auto kMutePanelTrayIconName = "telegram-mute-panel"_cs;
|
constexpr auto kMutePanelTrayIconName = "telegram-mute-panel"_cs;
|
||||||
constexpr auto kAttentionPanelTrayIconName = "telegram-attention-panel"_cs;
|
constexpr auto kAttentionPanelTrayIconName = "telegram-attention-panel"_cs;
|
||||||
constexpr auto kSNIWatcherService = "org.kde.StatusNotifierWatcher"_cs;
|
|
||||||
constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs;
|
constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs;
|
||||||
constexpr auto kTrayIconFilename = "tdesktop-trayicon-XXXXXX.png"_cs;
|
constexpr auto kTrayIconFilename = "tdesktop-trayicon-XXXXXX.png"_cs;
|
||||||
|
|
||||||
|
constexpr auto kSNIWatcherService = "org.kde.StatusNotifierWatcher"_cs;
|
||||||
|
constexpr auto kSNIWatcherObjectPath = "/StatusNotifierWatcher"_cs;
|
||||||
|
constexpr auto kSNIWatcherInterface = kSNIWatcherService;
|
||||||
|
|
||||||
constexpr auto kAppMenuService = "com.canonical.AppMenu.Registrar"_cs;
|
constexpr auto kAppMenuService = "com.canonical.AppMenu.Registrar"_cs;
|
||||||
constexpr auto kAppMenuObjectPath = "/com/canonical/AppMenu/Registrar"_cs;
|
constexpr auto kAppMenuObjectPath = "/com/canonical/AppMenu/Registrar"_cs;
|
||||||
constexpr auto kAppMenuInterface = kAppMenuService;
|
constexpr auto kAppMenuInterface = kAppMenuService;
|
||||||
|
@ -65,9 +74,6 @@ base::flat_map<int, QImage> TrayIconImageBack;
|
||||||
QIcon TrayIcon;
|
QIcon TrayIcon;
|
||||||
QString TrayIconThemeName, TrayIconName;
|
QString TrayIconThemeName, TrayIconName;
|
||||||
|
|
||||||
bool SNIAvailable = false;
|
|
||||||
bool AppMenuSupported = false;
|
|
||||||
|
|
||||||
QString GetPanelIconName(int counter, bool muted) {
|
QString GetPanelIconName(int counter, bool muted) {
|
||||||
return (counter > 0)
|
return (counter > 0)
|
||||||
? (muted
|
? (muted
|
||||||
|
@ -222,7 +228,13 @@ QIcon TrayIconGen(int counter, bool muted) {
|
||||||
iconImage.height() - layer.height() - 1,
|
iconImage.height() - layer.height() - 1,
|
||||||
layer);
|
layer);
|
||||||
} else {
|
} else {
|
||||||
App::wnd()->placeSmallCounter(iconImage, 16, counter, bg, QPoint(), fg);
|
App::wnd()->placeSmallCounter(
|
||||||
|
iconImage,
|
||||||
|
16,
|
||||||
|
counter,
|
||||||
|
bg,
|
||||||
|
QPoint(),
|
||||||
|
fg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,7 +324,7 @@ bool IsSNIAvailable() {
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
auto message = QDBusMessage::createMethodCall(
|
auto message = QDBusMessage::createMethodCall(
|
||||||
kSNIWatcherService.utf16(),
|
kSNIWatcherService.utf16(),
|
||||||
qsl("/StatusNotifierWatcher"),
|
kSNIWatcherObjectPath.utf16(),
|
||||||
kPropertiesInterface.utf16(),
|
kPropertiesInterface.utf16(),
|
||||||
qsl("Get"));
|
qsl("Get"));
|
||||||
|
|
||||||
|
@ -416,15 +428,26 @@ MainWindow::MainWindow(not_null<Window::Controller*> controller)
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::initHook() {
|
void MainWindow::initHook() {
|
||||||
SNIAvailable = IsSNIAvailable();
|
_sniAvailable = IsSNIAvailable();
|
||||||
|
LOG(("System tray available: %1").arg(Logs::b(trayAvailable())));
|
||||||
const auto trayAvailable = SNIAvailable
|
|
||||||
|| QSystemTrayIcon::isSystemTrayAvailable();
|
|
||||||
|
|
||||||
LOG(("System tray available: %1").arg(Logs::b(trayAvailable)));
|
|
||||||
Platform::SetTrayIconSupported(trayAvailable);
|
|
||||||
|
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
_sniDBusProxy = g_dbus_proxy_new_for_bus_sync(
|
||||||
|
G_BUS_TYPE_SESSION,
|
||||||
|
G_DBUS_PROXY_FLAGS_NONE,
|
||||||
|
nullptr,
|
||||||
|
kSNIWatcherService.utf8(),
|
||||||
|
kSNIWatcherObjectPath.utf8(),
|
||||||
|
kSNIWatcherInterface.utf8(),
|
||||||
|
nullptr,
|
||||||
|
nullptr);
|
||||||
|
|
||||||
|
g_signal_connect(
|
||||||
|
_sniDBusProxy,
|
||||||
|
"g-signal",
|
||||||
|
G_CALLBACK(sniSignalEmitted),
|
||||||
|
this);
|
||||||
|
|
||||||
auto sniWatcher = new QDBusServiceWatcher(
|
auto sniWatcher = new QDBusServiceWatcher(
|
||||||
kSNIWatcherService.utf16(),
|
kSNIWatcherService.utf16(),
|
||||||
QDBusConnection::sessionBus(),
|
QDBusConnection::sessionBus(),
|
||||||
|
@ -442,7 +465,7 @@ void MainWindow::initHook() {
|
||||||
handleSNIOwnerChanged(service, oldOwner, newOwner);
|
handleSNIOwnerChanged(service, oldOwner, newOwner);
|
||||||
});
|
});
|
||||||
|
|
||||||
AppMenuSupported = IsAppMenuSupported();
|
_appMenuSupported = IsAppMenuSupported();
|
||||||
|
|
||||||
auto appMenuWatcher = new QDBusServiceWatcher(
|
auto appMenuWatcher = new QDBusServiceWatcher(
|
||||||
kAppMenuService.utf16(),
|
kAppMenuService.utf16(),
|
||||||
|
@ -461,7 +484,7 @@ void MainWindow::initHook() {
|
||||||
handleAppMenuOwnerChanged(service, oldOwner, newOwner);
|
handleAppMenuOwnerChanged(service, oldOwner, newOwner);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (AppMenuSupported) {
|
if (_appMenuSupported) {
|
||||||
LOG(("Using D-Bus global menu."));
|
LOG(("Using D-Bus global menu."));
|
||||||
} else {
|
} else {
|
||||||
LOG(("Not using D-Bus global menu."));
|
LOG(("Not using D-Bus global menu."));
|
||||||
|
@ -484,7 +507,7 @@ void MainWindow::initHook() {
|
||||||
|
|
||||||
bool MainWindow::hasTrayIcon() const {
|
bool MainWindow::hasTrayIcon() const {
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
return trayIcon || (SNIAvailable && _sniTrayIcon);
|
return trayIcon || (_sniAvailable && _sniTrayIcon);
|
||||||
#else
|
#else
|
||||||
return trayIcon;
|
return trayIcon;
|
||||||
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
@ -564,22 +587,50 @@ void MainWindow::attachToSNITrayIcon() {
|
||||||
updateTrayMenu();
|
updateTrayMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::handleSNIOwnerChanged(
|
void MainWindow::sniSignalEmitted(
|
||||||
const QString &service,
|
GDBusProxy *proxy,
|
||||||
const QString &oldOwner,
|
gchar *sender_name,
|
||||||
const QString &newOwner) {
|
gchar *signal_name,
|
||||||
SNIAvailable = !newOwner.isEmpty();
|
GVariant *parameters,
|
||||||
|
MainWindow *window) {
|
||||||
|
if(signal_name == qstr("StatusNotifierHostRegistered")) {
|
||||||
|
window->handleSNIHostRegistered();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const auto trayAvailable = SNIAvailable
|
void MainWindow::handleSNIHostRegistered() {
|
||||||
|| QSystemTrayIcon::isSystemTrayAvailable();
|
if (_sniAvailable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Platform::SetTrayIconSupported(trayAvailable);
|
_sniAvailable = true;
|
||||||
|
|
||||||
if (Global::WorkMode().value() == dbiwmWindowOnly) {
|
if (Global::WorkMode().value() == dbiwmWindowOnly) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldOwner.isEmpty() && !newOwner.isEmpty()) {
|
LOG(("Switching to SNI tray icon..."));
|
||||||
|
|
||||||
|
if (trayIcon) {
|
||||||
|
trayIcon->setContextMenu(0);
|
||||||
|
trayIcon->deleteLater();
|
||||||
|
}
|
||||||
|
trayIcon = nullptr;
|
||||||
|
|
||||||
|
psSetupTrayIcon();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::handleSNIOwnerChanged(
|
||||||
|
const QString &service,
|
||||||
|
const QString &oldOwner,
|
||||||
|
const QString &newOwner) {
|
||||||
|
_sniAvailable = IsSNIAvailable();
|
||||||
|
|
||||||
|
if (Global::WorkMode().value() == dbiwmWindowOnly) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldOwner.isEmpty() && !newOwner.isEmpty() && _sniAvailable) {
|
||||||
LOG(("Switching to SNI tray icon..."));
|
LOG(("Switching to SNI tray icon..."));
|
||||||
} else if (!oldOwner.isEmpty() && newOwner.isEmpty()) {
|
} else if (!oldOwner.isEmpty() && newOwner.isEmpty()) {
|
||||||
LOG(("Switching to Qt tray icon..."));
|
LOG(("Switching to Qt tray icon..."));
|
||||||
|
@ -593,7 +644,7 @@ void MainWindow::handleSNIOwnerChanged(
|
||||||
}
|
}
|
||||||
trayIcon = nullptr;
|
trayIcon = nullptr;
|
||||||
|
|
||||||
if (trayAvailable) {
|
if (trayAvailable()) {
|
||||||
psSetupTrayIcon();
|
psSetupTrayIcon();
|
||||||
} else {
|
} else {
|
||||||
LOG(("System tray is not available."));
|
LOG(("System tray is not available."));
|
||||||
|
@ -605,14 +656,14 @@ void MainWindow::handleAppMenuOwnerChanged(
|
||||||
const QString &oldOwner,
|
const QString &oldOwner,
|
||||||
const QString &newOwner) {
|
const QString &newOwner) {
|
||||||
if (oldOwner.isEmpty() && !newOwner.isEmpty()) {
|
if (oldOwner.isEmpty() && !newOwner.isEmpty()) {
|
||||||
AppMenuSupported = true;
|
_appMenuSupported = true;
|
||||||
LOG(("Using D-Bus global menu."));
|
LOG(("Using D-Bus global menu."));
|
||||||
} else if (!oldOwner.isEmpty() && newOwner.isEmpty()) {
|
} else if (!oldOwner.isEmpty() && newOwner.isEmpty()) {
|
||||||
AppMenuSupported = false;
|
_appMenuSupported = false;
|
||||||
LOG(("Not using D-Bus global menu."));
|
LOG(("Not using D-Bus global menu."));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (AppMenuSupported && !_mainMenuPath.path().isEmpty()) {
|
if (_appMenuSupported && !_mainMenuPath.path().isEmpty()) {
|
||||||
RegisterAppMenu(winId(), _mainMenuPath);
|
RegisterAppMenu(winId(), _mainMenuPath);
|
||||||
} else {
|
} else {
|
||||||
UnregisterAppMenu(winId());
|
UnregisterAppMenu(winId());
|
||||||
|
@ -624,7 +675,7 @@ void MainWindow::psSetupTrayIcon() {
|
||||||
const auto counter = Core::App().unreadBadge();
|
const auto counter = Core::App().unreadBadge();
|
||||||
const auto muted = Core::App().unreadBadgeMuted();
|
const auto muted = Core::App().unreadBadgeMuted();
|
||||||
|
|
||||||
if (SNIAvailable) {
|
if (_sniAvailable) {
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
LOG(("Using SNI tray icon."));
|
LOG(("Using SNI tray icon."));
|
||||||
if (!_sniTrayIcon) {
|
if (!_sniTrayIcon) {
|
||||||
|
@ -654,7 +705,7 @@ void MainWindow::psSetupTrayIcon() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::workmodeUpdated(DBIWorkMode mode) {
|
void MainWindow::workmodeUpdated(DBIWorkMode mode) {
|
||||||
if (!Platform::TrayIconSupported()) {
|
if (!trayAvailable()) {
|
||||||
return;
|
return;
|
||||||
} else if (mode == dbiwmWindowOnly) {
|
} else if (mode == dbiwmWindowOnly) {
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
@ -953,7 +1004,7 @@ void MainWindow::createGlobalMenu() {
|
||||||
_mainMenuPath.path(),
|
_mainMenuPath.path(),
|
||||||
psMainMenu);
|
psMainMenu);
|
||||||
|
|
||||||
if (AppMenuSupported) {
|
if (_appMenuSupported) {
|
||||||
RegisterAppMenu(winId(), _mainMenuPath);
|
RegisterAppMenu(winId(), _mainMenuPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1074,7 +1125,7 @@ void MainWindow::updateGlobalMenuHook() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::handleVisibleChangedHook(bool visible) {
|
void MainWindow::handleVisibleChangedHook(bool visible) {
|
||||||
if (AppMenuSupported && !_mainMenuPath.path().isEmpty()) {
|
if (_appMenuSupported && !_mainMenuPath.path().isEmpty()) {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
RegisterAppMenu(winId(), _mainMenuPath);
|
RegisterAppMenu(winId(), _mainMenuPath);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1089,12 +1140,14 @@ MainWindow::~MainWindow() {
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
delete _sniTrayIcon;
|
delete _sniTrayIcon;
|
||||||
|
|
||||||
if (AppMenuSupported) {
|
if (_appMenuSupported) {
|
||||||
UnregisterAppMenu(winId());
|
UnregisterAppMenu(winId());
|
||||||
}
|
}
|
||||||
|
|
||||||
delete _mainMenuExporter;
|
delete _mainMenuExporter;
|
||||||
delete psMainMenu;
|
delete psMainMenu;
|
||||||
|
|
||||||
|
g_object_unref(_sniDBusProxy);
|
||||||
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
|
||||||
delete _trayIconMenuXEmbed;
|
delete _trayIconMenuXEmbed;
|
||||||
|
|
|
@ -16,7 +16,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include <QtCore/QTemporaryFile>
|
#include <QtCore/QTemporaryFile>
|
||||||
#include <QtDBus/QDBusObjectPath>
|
#include <QtDBus/QDBusObjectPath>
|
||||||
#include <dbusmenuexporter.h>
|
#include <dbusmenuexporter.h>
|
||||||
#endif
|
|
||||||
|
typedef char gchar;
|
||||||
|
typedef struct _GVariant GVariant;
|
||||||
|
typedef struct _GDBusProxy GDBusProxy;
|
||||||
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
|
||||||
namespace Platform {
|
namespace Platform {
|
||||||
|
|
||||||
|
@ -33,6 +37,14 @@ public:
|
||||||
|
|
||||||
void psShowTrayMenu();
|
void psShowTrayMenu();
|
||||||
|
|
||||||
|
bool trayAvailable() {
|
||||||
|
return _sniAvailable || QSystemTrayIcon::isSystemTrayAvailable();
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
void handleSNIHostRegistered();
|
||||||
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
|
||||||
static void LibsLoaded();
|
static void LibsLoaded();
|
||||||
|
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
|
@ -64,6 +76,7 @@ protected:
|
||||||
style::color color) = 0;
|
style::color color) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool _sniAvailable = false;
|
||||||
Ui::PopupMenu *_trayIconMenuXEmbed = nullptr;
|
Ui::PopupMenu *_trayIconMenuXEmbed = nullptr;
|
||||||
|
|
||||||
void updateIconCounters();
|
void updateIconCounters();
|
||||||
|
@ -71,8 +84,10 @@ private:
|
||||||
|
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
StatusNotifierItem *_sniTrayIcon = nullptr;
|
StatusNotifierItem *_sniTrayIcon = nullptr;
|
||||||
|
GDBusProxy *_sniDBusProxy = nullptr;
|
||||||
std::unique_ptr<QTemporaryFile> _trayIconFile = nullptr;
|
std::unique_ptr<QTemporaryFile> _trayIconFile = nullptr;
|
||||||
|
|
||||||
|
bool _appMenuSupported = false;
|
||||||
DBusMenuExporter *_mainMenuExporter = nullptr;
|
DBusMenuExporter *_mainMenuExporter = nullptr;
|
||||||
QDBusObjectPath _mainMenuPath;
|
QDBusObjectPath _mainMenuPath;
|
||||||
|
|
||||||
|
@ -124,6 +139,13 @@ private:
|
||||||
void psLinuxStrikeOut();
|
void psLinuxStrikeOut();
|
||||||
void psLinuxMonospace();
|
void psLinuxMonospace();
|
||||||
void psLinuxClearFormat();
|
void psLinuxClearFormat();
|
||||||
|
|
||||||
|
static void sniSignalEmitted(
|
||||||
|
GDBusProxy *proxy,
|
||||||
|
gchar *sender_name,
|
||||||
|
gchar *signal_name,
|
||||||
|
GVariant *parameters,
|
||||||
|
MainWindow *window);
|
||||||
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -85,8 +85,6 @@ constexpr auto kXCBFrameExtentsAtomName = "_GTK_FRAME_EXTENTS"_cs;
|
||||||
|
|
||||||
QStringList PlatformThemes;
|
QStringList PlatformThemes;
|
||||||
|
|
||||||
bool IsTrayIconSupported = false;
|
|
||||||
|
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
void PortalAutostart(bool autostart, bool silent = false) {
|
void PortalAutostart(bool autostart, bool silent = false) {
|
||||||
if (cExeName().isEmpty()) {
|
if (cExeName().isEmpty()) {
|
||||||
|
@ -1066,11 +1064,9 @@ bool AutostartSupported() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TrayIconSupported() {
|
bool TrayIconSupported() {
|
||||||
return IsTrayIconSupported;
|
return App::wnd()
|
||||||
}
|
? App::wnd()->trayAvailable()
|
||||||
|
: false;
|
||||||
void SetTrayIconSupported(bool supported) {
|
|
||||||
IsTrayIconSupported = supported;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StartSystemMove(QWindow *window) {
|
bool StartSystemMove(QWindow *window) {
|
||||||
|
|
|
@ -42,7 +42,6 @@ QString GetIconName();
|
||||||
inline void IgnoreApplicationActivationRightNow() {
|
inline void IgnoreApplicationActivationRightNow() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetTrayIconSupported(bool supported);
|
|
||||||
void InstallMainDesktopFile();
|
void InstallMainDesktopFile();
|
||||||
|
|
||||||
} // namespace Platform
|
} // namespace Platform
|
||||||
|
|
Loading…
Add table
Reference in a new issue