Skip to content

Commit 00a2be1

Browse files
ShaopengLinAdam Lamar
authored and
Adam Lamar
committed
Add Auto Reopening of Tabs from Previous Session
Users can now restore tabs opened before close in the same order and same url. Fix #186
1 parent 95de67c commit 00a2be1

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

src/kiwixapp.cpp

+31-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
3232
mp_manager(nullptr),
3333
mp_mainWindow(nullptr),
3434
mp_nameMapper(std::make_shared<kiwix::UpdatableNameMapper>(m_library.getKiwixLibrary(), false)),
35-
m_server(m_library.getKiwixLibrary(), mp_nameMapper)
35+
m_server(m_library.getKiwixLibrary(), mp_nameMapper),
36+
mp_session(nullptr)
3637
{
3738
try {
3839
m_translation.setTranslation(QLocale());
@@ -113,6 +114,8 @@ void KiwixApp::init()
113114
m_library.asyncUpdateFromDir(dir);
114115
}
115116
}
117+
118+
restoreTabs();
116119
}
117120

118121
KiwixApp::~KiwixApp()
@@ -176,6 +179,28 @@ QString KiwixApp::findLibraryDirectory()
176179
return currentDataDir;
177180
}
178181

182+
void KiwixApp::restoreTabs()
183+
{
184+
/* Place session file in our global library path */
185+
QDir dir(m_libraryDirectory);
186+
mp_session = new QSettings(dir.filePath("kiwix-desktop.session"),
187+
QSettings::defaultFormat(), this);
188+
QStringList tabsToOpen = mp_session->value("reopenTabList").toStringList();
189+
190+
/* Restart a new session to prevent duplicate records in openURL */
191+
saveListOfOpenTabs();
192+
for (const auto &zimUrl : tabsToOpen)
193+
{
194+
try
195+
{
196+
/* Throws exception if zim file cannot be found */
197+
m_library.getArchive(QUrl(zimUrl).host().split('.')[0]);
198+
openUrl(QUrl(zimUrl));
199+
}
200+
catch (std::exception &e) { /* Blank */ }
201+
}
202+
}
203+
179204
KiwixApp *KiwixApp::instance()
180205
{
181206
return static_cast<KiwixApp*>(QApplication::instance());
@@ -504,3 +529,8 @@ QString KiwixApp::parseStyleFromFile(QString filePath)
504529
file.close();
505530
return styleSheet;
506531
}
532+
533+
void KiwixApp::saveListOfOpenTabs()
534+
{
535+
return mp_session->setValue("reopenTabList", getTabWidget()->getTabUrls());
536+
}

src/kiwixapp.h

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class KiwixApp : public QtSingleApplication
8787
void setMonitorDir(const QString &dir);
8888
bool isCurrentArticleBookmarked();
8989
QString parseStyleFromFile(QString filePath);
90+
void saveListOfOpenTabs();
9091

9192
public slots:
9293
void newTab();
@@ -116,10 +117,12 @@ public slots:
116117
kiwix::Server m_server;
117118
Translation m_translation;
118119
QFileSystemWatcher m_watcher;
120+
QSettings* mp_session;
119121

120122
QAction* mpa_actions[MAX_ACTION];
121123

122124
QString findLibraryDirectory();
125+
void restoreTabs();
123126
};
124127

125128
QString gt(const QString &key);

src/tabbar.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ void TabBar::closeTabsByZimId(const QString &id)
268268
}
269269
}
270270

271+
QStringList TabBar::getTabUrls() const {
272+
QStringList idList;
273+
for (int index = 0; index <= mp_stackedWidget->count(); index++)
274+
{
275+
if (ZimView* zv = qobject_cast<ZimView*>(mp_stackedWidget->widget(index)))
276+
idList.push_back(zv->getWebView()->url().url());
277+
}
278+
return idList;
279+
}
280+
271281
void TabBar::closeTab(int index)
272282
{
273283
// The first and last tabs (i.e. the library tab and the + (new tab) button)
@@ -285,6 +295,8 @@ void TabBar::closeTab(int index)
285295
removeTab(index);
286296
view->close();
287297
view->deleteLater();
298+
299+
KiwixApp::instance()->saveListOfOpenTabs();
288300
}
289301

290302
void TabBar::onCurrentChanged(int index)
@@ -466,4 +478,6 @@ void TabBar::onTabMoved(int from, int to)
466478
QWidget *w_from = mp_stackedWidget->widget(from);
467479
mp_stackedWidget->removeWidget(w_from);
468480
mp_stackedWidget->insertWidget(to, w_from);
481+
482+
KiwixApp::instance()->saveListOfOpenTabs();
469483
}

src/tabbar.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TabBar : public QTabBar
4949
virtual QSize tabSizeHint(int index) const;
5050
void openFindInPageBar();
5151
void closeTabsByZimId(const QString &id);
52+
QStringList getTabUrls() const;
5253

5354
protected:
5455
void mousePressEvent(QMouseEvent *event);

src/webview.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,13 @@ QWebEngineView* WebView::createWindow(QWebEnginePage::WebWindowType type)
166166

167167
void WebView::onUrlChanged(const QUrl& url) {
168168
auto zimId = getZimIdFromUrl(url);
169+
auto app = KiwixApp::instance();
170+
app->saveListOfOpenTabs();
169171
if (m_currentZimId == zimId ) {
170172
return;
171173
}
172174
m_currentZimId = zimId;
173175
emit zimIdChanged(m_currentZimId);
174-
auto app = KiwixApp::instance();
175176
std::shared_ptr<zim::Archive> archive;
176177
try {
177178
archive = app->getLibrary()->getArchive(m_currentZimId);

0 commit comments

Comments
 (0)