~ubuntu-branches/ubuntu/precise/kactivities/precise-proposed

« back to all changes in this revision

Viewing changes to service/ActivityManager.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-13 11:53:27 UTC
  • Revision ID: package-import@ubuntu.com-20111213115327-vqhdel92qx65us8y
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2010 Ivan Cukic <ivan.cukic(at)kde.org>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU General Public License version 2,
 
6
 *   or (at your option) any later version, as published by the Free
 
7
 *   Software Foundation
 
8
 *
 
9
 *   This program 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
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "ActivityManager.h"
 
21
#include "ActivityManager_p.h"
 
22
 
 
23
#include <QUuid>
 
24
#include <QDBusConnection>
 
25
 
 
26
#include <KConfig>
 
27
#include <KConfigGroup>
 
28
#include <KCrash>
 
29
#include <KUrl>
 
30
#include <KDebug>
 
31
 
 
32
#include <KWindowSystem>
 
33
 
 
34
#ifdef HAVE_NEPOMUK
 
35
    #include <Nepomuk/ResourceManager>
 
36
    #include <Nepomuk/Resource>
 
37
    #include <Nepomuk/Variant>
 
38
    #include "nie.h"
 
39
    #include "kext.h"
 
40
#endif
 
41
 
 
42
#include "activitymanageradaptor.h"
 
43
#include "EventProcessor.h"
 
44
 
 
45
#include "config-features.h"
 
46
 
 
47
#ifdef HAVE_NEPOMUK
 
48
    #define NEPOMUK_RUNNING d->nepomukInitialized()
 
49
 
 
50
    using namespace Nepomuk::Vocabulary;
 
51
#else
 
52
    #define NEPOMUK_RUNNING false
 
53
#endif
 
54
 
 
55
// #define ACTIVITIES_PROTOCOL "activities://"
 
56
 
 
57
// copied from kdelibs\kdeui\notifications\kstatusnotifieritemdbus_p.cpp
 
58
// if there is a common place for such definitions please move
 
59
#ifdef Q_OS_WIN64
 
60
__inline int toInt(WId wid)
 
61
{
 
62
    return (int)((__int64)wid);
 
63
}
 
64
 
 
65
#else
 
66
__inline int toInt(WId wid)
 
67
{
 
68
    return (int)wid;
 
69
}
 
70
#endif
 
71
 
 
72
// Private
 
73
 
 
74
ActivityManagerPrivate::ActivityManagerPrivate(ActivityManager * parent,
 
75
            QHash < WId, SharedInfo::WindowData > & _windows,
 
76
            QHash < KUrl, SharedInfo::ResourceData > & _resources
 
77
        )
 
78
    : config("activitymanagerrc"),
 
79
      windows(_windows),
 
80
      resources(_resources),
 
81
#ifdef HAVE_NEPOMUK
 
82
      m_nepomukInitCalled(false),
 
83
#endif
 
84
      q(parent),
 
85
      ksmserverInterface(0)
 
86
{
 
87
    kDebug() << "\n\n-------------------------------------------------------";
 
88
    kDebug() << "Starting the KDE Activity Manager daemon" << QDateTime::currentDateTime();
 
89
    kDebug() << "-------------------------------------------------------";
 
90
 
 
91
    // Initializing config
 
92
    connect(&configSyncTimer, SIGNAL(timeout()),
 
93
             this, SLOT(configSync()));
 
94
 
 
95
    configSyncTimer.setSingleShot(true);
 
96
    configSyncTimer.setInterval(2 * 60 * 1000);
 
97
 
 
98
    // kDebug() << "reading activities:";
 
99
    foreach (const QString & activity, activitiesConfig().keyList()) {
 
100
        // kDebug() << activity;
 
101
        activities[activity] = ActivityManager::Stopped;
 
102
    }
 
103
 
 
104
    foreach (const QString & activity, mainConfig().readEntry("runningActivities", activities.keys())) {
 
105
        // kDebug() << "setting" << activity << "as" << "running";
 
106
        if (activities.contains(activity)) {
 
107
            activities[activity] = ActivityManager::Running;
 
108
        }
 
109
    }
 
110
 
 
111
    syncActivitiesWithNepomuk();
 
112
 
 
113
    currentActivity = mainConfig().readEntry("currentActivity", QString());
 
114
    // kDebug() << "currentActivity is" << currentActivity;
 
115
    SharedInfo::self()->setCurrentActivity(currentActivity);
 
116
 
 
117
    connect(KWindowSystem::self(), SIGNAL(windowRemoved(WId)),
 
118
            this, SLOT(windowClosed(WId)));
 
119
    connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)),
 
