From 379ff8cd9603a1beba74b7b9688d8fb13e7dc0d5 Mon Sep 17 00:00:00 2001 From: nowrep Date: Mon, 18 Apr 2011 21:35:57 +0200 Subject: [PATCH] Close all tabs but current now don't close pinned tabs, added some fixes to click2flash --- src/plugins/clicktoflash.cpp | 125 +++++++++++-------------------- src/plugins/clicktoflash.h | 8 +- src/plugins/webpluginfactory.cpp | 5 +- src/webview/tabwidget.cpp | 32 ++++---- src/webview/tabwidget.h | 1 + 5 files changed, 71 insertions(+), 100 deletions(-) diff --git a/src/plugins/clicktoflash.cpp b/src/plugins/clicktoflash.cpp index 07d8ad70c..a71797755 100644 --- a/src/plugins/clicktoflash.cpp +++ b/src/plugins/clicktoflash.cpp @@ -45,9 +45,11 @@ #include "adblockmanager.h" #include "adblocksubscription.h" -ClickToFlash::ClickToFlash(const QUrl &pluginUrl, QWidget* parent) +ClickToFlash::ClickToFlash(const QUrl &pluginUrl, const QStringList &argumentNames, const QStringList &argumentValues, QWidget* parent) : QWidget(parent) , m_url(pluginUrl) + , m_argumentNames(argumentNames) + , m_argumentValues(argumentValues) { //AdBlock AdBlockManager* manager = AdBlockManager::instance(); @@ -136,48 +138,12 @@ void ClickToFlash::toWhitelist() load(); } -void ClickToFlash::load() -{ -// QWidget* parent = parentWidget(); -// QWebView* view = 0; -// while (parent) { -// if (QWebView* aView = qobject_cast(parent)) { -// view = aView; -// break; -// } -// parent = parent->parentWidget(); -// } -// if (!view) -// return; - -// const QString selector = "%1[type=\"application/x-shockwave-flash\"]"; -// hide(); - -// QList frames; -// frames.append(view->page()->mainFrame()); -// while (!frames.isEmpty()) { -// QWebFrame* frame = frames.takeFirst(); -// QWebElement docElement = frame->documentElement(); - -// QWebElementCollection elements; -// elements.append(docElement.findAll(selector.arg("object"))); -// elements.append(docElement.findAll(selector.arg("embed"))); - -// foreach(QWebElement element, elements) { -// if (checkElement(element)) { -// QWebElement substitute = element.clone(); -// emit signalLoadClickToFlash(true); -// element.replace(substitute); -// deleteLater(); -// return; -// } -// } -// frames += frame->childFrames(); -// } - QWidget *parent = parentWidget(); - QWebView *view = 0; +void ClickToFlash::findElement() +{ + QWidget* parent = parentWidget(); + QWebView* view = 0; while (parent) { - if (QWebView *aView = qobject_cast(parent)) { + if (QWebView* aView = qobject_cast(parent)) { view = aView; break; } @@ -186,63 +152,62 @@ void ClickToFlash::load() if (!view) return; -// const QString selector = QLatin1String("%1[type=\"application/x-shockwave-flash\"]"); - const QString mime = QLatin1String("application/futuresplash"); - - hide(); QList frames; - frames.append(view->page()->mainFrame()); + m_mainFrame = view->page()->mainFrame(); + frames.append(m_mainFrame); + while (!frames.isEmpty()) { - QWebFrame *frame = frames.takeFirst(); + QWebFrame* frame = frames.takeFirst(); QWebElement docElement = frame->documentElement(); QWebElementCollection elements; - elements.append(docElement.findAll(QLatin1String("object"))); -// elements.append(docElement.findAll(selector.arg(QLatin1String("object")))); elements.append(docElement.findAll(QLatin1String("embed"))); -// elements.append(docElement.findAll(selector.arg(QLatin1String("embed")))); + elements.append(docElement.findAll(QLatin1String("object"))); QWebElement element; foreach (element, elements) { - if (!checkElement(element)) + if (!checkElement(element) && !checkUrlOnElement(element)) continue; - QWebElement substitute = element.clone(); - substitute.setAttribute(QLatin1String("type"), mime); - element.replace(substitute); + m_element = element; return; } - frames += frame->childFrames(); } } +void ClickToFlash::load() +{ + findElement(); + if (m_element.isNull()) { + + } else { + QWebElement substitute = m_element.clone(); + substitute.setAttribute(QLatin1String("type"), "application/futuresplash"); + m_element.replace(substitute); + } +} + +bool ClickToFlash::checkUrlOnElement(QWebElement el) +{ + QString checkString = QUrl(el.attribute("src")).toString(QUrl::RemoveQuery); + if (checkString.isEmpty()) + checkString = QUrl(el.attribute("data")).toString(QUrl::RemoveQuery); + if (checkString.isEmpty()) + checkString = QUrl(el.attribute("value")).toString(QUrl::RemoveQuery); + + if (m_url.toEncoded().contains(checkString.toAscii())) + return true; + return false; +} bool ClickToFlash::checkElement(QWebElement el) { - QString checkString; - QString urlString; - checkString = QUrl(el.attribute("src")).toString(QUrl::RemoveQuery); - if (checkString.isEmpty()) - checkString = QUrl(el.attribute("data")).toString(QUrl::RemoveQuery); - - if (checkString.isEmpty()) - return false; - urlString = m_url.toString(QUrl::RemoveQuery); - - if (urlString.contains(checkString)) + if (m_argumentNames == el.attributeNames()) { + foreach (QString name, m_argumentNames) { + if (m_argumentValues.indexOf(el.attribute(name)) == -1) + return false; + } return true; - - qDebug() << checkString << m_url; - -// QWebElementCollection collec = el.findAll("*"); -// int i = 0; -// while (i < collec.count()) { -// QWebElement el = collec.at(i); -// checkString = QUrl(el.attribute("src")).toString(QUrl::RemoveQuery); -// urlString = m_url.toString(QUrl::RemoveQuery); -// if (urlString.contains(checkString)) -// return true; -// i++; -// } + } return false; } diff --git a/src/plugins/clicktoflash.h b/src/plugins/clicktoflash.h index 0c38a979b..ae146c3e8 100644 --- a/src/plugins/clicktoflash.h +++ b/src/plugins/clicktoflash.h @@ -61,15 +61,21 @@ class ClickToFlash : public QWidget Q_OBJECT public: - explicit ClickToFlash(const QUrl &pluginUrl, QWidget* parent = 0); + explicit ClickToFlash(const QUrl &pluginUrl, const QStringList &argumentNames, const QStringList &argumentValues, QWidget* parent = 0); private slots: void load(); void customContextMenuRequested(const QPoint &pos); void toWhitelist(); + void findElement(); private: bool checkElement(QWebElement el); + bool checkUrlOnElement(QWebElement el); + QStringList m_argumentNames; + QStringList m_argumentValues; + QWebElement m_element; + QWebFrame* m_mainFrame; /** used to find the right QWebElement between the ones of the different plugins diff --git a/src/plugins/webpluginfactory.cpp b/src/plugins/webpluginfactory.cpp index 6e8464158..302552c0c 100644 --- a/src/plugins/webpluginfactory.cpp +++ b/src/plugins/webpluginfactory.cpp @@ -27,9 +27,6 @@ WebPluginFactory::WebPluginFactory(QObject* parent) QObject* WebPluginFactory::create(const QString &mimeType, const QUrl &url, const QStringList &argumentNames, const QStringList &argumentValues) const { - Q_UNUSED(argumentNames) - Q_UNUSED(argumentValues) - if (mimeType != "application/x-shockwave-flash") { if (mimeType != "application/futuresplash") qDebug() << mimeType; @@ -43,7 +40,7 @@ QObject* WebPluginFactory::create(const QString &mimeType, const QUrl &url, cons if (whitelist.contains(url.host()) || whitelist.contains("www."+url.host()) || whitelist.contains(url.host().remove("www."))) return 0; - ClickToFlash* ctf = new ClickToFlash(url); + ClickToFlash* ctf = new ClickToFlash(url, argumentNames, argumentValues); return ctf; } diff --git a/src/webview/tabwidget.cpp b/src/webview/tabwidget.cpp index b1702732e..4b53d6db1 100644 --- a/src/webview/tabwidget.cpp +++ b/src/webview/tabwidget.cpp @@ -310,22 +310,12 @@ void TabWidget::reloadAllTabs() void TabWidget::closeAllButCurrent(int index) { - WebView* akt = qobject_cast(widget(index)); + WebTab* akt = qobject_cast(widget(index)); - int cycleCounter = 0; // Only tab count * 1.6 attempts to - int maxCycles = count()*1.6; // close tabs -> it sometimes hangs here - while(count()!=1) { - for (int i = 0;i<=count();i++) { - if (widget(i) == akt) - continue; - closeTab(i); - cycleCounter++; - - if (cycleCounter >= maxCycles) - break; - } - if (cycleCounter >= maxCycles) - break; + foreach (WebTab* tab, allTabs(false)) { + if (akt == widget(tab->view()->tabIndex())) + continue; + closeTab(tab->view()->tabIndex()); } } @@ -352,6 +342,18 @@ void TabWidget::restoreClosedTab() m_canRestoreTab = false; } +QList TabWidget::allTabs(bool withPinned) +{ + QList allTabs; + for (int i = 0; i < count(); i++) { + WebTab* tab = qobject_cast(widget(i)); + if (!tab || (!withPinned && tab->isPinned()) ) + continue; + allTabs.append(tab); + } + return allTabs; +} + void TabWidget::savePinnedTabs() { QByteArray data; diff --git a/src/webview/tabwidget.h b/src/webview/tabwidget.h index 7fda3e44e..03439f888 100644 --- a/src/webview/tabwidget.h +++ b/src/webview/tabwidget.h @@ -51,6 +51,7 @@ public: inline TabBar* getTabBar() { return m_tabBar; } inline bool canRestoreTab() { return m_canRestoreTab; } + QList allTabs(bool withPinned = true); public slots: