Skip to content

Commit 0205db4

Browse files
authored
Merge pull request #1048 from ShaopengLin/Issue#186-remember-tabs-opened
Fix #186: Introduced Tab Restoration on Restart
2 parents 398a4fc + 01a6698 commit 0205db4

12 files changed

+141
-8
lines changed

resources/i18n/en.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,6 @@
162162
"content-type-searcher-placeholder": "Filter content type",
163163
"no-details": "Introduction only",
164164
"no-pictures": "No Pictures",
165-
"no-videos": "No Videos"
165+
"no-videos": "No Videos",
166+
"open-previous-tabs-at-startup": "Open previous tabs at startup"
166167
}

resources/i18n/qqq.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545
"monitor-dir-dialog-msg": "\"Monitor\" means \"watch\" in this context. The monitor directory is monitored/watched for new ZIM files.",
4646
"monitor-clear-dir-dialog-title": "\"Monitor\" means \"watch\" in this context. The monitor directory is monitored/watched for new ZIM files.",
4747
"monitor-clear-dir-dialog-msg": "\"Monitor\" means \"watch\" in this context. The monitor directory is monitored/watched for new ZIM files.",
48-
"open-book": "\"Open\" is a imperative, not an adjective."
48+
"open-book": "\"Open\" is a imperative, not an adjective.",
49+
"open-previous-tabs-at-startup": "The tabs that were open when the user closed the application is opened again when the application is restarted."
4950
}

src/kiwixapp.cpp

+34-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,31 @@ 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+
if (m_settingsManager.getReopenTab())
193+
{
194+
for (const auto &zimUrl : tabsToOpen)
195+
{
196+
try
197+
{
198+
/* Throws exception if zim file cannot be found */
199+
m_library.getArchive(QUrl(zimUrl).host().split('.')[0]);
200+
openUrl(QUrl(zimUrl));
201+
}
202+
catch (std::exception &e) { /* Blank */ }
203+
}
204+
}
205+
}
206+
179207
KiwixApp *KiwixApp::instance()
180208
{
181209
return static_cast<KiwixApp*>(QApplication::instance());
@@ -504,3 +532,8 @@ QString KiwixApp::parseStyleFromFile(QString filePath)
504532
file.close();
505533
return styleSheet;
506534
}
535+
536+
void KiwixApp::saveListOfOpenTabs()
537+
{
538+
return mp_session->setValue("reopenTabList", getTabWidget()->getTabUrls());
539+
}

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/settingsmanager.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ SettingsView* SettingsManager::getView()
1919
{
2020
if (m_view == nullptr) {
2121
auto view = new SettingsView();
22-
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir, m_moveToTrash);
22+
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir,
23+
m_moveToTrash, m_reopenTab);
2324
connect(view, &QObject::destroyed, this, [=]() { m_view = nullptr; });
2425
m_view = view;
2526
}
@@ -99,6 +100,13 @@ void SettingsManager::setMoveToTrash(bool moveToTrash)
99100
emit(moveToTrashChanged(m_moveToTrash));
100101
}
101102

103+
void SettingsManager::setReopenTab(bool reopenTab)
104+
{
105+
m_reopenTab = reopenTab;
106+
setSettings("reopenTab", m_reopenTab);
107+
emit(reopenTabChanged(reopenTab));
108+
}
109+
102110
QList<QVariant> SettingsManager::flattenPair(FilterList pairList)
103111
{
104112
QList<QVariant> res;
@@ -148,6 +156,7 @@ void SettingsManager::initSettings()
148156
m_kiwixServerIpAddress = m_settings.value("localKiwixServer/ipAddress", QString("0.0.0.0")).toString();
149157
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
150158
m_moveToTrash = m_settings.value("moveToTrash", true).toBool();
159+
m_reopenTab = m_settings.value("reopenTab", false).toBool();
151160
QString defaultLang = QLocale::languageToString(QLocale().language()) + '|' + QLocale().name().split("_").at(0);
152161

153162
/*

src/settingsmanager.h

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class SettingsManager : public QObject
2929
QString getDownloadDir() const { return m_downloadDir; }
3030
QString getMonitorDir() const { return m_monitorDir; }
3131
bool getMoveToTrash() const { return m_moveToTrash; }
32+
bool getReopenTab() const { return m_reopenTab; }
3233
FilterList getLanguageList() { return deducePair(m_langList); }
3334
FilterList getCategoryList() { return deducePair(m_categoryList); }
3435
FilterList getContentType() { return deducePair(m_contentTypeList); }
@@ -40,9 +41,11 @@ public slots:
4041
void setDownloadDir(QString downloadDir);
4142
void setMonitorDir(QString monitorDir);
4243
void setMoveToTrash(bool moveToTrash);
44+
void setReopenTab(bool reopenTab);
4345
void setLanguage(FilterList langList);
4446
void setCategory(FilterList categoryList);
4547
void setContentType(FilterList contentTypeList);
48+
4649
private:
4750
void initSettings();
4851
QList<QVariant> flattenPair(FilterList pairList);
@@ -54,6 +57,7 @@ public slots:
5457
void downloadDirChanged(QString downloadDir);
5558
void monitorDirChanged(QString monitorDir);
5659
void moveToTrashChanged(bool moveToTrash);
60+
void reopenTabChanged(bool reopenTab);
5761
void languageChanged(QList<QVariant> langList);
5862
void categoryChanged(QList<QVariant> categoryList);
5963
void contentTypeChanged(QList<QVariant> contentTypeList);
@@ -67,6 +71,7 @@ public slots:
6771
QString m_downloadDir;
6872
QString m_monitorDir;
6973
bool m_moveToTrash;
74+
bool m_reopenTab;
7075
QList<QVariant> m_langList;
7176
QList<QVariant> m_categoryList;
7277
QList<QVariant> m_contentTypeList;

src/settingsview.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SettingsView::SettingsView(QWidget *parent)
1212
ui->widget->setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/_settingsManager.css"));
1313
connect(ui->zoomPercentSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsView::setZoom);
1414
connect(ui->moveToTrashToggle, &QCheckBox::clicked, this, &SettingsView::setMoveToTrash);
15+
connect(ui->reopenTabToggle, &QCheckBox::clicked, this, &SettingsView::setReopenTab);
1516
connect(ui->browseButton, &QPushButton::clicked, this, &SettingsView::browseDownloadDir);
1617
connect(ui->resetButton, &QPushButton::clicked, this, &SettingsView::resetDownloadDir);
1718
connect(ui->monitorBrowse, &QPushButton::clicked, this, &SettingsView::browseMonitorDir);
@@ -20,6 +21,7 @@ SettingsView::SettingsView(QWidget *parent)
2021
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::monitorDirChanged, this, &SettingsView::onMonitorDirChanged);
2122
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::zoomChanged, this, &SettingsView::onZoomChanged);
2223
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::moveToTrashChanged, this, &SettingsView::onMoveToTrashChanged);
24+
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::reopenTabChanged, this, &SettingsView::onReopenTabChanged);
2325
ui->settingsLabel->setText(gt("settings"));
2426
ui->zoomPercentLabel->setText(gt("zoom-level-setting"));
2527
ui->downloadDirLabel->setText(gt("download-directory-setting"));
@@ -31,14 +33,18 @@ SettingsView::SettingsView(QWidget *parent)
3133
ui->monitorHelp->setText("<b>?</b>");
3234
ui->monitorHelp->setToolTip(gt("monitor-directory-tooltip"));
3335
ui->moveToTrashLabel->setText(gt("move-files-to-trash"));
36+
ui->reopenTabLabel->setText(gt("open-previous-tabs-at-startup"));
3437
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
38+
ui->line_5->hide();
3539
ui->moveToTrashLabel->hide();
3640
ui->moveToTrashToggle->hide();
3741
#endif
3842

3943
}
4044

41-
void SettingsView::init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const bool moveToTrash)
45+
void SettingsView::init(int zoomPercent, const QString &downloadDir,
46+
const QString &monitorDir, const bool moveToTrash,
47+
bool reopentab)
4248
{
4349
ui->zoomPercentSpinBox->setValue(zoomPercent);
4450
ui->downloadDirPath->setText(downloadDir);
@@ -47,6 +53,7 @@ void SettingsView::init(int zoomPercent, const QString &downloadDir, const QStri
4753
}
4854
ui->monitorDirPath->setText(monitorDir);
4955
ui->moveToTrashToggle->setChecked(moveToTrash);
56+
ui->reopenTabToggle->setChecked(reopentab);
5057
}
5158
bool SettingsView::confirmDialog( QString messageText, QString messageTitle)
5259
{
@@ -142,6 +149,11 @@ void SettingsView::setMoveToTrash(bool moveToTrash)
142149
KiwixApp::instance()->getSettingsManager()->setMoveToTrash(moveToTrash);
143150
}
144151

152+
void SettingsView::setReopenTab(bool reopen)
153+
{
154+
KiwixApp::instance()->getSettingsManager()->setReopenTab(reopen);
155+
}
156+
145157
void SettingsView::onDownloadDirChanged(const QString &dir)
146158
{
147159
ui->downloadDirPath->setText(dir);
@@ -167,3 +179,8 @@ void SettingsView::onMoveToTrashChanged(bool moveToTrash)
167179
{
168180
ui->moveToTrashToggle->setChecked(moveToTrash);
169181
}
182+
183+
void SettingsView::onReopenTabChanged(bool reopen)
184+
{
185+
ui->reopenTabToggle->setChecked(reopen);
186+
}

src/settingsview.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ class SettingsView : public QWidget
1111
public:
1212
SettingsView(QWidget *parent = nullptr);
1313
~SettingsView(){};
14-
void init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const bool moveToTrash);
15-
public Q_SLOTS:
14+
void init(int zoomPercent, const QString &downloadDir,
15+
const QString &monitorDir, const bool moveToTrash,
16+
bool reopentab);
17+
public Q_SLOTS:
1618
void resetDownloadDir();
1719
void browseDownloadDir();
1820
void browseMonitorDir();
1921
void clearMonitorDir();
2022
void setZoom(int zoomPercent);
2123
void setMoveToTrash(bool moveToTrash);
24+
void setReopenTab(bool reopen);
2225
void onDownloadDirChanged(const QString &dir);
2326
void onMonitorDirChanged(const QString &dir);
2427
void onZoomChanged(qreal zoomFactor);
2528
void onMoveToTrashChanged(bool moveToTrash);
29+
void onReopenTabChanged(bool reopen);
2630
private:
2731
bool confirmDialogDownloadDir(const QString& dir);
2832
bool confirmDialog(QString messageText, QString messageTitle);

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);

ui/settings.ui

+44
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,50 @@
328328
</item>
329329
</layout>
330330
</item>
331+
<item>
332+
<widget class="Line" name="line_6">
333+
<property name="orientation">
334+
<enum>Qt::Horizontal</enum>
335+
</property>
336+
</widget>
337+
</item>
338+
<item>
339+
<layout class="QHBoxLayout" name="horizontalLayout_9">
340+
<property name="topMargin">
341+
<number>0</number>
342+
</property>
343+
<property name="rightMargin">
344+
<number>0</number>
345+
</property>
346+
<item>
347+
<widget class="QLabel" name="reopenTabLabel">
348+
<property name="text">
349+
<string>Re-open closed tabs</string>
350+
</property>
351+
</widget>
352+
</item>
353+
<item>
354+
<spacer name="horizontalSpacer_5">
355+
<property name="orientation">
356+
<enum>Qt::Horizontal</enum>
357+
</property>
358+
<property name="sizeHint" stdset="0">
359+
<size>
360+
<width>40</width>
361+
<height>20</height>
362+
</size>
363+
</property>
364+
</spacer>
365+
</item>
366+
<item>
367+
<widget class="QCheckBox" name="reopenTabToggle">
368+
<property name="text">
369+
<string/>
370+
</property>
371+
</widget>
372+
</item>
373+
</layout>
374+
</item>
331375
<item>
332376
<spacer name="verticalSpacer">
333377
<property name="orientation">

0 commit comments

Comments
 (0)