File: status-handler.cpp

package info (click to toggle)
ktp-kded-integration-module 17.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 956 kB
  • sloc: cpp: 2,492; makefile: 5; sh: 3
file content (209 lines) | stat: -rw-r--r-- 7,273 bytes parent folder | download | duplicates (2)
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
199
200
201
202
203
204
205
206
207
208
209
/*
    Copyright (C) 2014  David Edmundson <kde@davidedmundson.co.uk>
    Copyright (C) 2011  Martin Klapetek <martin.klapetek@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "status-handler.h"

#include "autoconnect.h"
#include "autoaway.h"
#include "screensaveraway.h"
#include "telepathy-mpris.h"

#include <KTp/global-presence.h>

#include <TelepathyQt/PendingReady>
#include <TelepathyQt/AccountManager>

StatusHandler::StatusHandler(QObject* parent):
    m_autoConnect(0),
    m_globalPresence(0)
{
    connect(KTp::accountManager()->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(onAccountManagerReady(Tp::PendingOperation*)));

    QDBusConnection::sessionBus().connect(QString(), QLatin1String("/Telepathy"), QLatin1String("org.kde.Telepathy"),
                                          QLatin1String("settingsChange"), this, SIGNAL(settingsChanged()));
}

StatusHandler::~StatusHandler()
{

}

void StatusHandler::onAccountManagerReady(Tp::PendingOperation *op)
{
    if (op->isError()) {
        return;
    }

    m_globalPresence = new KTp::GlobalPresence(this);
    m_autoConnect = new AutoConnect(this);

    m_globalPresence->setAccountManager(KTp::accountManager());
    connect(m_globalPresence, SIGNAL(requestedPresenceChanged(KTp::Presence)),
            this, SLOT(onRequestedPresenceChanged(KTp::Presence)));

    AutoAway *autoAway = new AutoAway(m_globalPresence, this);
    connect(autoAway, SIGNAL(activate(bool)),
            this, SLOT(onPluginActivated(bool)));

    connect(this, SIGNAL(settingsChanged()),
            autoAway, SLOT(reloadConfig()));

    ScreenSaverAway *screenSaverAway = new ScreenSaverAway(m_globalPresence, this);
    connect(screenSaverAway, SIGNAL(activate(bool)),
            this, SLOT(onPluginActivated(bool)));

    connect(this, SIGNAL(settingsChanged()),
            screenSaverAway, SLOT(reloadConfig()));

    TelepathyMPRIS *mpris = new TelepathyMPRIS(m_globalPresence, this);
    connect(mpris, SIGNAL(activate(bool)),
            this, SLOT(onPluginActivated(bool)));

    connect(this, SIGNAL(settingsChanged()),
            mpris, SLOT(reloadConfig()));

    //earlier in list = higher priority
    m_pluginStack << autoAway << screenSaverAway;

    //status message plugins are second to user-set status messages and presences
    m_statusMessagePluginStack << mpris;


    m_lastUserPresence = m_globalPresence->requestedPresence();
}

void StatusHandler::onRequestedPresenceChanged(const KTp::Presence &presence)
{
    // the difference between user requested offline and network related offline is the connectionStatus is connected or not
    // offline caused by network offline shold not be recorded as user requested.
    if (presence.type() == Tp::ConnectionPresenceTypeOffline
     && m_globalPresence->connectionStatus() != Tp::ConnectionStatusConnected) {
        return;
    }

    //if it's changed to what we set it to. Ignore it.
    if (presence == presenceThrottle()) {
        return;
    }

    //user is manually setting the presence.
    m_lastUserPresence = presence;

    //save presence (needed for autoconnect)
    m_autoConnect->savePresence(presence);

    // keep status messages current. User-requested status message changes (or plugin-requested status and message) won't return
    // to automatic status message until the next status message plugin signals.
    if (activeStatusMessagePlugin()) {
        if (!presence.statusMessage().isEmpty()) {
            return;
        }
        if (presence != presenceThrottle()) {
            setPresence(presenceThrottle());
        }
    }
}

void StatusHandler::onPluginActivated(bool active)
{
    Q_UNUSED(active);
    // cut down on unneccessary status updates by only updating when the current global presence isn't the same as the filtered
    // presence. This also helps with misbehaving plugins, in particular the mpris2 plugin ticks and outputs every second.
    if (m_globalPresence->currentPresence() != presenceThrottle()) {
        setPresence(presenceThrottle());
    }
}

const QString StatusHandler::statusMessageStack()
{
    QString expectedStatusMessage = m_lastUserPresence.statusMessage();
    if (activeStatusMessagePlugin() && m_lastUserPresence.statusMessage().isEmpty()) {
        expectedStatusMessage = currentPluginStatusMessage();
    }
    if (activePlugin() && !currentPluginPresence().statusMessage().isEmpty()) {
        expectedStatusMessage = currentPluginPresence().statusMessage();
    }
    return expectedStatusMessage;
}

KTp::Presence StatusHandler::presenceThrottle()
{
    KTp::Presence expectedPresence = m_lastUserPresence;
    if (activePlugin()) {
        expectedPresence = currentPluginPresence();
    }
    expectedPresence.setStatusMessage(statusMessageStack());
    return expectedPresence;
}

void StatusHandler::setPresence(const KTp::Presence &presence)
{
    Q_FOREACH(const Tp::AccountPtr &account, KTp::accountManager()->allAccounts()) {
        if (account->requestedPresence() != Tp::Presence::offline()) {
            account->setRequestedPresence(presence);
        }
    }
}

KTp::Presence StatusHandler::currentPluginPresence() const
{
    KTp::Presence requestedPresence;
    //search plugins in priority order. If a plugin is active, return the state it thinks it should be in.
    Q_FOREACH(TelepathyKDEDModulePlugin* plugin, m_pluginStack) {
        if (plugin->isActive() && plugin->isEnabled()) {
            requestedPresence = plugin->requestedPresence();
        }
    }
    return requestedPresence;
}

QString StatusHandler::currentPluginStatusMessage()
{
    QString requestedStatusMessage;
    //search plugins in priority order. If a plugin is active, return the message it thinks it should.
    Q_FOREACH(TelepathyKDEDModulePlugin* plugin, m_statusMessagePluginStack) {
    if (plugin->isActive() && plugin->isEnabled()) {
        requestedStatusMessage = plugin->requestedStatusMessage();
        }
    }
    return requestedStatusMessage;
}

bool StatusHandler::activePlugin()
{
    bool activePlugin = false;
    Q_FOREACH(TelepathyKDEDModulePlugin* plugin, m_pluginStack) {
        if (plugin->isActive()) {
            activePlugin = true;
        }
    }
    return activePlugin;
}

bool StatusHandler::activeStatusMessagePlugin()
{
    bool activeStatusMessagePlugin = false;
    Q_FOREACH(TelepathyKDEDModulePlugin* plugin, m_statusMessagePluginStack) {
        if (plugin->isActive()) {
           activeStatusMessagePlugin = true;
        }
    }
    return activeStatusMessagePlugin;
}