Made plugins use KParts and so are able to load extra menus etc
[kworship.git] / kworship / bible / KwBiblePlugin.cpp
blobe0426695e061e233de9e29b31b7403f1e69eee89
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 setXMLFile("bible/kworship_bibleui.rc");
78 /// Destructor.
79 KwBiblePlugin::~KwBiblePlugin()
84 * Accessors
87 /// Get the current bible passage information.
88 bool KwBiblePlugin::resolvePassage(KwBibleManager** manager, KwBibleModule** module, KwBibleModule::Key* key, bool* usedSearch) const
90 // Get the current bible manager
91 int tab = m_managerTabs->currentIndex();
92 if (tab >= 0 && tab < m_managers.size())
94 const BibleManager& mgr = m_managers[tab];
95 *manager = mgr.manager;
97 // Is a bible selected?
98 int bibleInd = mgr.comboBibles->currentIndex();
99 QString bible;
100 KwBibleModule* mod = 0;
101 if (bibleInd >= 0)
103 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
104 mod = mgr.manager->module(bible);
106 if (0 != mod)
108 *module = mod;
109 // Is a book selected?
110 int bookIndex = m_comboBook->currentIndex();
111 int chapterIndex = -1;
112 if (bookIndex >= 0)
114 // Is a chapter selected?
115 chapterIndex = m_comboChapter->currentIndex();
118 bool valid;
119 KwBibleModule::Key relativeKey = mod->createKey(bookIndex, chapterIndex);
120 *key = mod->createKey(relativeKey, m_editRange->text(), &valid);
122 if (0 != usedSearch)
124 *usedSearch = true;
126 return valid;
129 if (0 != usedSearch)
131 *usedSearch = false;
133 return false;
137 * Private slots
140 /// Fired when the connect to bible button is pressed.
141 void KwBiblePlugin::slotConnect()
143 // Get the current bible manager
144 int tab = m_managerTabs->currentIndex();
145 Q_ASSERT(tab >= 0 && tab < m_managers.size());
146 BibleManager* mgr = &m_managers[tab];
148 // This will force the connection
149 fillBiblesList(mgr);
150 if (mgr->comboBibles->count() == 0)
152 KMessageBox::information(m_docker, i18n("No bibles found"));
154 else
156 mgr->comboBibles->setEnabled(true);
160 /// Fired when the bible is changed.
161 void KwBiblePlugin::slotBibleChanged()
163 // Get the current bible manager
164 int tab = m_managerTabs->currentIndex();
165 if (tab >= 0 && tab < m_managers.size())
167 BibleManager& mgr = m_managers[tab];
168 bool enabled = mgr.comboBibles->isEnabled();
170 // Is a bible selected?
171 int bibleInd = mgr.comboBibles->currentIndex();
172 QString bible;
173 KwBibleModule* module = 0;
174 if (bibleInd >= 0)
176 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
177 module = mgr.manager->module(bible);
180 // Update the list of books
181 QString book = m_comboBook->currentText();
182 int chapter = m_comboChapter->currentIndex();
183 m_comboBook->clear();
184 if (0 != module)
186 QStringList bookNames = module->books();
187 for (int i = 0; i < bookNames.size(); ++i)
189 const QString& bookName = bookNames[i];
190 m_comboBook->addItem(bookName, QVariant(i));
192 int index = m_comboBook->findText(book);
193 bool canPreserveBook = (index >= 0);
194 if (!canPreserveBook && bookNames.size() > 0)
196 index = 0;
198 m_comboBook->setCurrentIndex(index);
199 if (canPreserveBook && chapter >= 0)
201 // If we can restore book, also restore chapter
202 m_comboChapter->setCurrentIndex(chapter);
205 else
207 enabled = false;
210 m_comboBook->setEnabled(enabled);
211 m_comboChapter->setEnabled(enabled);
212 m_editRange->setEnabled(enabled);
216 /// Fired when the bible book is changed.
217 void KwBiblePlugin::slotBookChanged()
219 m_comboChapter->clear();
221 // Get the current bible manager
222 int tab = m_managerTabs->currentIndex();
223 if (tab >= 0 && tab < m_managers.size())
225 BibleManager& mgr = m_managers[tab];
227 // Is a bible selected?
228 int bibleInd = mgr.comboBibles->currentIndex();
229 QString bible;
230 KwBibleModule* module = 0;
231 if (bibleInd >= 0)
233 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
234 module = mgr.manager->module(bible);
236 if (0 != module)
238 // Is a book selected?
239 int index = m_comboBook->currentIndex();
240 if (index >= 0)
242 int bookIndex = m_comboBook->itemData(index).toInt();
243 int numChapters = module->numChapters(bookIndex);
244 for (int i = 0; i < numChapters; ++i)
246 m_comboChapter->addItem(QString("%1").arg(i+1), QVariant(i));
251 slotVerseRange();
254 /// Fired when the bible text needs to be retrieved.
255 void KwBiblePlugin::slotVerseRange()
257 KwBibleManager* manager;
258 KwBibleModule* module;
259 KwBibleModule::Key key;
260 bool usedSearch;
261 bool success = resolvePassage(&manager, &module, &key, &usedSearch);
262 if (usedSearch)
264 m_comboBook->setCurrentIndex(key.start.book);
265 m_comboChapter->setCurrentIndex(key.start.chapter);
267 KwBiblePassage passage;
268 module->fillPassage(key, &passage);
269 m_textPassage->document()->setHtml(passage.renderedText());
271 m_insertIntoPlaylistAction->setEnabled(true);
272 m_showNowAction->setEnabled(true);
274 // Update color of search box
275 static QPalette p = m_editRange->palette();
276 QPalette changedPal = p;
277 if (!success)
279 changedPal.setColor( QPalette::Normal, QPalette::Base, QColor(255, 127, 127) );
281 m_editRange->setPalette(changedPal);
283 return;
285 m_textPassage->document()->setPlainText(QString());
286 m_insertIntoPlaylistAction->setEnabled(false);
287 m_showNowAction->setEnabled(false);
290 /// Fired by the insert into playlist action.
291 void KwBiblePlugin::slotInsertIntoPlaylist()
293 KwBibleManager* manager;
294 KwBibleModule* module;
295 KwBibleModule::Key key;
296 bool success = resolvePassage(&manager, &module, &key);
298 if (success)
300 KwBiblePlaylistItem* item = new KwBiblePlaylistItem(module, key);
301 KwPlaylistModel* model = KwApplication::self()->mainWindow()->playlistModel();
302 model->addItem(QModelIndex(), item);
306 /// Fired by the show now action.
307 void KwBiblePlugin::slotShowNow()
312 * Loading and unloading virtual interface
315 #include <QtDebug>
316 void KwBiblePlugin::_load()
318 // Construct the bible managers and put into a list.
319 // Have the local bible managers first in the list.
320 KService::List offers = KServiceTypeTrader::self()->query("KWorship/Bible");
321 QList<KwBibleManager*> managers;
322 QList<KwBibleManager*> remoteManagers;
323 foreach (KService::Ptr service, offers)
325 qDebug() << " Found KWorship bible backend: " << service->desktopEntryName();
326 QString err;
327 KwBibleManager* backend = service->createInstance<KwBibleManager>(this, QVariantList(), &err);
328 if (backend)
330 qDebug() << " Loaded successfully";
331 if (backend->isRemote())
333 remoteManagers += backend;
335 else
337 managers += backend;
340 else
342 qDebug() << " Could not be loaded: " << err;
345 managers += remoteManagers;
347 // Set up the docker
348 m_docker = new QDockWidget(i18n("Bible"));
349 m_docker->setObjectName("dockBible");
350 QWidget* mainDockWidget = new QWidget(/*m_docker*/);
351 QVBoxLayout* dockLayout = new QVBoxLayout(mainDockWidget);
352 m_docker->setWidget(mainDockWidget);
354 // The tab bar of bible managers
355 m_managerTabs = new QTabWidget(m_docker);
356 m_managerTabs->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
357 connect(m_managerTabs, SIGNAL(currentChanged(int)),
358 this, SLOT(slotBibleChanged()));
359 dockLayout->addWidget(m_managerTabs);
361 // Bible navigation combo boxes
362 QWidget* bibleNavigationWidget = new QWidget(mainDockWidget);
363 QHBoxLayout* bibleNavigationLayout = new QHBoxLayout(bibleNavigationWidget);
364 dockLayout->addWidget(bibleNavigationWidget);
366 m_comboBook = new QComboBox(bibleNavigationWidget);
367 m_comboChapter = new QComboBox(bibleNavigationWidget);
368 bibleNavigationLayout->addWidget(m_comboBook);
369 bibleNavigationLayout->addWidget(m_comboChapter);
371 // Verse range text box and label
372 QWidget* verseRangeWidget = new QWidget(mainDockWidget);
373 QHBoxLayout* verseRangeLayout = new QHBoxLayout(verseRangeWidget);
374 dockLayout->addWidget(verseRangeWidget);
376 QLabel* labelRange = new QLabel(i18n("&Verses"), verseRangeWidget);
377 m_editRange = new QLineEdit(verseRangeWidget);
378 labelRange->setBuddy(m_editRange);
379 verseRangeLayout->addWidget(labelRange);
380 verseRangeLayout->addWidget(m_editRange);
382 m_textPassage = new QTextEdit(mainDockWidget);
383 m_textPassage->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
384 dockLayout->addWidget(m_textPassage);
386 // Toolbar
387 QToolBar* bibleToolBar = new QToolBar("bibleToolBar");
388 bibleToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
389 dockLayout->addWidget(bibleToolBar);
391 m_insertIntoPlaylistAction = new KAction(KIcon("player_playlist"), i18n("Insert Into Playlist"), bibleToolBar);
392 m_insertIntoPlaylistAction->setEnabled(false);
393 connect(m_insertIntoPlaylistAction, SIGNAL(triggered(bool)),
394 this, SLOT(slotInsertIntoPlaylist()));
395 bibleToolBar->addAction(m_insertIntoPlaylistAction);
397 m_showNowAction = new KAction(KIcon("player_playlist"), i18n("Show Now"), bibleToolBar);
398 m_showNowAction->setEnabled(false);
399 connect(m_showNowAction, SIGNAL(triggered(bool)),
400 this, SLOT(slotShowNow()));
401 bibleToolBar->addAction(m_showNowAction);
403 // Fill out tabs and manager data
404 foreach (KwBibleManager* manager, managers)
406 QString name = manager->name();
407 BibleManager mgr;
408 mgr.manager = manager;
410 QWidget* tabWidget = new QWidget();
411 QHBoxLayout* layout = new QHBoxLayout(tabWidget);
413 mgr.comboBibles = new QComboBox();
414 connect(mgr.comboBibles, SIGNAL(currentIndexChanged(int)),
415 this, SLOT(slotBibleChanged()));
417 // Only fill the module list if the manager is local
418 // Otherwise we should wait until the user requests it
419 if (manager->isRemote())
421 mgr.comboBibles->setEnabled(false);
423 mgr.toolBar = new QToolBar();
424 KAction* connectToBible = new KAction(KIcon("network-connect"), i18n("Connect to %1", name), mgr.toolBar);
425 connect(connectToBible, SIGNAL(triggered(bool)),
426 this, SLOT(slotConnect()));
427 mgr.toolBar->addAction(connectToBible);
428 layout->addWidget(mgr.toolBar);
430 // Toolbar should be as small as possible
431 QSizePolicy policy;
433 policy = mgr.toolBar->sizePolicy();
434 policy.setHorizontalStretch(1);
435 mgr.toolBar->setSizePolicy(policy);
437 policy = mgr.comboBibles->sizePolicy();
438 policy.setHorizontalStretch(10);
439 mgr.comboBibles->setSizePolicy(policy);
441 else
443 mgr.toolBar = 0;
444 fillBiblesList(&mgr);
447 layout->addWidget(mgr.comboBibles);
449 m_managerTabs->addTab(tabWidget, name);
450 m_managers.push_back(mgr);
452 // Ensure widgets are apropriately modified
453 connect(m_comboBook, SIGNAL(currentIndexChanged(int)),
454 this, SLOT(slotBookChanged()));
455 connect(m_comboChapter, SIGNAL(currentIndexChanged(int)),
456 this, SLOT(slotVerseRange()));
457 connect(m_editRange, SIGNAL(textChanged(const QString&)),
458 this, SLOT(slotVerseRange()));
460 KwApplication::self()->mainWindow()->addDockWidget(Qt::LeftDockWidgetArea, m_docker);
462 slotBibleChanged();
465 void KwBiblePlugin::_unload()
467 // Clean up the bible managers.
468 /// @todo IMPLEMENT!!!
469 for (int i = 0; i < m_managers.size(); ++i)
471 BibleManager& mgr = m_managers[i];
472 delete mgr.manager;
473 mgr.manager = 0;
475 delete m_docker;
479 * Private functions
482 /// Fill up the bibles list for a manager.
483 void KwBiblePlugin::fillBiblesList(BibleManager* mgr)
485 QStringList languages = mgr->manager->languages();
486 mgr->comboBibles->clear();
487 mgr->comboBibles->addItem(i18n("-- select a translation --"));
488 foreach (QString language, languages)
490 QStringList modules = mgr->manager->moduleNamesInLanguage(language);
491 if (!modules.isEmpty())
493 mgr->comboBibles->addItem(language, QVariant(modules.first()));
494 foreach (QString module, modules)
496 mgr->comboBibles->addItem(" " + module, QVariant(module));