120
            this, SLOT(activeWindowChanged(WId)));
 
121
 
 
122
    //listen to ksmserver for starting/stopping
 
123
    QDBusServiceWatcher *watcher = new QDBusServiceWatcher("org.kde.ksmserver",
 
124
                                                           QDBusConnection::sessionBus(),
 
125
                                                           QDBusServiceWatcher::WatchForRegistration);
 
126
    connect(watcher, SIGNAL(serviceRegistered(QString)), this, SLOT(sessionServiceRegistered()));
 
127
    sessionServiceRegistered();
 
128
}
 
129
 
 
130
void ActivityManagerPrivate::sessionServiceRegistered()
 
131
{
 
132
    delete ksmserverInterface;
 
133
    ksmserverInterface = new QDBusInterface("org.kde.ksmserver", "/KSMServer", "org.kde.KSMServerInterface");
 
134
    if (ksmserverInterface->isValid()) {
 
135
        ksmserverInterface->setParent(this);
 
136
        connect(ksmserverInterface, SIGNAL(subSessionOpened()), this, SLOT(startCompleted()));
 
137
        connect(ksmserverInterface, SIGNAL(subSessionClosed()), this, SLOT(stopCompleted()));
 
138
        connect(ksmserverInterface, SIGNAL(subSessionCloseCanceled()), this, SLOT(stopCancelled())); //spelling fail :)
 
139
    } else {
 
140
        delete ksmserverInterface;
 
141
        ksmserverInterface = 0;
 
142
        kDebug() << "couldn't connect to ksmserver! session stuff won't work";
 
143
    }
 
144
}
 
145
 
 
146
ActivityManagerPrivate::~ActivityManagerPrivate()
 
147
{
 
148
    configSync();
 
149
}
 
150
 
 
151
void ActivityManagerPrivate::windowClosed(WId windowId)
 
152
{
 
153
    // kDebug() << "Window closed..." << windowId
 
154
    //          << "one of ours?" << windows.contains(windowId);
 
155
 
 
156
    if (!windows.contains(windowId)) {
 
157
        return;
 
158
    }
 
159
 
 
160
    foreach (const KUrl & uri, windows[windowId].resources) {
 
161
        q->RegisterResourceEvent(windows[windowId].application,
 
162
                toInt(windowId), uri.url(), Event::Closed, resources[uri].reason);
 
163
    }
 
164
}
 
165
 
 
166
void ActivityManagerPrivate::activeWindowChanged(WId windowId)
 
167
{
 
168
    Q_UNUSED(windowId)
 
169
    // kDebug() << "Window focussed..." << windowId
 
170
    //          << "one of ours?" << windows.contains(windowId);
 
171
 
 
172
}
 
173
 
 
174
void ActivityManagerPrivate::setActivityState(const QString & id, ActivityManager::State state)
 
175
{
 
176
    if (activities[id] == state) return;
 
177
 
 
178
    // kDebug() << "Set the state of" << id << "to" << state;
 
179
 
 
180
    /**
 
181
     * Treating 'Starting' as 'Running', and 'Stopping' as 'Stopped'
 
182
     * as far as the config file is concerned
 
183
     */
 
184
    bool configNeedsUpdating = ((activities[id] & 4) != (state & 4));
 
185
 
 
186
    activities[id] = state;
 
187
 
 
188
    switch (state) {
 
189
        case ActivityManager::Running:
 
190
            // kDebug() << "sending ActivityStarted signal";
 
191
            emit q->ActivityStarted(id);
 
192
            break;
 
193
 
 
194
        case ActivityManager::Stopped:
 
195
            // kDebug() << "sending ActivityStopped signal";
 
196
            emit q->ActivityStopped(id);
 
197
            break;
 
198
 
 
199
        default:
 
200
            break;
 
201
    }
 
202
 
 
203
    // kDebug() << "sending ActivityStateChanged signal";
 
204
    emit q->ActivityStateChanged(id, state);
 
205
 
 
206
    if (configNeedsUpdating) {
 
207
        mainConfig().writeEntry("runningActivities",
 
208
                activities.keys(ActivityManager::Running) +
 
209
                activities.keys(ActivityManager::Starting));
 
210
        scheduleConfigSync();
 
211
    }
 
212
}
 
