UnityProxy.cpp
Go to the documentation of this file.
1 #include "UnityProxy.h"
2 #include <nsCOMPtr.h>
3 #include <nsIObserverService.h>
4 #include <nsServiceManagerUtils.h>
5 
6 static nsCOMPtr<nsIObserverService> observerService = NULL;
7 static UnityMusicPlayer *musicPlayer = NULL;
8 static UnityTrackMetadata *trackMetadata = NULL;
9 static GtkWindow *playerGtkWindow = NULL;
10 static gchar *playerName = NULL;
11 static gchar *playerIcon = NULL;
12 static gboolean lockedFromSoundMenu = false;
13 
14 static gboolean enableNotify = true;
15 
16 void onPlayPause (GtkWidget *window,
17  gpointer data)
18 {
19  UnityPlaybackState playbackState = unity_music_player_get_playback_state (musicPlayer);
20  if (playbackState == UNITY_PLAYBACK_STATE_PLAYING) {
21  observerService->NotifyObservers (NULL, "sound-menu-pause", NULL);
22  }
23  else {
24  observerService->NotifyObservers (NULL, "sound-menu-play", NULL);
25  }
26 }
27 
28 void onNext (GtkWidget *window,
29  gpointer data)
30 {
31  lockedFromSoundMenu = true;
32  observerService->NotifyObservers (NULL, "sound-menu-next", NULL);
33 }
34 
35 void onPrevious (GtkWidget *window,
36  gpointer data)
37 {
38  lockedFromSoundMenu = true;
39  observerService->NotifyObservers (NULL, "sound-menu-previous", NULL);
40 }
41 
42 void onRaise (GtkWidget *window,
43  gpointer data)
44 {
45  gdk_window_show( ((GtkWidget*) playerGtkWindow) -> window );
46 }
47 
48 void checkWindowTitle (gpointer data,
49  gpointer user_data)
50 {
51  const gchar *title = gtk_window_get_title ((GtkWindow*) data);
52 
53  if (playerGtkWindow || g_strcmp0 (title, (char*) user_data)) return;
54 
55  playerGtkWindow = (GtkWindow*) data;
56 }
57 
59 
61 {
62  g_message("Unity Integration: loading");
63 
64  observerService = do_GetService ("@mozilla.org/observer-service;1");
65 }
66 
68 {
69  if (musicPlayer != NULL) unity_music_player_unexport(musicPlayer);
70 }
71 
72 NS_IMETHODIMP UnityProxy::InitializeFor (const char* desktopFileName, const char* windowTitle)
73 {
74  g_message("Unity Integration: checking for unity components");
75  if (!isUnityRunning()) return NS_ERROR_NOT_INITIALIZED;
76 
77  g_message("Unity Integration: found them!");
78  trackMetadata = unity_track_metadata_new ();
79 
80  if (!desktopFileName || !windowTitle) return NS_ERROR_INVALID_ARG;
81 
82  GList* wlist = gtk_window_list_toplevels ();
83  g_list_foreach (wlist, checkWindowTitle, (gpointer) windowTitle);
84  g_list_free (wlist);
85 
86  if (!playerGtkWindow) return NS_ERROR_INVALID_ARG;
87 
88  GKeyFile *keyFile = g_key_file_new ();
89  gchar *desktopFilePath = g_build_filename ("/usr/share/applications", desktopFileName, NULL);
90  if (g_key_file_load_from_file (keyFile, desktopFilePath, G_KEY_FILE_NONE, NULL))
91  {
92  playerName = g_key_file_get_string (keyFile, "Desktop Entry", "Name", NULL);
93  playerIcon = g_key_file_get_string (keyFile, "Desktop Entry", "Icon", NULL);
94  }
95  g_key_file_free (keyFile);
96 
97  if (!playerName || !playerIcon) return NS_ERROR_UNEXPECTED;
98 
99  musicPlayer = unity_music_player_new (desktopFileName);
100  if (!musicPlayer) return NS_ERROR_INVALID_ARG;
101 
102  unity_music_player_set_title (musicPlayer, playerName);
103  unity_music_player_export (musicPlayer);
104 
105  unity_music_player_set_can_go_next (musicPlayer, false);
106  unity_music_player_set_can_play (musicPlayer, true);
107 
108  g_signal_connect (G_OBJECT (musicPlayer), "play_pause", G_CALLBACK (onPlayPause), NULL);
109  g_signal_connect (G_OBJECT (musicPlayer), "next", G_CALLBACK (onNext), NULL);
110  g_signal_connect (G_OBJECT (musicPlayer), "previous", G_CALLBACK (onPrevious), NULL);
111  g_signal_connect (G_OBJECT (musicPlayer), "raise", G_CALLBACK (onRaise), NULL);
112 
113  return NS_OK;
114 }
115 
116 NS_IMETHODIMP UnityProxy::SoundMenuSetTrackInfo (const char *title, const char *artist, const char *album, const char *coverFilePath)
117 {
118  if (!musicPlayer) return NS_ERROR_NOT_INITIALIZED;
119 
120  unity_track_metadata_set_artist (trackMetadata, artist);
121  unity_track_metadata_set_album (trackMetadata, album);
122  unity_track_metadata_set_title (trackMetadata, title);
123 
124  GFile *coverFile = g_file_new_for_path (coverFilePath);
125  unity_track_metadata_set_art_location (trackMetadata, coverFile);
126 
127  unity_music_player_set_current_track (musicPlayer, trackMetadata);
128 
129  g_object_unref (coverFile);
130 
131  return NS_OK;
132 }
133 
134 NS_IMETHODIMP UnityProxy::SoundMenuSetPlayingState (PRInt16 playing)
135 {
136  if (!musicPlayer) return NS_ERROR_NOT_INITIALIZED;
137 
138  if (playing < 0) {
139  unity_music_player_set_playback_state (musicPlayer, UNITY_PLAYBACK_STATE_PAUSED);
140  unity_music_player_set_current_track (musicPlayer, NULL);
141  }
142  else if (!!playing) {
143  unity_music_player_set_playback_state (musicPlayer, UNITY_PLAYBACK_STATE_PLAYING);
144  }
145  else {
146  unity_music_player_set_playback_state (musicPlayer, UNITY_PLAYBACK_STATE_PAUSED);
147  }
148 
149  return NS_OK;
150 }
151 
152 NS_IMETHODIMP UnityProxy::HideWindow ()
153 {
154  if (!musicPlayer) return NS_ERROR_NOT_INITIALIZED;
155 
156  gdk_window_hide ( ((GtkWidget*) playerGtkWindow) -> window );
157 
158  return NS_OK;
159 }
160 
161 NS_IMETHODIMP UnityProxy::ShowWindow ()
162 {
163  if (!musicPlayer) return NS_ERROR_NOT_INITIALIZED;
164 
165  gdk_window_show ( ((GtkWidget*) playerGtkWindow) -> window );
166 
167  return NS_OK;
168 }
static GtkWindow * playerGtkWindow
Definition: UnityProxy.cpp:9
return NS_OK
static nsCOMPtr< nsIObserverService > observerService
Definition: UnityProxy.cpp:6
void SoundMenuSetPlayingState(in PRInt16 playing)
static gboolean enableNotify
Definition: UnityProxy.cpp:14
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
void onNext(GtkWidget *window, gpointer data)
Definition: UnityProxy.cpp:28
void ShowWindow()
void InitializeFor(in string desktopFileName, in string windowTitle)
let window
static gboolean lockedFromSoundMenu
Definition: UnityProxy.cpp:12
virtual ~UnityProxy()
Definition: UnityProxy.cpp:67
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties artist
Definition: tuner2.js:40
static gchar * playerName
Definition: UnityProxy.cpp:10
const UNITY_PLAYBACK_STATE_PAUSED
Definition: sound-menu.js:8
void onPlayPause(GtkWidget *window, gpointer data)
Definition: UnityProxy.cpp:16
void onPrevious(GtkWidget *window, gpointer data)
Definition: UnityProxy.cpp:35
static gchar * playerIcon
Definition: UnityProxy.cpp:11
static UnityTrackMetadata * trackMetadata
Definition: UnityProxy.cpp:8
void SoundMenuSetTrackInfo(in string title, in string artist, in string album, in string coverFilePath)
const UNITY_PLAYBACK_STATE_PLAYING
Definition: sound-menu.js:9
static UnityMusicPlayer * musicPlayer
Definition: UnityProxy.cpp:7
void onRaise(GtkWidget *window, gpointer data)
Definition: UnityProxy.cpp:42
NS_DECL_ISUPPORTS NS_DECL_IUNITYPROXY bool isUnityRunning()
Definition: checkUnity.cpp:8
void checkWindowTitle(gpointer data, gpointer user_data)
Definition: UnityProxy.cpp:48
observe data
Definition: FeedWriter.js:1329
void HideWindow()