1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file UpKpr2Backend.cpp
22 * @brief KPresenter 2 presentation manager.
23 * @author James Hogan <james@albanarts.com>
26 #include "UpKpr2Backend.h"
27 #include "UpKpr2Process.h"
28 #include "UpKpr2Presentation.h"
32 #include <KGenericFactory>
34 #include <QDBusConnection>
35 #include <QDBusConnectionInterface>
36 #include <QDBusInterface>
37 #include <QStringList>
41 K_EXPORT_COMPONENT_FACTORY( unipresent_kpresenter2
, KGenericFactory
<UpKpr2Backend
>("unipresent_kpresenter2") )
44 * Constructors + destructor
47 /// Primary constructor.
48 UpKpr2Backend::UpKpr2Backend(QObject
* parent
, const QStringList
& params
)
49 : UpBackend(parent
, params
)
53 QDBusConnectionInterface
* interface
= QDBusConnection::sessionBus().interface();
54 connect(interface
, SIGNAL(serviceOwnerChanged(const QString
&, const QString
&, const QString
&)), this, SLOT(dbusServiceOwnerChange(const QString
&, const QString
&, const QString
&)));
56 QDBusReply
<QStringList
> reply
= interface
->registeredServiceNames();
57 assert(reply
.isValid());
58 QStringList serviceNames
= reply
;
59 foreach (QString serviceName
, serviceNames
)
61 if (serviceName
.startsWith("org.koffice.kpresenter-"))
63 dbusServiceOwnerChange(serviceName
, QString(), "dummy"); // the new owner string isn't used
69 UpKpr2Backend::~UpKpr2Backend()
71 foreach (UpKpr2Process
* process
, m_processes
)
78 * General meta information
81 QString
UpKpr2Backend::id() const
83 return "KOffice2/KPresenter";
86 QString
UpKpr2Backend::name() const
88 return i18n("KPresenter 2");
91 QString
UpKpr2Backend::description() const
93 return i18n("Controls a running KPresenter 2 presentation");
96 QStringList
UpKpr2Backend::mimeTypes() const
98 /// @todo Find mime types from kpresenter if possible
100 << "application/x-kpresenter"
101 << "application/vnd.oasis.opendocument.presentation"
105 QIcon
UpKpr2Backend::icon() const
114 bool UpKpr2Backend::isActive()
116 return !m_processes
.isEmpty();
119 bool UpKpr2Backend::activate()
124 void UpKpr2Backend::deactivate()
129 * Presentation management
132 QList
<UpPresentation
*> UpKpr2Backend::presentations()
134 return m_presentations
;
137 bool UpKpr2Backend::openPresentation(const QUrl
& url
)
139 // Ensure the program is up and running
142 // Check if the presentation is already open
143 foreach (UpPresentation
* presentation
, m_presentations
)
145 if (presentation
->url() == url
)
151 // Otherwise open it now
152 /// @todo Configurable kpresenter exec
153 bool started
= KRun::run("kpresenter", KUrl::List() << url
, 0);
162 /// DBus service ownership change.
163 void UpKpr2Backend::dbusServiceOwnerChange(const QString
& name
, const QString
& oldOwner
, const QString
& newOwner
)
165 // Is it an interesting service name?
166 if (name
.startsWith("org.koffice.kpresenter-"))
168 // Oooh, perhaps there are new presentations available
169 if (oldOwner
.isEmpty())
171 UpKpr2Process
* process
= new UpKpr2Process(name
, this);
172 m_processes
[name
] = process
;
173 // Link presentation event signals
174 connect(process
, SIGNAL(loadedPresentation(UpKpr2Presentation
*)),
175 this, SLOT (dbusLoadedPresentation(UpKpr2Presentation
*)));
176 connect(process
, SIGNAL(unloadedPresentation(UpKpr2Presentation
*)),
177 this, SLOT (dbusUnloadedPresentation(UpKpr2Presentation
*)));
178 // Trigger adding of presentations that are already open
179 QList
<UpKpr2Presentation
*> presentations
= process
->presentations();
180 foreach (UpKpr2Presentation
* pres
, presentations
)
182 dbusLoadedPresentation(pres
);
185 // Or perhaps the presentations are no longer available
186 else if (newOwner
.isEmpty())
188 QStringHashProcess::iterator it
= m_processes
.find(name
);
189 if (it
!= m_processes
.end())
192 m_processes
.erase(it
);
198 // Slots for UpKpr2Process signals
200 void UpKpr2Backend::dbusLoadedPresentation(UpKpr2Presentation
* presentation
)
202 m_presentations
.push_back(presentation
);
203 loadedPresentation(presentation
);
206 void UpKpr2Backend::dbusUnloadedPresentation(UpKpr2Presentation
* presentation
)
208 m_presentations
.removeOne(presentation
);
209 unloadedPresentation(presentation
);