Silence some unused parameter warnings
[kworship.git] / kworship / bible / KwBiblePlugin.cpp
blob9658ba5a7a3a388864b97b4ba25d50c05a361988
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 KwBiblePlugin.cpp
22 * @brief Bibles plugin.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwBiblePlugin.h"
27 #include <KwApplication.h>
28 #include <kworship.h>
29 #include <KwDocument.h>
31 #include "KwBibleManager.h"
32 #include "KwBibleModule.h"
33 #include "KwBiblePlaylistItem.h"
35 #include <KwPlaylistModel.h>
37 #include <KLocale>
38 #include <KAction>
39 #include <KMessageBox>
40 #include <KGenericFactory>
41 #include <KServiceTypeTrader>
43 #include <QMainWindow>
44 #include <QDockWidget>
45 #include <QHBoxLayout>
46 #include <QVBoxLayout>
47 #include <QComboBox>
48 #include <QLabel>
49 #include <QLineEdit>
50 #include <QTextEdit>
51 #include <QToolBar>
53 K_EXPORT_COMPONENT_FACTORY( kworship_bible, KGenericFactory<KwBiblePlugin>("kworship_bible") )
56 * Constructors + destructor
59 /// Default constructor.
60 KwBiblePlugin::KwBiblePlugin(QObject* parent, const QStringList& params)
61 : KwPlugin(parent, "bible",
62 i18n("Bible"),
63 i18n("The bible plugin allows for the navigation and display of "
64 "bible extracts from various sources."))
65 , m_managers()
66 , m_insertIntoPlaylistAction(0)
67 , m_showNowAction(0)
68 , m_docker(0)
69 , m_managerTabs(0)
70 , m_comboBook(0)
71 , m_comboChapter(0)
72 , m_editRange(0)
73 , m_textPassage(0)
75 Q_UNUSED(params);
76 setXMLFile("bible/kworship_bibleui.rc");
79 /// Destructor.
80 KwBiblePlugin::~KwBiblePlugin()
85 * Accessors
88 /// Get the current bible passage information.
89 bool KwBiblePlugin::resolvePassage(KwBibleManager** manager, KwBibleModule** module, KwBibleModule::Key* key, bool* usedSearch) const
91 // Get the current bible manager
92 int tab = m_managerTabs->currentIndex();
93 if (tab >= 0 && tab < m_managers.size())
95 const BibleManager& mgr = m_managers[tab];
96 *manager = mgr.manager;
98 // Is a bible selected?
99 int bibleInd = mgr.comboBibles->currentIndex();
100 QString bible;
101 KwBibleModule* mod = 0;
102 if (bibleInd >= 0)
104 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
105 mod = mgr.manager->module(bible);
107 if (0 != mod)
109 *module = mod;
110 // Is a book selected?
111 int bookIndex = m_comboBook->currentIndex();
112 int chapterIndex = -1;
113 if (bookIndex >= 0)
115 // Is a chapter selected?
116 chapterIndex = m_comboChapter->currentIndex()-1;
117 if (chapterIndex < 0)
119 return false;
123 bool valid;
124 KwBibleModule::Key relativeKey = mod->createKey(bookIndex, chapterIndex);
125 *key = mod->createKey(relativeKey, m_editRange->text(), &valid);
127 if (0 != usedSearch)
129 *usedSearch = true;
131 return valid;
134 if (0 != usedSearch)
136 *usedSearch = false;
138 return false;
142 * Private slots
145 /// Fired when the connect to bible button is pressed.
146 void KwBiblePlugin::slotConnect()
148 // Get the current bible manager
149 int tab = m_managerTabs->currentIndex();
150 Q_ASSERT(tab >= 0 && tab < m_managers.size());
151 BibleManager* mgr = &m_managers[tab];
153 // This will force the connection
154 fillBiblesList(mgr);
155 if (mgr->comboBibles->count() == 0)
157 KMessageBox::information(m_docker, i18n("No bibles found"));
159 else
161 mgr->comboBibles->setEnabled(true);
165 /// Fired when the bible is changed.
166 void KwBiblePlugin::slotBibleChanged()
168 // Get the current bible manager
169 int tab = m_managerTabs->currentIndex();
170 if (tab >= 0 && tab < m_managers.size())
172 BibleManager& mgr = m_managers[tab];
173 bool enabled = mgr.comboBibles->isEnabled();
175 // Is a bible selected?
176 int bibleInd = mgr.comboBibles->currentIndex();
177 QString bible;
178 KwBibleModule* module = 0;
179 if (bibleInd >= 0)
181 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
182 module = mgr.manager->module(bible);
185 // Update the list of books
186 QString book = m_comboBook->currentText();
187 int chapter = m_comboChapter->currentIndex()-1;
188 m_comboBook->clear();
189 if (0 != module)
191 QStringList bookNames = module->books();
192 for (int i = 0; i < bookNames.size(); ++i)
194 const QString& bookName = bookNames[i];
195 m_comboBook->addItem(bookName, QVariant(i));
197 int index = m_comboBook->findText(book);
198 bool canPreserveBook = (index >= 0);
199 if (!canPreserveBook && bookNames.size() > 0)
201 index = 0;
203 m_comboBook->setCurrentIndex(index);
204 if (canPreserveBook && chapter >= 0)
206 // If we can restore book, also restore chapter
207 m_comboChapter->setCurrentIndex(chapter+1);
210 else
212 enabled = false;
215 m_comboBook->setEnabled(enabled);
216 m_comboChapter->setEnabled(enabled);
217 m_editRange->setEnabled(enabled);
221 /// Fired when the bible book is changed.
222 void KwBiblePlugin::slotBookChanged()
224 m_comboChapter->clear();
225 m_comboChapter->addItem(QString(), QVariant(-1));
227 // Get the current bible manager
228 int tab = m_managerTabs->currentIndex();
229 if (tab >= 0 && tab < m_managers.size())
231 BibleManager& mgr = m_managers[tab];
233 // Is a bible selected?
234 int bibleInd = mgr.comboBibles->currentIndex();
235 QString bible;
236 KwBibleModule* module = 0;
237 if (bibleInd >= 0)
239 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
240 module = mgr.manager->module(bible);
242 if (0 != module)
244 // Is a book selected?
245 int index = m_comboBook->currentIndex();
246 if (index >= 0)
248 int bookIndex = m_comboBook->itemData(index).toInt();
249 int numChapters = module->numChapters(bookIndex);
250 for (int i = 0; i < numChapters; ++i)
252 m_comboChapter->addItem(QString("%1").arg(i+1), QVariant(i));
257 slotVerseRange();
260 /// Fired when the bible text needs to be retrieved.
261 void KwBiblePlugin::slotVerseRange()
263 KwBibleManager* manager;
264 KwBibleModule* module;
265 KwBibleModule::Key key;
266 bool usedSearch;
267 bool success = resolvePassage(&manager, &module, &key, &usedSearch);
268 if (usedSearch)
270 m_comboBook->setCurrentIndex(key.start.book);
271 m_comboChapter->setCurrentIndex(key.start.chapter+1);
273 KwBiblePassage passage;
274 module->fillPassage(key, &passage);
275 m_textPassage->document()->setHtml(passage.renderedText());
277 m_insertIntoPlaylistAction->setEnabled(true);
278 m_showNowAction->setEnabled(true);
280 // Update color of search box
281 static QPalette p = m_editRange->palette();
282 QPalette changedPal = p;
283 if (!success)
285 changedPal.setColor( QPalette::Normal, QPalette::Base, QColor(255, 127, 127) );
287 m_editRange->setPalette(changedPal);
289 return;
291 m_textPassage->document()->setPlainText(QString());
292 m_insertIntoPlaylistAction->setEnabled(false);
293 m_showNowAction->setEnabled(false);
296 /// Fired by the insert into playlist action.
297 void KwBiblePlugin::slotInsertIntoPlaylist()
299 KwBibleManager* manager;
300 KwBibleModule* module;
301 KwBibleModule::Key key;
302 bool success = resolvePassage(&manager, &module, &key);
304 if (success)
306 KwBiblePlaylistItem* item = new KwBiblePlaylistItem();
307 module->fillPassage(key, &item->passage());
308 KwPlaylistModel* model = KwApplication::self()->mainWindow()->playlistModel();
309 model->addItem(QModelIndex(), item);
313 /// Fired by the show now action.
314 void KwBiblePlugin::slotShowNow()
319 * Loading and unloading virtual interface
322 #include <QtDebug>
323 void KwBiblePlugin::_load()
325 // Construct the bible managers and put into a list.
326 // Have the local bible managers first in the list.
327 KService::List offers = KServiceTypeTrader::self()->query("KWorship/Bible");
328 QList<KwBibleManager*> managers;
329 QList<KwBibleManager*> remoteManagers;
330 foreach (KService::Ptr service, offers)
332 qDebug() << " Found KWorship bible backend: " << service->desktopEntryName();
333 QString err;
334 KwBibleManager* backend = service->createInstance<KwBibleManager>(this, QVariantList(), &err);
335 if (backend)
337 qDebug() << " Loaded successfully";
338 if (backend->isRemote())
340 remoteManagers += backend;
342 else
344 managers += backend;
347 else
349 qDebug() << " Could not be loaded: " << err;
352 managers += remoteManagers;
354 // Set up the docker
355 m_docker = new QDockWidget(i18n("Bible"));
356 m_docker->setObjectName("dockBible");
357 QWidget* mainDockWidget = new QWidget(/*m_docker*/);
358 QVBoxLayout* dockLayout = new QVBoxLayout(mainDockWidget);
359 m_docker->setWidget(mainDockWidget);
361 // The tab bar of bible managers
362 m_managerTabs = new QTabWidget(m_docker);
363 m_managerTabs->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
364 connect(m_managerTabs, SIGNAL(currentChanged(int)),
365 this, SLOT(slotBibleChanged()));
366 dockLayout->addWidget(m_managerTabs);
368 // Bible navigation combo boxes
369 QWidget* bibleNavigationWidget = new QWidget(mainDockWidget);
370 QHBoxLayout* bibleNavigationLayout = new QHBoxLayout(bibleNavigationWidget);
371 dockLayout->addWidget(bibleNavigationWidget);
373 m_comboBook = new QComboBox(bibleNavigationWidget);
374 m_comboChapter = new QComboBox(bibleNavigationWidget);
375 bibleNavigationLayout->addWidget(m_comboBook);
376 bibleNavigationLayout->addWidget(m_comboChapter);
378 // Verse range text box and label
379 QWidget* verseRangeWidget = new QWidget(mainDockWidget);
380 QHBoxLayout* verseRangeLayout = new QHBoxLayout(verseRangeWidget);
381 dockLayout->addWidget(verseRangeWidget);
383 QLabel* labelRange = new QLabel(i18n("&Verses"), verseRangeWidget);
384 m_editRange = new QLineEdit(verseRangeWidget);
385 labelRange->setBuddy(m_editRange);
386 verseRangeLayout->addWidget(labelRange);
387 verseRangeLayout->addWidget(m_editRange);
389 m_textPassage = new QTextEdit(mainDockWidget);
390 m_textPassage->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
391 dockLayout->addWidget(m_textPassage);
393 // Toolbar
394 QToolBar* bibleToolBar = new QToolBar("bibleToolBar");
395 bibleToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
396 dockLayout->addWidget(bibleToolBar);
398 m_insertIntoPlaylistAction = new KAction(KIcon("player_playlist"), i18n("Insert Into Playlist"), bibleToolBar);
399 m_insertIntoPlaylistAction->setEnabled(false);
400 connect(m_insertIntoPlaylistAction, SIGNAL(triggered(bool)),
401 this, SLOT(slotInsertIntoPlaylist()));
402 bibleToolBar->addAction(m_insertIntoPlaylistAction);
404 m_showNowAction = new KAction(KIcon("player_playlist"), i18n("Show Now"), bibleToolBar);
405 m_showNowAction->setEnabled(false);
406 connect(m_showNowAction, SIGNAL(triggered(bool)),
407 this, SLOT(slotShowNow()));
408 bibleToolBar->addAction(m_showNowAction);
410 // Fill out tabs and manager data
411 foreach (KwBibleManager* manager, managers)
413 QString name = manager->name();
414 BibleManager mgr;
415 mgr.manager = manager;
417 QWidget* tabWidget = new QWidget();
418 QHBoxLayout* layout = new QHBoxLayout(tabWidget);
420 mgr.comboBibles = new QComboBox();
421 connect(mgr.comboBibles, SIGNAL(currentIndexChanged(int)),
422 this, SLOT(slotBibleChanged()));
424 // Only fill the module list if the manager is local
425 // Otherwise we should wait until the user requests it
426 if (manager->isRemote())
428 mgr.comboBibles->setEnabled(false);
430 mgr.toolBar = new QToolBar();
431 KAction* connectToBible = new KAction(KIcon("network-connect"), i18n("Connect to %1", name), mgr.toolBar);
432 connect(connectToBible, SIGNAL(triggered(bool)),
433 this, SLOT(slotConnect()));
434 mgr.toolBar->addAction(connectToBible);
435 layout->addWidget(mgr.toolBar);
437 // Toolbar should be as small as possible
438 QSizePolicy policy;
440 policy = mgr.toolBar->sizePolicy();
441 policy.setHorizontalStretch(1);
442 mgr.toolBar->setSizePolicy(policy);
444 policy = mgr.comboBibles->sizePolicy();
445 policy.setHorizontalStretch(10);
446 mgr.comboBibles->setSizePolicy(policy);
448 else
450 mgr.toolBar = 0;
451 fillBiblesList(&mgr);
454 layout->addWidget(mgr.comboBibles);
456 m_managerTabs->addTab(tabWidget, name);
457 m_managers.push_back(mgr);
459 // Ensure widgets are apropriately modified
460 connect(m_comboBook, SIGNAL(currentIndexChanged(int)),
461 this, SLOT(slotBookChanged()));
462 connect(m_comboChapter, SIGNAL(currentIndexChanged(int)),
463 this, SLOT(slotVerseRange()));
464 connect(m_editRange, SIGNAL(textChanged(const QString&)),
465 this, SLOT(slotVerseRange()));
467 KwApplication::self()->mainWindow()->addDockWidget(Qt::LeftDockWidgetArea, m_docker);
469 slotBibleChanged();
472 void KwBiblePlugin::_unload()
474 // Clean up the bible managers.
475 /// @todo IMPLEMENT!!!
476 for (int i = 0; i < m_managers.size(); ++i)
478 BibleManager& mgr = m_managers[i];
479 delete mgr.manager;
480 mgr.manager = 0;
482 delete m_docker;
486 * Private functions
489 /// Fill up the bibles list for a manager.
490 void KwBiblePlugin::fillBiblesList(BibleManager* mgr)
492 QStringList languages = mgr->manager->languages();
493 mgr->comboBibles->clear();
494 mgr->comboBibles->addItem(i18n("-- select a translation --"));
495 foreach (QString language, languages)
497 QStringList modules = mgr->manager->moduleNamesInLanguage(language);
498 if (!modules.isEmpty())
500 mgr->comboBibles->addItem(language, QVariant(modules.first()));
501 foreach (QString module, modules)
503 mgr->comboBibles->addItem(" " + module, QVariant(module));