Skip to content

Commit

Permalink
Merge pull request #1048 from ShaopengLin/Issue#186-remember-tabs-opened
Browse files Browse the repository at this point in the history
Fix #186: Introduced Tab Restoration on Restart
  • Loading branch information
kelson42 authored Mar 30, 2024
2 parents 398a4fc + 01a6698 commit 0205db4
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 8 deletions.
3 changes: 2 additions & 1 deletion resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,6 @@
"content-type-searcher-placeholder": "Filter content type",
"no-details": "Introduction only",
"no-pictures": "No Pictures",
"no-videos": "No Videos"
"no-videos": "No Videos",
"open-previous-tabs-at-startup": "Open previous tabs at startup"
}
3 changes: 2 additions & 1 deletion resources/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@
"monitor-dir-dialog-msg": "\"Monitor\" means \"watch\" in this context. The monitor directory is monitored/watched for new ZIM files.",
"monitor-clear-dir-dialog-title": "\"Monitor\" means \"watch\" in this context. The monitor directory is monitored/watched for new ZIM files.",
"monitor-clear-dir-dialog-msg": "\"Monitor\" means \"watch\" in this context. The monitor directory is monitored/watched for new ZIM files.",
"open-book": "\"Open\" is a imperative, not an adjective."
"open-book": "\"Open\" is a imperative, not an adjective.",
"open-previous-tabs-at-startup": "The tabs that were open when the user closed the application is opened again when the application is restarted."
}
35 changes: 34 additions & 1 deletion src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
mp_manager(nullptr),
mp_mainWindow(nullptr),
mp_nameMapper(std::make_shared<kiwix::UpdatableNameMapper>(m_library.getKiwixLibrary(), false)),
m_server(m_library.getKiwixLibrary(), mp_nameMapper)
m_server(m_library.getKiwixLibrary(), mp_nameMapper),
mp_session(nullptr)
{
try {
m_translation.setTranslation(QLocale());
Expand Down Expand Up @@ -113,6 +114,8 @@ void KiwixApp::init()
m_library.asyncUpdateFromDir(dir);
}
}

restoreTabs();
}

KiwixApp::~KiwixApp()
Expand Down Expand Up @@ -176,6 +179,31 @@ QString KiwixApp::findLibraryDirectory()
return currentDataDir;
}

void KiwixApp::restoreTabs()
{
/* Place session file in our global library path */
QDir dir(m_libraryDirectory);
mp_session = new QSettings(dir.filePath("kiwix-desktop.session"),
QSettings::defaultFormat(), this);
QStringList tabsToOpen = mp_session->value("reopenTabList").toStringList();

/* Restart a new session to prevent duplicate records in openURL */
saveListOfOpenTabs();
if (m_settingsManager.getReopenTab())
{
for (const auto &zimUrl : tabsToOpen)
{
try
{
/* Throws exception if zim file cannot be found */
m_library.getArchive(QUrl(zimUrl).host().split('.')[0]);
openUrl(QUrl(zimUrl));
}
catch (std::exception &e) { /* Blank */ }
}
}
}

KiwixApp *KiwixApp::instance()
{
return static_cast<KiwixApp*>(QApplication::instance());
Expand Down Expand Up @@ -504,3 +532,8 @@ QString KiwixApp::parseStyleFromFile(QString filePath)
file.close();
return styleSheet;
}

void KiwixApp::saveListOfOpenTabs()
{
return mp_session->setValue("reopenTabList", getTabWidget()->getTabUrls());
}
3 changes: 3 additions & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class KiwixApp : public QtSingleApplication
void setMonitorDir(const QString &dir);
bool isCurrentArticleBookmarked();
QString parseStyleFromFile(QString filePath);
void saveListOfOpenTabs();

public slots:
void newTab();
Expand Down Expand Up @@ -116,10 +117,12 @@ public slots:
kiwix::Server m_server;
Translation m_translation;
QFileSystemWatcher m_watcher;
QSettings* mp_session;

QAction* mpa_actions[MAX_ACTION];

QString findLibraryDirectory();
void restoreTabs();
};

QString gt(const QString &key);
Expand Down
11 changes: 10 additions & 1 deletion src/settingsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ SettingsView* SettingsManager::getView()
{
if (m_view == nullptr) {
auto view = new SettingsView();
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir, m_moveToTrash);
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir,
m_moveToTrash, m_reopenTab);
connect(view, &QObject::destroyed, this, [=]() { m_view = nullptr; });
m_view = view;
}
Expand Down Expand Up @@ -99,6 +100,13 @@ void SettingsManager::setMoveToTrash(bool moveToTrash)
emit(moveToTrashChanged(m_moveToTrash));
}

