~ubuntu-branches/ubuntu/saucy/share-like-connect/saucy

« back to all changes in this revision

Viewing changes to activecontentservice/activecontentservice.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-09-17 11:30:06 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130917113006-5ohj8qt9kuf3izma
Tags: 1:0.4-0ubuntu1
* New upstream release LP: #1227602
* Use epoch to reset version number in line with upstream
* Add build-dep on nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *   Copyright 2011 by Aaron Seigo <aseigo@kde.org>
3
 
 *
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.
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 Library General Public License for more details
13
 
 *
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.
18
 
 */
19
 
 
20
 
#include "activecontentservice.h"
21
 
 
22
 
#include <QDBusConnection>
23
 
#include <QDBusConnectionInterface>
24
 
#include <QWeakPointer>
25
 
 
26
 
#include <KWindowSystem>
27
 
 
28
 
#include "activecontentdbusinterface_p.h"
29
 
 
30
 
namespace ActiveContent
31
 
{
32
 
 
33
 
static const QString SERVICE_NAME("org.kde.activeServiceContent");
34
 
 
35
 
class ActiveContentService::Private
36
 
{
37
 
public:
38
 
    Private(ActiveContentService *obj)
39
 
        : q(obj),
40
 
          active(false)
41
 
    {
42
 
    }
43
 
 
44
 
    void activeWindowChanged(WId wid);
45
 
 
46
 
    ActiveContentService *q;
47
 
    ActiveContent content;
48
 
    bool active;
49
 
 
50
 
    static QWeakPointer<ActiveContentDBusInterface> interface;
51
 
};
52
 
 
53
 
QWeakPointer<ActiveContentDBusInterface> ActiveContentService::Private::interface;
54
 
 
55
 
ActiveContentService::ActiveContentService(const ActiveContent &content, QObject *parent)
56
 
    : QObject(parent),
57
 
      d(new Private(this))
58
 
{
59
 
    setActiveContent(content);
60
 
}
61
 
 
62
 
ActiveContentService::ActiveContentService(QObject *parent)
63
 
    : QObject(parent),
64
 
      d(new Private(this))
65
 
{
66
 
}
67
 
 
68
 
ActiveContentService::~ActiveContentService()
69
 
{
70
 
    delete d;
71
 
}
72
 
 
73
 
void ActiveContentService::setActive(bool active)
74
 
{
75
 
    const bool isActive = d->interface && d->interface.data()->current() == this;
76
 
    if (isActive == active) {
77
 
        return;
78
 
    }
79
 
 
80
 
    if (active) {
81
 
        if (!d->interface) {
82
 
            d->interface = new ActiveContentDBusInterface;
83
 
        }
84
 
 
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);
91
 
        if (d->interface) {
92
 
            d->interface.data()->setCurrent(0);
93
 
        }
94
 
    }
95
 
}
96
 
 
97
 
bool ActiveContentService::isActive() const
98
 
{
99
 
    return d->active;
100
 
}
101
 
 
102
 
void ActiveContentService::setActiveContent(const ActiveContent &content)
103
 
{
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)));
113
 
        setActive(false);
114
 
    } else {
115
 
        // base it on the URL having something useful in it
116
 
        setActive(d->content.url().isValid());
117
 
    }
118
 
}
119
 
 
120
 
ActiveContent ActiveContentService::activeContent() const
121
 
{
122
 
    return d->content;
123
 
}
124
 
 
125
 
void ActiveContentService::Private::activeWindowChanged(WId wid)
126
 
{
127
 
    q->setActive(content.windowId() == wid);
128
 
}
129
 
 
130
 
} // namespace ActiveContent
131
 
 
132
 
#include "activecontentservice.moc"
133