1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.kernel.modules.notify; 22 23 import java.util.ArrayList; 24 import java.util.Collections; 25 import java.util.Comparator; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 import net.sourceforge.atunes.kernel.AbstractHandler; 31 import net.sourceforge.atunes.model.IAudioObject; 32 import net.sourceforge.atunes.model.IFullScreenHandler; 33 import net.sourceforge.atunes.model.INotificationEngine; 34 import net.sourceforge.atunes.model.INotificationsHandler; 35 import net.sourceforge.atunes.model.IStateCore; 36 import net.sourceforge.atunes.model.IStateUI; 37 import net.sourceforge.atunes.model.PlaybackState; 38 import net.sourceforge.atunes.utils.Logger; 39 40 /** 41 * Responsible of handling notifications 42 * @author alex 43 * 44 */ 45 public final class NotificationsHandler extends AbstractHandler implements INotificationsHandler { 46 47 private Map<String, INotificationEngine> engines; 48 49 private INotificationEngine defaultEngine; 50 51 private IStateUI stateUI; 52 53 private IStateCore stateCore; 54 55 /** 56 * @param stateCore 57 */ setStateCore(final IStateCore stateCore)58 public void setStateCore(final IStateCore stateCore) { 59 this.stateCore = stateCore; 60 } 61 62 /** 63 * @param stateUI 64 */ setStateUI(final IStateUI stateUI)65 public void setStateUI(final IStateUI stateUI) { 66 this.stateUI = stateUI; 67 } 68 getEngines()69 private Map<String, INotificationEngine> getEngines() { 70 if (engines == null) { 71 Logger.debug("Initializing notification engines"); 72 engines = new HashMap<String, INotificationEngine>(); 73 // Add here any new notification engine 74 addNotificationEngine(engines, getDefaultEngine()); 75 addNotificationEngine(engines, getBean("libnotifyNotificationEngine", INotificationEngine.class)); 76 addNotificationEngine(engines, getBean("growlNotificationEngine", INotificationEngine.class)); 77 } 78 return engines; 79 } 80 81 /** 82 * Adds a new notification engine to map of notifications 83 * @param engine 84 */ addNotificationEngine(final Map<String, INotificationEngine> engines, final INotificationEngine engine)85 private void addNotificationEngine(final Map<String, INotificationEngine> engines, final INotificationEngine engine) { 86 engines.put(engine.getName(), engine); 87 } 88 89 /** 90 * @return notification engine to use 91 */ getNotificationEngine()92 private INotificationEngine getNotificationEngine() { 93 INotificationEngine engine = getEngines().get(stateCore.getNotificationEngine()); 94 if (engine == null) { 95 engine = getDefaultEngine(); 96 } 97 return engine; 98 } 99 100 @Override applicationFinish()101 public void applicationFinish() { 102 getNotificationEngine().disposeNotifications(); 103 } 104 105 @Override applicationStateChanged()106 public void applicationStateChanged() { 107 getNotificationEngine().updateNotification(stateUI); 108 } 109 110 @Override showNotification(final IAudioObject audioObject)111 public void showNotification(final IAudioObject audioObject) { 112 // only show notification if not in full screen 113 if (!getBean(IFullScreenHandler.class).isVisible()) { 114 getNotificationEngine().showNotification(audioObject); 115 } 116 } 117 118 @Override playbackStateChanged(final PlaybackState newState, final IAudioObject currentAudioObject)119 public void playbackStateChanged(final PlaybackState newState, final IAudioObject currentAudioObject) { 120 if (stateUI.isShowOSD() && newState == PlaybackState.PLAYING) { 121 // Playing 122 showNotification(currentAudioObject); 123 } 124 } 125 126 @Override getNotificationEngines()127 public List<String> getNotificationEngines() { 128 List<String> names = new ArrayList<String>(getEngines().keySet()); 129 Collections.sort(names, new Comparator<String>() { 130 @Override 131 public int compare(final String o1, final String o2) { 132 if (o1.equals(getDefaultEngine().getName())) { 133 return -1; 134 } else if (o2.equals(getDefaultEngine().getName())) { 135 return 1; 136 } 137 return o1.compareTo(o2); 138 } 139 }); 140 return names; 141 } 142 143 @Override getNotificationEngine(final String name)144 public INotificationEngine getNotificationEngine(final String name) { 145 return getEngines().get(name); 146 } 147 148 @Override getDefaultEngine()149 public INotificationEngine getDefaultEngine() { 150 if (defaultEngine == null) { 151 defaultEngine = getBean("defaultNotificationEngine", INotificationEngine.class); 152 } 153 return defaultEngine; 154 } 155 } 156