From 4e3c479a0f279926e0bd9a359a0ee57b334e976e Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 8 Dec 2017 14:22:19 +0100 Subject: Replaced tinytoml with libconfig --- src/browser.cpp | 222 ++++++++++++-------------------------------------------- 1 file changed, 45 insertions(+), 177 deletions(-) (limited to 'src/browser.cpp') diff --git a/src/browser.cpp b/src/browser.cpp index 14fee3e..60b8810 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -21,16 +21,14 @@ #include "browser.h" #include "mainwindow.h" #include -#include #include -#include -#include "interfaces.h" Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) { setApplicationName("smolbote"); + setWindowIcon(QIcon(":/icon.svg")); #ifdef GIT_VERSION setApplicationVersion(GIT_VERSION); #else @@ -40,95 +38,58 @@ Browser::Browser(int &argc, char *argv[]) : Browser::~Browser() { - qDeleteAll(m_windows); - m_windows.clear(); +// qDeleteAll(m_windows); +// m_windows.clear(); } -QString Browser::applicationLongVersion() const +void Browser::setConfiguration(std::shared_ptr &config) { -#ifdef GIT_DESCRIBE - return QString(GIT_DESCRIBE); -#else - return applicationVersion(); -#endif + 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())); } -void Browser::loadPlugins() +void Browser::loadProfiles() { - // Loading plugins - qDebug(">> Looking for plugins..."); + Q_ASSERT(m_config); - // Look for plugins in "../lib/smolbote/plugins" - QDir dir = QDir::current(); - dir.cd("../lib/smolbote/plugins"); + const QString &path = QString::fromStdString(m_config->value("profile.path").value()); - // Load all plugins - const QStringList files = dir.entryList(QDir::Files | QDir::Readable); - for(const QString &filename : files) { - qDebug("Loading %s", qUtf8Printable(filename)); +#ifdef QT_DEBUG + qDebug(">> Looking for profiles... [%s]", qUtf8Printable(path)); +#endif - 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"); - } - } + // Build a profile list from the folders in the profile.path + QDir profileDir(path); + const QStringList profileList = profileDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); - 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); + m_profiles.insert(name, new WebEngineProfile(name, profileDir.absoluteFilePath(name), this)); } - qDebug("<< Profiles end..."); - //connect(this, SIGNAL(messageAvailable(QStringList)), this, SLOT(addWindow(QStringList))); - connect(this, &Browser::messageAvailable, this, [&](const QHash params) { + // Also add the Off-the-record profile + m_profiles.insert("", new WebEngineProfile(this)); - //qDebug("Creating new window for [%s]", qUtf8Printable(params["urls"].toString())); + // set default profile + m_defaultProfile = m_profiles[QString::fromStdString(m_config->value("browser.profile").value())]; - createWindow(params); - }); -} - -Browser *Browser::instance() -{ - return static_cast(QCoreApplication::instance()); -} - -Settings *Browser::settings() -{ - Q_ASSERT(m_settings); - return m_settings.get(); +#ifdef QT_DEBUG + qDebug("<< Profiles end..."); +#endif } -BookmarksWidget *Browser::bookmarks() +/* +std::shared_ptr &Browser::bookmarks() { - if(!m_bookmarksManager) { - m_bookmarksManager = QSharedPointer(new BookmarksWidget(settings()->value("bookmarks.path").toString()), &QObject::deleteLater); - } - return m_bookmarksManager.data(); + Q_ASSERT(m_bookmarksManager); + return m_bookmarksManager; } -DownloadsWidget *Browser::downloads() +std::shared_ptr &Browser::downloads() { - if(!m_downloadManager) { - m_downloadManager = QSharedPointer(new DownloadsWidget(settings()->value("downloads.path").toString()), &QObject::deleteLater); - } - return m_downloadManager.data(); + Q_ASSERT(m_downloadManager); + return m_downloadManager; } BlockerManager *Browser::blocklists() @@ -139,52 +100,6 @@ BlockerManager *Browser::blocklists() 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(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 %1 is empty.
" - "Using default values from %2.").arg(configLocation, defaultsLocation)); - } -#endif -} - MainWindow *Browser::activeWindow() { if(m_windows.empty()) { @@ -197,73 +112,30 @@ MainWindow *Browser::activeWindow() } } return m_windows.first().data(); -} +}*/ -MainWindow *Browser::createWindow(const QHash options) +MainWindow *Browser::createWindow() { - Q_ASSERT(options.contains("urls")); + // the window will delete itself when it closes, so we don't need to delete it + MainWindow *window = new MainWindow(m_config); + window->setProfile(m_defaultProfile); - MainWindow *w = new MainWindow(); + m_windows.append(window); - m_windows.append(QPointer(w)); - connect(w, &MainWindow::destroyed, this, [&]() { - clean(); + // has to be window.get(), but can't be *window + connect(window, &MainWindow::destroyed, this, [this, window]() { + m_windows.removeOne(window); }); + window->show(); - 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 - } - } + return window; } 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; @@ -273,8 +145,4 @@ QStringList Browser::profiles() } return l; } - -QObject *Browser::plugin(const QString name) -{ - return m_plugin; -} +*/ -- cgit v1.2.1