From fd7d24b11a3c99b46271e29d3759ea89876ebd6f Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sat, 18 Jun 2011 19:08:09 +0000 Subject: [PATCH] Add Clementine to the Unity system tray whitelist on startup. --- src/CMakeLists.txt | 2 + src/core/ubuntuunityhack.cpp | 85 ++++++++++++++++++++++++++++++++++++ src/core/ubuntuunityhack.h | 40 +++++++++++++++++ src/main.cpp | 6 +++ 4 files changed, 133 insertions(+) create mode 100644 src/core/ubuntuunityhack.cpp create mode 100644 src/core/ubuntuunityhack.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6ff8e1252..a3ab884d3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -90,6 +90,7 @@ set(SOURCES core/songloader.cpp core/stylesheetloader.cpp core/taskmanager.cpp + core/ubuntuunityhack.cpp core/urlhandler.cpp core/utilities.cpp @@ -317,6 +318,7 @@ set(HEADERS core/player.h core/songloader.h core/taskmanager.h + core/ubuntuunityhack.h core/urlhandler.h covers/albumcoverfetcher.h diff --git a/src/core/ubuntuunityhack.cpp b/src/core/ubuntuunityhack.cpp new file mode 100644 index 000000000..a45c35dac --- /dev/null +++ b/src/core/ubuntuunityhack.cpp @@ -0,0 +1,85 @@ +/* This file is part of Clementine. + Copyright 2010, David Sansome + + Clementine 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 + (at your option) any later version. + + Clementine 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 Clementine. If not, see . +*/ + +#include "ubuntuunityhack.h" +#include "core/logging.h" + +#include +#include +#include + +const char* UbuntuUnityHack::kGSettingsFileName = "gsettings"; +const char* UbuntuUnityHack::kUnityPanel = "com.canonical.Unity.Panel"; +const char* UbuntuUnityHack::kUnitySystrayWhitelist = "systray-whitelist"; + +UbuntuUnityHack::UbuntuUnityHack(QObject* parent) + : QObject(parent) +{ + // Get the systray whitelist from gsettings. If this fails we're probably + // not running on a system with unity + QProcess* get = new QProcess(this); + connect(get, SIGNAL(finished(int)), SLOT(GetFinished(int))); + connect(get, SIGNAL(error(QProcess::ProcessError)), SLOT(GetError())); + get->start(kGSettingsFileName, QStringList() + << "get" << kUnityPanel << kUnitySystrayWhitelist); +} + +void UbuntuUnityHack::GetError() { + QProcess* get = qobject_cast(sender()); + if (!get) { + return; + } + + get->deleteLater(); +} + +void UbuntuUnityHack::GetFinished(int exit_code) { + QProcess* get = qobject_cast(sender()); + if (!get) { + return; + } + + get->deleteLater(); + + if (exit_code != 0) { + // Probably not running in Unity. + return; + } + + QByteArray whitelist = get->readAllStandardOutput(); + + qLog(Debug) << "Unity whitelist is" << whitelist; + + int index = whitelist.lastIndexOf(']'); + if (index == -1 || whitelist.contains("'clementine'")) { + return; + } + + whitelist = whitelist.left(index) + QString(", 'clementine'").toUtf8() + + whitelist.mid(index); + + qLog(Debug) << "Setting unity whitelist to" << whitelist; + + QProcess* set = new QProcess(this); + connect(set, SIGNAL(finished(int)), set, SLOT(deleteLater())); + set->start(kGSettingsFileName, QStringList() + << "set" << kUnityPanel << kUnitySystrayWhitelist << whitelist); + + qLog(Info) << "Clementine has added itself to the Unity system tray" << + "whitelist, but this won't take effect until the next time" << + "you log out and log back in."; +} diff --git a/src/core/ubuntuunityhack.h b/src/core/ubuntuunityhack.h new file mode 100644 index 000000000..5ce8077a1 --- /dev/null +++ b/src/core/ubuntuunityhack.h @@ -0,0 +1,40 @@ +/* This file is part of Clementine. + Copyright 2010, David Sansome + + Clementine 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 + (at your option) any later version. + + Clementine 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 Clementine. If not, see . +*/ + +#ifndef UBUNTUUNITYHACK_H +#define UBUNTUUNITYHACK_H + +#include + +class QProcess; + +class UbuntuUnityHack : public QObject { + Q_OBJECT +public: + UbuntuUnityHack(QObject* parent = NULL); + +private slots: + void GetFinished(int exit_code); + void GetError(); + +private: + static const char* kGSettingsFileName; + static const char* kUnityPanel; + static const char* kUnitySystrayWhitelist; +}; + +#endif // UBUNTUUNITYHACK_H diff --git a/src/main.cpp b/src/main.cpp index 091e3c8e3..92b89d9cf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,7 @@ #include "core/potranslator.h" #include "core/song.h" #include "core/taskmanager.h" +#include "core/ubuntuunityhack.h" #include "core/utilities.h" #include "covers/albumcoverfetcher.h" #include "covers/artloader.h" @@ -377,6 +378,11 @@ int main(int argc, char *argv[]) { // later CoverProviders::instance(); + // In 11.04 Ubuntu decided that the system tray should be reserved for certain + // whitelisted applications. Clementine will override this setting and insert + // itself into the list of whitelisted apps. + UbuntuUnityHack hack; + // Create the tray icon and OSD scoped_ptr tray_icon(SystemTrayIcon::CreateSystemTrayIcon()); OSD osd(tray_icon.get());