Skip to content
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
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/global/paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Paths {
const QString ConfigLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/liri-browser/";
const QString DataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/liri-browser/";
const QString SettingsFile = ConfigLocation + "settings.json";
const QString SessionDataFile = DataLocation + "session.json";
}

#endif // PATHS_H
131 changes: 131 additions & 0 deletions src/core/session/session.cpp
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.";
Copy link
Collaborator

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".

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();
}
54 changes: 54 additions & 0 deletions src/core/session/session.h
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
63 changes: 63 additions & 0 deletions src/core/session/tabstate.cpp
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;
}
53 changes: 53 additions & 0 deletions src/core/session/tabstate.h
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
28 changes: 27 additions & 1 deletion src/core/settings/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ void Settings::load()
m_startConfig->setPrimaryStartUrl(QUrl(dataStart["primary_url"].toString()));
m_startConfig->setDarkStartUrl(QUrl(dataStart["dark_theme_url"].toString()));
m_startConfig->setIncognitoStartUrl(QUrl(dataStart["incognito_url"].toString()));

StartConfig::StartupType startupType;
QString startupTypeString = dataStart["startupType"].toString();

if (startupTypeString == "start_from_new_page")
startupType = StartConfig::StartupType::StartFromNewPage;
else if (startupTypeString == "start_from_previously_opened_tabs")
startupType = StartConfig::StartupType::StartFromPreviouslyOpenedTabs;

m_startConfig->setStartupType(startupType);

QJsonObject dataSearch = data["search"].toObject();
QString searchEngineString = dataSearch["engine"].toString();
SearchConfig::SearchEngine searchEngine;
Expand Down Expand Up @@ -133,7 +144,8 @@ QByteArray Settings::defaultJSON()
QJsonObject dataStart {
{"primary_url", m_startConfig->defaultPrimaryStartUrl().toString()},
{"dark_theme_url", m_startConfig->defaultDarkStartUrl().toString()},
{"incognito_url", m_startConfig->defaultIncognitoStartUrl().toString()}
{"incognito_url", m_startConfig->defaultIncognitoStartUrl().toString()},
{"startupType", "start_from_new_page"}
};
QJsonObject dataSearch {
{"engine", "duckduckgo"},
Expand Down Expand Up @@ -163,11 +175,25 @@ QByteArray Settings::json()
QJsonObject meta {
{"schema", "0.1"}
};

QString startupTypeString;

switch (m_startConfig->startupType()) {
case StartConfig::StartupType::StartFromNewPage:
startupTypeString = "start_from_new_page";
break;
case StartConfig::StartupType::StartFromPreviouslyOpenedTabs:
startupTypeString = "start_from_previously_opened_tabs";
break;
}

QJsonObject dataStart {
{"primary_url", m_startConfig->primaryStartUrl().toString()},
{"dark_theme_url", m_startConfig->darkStartUrl().toString()},
{"incognito_url", m_startConfig->incognitoStartUrl().toString()},
{"startupType", startupTypeString},
};

QString searchEngineString;
switch (m_searchConfig->searchEngine()) {
case SearchConfig::SearchEngine::DuckDuckGo:
Expand Down
1 change: 1 addition & 0 deletions src/core/settings/startconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ StartConfig::StartConfig(QObject *parent)
m_defaultPrimaryStartUrl = QUrl("https://duckduckgo.com");
m_defaultDarkStartUrl = QUrl("https://duckduckgo.com/?kae=#303030");
m_defaultIncognitoStartUrl = QUrl("https://duckduckgo.com/?kae=#37474f");
m_startupType = StartupType::StartFromNewPage;
}
Loading