void XbelReader::readFolder(QTreeWidgetItem *item) { Q_ASSERT(isStartElement() && name() == "folder"); QTreeWidgetItem *folder = createChildItem(item); bool folded = (attributes().value("folded") != "no"); treeWidget->setItemExpanded(folder, !folded); while (!atEnd()) { readNext(); if (isEndElement()) break; if (isStartElement()) { if (name() == "title") readTitle(folder); else if (name() == "folder") readFolder(folder); else if (name() == "bookmark") readBookmark(folder); else if (name() == "separator") readSeparator(folder); else readUnknownElement(); } } }
BookmarksModel::BookmarksModel(const QString &path, FormatMode mode, QObject *parent) : QStandardItemModel(parent), m_mode(mode) { BookmarksItem *rootItem = new BookmarksItem(); rootItem->setData(RootBookmark, TypeRole); rootItem->setData(((mode == NotesMode) ? tr("Notes") : tr("Bookmarks")), TitleRole); rootItem->setDragEnabled(false); BookmarksItem *trashItem = new BookmarksItem(); trashItem->setData(TrashBookmark, TypeRole); trashItem->setData(tr("Trash"), TitleRole); trashItem->setDragEnabled(false); trashItem->setEnabled(false); appendRow(rootItem); appendRow(trashItem); setItemPrototype(new BookmarksItem()); QFile file(path); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { Console::addMessage(((mode == NotesMode) ? tr("Failed to open notes file: %1") : tr("Failed to open bookmarks file: %1")).arg(file.errorString()), OtherMessageCategory, ErrorMessageLevel, path); return; } QXmlStreamReader reader(file.readAll()); if (reader.readNextStartElement() && reader.name() == QLatin1String("xbel") && reader.attributes().value(QLatin1String("version")).toString() == QLatin1String("1.0")) { while (reader.readNextStartElement()) { if (reader.name() == QLatin1String("folder") || reader.name() == QLatin1String("bookmark") || reader.name() == QLatin1String("separator")) { readBookmark(&reader, rootItem); } else { reader.skipCurrentElement(); } if (reader.hasError()) { getRootItem()->removeRows(0, getRootItem()->rowCount()); Console::addMessage(((m_mode == NotesMode) ? tr("Failed to load notes file: %1") : tr("Failed to load bookmarks file: %1")).arg(reader.errorString()), OtherMessageCategory, ErrorMessageLevel, path); QMessageBox::warning(NULL, tr("Error"), ((m_mode == NotesMode) ? tr("Failed to load notes file.") : tr("Failed to load bookmarks file.")), QMessageBox::Close); return; } } } connect(this, SIGNAL(itemChanged(QStandardItem*)), this, SIGNAL(modelModified())); connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(modelModified())); connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(modelModified())); connect(this, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), this, SIGNAL(modelModified())); }
void BookmarksManager::load() { m_bookmarks.clear(); m_identifier = 0; QFile file(SettingsManager::getPath() + QLatin1String("/bookmarks.xbel")); if (!file.open(QFile::ReadOnly | QFile::Text)) { return; } QXmlStreamReader reader(file.readAll()); if (reader.readNextStartElement() && reader.name() == QLatin1String("xbel") && reader.attributes().value(QLatin1String("version")).toString() == QLatin1String("1.0")) { while (reader.readNextStartElement()) { if (reader.name() == QLatin1String("folder") || reader.name() == QLatin1String("bookmark") || reader.name() == QLatin1String("separator")) { m_bookmarks.append(readBookmark(&reader, 0)); } else { reader.skipCurrentElement(); } } } updateUrls(); emit folderModified(0); }
//! [3] void XbelReader::readXBEL() { Q_ASSERT(xml.isStartElement() && xml.name() == "xbel"); while (xml.readNextStartElement()) { if (xml.name() == "folder") readFolder(0); else if (xml.name() == "bookmark") readBookmark(0); else if (xml.name() == "separator") readSeparator(0); else xml.skipCurrentElement(); } }
void XbelReader::readFolder(QTreeWidgetItem *item) { Q_ASSERT(xml.isStartElement() && xml.name() == "folder"); QTreeWidgetItem *folder = createChildItem(item); bool folded = (xml.attributes().value("folded") != "no"); treeWidget->setItemExpanded(folder, !folded); while (xml.readNextStartElement()) { if (xml.name() == "title") readTitle(folder); else if (xml.name() == "folder") readFolder(folder); else if (xml.name() == "bookmark") readBookmark(folder); else if (xml.name() == "separator") readSeparator(folder); else xml.skipCurrentElement(); } }
//! [3] void XbelReader::readXBEL() { Q_ASSERT(isStartElement() && name() == "xbel"); while (!atEnd()) { readNext(); if (isEndElement()) break; if (isStartElement()) { if (name() == "folder") readFolder(0); else if (name() == "bookmark") readBookmark(0); else if (name() == "separator") readSeparator(0); else readUnknownElement(); } } }
void BookmarksModel::readBookmark(QXmlStreamReader *reader, BookmarksItem *parent) { BookmarksItem *bookmark = NULL; if (reader->name() == QLatin1String("folder")) { bookmark = addBookmark(FolderBookmark, reader->attributes().value(QLatin1String("id")).toULongLong(), QUrl(), QString(), parent); bookmark->setData(QDateTime::fromString(reader->attributes().value(QLatin1String("added")).toString(), Qt::ISODate), TimeAddedRole); bookmark->setData(QDateTime::fromString(reader->attributes().value(QLatin1String("modified")).toString(), Qt::ISODate), TimeModifiedRole); while (reader->readNext()) { if (reader->isStartElement()) { if (reader->name() == QLatin1String("title")) { bookmark->setData(reader->readElementText().trimmed(), TitleRole); } else if (reader->name() == QLatin1String("desc")) { bookmark->setData(reader->readElementText().trimmed(), DescriptionRole); } else if (reader->name() == QLatin1String("folder") || reader->name() == QLatin1String("bookmark") || reader->name() == QLatin1String("separator")) { readBookmark(reader, bookmark); } else if (reader->name() == QLatin1String("info")) { while (reader->readNext()) { if (reader->isStartElement()) { if (reader->name() == QLatin1String("metadata") && reader->attributes().value(QLatin1String("owner")).toString().startsWith("http://otter-browser.org/")) { while (reader->readNext()) { if (reader->isStartElement()) { if (reader->name() == QLatin1String("keyword")) { bookmark->setData(reader->readElementText().trimmed(), KeywordRole); } else { reader->skipCurrentElement(); } } else if (reader->isEndElement() && reader->name() == QLatin1String("metadata")) { break; } } } else { reader->skipCurrentElement(); } } else if (reader->isEndElement() && reader->name() == QLatin1String("info")) { break; } } } else { reader->skipCurrentElement(); } } else if (reader->isEndElement() && reader->name() == QLatin1String("folder")) { break; } else if (reader->hasError()) { return; } } } else if (reader->name() == QLatin1String("bookmark")) { bookmark = addBookmark(UrlBookmark,reader->attributes().value(QLatin1String("id")).toULongLong(), reader->attributes().value(QLatin1String("href")).toString(), QString(), parent); bookmark->setData(QDateTime::fromString(reader->attributes().value(QLatin1String("added")).toString(), Qt::ISODate), TimeAddedRole); bookmark->setData(QDateTime::fromString(reader->attributes().value(QLatin1String("modified")).toString(), Qt::ISODate), TimeModifiedRole); bookmark->setData(QDateTime::fromString(reader->attributes().value(QLatin1String("visited")).toString(), Qt::ISODate), TimeVisitedRole); while (reader->readNext()) { if (reader->isStartElement()) { if (reader->name() == QLatin1String("title")) { bookmark->setData(reader->readElementText().trimmed(), TitleRole); } else if (reader->name() == QLatin1String("desc")) { bookmark->setData(reader->readElementText().trimmed(), DescriptionRole); } else if (reader->name() == QLatin1String("info")) { while (reader->readNext()) { if (reader->isStartElement()) { if (reader->name() == QLatin1String("metadata") && reader->attributes().value(QLatin1String("owner")).toString().startsWith("http://otter-browser.org/")) { while (reader->readNext()) { if (reader->isStartElement()) { if (reader->name() == QLatin1String("keyword")) { bookmark->setData(reader->readElementText().trimmed(), KeywordRole); } else if (reader->name() == QLatin1String("visits")) { bookmark->setData(reader->readElementText().toInt(), VisitsRole); } else { reader->skipCurrentElement(); } } else if (reader->isEndElement() && reader->name() == QLatin1String("metadata")) { break; } } } else { reader->skipCurrentElement(); } } else if (reader->isEndElement() && reader->name() == QLatin1String("info")) { break; } } } else { reader->skipCurrentElement(); } } else if (reader->isEndElement() && reader->name() == QLatin1String("bookmark")) { break; } else if (reader->hasError()) { return; } } } else if (reader->name() == QLatin1String("separator")) { addBookmark(SeparatorBookmark, 0, QUrl(), QString(), parent); reader->readNext(); } }
BookmarkInformation *BookmarksManager::readBookmark(QXmlStreamReader *reader, int parent) { BookmarkInformation *bookmark = new BookmarkInformation(); bookmark->parent = parent; if (reader->name() == QLatin1String("folder")) { bookmark->type = FolderBookmark; bookmark->identifier = ++m_identifier; while (reader->readNext()) { if (reader->isStartElement()) { if (reader->name() == QLatin1String("title")) { bookmark->title = reader->readElementText().trimmed(); } else if (reader->name() == QLatin1String("desc")) { bookmark->description = reader->readElementText().trimmed(); } else if (reader->name() == QLatin1String("folder") || reader->name() == QLatin1String("bookmark") || reader->name() == QLatin1String("separator")) { bookmark->children.append(readBookmark(reader, bookmark->identifier)); } else { reader->skipCurrentElement(); } } else if (reader->isEndElement() && reader->name() == QLatin1String("folder")) { break; } } m_pointers[bookmark->identifier] = bookmark; } else if (reader->name() == QLatin1String("bookmark")) { bookmark->type = UrlBookmark; bookmark->url = reader->attributes().value(QLatin1String("href")).toString(); while (reader->readNext()) { if (reader->isStartElement()) { if (reader->name() == QLatin1String("title")) { bookmark->title = reader->readElementText().trimmed(); } else if (reader->name() == QLatin1String("desc")) { bookmark->description = reader->readElementText().trimmed(); } else { reader->skipCurrentElement(); } } else if (reader->isEndElement() && reader->name() == QLatin1String("bookmark")) { break; } } } else if (reader->name() == QLatin1String("separator")) { bookmark->type = SeparatorBookmark; reader->readNext(); } m_allBookmarks.append(bookmark); return bookmark; }