Browse Source

DI the now playing plugin.

Change-Id: Ibe9c5f044aad1d51fc31a3bd5c9c7a98589d7018
Reviewed-on: http://gerrit.dmdirc.com/3214
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8
Greg Holmes 10 years ago
parent
commit
ba8c512303

+ 40
- 53
src/com/dmdirc/addons/nowplaying/ConfigPanel.java View File

@@ -27,7 +27,8 @@ import com.dmdirc.addons.ui_swing.components.reorderablelist.ListReorderButtonPa
27 27
 import com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList;
28 28
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
29 29
 import com.dmdirc.config.prefs.PreferencesInterface;
30
-import com.dmdirc.interfaces.config.IdentityController;
30
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
31
+import com.dmdirc.interfaces.config.ConfigProvider;
31 32
 import com.dmdirc.logger.ErrorLevel;
32 33
 import com.dmdirc.logger.Logger;
33 34
 
@@ -51,22 +52,27 @@ import javax.swing.UIManager;
51 52
 
52 53
 import net.miginfocom.swing.MigLayout;
53 54
 
55
+import static com.google.common.base.Preconditions.checkNotNull;
56
+
54 57
 /**
55 58
  * Now playing plugin config panel.
56 59
  */
57
-public class ConfigPanel extends JPanel implements PreferencesInterface,
58
-        KeyListener {
60
+public class ConfigPanel extends JPanel implements PreferencesInterface, KeyListener {
59 61
 
60 62
     /** A version number for this class. */
61 63
     private static final long serialVersionUID = 1;
62
-    /** Media source order list. */
63
-    private ReorderableJList<String> list;
64
+    /** Now playing manager to get and handle sources. */
65
+    private final NowPlayingManager manager;
66
+    /** Global configuration to read settings from. */
67
+    private final AggregateConfigProvider globalConfig;
68
+    /** User settings to write settings to. */
69
+    private final ConfigProvider userSettings;
70
+    /** This plugin's settings domain. */
71
+    private final String domain;
64 72
     /** Media sources. */
65 73
     private final List<String> sources;
66
-    /** The plugin that owns this panel. */
67
-    private final NowPlayingPlugin plugin;
68
-    /** The controller to read/write settings with. */
69
-    private final IdentityController identityController;
74
+    /** Media source order list. */
75
+    private ReorderableJList<String> list;
70 76
     /** Text field for our setting. */
71 77
     private JTextField textfield;
72 78
     /** Panel that the preview is in. */
@@ -79,24 +85,25 @@ public class ConfigPanel extends JPanel implements PreferencesInterface,
79 85
     /**
80 86
      * Creates a new instance of ConfigPanel.
81 87
      *
82
-     * @param identityController The controller to read/write settings with.
83
-     * @param plugin             The plugin that owns this panel
84
-     * @param sources            A list of sources to be used in the panel
88
+     * @param manager      Now playing manager to get and handle sources
89
+     * @param globalConfig Global config to read from
90
+     * @param userSettings Config to write settings to
91
+     * @param domain       This plugin's settings domain
92
+     * @param sources      A list of sources to be used in the panel
85 93
      */
86 94
     public ConfigPanel(
87
-            final IdentityController identityController,
88
-            final NowPlayingPlugin plugin,
95
+            final NowPlayingManager manager,
96
+            final AggregateConfigProvider globalConfig,
97
+            final ConfigProvider userSettings,
98
+            final String domain,
89 99
             final List<String> sources) {
90 100
         super();
91
-
92
-        if (sources == null) {
93
-            this.sources = new LinkedList<>();
94
-        } else {
95
-            this.sources = new LinkedList<>(sources);
96
-        }
97
-
98
-        this.identityController = identityController;
99
-        this.plugin = plugin;
101
+        this.manager = manager;
102
+        this.globalConfig = globalConfig;
103
+        this.userSettings = userSettings;
104
+        this.domain = domain;
105
+        this.sources = new LinkedList<>();
106
+        this.sources.addAll(checkNotNull(sources));
100 107
 
101 108
         initComponents();
102 109
     }
@@ -111,8 +118,7 @@ public class ConfigPanel extends JPanel implements PreferencesInterface,
111 118
             list.getModel().addElement(source);
112 119
         }
113 120
 
114
-        textfield = new JTextField(identityController.getGlobalConfiguration()
115
-                .getOption(plugin.getDomain(), "format"));
121
+        textfield = new JTextField(globalConfig.getOption(domain, "format"));
116 122
         textfield.addKeyListener(this);
117 123
         preview = new TextLabel("Preview:\n");
118 124
 
@@ -154,22 +160,20 @@ public class ConfigPanel extends JPanel implements PreferencesInterface,
154 160
     private void updatePreview() {
155 161
         updateTimer.cancel();
156 162
 
157
-        MediaSource source = plugin.getBestSource();
163
+        MediaSource source = manager.getBestSource();
158 164
 
159 165
         if (source == null) {
160 166
             source = new DummyMediaSource();
161 167
         }
162 168
 
163
-        final String text = plugin.doSubstitution(
169
+        final String text = manager.doSubstitution(
164 170
                 UIUtilities.invokeAndWait(new Callable<String>() {
165
-            /** {@inheritDoc} */
166
-            @Override
167
-            public String call() {
168
-                return textfield.getText();
169
-            }
170
-        }), source);
171
+                    @Override
172
+                    public String call() {
173
+                        return textfield.getText();
174
+                    }
175
+                }), source);
171 176
         SwingUtilities.invokeLater(new Runnable() {
172
-            /** {@inheritDoc} */
173 177
             @Override
174 178
             public void run() {
175 179
                 preview.setText("Preview:\n" + text);
@@ -197,39 +201,22 @@ public class ConfigPanel extends JPanel implements PreferencesInterface,
197 201
         return newSources;
198 202
     }
199 203
 
200
-    /** {@inheritDoc} */
201 204
     @Override
202 205
     public void save() {
203
-        plugin.saveSettings(getSources());
204
-        identityController.getUserSettings()
205
-                .setOption(plugin.getDomain(), "format", textfield.getText());
206
+        userSettings.setOption(domain, "sourceOrder", getSources());
207
+        userSettings.setOption(domain, "format", textfield.getText());
206 208
     }
207 209
 
208
-    /**
209
-     * {@inheritDoc}
210
-     *
211
-     * @param e Key event action
212
-     */
213 210
     @Override
214 211
     public void keyTyped(final KeyEvent e) {
215 212
         // Do nothing
216 213
     }
217 214
 
218
-    /**
219
-     * {@inheritDoc}
220
-     *
221
-     * @param e Key event action
222
-     */
223 215
     @Override
224 216
     public void keyPressed(final KeyEvent e) {
225 217
         // Do nothing
226 218
     }
227 219
 
228
-    /**
229
-     * {@inheritDoc}
230
-     *
231
-     * @param e Key event action
232
-     */
233 220
     @Override
234 221
     public void keyReleased(final KeyEvent e) {
235 222
         schedulePreviewUpdate();

+ 1
- 5
src/com/dmdirc/addons/nowplaying/MediaSource.java View File

@@ -25,8 +25,6 @@ package com.dmdirc.addons.nowplaying;
25 25
 /**
26 26
  * The media source describes one source of "now playing" information (i.e., one method of getting
27 27
  * information from one media player).
28
- *
29
- * @author chris
30 28
  */
31 29
 public interface MediaSource {
32 30
 
@@ -34,8 +32,6 @@ public interface MediaSource {
34 32
      * Get the state of this media source
35 33
      *
36 34
      * @return State for this media source.
37
-     *
38
-     * @since 0.6.3m1
39 35
      */
40 36
     MediaSourceState getState();
41 37
 
@@ -82,7 +78,7 @@ public interface MediaSource {
82 78
     String getTime();
83 79
 
84 80
     /**
85
-     * Retrives the format of the currently loaded track.
81
+     * Retrieves the format of the currently loaded track.
86 82
      *
87 83
      * @return Current track format
88 84
      */

+ 1
- 8
src/com/dmdirc/addons/nowplaying/MediaSourceComparator.java View File

@@ -28,16 +28,10 @@ import java.util.List;
28 28
 
29 29
 /**
30 30
  * Sorts media sources according to an ordered list of their names.
31
- *
32
- * @author chris
33 31
  */
34 32
 public class MediaSourceComparator implements Comparator<MediaSource>, Serializable {
35 33
 
36
-    /**
37
-     * A version number for this class. It should be changed whenever the class structure is changed
38
-     * (or anything else that would prevent serialized objects being unserialized with the new
39
-     * class).
40
-     */
34
+    /** A version number for this class. */
41 35
     private static final long serialVersionUID = 1;
42 36
     /** The order that the sources should be checked. */
43 37
     private final List<String> order;
@@ -52,7 +46,6 @@ public class MediaSourceComparator implements Comparator<MediaSource>, Serializa
52 46
         this.order = order;
53 47
     }
54 48
 
55
-    /** {@inheritDoc} */
56 49
     @Override
57 50
     public int compare(final MediaSource o1, final MediaSource o2) {
58 51
         return getPosition(o1) - getPosition(o2);

+ 0
- 2
src/com/dmdirc/addons/nowplaying/MediaSourceManager.java View File

@@ -27,8 +27,6 @@ import java.util.List;
27 27
 /**
28 28
  * The media source manager is a standard interface for an object that controls one or more media
29 29
  * sources.
30
- *
31
- * @author chris
32 30
  */
33 31
 public interface MediaSourceManager {
34 32
 

+ 0
- 3
src/com/dmdirc/addons/nowplaying/MediaSourceState.java View File

@@ -24,9 +24,6 @@ package com.dmdirc.addons.nowplaying;
24 24
 
25 25
 /**
26 26
  * The state of a media source.
27
- *
28
- * @author Shane McCormack
29
- * @since 0.6.3m1
30 27
  */
31 28
 public enum MediaSourceState {
32 29
 

+ 32
- 27
src/com/dmdirc/addons/nowplaying/NowPlayingCommand.java View File

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.nowplaying;
24 24
 
25
+import com.dmdirc.ClientModule.GlobalConfig;
25 26
 import com.dmdirc.FrameContainer;
26 27
 import com.dmdirc.MessageTarget;
27 28
 import com.dmdirc.commandparser.BaseCommandInfo;
@@ -32,13 +33,16 @@ import com.dmdirc.commandparser.commands.IntelligentCommand;
32 33
 import com.dmdirc.commandparser.commands.context.ChatCommandContext;
33 34
 import com.dmdirc.commandparser.commands.context.CommandContext;
34 35
 import com.dmdirc.interfaces.CommandController;
35
-import com.dmdirc.interfaces.config.IdentityController;
36
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
37
+import com.dmdirc.plugins.PluginDomain;
36 38
 import com.dmdirc.ui.input.AdditionalTabTargets;
37 39
 import com.dmdirc.ui.input.TabCompleter;
38 40
 
39 41
 import java.util.Arrays;
40 42
 import java.util.List;
41 43
 
44
+import javax.inject.Inject;
45
+
42 46
 /**
43 47
  * The now playing command retrieves the currently playing song from a variety of media players.
44 48
  */
@@ -49,28 +53,33 @@ public class NowPlayingCommand extends Command implements IntelligentCommand {
49 53
             "nowplaying [--sources|--source <source>] [format] - "
50 54
             + "tells the channel the song you're currently playing",
51 55
             CommandType.TYPE_CHAT);
52
-    /** The plugin that's using this command. */
53
-    private final NowPlayingPlugin parent;
54
-    /** The controller to read/write settings with. */
55
-    private final IdentityController identityController;
56
+    /** Now playing manager to get and handle sources. */
57
+    private final NowPlayingManager manager;
58
+    /** Global configuration to read settings from. */
59
+    private final AggregateConfigProvider globalConfig;
60
+    /** This plugin's settings domain. */
61
+    private final String domain;
56 62
 
57 63
     /**
58 64
      * Creates a new instance of this command.
59 65
      *
60
-     * @param controller         The controller to use for command information.
61
-     * @param parent             The plugin that owns this command.
62
-     * @param identityController The controller to use to read/write settings.
66
+     * @param controller   The controller to use for command information.
67
+     * @param manager      Now playing manager to get and handle sources.
68
+     * @param globalConfig Global config to read from
69
+     * @param domain       This plugin's settings domain
63 70
      */
71
+    @Inject
64 72
     public NowPlayingCommand(
65 73
             final CommandController controller,
66
-            final NowPlayingPlugin parent,
67
-            final IdentityController identityController) {
74
+            final NowPlayingManager manager,
75
+            @GlobalConfig final AggregateConfigProvider globalConfig,
76
+            @PluginDomain(NowPlayingPlugin.class) final String domain) {
68 77
         super(controller);
69
-        this.parent = parent;
70
-        this.identityController = identityController;
78
+        this.manager = manager;
79
+        this.globalConfig = globalConfig;
80
+        this.domain = domain;
71 81
     }
72 82
 
73
-    /** {@inheritDoc} */
74 83
     @Override
75 84
     public void execute(final FrameContainer origin,
76 85
             final CommandArguments args, final CommandContext context) {
@@ -82,7 +91,7 @@ public class NowPlayingCommand extends Command implements IntelligentCommand {
82 91
                 .equalsIgnoreCase("--source")) {
83 92
             if (args.getArguments().length > 1) {
84 93
                 final String sourceName = args.getArguments()[1];
85
-                final MediaSource source = parent.getSource(sourceName);
94
+                final MediaSource source = manager.getSource(sourceName);
86 95
 
87 96
                 if (source == null) {
88 97
                     sendLine(origin, args.isSilent(), FORMAT_ERROR, "Source not found.");
@@ -99,10 +108,10 @@ public class NowPlayingCommand extends Command implements IntelligentCommand {
99 108
                         "You must specify a source when using --source.");
100 109
             }
101 110
         } else {
102
-            if (parent.hasRunningSource()) {
111
+            if (manager.hasRunningSource()) {
103 112
                 target.getCommandParser().parseCommand(origin,
104
-                        getInformation(parent.getBestSource(), args.
105
-                        getArgumentsAsString(0)));
113
+                        getInformation(manager.getBestSource(), args.
114
+                                getArgumentsAsString(0)));
106 115
             } else {
107 116
                 sendLine(origin, args.isSilent(), FORMAT_ERROR,
108 117
                         "No running media sources available.");
@@ -119,7 +128,7 @@ public class NowPlayingCommand extends Command implements IntelligentCommand {
119 128
      */
120 129
     private void doSourceList(final FrameContainer origin, final boolean isSilent,
121 130
             final String format) {
122
-        final List<MediaSource> sources = parent.getSources();
131
+        final List<MediaSource> sources = manager.getSources();
123 132
 
124 133
         if (sources.isEmpty()) {
125 134
             sendLine(origin, isSilent, FORMAT_ERROR, "No media sources available.");
@@ -147,25 +156,21 @@ public class NowPlayingCommand extends Command implements IntelligentCommand {
147 156
     }
148 157
 
149 158
     /**
150
-     * Returns a formatted information string from the requested soruce.
159
+     * Returns a formatted information string from the requested source.
151 160
      *
152 161
      * @param source MediaSource to query
153 162
      * @param format Format to use
154 163
      *
155 164
      * @return Formatted information string
156
-     *
157
-     * @since 0.6.3
158 165
      */
159 166
     private String getInformation(final MediaSource source, final String format) {
160 167
         if (format.isEmpty()) {
161
-            return parent.doSubstitution(identityController.getGlobalConfiguration()
162
-                    .getOption(parent.getDomain(), "format"), source);
168
+            return manager.doSubstitution(globalConfig.getOption(domain, "format"), source);
163 169
         } else {
164
-            return parent.doSubstitution(format, source);
170
+            return manager.doSubstitution(format, source);
165 171
         }
166 172
     }
167 173
 
168
-    /** {@inheritDoc} */
169 174
     @Override
170 175
     public AdditionalTabTargets getSuggestions(final int arg,
171 176
             final IntelligentCommandContext context) {
@@ -185,7 +190,7 @@ public class NowPlayingCommand extends Command implements IntelligentCommand {
185 190
         } else if (arg == 1 && context.getPreviousArgs().get(0).equalsIgnoreCase("--source")) {
186 191
             final AdditionalTabTargets res = new AdditionalTabTargets();
187 192
             res.excludeAll();
188
-            for (MediaSource source : parent.getSources()) {
193
+            for (MediaSource source : manager.getSources()) {
189 194
                 if (source.getState() != MediaSourceState.CLOSED) {
190 195
                     res.add(source.getAppName());
191 196
                 }
@@ -199,7 +204,7 @@ public class NowPlayingCommand extends Command implements IntelligentCommand {
199 204
         } else {
200 205
             final AdditionalTabTargets res = TabCompleter
201 206
                     .getIntelligentResults(arg, context, context
202
-                    .getPreviousArgs().get(0).equalsIgnoreCase("--sources") ? 1 : 0);
207
+                            .getPreviousArgs().get(0).equalsIgnoreCase("--sources") ? 1 : 0);
203 208
             res.addAll(subsList);
204 209
             return res;
205 210
         }

+ 291
- 0
src/com/dmdirc/addons/nowplaying/NowPlayingManager.java View File

@@ -0,0 +1,291 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.nowplaying;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.ClientModule.UserConfig;
27
+import com.dmdirc.actions.CoreActionType;
28
+import com.dmdirc.interfaces.ActionController;
29
+import com.dmdirc.interfaces.ActionListener;
30
+import com.dmdirc.interfaces.actions.ActionType;
31
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
32
+import com.dmdirc.interfaces.config.ConfigProvider;
33
+import com.dmdirc.plugins.Plugin;
34
+import com.dmdirc.plugins.PluginDomain;
35
+import com.dmdirc.plugins.PluginInfo;
36
+import com.dmdirc.plugins.PluginManager;
37
+
38
+import java.util.ArrayList;
39
+import java.util.Collections;
40
+import java.util.List;
41
+
42
+import javax.inject.Inject;
43
+
44
+public class NowPlayingManager implements ActionListener {
45
+
46
+    /** Plugin manager to get plugins from. */
47
+    private final PluginManager pluginManager;
48
+    /** Action controller to register listeners. */
49
+    private final ActionController actionController;
50
+    /** Global configuration to read settings from. */
51
+    private final AggregateConfigProvider globalConfig;
52
+    /** This plugin's settings domain. */
53
+    private final String domain;
54
+    /** The sources that we know of. */
55
+    private final List<MediaSource> sources = new ArrayList<>();
56
+    /** The managers that we know of. */
57
+    private final List<MediaSourceManager> managers = new ArrayList<>();
58
+    /** The user's preferred order for source usage. */
59
+    private List<String> order;
60
+
61
+    @Inject
62
+    public NowPlayingManager(
63
+            final PluginManager pluginManager,
64
+            final ActionController actionController,
65
+            @GlobalConfig final AggregateConfigProvider globalConfig,
66
+            @UserConfig final ConfigProvider userConfig,
67
+            @PluginDomain(NowPlayingPlugin.class) final String domain) {
68
+        this.pluginManager = pluginManager;
69
+        this.actionController = actionController;
70
+        this.globalConfig = globalConfig;
71
+        this.domain = domain;
72
+    }
73
+
74
+    /**
75
+     * Loads the plugin.
76
+     */
77
+    public void onLoad() {
78
+        sources.clear();
79
+        managers.clear();
80
+        order = getSettings();
81
+        actionController.registerListener(this, CoreActionType.PLUGIN_LOADED,
82
+                CoreActionType.PLUGIN_UNLOADED);
83
+        for (PluginInfo target : pluginManager.getPluginInfos()) {
84
+            if (target.isLoaded()) {
85
+                addPlugin(target);
86
+            }
87
+        }
88
+    }
89
+
90
+    /**
91
+     * Unloads the plugin.
92
+     */
93
+    public void onUnload() {
94
+        sources.clear();
95
+        managers.clear();
96
+        actionController.unregisterListener(this);
97
+    }
98
+
99
+    /** Loads the plugins settings. */
100
+    public List<String> getSettings() {
101
+        if (globalConfig.hasOptionString(domain, "sourceOrder")) {
102
+            return globalConfig.getOptionList(domain, "sourceOrder");
103
+        } else {
104
+            return new ArrayList<>();
105
+        }
106
+    }
107
+
108
+    /**
109
+     * Checks to see if a plugin implements one of the Media Source interfaces and if it does, adds
110
+     * the source(s) to our list.
111
+     *
112
+     * @param target The plugin to be tested
113
+     */
114
+    private void addPlugin(final PluginInfo target) {
115
+        final Plugin targetPlugin = target.getPlugin();
116
+        if (targetPlugin instanceof MediaSource) {
117
+            sources.add((MediaSource) targetPlugin);
118
+            addSourceToOrder((MediaSource) targetPlugin);
119
+        }
120
+
121
+        if (targetPlugin instanceof MediaSourceManager) {
122
+            managers.add((MediaSourceManager) targetPlugin);
123
+
124
+            if (((MediaSourceManager) targetPlugin).getSources() != null) {
125
+                for (MediaSource source : ((MediaSourceManager) targetPlugin).getSources()) {
126
+                    addSourceToOrder(source);
127
+                }
128
+            }
129
+        }
130
+    }
131
+
132
+    /**
133
+     * Checks to see if the specified media source needs to be added to our order list, and adds it
134
+     * if necessary.
135
+     *
136
+     * @param source The media source to be tested
137
+     */
138
+    private void addSourceToOrder(final MediaSource source) {
139
+        if (!order.contains(source.getAppName())) {
140
+            order.add(source.getAppName());
141
+        }
142
+    }
143
+
144
+    /**
145
+     * Checks to see if a plugin implements one of the Media Source interfaces and if it does,
146
+     * removes the source(s) from our list.
147
+     *
148
+     * @param target The plugin to be tested
149
+     */
150
+    private void removePlugin(final PluginInfo target) {
151
+        final Plugin targetPlugin = target.getPlugin();
152
+        if (targetPlugin instanceof MediaSource) {
153
+            sources.remove((MediaSource) targetPlugin);
154
+        }
155
+
156
+        if (targetPlugin instanceof MediaSourceManager) {
157
+            managers.remove((MediaSourceManager) targetPlugin);
158
+        }
159
+    }
160
+
161
+    /**
162
+     * Determines if there are any valid sources (paused or not).
163
+     *
164
+     * @return True if there are running sources, false otherwise
165
+     */
166
+    public boolean hasRunningSource() {
167
+        for (final MediaSource source : getSources()) {
168
+            if (source.getState() != MediaSourceState.CLOSED) {
169
+                return true;
170
+            }
171
+        }
172
+
173
+        return false;
174
+    }
175
+
176
+    /**
177
+     * Retrieves the "best" source to use for displaying media information. The best source is
178
+     * defined as the earliest in the list that is running and not paused, or, if no such source
179
+     * exists, the earliest in the list that is running and paused. If neither condition is
180
+     * satisified returns null.
181
+     *
182
+     * @return The best source to use for media info
183
+     */
184
+    public MediaSource getBestSource() {
185
+        MediaSource paused = null;
186
+        final List<MediaSource> possibleSources = getSources();
187
+
188
+        Collections.sort(possibleSources, new MediaSourceComparator(order));
189
+
190
+        for (final MediaSource source : possibleSources) {
191
+            if (source.getState() != MediaSourceState.CLOSED) {
192
+                if (source.getState() == MediaSourceState.PLAYING) {
193
+                    return source;
194
+                } else if (paused == null) {
195
+                    paused = source;
196
+                }
197
+            }
198
+        }
199
+
200
+        return paused;
201
+    }
202
+
203
+    /**
204
+     * Substitutes the keywords in the specified format with the values with values from the
205
+     * specified source.
206
+     *
207
+     * @param format The format to be substituted
208
+     * @param source The source whose values should be used
209
+     *
210
+     * @return The substituted string
211
+     */
212
+    public String doSubstitution(final String format, final MediaSource source) {
213
+        final String artist = source.getArtist();
214
+        final String title = source.getTitle();
215
+        final String album = source.getAlbum();
216
+        final String app = source.getAppName();
217
+        final String bitrate = source.getBitrate();
218
+        final String filetype = source.getFormat();
219
+        final String length = source.getLength();
220
+        final String time = source.getTime();
221
+        final String state = source.getState().getNiceName();
222
+
223
+        return format.replace("$artist", sanitise(artist))
224
+                .replace("$title", sanitise(title))
225
+                .replace("$album", sanitise(album))
226
+                .replace("$app", sanitise(app))
227
+                .replace("$bitrate", sanitise(bitrate))
228
+                .replace("$format", sanitise(filetype))
229
+                .replace("$length", sanitise(length))
230
+                .replace("$state", sanitise(state))
231
+                .replace("$time", sanitise(time));
232
+    }
233
+
234
+    /**
235
+     * Sanitises the specified String so that it may be used as the replacement in a call to
236
+     * String.replaceAll. Namely, at present, this method returns an empty String if it is passed a
237
+     * null value; otherwise the input is returned as-is.
238
+     *
239
+     * @param input The string to be sanitised
240
+     *
241
+     * @return A sanitised version of the String
242
+     *
243
+     * @since 0.6.3
244
+     */
245
+    protected static String sanitise(final String input) {
246
+        return input == null ? "" : input;
247
+    }
248
+
249
+    /**
250
+     * Retrieves a source based on its name.
251
+     *
252
+     * @param name The name to search for
253
+     *
254
+     * @return The source with the specified name or null if none were found.
255
+     */
256
+    public MediaSource getSource(final String name) {
257
+        for (final MediaSource source : getSources()) {
258
+            if (source.getAppName().equalsIgnoreCase(name)) {
259
+                return source;
260
+            }
261
+        }
262
+
263
+        return null;
264
+    }
265
+
266
+    /**
267
+     * Retrieves all the sources registered with this plugin.
268
+     *
269
+     * @return All known media sources
270
+     */
271
+    public List<MediaSource> getSources() {
272
+        final List<MediaSource> res = new ArrayList<>(sources);
273
+
274
+        for (MediaSourceManager manager : managers) {
275
+            res.addAll(manager.getSources());
276
+        }
277
+
278
+        return res;
279
+    }
280
+
281
+    @Override
282
+    public void processEvent(final ActionType type, final StringBuffer format,
283
+            final Object... arguments) {
284
+        if (type == CoreActionType.PLUGIN_LOADED) {
285
+            addPlugin((PluginInfo) arguments[0]);
286
+        } else if (type == CoreActionType.PLUGIN_UNLOADED) {
287
+            removePlugin((PluginInfo) arguments[0]);
288
+        }
289
+    }
290
+
291
+}

+ 46
- 0
src/com/dmdirc/addons/nowplaying/NowPlayingModule.java View File

@@ -0,0 +1,46 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.nowplaying;
24
+
25
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
26
+import com.dmdirc.plugins.PluginDomain;
27
+import com.dmdirc.plugins.PluginInfo;
28
+
29
+import dagger.Module;
30
+import dagger.Provides;
31
+
32
+@Module(injects = {NowPlayingCommand.class, NowPlayingManager.class}, addsTo = SwingModule.class)
33
+public class NowPlayingModule {
34
+
35
+    private final PluginInfo pluginInfo;
36
+
37
+    public NowPlayingModule(final PluginInfo pluginInfo) {
38
+        this.pluginInfo = pluginInfo;
39
+    }
40
+    
41
+    @Provides
42
+    @PluginDomain(NowPlayingPlugin.class)
43
+    public String getSettingsDomain() {
44
+        return pluginInfo.getDomain();
45
+    }
46
+}

+ 26
- 264
src/com/dmdirc/addons/nowplaying/NowPlayingPlugin.java View File

@@ -22,104 +22,70 @@
22 22
 
23 23
 package com.dmdirc.addons.nowplaying;
24 24
 
25
-import com.dmdirc.actions.CoreActionType;
26 25
 import com.dmdirc.addons.ui_swing.UIUtilities;
27 26
 import com.dmdirc.config.prefs.PluginPreferencesCategory;
28 27
 import com.dmdirc.config.prefs.PreferencesCategory;
29 28
 import com.dmdirc.config.prefs.PreferencesDialogModel;
30
-import com.dmdirc.interfaces.ActionController;
31
-import com.dmdirc.interfaces.ActionListener;
32
-import com.dmdirc.interfaces.CommandController;
33
-import com.dmdirc.interfaces.actions.ActionType;
34
-import com.dmdirc.interfaces.config.IdentityController;
35
-import com.dmdirc.plugins.Plugin;
36 29
 import com.dmdirc.plugins.PluginInfo;
37 30
 import com.dmdirc.plugins.implementations.BaseCommandPlugin;
38 31
 
39
-import java.util.ArrayList;
40
-import java.util.Collections;
41
-import java.util.List;
42 32
 import java.util.concurrent.Callable;
43 33
 
34
+import dagger.ObjectGraph;
35
+
44 36
 /**
45 37
  * Plugin that allows users to advertise what they're currently playing or listening to.
46 38
  */
47
-public class NowPlayingPlugin extends BaseCommandPlugin implements ActionListener {
39
+public class NowPlayingPlugin extends BaseCommandPlugin {
48 40
 
49
-    /** The sources that we know of. */
50
-    private final List<MediaSource> sources = new ArrayList<>();
51
-    /** The managers that we know of. */
52
-    private final List<MediaSourceManager> managers = new ArrayList<>();
53
-    /** The user's preferred order for source usage. */
54
-    private List<String> order;
55 41
     /** This plugin's plugin info. */
56 42
     private final PluginInfo pluginInfo;
57
-    /** The action controller to use. */
58
-    private final ActionController actionController;
59
-    /** The identity controller to use. */
60
-    private final IdentityController identityController;
43
+    /** This plugin's settings domain. */
44
+    private final String domain;
45
+    /** Now playing manager. */
46
+    private NowPlayingManager nowplayingmanager;
61 47
 
62 48
     /**
63 49
      * Creates a new instance of this plugin.
64 50
      *
65
-     * @param pluginInfo         This plugin's plugin info
66
-     * @param actionController   The action controller to register listeners with
67
-     * @param commandController  Command controller to register commands
68
-     * @param identityController The identity controller to use.
51
+     * @param pluginInfo This plugin's plugin info
69 52
      */
70
-    public NowPlayingPlugin(final PluginInfo pluginInfo,
71
-            final ActionController actionController,
72
-            final CommandController commandController,
73
-            final IdentityController identityController) {
74
-        super(commandController);
75
-
53
+    public NowPlayingPlugin(final PluginInfo pluginInfo) {
76 54
         this.pluginInfo = pluginInfo;
77
-        this.actionController = actionController;
78
-        this.identityController = identityController;
55
+        this.domain = pluginInfo.getDomain();
56
+    }
79 57
 
80
-        registerCommand(new NowPlayingCommand(commandController, this, identityController),
81
-                NowPlayingCommand.INFO);
58
+    @Override
59
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
60
+        super.load(pluginInfo, graph);
61
+        setObjectGraph(graph.plus(new NowPlayingModule(pluginInfo)));
62
+        registerCommand(NowPlayingCommand.class, NowPlayingCommand.INFO);
63
+        nowplayingmanager = getObjectGraph().get(NowPlayingManager.class);
82 64
     }
83 65
 
84
-    /** {@inheritDoc} */
85 66
     @Override
86 67
     public void onLoad() {
87
-        sources.clear();
88
-        managers.clear();
89
-
90
-        loadSettings();
91
-
92
-        actionController.registerListener(this, CoreActionType.PLUGIN_LOADED,
93
-                CoreActionType.PLUGIN_UNLOADED);
94
-
95
-        for (PluginInfo target : pluginInfo.getMetaData().getManager().getPluginInfos()) {
96
-            if (target.isLoaded()) {
97
-                addPlugin(target);
98
-            }
99
-        }
100 68
         super.onLoad();
69
+        nowplayingmanager.onLoad();
101 70
     }
102 71
 
103
-    /** {@inheritDoc} */
104 72
     @Override
105 73
     public void onUnload() {
106
-        sources.clear();
107
-        managers.clear();
108
-        actionController.unregisterListener(this);
109
-
110 74
         super.onUnload();
75
+        nowplayingmanager.onUnload();
111 76
     }
112 77
 
113
-    /** {@inheritDoc} */
114 78
     @Override
115 79
     public void showConfig(final PreferencesDialogModel manager) {
116 80
         final ConfigPanel configPanel = UIUtilities.invokeAndWait(
117 81
                 new Callable<ConfigPanel>() {
118
-            @Override
119
-            public ConfigPanel call() {
120
-                return new ConfigPanel(identityController, NowPlayingPlugin.this, order);
121
-            }
122
-        });
82
+                    @Override
83
+                    public ConfigPanel call() {
84
+                        return new ConfigPanel(nowplayingmanager, manager.getConfigManager(),
85
+                                manager.getIdentity(), domain,
86
+                                nowplayingmanager.getSettings());
87
+                    }
88
+                });
123 89
 
124 90
         final PreferencesCategory category = new PluginPreferencesCategory(
125 91
                 pluginInfo, "Now Playing",
@@ -127,208 +93,4 @@ public class NowPlayingPlugin extends BaseCommandPlugin implements ActionListene
127 93
         manager.getCategory("Plugins").addSubCategory(category);
128 94
     }
129 95
 
130
-    /**
131
-     * Saves the plugins settings.
132
-     *
133
-     * @param newOrder The new order for sources
134
-     */
135
-    protected void saveSettings(final List<String> newOrder) {
136
-        order = newOrder;
137
-        identityController.getUserSettings().setOption(getDomain(), "sourceOrder", order);
138
-    }
139
-
140
-    /** Loads the plugins settings. */
141
-    private void loadSettings() {
142
-        if (identityController.getGlobalConfiguration().hasOptionString(getDomain(), "sourceOrder")) {
143
-            order = identityController.getGlobalConfiguration().getOptionList(getDomain(),
144
-                    "sourceOrder");
145
-        } else {
146
-            order = new ArrayList<>();
147
-        }
148
-    }
149
-
150
-    /** {@inheritDoc} */
151
-    @Override
152
-    public void processEvent(final ActionType type, final StringBuffer format,
153
-            final Object... arguments) {
154
-        if (type == CoreActionType.PLUGIN_LOADED) {
155
-            addPlugin((PluginInfo) arguments[0]);
156
-        } else if (type == CoreActionType.PLUGIN_UNLOADED) {
157
-            removePlugin((PluginInfo) arguments[0]);
158
-        }
159
-    }
160
-
161
-    /**
162
-     * Checks to see if a plugin implements one of the Media Source interfaces and if it does, adds
163
-     * the source(s) to our list.
164
-     *
165
-     * @param target The plugin to be tested
166
-     */
167
-    private void addPlugin(final PluginInfo target) {
168
-        final Plugin targetPlugin = target.getPlugin();
169
-        if (targetPlugin instanceof MediaSource) {
170
-            sources.add((MediaSource) targetPlugin);
171
-            addSourceToOrder((MediaSource) targetPlugin);
172
-        }
173
-
174
-        if (targetPlugin instanceof MediaSourceManager) {
175
-            managers.add((MediaSourceManager) targetPlugin);
176
-
177
-            if (((MediaSourceManager) targetPlugin).getSources() != null) {
178
-                for (MediaSource source : ((MediaSourceManager) targetPlugin).getSources()) {
179
-                    addSourceToOrder(source);
180
-                }
181
-            }
182
-        }
183
-    }
184
-
185
-    /**
186
-     * Checks to see if the specified media source needs to be added to our order list, and adds it
187
-     * if neccessary.
188
-     *
189
-     * @param source The media source to be tested
190
-     */
191
-    private void addSourceToOrder(final MediaSource source) {
192
-        if (!order.contains(source.getAppName())) {
193
-            order.add(source.getAppName());
194
-        }
195
-    }
196
-
197
-    /**
198
-     * Checks to see if a plugin implements one of the Media Source interfaces and if it does,
199
-     * removes the source(s) from our list.
200
-     *
201
-     * @param target The plugin to be tested
202
-     */
203
-    private void removePlugin(final PluginInfo target) {
204
-        final Plugin targetPlugin = target.getPlugin();
205
-        if (targetPlugin instanceof MediaSource) {
206
-            sources.remove((MediaSource) targetPlugin);
207
-        }
208
-
209
-        if (targetPlugin instanceof MediaSourceManager) {
210
-            managers.remove((MediaSourceManager) targetPlugin);
211
-        }
212
-    }
213
-
214
-    /**
215
-     * Determines if there are any valid sources (paused or not).
216
-     *
217
-     * @return True if there are running sources, false otherwise
218
-     */
219
-    public boolean hasRunningSource() {
220
-        for (final MediaSource source : getSources()) {
221
-            if (source.getState() != MediaSourceState.CLOSED) {
222
-                return true;
223
-            }
224
-        }
225
-
226
-        return false;
227
-    }
228
-
229
-    /**
230
-     * Retrieves the "best" source to use for displaying media information. The best source is
231
-     * defined as the earliest in the list that is running and not paused, or, if no such source
232
-     * exists, the earliest in the list that is running and paused. If neither condition is
233
-     * satisified returns null.
234
-     *
235
-     * @return The best source to use for media info
236
-     */
237
-    public MediaSource getBestSource() {
238
-        MediaSource paused = null;
239
-        final List<MediaSource> possibleSources = getSources();
240
-
241
-        Collections.sort(possibleSources, new MediaSourceComparator(order));
242
-
243
-        for (final MediaSource source : possibleSources) {
244
-            if (source.getState() != MediaSourceState.CLOSED) {
245
-                if (source.getState() == MediaSourceState.PLAYING) {
246
-                    return source;
247
-                } else if (paused == null) {
248
-                    paused = source;
249
-                }
250
-            }
251
-        }
252
-
253
-        return paused;
254
-    }
255
-
256
-    /**
257
-     * Substitutes the keywords in the specified format with the values with values from the
258
-     * specified source.
259
-     *
260
-     * @param format The format to be substituted
261
-     * @param source The source whose values should be used
262
-     *
263
-     * @return The substituted string
264
-     */
265
-    public String doSubstitution(final String format, final MediaSource source) {
266
-        final String artist = source.getArtist();
267
-        final String title = source.getTitle();
268
-        final String album = source.getAlbum();
269
-        final String app = source.getAppName();
270
-        final String bitrate = source.getBitrate();
271
-        final String filetype = source.getFormat();
272
-        final String length = source.getLength();
273
-        final String time = source.getTime();
274
-        final String state = source.getState().getNiceName();
275
-
276
-        return format.replace("$artist", sanitise(artist))
277
-                .replace("$title", sanitise(title))
278
-                .replace("$album", sanitise(album))
279
-                .replace("$app", sanitise(app))
280
-                .replace("$bitrate", sanitise(bitrate))
281
-                .replace("$format", sanitise(filetype))
282
-                .replace("$length", sanitise(length))
283
-                .replace("$state", sanitise(state))
284
-                .replace("$time", sanitise(time));
285
-    }
286
-
287
-    /**
288
-     * Sanitises the specified String so that it may be used as the replacement in a call to
289
-     * String.replaceAll. Namely, at present, this method returns an empty String if it is passed a
290
-     * null value; otherwise the input is returned as-is.
291
-     *
292
-     * @param input The string to be sanitised
293
-     *
294
-     * @return A sanitised version of the String
295
-     *
296
-     * @since 0.6.3
297
-     */
298
-    protected static String sanitise(final String input) {
299
-        return input == null ? "" : input;
300
-    }
301
-
302
-    /**
303
-     * Retrieves a source based on its name.
304
-     *
305
-     * @param name The name to search for
306
-     *
307
-     * @return The source with the specified name or null if none were found.
308
-     */
309
-    public MediaSource getSource(final String name) {
310
-        for (final MediaSource source : getSources()) {
311
-            if (source.getAppName().equalsIgnoreCase(name)) {
312
-                return source;
313
-            }
314
-        }
315
-
316
-        return null;
317
-    }
318
-
319
-    /**
320
-     * Retrieves all the sources registered with this plugin.
321
-     *
322
-     * @return All known media sources
323
-     */
324
-    public List<MediaSource> getSources() {
325
-        final List<MediaSource> res = new ArrayList<>(sources);
326
-
327
-        for (MediaSourceManager manager : managers) {
328
-            res.addAll(manager.getSources());
329
-        }
330
-
331
-        return res;
332
-    }
333
-
334 96
 }

Loading…
Cancel
Save