Bibletime – a bible study tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmanageindiceswidget.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*********
  2. *
  3. * This file is part of BibleTime's source code, http://www.bibletime.info/.
  4. *
  5. * Copyright 1999-2006 by the BibleTime developers.
  6. * The BibleTime source code is licensed under the GNU General Public License version 2.0.
  7. *
  8. **********/
  9. #include "cmanageindiceswidget.h"
  10. #include "cmoduleindexdialog.h"
  11. #include "cbtconfig.h"
  12. #include "util/ctoolclass.h"
  13. #include "util/cresmgr.h"
  14. #include "util/cpointers.h"
  15. #include "backend/cswordmoduleinfo.h"
  16. #include "backend/cswordbackend.h"
  17. //TQt includes
  18. #include <tqlabel.h>
  19. #include <tqlayout.h>
  20. #include <tqframe.h>
  21. #include <tqpushbutton.h>
  22. #include <tqdir.h>
  23. #include <tqlistview.h>
  24. #include <tqfileinfo.h>
  25. #include <tqcheckbox.h>
  26. //KDE includes
  27. #include <tdelocale.h>
  28. #include <tdelistview.h>
  29. #include <kiconloader.h>
  30. namespace BookshelfManager {
  31. /** Constructor */
  32. CManageIndicesWidget::CManageIndicesWidget(TQWidget* parent, const char* name) :
  33. ManageIndicesForm(parent, name) {
  34. initView();
  35. populateModuleList();
  36. };
  37. CManageIndicesWidget::~CManageIndicesWidget()
  38. {
  39. CBTConfig::set( CBTConfig::autoDeleteOrphanedIndices, m_autoDeleteOrphanedIndicesBox->isChecked() );
  40. }
  41. /** Initializes the look and feel of this page */
  42. void CManageIndicesWidget::initView()
  43. {
  44. // Set description label
  45. TQVBoxLayout* box = new TQVBoxLayout(m_labelFrame, 0, 0);
  46. TQLabel* mainLabel = CToolClass::explanationLabel(m_labelFrame,
  47. i18n("Manage module search indices"),
  48. i18n("You can use the list below to create and/or delete search indices for your installed works."));
  49. box->addWidget(mainLabel);
  50. // configure the list view
  51. m_moduleList->addColumn(i18n("Module"));
  52. m_moduleList->addColumn(i18n("Index size"));
  53. m_moduleList->setRootIsDecorated(true);
  54. m_moduleList->setColumnWidth(0, 150);
  55. m_moduleList->setColumnAlignment(1, TQt::AlignRight);
  56. m_moduleList->setSorting( -1 );
  57. m_autoDeleteOrphanedIndicesBox->setChecked( CBTConfig::get( CBTConfig::autoDeleteOrphanedIndices ) );
  58. // icons for our buttons
  59. m_createIndicesButton->setIconSet(SmallIcon("folder-new", 16));
  60. m_deleteIndicesButton->setIconSet(SmallIcon("remove", 16));
  61. // connect our signals/slots
  62. connect(m_createIndicesButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(createIndices()));
  63. connect(m_deleteIndicesButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteIndices()));
  64. }
  65. /** Populates the module list with installed modules and orphaned indices */
  66. void CManageIndicesWidget::populateModuleList() {
  67. m_moduleList->clear();
  68. // populate installed modules
  69. m_modsWithIndices = new TQCheckListItem(m_moduleList, i18n("Modules with indices"),
  70. TQCheckListItem::CheckBoxController);
  71. m_modsWithIndices->setOpen(true);
  72. m_modsWithoutIndices = new TQCheckListItem(m_moduleList, i18n("Modules without indices"),
  73. TQCheckListItem::CheckBoxController);
  74. m_modsWithoutIndices->setOpen(true);
  75. ListCSwordModuleInfo& modules = CPointers::backend()->moduleList();
  76. ListCSwordModuleInfo::iterator end_it = modules.end();
  77. for (ListCSwordModuleInfo::iterator it = modules.begin(); it != end_it; ++it) {
  78. TQCheckListItem* item = 0;
  79. if ((*it)->hasIndex()) {
  80. item = new TQCheckListItem(m_modsWithIndices, (*it)->name(),
  81. TQCheckListItem::CheckBox);
  82. item->setText(1, TQString("%1 ").arg((*it)->indexSize() / 1024) + i18n("KiB"));
  83. }
  84. else {
  85. item = new TQCheckListItem(m_modsWithoutIndices, (*it)->name(),
  86. TQCheckListItem::CheckBox);
  87. item->setText(1, TQString("0 ") + i18n("KiB"));
  88. }
  89. }
  90. }
  91. /** Creates indices for selected modules if no index currently exists */
  92. void CManageIndicesWidget::createIndices()
  93. {
  94. TQCheckListItem* top = m_modsWithoutIndices;
  95. bool indicesCreated = false;
  96. TQCheckListItem* item = (TQCheckListItem*)top->firstChild();
  97. ListCSwordModuleInfo moduleList;
  98. while (item) {
  99. if (item->isOn()) {
  100. CSwordModuleInfo* module =
  101. CPointers::backend()->findModuleByName(item->text().utf8());
  102. if (module) {
  103. moduleList.append( module );
  104. indicesCreated = true;
  105. }
  106. }
  107. item = (TQCheckListItem*)item->nextSibling();
  108. }
  109. //Shows the progress dialog
  110. if (indicesCreated) {
  111. CModuleIndexDialog::getInstance()->indexAllModules( moduleList );
  112. populateModuleList();
  113. }
  114. }
  115. /** Deletes indices for selected modules and selected orphans */
  116. void CManageIndicesWidget::deleteIndices()
  117. {
  118. // delete installed module indices
  119. TQCheckListItem* top = m_modsWithIndices;
  120. bool indicesDeleted = false;
  121. TQCheckListItem* item = (TQCheckListItem*)top->firstChild();
  122. while (item) {
  123. if (item->isOn()) {
  124. CSwordModuleInfo* module =
  125. CPointers::backend()->findModuleByName(item->text().utf8());
  126. if (module) {
  127. CSwordModuleInfo::deleteIndexForModule( module->name() );
  128. indicesDeleted = true;
  129. }
  130. }
  131. item = (TQCheckListItem*)item->nextSibling();
  132. }
  133. // repopulate the list if an action was taken
  134. if (indicesDeleted) {
  135. populateModuleList();
  136. }
  137. }
  138. void CManageIndicesWidget::deleteOrphanedIndices()
  139. {
  140. TQDir dir(CSwordModuleInfo::getGlobalBaseIndexLocation());
  141. dir.setFilter(TQDir::Dirs);
  142. CSwordModuleInfo* module;
  143. for (unsigned int i = 0; i < dir.count(); i++) {
  144. if (dir[i] != "." && dir[i] != "..") {
  145. if (module = CPointers::backend()->findModuleByName( dir[i] ) ) { //mod exists
  146. if (!module->hasIndex()){ //index files found, but wrong version etc.
  147. CSwordModuleInfo::deleteIndexForModule( dir[i] );
  148. }
  149. }
  150. else{ //no module exists
  151. if (CBTConfig::get( CBTConfig::autoDeleteOrphanedIndices ) ){
  152. CSwordModuleInfo::deleteIndexForModule( dir[i] );
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. #include "cmanageindiceswidget.moc"