File indexing completed on 2024-12-22 04:41:13
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "qmlbookmarks.h" 0019 #include "bookmarks.h" 0020 #include "qml/qmlstaticdata.h" 0021 #include <QDebug> 0022 #include <QQmlEngine> 0023 0024 QmlBookmarks::QmlBookmarks(QObject *parent) 0025 : QObject(parent) 0026 { 0027 connect(mApp->bookmarks(), &Bookmarks::bookmarkAdded, this, [this](BookmarkItem *item){ 0028 auto treeNode = QmlStaticData::instance().getBookmarkTreeNode(item); 0029 Q_EMIT created(treeNode); 0030 }); 0031 0032 connect(mApp->bookmarks(), &Bookmarks::bookmarkChanged, this, [this](BookmarkItem *item){ 0033 auto treeNode = QmlStaticData::instance().getBookmarkTreeNode(item); 0034 Q_EMIT changed(treeNode); 0035 }); 0036 0037 connect(mApp->bookmarks(), &Bookmarks::bookmarkRemoved, this, [this](BookmarkItem *item){ 0038 auto treeNode = QmlStaticData::instance().getBookmarkTreeNode(item); 0039 Q_EMIT removed(treeNode); 0040 }); 0041 } 0042 0043 BookmarkItem *QmlBookmarks::getBookmarkItem(QmlBookmarkTreeNode *treeNode) const 0044 { 0045 auto bookmarks = mApp->bookmarks(); 0046 QList<BookmarkItem*> items; 0047 0048 if (treeNode->url().isEmpty()) { 0049 if (treeNode->item() == bookmarks->rootItem()) { 0050 return bookmarks->rootItem(); 0051 0052 } else if (treeNode->item() == bookmarks->toolbarFolder()) { 0053 return bookmarks->toolbarFolder(); 0054 0055 } else if (treeNode->item() == bookmarks->menuFolder()) { 0056 return bookmarks->menuFolder(); 0057 0058 } else if (treeNode->item() == bookmarks->unsortedFolder()) { 0059 return bookmarks->unsortedFolder(); 0060 } 0061 0062 items = bookmarks->searchBookmarks(treeNode->title()); 0063 } else { 0064 items = bookmarks->searchBookmarks(QUrl::fromEncoded(treeNode->url().toUtf8())); 0065 } 0066 0067 for (BookmarkItem *item : std::as_const(items)) { 0068 if (treeNode->item() == item) { 0069 return item; 0070 } 0071 } 0072 0073 return nullptr; 0074 } 0075 0076 BookmarkItem *QmlBookmarks::getBookmarkItem(QObject *object) const 0077 { 0078 auto treeNode = qobject_cast<QmlBookmarkTreeNode*>(object); 0079 if (!treeNode) { 0080 return nullptr; 0081 } 0082 0083 auto item = getBookmarkItem(treeNode); 0084 if (!item || item->urlString() != treeNode->url()) { 0085 return nullptr; 0086 } 0087 0088 return item; 0089 } 0090 0091 bool QmlBookmarks::isBookmarked(const QString &url) const 0092 { 0093 return mApp->bookmarks()->isBookmarked(QUrl::fromEncoded(url.toUtf8())); 0094 } 0095 0096 QmlBookmarkTreeNode *QmlBookmarks::rootItem() const 0097 { 0098 return QmlStaticData::instance().getBookmarkTreeNode(mApp->bookmarks()->rootItem()); 0099 } 0100 0101 QmlBookmarkTreeNode *QmlBookmarks::toolbarFolder() const 0102 { 0103 return QmlStaticData::instance().getBookmarkTreeNode(mApp->bookmarks()->toolbarFolder()); 0104 } 0105 0106 QmlBookmarkTreeNode *QmlBookmarks::menuFolder() const 0107 { 0108 return QmlStaticData::instance().getBookmarkTreeNode(mApp->bookmarks()->menuFolder()); 0109 } 0110 0111 QmlBookmarkTreeNode *QmlBookmarks::unsortedFolder() const 0112 { 0113 return QmlStaticData::instance().getBookmarkTreeNode(mApp->bookmarks()->unsortedFolder()); 0114 } 0115 0116 QmlBookmarkTreeNode *QmlBookmarks::lastUsedFolder() const 0117 { 0118 return QmlStaticData::instance().getBookmarkTreeNode(mApp->bookmarks()->lastUsedFolder()); 0119 } 0120 0121 bool QmlBookmarks::create(const QVariantMap &map) const 0122 { 0123 if (!map.contains(QSL("parent"))) { 0124 qWarning() << "Unable to create new bookmark:" << "parent not found"; 0125 return false; 0126 } 0127 const QString title = map.value(QSL("title")).toString(); 0128 const QString urlString = map.value(QSL("url")).toString(); 0129 const QString description = map.value(QSL("description")).toString(); 0130 0131 BookmarkItem::Type type; 0132 if (map.contains(QSL("type"))) { 0133 type = BookmarkItem::Type(map.value(QSL("type")).toInt()); 0134 } else if (urlString.isEmpty()){ 0135 if (!title.isEmpty()) { 0136 type = BookmarkItem::Folder; 0137 } else { 0138 type = BookmarkItem::Invalid; 0139 } 0140 } else { 0141 type = BookmarkItem::Url; 0142 } 0143 0144 auto object = map.value(QSL("parent")).value<QObject*>(); 0145 auto parent = getBookmarkItem(object); 0146 if (!parent) { 0147 qWarning() << "Unable to create new bookmark:" << "parent not found"; 0148 return false; 0149 } 0150 auto item = new BookmarkItem(type); 0151 item->setTitle(title); 0152 item->setUrl(QUrl::fromEncoded(urlString.toUtf8())); 0153 item->setDescription(description); 0154 mApp->bookmarks()->addBookmark(parent, item); 0155 return true; 0156 } 0157 0158 bool QmlBookmarks::remove(QmlBookmarkTreeNode *treeNode) const 0159 { 0160 auto item = getBookmarkItem(treeNode); 0161 if (!item) { 0162 qWarning() << "Unable to remove bookmark:" <<"BookmarkItem not found"; 0163 return false; 0164 } 0165 return mApp->bookmarks()->removeBookmark(item); 0166 } 0167 0168 QList<QObject*> QmlBookmarks::search(const QVariantMap &map) const 0169 { 0170 if (!map.contains(QSL("query")) && !map.contains(QSL("url"))) { 0171 qWarning() << "Unable to search bookmarks"; 0172 return {}; 0173 } 0174 0175 const QString query = map.value(QSL("query")).toString(); 0176 const QString urlString = map.value(QSL("url")).toString(); 0177 QList<BookmarkItem*> items; 0178 if (urlString.isEmpty()) { 0179 items = mApp->bookmarks()->searchBookmarks(query); 0180 } else { 0181 items = mApp->bookmarks()->searchBookmarks(QUrl::fromEncoded(urlString.toUtf8())); 0182 } 0183 QList<QObject*> ret; 0184 ret.reserve(items.size()); 0185 for (auto item : std::as_const(items)) { 0186 ret.append(QmlStaticData::instance().getBookmarkTreeNode(item)); 0187 } 0188 return ret; 0189 } 0190 0191 bool QmlBookmarks::update(QObject *object, const QVariantMap &changes) const 0192 { 0193 auto treeNode = qobject_cast<QmlBookmarkTreeNode*>(object); 0194 if (!treeNode) { 0195 qWarning() << "Unable to update bookmark:" << "unable to cast QVariant to QmlBookmarkTreeNode"; 0196 return false; 0197 } 0198 0199 auto item = getBookmarkItem(treeNode); 0200 if (!item) { 0201 qWarning() << "Unable to update bookmark:" << "bookmark not found"; 0202 return false; 0203 } 0204 0205 if (!mApp->bookmarks()->canBeModified(item)) { 0206 qWarning() << "Unable to update bookmark:" << "bookmark can not be modified"; 0207 } 0208 0209 QString newTitle = treeNode->title(); 0210 if (changes.contains(QSL("title"))) { 0211 newTitle = changes.value(QSL("title")).toString(); 0212 } 0213 QString newDescription = treeNode->description(); 0214 if (changes.contains(QSL("description"))) { 0215 newDescription = changes.value(QSL("description")).toString(); 0216 } 0217 QString newKeyword = treeNode->keyword(); 0218 if (changes.contains(QSL("keyword"))) { 0219 newKeyword = changes.value(QSL("keyword")).toString(); 0220 } 0221 0222 item->setTitle(newTitle); 0223 item->setDescription(newDescription); 0224 item->setKeyword(newKeyword); 0225 mApp->bookmarks()->changeBookmark(item); 0226 return true; 0227 } 0228 0229 QmlBookmarkTreeNode *QmlBookmarks::get(const QString &string) const 0230 { 0231 const QList<BookmarkItem*> items = mApp->bookmarks()->searchBookmarks(QUrl(string)); 0232 for (BookmarkItem *item : items) { 0233 if (item->urlString() == string) { 0234 return QmlStaticData::instance().getBookmarkTreeNode(item); 0235 } 0236 } 0237 0238 return nullptr; 0239 } 0240 0241 QList<QObject*> QmlBookmarks::getChildren(QObject *object) const 0242 { 0243 QList<QObject*> ret; 0244 0245 auto bookmarkItem = getBookmarkItem(object); 0246 if (!bookmarkItem) { 0247 qWarning() << "Unable to get children:" << "parent not found"; 0248 return ret; 0249 } 0250 0251 const QList<BookmarkItem*> items = bookmarkItem->children(); 0252 for (BookmarkItem *item : items) { 0253 ret.append(QmlStaticData::instance().getBookmarkTreeNode(item)); 0254 } 0255 0256 return ret; 0257 }