aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarkmodel.h
blob: 1ddab1ebb3ed2ec26ecc759f902b35cffb3b39e0 (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
/*
 * 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
 */

#ifndef SMOLBOTE_BOOKMARKMODEL_H
#define SMOLBOTE_BOOKMARKMODEL_H

#include "bookmarkformat.h"
#include "bookmarkitem.h"
#include <QAbstractItemModel>

class BookmarkModel : public QAbstractItemModel
{
    Q_OBJECT

    template <BookmarkFormats T>
    friend void operator<<(BookmarkModel *model, const BookmarkFormat<T> &format);
    template <BookmarkFormats T>
    friend void operator>>(const BookmarkFormat<T> &format, BookmarkModel *model);

public:
    explicit BookmarkModel(QObject *parent = nullptr);
    ~BookmarkModel() override;

    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    QVariant data(const QModelIndex &index, int role) const override;
    QVariant data(const QModelIndex &index, int column, int role) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role) override;
    bool setData(const QModelIndex &index, const QVariant &value, BookmarkItem::Fields column, int role);
    Qt::ItemFlags flags(const QModelIndex &index) const override;

    bool isItemExpanded(const QModelIndex &index) const;
    void setItemExpanded(const QModelIndex &index, bool expanded);

    int rowCount(const QModelIndex &index) const override;
    QModelIndex appendBookmark(const QString &title, const QString &url, const QModelIndex &parent);
    QModelIndex appendFolder(const QString &title, const QModelIndex &parent);
    bool removeRows(int position, int rows, const QModelIndex &parent) override;
    int columnCount(const QModelIndex &index) const override;

    Qt::DropActions supportedDropActions() const override;
    QStringList mimeTypes() const override;
    QMimeData *mimeData(const QModelIndexList &indexes) const override;
    bool dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;

    QModelIndex index(int row, int column, const QModelIndex &parent) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    QModelIndex parentFolder(const QModelIndex &index) const;

    const BookmarkItem *root() const
    {
        return rootItem;
    }

    QStringList search(const QString &term) const;

    void resetModified()
    {
        m_isModified = false;
    }
    bool isModified() const
    {
        return m_isModified;
    }

private:
    const QLatin1String mimeType = QLatin1String("application/xbel");

    BookmarkItem *getItem(const QModelIndex &index) const;
    BookmarkItem *rootItem;

    bool m_isModified = false;
};

template <BookmarkFormats T>
void operator<<(BookmarkModel *model, const BookmarkFormat<T> &format)
{
    format.read(model->rootItem);
}

template <BookmarkFormats T>
void operator>>(const BookmarkFormat<T> &format, BookmarkModel *model)
{
    format.read(model->rootItem);
}

template <BookmarkFormats T>
void operator<<(BookmarkFormat<T> &format, BookmarkModel *model)
{
    format.write(model->root());
    model->resetModified();
}

template <BookmarkFormats T>
void operator>>(BookmarkModel *model, BookmarkFormat<T> &format)
{
    format.write(model->root());
    model->resetModified();
}

#endif // SMOLBOTE_BOOKMARKMODEL_H