1 /*************************************************************************/
2 /* Copyright (C) 2009-2015 matias <mati86dl@gmail.com>                   */
3 /* Copyright (C) 2007-2009 sujith <m.sujith@gmail.com>                   */
4 /*                                                                       */
5 /* This program is free software: you can redistribute it and/or modify  */
6 /* it under the terms of the GNU General Public License as published by  */
7 /* the Free Software Foundation, either version 3 of the License, or     */
8 /* (at your option) any later version.                                   */
9 /*                                                                       */
10 /* This program is distributed in the hope that it will be useful,       */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of        */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
13 /* GNU General Public License for more details.                          */
14 /*                                                                       */
15 /* You should have received a copy of the GNU General Public License     */
16 /* along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17 /*************************************************************************/
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #if defined(GETTEXT_PACKAGE)
24 #include <glib/gi18n-lib.h>
25 #else
26 #include <glib/gi18n.h>
27 #endif
28 
29 #include <glib.h>
30 #include <glib-object.h>
31 #include <gmodule.h>
32 #include <gtk/gtk.h>
33 
34 #include <cdio/paranoia/cdda.h>
35 #include <cdio/cd_types.h>
36 #include <cddb/cddb.h>
37 
38 #include <libpeas/peas.h>
39 
40 #include "src/pragha.h"
41 #include "src/pragha-hig.h"
42 #include "src/pragha-utils.h"
43 #include "src/pragha-menubar.h"
44 #include "src/pragha-musicobject.h"
45 #include "src/pragha-musicobject-mgmt.h"
46 #include "src/pragha-plugins-engine.h"
47 #include "src/pragha-statusicon.h"
48 #include "src/pragha-music-enum.h"
49 #include "src/pragha-window.h"
50 
51 #if HAVE_GUDEV
52 #include "plugins/devices/pragha-devices-plugin.h"
53 #include "plugins/devices/pragha-device-client.h"
54 #endif
55 
56 #include "plugins/pragha-plugin-macros.h"
57 
58 #define PRAGHA_TYPE_CDROM_PLUGIN         (pragha_cdrom_plugin_get_type ())
59 #define PRAGHA_CDROM_PLUGIN(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), PRAGHA_TYPE_CDROM_PLUGIN, PraghaCdromPlugin))
60 #define PRAGHA_CDROM_PLUGIN_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), PRAGHA_TYPE_CDROM_PLUGIN, PraghaCdromPlugin))
61 #define PRAGHA_IS_CDROM_PLUGIN(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), PRAGHA_TYPE_CDROM_PLUGIN))
62 #define PRAGHA_IS_CDROM_PLUGIN_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), PRAGHA_TYPE_CDROM_PLUGIN))
63 #define PRAGHA_CDROM_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), PRAGHA_TYPE_CDROM_PLUGIN, PraghaCdromPluginClass))
64 
65 struct _PraghaCdromPluginPrivate {
66 	PraghaApplication *pragha;
67 
68 	GtkWidget          *device_setting_widget;
69 	GtkWidget          *audio_cd_device_w;
70 	GtkWidget          *cddb_setting_widget;
71 	GtkWidget          *use_cddb_w;
72 
73 	gchar              *audio_cd_device;
74 	gboolean            use_cddb;
75 
76 	GtkActionGroup    *action_group_main_menu;
77 	guint              merge_id_main_menu;
78 	guint              merge_id_syst_menu;
79 };
80 typedef struct _PraghaCdromPluginPrivate PraghaCdromPluginPrivate;
81 
PRAGHA_PLUGIN_REGISTER(PRAGHA_TYPE_CDROM_PLUGIN,PraghaCdromPlugin,pragha_cdrom_plugin)82 PRAGHA_PLUGIN_REGISTER (PRAGHA_TYPE_CDROM_PLUGIN,
83                         PraghaCdromPlugin,
84                         pragha_cdrom_plugin)
85 
86 /*
87  * CDROM plugin.
88  */
89 #define KEY_USE_CDDB        "use_cddb"
90 #define KEY_AUDIO_CD_DEVICE "audio_cd_device"
91 
92 static gboolean
93 pragha_preferences_get_use_cddb (PraghaPreferences *preferences)
94 {
95 	gchar *plugin_group = NULL;
96 	gboolean use_cddb = FALSE;
97 
98 	plugin_group = pragha_preferences_get_plugin_group_name (preferences, "cdrom");
99 	use_cddb = pragha_preferences_get_boolean (preferences,
100 	                                           plugin_group,
101 	                                           KEY_USE_CDDB);
102 	g_free (plugin_group);
103 
104 	return use_cddb;
105 }
106 
107 static void
pragha_preferences_set_use_cddb(PraghaPreferences * preferences,gboolean use_cddb)108 pragha_preferences_set_use_cddb (PraghaPreferences *preferences,
109                                  gboolean           use_cddb)
110 {
111 	gchar *plugin_group = NULL;
112 	plugin_group = pragha_preferences_get_plugin_group_name (preferences, "cdrom");
113 	pragha_preferences_set_boolean (preferences,
114 	                                plugin_group,
115 	                                KEY_USE_CDDB,
116 	                                use_cddb);
117 	g_free (plugin_group);
118 }
119 
120 static gchar *
pragha_preferences_get_audio_cd_device(PraghaPreferences * preferences)121 pragha_preferences_get_audio_cd_device (PraghaPreferences *preferences)
122 {
123 	gchar *plugin_group = NULL, *audio_cd_device = NULL;
124 
125 	plugin_group = pragha_preferences_get_plugin_group_name (preferences, "cdrom");
126 	audio_cd_device = pragha_preferences_get_string (preferences,
127 	                                                 plugin_group,
128 	                                                 KEY_AUDIO_CD_DEVICE);
129 	g_free (plugin_group);
130 
131 	return audio_cd_device;
132 }
133 
134 static void
pragha_preferences_set_audio_cd_device(PraghaPreferences * preferences,const gchar * device)135 pragha_preferences_set_audio_cd_device (PraghaPreferences *preferences,
136                                         const gchar       *device)
137 {
138 	gchar *plugin_group = NULL;
139 	plugin_group = pragha_preferences_get_plugin_group_name (preferences, "cdrom");
140 
141 	if (string_is_not_empty(device))
142 		pragha_preferences_set_string (preferences,
143 		                               plugin_group,
144 		                               KEY_AUDIO_CD_DEVICE,
145 		                               device);
146 	else
147 		pragha_preferences_remove_key (preferences,
148 		                               plugin_group,
149 		                               KEY_AUDIO_CD_DEVICE);
150 	g_free (plugin_group);
151 }
152 
153 static PraghaMusicobject *
new_musicobject_from_cdda(PraghaApplication * pragha,cdrom_drive_t * cdda_drive,cddb_disc_t * cddb_disc,gint track_no)154 new_musicobject_from_cdda (PraghaApplication *pragha,
155                            cdrom_drive_t *cdda_drive,
156                            cddb_disc_t *cddb_disc,
157                            gint track_no)
158 {
159 	PraghaPreferences *preferences;
160 	PraghaMusicEnum *enum_map = NULL;
161 	PraghaMusicobject *mobj = NULL;
162 	gint channels, start, end;
163 	gchar *ntitle = NULL, *nfile = NULL;
164 
165 	CDEBUG(DBG_PLUGIN, "Creating new musicobject from cdda: %d", track_no);
166 
167 	channels = cdio_get_track_channels(cdda_drive->p_cdio,
168 					   track_no);
169 	start = cdio_cddap_track_firstsector(cdda_drive, track_no);
170 	end = cdio_cddap_track_lastsector(cdda_drive, track_no);
171 
172 	mobj = g_object_new (PRAGHA_TYPE_MUSICOBJECT, NULL);
173 
174 	preferences = pragha_application_get_preferences (pragha);
175 	if (pragha_preferences_get_use_cddb (preferences) && cddb_disc) {
176 		cddb_track_t *track;
177 		const gchar *title, *artist, *album, *genre;
178 		gint year;
179 
180 		track = cddb_disc_get_track(cddb_disc, track_no - 1);
181 		if (track) {
182 			title = cddb_track_get_title(track);
183 			if (title)
184 				ntitle = g_strdup(title);
185 
186 			artist = cddb_track_get_artist(track);
187 			if(artist)
188 				pragha_musicobject_set_artist(mobj, artist);
189 
190 			album = cddb_disc_get_title(cddb_disc);
191 			if(album)
192 				pragha_musicobject_set_album(mobj, album);
193 
194 			year = cddb_disc_get_year(cddb_disc);
195 			if(year)
196 				pragha_musicobject_set_year(mobj, year);
197 
198 			genre = cddb_disc_get_genre(cddb_disc);
199 			if(genre)
200 				pragha_musicobject_set_genre(mobj, genre);
201 		}
202 	}
203 
204 	enum_map = pragha_music_enum_get ();
205 	pragha_musicobject_set_source (mobj, pragha_music_enum_map_get(enum_map, "FILE_CDDA"));
206 	g_object_unref (enum_map);
207 
208 	nfile = g_strdup_printf("cdda://%d", track_no);
209 	pragha_musicobject_set_file(mobj, nfile);
210 	pragha_musicobject_set_track_no(mobj, track_no);
211 
212 	if (!ntitle)
213 		ntitle = g_strdup_printf("Track %d", track_no);
214 	pragha_musicobject_set_title(mobj, ntitle);
215 
216 	pragha_musicobject_set_length(mobj, (end - start) / CDIO_CD_FRAMES_PER_SEC);
217 	pragha_musicobject_set_channels(mobj, (channels > 0) ? channels : 0);
218 
219 	g_free(nfile);
220 	g_free(ntitle);
221 
222 	return mobj;
223 }
224 
225 static gint
cddb_add_tracks(cdrom_drive_t * cdda_drive,cddb_disc_t * cddb_disc)226 cddb_add_tracks (cdrom_drive_t *cdda_drive, cddb_disc_t *cddb_disc)
227 {
228 	cddb_track_t *track;
229 	lba_t lba;
230 	gint num_tracks, first_track, i = 0;
231 
232 	num_tracks = cdio_cddap_tracks(cdda_drive);
233 	if (!num_tracks)
234 		return -1;
235 
236 	first_track = cdio_get_first_track_num(cdda_drive->p_cdio);
237 	for (i = first_track; i <= num_tracks; i++) {
238 		track = cddb_track_new();
239 		if (!track)
240 			return -1;
241 
242 		lba = cdio_get_track_lba(cdda_drive->p_cdio, i);
243 		if (lba == CDIO_INVALID_LBA)
244 			return -1;
245 
246 		cddb_disc_add_track(cddb_disc, track);
247 		cddb_track_set_frame_offset(track, lba);
248 	}
249 
250 	return 0;
251 }
252 
253 static void
add_audio_cd_tracks(PraghaApplication * pragha,cdrom_drive_t * cdda_drive,cddb_disc_t * cddb_disc)254 add_audio_cd_tracks (PraghaApplication *pragha, cdrom_drive_t *cdda_drive, cddb_disc_t *cddb_disc)
255 {
256 	PraghaPlaylist *playlist;
257 	PraghaMusicobject *mobj;
258 
259 	gint num_tracks = 0, i = 0;
260 	GList *list = NULL;
261 
262 	num_tracks = cdio_cddap_tracks(cdda_drive);
263 	if (!num_tracks)
264 		return;
265 
266 	for (i = 1; i <= num_tracks; i++) {
267 		mobj = new_musicobject_from_cdda(pragha, cdda_drive, cddb_disc, i);
268 		if (G_LIKELY(mobj))
269 			list = g_list_append(list, mobj);
270 
271 		pragha_process_gtk_events ();
272 	}
273 	if (list) {
274 		playlist = pragha_application_get_playlist (pragha);
275 		pragha_playlist_append_mobj_list(playlist, list);
276 		g_list_free (list);
277 	}
278 }
279 
280 static cdrom_drive_t*
find_audio_cd(PraghaApplication * pragha)281 find_audio_cd (PraghaApplication *pragha)
282 {
283 	cdrom_drive_t *drive = NULL;
284 	gchar **cdda_devices = NULL;
285 	PraghaPreferences *preferences;
286 
287 	preferences = pragha_application_get_preferences (pragha);
288 	const gchar *audio_cd_device = pragha_preferences_get_audio_cd_device(preferences);
289 
290 	if (!audio_cd_device) {
291 		cdda_devices = cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, FALSE);
292 		if (!cdda_devices || (cdda_devices && !*cdda_devices)) {
293 			g_warning("No Audio CD found");
294 			return NULL;
295 		}
296 
297 		CDEBUG(DBG_PLUGIN, "Trying Audio CD Device: %s", *cdda_devices);
298 
299 		drive = cdio_cddap_identify(*cdda_devices, 0, NULL);
300 		if (!drive) {
301 			g_warning("Unable to identify Audio CD");
302 			goto exit;
303 		}
304 	} else {
305 		CDEBUG(DBG_PLUGIN, "Trying Audio CD Device: %s", audio_cd_device);
306 
307 		drive = cdio_cddap_identify(audio_cd_device, 0, NULL);
308 		if (!drive) {
309 			g_warning("Unable to identify Audio CD");
310 			return NULL;
311 		}
312 	}
313 exit:
314 	if (cdda_devices)
315 		cdio_free_device_list(cdda_devices);
316 
317 	return drive;
318 }
319 
320 void
pragha_application_append_audio_cd(PraghaApplication * pragha)321 pragha_application_append_audio_cd (PraghaApplication *pragha)
322 {
323 	lba_t lba;
324 	gint matches;
325 	cddb_disc_t *cddb_disc = NULL;
326 	cddb_conn_t *cddb_conn = NULL;
327 	PraghaPreferences *preferences;
328 
329 	cdrom_drive_t *cdda_drive = find_audio_cd(pragha);
330 	if (!cdda_drive)
331 		return;
332 
333 	if (cdio_cddap_open(cdda_drive)) {
334 		g_warning("Unable to open Audio CD");
335 		return;
336 	}
337 
338 	preferences = pragha_application_get_preferences (pragha);
339 	if (pragha_preferences_get_use_cddb (preferences)) {
340 		cddb_conn = cddb_new ();
341 		if (!cddb_conn)
342 			goto add;
343 
344 		cddb_disc = cddb_disc_new();
345 		if (!cddb_disc)
346 			goto add;
347 
348 		lba = cdio_get_track_lba(cdda_drive->p_cdio,
349 					 CDIO_CDROM_LEADOUT_TRACK);
350 		if (lba == CDIO_INVALID_LBA)
351 			goto add;
352 
353 		cddb_disc_set_length(cddb_disc, FRAMES_TO_SECONDS(lba));
354 		if (cddb_add_tracks(cdda_drive, cddb_disc) < 0)
355 			goto add;
356 
357 		if (!cddb_disc_calc_discid(cddb_disc))
358 			goto add;
359 
360 		cddb_disc_set_category(cddb_disc, CDDB_CAT_MISC);
361 
362 		matches = cddb_query(cddb_conn, cddb_disc);
363 		if (matches == -1)
364 			goto add;
365 
366 		if (!cddb_read(cddb_conn,
367 			       cddb_disc)) {
368 			cddb_error_print(cddb_errno(cddb_conn));
369 			goto add;
370 		}
371 
372 		CDEBUG(DBG_PLUGIN, "Successfully initialized CDDB");
373 		goto add;
374 	}
375 
376 add:
377 	add_audio_cd_tracks(pragha, cdda_drive, cddb_disc);
378 	CDEBUG(DBG_PLUGIN, "Successfully opened Audio CD device");
379 
380 	if (cdda_drive)
381 		cdio_cddap_close(cdda_drive);
382 	if (cddb_disc)
383 		cddb_disc_destroy(cddb_disc);
384 	if (cddb_conn)
385 		cddb_destroy(cddb_conn);
386 }
387 
388 static gboolean
pragha_musicobject_is_cdda_type(PraghaMusicobject * mobj)389 pragha_musicobject_is_cdda_type (PraghaMusicobject *mobj)
390 {
391 	PraghaMusicEnum *enum_map = NULL;
392 	PraghaMusicSource file_source = FILE_NONE;
393 
394 	enum_map = pragha_music_enum_get ();
395 	file_source = pragha_music_enum_map_get(enum_map, "FILE_CDDA");
396 	g_object_unref (enum_map);
397 
398 	return (file_source == pragha_musicobject_get_source (mobj));
399 }
400 
401 static void
pragha_cdrom_plugin_set_device(PraghaBackend * backend,GObject * obj,gpointer user_data)402 pragha_cdrom_plugin_set_device (PraghaBackend *backend, GObject *obj, gpointer user_data)
403 {
404 	PraghaMusicobject *mobj = NULL;
405 	GObject *source;
406 
407 	PraghaCdromPlugin *plugin = user_data;
408 	PraghaCdromPluginPrivate *priv = plugin->priv;
409 
410 	mobj = pragha_backend_get_musicobject (backend);
411 	if (!pragha_musicobject_is_cdda_type (mobj))
412 		return;
413 
414 	g_object_get (obj, "source", &source, NULL);
415 	if (source) {
416 		PraghaPreferences *preferences = pragha_application_get_preferences (priv->pragha);
417 		const gchar *audio_cd_device = pragha_preferences_get_audio_cd_device (preferences);
418 		if (audio_cd_device) {
419 			g_object_set (source, "device", audio_cd_device, NULL);
420 		}
421 		g_object_unref (source);
422 	}
423 }
424 
425 static void
pragha_cdrom_plugin_prepare_source(PraghaBackend * backend,gpointer user_data)426 pragha_cdrom_plugin_prepare_source (PraghaBackend *backend, gpointer user_data)
427 {
428 	PraghaMusicobject *mobj;
429 	const gchar *uri = NULL;
430 
431 	mobj = pragha_backend_get_musicobject (backend);
432 	if (!pragha_musicobject_is_cdda_type (mobj))
433 		return;
434 
435 	uri = pragha_musicobject_get_file (mobj);
436 	pragha_backend_set_playback_uri (backend, uri);
437 }
438 
439 /*
440  * GUDEV signals.
441  */
442 
443 #ifdef HAVE_GUDEV
444 static void
pragha_cdrom_plugin_device_added_response(GtkWidget * dialog,gint response,gpointer user_data)445 pragha_cdrom_plugin_device_added_response (GtkWidget *dialog,
446                                            gint       response,
447                                            gpointer   user_data)
448 {
449 	PraghaCdromPlugin *plugin = user_data;
450 	PraghaCdromPluginPrivate *priv = plugin->priv;
451 
452 	switch (response) {
453 		case PRAGHA_DEVICE_RESPONSE_PLAY:
454 			pragha_application_append_audio_cd (priv->pragha);
455 			break;
456 		case PRAGHA_DEVICE_RESPONSE_NONE:
457 		default:
458 			break;
459 	}
460 
461 	gtk_widget_destroy (dialog);
462 }
463 
464 static void
pragha_cdrom_plugin_device_added(PraghaDeviceClient * device_client,PraghaDeviceType device_type,GUdevDevice * u_device,gpointer user_data)465 pragha_cdrom_plugin_device_added (PraghaDeviceClient *device_client,
466                                   PraghaDeviceType    device_type,
467                                   GUdevDevice        *u_device,
468                                   gpointer            user_data)
469 {
470 	GtkWidget *dialog;
471 
472 	PraghaCdromPlugin *plugin = user_data;
473 
474 	if (device_type != PRAGHA_DEVICE_AUDIO_CD)
475 		return;
476 
477 	dialog = pragha_gudev_dialog_new (NULL, _("Audio/Data CD"), "media-optical",
478 	                                 _("An audio CD was inserted"), NULL,
479 	                                 _("Add Audio _CD"), PRAGHA_DEVICE_RESPONSE_PLAY);
480 
481 	g_signal_connect (G_OBJECT (dialog), "response",
482 	                  G_CALLBACK (pragha_cdrom_plugin_device_added_response), plugin);
483 
484 	gtk_widget_show_all (dialog);
485 }
486 
487 void
pragha_cdrom_plugin_device_removed(PraghaDeviceClient * device_client,PraghaDeviceType device_type,GUdevDevice * u_device,gpointer user_data)488 pragha_cdrom_plugin_device_removed (PraghaDeviceClient *device_client,
489                                     PraghaDeviceType    device_type,
490                                     GUdevDevice        *u_device,
491                                     gpointer            user_data)
492 {
493 	if (device_type != PRAGHA_DEVICE_AUDIO_CD)
494 		return;
495 
496 	g_print ("CDROM REMOVEDDDDD.. Cri cri.. never detect it.. .\n");
497 }
498 #endif
499 
500 /*
501  * Menubar
502  */
503 static void
pragha_cdrom_plugin_append_action(GtkAction * action,PraghaCdromPlugin * plugin)504 pragha_cdrom_plugin_append_action (GtkAction *action, PraghaCdromPlugin *plugin)
505 {
506 	PraghaCdromPluginPrivate *priv = plugin->priv;
507 	pragha_application_append_audio_cd (priv->pragha);
508 }
509 
510 static void
pragha_gmenu_add_cdrom_action(GSimpleAction * action,GVariant * parameter,gpointer user_data)511 pragha_gmenu_add_cdrom_action (GSimpleAction *action,
512                                GVariant      *parameter,
513                                gpointer       user_data)
514 {
515 	pragha_cdrom_plugin_append_action (NULL, PRAGHA_CDROM_PLUGIN(user_data));
516 }
517 
518 static const GtkActionEntry main_menu_actions [] = {
519 	{"Add Audio CD", NULL, N_("Add Audio _CD"),
520 	 "", "Append a Audio CD", G_CALLBACK(pragha_cdrom_plugin_append_action)}
521 };
522 
523 static const gchar *main_menu_xml = "<ui>							\
524 	<menubar name=\"Menubar\">										\
525 		<menu action=\"PlaylistMenu\">								\
526 			<placeholder name=\"pragha-append-music-placeholder\">	\
527 				<menuitem action=\"Add Audio CD\"/>					\
528 			</placeholder>											\
529 		</menu>														\
530 	</menubar>														\
531 </ui>";
532 
533 static const gchar *syst_menu_xml = "<ui>							\
534 	<popup>															\
535 	<placeholder name=\"pragha-append-music-placeholder\">			\
536 		<menuitem action=\"Add Audio CD\"/>							\
537 	</placeholder>													\
538 	</popup>														\
539 	</ui>";
540 
541 /*
542  * Cdrom Settings
543  */
544 static void
pragha_cdrom_preferences_dialog_response(GtkDialog * dialog_w,gint response_id,PraghaCdromPlugin * plugin)545 pragha_cdrom_preferences_dialog_response (GtkDialog         *dialog_w,
546                                           gint               response_id,
547                                           PraghaCdromPlugin *plugin)
548 {
549 	PraghaPreferences *preferences;
550 	const gchar *audio_cd_device;
551 
552 	PraghaCdromPluginPrivate *priv = plugin->priv;
553 
554 	preferences = pragha_preferences_get();
555 	switch(response_id) {
556 	case GTK_RESPONSE_CANCEL:
557 		pragha_gtk_entry_set_text(GTK_ENTRY(priv->audio_cd_device_w),
558 			priv->audio_cd_device);
559 		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->use_cddb_w),
560 			priv->use_cddb);
561 		break;
562 	case GTK_RESPONSE_OK:
563 		audio_cd_device = gtk_entry_get_text (GTK_ENTRY(priv->audio_cd_device_w));
564 		if (audio_cd_device) {
565 			pragha_preferences_set_audio_cd_device (preferences, audio_cd_device);
566 
567 			g_free (priv->audio_cd_device);
568 			priv->audio_cd_device = g_strdup(audio_cd_device);
569 		}
570 		priv->use_cddb =
571 			gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->use_cddb_w));
572 		pragha_preferences_set_use_cddb (preferences, priv->use_cddb);
573 		break;
574 	default:
575 		break;
576 	}
577 	g_object_unref (preferences);
578 }
579 
580 static void
pragha_cdrom_init_settings(PraghaCdromPlugin * plugin)581 pragha_cdrom_init_settings (PraghaCdromPlugin *plugin)
582 {
583 	PraghaPreferences *preferences;
584 	gchar *plugin_group = NULL;
585 
586 	PraghaCdromPluginPrivate *priv = plugin->priv;
587 
588 	preferences = pragha_preferences_get();
589 	plugin_group = pragha_preferences_get_plugin_group_name (preferences, "cdrom");
590 	if (pragha_preferences_has_group (preferences, plugin_group)) {
591 		priv->audio_cd_device =
592 			pragha_preferences_get_audio_cd_device (preferences);
593 		priv->use_cddb =
594 			pragha_preferences_get_use_cddb(preferences);
595 	}
596 	else {
597 		priv->audio_cd_device = NULL;
598 		priv->use_cddb = TRUE;
599 	}
600 
601 	pragha_gtk_entry_set_text(GTK_ENTRY(priv->audio_cd_device_w), priv->audio_cd_device);
602 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->use_cddb_w), priv->use_cddb);
603 
604 	g_object_unref (preferences);
605 	g_free (plugin_group);
606 }
607 
608 static void
pragha_cdrom_plugin_append_setting(PraghaCdromPlugin * plugin)609 pragha_cdrom_plugin_append_setting (PraghaCdromPlugin *plugin)
610 {
611 	PreferencesDialog *dialog;
612 	GtkWidget *table;
613 	GtkWidget *audio_cd_device_label,*audio_cd_device_entry, *use_cddb;
614 	guint row = 0;
615 
616 	PraghaCdromPluginPrivate *priv = plugin->priv;
617 
618 	/* Cd Device */
619 
620 	table = pragha_hig_workarea_table_new();
621 
622 	pragha_hig_workarea_table_add_section_title(table, &row, _("Audio CD"));
623 
624 	audio_cd_device_label = gtk_label_new(_("Audio CD Device"));
625 	gtk_misc_set_alignment (GTK_MISC (audio_cd_device_label), 0, 0);
626 
627 	audio_cd_device_entry = gtk_entry_new ();
628 	gtk_entry_set_max_length (GTK_ENTRY(audio_cd_device_entry), AUDIO_CD_DEVICE_ENTRY_LEN);
629 	gtk_entry_set_activates_default (GTK_ENTRY(audio_cd_device_entry), TRUE);
630 
631 	pragha_hig_workarea_table_add_row (table, &row, audio_cd_device_label, audio_cd_device_entry);
632 
633 	/* Store references */
634 
635 	priv->device_setting_widget = table;
636 	priv->audio_cd_device_w = audio_cd_device_entry;
637 
638 	/* CDDB Option */
639 	row = 0;
640 	table = pragha_hig_workarea_table_new();
641 
642 	pragha_hig_workarea_table_add_section_title (table, &row, "CDDB");
643 
644 	use_cddb = gtk_check_button_new_with_label (_("Connect to CDDB server"));
645 	pragha_hig_workarea_table_add_wide_control (table, &row, use_cddb);
646 
647 	priv->cddb_setting_widget = table;
648 	priv->use_cddb_w = use_cddb;
649 
650 	/* Append panes */
651 
652 	dialog = pragha_application_get_preferences_dialog (priv->pragha);
653 	pragha_preferences_append_audio_setting (dialog,
654 	                                         priv->device_setting_widget, FALSE);
655 	pragha_preferences_append_services_setting (dialog,
656 	                                            priv->cddb_setting_widget, FALSE);
657 
658 	/* Configure handler and settings */
659 	pragha_preferences_dialog_connect_handler (dialog,
660 	                                           G_CALLBACK(pragha_cdrom_preferences_dialog_response),
661 	                                           plugin);
662 
663 	pragha_cdrom_init_settings (plugin);
664 }
665 
666 static void
pragha_cdrom_plugin_remove_setting(PraghaCdromPlugin * plugin)667 pragha_cdrom_plugin_remove_setting (PraghaCdromPlugin *plugin)
668 {
669 	PreferencesDialog *dialog;
670 	PraghaCdromPluginPrivate *priv = plugin->priv;
671 
672 	dialog = pragha_application_get_preferences_dialog (priv->pragha);
673 
674 	pragha_preferences_dialog_disconnect_handler (dialog,
675 	                                              G_CALLBACK(pragha_cdrom_preferences_dialog_response),
676 	                                              plugin);
677 
678 	pragha_preferences_remove_audio_setting (dialog,
679 	                                         priv->device_setting_widget);
680 	pragha_preferences_remove_services_setting (dialog,
681 	                                            priv->cddb_setting_widget);
682 }
683 
684 /*
685  * Cdrom plugin
686  */
687 static void
pragha_plugin_activate(PeasActivatable * activatable)688 pragha_plugin_activate (PeasActivatable *activatable)
689 {
690 	GMenuItem *item;
691 	GSimpleAction *action;
692 	PraghaBackend *backend;
693 	PraghaStatusIcon *status_icon = NULL;
694 	PraghaMusicEnum *enum_map = NULL;
695 
696 	PraghaCdromPlugin *plugin = PRAGHA_CDROM_PLUGIN (activatable);
697 	PraghaCdromPluginPrivate *priv = plugin->priv;
698 
699 	CDEBUG(DBG_PLUGIN,"CDROM plugin %s", G_STRFUNC);
700 
701 	priv->pragha = g_object_get_data (G_OBJECT (plugin), "object");
702 
703 	/* Attach main menu */
704 
705 	priv->action_group_main_menu = gtk_action_group_new ("PraghaCdromPlugin");
706 	gtk_action_group_set_translation_domain (priv->action_group_main_menu, GETTEXT_PACKAGE);
707 	gtk_action_group_add_actions (priv->action_group_main_menu,
708 	                              main_menu_actions,
709 	                              G_N_ELEMENTS (main_menu_actions),
710 	                              plugin);
711 
712 	priv->merge_id_main_menu = pragha_menubar_append_plugin_action (priv->pragha,
713 	                                                                priv->action_group_main_menu,
714 	                                                                main_menu_xml);
715 
716 	/* Systray */
717 
718 	status_icon = pragha_application_get_status_icon(priv->pragha);
719 	priv->merge_id_syst_menu = pragha_systray_append_plugin_action (status_icon,
720 	                                                                priv->action_group_main_menu,
721 	                                                                syst_menu_xml);
722 	g_object_ref (priv->action_group_main_menu);
723 
724 	/* Gear Menu */
725 
726 	action = g_simple_action_new ("add-cdrom", NULL);
727 	g_signal_connect (G_OBJECT (action), "activate",
728 	                  G_CALLBACK (pragha_gmenu_add_cdrom_action), plugin);
729 
730 	item = g_menu_item_new (_("Add Audio _CD"), "win.add-cdrom");
731 
732 	pragha_menubar_append_action (priv->pragha, "pragha-plugins-append-music", action, item);
733 
734 	/* Connect signals */
735 
736 	backend = pragha_application_get_backend (priv->pragha);
737 	g_signal_connect (backend, "set-device",
738 	                  G_CALLBACK(pragha_cdrom_plugin_set_device), plugin);
739 	g_signal_connect (backend, "prepare-source",
740 	                  G_CALLBACK(pragha_cdrom_plugin_prepare_source), plugin);
741 
742 	#ifdef HAVE_GUDEV
743 	PraghaDeviceClient *device_client;
744 	device_client = pragha_device_client_get();
745 
746 	g_signal_connect (G_OBJECT(device_client), "device-added",
747 	                  G_CALLBACK(pragha_cdrom_plugin_device_added), plugin);
748 	g_signal_connect (G_OBJECT(device_client), "device-removed",
749 	                  G_CALLBACK(pragha_cdrom_plugin_device_removed), plugin);
750 	g_object_unref (device_client);
751 	#endif
752 
753 	enum_map = pragha_music_enum_get ();
754 	pragha_music_enum_map_get (enum_map, "FILE_CDDA");
755 	g_object_unref (enum_map);
756 
757 	/* Settings */
758 	pragha_cdrom_plugin_append_setting (plugin);
759 }
760 
761 static void
pragha_plugin_deactivate(PeasActivatable * activatable)762 pragha_plugin_deactivate (PeasActivatable *activatable)
763 {
764 	PraghaBackend *backend;
765 	PraghaPreferences *preferences;
766 	PraghaStatusIcon *status_icon = NULL;
767 	PraghaMusicEnum *enum_map = NULL;
768 	gchar *plugin_group = NULL;
769 
770 	PraghaCdromPlugin *plugin = PRAGHA_CDROM_PLUGIN (activatable);
771 	PraghaCdromPluginPrivate *priv = plugin->priv;
772 
773 	CDEBUG(DBG_PLUGIN,"CDROM plugin %s", G_STRFUNC);
774 
775 	pragha_menubar_remove_plugin_action (priv->pragha,
776 	                                     priv->action_group_main_menu,
777 	                                     priv->merge_id_main_menu);
778 	priv->merge_id_main_menu = 0;
779 
780 	status_icon = pragha_application_get_status_icon(priv->pragha);
781 	pragha_systray_remove_plugin_action (status_icon,
782 	                                     priv->action_group_main_menu,
783 	                                     priv->merge_id_syst_menu);
784 	priv->merge_id_syst_menu = 0;
785 
786 	pragha_menubar_remove_action (priv->pragha, "pragha-plugins-append-music", "add-cdrom");
787 
788 	backend = pragha_application_get_backend (priv->pragha);
789 	g_signal_handlers_disconnect_by_func (backend, pragha_cdrom_plugin_set_device, plugin);
790 	g_signal_handlers_disconnect_by_func (backend, pragha_cdrom_plugin_prepare_source, plugin);
791 
792 	#ifdef HAVE_GUDEV
793 	PraghaDeviceClient *device_client;
794 	device_client = pragha_device_client_get();
795 	g_signal_handlers_disconnect_by_func (device_client,
796 	                                      pragha_cdrom_plugin_device_added,
797 	                                      plugin);
798 	g_signal_handlers_disconnect_by_func (device_client,
799 	                                      pragha_cdrom_plugin_device_removed,
800 	                                      plugin);
801 	g_object_unref (device_client);
802 	#endif
803 
804 	pragha_cdrom_plugin_remove_setting (plugin);
805 
806 	preferences = pragha_application_get_preferences (priv->pragha);
807 	plugin_group = pragha_preferences_get_plugin_group_name (preferences, "lastfm");
808 	if (!pragha_plugins_is_shutdown(pragha_application_get_plugins_engine(priv->pragha))) {
809 		pragha_preferences_remove_group (preferences, plugin_group);
810 	}
811 	g_free (plugin_group);
812 
813 	enum_map = pragha_music_enum_get ();
814 	pragha_music_enum_map_remove (enum_map, "FILE_CDDA");
815 	g_object_unref (enum_map);
816 
817 	libcddb_shutdown ();
818 }
819