diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-11-28 16:31:48 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-11-28 16:31:48 +0100 |
commit | f0a7c1760a0186ebfb7d80da34d6675b6d987303 (patch) | |
tree | 5a20e7618f093c13cbc7ba844d37fe4066a5ba48 /src | |
parent | Rewrite Session saving and loading (diff) | |
download | smolbote-f0a7c1760a0186ebfb7d80da34d6675b6d987303.tar.xz |
Remove old session code
Diffstat (limited to 'src')
-rw-r--r-- | src/browser.cpp | 42 | ||||
-rw-r--r-- | src/browser.h | 5 | ||||
-rw-r--r-- | src/main.cpp | 10 | ||||
-rw-r--r-- | src/session/session.cpp | 78 | ||||
-rw-r--r-- | src/session/session.h | 4 | ||||
-rw-r--r-- | src/session/sessiondialog.cpp | 24 | ||||
-rw-r--r-- | src/session/sessiondialog.h | 3 | ||||
-rw-r--r-- | src/session/sessiondialog.ui | 5 | ||||
-rw-r--r-- | src/subwindow/subwindow.cpp | 6 |
9 files changed, 69 insertions, 108 deletions
diff --git a/src/browser.cpp b/src/browser.cpp index 0a0bd91..3a80439 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -195,48 +195,6 @@ void Browser::setup(QVector<QPluginLoader *> plugins) timer->start(5 * 60 * 1000); } -void Browser::createSession(const QJsonObject &object) -{ - MainWindow *mainwindow = nullptr; - if(m_windows.isEmpty()) - mainwindow = createWindow(); - else - mainwindow = m_windows.last(); - - const QJsonArray subwindows = object.value("subwindows").toArray(); - - for(const QJsonValue &s : subwindows) { - const QJsonObject subwindow = s.toObject(); - const QString profileId = subwindow.value("profile").toString(); - WebProfile *profile = m_profileManager->profile(profileId); - if(profile == nullptr) - profile = WebProfile::defaultProfile(); - Q_CHECK_PTR(profile); - - SubWindow *window = nullptr; - for(SubWindow *w : mainwindow->subWindows()) { - if(w->profile() == profile) { - window = w; - break; - } - } - if(window == nullptr) - window = mainwindow->createSubWindow(m_config.get(), profile); - - const QJsonArray tabs = subwindow.value("tabs").toArray(); - if(tabs.isEmpty()) - window->addTab(profile->newtab()); - else { - for(const auto &t : tabs) { - const QJsonObject tab = t.toObject(); - const QUrl url = QUrl::fromUserInput(tab.value("url").toString()); - WebProfile *p = m_profileManager->profile(tab.value("profile").toString()); - window->addTab(url, p); - } - } - } -} - MainWindow *Browser::createWindow() { // the window will delete itself when it closes, so we don't need to delete it diff --git a/src/browser.h b/src/browser.h index 5a6404b..f5c63f8 100644 --- a/src/browser.h +++ b/src/browser.h @@ -57,13 +57,8 @@ public: { return qAsConst(m_windows); } - QJsonObject session() const - { - return Session::session(m_windows); - } public slots: - void createSession(const QJsonObject &object); MainWindow *createWindow(); private: diff --git a/src/main.cpp b/src/main.cpp index a80784b..34f8a1a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -223,13 +223,15 @@ int main(int argc, char **argv) } } } + if(urls.isEmpty()) + urls.append(QString()); // if app is primary, create new sessions from received messages if(app.isPrimary() && !isStandalone) { - QObject::connect(&app, &Browser::receivedMessage, &app, [&app](quint32 instanceId, QByteArray message) { + QObject::connect(&app, &Browser::receivedMessage, &app, [](quint32 instanceId, QByteArray message) { Q_UNUSED(instanceId); auto doc = QJsonDocument::fromJson(message); - app.createSession(doc.object()); + Session::restoreSession(doc.object()); }); } @@ -242,11 +244,11 @@ int main(int argc, char **argv) sessionJson.close(); } } else { - sessionData = Session::window(profile.value(), urls); + sessionData = Session::fromCommandLine(profile.value(), urls); } if(app.isPrimary() || isStandalone) { - app.createSession(sessionData); + Session::restoreSession(sessionData); } else { // app is secondary and not standalone return app.sendMessage(QJsonDocument(sessionData).toJson()); diff --git a/src/session/session.cpp b/src/session/session.cpp index 0853c70..10aca4e 100644 --- a/src/session/session.cpp +++ b/src/session/session.cpp @@ -19,46 +19,7 @@ #include <memory> #include "configuration.h" -QJsonObject Session::session(QVector<MainWindow *> windows) -{ - QJsonObject obj; - - QJsonArray windowsArray; - for(const MainWindow *window : windows) { - windowsArray.append(Session::window(window)); - } - obj.insert("windows", windowsArray); - - return obj; -} - -QJsonObject Session::window(const MainWindow *window) -{ - QJsonObject obj; - auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager(); - - QJsonArray subwindows; - for(const SubWindow *subwindow : window->subWindows()) { - QJsonObject window; - window.insert("profile", profileManager->id(subwindow->profile())); - - QJsonArray tabs; - for(int i = 0; i < subwindow->tabCount(); ++i) { - QJsonObject tab; - tab.insert("url", subwindow->view(i)->url().toString()); - tab.insert("profile", profileManager->id(subwindow->view(i)->profile())); - tabs.append(tab); - } - window.insert("tabs", tabs); - - subwindows.append(window); - } - obj.insert("subwindows", subwindows); - - return obj; -} - -QJsonObject Session::window(const QString &profile, const QStringList &urls) +QJsonObject Session::fromCommandLine(const QString &profile, const QStringList &urls) { QJsonObject session; @@ -162,10 +123,15 @@ void Session::restoreView(WebView *view, const QJsonObject &data) if(profile) view->setProfile(profile); - // restore history: toLatin1 (turn into bytearray), fromBase64, uncompress - QByteArray historyData = qUncompress(QByteArray::fromBase64(data["history"].toString().toLatin1())); - QDataStream historyStream(&historyData, QIODevice::ReadOnly); - historyStream >> *view->history(); + auto url = data.value("url"); + if(url.isString()) + view->load(QUrl::fromUserInput(url.toString())); + else { + // restore history: toLatin1 (turn into bytearray), fromBase64, uncompress + QByteArray historyData = qUncompress(QByteArray::fromBase64(data["history"].toString().toLatin1())); + QDataStream historyStream(&historyData, QIODevice::ReadOnly); + historyStream >> *view->history(); + } } void Session::restoreSession(const QJsonObject &sessionData) @@ -175,12 +141,32 @@ void Session::restoreSession(const QJsonObject &sessionData) auto *profileManager = browser->getProfileManager(); Q_CHECK_PTR(profileManager); + for(const auto subwindowData : sessionData["subwindows"].toArray()) { + MainWindow *window = nullptr; + SubWindow *subwindow = nullptr; + + if(!browser->windows().isEmpty()) { + window = browser->windows().last(); + subwindow = window->currentSubWindow(); + } else { + window = browser->createWindow(); + subwindow = window->createSubWindow(browser->getConfiguration(), profileManager->profile(subwindowData.toObject()["profile"].toString())); + } + + Q_CHECK_PTR(window); + Q_CHECK_PTR(subwindow); + + for(const auto tabData : subwindowData.toObject()["tabs"].toArray()) { + auto *view = subwindow->view(subwindow->addTab()); + Session::restoreView(view, tabData.toObject()); + } + } + for(const auto windowData : sessionData["windows"].toArray()) { auto *window = browser->createWindow(); for(const auto subwindowData : windowData.toObject()["subwindows"].toArray()) { - auto *subwindow = window->createSubWindow(browser->getConfiguration(), WebProfile::defaultProfile()); - subwindow->setProfile(profileManager->profile(subwindowData.toObject()["profile"].toString())); + auto *subwindow = window->createSubWindow(browser->getConfiguration(), profileManager->profile(subwindowData.toObject()["profile"].toString())); for(const auto tabData : subwindowData.toObject()["tabs"].toArray()) { auto *view = subwindow->view(subwindow->addTab()); diff --git a/src/session/session.h b/src/session/session.h index 720b711..8a96bb2 100644 --- a/src/session/session.h +++ b/src/session/session.h @@ -16,9 +16,7 @@ class SubWindow; class WebView; namespace Session { -QJsonObject session(QVector<MainWindow *> windows); -QJsonObject window(const MainWindow *window); -QJsonObject window(const QString &profile, const QStringList &urls); +QJsonObject fromCommandLine(const QString &profile, const QStringList &urls); // TODO: QJsonObject _session(const QVector<MainWindow *> windows); diff --git a/src/session/sessiondialog.cpp b/src/session/sessiondialog.cpp index 5fd65c3..287950d 100644 --- a/src/session/sessiondialog.cpp +++ b/src/session/sessiondialog.cpp @@ -37,9 +37,7 @@ SessionDialog::SessionDialog(QWidget *parent) ui->listWidget->setItemWidget(item, widget); } -#ifdef QT_DEBUG - ui->listWidget->addItem(tr("Start new session")); -#endif + ui->listWidget->addItem(tr("Create a new window")); connect(ui->listWidget, &QListWidget::currentItemChanged, this, [this](QListWidgetItem *currentItem, QListWidgetItem *previousItem) { auto *currentWidget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(currentItem)); @@ -60,11 +58,15 @@ SessionDialog::SessionDialog(QWidget *parent) } }); - connect(this, &SessionDialog::accepted, this, [this]() { + connect(this, &SessionDialog::accepted, this, [this, browser]() { auto *currentWidget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(ui->listWidget->currentItem())); if(currentWidget) this->openSession(currentWidget->ui->label->text()); + else + browser->createWindow(); }); + + connect(ui->search, &QLineEdit::textEdited, this, &SessionDialog::search); } SessionDialog::~SessionDialog() @@ -72,6 +74,19 @@ SessionDialog::~SessionDialog() delete ui; } +void SessionDialog::search(const QString &text) +{ + for(int i = 0; i < ui->listWidget->count(); ++i) { + auto *item = ui->listWidget->item(i); + auto *widget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(item)); + + if(widget == nullptr) + item->setHidden(!text.isEmpty()); + else + item->setHidden(!widget->ui->label->text().contains(text)); + } +} + void SessionDialog::openSession(const QString &filename) { auto *browser = qobject_cast<Browser *>(qApp); @@ -79,7 +94,6 @@ void SessionDialog::openSession(const QString &filename) QFile json(filename); if(json.open(QIODevice::ReadOnly | QIODevice::Text)) { - auto *browser = qobject_cast<Browser *>(qApp); auto doc = QJsonDocument::fromJson(json.readAll()); Session::restoreSession(doc.object()); json.close(); diff --git a/src/session/sessiondialog.h b/src/session/sessiondialog.h index 7fea3c2..ff4d3af 100644 --- a/src/session/sessiondialog.h +++ b/src/session/sessiondialog.h @@ -24,7 +24,8 @@ public: explicit SessionDialog(QWidget *parent = nullptr); ~SessionDialog() override; -private: +private slots: + void search(const QString &text); void openSession(const QString &filename); private: diff --git a/src/session/sessiondialog.ui b/src/session/sessiondialog.ui index 3a61dc1..dab49d9 100644 --- a/src/session/sessiondialog.ui +++ b/src/session/sessiondialog.ui @@ -24,10 +24,13 @@ </widget> </item> <item> - <widget class="QLineEdit" name="lineEdit"> + <widget class="QLineEdit" name="search"> <property name="placeholderText"> <string>Search</string> </property> + <property name="clearButtonEnabled"> + <bool>true</bool> + </property> </widget> </item> </layout> diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp index 1006200..0844f1f 100644 --- a/src/subwindow/subwindow.cpp +++ b/src/subwindow/subwindow.cpp @@ -159,7 +159,9 @@ int SubWindow::tabCount() const void SubWindow::setProfile(WebProfile *profile) { - Q_CHECK_PTR(profile); + if(profile == nullptr) + return; + this->m_profile = profile; for(int i = 0; i < tabWidget->count(); ++i) { auto *view = qobject_cast<WebView *>(tabWidget->widget(i)); @@ -174,6 +176,8 @@ WebProfile *SubWindow::profile() const int SubWindow::addTab(const QUrl &url, WebProfile *profile) { + Q_CHECK_PTR(m_profile); + auto *view = new WebView((profile == nullptr) ? m_profile : profile, this); if(!url.isEmpty()) view->load(url); |