summaryrefslogtreecommitdiff
path: root/src/backend/btmoduletreeitem.cpp
blob: d11ecbd13fa560ce114ce5435b8da618e84541a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "btmoduletreeitem.h"

#include "backend/drivers/cswordmoduleinfo.h"
#include "util/cpointers.h"
#include "backend/managers/cswordbackend.h"
#include "util/cresmgr.h"
#include "util/ctoolclass.h"

#include <QString>
#include <QList>

#include <QDebug>



//This ctor creates the root item and the tree.
BTModuleTreeItem::BTModuleTreeItem(QList<BTModuleTreeItem::Filter*>& filters, BTModuleTreeItem::Grouping grouping, QList<CSwordModuleInfo*>* modules)
        : m_moduleInfo(0),
        m_firstChild(0),
        m_next(0),
        m_type(BTModuleTreeItem::Root),
        m_category(CSwordModuleInfo::UnknownCategory) {
    if (modules) {
        m_originalModuleList = *modules;
    }
    else {
        m_originalModuleList = CPointers::backend()->moduleList();
    }
    //populate the tree with groups/modules
    create_tree(filters, grouping);
}

/**
* Private constructor which sets the members of the non-root item. This will be the first child of the
* parent, the previous firstChild will be the next sibling of this.
*/
BTModuleTreeItem::BTModuleTreeItem(BTModuleTreeItem* parentItem, const QString& text, BTModuleTreeItem::Type type, CSwordModuleInfo* info, CSwordModuleInfo::Category category)
        : m_moduleInfo(info),
        m_text(text),
        m_firstChild(0),
        m_next(0),
        m_type(type),
        m_category(category) {
    if (info) {
        m_text = info->name();
        m_type = BTModuleTreeItem::Module;
    }
    BTModuleTreeItem* tmp = parentItem->m_firstChild;
    parentItem->m_firstChild = this;
    if (tmp) this->m_next = tmp;
}


BTModuleTreeItem::~BTModuleTreeItem() {
    // this works recursively
    foreach(BTModuleTreeItem* i, children()) {
        delete i;
    }
}

QList<BTModuleTreeItem*> BTModuleTreeItem::children() const {
    //qDebug("BTModuleTreeItem::children");
    QList<BTModuleTreeItem*> childList;
    if (m_firstChild) {
        BTModuleTreeItem* child = m_firstChild;
        while (child) {
            //qDebug() << "child:" << child->text();
            childList.append(child);
            child = child->m_next;
        }
    }
    return childList;
}

//TODO
QString BTModuleTreeItem::iconName() const {
    if (m_type == Category) {
        switch ( m_category) {
            case CSwordModuleInfo::Bibles:
                return CResMgr::categories::bibles::icon;
                break;
            case CSwordModuleInfo::Commentaries:
                return CResMgr::categories::commentaries::icon;
                break;
            case CSwordModuleInfo::Books:
                return CResMgr::categories::books::icon;
                break;
            case CSwordModuleInfo::Cult:
                return CResMgr::categories::cults::icon;
                break;
            case CSwordModuleInfo::Images:
                return CResMgr::categories::images::icon;
                break;
            case CSwordModuleInfo::DailyDevotional:
                return CResMgr::categories::dailydevotional::icon;
                break;
            case CSwordModuleInfo::Lexicons:
                return CResMgr::categories::lexicons::icon;
                break;
            case CSwordModuleInfo::Glossary:
                return CResMgr::categories::glossary::icon;
                break;
            default:
                break;
        }
    }
    else if (m_type == Module) {
        return CToolClass::getIconNameForModule(m_moduleInfo);
    }
    else if (m_type == Language) {
        //TODO: don't hardcode here
        return "flag.svg";
    }

    return QString::null;
}