213
 
 
214
KConfigGroup ActivityManagerPrivate::activitiesConfig()
 
215
{
 
216
    return KConfigGroup(&config, "activities");
 
217
}
 
218
 
 
219
KConfigGroup ActivityManagerPrivate::mainConfig()
 
220
{
 
221
    return KConfigGroup(&config, "main");
 
222
}
 
223
 
 
224
void ActivityManagerPrivate::ensureCurrentActivityIsRunning()
 
225
{
 
226
    QStringList runningActivities = q->ListActivities(ActivityManager::Running);
 
227
 
 
228
    if (!runningActivities.contains(currentActivity)) {
 
229
        if (runningActivities.size() > 0) {
 
230
            setCurrentActivity(runningActivities.first());
 
231
        } else {
 
232
            // kDebug() << "there are no running activities! eek!";
 
233
        }
 
234
    }
 
235
}
 
236
 
 
237
bool ActivityManagerPrivate::setCurrentActivity(const QString & id)
 
238
{
 
239
    kDebug() << "Changing rhe activity to:" << id;
 
240
    if (id.isEmpty()) {
 
241
        currentActivity.clear();
 
242
 
 
243
    } else {
 
244
        if (!activities.contains(id)) {
 
245
            return false;
 
246
        }
 
247
 
 
248
        // if (currentActivity != id) {
 
249
        //     kDebug() << "registering the events";
 
250
        //     // Closing the previous activity:
 
251
        //     if (!currentActivity.isEmpty()) {
 
252
        //         q->RegisterResourceEvent(
 
253
        //                 "kactivitymanagerd", 0,
 
254
        //                 "activities://" + currentActivity,
 
255
        //                 Event::Closed, Event::User
 
256
        //             );
 
257
        //     }
 
258
 
 
259
        //     q->RegisterResourceEvent(
 
260
        //             "kactivitymanagerd", 0,
 
261
        //             "activities://" + id,
 
262
        //             Event::Accessed, Event::User
 
263
        //         );
 
264
        //     q->RegisterResourceEvent(
 
265
        //             "kactivitymanagerd", 0,
 
266
        //             "activities://" + id,
 
267
        //             Event::Opened, Event::User
 
268
        //         );
 
269
        // }
 
270
 
 
271
        q->StartActivity(id);
 
272
 
 
273
        currentActivity = id;
 
274
        mainConfig().writeEntry("currentActivity", id);
 
275
 
 
276
        scheduleConfigSync();
 
277
    }
 
278
 
 
279
    // kDebug() << (void*) SharedInfo::self() << "Rankings << shared info";
 
280
    SharedInfo::self()->setCurrentActivity(id);
 
281
    emit q->CurrentActivityChanged(id);
 
282
    return true;
 
283
}
 
284
 
 
285
QString ActivityManagerPrivate::activityName(const QString & id)
 
286
{
 
287
    return activitiesConfig().readEntry(id, QString());
 
288
}
 
289
 
 
290
void ActivityManagerPrivate::scheduleConfigSync()
 
291
{
 
292
    if (!configSyncTimer.isActive()) {
 
293
        configSyncTimer.start();
 
294
    }
 
295
}
 
296
 
 
297
void ActivityManagerPrivate::configSync()
 
298
{
 
299
    configSyncTimer.stop();
 
300
    config.sync();
 
301
}
 
302
 
 
303
void ActivityManagerPrivate::syncActivitiesWithNepomuk()
 
304
{
 
305
#ifdef HAVE_NEPOMUK
 
306
    foreach (const QString & activityId, activities.keys()) {
 
307
        Nepomuk::Resource activityResource(activityId, KEXT::Activity());
 
308
 
 
309
        QString name = activitiesConfig().readEntry(activityId, QString());
 
310
 
 
311
        activityResource.setProperty(KEXT::activityIdentifier(), activityId);
 
312
 
 
313
        if (!name.isEmpty()) {
 
314
            activityResource.setLabel(name);
 
315
        }
 
316
    }
 
317
#endif // HAVE_NEPOMUK
 
318
}
 
