1 /*
2  * Created on Sep 22, 2008
3  * Created by Paul Gardner
4  *
5  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 
20 
21 package com.aelitis.azureus.core.subs;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 import org.gudy.azureus2.core3.download.DownloadManager;
27 import org.gudy.azureus2.core3.torrent.TOTorrent;
28 
29 import com.aelitis.azureus.core.AzureusCore;
30 import com.aelitis.azureus.core.metasearch.Engine;
31 import com.aelitis.azureus.core.metasearch.impl.web.WebEngine;
32 
33 public class
34 SubscriptionUtils
35 {
36 	public static SubscriptionDownloadDetails[]
getAllCachedDownloadDetails(AzureusCore core)37 	getAllCachedDownloadDetails(AzureusCore core)
38 	{
39 		List<DownloadManager> 	dms 	= core.getGlobalManager().getDownloadManagers();
40 
41 		List<SubscriptionDownloadDetails>	result 	= new ArrayList<SubscriptionDownloadDetails>();
42 
43 		SubscriptionManager sub_man = SubscriptionManagerFactory.getSingleton();
44 
45 		for (int i=0;i<dms.size();i++){
46 
47 			DownloadManager	dm = dms.get(i);
48 
49 			TOTorrent torrent = dm.getTorrent();
50 
51 			if ( torrent != null ){
52 
53 				try{
54 					Subscription[] subs = sub_man.getKnownSubscriptions( torrent.getHash());
55 
56 					if ( subs != null && subs.length > 0 ){
57 
58 						if ( sub_man.hideSearchTemplates()){
59 
60 							List<Subscription>	filtered = new ArrayList<Subscription>();
61 
62 							for ( Subscription s: subs ){
63 
64 								if ( !s.isSearchTemplate()){
65 
66 									filtered.add( s );
67 								}
68 							}
69 
70 							if ( filtered.size() > 0 ){
71 
72 								result.add( new SubscriptionDownloadDetails( dm, filtered.toArray( new Subscription[filtered.size()] )));
73 							}
74 						}else{
75 
76 							result.add( new SubscriptionDownloadDetails( dm, subs ));
77 						}
78 					}
79 				}catch( Throwable e ){
80 				}
81 			}
82 		}
83 
84 		return(result.toArray( new SubscriptionDownloadDetails[result.size()]));
85 	}
86 
87 	public static String
getSubscriptionChatKey( Subscription subs )88 	getSubscriptionChatKey(
89 		Subscription		subs )
90 	{
91 		try{
92 			String key = null;
93 
94 			Engine engine = subs.getEngine();
95 
96 			if ( engine instanceof WebEngine ){
97 
98 				WebEngine web_engine = (WebEngine)subs.getEngine();
99 
100 				key = web_engine.getSearchUrl( true );
101 
102 			}else{
103 
104 				key = subs.getQueryKey();
105 			}
106 
107 			if ( key != null ){
108 
109 				key = "Subscription: " + key;
110 			}
111 
112 			return( key );
113 
114 		}catch( Throwable e ){
115 
116 			return( null );
117 		}
118 	}
119 
120 	public static void
peekChatAsync( final String net, final String key, final Runnable done )121 	peekChatAsync(
122 		final String		net,
123 		final String		key,
124 		final Runnable		done )
125 	{
126 			// this is here to decouple subscriptions from chat
127 
128 		try{
129 			Class<?> utils = SubscriptionUtils.class.getClassLoader().loadClass( "com.aelitis.azureus.plugins.net.buddy.BuddyPluginUtils" );
130 
131 			if ( utils != null ){
132 
133 				utils.getMethod( "peekChatAsync", String.class, String.class, Runnable.class ).invoke( null, net, key, done );
134 			}
135 		}catch( Throwable e ){
136 
137 		}
138 	}
139 
140 	public static class
141 	SubscriptionDownloadDetails
142 	{
143 		private DownloadManager		download;
144 		private Subscription[]		subscriptions;
145 
146 		protected
SubscriptionDownloadDetails( DownloadManager dm, Subscription[] subs )147 		SubscriptionDownloadDetails(
148 			DownloadManager		dm,
149 			Subscription[]		subs )
150 		{
151 			download 		= dm;
152 			subscriptions	= subs;
153 		}
154 
155 		public DownloadManager
getDownload()156 		getDownload()
157 		{
158 			return( download );
159 		}
160 
161 		public Subscription[]
getSubscriptions()162 		getSubscriptions()
163 		{
164 			return( subscriptions );
165 		}
166 	}
167 }
168