1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail    : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include <stdlib.h>
21 #include <string.h>
22 #include <cairo-dock.h>
23 
24 #include "applet-struct.h"
25 #include "applet-dbus.h"
26 #include "applet-draw.h"
27 #include "applet-musicplayer.h"
28 
29 
cd_musicplayer_dbus_connect_handler(MusicPlayerHandler * pHandler)30 gboolean cd_musicplayer_dbus_connect_handler (MusicPlayerHandler *pHandler)
31 {
32 	g_return_val_if_fail (pHandler != NULL && pHandler->cMprisService != NULL, FALSE);
33 	if (cairo_dock_dbus_is_enabled ())
34 	{
35 		if (pHandler->path != NULL)
36 		{
37 			myData.dbus_proxy_player = cairo_dock_create_new_session_proxy (
38 				pHandler->cMprisService,
39 				pHandler->path,
40 				pHandler->interface);
41 		}
42 		if (pHandler->path2 != NULL)
43 		{
44 			myData.dbus_proxy_shell = cairo_dock_create_new_session_proxy (
45 				pHandler->cMprisService,
46 				pHandler->path2,
47 				pHandler->interface2);
48 		}
49 		return (myData.dbus_proxy_player != NULL || myData.dbus_proxy_shell != NULL);
50 	}
51 	return FALSE;
52 }
53 
54 
cd_musicplayer_dbus_disconnect_from_bus(void)55 void cd_musicplayer_dbus_disconnect_from_bus (void)
56 {
57 	if (myData.dbus_proxy_player != NULL)
58 	{
59 		g_object_unref (myData.dbus_proxy_player);
60 		myData.dbus_proxy_player = NULL;
61 	}
62 	if (myData.pDetectPlayerCall != NULL)
63 	{
64 		DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
65 		dbus_g_proxy_cancel_call (pProxy, myData.pDetectPlayerCall);
66 		myData.pDetectPlayerCall = NULL;
67 	}
68 	if (myData.dbus_proxy_shell != NULL)
69 	{
70 		g_object_unref (myData.dbus_proxy_shell);
71 		myData.dbus_proxy_shell = NULL;
72 	}
73 }
74 
75 
cd_musicplayer_dbus_find_opened_player(void)76 MusicPlayerHandler *cd_musicplayer_dbus_find_opened_player (void)
77 {
78 	if (myData.pCurrentHandler != NULL && myData.bIsRunning)
79 		return myData.pCurrentHandler;
80 
81 	// get the list of services.
82 	MusicPlayerHandler *pHandler = NULL;
83 	gchar **name_list = cairo_dock_dbus_get_services ();
84 
85 	if (name_list != NULL)
86 	{
87 		// check if an MPRIS2 service is present.
88 		int i;
89 		for (i = 0; name_list[i] != NULL; i ++)
90 		{
91 			if (strncmp (name_list[i], CD_MPRIS2_SERVICE_BASE, strlen (CD_MPRIS2_SERVICE_BASE)) == 0)  // it's an MPRIS2 player.
92 			{
93 				pHandler = cd_musicplayer_get_handler_by_name ("Mpris2");
94 				g_free ((gchar*)pHandler->cMprisService);
95 				pHandler->cMprisService = g_strdup (name_list[i]);
96 				pHandler->launch = g_strdup (name_list[i] + strlen (CD_MPRIS2_SERVICE_BASE)+1);  // only used to be displayed in the combo-box; we'll get the real data once we connect to it on the bus.
97 				gchar *str = strchr (pHandler->launch, '.');
98 				if (str)
99 					*str = '\0';
100 				break;
101 			}
102 		}
103 
104 		// if no MPRIS2 service is present, look for a known handler.
105 		if (pHandler == NULL)
106 		{
107 			GList *h;
108 			MusicPlayerHandler *handler;
109 			for (i = 0; name_list[i] != NULL; i ++)
110 			{
111 				for (h = myData.pHandlers; h != NULL; h = h->next)  // see if a known handler matches.
112 				{
113 					handler = h->data;
114 					if (handler->cMprisService == NULL)
115 						continue;
116 					if (strcmp (name_list[i], handler->cMprisService) == 0)
117 					{
118 						pHandler = handler;
119 						break;
120 					}
121 				}
122 			}
123 		}
124 
125 		g_strfreev (name_list);
126 	}
127 	return pHandler;
128 }
129