319
 
 
320
#ifdef HAVE_NEPOMUK
 
321
 
 
322
Nepomuk::Resource ActivityManagerPrivate::activityResource(const QString & id)
 
323
{
 
324
    // kDebug() << "testing for nepomuk";
 
325
 
 
326
    if (nepomukInitialized()) {
 
327
        return Nepomuk::Resource(
 
328
                id, KEXT::Activity());
 
329
    } else {
 
330
        return Nepomuk::Resource();
 
331
    }
 
332
}
 
333
 
 
334
/* lazy init of nepomuk */
 
335
bool ActivityManagerPrivate::nepomukInitialized()
 
336
{
 
337
    if (m_nepomukInitCalled) return
 
338
        Nepomuk::ResourceManager::instance()->initialized();
 
339
 
 
340
    m_nepomukInitCalled = true;
 
341
 
 
342
    connect(Nepomuk::ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(backstoreAvailable()));
 
343
 
 
344
    return (Nepomuk::ResourceManager::instance()->init() == 0);
 
345
}
 
346
 
 
347
void ActivityManagerPrivate::backstoreAvailable()
 
348
{
 
349
    //emit q->BackstoreAvailable();
 
350
    //kick the icons, so that clients don't need to know that they depend on nepomuk
 
351
    for (QHash<QString, ActivityManager::State>::const_iterator i = activities.constBegin();
 
352
         i != activities.constEnd(); ++i) {
 
353
        emit q->ActivityChanged(i.key());
 
354
    }
 
355
}
 
356
 
 
357
#else // HAVE_NEPOMUK
 
358
 
 
359
void ActivityManagerPrivate::backstoreAvailable()
 
360
{
 
361
}
 
362
 
 
363
#endif // HAVE_NEPOMUK
 
364
 
 
365
// Main
 
366
 
 
367
ActivityManager::ActivityManager()
 
368
    : d(new ActivityManagerPrivate(this,
 
369
            SharedInfo::self()->m_windows,
 
370
            SharedInfo::self()->m_resources))
 
371
{
 
372
 
 
373
    QDBusConnection dbus = QDBusConnection::sessionBus();
 
374
    new ActivityManagerAdaptor(this);
 
375
    dbus.registerObject("/ActivityManager", this);
 
376
 
 
377
    // TODO: Sync activities in nepomuk with currently existing ones
 
378
    // but later, when we are sure nepomuk is running
 
379
 
 
380
    // ensureCurrentActivityIsRunning();
 
381
 
 
382
    KCrash::setFlags(KCrash::AutoRestart);
 
383
 
 
384
    EventProcessor::self();
 
385
 
 
386
    // kDebug() << "RegisterResourceEvent open" << d->currentActivity;
 
387
    // RegisterResourceEvent(
 
388
    //         "kactivitymanagerd", 0,
 
389
    //         "activities://" + d->currentActivity,
 
390
    //         Event::Accessed, Event::User
 
391
    //     );
 
392
    // RegisterResourceEvent(
 
393
    //         "kactivitymanagerd", 0,
 
394
    //         "activities://" + d->currentActivity,
 
395
    //         Event::Opened, Event::User
 
396
    //     );
 
397
 
 
398
}
 
399
 
 
400
ActivityManager::~ActivityManager()
 
401
{
 
402
    // kDebug() << "RegisterResourceEvent close" << d->currentActivity;
 
403
    // RegisterResourceEvent(
 
404
    //         "kactivitymanagerd", 0,
 
405
    //         "activities://" + d->currentActivity,
 
406
    //         Event::Closed, Event::User
 
407
    //     );
 
408
    delete d;
 
409
}
 
410
 
 
411
void ActivityManager::Start()
 
412
{
 
413
    // doing absolutely nothing
 
414
}
 
415
 
 
416
void ActivityManager::Stop()
 
417
{
 
418
    d->configSync();
 
419
    QCoreApplication::quit();
 
420
}
 
