|
@@ -0,0 +1,162 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
|
|
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.actions.ActionType;
|
|
26
|
+import com.dmdirc.actions.CoreActionType;
|
|
27
|
+import com.dmdirc.plugins.EventPlugin;
|
|
28
|
+import com.dmdirc.plugins.Plugin;
|
|
29
|
+import com.dmdirc.plugins.PluginManager;
|
|
30
|
+import java.util.ArrayList;
|
|
31
|
+import java.util.List;
|
|
32
|
+
|
|
33
|
+public class NowPlayingPlugin extends Plugin implements EventPlugin {
|
|
34
|
+
|
|
35
|
+ /** The sources that we know of. */
|
|
36
|
+ private List<MediaSource> sources;
|
|
37
|
+
|
|
38
|
+ /** {@inheritDoc} */
|
|
39
|
+ public boolean onLoad() {
|
|
40
|
+ return true;
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ /** {@inheritDoc} */
|
|
44
|
+ @Override
|
|
45
|
+ protected void onActivate() {
|
|
46
|
+ sources = new ArrayList<MediaSource>();
|
|
47
|
+
|
|
48
|
+ for (Plugin target : PluginManager.getPluginManager().getPlugins()) {
|
|
49
|
+ if (target.isActive()) {
|
|
50
|
+ addPlugin(target);
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ /** {@inheritDoc} */
|
|
56
|
+ @Override
|
|
57
|
+ protected void onDeactivate() {
|
|
58
|
+ sources = null;
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ /** {@inheritDoc} */
|
|
62
|
+ public String getVersion() {
|
|
63
|
+ return "0.1";
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ /** {@inheritDoc} */
|
|
67
|
+ public String getAuthor() {
|
|
68
|
+ return "Chris <chris@dmdirc.com>";
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ /** {@inheritDoc} */
|
|
72
|
+ public String getDescription() {
|
|
73
|
+ return "Adds a nowplaying command";
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ /** {@inheritDoc} */
|
|
77
|
+ public String toString() {
|
|
78
|
+ return "Now playing command";
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ /** {@inheritDoc} */
|
|
82
|
+ public void processEvent(ActionType type, StringBuffer format, Object... arguments) {
|
|
83
|
+ if (type == CoreActionType.PLUGIN_ACTIVATED) {
|
|
84
|
+ addPlugin((Plugin) arguments[0]);
|
|
85
|
+ } else if (type == CoreActionType.PLUGIN_DEACTIVATED) {
|
|
86
|
+ removePlugin((Plugin) arguments[0]);
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ /**
|
|
91
|
+ * Checks to see if a plugin implements one of the Media Source interfaces
|
|
92
|
+ * and if it does, adds the source(s) to our list.
|
|
93
|
+ *
|
|
94
|
+ * @param target The plugin to be tested
|
|
95
|
+ */
|
|
96
|
+ private void addPlugin(final Plugin target) {
|
|
97
|
+ if (target instanceof MediaSource) {
|
|
98
|
+ sources.add((MediaSource) target);
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ if (target instanceof MediaSourceManager) {
|
|
102
|
+ sources.addAll(((MediaSourceManager) target).getSources());
|
|
103
|
+ }
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ /**
|
|
107
|
+ * Checks to see if a plugin implements one of the Media Source interfaces
|
|
108
|
+ * and if it does, removes the source(s) from our list.
|
|
109
|
+ *
|
|
110
|
+ * @param target The plugin to be tested
|
|
111
|
+ */
|
|
112
|
+ private void removePlugin(final Plugin target) {
|
|
113
|
+ if (target instanceof MediaSource) {
|
|
114
|
+ sources.remove((MediaSource) target);
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ if (target instanceof MediaSourceManager) {
|
|
118
|
+ sources.removeAll(((MediaSourceManager) target).getSources());
|
|
119
|
+ }
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ /**
|
|
123
|
+ * Determines if there are any valid sources (paused or not).
|
|
124
|
+ *
|
|
125
|
+ * @return True if there are running sources, false otherwise
|
|
126
|
+ */
|
|
127
|
+ public boolean hasRunningSource() {
|
|
128
|
+ for (MediaSource source : sources) {
|
|
129
|
+ if (source.isRunning()) {
|
|
130
|
+ return true;
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ return false;
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ /**
|
|
138
|
+ * Retrieves the "best" source to use for displaying media information.
|
|
139
|
+ * The best source is defined as the earliest in the list that is running
|
|
140
|
+ * and not paused, or, if no such source exists, the earliest in the list
|
|
141
|
+ * that is running and paused. If neither condition is satisified returns
|
|
142
|
+ * null.
|
|
143
|
+ *
|
|
144
|
+ * @return The best source to use for media info
|
|
145
|
+ */
|
|
146
|
+ public MediaSource getBestSource() {
|
|
147
|
+ MediaSource paused = null;
|
|
148
|
+
|
|
149
|
+ for (MediaSource source : sources) {
|
|
150
|
+ if (source.isRunning()) {
|
|
151
|
+ if (source.isPlaying()) {
|
|
152
|
+ return source;
|
|
153
|
+ } else if (paused == null) {
|
|
154
|
+ paused = source;
|
|
155
|
+ }
|
|
156
|
+ }
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ return paused;
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+}
|