forked from lirios/browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Fateev
authored and
Ivan Fateev
committed
Mar 14, 2018
1 parent
98705cc
commit 2f7dbd0
Showing
11 changed files
with
268 additions
and
5 deletions.
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,93 @@ | ||
#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) | ||
{ | ||
load(); | ||
} | ||
|
||
void Session::save(TabsModel* tabs) | ||
{ | ||
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()); | ||
m_tabs.append(state); | ||
} | ||
} | ||
|
||
QByteArray Session::json(TabsModel* tabs) | ||
{ | ||
QJsonObject meta { | ||
{"schema", "0.1"} | ||
}; | ||
|
||
QJsonArray tabsArray; | ||
|
||
for (int i = 0; i < tabs->count(); ++i) { | ||
QJsonObject tabObject; | ||
tabObject["url"] = tabs->get(i)->url().toString(); | ||
tabObject["readingProgress"] = 0.f; | ||
tabsArray.append(tabObject); | ||
} | ||
|
||
QJsonObject root { | ||
{"meta", meta}, | ||
{"tabs", tabsArray}, | ||
}; | ||
|
||
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,31 @@ | ||
#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(); | ||
signals: | ||
|
||
public slots: | ||
|
||
private: | ||
void load(); | ||
QByteArray json(TabsModel *tabs); | ||
|
||
private: | ||
QList<TabState*> m_tabs; | ||
}; | ||
|
||
#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,29 @@ | ||
#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; | ||
} | ||
|
||
float TabState::readingProgress() const | ||
{ | ||
return m_readingProgress; | ||
} | ||
|
||
void TabState::setReadingProgress(float readingProgress) | ||
{ | ||
m_readingProgress = readingProgress; | ||
} |
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,30 @@ | ||
#ifndef TABSTATE_H | ||
#define TABSTATE_H | ||
|
||
#include <QObject> | ||
|
||
class TabState : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
Q_PROPERTY(QString url READ url WRITE setUrl) | ||
Q_PROPERTY(float readingProgress READ readingProgress WRITE setReadingProgress) | ||
|
||
public: | ||
explicit TabState(QObject *parent = 0); | ||
|
||
void setUrl(QString url); | ||
QString url() const; | ||
float readingProgress() const; | ||
void setReadingProgress(float readingProgress); | ||
|
||
signals: | ||
|
||
public slots: | ||
|
||
private: | ||
QString m_url; | ||
float m_readingProgress; | ||
}; | ||
|
||
#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
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
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