421
 
 
422
bool ActivityManager::IsBackstoreAvailable() const
 
423
{
 
424
    return NEPOMUK_RUNNING;
 
425
}
 
426
 
 
427
 
 
428
// workspace activities control
 
429
 
 
430
QString ActivityManager::CurrentActivity() const
 
431
{
 
432
    return d->currentActivity;
 
433
}
 
434
 
 
435
bool ActivityManager::SetCurrentActivity(const QString & id)
 
436
{
 
437
    // kDebug() << id;
 
438
 
 
439
    if (id.isEmpty()) {
 
440
        return false;
 
441
    }
 
442
 
 
443
    return d->setCurrentActivity(id);
 
444
}
 
445
 
 
446
QString ActivityManager::AddActivity(const QString & name)
 
447
{
 
448
    // kDebug() << name;
 
449
 
 
450
    QString id;
 
451
 
 
452
    // Ensuring a new Uuid. The loop should usually end after only
 
453
    // one iteration
 
454
    QStringList existingActivities = d->activities.keys();
 
455
    while (id.isEmpty() || existingActivities.contains(id)) {
 
456
        id = QUuid::createUuid();
 
457
        id.replace(QRegExp("[{}]"), QString());
 
458
    }
 
459
 
 
460
    d->setActivityState(id, Running);
 
461
 
 
462
    SetActivityName(id, name);
 
463
 
 
464
    emit ActivityAdded(id);
 
465
 
 
466
    d->configSync();
 
467
    return id;
 
468
}
 
469
 
 
470
void ActivityManager::RemoveActivity(const QString & id)
 
471
{
 
472
    // kDebug() << id;
 
473
 
 
474
    if (d->activities.size() < 2 ||
 
475
            !d->activities.contains(id)) {
 
476
        return;
 
477
    }
 
478
 
 
479
    // If the activity is running, stash it
 
480
    StopActivity(id);
 
481
 
 
482
    d->setActivityState(id, Invalid);
 
483
 
 
484
    // Removing the activity
 
485
    d->activities.remove(id);
 
486
    d->activitiesConfig().deleteEntry(id);
 
487
 
 
488
    // If the removed activity was the current one,
 
489
    // set another activity as current
 
490
    if (d->currentActivity == id) {
 
491
        d->ensureCurrentActivityIsRunning();
 
492
    }
 
493
 
 
494
    if (d->transitioningActivity == id) {
 
495
        //very unlikely, but perhaps not impossible
 
496
        //but it being deleted doesn't mean ksmserver is un-busy..
 
497
        //in fact, I'm not quite sure what would happen.... FIXME
 
498
        //so I'll just add some output to warn that it happened.
 
499
        // kDebug() << "deleting activity in transition. watch out!";
 
500
    }
 
501
 
 
502
    emit ActivityRemoved(id);
 
503
    d->configSync();
 
504
}
 
505
 
 
506
void ActivityManager::StartActivity(const QString & id)
 
507
{
 
508
    // kDebug() << id;
 
509
 
 
510
    if (!d->activities.contains(id) ||
 
511
            d->activities[id] != Stopped) {
 
512
        return;
 
513
    }
 
514
 
 
515
    if (!d->transitioningActivity.isEmpty()) {
 
516
        // kDebug() << "busy!!";
 
517
        //TODO: implement a queue instead
 
518
        return;
 
519
    }
 
520
 
 
521
    d->transitioningActivity = id;
 
522
    d->setActivityState(id, Starting);
 
523
 
 
524
    //ugly hack to avoid dbus deadlocks
 
525
    QMetaObject::invokeMethod(d, "reallyStartActivity", Qt::QueuedConnection, Q_ARG(QString, id));
 
526
}
 
527
 
 
528
void ActivityManagerPrivate::reallyStartActivity(const QString & id)
 
