diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-06-28 10:21:47 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-06-28 10:21:47 +0200 |
commit | 0d93cf6fea0a5bae6bdf4adc15d5d6fd3306d0e6 (patch) | |
tree | 8055cb02033892be64b16221bd7710f8012bdeed /src/mainwindow | |
parent | Socket messages are json formatted (diff) | |
download | smolbote-0d93cf6fea0a5bae6bdf4adc15d5d6fd3306d0e6.tar.xz |
Add --session parameter to load a session json
Add Session menu to main window
Diffstat (limited to 'src/mainwindow')
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 25 | ||||
-rw-r--r-- | src/mainwindow/subwindow.cpp | 28 | ||||
-rw-r--r-- | src/mainwindow/subwindow.h | 5 |
3 files changed, 31 insertions, 27 deletions
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 <QUrl> #include <configuration/configuration.h> #include <webprofile.h> - -#ifdef QT_DEBUG #include "session.h" -#endif +#include <QFileDialog> #ifdef PLASMA_BLUR #include <KWindowEffects> @@ -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<Browser *>(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<WebView *>(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<WebView *>(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: |