2
* Copyright 2011 by Aaron Seigo <aseigo@kde.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Library General Public License as
6
* published by the Free Software Foundation; either version 2, or
7
* (at your option) any later version.
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 Library General Public License for more details
14
* You should have received a copy of the GNU Library 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.
20
#include "activecontentservice.h"
22
#include <QDBusConnection>
23
#include <QDBusConnectionInterface>
24
#include <QWeakPointer>
26
#include <KWindowSystem>
28
#include "activecontentdbusinterface_p.h"
30
namespace ActiveContent
33
static const QString SERVICE_NAME("org.kde.activeServiceContent");
35
class ActiveContentService::Private
38
Private(ActiveContentService *obj)
44
void activeWindowChanged(WId wid);
46
ActiveContentService *q;
47
ActiveContent content;
50
static QWeakPointer<ActiveContentDBusInterface> interface;
53
QWeakPointer<ActiveContentDBusInterface> ActiveContentService::Private::interface;
55
ActiveContentService::ActiveContentService(const ActiveContent &content, QObject *parent)
59
setActiveContent(content);
62
ActiveContentService::ActiveContentService(QObject *parent)
68
ActiveContentService::~ActiveContentService()
73
void ActiveContentService::setActive(bool active)
75
const bool isActive = d->interface && d->interface.data()->current() == this;
76
if (isActive == active) {
82
d->interface = new ActiveContentDBusInterface;
85
d->interface.data()->setCurrent(this);
86
QDBusConnection::sessionBus().interface()->registerService(SERVICE_NAME,
87
QDBusConnectionInterface::ReplaceExistingService,
88
QDBusConnectionInterface::AllowReplacement);
89
} else if (isActive) {
90
QDBusConnection::sessionBus().interface()->unregisterService(SERVICE_NAME);
92
d->interface.data()->setCurrent(0);
97
bool ActiveContentService::isActive() const
102
void ActiveContentService::setActiveContent(const ActiveContent &content)
104
const bool hadWindow = d->content.windowId();
105
d->content = content;
106
if (d->content.windowId()) {
107
connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)),
108
this, SLOT(activeWindowChanged(WId)), Qt::UniqueConnection);
109
d->activeWindowChanged(KWindowSystem::activeWindow());
110
} else if (hadWindow) {
111
disconnect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)),
112
this, SLOT(activeWindowChanged(WId)));
115
// base it on the URL having something useful in it
116
setActive(d->content.url().isValid());
120
ActiveContent ActiveContentService::activeContent() const
125
void ActiveContentService::Private::activeWindowChanged(WId wid)
127
q->setActive(content.windowId() == wid);
130
} // namespace ActiveContent
132
#include "activecontentservice.moc"