~vcs-imports/bibletime/trunk

« back to all changes in this revision

Viewing changes to bibletime/frontend/cmanageindiceswidget.cpp

  • Committer: mgruner
  • Date: 2007-05-08 15:51:07 UTC
  • Revision ID: vcs-imports@canonical.com-20070508155107-0rj7jdmm5ivf8685
-imported source and data files to new svn module
-this is where KDE4-based development will take place

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
10
 
 
11
 
 
12
#include "cmanageindiceswidget.h"
 
13
 
 
14
#include "cmoduleindexdialog.h"
 
15
 
 
16
#include "cbtconfig.h"
 
17
 
 
18
#include "util/ctoolclass.h"
 
19
#include "util/cresmgr.h"
 
20
#include "util/cpointers.h"
 
21
 
 
22
#include "backend/cswordmoduleinfo.h"
 
23
#include "backend/cswordbackend.h"
 
24
 
 
25
//Qt includes
 
26
#include <qlabel.h>
 
27
#include <qlayout.h>
 
28
#include <qframe.h>
 
29
#include <qpushbutton.h>
 
30
#include <qdir.h>
 
31
#include <qlistview.h>
 
32
#include <qfileinfo.h>
 
33
#include <qcheckbox.h>
 
34
 
 
35
//KDE includes
 
36
#include <klocale.h>
 
37
#include <klistview.h>
 
38
#include <kiconloader.h>
 
