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>
35 #include <QDBusConnection>
36 #include <QDBusConnectionInterface>
37 #include <QDBusInterface>
38 #include <QStringList>
42 K_EXPORT_COMPONENT_FACTORY( unipresent_kpresenter2
, KGenericFactory
<UpKpr2Backend
>("unipresent_kpresenter2") )
45 * Constructors + destructor
48 /// Primary constructor.
49 UpKpr2Backend::UpKpr2Backend(QObject
* parent
, const QStringList
& params
)
50 : UpBackend(parent
, params
)
54 QDBusConnectionInterface
* interface
= QDBusConnection::sessionBus().interface();
55 connect(interface
, SIGNAL(serviceOwnerChanged(const QString
&, const QString
&, const QString
&)), this, SLOT(dbusServiceOwnerChange(const QString
&, const QString
&, const QString
&)));
57 QDBusReply
<QStringList
> reply
= interface
->registeredServiceNames();
58 assert(reply
.isValid());
59 QStringList serviceNames
= reply
;
60 foreach (QString serviceName
, serviceNames
)
62 if (serviceName
.startsWith("org.koffice.kpresenter-"))
64 dbusServiceOwnerChange(serviceName
, QString(), "dummy"); // the new owner string isn't used
70 UpKpr2Backend::~UpKpr2Backend()
72 foreach (UpKpr2Process
* process
, m_processes
)
79 * General meta information
82 QString
UpKpr2Backend::id() const
84 return "KOffice2/KPresenter";
87 QString
UpKpr2Backend::name() const
89 return i18n("KPresenter 2");
92 QString
UpKpr2Backend::description() const
94 return i18n("Controls a running KPresenter 2 presentation");
97 QStringList
UpKpr2Backend::mimeTypes() const
99 /// @todo Find mime types from kpresenter if possible
101 << "application/x-kpresenter"
102 << "application/vnd.oasis.opendocument.presentation"
106 QIcon
UpKpr2Backend::icon() const
108 return KIcon("kpresenter");
115 bool UpKpr2Backend::isActive()
117 return !m_processes
.isEmpty();
120 bool UpKpr2Backend::activate()
125 void UpKpr2Backend::deactivate()
130 * Presentation management
133 QList
<UpPresentation
*> UpKpr2Backend::presentations()
135 return m_presentations
;
138 bool UpKpr2Backend::openPresentation(const QUrl
& url
)
140 // Ensure the program is up and running
143 // Check if the presentation is already open
144 foreach (UpPresentation
* presentation
, m_presentations
)
146 if (presentation
->url() == url
)
152 // Otherwise open it now
153 /// @todo Configurable kpresenter exec
154 bool started
= KRun::run("kpresenter", KUrl::List() << url
, 0);
163 /// DBus service ownership change.
164 void UpKpr2Backend::dbusServiceOwnerChange(const QString
& name
, const QString
& oldOwner
, const QString
& newOwner
)
166 // Is it an interesting service name?
167 if (name
.startsWith("org.koffice.kpresenter-"))
169 // Oooh, perhaps there are new presentations available
170 if (oldOwner
.isEmpty())
172 UpKpr2Process
* process
= new UpKpr2Process(name
, this);
173 m_processes
[name
] = process
;
174 // Link presentation event signals
175 connect(process
, SIGNAL(loadedPresentation(UpKpr2Presentation
*)),
176 this, SLOT (dbusLoadedPresentation(UpKpr2Presentation
*)));
177 connect(process
, SIGNAL(unloadedPresentation(UpKpr2Presentation
*)),
178 this, SLOT (dbusUnloadedPresentation(UpKpr2Presentation
*)));
179 // Trigger adding of presentations that are already open
180 QList
<UpKpr2Presentation
*> presentations
= process
->presentations();
181 foreach (UpKpr2Presentation
* pres
, presentations
)
183 dbusLoadedPresentation(pres
);
186 // Or perhaps the presentations are no longer available
187 else if (newOwner
.isEmpty())
189 QStringHashProcess::iterator it
= m_processes
.find(name
);
190 if (it
!= m_processes
.end())
193 m_processes
.erase(it
);
199 // Slots for UpKpr2Process signals
201 void UpKpr2Backend::dbusLoadedPresentation(UpKpr2Presentation
* presentation
)
203 m_presentations
.push_back(presentation
);
204 loadedPresentation(presentation
);
207 void UpKpr2Backend::dbusUnloadedPresentation(UpKpr2Presentation
* presentation
)
209 m_presentations
.removeOne(presentation
);
210 unloadedPresentation(presentation
);