PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/azureus-4.7.0.2/com/aelitis/azureus/ui/swt/feature/FeatureManagerUIListener.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 449 lines | 355 code | 89 blank | 5 comment | 95 complexity | 6c12fad25103621a89e6ab79d16bdc31 MD5 | raw file
  1. package com.aelitis.azureus.ui.swt.feature;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.eclipse.swt.SWT;
  5. import org.gudy.azureus2.core3.internat.MessageText;
  6. import org.gudy.azureus2.core3.util.*;
  7. import org.gudy.azureus2.plugins.PluginException;
  8. import org.gudy.azureus2.plugins.PluginInterface;
  9. import org.gudy.azureus2.plugins.ui.UIInstance;
  10. import org.gudy.azureus2.plugins.ui.UIManager;
  11. import org.gudy.azureus2.plugins.ui.UIManagerListener;
  12. import org.gudy.azureus2.plugins.utils.*;
  13. import org.gudy.azureus2.plugins.utils.FeatureManager.FeatureManagerListener;
  14. import org.gudy.azureus2.plugins.utils.FeatureManager.Licence;
  15. import org.gudy.azureus2.plugins.utils.FeatureManager.Licence.LicenceInstallationListener;
  16. import org.gudy.azureus2.pluginsimpl.local.PluginInitializer;
  17. import org.gudy.azureus2.ui.swt.Utils;
  18. import org.gudy.azureus2.ui.swt.plugins.UISWTInstance;
  19. import org.gudy.azureus2.ui.swt.shells.MessageBoxShell;
  20. import com.aelitis.azureus.activities.*;
  21. import com.aelitis.azureus.ui.UIFunctions;
  22. import com.aelitis.azureus.ui.UIFunctionsManager;
  23. import com.aelitis.azureus.ui.UserPrompterResultListener;
  24. import com.aelitis.azureus.ui.mdi.MdiEntry;
  25. import com.aelitis.azureus.ui.mdi.MultipleDocumentInterface;
  26. import com.aelitis.azureus.ui.swt.UIFunctionsManagerSWT;
  27. import com.aelitis.azureus.ui.swt.UIFunctionsSWT;
  28. import com.aelitis.azureus.ui.swt.skin.SWTSkin;
  29. import com.aelitis.azureus.ui.swt.skin.SWTSkinFactory;
  30. import com.aelitis.azureus.ui.swt.skin.SWTSkinObject;
  31. import com.aelitis.azureus.ui.swt.views.skin.*;
  32. public class FeatureManagerUIListener
  33. implements FeatureManagerListener
  34. {
  35. private final static boolean DEBUG = Constants.IS_CVS_VERSION;
  36. private static final String ID_ACTIVITY_EXPIRING = "ExpiringEntry";
  37. private static final String ID_ACTIVITY_OFFLINE = "OfflineExpiredEntry";
  38. private static final String ID_ACTIVITY_EXPIRED = "ExpiredEntry";
  39. private final FeatureManager featman;
  40. private String pendingAuthForKey;
  41. private Map<String, Object[]> licence_map = new HashMap<String, Object[]>();
  42. public FeatureManagerUIListener(FeatureManager featman) {
  43. if (DEBUG) {
  44. System.out.println("FEAT:");
  45. }
  46. this.featman = featman;
  47. }
  48. public void licenceAdded(Licence licence) {
  49. updateUI();
  50. mapLicence( licence );
  51. if (DEBUG) {
  52. System.out.println("FEAT: Licence " + licence.getKey() + " Added with state " + licence.getState());
  53. }
  54. if (licence.getState() == Licence.LS_PENDING_AUTHENTICATION) {
  55. pendingAuthForKey = licence.getKey();
  56. FeatureManagerUI.openLicenceValidatingWindow();
  57. }
  58. if (licence.isFullyInstalled()) {
  59. return;
  60. }else{
  61. licence.retryInstallation();
  62. }
  63. }
  64. public void licenceChanged(Licence licence) {
  65. int state = licence.getState();
  66. boolean stateChanged = true;
  67. Licence lastLicence = mapLicence( licence );
  68. if (lastLicence != null) {
  69. stateChanged = lastLicence.getState() != licence.getState();
  70. if ( ( !stateChanged ) &&
  71. licence.getState() == Licence.LS_AUTHENTICATED &&
  72. lastLicence.isFullyInstalled() != licence.isFullyInstalled()){
  73. stateChanged = true;
  74. }
  75. } else {
  76. // licenceChanged gets fired for all licences after listener is added
  77. // (via code in FeatureManagerUI)
  78. // skip case where licence is already cancelled
  79. if (state == Licence.LS_CANCELLED || state == Licence.LS_REVOKED
  80. || state == Licence.LS_ACTIVATION_DENIED) {
  81. stateChanged = false;
  82. }
  83. }
  84. updateUI();
  85. if (DEBUG) {
  86. System.out.println("FEAT: License " + licence.getKey()
  87. + " State Changed: " + state + "; changed? " + stateChanged);
  88. }
  89. if (!stateChanged) {
  90. return;
  91. }
  92. if (state == Licence.LS_PENDING_AUTHENTICATION) {
  93. pendingAuthForKey = licence.getKey();
  94. FeatureManagerUI.openLicenceValidatingWindow();
  95. } else {
  96. FeatureManagerUI.closeLicenceValidatingWindow();
  97. if (state == Licence.LS_AUTHENTICATED) {
  98. if (licence.getKey().equals(pendingAuthForKey)) {
  99. if (licence.isFullyInstalled()) {
  100. pendingAuthForKey = null;
  101. FeatureManagerUI.openLicenceSuccessWindow();
  102. } // else assumed install process is taking place
  103. }
  104. } else if (state == Licence.LS_INVALID_KEY) {
  105. FeatureManagerUI.openLicenceFailedWindow(state, licence.getKey());
  106. if (licence.getKey().equals(pendingAuthForKey)) {
  107. pendingAuthForKey = null;
  108. }
  109. } else if (state == Licence.LS_REVOKED) {
  110. FeatureManagerUI.openLicenceRevokedWindow(licence);
  111. } else if (state == Licence.LS_ACTIVATION_DENIED) {
  112. FeatureManagerUI.openLicenceActivationDeniedWindow(licence);
  113. }
  114. }
  115. }
  116. private Licence
  117. mapLicence(
  118. Licence licence )
  119. {
  120. Licence existing_licence;
  121. LicenceInstallationListener new_listener = null;
  122. synchronized ( licence_map ){
  123. String key = licence.getKey();
  124. Object[] entry = licence_map.get( key );
  125. if ( entry == null ){
  126. existing_licence = null;
  127. new_listener =
  128. new LicenceInstallationListener()
  129. {
  130. FeatureManagerInstallWindow install_window = null;
  131. public void start(String licence_key) {
  132. if (DEBUG) {
  133. System.out.println("FEATINST: START! " + licence_key);
  134. }
  135. try {
  136. Licence licence = featman.addLicence(licence_key);
  137. install_window = new FeatureManagerInstallWindow(licence);
  138. install_window.open();
  139. } catch (PluginException e) {
  140. Debug.out(e);
  141. }
  142. }
  143. public void reportProgress(String licenceKey, String install, int percent) {
  144. if (DEBUG) {
  145. System.out.println("FEATINST: " + install + ": " + percent);
  146. }
  147. }
  148. public void reportActivity(String licenceKey, String install,
  149. String activity) {
  150. if (DEBUG) {
  151. System.out.println("FEAT: ACTIVITY: " + install + ": " + activity);
  152. }
  153. }
  154. public boolean alreadyFailing = false;
  155. public void failed(String licenceKey, PluginException error) {
  156. if (DEBUG) {
  157. System.out.println("FEAT: FAIL: " + licenceKey + ": " + error.toString());
  158. }
  159. if ( install_window != null ){
  160. install_window.close();
  161. }
  162. if ( licenceKey.equals(pendingAuthForKey)){
  163. pendingAuthForKey = null;
  164. }
  165. if (alreadyFailing) {
  166. return;
  167. }
  168. alreadyFailing = true;
  169. String s = Debug.getNestedExceptionMessage(error);
  170. MessageBoxShell mb = new MessageBoxShell(
  171. SWT.ICON_ERROR | SWT.OK,
  172. "License Addition Error for " + licenceKey,
  173. s );
  174. mb.open( new UserPrompterResultListener() {
  175. public void prompterClosed(int result) {
  176. alreadyFailing = false;
  177. }
  178. } );
  179. }
  180. public void complete(String licenceKey) {
  181. if ( licenceKey.equals(pendingAuthForKey)){
  182. pendingAuthForKey = null;
  183. FeatureManagerUI.openLicenceSuccessWindow();
  184. }
  185. }
  186. };
  187. licence_map.put( key, new Object[]{ licence, new_listener });
  188. }else{
  189. existing_licence = (Licence)entry[0];
  190. entry[0] = licence;
  191. }
  192. }
  193. if ( new_listener != null ){
  194. licence.addInstallationListener( new_listener );
  195. }
  196. return( existing_licence );
  197. }
  198. private void updateUI() {
  199. PluginInterface plugin_interface = PluginInitializer.getDefaultInterface();
  200. UIManager ui_manager = plugin_interface.getUIManager();
  201. ui_manager.addUIListener(new UIManagerListener() {
  202. public void UIDetached(UIInstance instance) {
  203. }
  204. public void UIAttached(UIInstance instance) {
  205. if (instance instanceof UISWTInstance) {
  206. _updateUI();
  207. }
  208. }
  209. });
  210. }
  211. private void _updateUI() {
  212. final boolean hasFullLicence = FeatureManagerUI.hasFullLicence();
  213. try {
  214. buildNotifications();
  215. } catch (Exception e) {
  216. Debug.out(e);
  217. }
  218. final SWTSkin skin = SWTSkinFactory.getInstance();
  219. if (skin != null) {
  220. SWTSkinObject soHeader = skin.getSkinObject("plus-header");
  221. if (soHeader != null) {
  222. soHeader.setVisible(hasFullLicence);
  223. }
  224. Utils.execSWTThread(new AERunnable() {
  225. public void runSupport() {
  226. UIFunctionsSWT uif = UIFunctionsManagerSWT.getUIFunctionsSWT();
  227. uif.getMainShell().setText(hasFullLicence ? "Vuze Plus" : "Vuze");
  228. }
  229. });
  230. }
  231. UIFunctions uif = UIFunctionsManager.getUIFunctions();
  232. MultipleDocumentInterface mdi = uif.getMDI();
  233. if (mdi != null) {
  234. MdiEntry entry = mdi.getEntry(MultipleDocumentInterface.SIDEBAR_SECTION_PLUS);
  235. if (entry != null) {
  236. entry.setTitleID(hasFullLicence ? "mdi.entry.plus.full"
  237. : "mdi.entry.plus.free");
  238. SBC_PlusFTUX view = (SBC_PlusFTUX) SkinViewManager.getByClass(SBC_PlusFTUX.class);
  239. if (view != null) {
  240. view.updateLicenceInfo();
  241. }
  242. SkinView[] views = SkinViewManager.getMultiByClass(SBC_BurnFTUX.class);
  243. if (views != null) {
  244. for (SkinView bview : views) {
  245. ((SBC_BurnFTUX) bview).updateLicenceInfo();
  246. }
  247. }
  248. }
  249. }
  250. }
  251. public static void buildNotifications() {
  252. VuzeActivitiesManager.addListener(new VuzeActivitiesLoadedListener() {
  253. public void vuzeActivitiesLoaded() {
  254. _buildNotifications();
  255. }
  256. });
  257. }
  258. private static void _buildNotifications() {
  259. long plusDisplayExpiryTimeStamp = FeatureManagerUI.getPlusExpiryDisplayTimeStamp();
  260. long plusExpiryTimeStamp = FeatureManagerUI.getPlusExpiryTimeStamp();
  261. if (plusExpiryTimeStamp <= 0) {
  262. return;
  263. }
  264. long msDisplayLeft = plusDisplayExpiryTimeStamp - SystemTime.getCurrentTime();
  265. long daysDisplayLeft = (long) Math.ceil(msDisplayLeft / 86400000.0);
  266. long msLeft = plusExpiryTimeStamp - SystemTime.getCurrentTime();
  267. long daysLeft = (long) Math.ceil(msLeft / 86400000.0);
  268. if (daysLeft > 30 || daysDisplayLeft > 30) {
  269. VuzeActivitiesEntry entry1 = VuzeActivitiesManager.getEntryByID(ID_ACTIVITY_EXPIRED);
  270. VuzeActivitiesEntry entry2 = VuzeActivitiesManager.getEntryByID(ID_ACTIVITY_EXPIRING);
  271. VuzeActivitiesEntry entry3 = VuzeActivitiesManager.getEntryByID(ID_ACTIVITY_OFFLINE);
  272. if (entry1 != null || entry2 != null || entry3 != null) {
  273. VuzeActivitiesManager.removeEntries(new VuzeActivitiesEntry[] {
  274. entry1,
  275. entry2,
  276. entry3,
  277. }, true);
  278. }
  279. return;
  280. }
  281. String s;
  282. String id;
  283. String ref = "plus_note_" + (daysDisplayLeft >= 0 ? "expiring_" : "expired_")
  284. + Math.abs(daysDisplayLeft);
  285. String strA = "TARGET=\"" + MultipleDocumentInterface.SIDEBAR_SECTION_PLUS
  286. + "\" HREF=\"#" + ref + "\"";
  287. if (daysLeft < 0 && daysDisplayLeft > 0) {
  288. // if no days left but our display days > 0, that means we ran out
  289. // of offline time and daysDisplayLeft is when the real license expires
  290. VuzeActivitiesEntry entry1 = VuzeActivitiesManager.getEntryByID(ID_ACTIVITY_EXPIRED);
  291. VuzeActivitiesEntry entry2 = VuzeActivitiesManager.getEntryByID(ID_ACTIVITY_EXPIRING);
  292. if (entry1 != null || entry2 != null) {
  293. VuzeActivitiesManager.removeEntries(new VuzeActivitiesEntry[] {
  294. entry1,
  295. entry2
  296. }, true);
  297. }
  298. String msgID = "plus.notificaiton." + ID_ACTIVITY_OFFLINE;
  299. s = MessageText.getString(msgID, new String[] {
  300. "" + daysDisplayLeft,
  301. });
  302. id = ID_ACTIVITY_OFFLINE;
  303. } else {
  304. VuzeActivitiesEntry entry3 = VuzeActivitiesManager.getEntryByID(ID_ACTIVITY_OFFLINE);
  305. if (entry3 != null) {
  306. VuzeActivitiesManager.removeEntries(new VuzeActivitiesEntry[] {
  307. entry3,
  308. }, true);
  309. }
  310. if (daysDisplayLeft > 0) {
  311. String msgID = "plus.notificaiton." + ID_ACTIVITY_EXPIRING
  312. + (daysDisplayLeft == 1 ? ".s" : ".p");
  313. s = MessageText.getString(msgID, new String[] {
  314. "" + daysDisplayLeft,
  315. strA
  316. });
  317. id = ID_ACTIVITY_EXPIRING;
  318. } else {
  319. String msgID = "plus.notificaiton." + ID_ACTIVITY_EXPIRED
  320. + (daysDisplayLeft == -1 ? ".s" : ".p");
  321. s = MessageText.getString(msgID, new String[] {
  322. "" + -daysDisplayLeft,
  323. strA
  324. });
  325. id = ID_ACTIVITY_EXPIRED;
  326. }
  327. }
  328. VuzeActivitiesEntry entry = VuzeActivitiesManager.getEntryByID(id);
  329. if (entry == null) {
  330. boolean existed = VuzeActivitiesManager.isEntryIdRemoved(id);
  331. if (existed) {
  332. return;
  333. }
  334. entry = new VuzeActivitiesEntry(SystemTime.getCurrentTime(), s,
  335. VuzeActivitiesConstants.TYPEID_VUZENEWS);
  336. entry.setID(id);
  337. entry.setIconID("image.sidebar.plus");
  338. if (daysLeft < 0 && daysDisplayLeft < 0) {
  339. UIFunctionsManager.getUIFunctions().getMDI().showEntryByID(
  340. MultipleDocumentInterface.SIDEBAR_SECTION_PLUS);
  341. }
  342. } else {
  343. entry.setText(s);
  344. entry.setTimestamp(SystemTime.getCurrentTime());
  345. }
  346. VuzeActivitiesManager.addEntries(new VuzeActivitiesEntry[] {
  347. entry
  348. });
  349. }
  350. public void licenceRemoved(Licence licence) {
  351. Object[] entry;
  352. synchronized( licence_map ){
  353. entry = licence_map.remove(licence.getKey());
  354. }
  355. if ( entry != null ){
  356. licence.removeInstallationListener( (LicenceInstallationListener)entry[1] );
  357. }
  358. updateUI();
  359. }
  360. }