aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/xbel.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-01-11 17:30:38 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-01-11 17:30:38 +0100
commit5d0adf426a33440542eb88eca83c804dcec58475 (patch)
tree34358c8039c2f6cb2a121a169374dc4a7a21b614 /lib/bookmarks/xbel.cpp
parentRemoved docs (moved to site) (diff)
downloadsmolbote-5d0adf426a33440542eb88eca83c804dcec58475.tar.xz
Writing BookmarksModel to xbel
Diffstat (limited to 'lib/bookmarks/xbel.cpp')
-rw-r--r--lib/bookmarks/xbel.cpp72
1 files changed, 62 insertions, 10 deletions
diff --git a/lib/bookmarks/xbel.cpp b/lib/bookmarks/xbel.cpp
index 7a7632c..122ccf8 100644
--- a/lib/bookmarks/xbel.cpp
+++ b/lib/bookmarks/xbel.cpp
@@ -14,21 +14,17 @@ Xbel::Xbel(const QString &path)
m_path = path;
}
-BookmarkItem *Xbel::read()
+bool Xbel::read(BookmarkItem *root)
{
- BookmarkItem *root = new BookmarkItem(BookmarkItem::Root, nullptr);
- root->title = QObject::tr("Title");
- root->href = QObject::tr("href");
-
// if the path is empty, there is nothing to load
if(m_path.isEmpty()) {
- return root;
+ return false;
}
QFile file(m_path);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
// file cannot be opened for reading
- return root;
+ return false;
}
QXmlStreamReader xmlReader(&file);
@@ -37,13 +33,14 @@ BookmarkItem *Xbel::read()
if(!(xmlReader.name() == "xbel" && xmlReader.attributes().value("version") == "1.0")) {
// invalid xbel
qWarning("Error parsing %s", qUtf8Printable(m_path));
- return root;
+ return false;
}
readChildElements(xmlReader, root);
}
- return root;
+ file.close();
+ return true;
}
void Xbel::readChildElements(QXmlStreamReader &reader, BookmarkItem *parentItem)
@@ -54,7 +51,6 @@ void Xbel::readChildElements(QXmlStreamReader &reader, BookmarkItem *parentItem)
} else if(reader.name() == "folder") {
BookmarkItem *item = new BookmarkItem(BookmarkItem::Folder, parentItem);
- item->href = reader.attributes().value("href").toString();
item->folded = reader.attributes().value("folded") == QLatin1String("yes");
readChildElements(reader, item);
@@ -68,3 +64,59 @@ void Xbel::readChildElements(QXmlStreamReader &reader, BookmarkItem *parentItem)
}
}
}
+
+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)
+{
+ for(int i = 0; i < parentItem->childCount(); ++i) {
+ BookmarkItem *node = parentItem->child(i);
+ switch(node->type()) {
+ case BookmarkItem::Root:
+ break;
+ case BookmarkItem::Folder:
+ writer.writeStartElement("folder");
+ writer.writeAttribute("folded", node->folded ? "yes" : "no");
+ writer.writeTextElement("title", node->title);
+ for(int j = 0; j < node->childCount(); ++j) {
+ writeChildElements(writer, node);
+ }
+ writer.writeEndElement();
+ break;
+ case BookmarkItem::Bookmark:
+ writer.writeStartElement("bookmark");
+ writer.writeAttribute("href", node->href);
+ writer.writeTextElement("title", node->title);
+ writer.writeEndElement();
+ break;
+ }
+ }
+}