~dyams/unity-2d/remove-glow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * This file is part of unity-2d
 *
 * Copyright 2011 Canonical Ltd.
 *
 * Authors:
 * - Aurélien Gâteau <aurelien.gateau@canonical.com>
 *
 * 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; version 3.
 *
 * 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/>.
 */
// Self
#include "dashclient.h"

// Local
#include "config.h"

// libunity-2d
#include <debug_p.h>

// Qt
#include <QApplication>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusInterface>
#include <QDBusServiceWatcher>
#include <QDesktopWidget>
#include <QRect>

static const char* DASH_DBUS_SERVICE = "com.canonical.Unity2d.Dash";
static const char* DASH_DBUS_PATH = "/Dash";
static const char* DASH_DBUS_INTERFACE = "com.canonical.Unity2d.Dash";

static const int DASH_MIN_SCREEN_WIDTH = 1280;
static const int DASH_MIN_SCREEN_HEIGHT = 1084;

DashClient::DashClient(QObject* parent)
: QObject(parent)
, m_dashDbusIface(0)
, m_dashActive(false)
, m_alwaysFullScreen(false)
{
    /* Check if the dash is already up and running by asking the bus instead of
       trying to create an instance of the interface. Creating an instance would
       cause D-Bus to activate the dash and we don’t want this to happen, the
       dash should be started on demand only. */
    QDBusConnectionInterface* sessionBusIFace = QDBusConnection::sessionBus().interface();
    QDBusReply<bool> reply = sessionBusIFace->isServiceRegistered(DASH_DBUS_SERVICE);
    if (reply.isValid() && reply.value()) {
        connectToDash();
    } else {
        /* The dash is not running: monitor its registration on the bus so we
           can connect to it when it comes up. */
        QDBusServiceWatcher* watcher = new QDBusServiceWatcher(DASH_DBUS_SERVICE,
                                                               QDBusConnection::sessionBus(),
                                                               QDBusServiceWatcher::WatchForRegistration,
                                                               this);
        connect(watcher, SIGNAL(serviceRegistered(QString)), SLOT(connectToDash()));
    }

    connect(QApplication::desktop(), SIGNAL(resized(int)), SLOT(updateAlwaysFullScreen()));

    // FIXME: we need to use a queued connection here otherwise QConf will deadlock for some reason
    // when we read any property from the slot (which we need to do). We need to check why this
    // happens and report a bug to dconf-qt to get it fixed.
    connect(&unity2dConfiguration(), SIGNAL(formFactorChanged(QString)),
                                     SLOT(updateAlwaysFullScreen()), Qt::QueuedConnection);
    updateAlwaysFullScreen();
}

void DashClient::connectToDash()
{
    if (m_dashDbusIface) {
        return;
    }

    m_dashDbusIface = new QDBusInterface(DASH_DBUS_SERVICE, DASH_DBUS_PATH, DASH_DBUS_INTERFACE,
                                         QDBusConnection::sessionBus(), this);
    connect(m_dashDbusIface, SIGNAL(activeChanged(bool)),
            SLOT(slotDashActiveChanged(bool)));
    connect(m_dashDbusIface, SIGNAL(activeLensChanged(const QString&)),
            SLOT(slotDashActiveLensChanged(const QString&)));

    QVariant value = m_dashDbusIface->property("active");
    if (value.isValid()) {
        m_dashActive = value.toBool();
    } else {
        UQ_WARNING << "Fetching Dash.active property failed";
    }
    value = m_dashDbusIface->property("activeLens");
    if (value.isValid()) {
        m_dashActiveLens = value.toString();
    } else {
        UQ_WARNING << "Fetching Dash.activeLens property failed";
    }

    updateActivePage();
}

DashClient* DashClient::instance()
{
    static DashClient* client = new DashClient(qApp);
    return client;
}

void DashClient::slotDashActiveChanged(bool value)
{
    if (m_dashActive != value) {
        m_dashActive = value;
        updateActivePage();
    }
}

void DashClient::slotDashActiveLensChanged(const QString& lens)
{
    if (m_dashActiveLens != lens) {
        m_dashActiveLens = lens;
        updateActivePage();
    }
}

QString DashClient::activePage() const
{
    return m_activePage;
}

void DashClient::setActivePage(const QString& page, const QString& lensId)
{
    if (m_activePage == page) {
        return;
    }
    if (page.isEmpty()) {
        // Use m_dashDbusIface to close the dash, but only if it is running
        if (m_dashDbusIface) {
            m_dashDbusIface->setProperty("active", false);
        }
        return;
    }
    // Use a separate QDBusInterface so that the dash is started if it is not
    // already running
    QDBusInterface iface(DASH_DBUS_SERVICE, DASH_DBUS_PATH, DASH_DBUS_INTERFACE);
    if (page == "home") {
        iface.asyncCall("activateHome");
    } else {
        iface.asyncCall("activateLens", lensId);
    }
}

void DashClient::updateActivePage()
{
    QString activePage;
    if (m_dashActive) {
        activePage = m_dashActiveLens.isEmpty() ? "home" : m_dashActiveLens;
    }

    if (m_activePage != activePage) {
        m_activePage = activePage;
        activePageChanged(m_activePage);
    }
}

QSize DashClient::minimumSizeForDesktop()
{
    return QSize(DASH_MIN_SCREEN_WIDTH, DASH_MIN_SCREEN_HEIGHT);
}

void DashClient::updateAlwaysFullScreen()
{
    bool alwaysFullScreen;
    if (unity2dConfiguration().property("formFactor").toString() != "desktop") {
        alwaysFullScreen = true;
    } else {
        QRect rect = QApplication::desktop()->screenGeometry(QPoint());
        QSize minSize = minimumSizeForDesktop();
        alwaysFullScreen = rect.width() < minSize.width() && rect.height() < minSize.height();
    }

    if (m_alwaysFullScreen != alwaysFullScreen) {
        m_alwaysFullScreen = alwaysFullScreen;
        Q_EMIT alwaysFullScreenChanged();
    }
}

bool DashClient::alwaysFullScreen() const
{
    return m_alwaysFullScreen;
}

#include "dashclient.moc"