summaryrefslogtreecommitdiffstats
path: root/src/upnpmanager.cpp
blob: 8c95f151add5436be38ee3d1b420b767d6e3ceb1 (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
/***************************************************************************
 * $Id: upnpmanager.cpp,v 1.5 2008/04/14 11:15:53 hoganrobert Exp $
 *   Copyright (C) 2008 by Robert Hogan                                    *
 *   robert@roberthogan.net                                                *
 *                                                                         *
 *   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 <tdemessagebox.h>

#include "upnpmanager.h"
#include "kdebug.h"

using namespace bt;

UPnPManager* UPnPManager::manager = 0;

    UPnPManager* UPnPManager::Manager () 
    {
      if (manager == 0)
      {  
        manager = new UPnPManager; 
      }
      return manager; 
    }

	UPnPManager::UPnPManager(TQWidget* parent, const char* name)
                 : TQWidget(parent, name)
	{
		sock = new UPnPMCastSocket();
        connect(sock,SIGNAL(discovered( kt::UPnPRouter* )),
                this,SLOT(discovered( kt::UPnPRouter* )));
        discover();
	}
	
	UPnPManager::~UPnPManager()
	{
		delete sock;

	}
	
// 	void UPnPManager::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 UPnPManager::discover()
	{
        sock->discover();
    }	
	
	void UPnPManager::forward(const net::ForwardPortList &forwardPorts, 
                              bool force, bool silent)
	{
        m_silent = silent;
		TQValueList<kt::UPnPRouter*>::iterator r = discoveredRouters.begin();
		while (r != discoveredRouters.end())
		{
            disconnect((*r),SIGNAL(replyError(kt::UPnPRouter*,bt::HTTPRequest* ,const TQString&, bool )),
                this,SLOT(onReplyError(kt::UPnPRouter*,bt::HTTPRequest* ,const TQString&, bool )));
            disconnect((*r),SIGNAL(replyOK(kt::UPnPRouter*,bt::HTTPRequest* ,const TQString&, bool )),
                this,SLOT(onReplyOK(kt::UPnPRouter*,bt::HTTPRequest* ,const TQString&, bool )));

            connect((*r),SIGNAL(replyError(kt::UPnPRouter*,bt::HTTPRequest* ,const TQString&, bool )),
                    this,SLOT(onReplyError(kt::UPnPRouter*,bt::HTTPRequest* ,const TQString&, bool )));
            connect((*r),SIGNAL(replyOK(kt::UPnPRouter*,bt::HTTPRequest* ,const TQString&, bool )),
                    this,SLOT(onReplyOK(kt::UPnPRouter*,bt::HTTPRequest* ,const TQString&, bool )));

            for (net::ForwardPortList::ConstIterator i = forwardPorts.begin();
                 i != forwardPorts.end();i++)
            {
                const net::ForwardPort & s = *i;

/*                (*r)->undoForward(net::Port(s.extnumber,s.proto,s.forward),false);*/
                (*r)->forward(net::Port(s.extnumber,s.proto,s.forward),
                              net::Port(s.intnumber,s.proto,s.forward),
                              force);
            }
            r++;
		}

		kdDebug()  << "Done" << endl;
    }

    void UPnPManager::onReplyError(kt::UPnPRouter* router,bt::HTTPRequest* req,const TQString&, bool fwd )
    {

        emit forwardingError(router,req->showPayload(),fwd);
    }

    void UPnPManager::onReplyOK(kt::UPnPRouter* router,bt::HTTPRequest* req,const TQString&, bool fwd )
    {

        kdDebug() << "forwarding OK" << endl;
        emit forwardingOK(router,req->showPayload(),fwd);
    }

	void UPnPManager::undoForward(const net::ForwardPortList &forwardPorts,
                                  bool silent)
	{
        m_silent = silent;
		TQValueList<kt::UPnPRouter*>::iterator r = discoveredRouters.begin();
		while (r != discoveredRouters.end())
		{
            for (net::ForwardPortList::ConstIterator i = forwardPorts.begin();i != forwardPorts.end();i++)
            {
                const net::ForwardPort & s = *i;
                (*r)->undoForward(net::Port(s.extnumber,s.proto,s.forward),
                                  net::Port(s.intnumber,s.proto,s.forward), 0);
            }
            r++;
		}

		kdDebug()  << "Done" << endl;
    }

	TQStringList UPnPManager::discoveredRoutersNameList()
	{
        TQStringList routers;
		TQValueList<kt::UPnPRouter*>::iterator r = discoveredRouters.begin();
		while (r != discoveredRouters.end())
		{
            routers.append((*r)->getDescription().friendlyName);
            r++;
		}
        return routers;
    }

	void UPnPManager::discovered(UPnPRouter* r)
	{
        discoveredRouters.append(r);
        emit routerDiscovered(r);
    }


	void UPnPManager::unload()
	{
/*		TQString routers_file = TDEGlobal::dirs()->saveLocation("data","tork") + "routers";
		sock->saveRouters(routers_file);*/
		sock->close();
		delete sock;
		sock = 0;
	}
	

#include "upnpmanager.moc"