529
{
 
530
    bool called = false;
 
531
    // start the starting :)
 
532
    QDBusInterface kwin("org.kde.kwin", "/KWin", "org.kde.KWin");
 
533
    if (kwin.isValid()) {
 
534
        QDBusMessage reply = kwin.call("startActivity", id);
 
535
        if (reply.type() == QDBusMessage::ErrorMessage) {
 
536
            // kDebug() << "dbus error:" << reply.errorMessage();
 
537
 
 
538
        } else {
 
539
            QList<QVariant> ret = reply.arguments();
 
540
            if (ret.length() == 1 && ret.first().toBool()) {
 
541
                called = true;
 
542
            } else {
 
543
                // kDebug() << "call returned false; probably ksmserver is busy";
 
544
                setActivityState(transitioningActivity, ActivityManager::Stopped);
 
545
                transitioningActivity.clear();
 
546
                return; //assume we're mid-logout and just don't touch anything
 
547
            }
 
548
        }
 
549
    } else {
 
550
        // kDebug() << "couldn't get kwin interface";
 
551
    }
 
552
 
 
553
    if (!called) {
 
554
        //maybe they use compiz?
 
555
        //go ahead without the session
 
556
        startCompleted();
 
557
    }
 
558
    configSync(); //force immediate sync
 
559
}
 
560
 
 
561
void ActivityManagerPrivate::startCompleted()
 
562
{
 
563
    if (transitioningActivity.isEmpty()) {
 
564
        // kDebug() << "huh?";
 
565
        return;
 
566
    }
 
567
    setActivityState(transitioningActivity, ActivityManager::Running);
 
568
    transitioningActivity.clear();
 
569
}
 
570
 
 
571
void ActivityManager::StopActivity(const QString & id)
 
572
{
 
573
    // kDebug() << id;
 
574
 
 
575
    if (!d->activities.contains(id) ||
 
576
            d->activities[id] == Stopped) {
 
577
        return;
 
578
    }
 
579
 
 
580
    if (!d->transitioningActivity.isEmpty()) {
 
581
        // kDebug() << "busy!!";
 
582
        //TODO: implement a queue instead
 
583
        return;
 
584
    }
 
585
 
 
586
    d->transitioningActivity = id;
 
587
    d->setActivityState(id, Stopping);
 
588
 
 
589
    //ugly hack to avoid dbus deadlocks
 
590
    QMetaObject::invokeMethod(d, "reallyStopActivity", Qt::QueuedConnection, Q_ARG(QString, id));
 
591
}
 
592
 
 
593
void ActivityManagerPrivate::reallyStopActivity(const QString & id)
 
594
{
 
595
    bool called = false;
 
596
    // start the stopping :)
 
597
    QDBusInterface kwin("org.kde.kwin", "/KWin", "org.kde.KWin");
 
598
    if (kwin.isValid()) {
 
599
        QDBusMessage reply = kwin.call("stopActivity", id);
 
600
        if (reply.type() == QDBusMessage::ErrorMessage) {
 
601
            // kDebug() << "dbus error:" << reply.errorMessage();
 
602
 
 
603
        } else {
 
604
            QList<QVariant> ret = reply.arguments();
 
605
            if (ret.length() == 1 && ret.first().toBool()) {
 
606
                called = true;
 
607
 
 
608
            } else {
 
609
                // kDebug() << "call returned false; probably ksmserver is busy";
 
610
                stopCancelled();
 
611
                return; //assume we're mid-logout and just don't touch anything
 
612
            }
 
613
        }
 
614
    } else {
 
615
        // kDebug() << "couldn't get kwin interface";
 
616
    }
 
617
 
 
618
    if (!called) {
 
619
        //maybe they use compiz?
 
620
        //go ahead without the session
 
621
        stopCompleted();
 
622
    }
 
623
}
 
624
 
 
625
void ActivityManagerPrivate::stopCompleted()
 
626
{
 
627
    if (transitioningActivity.isEmpty()) {
 
628
        // kDebug() << "huh?";
 
629
        return;
 
630
    }
 
631
    setActivityState(transitioningActivity, ActivityManager::Stopped);
 
632
    if (currentActivity == transitioningActivity) {
 
633
        ensureCurrentActivityIsRunning();
 
634
    }
 
635
    transitioningActivity.clear();
 
636
    configSync(); //force immediate sync
 
637
}
 
638
 
 
639
void ActivityManagerPrivate::stopCancelled()
 
640
{
 
641
    if (transitioningActivity.isEmpty()) {
 
642
        // kDebug() << "huh?";
 
643
        return;
 
644
    }
 
645
    setActivityState(transitioningActivity, ActivityManager::Running);
 
646
    transitioningActivity.clear();
 
647
}
 
