~ubuntu-branches/ubuntu/lucid/konversation/lucid-updates

« back to all changes in this revision

Viewing changes to src/awaymanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2010-02-11 14:52:14 UTC
  • mto: This revision was merged to the branch mainline in revision 43.
  • Revision ID: james.westby@ubuntu.com-20100211145214-xsbd3hmrnu5fmb8f
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
*/
7
7
 
8
8
/*
9
 
  Copyright (C) 1999 Martin R. Jones <mjones@kde.org>
 
9
  Copyright (c) 1999 Martin R. Jones <mjones@kde.org>
10
10
  Copyright (C) 2008 Eike Hein <hein@kde.org>
11
 
  Copyright (C) 2010 Martin Blumenstingl <darklight.xdarklight@googlemail.com>
12
11
*/
13
12
 
14
13
#include "awaymanager.h"
64
63
    bool useMit;
65
64
};
66
65
 
67
 
AwayManager::AwayManager(QObject* parent) : AbstractAwayManager(parent)
 
66
AwayManager::AwayManager(QObject* parent) : QObject(parent)
68
67
{
69
68
    int dummy = 0;
70
69
    dummy = dummy;
76
75
    d->useXidle = false;
77
76
    d->useMit = false;
78
77
 
 
78
    m_connectionManager = static_cast<Application*>(kapp)->getConnectionManager();
 
79
 
79
80
#ifdef HasXHeaders
80
81
    Display* display = QX11Info::display();
81
82
    d->root = DefaultRootWindow(display);
97
98
    m_activityTimer->setObjectName("AwayTimer");
98
99
    connect(m_activityTimer, SIGNAL(timeout()), this, SLOT(checkActivity()));
99
100
 
100
 
    // Initially reset the idle status.
101
101
    resetIdle();
102
102
}
103
103
 
106
106
    delete d;
107
107
}
108
108
 
109
 
void AwayManager::simulateUserActivity()
110
 
{
111
 
    resetIdle();
112
 
}
113
 
 
114
109
void AwayManager::resetIdle()
115
110
{
116
 
    // Set the time of the idleTimer to the current time.
117
111
    m_idleTime.start();
118
112
}
119
113
 
120
 
int AwayManager::idleTime()
121
 
{
122
 
    // Calculate the idle time in milliseconds.
123
 
    return m_idleTime.elapsed();
124
 
}
125
 
 
126
 
void AwayManager::setManagedIdentitiesAway()
127
 
{
128
 
    // Used to skip X-based activity checking for one round, to avoid jumping
129
 
    // on residual mouse activity after manual screensaver activation.
130
 
    d->mouseX = -1;
131
 
    
132
 
    // Call the base implementation.
133
 
    AbstractAwayManager::setManagedIdentitiesAway();
134
 
}
135
 
 
136
 
void AwayManager::identitiesOnAutoAwayChanged()
137
 
