aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarkswidget.cpp
blob: aed7e97d98dff9bd043647ed719e9a67a5bcaffa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * 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: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "bookmarkswidget.h"
#include "forms/editbookmarkdialog.h"
#include "model/bookmarkitem.h"
#include "model/bookmarkmodel.h"
#include "ui_bookmarksform.h"
#include "formats/format.h"
#include <QTreeView>
#include <QUrl>

inline void expandChildren(QTreeView *view, BookmarkModel *model, const QModelIndex &rootIndex)
{
    for(int i = 0; i < model->rowCount(rootIndex); ++i) {
        QModelIndex idx = model->index(i, 0, rootIndex);
        if(model->isItemExpanded(idx))
            view->expand(idx);

        // check if index has children
        if(model->rowCount(idx) > 0)
            expandChildren(view, model, idx);
    }
}

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);

    ui->setupUi(this);
    ui->bookmark_groupBox->setVisible(false);
    ui->folder_groupBox->setVisible(false);

    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);

    model = new BookmarkModel(this);
    m_bookmarksPath = path;
    QFile bookmarksFile(m_bookmarksPath);
    if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        BookmarksFormat<XbelFormat> format(&bookmarksFile);
        format.read(model->root());
        bookmarksFile.close();
    }
    model->resetModified();

    ui->treeView->setModel(model);
    expandChildren(ui->treeView, model, QModelIndex());

    // item activated
    connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) {
        if(index.column() == 1)
            emit openUrl(index.data(Qt::DisplayRole).toUrl());
        else
            editBookmark(index);
    });

    connect(ui->treeView, &QTreeView::expanded, this, [this](const QModelIndex &index) {
        model->setItemExpanded(index, true);
    });
    connect(ui->treeView, &QTreeView::collapsed, this, [this](const QModelIndex &index) {
        model->setItemExpanded(index, false);
    });

    ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->treeView, &QTreeView::customContextMenuRequested, this, [this](const QPoint &pos) {
        const QModelIndex idx = ui->treeView->indexAt(pos);
        if(idx.isValid()) {
            const QUrl url = model->data(idx, 1, Qt::DisplayRole).toUrl();
            const QPoint _pos = ui->treeView->viewport()->mapToGlobal(pos);

            emit showContextMenu(url, _pos);
        }
    });

    // addBookmark
    connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [this]() {
        const QModelIndex idx = model->parentFolder(ui->treeView->currentIndex());
        const QModelIndex childIdx = model->appendBookmark(tr("Title"), QString(), idx);
        ui->treeView->setCurrentIndex(childIdx);
        editBookmark(childIdx);
    });

    // addFolder
    connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [this]() {
        const QModelIndex idx = model->parentFolder(ui->treeView->currentIndex());
        const QModelIndex childIdx = model->appendFolder(tr("Title"), idx);
        ui->treeView->setCurrentIndex(childIdx);
        editBookmark(childIdx);
    });

    // deleteItem
    connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() {
        const QModelIndex idx = ui->treeView->currentIndex();
        model->removeRow(idx.row(), idx.parent());
    });
}

BookmarksWidget::~BookmarksWidget()
{
    delete ui;
}

void BookmarksWidget::editBookmark(const QModelIndex &index)
{
    auto *dlg = new EditBookmarkDialog(model, index, this);
    dlg->exec();
}

void BookmarksWidget::save()
{
    if(!model->isModified())
        return;

    QFile bookmarksFile(m_bookmarksPath);
    if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
        BookmarksFormat<XbelFormat> format(&bookmarksFile);
        format.write(model->root());
        bookmarksFile.flush();
        bookmarksFile.close();
        model->resetModified();
    }
}

void BookmarksWidget::addBookmark(const QString &title, const QString &url)
{
    model->appendBookmark(title, url, QModelIndex());
}

void BookmarksWidget::search(const QString &term, const std::function<void(QStringList &)> &callback) const
{
    QStringList ret = model->search(term);
    callback(ret);
}