~dhananjaysathe/nuvola-player/TelepathyExt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright 2012 Jiří Janoušek <janousek.jiri@gmail.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

string prog_name;
MPRISPlayer player;
MPRISApplication app;

[CCode(cheader_filename = "config.h")]
namespace Config
{
	[CCode(cname="G_LOG_DOMAIN")]
	extern extern const string LOG_DOMAIN;
	[CCode(cname="GETTEXT_PACKAGE")]
	extern const string GETTEXT_PACKAGE;
	[CCode(cname="LOCALES_DIR")]
	extern const string LOCALES_DIR;
	extern const string APPNAME;
}

/**
 * Main entry point
 */
int main(string[] args){
	const string SERVICE_NAME = "org.mpris.MediaPlayer2." + Config.APPNAME;
	const string SERVICE_OBJECT = "/org/mpris/MediaPlayer2";
	prog_name = args[0];
	
	Intl.textdomain(Config.APPNAME);
	Intl.bindtextdomain(Config.APPNAME, Config.LOCALES_DIR);
	
	try{
		app = Bus.get_proxy_sync(BusType.SESSION, SERVICE_NAME, SERVICE_OBJECT);
	}
	catch(IOError e){
		/// Nuvola Player client error message; %s will be replaced by a message
		stderr.printf(_("Unable to connect to application interface: %s\n"), e.message);
		return 1;
	}
	
	try{
		player = Bus.get_proxy_sync(BusType.SESSION, SERVICE_NAME, SERVICE_OBJECT);
	}
	catch(IOError e){
		/// Nuvola Player client error message; %s will be replaced by a message
		stderr.printf(_("Unable to connect to player interface: %s\n"), e.message);
		return 1;
	}
	
	if(args.length == 1){
		usage(prog_name);
		return 1;
	}
	else if(args.length > 2){
		/// Nuvola Player client error message
		usage(prog_name, _("Wrong number of arguments.\n"));
		return 1;
	}
	
	int exit_code;
	if (!call_command(args[1], out exit_code)){
		// DBus Call failed
		stderr.printf("\n%s\n%s\n",
		_("Unable to connect to the Remote Player Interface of Nuvola Player."),
		_("Make sure Nuvola Player is running and extension 'Remote Player Interface' is enabled in the Preferences dialog."));
		return 2;
	}
	
	return exit_code;
}

/**
 * Call client command.
 * 
 * @param cmd				command to execute
 * @param return_code	application exit code
 * @return					false if call failed because of invalid DBus proxy object,
 * 							true otherwise
 */
bool call_command(string cmd, out int exit_code){
	exit_code = -1;
	/* DBus call to invalid DBus object proxy produces error message and
	 * returns false instead of throwing IOError. See generated C code for
	 * details. */
	try{
		switch(cmd){
		case "play": player.play(); break;
		case "pause": player.pause(); break;
		case "toggle": player.play_pause(); break;
		case "next": player.next(); break;
		case "prev": player.previous(); break;
		case "raise": app.raise(); break;
		case "quit": app.quit(); break;
		case "status":
			var status = player.playback_status;
			stdout.printf("Status: %s\n", status);
			if(status != "Stopped"){
				var metadata = player.metadata;
				
				string song = metadata.lookup("xesam:title").get_string();
				if(song != ""){
					/// Nuvola Player client, output of 'status' command; %s will be replaced by a song name
					stdout.printf(_("Song: %s\n"), song);
				}
				
				string[] artistArray = metadata.lookup("xesam:artist").dup_strv();
				string artist = artistArray.length > 0 ? artistArray[0] : "";
				if(artist != ""){
					/// Nuvola Player client, output of 'status' command; %s will be replaced by an artist name
					stdout.printf(_("Artist: %s\n"), artist);
				}
				
				string album = metadata.lookup("xesam:album").get_string();
				if(album != ""){
					/// Nuvola Player client, output of 'status' command; %s will be replaced by an album name
					stdout.printf(_("Album: %s\n"), album);
				}
				
			}
			break;
		default:
			/// Nuvola Player client, error message, %s = argument name
			usage(prog_name, _("Wrong argument: %s\n").printf(cmd));
			exit_code = 1;
			return true;
		}
	}
	catch(IOError e){
		/// Nuvola Player client, error message, %1$s = commnad name, %2$s = message
		stderr.printf("Unable to execute command %1$s: %2$s\n", cmd, e.message);
		exit_code = 1;
		return true;
	}
	
	exit_code = 0;
	return true;
}

void usage(string command, string? error=null){
	if(error != null){
		stderr.puts(error);
		stderr.puts("\n");
	}
	/// Nuvola Player client, usage information, %s will be replaced by executable name, do not translate command names (command name: description)
	stderr.printf("""Usage:
  %s [COMMAND]

Available commands:
  status    print current status (playback state, song info)
  play      start playback
  pause     pause playback
  toggle    toggle play/pause
  next      skip to next song
  prev      skip to previous song
  raise     raise Nuvola Player window
  quit      quit Nuvola Player
""", command);
}

[DBus(name = "org.mpris.MediaPlayer2")]
public interface MPRISApplication: GLib.Object{
	public abstract void raise() throws IOError;
	public abstract void quit() throws IOError;
}

[DBus(name = "org.mpris.MediaPlayer2.Player")]
public interface MPRISPlayer : GLib.Object{
	public abstract string playback_status { owned get; private set;}
	public abstract HashTable<string,Variant>? metadata { owned get; private set;}
	public abstract void next() throws IOError;
	public abstract void previous() throws IOError;
	public abstract void pause() throws IOError;
	public abstract void play_pause() throws IOError;
	public abstract void stop() throws IOError;
	public abstract void play() throws IOError;
}