Database: sqlite default to appdata kworship.db
[kworship.git] / unipresent / common / UpManager.cpp
blob6f5de511ea766946b39d32f6bba0857a0e70fcfe
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 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 UpManager.cpp
22 * @brief Overall unipresent manager.
23 * @author James Hogan <james@albanarts.com>
26 #include "UpManager.h"
27 #include "UpManagerNode.h"
28 #include "UpBackend.h"
29 #include "UpPresentationsModel.h"
31 #include <KMimeType>
32 #include <KServiceTypeTrader>
34 #include <cassert>
36 /// Singleton object.
37 UpManager* UpManager::s_singleton = 0;
40 * Singleton access.
43 /// Get the singleton object.
44 UpManager* UpManager::self()
46 return s_singleton;
50 * Constructors + destructor
53 /// Primary constructor.
54 UpManager::UpManager(QObject* parent)
55 : QObject(parent)
56 , m_backends()
57 , m_presentationsModel(new UpPresentationsModel(this))
58 , m_backendAssociations()
59 , m_preferredBackends()
61 assert(s_singleton == 0);
62 s_singleton = this;
64 m_presentationsModel->setRootNode(new UpManagerNode(0, this));
67 /// Destructor.
68 UpManager::~UpManager()
70 foreach(UpBackend* backend, m_backends)
72 backendRemoved(backend);
73 delete backend;
75 s_singleton = 0;
79 * Presentation management
82 /// Get a list of presentations.
83 QList<UpPresentation*> UpManager::presentations()
85 QList<UpPresentation*> presentations;
86 foreach(UpBackend* backend, m_backends)
88 presentations << backend->presentations();
90 return presentations;
93 /// Get a presentations model.
94 UpPresentationsModel* UpManager::presentationsModel()
96 return m_presentationsModel;
99 /// Open a new presentation.
100 bool UpManager::openPresentation(const QUrl& url, bool* attemptFailed)
102 *attemptFailed = false;
103 // This macro tries to open the url with the given backend
104 #define TRY_BACKEND_OPEN_PRESENTATION(BACKEND) \
105 do \
107 if (0 != (BACKEND)) \
109 bool loaded = (BACKEND)->openPresentation(url); \
110 if (loaded) \
112 return true; \
114 *attemptFailed = true; \
116 } while (0)
118 KSharedPtr<KMimeType> mimeType = KMimeType::findByUrl(url);
120 // Check if this mime type has a specific association
121 QStringHashString::const_iterator it = m_backendAssociations.constFind(mimeType->name());
122 if (it != m_backendAssociations.constEnd())
124 UpBackend* backend = backendById(*it);
125 TRY_BACKEND_OPEN_PRESENTATION(backend);
128 // Check if each preferred backend supports mime type
129 foreach (QString backendId, m_preferredBackends)
131 UpBackend* backend = backendById(backendId);
132 if (0 != backend)
134 QStringList supportedMimes = backend->mimeTypes();
135 foreach (QString mime, supportedMimes)
137 if (mimeType->is(mime))
139 TRY_BACKEND_OPEN_PRESENTATION(backend);
145 // Check all backends if they support mimetype
146 foreach (UpBackend* backend, m_backends)
148 QStringList supportedMimes = backend->mimeTypes();
149 foreach (QString mime, supportedMimes)
151 if (mimeType->is(mime))
153 TRY_BACKEND_OPEN_PRESENTATION(backend);
158 return false;
160 #undef TRY_BACKEND_OPEN_PRESENTATION
164 * Backend management
167 /// Get the number of backends.
168 int UpManager::numBackends() const
170 return m_backends.size();
173 /// Get a specific backend.
174 UpBackend* UpManager::backendByIndex(int index)
176 return m_backends.at(index);
179 /// Get a specific backend by name.
180 UpBackend* UpManager::backendById(QString id)
182 /// @todo This could be more efficient
183 foreach (UpBackend* backend, m_backends)
185 if (backend->id() == id)
187 return backend;
190 return 0;
193 /// Get a list of backends.
194 QList<UpBackend*> UpManager::backends()
196 return m_backends;
199 /// Add a backend object.
200 void UpManager::addBackend(UpBackend* backend)
202 m_backends.push_back(backend);
203 connect(backend, SIGNAL(loadedPresentation(UpPresentation*)), m_presentationsModel, SLOT(loadedPresentation(UpPresentation*)));
204 connect(backend, SIGNAL(unloadedPresentation(UpPresentation*)), m_presentationsModel, SLOT(unloadedPresentation(UpPresentation*)));
205 backendAdded(backend);
208 #include <QtDebug>
209 /// Load dynamic backend.
210 void UpManager::loadBackends()
212 KService::List offers = KServiceTypeTrader::self()->query("UniPresent/Backend");
214 foreach (KService::Ptr service, offers)
216 qDebug() << "Found UniPresent backend: " << service->desktopEntryName();
217 QString err;
218 UpBackend* backend = service->createInstance<UpBackend>(this, QVariantList(), &err);
219 if (backend)
221 qDebug() << " Loaded successfully";
222 addBackend(backend);
224 else
226 qDebug() << " Could not be loaded: " << err;