aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFelix (xq) Queißner <git@mq32.de>2021-03-15 11:54:48 +0100
committerFelix Queißner <felix@ib-queissner.de>2021-03-17 09:49:58 +0100
commit0d1fd257093c58bfb606aaac530aed3b0877f7fd (patch)
treeca998bde0422823b64f325c4830c7d5a37beaf9a /src
parent856924aa05815c565bfd67ad8dff12127f545b0b (diff)
downloadkristall-0d1fd257093c58bfb606aaac530aed3b0877f7fd.tar.gz
First draft of session management. Always restores the latest session.
Diffstat (limited to 'src')
-rw-r--r--src/kristall.hpp6
-rw-r--r--src/main.cpp133
-rw-r--r--src/mainwindow.cpp15
-rw-r--r--src/mainwindow.hpp1
4 files changed, 130 insertions, 25 deletions
diff --git a/src/kristall.hpp b/src/kristall.hpp
index 4217d09..73ed9e4 100644
--- a/src/kristall.hpp
+++ b/src/kristall.hpp
@@ -202,6 +202,12 @@ namespace kristall
//! Opens a new window with the given list of urls.
//! If the list is empty, no new tab will spawned.
MainWindow * openNewWindow(QVector<QUrl> const & urls);
+
+ //! Returns the number of currently open windows
+ int getWindowCount();
+
+ //! Saves the current session including all windows, tabs and positions.
+ void saveSession();
}
#endif // KRISTALL_HPP
diff --git a/src/main.cpp b/src/main.cpp
index 9e98cc7..cd8e5ef 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -47,8 +47,8 @@ QString toFingerprintString(QSslCertificate const & certificate)
char const * const app_window_property = "kristall:app-window";
static QSettings * app_settings_ptr= nullptr;
+static QSettings * session_settings_ptr= nullptr;
static MainWindow * last_focused_window = nullptr;
-static bool closing_state_saved = false;
#define SSTR(X) STR(X)
#define STR(X) #X
@@ -505,6 +505,18 @@ int main(int argc, char *argv[])
};
app_settings_ptr = &app_settings;
+ std::unique_ptr<QSettings> session_store;
+
+ // isolated sessions don't have session management
+ if(not isolated_session)
+ {
+ session_store = std::make_unique<QSettings>(
+ kristall::globals().dirs.config_root.absoluteFilePath("session.ini"),
+ QSettings::IniFormat
+ );
+ }
+ session_settings_ptr = session_store.get();
+
{
QSettings deprecated_settings { "xqTechnologies", "Kristall" };
if(QFile(deprecated_settings.fileName()).exists())
@@ -668,28 +680,71 @@ int main(int argc, char *argv[])
});
}
- // Open all URLs in the new window
+ // Stores the first window from the restored session (if any)
+ MainWindow * root_window = nullptr;
+ if(session_store != nullptr)
+ {
+ auto & settings = *session_store;
+
+ int window_count = settings.beginReadArray("windows");
+
+
+ for(int index = 0; index < window_count; index += 1)
+ {
+ settings.setArrayIndex(index);
+
+ QVector<QUrl> urls;
+
+ int tab_count = settings.beginReadArray("tabs");
+ for(int i = 0; i < tab_count; i++)
+ {
+ settings.setArrayIndex(i);
+ urls.push_back(settings.value("url").toString());
+ }
+ settings.endArray();
+
+ auto * const window = kristall::openNewWindow(urls);
+
+ if(settings.contains("state")) {
+ window->restoreState(settings.value("state").toByteArray());
+ }
+ if(settings.contains("geometry") != QVariant {}) {
+ window->restoreGeometry(settings.value("geometry").toByteArray());
+ }
+
+ if(root_window == nullptr)
+ root_window = window;
+ }
+
+ settings.endArray();
+ }
+
if(urls.size() > 0) {
- kristall::openNewWindow(urls);
+
+ if(root_window == nullptr or open_new_window)
+ {
+ // Open all URLs in a new window
+ // if we either got no previous window
+ // or the user explicitly requested a new one.
+ kristall::openNewWindow(urls);
+ }
+ else
+ {
+ // Otherwise, open all URLs in the first window
+ for(auto const & url : urls) {
+ root_window->addNewTab(true, url);
+ }
+ }
}
- else {
+ else if(root_window == nullptr) {
+ // If we start kristall in a blank state,
+ // just open a window with the default url
kristall::openNewWindow(true);
}
- //app_settings.beginGroup("Window State");
- //if(app_settings.contains("geometry")) {
- // w.restoreGeometry(app_settings.value("geometry").toByteArray());
- //}
- //if(app_settings.contains("state")) {
- // w.restoreState(app_settings.value("state").toByteArray());
- //}
- //app_settings.endGroup();
int exit_code = app.exec();
- if (!closing_state_saved)
- kristall::saveWindowState();
-
return exit_code;
}
@@ -972,14 +1027,48 @@ void kristall::setUiDensity(UIDensity density, bool previewing)
});
}
-void kristall::saveWindowState()
+int kristall::getWindowCount()
+{
+ int count = 0;
+ forAllAppWindows([&count](MainWindow *) {
+ count += 1;
+ });
+ return count;
+}
+
+void kristall::saveSession()
{
- closing_state_saved = true;
+ if(session_settings_ptr == nullptr)
+ return;
+ auto & settings = *session_settings_ptr;
+
+ settings.clear();
+ settings.beginWriteArray("windows");
+
+ int window_index = 0;
+ int tab_count = 0;
+ forAllAppWindows([&settings, &window_index, &tab_count](MainWindow * main_window) {
+ settings.setArrayIndex(window_index);
+
+ settings.setValue("state", main_window->saveState());
+ settings.setValue("geometry", main_window->saveGeometry());
+
+ int count = main_window->tabCount();
+ settings.beginWriteArray("tabs", count);
+ for(int i = 0; i < count; i++)
+ {
+ settings.setArrayIndex(i);
+ settings.setValue("url", main_window->tabAt(i)->current_location.toString(QUrl::FullyEncoded));
+ tab_count += 1;
+ }
+ settings.endArray();
+
+ window_index += 1;
+ });
+
+ settings.endArray();
- app_settings_ptr->beginGroup("Window State");
- //app_settings_ptr->setValue("geometry", main_window->saveGeometry());
- //app_settings_ptr->setValue("state", main_window->saveState());
- app_settings_ptr->endGroup();
+ qDebug() << "Saved session with" << window_index << "windows and" << tab_count << "tabs in total.";
- kristall::saveSettings();
+ settings.sync();
}
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index d9ee161..95e6278 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -29,6 +29,8 @@ MainWindow::MainWindow(QApplication * app, QWidget *parent) :
file_mime(new QLabel(this)),
load_time(new QLabel(this))
{
+ this->setAttribute(Qt::WA_DeleteOnClose);
+
ui->setupUi(this);
this->url_status->setElideMode(Qt::ElideMiddle);
@@ -177,6 +179,11 @@ BrowserTab * MainWindow::tabAt(int index) const
return qobject_cast<BrowserTab*>(this->ui->browser_tabs->widget(index));
}
+int MainWindow::tabCount() const
+{
+ return this->ui->browser_tabs->count();
+}
+
void MainWindow::setUrlPreview(const QUrl &url)
{
if(url.isValid()) {
@@ -323,8 +330,9 @@ void MainWindow::mousePressEvent(QMouseEvent *event)
void MainWindow::closeEvent(QCloseEvent *event)
{
- kristall::saveWindowState();
-
+ if(kristall::getWindowCount() == 1) {
+ kristall::saveSession();
+ }
event->accept();
}
@@ -458,6 +466,7 @@ void MainWindow::on_actionNew_Tab_triggered()
void MainWindow::on_actionQuit_triggered()
{
+ kristall::saveSession();
QApplication::quit();
}
@@ -774,7 +783,7 @@ void MainWindow::on_actionNew_window_triggered()
void MainWindow::on_actionClose_Window_triggered()
{
- this->deleteLater();
+ this->close();
}
void MainWindow::on_favourites_view_activated(const QModelIndex &index)
diff --git a/src/mainwindow.hpp b/src/mainwindow.hpp
index e0e738f..7cce992 100644
--- a/src/mainwindow.hpp
+++ b/src/mainwindow.hpp
@@ -34,6 +34,7 @@ public:
BrowserTab * addNewTab(bool focus_new, QUrl const & url);
BrowserTab * curTab() const;
BrowserTab * tabAt(int index) const;
+ int tabCount() const;
void setUrlPreview(QUrl const & url);