Browse Source

Work on the nowplaying plugin

git-svn-id: http://svn.dmdirc.com/trunk@1569 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5
Chris Smith 17 years ago
parent
commit
c43c346463

+ 58
- 0
src/com/dmdirc/addons/dcop/AmarokSource.java View File

@@ -0,0 +1,58 @@
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.dcop;
24
+
25
+import com.dmdirc.addons.nowplaying.MediaSource;
26
+
27
+/**
28
+ * Uses DCOP to retrieve now playing info from Amarok.
29
+ * 
30
+ * @author chris
31
+ */
32
+public class AmarokSource implements MediaSource {
33
+
34
+    /** {@inheritDoc} */
35
+    public boolean isRunning() {
36
+        final String result = DcopPlugin.getDcopResult("dcop amarok player isPlaying").get(0);
37
+        
38
+        return result.indexOf("failed") != -1;
39
+    }
40
+
41
+    /** {@inheritDoc} */
42
+    public boolean isPlaying() {
43
+        final String result = DcopPlugin.getDcopResult("dcop amarok player isPlaying").get(0);
44
+        
45
+        return Boolean.parseBoolean(result);
46
+    }
47
+
48
+    /** {@inheritDoc} */
49
+    public String getInformation() {       
50
+        return DcopPlugin.getDcopResult("dcop amarok player nowPlaying").get(0);
51
+    }
52
+
53
+    /** {@inheritDoc} */
54
+    public String getName() {
55
+        return "Amarok";
56
+    }
57
+    
58
+}

+ 3
- 7
src/com/dmdirc/addons/dcop/DcopCommand.java View File

