Skip to content

Commit

Permalink
Introduced Restore Tab Function
Browse files Browse the repository at this point in the history
Users can now restore tabs opened before close in the same order and same url. Option can be set in settings tab. Fix kiwix#186
  • Loading branch information
ShaopengLin committed Mar 22, 2024
1 parent d4aacc4 commit 3f986b8
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 5 deletions.
25 changes: 24 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,28 @@ void KiwixApp::init()
m_library.asyncUpdateFromDir(dir);
}
}

/* Place session file in our global library path */
QDir dir(m_libraryDirectory);
this->mp_session = new QSettings(std::move(dir).filePath("user.session"),
QSettings::defaultFormat(), this);
QStringList tabsToOpen = this->getTabWidget()->getListOfOpenTabsFromSession();

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

KiwixApp::~KiwixApp()
Expand Down
2 changes: 2 additions & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class KiwixApp : public QtSingleApplication
kiwix::Server* getLocalServer() { return &m_server; }
SettingsManager* getSettingsManager() { return &m_settingsManager; };
QString getText(const QString &key) { return m_translation.getText(key); };
QSettings* getSession() { return mp_session; }
void setMonitorDir(const QString &dir);
bool isCurrentArticleBookmarked();
QString parseStyleFromFile(QString filePath);
Expand Down Expand Up @@ -116,6 +117,7 @@ public slots:
kiwix::Server m_server;
Translation m_translation;
QFileSystemWatcher m_watcher;
QSettings* mp_session;

QAction* mpa_actions[MAX_ACTION];

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
18 changes: 17 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-on-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,7 @@ 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
29 changes: 29 additions & 0 deletions src/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ ZimView* TabBar::createNewTab(bool setCurrent, bool nextToCurrentTab)
connect(tab, &ZimView::webActionEnabledChanged,
this, &TabBar::onWebviewHistoryActionChanged);

this->saveListOfOpenTabsToSession();
return tab;
}

Expand Down Expand Up @@ -268,6 +269,30 @@ void TabBar::closeTabsByZimId(const QString &id)
}
}

QStringList TabBar::getTabUrls() {
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;
}

QStringList TabBar::getListOfOpenTabsFromSession()
{
return KiwixApp::instance()
->getSession()
->value("reopenTabList", QStringList{})
.value<QStringList>();
}

void TabBar::saveListOfOpenTabsToSession()
{
KiwixApp::instance()->getSession()->setValue("reopenTabList",
this->getTabUrls());
}

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

this->saveListOfOpenTabsToSession();
}

void TabBar::onCurrentChanged(int index)
Expand Down Expand Up @@ -466,4 +493,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);

this->saveListOfOpenTabsToSession();
}
3 changes: 3 additions & 0 deletions src/tabbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class TabBar : public QTabBar
virtual QSize tabSizeHint(int index) const;
void openFindInPageBar();
void closeTabsByZimId(const QString &id);
QStringList getTabUrls();
QStringList getListOfOpenTabsFromSession();
void saveListOfOpenTabsToSession();

protected:
void mousePressEvent(QMouseEvent *event);
Expand Down
2 changes: 2 additions & 0 deletions src/webpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio
QDesktopServices::openUrl(url);
return false;
}

KiwixApp::instance()->getTabWidget()->saveListOfOpenTabsToSession();
return true;
}
2 changes: 2 additions & 0 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ void WebView::onUrlChanged(const QUrl& url) {
m_icon = QIcon(pixmap);
emit iconChanged(m_icon);
} catch (zim::EntryNotFound& e) {}

app->getTabWidget()->saveListOfOpenTabsToSession();
}

void WebView::wheelEvent(QWheelEvent *event) {
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 3f986b8

Please sign in to comment.