/*******************************************************************************
**
** 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
#include
#include "webengine/urlinterceptor.h"
#include "mainwindow.h"
#include
#include
Browser::Browser(int &argc, char *argv[]) :
SingleApplication(argc, argv)
{
setApplicationName("smolbote");
setWindowIcon(QIcon(":/icon.svg"));
}
Browser::~Browser()
{
// TODO: fix crash here
// qDeleteAll(m_windows);
// m_windows.clear();
// qDeleteAll(m_profiles);
// m_profiles.clear();
}
void Browser::setConfiguration(std::shared_ptr &config)
{
m_config = config;
m_bookmarksManager = std::make_shared(QString::fromStdString(m_config->value("bookmarks.path").value()));
m_downloadManager = std::make_shared(QString::fromStdString(m_config->value("downloads.path").value()));
// TODO: change path
m_urlRequestInterceptor = std::make_shared(QString::fromStdString(m_config->value("browser.filterPath").value_or("")));
}
void Browser::loadProfiles()
{
Q_ASSERT(m_config);
const QString &path = QString::fromStdString(m_config->value("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) {
std::shared_ptr profile = std::make_shared(name, profileDir.absoluteFilePath(name), this);
profile->setRequestInterceptor(m_urlRequestInterceptor.get());
m_profiles.insert(name, profile);
}
// Also add the Off-the-record profile
std::shared_ptr otr = std::make_shared(this);
otr->setRequestInterceptor(m_urlRequestInterceptor.get());
m_profiles.insert("", otr);
// set default profile
m_defaultProfile = m_profiles[QString::fromStdString(m_config->value("browser.profile").value())];
#ifdef QT_DEBUG
qDebug("<< Profiles end...");
#endif
}
/*
std::shared_ptr &Browser::bookmarks()
{
Q_ASSERT(m_bookmarksManager);
return m_bookmarksManager;
}
std::shared_ptr &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;
}
std::shared_ptr 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;
}
*/