| 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 <glib/gstdio.h> |
| 21 |
#include "applet-amazon.h" |
| 22 |
#include "applet-draw.h" |
| 23 |
#include "applet-cover.h" |
| 24 |
|
| 25 |
static void cd_musicplayer_dl_cover (void); |
| 26 |
|
| 27 |
static const gchar *cCoverNames[] = {"cover.jpg", "Cover.jpg", "cover.jpeg", "album.jpg", "albumart.jpg", "folder.jpg", "album.jpeg", "albumart.jpeg", "folder.jpeg", NULL}; |
| 28 |
|
| 29 |
static gchar *_find_cover_in_common_dirs (void) |
| 30 |
{ |
| 31 |
gchar *cCoverPath = NULL; |
| 32 |
// search a cover in the local folder where the song is located. |
| 33 |
gchar *cSongPath = (myData.cPlayingUri ? g_filename_from_uri (myData.cPlayingUri, NULL, NULL) : NULL); |
| 34 |
if (cSongPath != NULL) // local file, let's go on. |
| 35 |
{ |
| 36 |
gchar *cSongDir = g_path_get_dirname (cSongPath); |
| 37 |
g_free (cSongPath); |
| 38 |
|
| 39 |
cCoverPath = g_strdup_printf ("%s/%s - %s.jpg", cSongDir, myData.cArtist, myData.cAlbum); |
| 40 |
if (! g_file_test (cCoverPath, G_FILE_TEST_EXISTS)) // the default path doesn't exist, loop on the common names |
| 41 |
{ |
| 42 |
int i; |
| 43 |
for (i = 0; cCoverNames[i] != NULL; i ++) |
| 44 |
{ |
| 45 |
cCoverPath = g_strdup_printf ("%s/%s", cSongDir, cCoverNames[i]); |
| 46 |
if (! g_file_test (cCoverPath, G_FILE_TEST_EXISTS)) |
| 47 |
{ |
| 48 |
g_free (cCoverPath); |
| 49 |
cCoverPath = NULL; |
| 50 |
} |
| 51 |
else |
| 52 |
break; |
| 53 |
} |
| 54 |
} |
| 55 |
cd_debug ("MP - CoverPath: %s", cCoverPath); |
| 56 |
g_free (cSongDir); |
| 57 |
} |
| 58 |
|
| 59 |
// if not found, search in the cache |
| 60 |
if (cCoverPath == NULL) |
| 61 |
{ |
| 62 |
cd_debug("MP : we can also check the 'cache' directory"); |
| 63 |
if (myData.pCurrentHandler->cCoverDir) // the player has its own cache. |
| 64 |
{ |
| 65 |
cCoverPath = g_strdup_printf("%s/%s - %s.jpg", myData.pCurrentHandler->cCoverDir, myData.cArtist, myData.cAlbum); |
| 66 |
} |
| 67 |
else // else, use our own cache. |
| 68 |
{ |
| 69 |
cCoverPath = g_strdup_printf ("%s/musicplayer/%s - %s.jpg", g_cCairoDockDataDir, myData.cArtist, myData.cAlbum); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return cCoverPath; |
| 74 |
} |
| 75 |
|
| 76 |
static gboolean cd_musicplayer_check_size_is_constant (const gchar *cFilePath) |
| 77 |
{ |
| 78 |
int iSize = cairo_dock_get_file_size (cFilePath); |
| 79 |
gboolean bConstantSize = (iSize != 0 && iSize == myData.iCurrentFileSize); |
| 80 |
myData.iCurrentFileSize = iSize; |
| 81 |
cd_debug ("MP: file size: %d", iSize); |
| 82 |
return bConstantSize; |
| 83 |
} |
| 84 |
|
| 85 |
static gboolean _check_cover_file_size (gpointer data) |
| 86 |
{ |
| 87 |
myData.iNbCheckCover ++; |
| 88 |
if (myData.iNbCheckCover > 5) |
| 89 |
{ |
| 90 |
// still no file, try to set it ourselves. |
| 91 |
g_free (myData.cCoverPath); |
| 92 |
myData.cCoverPath = _find_cover_in_common_dirs (); |
| 93 |
if (myData.cCoverPath != NULL) |
| 94 |
{ |
| 95 |
if (cairo_dock_strings_differ (myData.cCoverPath, myData.cPreviousCoverPath)) // cover has changed, apply it. |
| 96 |
{ |
| 97 |
cd_musiplayer_apply_cover (); |
| 98 |
} |
| 99 |
} |
| 100 |
else if (myConfig.bDownload) |
| 101 |
{ |
| 102 |
cd_musicplayer_dl_cover (); |
| 103 |
} |
| 104 |
|
| 105 |
myData.iSidCheckCover = 0; |
| 106 |
return FALSE; |
| 107 |
} |
| 108 |
if (cd_musicplayer_check_size_is_constant (myData.cCoverPath)) |
| 109 |
{ |
| 110 |
/// file is complete, apply it on the icon... |
| 111 |
myData.cover_exist = TRUE; |
| 112 |
if (myData.iPlayingStatus == PLAYER_PLAYING || myData.iPlayingStatus == PLAYER_PAUSED) // if it has stopped before we got the cover, don't draw it. |
| 113 |
{ |
| 114 |
cd_musiplayer_apply_cover (); |
| 115 |
} |
| 116 |
|
| 117 |
myData.iSidCheckCover = 0; |
| 118 |
return FALSE; |
| 119 |
} |
| 120 |
return TRUE; |
| 121 |
} |
| 122 |
static gboolean _check_cover_file_exists (gpointer data) |
| 123 |
{ |
| 124 |
myData.iNbCheckCover ++; |
| 125 |
if (myData.iNbCheckCover > 3) |
| 126 |
{ |
| 127 |
// still no file, try to set it ourselves. |
| 128 |
g_free (myData.cCoverPath); |
| 129 |
myData.cCoverPath = _find_cover_in_common_dirs (); |
| 130 |
if (myData.cCoverPath != NULL) |
| 131 |
{ |
| 132 |
if (cairo_dock_strings_differ (myData.cCoverPath, myData.cPreviousCoverPath)) // cover has changed, apply it. |
| 133 |
{ |
| 134 |
myData.iNbCheckCover = 0; |
| 135 |
myData.iSidCheckCover = g_timeout_add_seconds (1, (GSourceFunc)_check_cover_file_size, NULL); |
| 136 |
return FALSE; |
| 137 |
} |
| 138 |
} |
| 139 |
else if (myConfig.bDownload) |
| 140 |
{ |
| 141 |
cd_musicplayer_dl_cover (); |
| 142 |
} |
| 143 |
|
| 144 |
myData.iSidCheckCover = 0; |
| 145 |
return FALSE; |
| 146 |
} |
| 147 |
|
| 148 |
if (myData.cCoverPath && g_file_test (myData.cCoverPath, G_FILE_TEST_EXISTS)) |
| 149 |
{ |
| 150 |
/// file exists, now check for its size |
| 151 |
myData.iNbCheckCover = 0; |
| 152 |
myData.iSidCheckCover = g_timeout_add_seconds (1, (GSourceFunc)_check_cover_file_size, NULL); |
| 153 |
|
| 154 |
return FALSE; |
| 155 |
} |
| 156 |
return TRUE; |
| 157 |
} |
| 158 |
/* Not used |
| 159 |
static gboolean _get_cover_again (gpointer data) |
| 160 |
{ |
| 161 |
cd_debug ("%s ()", __func__); |
| 162 |
myData.iNbCheckCover ++; |
| 163 |
if (myData.iNbCheckCover > 1) |
| 164 |
{ |
| 165 |
// still no file, try to set it ourselves. |
| 166 |
g_free (myData.cCoverPath); |
| 167 |
myData.cCoverPath = _find_cover_in_common_dirs (); |
| 168 |
if (myData.cCoverPath != NULL) |
| 169 |
{ |
| 170 |
if (cairo_dock_strings_differ (myData.cCoverPath, myData.cPreviousCoverPath)) // cover has changed, apply it. |
| 171 |
{ |
| 172 |
myData.iNbCheckCover = 0; |
| 173 |
myData.iSidCheckCover = g_timeout_add_seconds (1, (GSourceFunc)_check_cover_file_size, NULL); |
| 174 |
return FALSE; |
| 175 |
} |
| 176 |
} |
| 177 |
else if (myConfig.bDownload) |
| 178 |
{ |
| 179 |
cd_musicplayer_dl_cover (); |
| 180 |
} |
| 181 |
|
| 182 |
myData.iSidCheckCover = 0; |
| 183 |
return FALSE; |
| 184 |
} |
| 185 |
|
| 186 |
myData.pCurrentHandler->get_cover (); // will call 'cd_musicplayer_set_cover_path()' if it got a cover. |
| 187 |
return TRUE; |
| 188 |
} |
| 189 |
*/ |
| 190 |
/* 3 cases: |
| 191 |
- a file is given and exists -> wait until its size is constant |
| 192 |
- a file is given but doesn't exist yet -> wait until it exists, then until its size is constant |
| 193 |
- nothing -> search in the local dir, then in the cache -> if exists, wait until its size is constant, else download it. |
| 194 |
*/ |
| 195 |
static void _reset_cover_state (void) |
| 196 |
{ |
| 197 |
myData.cover_exist = FALSE; |
| 198 |
myData.iCurrentFileSize = 0; |
| 199 |
if (myData.iSidCheckCover != 0) |
| 200 |
{ |
| 201 |
g_source_remove (myData.iSidCheckCover); |
| 202 |
myData.iSidCheckCover = 0; |
| 203 |
} |
| 204 |
myData.iNbCheckCover = 0; |
| 205 |
if (myData.pCoverTask != NULL) |
| 206 |
{ |
| 207 |
gldi_task_discard (myData.pCoverTask); |
| 208 |
myData.pCoverTask = NULL; |
| 209 |
} |
| 210 |
} |
| 211 |
void cd_musicplayer_set_cover_path (const gchar *cGivenCoverPath) |
| 212 |
{ |
| 213 |
if (! myConfig.bEnableCover) // cover not welcome => abort the mission. |
| 214 |
{ |
| 215 |
myData.cover_exist = FALSE; |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if (myData.cCoverPath && ! cairo_dock_strings_differ (myData.cCoverPath, cGivenCoverPath)) // cover has not changed => nothing to do (if we are checking for it, keep doing). |
| 220 |
{ |
| 221 |
return; |
| 222 |
} |
| 223 |
cd_debug ("%s (%s -> %s)", __func__, myData.cCoverPath, cGivenCoverPath); |
| 224 |
|
| 225 |
g_free (myData.cPreviousCoverPath); |
| 226 |
myData.cPreviousCoverPath = myData.cCoverPath; // remember the previous cover. |
| 227 |
myData.cCoverPath = NULL; |
| 228 |
|
| 229 |
if (cGivenCoverPath != NULL) // we got something from the service, check it. |
| 230 |
{ |
| 231 |
if (strncmp (cGivenCoverPath, "file://", 7) == 0) // local URI |
| 232 |
{ |
| 233 |
myData.cCoverPath = g_filename_from_uri (cGivenCoverPath, NULL, NULL); |
| 234 |
} |
| 235 |
else // local file or remote adress. |
| 236 |
{ |
| 237 |
myData.cCoverPath = g_strdup (cGivenCoverPath); |
| 238 |
} |
| 239 |
|
| 240 |
if (myData.cCoverPath != NULL && cairo_dock_strings_differ (myData.cCoverPath, myData.cPreviousCoverPath)) // cover is valid and different from the previous one |
| 241 |
{ |
| 242 |
_reset_cover_state (); |
| 243 |
if (! g_file_test (myData.cCoverPath, G_FILE_TEST_EXISTS)) // file does not exist, re-try a few times. |
| 244 |
{ |
| 245 |
myData.iSidCheckCover = g_timeout_add_seconds (1, (GSourceFunc)_check_cover_file_exists, NULL); |
| 246 |
} |
| 247 |
else // file exists, check its size until it's constant |
| 248 |
{ |
| 249 |
myData.iSidCheckCover = g_timeout_add_seconds (1, (GSourceFunc)_check_cover_file_size, NULL); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
else // no data from the service, search by ourselves. |
| 254 |
{ |
| 255 |
myData.cCoverPath = _find_cover_in_common_dirs (); |
| 256 |
if (myData.cCoverPath != NULL && cairo_dock_strings_differ (myData.cCoverPath, myData.cPreviousCoverPath)) // cover is valid and different from the previous one. |
| 257 |
{ |
| 258 |
_reset_cover_state (); |
| 259 |
if (g_file_test (myData.cCoverPath, G_FILE_TEST_EXISTS)) // cover is already persent on the disk -> check it |
| 260 |
{ |
| 261 |
myData.iSidCheckCover = g_timeout_add_seconds (1, (GSourceFunc)_check_cover_file_size, NULL); |
| 262 |
} |
| 263 |
else // cover is not yet on the disk (cache) -> download it. |
| 264 |
{ |
| 265 |
if (myConfig.bDownload) |
| 266 |
{ |
| 267 |
cd_musicplayer_dl_cover (); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
static void _get_cover_async (CDSharedMemory *pSharedMemory) |
| 276 |
{ |
| 277 |
pSharedMemory->bSuccess = cd_amazon_dl_cover (pSharedMemory->cArtist, pSharedMemory->cAlbum, pSharedMemory->cPlayingUri, pSharedMemory->cLocalPath); |
| 278 |
} |
| 279 |
static gboolean _on_got_cover (CDSharedMemory *pSharedMemory) |
| 280 |
{ |
| 281 |
CD_APPLET_ENTER; |
| 282 |
if (pSharedMemory->bSuccess) |
| 283 |
{ |
| 284 |
myData.cover_exist = TRUE; |
| 285 |
cd_musiplayer_apply_cover (); |
| 286 |
} |
| 287 |
gldi_task_discard (myData.pCoverTask); |
| 288 |
myData.pCoverTask = NULL; |
| 289 |
CD_APPLET_LEAVE (FALSE); |
| 290 |
} |
| 291 |
static void _free_shared_memory (CDSharedMemory *pSharedMemory) |
| 292 |
{ |
| 293 |
g_free (pSharedMemory->cArtist); |
| 294 |
g_free (pSharedMemory->cAlbum); |
| 295 |
g_free (pSharedMemory->cPlayingUri); |
| 296 |
g_free (pSharedMemory->cLocalPath); |
| 297 |
g_free (pSharedMemory); |
| 298 |
} |
| 299 |
static void cd_musicplayer_dl_cover (void) |
| 300 |
{ |
| 301 |
cd_debug ("MP-COVER - %s (%s, %s, %s)", __func__, myData.cArtist, myData.cAlbum, myData.cPlayingUri); |
| 302 |
|
| 303 |
if (myData.pCoverTask != NULL) |
| 304 |
{ |
| 305 |
gldi_task_discard (myData.pCoverTask); |
| 306 |
myData.pCoverTask = NULL; |
| 307 |
} |
| 308 |
|
| 309 |
CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1); |
| 310 |
pSharedMemory->cArtist = g_strdup (myData.cArtist); |
| 311 |
pSharedMemory->cAlbum = g_strdup (myData.cAlbum); |
| 312 |
pSharedMemory->cPlayingUri = g_strdup (myData.cPlayingUri); |
| 313 |
pSharedMemory->cLocalPath = g_strdup (myData.cCoverPath); |
| 314 |
|
| 315 |
myData.pCoverTask = gldi_task_new_full (0, |
| 316 |
(GldiGetDataAsyncFunc) _get_cover_async, |
| 317 |
(GldiUpdateSyncFunc) _on_got_cover, |
| 318 |
(GFreeFunc) _free_shared_memory, |
| 319 |
pSharedMemory); |
| 320 |
gldi_task_launch (myData.pCoverTask); |
| 321 |
} |