648
 
 
649
int ActivityManager::ActivityState(const QString & id) const
 
650
{
 
651
    //kDebug() << id << "- is it in" << d->activities << "?";
 
652
    if (!d->activities.contains(id)) {
 
653
        return Invalid;
 
654
    } else {
 
655
        // kDebug() << "state of" << id << "is" << d->activities[id];
 
656
        return d->activities[id];
 
657
    }
 
658
}
 
659
 
 
660
QStringList ActivityManager::ListActivities() const
 
661
{
 
662
    return d->activities.keys();
 
663
}
 
664
 
 
665
QStringList ActivityManager::ListActivities(int state) const
 
666
{
 
667
    return d->activities.keys((State)state);
 
668
}
 
669
 
 
670
QString ActivityManager::ActivityName(const QString & id) const
 
671
{
 
672
    return d->activityName(id);
 
673
}
 
674
 
 
675
void ActivityManager::SetActivityName(const QString & id, const QString & name)
 
676
{
 
677
    // kDebug() << id << name;
 
678
 
 
679
    if (!d->activities.contains(id)) {
 
680
        return;
 
681
    }
 
682
 
 
683
    d->activitiesConfig().writeEntry(id, name);
 
684
 
 
685
#ifdef HAVE_NEPOMUK
 
686
    if (NEPOMUK_RUNNING) {
 
687
        d->activityResource(id).setLabel(name);
 
688
    }
 
689
#endif
 
690
 
 
691
    d->scheduleConfigSync();
 
692
 
 
693
    // kDebug() << "emit ActivityChanged" << id;
 
694
    emit ActivityChanged(id);
 
695
}
 
696
 
 
697
QString ActivityManager::ActivityDescription(const QString & id) const
 
698
{
 
699
    if (!NEPOMUK_RUNNING || !d->activities.contains(id)) {
 
700
        return QString();
 
701
    }
 
702
 
 
703
#ifdef HAVE_NEPOMUK
 
704
    return d->activityResource(id).description();
 
705
#endif
 
706
}
 
707
 
 
708
void ActivityManager::SetActivityDescription(const QString & id, const QString & description)
 
709
{
 
710
    // kDebug() << id << description;
 
711
 
 
712
    if (!NEPOMUK_RUNNING || !d->activities.contains(id)) {
 
713
        return;
 
714
    }
 
715
 
 
716
#ifdef HAVE_NEPOMUK
 
717
    d->activityResource(id).setDescription(description);
 
718
#endif
 
719
 
 
720
    // kDebug() << "emit ActivityChanged" << id;
 
721
    emit ActivityChanged(id);
 
722
}
 
723
 
 
724
QString ActivityManager::ActivityIcon(const QString & id) const
 
725
{
 
726
    if (!NEPOMUK_RUNNING || !d->activities.contains(id)) {
 
727
        return QString();
 
728
    }
 
729
 
 
730
#ifdef HAVE_NEPOMUK
 
731
    QStringList symbols = d->activityResource(id).symbols();
 
732
 
 
733
    if (symbols.isEmpty()) {
 
734
        return QString();
 
735
    } else {
 
736
        return symbols.first();
 
737
    }
 
738
#else
 
739
    return QString();
 
740
#endif
 
741
}
 
742
 
 
743
void ActivityManager::SetActivityIcon(const QString & id, const QString & icon)
 
744
{
 
745
    // kDebug() << id << icon;
 
746
 
 
747
    if (!NEPOMUK_RUNNING || !d->activities.contains(id)) {
 
748
        return;
 
749
    }
 
750
 
 
751
#ifdef HAVE_NEPOMUK
 
752
    d->activityResource(id).setSymbols(QStringList() << icon);
 
753
 
 
754
    // kDebug() << "emit ActivityChanged" << id;
 
755
    emit ActivityChanged(id);
 
756
#endif
 
757
}
 
758
 
 
759
 
 
760
// Resource related mothods
 
761
void ActivityManager::RegisterResourceEvent(const QString & application, uint _windowId,
 
762
        const QString & uri, uint event, uint reason)
 
