File: main.c

package info (click to toggle)
rhythmbox 0.8.8-13
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,004 kB
  • ctags: 4,769
  • sloc: ansic: 44,295; sh: 8,373; xml: 3,595; makefile: 687
file content (486 lines) | stat: -rw-r--r-- 13,484 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/*
 *  arch-tag: The Rhythmbox main entrypoint
 *
 *  Copyright (C) 2002 Jorn Baayen
 *  Copyright (C) 2003 Colin Walters <walters@gnome.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <config.h>
#include <libintl.h>
#include <locale.h>
#include <libgnome/gnome-program.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-ui-init.h>
#include <gtk/gtk.h>
#include <bonobo/Bonobo.h>
#include <bonobo/bonobo-main.h>
#include <libbonobo.h>
#include <bonobo/bonobo-property-bag-client.h>
#include <glade/glade-init.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#ifdef HAVE_GSTREAMER
#include <gst/gst.h>
#include <gst/gconf/gconf.h>
#include <gst/control/control.h>
#endif
#ifdef WITH_MONKEYMEDIA
#include "monkey-media.h"
#endif

#include "rb-shell.h"
#include "rb-debug.h"
#include "rb-dialog.h"
#include "rb-file-helpers.h"
#include "rb-string-helpers.h"
#include "rb-thread-helpers.h"
#include "rb-stock-icons.h"
#include "eel-gconf-extensions.h"

static gboolean rb_init (RBShell *shell);
static void rb_handle_cmdline (char **argv, int argc,
			       gboolean already_running);

static CORBA_Environment ev;

static gboolean debug           = FALSE;
static gboolean quit            = FALSE;
static gboolean no_registration = FALSE;
static gboolean no_update	= FALSE;
static gboolean dry_run		= FALSE;
static char *rhythmdb_file = NULL;
static gboolean print_playing = FALSE;
static gboolean print_playing_path = FALSE;
static gboolean playpause       = FALSE;
static gboolean focus           = FALSE;
static gboolean previous        = FALSE;
static gboolean next            = FALSE;
static gboolean volume_up       = FALSE;
static gboolean volume_down     = FALSE;
static gboolean toggle_mute     = FALSE;
static gboolean rate_up         = FALSE;
static gboolean rate_down       = FALSE;
static gboolean shuffle         = FALSE;
static gboolean print_play_time = FALSE;
static gboolean print_song_length = FALSE;
static long seek_time           = 0;

int
main (int argc, char **argv)
{
	GnomeProgram *program;
	CORBA_Object object;
	RBShell *rb_shell;
	char **new_argv;

	struct poptOption popt_options[] =
	{
		{ "print-playing",	0,  POPT_ARG_NONE,          &print_playing,                                  0, N_("Print the playing song and exit"),     NULL },
		{ "print-playing-path",	0,  POPT_ARG_NONE,          &print_playing_path,                          0, N_("Print the playing song URI and exit"),     NULL },
        
        { "print-song-length",	0,  POPT_ARG_NONE,			&print_song_length,		0, N_("Print the playing song length in seconds and exit"),	NULL },
        { "print-play-time", 	0,  POPT_ARG_NONE,			&print_play_time,		0, N_("Print the current elapsed time of playing song and exit"),	NULL },
        { "set-play-time", 	    0,  POPT_ARG_LONG,			&seek_time,				0, N_("Seek to the specified time in playing song if possible and exit"),	NULL },
        
		{ "play-pause",		    0,  POPT_ARG_NONE,			&playpause,	        	0, N_("Toggle play/pause mode"),     NULL },
		{ "focus",	    0,  POPT_ARG_NONE,			&focus,	        	0, N_("Focus the running player"),     NULL },
		{ "previous",		    0,  POPT_ARG_NONE,			&previous,	        	0, N_("Jump to previous song"),     NULL },
		{ "next",		        0,  POPT_ARG_NONE,			&next,		        	0, N_("Jump to next song"),     NULL },
		{ "volume-up",		        0,  POPT_ARG_NONE,			&volume_up,		        	0, N_("Turn up volume"),     NULL },
		{ "volume-down",       	        0,  POPT_ARG_NONE,			&volume_down,		        	0, N_("Turn down volume"),     NULL },
		{ "toggle-mute",		0,  POPT_ARG_NONE,			&toggle_mute,		        	0, N_("Toggle muting"),     NULL },
		{ "rate-up",		        0,  POPT_ARG_NONE,			&rate_up,		        	0, N_("Increase rating"),     NULL },
		{ "rate-down",       	        0,  POPT_ARG_NONE,			&rate_down,		        	0, N_("Decrease rating"),     NULL },
		
		{ "shuffle",		        0,  POPT_ARG_NONE,			&shuffle,		        	0, N_("Toggle shuffling"),     NULL },

		{ "debug",           'd',  POPT_ARG_NONE,          &debug,                                        0, N_("Enable debugging code"),     NULL },
		{ "no-update", 0,  POPT_ARG_NONE,          &no_update,                              0, N_("Do not update the library"), NULL },
		{ "no-registration", 'n',  POPT_ARG_NONE,          &no_registration,                              0, N_("Do not register the shell"), NULL },
		{ "dry-run", 0,  POPT_ARG_NONE,          &dry_run,                             0, N_("Don't save any data permanently (implies --no-registration)"), NULL },
		{ "rhythmdb-file", 0,  POPT_ARG_STRING,          &rhythmdb_file,                             0, N_("Path for database file to use"), NULL },
		{ "quit",            'q',  POPT_ARG_NONE,          &quit,                                         0, N_("Quit Rhythmbox"),            NULL },
#ifdef HAVE_GSTREAMER
		{NULL, '\0', POPT_ARG_INCLUDE_TABLE, NULL, 0, "GStreamer", NULL},
#endif
		POPT_TABLEEND
	};

	/* Disable event sounds for now by passing "--disable-sound" to libgnomeui.
	 * See: http://bugzilla.gnome.org/show_bug.cgi?id=119222 */
	new_argv = g_strdupv (argv);
	new_argv = g_realloc (new_argv, (argc+2)*sizeof(char*));
	new_argv[argc] = g_strdup ("--disable-sound");
	new_argv[argc+1] = NULL;

