aboutsummaryrefslogtreecommitdiff
path: root/src/browser.cpp
blob: e5e83e821cca7881804dd35da5cddbaa4fdad220 (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
/*
 * 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 "mainwindow/mainwindow.h"
#include "mainwindow/window.h"
#include "webengine/webprofile.h"
#include <QAction>
#include <configuration/configuration.h>
#include <bookmarks/bookmarkswidget.h>
#include <downloads/downloadswidget.h>
#include <version.h>
#include "webengine/urlinterceptor.h"
#include "addressbar/addressbar.h"

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

Browser::~Browser()
{
    qDeleteAll(m_windows);
    m_windows.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");

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

    // 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);
    });

    // downloads
    m_downloads = std::make_shared<DownloadsWidget>(QString::fromStdString(m_config->value<std::string>("downloads.path").value()));

    // load profiles
    if(defaultProfile == "") {
        auto *p = new WebProfile(this);
        p->loadProfile(m_config->section("profile"));
        p->setRequestInterceptor(m_urlFilter.get());
        connect(p, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload);
        WebProfile::setDefaultProfile(p);
    } else {
        auto *p = new WebProfile(defaultProfile, this);
        p->setRequestInterceptor(m_urlFilter.get());
        connect(p, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload);
        WebProfile::setDefaultProfile(p);
    }
}

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

    auto *mainwindow = m_windows.last();
    if(newWindow) {
        QUrl firstUrl = WebProfile::defaultProfile()->homepage();
        if(!urls.isEmpty())
            firstUrl = QUrl::fromUserInput(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 {
        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);
    window->createSubWindow(WebProfile::defaultProfile()->newtab());

    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]() {
        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]() {
        window->addDockWidget(Qt::RightDockWidgetArea, m_downloads.get());
    });
    window->addAction(MainWindow::ToolsMenu, downloadsAction);

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

    return window;
}