|
|
|
|
@ -28,22 +28,18 @@ |
|
|
|
|
#include "qztools.h" |
|
|
|
|
|
|
|
|
|
#include <QMenu> |
|
|
|
|
#include <QTimer> |
|
|
|
|
|
|
|
|
|
AdBlockIcon::AdBlockIcon(QObject *parent) |
|
|
|
|
: AbstractButtonInterface(parent) |
|
|
|
|
, m_menuAction(0) |
|
|
|
|
, m_flashTimer(0) |
|
|
|
|
, m_timerTicks(0) |
|
|
|
|
, m_enabled(false) |
|
|
|
|
{ |
|
|
|
|
setTitle(tr("AdBlock")); |
|
|
|
|
setToolTip(tr("AdBlock lets you block unwanted content on web pages")); |
|
|
|
|
setIcon(QIcon(QSL(":icons/other/adblock.png"))); |
|
|
|
|
|
|
|
|
|
connect(this, &AbstractButtonInterface::clicked, this, &AdBlockIcon::clicked); |
|
|
|
|
updateState(); |
|
|
|
|
|
|
|
|
|
setEnabled(AdBlockManager::instance()->isEnabled()); |
|
|
|
|
connect(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool))); |
|
|
|
|
connect(this, &AbstractButtonInterface::clicked, this, &AdBlockIcon::clicked); |
|
|
|
|
connect(this, &AbstractButtonInterface::webPageChanged, this, &AdBlockIcon::webPageChanged); |
|
|
|
|
connect(AdBlockManager::instance(), &AdBlockManager::enabledChanged, this, &AdBlockIcon::updateState); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
AdBlockIcon::~AdBlockIcon() |
|
|
|
|
@ -79,46 +75,68 @@ void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) |
|
|
|
|
m_blockedPopups.append(pair); |
|
|
|
|
|
|
|
|
|
mApp->desktopNotifications()->showNotification(QPixmap(":html/adblock_big.png"), tr("Blocked popup window"), tr("AdBlock blocked unwanted popup window.")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!m_flashTimer) { |
|
|
|
|
m_flashTimer = new QTimer(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (m_flashTimer->isActive()) { |
|
|
|
|
stopAnimation(); |
|
|
|
|
void AdBlockIcon::toggleCustomFilter() |
|
|
|
|
{ |
|
|
|
|
QAction* action = qobject_cast<QAction*>(sender()); |
|
|
|
|
if (!action) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
m_flashTimer->setInterval(500); |
|
|
|
|
m_flashTimer->start(); |
|
|
|
|
const QString filter = action->data().toString(); |
|
|
|
|
AdBlockManager* manager = AdBlockManager::instance(); |
|
|
|
|
AdBlockCustomList* customList = manager->customList(); |
|
|
|
|
|
|
|
|
|
connect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon())); |
|
|
|
|
if (customList->containsFilter(filter)) { |
|
|
|
|
customList->removeFilter(filter); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
AdBlockRule* rule = new AdBlockRule(filter, customList); |
|
|
|
|
customList->addRule(rule); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QAction* AdBlockIcon::menuAction() |
|
|
|
|
void AdBlockIcon::updateState() |
|
|
|
|
{ |
|
|
|
|
if (!m_menuAction) { |
|
|
|
|
m_menuAction = new QAction(tr("AdBlock"), this); |
|
|
|
|
m_menuAction->setMenu(new QMenu); |
|
|
|
|
connect(m_menuAction->menu(), SIGNAL(aboutToShow()), this, SLOT(createMenu())); |
|
|
|
|
WebPage *page = webPage(); |
|
|
|
|
if (!page) { |
|
|
|
|
setActive(false); |
|
|
|
|
setToolTip(name()); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
m_menuAction->setIcon(QIcon(m_enabled ? ":icons/other/adblock.png" : ":icons/other/adblock-disabled.png")); |
|
|
|
|
|
|
|
|
|
return m_menuAction; |
|
|
|
|
if (!AdBlockManager::instance()->isEnabled()) { |
|
|
|
|
setActive(false); |
|
|
|
|
setToolTip(tr("AdBlock is disabled")); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (!AdBlockManager::instance()->canRunOnScheme(page->url().scheme())) { |
|
|
|
|
setActive(false); |
|
|
|
|
setToolTip(tr("AdBlock is disabled on this site ")); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
setActive(true); |
|
|
|
|
setToolTip(tr("AdBlock is active")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AdBlockIcon::createMenu(QMenu* menu) |
|
|
|
|
void AdBlockIcon::webPageChanged(WebPage *page) |
|
|
|
|
{ |
|
|
|
|
if (!menu) { |
|
|
|
|
menu = qobject_cast<QMenu*>(sender()); |
|
|
|
|
if (!menu) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
updateState(); |
|
|
|
|
|
|
|
|
|
if (m_page) { |
|
|
|
|
disconnect(m_page, &QWebEnginePage::urlChanged, this, &AdBlockIcon::updateState); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
menu->clear(); |
|
|
|
|
m_page = page; |
|
|
|
|
|
|
|
|
|
WebPage* page = webPage(); |
|
|
|
|
if (m_page) { |
|
|
|
|
connect(m_page, &QWebEnginePage::urlChanged, this, &AdBlockIcon::updateState); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AdBlockIcon::clicked(ClickController *controller) |
|
|
|
|
{ |
|
|
|
|
WebPage *page = webPage(); |
|
|
|
|
if (!page) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
@ -128,103 +146,42 @@ void AdBlockIcon::createMenu(QMenu* menu) |
|
|
|
|
|
|
|
|
|
const QUrl pageUrl = page->url(); |
|
|
|
|
|
|
|
|
|
menu->addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog())); |
|
|
|
|
menu->addSeparator(); |
|
|
|
|
QMenu menu; |
|
|
|
|
menu.addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog())); |
|
|
|
|
menu.addSeparator(); |
|
|
|
|
|
|
|
|
|
if (!pageUrl.host().isEmpty() && m_enabled && manager->canRunOnScheme(pageUrl.scheme())) { |
|
|
|
|
if (!pageUrl.host().isEmpty() && manager->isEnabled() && manager->canRunOnScheme(pageUrl.scheme())) { |
|
|
|
|
const QString host = page->url().host().contains(QLatin1String("www.")) ? pageUrl.host().mid(4) : pageUrl.host(); |
|
|
|
|
const QString hostFilter = QString("@@||%1^$document").arg(host); |
|
|
|
|
const QString pageFilter = QString("@@|%1|$document").arg(pageUrl.toString()); |
|
|
|
|
|
|
|
|
|
QAction* act = menu->addAction(tr("Disable on %1").arg(host)); |
|
|
|
|
QAction* act = menu.addAction(tr("Disable on %1").arg(host)); |
|
|
|
|
act->setCheckable(true); |
|
|
|
|
act->setChecked(customList->containsFilter(hostFilter)); |
|
|
|
|
act->setData(hostFilter); |
|
|
|
|
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter())); |
|
|
|
|
|
|
|
|
|
act = menu->addAction(tr("Disable only on this page")); |
|
|
|
|
act = menu.addAction(tr("Disable only on this page")); |
|
|
|
|
act->setCheckable(true); |
|
|
|
|
act->setChecked(customList->containsFilter(pageFilter)); |
|
|
|
|
act->setData(pageFilter); |
|
|
|
|
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter())); |
|
|
|
|
|
|
|
|
|
menu->addSeparator(); |
|
|
|
|
menu.addSeparator(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!m_blockedPopups.isEmpty()) { |
|
|
|
|
menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false); |
|
|
|
|
menu.addAction(tr("Blocked Popup Windows"))->setEnabled(false); |
|
|
|
|
for (int i = 0; i < m_blockedPopups.count(); i++) { |
|
|
|
|
const QPair<AdBlockRule*, QUrl> &pair = m_blockedPopups.at(i); |
|
|
|
|
|
|
|
|
|
QString address = pair.second.toString().right(55); |
|
|
|
|
QString actionText = tr("%1 with (%2)").arg(address, pair.first->filter()).replace(QLatin1Char('&'), QLatin1String("&&")); |
|
|
|
|
|
|
|
|
|
QAction* action = menu->addAction(actionText, manager, SLOT(showRule())); |
|
|
|
|
QAction* action = menu.addAction(actionText, manager, SLOT(showRule())); |
|
|
|
|
action->setData(QVariant::fromValue((void*)pair.first)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AdBlockIcon::toggleCustomFilter() |
|
|
|
|
{ |
|
|
|
|
QAction* action = qobject_cast<QAction*>(sender()); |
|
|
|
|
if (!action) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const QString filter = action->data().toString(); |
|
|
|
|
AdBlockManager* manager = AdBlockManager::instance(); |
|
|
|
|
AdBlockCustomList* customList = manager->customList(); |
|
|
|
|
|
|
|
|
|
if (customList->containsFilter(filter)) { |
|
|
|
|
customList->removeFilter(filter); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
AdBlockRule* rule = new AdBlockRule(filter, customList); |
|
|
|
|
customList->addRule(rule); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AdBlockIcon::animateIcon() |
|
|
|
|
{ |
|
|
|
|
++m_timerTicks; |
|
|
|
|
if (m_timerTicks > 10) { |
|
|
|
|
stopAnimation(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (icon().isNull()) { |
|
|
|
|
setIcon(QIcon(QSL(":icons/other/adblock.png"))); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
setIcon(QIcon()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AdBlockIcon::stopAnimation() |
|
|
|
|
{ |
|
|
|
|
m_timerTicks = 0; |
|
|
|
|
m_flashTimer->stop(); |
|
|
|
|
disconnect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon())); |
|
|
|
|
|
|
|
|
|
setEnabled(m_enabled); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AdBlockIcon::clicked(ClickController *controller) |
|
|
|
|
{ |
|
|
|
|
QMenu menu; |
|
|
|
|
createMenu(&menu); |
|
|
|
|
menu.exec(controller->popupPosition(menu.sizeHint())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AdBlockIcon::setEnabled(bool enabled) |
|
|
|
|
{ |
|
|
|
|
if (enabled) { |
|
|
|
|
setIcon(QIcon(QSL(":icons/other/adblock.png"))); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
setIcon(QIcon(QSL(":icons/other/adblock-disabled.png"))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
m_enabled = enabled; |
|
|
|
|
} |
|
|
|
|
|