#ifdef HAVE_GSTREAMER	
	popt_options[(sizeof(popt_options)/sizeof(popt_options[0]))-2].arg
		= (void *) gst_init_get_popt_table ();
#endif

	gtk_set_locale ();
	program = gnome_program_init (PACKAGE, VERSION,
				      LIBGNOMEUI_MODULE, argc+1, new_argv,
				      GNOME_PARAM_POPT_TABLE, popt_options,
				      GNOME_PARAM_HUMAN_READABLE_NAME, _("Rhythmbox"),
				      GNOME_PARAM_APP_DATADIR, DATADIR,
				      NULL);

	/* Disabled because it breaks internet radio and other things -
	 * doing synchronous calls in the main thread causes deadlock.
	 * This is too hard to fix right now, so we punt.
	 */
#if 0
	gnome_authentication_manager_init ();
#endif

	g_random_set_seed (time(0));

#ifdef ENABLE_NLS
	/* initialize i18n */
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
#endif

#ifdef HAVE_GSTREAMER	
	gst_control_init (&argc, &argv);
#endif
#ifdef WITH_MONKEYMEDIA
	monkey_media_init (&argc, &argv);
#endif

	rb_debug_init (debug);
	
	gdk_threads_init ();

	CORBA_exception_init (&ev);

	rb_debug ("initializing Rhythmbox %s", VERSION);

	rb_file_helpers_init ();
	rb_string_helpers_init ();

	if (no_registration == FALSE && dry_run == FALSE) {
		object = bonobo_activation_activate_from_id (RB_SHELL_OAFIID,
			 				     Bonobo_ACTIVATION_FLAG_EXISTING_ONLY,
							     NULL, NULL);
	}
	else
		object = NULL;

	if (object == NULL) {
		rb_debug ("Going to create a new shell");

		glade_gnome_init ();

		rb_stock_icons_init ();

		rb_shell = rb_shell_new (argc, argv, no_registration,
					 no_update, dry_run, rhythmdb_file);
		
		g_idle_add ((GSourceFunc) rb_init, rb_shell);
		
		bonobo_main ();
	} else {
		rb_debug ("already running");

		rb_handle_cmdline (argv, argc, TRUE);
	}

	/* cleanup */
	CORBA_exception_free (&ev);