39
 
 
40
namespace BookshelfManager {
 
41
 
 
42
/** Constructor */
 
43
CManageIndicesWidget::CManageIndicesWidget(QWidget* parent, const char* name) :
 
44
        ManageIndicesForm(parent, name) {
 
45
        
 
46
        initView();
 
47
        populateModuleList();
 
48
};
 
49
 
 
50
CManageIndicesWidget::~CManageIndicesWidget()
 
51
{
 
52
        CBTConfig::set( CBTConfig::autoDeleteOrphanedIndices, m_autoDeleteOrphanedIndicesBox->isChecked() );
 
53
 
 
54
}
 
55
 
 
56
/** Initializes the look and feel of this page */
 
57
void CManageIndicesWidget::initView()
 
58
{
 
59
        // Set description label
 
60
        QVBoxLayout* box = new QVBoxLayout(m_labelFrame, 0, 0);
 
61
        QLabel* mainLabel = CToolClass::explanationLabel(m_labelFrame,
 
62
                i18n("Manage module search indices"),
 
63
                i18n("You can use the list below to create and/or delete search indices for your installed works."));
 
64
        box->addWidget(mainLabel);
 
65
 
 
66
        // configure the list view
 
67
        m_moduleList->addColumn(i18n("Module"));
 
68
        m_moduleList->addColumn(i18n("Index size"));
 
69
        m_moduleList->setRootIsDecorated(true);
 
70
        m_moduleList->setColumnWidth(0, 150);
 
71
        m_moduleList->setColumnAlignment(1, Qt::AlignRight);
 
72
        m_moduleList->setSorting( -1 );
 
73
 
 
74
        m_autoDeleteOrphanedIndicesBox->setChecked( CBTConfig::get( CBTConfig::autoDeleteOrphanedIndices ) );
 
75
 
 
76
        // icons for our buttons
 
77
        m_createIndicesButton->setIconSet(SmallIcon("folder_new", 16));
 
78
        m_deleteIndicesButton->setIconSet(SmallIcon("remove", 16));
 
79
 
 
80
        // connect our signals/slots
 
81
        connect(m_createIndicesButton, SIGNAL(clicked()), this, SLOT(createIndices()));
 
82
        connect(m_deleteIndicesButton, SIGNAL(clicked()), this, SLOT(deleteIndices()));
 
83
}
 
84
 
 
85
/** Populates the module list with installed modules and orphaned indices */
 
86
void CManageIndicesWidget::populateModuleList() {
 
87
        m_moduleList->clear();
 
88
                
 
89
        // populate installed modules
 
90
        m_modsWithIndices = new QCheckListItem(m_moduleList, i18n("Modules with indices"),
 
91
                QCheckListItem::CheckBoxController);
 
92
        m_modsWithIndices->setOpen(true);
 
93
 
 
94
        m_modsWithoutIndices = new QCheckListItem(m_moduleList, i18n("Modules without indices"),
 
95
                QCheckListItem::CheckBoxController);
 
96
        m_modsWithoutIndices->setOpen(true);
 
97
 
 
98
        ListCSwordModuleInfo& modules = CPointers::backend()->moduleList();
 
99
        ListCSwordModuleInfo::iterator end_it = modules.end();
 
100
        for (ListCSwordModuleInfo::iterator it = modules.begin(); it != end_it; ++it) {
 
101
                QCheckListItem* item = 0;
 
102
                
 
103
                if ((*it)->hasIndex()) {
 
104
                        item = new QCheckListItem(m_modsWithIndices, (*it)->name(),
 
105
                                QCheckListItem::CheckBox);
 
106
                        item->setText(1, QString("%1 ").arg((*it)->indexSize() / 1024) + i18n("KiB"));
 
107
                }
 
108
                else {
 
109
                        item = new QCheckListItem(m_modsWithoutIndices, (*it)->name(),
 
110
                                QCheckListItem::CheckBox);
 
111
                        item->setText(1, QString("0 ") + i18n("KiB"));
 
112
                }
 
113
        }
 
114
}
 
115
 
 
116
/** Creates indices for selected modules if no index currently exists */
 
117
void CManageIndicesWidget::createIndices()
 
118
{
 
119
        QCheckListItem* top = m_modsWithoutIndices;
 
120
        bool indicesCreated = false;
 
121
        QCheckListItem* item = (QCheckListItem*)top->firstChild();
 
122
 
 
123
        ListCSwordModuleInfo moduleList;
 
124
        while (item) {
 
125
                if (item->isOn()) {
 
126
                        CSwordModuleInfo* module =
 
127
                                CPointers::backend()->findModuleByName(item->text().utf8());
 
128
 
 
129
                        
 
130
                        if (module) {
 
131
                                moduleList.append( module );
 
132
                                indicesCreated = true;
 
133
                        }
 
134
                }
 
135
                item = (QCheckListItem*)item->nextSibling();
 
136
        }
 
137
 
 
138
        //Shows the progress dialog
 
139
        if (indicesCreated) {
 
140
                CModuleIndexDialog::getInstance()->indexAllModules( moduleList );
 
141
                populateModuleList();
 
142
        }
 
143
}
 
144
 
 
145
/** Deletes indices for selected modules and selected orphans */
 
146
void CManageIndicesWidget::deleteIndices()
 
147
{
 
148
        // delete installed module indices
 
149
        QCheckListItem* top = m_modsWithIndices;
 
150
        bool indicesDeleted = false;
 
151
        QCheckListItem* item = (QCheckListItem*)top->firstChild();
 
152
        while (item) {
 
153
                if (item->isOn()) {
 
154
                        CSwordModuleInfo* module =
 
155
                                CPointers::backend()->findModuleByName(item->text().utf8());
 
156
                        if (module) {
 
157
                                CSwordModuleInfo::deleteIndexForModule( module->name() );
 
158
                                indicesDeleted = true;
 
159
                        }
 
160
                }
 
161
                item = (QCheckListItem*)item->nextSibling();
 
162
        }
 
163
 
 
164
        // repopulate the list if an action was taken
 
165
        if (indicesDeleted) {
 
166
                populateModuleList();
 
167
        }
 
168
}
 
169
 
 
170
void CManageIndicesWidget::deleteOrphanedIndices()
 
171
{
 
172
        QDir dir(CSwordModuleInfo::getGlobalBaseIndexLocation());
 
173
        dir.setFilter(QDir::Dirs);
 
174
        CSwordModuleInfo* module;
 
175
        
 
176
        for (unsigned int i = 0; i < dir.count(); i++) {
 
177
                if (dir[i] != "." && dir[i] != "..") {
 
178
                        if (module = CPointers::backend()->findModuleByName( dir[i] ) ) { //mod exists
 
179
                                if (!module->hasIndex()){ //index files found, but wrong version etc.
 
180
                                        CSwordModuleInfo::deleteIndexForModule( dir[i] );
 
181
                                }
 
182
                        }
 
183
                        else{ //no module exists
 
184
                                if (CBTConfig::get( CBTConfig::autoDeleteOrphanedIndices ) ){
 
185
                                        CSwordModuleInfo::deleteIndexForModule( dir[i] );
 
186
                                }
 
187
                        }
 
188
                }
 
189
        }
 
190
}
 
191
 
 
192
 
 
193
}