File Source: DllSource.java
/*
P/P * Method: com.dmdirc.addons.mediasource_windows.DllSource__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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.mediasource_windows;
24
25 import com.dmdirc.addons.nowplaying.MediaSource;
26 import com.dmdirc.addons.nowplaying.MediaSourceState;
27
28 /**
29 * Uses WindowsMediaSourcePlugin to retrieve now playing info.
30 *
31 * @author Shane
32 */
33 public class DllSource implements MediaSource {
34 /** Player name */
35 private final String playerName;
36
37 /** Use getArtistTitle */
38 private final boolean useArtistTitle;
39
40
41 /**
42 * Instantiates the media source.
43 *
44 * @param playerName Name of Player and DLL
45 */
46 public DllSource(final String playerName) {
/*
P/P * Method: void com.dmdirc.addons.mediasource_windows.DllSource(String)
*
* Postconditions:
* this.playerName == playerName
* init'ed(this.playerName)
* this.useArtistTitle == 0
*/
47 this(playerName, false);
48 }
49
50 /**
51 * Instantiates the media source.
52 *
53 * @param playerName Name of Player and DLL
54 * @param useArtistTitle True if getArtistTitle should be parsed rather than
55 * using getArtist() and getTitle()
56 */
/*
P/P * Method: void com.dmdirc.addons.mediasource_windows.DllSource(String, bool)
*
* Postconditions:
* this.playerName == playerName
* init'ed(this.playerName)
* this.useArtistTitle == useArtistTitle
* init'ed(this.useArtistTitle)
*/
57 public DllSource(final String playerName, final boolean useArtistTitle) {
58 this.playerName = playerName;
59 this.useArtistTitle = useArtistTitle;
60 }
61
62 /** {@inheritDoc} */
63 @Override
64 public String getAppName() {
/*
P/P * Method: String getAppName()
*
* Postconditions:
* return_value == this.playerName
* init'ed(return_value)
*/
65 return playerName;
66 }
67
68 /**
69 * Get the "goodoutput" from GetMediaInfo for the given command
70 *
71 * @param command Command to run
72 * @return "Good" Output
73 */
74 private String getOutput(final String command) {
/*
P/P * Method: String getOutput(String)
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* init'ed(return_value)
*/
75 return WindowsMediaSourcePlugin.getOutput(playerName, command).getGoodOutput();
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public MediaSourceState getState() {
/*
P/P * Method: MediaSourceState getState()
*
* Presumptions:
* result.output != null
*
* Postconditions:
* return_value in Addr_Set{&com.dmdirc.addons.nowplaying.MediaSourceState__static_init.new MediaSourceState(MediaSourceState__static_init#1),&com.dmdirc.addons.nowplaying.MediaSourceState__static_init.new MediaSourceState(MediaSourceState__static_init#5),&com.dmdirc.addons.nowplaying.MediaSourceState__static_init.new MediaSourceState(MediaSourceState__static_init#3),&com.dmdirc.addons.nowplaying.MediaSourceState__static_init.new MediaSourceState(MediaSourceState__static_init#4),&com.dmdirc.addons.nowplaying.MediaSourceState__static_init.new MediaSourceState(MediaSourceState__static_init#2)}
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@85: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@87: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@89: {0}, {1}
* result.exitCode: {-231..-1, 1..232-1}, {0}
*/
81 final MediaInfoOutput result = WindowsMediaSourcePlugin.getOutput(playerName, "getPlayState");
82
83 if (result.getExitCode() == 0) {
84 final String output = result.getGoodOutput();
85 if (output.equalsIgnoreCase("stopped")) {
86 return MediaSourceState.STOPPED;
87 } else if (output.equalsIgnoreCase("playing")) {
88 return MediaSourceState.PLAYING;
89 } else if (output.equalsIgnoreCase("paused")) {
90 return MediaSourceState.PAUSED;
91 } else {
92 return MediaSourceState.NOTKNOWN;
93 }
94 } else {
95 return MediaSourceState.CLOSED;
96 }
97 }
98
99 /** {@inheritDoc} */
100 @Override
101 public String getArtist() {
/*
P/P * Method: String getArtist()
*
* Presumptions:
* getOutput(...)@103 init'ed
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* possibly_updated(return_value)
*
* Test Vectors:
* this.useArtistTitle: {0}, {1}
*/
102 if (useArtistTitle) {
103 return getOutput("getArtistTitle").split("\\s-\\s", 2)[0];
104 } else {
105 return getOutput("getArtist");
106 }
107 }
108
109 /** {@inheritDoc} */
110 @Override
111 public String getTitle() {
/*
P/P * Method: String getTitle()
*
* Presumptions:
* getOutput(...)@113 init'ed
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* init'ed(return_value)
*
* Test Vectors:
* this.useArtistTitle: {0}, {1}
*/
112 if (useArtistTitle) {
113 String bits[] = getOutput("getArtistTitle").split("\\s-\\s", 2);
114 return (bits.length > 1) ? bits[1] : "";
115 } else {
116 return getOutput("getTitle");
117 }
118 }
119
120 /** {@inheritDoc} */
121 @Override
122 public String getAlbum() {
/*
P/P * Method: String getAlbum()
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* init'ed(return_value)
*/
123 return getOutput("getAlbum");
124 }
125
126 /**
127 * Get the duration in seconds as a string.
128 *
129 * @param secondsInput to get duration for
130 * @return Duration as a string
131 */
132 private String duration(final long secondsInput) {
/*
P/P * Method: String duration(long)
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &java.lang.StringBuilder:toString(...)
*
* Test Vectors:
* secondsInput: {-263..3_599}, {3_600..264-1}
* secondsInput/3_600: {-2_562_047_788_015_215..0}, {1..5_124_095_576_030_431}
* secondsInput/60: {-153_722_867_280_912_930..59}, {60..307_445_734_561_825_860}
*/
133 final StringBuilder result = new StringBuilder();
134 final long hours = (secondsInput / 3600);
135 final long minutes = (secondsInput / 60 % 60);
136 final long seconds = (secondsInput % 60);
137
138 if (hours > 0) { result.append(hours+":"); }
139 result.append(String.format("%0,2d:%0,2d",minutes,seconds));
140
141 return result.toString();
142 }
143
144 /** {@inheritDoc} */
145 @Override
146 public String getLength() {
147 try {
/*
P/P * Method: String getLength()
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value in Addr_Set{&java.lang.StringBuilder:toString(...),&"Unknown"}
*/
148 final int seconds = Integer.parseInt(getOutput("getLength"));
149 return duration(seconds);
150 } catch (NumberFormatException nfe) { }
151 return "Unknown";
152 }
153
154 /** {@inheritDoc} */
155 @Override
156 public String getTime() {
157 try {
/*
P/P * Method: String getTime()
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value in Addr_Set{&java.lang.StringBuilder:toString(...),&"Unknown"}
*/
158 final int seconds = Integer.parseInt(getOutput("getTime"));
159 return duration(seconds);
160 } catch (NumberFormatException nfe) { }
161 return "Unknown";
162 }
163
164 /** {@inheritDoc} */
165 @Override
166 public String getFormat() {
/*
P/P * Method: String getFormat()
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* init'ed(return_value)
*/
167 return getOutput("getFormat");
168 }
169
170 /** {@inheritDoc} */
171 @Override
172 public String getBitrate() {
/*
P/P * Method: String getBitrate()
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* init'ed(return_value)
*/
173 return getOutput("getBitrate");
174 }
175 }
SofCheck Inspector Build Version : 2.17854
| DllSource.java |
2009-Jun-25 01:54:24 |
| DllSource.class |
2009-Sep-02 17:04:15 |