#ifdef WITH_MONKEYMEDIA
	monkey_media_shutdown ();
#endif
	rb_file_helpers_shutdown ();
	rb_string_helpers_shutdown ();
	if (object == NULL)
		rb_stock_icons_shutdown ();

	g_strfreev (new_argv);

	return 0;
}

static gboolean
rb_init (RBShell *shell)
{
	char **argv;
	int argc;

	GDK_THREADS_ENTER ();
	
	rb_shell_construct (shell);

	g_object_get (G_OBJECT (shell), "argc", &argc, "argv", &argv, NULL);

	if (!no_registration && !dry_run)
		rb_handle_cmdline (argv, argc, FALSE);
	
	GDK_THREADS_LEAVE ();

	return FALSE;
}

static GNOME_Rhythmbox_SongInfo*
get_song_info (GNOME_Rhythmbox shell)
{
	Bonobo_PropertyBag pb;
	CORBA_any *any;
	GNOME_Rhythmbox_SongInfo *song_info = NULL;

	pb = GNOME_Rhythmbox_getPlayerProperties (shell, &ev);
	if (BONOBO_EX (&ev)) {
		char *err = bonobo_exception_get_text (&ev);
		g_warning (_("An exception occured '%s'"), err);
		return NULL;
	}

	any = bonobo_pbclient_get_value (pb, "song", 
					 TC_GNOME_Rhythmbox_SongInfo, 
					 &ev);
	if (BONOBO_EX (&ev)) {
		char *err = bonobo_exception_get_text (&ev);
		g_warning (_("An exception occured '%s'"), err);
		g_free (err);
		bonobo_object_release_unref ((Bonobo_Unknown) pb, &ev);
		return NULL;
	}
	
	if ((any == NULL) || (!CORBA_TypeCode_equivalent (any->_type, TC_GNOME_Rhythmbox_SongInfo, NULL))) {
		song_info = NULL;
	} else {
		song_info = (GNOME_Rhythmbox_SongInfo*)any->_value;
		any->_release = FALSE;
		CORBA_free (any);
	}

	bonobo_object_release_unref ((Bonobo_Unknown) pb, &ev);
	return song_info;
}

static void
rb_toggle_shuffle (GNOME_Rhythmbox shell)
{
	Bonobo_PropertyBag pb;
	gboolean shuffle;
		
	pb = GNOME_Rhythmbox_getPlayerProperties (shell, &ev);

	g_return_if_fail (!BONOBO_EX (&ev));

	shuffle = bonobo_pbclient_get_boolean (pb,
					       "shuffle",
					       &ev);
	g_return_if_fail (!BONOBO_EX (&ev));

		
	bonobo_pbclient_set_boolean (pb,
				     "shuffle",
				     shuffle ? FALSE : TRUE,
				     &ev);
	g_return_if_fail (!BONOBO_EX (&ev));

	bonobo_object_release_unref ((Bonobo_Unknown)pb, &ev);
}