@@ -56,13 +56,9 @@ public final class DcopCommand extends ServerCommand {
56 56
      */
57 57
     public void execute(final InputWindow origin, final Server server,
58 58
             final boolean isSilent, final String... args) {
59
-        try {
60
-            final List<String> res = DcopPlugin.getDcopResult("dcop " + implodeArgs(args));
61
-            for (String line : res) {
62
-                sendLine(origin, isSilent, "commandOutput", line);
63
-            }
64
-        } catch (IOException ex) {
65
-            Logger.error(ErrorLevel.ERROR, "Unable to execute dcop", ex);
59
+        final List<String> res = DcopPlugin.getDcopResult("dcop " + implodeArgs(args));
60
+        for (String line : res) {
61
+            sendLine(origin, isSilent, "commandOutput", line);
66 62
         }
67 63
     }
68 64
     

+ 37
- 11
src/com/dmdirc/addons/dcop/DcopPlugin.java View File

@@ -22,6 +22,8 @@
22 22
 
23 23
 package com.dmdirc.addons.dcop;
24 24
 
25
+import com.dmdirc.addons.nowplaying.MediaSource;
26
+import com.dmdirc.addons.nowplaying.MediaSourceManager;
25 27
 import com.dmdirc.plugins.Plugin;
26 28
 
27 29
 import java.io.BufferedReader;
@@ -32,31 +34,46 @@ import java.util.List;
32 34
 
33 35
 /**
34 36
  * Allows the user to execute dcop commands (and read the results).
37
+ * 
35 38
  * @author chris
36 39
  */
37
-public final class DcopPlugin extends Plugin {
40
+public final class DcopPlugin extends Plugin implements MediaSourceManager {
38 41
     
39 42
     /** Creates a new instance of DcopPlugin. */
40 43
     public DcopPlugin() {
41
-        
44
+        super();
42 45
     }
43 46
     
44 47
     /**
45 48
      * Retrieves the result from executing the specified command.
49
+     *
46 50
      * @param command The command to be executed
47 51
      * @return The output of the specified command
48
-     * @throws IOException on dcop failure
49 52
      */
50
-    public static List<String> getDcopResult(final String command) throws IOException {
53
+    public static List<String> getDcopResult(final String command) {
51 54
         final ArrayList<String> result = new ArrayList<String>();
55
+
56
+        InputStreamReader reader;
57
+        BufferedReader input;
58
+        Process process;
52 59
         
53
-        final Process process = Runtime.getRuntime().exec(command);
54
-        final BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
55
-        
56
-        String line = "";
57
-        
58
-        while ((line = input.readLine()) != null) {
59
-            result.add(line);
60
+        try {
61
+            process = Runtime.getRuntime().exec(command);
62
+            
63
+            reader = new InputStreamReader(process.getInputStream());
64
+            input = new BufferedReader(reader);
65
+            
66
+            String line = "";
67
+            
68
+            while ((line = input.readLine()) != null) {
69
+                result.add(line);
70
+            }
71
+            
72
+            reader.close();
73
+            input.close();
74
+            process.destroy();
75
+        } catch (IOException ex) {
76
+            // Do nothing
60 77
         }
61 78
         
62 79
         return result;
@@ -94,4 +111,13 @@ public final class DcopPlugin extends Plugin {
94 111
     public String toString() {
95 112
         return "DCOP Plugin";
96 113
     }
114
+
115
+    /** {@inheritDoc} */
116
+    public List<MediaSource> getSources() {
117
+        final ArrayList<MediaSource> res = new ArrayList<MediaSource>();
118
+        
119
+        res.add(new AmarokSource());
120
+        
121
+        return res;
122
+    }
97 123
 }

+ 8
- 13
src/com/dmdirc/addons/dcop/NowPlayingCommand.java View File

@@ -56,20 +56,15 @@ public final class NowPlayingCommand extends ChannelCommand {
56 56
      * @param channel The channel object this command is associated with
57 57
      * @param isSilent Whether this command is silenced or not
58 58
      * @param args The user supplied arguments
59
-     */    
60
-    public void execute(final InputWindow origin, final Server server, 
59
+     */
60
+    public void execute(final InputWindow origin, final Server server,
61 61
             final Channel channel, final boolean isSilent, final String... args) {
62
-        try {
63
-            
64
-            final List<String> res = DcopPlugin.getDcopResult("dcop amarok default nowPlaying");
65
-            
66
-            channel.sendAction("is listening to " + res.get(0));
67
-            
68
-        } catch (IOException ex) {
69
-            Logger.error(ErrorLevel.ERROR, "Unable to execute dcop", ex);
70
-        }        
71
-    }
62
+        final List<String> res = DcopPlugin.getDcopResult("dcop amarok default nowPlaying");
72 63
         
64
+        channel.sendAction("is listening to " + res.get(0));
65
+        
66
+    }
67
+    
73 68
     
74 69
     
75 70
     /** {@inheritDoc}. */
@@ -96,5 +91,5 @@ public final class NowPlayingCommand extends ChannelCommand {
96 91
     public String getHelp() {
97 92
         return "nowplaying - tells the channel the song you're currently playing";
98 93
     }
99
-
94
+    
100 95
 }

+ 9
- 2
src/com/dmdirc/addons/nowplaying/MediaSource.java View File

@@ -31,14 +31,14 @@ package com.dmdirc.addons.nowplaying;
31 31
 public interface MediaSource {
32 32
     
33 33
     /**
34
-     * Determine if the application for this source is running or not
34
+     * Determine if the application for this source is running or not.
35 35
      * 
36 36
      * @return True if this source is running, false otherwise
37 37
      */
38 38
     boolean isRunning();
39 39
     
40 40
     /**
41
-     * Determine if this source is currently playing or not
41
+     * Determine if this source is currently playing or not.
42 42
      * 
43 43
      * @return True if this source is playing, false otherwise
44 44
      */
@@ -51,5 +51,12 @@ public interface MediaSource {
51 51
      * @return The currently playing media
52 52
      */
53 53
     String getInformation();
54
+    
55
+    /**
56
+     * Retrieves the name of the application that this source is for.
57
+     * 
58
+     * @return This source's application name
59
+     */
60
+    String getName();
54 61
 
55 62
 }

+ 162
- 0
src/com/dmdirc/addons/nowplaying/NowPlayingPlugin.java View File

@@ -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
+}

Loading…
Cancel
Save