/src/ubuntuunityhack.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 83 lines · 48 code · 16 blank · 19 comment · 7 complexity · 05dfc2c47cdf3693901728adacf5bff8 MD5 · raw file

  1. /* This file is part of Clementine.
  2. Copyright 2010, David Sansome <me@davidsansome.com>
  3. Clementine is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. Clementine is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Clementine. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "UbuntuUnityHack.h"
  15. #include "utils/Logger.h"
  16. #include <QProcess>
  17. const char* UbuntuUnityHack::kGSettingsFileName = "gsettings";
  18. const char* UbuntuUnityHack::kUnityPanel = "com.canonical.Unity.Panel";
  19. const char* UbuntuUnityHack::kUnitySystrayWhitelist = "systray-whitelist";
  20. UbuntuUnityHack::UbuntuUnityHack(QObject* parent)
  21. : QObject(parent)
  22. {
  23. // Get the systray whitelist from gsettings. If this fails we're probably
  24. // not running on a system with unity
  25. QProcess* get = new QProcess(this);
  26. connect(get, SIGNAL(finished(int)), SLOT(GetFinished(int)));
  27. connect(get, SIGNAL(error(QProcess::ProcessError)), SLOT(GetError()));
  28. get->start(kGSettingsFileName, QStringList()
  29. << "get" << kUnityPanel << kUnitySystrayWhitelist);
  30. }
  31. void UbuntuUnityHack::GetError() {
  32. QProcess* get = qobject_cast<QProcess*>(sender());
  33. if (!get) {
  34. return;
  35. }
  36. get->deleteLater();
  37. }
  38. void UbuntuUnityHack::GetFinished(int exit_code) {
  39. QProcess* get = qobject_cast<QProcess*>(sender());
  40. if (!get) {
  41. return;
  42. }
  43. get->deleteLater();
  44. if (exit_code != 0) {
  45. // Probably not running in Unity.
  46. return;
  47. }
  48. QByteArray whitelist = get->readAllStandardOutput();
  49. tDebug() << "Unity whitelist is" << whitelist;
  50. int index = whitelist.lastIndexOf(']');
  51. if (index == -1 || whitelist.contains("'tomahawk'")) {
  52. return;
  53. }
  54. whitelist = whitelist.left(index) + QString(", 'tomahawk'").toUtf8() +
  55. whitelist.mid(index);
  56. tLog() << "Setting unity whitelist to" << whitelist;
  57. QProcess* set = new QProcess(this);
  58. connect(set, SIGNAL(finished(int)), set, SLOT(deleteLater()));
  59. set->start(kGSettingsFileName, QStringList()
  60. << "set" << kUnityPanel << kUnitySystrayWhitelist << whitelist);
  61. tLog() << "Tomahawk has added itself to the Unity system tray" <<
  62. "whitelist, but this won't take effect until the next time" <<
  63. "you log out and log back in.";
  64. }