aboutsummaryrefslogtreecommitdiff
path: root/src/browser.cpp
blob: c32aee7f599d31844611a31136f3e03dc4ca5ba5 (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
/*
 * 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 "configuration/configuration.h"
#include "mainwindow/mainwindow.h"
#include "webengine/urlinterceptor.h"
#include "webengine/webengineprofile.h"
#include <QWebEngineDownloadItem>
#include <QtConcurrent>
#include <bookmarks/bookmarkswidget.h>
#include <downloads/downloadswidget.h>

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

    connect(this, &Browser::messageAvailable, this, &Browser::createSession);
}

Browser::~Browser()
{
    if(m_bookmarksManager) {
        QtConcurrent::run(QThreadPool::globalInstance(), m_bookmarksManager.get(), &BookmarksWidget::save);
    }

    qDebug("Waiting for threads to wind down...");
    qDebug("Thread pool is done: %s", QThreadPool::globalInstance()->waitForDone() ? "okay" : "failed");

    delete m_urlRequestInterceptor;
}

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

    QDir pluginsDir(QString::fromStdString(m_config->value<std::string>("plugins.path").value()));
    if(pluginsDir.exists()) {
        const QStringList entries = pluginsDir.entryList(QDir::Files | QDir::Readable);
        for(const QString &name : entries) {
            QPluginLoader loader(pluginsDir.absoluteFilePath(name));
            qDebug("Loading plugin %s: %s", qUtf8Printable(name), loader.load() ? "ok" : "failed");
            if(!loader.isLoaded()) {
                qDebug("Error: %s", qUtf8Printable(loader.errorString()));
            } else {
                Plugin d;
                d.meta = loader.metaData()["MetaData"].toObject();
                d.pointer = loader.instance();
                d.pointer->setParent(this);
                m_plugins.append(d);
            }
        }
    }

    m_bookmarksManager = std::make_shared<BookmarksWidget>(QString::fromStdString(m_config->value<std::string>("bookmarks.path").value()));
    m_downloadManager = std::make_shared<DownloadsWidget>(QString::fromStdString(m_config->value<std::string>("downloads.path").value()));

    m_urlRequestInterceptor = new UrlRequestInterceptor(QString::fromStdString(m_config->value<std::string>("filter.path").value()));

    // set default profile
    m_defaultProfile = profile(QString::fromStdString(m_config->value<std::string>("browser.profile").value()));
}

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);
    window->setBookmarksWidget(m_bookmarksManager);
    window->setDownloadsWidget(m_downloadManager);
    window->setProfile(m_defaultProfile);
    window->addPlugins(m_plugins);

    m_windows.append(window);

    // has to be window.get(), but can't be *window
    connect(window, &MainWindow::destroyed, this, [this, window]() {
        m_windows.removeOne(window);
    });
    window->show();

    return window;
}

MainWindow *Browser::createSession(const QString &profileName, bool newWindow, const QStringList &urls)
{
    MainWindow *window = nullptr;

    // if we need to open in a new window, or there are no windows, make one
    if(newWindow || m_windows.isEmpty()) {
        window = createWindow();
        window->setProfile(profile(profileName));
    } else {
        // reverse-iterate through windows to check for window with the same profile
        for(auto it = m_windows.rbegin(); it != m_windows.rend(); ++it) {
            if((*it)->profile()->storageName() == profileName) {
                window = *it;
                break;
            }
        }
        // if none is found, create one
        if(window == nullptr) {
            window = createWindow();
            window->setProfile(profile(profileName));
        }
    }

    Q_CHECK_PTR(window);

    if(urls.isEmpty()) {
        // no URLs were given
        window->newTab(QUrl::fromUserInput(m_config->value<std::string>("profile.homepage").value().c_str()));
    } else {
        for(const QString &url : urls) {
            window->newTab(QUrl::fromUserInput(url));
        }
    }

    return window;
}

std::shared_ptr<WebEngineProfile> Browser::profile(const QString &storageName)
{
    if(m_profiles.contains(storageName)) {
        return m_profiles[storageName];
    }

    // profile with name storageName has not been loaded
    Q_ASSERT(m_config);

    const QString &path = QString::fromStdString(m_config->value<std::string>("profile.path").value());
    std::shared_ptr<WebEngineProfile> _profile;

    if(storageName.isEmpty()) {
        // create off-the-record profile
        _profile = std::make_shared<WebEngineProfile>(nullptr);
        _profile->loadProfile(m_config->section("profile"), path + "/otr.ini");
    } else {
        // regular profile
        _profile = std::make_shared<WebEngineProfile>(storageName, nullptr);
        _profile->loadProfile(m_config->section("profile"), path + "/" + storageName + "/profile.ini");
        _profile->setPersistentStoragePath(path + "/storage");
        _profile->setCachePath(path + "/cache");
    }

    _profile->setRequestInterceptor(m_urlRequestInterceptor);

    connect(_profile.get(), &WebEngineProfile::downloadRequested, m_downloadManager.get(), &DownloadsWidget::addDownload);

    m_profiles.insert(storageName, _profile);
    return _profile;
}