aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/formats/xbel.cpp
blob: 7ff79a9ed2d3611b1f0d5f7eb776fe4fec1ba317 (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
/*
 * 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/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "xbel.h"
#include "bookmarkitem.h"
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QDateTime>

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

        } else if(reader.name() == "dateAdded") {
            parent->setData(BookmarkItem::DateAdded, QDateTime::fromString(reader.readElementText(), Qt::RFC2822Date));

        } else if(reader.name() == "lastModified") {
            parent->setData(BookmarkItem::LastModified, QDateTime::fromString(reader.readElementText(), Qt::RFC2822Date));

        } else if(reader.name() == "tags") {
            parent->setData(BookmarkItem::Tags, reader.readElementText().split(";"));

        } else if(reader.name() == "description") {
            parent->setData(BookmarkItem::Description, reader.readElementText());

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

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

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

void Xbel::read(QIODevice *device, BookmarkItem *item)
{
    QXmlStreamReader qXmlStreamReader(device);

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

        readChildElements(qXmlStreamReader, item);
    }
}

inline void writeCommon(QXmlStreamWriter &writer, const BookmarkItem *item)
{
    writer.writeTextElement("title", item->data(BookmarkItem::Title).toString());

    const auto dateAdded = item->data(BookmarkItem::DateAdded);
    if(!dateAdded.isNull())
        writer.writeTextElement("dateAdded", dateAdded.toDateTime().toString(Qt::RFC2822Date));

    const auto lastModified = item->data(BookmarkItem::LastModified);
    if(!lastModified.isNull())
        writer.writeTextElement("lastModified", lastModified.toDateTime().toString(Qt::RFC2822Date));
}

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

    case BookmarkItem::Folder:
        writer.writeStartElement("folder");
        writer.writeAttribute("folded", !item->isExpanded() ? "yes" : "no");
        writeCommon(writer, item);
        if(!item->data(BookmarkItem::Tags).isNull())
            writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";"));
        if(!item->data(BookmarkItem::Description).isNull())
            writer.writeTextElement("description", item->data(BookmarkItem::Description).toString());

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

        writer.writeEndElement();
        break;

    case BookmarkItem::Bookmark:
        writer.writeStartElement("bookmark");
        writer.writeAttribute("href", item->data(BookmarkItem::Href).toString());
        writeCommon(writer, item);
        if(!item->data(BookmarkItem::Tags).isNull())
            writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";"));
        if(!item->data(BookmarkItem::Description).isNull())
            writer.writeTextElement("description", item->data(BookmarkItem::Description).toString());

        writer.writeEndElement();
        break;
    }
}

void Xbel::write(QIODevice *device, const BookmarkItem *item)
{
    QXmlStreamWriter xmlWriter(device);
    xmlWriter.setAutoFormatting(true);

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

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

    writeChildElements(xmlWriter, item);

    xmlWriter.writeEndDocument();
}