diff --git a/src/lib/plugins/qml/api/bookmarks/qmlbookmarks.cpp b/src/lib/plugins/qml/api/bookmarks/qmlbookmarks.cpp index 9663af14..9d40775d 100644 --- a/src/lib/plugins/qml/api/bookmarks/qmlbookmarks.cpp +++ b/src/lib/plugins/qml/api/bookmarks/qmlbookmarks.cpp @@ -1,350 +1,350 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "qmlbookmarks.h" #include "bookmarks.h" #include Q_GLOBAL_STATIC(QmlBookmarkTreeNodeData, bookmarkTreeNodeData) QmlBookmarks::QmlBookmarks(QObject *parent) : QObject(parent) { connect(mApp->bookmarks(), &Bookmarks::bookmarkAdded, this, [this](BookmarkItem *item){ auto treeNode = bookmarkTreeNodeData->get(item); emit created(treeNode); }); connect(mApp->bookmarks(), &Bookmarks::bookmarkChanged, this, [this](BookmarkItem *item){ auto treeNode = bookmarkTreeNodeData->get(item); emit changed(treeNode); }); connect(mApp->bookmarks(), &Bookmarks::bookmarkRemoved, this, [this](BookmarkItem *item){ auto treeNode = bookmarkTreeNodeData->get(item); emit removed(treeNode); }); } BookmarkItem *QmlBookmarks::getBookmarkItem(QmlBookmarkTreeNode *treeNode) const { auto bookmarks = mApp->bookmarks(); QList items; if (treeNode->url().isEmpty()) { if (isTreeNodeEqualsItem(treeNode, bookmarks->rootItem())) { return bookmarks->rootItem(); } else if (isTreeNodeEqualsItem(treeNode, bookmarks->toolbarFolder())) { return bookmarks->toolbarFolder(); } else if (isTreeNodeEqualsItem(treeNode, bookmarks->menuFolder())) { return bookmarks->menuFolder(); } else if (isTreeNodeEqualsItem(treeNode, bookmarks->unsortedFolder())) { return bookmarks->unsortedFolder(); } items = bookmarks->searchBookmarks(treeNode->title()); } else { items = bookmarks->searchBookmarks(QUrl::fromEncoded(treeNode->url().toUtf8())); } - for (auto item : items) { + for (const auto item : qAsConst(items)) { if (isTreeNodeEqualsItem(treeNode, item)) { return item; } } return nullptr; } BookmarkItem *QmlBookmarks::getBookmarkItem(QObject *object) const { auto treeNode = qobject_cast(object); if (!treeNode) { return nullptr; } auto item = getBookmarkItem(treeNode); if (!item || item->urlString() != treeNode->url()) { return nullptr; } return item; } /** * @brief Checks if the url is bookmarked * @param String representing the url to check * @return true if bookmarked, else false */ bool QmlBookmarks::isBookmarked(const QString &url) const { return mApp->bookmarks()->isBookmarked(QUrl::fromEncoded(url.toUtf8())); } /** * @brief Get the root bookmark item * @return Root boomkark item */ QmlBookmarkTreeNode *QmlBookmarks::rootItem() const { return bookmarkTreeNodeData->get(mApp->bookmarks()->rootItem()); } /** * @brief Get the bookmarks toolbar * @return Bookmarks toolbar */ QmlBookmarkTreeNode *QmlBookmarks::toolbarFolder() const { return bookmarkTreeNodeData->get(mApp->bookmarks()->toolbarFolder()); } /** * @brief Get the bookmarks menu folder * @return Bookmarks menu folder */ QmlBookmarkTreeNode *QmlBookmarks::menuFolder() const { return bookmarkTreeNodeData->get(mApp->bookmarks()->menuFolder()); } /** * @brief Get the unsorted bookmarks folder * @return Unsorted bookmarks folder */ QmlBookmarkTreeNode *QmlBookmarks::unsortedFolder() const { return bookmarkTreeNodeData->get(mApp->bookmarks()->unsortedFolder()); } /** * @brief Get the last used bookmarks folder * @return Last used bookmarks folder */ QmlBookmarkTreeNode *QmlBookmarks::lastUsedFolder() const { return bookmarkTreeNodeData->get(mApp->bookmarks()->lastUsedFolder()); } /** * @brief Creates a bookmark item * @param A JavaScript object containing * - parent: * Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode), representing * the parent of the new bookmark item. This is required field. * - title: * String representing the title of the new bookmark item. Defaults to empty string * - url: * String representing the url of the new bookmark item. Defaults to empty string * - description * String representing the description of the new bookmark item. Defaults to empty string * - type: * [Type](@ref QmlBookmarkTreeNode::Type) representing the type of the new bookmark item. * Defaults to [Url](@ref QmlBookmarkTreeNode::Url) if url is provided, else * [Folder](@ref QmlBookmarkTreeNode::Folder) if title is provided, else * [Invalid](@ref QmlBookmarkTreeNode::Invalid) * @return true if the bookmark it created, else false */ bool QmlBookmarks::create(const QVariantMap &map) const { if (!map.contains(QSL("parent"))) { qWarning() << "Unable to create new bookmark:" << "parent not found"; return false; } const QString title = map.value(QSL("title")).toString(); const QString urlString = map.value(QSL("url")).toString(); const QString description = map.value(QSL("description")).toString(); BookmarkItem::Type type; if (map.contains(QSL("type"))) { type = BookmarkItem::Type(map.value(QSL("type")).toInt()); } else if (urlString.isEmpty()){ if (!title.isEmpty()) { type = BookmarkItem::Folder; } else { type = BookmarkItem::Invalid; } } else { type = BookmarkItem::Url; } auto object = map.value(QSL("parent")).value(); auto parent = getBookmarkItem(object); if (!parent) { qWarning() << "Unable to create new bookmark:" << "parent not found"; return false; } auto item = new BookmarkItem(type); item->setTitle(title); item->setUrl(QUrl::fromEncoded(urlString.toUtf8())); item->setDescription(description); mApp->bookmarks()->addBookmark(parent, item); return true; } /** * @brief Removes a bookmark item * @param treeNode: * Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode) to be removed * @return true if the bookmark is removed, else false */ bool QmlBookmarks::remove(QmlBookmarkTreeNode *treeNode) const { auto item = getBookmarkItem(treeNode); if (!item) { qWarning() << "Unable to remove bookmark:" <<"BookmarkItem not found"; return false; } return mApp->bookmarks()->removeBookmark(item); } /** * @brief QmlBookmarks::search * @param A JavaScript object containing * - query: * String containing search query * - url: * String representing url to be search * @return List containing [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode). If both * query and url are not supplied then empty list is returned. */ QList QmlBookmarks::search(const QVariantMap &map) const { if (!map.contains(QSL("query")) && !map.contains(QSL("url"))) { qWarning() << "Unable to search bookmarks"; return QList(); } const QString query = map.value(QSL("query")).toString(); const QString urlString = map.value(QSL("url")).toString(); QList items; if (urlString.isEmpty()) { items = mApp->bookmarks()->searchBookmarks(query); } else { items = mApp->bookmarks()->searchBookmarks(QUrl::fromEncoded(urlString.toUtf8())); } QList ret; - for (auto item : items) { + for (auto item : qAsConst(items)) { ret.append(bookmarkTreeNodeData->get(item)); } return ret; } /** * @brief Updates a bookmark item * @param Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode), representing the bookmark * to update * @param JavaScript object containing the values to be updated * - title: * String representing the new title of the bookmark item * - description: * String representing the new description of the bookmark item * - keyword: * String representing the new keyword of the bookmark item * @return true if the bookmark is updated, else false */ bool QmlBookmarks::update(QObject *object, const QVariantMap &changes) const { auto treeNode = qobject_cast(object); if (!treeNode) { qWarning() << "Unable to update bookmark:" << "unable to cast QVariant to QmlBookmarkTreeNode"; return false; } auto item = getBookmarkItem(treeNode); if (!item) { qWarning() << "Unable to update bookmark:" << "bookmark not found"; return false; } if (!mApp->bookmarks()->canBeModified(item)) { qWarning() << "Unable to update bookmark:" << "bookmark can not be modified"; } QString newTitle = treeNode->title(); if (changes.contains(QSL("title"))) { newTitle = changes.value(QSL("title")).toString(); } QString newDescription = treeNode->description(); if (changes.contains(QSL("description"))) { newDescription = changes.value(QSL("description")).toString(); } QString newKeyword = treeNode->keyword(); if (changes.contains(QSL("keyword"))) { newKeyword = changes.value(QSL("keyword")).toString(); } item->setTitle(newTitle); item->setDescription(newDescription); item->setKeyword(newKeyword); mApp->bookmarks()->changeBookmark(item); return true; } /** * @brief Get the first matched bookmark item * @param String representing the query * @return Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode) if * the query is matched with a bookmark, else null */ QmlBookmarkTreeNode *QmlBookmarks::get(const QString &string) const { auto items = mApp->bookmarks()->searchBookmarks(QUrl(string)); - for (auto item : items) { + for (const auto item : qAsConst(items)) { if (item->urlString() == string) { return bookmarkTreeNodeData->get(item); } } return nullptr; } /** * @brief Get children of the bookmark item * @param Object of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode), representing * the parent whose children are requested. * @return List containing the children, of type [QmlBookmarkTreeNode](@ref QmlBookmarkTreeNode) */ QList QmlBookmarks::getChildren(QObject *object) const { QList ret; auto bookmarkItem = getBookmarkItem(object); if (!bookmarkItem) { qWarning() << "Unable to get children:" << "parent not found"; return ret; } auto items = bookmarkItem->children(); - for (auto item : items) { + for (auto item : qAsConst(items)) { ret.append(bookmarkTreeNodeData->get(item)); } return ret; } bool QmlBookmarks::isTreeNodeEqualsItem(QmlBookmarkTreeNode *treeNode, BookmarkItem *item) const { return treeNode->title() == item->title() && treeNode->url() == item->urlString() && treeNode->description() == item->description() && (int)(treeNode->type()) == (int)(item->type()); } diff --git a/src/lib/plugins/qml/api/bookmarks/qmlbookmarktreenode.cpp b/src/lib/plugins/qml/api/bookmarks/qmlbookmarktreenode.cpp index f3d1cba3..1648ae79 100644 --- a/src/lib/plugins/qml/api/bookmarks/qmlbookmarktreenode.cpp +++ b/src/lib/plugins/qml/api/bookmarks/qmlbookmarktreenode.cpp @@ -1,142 +1,142 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "qmlbookmarktreenode.h" #include "mainapplication.h" #include "bookmarks.h" #include Q_GLOBAL_STATIC(QmlBookmarkTreeNodeData, bookmarkTreeNodeData) QmlBookmarkTreeNode::QmlBookmarkTreeNode(BookmarkItem *item) : m_item(item) { QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); } QmlBookmarkTreeNode::Type QmlBookmarkTreeNode::type() const { if (!m_item) { return Invalid; } switch (m_item->type()) { case BookmarkItem::Root: return Root; case BookmarkItem::Url: return Url; case BookmarkItem::Folder: return Folder; case BookmarkItem::Separator: return Separator; case BookmarkItem::Invalid: default: return Invalid; } } QString QmlBookmarkTreeNode::title() const { if (!m_item) { return QString(); } return m_item->title(); } QString QmlBookmarkTreeNode::url() const { if (!m_item) { return QString(); } return m_item->urlString(); } QString QmlBookmarkTreeNode::description() const { if (!m_item) { return QString(); } return m_item->description(); } QString QmlBookmarkTreeNode::keyword() const { if (!m_item) { return QString(); } return m_item->keyword(); } int QmlBookmarkTreeNode::visitCount() const { if (!m_item) { return 0; } return m_item->visitCount(); } QmlBookmarkTreeNode *QmlBookmarkTreeNode::parent() const { if (!m_item) { return nullptr; } return bookmarkTreeNodeData->get(m_item->parent()); } bool QmlBookmarkTreeNode::unmodifiable() const { return !mApp->bookmarks()->canBeModified(m_item); } QList QmlBookmarkTreeNode::children() const { const auto items = m_item->children(); QList ret; - for (const auto &item : items) { + for (const auto &item : qAsConst(items)) { ret.append(bookmarkTreeNodeData->get(item)); } return ret; } QmlBookmarkTreeNodeData::QmlBookmarkTreeNodeData() { } QmlBookmarkTreeNodeData::~QmlBookmarkTreeNodeData() { qDeleteAll(m_nodes); } QmlBookmarkTreeNode *QmlBookmarkTreeNodeData::get(BookmarkItem *item) { QmlBookmarkTreeNode *node = m_nodes.value(item); if (!node) { node = new QmlBookmarkTreeNode(item); m_nodes.insert(item, node); } return node; } diff --git a/src/lib/plugins/qml/api/fileutils/qmlfileutils.cpp b/src/lib/plugins/qml/api/fileutils/qmlfileutils.cpp index 02adfcfa..8e6b0162 100644 --- a/src/lib/plugins/qml/api/fileutils/qmlfileutils.cpp +++ b/src/lib/plugins/qml/api/fileutils/qmlfileutils.cpp @@ -1,62 +1,62 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "qmlfileutils.h" #include "datapaths.h" #include "qztools.h" #include #include QmlFileUtils::QmlFileUtils(QString filePath, QObject *parent) : QObject(parent) { // Get the plugin root directory - of the form // some-path-in-plugin-paths/qml/plugin-directory const QStringList dirs = DataPaths::allPaths(DataPaths::Plugins); - for (QString path : dirs) { + for (QString path : qAsConst(dirs)) { path.append(QSL("/qml/")); if (filePath.contains(path)) { m_path = path; QString pluginDirName = filePath.remove(path).split(QSL("/"))[0]; m_path.append(pluginDirName); m_path.append(QSL("/")); break; } } } QString QmlFileUtils::resolve(const QString &filePath) { const QUrl resolvedUrl = QUrl::fromLocalFile(m_path).resolved(QUrl::fromEncoded(filePath.toUtf8())); const QString resolvedPath = resolvedUrl.toLocalFile(); if (resolvedPath.contains(m_path)) { return resolvedPath; } return QString(); } QByteArray QmlFileUtils::readAllFileContents(const QString &fileName) { const QString path = resolve(fileName); return QzTools::readAllFileByteContents(path); } bool QmlFileUtils::exists(const QString &filePath) { const QString path = resolve(filePath); return QFile(path).exists(); } diff --git a/src/lib/plugins/qml/api/history/qmlhistory.cpp b/src/lib/plugins/qml/api/history/qmlhistory.cpp index 0c83b246..539417d7 100644 --- a/src/lib/plugins/qml/api/history/qmlhistory.cpp +++ b/src/lib/plugins/qml/api/history/qmlhistory.cpp @@ -1,123 +1,123 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "qmlhistory.h" #include "mainapplication.h" #include "history.h" Q_GLOBAL_STATIC(QmlHistoryItemData, historyItemData) QmlHistory::QmlHistory(QObject *parent) : QObject(parent) { connect(mApp->history(), &History::historyEntryAdded, this, [this](HistoryEntry entry){ QmlHistoryItem *historyItem = historyItemData->get(&entry); emit visited(historyItem); }); connect(mApp->history(), &History::historyEntryDeleted, this, [this](HistoryEntry entry){ QmlHistoryItem *historyItem = historyItemData->get(&entry); emit visitRemoved(historyItem); }); } /** * @brief Searches History Entries against a search query * @param String representing the search query * @return List of History Entries, each of type [QmlHistoryItem](@ref QmlHistoryItem), * matching the search query */ QList QmlHistory::search(const QString &text) { QList list; QList result = mApp->history()->searchHistoryEntry(text); - foreach(auto entry, result) { + for (const auto entry : qAsConst(result)) { auto item = historyItemData->get(entry); list.append(item); } return list; } /** * @brief Get the visit count of a url * @param String representing the url * @return Integer representing the visit count of the given url */ int QmlHistory::getVisits(const QString &url) { HistoryEntry *entry = mApp->history()->getHistoryEntry(url); return entry->count; } /** * @brief Add url to the history * @param A JavaScript object containing * - title: * String representing the title of the hisotry entry * - url: * String representing the url of the history entry */ void QmlHistory::addUrl(const QVariantMap &map) { if (!map.contains(QSL("title")) || !map.contains(QSL("url"))) { qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__; return; } QString title = map.value(QSL("title")).toString(); const QString url = map.value(QSL("url")).toString(); title = title.isEmpty() ? url : title; mApp->history()->addHistoryEntry(QUrl::fromEncoded(url.toUtf8()), title); } /** * @brief Deletes a url from the history * @param String representing the url of the history entry */ void QmlHistory::deleteUrl(const QString &url) { mApp->history()->deleteHistoryEntry(url); } /** * @brief Deletes history entries within the given range * @param A JavaScript object containing * - startTime: * A JavaScript Date object representing the start time * - endTime: * A JavaScript Date object representing the end time */ void QmlHistory::deleteRange(const QVariantMap &map) { if (!map.contains(QSL("startTime")) || !map.contains(QSL("endTime"))) { qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__; return; } const double startTime = map.value(QSL("startTime")).toDouble(); const double endTime = map.value(QSL("endTime")).toDouble(); mApp->history()->deleteRange(startTime, endTime); } /** * @brief Clears all the history */ void QmlHistory::deleteAll() { mApp->history()->clearHistory(); } diff --git a/src/lib/plugins/qml/api/menus/qmlaction.cpp b/src/lib/plugins/qml/api/menus/qmlaction.cpp index c6e709db..2895a934 100644 --- a/src/lib/plugins/qml/api/menus/qmlaction.cpp +++ b/src/lib/plugins/qml/api/menus/qmlaction.cpp @@ -1,69 +1,72 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "qmlaction.h" #include "qztools.h" #include "qml/api/fileutils/qmlfileutils.h" QmlAction::QmlAction(QAction *action, QObject *parent) : QObject(parent) , m_action(action) { connect(m_action, &QAction::triggered, this, &QmlAction::triggered); } void QmlAction::setProperties(const QVariantMap &map) { if (!m_action) { return; } - for (const QString &key : map.keys()) { + QMapIterator it(map); + while (it.hasNext()) { + it.next(); + const QString key = it.key(); if (key == QSL("icon")) { QString iconPath = map.value(key).toString(); QIcon icon; if (QIcon::hasThemeIcon(iconPath)) { icon = QIcon::fromTheme(iconPath); } else if (iconPath.startsWith(QSL(":"))) { // Icon is loaded from falkon resource icon = QIcon(iconPath); } else { QmlFileUtils fileUtils(m_pluginPath); icon = QIcon(fileUtils.resolve(iconPath)); } m_action->setIcon(icon); } else if (key == QSL("shortcut")) { m_action->setShortcut(QKeySequence(map.value(key).toString())); } else { m_action->setProperty(key.toUtf8(), map.value(key)); } } } /** * @brief Updates the properties of the action * @param A JavaScript object containing the updated properties of the action. */ void QmlAction::update(const QVariantMap &map) { setProperties(map); } void QmlAction::setPluginPath(const QString &path) { m_pluginPath = path; } diff --git a/src/lib/plugins/qml/api/menus/qmlmenu.cpp b/src/lib/plugins/qml/api/menus/qmlmenu.cpp index 85ba04fb..35a68b1b 100644 --- a/src/lib/plugins/qml/api/menus/qmlmenu.cpp +++ b/src/lib/plugins/qml/api/menus/qmlmenu.cpp @@ -1,106 +1,108 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "qmlmenu.h" #include "qztools.h" #include "qml/api/fileutils/qmlfileutils.h" #include QmlMenu::QmlMenu(QMenu *menu, QObject *parent) : QObject(parent) , m_menu(menu) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); connect(m_menu, &QMenu::triggered, this, &QmlMenu::triggered); } /** * @brief Adds action to menu * @param A JavaScript object containing properties for action. * The icon property must be in form of url of the path * and shortcut in form string. * @return action of type [QmlAction](@ref QmlAction) */ QmlAction *QmlMenu::addAction(const QVariantMap &map) { if (!m_menu) { return nullptr; } QAction *action = new QAction(); QmlAction *qmlAction = new QmlAction(action, this); qmlAction->setPluginPath(m_pluginPath); qmlAction->setProperties(map); m_menu->addAction(action); return qmlAction; } /** * @brief Adds sub-menu to menu * @param A JavaScript object containing properties of menu. * The icon property must be in form of url of the path. * @return menu of type [QmlMenu](@ref QmlMenu) */ QmlMenu *QmlMenu::addMenu(const QVariantMap &map) { if (!m_menu) { return nullptr; } QMenu *newMenu = new QMenu(); - for (const QString &key : map.keys()) { + QMapIterator it(map); + while (it.hasNext()) { + const QString key = it.key(); if (key == QSL("icon")) { QString iconPath = map.value(key).toString(); QIcon icon; if (QIcon::hasThemeIcon(iconPath)) { icon = QIcon::fromTheme(iconPath); } else if (iconPath.startsWith(QSL(":"))) { // Icon is loaded from falkon resource icon = QIcon(iconPath); } else { QmlFileUtils fileUtils(m_pluginPath); icon = QIcon(fileUtils.resolve(iconPath)); } newMenu->setIcon(icon); continue; } newMenu->setProperty(key.toUtf8(), map.value(key)); } m_menu->addMenu(newMenu); QmlMenu *newQmlMenu = new QmlMenu(newMenu, this); newQmlMenu->setPluginPath(m_pluginPath); return newQmlMenu; } /** * @brief Adds a separator to menu */ void QmlMenu::addSeparator() { if (!m_menu) { return; } m_menu->addSeparator(); } void QmlMenu::setPluginPath(const QString &path) { m_pluginPath = path; } diff --git a/src/lib/plugins/qml/api/tabs/qmltabs.cpp b/src/lib/plugins/qml/api/tabs/qmltabs.cpp index 74a0553b..a423e8cf 100644 --- a/src/lib/plugins/qml/api/tabs/qmltabs.cpp +++ b/src/lib/plugins/qml/api/tabs/qmltabs.cpp @@ -1,502 +1,502 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "qmltabs.h" #include "tabwidget.h" #include "pluginproxy.h" Q_GLOBAL_STATIC(QmlTabData, tabData) QmlTabs::QmlTabs(QObject *parent) : QObject(parent) { for (BrowserWindow *window : mApp->windows()) { windowCreated(window); } connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &QmlTabs::windowCreated); } /** * @brief Sets the current tab in a window * @param A JavaScript object containing * - index: * Integer representing new current index * - windowId: * The id of window containing the tab * @return True if success, else false */ bool QmlTabs::setCurrentIndex(const QVariantMap &map) { if (!map.contains(QSL("index"))) { qWarning() << "Unable to set current index:" << "index not defined"; return false; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return false; } window->tabWidget()->setCurrentIndex(index); return true; } /** * @brief Sets the next tab as current tab * @param Integer representing the window * @return True if success, else false */ bool QmlTabs::nextTab(int windowId) { const auto window = getWindow(windowId); if (!window) { return false; } window->tabWidget()->nextTab(); return true; } /** * @brief Sets the prvious tab as current tab * @param Integer representing the window * @return True if success, else false */ bool QmlTabs::previousTab(int windowId) { const auto window = getWindow(windowId); if (!window) { return false; } window->tabWidget()->previousTab(); return true; } /** * @brief Moves a tab * @param A JavaScript object containing * - from: * The initial index of the tab * - to: * The final index of the tab * - windowId: * The id of window containing the tab * @return True if tab is moved, else false */ bool QmlTabs::moveTab(const QVariantMap &map) { if (!map.contains(QSL("from"))) { qWarning() << "Unable to move tab:" << "from not defined"; return false; } if (!map.contains(QSL("to"))) { qWarning() << "Unable to move tab:" << "to not defined"; return false; } const int from = map.value(QSL("from")).toInt(); const int to = map.value(QSL("to")).toInt(); const auto window = getWindow(map); if (!window) { return false; } window->tabWidget()->moveTab(from, to); return true; } /** * @brief Pins a tab * @param A JavaScript object containing * - index: * Integer representing the tab to be pinned * - windowId: * The id of window containing the tab * @return True if success, else false */ bool QmlTabs::pinTab(const QVariantMap &map) { if (!map.contains(QSL("index"))) { qWarning() << "Unable to pin tab:" << "index not defined"; return false; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return false; } WebTab *webTab = window->tabWidget()->webTab(index); if (webTab->isPinned()) { return false; } webTab->togglePinned(); return true; } /** * @brief Un-pins a tab * @param A JavaScript object containing * - index: * Integer representing the tab to be unpinned * - windowId: * The id of window containing the tab * @return True if success, else false */ bool QmlTabs::unpinTab(const QVariantMap &map) { if (!map.contains(QSL("index"))) { qWarning() << "Unable to unpin tab:" << "index not defined"; return false; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return false; } WebTab *webTab = window->tabWidget()->webTab(index); if (!webTab->isPinned()) { return false; } webTab->togglePinned(); return true; } /** * @brief Detaches a tab * @param A JavaScript object containing * - index: * Integer representing the tab to be detached * - windowId: * The id of window containing the tab * @return True if tab is detached, else false */ bool QmlTabs::detachTab(const QVariantMap &map) { if (!map.contains(QSL("index"))) { qWarning() << "Unable to detatch tab:" << "index not defined"; return false; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return false; } window->tabWidget()->detachTab(index); return true; } /** * @brief Duplicates a tab * @param A JavaScript object containing * - index: * Integer representing the tab to duplicate * - windowId: * The id of window containing the tab * @return True if success, else false */ bool QmlTabs::duplicate(const QVariantMap &map) { if (!map.contains(QSL("index"))) { qWarning() << "Unable to duplicate:" << "index not defined"; return false; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return false; } window->tabWidget()->duplicateTab(index); return true; } /** * @brief Close a tab * @param A JavaScript object containing * - index: * Integer representing the tab to be closed * - windowId: * The id of window containing the tab * @return True if success, else false */ bool QmlTabs::closeTab(const QVariantMap &map) { if (!map.contains(QSL("index"))) { qWarning() << "Unable to close tab:" << "index not defined"; return false; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return false; } window->tabWidget()->closeTab(index); return true; } /** * @brief Reloads a tab * @param A JavaScript object containing * - index: * Integer representing the tab to be reloaded * - windowId: * The id of window containing the tab * @return True if success, else false */ bool QmlTabs::reloadTab(const QVariantMap &map) { if (!map.contains(QSL("index"))) { qWarning() << "Unable to reload tab:" << "index not defined"; return false; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return false; } window->tabWidget()->reloadTab(index); return true; } /** * @brief Stops a tab * @param A JavaScript object containing * - index: * Integer representing the tab to be stoped * - windowId: * The id of window containing the tab * @return True if success, else false */ bool QmlTabs::stopTab(const QVariantMap &map) { if (!map.contains(QSL("index"))) { qWarning() << "Unable to close tab:" << "index not defined"; return false; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return false; } window->tabWidget()->stopTab(index); return true; } /** * @brief Gets a tab * @param A JavaScript object contining * - index: * Integer representign the index of the tab * - windowId: * The id of window containing the tab * @return Tab of type [QmlTab](@ref QmlTab) if exists, else null */ QmlTab *QmlTabs::get(const QVariantMap &map) const { if (!map.contains(QSL("index"))) { qWarning() << "Unable to set current index:" << "index not defined"; return nullptr; } const int index = map.value(QSL("index")).toInt(); const auto window = getWindow(map); if (!window) { return nullptr; } const auto webTab = window->tabWidget()->webTab(index); return tabData->get(webTab); } /** * @brief Get the normal tabs count in a window * @param Integer representing the window * @return Number of normal tabs in the window */ int QmlTabs::normalTabsCount(int windowId) const { const auto window = getWindow(windowId); if (!window) { return -1; } return window->tabWidget()->normalTabsCount(); } /** * @brief Get the pinned tabs count in a window * @param Integer representing the window * @return Number of pinned tabs in the window */ int QmlTabs::pinnedTabsCount(int windowId) const { const auto window = getWindow(windowId); if (!window) { return -1; } return window->tabWidget()->pinnedTabsCount(); } /** * @brief Gets all the tabs of a window * @param A JavaScript object containing * - windowId: * The id of window containing the tab * - withPinned: * Bool representing if the searched tab can be pinned * @return List of tabs, each of type [QmlTab](@ref QmlTab) */ QList QmlTabs::getAll(const QVariantMap &map) const { const auto window = getWindow(map); if (!window) { return QList(); } const bool withPinned = map.value(QSL("withPinned")).toBool(); const auto tabList = window->tabWidget()->allTabs(withPinned); QList list; for (auto tab : tabList) { list.append(tabData->get(tab)); } return list; } /** * @brief Searches tabs against a criteria * @param A JavaScript object containing * - title: * String representing the title to be searched * - url: * String representing the url to be searched * - withPinned: * Bool representing if the searched tab can be pinned * @return List of tabs, each of type [QmlTab](@ref QmlTab), which are * matched against the criteria */ QList QmlTabs::search(const QVariantMap &map) { const QString title = map.value(QSL("title")).toString(); const QString url = map.value(QSL("url")).toString(); const bool withPinned = map.value(QSL("withPinned")).toBool(); QList list; foreach (BrowserWindow *window, mApp->windows()) { foreach (WebTab *webTab, window->tabWidget()->allTabs(withPinned)) { if (webTab->title().contains(title, Qt::CaseInsensitive) || QString::fromUtf8(webTab->url().toEncoded()).contains(url, Qt::CaseInsensitive)) { list.append(tabData->get(webTab)); } } } return list; } /** * @brief Adds a tab * @param A JavaScript object containing * - url: * String representing the url of the tab * - windowId: * The id of window containing the tab * @return True if the tab is added, else false */ bool QmlTabs::addTab(const QVariantMap &map) { const QString urlString = map.value(QSL("url")).toString(); const auto window = getWindow(map); if (!window) { qDebug() << "Unable to add tab:" << "window not found"; return false; } LoadRequest req(QUrl::fromEncoded(urlString.toUtf8())); const int ret = window->tabWidget()->addView(req); return ret != -1 ? true : false; } BrowserWindow *QmlTabs::getWindow(const QVariantMap &map) const { const int windowId = map.value(QSL("windowId"), -1).toInt(); return getWindow(windowId); } BrowserWindow *QmlTabs::getWindow(int windowId) const { if (windowId == -1) { return mApp->getWindow(); } - for(BrowserWindow *window : mApp->windowIdHash().keys()) { + for (BrowserWindow *window : mApp->windowIdHash().keys()) { if (mApp->windowIdHash().value(window) == windowId) { return window; } } qWarning() << "Unable to get window with given windowId"; return nullptr; } void QmlTabs::windowCreated(BrowserWindow *window) { const int windowId = mApp->windowIdHash().value(window); connect(window->tabWidget(), &TabWidget::changed, this, [this, windowId]{ emit changed(windowId); }); connect(window->tabWidget(), &TabWidget::tabInserted, this, [this, windowId](int index){ QVariantMap map; map.insert(QSL("windowId"), windowId); map.insert(QSL("index"), index); emit tabInserted(map); }); connect(window->tabWidget(), &TabWidget::tabRemoved, this, [this, windowId](int index){ QVariantMap map; map.insert(QSL("windowId"), windowId); map.insert(QSL("index"), index); emit tabRemoved(map); }); connect(window->tabWidget(), &TabWidget::tabMoved, this, [this, windowId](int from, int to){ QVariantMap map; map.insert(QSL("windowId"), windowId); map.insert(QSL("from"), from); map.insert(QSL("to"), to); emit tabMoved(map); }); } diff --git a/src/lib/plugins/qml/api/userscript/qmluserscripts.cpp b/src/lib/plugins/qml/api/userscript/qmluserscripts.cpp index dacb0790..2842f805 100644 --- a/src/lib/plugins/qml/api/userscript/qmluserscripts.cpp +++ b/src/lib/plugins/qml/api/userscript/qmluserscripts.cpp @@ -1,154 +1,154 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "qmluserscripts.h" #include "mainapplication.h" #include #include QmlUserScripts::QmlUserScripts(QObject *parent) : QObject(parent) { } QmlUserScripts::~QmlUserScripts() { // remove scripts added by the plugin - for (const QWebEngineScript &webEngineScript : m_webEngineScripts) { + for (const QWebEngineScript &webEngineScript : qAsConst(m_webEngineScripts)) { mApp->webProfile()->scripts()->remove(webEngineScript); } } int QmlUserScripts::count() const { return mApp->webProfile()->scripts()->count(); } int QmlUserScripts::size() const { return mApp->webProfile()->scripts()->size(); } bool QmlUserScripts::empty() const { return mApp->webProfile()->scripts()->isEmpty(); } QList QmlUserScripts::toQObjectList(QList list) const { QList userScriptList; - for (const QWebEngineScript &script : list) { + for (const QWebEngineScript &script : qAsConst(list)) { QmlUserScript *userScript = new QmlUserScript(); userScript->setWebEngineScript(script); userScriptList.append(userScript); } return userScriptList; } /** * @brief Checks if the script is in collection * @param object of type QmlUserScript * @return true if the the script in in collection, else false */ bool QmlUserScripts::contains(QObject *object) const { QmlUserScript *userScript = qobject_cast(object); if (!userScript) { return false; } QWebEngineScript webEngineScript = userScript->webEngineScript(); return mApp->webProfile()->scripts()->contains(webEngineScript); } /** * @brief Finds a script in collection by name * @param name of the script * @return object of type QmlUserScript, representing the script of given name */ QObject *QmlUserScripts::findScript(const QString &name) const { QWebEngineScript webEngineScript = mApp->webProfile()->scripts()->findScript(name); QmlUserScript *qmlUserScript = new QmlUserScript(); qmlUserScript->setWebEngineScript(webEngineScript); return qmlUserScript; } /** * @brief Finds all scripts in collection by a given name * @return list of objects, each of type QmlUserScript, representing the script of given name */ QList QmlUserScripts::findScripts(const QString &name) const { QList list = mApp->webProfile()->scripts()->findScripts(name); return toQObjectList(list); } /** * @brief Inserts a script into collection * @param object of type QmlUserScript */ void QmlUserScripts::insert(QObject *object) { QmlUserScript *userScript = qobject_cast(object); if (!userScript) { return; } QWebEngineScript webEngineScript = userScript->webEngineScript(); mApp->webProfile()->scripts()->insert(webEngineScript); m_webEngineScripts.append(webEngineScript); } /** * @brief Inserts a list of scripts into collection * @param list of objects, each of type QmlUserScript */ void QmlUserScripts::insert(const QList &list) { for (QObject *object : list) { QmlUserScript *userScript = qobject_cast(object); if (!userScript) { continue; } QWebEngineScript webEngineScript = userScript->webEngineScript(); mApp->webProfile()->scripts()->insert(webEngineScript); m_webEngineScripts.append(webEngineScript); } } /** * @brief Removes a script from collection * @param object of type QmlUserScript */ void QmlUserScripts::remove(QObject *object) const { QmlUserScript *userScript = qobject_cast(object); if (!userScript) { return; } QWebEngineScript webEngineScript = userScript->webEngineScript(); mApp->webProfile()->scripts()->remove(webEngineScript); } /** * @brief Gets all the scripts of the collection * @return list of objects, each of type QmlUserScript */ QList QmlUserScripts::toList() const { QList list = mApp->webProfile()->scripts()->toList(); return toQObjectList(list); }