void BTModuleTreeItem::create_tree(QList<BTModuleTreeItem::Filter*>& filters, BTModuleTreeItem::Grouping grouping) {
    qDebug("BTModuleTreeItem::create_tree");
    static bool map_initialized = false;
    static QMap<CSwordModuleInfo::Category, QString> CategoryNamesMap;
    if (!map_initialized) {
        CategoryNamesMap.insert(CSwordModuleInfo::Commentaries, QObject::tr("Commentaries"));
        CategoryNamesMap.insert(CSwordModuleInfo::Cult, QObject::tr("Cults/Unorthodox"));
        CategoryNamesMap.insert(CSwordModuleInfo::Images, QObject::tr("Maps and Images"));
        CategoryNamesMap.insert(CSwordModuleInfo::DailyDevotional, QObject::tr("Daily Devotionals"));
        CategoryNamesMap.insert(CSwordModuleInfo::Lexicons, QObject::tr("Lexicons and Dictionaries"));
        CategoryNamesMap.insert(CSwordModuleInfo::Bibles, QObject::tr("Bibles"));
        CategoryNamesMap.insert(CSwordModuleInfo::Glossary, QObject::tr("Glossaries"));
        CategoryNamesMap.insert(CSwordModuleInfo::Books, QObject::tr("Books"));

        map_initialized = true;
    }

    //QList<CSwordModuleInfo*> originalInfoList = CPointers::backend()->moduleList();

    foreach (CSwordModuleInfo* info, m_originalModuleList) {
        bool included;
        included = true;
        foreach (BTModuleTreeItem::Filter* f, filters) {
            if (!f->filter(info)) {
                included = false;
                break;
            }
        }
        if (included) {
            //qDebug() << "a module will be included: " << info->name();

            BTModuleTreeItem* parentGroupForModule = this;
            BTModuleTreeItem* parentGroupForLanguage = this;
            BTModuleTreeItem* parentGroupForCategory = this;

            //the order of if(grouping...) clauses is important
            if (grouping == BTModuleTreeItem::LangMod || grouping == BTModuleTreeItem::LangCatMod) {
                BTModuleTreeItem* langItem = create_parent_item(parentGroupForLanguage, info->language()->translatedName(), BTModuleTreeItem::Language);

                if (grouping == BTModuleTreeItem::LangMod)
                    parentGroupForModule = langItem;
                else
                    parentGroupForCategory = langItem;
            }

            if (grouping == BTModuleTreeItem::CatMod || grouping == BTModuleTreeItem::CatLangMod) {
                BTModuleTreeItem* catItem = create_parent_item(parentGroupForCategory, CategoryNamesMap.value(info->category()), BTModuleTreeItem::Category, info->category());

                if (grouping == BTModuleTreeItem::CatMod)
                    parentGroupForModule = catItem;
                else
                    parentGroupForLanguage = catItem;
            }

            if (grouping == BTModuleTreeItem::CatLangMod) {
                // category is there already, create language and make it the parent for the module
                parentGroupForModule = create_parent_item(parentGroupForLanguage, info->language()->translatedName(), BTModuleTreeItem::Language);
            }

            if (grouping == BTModuleTreeItem::LangCatMod) {
                //language is there already, create category and make it the parent for the module
                parentGroupForModule = create_parent_item(parentGroupForCategory, CategoryNamesMap.value(info->category()), BTModuleTreeItem::Category, info->category());
            }

            // the parent group for module has been set above, now just add the module to it
            new BTModuleTreeItem(parentGroupForModule, QString::null, BTModuleTreeItem::Module, info);

        } // end: if (included)
    }

    // Finally sort the items
    sort_children(this);
}

BTModuleTreeItem* BTModuleTreeItem::create_parent_item(
    BTModuleTreeItem* parentGroup,
    const QString& itemText,
    BTModuleTreeItem::Type type,
    CSwordModuleInfo::Category category) {
    BTModuleTreeItem* item = 0;
    foreach(BTModuleTreeItem* it, parentGroup->children()) {
        if (it->text() == itemText) {
            item = it;
            break;
        }
    }
    if (!item)
        item = new BTModuleTreeItem(parentGroup, itemText, type, 0, category);

    return item;
}

void BTModuleTreeItem::sort_children(BTModuleTreeItem* parent) {
    //qDebug("BTModuleTreeItem::sort_children");

    // sort each child recursively depth-first
    foreach(BTModuleTreeItem* item, parent->children()) {
        sort_children(item);
    }

    QList<BTModuleTreeItem*> items = parent->children();
    if (items.size() > 0) {
        // Sort the list of the children according to each item's text
        qSort(items.begin(), items.end(), BTModuleTreeItem::localeAwareLessThan);
        //put the children back to tree in sorted order
        BTModuleTreeItem* first = items.at(0);
        BTModuleTreeItem* prev = first;
        foreach (BTModuleTreeItem* item2, items) {
            prev->m_next = item2;
            prev = item2;
        }
        prev->m_next = 0;
        parent->m_firstChild = first; // attach the partial tree to the parent
    }
}

bool BTModuleTreeItem::localeAwareLessThan(BTModuleTreeItem* first, BTModuleTreeItem* second) {
    static bool map_initialized = false;
    static QMap<QString, int> CategoryNameValueMap;
    if (!map_initialized) {
        //this is the sorting order for categories
        CategoryNameValueMap.insert(QObject::tr("Bibles"), 1);
        CategoryNameValueMap.insert(QObject::tr("Commentaries"), 2);
        CategoryNameValueMap.insert(QObject::tr("Books"), 3);
        CategoryNameValueMap.insert(QObject::tr("Lexicons and Dictionaries"), 4);
        CategoryNameValueMap.insert(QObject::tr("Glossaries"), 5);
        CategoryNameValueMap.insert(QObject::tr("Daily Devotionals"), 6);
        CategoryNameValueMap.insert(QObject::tr("Maps and Images"), 7);
        CategoryNameValueMap.insert(QObject::tr("Cults/Unorthodox"), 8);
        map_initialized = true;
    }

    //Categories are always in the same order, not alphabetically
    if (first->type() == BTModuleTreeItem::Category) {
        return (CategoryNameValueMap.value(first->text()) < CategoryNameValueMap.value(second->text()));
    }
    return (QString::localeAwareCompare(first->text(), second->text()) < 0 );
}