36 #include "pimcommon/util/pimutil.h"
39 #include <KSharedConfig>
40 #include <KStandardDirs>
43 #include <KMessageBox>
47 #include <QListWidgetItem>
50 #include <QTextStream>
52 using namespace MessageViewer;
59 hintLabel->setText(i18n(
"<qt>Filter expression (e.g. <tt>http://www.example.com/ad/*</tt>, <a href=\"filterhelp\">more information</a>):"));
60 connect(hintLabel, SIGNAL(linkActivated(QString)),
this, SLOT(slotInfoLinkActivated(QString)));
62 manualFiltersListWidget->setSelectionMode(QAbstractItemView::MultiSelection);
64 searchLine->setListWidget(manualFiltersListWidget);
66 insertButton->setIcon(KIcon(QLatin1String(
"list-add")));
67 connect(insertButton, SIGNAL(clicked()),
this, SLOT(insertRule()));
69 removeButton->setIcon(KIcon(QLatin1String(
"list-remove")));
70 connect(removeButton, SIGNAL(clicked()),
this, SLOT(removeRule()));
71 connect(removeSubscription, SIGNAL(clicked()), SLOT(slotRemoveSubscription()));
72 connect(manualFiltersListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(slotUpdateManualButtons()));
74 spinBox->setSuffix(ki18np(
" day",
" days"));
76 removeSubscription->setEnabled(
false);
77 showList->setEnabled(
false);
79 connect(checkEnableAdblock, SIGNAL(stateChanged(
int)),
this, SLOT(hasChanged()));
80 connect(checkHideAds, SIGNAL(stateChanged(
int)),
this, SLOT(hasChanged()));
81 connect(spinBox, SIGNAL(valueChanged(
int)),
this, SLOT(hasChanged()));
82 connect(addFilters, SIGNAL(clicked()),
this, SLOT(slotAddFilter()));
83 connect(showList, SIGNAL(clicked()),
this, SLOT(slotShowList()));
84 connect(editFilter, SIGNAL(clicked()),
this, SLOT(slotEditFilter()));
86 connect(automaticFiltersListWidget, SIGNAL(itemChanged(QListWidgetItem*)),
this, SLOT(hasChanged()));
87 connect(automaticFiltersListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(slotUpdateButtons()));
88 connect(automaticFiltersListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(slotAutomaticFilterDouble(QListWidgetItem*)));
90 connect(importFilters, SIGNAL(clicked()), SLOT(slotImportFilters()));
91 connect(exportFilters, SIGNAL(clicked()), SLOT(slotExportFilters()));
92 connect(addFilterLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(slotManualFilterLineEditTextChanged(QString)));
93 slotUpdateManualButtons();
94 insertButton->setEnabled(
false);
97 void AdBlockSettingWidget::slotManualFilterLineEditTextChanged(
const QString &text)
99 insertButton->setEnabled(!text.isEmpty());
102 void AdBlockSettingWidget::slotEditFilter()
104 QListWidgetItem *item = manualFiltersListWidget->currentItem();
106 manualFiltersListWidget->editItem(item);
110 void AdBlockSettingWidget::slotUpdateButtons()
112 const bool enabled = automaticFiltersListWidget->currentItem();
113 removeSubscription->setEnabled(enabled);
114 showList->setEnabled(enabled);
117 void AdBlockSettingWidget::slotUpdateManualButtons()
119 const bool enabled = manualFiltersListWidget->currentItem();
120 removeButton->setEnabled(enabled);
121 editFilter->setEnabled(enabled);
122 exportFilters->setEnabled(manualFiltersListWidget->count()>0);
125 void AdBlockSettingWidget::slotInfoLinkActivated(
const QString &url)
129 const QString hintHelpString = i18n("<qt><p>Enter an expression to filter. Filters can be defined as either:"
130 "<ul><li>a shell-style wildcard, e.g. <tt>http:
131 "the wildcards <tt>*?[]</tt> may be used</li>"
132 "<li>a full regular expression by surrounding the
string with '<tt>/</tt>', "
133 "e.g. <tt>/\\/(ad|banner)\\./</tt></li></ul>"
134 "<p>Any filter
string can be preceded by '<tt>@@</tt>' to whitelist (allow) any matching URL, "
135 "which takes priority over any blacklist (blocking) filter.");
137 QWhatsThis::showText(QCursor::pos(), hintHelpString);
143 const QString rule = addFilterLineEdit->text();
146 const int numberItem(manualFiltersListWidget->count());
147 for (
int i = 0; i < numberItem; ++i) {
148 if (manualFiltersListWidget->item(i)->text() == rule) {
149 addFilterLineEdit->clear();
154 addManualFilter(rule);
155 exportFilters->setEnabled(manualFiltersListWidget->count()>0);
156 addFilterLineEdit->clear();
161 void AdBlockSettingWidget::removeRule()
163 QList<QListWidgetItem *> select = manualFiltersListWidget->selectedItems();
164 if (select.isEmpty()) {
167 Q_FOREACH (QListWidgetItem *item, select) {
170 exportFilters->setEnabled(manualFiltersListWidget->count()>0);
181 spinBox->setValue(days);
187 manualFiltersListWidget->clear();
188 automaticFiltersListWidget->clear();
195 spinBox->setValue(days);
200 KConfig config(QLatin1String(
"messagevieweradblockrc"));
202 const QStringList itemList = config.groupList().filter( QRegExp( QLatin1String(
"FilterList \\d+") ) );
203 Q_FOREACH(
const QString &item, itemList) {
204 KConfigGroup filtersGroup(&config, item);
205 const bool isFilterEnabled = filtersGroup.readEntry(QLatin1String(
"FilterEnabled"),
false);
206 const QString url = filtersGroup.readEntry(QLatin1String(
"url"));
207 const QString path = filtersGroup.readEntry(QLatin1String(
"path"));
208 const QString name = filtersGroup.readEntry(QLatin1String(
"name"));
209 const QDateTime lastUpdate = filtersGroup.readEntry(QLatin1String(
"lastUpdate"), QDateTime());
210 if (url.isEmpty() || path.isEmpty() || name.isEmpty())
213 QListWidgetItem *subItem =
new QListWidgetItem(automaticFiltersListWidget);
214 subItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable| Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
216 subItem->setCheckState(Qt::Checked);
218 subItem->setCheckState(Qt::Unchecked);
220 subItem->setData(UrlList, url);
221 subItem->setData(PathList, path);
222 subItem->setData(LastUpdateList, lastUpdate);
223 subItem->setText(name);
229 const QString localRulesFilePath = KStandardDirs::locateLocal(
"appdata" , QLatin1String(
"adblockrules_local"));
231 QFile ruleFile(localRulesFilePath);
232 if (!ruleFile.open(QFile::ReadOnly | QFile::Text)) {
233 kDebug() <<
"Unable to open rule file" << localRulesFilePath;
237 QTextStream in(&ruleFile);
238 while (!in.atEnd()) {
239 QString stringRule = in.readLine();
240 addManualFilter(stringRule);
256 KConfig config(QLatin1String(
"messagevieweradblockrc"));
257 const QStringList list = config.groupList().filter( QRegExp( QLatin1String(
"FilterList \\d+") ) );
258 foreach (
const QString &group, list ) {
259 config.deleteGroup( group );
262 const int numberItem(automaticFiltersListWidget->count());
263 for (
int i = 0; i < numberItem; ++i) {
264 QListWidgetItem *subItem = automaticFiltersListWidget->item(i);
265 KConfigGroup grp = config.group(QString::fromLatin1(
"FilterList %1").arg(i));
266 grp.writeEntry(QLatin1String(
"FilterEnabled"), subItem->checkState() == Qt::Checked);
267 grp.writeEntry(QLatin1String(
"url"), subItem->data(UrlList).toString());
268 grp.writeEntry(QLatin1String(
"name"), subItem->text());
269 grp.writeEntry(QLatin1String(
"lastUpdate"), subItem->data(LastUpdateList).toDateTime());
270 QString path = subItem->data(PathList).toString();
271 if (path.isEmpty()) {
272 path = KStandardDirs::locateLocal(
"appdata", QString::fromLatin1(
"adblockrules-%1").arg(i));
274 grp.writeEntry(QLatin1String(
"path"), path);
279 const QString localRulesFilePath = KStandardDirs::locateLocal(
"appdata" , QLatin1String(
"adblockrules_local"));
281 QFile ruleFile(localRulesFilePath);
282 if (!ruleFile.open(QFile::WriteOnly | QFile::Text)) {
283 kDebug() <<
"Unable to open rule file" << localRulesFilePath;
287 QTextStream out(&ruleFile);
288 for (
int i = 0; i < manualFiltersListWidget->count(); ++i) {
289 QListWidgetItem *subItem = manualFiltersListWidget->item(i);
290 const QString stringRule = subItem->text();
291 if (!stringRule.trimmed().isEmpty())
292 out << stringRule <<
'\n';
302 void AdBlockSettingWidget::hasChanged()
305 checkHideAds->setEnabled(checkEnableAdblock->isChecked());
306 tabWidget->setEnabled(checkEnableAdblock->isChecked());
317 void AdBlockSettingWidget::slotAddFilter()
319 QStringList excludeList;
320 const int numberItem(automaticFiltersListWidget->count());
321 for (
int i = 0; i < numberItem; ++i) {
322 excludeList << automaticFiltersListWidget->item(i)->text();
328 dlg->selectedList(name, url);
329 QListWidgetItem *subItem =
new QListWidgetItem(automaticFiltersListWidget);
330 subItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
331 subItem->setCheckState(Qt::Checked);
332 subItem->setText(name);
333 subItem->setData(UrlList, url);
334 subItem->setData(LastUpdateList, QDateTime());
335 subItem->setData(PathList, QString());
341 void AdBlockSettingWidget::slotRemoveSubscription()
343 QListWidgetItem *item = automaticFiltersListWidget->currentItem();
345 if (KMessageBox::questionYesNo(
this, i18n(
"Do you want to delete list \"%1\"?", item->text()), i18n(
"Delete current list")) == KMessageBox::Yes) {
346 const QString path = item->data(PathList).toString();
347 if (!path.isEmpty()) {
348 if (!QFile(path).remove())
349 qDebug()<<
" we can not remove file:"<<path;
357 void AdBlockSettingWidget::slotShowList()
359 showAutomaticFilterList(automaticFiltersListWidget->currentItem());
362 void AdBlockSettingWidget::showAutomaticFilterList(QListWidgetItem *item)
366 dlg->setAdBlockListPath(item->data(PathList).toString(), item->data(UrlList).toString());
372 void AdBlockSettingWidget::slotImportFilters()
374 const QString filter = i18n(
"*|all files (*)" );
375 const QString result = PimCommon::Util::loadToFile(filter,
this);
376 if (result.isEmpty()) {
379 const QStringList listFilter = result.split(QLatin1Char(
'\n'));
380 QStringList excludeFilter;
381 const int numberOfElement(manualFiltersListWidget->count());
382 for (
int i = 0; i < numberOfElement; ++i) {
383 QListWidgetItem *subItem = manualFiltersListWidget->item(i);
384 excludeFilter.append(subItem->text());
387 Q_FOREACH (
const QString &element, listFilter) {
388 if (element == QLatin1String(
"\n"))
390 if (excludeFilter.contains(element))
392 addManualFilter(element);
396 void AdBlockSettingWidget::addManualFilter(
const QString &text)
398 QListWidgetItem *subItem =
new QListWidgetItem(manualFiltersListWidget);
399 subItem->setFlags(subItem->flags() | Qt::ItemIsEditable);
400 subItem->setText(text);
403 void AdBlockSettingWidget::slotExportFilters()
405 const QString filter = i18n(
"*|all files (*)" );
406 QString exportFilters;
407 const int numberOfElement(manualFiltersListWidget->count());
408 for (
int i = 0; i < numberOfElement; ++i) {
409 QListWidgetItem *subItem = manualFiltersListWidget->item(i);
410 const QString stringRule = subItem->text();
411 if (!stringRule.isEmpty())
412 exportFilters += stringRule + QLatin1Char(
'\n');
414 PimCommon::Util::saveTextAs(exportFilters, filter,
this);
417 void AdBlockSettingWidget::slotAutomaticFilterDouble(QListWidgetItem *item)
419 showAutomaticFilterList(item);
422 #include "adblocksettingwidget.moc"
static GlobalSettings * self()
static AdBlockManager * self()
Entry point.