1 /*
2  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details ( see the LICENSE file ).
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 package com.aelitis.azureus.ui.swt.subscriptions;
20 
21 
22 
23 import org.gudy.azureus2.core3.internat.MessageText;
24 import org.gudy.azureus2.core3.util.*;
25 import org.gudy.azureus2.plugins.PluginInterface;
26 import org.gudy.azureus2.plugins.ui.UIManager;
27 import org.gudy.azureus2.plugins.ui.menus.*;
28 import org.gudy.azureus2.pluginsimpl.local.PluginInitializer;
29 import org.gudy.azureus2.ui.swt.plugins.UISWTViewEventListener;
30 
31 
32 import com.aelitis.azureus.core.subs.*;
33 import com.aelitis.azureus.ui.common.viewtitleinfo.ViewTitleInfo;
34 import com.aelitis.azureus.ui.common.viewtitleinfo.ViewTitleInfoManager;
35 import com.aelitis.azureus.ui.mdi.MdiEntry;
36 import com.aelitis.azureus.ui.mdi.MdiEntryVitalityImage;
37 import com.aelitis.azureus.ui.swt.mdi.BaseMdiEntry;
38 
39 public class SubscriptionMDIEntry implements SubscriptionListener, ViewTitleInfo
40 {
41 	private static final String ALERT_IMAGE_ID	= "image.sidebar.vitality.alert";
42 	private static final String AUTH_IMAGE_ID	= "image.sidebar.vitality.auth";
43 
44 	private final MdiEntry mdiEntry;
45 
46 	MdiEntryVitalityImage spinnerImage;
47 
48 	private MdiEntryVitalityImage warningImage;
49 	private final Subscription subs;
50 	private String key;
51 	private String current_parent;
52 
SubscriptionMDIEntry(Subscription subs, MdiEntry entry)53 	public SubscriptionMDIEntry(Subscription subs, MdiEntry entry) {
54 		this.subs = subs;
55 		this.mdiEntry = entry;
56 		key = "Subscription_" + ByteFormatter.encodeString(subs.getPublicKey());
57 		current_parent = subs.getParent();
58 		if ( current_parent != null && current_parent.length() == 0 ){
59 			current_parent = null;
60 		}
61 		setupMdiEntry();
62 	}
63 
setupMdiEntry()64 	private void setupMdiEntry() {
65 		if (mdiEntry == null) {
66 			return;
67 		}
68 
69 		mdiEntry.setViewTitleInfo(this);
70 
71 		mdiEntry.setImageLeftID("image.sidebar.subscriptions");
72 
73 		warningImage = mdiEntry.addVitalityImage( ALERT_IMAGE_ID );
74 
75 		spinnerImage = mdiEntry.addVitalityImage("image.sidebar.vitality.dots");
76 
77 		if (spinnerImage != null) {
78 			spinnerImage.setVisible(false);
79 		}
80 
81 		setWarning();
82 
83 		PluginInterface pi = PluginInitializer.getDefaultInterface();
84 		UIManager uim = pi.getUIManager();
85 
86 		final MenuManager menu_manager = uim.getMenuManager();
87 
88 		SubscriptionManagerUI.MenuCreator menu_creator =
89 			new SubscriptionManagerUI.MenuCreator()
90 			{
91 				public MenuItem
92 				createMenu(
93 					String 	resource_id )
94 				{
95 					return( menu_manager.addMenuItem("sidebar." + key, resource_id ));
96 				}
97 
98 				public void refreshView() {
99 					SubscriptionMDIEntry.this.refreshView();
100 				}
101 			};
102 
103 		SubscriptionManagerUI.createMenus( menu_manager, menu_creator, new Subscription[]{ subs });
104 
105 		subs.addListener(this);
106 	}
107 
108 	protected String
getCurrentParent()109 	getCurrentParent()
110 	{
111 		return( current_parent );
112 	}
113 
114 	protected boolean
isDisposed()115 	isDisposed()
116 	{
117 		return( mdiEntry.isDisposed());
118 	}
119 
subscriptionDownloaded(Subscription subs, boolean auto)120 	public void subscriptionDownloaded(Subscription subs, boolean auto) {
121 	}
122 
subscriptionChanged(Subscription subs)123 	public void subscriptionChanged(Subscription subs) {
124 		mdiEntry.redraw();
125 		ViewTitleInfoManager.refreshTitleInfo(mdiEntry.getViewTitleInfo());
126 	}
127 
refreshView()128 	protected void refreshView() {
129 		if (!(mdiEntry instanceof BaseMdiEntry)) {
130 			return;
131 		}
132 		UISWTViewEventListener eventListener = ((BaseMdiEntry)mdiEntry).getEventListener();
133 		if (eventListener instanceof SubscriptionView) {
134 			SubscriptionView subsView = (SubscriptionView) eventListener;
135 			subsView.refreshView();
136 		}
137 	}
138 
139 	protected void
setWarning()140 	setWarning()
141 	{
142 			// possible during initialisation, status will be shown again on complete
143 
144 		if ( warningImage == null ){
145 
146 			return;
147 		}
148 
149 		SubscriptionHistory history = subs.getHistory();
150 
151 		String	last_error = history.getLastError();
152 
153 		boolean	auth_fail = history.isAuthFail();
154 
155 			// don't report problem until its happened a few times, but not for auth fails as this is a perm error
156 
157 		if ( history.getConsecFails() < 3 && !auth_fail ){
158 
159 			last_error = null;
160 		}
161 
162 		boolean	trouble = last_error != null;
163 
164 		if ( trouble ){
165 
166 			warningImage.setToolTip( last_error );
167 
168 			warningImage.setImageID( auth_fail?AUTH_IMAGE_ID:ALERT_IMAGE_ID );
169 
170 			warningImage.setVisible( true );
171 
172 		}else{
173 
174 			warningImage.setVisible( false );
175 
176 			warningImage.setToolTip( "" );
177 		}
178 	}
179 
180 	public Object
getTitleInfoProperty( int propertyID )181 	getTitleInfoProperty(
182 		int propertyID )
183 	{
184 		// This should work, but since we have subs already in class, use that
185 		//if (mdiEntry == null) {
186 		//	return null;
187 		//}
188 		//Object ds = mdiEntry.getDatasource();
189 		//if (!(ds instanceof Subscription)) {
190 		//	return null;
191 		//}
192 		//Subscription subs = (Subscription) ds;
193 
194 		switch( propertyID ){
195 
196 			case ViewTitleInfo.TITLE_TEXT:{
197 
198 				return( subs.getName());
199 			}
200 			case ViewTitleInfo.TITLE_INDICATOR_TEXT_TOOLTIP:{
201 
202 				long	pop = subs.getCachedPopularity();
203 
204 				String res = subs.getName();
205 
206 				if ( pop > 1 ){
207 
208 					res += " (" + MessageText.getString("subscriptions.listwindow.popularity").toLowerCase() + "=" + pop + ")";
209 				}
210 
211 				return( res );
212 			}
213 			case ViewTitleInfo.TITLE_INDICATOR_TEXT :{
214 
215 				SubscriptionMDIEntry mdi = (SubscriptionMDIEntry) subs.getUserData(SubscriptionManagerUI.SUB_ENTRYINFO_KEY);
216 
217 				if ( mdi != null ){
218 
219 					mdi.setWarning();
220 				}
221 
222 				if( subs.getHistory().getNumUnread() > 0 ){
223 
224 					return ( "" + subs.getHistory().getNumUnread());
225 				}
226 
227 				return null;
228 			}
229 		}
230 
231 		return( null );
232 	}
233 
234 	protected void
removeWithConfirm()235 	removeWithConfirm()
236 	{
237 		SubscriptionManagerUI.removeWithConfirm( subs );
238 	}
239 }
240