aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarkswidget.cpp
blob: 09f8c225c693489beee70385a08475d09cff78e4 (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
/*
 * 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/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "bookmarkswidget.h"
#include "ui_bookmarksform.h"
#include <QUrl>

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

    m_model = new BookmarksModel(style(), this);
    ui->treeView->setModel(m_model);

    xbel = new Xbel(path);
    qDebug("Reading bookmarks [%s] %s", qUtf8Printable(path), m_model->read(xbel) ? "ok" : "failed");
    m_model->expandItems(ui->treeView);

    // open bookmark action
    connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) {
        emit openUrl(m_model->data(index, BookmarksModel::OpenUrlRole).toUrl());
    });

    // add bookmark action
    connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [this]() {
        QModelIndex idx = ui->treeView->currentIndex();
        m_model->insertItem(BookmarkItem::Bookmark, idx);
    });

    // add folder action
    connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [this]() {
        QModelIndex idx = ui->treeView->currentIndex();
        m_model->insertItem(BookmarkItem::Folder, idx);
    });

    // set folder title action
    connect(ui->folderTitle, &QLineEdit::returnPressed, this, [this]() {
        QModelIndex idx = ui->treeView->currentIndex();
        Q_ASSERT(m_model->type(idx) == BookmarkItem::Folder);
        m_model->setData(idx, ui->folderTitle->text(), BookmarksModel::TitleRole);
    });

    // set bookmark title action
    connect(ui->bookmarkTitle, &QLineEdit::returnPressed, this, [this]() {
        QModelIndex idx = ui->treeView->currentIndex();
        Q_ASSERT(m_model->type(idx) == BookmarkItem::Bookmark);
        m_model->setData(idx, ui->bookmarkTitle->text(), BookmarksModel::TitleRole);
    });

    // set bookmark href action
    connect(ui->bookmarkHref, &QLineEdit::returnPressed, this, [this]() {
        QModelIndex idx = ui->treeView->currentIndex();
        Q_ASSERT(m_model->type(idx) == BookmarkItem::Bookmark);
        m_model->setData(idx, ui->bookmarkHref->text(), BookmarksModel::OpenUrlRole);
    });

    // set item action
    connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged, this, [this](const QModelIndex &current, const QModelIndex &previous) {
        // save the previous item
        if(previous.isValid()) {
            if(m_model->type(previous) == BookmarkItem::Folder) {
                m_model->setData(previous, ui->folderTitle->text(), BookmarksModel::TitleRole);
                ui->folder_groupBox->setVisible(false);
            } else if(m_model->type(previous) == BookmarkItem::Bookmark) {
                m_model->setData(previous, ui->bookmarkTitle->text(), BookmarksModel::TitleRole);
                m_model->setData(previous, ui->bookmarkHref->text(), BookmarksModel::OpenUrlRole);
                ui->bookmark_groupBox->setVisible(false);
            }
        }

        if(current.isValid()) {
            if(m_model->type(current) == BookmarkItem::Folder) {
                ui->folderTitle->setText(m_model->data(current, BookmarksModel::TitleRole).toString());
                ui->folder_groupBox->setVisible(true);
            } else if(m_model->type(current) == BookmarkItem::Bookmark) {
                ui->bookmarkTitle->setText(m_model->data(current, BookmarksModel::TitleRole).toString());
                ui->bookmarkHref->setText(m_model->data(current, BookmarksModel::OpenUrlRole).toString());
                ui->bookmark_groupBox->setVisible(true);
            }
        }
    });
}

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

void BookmarksWidget::save()
{
    if(m_model->isModified())
        qDebug("Writing bookmarks %s", m_model->write(xbel) ? "ok" : "failed");
    else
        qDebug("Writing bookmarks skipped");
}

QAbstractItemModel *BookmarksWidget::model() const
{
    Q_CHECK_PTR(m_model);
    return m_model;
}

void BookmarksWidget::closeOthers()
{
    emit closeOthersSignal();
}