/*******************************************************************************
**
** 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 .
**
******************************************************************************/
#include "browser.h"
#include "mainwindow.h"
#include
#include
#include
#include
#include "interfaces.h"
Browser::Browser(int &argc, char *argv[]) :
SingleApplication(argc, argv)
{
m_settings = nullptr;
m_networkAccessManager = nullptr;
m_urlRequestInterceptor = nullptr;
m_bookmarksManager = nullptr;
m_downloadManager = nullptr;
m_blocklistManager = nullptr;
m_plugin = nullptr;
setApplicationName("smolbote");
#ifdef GIT_VERSION
setApplicationVersion(GIT_VERSION);
#else
setApplicationVersion("1.0.0");
#endif
}
Browser::~Browser()
{
qDeleteAll(m_windows);
m_windows.clear();
if(m_networkAccessManager) {
delete m_networkAccessManager;
}
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
for(QString filename : dir.entryList(QDir::Files | QDir::Readable)) {
qDebug("Loading %s", qUtf8Printable(filename));
QPluginLoader loader(dir.absoluteFilePath(filename));
QObject *plugin = loader.instance();
if(plugin) {
PluginInterface *p = qobject_cast(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());
for(const QString name : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
qDebug("- Adding profile %s", qUtf8Printable(name));
profile(name);
}
qDebug("<< Profiles end...");
connect(this, SIGNAL(messageAvailable(QStringList)), this, SLOT(addWindow(QStringList)));
}
Browser *Browser::instance()
{
return static_cast(QCoreApplication::instance());
}
Settings *Browser::settings()
{
return m_settings;
}
QNetworkAccessManager *Browser::network()
{
if(!m_networkAccessManager) {
m_networkAccessManager = new QNetworkAccessManager();
}
return m_networkAccessManager;
}
BookmarksWidget *Browser::bookmarks()
{
if(!m_bookmarksManager) {
m_bookmarksManager = new BookmarksWidget();
}
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::setConfigPath(const QString &path)
{
// set custom config path if any
if(!path.isEmpty()) {
m_settings = new Settings(path);
return;
}
// check if config file exists for this user
QString cpath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/poi.conf";
if(QFile::exists(cpath)) {
m_settings = new Settings(cpath);
}
// check system-specific locations
#ifdef Q_OS_LINUX
else if(QFile::exists("/usr/share/smolbote/poi.conf")) {
m_settings = new Settings("/usr/share/smolbote/poi.conf");
} else if(QFile::exists("/usr/local/share/smolbote/poi.conf")) {
m_settings = new Settings("/usr/local/share/smolbote/poi.conf");
}
#endif
else {
m_settings = new Settings();
}
if(m_settings->isEmpty()) {
// There are no keys in the settings
QMessageBox::information(0,
tr("Configuration is empty"),
tr("The configuration file %1 is empty. Using default values").arg(m_settings->filePath()));
}
}
MainWindow *Browser::mainWindow()
{
if(m_windows.isEmpty()) {
addWindow(new MainWindow());
}
return m_windows.first();
}
void Browser::addWindow(MainWindow *window)
{
if(m_windows.contains(window)) {
return;
}
m_windows.append(window);
connect(window, &QObject::destroyed, [this, window]() {
this->removeWindow(window);
});
window->show();
}
void Browser::addWindow(const QStringList params)
{
QString p; // get default profile
QStringList urls;
for(int i = 0; i < params.length(); i++) {
if(params.at(i) == "-p" || params.at(i) == "--profile") {
i++;
p = params.at(i);
} else if(!params.at(i).startsWith('-') && i > 0) {
qDebug("Appending url: %s", qUtf8Printable(params.at(i)));
urls.append(params.at(i));
}
}
MainWindow *w = new MainWindow(urls);
w->setProfile(profile(p));
addWindow(w);
}
void Browser::removeWindow(MainWindow *window)
{
m_windows.removeOne(window);
}
WebEngineProfile* Browser::profile(const QString name)
{
if(!m_profiles.contains(name)) {
// name can be empty --> off-the-record profile
if(name.isEmpty()) {
m_profiles.insert(name, new WebEngineProfile(this));
} else {
m_profiles.insert(name, new WebEngineProfile(name, settings()->value("browser.profile.path").toString(), 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;
for(QString key : m_profiles.keys()) {
l.append(key);
}
return l;
}
QObject *Browser::plugin(const QString name)
{
return m_plugin;
}