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
|
/* ============================================================
* rekonq
* ============================================================
* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
* ============================================================
* Description: rekonq bookmarks model
* ============================================================ */
#include "bookmarkstreeformat_xbel.h"
#include "bookmarkstreeitem.hpp"
#include <QXmlStreamReader>
inline void writeNodeAttributes(QXmlStreamWriter &writer, const BookmarksTreeItem *item)
{
// id
if (const auto id = item->data(BookmarksTreeItem::Id).toString(); !id.isEmpty())
writer.writeAttribute(xbel::attr_id, id);
// added
if (const auto added = item->data(BookmarksTreeItem::Added).toDateTime(); !added.isNull())
writer.writeAttribute(xbel::attr_added, added.toString(Qt::ISODate));
}
inline void writeUrlAttributes(QXmlStreamWriter &writer, const BookmarksTreeItem *item)
{
// href (required)
writer.writeAttribute(xbel::attr_href, item->data(BookmarksTreeItem::Href).toString());
// visited
if (const auto visited = item->data(BookmarksTreeItem::Visited).toDateTime(); !visited.isNull())
writer.writeAttribute(xbel::attr_visited, visited.toString(Qt::ISODate));
// modified
if (const auto modified = item->data(BookmarksTreeItem::Modified).toDateTime(); !modified.isNull())
writer.writeAttribute(xbel::attr_modified, modified.toString(Qt::ISODate));
}
inline void writeItemTitle(QXmlStreamWriter &writer, const BookmarksTreeItem *item)
{
if (const auto title = item->data(BookmarksTreeItem::Title).toString(); !title.isEmpty())
writer.writeTextElement(xbel::elem_title, title);
}
inline void writeItemInfo(QXmlStreamWriter &writer, const BookmarksTreeItem *item)
{
if (!item->metadata.isEmpty()) {
writer.writeStartElement(xbel::elem_info);
for (const auto &meta : item->metadata) {
writer.writeStartElement(xbel::elem_metadata);
writer.writeAttribute(xbel::elem_metadata_owner, xbel::elem_metadata_owner_value);
writer.writeCDATA(meta);
writer.writeEndElement(); // elem_metadata
}
writer.writeEndElement(); // elem_info
}
}
inline void writeItemDesc(QXmlStreamWriter &writer, const BookmarksTreeItem *item)
{
if (const auto desc = item->data(BookmarksTreeItem::Description).toString(); !desc.isEmpty())
writer.writeTextElement(xbel::elem_desc, desc);
}
inline void writeChildElements(QXmlStreamWriter &writer, const BookmarksTreeItem *item)
{
switch (item->type()) {
case BookmarksTreeItem::Root:
// node.att
writeNodeAttributes(writer, item);
// title
writeItemTitle(writer, item);
// info
writeItemInfo(writer, item);
// desc
writeItemDesc(writer, item);
// children
for (int i = 0; i < item->childCount(); ++i) { writeChildElements(writer, item->child(i)); }
break;
case BookmarksTreeItem::Folder:
writer.writeStartElement(xbel::elem_folder);
// note.att
writeNodeAttributes(writer, item);
// folded
writer.writeAttribute(xbel::attr_folded, !item->isExpanded() ? xbel::attr_folded_yes : xbel::attr_folded_no);
// title
writeItemTitle(writer, item);
// info
writeItemInfo(writer, item);
// desc
writeItemDesc(writer, item);
// children
for (int i = 0; i < item->childCount(); ++i) { writeChildElements(writer, item->child(i)); }
break;
case BookmarksTreeItem::Bookmark:
writer.writeStartElement(xbel::elem_bookmark);
// node.att
writeNodeAttributes(writer, item);
// url.att
writeUrlAttributes(writer, item);
// title
writeItemTitle(writer, item);
// info
writeItemInfo(writer, item);
// desc
writeItemDesc(writer, item);
break;
case BookmarksTreeItem::Separator:
writer.writeStartElement(xbel::elem_separator);
// empty
break;
case BookmarksTreeItem::Alias:
writer.writeStartElement(xbel::elem_alias);
// ref
writer.writeAttribute(xbel::attr_ref, item->data(BookmarksTreeItem::Id).toString());
break;
}
writer.writeEndElement();
}
bool xbel::write(QIODevice *device, const QVector<const BookmarksTreeItem *> &items)
{
QXmlStreamWriter xmlWriter(device);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();
xmlWriter.writeDTD("<!DOCTYPE xbel>");
xmlWriter.writeStartElement(xbel::elem_xbel);
xmlWriter.writeAttribute(xbel::attr_version, xbel::attr_version_value);
for (const auto *item : items) writeChildElements(xmlWriter, item);
xmlWriter.writeEndDocument();
return true;
}
|