aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/xbel.cpp
blob: 1be9c7e909d0a8e92cdf43d2f067cc23f1758200 (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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "xbel.h"
#include <QFile>

Xbel::Xbel(const QString &path)
{
    m_path = path;
}

bool Xbel::read(BookmarkItem *root)
{
    // if the path is empty, there is nothing to load
    if(m_path.isEmpty()) {
        return false;
    }

    QFile file(m_path);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        // file cannot be opened for reading
        return false;
    }

    QXmlStreamReader xmlReader(&file);

    if(xmlReader.readNextStartElement()) {
        if(!(xmlReader.name() == "xbel" && xmlReader.attributes().value("version") == "1.0")) {
            // invalid xbel
            qWarning("Error parsing %s", qUtf8Printable(m_path));
            return false;
        }

        readChildElements(xmlReader, root);
    }

    file.close();
    return true;
}

void Xbel::readChildElements(QXmlStreamReader &reader, BookmarkItem *parentItem)
{
    while(reader.readNextStartElement()) {
        if(reader.name() == "title") {
            parentItem->title = reader.readElementText();

        } else if(reader.name() == "folder") {
            BookmarkItem *item = new BookmarkItem(BookmarkItem::Folder, parentItem);
            item->folded = reader.attributes().value("folded") == QLatin1String("yes");
            readChildElements(reader, item);

        } else if(reader.name() == "bookmark") {
            BookmarkItem *item = new BookmarkItem(BookmarkItem::Bookmark, parentItem);
            item->href = reader.attributes().value("href").toString();
            readChildElements(reader, item);

        } else {
            reader.skipCurrentElement();
        }
    }
}

bool Xbel::write(BookmarkItem *root)
{

    // if the path is empty, there is nothing to save to
    if(m_path.isEmpty())
        return false;

    QFile file(m_path);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        // file cannot be opened for writing
        return false;
    }

    QXmlStreamWriter xmlWriter(&file);
    xmlWriter.setAutoFormatting(true);

    xmlWriter.writeStartDocument();
    xmlWriter.writeDTD("<!DOCTYPE xbel>");

    xmlWriter.writeStartElement("xbel");
    xmlWriter.writeAttribute("version", "1.0");

    writeChildElements(xmlWriter, root);

    xmlWriter.writeEndDocument();

    file.close();
    return true;
}

void Xbel::writeChildElements(QXmlStreamWriter &writer, BookmarkItem *parentItem)
{
    switch(parentItem->type()) {
    case BookmarkItem::Root:
        for(int i = 0; i < parentItem->childCount(); ++i) {
            writeChildElements(writer, parentItem->child(i));
        }
        break;

    case BookmarkItem::Folder:
        writer.writeStartElement("folder");
        writer.writeAttribute("folded", parentItem->folded ? "yes" : "no");
        writer.writeTextElement("title", parentItem->title);

        for(int i = 0; i < parentItem->childCount(); ++i) {
            writeChildElements(writer, parentItem->child(i));
        }

        writer.writeEndElement();
        break;

    case BookmarkItem::Bookmark:
        writer.writeStartElement("bookmark");
        writer.writeAttribute("href", parentItem->href);
        writer.writeTextElement("title", parentItem->title);
        writer.writeEndElement();
        break;
    }
}