1 /*
2  * File    : TorrentAnnounceURLListImpl.java
3  * Created : 03-Mar-2004
4  * By      : parg
5  *
6  * Azureus - a Java Bittorrent client
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details ( see the LICENSE file ).
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 package org.gudy.azureus2.pluginsimpl.local.torrent;
24 
25 /**
26  * @author parg
27  *
28  */
29 
30 import java.net.URL;
31 
32 import org.gudy.azureus2.core3.torrent.*;
33 import org.gudy.azureus2.core3.util.*;
34 import org.gudy.azureus2.plugins.torrent.*;
35 
36 public class
37 TorrentAnnounceURLListImpl
38 	implements TorrentAnnounceURLList
39 {
40 	protected TorrentImpl		torrent;
41 
42 	protected
TorrentAnnounceURLListImpl( TorrentImpl _torrent )43 	TorrentAnnounceURLListImpl(
44 		TorrentImpl	_torrent )
45 	{
46 		torrent	= _torrent;
47 	}
48 
49 	public TorrentAnnounceURLListSet[]
getSets()50 	getSets()
51 	{
52 		TOTorrentAnnounceURLGroup	group = torrent.getTorrent().getAnnounceURLGroup();
53 
54 		TOTorrentAnnounceURLSet[]	sets = group.getAnnounceURLSets();
55 
56 		TorrentAnnounceURLListSet[]	res = new TorrentAnnounceURLListSet[sets.length];
57 
58 		for (int i=0;i<res.length;i++){
59 
60 			res[i] = new TorrentAnnounceURLListSetImpl( this, sets[i]);
61 		}
62 
63 		return( res );
64 	}
65 
66 	public void
setSets( TorrentAnnounceURLListSet[] sets )67 	setSets(
68 		TorrentAnnounceURLListSet[]		sets )
69 	{
70 		TOTorrentAnnounceURLGroup	group = torrent.getTorrent().getAnnounceURLGroup();
71 
72 		TOTorrentAnnounceURLSet[]	res = new TOTorrentAnnounceURLSet[sets.length];
73 
74 		for (int i=0;i<res.length;i++){
75 
76 			res[i] = ((TorrentAnnounceURLListSetImpl)sets[i]).getSet();
77 		}
78 
79 		group.setAnnounceURLSets( res );
80 
81 		updated();
82 	}
83 
84 	public TorrentAnnounceURLListSet
create( URL[] urls )85 	create(
86 		URL[]		urls )
87 	{
88 		return( new TorrentAnnounceURLListSetImpl( this, torrent.getTorrent().getAnnounceURLGroup().createAnnounceURLSet(urls)));
89 	}
90 
91 	public void
addSet( URL[] urls )92 	addSet(
93 		URL[]		urls )
94 	{
95 		if ( setAlreadyExists( urls )){
96 
97 			return;
98 		}
99 
100 		TorrentUtils.announceGroupsInsertLast( torrent.getTorrent(), urls );
101 
102 		updated();
103 	}
104 
105 	public void
insertSetAtFront( URL[] urls )106 	insertSetAtFront(
107 		URL[]		urls )
108 	{
109 		if ( setAlreadyExists( urls )){
110 
111 			return;
112 		}
113 
114 		TorrentUtils.announceGroupsInsertFirst( torrent.getTorrent(), urls );
115 
116 		updated();
117 	}
118 
119 	protected boolean
setAlreadyExists( URL[] urls )120 	setAlreadyExists(
121 		URL[]		urls )
122 	{
123 		TOTorrentAnnounceURLGroup	group = torrent.getTorrent().getAnnounceURLGroup();
124 
125 		TOTorrentAnnounceURLSet[]	sets = group.getAnnounceURLSets();
126 
127 		for (int i=0;i<sets.length;i++){
128 
129 			URL[]	u = sets[i].getAnnounceURLs();
130 
131 			if ( u.length != urls.length ){
132 
133 				continue;
134 			}
135 
136 			boolean	all_found = true;
137 
138 			for (int j=0;j<urls.length;j++){
139 
140 				URL	u1 = urls[j];
141 
142 				boolean	this_found = false;
143 
144 				for ( int k=0;k<u.length;k++){
145 
146 					URL	u2 = u[k];
147 
148 					if ( u1.toString().equals( u2.toString())){
149 
150 						this_found = true;
151 
152 						break;
153 					}
154 				}
155 
156 				if ( !this_found ){
157 
158 					all_found = false;
159 
160 					break;
161 				}
162 			}
163 
164 			if ( all_found ){
165 
166 				return( true );
167 			}
168 		}
169 
170 		return( false );
171 	}
172 
173 	protected void
updated()174 	updated()
175 	{
176 		torrent.updated();
177 	}
178 }
179