{
138
 
    if (m_idetitiesWithIdleTimesOnAutoAway.count() > 0)
 
114
void AwayManager::identitiesChanged()
 
115
{
 
116
    QList<int> newIdentityList;
 
117
 
 
118
    const QList<Server*> serverList = m_connectionManager->getServerList();
 
119
 
 
120
    foreach (Server* server, serverList)
 
121
    {
 
122
        IdentityPtr identity = server->getIdentity();
 
123
 
 
124
        if (identity && identity->getAutomaticAway() && server->isConnected())
 
125
            newIdentityList.append(identity->id());
 
126
    }
 
127
 
 
128
    m_identitiesOnAutoAway = newIdentityList;
 
129
 
 
130
    toggleTimer();
 
131
}
 
132
 
 
133
void AwayManager::identityOnline(int identityId)
 
134
{
 
135
    IdentityPtr identity = Preferences::identityById(identityId);
 
136
 
 
137
    if (identity && identity->getAutomaticAway() &&
 
138
        !m_identitiesOnAutoAway.contains(identityId))
 
139
    {
 
140
        m_identitiesOnAutoAway.append(identityId);
 
141
 
 
142
        toggleTimer();
 
143
    }
 
144
}
 
145
 
 
146
void AwayManager::identityOffline(int identityId)
 
147
{
 
148
    if (m_identitiesOnAutoAway.removeOne(identityId))
 
149
    {
 
150
        toggleTimer();
 
151
    }
 
152
}
 
153
 
 
154
void AwayManager::toggleTimer()
 
155
{
 
156
    if (m_identitiesOnAutoAway.count() > 0)
139
157
    {
140
158
        if (!m_activityTimer->isActive())
141
159
            m_activityTimer->start(Preferences::self()->autoAwayPollInterval() * 1000);
144
162
        m_activityTimer->stop();
145
163
}
146
164
 
147
 
void AwayManager::identityOnAutoAwayWentOnline(int identityId)
148
 
{
149
 
    Q_UNUSED(identityId);
150
 
 
151
 
    identitiesOnAutoAwayChanged();
152
 
}
153
 
 
154
 
void AwayManager::identityOnAutoAwayWentOffline(int identityId)
155
 
{
156
 
    Q_UNUSED(identityId);
157
 
 
158
 
    identitiesOnAutoAwayChanged();
159
 
}
160
 
 
161
165
void AwayManager::checkActivity()
162
166
{
163
167
    // Allow the event loop to be called, to avoid deadlock.
170
174
    rentrencyProtection = false;
171
175
 
172
176
    if (!isBlanked.isValid() || !isBlanked.value())
173
 
    {
174
 
        // If there was activity we un-away all identities.
175
 
        // Otherwise we set all identities to away (if they
176
 
        // were idle long enough).
177
 
        if (Xactivity())
178
 
            setManagedIdentitiesUnaway();
179
 
        else
180
 
            implementIdleAutoAway();
181
 
    }
 
177
         implementIdleAutoAway(Xactivity());
182
178
}
183
179
 
184
180
bool AwayManager::Xactivity()
245
241
    return activity;
246
242
}
247
243
 
248
 
void AwayManager::implementIdleAutoAway()
249
 
{
250
 
    QHash<int, int>::ConstIterator it;
251
 
 
252
 
    for (it = m_idetitiesWithIdleTimesOnAutoAway.constBegin(); it != m_idetitiesWithIdleTimesOnAutoAway.constEnd(); ++it)
253
 
    {
254
 
        // Check if the auto-away timeout (which the user has configured for the given identity)
255
 
        // has already elapsed - if it has we mark the identity as away.
256
 
        if (idleTime() >= it.value())
257
 
            implementManagedAway(it.key());
258
 
    }
259
 
}
260
 
 
261
 
void AwayManager::implementManagedUnaway(const QList<int>& identityList)
262
 
{
263
 
    // Call the base implementation (which does all workflow logic).
264
 
    AbstractAwayManager::implementManagedUnaway(identityList);
265
 
 
266
 
    // Then reset the idle status as the user is not idle anymore.
267
 
    resetIdle();
 
244
void AwayManager::implementIdleAutoAway(bool activity)
 
245
{
 
246
    if (activity)
 
247
    {
 
248
        resetIdle();
 
249
 
 
250
        const QList<Server*> serverList = m_connectionManager->getServerList();
 
251
 
 
252
        foreach (Server* server, serverList)
 
253
        {
 
254
            IdentityPtr identity = server->getIdentity();
 
255
 
 
256
            if (m_identitiesOnAutoAway.contains(identity->id()) && identity->getAutomaticUnaway()
 
257
                && server->isConnected() && server->isAway())
 
258
            {
 
259
                server->requestUnaway();
 
260
            }
 
261
        }
 
262
    }
 
263
    else
 
264
    {
 
265
        long int idleTime = m_idleTime.elapsed() / 1000;
 
266
 
 
267
        QList<int> identitiesIdleTimeExceeded;
 
268
        QList<int>::ConstIterator it;
 
269
 
 
270
        for (it = m_identitiesOnAutoAway.constBegin(); it != m_identitiesOnAutoAway.constEnd(); ++it)
 
271
        {
 
272
            if (idleTime >= Preferences::identityById((*it))->getAwayInactivity() * 60)
 
273
                identitiesIdleTimeExceeded.append((*it));
 
274
        }
 
275
 
 
276
        const QList<Server*> serverList = m_connectionManager->getServerList();
 
277
 
 
278
        foreach (Server* server, serverList)
 
279
        {
 
280
            int identityId = server->getIdentity()->id();
 
281
 
 
282
            if (identitiesIdleTimeExceeded.contains(identityId) && server->isConnected() && !server->isAway())
 
283
                server->requestAway();
 
284
        }
 
285
    }
 
286
}
 
287
 
 
288
void AwayManager::setManagedIdentitiesAway()
 
289
{
 
290
    // Used to skip X-based activity checking for one round, to avoid jumping
 
291
    // on residual mouse activity after manual screensaver activation.
 
292
    d->mouseX = -1;
 
293
 
 
294
    const QList<Server*> serverList = m_connectionManager->getServerList();
 
295
 
 
296
    foreach (Server* server, serverList)
 
297
    {
 
298
        if (m_identitiesOnAutoAway.contains(server->getIdentity()->id()) && server->isConnected() && !server->isAway())
 
299
            server->requestAway();
 
300
    }
 
301
}
 
302
 
 
303
void AwayManager::setManagedIdentitiesUnaway()
 
304
{
 
305
    const QList<Server*> serverList = m_connectionManager->getServerList();
 
306
 
 
307
    foreach (Server* server, serverList)
 
308
    {
 
309
        IdentityPtr identity = server->getIdentity();
 
310
 
 
311
        if (m_identitiesOnAutoAway.contains(identity->id()) && identity->getAutomaticUnaway()
 
312
            && server->isConnected() && server->isAway())
 
313
        {
 
314
            server->requestUnaway();
 
315
        }
 
316
    }
 
317
}
 
318
 
 
319
void AwayManager::requestAllAway(const QString& reason)
 
320
{
 
321
    const QList<Server*> serverList = m_connectionManager->getServerList();
 
322
 
 
323
    foreach (Server* server, serverList)
 
324
        if (server->isConnected()) server->requestAway(reason);
 
325
}
 
326
 
 
327
void AwayManager::requestAllUnaway()
 
328
{
 
329
    const QList<Server*> serverList = m_connectionManager->getServerList();
 
330
 
 
331
    foreach (Server* server, serverList)
 
332
        if (server->isConnected() && server->isAway()) server->requestUnaway();
 
333
}
 
334
 
 
335
void AwayManager::toggleGlobalAway(bool away)
 
336
{
 
337
    if (away)
 
338
        requestAllAway();
 
339
    else
 
340
        requestAllUnaway();
 
341
}
 
342
 
 
343
void AwayManager::updateGlobalAwayAction(bool away)
 
344
{
 
345
    // FIXME: For now, our only triggers for resetting the idle time
 
346
    // are mouse movement and the screensaver getting disabled. This
 
347
    // means that typing '/unaway' or '/back' does not reset the idle
 
348
    // time and won't prevent AwayManager from setting a connection
 
349
    // away again shortly after when its identity's maximum auto-away
 
350
    // idle time, counted from the last mouse movement or screensaver
 
351
    // deactivation rather than the actual last user activity (the key
 
352
    // presses), has been exceeded. We work around this here by reset-
 
353
    // ting the idle time whenever any connection changes its state to
 
354
    // unaway in response to the server until we find a better solu-
 
355
    // tion (i.e. a reliable way to let keyboard activity in the sys-
 
356
    // tem reset the idle time).
 
357
    if(!away) resetIdle();
 
358
 
 
359
    Application* konvApp = static_cast<Application*>(kapp);
 
360
    KToggleAction* awayAction = qobject_cast<KToggleAction*>(konvApp->getMainWindow()->actionCollection()->action("toggle_away"));
 
361
 
 
362
    if (!awayAction) return;
 
363
 
 
364
    if (away)
 
365
    {
 
366
        const QList<Server*> serverList = m_connectionManager->getServerList();
 
367
        int awayCount = 0;
 
368
 
 
369
        foreach (Server* server, serverList)
 
370
        {
 
371
            if (server->isAway())
 
372
                awayCount++;
 
373
        }
 
374
 
 
375
        if (awayCount == serverList.count())
 
376
        {
 
377
            awayAction->setChecked(true);
 
378
            awayAction->setIcon(KIcon("im-user-away"));
 
379
        }
 
380
    }
 
381
    else
 
382
    {
 
383
        awayAction->setChecked(false);
 
384
        awayAction->setIcon(KIcon("im-user"));
 
385
    }
268
386
}
269
387
 
270
388
#include "awaymanager.moc"