File indexing completed on 2024-12-22 04:41:15

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 "qmlwindows.h"
0019 #include "mainapplication.h"
0020 #include "pluginproxy.h"
0021 #include "qml/qmlstaticdata.h"
0022 #include <QQmlEngine>
0023 
0024 QmlWindows::QmlWindows(QObject *parent)
0025     : QObject(parent)
0026 {
0027     connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, [this](BrowserWindow *window){
0028         QmlWindow *qmlWindow = QmlStaticData::instance().getWindow(window);
0029         Q_EMIT created(qmlWindow);
0030     });
0031 
0032     connect(mApp->plugins(), &PluginProxy::mainWindowDeleted, this, [this](BrowserWindow *window){
0033         QmlWindow *qmlWindow = QmlStaticData::instance().getWindow(window);
0034         Q_EMIT removed(qmlWindow);
0035     });
0036 }
0037 
0038 QmlWindow *QmlWindows::get(int id) const
0039 {
0040     return QmlStaticData::instance().getWindow(getBrowserWindow(id));
0041 }
0042 
0043 QmlWindow *QmlWindows::getCurrent() const
0044 {
0045     return QmlStaticData::instance().getWindow(mApp->getWindow());
0046 }
0047 
0048 QList<QObject*> QmlWindows::getAll() const
0049 {
0050     QList<QObject*> list;
0051     const QList<BrowserWindow*> windows = mApp->windows();
0052     list.reserve(windows.size());
0053     for (BrowserWindow *window : windows) {
0054         list.append(QmlStaticData::instance().getWindow(window));
0055     }
0056     return list;
0057 }
0058 
0059 QmlWindow *QmlWindows::create(const QVariantMap &map) const
0060 {
0061     const QUrl url = QUrl::fromEncoded(map.value(QSL("url")).toString().toUtf8());
0062     const Qz::BrowserWindowType type = Qz::BrowserWindowType(map.value(QSL("type"), QmlEnums::NewWindow).toInt());
0063     BrowserWindow *window = mApp->createWindow(type, url);
0064     return QmlStaticData::instance().getWindow(window);
0065 }
0066 
0067 void QmlWindows::remove(int windowId) const
0068 {
0069     BrowserWindow *window = getBrowserWindow(windowId);
0070     window->close();
0071 }
0072 
0073 BrowserWindow *QmlWindows::getBrowserWindow(int windowId) const
0074 {
0075     const QList<BrowserWindow*> windows = mApp->windows();
0076     for (BrowserWindow *window : windows) {
0077         if (QmlStaticData::instance().windowIdHash().value(window, -1) == windowId) {
0078             return window;
0079         }
0080     }
0081 
0082     qWarning() << "Unable to get window with id:" << windowId;
0083     return nullptr;
0084 }