aboutsummaryrefslogtreecommitdiff
path: root/src/browser.cpp
blob: 512f2074c119ffee07b50f8ef8ef01856253db85 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * 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/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "browser.h"
#include "addressbar/addressbar.h"
#include "mainwindow/mainwindow.h"
#include "mainwindow/window.h"
#include "webengine/urlinterceptor.h"
#include "webengine/webprofile.h"
#include <QAction>
#include <bookmarks/bookmarkswidget.h>
#include <configuration/configuration.h>
#include <downloads/downloadswidget.h>
#include <version.h>
#include <QDir>
#include <QPluginLoader>

QVector<Plugin> loadPlugins(const QString &location)
{
    QDir pluginsDir(location);
    QVector<Plugin> list;

    if(pluginsDir.exists()) {
        const QStringList entries = pluginsDir.entryList(QDir::Files | QDir::Readable);

        for(const QString &name : entries) {
            QPluginLoader loader(pluginsDir.absoluteFilePath(name));

            if(loader.load()) {
#ifdef QT_DEBUG
                qDebug("Loading plugin: %s [ok]", qUtf8Printable(name));
#endif
                Plugin p;
                p.instance = std::shared_ptr<QObject>(loader.instance());
                list.append(p);
            } else {
#ifdef QT_DEBUG
                qDebug("Loading plugin: %s [failed]", qUtf8Printable(name));
#endif
            }
        }
    }

    return list;
}

Browser::Browser(int &argc, char *argv[])
    : SingleApplication(argc, argv)
{
    setApplicationName("smolbote");
    setWindowIcon(QIcon(":/icon.svg"));
    setApplicationVersion(SMOLBOTE_VERSION);
}

Browser::~Browser()
{
    if(m_bookmarks)
        m_bookmarks->save();

    qDeleteAll(m_windows);
    m_windows.clear();

    //qDeleteAll(m_plugins);
    m_plugins.clear();
}

void Browser::setConfiguration(std::shared_ptr<Configuration> &config)
{
    Q_ASSERT(config);
    m_config = config;
}

void Browser::setup(const QString &defaultProfile)
{
    Q_ASSERT_X(m_config, "Browser::setup", "Configuration not set");

    // load profiles
    if(defaultProfile == "") {
        auto *p = new WebProfile(this);
        p->loadProfile(m_config->section("profile"));
        WebProfile::setDefaultProfile(p);
    } else {
        auto *p = new WebProfile(defaultProfile, this);
        WebProfile::setDefaultProfile(p);
    }

    // plugins
    m_plugins.append(loadPlugins(QString::fromStdString(m_config->value<std::string>("plugins.path").value())));

    // register commands
    for(Plugin p : m_plugins) {
        auto *plugin = qobject_cast<PluginInterface *>(p.instance.get());
        if(plugin) {
            m_commands.unite(plugin->commands());
        }
        if(p.instance->inherits("ProfileInterface")) {
            auto *profileEditor = qobject_cast<ProfileInterface *>(p.instance.get());
            Q_ASSERT_X(profileEditor != nullptr, "Browser::setup", "profile interface cast failed");
            m_commands.insert("profileEditor:editDefaultProfile", [profileEditor]() -> int {
                return profileEditor->createWidget(WebProfile::defaultProfile(), nullptr)->exec();
            });
        }
    }

    // url request filter
    m_urlFilter = std::make_shared<UrlRequestInterceptor>(QString::fromStdString(m_config->value<std::string>("filter.path").value()));
    WebProfile::defaultProfile()->setRequestInterceptor(m_urlFilter.get());

    // cookie request filter

    // bookmarks
    m_bookmarks = std::make_shared<BookmarksWidget>(QString::fromStdString(m_config->value<std::string>("bookmarks.path").value()));
    connect(m_bookmarks.get(), &BookmarksWidget::openUrl, this, [this](const QUrl &url) {
        m_windows.last()->createTab(url);
    });
    connect(WebProfile::defaultProfile(), &WebProfile::addBookmarkRequested, m_bookmarks.get(), &BookmarksWidget::addBookmark);

    // downloads
    m_downloads = std::make_shared<DownloadsWidget>(QString::fromStdString(m_config->value<std::string>("downloads.path").value()));
    connect(WebProfile::defaultProfile(), &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload);
}

int Browser::command(const QString &command)
{
    if(m_commands.contains(command)) {
        return m_commands.value(command)();
    } else {
        qWarning("No such command: %s", qUtf8Printable(command));
        return -1;
    }
}

void Browser::createSession(const QString &profileName, bool newWindow, const QStringList &urls)
{
    if(m_windows.isEmpty()) {
        createWindow();
    }

    auto *mainwindow = m_windows.last();
    if(newWindow) {
        QString firstUrl;// = WebProfile::defaultProfile()->homepage();
        if(!urls.isEmpty())
            firstUrl = urls.at(0);
        auto *w = mainwindow->createSubWindow(firstUrl);
        for(int i = 1; i < urls.count() - 1; i++) {
            w->addTab(QUrl::fromUserInput(urls.at(i)));
        }
    } else {
        if(urls.isEmpty())
            mainwindow->createTab(WebProfile::defaultProfile()->homepage());
        else {
            for(const QString &url : urls) {
                mainwindow->createTab(QUrl::fromUserInput(url));
            }
        }
    }
}

MainWindow *Browser::createWindow()
{
    // the window will delete itself when it closes, so we don't need to delete it
    MainWindow *window = new MainWindow(m_config);
    connect(window->addressBar, &AddressBar::complete, m_bookmarks.get(), &BookmarksWidget::search);

    auto *bookmarksAction = new QAction(tr("Bookmarks"), window);
    bookmarksAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value<std::string>("bookmarks.shortcut").value())));
    connect(bookmarksAction, &QAction::triggered, window, [this, window]() {
        bool wasVisible = m_bookmarks->isVisible();
        for(MainWindow *w : m_windows) {
            w->removeDockWidget(m_bookmarks.get());
        }
        if(!wasVisible) {
            window->addDockWidget(Qt::RightDockWidgetArea, m_bookmarks.get());
        }
    });
    window->addAction(MainWindow::ToolsMenu, bookmarksAction);

    auto *downloadsAction = new QAction(tr("Downloads"), window);
    downloadsAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value<std::string>("downloads.shortcut").value())));
    connect(downloadsAction, &QAction::triggered, window, [this, window]() {
        bool wasVisible = m_downloads->isVisible();
        for(MainWindow *w : m_windows) {
            w->removeDockWidget(m_downloads.get());
        }
        if(!wasVisible) {
            window->addDockWidget(Qt::RightDockWidgetArea, m_downloads.get());
        }
    });
    window->addAction(MainWindow::ToolsMenu, downloadsAction);

    for(Plugin p : m_plugins) {
        auto *profileEditor = qobject_cast<ProfileInterface *>(p.instance.get());
        if(profileEditor) {
            auto *profileAction = new QAction(tr("Profile"), window);
            connect(profileAction, &QAction::triggered, window, [profileEditor]() {
                //window->currentSubWindow()
                profileEditor->createWidget(WebProfile::defaultProfile(), nullptr)->show();
            });
            window->addAction(MainWindow::ToolsMenu, profileAction);
        }
    }

    m_windows.append(window);
    connect(window, &MainWindow::destroyed, this, [this, window]() {
        m_windows.removeOne(window);
    });

    return window;
}