Database: sqlite default to appdata kworship.db
[kworship.git] / unipresent / okular / UpOkBackend.cpp
blob073c05566444ca5a9ba08a6bea9cf5312e48b1ec
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008-2009 James Hogan <james@albanarts.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 /**
21 * @file UpOkBackend.cpp
22 * @brief Okular presentation manager.
23 * @author James Hogan <james@albanarts.com>
26 #include "UpOkBackend.h"
27 #include "UpOkProcess.h"
28 #include "UpOkPresentation.h"
30 #include <KRun>
31 #include <KLocale>
32 #include <KGenericFactory>
33 #include <KIcon>
35 #include <QDBusConnection>
36 #include <QDBusConnectionInterface>
37 #include <QDBusInterface>
38 #include <QStringList>
40 #include <cassert>
42 K_EXPORT_COMPONENT_FACTORY( unipresent_okular, KGenericFactory<UpOkBackend>("unipresent_okular") )
45 * Constructors + destructor
48 /// Primary constructor.
49 UpOkBackend::UpOkBackend(QObject* parent, const QStringList& params)
50 : UpBackend(parent, params)
51 , m_processes()
52 , m_presentations()
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.kde.okular-"))
64 dbusServiceOwnerChange(serviceName, QString(), "dummy"); // the new owner string isn't used
69 /// Destructor.
70 UpOkBackend::~UpOkBackend()
72 foreach (UpOkProcess* process, m_processes)
74 delete process;
79 * General meta information
82 QString UpOkBackend::id() const
84 return "Okular";
87 QString UpOkBackend::name() const
89 return i18n("Okular");
92 QString UpOkBackend::description() const
94 return i18n("Controls a running Okular presentation");
97 QStringList UpOkBackend::mimeTypes() const
99 /// @todo Find mime types from okular if possible
100 return QStringList()
101 << "application/pdf"
105 QIcon UpOkBackend::icon() const
107 return KIcon("okular");
111 * Activation
114 bool UpOkBackend::isActive()
116 return !m_processes.isEmpty();
119 bool UpOkBackend::activate()
121 return true;
124 void UpOkBackend::deactivate()
129 * Presentation management
132 QList<UpPresentation*> UpOkBackend::presentations()
134 return m_presentations;
137 bool UpOkBackend::openPresentation(const QUrl& url)
139 // Ensure the program is up and running
140 activate();
142 // Check if the presentation is already open
143 foreach (UpPresentation* presentation, m_presentations)
145 if (presentation->url() == url)
147 return true;
151 // Otherwise open it now
152 /// @todo Configurable okular exec
153 bool started = KRun::run("okular", KUrl::List() << url, 0);
155 return started;
159 * Private slots
162 /// DBus service ownership change.
163 void UpOkBackend::dbusServiceOwnerChange(const QString& name, const QString& oldOwner, const QString& newOwner)
165 // Is it an interesting service name?
166 if (name.startsWith("org.kde.okular-"))
168 // Oooh, perhaps there are new presentations available
169 if (oldOwner.isEmpty())
171 UpOkProcess* process = new UpOkProcess(name, this);
172 m_processes[name] = process;
173 // Link presentation event signals
174 connect(process, SIGNAL(loadedPresentation(UpOkPresentation*)),
175 this, SLOT (dbusLoadedPresentation(UpOkPresentation*)));
176 connect(process, SIGNAL(unloadedPresentation(UpOkPresentation*)),
177 this, SLOT (dbusUnloadedPresentation(UpOkPresentation*)));
178 // Trigger adding of presentations that are already open
179 QList<UpOkPresentation*> presentations = process->presentations();
180 foreach (UpOkPresentation* 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())
191 delete *it;
192 m_processes.erase(it);
198 // Slots for UpOkProcess signals
200 void UpOkBackend::dbusLoadedPresentation(UpOkPresentation* presentation)
202 m_presentations.push_back(presentation);
203 loadedPresentation(presentation);
206 void UpOkBackend::dbusUnloadedPresentation(UpOkPresentation* presentation)
208 m_presentations.removeOne(presentation);
209 unloadedPresentation(presentation);