763
{
 
764
    if (event > Event::LastEventType || reason > Event::LastEventReason)
 
765
        return;
 
766
 
 
767
    // Dirty way to skip special web browser URIs
 
768
    if (uri.startsWith("about:"))
 
769
        return;
 
770
 
 
771
    KUrl kuri(uri);
 
772
    WId windowId = (WId) _windowId;
 
773
 
 
774
    kDebug() << "New event on the horizon" << application << windowId << event;
 
775
 
 
776
#ifdef HAVE_NEPOMUK
 
777
    if (uri.startsWith("nepomuk:")) {
 
778
        Nepomuk::Resource resource(kuri);
 
779
 
 
780
        if (resource.hasProperty(NIE::url())) {
 
781
            kuri = resource.property(NIE::url()).toUrl();
 
782
            // kDebug() << "Passing real url" << kuri;
 
783
        } else {
 
784
            // kWarning() << "Passing nepomuk:// url" << kuri;
 
785
        }
 
786
    }
 
787
#endif
 
788
 
 
789
    if (event == Event::Opened) {
 
790
 
 
791
        // kDebug() << "Saving the open event for the window" << windowId;
 
792
 
 
793
        d->windows[windowId].resources << kuri;
 
794
        d->resources[kuri].activities << CurrentActivity();
 
795
 
 
796
        // kDebug() << d->windows.keys();
 
797
 
 
798
    } else if (event == Event::Closed) {
 
799
 
 
800
        // TODO: Remove from d->resources if needed
 
801
        d->windows.remove(windowId);
 
802
 
 
803
    }
 
804
 
 
805
    EventProcessor::self()->addEvent(application, windowId,
 
806
            kuri.url(), (Event::Type) event, (Event::Reason) reason);
 
807
 
 
808
}
 
809
 
 
810
void ActivityManager::RegisterResourceMimeType(const QString & uri, const QString & mimetype)
 
811
{
 
812
    KUrl kuri(uri);
 
813
 
 
814
    d->resources[kuri].mimetype = mimetype;
 
815
}
 
816
 
 
817
void ActivityManager::RegisterResourceTitle(const QString & uri, const QString & title)
 
818
{
 
819
    KUrl kuri(uri);
 
820
 
 
821
    d->resources[kuri].title = title;
 
822
}
 
823
 
 
824
void ActivityManager::LinkResourceToActivity(const QString & uri, const QString & activity)
 
825
{
 
826
#ifdef HAVE_NEPOMUK
 
827
    if (!d->nepomukInitialized()) return;
 
828
 
 
829
    kDebug() << "Linking" << uri << "to" << activity << CurrentActivity();
 
830
 
 
831
    // TODO:
 
832
    // I'd like a resource isRelated activity more than vice-versa
 
833
    // but the active models are checking for the other way round.
 
834
    // It is defined in the ontologies as a symmetric relation, but
 
835
    // Nepomuk doesn't care about that.
 
836
 
 
837
    // Nepomuk::Resource(KUrl(uri)).
 
838
    //     addIsRelated(d->activityResource(
 
839
    //         activity.isEmpty() ?
 
840
    //             CurrentActivity() : activity
 
841
    //         )
 
842
    //     );
 
843
 
 
844
    d->activityResource(activity.isEmpty() ? CurrentActivity() : activity).
 
845
        addIsRelated(Nepomuk::Resource(KUrl(uri))
 
846
    );
 
847
 
 
848
#endif
 
849
}
 
850
 
 
851
// void ActivityManager::UnlinkResourceFromActivity(const QString & uri, const QString & activity)
 
852
// {
 
853
// #ifdef HAVE_NEPOMUK
 
854
//     if (!d->nepomukInitialized()) return;
 
855
//
 
856
//     Nepomuk::Resource(KUrl(uri)).
 
857
//         addIsRelated(d->activityResource(
 
858
//             activity.isEmpty() ?
 
859
//                 CurrentActivity() : activity
 
860
//             )
 
861
//         );
 
862
//
 
863
// #endif
 
864
// }
 
865
 
 
866
// static
 
867
ActivityManager * ActivityManager::self()
 
868
{
 
869
    return static_cast<ActivityManager*>(kapp);
 
870
}
 
871
 
 
872