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 ***************************************************************************/
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"
32 #include <KServiceTypeTrader>
37 UpManager
* UpManager::s_singleton
= 0;
43 /// Get the singleton object.
44 UpManager
* UpManager::self()
50 * Constructors + destructor
53 /// Primary constructor.
54 UpManager::UpManager(QObject
* parent
)
57 , m_presentationsModel(new UpPresentationsModel(this))
58 , m_backendAssociations()
59 , m_preferredBackends()
61 assert(s_singleton
== 0);
64 m_presentationsModel
->setRootNode(new UpManagerNode(0, this));
68 UpManager::~UpManager()
70 foreach(UpBackend
* backend
, m_backends
)
72 backendRemoved(backend
);
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();
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) \
107 if (0 != (BACKEND)) \
109 bool loaded = (BACKEND)->openPresentation(url); \
114 *attemptFailed = true; \
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
);
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
);
160 #undef TRY_BACKEND_OPEN_PRESENTATION
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
)
193 /// Get a list of backends.
194 QList
<UpBackend
*> UpManager::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
);
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();
218 UpBackend
* backend
= service
->createInstance
<UpBackend
>(this, QVariantList(), &err
);
221 qDebug() << " Loaded successfully";
226 qDebug() << " Could not be loaded: " << err
;