Verified Commit 9bf37022 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

Browsermanager: Port tabs to simpler QString based storage

parent d74cbbd7
......@@ -118,67 +118,58 @@ int BrowserManager::currentTab() const
return m_current_tab;
}
QString BrowserManager::tabs() const
{
QJsonArray arr;
for (auto i=m_tabs.constBegin(); i != m_tabs.constEnd(); ++i) {
QJsonObject o;
o["url"] = i->url;
o["isMobile"] = i->isMobile;
arr.append(o);
}
return QJsonDocument(arr).toJson();
QList<QString> BrowserManager::tabs() const
{
return m_tabs;
}
void BrowserManager::loadTabs()
{
QJsonArray arr = QJsonDocument::fromJson(m_settings->value("browser/tabs").toByteArray()).array();
m_tabs.clear();
for (auto i = arr.constBegin(); i != arr.constEnd(); ++i) {
QJsonObject o = i->toObject();
TabState ts(o.value("url").toString(), o.value("isMobile").toBool());
m_tabs.push_back(ts);
}
m_tabs = m_settings->value("browser/tabs").toStringList();
m_current_tab = m_settings->value("browser/current_tab", 0).toInt();
}
void BrowserManager::saveTabs()
{
m_settings->setValue("browser/tabs", tabs());
qDebug() << "saveTabs called" << m_tabs;
m_settings->setValue("browser/tabs", QVariant(m_tabs));
}
void BrowserManager::setCurrentTab(int index)
{
if (m_tabs_readonly) return;
if (m_tabsReadonly) return;
m_current_tab = index;
m_settings->setValue("browser/current_tab", m_current_tab);
}
void BrowserManager::setTab(int index, QString url, bool isMobile)
{
if (m_tabs_readonly) return;
if (m_tabsReadonly)
return;
while (m_tabs.length() <= index) {
m_tabs.append(TabState());
m_tabs.append(QString());
}
m_tabs[index] = TabState(url, isMobile);
m_tabs[index] = url;
saveTabs();
}
void BrowserManager::setTabIsMobile(int index, bool isMobile)
{
TabState ts = m_tabs.value(index);
setTab(index, ts.url, isMobile);
tabsChanged();
}
void BrowserManager::setTabUrl(int index, QString url)
{
TabState ts = m_tabs.value(index);
setTab(index, url, ts.isMobile);
while (m_tabs.length() <= index) {
m_tabs.append(QString());
}
m_tabs[index] = url;
saveTabs();
tabsChanged();
}
void BrowserManager::setTabsWritable()
{
m_tabs_readonly = false;
m_tabsReadonly = false;
}
void BrowserManager::rmTab(int index)
......
......@@ -47,6 +47,9 @@ class BrowserManager : public QObject
Q_PROPERTY(QString searchBaseUrl READ searchBaseUrl WRITE setSearchBaseUrl NOTIFY
searchBaseUrlChanged)
Q_PROPERTY(int currentTab READ currentTab NOTIFY currentTabChanged)
Q_PROPERTY(QList<QString> tabs READ tabs NOTIFY tabsChanged)
public:
BrowserManager(QObject *parent = nullptr);
~BrowserManager() override;
......@@ -58,10 +61,9 @@ public:
QString searchBaseUrl();
Q_INVOKABLE int currentTab() const;
Q_INVOKABLE QString tabs() const;
Q_INVOKABLE QList<QString> tabs() const;
Q_INVOKABLE void setCurrentTab(int index);
Q_INVOKABLE void setTab(int index, QString url, bool isMobile);
Q_INVOKABLE void setTabIsMobile(int index, bool isMobile);
Q_INVOKABLE void setTabUrl(int index, QString url);
Q_INVOKABLE void setTabsWritable();
Q_INVOKABLE void rmTab(int index);
......@@ -76,6 +78,9 @@ signals:
void loadUrlRequested(const QString &url);
void currentTabChanged();
void tabsChanged();
public slots:
void reload();
......@@ -97,15 +102,9 @@ private:
UrlModel *m_history = nullptr;
QSettings *m_settings;
struct TabState {
TabState(QString u=QString(), bool im=false): url(u), isMobile(im) {}
QString url;
bool isMobile = false;
};
int m_current_tab = 0;
QList<TabState> m_tabs;
bool m_tabs_readonly = true;
QList<QString> m_tabs;
bool m_tabsReadonly = true;
};
} // namespace
......
......@@ -379,17 +379,16 @@ Kirigami.ApplicationWindow {
Component.onCompleted: {
if (!webappcontainer) {
// initialize tabs
var t = JSON.parse(browserManager.tabs());
var ct = browserManager.currentTab();
for (var i = 0; i < t.length; i++) {
for (var i = 0; i < browserManager.tabs.length; i++) {
if (i < regularTabs.count)
regularTabs.itemAt(i).url = t[i].url;
regularTabs.itemAt(i).url = browserManager.tabs[i];
else
regularTabs.newTab(t[i].url);
regularTabs.itemAt(i).userAgent.isMobile = t[i].isMobile;
regularTabs.newTab(browserManager.tabs[i]);
regularTabs.itemAt(i).reloadOnVisible = true;
}
if (ct >= 0 && ct < regularTabs.count) regularTabs.currentIndex = ct;
if (browserManager.currentTab >= 0 && browserManager.currentTab < regularTabs.count)
regularTabs.currentIndex = browserManager.currentTab;
browserManager.setTabsWritable();
if (initialUrl) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment