Skip to content

Commit

Permalink
Introduced Restore Tab Function
Browse files Browse the repository at this point in the history
User can now restore tabs opened before close in the same order and same url with a pop-up box on startup. Fix #186
  • Loading branch information
ShaopengLin committed Mar 7, 2024
1 parent 6c03254 commit 2a7f6ca
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <QPrintDialog>
#include <thread>
#include <QMessageBox>
#include <QSettings>
#include <QStringList>
#ifdef Q_OS_WIN
#include <QtPlatformHeaders\QWindowsWindowFunctions>
#endif
Expand Down Expand Up @@ -111,6 +113,7 @@ void KiwixApp::init()
m_library.asyncUpdateFromDir(dir);
}
}
restoreTabs();
}

KiwixApp::~KiwixApp()
Expand Down Expand Up @@ -164,6 +167,72 @@ QString KiwixApp::findLibraryDirectory()
return currentDataDir;
}

void KiwixApp::restoreTabs() {
QSettings state;
QStringList tabs = state.value("tabs/").value<QStringList>();

if (!tabs.empty())
{
QMessageBox msgBox(
QMessageBox::Question, // Icon
gt("Restore Pages?"), // Title
gt("You have tabs opened from your previous session"), // Text
QMessageBox::Yes | QMessageBox::Cancel // Buttons
);
msgBox.setButtonText(QMessageBox::Yes, "Restore");
msgBox.setDefaultButton(QMessageBox::Yes);

/* Restart a new session to prevent duplicate records in openURL */
state.setValue("tabs/", QStringList{});
if (msgBox.exec() == QMessageBox::Cancel)
return;

for (const auto &zimFile : tabs)
if (!zimFile.isEmpty())
openUrl(zimFile);
}
}

void KiwixApp::changeTabUrlInSave(int index, const QString &url) {
if (index < 0)
return;
QSettings state;
QStringList tabs = state.value("tabs/").value<QStringList>();

tabs[index] = url;
state.setValue("tabs/", tabs);
}

void KiwixApp::insertTabToSave(int index, const QString &url) {
if (index < 0)
return;
QSettings state;
QStringList tabs = state.value("tabs/").value<QStringList>();

tabs.insert(index, url);
state.setValue("tabs/", tabs);
}

void KiwixApp::removeTabFromSave(int index) {
if (index < 0)
return;
QSettings state;
QStringList tabs = state.value("tabs/").value<QStringList>();

tabs.removeAt(index);
state.setValue("tabs/", tabs);
}

void KiwixApp::swapTabInSave(int from, int to) {
QSettings state;
QStringList tabs = state.value("tabs/").value<QStringList>();

QString tabUrl = tabs[from];
tabs.removeAt(from);
tabs.insert(to, tabUrl);
state.setValue("tabs/", tabs);
}

KiwixApp *KiwixApp::instance()
{
return static_cast<KiwixApp*>(QApplication::instance());
Expand Down
5 changes: 5 additions & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class KiwixApp : public QtSingleApplication
void setMonitorDir(const QString &dir);
bool isCurrentArticleBookmarked();
QString parseStyleFromFile(QString filePath);
void restoreTabs();
void changeTabUrlInSave(int index, const QString& url);
void insertTabToSave(int index, const QString& url);
void removeTabFromSave(int index);
void swapTabInSave(int from, int to);

public slots:
void openZimFile(const QString& zimfile="");
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ int main(int argc, char *argv[])
QWebEngineUrlScheme scheme("zim");
QWebEngineUrlScheme::registerScheme(scheme);
#endif
QCoreApplication::setOrganizationName("Kiwix");
QCoreApplication::setApplicationName("kiwix-desktop");

KiwixApp a(argc, argv);

QCommandLineParser parser;
Expand Down
16 changes: 16 additions & 0 deletions src/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ ZimView* TabBar::createNewTab(bool setCurrent, bool adjacentToCurrentTab)
connect(tab, &ZimView::webActionEnabledChanged,
this, &TabBar::onWebviewHistoryActionChanged);

KiwixApp::instance()->insertTabToSave(index - 1, "");
return tab;
}

Expand Down Expand Up @@ -276,6 +277,17 @@ void TabBar::closeTabsByZimId(const QString &id)
}
}

int TabBar::indexOfWebView(QObject *object) const
{
for (int index = 0; index <= mp_stackedWidget->count(); index++)
{
if (ZimView* zv = qobject_cast<ZimView*>(mp_stackedWidget->widget(index)))
if (zv->getWebView() == object)
return index;
}
return -1;
}

void TabBar::closeTab(int index)
{
// the last tab is + button, cannot be closed
Expand All @@ -289,6 +301,8 @@ void TabBar::closeTab(int index)
// library tab cannot be closed
if (qobject_cast<ContentManagerView*>(view)) {
return;
} else if (qobject_cast<ZimView*>(view)) {
KiwixApp::instance()->removeTabFromSave(index - 1);
}

mp_stackedWidget->removeWidget(view);
Expand Down Expand Up @@ -486,4 +500,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()->swapTabInSave(from - 1, to - 1);
}
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);
int indexOfWebView(QObject *object) const;

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

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

app->changeTabUrlInSave(app->getTabWidget()->indexOfWebView(this) - 1,
url.url());
}

void WebView::wheelEvent(QWheelEvent *event) {
Expand Down

0 comments on commit 2a7f6ca

Please sign in to comment.