1 /* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #pragma once
19 
20 #include "qmlbookmarktreenode.h"
21 #include "mainapplication.h"
22 
23 #include <QObject>
24 
25 /**
26  * @brief The class exposing the Bookmarks API to QML
27  */
28 class QmlBookmarks : public QObject
29 {
30     Q_OBJECT
31 
32 public:
33     explicit QmlBookmarks(QObject *parent = nullptr);
34 
35     /**
36      * @brief Checks if the url is bookmarked
37      * @param String representing the url to check
38      * @return true if bookmarked, else false
39      */
40     Q_INVOKABLE bool isBookmarked(const QString &url) const;
41     /**
42      * @brief Get the root bookmark item
43      * @return Root boomkark item
44      */
45     Q_INVOKABLE QmlBookmarkTreeNode *rootItem() const;
46     /**
47      * @brief Get the bookmarks toolbar
48      * @return Bookmarks toolbar
49      */
50     Q_INVOKABLE QmlBookmarkTreeNode *toolbarFolder() const;
51     /**
52      * @brief Get the bookmarks menu folder
53      * @return Bookmarks menu folder
54      */
55     Q_INVOKABLE QmlBookmarkTreeNode *menuFolder() const;
56     /**
57      * @brief Get the unsorted bookmarks folder
58      * @return Unsorted bookmarks folder
59      */
60     Q_INVOKABLE QmlBookmarkTreeNode *unsortedFolder() const;
61     /**
62      * @brief Get the last used bookmarks folder
63      * @return Last used bookmarks folder
64      */
65     Q_INVOKABLE QmlBookmarkTreeNode *lastUsedFolder() const;
66     /**
67      * @brief Creates a bookmark item
68      * @param A JavaScript object containing
69      *        - parent:
70      *          Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode), representing
71      *          the parent of the new bookmark item. This is required field.
72      *        - title:
73      *          String representing the title of the new bookmark item. Defaults to empty string
74      *        - url:
75      *          String representing the url of the new bookmark item. Defaults to empty string
76      *        - description
77      *          String representing the description of the new bookmark item. Defaults to empty string
78      *        - type:
79      *          [Type](@ref QmlBookmarkTreeNode::Type) representing the type of the new bookmark item.
80      *          Defaults to [Url](@ref QmlBookmarkTreeNode::Url) if url is provided, else
81      *          [Folder](@ref QmlBookmarkTreeNode::Folder) if title is provided, else
82      *          [Invalid](@ref QmlBookmarkTreeNode::Invalid)
83      * @return true if the bookmark it created, else false
84      */
85     Q_INVOKABLE bool create(const QVariantMap &map) const;
86     /**
87      * @brief Removes a bookmark item
88      * @param treeNode:
89      *        Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode) to be removed
90      * @return true if the bookmark is removed, else false
91      */
92     Q_INVOKABLE bool remove(QmlBookmarkTreeNode *treeNode) const;
93     /**
94      * @brief QmlBookmarks::search
95      * @param A JavaScript object containing
96      *        - query:
97      *          String containing search query
98      *        - url:
99      *          String representing url to be search
100      * @return List containing [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode). If both
101      *         query and url are not supplied then empty list is returned.
102      */
103     Q_INVOKABLE QList<QObject*> search(const QVariantMap &map) const;
104     /**
105      * @brief Updates a bookmark item
106      * @param Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode), representing the bookmark
107      *        to update
108      * @param JavaScript object containing the values to be updated
109      *        - title:
110      *          String representing the new title of the bookmark item
111      *        - description:
112      *          String representing the new description of the bookmark item
113      *        - keyword:
114      *          String representing the new keyword of the bookmark item
115      * @return true if the bookmark is updated, else false
116      */
117     Q_INVOKABLE bool update(QObject *object, const QVariantMap &changes) const;
118     /**
119      * @brief Get the first matched bookmark item
120      * @param String representing the query
121      * @return Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode) if
122      *         the query is matched with a bookmark, else null
123      */
124     Q_INVOKABLE QmlBookmarkTreeNode *get(const QString &string) const;
125     /**
126      * @brief Get children of the bookmark item
127      * @param Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode), representing
128      *        the parent whose children are requested.
129      * @return List containing the children, of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode)
130      */
131     Q_INVOKABLE QList<QObject*> getChildren(QObject *object) const;
132 
133 Q_SIGNALS:
134     /**
135      * @brief This signal is emitted when a new bookmark item is created
136      * @param bookmark item, exposed to QML as [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode)
137      */
138     void created(QmlBookmarkTreeNode *treeNode);
139 
140     /**
141      * @brief This signal is emitted when a bookmark item is edited
142      * @param bookmark item, exposed to QML as [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode)
143      */
144     void changed(QmlBookmarkTreeNode *treeNode);
145 
146     /**
147      * @brief This signal is emitted when a bookmark item is removed
148      * @param bookmark item, exposed to QML as [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode)
149      */
150     void removed(QmlBookmarkTreeNode *treeNode);
151 
152 private:
153     BookmarkItem *getBookmarkItem(QmlBookmarkTreeNode *treeNode) const;
154     BookmarkItem *getBookmarkItem(QObject *object) const;
155 };
156