File indexing completed on 2024-05-12 04:58:21
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 0019 #include "recoveryjsobject.h" 0020 #include "mainapplication.h" 0021 #include "webpage.h" 0022 #include "tabbedwebview.h" 0023 #include "browserwindow.h" 0024 #include "qztools.h" 0025 #include "iconprovider.h" 0026 0027 #include <QJsonObject> 0028 0029 RecoveryJsObject::RecoveryJsObject(RestoreManager *manager) 0030 : QObject() 0031 , m_manager(manager) 0032 , m_page(nullptr) 0033 { 0034 } 0035 0036 void RecoveryJsObject::setPage(WebPage *page) 0037 { 0038 Q_ASSERT(page); 0039 0040 m_page = page; 0041 } 0042 0043 QJsonArray RecoveryJsObject::restoreData() const 0044 { 0045 QJsonArray out; 0046 0047 int i = 0; 0048 const auto windows = m_manager->restoreData().windows; 0049 for (const BrowserWindow::SavedWindow &w : windows) { 0050 int j = 0; 0051 QJsonArray tabs; 0052 const auto windowTabs = w.tabs; 0053 for (const WebTab::SavedTab &t : windowTabs) { 0054 const QIcon icon = t.icon.isNull() ? IconProvider::emptyWebIcon() : t.icon; 0055 QJsonObject tab; 0056 tab[QSL("tab")] = j; 0057 tab[QSL("icon")] = QzTools::pixmapToDataUrl(icon.pixmap(16)).toString(); 0058 tab[QSL("title")] = t.title; 0059 tab[QSL("url")] = t.url.toString(); 0060 tab[QSL("pinned")] = t.isPinned; 0061 tab[QSL("current")] = w.currentTab == j; 0062 tabs.append(tab); 0063 j++; 0064 } 0065 0066 QJsonObject window; 0067 window[QSL("window")] = i++; 0068 window[QSL("tabs")] = tabs; 0069 out.append(window); 0070 } 0071 0072 return out; 0073 } 0074 0075 void RecoveryJsObject::startNewSession() 0076 { 0077 auto *view = qobject_cast<TabbedWebView*>(m_page->view()); 0078 if (!view) { 0079 return; 0080 } 0081 0082 if (view->browserWindow()->tabCount() > 1) { 0083 view->closeView(); 0084 } else { 0085 auto *oldWindow = view->browserWindow(); 0086 mApp->createWindow(Qz::BW_NewWindow); 0087 oldWindow->close(); 0088 } 0089 0090 mApp->restoreManager()->clearRestoreData(); 0091 mApp->destroyRestoreManager(); 0092 } 0093 0094 void RecoveryJsObject::restoreSession(const QStringList &excludeWin, const QStringList &excludeTab) 0095 { 0096 Q_ASSERT(excludeWin.size() == excludeTab.size()); 0097 0098 // This assumes that excludeWin and excludeTab are sorted in descending order 0099 0100 RestoreData data = m_manager->restoreData(); 0101 0102 for (int i = 0; i < excludeWin.size(); ++i) { 0103 int win = excludeWin.at(i).toInt(); 0104 int tab = excludeTab.at(i).toInt(); 0105 0106 if (!QzTools::containsIndex(data.windows, win) || !QzTools::containsIndex(data.windows.at(win).tabs, tab)) 0107 continue; 0108 0109 BrowserWindow::SavedWindow &wd = data.windows[win]; 0110 0111 wd.tabs.remove(tab); 0112 if (wd.currentTab >= tab) 0113 --wd.currentTab; 0114 0115 if (wd.tabs.isEmpty()) { 0116 data.windows.remove(win); 0117 continue; 0118 } 0119 0120 if (wd.currentTab < 0) 0121 wd.currentTab = wd.tabs.size() - 1; 0122 } 0123 0124 if (mApp->restoreSession(nullptr, data)) { 0125 closeTab(); 0126 } else { 0127 startNewSession(); 0128 } 0129 } 0130 0131 void RecoveryJsObject::closeTab() 0132 { 0133 auto *view = qobject_cast<TabbedWebView*>(m_page->view()); 0134 if (!view) { 0135 return; 0136 } 0137 0138 if (view->browserWindow()->tabCount() > 1) { 0139 view->closeView(); 0140 } else { 0141 view->browserWindow()->close(); 0142 } 0143 }