summaryrefslogtreecommitdiffstats
path: root/plugins/upnp/upnpprefwidget.cpp
blob: bd4e593858ab426da9c017213b60a14407b2b55a (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
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@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 <klistview.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <torrent/udptrackersocket.h>
#include <torrent/globals.h>
#include <torrent/server.h>
#include <kademlia/dhtbase.h>
#include "upnpprefwidget.h"
#include <util/log.h>
#include <util/error.h>
#include <util/waitjob.h>
#include <util/httprequest.h>
#include <torrent/globals.h>
#include "upnppluginsettings.h"

using namespace bt;

namespace kt
{
	UPnPPrefWidget::UPnPPrefWidget(TQWidget* tqparent, const char* name, WFlags fl)
			: UPnPWidget(tqparent,name,fl)
	{
		def_router = 0;
		connect(m_forward_btn,TQT_SIGNAL(clicked()),this,TQT_SLOT(onForwardBtnClicked()));
		connect(m_undo_forward_btn,TQT_SIGNAL(clicked()),this,TQT_SLOT(onUndoForwardBtnClicked()));
		connect(m_rescan,TQT_SIGNAL(clicked()),this,TQT_SLOT(onRescanClicked()));
		bt::Globals::instance().getPortList().setListener(this);
	}
	
	UPnPPrefWidget::~UPnPPrefWidget()
	{
		bt::Globals::instance().getPortList().setListener(0);
	}
	
	void UPnPPrefWidget::shutdown(bt::WaitJob* job)
	{
		if (!def_router)
			return;
		
		net::PortList & pl = bt::Globals::instance().getPortList();
		if (pl.count() == 0)
			return;
		
		for (net::PortList::iterator i = pl.begin(); i != pl.end();i++)
		{
			net::Port & p = *i;
			if (p.forward)
				def_router->undoForward(p,job);
		}
	}
	
	
	void UPnPPrefWidget::addDevice(UPnPRouter* r)
	{
		connect(r,TQT_SIGNAL(updateGUI()),this,TQT_SLOT(updatePortMappings()));
		KListViewItem* item = new KListViewItem(m_device_list,r->getDescription().friendlyName);
		item->setMultiLinesEnabled(true);
		itemmap[item] = r;
		// if we have discovered the default device or there is none
		// forward it's ports
		TQString def_dev = UPnPPluginSettings::defaultDevice();
		if (def_dev == r->getServer() || def_dev.length() == 0)
		{
			Out(SYS_PNP|LOG_DEBUG) << "Doing default port mappings ..." << endl;
			UPnPPluginSettings::setDefaultDevice(r->getServer());
			UPnPPluginSettings::writeConfig();
			
			try
			{
				net::PortList & pl = bt::Globals::instance().getPortList();
			
				for (net::PortList::iterator i = pl.begin(); i != pl.end();i++)
				{
					net::Port & p = *i;
					if (p.forward)
						r->forward(p);
				}
				
				def_router = r;
			}
			catch (Error & e)
			{
				KMessageBox::error(this,e.toString());
			}
		}
	}
		
	void UPnPPrefWidget::onForwardBtnClicked()
	{
		KListViewItem* item = (KListViewItem*)m_device_list->currentItem();;
		if (!item)
			return;
		
		UPnPRouter* r = itemmap[item];
		if (!r)
			return;
		
		try
		{
			net::PortList & pl = bt::Globals::instance().getPortList();
			
			for (net::PortList::iterator i = pl.begin(); i != pl.end();i++)
			{
				net::Port & p = *i;
				if (p.forward)
					r->forward(p);
			}
			
			TQString def_dev = UPnPPluginSettings::defaultDevice();
			if (def_dev != r->getServer())
			{
				UPnPPluginSettings::setDefaultDevice(r->getServer());
				UPnPPluginSettings::writeConfig();
				def_router = r;
			}
			
		}
		catch (Error & e)
		{
			KMessageBox::error(this,e.toString());
		}	
	}
	
	void UPnPPrefWidget::onRescanClicked()
	{
		// clear the list and emit the signal
		rescan();
	}
	
	void UPnPPrefWidget::onUndoForwardBtnClicked()
	{
		KListViewItem* item = (KListViewItem*)m_device_list->currentItem();;
		if (!item)
			return;
		
		UPnPRouter* r =  itemmap[item];
		if (!r)
			return;
		
		try
		{
			net::PortList & pl = bt::Globals::instance().getPortList();
			
			for (net::PortList::iterator i = pl.begin(); i != pl.end();i++)
			{
				net::Port & p = *i;
				if (p.forward)
					r->undoForward(p,false);
			}
			
			TQString def_dev = UPnPPluginSettings::defaultDevice();
			if (def_dev == r->getServer())
			{
				UPnPPluginSettings::setDefaultDevice(TQString());
				UPnPPluginSettings::writeConfig();
				def_router = 0;
			}
		}
		catch (Error & e)
		{
			KMessageBox::error(this,e.toString());
		}
	}

	
	void UPnPPrefWidget::updatePortMappings()
	{
		// update all port mappings
		TQMap<KListViewItem*,UPnPRouter*>::iterator i = itemmap.begin();
		while (i != itemmap.end())
		{
			UPnPRouter* r = i.data();
			KListViewItem* item = i.key();
			TQString msg,services;
			TQValueList<UPnPRouter::Forwarding>::iterator j = r->beginPortMappings();
			while (j != r->endPortMappings())
			{
				UPnPRouter::Forwarding & f = *j;
				if (!f.pending_req)
				{
					msg += TQString::number(f.port.number) + " (";
					TQString prot = (f.port.proto == net::UDP ? "UDP" : "TCP");
					msg +=  prot + ")";
					if (f.service->servicetype.tqcontains("WANPPPConnection"))
						services += "PPP";
					else
						services += "IP";
				}
				j++;
				if (j != r->endPortMappings())
				{
					msg += "\n";
					services += "\n";
				}
			}
			item->setText(1,msg);
			item->setText(2,services);
			i++;
		}
	}
	

	void UPnPPrefWidget::portAdded(const net::Port & port)
	{
		try
		{
			if (def_router && port.forward)
				def_router->forward(port);
		}
		catch (Error & e)
		{
			Out(SYS_PNP|LOG_DEBUG) << "Error : " << e.toString() << endl;
		}
	}
	
	void UPnPPrefWidget::portRemoved(const net::Port & port)
	{
		try
		{
			if (def_router && port.forward)
				def_router->undoForward(port,false);
		}
		catch (Error & e)
		{
			Out(SYS_PNP|LOG_DEBUG) << "Error : " << e.toString() << endl;
		}
	}
}



#include "upnpprefwidget.moc"