Debian Codegraph @

musicPlayer/src/applet-draw.c

raw
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 <glib/gstdio.h>
23 #include <cairo-dock.h>
24
25 #include "applet-struct.h"
26 #include "applet-dbus.h"
27 #include "3dcover-draw.h"
28 #include "applet-musicplayer.h"
29 #include "applet-cover.h"
30 #include "applet-dbus.h"
31 #include "applet-draw.h"
32
33 static const gchar *s_cDefaultIconName[PLAYER_NB_STATUS] = {"default.svg", "play.svg", "pause.svg", "stop.svg", "broken.svg"};
34 static const gchar *s_cDefaultIconName3D[PLAYER_NB_STATUS] = {"default.jpg", "play.jpg", "pause.jpg", "stop.jpg", "broken.jpg"};
35
36
37 /* Update the icon on new song / status.
38 */
39 void cd_musicplayer_update_icon (void)
40 {
41 cd_message ("%s (uri : %s / title : %s)", __func__, myData.cPlayingUri, myData.cTitle);
42 if (myData.cPlayingUri != NULL || myData.cTitle != NULL)
43 {
44 if (myData.iPlayingStatus == PLAYER_PLAYING || myData.iPlayingStatus == PLAYER_PAUSED)
45 {
46 // display current song on the label
47 if (myDock)
48 {
49 if ((!myData.cArtist || !myData.cTitle) && myData.cPlayingUri)
50 {
51 gchar *str = strrchr (myData.cPlayingUri, '/');
52 if (str)
53 str ++;
54 else
55 str = myData.cPlayingUri;
56 CD_APPLET_SET_NAME_FOR_MY_ICON (str);
57 }
58 else
59 CD_APPLET_SET_NAME_FOR_MY_ICON_PRINTF ("%s - %s", myData.cArtist ? myData.cArtist : D_("Unknown artist"), myData.cTitle ? myData.cTitle : D_("Unknown title"));
60 }
61
62 // display current track on the quick-info.
63 if (myConfig.iQuickInfoType == MY_APPLET_TRACK && myData.iTrackListLength > 0 && myData.iTrackListIndex > 0)
64 {
65 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%s%d", (myDesklet && myDesklet->container.iWidth >= 64 ? D_("Track") : ""), myData.iTrackListIndex); // inutile de redessiner notre icone, ce sera fait plus loin.
66 }
67 else
68 {
69 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
70 }
71
72 // animate the icon and pop-up the dialog.
73 if (myData.iPlayingStatus == PLAYER_PLAYING)
74 {
75 cd_musicplayer_animate_icon (1);
76 if(myConfig.bEnableDialogs)
77 {
78 cd_musicplayer_popup_info (myConfig.iDialogDuration);
79 }
80 }
81 }
82 /**else
83 {
84 cd_musicplayer_apply_status_surface (PLAYER_STOPPED);
85 CD_APPLET_SET_NAME_FOR_MY_ICON (myData.cTitle ? myData.cTitle : myData.pCurrentHandler ? myData.pCurrentHandler->name : myConfig.cDefaultTitle);
86 }*/
87
88 // re-paint the icon if needed.
89 if (myConfig.bEnableCover && myData.cover_exist && myData.cCoverPath != NULL) // cover is available
90 {
91 if (cairo_dock_strings_differ (myData.cCoverPath, myData.cPreviousCoverPath)) // and cover has changed -> apply the new one
92 cd_musiplayer_apply_cover ();
93 }
94 else // no cover -> set the status surface.
95 {
96 if ((myConfig.bEnableCover && myData.cPreviousCoverPath != NULL) // currently a cover is set
97 || myData.pPreviousPlayingStatus != myData.iPlayingStatus) // or the status has changed
98 cd_musicplayer_apply_status_surface (myData.iPlayingStatus);
99 }
100 }
101 else // aucune donnees, c'est soit un probleme soit le lecteur qui s'est ferme.
102 {
103 if (myData.bIsRunning)
104 {
105 cd_musicplayer_apply_status_surface (PLAYER_STOPPED);
106 if (myConfig.cDefaultTitle)
107 {
108 CD_APPLET_SET_NAME_FOR_MY_ICON (myConfig.cDefaultTitle);
109 }
110 else if (myData.pCurrentHandler->cDisplayedName != NULL)
111 {
112 CD_APPLET_SET_NAME_FOR_MY_ICON (myData.pCurrentHandler->cDisplayedName);
113 }
114 else
115 {
116 CD_APPLET_SET_NAME_FOR_MY_ICON (myData.pCurrentHandler->name);
117 }
118 }
119 else
120 {
121 cd_musicplayer_apply_status_surface (PLAYER_NONE);
122 if (myConfig.cDefaultTitle)
123 {
124 CD_APPLET_SET_NAME_FOR_MY_ICON (myConfig.cDefaultTitle);
125 }
126 else
127 {
128 CD_APPLET_SET_NAME_FOR_MY_ICON (myApplet->pModule->pVisitCard->cTitle);
129 }
130 }
131 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
132 }
133 }
134
135
136 /* Display information about the current song in a dialog.
137 */
138 void cd_musicplayer_popup_info (gint iDialogDuration)
139 {
140 gldi_dialogs_remove_on_icon (myIcon);
141 if (myData.iPlayingStatus == PLAYER_PLAYING || myData.iPlayingStatus == PLAYER_PAUSED)
142 {
143 if (myData.cTitle || myData.cArtist || myData.cAlbum)
144 {
145 /* It's maybe better to display nothing if we don't have (optional)
146 * info about track number and id than displaying a wrong number
147 */
148 GString *sTrack = g_string_new ("");
149 if (myData.iTrackNumber > 0)
150 g_string_printf (sTrack, "\n%s %d", D_("Track n°"), myData.iTrackNumber);
151 if (myData.iTrackListIndex > 0 || myData.iTrackListLength > 0) // 0/0
152 {
153 g_string_append_printf (sTrack, "%s%s %d",
154 sTrack->len > 0 ? ", " : "\n",
155 D_("Song n°"), myData.iTrackListIndex + 1); // iTrackListIndex start with 0.
156 if (myData.iTrackListLength > 0)
157 g_string_append_printf (sTrack, "/%d", myData.iTrackListLength);
158 }
159
160 gldi_dialog_show_temporary_with_icon_printf (
161 "%s: %s\n%s: %s\n%s: %s\n%s: %d:%02d%s",
162 myIcon,
163 myContainer,
164 iDialogDuration,
165 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE,
166 D_("Artist"),
167 myData.cArtist != NULL ? myData.cArtist : D_("Unknown"),
168 D_("Title"),
169 myData.cTitle != NULL ? myData.cTitle : D_("Unknown"),
170 D_("Album"),
171 myData.cAlbum != NULL ? myData.cAlbum : D_("Unknown"),
172 D_("Length"),
173 myData.iSongLength/60, myData.iSongLength%60, // it's not often to have a song more than 1h!
174 sTrack->str);
175
176 g_string_free (sTrack, TRUE);
177 }
178 else if (myData.cPlayingUri)
179 {
180 // no tags but with a path...
181 gchar *str = strrchr (myData.cPlayingUri, '/');
182 if (str)
183 str ++;
184 else
185 str = myData.cPlayingUri;
186 cairo_dock_remove_html_spaces (str); // %20 => " "
187 gldi_dialog_show_temporary_with_icon_printf ("%s : %s",
188 myIcon,
189 myContainer,
190 iDialogDuration,
191 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE,
192 D_("Current song"),
193 str);
194 } // else just ignore silently
195 }
196 else
197 {
198 gldi_dialog_show_temporary_with_icon (D_("There is no media playing."),
199 myIcon,
200 myContainer,
201 iDialogDuration,
202 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
203 }
204 }
205
206 /* Anime l'icone au changement de musique
207 */
208 void cd_musicplayer_animate_icon (int animationLength)
209 {
210 if (myDock && myConfig.cChangeAnimation != NULL)
211 {
212 CD_APPLET_ANIMATE_MY_ICON (myConfig.cChangeAnimation, animationLength);
213 }
214 }
215
216 /* Applique la surface correspondant a un etat sur l'icone.
217 */
218 void cd_musicplayer_apply_status_surface (MyPlayerStatus iStatus)
219 {
220 cd_debug ("%s (%d)", __func__, iStatus);
221 g_return_if_fail (iStatus < PLAYER_NB_STATUS);
222 gboolean bUse3DTheme = (CD_APPLET_MY_CONTAINER_IS_OPENGL && myConfig.bOpenglThemes);
223 cairo_surface_t *pSurface = myData.pSurfaces[iStatus];
224
225 // load the surface if not already in cache
226 if (pSurface == NULL)
227 {
228 gchar *cUserIcon = myConfig.cUserImage[iStatus];
229 if (cUserIcon != NULL) // l'utilisateur a defini une icone perso pour ce statut => on essaye de la charger.
230 {
231 gchar *cUserImagePath = cairo_dock_search_icon_s_path (cUserIcon, MAX (myIcon->image.iWidth, myIcon->image.iHeight));
232 myData.pSurfaces[iStatus] = CD_APPLET_LOAD_SURFACE_FOR_MY_APPLET (cUserImagePath ? cUserImagePath : cUserIcon); // si on a trouve une icone, on la prend, sinon on considere le fichier comme une image.
233 g_free (cUserImagePath);
234 }
235 if (myData.pSurfaces[iStatus] == NULL) // pas d'icone perso pour ce statut, ou l'icone specifiee n'a pas ete trouvee ou pas ete chargee => on prend l'icone par defaut.
236 {
237 const gchar **cIconName = (bUse3DTheme ? s_cDefaultIconName3D : s_cDefaultIconName);
238 gchar *cImagePath = g_strdup_printf (MY_APPLET_SHARE_DATA_DIR"/%s", cIconName[iStatus]);
239 myData.pSurfaces[iStatus] = CD_APPLET_LOAD_SURFACE_FOR_MY_APPLET (cImagePath);
240 g_free (cImagePath);
241 }
242 pSurface = myData.pSurfaces[iStatus];
243 g_return_if_fail (pSurface != NULL);
244 }
245
246 // apply the surface
247 if (bUse3DTheme) // 3D theme -> make a transition
248 {
249 if (myData.iPrevTextureCover != 0)
250 _cairo_dock_delete_texture (myData.iPrevTextureCover);
251 myData.iPrevTextureCover = myData.TextureCover;
252 myData.TextureCover = cairo_dock_create_texture_from_surface (pSurface);
253 if (myData.iPrevTextureCover != 0)
254 {
255 myData.iCoverTransition = NB_TRANSITION_STEP;
256 cairo_dock_launch_animation (myContainer);
257 }
258 else
259 {
260 cd_opengl_render_to_texture (myApplet);
261 CD_APPLET_REDRAW_MY_ICON;
262 }
263 }
264 else // just apply the surface (we could make a transition too ...)
265 {
266 CD_APPLET_SET_SURFACE_ON_MY_ICON (pSurface);
267 }
268 }
269
270 void cd_musiplayer_apply_cover (void)
271 {
272 cd_debug ("%s (%s)", __func__, myData.cCoverPath);
273 g_return_if_fail (myData.cCoverPath != NULL);
274
275 if (CD_APPLET_MY_CONTAINER_IS_OPENGL && myConfig.bOpenglThemes)
276 {
277 if (myData.iPrevTextureCover != 0)
278 _cairo_dock_delete_texture (myData.iPrevTextureCover);
279 myData.iPrevTextureCover = myData.TextureCover;
280 myData.TextureCover = cairo_dock_create_texture_from_image (myData.cCoverPath);
281 if (myData.iPrevTextureCover != 0)
282 {
283 myData.iCoverTransition = NB_TRANSITION_STEP;
284 cairo_dock_launch_animation (myContainer);
285 }
286 else
287 {
288 cd_opengl_render_to_texture (myApplet);
289 CD_APPLET_REDRAW_MY_ICON;
290 }
291 }
292 else
293 {
294 CD_APPLET_SET_IMAGE_ON_MY_ICON (myData.cCoverPath);
295 CD_APPLET_REDRAW_MY_ICON;
296 }
297 }