aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/xbel.cpp
blob: cd17f2dee1f9fc16f834560c00977dc4cd1720ec (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
/*
 * 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>

XbelReader::XbelReader(QIODevice *file)
{
    Q_CHECK_PTR(file);
    m_file = file;
}

void readChildElements(QXmlStreamReader &reader, BookmarksView *widget, QTreeWidgetItem *parentItem = nullptr)
{
    while(reader.readNextStartElement()) {
        if(reader.name() == "title") {
            Q_CHECK_PTR(parentItem);
            parentItem->setText(0, reader.readElementText());

        } else if(reader.name() == "folder") {
            auto *item = widget->createFolder(parentItem);
            widget->setItemExpanded(item, !(reader.attributes().value("folded") == QLatin1String("yes")));
            readChildElements(reader, widget, item);

        } else if(reader.name() == "bookmark") {
            auto *item = widget->createBookmark(parentItem);
            item->setText(1, reader.attributes().value("href").toString());
            readChildElements(reader, widget, item);

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

void XbelReader::read(BookmarksView *treeWidget)
{
    QXmlStreamReader qXmlStreamReader(m_file);

    if(qXmlStreamReader.readNextStartElement()) {
        if(!(qXmlStreamReader.name() == "xbel" && qXmlStreamReader.attributes().value("version") == "1.0")) {
            return;
        }

        readChildElements(qXmlStreamReader, treeWidget);
    }
}

XbelWriter::XbelWriter(QIODevice *file)
{
    Q_CHECK_PTR(file);
    m_file = file;
}

void writeChildElements(QXmlStreamWriter &writer, BookmarksView *treeWidget, QTreeWidgetItem *parentItem)
{
    switch(treeWidget->itemType(parentItem)) {
    case BookmarksView::Folder:
        writer.writeStartElement("folder");
        writer.writeAttribute("folded", !treeWidget->isItemExpanded(parentItem) ? "yes" : "no");
        writer.writeTextElement("title", parentItem->text(0));

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

        writer.writeEndElement();
        break;

    case BookmarksView::Bookmark:
        writer.writeStartElement("bookmark");
        writer.writeAttribute("href", parentItem->text(1));
        writer.writeTextElement("title", parentItem->text(0));
        writer.writeEndElement();
        break;

    case BookmarksView::Invalid:
        break;
    }
}

void XbelWriter::write(BookmarksView *treeWidget)
{
    QXmlStreamWriter xmlWriter(m_file);
    xmlWriter.setAutoFormatting(true);

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

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

    for(int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
        writeChildElements(xmlWriter, treeWidget, treeWidget->topLevelItem(i));
    }

    xmlWriter.writeEndDocument();
}