void BookmarksModel::trashBookmark(BookmarksItem *bookmark) { if (!bookmark) { return; } const BookmarkType type = static_cast<BookmarkType>(bookmark->data(TypeRole).toInt()); if (type != RootBookmark && type != TrashBookmark) { if (type == SeparatorBookmark || bookmark->data(IsTrashedRole).toBool()) { bookmark->remove(); } else { BookmarksItem *trashItem = getTrashItem(); m_trash[bookmark] = qMakePair(bookmark->parent()->index(), bookmark->row()); trashItem->appendRow(bookmark->parent()->takeRow(bookmark->row())); trashItem->setEnabled(true); removeBookmarkUrl(bookmark); emit bookmarkModified(bookmark); emit bookmarkTrashed(bookmark); emit modelModified(); } } }
void BookmarksModel::restoreBookmark(BookmarksItem *bookmark) { if (!bookmark) { return; } BookmarksItem *formerParent = (m_trash.contains(bookmark) ? getBookmark(m_trash[bookmark].first) : getRootItem()); if (!formerParent || static_cast<BookmarkType>(formerParent->data(TypeRole).toInt()) != FolderBookmark) { formerParent = getRootItem(); } if (m_trash.contains(bookmark)) { formerParent->insertRow(m_trash[bookmark].second, bookmark->parent()->takeRow(bookmark->row())); m_trash.remove(bookmark); } else { formerParent->appendRow(bookmark->parent()->takeRow(bookmark->row())); } readdBookmarkUrl(bookmark); BookmarksItem *trashItem = getTrashItem(); trashItem->setEnabled(trashItem->rowCount() > 0); emit bookmarkModified(bookmark); emit bookmarkRestored(bookmark); emit modelModified(); }
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 BookmarksModel::emptyTrash() { BookmarksItem *trashItem = getTrashItem(); trashItem->removeRows(0, trashItem->rowCount()); trashItem->setEnabled(false); m_trash.clear(); emit modelModified(); }