summaryrefslogtreecommitdiffstats
path: root/plugins/ipfilter/ipblockingprefpage.cpp
blob: d68651a535a4992b3b3b5562c379262a5a58b9e5 (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
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   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 "ipblockingprefpage.h"
#include "ipblockingpref.h"
#include "ipfilterpluginsettings.h"
#include "ipfilterplugin.h"
#include "convertdialog.h"

#include <tdeapplication.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kurlrequester.h>
#include <kurl.h>
#include <kmessagebox.h>
#include <tdeio/netaccess.h>
#include <kprogress.h>
#include <kmimetype.h>

#include <util/log.h>
#include <torrent/globals.h>
#include <interfaces/coreinterface.h>

#include <tqthread.h>
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqregexp.h>
#include <tqvalidator.h>
#include <tqlayout.h>
#include <tqdialog.h>
#include <tqobject.h>

using namespace bt;

#define MAX_RANGES 500

namespace kt
{
	IPBlockingPrefPageWidget::IPBlockingPrefPageWidget(TQWidget* parent) : IPBlockingPref(parent)
	{
		m_url->setURL(IPBlockingPluginSettings::filterURL());
		if (m_url->url() == "")
			m_url->setURL(TQString("http://www.bluetack.co.uk/config/splist.zip"));
		
		bool use_level1 = IPBlockingPluginSettings::useLevel1();

		checkUseLevel1->setChecked(use_level1);
		
		if(use_level1)
		{
			lbl_status1->setText(i18n("Status: Loaded and running."));
			m_url->setEnabled(true);
			btnDownload->setEnabled(true);
		}
		else
		{
			lbl_status1->setText(i18n("Status: Not loaded."));
			m_url->setEnabled(false);
			btnDownload->setEnabled(false);
		}
		
		m_plugin = 0;
	}

	void IPBlockingPrefPageWidget::apply()
	{
		IPBlockingPluginSettings::setFilterURL(m_url->url());
		IPBlockingPluginSettings::setUseLevel1(checkUseLevel1->isChecked());
		IPBlockingPluginSettings::writeConfig();
		
		if(checkUseLevel1->isChecked())
		{
			TQFile target(TDEGlobal::dirs()->saveLocation("data","ktorrent") + "level1.dat");
			if(target.exists())
				lbl_status1->setText(i18n("Status: Loaded and running."));
			else
				lbl_status1->setText(i18n("Status: <font color=\"#ff0000\">Filter file not found.</font> Download and convert filter file."));
		}
		else
			lbl_status1->setText(i18n("Status: Not loaded."));
	}

	void IPBlockingPrefPageWidget::btnDownload_clicked()
	{
		TQString target(TDEGlobal::dirs()->saveLocation("data","ktorrent") + "level1");
		TQFile target_file(target);
		TQFile txtfile(target + ".txt");
		KURL url(m_url->url());
		KURL dest(target);
		KURL temp(TDEGlobal::dirs()->saveLocation("data","ktorrent") + "level1.tmp");
		if(TDEIO::NetAccess::exists(temp,false, this))
			TDEIO::NetAccess::del(temp,this);

		bool download = true;

		if(txtfile.exists())
		{
			if((KMessageBox::questionYesNo(this, i18n("Selected file already exists, do you want to download it again?"),i18n("File Exists")) == 4))
				download = false;
			else
				TDEIO::NetAccess::move(target, temp);
		}

		if(download)
		{
			if(!url.isLocalFile())
			{
				if (TDEIO::NetAccess::download(url,target,NULL))
				{
					//Level1 list successfully downloaded, remove temporary file
					TDEIO::NetAccess::removeTempFile(target);
					TDEIO::NetAccess::del(temp, this);
				}
				else
				{
					TQString err = TDEIO::NetAccess::lastErrorString();
					if(err != TQString())
						KMessageBox::error(0,TDEIO::NetAccess::lastErrorString(),i18n("Error"));
					else
						TDEIO::NetAccess::move(temp, target);
					
					
					//we don't want to convert since download failed
					return;
				}
			}
			else
			{
				if (!TDEIO::NetAccess::file_copy(url,dest, -1, true))
				{
					KMessageBox::error(0,TDEIO::NetAccess::lastErrorString(),i18n("Error"));
					return;
				}
			}
			
			//now determine if it's ZIP or TXT file
			KMimeType::Ptr ptr = KMimeType::findByPath(target);
			if(ptr->name() == "application/x-zip")
			{
				KURL zipfile("zip:" + target + "/splist.txt");
				KURL destinationfile(target + ".txt");
				TDEIO::NetAccess::file_copy(zipfile,destinationfile, -1, true);
			}
			else
			{
				KURL zipfile(target);
				KURL destinationfile(target + ".txt");
				TDEIO::NetAccess::file_copy(zipfile,destinationfile, -1, true);
			}
			
		}
		convert();
	}
	
	void IPBlockingPrefPageWidget::checkUseLevel1_toggled(bool check)
	{
		if(check)
		{
			m_url->setEnabled(true);
			btnDownload->setEnabled(true);
		}
		else
		{
			lbl_status1->setText("");
			m_url->setEnabled(false);
			btnDownload->setEnabled(false);
		}
	}
	
	void IPBlockingPrefPageWidget::convert()
	{
		TQFile target(TDEGlobal::dirs()->saveLocation("data","ktorrent") + "level1.dat");
		if(target.exists())
		{
			if((KMessageBox::questionYesNo(this,i18n("Filter file (level1.dat) already exists, do you want to convert it again?"),i18n("File Exists")) == 4))
				return;
// 			else
// 				TDEIO::NetAccess::del(TDEGlobal::dirs()->saveLocation("data","ktorrent") + "level1.dat", NULL);
		}
		ConvertDialog dlg(m_plugin);
		dlg.exec();
	}
	
	void IPBlockingPrefPageWidget::setPlugin(IPFilterPlugin* p)
	{
		m_plugin = p;
	}
	
	void IPBlockingPrefPageWidget::setPrefPage( IPBlockingPrefPage * p )
	{
		m_prefpage = p;
	}
	
	void IPBlockingPrefPageWidget::setConverting(bool enable)
	{
		btnDownload->setEnabled(enable);
		lbl_status1->setText("");
	}
	
	
	////////////////////////////////////////////////////////////////////////////////////

	IPBlockingPrefPage::IPBlockingPrefPage(CoreInterface* core, IPFilterPlugin* p)
	: PrefPageInterface(i18n("IPBlocking Filter"), i18n("IPBlocking Filter Options"), TDEGlobal::iconLoader()->loadIcon("filter",TDEIcon::NoGroup)), m_core(core), m_plugin(p)
	{
		widget = 0;
	}

	IPBlockingPrefPage::~IPBlockingPrefPage()
	{}

	bool IPBlockingPrefPage::apply()
	{
		widget->apply();
		
		if(IPBlockingPluginSettings::useLevel1())
			m_plugin->loadAntiP2P();
		else
			m_plugin->unloadAntiP2P();
		
		return true;
	}
	
	void IPBlockingPrefPage::createWidget(TQWidget* parent)
	{
		widget = new IPBlockingPrefPageWidget(parent);
		widget->setPlugin(m_plugin);
		widget->setPrefPage(this);
	}

	void IPBlockingPrefPage::deleteWidget()
	{
		delete widget;
		widget = 0;
	}

	void IPBlockingPrefPage::updateData()
	{}
}