2011-03-29 20:30:05 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-03-29 20:30:05 +02: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 "adblockicon.h"
|
|
|
|
#include "adblockmanager.h"
|
2012-02-04 18:45:59 +01:00
|
|
|
#include "mainapplication.h"
|
2011-03-29 20:30:05 +02:00
|
|
|
#include "qupzilla.h"
|
|
|
|
#include "webpage.h"
|
2012-01-21 23:19:38 +01:00
|
|
|
#include "tabbedwebview.h"
|
|
|
|
#include "tabwidget.h"
|
2012-02-04 18:45:59 +01:00
|
|
|
#include "desktopnotificationsfactory.h"
|
2011-03-29 20:30:05 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QTimer>
|
|
|
|
|
2011-10-15 18:08:52 +02:00
|
|
|
AdBlockIcon::AdBlockIcon(QupZilla* mainClass, QWidget* parent)
|
2011-11-06 17:01:23 +01:00
|
|
|
: ClickableLabel(parent)
|
|
|
|
, p_QupZilla(mainClass)
|
2012-02-04 18:45:59 +01:00
|
|
|
, m_menuAction(0)
|
|
|
|
, m_flashTimer(0)
|
|
|
|
, m_timerTicks(0)
|
|
|
|
, m_enabled(false)
|
2011-03-29 20:30:05 +02:00
|
|
|
{
|
|
|
|
setMaximumHeight(16);
|
|
|
|
setCursor(Qt::PointingHandCursor);
|
2012-01-07 20:56:58 +01:00
|
|
|
setToolTip(tr("AdBlock lets you block unwanted content on web pages"));
|
2011-03-29 20:30:05 +02:00
|
|
|
|
|
|
|
connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint)));
|
|
|
|
}
|
|
|
|
|
2012-02-04 18:45:59 +01:00
|
|
|
void AdBlockIcon::popupBlocked(const QString &rule, const QUrl &url)
|
|
|
|
{
|
|
|
|
QPair<QString, QUrl> pair;
|
|
|
|
pair.first = rule;
|
|
|
|
pair.second = url;
|
|
|
|
|
|
|
|
m_blockedPopups.append(pair);
|
|
|
|
|
|
|
|
mApp->desktopNotifications()->showNotifications(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();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_flashTimer->setInterval(500);
|
|
|
|
m_flashTimer->start();
|
|
|
|
|
|
|
|
connect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction* AdBlockIcon::menuAction()
|
2011-03-29 20:30:05 +02:00
|
|
|
{
|
2012-02-04 18:45:59 +01:00
|
|
|
if (!m_menuAction) {
|
|
|
|
m_menuAction = new QAction(tr("AdBlock"), this);
|
|
|
|
m_menuAction->setMenu(new QMenu);
|
|
|
|
connect(m_menuAction->menu(), SIGNAL(aboutToShow()), this, SLOT(createMenu()));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_menuAction->setIcon(QIcon(m_enabled ? ":icons/other/adblock.png" : ":icons/other/adblock-disabled.png"));
|
|
|
|
|
|
|
|
return m_menuAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockIcon::createMenu(QMenu* menu)
|
|
|
|
{
|
|
|
|
if (!menu) {
|
|
|
|
menu = qobject_cast<QMenu*>(sender());
|
|
|
|
if (!menu) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
menu->clear();
|
|
|
|
|
2011-03-29 20:30:05 +02:00
|
|
|
AdBlockManager* manager = AdBlockManager::instance();
|
|
|
|
|
2012-02-04 18:45:59 +01:00
|
|
|
menu->addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog()));
|
|
|
|
menu->addSeparator();
|
|
|
|
if (!m_blockedPopups.isEmpty()) {
|
|
|
|
menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false);
|
|
|
|
for (int i = 0; i < m_blockedPopups.count(); i++) {
|
|
|
|
const QPair<QString, QUrl> &pair = m_blockedPopups.at(i);
|
|
|
|
QString address = pair.second.toString().right(55);
|
|
|
|
menu->addAction(tr("%1 with (%2)").arg(address, pair.first).replace("&", "&&"), manager, SLOT(showRule()))->setData(pair.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
menu->addSeparator();
|
2011-03-29 20:30:05 +02:00
|
|
|
QList<WebPage::AdBlockedEntry> entries = p_QupZilla->weView()->webPage()->adBlockedEntries();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (entries.isEmpty()) {
|
2012-02-04 18:45:59 +01:00
|
|
|
menu->addAction(tr("No content blocked"))->setEnabled(false);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-29 20:30:05 +02:00
|
|
|
else {
|
2012-02-04 18:45:59 +01:00
|
|
|
menu->addAction(tr("Blocked URL (AdBlock Rule) - click to edit rule"))->setEnabled(false);
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const WebPage::AdBlockedEntry & entry, entries) {
|
2011-03-29 20:30:05 +02:00
|
|
|
QString address = entry.url.toString().right(55);
|
2012-02-04 18:45:59 +01:00
|
|
|
menu->addAction(tr("%1 with (%2)").arg(address, entry.rule).replace("&", "&&"), manager, SLOT(showRule()))->setData(entry.rule);
|
2011-03-29 20:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-04 18:45:59 +01:00
|
|
|
menu->addSeparator();
|
|
|
|
menu->addAction(tr("Learn About Writing &Rules"), this, SLOT(learnAboutRules()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockIcon::showMenu(const QPoint &pos)
|
|
|
|
{
|
|
|
|
QMenu menu;
|
|
|
|
createMenu(&menu);
|
2011-03-29 20:30:05 +02:00
|
|
|
|
|
|
|
menu.exec(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockIcon::learnAboutRules()
|
|
|
|
{
|
2012-01-21 23:19:38 +01:00
|
|
|
p_QupZilla->tabWidget()->addView(QUrl("http://adblockplus.org/en/filters"), Qz::NT_SelectedTab);
|
2011-03-29 20:30:05 +02:00
|
|
|
}
|
|
|
|
|
2012-02-04 18:45:59 +01:00
|
|
|
void AdBlockIcon::animateIcon()
|
|
|
|
{
|
|
|
|
++m_timerTicks;
|
|
|
|
if (m_timerTicks > 10) {
|
|
|
|
stopAnimation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pixmap()->isNull()) {
|
|
|
|
setPixmap(QPixmap(":icons/other/adblock.png"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setPixmap(QPixmap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockIcon::stopAnimation()
|
|
|
|
{
|
|
|
|
m_timerTicks = 0;
|
|
|
|
m_flashTimer->stop();
|
|
|
|
disconnect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
|
|
|
|
|
|
|
|
setEnabled(m_enabled);
|
|
|
|
}
|
|
|
|
|
2011-10-15 16:37:32 +02:00
|
|
|
void AdBlockIcon::setEnabled(bool enabled)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (enabled) {
|
2011-10-15 16:37:32 +02:00
|
|
|
setPixmap(QPixmap(":icons/other/adblock.png"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-10-15 16:37:32 +02:00
|
|
|
setPixmap(QPixmap(":icons/other/adblock-disabled.png"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-02-04 18:45:59 +01:00
|
|
|
|
|
|
|
m_enabled = enabled;
|
2011-10-15 16:37:32 +02:00
|
|
|
}
|
|
|
|
|
2011-03-29 20:30:05 +02:00
|
|
|
AdBlockIcon::~AdBlockIcon()
|
|
|
|
{
|
|
|
|
}
|