24 #include <KTreeWidgetSearchLine>
26 #include <KConfigGroup>
27 #include <KStandardDirs>
30 #include <QHeaderView>
31 #include <QTreeWidget>
32 #include <QVBoxLayout>
34 #include <QWebElement>
38 #include <QApplication>
41 using namespace MessageViewer;
46 QVBoxLayout *lay =
new QVBoxLayout;
48 mListItems =
new QTreeWidget;
50 mListItems->setContextMenuPolicy(Qt::CustomContextMenu);
51 mListItems->setAlternatingRowColors(
true);
52 mListItems->setRootIsDecorated(
false);
53 connect(mListItems, SIGNAL(customContextMenuRequested(QPoint)),
54 this, SLOT(customContextMenuRequested(QPoint)));
57 lst << i18n(
"Filter") << i18n(
"Address") << i18n(
"Type");
58 mListItems->setHeaderLabels(lst);
60 KTreeWidgetSearchLine *searchLine =
new KTreeWidgetSearchLine(
this, mListItems);
61 searchLine->setPlaceholderText(i18n(
"Search..."));
63 lay->addWidget(searchLine);
64 lay->addWidget(mListItems);
67 mListItems->header()->restoreState(config.readEntry(
"HeaderState",QByteArray()));
73 groupHeader.writeEntry(
"HeaderState", mListItems->header()->saveState());
80 searchBlockableElement(frame);
88 result = i18n(
"Image");
91 result = i18n(
"Script");
94 result = i18n(
"Stylesheet");
97 result = i18n(
"Font");
100 result = i18n(
"Frame");
103 result = i18n(
"XML Request");
106 result = i18n(
"Object");
109 result = i18n(
"Audio/Video");
112 result = i18n(
"Popup window");
116 result = i18n(
"Unknown");
126 result = QLatin1String(
"image");
129 result = QLatin1String(
"script");
132 result = QLatin1String(
"stylesheet");
135 result = QLatin1String(
"font");
138 result = QLatin1String(
"frame");
141 result = QLatin1String(
"xmlhttprequest");
144 result = QLatin1String(
"other");
147 result = QLatin1String(
"media");
150 result = QLatin1String(
"popup");
154 qDebug()<<
" unknown type "<<
type;
159 void AdBlockBlockableItemsWidget::adaptSrc(QString &src,
const QString &hostName)
161 if (src.startsWith(QLatin1String(
"http://")) || src.startsWith(QLatin1String(
"https://")) ) {
163 }
else if (src.startsWith(QLatin1String(
"//"))) {
164 src = QLatin1String(
"https:") + src;
165 }
else if (src.startsWith(QLatin1Char(
'/'))){
166 src = QLatin1String(
"https://") + hostName + src;
172 void AdBlockBlockableItemsWidget::searchBlockableElement(QWebFrame *frame)
174 const QUrl url = frame->requestedUrl();
175 const QString host = url.host();
176 const QWebElementCollection images = frame->findAllElements(QLatin1String(
"img"));
177 Q_FOREACH (
const QWebElement &img, images) {
178 if (img.hasAttribute(QLatin1String(
"src"))) {
179 QString src = img.attribute(QLatin1String(
"src"));
185 QTreeWidgetItem *item =
new QTreeWidgetItem(mListItems);
186 item->setText(Url, src);
188 item->setTextColor(FilterValue, Qt::red);
189 item->setData(Type, Element,
Image);
192 const QWebElementCollection scripts = frame->findAllElements(QLatin1String(
"script"));
193 Q_FOREACH (
const QWebElement &script, scripts) {
194 QString src = script.attribute(QLatin1String(
"src"));
200 QTreeWidgetItem *item =
new QTreeWidgetItem(mListItems);
201 item->setText(Url, src);
203 item->setTextColor(FilterValue, Qt::red);
204 item->setData(Type, Element,
Script);
206 foreach(QWebFrame *childFrame, frame->childFrames()) {
207 searchBlockableElement(childFrame);
211 void AdBlockBlockableItemsWidget::customContextMenuRequested(
const QPoint &)
213 QTreeWidgetItem *item = mListItems->currentItem();
218 menu.addAction(i18n(
"Copy url"),
this,SLOT(slotCopyItem()));
219 if (!item->text(FilterValue).isEmpty())
220 menu.addAction(i18n(
"Copy filter"),
this,SLOT(slotCopyFilterItem()));
221 menu.addAction(i18n(
"Block item..."),
this,SLOT(slotBlockItem()));
223 menu.addAction(i18n(
"Open"),
this, SLOT(slotOpenItem()));
224 if (!item->text(FilterValue).isEmpty()) {
226 menu.addAction(i18n(
"Remove filter"),
this,SLOT(slotRemoveFilter()));
228 menu.exec(QCursor::pos());
231 void AdBlockBlockableItemsWidget::slotCopyFilterItem()
233 QTreeWidgetItem *item = mListItems->currentItem();
236 QClipboard *cb = QApplication::clipboard();
237 cb->setText(item->text(FilterValue), QClipboard::Clipboard);
240 void AdBlockBlockableItemsWidget::slotOpenItem()
242 QTreeWidgetItem *item = mListItems->currentItem();
245 const KUrl url(item->text(Url));
246 KRun *runner =
new KRun( url,
this );
247 runner->setRunExecutables(
false );
250 void AdBlockBlockableItemsWidget::slotBlockItem()
252 QTreeWidgetItem *item = mListItems->currentItem();
257 dlg->setPattern(static_cast<TypeElement>(item->data(Type, Element).toInt()), item->text(Url));
259 const QString filter = dlg->filter();
260 item->setText(FilterValue, filter);
265 void AdBlockBlockableItemsWidget::slotCopyItem()
267 QTreeWidgetItem *item = mListItems->currentItem();
270 QClipboard *cb = QApplication::clipboard();
271 cb->setText(item->text(Url), QClipboard::Clipboard);
276 const int numberOfElement(mListItems->topLevelItemCount());
278 for (
int i = 0; i < numberOfElement; ++i) {
279 QTreeWidgetItem *item = mListItems->topLevelItem(i);
280 if (!item->text(FilterValue).isEmpty()) {
281 if (filters.isEmpty()) {
282 filters = item->text(FilterValue);
284 filters += QLatin1Char(
'\n') + item->text(FilterValue);
289 if (filters.isEmpty())
292 const QString localRulesFilePath = KStandardDirs::locateLocal(
"appdata" , QLatin1String(
"adblockrules_local"));
294 QFile ruleFile(localRulesFilePath);
295 if (!ruleFile.open(QFile::WriteOnly | QFile::Text)) {
296 kDebug() <<
"Unable to open rule file" << localRulesFilePath;
300 QTextStream out(&ruleFile);
306 void AdBlockBlockableItemsWidget::slotRemoveFilter()
308 QTreeWidgetItem *item = mListItems->currentItem();
311 item->setText(FilterValue, QString());
314 #include "adblockblockableitemswidget.moc"
static GlobalSettings * self()
static AdBlockManager * self()
Entry point.