1 /*
2  * Created on Nov 5, 2014
3  * Created by Paul Gardner
4  *
5  * Copyright 2014 Azureus Software, Inc.  All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 
23 package com.aelitis.azureus.plugins.net.buddy;
24 
25 import java.util.Map;
26 
27 import org.gudy.azureus2.core3.torrent.TOTorrent;
28 import org.gudy.azureus2.core3.util.AENetworkClassifier;
29 import org.gudy.azureus2.core3.util.AERunnable;
30 import org.gudy.azureus2.core3.util.AEThread2;
31 import org.gudy.azureus2.core3.util.AsyncDispatcher;
32 import org.gudy.azureus2.core3.util.ByteFormatter;
33 import org.gudy.azureus2.core3.util.Debug;
34 import org.gudy.azureus2.plugins.PluginInterface;
35 import org.gudy.azureus2.plugins.download.Download;
36 import org.gudy.azureus2.plugins.torrent.Torrent;
37 import org.gudy.azureus2.pluginsimpl.local.PluginCoreUtils;
38 
39 import com.aelitis.azureus.core.AzureusCoreFactory;
40 import com.aelitis.azureus.plugins.net.buddy.BuddyPluginBeta.ChatInstance;
41 import com.aelitis.azureus.plugins.net.buddy.BuddyPluginViewInterface.ViewListener;
42 
43 
44 public class
45 BuddyPluginUtils
46 {
47 	private static BuddyPlugin
getPlugin()48 	getPlugin()
49 	{
50 		PluginInterface pi = AzureusCoreFactory.getSingleton().getPluginManager().getPluginInterfaceByID( "azbuddy", true );
51 
52 		if ( pi != null ){
53 
54 			return((BuddyPlugin)pi.getPlugin());
55 		}
56 
57 		return( null );
58 	}
59 
60 	public static BuddyPluginBeta
getBetaPlugin()61 	getBetaPlugin()
62 	{
63 		BuddyPlugin bp = getPlugin();
64 
65 		if ( bp != null && bp.isBetaEnabled()){
66 
67 			BuddyPluginBeta beta = bp.getBeta();
68 
69 			if ( beta.isAvailable()){
70 
71 				return( beta );
72 			}
73 		}
74 
75 		return( null );
76 	}
77 
78 	public static boolean
isBetaChatAvailable()79 	isBetaChatAvailable()
80 	{
81 		BuddyPlugin bp = getPlugin();
82 
83 		if ( bp != null && bp.isBetaEnabled()){
84 
85 			return( bp.getBeta().isAvailable());
86 		}
87 
88 		return( false );
89 	}
90 
91 	public static boolean
isBetaChatAnonAvailable()92 	isBetaChatAnonAvailable()
93 	{
94 		BuddyPlugin bp = getPlugin();
95 
96 		if ( bp != null && bp.isBetaEnabled()){
97 
98 			return( bp.getBeta().isAvailable() && bp.getBeta().isI2PAvailable());
99 		}
100 
101 		return( false );
102 	}
103 
104 	public static void
createBetaChat( final String network, final String key, final Runnable callback )105 	createBetaChat(
106 		final String		network,
107 		final String		key,
108 		final Runnable		callback )
109 	{
110 		new AEThread2( "Chat create async" )
111 		{
112 			public void
113 			run()
114 			{
115 				try{
116 					BuddyPlugin bp = getPlugin();
117 
118 					bp.getBeta().getAndShowChat( network, key );
119 
120 				}catch( Throwable e ){
121 
122 					Debug.out( e );
123 
124 				}finally{
125 
126 					if ( callback != null ){
127 
128 						callback.run();
129 					}
130 				}
131 			}
132 		}.start();
133 	}
134 
135 	public static Map<String,Object>
peekChat( String net, String key )136 	peekChat(
137 		String		net,
138 		String		key )
139 	{
140 		BuddyPlugin bp = getPlugin();
141 
142 		if ( bp != null && bp.isBetaEnabled()){
143 
144 			return( bp.getBeta().peekChat( net, key ));
145 		}
146 
147 		return( null );
148 	}
149 
150 	public static Map<String,Object>
peekChat( Download download )151 	peekChat(
152 		Download		download )
153 	{
154 		BuddyPlugin bp = getPlugin();
155 
156 		if ( bp != null && bp.isBetaEnabled()){
157 
158 			return( bp.getBeta().peekChat( download, false ));
159 		}
160 
161 		return( null );
162 	}
163 
164 	private static AsyncDispatcher peek_dispatcher = new AsyncDispatcher( "peeker" );
165 
166 	public static void
peekChatAsync( final String net, final String key, final Runnable done )167 	peekChatAsync(
168 		final String		net,
169 		final String		key,
170 		final Runnable		done )
171 	{
172 		boolean	async = false;
173 
174 		try{
175 			if ( isBetaChatAvailable()){
176 
177 				if ( net != AENetworkClassifier.AT_PUBLIC && !isBetaChatAnonAvailable()){
178 
179 					return;
180 				}
181 
182 				if ( peek_dispatcher.getQueueSize() > 200 ){
183 
184 					return;
185 				}
186 
187 				peek_dispatcher.dispatch(
188 					new AERunnable() {
189 
190 						@Override
191 						public void
192 						runSupport()
193 						{
194 							try{
195 								Map<String,Object> peek_data = BuddyPluginUtils.peekChat( net, key );
196 
197 								if ( peek_data != null ){
198 
199 									Number	message_count 	= (Number)peek_data.get( "m" );
200 									Number	node_count 		= (Number)peek_data.get( "n" );
201 
202 									if ( message_count != null && node_count != null ){
203 
204 										if ( message_count.intValue() > 0 ){
205 
206 											BuddyPluginBeta.ChatInstance chat = BuddyPluginUtils.getChat( net, key );
207 
208 											if ( chat != null ){
209 
210 												chat.setAutoNotify( true );
211 											}
212 										}
213 									}
214 								}
215 							}finally{
216 
217 								done.run();
218 							}
219 						}
220 					});
221 
222 				async = true;
223 			}
224 		}finally{
225 
226 			if ( !async ){
227 
228 				done.run();
229 			}
230 		}
231 	}
232 
233 	public static ChatInstance
getChat( String net, String key )234 	getChat(
235 		String		net,
236 		String		key )
237 	{
238 		BuddyPlugin bp = getPlugin();
239 
240 		if ( bp != null && bp.isBetaEnabled()){
241 
242 			try{
243 				return( bp.getBeta().getChat( net, key ));
244 
245 			}catch( Throwable e ){
246 
247 			}
248 		}
249 
250 		return( null );
251 	}
252 
253 	public static ChatInstance
getChat( String net, String key, Map<String,Object> options )254 	getChat(
255 		String					net,
256 		String					key,
257 		Map<String,Object>		options )
258 	{
259 		BuddyPlugin bp = getPlugin();
260 
261 		if ( bp != null && bp.isBetaEnabled()){
262 
263 			try{
264 				return( bp.getBeta().getChat( net, key, options ));
265 
266 			}catch( Throwable e ){
267 
268 			}
269 		}
270 
271 		return( null );
272 	}
273 
274 	public static ChatInstance
getChat( Download download )275 	getChat(
276 		Download		download )
277 	{
278 		BuddyPlugin bp = getPlugin();
279 
280 		if ( bp != null && bp.isBetaEnabled()){
281 
282 			return( bp.getBeta().getChat( download ));
283 		}
284 
285 		return( null );
286 	}
287 
288 	public static BuddyPluginViewInterface.View
buildChatView( Map<String,Object> properties, ViewListener listener )289 	buildChatView(
290 		Map<String,Object>	properties,
291 		ViewListener		listener )
292 	{
293 		BuddyPlugin bp = getPlugin();
294 
295 		if ( bp != null && bp.isBetaEnabled() && bp.getBeta().isAvailable()){
296 
297 			BuddyPluginViewInterface ui = bp.getSWTUI();
298 
299 			if ( ui != null ){
300 
301 				return( ui.buildView( properties, listener ));
302 			}
303 		}
304 
305 		return( null );
306 	}
307 
308 	public static String
getChatKey( TOTorrent torrent )309 	getChatKey(
310 		TOTorrent		torrent )
311 	{
312 		if ( torrent == null ){
313 
314 			return( null );
315 		}
316 
317 		return( getChatKey( PluginCoreUtils.wrap( torrent )));
318 	}
319 
320 	public static String
getChatKey( Download download )321 	getChatKey(
322 		Download		download )
323 	{
324 		return( getChatKey( download.getTorrent()));
325 	}
326 
327 	public static String
getChatKey( Torrent torrent )328 	getChatKey(
329 		Torrent		torrent )
330 	{
331 		if ( torrent == null ){
332 
333 			return( null );
334 		}
335 
336 			// use torrent name here to canonicalize things in case user has renamed download display name
337 			// also it is more important to get a consistent string rather than something correctly localised
338 
339 		String	torrent_name = null;
340 
341 		try{
342 			TOTorrent to_torrent = PluginCoreUtils.unwrap( torrent );
343 
344 			torrent_name = to_torrent.getUTF8Name();
345 
346 			if ( torrent_name == null ){
347 
348 				torrent_name = new String( to_torrent.getName(), "UTF-8" );
349 			}
350 		}catch( Throwable e ){
351 
352 		}
353 
354 		if ( torrent_name == null ){
355 
356 			torrent_name = torrent.getName();
357 		}
358 
359 		String key = "Download: " + torrent_name + " {" + ByteFormatter.encodeString( torrent.getHash()) + "}";
360 
361 		return( key );
362 	}
363 
364 
365 }
366