File indexing completed on 2024-05-12 04:58:26
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 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 #include "closedwindowsmanager.h" 0019 #include "mainapplication.h" 0020 #include "tabbedwebview.h" 0021 #include "qztools.h" 0022 0023 #include <QAction> 0024 0025 ClosedWindowsManager::ClosedWindowsManager(QObject *parent) 0026 : QObject(parent) 0027 { 0028 } 0029 0030 bool ClosedWindowsManager::isClosedWindowAvailable() const 0031 { 0032 return !m_closedWindows.isEmpty(); 0033 } 0034 0035 QVector<ClosedWindowsManager::Window> ClosedWindowsManager::closedWindows() const 0036 { 0037 return m_closedWindows; 0038 } 0039 0040 void ClosedWindowsManager::saveWindow(BrowserWindow *window) 0041 { 0042 if (mApp->isPrivate() || mApp->windowCount() == 1 || !window->weView()) { 0043 return; 0044 } 0045 0046 Window closedWindow; 0047 closedWindow.icon = window->weView()->icon(); 0048 closedWindow.title = window->weView()->title(); 0049 closedWindow.windowState = BrowserWindow::SavedWindow(window); 0050 m_closedWindows.prepend(closedWindow); 0051 } 0052 0053 ClosedWindowsManager::Window ClosedWindowsManager::takeLastClosedWindow() 0054 { 0055 Window window; 0056 if (!m_closedWindows.isEmpty()) { 0057 window = m_closedWindows.takeFirst(); 0058 } 0059 return window; 0060 } 0061 0062 ClosedWindowsManager::Window ClosedWindowsManager::takeClosedWindowAt(int index) 0063 { 0064 Window window; 0065 if (QzTools::containsIndex(m_closedWindows, index)) { 0066 window = m_closedWindows.takeAt(index); 0067 } 0068 return window; 0069 } 0070 0071 void ClosedWindowsManager::restoreClosedWindow() 0072 { 0073 Window window; 0074 auto *act = qobject_cast<QAction*>(sender()); 0075 if (act) { 0076 window = takeClosedWindowAt(act->data().toInt()); 0077 } else { 0078 window = takeLastClosedWindow(); 0079 } 0080 0081 if (!window.isValid()) { 0082 return; 0083 } 0084 0085 mApp->createWindow(Qz::BW_OtherRestoredWindow)->restoreWindow(window.windowState); 0086 } 0087 0088 void ClosedWindowsManager::restoreAllClosedWindows() 0089 { 0090 const int count = m_closedWindows.count(); 0091 for (int i = 0; i < count; ++i) { 0092 restoreClosedWindow(); 0093 } 0094 } 0095 0096 void ClosedWindowsManager::clearClosedWindows() 0097 { 0098 m_closedWindows.clear(); 0099 } 0100 0101 static const int closedWindowsVersion = 1; 0102 0103 QByteArray ClosedWindowsManager::saveState() const 0104 { 0105 QByteArray data; 0106 QDataStream stream(&data, QIODevice::WriteOnly); 0107 0108 stream << closedWindowsVersion; 0109 0110 // Only save last 3 windows 0111 const int windowCount = qBound(0, m_closedWindows.count(), 3); 0112 stream << windowCount; 0113 0114 for (int i = 0; i < windowCount; ++i) { 0115 stream << m_closedWindows.at(i).windowState; 0116 } 0117 0118 return data; 0119 } 0120 0121 void ClosedWindowsManager::restoreState(const QByteArray &state) 0122 { 0123 QDataStream stream(state); 0124 0125 int version; 0126 stream >> version; 0127 0128 if (version < 1) { 0129 return; 0130 } 0131 0132 m_closedWindows.clear(); 0133 0134 int windowCount; 0135 stream >> windowCount; 0136 m_closedWindows.reserve(windowCount); 0137 0138 for (int i = 0; i < windowCount; ++i) { 0139 Window window; 0140 stream >> window.windowState; 0141 if (!window.isValid()) { 0142 continue; 0143 } 0144 window.icon = window.windowState.tabs.at(0).icon; 0145 window.title = window.windowState.tabs.at(0).title; 0146 m_closedWindows.append(window); 0147 } 0148 }