void SettingsManager::setReopenTab(bool reopenTab)
{
m_reopenTab = reopenTab;
setSettings("reopenTab", m_reopenTab);
emit(reopenTabChanged(reopenTab));
}

QList<QVariant> SettingsManager::flattenPair(FilterList pairList)
{
QList<QVariant> res;
Expand Down Expand Up @@ -148,6 +156,7 @@ void SettingsManager::initSettings()
m_kiwixServerIpAddress = m_settings.value("localKiwixServer/ipAddress", QString("0.0.0.0")).toString();
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
m_moveToTrash = m_settings.value("moveToTrash", true).toBool();
m_reopenTab = m_settings.value("reopenTab", false).toBool();
QString defaultLang = QLocale::languageToString(QLocale().language()) + '|' + QLocale().name().split("_").at(0);

/*
Expand Down
5 changes: 5 additions & 0 deletions src/settingsmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SettingsManager : public QObject
QString getDownloadDir() const { return m_downloadDir; }
QString getMonitorDir() const { return m_monitorDir; }
bool getMoveToTrash() const { return m_moveToTrash; }
bool getReopenTab() const { return m_reopenTab; }
FilterList getLanguageList() { return deducePair(m_langList); }
FilterList getCategoryList() { return deducePair(m_categoryList); }
FilterList getContentType() { return deducePair(m_contentTypeList); }
Expand All @@ -40,9 +41,11 @@ public slots:
void setDownloadDir(QString downloadDir);
void setMonitorDir(QString monitorDir);
void setMoveToTrash(bool moveToTrash);
void setReopenTab(bool reopenTab);
void setLanguage(FilterList langList);
void setCategory(FilterList categoryList);
void setContentType(FilterList contentTypeList);

private:
void initSettings();
QList<QVariant> flattenPair(FilterList pairList);
Expand All @@ -54,6 +57,7 @@ public slots:
void downloadDirChanged(QString downloadDir);
void monitorDirChanged(QString monitorDir);
void moveToTrashChanged(bool moveToTrash);
void reopenTabChanged(bool reopenTab);
void languageChanged(QList<QVariant> langList);
void categoryChanged(QList<QVariant> categoryList);
void contentTypeChanged(QList<QVariant> contentTypeList);
Expand All @@ -67,6 +71,7 @@ public slots:
QString m_downloadDir;
QString m_monitorDir;
bool m_moveToTrash;
bool m_reopenTab;
QList<QVariant> m_langList;
QList<QVariant> m_categoryList;
QList<QVariant> m_contentTypeList;
Expand Down
19 changes: 18 additions & 1 deletion src/settingsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SettingsView::SettingsView(QWidget *parent)
ui->widget->setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/_settingsManager.css"));
connect(ui->zoomPercentSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsView::setZoom);
connect(ui->moveToTrashToggle, &QCheckBox::clicked, this, &SettingsView::setMoveToTrash);
connect(ui->reopenTabToggle, &QCheckBox::clicked, this, &SettingsView::setReopenTab);
connect(ui->browseButton, &QPushButton::clicked, this, &SettingsView::browseDownloadDir);
connect(ui->resetButton, &QPushButton::clicked, this, &SettingsView::resetDownloadDir);
connect(ui->monitorBrowse, &QPushButton::clicked, this, &SettingsView::browseMonitorDir);
Expand All @@ -20,6 +21,7 @@ SettingsView::SettingsView(QWidget *parent)
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::monitorDirChanged, this, &SettingsView::onMonitorDirChanged);
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::zoomChanged, this, &SettingsView::onZoomChanged);
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::moveToTrashChanged, this, &SettingsView::onMoveToTrashChanged);
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::reopenTabChanged, this, &SettingsView::onReopenTabChanged);
ui->settingsLabel->setText(gt("settings"));
ui->zoomPercentLabel->setText(gt("zoom-level-setting"));
ui->downloadDirLabel->setText(gt("download-directory-setting"));
Expand All @@ -31,14 +33,18 @@ SettingsView::SettingsView(QWidget *parent)
ui->monitorHelp->setText("<b>?</b>");
ui->monitorHelp->setToolTip(gt("monitor-directory-tooltip"));
ui->moveToTrashLabel->setText(gt("move-files-to-trash"));
ui->reopenTabLabel->setText(gt("open-previous-tabs-at-startup"));
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
ui->line_5->hide();
ui->moveToTrashLabel->hide();
ui->moveToTrashToggle->hide();
#endif

}

void SettingsView::init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const bool moveToTrash)
void SettingsView::init(int zoomPercent, const QString &downloadDir,
const QString &monitorDir, const bool moveToTrash,
bool reopentab)
{
ui->zoomPercentSpinBox->setValue(zoomPercent);
ui->downloadDirPath->setText(downloadDir);
Expand All @@ -47,6 +53,7 @@ void SettingsView::init(int zoomPercent, const QString &downloadDir, const QStri
}
ui->monitorDirPath->setText(monitorDir);
ui->moveToTrashToggle->setChecked(moveToTrash);
ui->reopenTabToggle->setChecked(reopentab);
}
bool SettingsView::confirmDialog( QString messageText, QString messageTitle)
{
Expand Down Expand Up @@ -142,6 +149,11 @@ void SettingsView::setMoveToTrash(bool moveToTrash)
KiwixApp::instance()->getSettingsManager()->setMoveToTrash(moveToTrash);
}

void SettingsView::setReopenTab(bool reopen)
{
KiwixApp::instance()->getSettingsManager()->setReopenTab(reopen);
}

void SettingsView::onDownloadDirChanged(const QString &dir)
{
ui->downloadDirPath->setText(dir);
Expand All @@ -167,3 +179,8 @@ void SettingsView::onMoveToTrashChanged(bool moveToTrash)
{
ui->moveToTrashToggle->setChecked(moveToTrash);
}

void SettingsView::onReopenTabChanged(bool reopen)
{
ui->reopenTabToggle->setChecked(reopen);
}
8 changes: 6 additions & 2 deletions src/settingsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ class SettingsView : public QWidget
public:
SettingsView(QWidget *parent = nullptr);
~SettingsView(){};
void init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const bool moveToTrash);
public Q_SLOTS:
void init(int zoomPercent, const QString &downloadDir,
const QString &monitorDir, const bool moveToTrash,
bool reopentab);
public Q_SLOTS:
void resetDownloadDir();
void browseDownloadDir();
void browseMonitorDir();
void clearMonitorDir();
void setZoom(int zoomPercent);
void setMoveToTrash(bool moveToTrash);
void setReopenTab(bool reopen);
void onDownloadDirChanged(const QString &dir);
void onMonitorDirChanged(const QString &dir);
void onZoomChanged(qreal zoomFactor);
void onMoveToTrashChanged(bool moveToTrash);
void onReopenTabChanged(bool reopen);
private:
bool confirmDialogDownloadDir(const QString& dir);
bool confirmDialog(QString messageText, QString messageTitle);
Expand Down
14 changes: 14 additions & 0 deletions src/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ void TabBar::closeTabsByZimId(const QString &id)
}
}

QStringList TabBar::getTabUrls() const {
QStringList idList;
for (int index = 0; index <= mp_stackedWidget->count(); index++)
{
if (ZimView* zv = qobject_cast<ZimView*>(mp_stackedWidget->widget(index)))
idList.push_back(zv->getWebView()->url().url());
}
return idList;
}

void TabBar::closeTab(int index)
{
// The first and last tabs (i.e. the library tab and the + (new tab) button)
Expand All @@ -285,6 +295,8 @@ void TabBar::closeTab(int index)
removeTab(index);
view->close();
view->deleteLater();

KiwixApp::instance()->saveListOfOpenTabs();
}

void TabBar::onCurrentChanged(int index)
Expand Down Expand Up @@ -466,4 +478,6 @@ void TabBar::onTabMoved(int from, int to)
QWidget *w_from = mp_stackedWidget->widget(from);
mp_stackedWidget->removeWidget(w_from);
mp_stackedWidget->insertWidget(to, w_from);

KiwixApp::instance()->saveListOfOpenTabs();
}
1 change: 1 addition & 0 deletions src/tabbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TabBar : public QTabBar
virtual QSize tabSizeHint(int index) const;
void openFindInPageBar();
void closeTabsByZimId(const QString &id);
QStringList getTabUrls() const;

protected:
void mousePressEvent(QMouseEvent *event);
Expand Down
3 changes: 2 additions & 1 deletion src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@ QWebEngineView* WebView::createWindow(QWebEnginePage::WebWindowType type)

void WebView::onUrlChanged(const QUrl& url) {
auto zimId = getZimIdFromUrl(url);
auto app = KiwixApp::instance();
app->saveListOfOpenTabs();
if (m_currentZimId == zimId ) {
return;
}
m_currentZimId = zimId;
emit zimIdChanged(m_currentZimId);
auto app = KiwixApp::instance();
std::shared_ptr<zim::Archive> archive;
try {
archive = app->getLibrary()->getArchive(m_currentZimId);
Expand Down
44 changes: 44 additions & 0 deletions ui/settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,50 @@
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_6">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_9">
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="reopenTabLabel">
<property name="text">
<string>Re-open closed tabs</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="reopenTabToggle">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand Down

0 comments on commit 0205db4

Please sign in to comment.