~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to ktorrent/ipfilterwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-02-16 18:37:14 UTC
  • mfrom: (1.1.25 upstream) (0.4.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090216183714-52tf47jrnmk4xkmp
Tags: 3.2+dfsg.1-2ubuntu1
* Merge with Debian, remaining changes: (LP: #296433)
  - Use Kubuntu's kde4.mk
  - Build-depend on libboost-serialization1.35-dev since unversioned -dev is
    in universe
  - Change plasma-applet-ktorrent to plasma-widget-ktorrent since we're
    supposed to call them widgets for the users

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 ***************************************************************************/
20
20
#include "ipfilterwidget.h"
21
21
 
22
 
#include <torrent/ipblocklist.h>
 
22
#include <peer/accessmanager.h>
23
23
#include <torrent/globals.h>
24
24
#include <util/log.h>
 
25
#include <util/error.h>
25
26
#include <util/constants.h>
 
27
#include <interfaces/functions.h>
26
28
 
27
29
#include <QtGui>
28
30
#include <QtCore>
31
33
#include <KFileDialog>
32
34
#include <KUrl>
33
35
 
34
 
/*#include <ksocketaddress.h>
35
 
#include <kfiledialog.h>
36
 
#include <klocale.h>
37
 
#include <kmessagebox.h>*/
 
36
#include "ipfilterlist.h"
38
37
 
39
38
#define MAX_RANGES 500
40
39
 
41
40
using namespace bt;
42
41
 
43
 
IPFilterWidget::IPFilterWidget ( QWidget* parent, Qt::WFlags fl )
44
 
                :QDialog ( parent, fl )
45
 
{
46
 
        setupUi ( this );
47
 
        btnAdd->setGuiItem(KStandardGuiItem::add());
48
 
        btnOk->setGuiItem(KStandardGuiItem::ok());
49
 
        btnApply->setGuiItem(KStandardGuiItem::apply());
50
 
        btnClear->setGuiItem(KStandardGuiItem::clear());
51
 
        btnSave->setGuiItem(KStandardGuiItem::saveAs());
52
 
        btnOpen->setGuiItem(KStandardGuiItem::open());
53
 
        btnRemove->setGuiItem(KStandardGuiItem::remove());
54
 
        btnCancel->setGuiItem(KStandardGuiItem::close());
55
 
 
56
 
        IPBlocklist& ipfilter = IPBlocklist::instance();
57
 
        QStringList* blocklist = ipfilter.getBlocklist();
58
 
 
59
 
        for ( QStringList::Iterator it = blocklist->begin(); it != blocklist->end(); ++it )
60
 
        {
61
 
                ( new QListWidgetItem ( lstPeers ) )->setText ( *it );
62
 
        }
63
 
 
64
 
        delete blocklist;
65
 
 
66
 
        setupConnections();
67
 
}
68
 
 
69
 
void IPFilterWidget::setupConnections()
70
 
{
71
 
        connect(btnAdd, SIGNAL(clicked()), this, SLOT(btnAdd_clicked()));
72
 
        connect(btnClear, SIGNAL(clicked()), this, SLOT(btnClear_clicked()));
73
 
        connect(btnApply, SIGNAL(clicked()), this, SLOT(btnApply_clicked()));
74
 
        connect(btnOk, SIGNAL(clicked()), this, SLOT(btnOk_clicked()));
75
 
        connect(btnSave, SIGNAL(clicked()), this, SLOT(btnSave_clicked()));
76
 
        connect(btnOpen, SIGNAL(clicked()), this, SLOT(btnOpen_clicked()));
77
 
        connect(btnRemove, SIGNAL(clicked()), this, SLOT(btnRemove_clicked())); 
78
 
        connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
79
 
}
80
 
 
81
 
void IPFilterWidget::btnAdd_clicked()
82
 
{
83
 
        int var=0;
84
 
 
85
 
        QRegExp rx ( "([*]|[0-9]{1,3}).([*]|[0-9]{1,3}).([*]|[0-9]{1,3}).([*]|[0-9]{1,3})" );
86
 
        QRegExpValidator v ( rx,0 );
87
 
 
88
 
        QString ip = peerIP->text();
89
 
 
90
 
        if ( v.validate ( ip, var ) == QValidator::Acceptable )
91
 
        {
92
 
                if ( lstPeers->findItems ( ip, 0 ).empty() )
93
 
                        ( new QListWidgetItem ( lstPeers ) )->setText ( ip );
94
 
        }
95
 
        else
96
 
                KMessageBox::sorry ( 0, i18n ( "You must enter IP in format 'XXX.XXX.XXX.XXX'. You can also use wildcards for ranges like '127.0.0.*'." ) );
97
 
}
98
 
 
99
 
void IPFilterWidget::btnRemove_clicked()
100
 
{
101
 
        if ( lstPeers->currentItem() )
102
 
                delete lstPeers->currentItem();
103
 
}
104
 
 
105
 
void IPFilterWidget::btnClear_clicked()
106
 
{
107
 
        lstPeers->clear();
108
 
}
109
 
 
110
 
void IPFilterWidget::btnOpen_clicked()
111
 
{
112
 
        QString lf = KFileDialog::getOpenFileName ( KUrl("kfiledialog:///openTorrent"), "*.txt|",this,i18n ( "Choose a file" ) );
113
 
 
114
 
        if ( lf.isEmpty() )
115
 
                return;
116
 
 
117
 
        btnClear_clicked();
118
 
 
119
 
        loadFilter ( lf );
120
 
}
121
 
 
122
 
void IPFilterWidget::btnSave_clicked()
123
 
{
124
 
        QString sf = KFileDialog::getSaveFileName ( KUrl("kfiledialog:///openTorrent"),"*.txt|",this,i18n ( "Choose a filename to save under" ) );
125
 
 
126
 
        if ( sf.isEmpty() )
127
 
                return;
128
 
 
129
 
        saveFilter ( sf );
130
 
}
131
 
 
132
 
void IPFilterWidget::btnOk_clicked()
133
 
{
134
 
        btnApply_clicked();
135
 
        this->accept();
136
 
}
137
 
 
138
 
void IPFilterWidget::btnApply_clicked()
139
 
{
140
 
        IPBlocklist& ipfilter = IPBlocklist::instance();
141
 
 
142
 
        QStringList* peers = new QStringList();
143
 
 
144
 
        for ( int i=0; i<lstPeers->count(); ++i )
145
 
        {
146
 
                *peers << lstPeers->item ( i )->text();
147
 
        }
148
 
 
149
 
        ipfilter.setBlocklist ( peers );
150
 
 
151
 
        delete peers;
152
 
 
153
 
        Out ( SYS_IPF|LOG_NOTICE ) << "Loaded " << lstPeers->count() << " blocked IP ranges." << endl;
154
 
}
155
 
 
156
 
void IPFilterWidget::saveFilter ( QString& fn )
157
 
{
158
 
        QFile fptr ( fn );
159
 
 
160
 
        if ( !fptr.open ( QIODevice::WriteOnly ) )
161
 
        {
162
 
                Out ( SYS_GEN|LOG_NOTICE ) << QString ( "Could not open file %1 for writing." ).arg ( fn ) << endl;
163
 
                return;
164
 
        }
165
 
 
166
 
        QTextStream out ( &fptr );
167
 
 
168
 
        for ( int i=0; i<lstPeers->count(); ++i )
169
 
        {
170
 
                out << lstPeers->item ( i )->text() << ::endl;
171
 
        }
172
 
 
173
 
        fptr.close();
174
 
}
175
 
 
176
 
void IPFilterWidget::loadFilter ( QString& fn )
177
 
{
178
 
        QFile dat ( fn );
179
 
        dat.open ( QIODevice::ReadOnly );
180
 
 
181
 
        QTextStream stream ( &dat );
182
 
        QString line;
183
 
 
184
 
        QRegExp rx ( "([*]|[0-9]{1,3}).([*]|[0-9]{1,3}).([*]|[0-9]{1,3}).([*]|[0-9]{1,3})" );
185
 
        QRegExpValidator v ( rx,0 );
186
 
 
187
 
 
188
 
        int i=0;
189
 
        int var=0;
190
 
        bool err = false;
191
 
 
192
 
        while ( !stream.atEnd() && i < MAX_RANGES )
193
 
        {
194
 
                line = stream.readLine();
195
 
                if ( v.validate ( line, var ) != QValidator::Acceptable )
196
 
                {
197
 
                        err = true;
198
 
                        continue;
199
 
                }
200
 
 
201
 
                ( new QListWidgetItem ( lstPeers ) )->setText ( line );
202
 
                ++i;
203
 
        }
204
 
 
205
 
        if ( err )
206
 
                Out ( SYS_IPF|LOG_NOTICE ) << "Some lines could not be loaded. Check your filter file..." << endl;
207
 
 
208
 
        dat.close();
209
 
}
210
 
 
 
42
namespace kt
 
43
{
 
44
        
 
45
        IPFilterList* IPFilterWidget::filter_list = 0;
 
46
        
 
47
        
 
48
        IPFilterWidget::IPFilterWidget(QWidget* parent)
 
49
                        : KDialog(parent)
 
50
        {
 
51
                setupUi(mainWidget());
 
52
                setButtons(KDialog::None);
 
53
                setCaption(i18n("IP Filter List"));
 
54
                
 
55
                m_add->setGuiItem(KStandardGuiItem::add());
 
56
                m_clear->setGuiItem(KStandardGuiItem::clear());
 
57
                m_save_as->setGuiItem(KStandardGuiItem::saveAs());
 
58
                m_open->setGuiItem(KStandardGuiItem::open());
 
59
                m_remove->setGuiItem(KStandardGuiItem::remove());
 
60
                m_close->setGuiItem(KStandardGuiItem::close());
 
61
        
 
62
                if (!filter_list)
 
63
                {
 
64
                        filter_list = new IPFilterList();
 
65
                        AccessManager::instance().addBlockList(filter_list);
 
66
                        loadFilter(kt::DataDir() + "ip_filter");
 
67
                }
 
68
                
 
69
                m_ip_list->setModel(filter_list);
 
70
                m_ip_list->setSelectionMode(QAbstractItemView::ContiguousSelection);
 
71
        
 
72
                setupConnections();
 
73
        }
 
74
        
 
75
        IPFilterWidget::~IPFilterWidget()
 
76
        {
 
77
        }
 
78
        
 
79
        void IPFilterWidget::setupConnections()
 
80
        {
 
81
                connect(m_add, SIGNAL(clicked()), this, SLOT(add()));
 
82
                connect(m_close, SIGNAL(clicked()), this, SLOT(accept()));
 
83
                connect(m_clear, SIGNAL(clicked()), this, SLOT(clear()));
 
84
                connect(m_save_as, SIGNAL(clicked()), this, SLOT(save()));
 
85
                connect(m_open, SIGNAL(clicked()), this, SLOT(open()));
 
86
                connect(m_remove, SIGNAL(clicked()), this, SLOT(remove()));     
 
87
                connect(this, SIGNAL(closeClicked()), this, SLOT(reject()));
 
88
        }
 
89
        
 
90
        void IPFilterWidget::add()
 
91
        {
 
92
                int var=0;
 
93
        
 
94
                QRegExp rx ( "([*]|[0-9]{1,3}).([*]|[0-9]{1,3}).([*]|[0-9]{1,3}).([*]|[0-9]{1,3})" );
 
95
                QRegExpValidator v ( rx,0 );
 
96
        
 
97
                QString ip = m_ip_to_add->text();
 
98
        
 
99
                if (v.validate(ip, var) != QValidator::Acceptable)
 
100
                {
 
101
                        KMessageBox::sorry(this, i18n ("Invalid IP address %1. You must enter an IP address in the format 'XXX.XXX.XXX.XXX'."
 
102
                                        " You can also use wildcards for ranges like '127.0.0.*'.",ip ) );
 
103
                        return;
 
104
                }
 
105
                
 
106
                try
 
107
                {
 
108
                        filter_list->add(ip);
 
109
                }
 
110
                catch (bt::Error & err)
 
111
                {
 
112
                        KMessageBox::sorry(this,err.toString());
 
113
                }
 
114
        }
 
115
        
 
116
        void IPFilterWidget::remove()
 
117
        {
 
118
                QModelIndexList idx = m_ip_list->selectionModel()->selectedRows();
 
119
                if (idx.count() == 0)
 
120
                        return;
 
121
                
 
122
                filter_list->remove(idx.at(0).row(),idx.count());
 
123
        }
 
124
        
 
125
        void IPFilterWidget::clear()
 
126
        {
 
127
                filter_list->clear();
 
128
        }
 
129
        
 
130
        void IPFilterWidget::open()
 
131
        {
 
132
                QString lf = KFileDialog::getOpenFileName ( KUrl("kfiledialog:///openTorrent"), "*.txt|",this,i18n ( "Choose a file" ) );
 
133
        
 
134
                if (lf.isEmpty())
 
135
                        return;
 
136
        
 
137
                clear();
 
138
        
 
139
                loadFilter(lf);
 
140
        }
 
141
        
 
142
        void IPFilterWidget::save()
 
143
        {
 
144
                QString sf = KFileDialog::getSaveFileName ( KUrl("kfiledialog:///openTorrent"),"*.txt|",this,i18n ( "Choose a filename to save under" ) );
 
145
        
 
146
                if ( sf.isEmpty() )
 
147
                        return;
 
148
        
 
149
                saveFilter(sf);
 
150
        }
 
151
        
 
152
        void IPFilterWidget::accept()
 
153
        {
 
154
                saveFilter(kt::DataDir() + "ip_filter");
 
155
                KDialog::accept();
 
156
        }
 
157
        
 
158
        void IPFilterWidget::saveFilter(const QString & fn)
 
159
        {
 
160
                QFile fptr ( fn );
 
161
        
 
162
                if ( !fptr.open ( QIODevice::WriteOnly ) )
 
163
                {
 
164
                        Out ( SYS_GEN|LOG_NOTICE ) << QString ( "Could not open file %1 for writing." ).arg ( fn ) << endl;
 
165
                        return;
 
166
                }
 
167
        
 
168
                QTextStream out(&fptr);
 
169
        
 
170
                for (int i = 0; i < filter_list->rowCount(); ++i)
 
171
                {
 
172
                        out << filter_list->data(filter_list->index(i,0),Qt::DisplayRole).toString() << ::endl;
 
173
                }
 
174
        
 
175
                fptr.close();
 
176
        }
 
177
        
 
178
        void IPFilterWidget::loadFilter(const QString & fn)
 
179
        {
 
180
                QFile dat(fn);
 
181
                dat.open(QIODevice::ReadOnly);
 
182
        
 
183
                QTextStream stream(&dat);
 
184
                QString line;
 
185
        
 
186
                QRegExp rx("([*]|[0-9]{1,3}).([*]|[0-9]{1,3}).([*]|[0-9]{1,3}).([*]|[0-9]{1,3})");
 
187
                QRegExpValidator v(rx,0);
 
188
        
 
189
                bool err = false;
 
190
                int pos = 0;
 
191
                
 
192
                while (!stream.atEnd())
 
193
                {
 
194
                        line = stream.readLine();
 
195
                        if (v.validate(line,pos) != QValidator::Acceptable)
 
196
                        {
 
197
                                err = true;
 
198
                        }
 
199
                        else
 
200
                        {
 
201
                                try 
 
202
                                {
 
203
                                        filter_list->add(line);
 
204
                                }
 
205
                                catch (...)
 
206
                                {
 
207
                                        err = true;
 
208
                                }
 
209
                        }
 
210
                }
 
211
        
 
212
                if (err)
 
213
                        Out(SYS_IPF|LOG_NOTICE) << "Some lines could not be loaded. Check your filter file..." << endl;
 
214
        
 
215
                dat.close();
 
216
        }
 
217
}
211
218
#include "ipfilterwidget.moc"