summaryrefslogtreecommitdiffstats
path: root/kcontrol/konqhtml/filteropts.cpp
blob: 3ae9091a0974edaedccd4b9f6311e788b786a810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
   Copyright (C) 2005 Ivor Hewitt <ivor@ivor.org>

   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 2 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, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <tdeconfig.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <tdeaboutdata.h>
#include <tdefiledialog.h>
#include <dcopclient.h>

#include <tqlayout.h>
#include <tqlistbox.h>
#include <tqpushbutton.h>
#include <tqgroupbox.h>
#include <tqhbox.h>
#include <tqvbox.h>
#include <tqlineedit.h>
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqwhatsthis.h>
#include <tqregexp.h>

#include "filteropts.h"
#include "filteropts.moc"

KCMFilter::KCMFilter(TDEConfig *config, TQString group,
                     TQWidget *parent, const char * )
    : TDECModule( parent, "kcmkonqhtml" ),
      mConfig( config ),
      mGroupname( group ),
      mSelCount(0)
{
    setButtons(Default|Apply);

    TQVBoxLayout *topLayout = new TQVBoxLayout(this, 0, KDialog::spacingHint());

    mEnableCheck = new TQCheckBox(i18n("Enable filters"), this);
    topLayout->addWidget( mEnableCheck );

    mKillCheck = new TQCheckBox(i18n("Hide filtered images"), this);
    topLayout->addWidget( mKillCheck );

    TQGroupBox *topBox = new TQGroupBox( 1, Qt::Horizontal, i18n("URL Expressions to Filter"), this );
    topLayout->addWidget( topBox );

    mListBox = new TQListBox( topBox );
    mListBox->setSelectionMode(TQListBox::Extended);
    new TQLabel( i18n("Expression (e.g. http://www.site.com/ad/*):"), topBox);
    mString = new TQLineEdit( topBox );

    TQHBox *buttonBox = new TQHBox( topBox );
    buttonBox->setSpacing( KDialog::spacingHint() );

    mInsertButton = new TQPushButton( i18n("Insert"), buttonBox );
    connect( mInsertButton, TQT_SIGNAL( clicked() ), TQT_SLOT( insertFilter() ) );
    mUpdateButton = new TQPushButton( i18n("Update"), buttonBox );
    connect( mUpdateButton, TQT_SIGNAL( clicked() ), TQT_SLOT( updateFilter() ) );
    mRemoveButton = new TQPushButton( i18n("Remove"), buttonBox );
    connect( mRemoveButton, TQT_SIGNAL( clicked() ), TQT_SLOT( removeFilter() ) );

    mImportButton = new TQPushButton(i18n("Import..."),buttonBox);
    connect( mImportButton, TQT_SIGNAL( clicked() ), TQT_SLOT( importFilters() ) );
    mExportButton = new TQPushButton(i18n("Export..."),buttonBox);
    connect( mExportButton, TQT_SIGNAL( clicked() ), TQT_SLOT( exportFilters() ) );

    connect( mEnableCheck, TQT_SIGNAL( clicked()), this, TQT_SLOT( slotEnableChecked()));
    connect( mKillCheck, TQT_SIGNAL( clicked()), this, TQT_SLOT( slotKillChecked()));
    connect( mListBox, TQT_SIGNAL( selectionChanged ()),this, TQT_SLOT( slotItemSelected()));

/*
 * Whats this items
 */
    TQWhatsThis::add( mEnableCheck, i18n("Enable or disable AdBlocK filters. When enabled a set of expressions "
                                        "to be blocked should be defined in the filter list for blocking to "
                                        "take effect."));
    TQWhatsThis::add( mKillCheck, i18n("When enabled blocked images will be removed from the page completely "
                                      "otherwise a placeholder 'blocked' image will be used."));
    TQWhatsThis::add( mListBox, i18n("This is the list of URL filters that will be applied to all linked "
                                    "images and frames. The filters are processed in order so place "
                                    "more generic filters towards the top of the list."));
    TQWhatsThis::add( mString, i18n("Enter an expression to filter. Expressions can be defined as either "
                                   "a filename style wildcard e.g. http://www.site.com/ads* or as a full "
                                   "regular expression by surrounding the string with '/' e.g. "
                                   " //(ad|banner)\\./"));
    load();
    updateButton();
}

KCMFilter::~KCMFilter()
{
    delete mConfig;
}

void KCMFilter::slotKillChecked()
{
    emit changed( true );
}

void KCMFilter::slotEnableChecked()
{
    updateButton();
    emit changed( true );
}

void KCMFilter::slotItemSelected()
{
    int currentId=-1;
    unsigned int i;
    for( i=0,mSelCount=0; i < mListBox->count() && mSelCount<2; ++i )
    {
        if (mListBox->isSelected(i))
        {
            currentId=i;
            mSelCount++;
        }
    }

    if ( currentId >= 0 )
    {
        mString->setText(mListBox->text(currentId));
    }
    updateButton();
}

