1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 17:42:10 +01:00
falkonOfficial/src/lib/webview/tabwidget.cpp

940 lines
24 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
2011-03-03 18:29:20 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "tabwidget.h"
#include "tabbar.h"
#include "tabbedwebview.h"
2011-03-02 16:57:41 +01:00
#include "webpage.h"
#include "qupzilla.h"
#include "iconprovider.h"
2011-03-02 16:57:41 +01:00
#include "mainapplication.h"
#include "webtab.h"
#include "clickablelabel.h"
#include "closedtabsmanager.h"
#include "progressbar.h"
#include "navigationbar.h"
#include "locationbar.h"
#include "websearchbar.h"
#include "settings.h"
#include "qzsettings.h"
#include <QMovie>
#include <QMenu>
#include <QMimeData>
#include <QStackedWidget>
2012-08-31 15:19:07 +02:00
#include <QMouseEvent>
#include <QWebHistory>
#include <QClipboard>
2012-03-05 23:34:00 +01:00
#include <QFile>
AddTabButton::AddTabButton(TabWidget* tabWidget, TabBar* tabBar)
: ToolButton(tabWidget)
, m_tabBar(tabBar)
, m_tabWidget(tabWidget)
{
setObjectName("tabwidget-button-addtab");
setAutoRaise(true);
setFocusPolicy(Qt::NoFocus);
setAcceptDrops(true);
setToolTip(TabWidget::tr("New Tab"));
}
void AddTabButton::wheelEvent(QWheelEvent* event)
{
m_tabBar->wheelEvent(event);
}
void AddTabButton::mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() == Qt::MiddleButton && rect().contains(event->pos())) {
QString selectionClipboard = QApplication::clipboard()->text(QClipboard::Selection);
QUrl guessedUrl = WebView::guessUrlFromString(selectionClipboard);
if (!guessedUrl.isEmpty()) {
m_tabWidget->addView(guessedUrl, Qz::NT_SelectedNewEmptyTab);
}
}
ToolButton::mouseReleaseEvent(event);
}
void AddTabButton::dragEnterEvent(QDragEnterEvent* event)
{
const QMimeData* mime = event->mimeData();
if (mime->hasUrls()) {
event->acceptProposedAction();
return;
}
ToolButton::dragEnterEvent(event);
}
void AddTabButton::dropEvent(QDropEvent* event)
{
const QMimeData* mime = event->mimeData();
if (!mime->hasUrls()) {
ToolButton::dropEvent(event);
return;
}
foreach (const QUrl &url, mime->urls()) {
m_tabWidget->addView(url, Qz::NT_SelectedNewEmptyTab);
}
}
2011-03-02 16:57:41 +01:00
TabWidget::TabWidget(QupZilla* mainClass, QWidget* parent)
: QTabWidget(parent)
, p_QupZilla(mainClass)
, m_lastTabIndex(-1)
, m_lastBackgroundTabIndex(-1)
, m_isClosingToLastTabIndex(false)
, m_isRestoringState(false)
, m_closedTabsManager(new ClosedTabsManager)
, m_locationBars(new QStackedWidget)
2011-03-02 16:57:41 +01:00
{
setObjectName("tabwidget");
m_tabBar = new TabBar(p_QupZilla, this);
2011-03-02 16:57:41 +01:00
setTabBar(m_tabBar);
connect(this, SIGNAL(currentChanged(int)), p_QupZilla, SLOT(refreshHistory()));
connect(m_tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
2011-03-02 16:57:41 +01:00
connect(m_tabBar, SIGNAL(reloadTab(int)), this, SLOT(reloadTab(int)));
connect(m_tabBar, SIGNAL(stopTab(int)), this, SLOT(stopTab(int)));
connect(m_tabBar, SIGNAL(closeTab(int)), this, SLOT(closeTab(int)));
connect(m_tabBar, SIGNAL(closeAllButCurrent(int)), this, SLOT(closeAllButCurrent(int)));
2011-04-08 17:27:08 +02:00
connect(m_tabBar, SIGNAL(duplicateTab(int)), this, SLOT(duplicateTab(int)));
connect(m_tabBar, SIGNAL(tabMoved(int,int)), this, SLOT(tabMoved(int,int)));
connect(m_tabBar, SIGNAL(moveAddTabButton(int)), this, SLOT(moveAddTabButton(int)));
connect(m_tabBar, SIGNAL(showButtons()), this, SLOT(showButtons()));
connect(m_tabBar, SIGNAL(hideButtons()), this, SLOT(hideButtons()));
2011-03-02 16:57:41 +01:00
m_buttonListTabs = new ToolButton(this);
m_buttonListTabs->setObjectName("tabwidget-button-opentabs");
m_menuTabs = new QMenu(this);
2011-03-02 16:57:41 +01:00
m_buttonListTabs->setMenu(m_menuTabs);
m_buttonListTabs->setPopupMode(QToolButton::InstantPopup);
m_buttonListTabs->setToolTip(tr("List of tabs"));
m_buttonListTabs->setAutoRaise(true);
m_buttonListTabs->setFocusPolicy(Qt::NoFocus);
2011-03-02 16:57:41 +01:00
m_buttonAddTab = new AddTabButton(this, m_tabBar);
connect(m_buttonAddTab, SIGNAL(clicked()), p_QupZilla, SLOT(addTab()));
connect(m_menuTabs, SIGNAL(aboutToShow()), this, SLOT(aboutToShowClosedTabsMenu()));
loadSettings();
2011-03-02 16:57:41 +01:00
}
void TabWidget::loadSettings()
{
Settings settings;
2011-03-02 16:57:41 +01:00
settings.beginGroup("Browser-Tabs-Settings");
m_hideTabBarWithOneTab = settings.value("hideTabsWithOneTab", false).toBool();
m_dontQuitWithOneTab = settings.value("dontQuitWithOneTab", false).toBool();
m_closedInsteadOpened = settings.value("closedInsteadOpenedTabs", false).toBool();
m_newTabAfterActive = settings.value("newTabAfterActive", true).toBool();
m_newEmptyTabAfterActive = settings.value("newEmptyTabAfterActive", false).toBool();
2011-03-02 16:57:41 +01:00
settings.endGroup();
2011-03-02 16:57:41 +01:00
settings.beginGroup("Web-URL-Settings");
m_urlOnNewTab = settings.value("newTabUrl", "qupzilla:speeddial").toUrl();
2011-03-02 16:57:41 +01:00
settings.endGroup();
m_tabBar->loadSettings();
}
void TabWidget::resizeEvent(QResizeEvent* e)
{
QPoint posit;
posit.setY(0);
//RTL Support
if (QApplication::layoutDirection() == Qt::RightToLeft) {
posit.setX(0);
}
else {
posit.setX(width() - m_buttonListTabs->width());
}
m_buttonListTabs->move(posit);
QTabWidget::resizeEvent(e);
}
WebTab* TabWidget::weTab()
{
return weTab(currentIndex());
}
WebTab* TabWidget::weTab(int index)
{
return qobject_cast<WebTab*>(widget(index));
}
void TabWidget::showButtons()
{
m_buttonListTabs->show();
m_buttonAddTab->show();
}
void TabWidget::hideButtons()
{
m_buttonListTabs->hide();
m_buttonAddTab->hide();
}
void TabWidget::moveAddTabButton(int posX)
{
int posY = (m_tabBar->height() - m_buttonAddTab->height()) / 2;
//RTL Support
if (QApplication::layoutDirection() == Qt::RightToLeft) {
posX = qMax(posX - m_buttonAddTab->width(), m_buttonListTabs->width());
}
else {
posX = qMin(posX, m_tabBar->width() - m_buttonAddTab->width() - m_buttonListTabs->width());
}
m_buttonAddTab->move(posX, posY);
}
2011-03-02 16:57:41 +01:00
void TabWidget::aboutToShowTabsMenu()
{
m_menuTabs->clear();
WebTab* actTab = weTab();
if (!actTab) {
2011-03-02 16:57:41 +01:00
return;
}
for (int i = 0; i < count(); i++) {
WebTab* tab = weTab(i);
if (!tab) {
2011-03-02 16:57:41 +01:00
continue;
}
2011-03-02 16:57:41 +01:00
QAction* action = new QAction(this);
if (tab == actTab) {
action->setIcon(QIcon(":/icons/menu/dot.png"));
}
else {
action->setIcon(tab->icon());
}
if (tab->title().isEmpty()) {
if (tab->isLoading()) {
2011-03-02 16:57:41 +01:00
action->setText(tr("Loading..."));
action->setIcon(QIcon(":/icons/other/progress.gif"));
}
else {
2011-03-02 16:57:41 +01:00
action->setText(tr("No Named Page"));
}
2011-03-02 16:57:41 +01:00
}
else {
QString title = tab->title();
title.replace(QLatin1Char('&'), QLatin1String("&&"));
if (title.length() > 40) {
2011-03-02 16:57:41 +01:00
title.truncate(40);
title += QLatin1String("..");
2011-03-02 16:57:41 +01:00
}
action->setText(title);
}
action->setData(i);
connect(action, SIGNAL(triggered()), this, SLOT(actionChangeIndex()));
m_menuTabs->addAction(action);
}
m_menuTabs->addSeparator();
m_menuTabs->addAction(tr("Currently you have %1 opened tabs").arg(count()))->setEnabled(false);
2011-03-02 16:57:41 +01:00
}
void TabWidget::actionChangeIndex()
{
2011-03-17 17:03:04 +01:00
if (QAction* action = qobject_cast<QAction*>(sender())) {
2011-03-02 16:57:41 +01:00
setCurrentIndex(action->data().toInt());
}
}
int TabWidget::addView(const QUrl &url, const Qz::NewTabPositionFlags &openFlags, bool selectLine)
{
return addView(QNetworkRequest(url), openFlags, selectLine);
}
int TabWidget::addView(const QNetworkRequest &req, const Qz::NewTabPositionFlags &openFlags, bool selectLine)
2011-03-02 16:57:41 +01:00
{
return addView(req, tr("New tab"), openFlags, selectLine);
}
int TabWidget::addView(const QUrl &url, const QString &title, const Qz::NewTabPositionFlags &openFlags, bool selectLine, int position)
{
return addView(QNetworkRequest(url), title, openFlags, selectLine, position);
}
int TabWidget::addView(QNetworkRequest req, const QString &title, const Qz::NewTabPositionFlags &openFlags, bool selectLine, int position)
{
QUrl url = req.url();
m_lastTabIndex = currentIndex();
if (url.isEmpty() && !(openFlags & Qz::NT_CleanTab)) {
2011-03-02 16:57:41 +01:00
url = m_urlOnNewTab;
}
2011-03-02 16:57:41 +01:00
bool openAfterActive = m_newTabAfterActive && !(openFlags & Qz::NT_TabAtTheEnd);
if (openFlags == Qz::NT_SelectedNewEmptyTab && m_newEmptyTabAfterActive) {
openAfterActive = true;
}
if (openAfterActive && position == -1) {
// If we are opening newBgTab from pinned tab, make sure it won't be
// opened between other pinned tabs
if (openFlags & Qz::NT_NotSelectedTab && m_lastBackgroundTabIndex != -1) {
position = m_lastBackgroundTabIndex + 1;
}
else {
position = qMax(currentIndex() + 1, m_tabBar->pinnedTabsCount());
}
}
LocationBar* locBar = new LocationBar(p_QupZilla);
m_locationBars->addWidget(locBar);
int index;
if (position == -1) {
index = addTab(new WebTab(p_QupZilla, locBar), QString());
}
else {
index = insertTab(position, new WebTab(p_QupZilla, locBar), QString());
}
TabbedWebView* webView = weTab(index)->view();
locBar->setWebView(webView);
2011-03-02 16:57:41 +01:00
setTabText(index, title);
setTabIcon(index, qIconProvider->emptyWebIcon());
2011-03-02 16:57:41 +01:00
if (openFlags & Qz::NT_SelectedTab) {
2011-03-02 16:57:41 +01:00
setCurrentIndex(index);
}
else {
m_lastBackgroundTabIndex = index;
}
connect(webView, SIGNAL(wantsCloseTab(int)), this, SLOT(closeTab(int)));
connect(webView, SIGNAL(changed()), mApp, SLOT(setStateChanged()));
connect(webView, SIGNAL(ipChanged(QString)), p_QupZilla->ipLabel(), SLOT(setText(QString)));
2011-03-02 16:57:41 +01:00
if (url.isValid()) {
req.setUrl(url);
webView->load(req);
}
if (selectLine && p_QupZilla->locationBar()->text().isEmpty()) {
2011-03-02 16:57:41 +01:00
p_QupZilla->locationBar()->setFocus();
}
2012-04-03 20:23:15 +02:00
if (openFlags & Qz::NT_SelectedTab || openFlags & Qz::NT_NotSelectedTab) {
m_isClosingToLastTabIndex = true;
}
2011-03-02 16:57:41 +01:00
if (openFlags & Qz::NT_NotSelectedTab) {
WebTab* currentWebTab = weTab();
// Workarounding invalid QWebPage::viewportSize() until QWebView is shown
// Fixes invalid scrolling to anchor(#) links
if (currentWebTab && currentWebTab->view()) {
TabbedWebView* currentView = currentWebTab->view();
webView->resize(currentView->size());
webView->page()->setViewportSize(currentView->page()->viewportSize());
}
}
2011-03-02 16:57:41 +01:00
return index;
}
void TabWidget::closeTab(int index, bool force)
2011-03-02 16:57:41 +01:00
{
if (index == -1) {
2011-03-02 16:57:41 +01:00
index = currentIndex();
}
2011-03-02 16:57:41 +01:00
WebTab* webTab = weTab(index);
if (!webTab || !validIndex(index)) {
return;
}
TabbedWebView* webView = webTab->view();
2012-04-03 20:23:15 +02:00
WebPage* webPage = webView->page();
if (!force && webView->url().toString() == QLatin1String("qupzilla:restore") && mApp->restoreManager()) {
// Don't close restore page!
return;
}
if (!force && count() == 1) {
if (m_dontQuitWithOneTab && mApp->windowCount() == 1) {
webView->load(m_urlOnNewTab);
return;
}
else {
p_QupZilla->close();
return;
}
}
if (webTab->isPinned()) {
emit pinnedTabClosed();
}
m_locationBars->removeWidget(webView->webTab()->locationBar());
disconnect(webView, SIGNAL(wantsCloseTab(int)), this, SLOT(closeTab(int)));
disconnect(webView, SIGNAL(changed()), mApp, SLOT(setStateChanged()));
disconnect(webView, SIGNAL(ipChanged(QString)), p_QupZilla->ipLabel(), SLOT(setText(QString)));
//Save last tab url and history
m_closedTabsManager->saveView(webTab, index);
if (m_isClosingToLastTabIndex && m_lastTabIndex < count() && index == currentIndex()) {
setCurrentIndex(m_lastTabIndex);
}
m_lastBackgroundTabIndex = -1;
webPage->disconnectObjects();
webView->disconnectObjects();
webTab->disconnectObjects();
webTab->deleteLater();
}
void TabWidget::currentTabChanged(int index)
{
if (!validIndex(index) || m_isRestoringState) {
return;
}
2012-04-03 20:23:15 +02:00
m_isClosingToLastTabIndex = m_lastBackgroundTabIndex == index;
m_lastBackgroundTabIndex = -1;
m_lastTabIndex = index;
WebTab* webTab = weTab(index);
LocationBar* locBar = webTab->locationBar();
if (locBar && m_locationBars->indexOf(locBar) != -1) {
m_locationBars->setCurrentWidget(locBar);
}
webTab->setCurrentTab();
p_QupZilla->currentTabChanged();
showNavigationBar(p_QupZilla->navigationContainer());
2011-03-02 16:57:41 +01:00
}
void TabWidget::tabMoved(int before, int after)
{
Q_UNUSED(before)
Q_UNUSED(after)
m_isClosingToLastTabIndex = false;
m_lastBackgroundTabIndex = -1;
m_lastTabIndex = before;
}
void TabWidget::tabInserted(int index)
{
Q_UNUSED(index)
tabBar()->setVisible(!(count() == 1 && m_hideTabBarWithOneTab));
}
void TabWidget::tabRemoved(int index)
{
Q_UNUSED(index)
tabBar()->setVisible(!(count() == 1 && m_hideTabBarWithOneTab));
}
void TabWidget::startTabAnimation(int index)
{
if (!validIndex(index)) {
return;
}
QLabel* label = qobject_cast<QLabel*>(m_tabBar->tabButton(index, m_tabBar->iconButtonPosition()));
if (!label) {
label = new QLabel();
label->setObjectName("tab-icon");
m_tabBar->setTabButton(index, m_tabBar->iconButtonPosition(), label);
}
if (label->movie()) {
label->movie()->start();
return;
}
QMovie* movie = new QMovie(":icons/other/progress.gif", QByteArray(), label);
movie->start();
label->setMovie(movie);
}
void TabWidget::stopTabAnimation(int index)
{
if (!validIndex(index)) {
return;
}
QLabel* label = qobject_cast<QLabel*>(m_tabBar->tabButton(index, m_tabBar->iconButtonPosition()));
if (label && label->movie()) {
label->movie()->stop();
}
}
void TabWidget::setCurrentIndex(int index)
{
m_lastTabIndex = currentIndex();
QTabWidget::setCurrentIndex(index);
}
void TabWidget::setTabIcon(int index, const QIcon &icon)
{
if (!validIndex(index)) {
return;
}
QLabel* label = qobject_cast<QLabel*>(m_tabBar->tabButton(index, m_tabBar->iconButtonPosition()));
if (!label) {
label = new QLabel();
label->setObjectName("tab-icon");
label->resize(16, 16);
m_tabBar->setTabButton(index, m_tabBar->iconButtonPosition(), label);
}
label->setPixmap(icon.pixmap(16, 16));
}
void TabWidget::setTabText(int index, const QString &text)
2011-03-02 16:57:41 +01:00
{
if (!validIndex(index)) {
return;
}
QString newtext = text;
newtext.replace(QLatin1Char('&'), QLatin1String("&&")); // Avoid Alt+letter shortcuts
if (WebTab* webTab = weTab(index)) {
if (webTab->isPinned()) {
newtext.clear();
}
}
2011-03-02 16:57:41 +01:00
setTabToolTip(index, text);
QTabWidget::setTabText(index, newtext);
}
void TabWidget::nextTab()
{
QKeyEvent fakeEvent(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::ControlModifier);
keyPressEvent(&fakeEvent);
}
void TabWidget::previousTab()
{
QKeyEvent fakeEvent(QKeyEvent::KeyPress, Qt::Key_Backtab, QFlags<Qt::KeyboardModifier>(Qt::ControlModifier + Qt::ShiftModifier));
keyPressEvent(&fakeEvent);
}
int TabWidget::normalTabsCount() const
{
return m_tabBar->normalTabsCount();
}
int TabWidget::pinnedTabsCount() const
{
return m_tabBar->pinnedTabsCount();
}
void TabWidget::reloadTab(int index)
{
if (!validIndex(index)) {
return;
}
weTab(index)->reload();
}
2011-03-02 16:57:41 +01:00
void TabWidget::showTabBar()
{
if (count() == 1 && m_hideTabBarWithOneTab) {
tabBar()->hide();
}
else {
tabBar()->show();
}
2011-03-02 16:57:41 +01:00
}
int TabWidget::lastTabIndex() const
{
return m_lastTabIndex;
}
void TabWidget::showNavigationBar(QWidget* bar)
{
WebTab* tab = weTab();
if (tab) {
tab->showNavigationBar(bar);
}
}
TabBar* TabWidget::getTabBar() const
{
return m_tabBar;
}
ClosedTabsManager* TabWidget::closedTabsManager() const
{
return m_closedTabsManager;
}
2011-03-02 16:57:41 +01:00
void TabWidget::reloadAllTabs()
{
for (int i = 0; i < count(); i++) {
2011-03-02 16:57:41 +01:00
reloadTab(i);
}
}
void TabWidget::stopTab(int index)
{
if (!validIndex(index)) {
return;
}
weTab(index)->stop();
}
2011-03-02 16:57:41 +01:00
void TabWidget::closeAllButCurrent(int index)
{
if (!validIndex(index)) {
return;
}
WebTab* akt = weTab(index);
foreach (WebTab* tab, allTabs(false)) {
int tabIndex = tab->tabIndex();
if (akt == widget(tabIndex)) {
continue;
}
closeTab(tabIndex);
2011-03-02 16:57:41 +01:00
}
}
int TabWidget::duplicateTab(int index)
2011-04-08 17:27:08 +02:00
{
if (!validIndex(index)) {
return -1;
}
WebTab* webTab = weTab(index);
const QUrl &url = webTab->url();
const QString &title = webTab->title();
const QByteArray &history = webTab->historyData();
2011-04-08 17:27:08 +02:00
QNetworkRequest req(url);
req.setRawHeader("Referer", url.toEncoded());
req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1"));
int id = addView(req, title, Qz::NT_CleanNotSelectedTab);
weTab(id)->setHistoryData(history);
return id;
2011-04-08 17:27:08 +02:00
}
void TabWidget::restoreClosedTab(QObject* obj)
2011-03-02 16:57:41 +01:00
{
if (!obj) {
obj = sender();
}
if (!m_closedTabsManager->isClosedTabAvailable()) {
2011-03-02 16:57:41 +01:00
return;
}
ClosedTabsManager::Tab tab;
QAction* action = qobject_cast<QAction*>(obj);
if (action && action->data().toInt() != 0) {
tab = m_closedTabsManager->getTabAt(action->data().toInt());
}
else {
tab = m_closedTabsManager->getFirstClosedTab();
}
int index = addView(QUrl(), tab.title, Qz::NT_CleanSelectedTab, false, tab.position);
WebTab* webTab = weTab(index);
webTab->p_restoreTab(tab.url, tab.history);
}
void TabWidget::restoreAllClosedTabs()
{
if (!m_closedTabsManager->isClosedTabAvailable()) {
return;
}
const QVector<ClosedTabsManager::Tab> &closedTabs = m_closedTabsManager->allClosedTabs();
foreach (const ClosedTabsManager::Tab &tab, closedTabs) {
int index = addView(QUrl(), tab.title, Qz::NT_CleanSelectedTab);
WebTab* webTab = weTab(index);
webTab->p_restoreTab(tab.url, tab.history);
}
m_closedTabsManager->clearList();
}
void TabWidget::clearClosedTabsList()
{
m_closedTabsManager->clearList();
}
bool TabWidget::canRestoreTab() const
{
return m_closedTabsManager->isClosedTabAvailable();
2011-03-02 16:57:41 +01:00
}
QStackedWidget* TabWidget::locationBars() const
{
return m_locationBars;
}
ToolButton* TabWidget::buttonListTabs() const
{
return m_buttonListTabs;
}
AddTabButton* TabWidget::buttonAddTab() const
{
return m_buttonAddTab;
}
void TabWidget::aboutToShowClosedTabsMenu()
{
if (!m_closedInsteadOpened) {
aboutToShowTabsMenu();
}
else {
m_menuTabs->clear();
int i = 0;
foreach (const ClosedTabsManager::Tab &tab, closedTabsManager()->allClosedTabs()) {
QString title = tab.title;
if (title.length() > 40) {
title.truncate(40);
title += "..";
}
m_menuTabs->addAction(_iconForUrl(tab.url), title, this, SLOT(restoreClosedTab()))->setData(i);
i++;
}
m_menuTabs->addSeparator();
if (i == 0) {
m_menuTabs->addAction(tr("Empty"))->setEnabled(false);
}
else {
m_menuTabs->addAction(tr("Restore All Closed Tabs"), this, SLOT(restoreAllClosedTabs()));
m_menuTabs->addAction(tr("Clear list"), this, SLOT(clearClosedTabsList()));
}
}
}
QList<WebTab*> TabWidget::allTabs(bool withPinned)
{
QList<WebTab*> allTabs;
for (int i = 0; i < count(); i++) {
WebTab* tab = weTab(i);
if (!tab || (!withPinned && tab->isPinned())) {
continue;
}
allTabs.append(tab);
}
return allTabs;
}
2011-03-25 19:16:21 +01:00
void TabWidget::savePinnedTabs()
{
if (mApp->isPrivateSession()) {
return;
}
2011-03-25 19:16:21 +01:00
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << Qz::sessionVersion;
2011-03-25 19:16:21 +01:00
QStringList tabs;
QList<QByteArray> tabsHistory;
for (int i = 0; i < count(); ++i) {
WebTab* tab = weTab(i);
if (!tab || !tab->isPinned()) {
continue;
2011-03-25 19:16:21 +01:00
}
tabs.append(tab->url().toEncoded());
tabsHistory.append(tab->historyData());
2011-03-25 19:16:21 +01:00
}
2011-03-25 19:16:21 +01:00
stream << tabs;
stream << tabsHistory;
QFile file(mApp->currentProfilePath() + "pinnedtabs.dat");
2011-03-25 19:16:21 +01:00
file.open(QIODevice::WriteOnly);
file.write(data);
file.close();
}
void TabWidget::restorePinnedTabs()
{
if (mApp->isPrivateSession()) {
return;
}
QFile file(mApp->currentProfilePath() + "pinnedtabs.dat");
2011-03-25 19:16:21 +01:00
file.open(QIODevice::ReadOnly);
QByteArray sd = file.readAll();
file.close();
QDataStream stream(&sd, QIODevice::ReadOnly);
if (stream.atEnd()) {
2011-03-25 19:16:21 +01:00
return;
}
2011-03-25 19:16:21 +01:00
int version;
stream >> version;
if (version != Qz::sessionVersion) {
return;
}
2011-03-25 19:16:21 +01:00
QStringList pinnedTabs;
stream >> pinnedTabs;
QList<QByteArray> tabHistory;
stream >> tabHistory;
m_isRestoringState = true;
2011-03-25 19:16:21 +01:00
for (int i = 0; i < pinnedTabs.count(); ++i) {
QUrl url = QUrl::fromEncoded(pinnedTabs.at(i).toUtf8());
QByteArray historyState = tabHistory.value(i);
int addedIndex;
2011-03-25 19:16:21 +01:00
if (!historyState.isEmpty()) {
addedIndex = addView(QUrl(), Qz::NT_CleanSelectedTab);
weTab(addedIndex)->p_restoreTab(url, historyState);
}
else {
2011-03-25 19:16:21 +01:00
addedIndex = addView(url);
}
WebTab* webTab = weTab(addedIndex);
if (webTab) {
2011-03-25 19:16:21 +01:00
webTab->setPinned(true);
emit pinnedTabAdded();
}
2011-03-25 19:16:21 +01:00
m_tabBar->updatePinnedTabCloseButton(addedIndex);
2011-03-25 19:16:21 +01:00
}
m_isRestoringState = false;
2011-03-25 19:16:21 +01:00
}
2011-03-02 16:57:41 +01:00
QByteArray TabWidget::saveState()
{
QVector<WebTab::SavedTab> tabList;
for (int i = 0; i < count(); ++i) {
WebTab* webTab = weTab(i);
if (!webTab || webTab->isPinned()) {
continue;
}
WebTab::SavedTab tab(webTab);
tabList.append(tab);
}
2011-03-02 16:57:41 +01:00
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << tabList.count();
2011-03-25 19:16:21 +01:00
foreach (const WebTab::SavedTab &tab, tabList) {
stream << tab;
2011-03-02 16:57:41 +01:00
}
2011-03-02 16:57:41 +01:00
stream << currentIndex();
return data;
}
bool TabWidget::restoreState(const QVector<WebTab::SavedTab> &tabs, int currentTab)
2011-03-02 16:57:41 +01:00
{
m_isRestoringState = true;
setUpdatesEnabled(false);
Qz::BrowserWindow type = p_QupZilla->windowType();
if (type == Qz::BW_FirstAppWindow || type == Qz::BW_MacFirstWindow) {
restorePinnedTabs();
}
for (int i = 0; i < tabs.size(); ++i) {
WebTab::SavedTab tab = tabs.at(i);
int index = addView(QUrl(), Qz::NT_CleanSelectedTab);
weTab(index)->restoreTab(tab);
2011-03-02 16:57:41 +01:00
}
m_isRestoringState = false;
setUpdatesEnabled(true);
2011-03-25 19:16:21 +01:00
setCurrentIndex(currentTab);
currentTabChanged(currentTab);
2011-03-02 16:57:41 +01:00
return true;
}
void TabWidget::closeRecoveryTab()
{
foreach (WebTab* tab, allTabs(false)) {
if (tab->url().toString() == QLatin1String("qupzilla:restore")) {
closeTab(tab->tabIndex(), true);
}
}
}
void TabWidget::disconnectObjects()
{
disconnect(this);
disconnect(mApp);
2012-03-05 13:16:34 +01:00
disconnect(p_QupZilla);
disconnect(p_QupZilla->ipLabel());
}
2011-03-02 16:57:41 +01:00
TabWidget::~TabWidget()
{
delete m_closedTabsManager;
2011-03-02 16:57:41 +01:00
}