From 0d93cf6fea0a5bae6bdf4adc15d5d6fd3306d0e6 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 28 Jun 2018 10:21:47 +0200 Subject: Add --session parameter to load a session json Add Session menu to main window --- src/mainwindow/mainwindow.cpp | 25 ++++++++++++++++++++++--- src/mainwindow/subwindow.cpp | 28 +++++++--------------------- src/mainwindow/subwindow.h | 5 ++--- 3 files changed, 31 insertions(+), 27 deletions(-) (limited to 'src/mainwindow') diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index 9a9b8e8..7adc9db 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -29,10 +29,8 @@ #include #include #include - -#ifdef QT_DEBUG #include "session.h" -#endif +#include #ifdef PLASMA_BLUR #include @@ -175,6 +173,27 @@ void MainWindow::createMenuBar() auto *quitAction = smolboteMenu->addAction(tr("Quit"), qApp, &QApplication::quit); m_config->setShortcut(quitAction, "mainwindow.shortcuts.quit"); + // session menu + auto *sessionMenu = menuBar()->addMenu(tr("Session")); + + sessionMenu->addAction(tr("Save Session"), this, [this]() { + const QString filename = QFileDialog::getSaveFileName(this, tr("Save Session"), QDir::homePath(), tr("JSON (*.json)")); + QFile output(filename); + if(output.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { + output.write(QJsonDocument(Session::toJsonObject(this)).toJson()); + output.close(); + } + }); + sessionMenu->addAction(tr("Load Session"), this, [this]() { + const QString filename = QFileDialog::getOpenFileName(this, tr("Load Session"), QDir::homePath(), tr("JSON (*.json)")); + QFile json(filename); + if(json.open(QIODevice::ReadOnly | QIODevice::Text)) { + auto *browser = qobject_cast(qApp); + browser->sendMessage(json.readAll()); + json.close(); + } + }); + // window menu auto *windowMenu = menuBar()->addMenu(tr("Window")); diff --git a/src/mainwindow/subwindow.cpp b/src/mainwindow/subwindow.cpp index bcddabf..5a1eeda 100644 --- a/src/mainwindow/subwindow.cpp +++ b/src/mainwindow/subwindow.cpp @@ -127,6 +127,11 @@ WebView *SubWindow::view(int index) const return qobject_cast(tabWidget->widget(index)); } +int SubWindow::tabCount() const +{ + return tabWidget->count(); +} + void SubWindow::setProfile(WebProfile *profile) { Q_CHECK_PTR(profile); @@ -142,9 +147,9 @@ WebProfile *SubWindow::profile() const return m_profile; } -int SubWindow::addTab(const QUrl &url) +int SubWindow::addTab(const QUrl &url, WebProfile *profile) { - auto *view = new WebView(m_profile, this); + auto *view = new WebView((profile == nullptr) ? m_profile : profile, this); if(!url.isEmpty()) view->load(url); return tabWidget->addTab(view); @@ -154,22 +159,3 @@ void SubWindow::setCurrentTab(int index) { tabWidget->setCurrentIndex(index); } - -QJsonObject SubWindow::session() const -{ - QJsonObject obj; - obj.insert("profile", ProfileManager::id(m_profile)); - - QJsonArray tabs; - for(int i = 0; i < tabWidget->count(); ++i) { - auto *view = qobject_cast(tabWidget->widget(i)); - if(view) { - QJsonObject tab; - tab.insert(view->url().toString(), ProfileManager::id(view->profile())); - tabs.append(tab); - } - } - obj.insert("tabs", tabs); - - return obj; -} diff --git a/src/mainwindow/subwindow.h b/src/mainwindow/subwindow.h index e6d7177..1283481 100644 --- a/src/mainwindow/subwindow.h +++ b/src/mainwindow/subwindow.h @@ -26,18 +26,17 @@ public: WebView *currentView(); WebView *view(int index) const; + int tabCount() const; void setProfile(WebProfile *profile); WebProfile *profile() const; - QJsonObject session() const; - signals: void currentViewChanged(WebView *view); void showStatusMessage(const QString &message, int timeout = 0); public slots: - int addTab(const QUrl &url = QUrl()); + int addTab(const QUrl &url = QUrl(), WebProfile *profile = nullptr); void setCurrentTab(int index); private: -- cgit v1.2.1