summaryrefslogtreecommitdiff
path: root/src/frontend/bookmarks/btbookmarkloader.cpp
blob: 63e0797de6e80e8d2caabdf8401c4a80970f61ab (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2014 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/bookmarks/btbookmarkloader.h"

#include <QDomElement>
#include <QDomNode>
#include <QDomDocument>
#include <QFile>
#include <QIODevice>
#include <QTextCodec>
#include <QTextStream>
#include <QTreeWidgetItem>
#include "backend/drivers/cswordmoduleinfo.h"
#include "frontend/bookmarks/btbookmarkitem.h"
#include "frontend/bookmarks/btbookmarkfolder.h"
#include "util/tool.h"


#define CURRENT_SYNTAX_VERSION 1

QList<QTreeWidgetItem*> BtBookmarkLoader::loadTree(QString fileName) {
    QList<QTreeWidgetItem*> itemList;

    QDomDocument doc;
    doc.setContent(loadXmlFromFile(fileName));

    //bookmarkfolder::loadBookmarksFromXML()

    QDomElement document = doc.documentElement();
    if ( document.tagName() != "SwordBookmarks" ) {
        qWarning("Not a BibleTime Bookmark XML file");
        return QList<QTreeWidgetItem*>();
    }

    QDomElement child = document.firstChild().toElement();

    while ( !child.isNull() && child.parentNode() == document) {
        QTreeWidgetItem* i = handleXmlElement(child, 0);
        itemList.append(i);
        if (!child.nextSibling().isNull()) {
            child = child.nextSibling().toElement();
        }
        else {
            child = QDomElement(); //null
        }

    }

    return itemList;
}

QTreeWidgetItem* BtBookmarkLoader::handleXmlElement(QDomElement& element, QTreeWidgetItem* parent) {
    QTreeWidgetItem* newItem = 0;
    if (element.tagName() == "Folder") {
        BtBookmarkFolder* newFolder = new BtBookmarkFolder(QString::null, parent);
        if (element.hasAttribute("caption")) {
            newFolder->setText(0, element.attribute("caption"));
        }
        QDomNodeList childList = element.childNodes();
#if QT_VERSION < 0x050000
        for (unsigned int i = 0; i < childList.length(); i++) {
#else
        for (int i = 0; i < childList.length(); i++) {
#endif
            QDomElement newElement = childList.at(i).toElement();
            QTreeWidgetItem* newChildItem = handleXmlElement(newElement, newFolder);
            newFolder->addChild(newChildItem);
        }
        newFolder->update();
        newItem = newFolder;
    }
    else if (element.tagName() == "Bookmark") {
        BtBookmarkItem* newBookmarkItem = new BtBookmarkItem(parent);
        if (element.hasAttribute("modulename")) {
            //we use the name in all cases, even if the module isn't installed anymore
            newBookmarkItem->m_moduleName = element.attribute("modulename");
        }
        if (element.hasAttribute("key")) {
            newBookmarkItem->m_key = element.attribute("key");
        }
        if (element.hasAttribute("description")) {
            newBookmarkItem->m_description = element.attribute("description");
        }
        if (element.hasAttribute("title")) {
            newBookmarkItem->m_title = element.attribute("title");
        }
        newBookmarkItem->update();
        newItem = newBookmarkItem;
    }
    return newItem;
}


QString BtBookmarkLoader::loadXmlFromFile(QString fileName) {
    namespace DU = util::directory;

    if (fileName.isNull()) {
        fileName = DU::getUserBaseDir().absolutePath() + "/bookmarks.xml";
    }
    QFile file(fileName);
    if (!file.exists())
        return QString::null;

    QString xml;
    if (file.open(QIODevice::ReadOnly)) {
        QTextStream t;
        t.setAutoDetectUnicode(false);
        t.setCodec(QTextCodec::codecForName("UTF-8"));
        t.setDevice(&file);
        xml = t.readAll();
        file.close();
    }
    return xml;
}

void BtBookmarkLoader::saveTreeFromRootItem(QTreeWidgetItem* rootItem, QString fileName, bool forceOverwrite) {
    namespace DU = util::directory;

    Q_ASSERT(rootItem);
    if (fileName.isNull()) {
        fileName = DU::getUserBaseDir().absolutePath() + "/bookmarks.xml";
    }

    QDomDocument doc("DOC");
    doc.appendChild( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );

    QDomElement content = doc.createElement("SwordBookmarks");
    content.setAttribute("syntaxVersion", CURRENT_SYNTAX_VERSION);
    doc.appendChild(content);

    //append the XML nodes of all child items

    for (int i = 0; i < rootItem->childCount(); i++) {
        saveItem(rootItem->child(i), content);
    }
    util::tool::savePlainFile(fileName, doc.toString(), forceOverwrite, QTextCodec::codecForName("UTF-8"));

}

void BtBookmarkLoader::saveItem(QTreeWidgetItem* item, QDomElement& parentElement) {
    BtBookmarkFolder* folderItem = 0;
    BtBookmarkItem* bookmarkItem = 0;

    if ((folderItem = dynamic_cast<BtBookmarkFolder*>(item))) {
        QDomElement elem = parentElement.ownerDocument().createElement("Folder");
        elem.setAttribute("caption", folderItem->text(0));

        parentElement.appendChild(elem);

        for (int i = 0; i < folderItem->childCount(); i++) {
            saveItem(folderItem->child(i), elem);
        }
    }
    else if ((bookmarkItem = dynamic_cast<BtBookmarkItem*>(item))) {
        QDomElement elem = parentElement.ownerDocument().createElement("Bookmark");

        elem.setAttribute("key", bookmarkItem->englishKey());
        elem.setAttribute("description", bookmarkItem->description());
        elem.setAttribute("modulename", bookmarkItem->m_moduleName);
        elem.setAttribute("moduledescription", bookmarkItem->module() ? bookmarkItem->module()->config(CSwordModuleInfo::Description) : QString::null);
    if ( ! bookmarkItem->title().isEmpty()) {
      elem.setAttribute("title", bookmarkItem->m_title);
    }
        parentElement.appendChild(elem);
    }
}