bool BookmarksModel::save(const QString &path) const { QSaveFile file(path); if (!file.open(QIODevice::WriteOnly)) { return false; } QXmlStreamWriter writer(&file); writer.setAutoFormatting(true); writer.setAutoFormattingIndent(-1); writer.writeStartDocument(); writer.writeDTD(QLatin1String("<!DOCTYPE xbel>")); writer.writeStartElement(QLatin1String("xbel")); writer.writeAttribute(QLatin1String("version"), QLatin1String("1.0")); QStandardItem *rootItem = item(0, 0); for (int i = 0; i < rootItem->rowCount(); ++i) { writeBookmark(&writer, rootItem->child(i, 0)); } writer.writeEndDocument(); return file.commit(); }
bool BookmarksManager::save(const QString &path) { QFile file(path.isEmpty() ? SettingsManager::getPath() + QLatin1String("/bookmarks.xbel") : path); if (!file.open(QFile::WriteOnly)) { return false; } QXmlStreamWriter writer(&file); writer.setAutoFormatting(true); writer.setAutoFormattingIndent(-1); writer.writeStartDocument(); writer.writeDTD(QLatin1String("<!DOCTYPE xbel>")); writer.writeStartElement(QLatin1String("xbel")); writer.writeAttribute(QLatin1String("version"), QLatin1String("1.0")); for (int i = 0; i < m_bookmarks.count(); ++i) { writeBookmark(&writer, m_bookmarks.at(i)); } writer.writeEndDocument(); return true; }
void BookmarksManager::writeBookmark(QXmlStreamWriter *writer, BookmarkInformation *bookmark) { switch (bookmark->type) { case FolderBookmark: writer->writeStartElement(QLatin1String("folder")); writer->writeTextElement(QLatin1String("title"), bookmark->title); for (int i = 0; i < bookmark->children.count(); ++i) { writeBookmark(writer, bookmark->children.at(i)); } writer->writeEndElement(); break; case UrlBookmark: writer->writeStartElement(QLatin1String("bookmark")); if (!bookmark->url.isEmpty()) { writer->writeAttribute(QLatin1String("href"), bookmark->url); } writer->writeTextElement(QLatin1String("title"), bookmark->title); if (!bookmark->description.isEmpty()) { writer->writeTextElement(QLatin1String("desc"), bookmark->description); } writer->writeEndElement(); break; default: writer->writeEmptyElement(QLatin1String("separator")); break; } }
void BookmarksModel::writeBookmark(QXmlStreamWriter *writer, QStandardItem *bookmark) const { if (!bookmark) { return; } switch (static_cast<BookmarkType>(bookmark->data(TypeRole).toInt())) { case FolderBookmark: writer->writeStartElement(QLatin1String("folder")); writer->writeAttribute(QLatin1String("id"), QString::number(bookmark->data(IdentifierRole).toULongLong())); if (bookmark->data(TimeAddedRole).toDateTime().isValid()) { writer->writeAttribute(QLatin1String("added"), bookmark->data(TimeAddedRole).toDateTime().toString(Qt::ISODate)); } if (bookmark->data(TimeModifiedRole).toDateTime().isValid()) { writer->writeAttribute(QLatin1String("modified"), bookmark->data(TimeModifiedRole).toDateTime().toString(Qt::ISODate)); } writer->writeTextElement(QLatin1String("title"), bookmark->data(TitleRole).toString()); if (!bookmark->data(DescriptionRole).toString().isEmpty()) { writer->writeTextElement(QLatin1String("desc"), bookmark->data(DescriptionRole).toString()); } if (m_mode == BookmarksMode && !bookmark->data(KeywordRole).toString().isEmpty()) { writer->writeStartElement(QLatin1String("info")); writer->writeStartElement(QLatin1String("metadata")); writer->writeAttribute(QLatin1String("owner"), QLatin1String("http://otter-browser.org/otter-xbel-bookmark")); writer->writeTextElement(QLatin1String("keyword"), bookmark->data(KeywordRole).toString()); writer->writeEndElement(); writer->writeEndElement(); } for (int i = 0; i < bookmark->rowCount(); ++i) { writeBookmark(writer, bookmark->child(i, 0)); } writer->writeEndElement(); break; case UrlBookmark: writer->writeStartElement(QLatin1String("bookmark")); writer->writeAttribute(QLatin1String("id"), QString::number(bookmark->data(IdentifierRole).toULongLong())); if (!bookmark->data(UrlRole).toString().isEmpty()) { writer->writeAttribute(QLatin1String("href"), bookmark->data(UrlRole).toString()); } if (bookmark->data(TimeAddedRole).toDateTime().isValid()) { writer->writeAttribute(QLatin1String("added"), bookmark->data(TimeAddedRole).toDateTime().toString(Qt::ISODate)); } if (bookmark->data(TimeModifiedRole).toDateTime().isValid()) { writer->writeAttribute(QLatin1String("modified"), bookmark->data(TimeModifiedRole).toDateTime().toString(Qt::ISODate)); } if (m_mode != NotesMode) { if (bookmark->data(TimeVisitedRole).toDateTime().isValid()) { writer->writeAttribute(QLatin1String("visited"), bookmark->data(TimeVisitedRole).toDateTime().toString(Qt::ISODate)); } writer->writeTextElement(QLatin1String("title"), bookmark->data(TitleRole).toString()); } if (!bookmark->data(DescriptionRole).toString().isEmpty()) { writer->writeTextElement(QLatin1String("desc"), bookmark->data(DescriptionRole).toString()); } if (m_mode == BookmarksMode && (!bookmark->data(KeywordRole).toString().isEmpty() || bookmark->data(VisitsRole).toInt() > 0)) { writer->writeStartElement(QLatin1String("info")); writer->writeStartElement(QLatin1String("metadata")); writer->writeAttribute(QLatin1String("owner"), QLatin1String("http://otter-browser.org/otter-xbel-bookmark")); if (!bookmark->data(KeywordRole).toString().isEmpty()) { writer->writeTextElement(QLatin1String("keyword"), bookmark->data(KeywordRole).toString()); } if (bookmark->data(VisitsRole).toInt() > 0) { writer->writeTextElement(QLatin1String("visits"), QString::number(bookmark->data(VisitsRole).toInt())); } writer->writeEndElement(); writer->writeEndElement(); } writer->writeEndElement(); break; default: writer->writeEmptyElement(QLatin1String("separator")); break; } }