aboutsummaryrefslogtreecommitdiff
path: root/src/browser.cpp
blob: 82972dd10c046ec58f2a0fb476d082e05f91df49 (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
/*******************************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** 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 <http://www.gnu.org/licenses/>.
 **
 ******************************************************************************/

#include "browser.h"
#include <bookmarks/bookmarkswidget.h>
#include <downloads/downloadswidget.h>
#include "webengine/urlinterceptor.h"
#include "mainwindow.h"
#include <QtWebEngine>
#include <QDir>

Browser::Browser(int &argc, char *argv[]) :
    SingleApplication(argc, argv)
{
    setApplicationName("smolbote");
    setWindowIcon(QIcon(":/icon.svg"));
#ifdef GIT_VERSION
    setApplicationVersion(GIT_VERSION);
#else
    setApplicationVersion("1.0.0");
#endif
}

Browser::~Browser()
{
    // TODO: fix crash here
//    qDeleteAll(m_windows);
//    m_windows.clear();

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

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

    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 = std::make_shared<UrlRequestInterceptor>(QString::fromStdString(m_config->value<std::string>("browser.filterPath").value()));

}

void Browser::loadProfiles()
{
    Q_ASSERT(m_config);

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

#ifdef QT_DEBUG
    qDebug(">> Looking for profiles... [%s]", qUtf8Printable(path));
#endif

    // Build a profile list from the folders in the profile.path
    QDir profileDir(path);
    const QStringList profileList = profileDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);

    for(const QString &name : profileList) {
        WebEngineProfile *profile = new WebEngineProfile(name, profileDir.absoluteFilePath(name), this);
        profile->setRequestInterceptor(m_urlRequestInterceptor.get());
        m_profiles.insert(name, profile);
    }

    // Also add the Off-the-record profile
    WebEngineProfile *otr = new WebEngineProfile(this);
    otr->setRequestInterceptor(m_urlRequestInterceptor.get());
    m_profiles.insert("", otr);

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

#ifdef QT_DEBUG
    qDebug("<< Profiles end...");
#endif
}

/*
std::shared_ptr<BookmarksWidget> &Browser::bookmarks()
{
    Q_ASSERT(m_bookmarksManager);
    return m_bookmarksManager;
}

std::shared_ptr<DownloadsWidget> &Browser::downloads()
{
    Q_ASSERT(m_downloadManager);
    return m_downloadManager;
}

BlockerManager *Browser::blocklists()
{
    if(!m_blocklistManager) {
        m_blocklistManager = new BlockerManager();
    }
    return m_blocklistManager;
}

MainWindow *Browser::activeWindow()
{
    if(m_windows.empty()) {
        return nullptr;
    }

    for(auto it = m_windows.cbegin(); it != m_windows.cend(); it++) {
        if(it->data()->isActiveWindow()) {
            return it->data();
        }
    }
    return m_windows.first().data();
}*/

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

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

WebEngineProfile* Browser::profile(const QString name)
{
    return m_profiles[name];
}
/*
QStringList Browser::profiles()
{
    QStringList l;
    const QStringList keys = m_profiles.keys();
    for(const QString &key : keys) {
        l.append(key);
    }
    return l;
}
*/