aboutsummaryrefslogtreecommitdiff
path: root/src/browser.cpp
blob: c0289a6055b2225dbdb6e49385749e0b3f632861 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*******************************************************************************
 **
 ** 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 "mainwindow.h"
#include <QtWebEngine>
#include <QMessageBox>

#include <QDir>
#include <QPluginLoader>
#include "interfaces.h"

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

Browser::~Browser()
{
    qDeleteAll(m_windows);
    m_windows.clear();

    if(m_bookmarksManager) {
        delete m_bookmarksManager;
    }

    if(m_downloadManager) {
        delete m_downloadManager;
    }
}

QString Browser::applicationLongVersion() const
{
#ifdef GIT_DESCRIBE
    return QString(GIT_DESCRIBE);
#else
    return applicationVersion();
#endif
}

void Browser::loadPlugins()
{
    // Loading plugins
    qDebug(">> Looking for plugins...");

    // Look for plugins in "../lib/smolbote/plugins"
    QDir dir = QDir::current();
    dir.cd("../lib/smolbote/plugins");

    // Load all plugins
    const QStringList files = dir.entryList(QDir::Files | QDir::Readable);
    for(const QString &filename : files) {
        qDebug("Loading %s", qUtf8Printable(filename));

        QPluginLoader loader(dir.absoluteFilePath(filename));
        QObject *plugin = loader.instance();
        if(plugin) {
            PluginInterface *p = qobject_cast<PluginInterface *>(plugin);
            if(p) {
                qDebug("Successfully loaded plugin [name = %s]", qUtf8Printable(p->name()));
                m_plugin = plugin;
            }
        } else {
            qDebug("Plugin load failed");
        }
    }

    qDebug("<< Plugins end...");
}

void Browser::loadProfiles()
{
    qDebug(">> Looking for profiles...");
    profile("");
    QDir dir(settings()->value("browser.profile.path").toString());
    const QStringList profileList = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
    for(const QString &name : profileList) {
        qDebug("- Adding profile %s", qUtf8Printable(name));
        profile(name);
    }
    qDebug("<< Profiles end...");

    //connect(this, SIGNAL(messageAvailable(QStringList)), this, SLOT(addWindow(QStringList)));
    connect(this, &Browser::messageAvailable, this, [&](const QHash<QString, QVariant> params) {

        //qDebug("Creating new window for [%s]", qUtf8Printable(params["urls"].toString()));

        createWindow(params);
    });
}

Browser *Browser::instance()
{
    return static_cast<Browser *>(QCoreApplication::instance());
}

Settings *Browser::settings()
{
    Q_ASSERT(m_settings);
    return m_settings.get();
}

BookmarksWidget *Browser::bookmarks()
{
    if(!m_bookmarksManager) {
        m_bookmarksManager = new BookmarksWidget(settings()->value("bookmarks.path").toString());
    }
    return m_bookmarksManager;
}

DownloadsWidget *Browser::downloads()
{
    if(!m_downloadManager) {
        m_downloadManager = new DownloadsWidget();
    }
    return m_downloadManager;
}

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

void Browser::loadSettings(const QString &path)
{
    QString configLocation, defaultsLocation;

    // set custom config path if any
    if(!path.isEmpty()) {
        configLocation = path;

    } else {
        // no custom config has been set
        // check if config file exists for this user
        QString cpath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/poi.conf";
        if(QFile::exists(cpath)) {
            configLocation = cpath;
        }
        // else there is no user overrides
    }

    // set defaults location
    // check system-specific locations
#ifdef Q_OS_LINUX
    if(QFile::exists("/usr/share/smolbote/poi.conf")) {
        defaultsLocation = "/usr/share/smolbote/poi.conf";
    } else if(QFile::exists("/usr/local/share/smolbote/poi.conf")) {
        defaultsLocation = "/usr/local/share/smolbote/poi.conf";
    }
#endif

    // if no default config is found, then use the built-in one
    if(defaultsLocation.isEmpty()) {
        defaultsLocation = ":/poi.toml";
    }

    m_settings = std::unique_ptr<Settings>(new Settings(configLocation, defaultsLocation));

#ifdef QT_DEBUG
    if(m_settings->isEmpty()) {
        // There are no keys in the settings
        QMessageBox::information(0,
                                 tr("Configuration is empty"),
                                 tr("The configuration file <i>%1</i> is empty.<br>"
                                    "Using default values from <i>%2</i>.").arg(configLocation, defaultsLocation));
    }
#endif
}

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(const QHash<QString, QVariant> options)
{
    Q_ASSERT(options.contains("urls"));

    MainWindow *w = new MainWindow();

    m_windows.append(QPointer<MainWindow>(w));
    connect(w, &MainWindow::destroyed, this, [&]() {
        clean();
    });

    QString profileName;
    if(options.contains("profile")) {
        profileName = options.value("profile").toString();
    }
    const QStringList urls = options.value("urls").toStringList();
    if(urls.isEmpty()) {
        w->newTab(profile(profileName)->homepage());
    } else {
        for(const QString url : urls) {
            w->newTab(QUrl::fromUserInput(url));
        }
    }

    w->show();

    return w;
}

void Browser::clean()
{
    for(int i = m_windows.count() - 1; i >= 0; i--) {
        if(m_windows[i].isNull()) {
            m_windows.removeAt(i);
#ifdef QT_DEBUG
            qDebug("Removed deleted window from window list");
#endif
        }
    }
}

WebEngineProfile* Browser::profile(const QString name)
{
    if(!m_profiles.contains(name)) {
        if(name.isEmpty()) {
            // Create off-the-record profile
            m_profiles.insert(name, new WebEngineProfile(this));
        } else {
            // Create regular profile
            m_profiles.insert(name, new WebEngineProfile(name, this));
        }

        if(!m_urlRequestInterceptor) {
            m_urlRequestInterceptor = new UrlRequestInterceptor(this);
            m_urlRequestInterceptor->setSubscription(blocklists());
        }

        m_profiles[name]->setRequestInterceptor(m_urlRequestInterceptor);

        connect(m_profiles[name], SIGNAL(downloadRequested(QWebEngineDownloadItem*)), downloads(), SLOT(addDownload(QWebEngineDownloadItem*)));
    }

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

QObject *Browser::plugin(const QString name)
{
    return m_plugin;
}