2015-09-24 22:46:35 +02:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2018-01-02 12:56:10 +01:00
|
|
|
* Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
|
2015-09-24 22:46:35 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
|
|
|
|
#include "recoveryjsobject.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "webpage.h"
|
|
|
|
#include "tabbedwebview.h"
|
|
|
|
#include "browserwindow.h"
|
2015-09-25 15:48:26 +02:00
|
|
|
#include "qztools.h"
|
2017-01-01 13:05:14 +01:00
|
|
|
#include "iconprovider.h"
|
2015-09-25 15:48:26 +02:00
|
|
|
|
|
|
|
#include <QJsonObject>
|
2015-09-24 22:46:35 +02:00
|
|
|
|
|
|
|
RecoveryJsObject::RecoveryJsObject(RestoreManager *manager)
|
|
|
|
: QObject()
|
|
|
|
, m_manager(manager)
|
|
|
|
, m_page(Q_NULLPTR)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecoveryJsObject::setPage(WebPage *page)
|
|
|
|
{
|
|
|
|
Q_ASSERT(page);
|
|
|
|
|
|
|
|
m_page = page;
|
|
|
|
}
|
|
|
|
|
2015-09-25 15:48:26 +02:00
|
|
|
QJsonArray RecoveryJsObject::restoreData() const
|
|
|
|
{
|
|
|
|
QJsonArray out;
|
|
|
|
|
|
|
|
int i = 0;
|
2018-01-02 12:56:10 +01:00
|
|
|
Q_FOREACH (const BrowserWindow::SavedWindow &w, m_manager->restoreData()) {
|
2015-09-25 15:48:26 +02:00
|
|
|
int j = 0;
|
|
|
|
QJsonArray tabs;
|
2018-01-02 12:56:10 +01:00
|
|
|
Q_FOREACH (const WebTab::SavedTab &t, w.tabs) {
|
2017-01-01 13:05:14 +01:00
|
|
|
const QIcon icon = t.icon.isNull() ? IconProvider::emptyWebIcon() : t.icon;
|
2015-09-25 15:48:26 +02:00
|
|
|
QJsonObject tab;
|
2016-12-27 20:02:31 +01:00
|
|
|
tab[QSL("tab")] = j;
|
2017-01-01 13:05:14 +01:00
|
|
|
tab[QSL("icon")] = QzTools::pixmapToDataUrl(icon.pixmap(16)).toString();
|
2015-09-25 15:48:26 +02:00
|
|
|
tab[QSL("title")] = t.title;
|
2016-12-27 19:15:10 +01:00
|
|
|
tab[QSL("url")] = t.url.toString();
|
2016-12-27 20:02:31 +01:00
|
|
|
tab[QSL("pinned")] = t.isPinned;
|
|
|
|
tab[QSL("current")] = w.currentTab == j;
|
2015-09-25 15:48:26 +02:00
|
|
|
tabs.append(tab);
|
2016-12-27 20:02:31 +01:00
|
|
|
j++;
|
2015-09-25 15:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject window;
|
|
|
|
window[QSL("window")] = i++;
|
|
|
|
window[QSL("tabs")] = tabs;
|
|
|
|
out.append(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:46:35 +02:00
|
|
|
void RecoveryJsObject::startNewSession()
|
|
|
|
{
|
2015-09-24 23:08:55 +02:00
|
|
|
BrowserWindow *window = getBrowserWindow();
|
|
|
|
if (!window)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_page->load(window->homepageUrl());
|
2015-09-24 22:46:35 +02:00
|
|
|
|
|
|
|
mApp->destroyRestoreManager();
|
|
|
|
}
|
|
|
|
|
2015-09-25 15:48:26 +02:00
|
|
|
void RecoveryJsObject::restoreSession(const QStringList &excludeWin, const QStringList &excludeTab)
|
2015-09-24 22:46:35 +02:00
|
|
|
{
|
2015-09-25 15:48:26 +02:00
|
|
|
Q_ASSERT(excludeWin.size() == excludeTab.size());
|
|
|
|
|
2016-12-27 19:45:51 +01:00
|
|
|
// This assumes that excludeWin and excludeTab are sorted in descending order
|
|
|
|
|
2015-09-25 15:48:26 +02:00
|
|
|
RestoreData data = m_manager->restoreData();
|
|
|
|
|
|
|
|
for (int i = 0; i < excludeWin.size(); ++i) {
|
|
|
|
int win = excludeWin.at(i).toInt();
|
|
|
|
int tab = excludeTab.at(i).toInt();
|
|
|
|
|
2018-01-02 12:56:10 +01:00
|
|
|
if (!QzTools::containsIndex(data, win) || !QzTools::containsIndex(data.at(win).tabs, tab))
|
2015-09-25 15:48:26 +02:00
|
|
|
continue;
|
|
|
|
|
2018-01-02 12:56:10 +01:00
|
|
|
BrowserWindow::SavedWindow &wd = data[win];
|
2015-09-25 15:48:26 +02:00
|
|
|
|
2018-01-02 12:56:10 +01:00
|
|
|
wd.tabs.remove(tab);
|
2015-09-25 15:48:26 +02:00
|
|
|
if (wd.currentTab >= tab)
|
|
|
|
--wd.currentTab;
|
|
|
|
|
2018-01-02 12:56:10 +01:00
|
|
|
if (wd.tabs.isEmpty()) {
|
2015-09-25 15:48:26 +02:00
|
|
|
data.remove(win);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wd.currentTab < 0)
|
2018-01-02 12:56:10 +01:00
|
|
|
wd.currentTab = wd.tabs.size() - 1;
|
2015-09-25 15:48:26 +02:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:08:55 +02:00
|
|
|
BrowserWindow *window = getBrowserWindow();
|
|
|
|
if (!window)
|
|
|
|
return;
|
|
|
|
|
2018-01-02 12:56:10 +01:00
|
|
|
if (!mApp->restoreSession(window, data))
|
2015-09-24 22:46:35 +02:00
|
|
|
startNewSession();
|
2015-09-24 23:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BrowserWindow *RecoveryJsObject::getBrowserWindow() const
|
|
|
|
{
|
|
|
|
TabbedWebView *view = qobject_cast<TabbedWebView*>(m_page->view());
|
|
|
|
return view ? view->browserWindow() : Q_NULLPTR;
|
2015-09-24 22:46:35 +02:00
|
|
|
}
|