/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: git://neueland.iserlohn-fortress.net/smolbote.git * * SPDX-License-Identifier: GPL-3.0 */ #include "bookmarkswidget.h" #include "ui_bookmarksform.h" #include BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) : QWidget(parent) , ui(new Ui::BookmarksDialog) { // make sure this dialog does not get deleted on close setAttribute(Qt::WA_DeleteOnClose, false); setWindowTitle(tr("Bookmarks")); ui->setupUi(this); ui->treeView->header()->setSectionResizeMode(QHeaderView::Stretch); ui->addFolder_toolButton->setIcon(style()->standardPixmap(QStyle::SP_DirIcon)); ui->addBookmark_toolButton->setIcon(style()->standardPixmap(QStyle::SP_FileIcon)); ui->deleteItem_toolButton->setIcon(style()->standardPixmap(QStyle::SP_TrashIcon)); ui->deleteItem_toolButton->setShortcut(QKeySequence::Delete); m_model = new BookmarksModel(style(), this); ui->treeView->setModel(m_model); xbel = new Xbel(path); BookmarkItem *rootNode = xbel->read(); m_model->setRoot(rootNode); expandNodes(rootNode); //qDebug("Reading bookmarks [%s] %s", qUtf8Printable(m_path), xbel->read(m_path) ? "ok" : "failed"); connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) { emit openUrl(m_model->data(index, BookmarksModel::OpenUrlRole).toUrl()); }); } BookmarksWidget::~BookmarksWidget() { delete xbel; delete ui; } void BookmarksWidget::save() { // TODO: check if saving is needed //qDebug("Writing bookmarks [%s] %s", qUtf8Printable(m_path), xbel->write(m_path) ? "ok" : "failed"); } QAbstractItemModel *BookmarksWidget::model() const { Q_CHECK_PTR(m_model); return m_model; } void BookmarksWidget::expandNodes(BookmarkItem *node) { // iterate through children for(int i = 0; i < node->childCount(); ++i) { BookmarkItem *childNode = node->child(i); // and if it's a folder if(childNode->type() == BookmarkItem::Folder) { QModelIndex index = m_model->index(childNode); ui->treeView->setExpanded(index, !childNode->folded); // only folders have children, so go through them expandNodes(childNode); } } } void BookmarksWidget::closeOthers() { emit closeOthersSignal(); }