static void
rb_handle_cmdline (char **argv, int argc,
		   gboolean already_running)
{
	GNOME_Rhythmbox shell;
	int i;
	gboolean grab_focus = TRUE;
	
	/*
	 * All 'remote control' type actions should set grab_focus 
	 * to false.  There are two ways to focus the window.  
	 * Running 'rhythmbox' with no arguments when it is already
	 * running.  Explicitly adding the --focus argument combined 
	 * with other 'remote control' arguments.
	 */

	shell = bonobo_activation_activate_from_id (RB_SHELL_OAFIID, 0, NULL, &ev);
	if (shell == NULL)
	{
		char *msg = rb_shell_corba_exception_to_string (&ev);
		rb_warning_dialog (_("Failed to activate the shell:\n%s"), msg);
		g_free (msg);
		
		return;
	}

	if (rate_up)
	{
		GNOME_Rhythmbox_SongInfo *song_info;

		song_info = get_song_info (shell);
		if (song_info != NULL) {
			GNOME_Rhythmbox_setRating (shell, song_info->rating + 1, &ev);
			CORBA_free (song_info);
		}
		grab_focus = FALSE;
	}

	if (rate_down)
	{
		GNOME_Rhythmbox_SongInfo *song_info;

		song_info = get_song_info (shell);
		if (song_info != NULL) {
			GNOME_Rhythmbox_setRating (shell, song_info->rating - 1, &ev);
			CORBA_free (song_info);
		}
		grab_focus = FALSE;
	}

        if (volume_up) {
                GNOME_Rhythmbox_volumeUp (shell, &ev);
		grab_focus = FALSE;
        }

        if (volume_down) {
                GNOME_Rhythmbox_volumeDown (shell, &ev);
		grab_focus = FALSE;
        }

        if (toggle_mute) {
                GNOME_Rhythmbox_toggleMute (shell, &ev);
		grab_focus = FALSE;
        }

	if (print_playing)
	{
		GNOME_Rhythmbox_SongInfo *song_info;

		song_info = get_song_info (shell);
		if (song_info == NULL) {
			g_print ("\n");
		} else {
			g_print ("%s\n", song_info->title);
			CORBA_free (song_info);
		}
		grab_focus = FALSE;
	}
	
	if (print_playing_path)
	{
		GNOME_Rhythmbox_SongInfo *song_info;

		song_info = get_song_info (shell);
		if (song_info == NULL) {
			g_print ("\n");
		} else {
			g_print ("%s\n", song_info->path);
			CORBA_free (song_info);
		}
		grab_focus = FALSE;
	}
	
	if (print_song_length)
	{
		GNOME_Rhythmbox_SongInfo *song_info;
		song_info = get_song_info (shell);
		if (song_info == NULL) {
			g_print ("-1\n");
		} else {
			g_print ("%d\n", song_info->duration);
			CORBA_free (song_info);
		}
		grab_focus = FALSE;
	}
	
	if (print_play_time)
	{
		long play_time = GNOME_Rhythmbox_getPlayingTime (shell, &ev);
		printf ("%ld\n", play_time);
		grab_focus = FALSE;
	}
	
	if (seek_time > 0)
	{
		GNOME_Rhythmbox_setPlayingTime (shell, seek_time, &ev);
		grab_focus = FALSE;
	}
	
	if (playpause)
	{
		GNOME_Rhythmbox_playPause (shell, &ev);
		grab_focus = FALSE;
	}

	if (previous)
	{
		GNOME_Rhythmbox_previous (shell, &ev);
		grab_focus = FALSE;
	}

	if (next)
	{
		GNOME_Rhythmbox_next (shell, &ev);
		grab_focus = FALSE;
	}

	if (shuffle)
	{
		rb_toggle_shuffle (shell);
	}

	for (i = 1; i < argc; i++)
	{
		char *tmp;

		tmp = rb_uri_resolve_relative (argv[i]);
			
		if (rb_uri_exists (tmp) == TRUE) {
			GError *error = NULL;
			char *utf8 = g_filename_to_utf8 (tmp, -1, NULL, NULL, &error);
			if (!utf8 && error) {
				g_error (error->message);
				continue;
			}
 			GNOME_Rhythmbox_handleFile (shell, utf8, &ev);
			g_free (utf8);
		}
		
		g_free (tmp);
		grab_focus = FALSE;
	}
	
	if (quit == TRUE)
	{
		GNOME_Rhythmbox_quit (shell, &ev);
	}

	if (focus)
		grab_focus = TRUE;

	if (already_running) {
		gdk_notify_startup_complete ();
	}

	/* at the very least, we focus the window */
	if (already_running == TRUE && grab_focus == TRUE)
		GNOME_Rhythmbox_grabFocus (shell, &ev);
}