diff options
-rw-r--r-- | src/browser.cpp | 45 | ||||
-rw-r--r-- | src/browser.h | 8 | ||||
-rw-r--r-- | src/mainwindow/window.cpp | 35 | ||||
-rw-r--r-- | src/mainwindow/window.h | 2 |
4 files changed, 77 insertions, 13 deletions
diff --git a/src/browser.cpp b/src/browser.cpp index 512f207..05d9a0b 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -18,9 +18,11 @@ #include <downloads/downloadswidget.h> #include <version.h> #include <QDir> +#include <QFileInfo> +#include <QFileInfoList> #include <QPluginLoader> -QVector<Plugin> loadPlugins(const QString &location) +inline QVector<Plugin> loadPlugins(const QString &location) { QDir pluginsDir(location); QVector<Plugin> list; @@ -49,6 +51,21 @@ QVector<Plugin> loadPlugins(const QString &location) return list; } +inline QHash<QString, WebProfile *> loadProfiles(const QString &location) +{ + QDir profilesDir(location); + QHash<QString, WebProfile *> list; + + if(profilesDir.exists()) { + const QFileInfoList entries = profilesDir.entryInfoList({ "*.profile" }, QDir::Files | QDir::Readable); + for(const auto &entry : entries) { + list.insert(entry.baseName(), new WebProfile(entry.baseName())); + } + } + + return list; +} + Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) { @@ -67,6 +84,7 @@ Browser::~Browser() //qDeleteAll(m_plugins); m_plugins.clear(); + qDeleteAll(m_profiles); } void Browser::setConfiguration(std::shared_ptr<Configuration> &config) @@ -80,13 +98,16 @@ void Browser::setup(const QString &defaultProfile) Q_ASSERT_X(m_config, "Browser::setup", "Configuration not set"); // load profiles - if(defaultProfile == "") { - auto *p = new WebProfile(this); - p->loadProfile(m_config->section("profile")); - WebProfile::setDefaultProfile(p); - } else { - auto *p = new WebProfile(defaultProfile, this); - WebProfile::setDefaultProfile(p); + { + auto *otr = new WebProfile(this); + m_profiles.insert(tr("Off-the-record"), otr); + m_profiles.unite(loadProfiles(QString::fromStdString(m_config->value<std::string>("profile.path").value()))); + + if(defaultProfile == "") { + WebProfile::setDefaultProfile(otr); + } else { + WebProfile::setDefaultProfile(m_profiles.value(defaultProfile)); + } } // plugins @@ -125,6 +146,14 @@ void Browser::setup(const QString &defaultProfile) connect(WebProfile::defaultProfile(), &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); } +WebProfile *Browser::profile(const QString &name) const +{ + if(m_profiles.contains(name)) + return m_profiles.value(name); + else + return nullptr; +} + int Browser::command(const QString &command) { if(m_commands.contains(command)) { diff --git a/src/browser.h b/src/browser.h index 0a0d956..03126d3 100644 --- a/src/browser.h +++ b/src/browser.h @@ -23,6 +23,7 @@ class BookmarksWidget; class DownloadsWidget; class UrlRequestInterceptor; class MainWindow; +class WebProfile; class Browser : public SingleApplication { Q_OBJECT @@ -35,6 +36,12 @@ public: void setConfiguration(std::shared_ptr<Configuration> &config); void setup(const QString &defaultProfile); + WebProfile *profile(const QString &name) const; + QStringList profiles() const + { + return m_profiles.keys(); + } + int command(const QString &command); QStringList commands() const { @@ -51,6 +58,7 @@ private: std::shared_ptr<DownloadsWidget> m_downloads; std::shared_ptr<UrlRequestInterceptor> m_urlFilter; + QHash<QString, WebProfile*> m_profiles; QVector<MainWindow *> m_windows; QVector<Plugin> m_plugins; QHash<QString, std::function<int()>> m_commands; diff --git a/src/mainwindow/window.cpp b/src/mainwindow/window.cpp index 47f8489..e3d5516 100644 --- a/src/mainwindow/window.cpp +++ b/src/mainwindow/window.cpp @@ -18,6 +18,7 @@ #include <QJsonObject> #include <QJsonArray> #include <QJsonDocument> +#include "browser.h" Window::Window(const QHash<QString, QString> &config, QWidget *parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) @@ -31,15 +32,28 @@ Window::Window(const QHash<QString, QString> &config, QWidget *parent, Qt::Windo profile = WebProfile::defaultProfile(); -#ifdef QT_DEBUG { auto *menu = systemMenu(); - menu->addSeparator(); - menu->addAction(tr("Debug menu"))->setEnabled(false); - menu->addAction(tr("Profile: %1").arg(profile->isOffTheRecord() ? tr("off-the-record") : profile->storageName()))->setEnabled(false); + auto *profileName_action = menu->addAction(tr("Profile: %1").arg(profile->isOffTheRecord() ? tr("off-the-record") : profile->storageName())); + profileName_action->setEnabled(false); + auto *loadProfile_menu = menu->addMenu(tr("Load profile")); + + Browser *instance = qobject_cast<Browser*>(qApp); + Q_ASSERT_X(instance != nullptr, "Window::Window", "Could not cast instance"); + for(const QString &name : instance->profiles()) { + auto *loadAction = loadProfile_menu->addAction(name); + connect(loadAction, &QAction::triggered, this, [name, instance, profileName_action, this]() { + auto *profile = instance->profile(name); + this->setProfile(profile); + profileName_action->setText(tr("Profile: %1").arg(name)); + }); + } + +#ifdef QT_DEBUG menu->addSeparator(); + menu->addAction(tr("Debug menu"))->setEnabled(false); auto *saveSession_action = menu->addAction(tr("Save session")); menu->addAction(tr("Load session"))->setEnabled(false); setSystemMenu(menu); @@ -48,8 +62,8 @@ Window::Window(const QHash<QString, QString> &config, QWidget *parent, Qt::Windo QJsonDocument doc(session()); qDebug("%s", qUtf8Printable(doc.toJson())); }); - } #endif + } // new tab button auto *newTab_button = new QToolButton(this); @@ -117,6 +131,17 @@ WebView *Window::view(int index) const return qobject_cast<WebView *>(tabWidget->widget(index)); } +void Window::setProfile(WebProfile *profile) +{ + Q_ASSERT_X(profile != nullptr, "Window::setProfile", "Tried to set null profile"); + for(int i = 0; i < tabWidget->count(); ++i) { + auto *view = qobject_cast<WebView *>(tabWidget->widget(i)); + const auto url = view->url(); + view->setPage(new WebPage(profile, view)); + view->load(url); + } +} + int Window::addTab(const QUrl &url) { auto *view = new WebView(profile, this); diff --git a/src/mainwindow/window.h b/src/mainwindow/window.h index a569214..6431466 100644 --- a/src/mainwindow/window.h +++ b/src/mainwindow/window.h @@ -27,6 +27,8 @@ public: WebView *currentView(); WebView *view(int index) const; + void setProfile(WebProfile *profile); + QJsonObject session() const; void restoreSession(const QJsonObject &sessionData); |