Commit 6cfaa046 authored by David Faure's avatar David Faure

konqueror-related code reformatted using astyle-kdelibs

parent 1214f687
// -*- mode: c++; c-basic-offset: 4 -*-
/*
Copyright (c) 2008 Laurent Montel <montel@kde.org>
Copyright (C) 2006 Daniele Galdi <daniele.galdi@gmail.com>
......@@ -54,45 +53,43 @@ using namespace DOM;
#include <qcursor.h>
#include <qregexp.h>
K_PLUGIN_FACTORY( AdBlockFactory, registerPlugin< AdBlock >(); )
K_EXPORT_PLUGIN( AdBlockFactory( "adblock" ) )
K_PLUGIN_FACTORY(AdBlockFactory, registerPlugin< AdBlock >();)
K_EXPORT_PLUGIN(AdBlockFactory("adblock"))
AdBlock::AdBlock(QObject *parent, const QVariantList & /*args*/) :
Plugin(parent),
m_menu(0), m_elements(0)
{
m_part = dynamic_cast<KHTMLPart *>(parent);
if(!m_part)
{
if (!m_part) {
kDebug() << "couldn't get KHTMLPart";
return;
}
m_menu = new KActionMenu(KIcon( "preferences-web-browser-adblock" ), i18n("Adblock"),
actionCollection() );
actionCollection()->addAction( "action adblock", m_menu );
m_menu->setDelayed( false );
m_menu = new KActionMenu(KIcon("preferences-web-browser-adblock"), i18n("Adblock"),
actionCollection());
actionCollection()->addAction("action adblock", m_menu);
m_menu->setDelayed(false);
QAction *a = actionCollection()->addAction( "show_elements");
QAction *a = actionCollection()->addAction("show_elements");
a->setText(i18n("Show Blockable Elements..."));
connect(a, SIGNAL(triggered()), this, SLOT(slotConfigure()));
m_menu->addAction(a);
a = actionCollection()->addAction( "configure");
a = actionCollection()->addAction("configure");
a->setText(i18n("Configure Filters..."));
connect(a, SIGNAL(triggered()), this, SLOT(showKCModule()));
m_menu->addAction(a);
a = actionCollection()->addAction( "separator" );
a->setSeparator( true );
a = actionCollection()->addAction("separator");
a->setSeparator(true);
m_menu->addAction(a);
a = actionCollection()->addAction( "disable_for_this_page");
a = actionCollection()->addAction("disable_for_this_page");
a->setText(i18n("No blocking for this page"));
connect(a, SIGNAL(triggered()), this, SLOT(slotDisableForThisPage()));
m_menu->addAction(a);
a = actionCollection()->addAction( "disable_for_this_site");
a = actionCollection()->addAction("disable_for_this_site");
a->setText(i18n("No blocking for this site"));
connect(a, SIGNAL(triggered()), this, SLOT(slotDisableForThisSite()));
m_menu->addAction(a);
......@@ -104,8 +101,9 @@ AdBlock::~AdBlock()
{
KParts::StatusBarExtension *statusBarEx = KParts::StatusBarExtension::childObject(m_part);
if (statusBarEx && m_label)
if (statusBarEx && m_label) {
statusBarEx->removeStatusBarItem(m_label.data());
}
delete m_label.data();
m_label.clear();
delete m_menu;
......@@ -116,7 +114,9 @@ AdBlock::~AdBlock()
void AdBlock::initLabel()
{
if (m_label) return;
if (m_label) {
return;
}
KParts::StatusBarExtension *statusBarEx = KParts::StatusBarExtension::childObject(m_part);
......@@ -125,7 +125,7 @@ void AdBlock::initLabel()
return;
}
KUrlLabel* label = new KUrlLabel(statusBarEx->statusBar());
KUrlLabel *label = new KUrlLabel(statusBarEx->statusBar());
KIconLoader *loader = KIconLoader::global();
......@@ -137,18 +137,17 @@ void AdBlock::initLabel()
statusBarEx->addStatusBarItem(label, 0, false);
connect(label, SIGNAL(leftClickedUrl()), this, SLOT(slotConfigure()));
connect(label, SIGNAL(rightClickedUrl()), this, SLOT(contextMenu()));
m_label = label;
}
void AdBlock::disableForUrl(KUrl url)
{
url.setQuery(QString());
url.setRef(QString());
KHTMLSettings *settings = const_cast<KHTMLSettings *>(m_part->settings());
settings->addAdFilter("@@"+url.url());
settings->addAdFilter("@@" + url.url());
}
void AdBlock::slotDisableForThisPage()
......@@ -163,16 +162,14 @@ void AdBlock::slotDisableForThisSite()
disableForUrl(u);
}
void AdBlock::slotConfigure()
{
if (!m_part->settings()->isAdFilterEnabled())
{
KMessageBox::error(0,
if (!m_part->settings()->isAdFilterEnabled()) {
KMessageBox::error(0,
i18n("Please enable Konqueror's Adblock"),
i18nc("@title:window", "Adblock disabled"));
return;
return;
}
m_elements = new AdElementList;
......@@ -187,7 +184,7 @@ void AdBlock::slotConfigure()
void AdBlock::showKCModule()
{
KCMultiDialog* dialogue = new KCMultiDialog(m_part->widget());
KCMultiDialog *dialogue = new KCMultiDialog(m_part->widget());
dialogue->addModule("khtml_filter");
connect(dialogue, SIGNAL(cancelClicked()), dialogue, SLOT(delayedDestruct()));
connect(dialogue, SIGNAL(closeClicked()), dialogue, SLOT(delayedDestruct()));
......@@ -199,15 +196,13 @@ void AdBlock::contextMenu()
m_menu->menu()->exec(QCursor::pos());
}
void AdBlock::fillBlockableElements()
{
fillWithHtmlTag("script", "src", i18n( "script" ));
fillWithHtmlTag("embed" , "src", i18n( "object" ));
fillWithHtmlTag("object", "src", i18n( "object" ));
fillWithHtmlTag("script", "src", i18n("script"));
fillWithHtmlTag("embed", "src", i18n("object"));
fillWithHtmlTag("object", "src", i18n("object"));
// TODO: iframe's are not blocked by KHTML yet
fillWithHtmlTag("iframe", "src", i18n( "frame" ));
fillWithHtmlTag("iframe", "src", i18n("frame"));
fillWithImages();
updateFilters();
......@@ -219,45 +214,45 @@ void AdBlock::fillWithImages()
HTMLCollection images = htmlDoc.images();
for (unsigned int i = 0; i < images.length(); i++)
{
HTMLImageElement image = static_cast<HTMLImageElement>( images.item(i) );
for (unsigned int i = 0; i < images.length(); i++) {
HTMLImageElement image = static_cast<HTMLImageElement>(images.item(i));
DOMString src = image.src();
DOMString src = image.src();
QString url = htmlDoc.completeURL(src).string();
if (!url.isEmpty() && url != m_part->baseURL().url())
{
AdElement element(url, i18n( "image" ), "IMG", false, image);
if (!m_elements->contains( element ))
m_elements->append( element);
}
QString url = htmlDoc.completeURL(src).string();
if (!url.isEmpty() && url != m_part->baseURL().url()) {
AdElement element(url, i18n("image"), "IMG", false, image);
if (!m_elements->contains(element)) {
m_elements->append(element);
}
}
}
}
void AdBlock::fillWithHtmlTag(const DOMString &tagName,
const DOMString &attrName,
const QString &category)
const DOMString &attrName,
const QString &category)
{
Document doc = m_part->document();
NodeList nodes = doc.getElementsByTagName(tagName);
for (unsigned int i = 0; i < nodes.length(); i++)
{
Node node = nodes.item(i);
Node attr = node.attributes().getNamedItem(attrName);
DOMString src = attr.nodeValue();
if (src.isNull()) continue;
QString url = doc.completeURL(src).string();
if (!url.isEmpty() && url != m_part->baseURL().url())
{
AdElement element(url, category, tagName.string().toUpper(), false, attr);
if (!m_elements->contains( element ))
m_elements->append( element);
}
for (unsigned int i = 0; i < nodes.length(); i++) {
Node node = nodes.item(i);
Node attr = node.attributes().getNamedItem(attrName);
DOMString src = attr.nodeValue();
if (src.isNull()) {
continue;
}
QString url = doc.completeURL(src).string();
if (!url.isEmpty() && url != m_part->baseURL().url()) {
AdElement element(url, category, tagName.string().toUpper(), false, attr);
if (!m_elements->contains(element)) {
m_elements->append(element);
}
}
}
}
......@@ -269,53 +264,46 @@ void AdBlock::addAdFilter(const QString &url)
updateFilters();
}
void AdBlock::updateFilters()
{
const KHTMLSettings *settings = m_part->settings();
AdElementList::iterator it;
for ( it = m_elements->begin(); it != m_elements->end(); ++it )
{
AdElement &element = (*it);
for (it = m_elements->begin(); it != m_elements->end(); ++it) {
AdElement &element = (*it);
bool isWhitelist;
QString filter = settings->adFilteredBy(element.url(), &isWhitelist);
if (!filter.isEmpty())
{
if (!isWhitelist)
{
if (!filter.isEmpty()) {
if (!isWhitelist) {
element.setBlocked(true);
element.setBlockedBy(i18n("Blocked by %1",filter));
element.setBlockedBy(i18n("Blocked by %1", filter));
} else {
element.setBlockedBy(i18n("Allowed by %1", filter));
}
else
element.setBlockedBy(i18n("Allowed by %1",filter));
}
}
}
// ----------------------------------------------------------------------------
AdElement::AdElement() :
m_blocked(false) {}
m_blocked(false) {}
AdElement::AdElement(const QString &url, const QString &category,
const QString &type, bool blocked, const DOM::Node&node) :
m_url(url), m_category(category), m_type(type), m_blocked(blocked),m_node( node ) {}
const QString &type, bool blocked, const DOM::Node &node) :
m_url(url), m_category(category), m_type(type), m_blocked(blocked), m_node(node) {}
AdElement &AdElement::operator=(const AdElement &obj)
{
m_blocked = obj.m_blocked;
m_blockedBy = obj.m_blockedBy;
m_url = obj.m_url;
m_category = obj.m_category;
m_type = obj.m_type;
m_node = obj.m_node;
return *this;
m_blocked = obj.m_blocked;
m_blockedBy = obj.m_blockedBy;
m_url = obj.m_url;
m_category = obj.m_category;
m_type = obj.m_type;
m_node = obj.m_node;
return *this;
}
bool AdElement::operator==(const AdElement &obj)
......@@ -325,29 +313,19 @@ bool AdElement::operator==(const AdElement &obj)
bool AdElement::isBlocked() const
{
return m_blocked;
return m_blocked;
}
QString AdElement::blockedBy() const
{
return m_blockedBy;
}
void AdElement::setBlockedBy(const QString &by)
{
m_blockedBy = by;
}
void AdElement::setBlocked(bool blocked)
{
m_blocked = blocked;
......@@ -355,17 +333,17 @@ void AdElement::setBlocked(bool blocked)
QString AdElement::url() const
{
return m_url;
return m_url;
}
QString AdElement::category() const
{
return m_category;
return m_category;
}
QString AdElement::type() const
{
return m_type;
return m_type;
}
DOM::Node AdElement::node() const
......
// -*- mode: c++; c-basic-offset: 4 -*-
/*
Copyright (c) 2008 Laurent Montel <montel@kde.org>
Copyright (C) 2006 Daniele Galdi <daniele.galdi@gmail.com>
......@@ -39,7 +38,7 @@ namespace KParts
namespace DOM
{
class DOMString;
class DOMString;
}
typedef QList<AdElement> AdElementList;
......@@ -49,7 +48,7 @@ class AdBlock : public KParts::Plugin
Q_OBJECT
public:
AdBlock(QObject* parent = 0, const QVariantList &args = QVariantList());
AdBlock(QObject *parent = 0, const QVariantList &args = QVariantList());
~AdBlock();
private:
......@@ -60,8 +59,8 @@ private:
void fillBlockableElements();
void fillWithImages();
void fillWithHtmlTag(const DOM::DOMString &tagName,
const DOM::DOMString &attrName,
const QString &category);
const DOM::DOMString &attrName,
const QString &category);
private slots:
void initLabel();
......@@ -86,7 +85,7 @@ class AdElement
public:
AdElement();
AdElement(const QString &url, const QString &category,
const QString &type, bool blocked, const DOM::Node& node);
const QString &type, bool blocked, const DOM::Node &node);
AdElement &operator=(const AdElement &);
bool operator==(const AdElement &e1);
......
// -*- mode: c++; c-basic-offset: 4 -*-
/*
Copyright (c) 2008 Laurent Montel <montel@kde.org>
Copyright (C) 2006 Daniele Galdi <daniele.galdi@gmail.com>
......@@ -44,17 +43,26 @@
class ListViewItem : public QTreeWidgetItem
{
public:
ListViewItem(QTreeWidget *listView, const QStringList& lst,const AdElement *element)
: QTreeWidgetItem( listView, lst),
ListViewItem(QTreeWidget *listView, const QStringList &lst, const AdElement *element)
: QTreeWidgetItem(listView, lst),
m_element(element),
m_blocked(false){};
m_blocked(false) {};
bool isBlocked() const { return (m_blocked); };
bool isBlocked() const
{
return (m_blocked);
};
void setBlocked(bool blocked);
void setBlockedBy(const QString &reason);
void setNode( const DOM::Node& node );
DOM::Node node()const { return (m_node); }
const AdElement *element() const { return (m_element); }
void setNode(const DOM::Node &node);
DOM::Node node()const
{
return (m_node);
}
const AdElement *element() const
{
return (m_element);
}
private:
const AdElement *m_element;
......@@ -62,41 +70,39 @@ private:
DOM::Node m_node;
};
void ListViewItem::setBlocked(bool blocked)
{
m_blocked = blocked;
setData ( 0,Qt::TextColorRole, (blocked ? Qt::red : Qt::black));
setData(0, Qt::TextColorRole, (blocked ? Qt::red : Qt::black));
QFont itemFont = font(0);
itemFont.setItalic( blocked );
itemFont.setBold( blocked );
setData( 0, Qt::FontRole, itemFont );
itemFont.setItalic(blocked);
itemFont.setBold(blocked);
setData(0, Qt::FontRole, itemFont);
}
void ListViewItem::setBlockedBy(const QString &reason)
{
setToolTip( 0, reason );
setToolTip(0, reason);
}
void ListViewItem::setNode( const DOM::Node& node )
void ListViewItem::setNode(const DOM::Node &node)
{
m_node = node;
}
// ----------------------------------------------------------------------------
AdBlockDlg::AdBlockDlg(QWidget *parent, const AdElementList *elements, KHTMLPart*part) :
KDialog( parent ), m_part( part )
AdBlockDlg::AdBlockDlg(QWidget *parent, const AdElementList *elements, KHTMLPart *part) :
KDialog(parent), m_part(part)
{
setModal( true );
setCaption( i18nc("@title:window", "Blockable items on this page") );
setButtons( KDialog::User1 | KDialog::User2 | KDialog::Close );
setDefaultButton( KDialog::User2 );
setButtonText( KDialog::User1, i18n("Configure Filters...") );
setButtonIcon( KDialog::User1, KIcon("preferences-web-browser-adblock") );
setButtonText( KDialog::User2, i18n("Add filter") );
setButtonIcon( KDialog::User2, KStandardGuiItem::add().icon() );
setModal(true);
setCaption(i18nc("@title:window", "Blockable items on this page"));
setButtons(KDialog::User1 | KDialog::User2 | KDialog::Close);