-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/start with last opened tabs #55
Changes from 9 commits
2f7dbd0
5169e92
4c786df
6ef5bde
a187110
b2f5836
c57252e
ae27765
aa3de28
a7d0768
370fa1b
2457b9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* This file is part of Liri Browser | ||
* | ||
* Copyright (C) 2017 Ivan Fateev <[email protected]> | ||
* | ||
* $BEGIN_LICENSE:GPL3+$ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* $END_LICENSE$ | ||
*/ | ||
|
||
#include "session.h" | ||
|
||
#include <QDir> | ||
#include <QDebug> | ||
#include <QJsonDocument> | ||
#include <QJsonObject> | ||
#include <QJsonArray> | ||
|
||
#include "../global/paths.h" | ||
#include "../models/tabsmodel.h" | ||
#include "tabstate.h" | ||
|
||
Session::Session(QObject *parent) | ||
: QObject(parent) | ||
, m_activeTab(0) | ||
{ | ||
load(); | ||
} | ||
|
||
void Session::save(TabsModel* tabs) | ||
{ | ||
if (!QDir(Paths::DataLocation).exists()) { | ||
qDebug() << "DataLocation path doesn't exist."; | ||
qDebug() << "Creating" << Paths::DataLocation << "..."; | ||
QDir().mkpath(Paths::DataLocation); | ||
} | ||
QFile file(Paths::SessionDataFile); | ||
if (!file.open(QIODevice::WriteOnly)) { | ||
qWarning("Couldn't open session file for write!"); | ||
return; | ||
} | ||
QTextStream stream(&file); | ||
stream << json(tabs); | ||
file.close(); | ||
|
||
qDebug() << "Session written to" << Paths::SessionDataFile; | ||
} | ||
|
||
QVariantList Session::getTabsToRestore() | ||
{ | ||
QVariantList tabs; | ||
for (TabState* state : m_tabs) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found another curly brace :) |
||
tabs.append(QVariant::fromValue(state)); | ||
} | ||
m_tabs.clear(); | ||
return tabs; | ||
} | ||
|
||
void Session::load() | ||
{ | ||
QFile file(Paths::SessionDataFile); | ||
if (!file.open(QIODevice::ReadOnly)) { | ||
qWarning("Couldn't open session file for read!"); | ||
return; | ||
} | ||
|
||
QByteArray bytes = file.readAll(); | ||
QJsonDocument doc(QJsonDocument::fromJson(bytes)); | ||
|
||
QJsonObject root = doc.object(); | ||
QJsonObject meta = root["meta"].toObject(); | ||
QString metaSchema = meta["schema"].toString(); | ||
if (metaSchema != "0.1") { | ||
qWarning() << "Unknown session schema version " << metaSchema << "!"; | ||
return; | ||
} | ||
QJsonArray tabs = root["tabs"].toArray(); | ||
for (QJsonValue tab : tabs) { | ||
auto tabObj = tab.toObject(); | ||
auto state = new TabState(this); | ||
state->setUrl(tabObj["url"].toString()); | ||
state->setTitle(tabObj["title"].toString()); | ||
state->setIcon(tabObj["icon"].toString()); | ||
m_tabs.append(state); | ||
} | ||
|
||
m_activeTab = std::max(m_tabs.count() - 1, 0); | ||
if (root.find("activeTab") != root.end()) { | ||
m_activeTab = root["activeTab"].toInt(); | ||
} | ||
} | ||
|
||
QByteArray Session::json(TabsModel* tabs) | ||
{ | ||
QJsonObject meta { | ||
{"schema", "0.1"} | ||
}; | ||
|
||
QJsonArray tabsArray; | ||
|
||
for (int i = 0; i < tabs->count(); ++i) { | ||
QJsonObject tabObject; | ||
auto tab = tabs->get(i); | ||
tabObject["url"] = tab->url().toString(); | ||
tabObject["title"] = tab->title(); | ||
tabObject["icon"] = tab->iconUrl().toString().replace("image://favicon/",""); | ||
tabObject["readingProgress"] = 0.f; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JSON fields for settings are currently all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No objections. Should I leave it like that for now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, let's :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
tabsArray.append(tabObject); | ||
} | ||
|
||
QJsonObject root { | ||
{"meta", meta}, | ||
{"tabs", tabsArray}, | ||
{"activeTab", tabs->activeIndex()}, | ||
}; | ||
|
||
QJsonDocument doc(root); | ||
return doc.toJson(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* This file is part of Liri Browser | ||
* | ||
* Copyright (C) 2017 Ivan Fateev <[email protected]> | ||
* | ||
* $BEGIN_LICENSE:GPL3+$ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* $END_LICENSE$ | ||
*/ | ||
|
||
#ifndef SESSION_H | ||
#define SESSION_H | ||
|
||
#include <QObject> | ||
#include <QVariantList> | ||
#include <QList> | ||
|
||
class TabsModel; | ||
class TabState; | ||
|
||
class Session : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit Session(QObject *parent = 0); | ||
|
||
Q_INVOKABLE void save(TabsModel* tabs); | ||
Q_INVOKABLE QVariantList getTabsToRestore(); | ||
Q_PROPERTY(int activeTab READ getActiveTab) | ||
|
||
int getActiveTab() { return m_activeTab; } | ||
private: | ||
void load(); | ||
QByteArray json(TabsModel *tabs); | ||
|
||
private: | ||
QList<TabState*> m_tabs; | ||
int m_activeTab; | ||
}; | ||
|
||
#endif // SESSION_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* This file is part of Liri Browser | ||
* | ||
* Copyright (C) 2017 Ivan Fateev <[email protected]> | ||
* | ||
* $BEGIN_LICENSE:GPL3+$ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* $END_LICENSE$ | ||
*/ | ||
|
||
|
||
#include "tabstate.h" | ||
|
||
TabState::TabState(QObject *parent) : QObject(parent) | ||
{ | ||
|
||
} | ||
|
||
void TabState::setUrl(QString url) | ||
{ | ||
if (m_url == url) | ||
return; | ||
|
||
m_url = url; | ||
} | ||
|
||
QString TabState::url() const | ||
{ | ||
return m_url; | ||
} | ||
|
||
void TabState::setTitle(QString title) | ||
{ | ||
m_title = title; | ||
} | ||
|
||
QString TabState::title() const | ||
{ | ||
return m_title; | ||
} | ||
|
||
void TabState::setIcon(QString icon) | ||
{ | ||
m_icon = icon; | ||
} | ||
|
||
QString TabState::icon() const | ||
{ | ||
return m_icon; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* This file is part of Liri Browser | ||
* | ||
* Copyright (C) 2017 Ivan Fateev <[email protected]> | ||
* | ||
* $BEGIN_LICENSE:GPL3+$ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* $END_LICENSE$ | ||
*/ | ||
|
||
#ifndef TABSTATE_H | ||
#define TABSTATE_H | ||
|
||
#include <QObject> | ||
|
||
class TabState : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
Q_PROPERTY(QString url READ url WRITE setUrl) | ||
Q_PROPERTY(QString title READ title WRITE setTitle) | ||
Q_PROPERTY(QString icon READ icon WRITE setIcon) | ||
|
||
public: | ||
explicit TabState(QObject *parent = 0); | ||
|
||
void setUrl(QString url); | ||
QString url() const; | ||
void setTitle(QString title); | ||
QString title() const; | ||
void setIcon(QString icon); | ||
QString icon() const; | ||
|
||
private: | ||
QString m_url; | ||
QString m_title; | ||
QString m_icon; | ||
}; | ||
|
||
#endif // TABSTATE_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this a bit more human-friendly, like "App data path doesn't exist".