void KCMFilter::updateButton()
{
    bool state = mEnableCheck->isChecked();

    mUpdateButton->setEnabled(state && (mSelCount == 1));
    mRemoveButton->setEnabled(state && (mSelCount > 0));
    mInsertButton->setEnabled(state);
    mImportButton->setEnabled(state);
    mExportButton->setEnabled(state);

    mListBox->setEnabled(state);
    mString->setEnabled(state);
    mKillCheck->setEnabled(state);
}

void KCMFilter::importFilters()
{
    TQString inFile = KFileDialog::getOpenFileName();
    if (inFile.length() > 0)
    {
        TQFile f(inFile);
        if ( f.open( IO_ReadOnly ) )
        {
            TQTextStream ts( &f );
            TQStringList paths;
            TQString line;
            while (!ts.atEnd())
            {
                line = ts.readLine();
                if (line.lower().compare("[adblock]") == 0)
                    continue;
                
                // Treat leading ! as filter comment, otherwise check expressions
                // are valid.
                if (!line.startsWith("!"))
                {
                    if (line.length()>2 && line[0]=='/' && line[line.length()-1] == '/')
                    {
                        TQString inside = line.mid(1, line.length()-2);
                        TQRegExp rx(inside);
                        if (!rx.isValid())
                            continue;
                    }
                    else
                    {
                        TQRegExp rx(line);
                        rx.setWildcard(true);
                        if (!rx.isValid())
                            continue;
                    }
                }

                if (!line.isEmpty() && mListBox->findItem(line, TQt::CaseSensitive|TQt::ExactMatch) == 0)
                    paths.append(line);
            }
            f.close();
            
            mListBox->insertStringList( paths );
            emit changed(true);
        }
    }
}

void KCMFilter::exportFilters()
{
  TQString outFile = KFileDialog::getSaveFileName();
  if (outFile.length() > 0)
  {
    TQFile f(outFile);
    if ( f.open( IO_WriteOnly ) )
    {
      TQTextStream ts( &f );
      ts.setEncoding( TQTextStream::UnicodeUTF8 );
      ts << "[AdBlock]" << endl;

      uint i;
      for( i = 0; i < mListBox->count(); ++i )
        ts << mListBox->text(i) << endl;

      f.close();
    }
  }
}

void KCMFilter::defaults()
{
	load( true );
}

void KCMFilter::save()
{
    mConfig->deleteGroup(mGroupname);
    mConfig->setGroup(mGroupname);

    mConfig->writeEntry("Enabled",mEnableCheck->isChecked());
    mConfig->writeEntry("Shrink",mKillCheck->isChecked());

    uint i;
    for( i = 0; i < mListBox->count(); ++i )
    {
        TQString key = "Filter-" + TQString::number(i);
        mConfig->writeEntry(key, mListBox->text(i));
    }
    mConfig->writeEntry("Count",mListBox->count());

    mConfig->sync();
    DCOPClient::mainClient()->send("konqueror*","KonquerorIface","reparseConfiguration()",TQByteArray());

}
void KCMFilter::load()
{
	load( false );
}

void KCMFilter::load( bool useDefaults )
{
    TQStringList paths;

	 mConfig->setReadDefaults( useDefaults );

    mConfig->setGroup( mGroupname );
    mEnableCheck->setChecked( mConfig->readBoolEntry("Enabled",false));
    mKillCheck->setChecked( mConfig->readBoolEntry("Shrink",false));

    TQMap<TQString,TQString> entryMap = mConfig->entryMap( mGroupname );
    TQMap<TQString,TQString>::ConstIterator it;
    int num = mConfig->readNumEntry("Count",0);
    for (int i=0; i<num; ++i)
    {
        TQString key = "Filter-" + TQString::number(i);
        it = entryMap.find(key);
        if (it != entryMap.end())
            paths.append(it.data());
    }

    mListBox->insertStringList( paths );

	 emit changed( useDefaults );
}

void KCMFilter::insertFilter()
{
    if ( !mString->text().isEmpty() )
    {
        mListBox->insertItem( mString->text() );
        int id=mListBox->count()-1;
        mListBox->clearSelection();
        mListBox->setSelected(id,true);
        mListBox->setCurrentItem(id);
        mListBox->ensureCurrentVisible();
        mString->clear();
        emit changed( true );
    }
    updateButton();
}

void KCMFilter::removeFilter()
{
    for( int i = mListBox->count(); i >= 0; --i )
    {
        if (mListBox->isSelected(i))
            mListBox->removeItem(i);
    }
    mString->clear();
    emit changed( true );
    updateButton();
}

void KCMFilter::updateFilter()
{
    if ( !mString->text().isEmpty() )
    {
        int index = mListBox->currentItem();
        if ( index >= 0 )
        {
            mListBox->changeItem( mString->text(), index );
            emit changed( true );
        }
    }
    updateButton();
}

TQString KCMFilter::quickHelp() const
{
    return i18n("<h1>Konqueror AdBlocK</h1> Konqueror AdBlocK allows you to create a list of filters"
                " that are checked against linked images and frames. URL's that match are either discarded or"
                " replaced with a placeholder image. ");
}