PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

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