-
-
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
Merged
timsueberkrueb
merged 12 commits into
lirios:develop
from
PoisonousJohn:feature/start_with_last_opened_tabs
Mar 21, 2018
+495
−41
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2f7dbd0
Finished main work on #50
5169e92
Added missing copyrights header
4c786df
fixed sesson header
6ef5bde
Finished main work on #50
a187110
Added missing copyrights header
b2f5836
do not load all tabs upon restoring
c57252e
fixes according to the review
ae27765
forgotten stylefix
aa3de28
more stylefixes
a7d0768
fixed loading of icons, few style fixes as well
370fa1b
added missing qsTr
2457b9d
removed https hack
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* 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) { | ||
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/" ,""); | ||
tabsArray.append(tabObject); | ||
} | ||
|
||
QJsonObject root { | ||
{"meta", meta}, | ||
{"tabs", tabsArray}, | ||
{"activeTab", tabs->activeIndex()}, | ||
}; | ||
|
||
QJsonDocument doc(root); | ||
return doc.toJson(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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".