Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
xbelsupport.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3#include "tracer.h"
4
5#include "xbelsupport.h"
6
7#include "bookmarkitem.h"
9
10#include <QtCore/QDate>
11#include <QtCore/QModelIndex>
12
14
15using namespace Qt::StringLiterals;
16
22
26{
28 setAutoFormatting(true);
29}
30
31void XbelWriter::writeToFile(QIODevice *device)
32{
34 setDevice(device);
35
36 writeStartDocument();
37 writeDTD("<!DOCTYPE xbel>"_L1);
38 writeStartElement("xbel"_L1);
39 writeAttribute("version"_L1, "1.0"_L1);
40
41 const QModelIndex root;
42 for (int i = 0; i < bookmarkModel->rowCount(root); ++i)
43 writeData(bookmarkModel->index(i, 0, root));
44 writeEndDocument();
45}
46
47void XbelWriter::writeData(const QModelIndex &index)
48{
50 if (index.isValid()) {
51 Bookmark entry;
52 entry.title = index.data().toString();
53 entry.url = index.data(UserRoleUrl).toString();
54
55 if (index.data(UserRoleFolder).toBool()) {
56 writeStartElement("folder"_L1);
57 entry.folded = !index.data(UserRoleExpanded).toBool();
58 writeAttribute("folded"_L1, entry.folded ? "yes"_L1 : "no"_L1);
59 writeTextElement("title"_L1, entry.title);
60 for (int i = 0; i < bookmarkModel->rowCount(index); ++i)
61 writeData(bookmarkModel->index(i, 0 , index));
62 writeEndElement();
63 } else {
64 writeStartElement("bookmark"_L1);
65 writeAttribute("href"_L1, entry.url);
66 writeTextElement("title"_L1, entry.title);
67 writeEndElement();
68 }
69 }
70}
71
72// -- XbelReader
73
80
81bool XbelReader::readFromFile(QIODevice *device)
82{
84 setDevice(device);
85
86 while (!atEnd()) {
87 readNext();
88
89 if (isStartElement()) {
90 if (name() == "xbel"_L1 && attributes().value("version"_L1) == "1.0"_L1) {
91 const QModelIndex root;
92 parents.append(bookmarkModel->addItem(root, true));
93 readXBEL();
94 bookmarkModel->setData(parents.first(),
95 QDate::currentDate().toString(Qt::ISODate), Qt::EditRole);
96 } else {
97 raiseError("The file is not an XBEL version 1.0 file."_L1);
98 }
99 }
100 }
101
102 return !error();
103}
104
105void XbelReader::readXBEL()
106{
108 while (!atEnd()) {
109 readNext();
110
111 if (isEndElement())
112 break;
113
114 if (isStartElement()) {
115 if (name() == "folder"_L1)
116 readFolder();
117 else if (name() == "bookmark"_L1)
118 readBookmark();
119 else
120 readUnknownElement();
121 }
122 }
123}
124
125void XbelReader::readFolder()
126{
128 parents.append(bookmarkModel->addItem(parents.last(), true));
129 bookmarkModel->setData(parents.last(), attributes().value("folded"_L1) == "no"_L1,
130 UserRoleExpanded);
131
132 while (!atEnd()) {
133 readNext();
134
135 if (isEndElement())
136 break;
137
138 if (isStartElement()) {
139 if (name() == "title"_L1)
140 bookmarkModel->setData(parents.last(), readElementText(), Qt::EditRole);
141 else if (name() == "folder"_L1)
142 readFolder();
143 else if (name() == "bookmark"_L1)
144 readBookmark();
145 else
146 readUnknownElement();
147 }
148 }
149
150 parents.removeLast();
151}
152
153void XbelReader::readBookmark()
154{
156 const QModelIndex &index = bookmarkModel->addItem(parents.last(), false);
157 if (BookmarkItem* item = bookmarkModel->itemFromIndex(index))
158 item->setData(UserRoleUrl, attributes().value("href"_L1).toString());
159
160 while (!atEnd()) {
161 readNext();
162
163 if (isEndElement())
164 break;
165
166 if (isStartElement()) {
167 if (name() == "title"_L1)
168 bookmarkModel->setData(index, readElementText(), Qt::EditRole);
169 else
170 readUnknownElement();
171 }
172 }
173}
174
175void XbelReader::readUnknownElement()
176{
178 while (!atEnd()) {
179 readNext();
180
181 if (isEndElement())
182 break;
183
184 if (isStartElement())
185 readUnknownElement();
186 }
187}
188
189QT_END_NAMESPACE
@ UserRoleExpanded
@ UserRoleFolder
@ UserRoleUrl
bool readFromFile(QIODevice *device)
XbelReader(BookmarkModel *model)
void writeToFile(QIODevice *device)
XbelWriter(BookmarkModel *model)
Combined button and popup list for selecting options.
QString url
QString title
#define TRACE_OBJ
Definition tracer.h:34