| 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/gi18n.h> |
| 23 |
|
| 24 |
#include "applet-struct.h" |
| 25 |
#include "applet-load-icons.h" |
| 26 |
#include "applet-drives.h" |
| 27 |
#include "applet-bookmarks.h" |
| 28 |
|
| 29 |
#define CD_SHORTCUT_DEFAULT_DIRECTORY_ICON_FILENAME "inode-directory" |
| 30 |
|
| 31 |
static const gchar * _get_custom_name_and_uri (gchar *cOneBookmark, gchar **cURI) |
| 32 |
{ |
| 33 |
const gchar *cUserName = NULL; |
| 34 |
// should not happen if we add bookmarks via the dock or Nautilus |
| 35 |
if (*cOneBookmark == '/') |
| 36 |
{ |
| 37 |
// for 'gvfs_launch_uri': |
| 38 |
*cURI = g_strconcat ("file://", cOneBookmark, NULL); |
| 39 |
g_free (cOneBookmark); |
| 40 |
} |
| 41 |
else // it's a valid URI but does it have a custom name? |
| 42 |
{ |
| 43 |
*cURI = cOneBookmark; |
| 44 |
// a custom name is separated with a whitespace (no whitespace in the URI) |
| 45 |
gchar *str = strchr (cOneBookmark, ' '); |
| 46 |
if (str != NULL) |
| 47 |
{ |
| 48 |
cUserName = str + 1; |
| 49 |
*str = '\0'; |
| 50 |
} |
| 51 |
} |
| 52 |
return cUserName; |
| 53 |
} |
| 54 |
|
| 55 |
static Icon * _cd_shortcuts_get_icon (gchar *cFileName, const gchar *cUserName, double fCurrentOrder) |
| 56 |
{ |
| 57 |
cd_debug ("New icon: %s, %s, %f", cFileName, cUserName, fCurrentOrder); |
| 58 |
|
| 59 |
/* Nautilus adds custom prefixes which are not supported by gvfs... |
| 60 |
* gvfs-integration plugin can read x-nautilus-desktop but not others, e.g.: |
| 61 |
* x-nautilus-search://0/ => specific to Nautilus: open these URI with it. |
| 62 |
* Note that all these URI have a user-name |
| 63 |
*/ |
| 64 |
if (g_str_has_prefix (cFileName, "x-nautilus-") |
| 65 |
&& ! g_str_has_prefix (cFileName, "x-nautilus-desktop://")) |
| 66 |
{ |
| 67 |
Icon *pNewIcon = cairo_dock_create_dummy_launcher ( |
| 68 |
cUserName ? g_strdup (cUserName) : g_strdup (cFileName), |
| 69 |
cairo_dock_search_icon_s_path ( |
| 70 |
CD_SHORTCUT_DEFAULT_DIRECTORY_ICON_FILENAME, |
| 71 |
CAIRO_DOCK_DEFAULT_ICON_SIZE), |
| 72 |
g_strdup_printf ("nautilus %s", cFileName), |
| 73 |
NULL, |
| 74 |
fCurrentOrder); |
| 75 |
pNewIcon->iGroup = CD_BOOKMARK_GROUP; |
| 76 |
pNewIcon->cBaseURI = cFileName; |
| 77 |
pNewIcon->iVolumeID = CD_VOLUME_ID_BOOKMARK_CMD; |
| 78 |
return pNewIcon; |
| 79 |
} |
| 80 |
|
| 81 |
gchar *cName, *cRealURI, *cIconName; |
| 82 |
gboolean bIsDirectory; |
| 83 |
gint iVolumeID; |
| 84 |
gdouble fOrder; |
| 85 |
if (! cairo_dock_fm_get_file_info (cFileName, &cName, &cRealURI, &cIconName, |
| 86 |
&bIsDirectory, &iVolumeID, &fOrder, CAIRO_DOCK_FM_SORT_BY_NAME)) |
| 87 |
return NULL; |
| 88 |
if (cUserName != NULL) |
| 89 |
{ |
| 90 |
g_free (cName); |
| 91 |
if (cName == NULL) // a bookmark on a unmounted system or a folder that doesn't exist any more |
| 92 |
cName = g_strdup_printf ("%s\n[%s]", cUserName, D_("Unmounted")); |
| 93 |
else |
| 94 |
cName = g_strdup (cUserName); |
| 95 |
} |
| 96 |
else if (cName == NULL) // a bookmark on a unmounted system |
| 97 |
{ |
| 98 |
gchar *cGuessedName = g_path_get_basename (cFileName); |
| 99 |
cairo_dock_remove_html_spaces (cGuessedName); // or: g_uri_unescape_string |
| 100 |
cName = g_strdup_printf ("%s\n[%s]", cGuessedName, D_("Unmounted")); |
| 101 |
g_free (cGuessedName); |
| 102 |
} |
| 103 |
if (cRealURI == NULL) |
| 104 |
cRealURI = g_strdup (cFileName); |
| 105 |
if (cIconName == NULL) |
| 106 |
cIconName = cairo_dock_search_icon_s_path ( |
| 107 |
CD_SHORTCUT_DEFAULT_DIRECTORY_ICON_FILENAME, |
| 108 |
CAIRO_DOCK_DEFAULT_ICON_SIZE); // should be the default icon |
| 109 |
|
| 110 |
Icon *pNewIcon = cairo_dock_create_dummy_launcher (cName, |
| 111 |
cIconName, |
| 112 |
cRealURI, |
| 113 |
NULL, |
| 114 |
fCurrentOrder); |
| 115 |
pNewIcon->iGroup = CD_BOOKMARK_GROUP; |
| 116 |
pNewIcon->cBaseURI = cFileName; |
| 117 |
pNewIcon->iVolumeID = iVolumeID; |
| 118 |
return pNewIcon; |
| 119 |
} |
| 120 |
|
| 121 |
static GList * _get_item_with_base_uri_icon (GList *pIconList, const gchar *cBaseURI) |
| 122 |
{ |
| 123 |
GList* ic; |
| 124 |
Icon *pIcon; |
| 125 |
for (ic = pIconList; ic != NULL; ic = ic->next) |
| 126 |
{ |
| 127 |
pIcon = ic->data; |
| 128 |
if (pIcon->cBaseURI != NULL && strcmp (pIcon->cBaseURI, cBaseURI) == 0) |
| 129 |
return ic; |
| 130 |
} |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
|
| 134 |
static void _remove_old_icons_and_free_list (GList *pOldBookmarkList) |
| 135 |
{ |
| 136 |
GList* ic; |
| 137 |
Icon *pIcon; |
| 138 |
for (ic = pOldBookmarkList; ic != NULL; ic = ic->next) |
| 139 |
{ |
| 140 |
pIcon = ic->data; |
| 141 |
CD_APPLET_REMOVE_ICON_FROM_MY_ICONS_LIST (pIcon); |
| 142 |
} |
| 143 |
g_list_free (pOldBookmarkList); |
| 144 |
} |
| 145 |
|
| 146 |
void cd_shortcuts_on_bookmarks_event (CairoDockFMEventType iEventType, const gchar *cURI, GldiModuleInstance *myApplet) |
| 147 |
{ |
| 148 |
static int iTime = 0; |
| 149 |
iTime ++; |
| 150 |
CD_APPLET_ENTER; |
| 151 |
//g_print ("%s (%d)\n", __func__, iEventType); |
| 152 |
GList *pIconsList = CD_APPLET_MY_ICONS_LIST; |
| 153 |
GList *pOldBookmarkList; |
| 154 |
Icon *icon; |
| 155 |
GList *ic; |
| 156 |
double fCurrentOrder = 1.; |
| 157 |
// optimization: skip the disks and networks, and point on the first bookmark. |
| 158 |
for (ic = pIconsList; ic != NULL; ic = ic->next) |
| 159 |
{ |
| 160 |
icon = ic->data; |
| 161 |
icon->fOrder = fCurrentOrder++; |
| 162 |
if (icon->iGroup == (CairoDockIconGroup) CD_BOOKMARK_GROUP) |
| 163 |
break; |
| 164 |
} |
| 165 |
/* Note that since the first bookmark is always the Home Folder, |
| 166 |
* 'pIconsList' will never change when inserting/removing a bookmark. |
| 167 |
*/ |
| 168 |
GldiContainer *pContainer = CD_APPLET_MY_ICONS_LIST_CONTAINER; |
| 169 |
CD_APPLET_LEAVE_IF_FAIL (pContainer != NULL); |
| 170 |
|
| 171 |
// make a copy of a sublist that can be manipulated independently |
| 172 |
// of pIconsList which is part of our subdock's icons |
| 173 |
pOldBookmarkList = ic->next; |
| 174 |
if (pOldBookmarkList) |
| 175 |
{ |
| 176 |
pOldBookmarkList->prev = NULL; |
| 177 |
pOldBookmarkList = g_list_copy (pOldBookmarkList); |
| 178 |
ic->next->prev = ic; |
| 179 |
} |
| 180 |
|
| 181 |
// Bookmarks file has been modified |
| 182 |
if (iEventType == CAIRO_DOCK_FILE_CREATED || iEventType == CAIRO_DOCK_FILE_MODIFIED) |
| 183 |
{ |
| 184 |
cd_message ("The bookmarks list has changed"); |
| 185 |
|
| 186 |
//\____________________ Read bookmarks file |
| 187 |
gchar *cContent = NULL; |
| 188 |
gsize length=0; |
| 189 |
GError *erreur = NULL; |
| 190 |
g_file_get_contents (myData.cBookmarksURI, &cContent, &length, &erreur); |
| 191 |
if (erreur != NULL) |
| 192 |
{ |
| 193 |
cd_warning ("when trying to get the bookmarks : %s", erreur->message); |
| 194 |
g_error_free (erreur); |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
gchar **cBookmarksList = g_strsplit (cContent, "\n", -1); |
| 199 |
g_free (cContent); |
| 200 |
|
| 201 |
//\____________________ Read the content. |
| 202 |
/* Bookmarks are listed in the order of the file; we need to |
| 203 |
* reorder each icon in case a bookmark has changed its place, or |
| 204 |
* if a new one appeared (the first one is always the Home Folder). |
| 205 |
*/ |
| 206 |
gchar *cOneBookmark; |
| 207 |
Icon *pNewIcon, *pExistingIcon; |
| 208 |
GList *pExistingIconNode; |
| 209 |
const gchar *cUserName; |
| 210 |
int i; |
| 211 |
for (i = 0; cBookmarksList[i] != NULL; i ++) |
| 212 |
{ |
| 213 |
cOneBookmark = cBookmarksList[i]; |
| 214 |
if (*cOneBookmark == '\0' || *cOneBookmark == '#') |
| 215 |
{ |
| 216 |
g_free (cOneBookmark); |
| 217 |
continue; |
| 218 |
} |
| 219 |
|
| 220 |
// Grab the custom name if any |
| 221 |
cUserName = _get_custom_name_and_uri (cBookmarksList[i], &cOneBookmark); |
| 222 |
|
| 223 |
// Check if the icon already exists and has changed |
| 224 |
pExistingIconNode = _get_item_with_base_uri_icon (pOldBookmarkList, cOneBookmark); |
| 225 |
if (pExistingIconNode != NULL) |
| 226 |
{ |
| 227 |
pExistingIcon = pExistingIconNode->data; |
| 228 |
// move this node to the subdock icons list |
| 229 |
pOldBookmarkList = g_list_delete_link (pOldBookmarkList, pExistingIconNode); |
| 230 |
if (cUserName && g_strcmp0 (pExistingIcon->cName, cUserName) != 0) |
| 231 |
{ |
| 232 |
CD_APPLET_REMOVE_ICON_FROM_MY_ICONS_LIST (pExistingIcon); // will destroy it |
| 233 |
pExistingIcon = NULL; |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
pExistingIcon->fOrder = fCurrentOrder++; |
| 238 |
g_free (cOneBookmark); |
| 239 |
} |
| 240 |
} |
| 241 |
else |
| 242 |
pExistingIcon = NULL; |
| 243 |
|
| 244 |
if (pExistingIcon == NULL) |
| 245 |
{ |
| 246 |
pNewIcon = _cd_shortcuts_get_icon (cOneBookmark, |
| 247 |
cUserName, fCurrentOrder); |
| 248 |
if (pNewIcon) |
| 249 |
{ |
| 250 |
CD_APPLET_ADD_ICON_IN_MY_ICONS_LIST (pNewIcon); |
| 251 |
fCurrentOrder++; |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
cd_warning ("couldn't get info on bookmark '%s'", cOneBookmark); |
| 256 |
g_free (cOneBookmark); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
g_free (cBookmarksList); |
| 261 |
|
| 262 |
/* Again, since 'Home Folder' is always the first bookmark, |
| 263 |
* the head of the list won't change even if there are only bookmarks |
| 264 |
* (so we don't need to re-assigne it to the container). |
| 265 |
*/ |
| 266 |
cairo_dock_sort_icons_by_order (pIconsList); |
| 267 |
} |
| 268 |
} |
| 269 |
_remove_old_icons_and_free_list (pOldBookmarkList); |
| 270 |
CD_APPLET_LEAVE(); |
| 271 |
} |
| 272 |
|
| 273 |
void cd_shortcuts_remove_one_bookmark (const gchar *cURI, GldiModuleInstance *myApplet) |
| 274 |
{ |
| 275 |
g_return_if_fail (cURI != NULL); |
| 276 |
cd_message ("%s (%s)", __func__, cURI); |
| 277 |
|
| 278 |
gchar *cContent = NULL; |
| 279 |
gsize length=0; |
| 280 |
GError *erreur = NULL; |
| 281 |
g_file_get_contents (myData.cBookmarksURI, &cContent, &length, &erreur); |
| 282 |
if (erreur != NULL) |
| 283 |
{ |
| 284 |
cd_warning ("while trying to read bookmarks file : %s", erreur->message); |
| 285 |
g_error_free (erreur); |
| 286 |
} |
| 287 |
else |
| 288 |
{ |
| 289 |
gchar **cBookmarksList = g_strsplit (cContent, "\n", -1); |
| 290 |
g_free (cContent); |
| 291 |
gchar *cOneBookmark, *str; |
| 292 |
gboolean bFound = FALSE; |
| 293 |
int i = 0; |
| 294 |
for (i = 0; cBookmarksList[i] != NULL; i ++) |
| 295 |
{ |
| 296 |
cOneBookmark = cBookmarksList[i]; |
| 297 |
if (*cOneBookmark == '\0' || *cOneBookmark == '#') |
| 298 |
continue; |
| 299 |
|
| 300 |
str = strchr (cOneBookmark, ' '); |
| 301 |
if ((str && strncmp (cOneBookmark, cURI, str - cOneBookmark) == 0) |
| 302 |
|| (!str && strcmp (cOneBookmark, cURI) == 0)) |
| 303 |
{ |
| 304 |
// remove this element from the array |
| 305 |
int j; |
| 306 |
for (j = i; cBookmarksList[j] != NULL; j ++) |
| 307 |
{ |
| 308 |
cBookmarksList[j] = cBookmarksList[j+1]; |
| 309 |
} |
| 310 |
// free the removed element. |
| 311 |
g_free (cOneBookmark); |
| 312 |
// quit the loop |
| 313 |
bFound = TRUE; |
| 314 |
break; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if (! bFound) |
| 319 |
{ |
| 320 |
cd_warning ("bookmark '%s' not found", cURI); |
| 321 |
} |
| 322 |
else |
| 323 |
{ |
| 324 |
cContent = g_strjoinv ("\n", cBookmarksList); |
| 325 |
g_file_set_contents (myData.cBookmarksURI, cContent, -1, &erreur); |
| 326 |
if (erreur != NULL) |
| 327 |
{ |
| 328 |
cd_warning ("while trying to write bookmarks file : %s", erreur->message); |
| 329 |
g_error_free (erreur); |
| 330 |
} |
| 331 |
g_free (cContent); |
| 332 |
} |
| 333 |
g_strfreev (cBookmarksList); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
void cd_shortcuts_rename_one_bookmark (const gchar *cURI, const gchar *cName, GldiModuleInstance *myApplet) |
| 338 |
{ |
| 339 |
g_return_if_fail (cURI != NULL); |
| 340 |
cd_message ("%s (%s, %s)", __func__, cURI, cName); |
| 341 |
|
| 342 |
gchar *cContent = NULL; |
| 343 |
gsize length=0; |
| 344 |
GError *erreur = NULL; |
| 345 |
g_file_get_contents (myData.cBookmarksURI, &cContent, &length, &erreur); |
| 346 |
if (erreur != NULL) |
| 347 |
{ |
| 348 |
cd_warning ("while trying to read bookmarks file : %s", erreur->message); |
| 349 |
g_error_free (erreur); |
| 350 |
} |
| 351 |
else |
| 352 |
{ |
| 353 |
gchar **cBookmarksList = g_strsplit (cContent, "\n", -1); |
| 354 |
g_free (cContent); |
| 355 |
gchar *cOneBookmark, *str; |
| 356 |
int i = 0; |
| 357 |
for (i = 0; cBookmarksList[i] != NULL; i ++) |
| 358 |
{ |
| 359 |
cOneBookmark = cBookmarksList[i]; |
| 360 |
if (*cOneBookmark == '\0' || *cOneBookmark == '#') |
| 361 |
continue; |
| 362 |
|
| 363 |
str = strchr (cOneBookmark, ' '); |
| 364 |
if ((str && strncmp (cOneBookmark, cURI, str - cOneBookmark) == 0) || (!str && strcmp (cOneBookmark, cURI) == 0)) |
| 365 |
{ |
| 366 |
cBookmarksList[i] = g_strdup_printf ("%s %s", cURI, cName); |
| 367 |
g_free (cOneBookmark); |
| 368 |
break; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if (cBookmarksList[i] == NULL) |
| 373 |
{ |
| 374 |
cd_warning ("bookmark '%s' not found", cURI); |
| 375 |
} |
| 376 |
else |
| 377 |
{ |
| 378 |
cContent = g_strjoinv ("\n", cBookmarksList); |
| 379 |
g_file_set_contents (myData.cBookmarksURI, cContent, -1, &erreur); |
| 380 |
if (erreur != NULL) |
| 381 |
{ |
| 382 |
cd_warning ("while trying to write bookmarks file : %s", erreur->message); |
| 383 |
g_error_free (erreur); |
| 384 |
} |
| 385 |
g_free (cContent); |
| 386 |
} |
| 387 |
g_strfreev (cBookmarksList); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
void cd_shortcuts_add_one_bookmark (const gchar *cURI, GldiModuleInstance *myApplet) |
| 392 |
{ |
| 393 |
g_return_if_fail (cURI != NULL); |
| 394 |
cd_message ("%s (%s)", __func__, cURI); |
| 395 |
|
| 396 |
// see if we need to add a new line before the new URI. |
| 397 |
gchar *cContent = NULL; |
| 398 |
gsize length = 0; |
| 399 |
g_file_get_contents (myData.cBookmarksURI, |
| 400 |
&cContent, |
| 401 |
&length, |
| 402 |
NULL); |
| 403 |
gboolean bAddNewLine = (cContent && length > 0 && cContent[length-1] != '\n'); |
| 404 |
g_free (cContent); |
| 405 |
|
| 406 |
// append the new URI to the file. |
| 407 |
FILE *f = fopen (myData.cBookmarksURI, "a"); |
| 408 |
if (f != NULL) |
| 409 |
{ |
| 410 |
gchar *cNewLine = g_strdup_printf ("%s%s\n", bAddNewLine ? "\n" : "", cURI); |
| 411 |
fputs(cNewLine, f); |
| 412 |
g_free (cNewLine); |
| 413 |
fclose (f); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
GList *cd_shortcuts_list_bookmarks (gchar *cBookmarkFilePath, GldiModuleInstance *myApplet) |
| 418 |
{ |
| 419 |
GList *pBookmarkIconList = NULL; |
| 420 |
Icon *pNewIcon; |
| 421 |
double fCurrentOrder = 0.; |
| 422 |
|
| 423 |
// Home |
| 424 |
gchar *cHome = g_strdup_printf ("file://%s", g_getenv ("HOME")); |
| 425 |
pNewIcon = _cd_shortcuts_get_icon (cHome, D_("Home Folder"), fCurrentOrder++); |
| 426 |
if (pNewIcon != NULL) |
| 427 |
{ |
| 428 |
_init_disk_usage (pNewIcon, myApplet); |
| 429 |
CDDiskUsage *pDiskUsage = CD_APPLET_GET_MY_ICON_DATA (pNewIcon); |
| 430 |
if (pDiskUsage) // so that this bookmark will never be considered old, and therefore removed. |
| 431 |
pDiskUsage->iLastCheckTime = 1e9; |
| 432 |
pBookmarkIconList = g_list_append (pBookmarkIconList, pNewIcon); |
| 433 |
} |
| 434 |
else |
| 435 |
g_free (cHome); |
| 436 |
|
| 437 |
gchar *cContent = NULL; |
| 438 |
gsize length = 0; |
| 439 |
GError *erreur = NULL; |
| 440 |
g_file_get_contents (cBookmarkFilePath, &cContent, &length, &erreur); |
| 441 |
if (erreur != NULL) |
| 442 |
{ |
| 443 |
cd_warning ("Attention: %s\n no bookmark will be available", erreur->message); |
| 444 |
g_error_free (erreur); |
| 445 |
} |
| 446 |
else |
| 447 |
{ |
| 448 |
gchar **cBookmarksList = g_strsplit (cContent, "\n", -1); |
| 449 |
g_free (cContent); |
| 450 |
|
| 451 |
gchar *cOneBookmark; |
| 452 |
const gchar *cUserName; |
| 453 |
int i = 0; |
| 454 |
for (i = 0; cBookmarksList[i] != NULL; i ++) |
| 455 |
{ |
| 456 |
cUserName = _get_custom_name_and_uri (cBookmarksList[i], &cOneBookmark); |
| 457 |
if (*cOneBookmark != '\0' && *cOneBookmark != '#') |
| 458 |
{ |
| 459 |
cd_message (" + 1 bookmark : %s", cOneBookmark); |
| 460 |
pNewIcon = _cd_shortcuts_get_icon (cOneBookmark, cUserName, fCurrentOrder++); |
| 461 |
if (pNewIcon) |
| 462 |
pBookmarkIconList = g_list_append (pBookmarkIconList, pNewIcon); |
| 463 |
else |
| 464 |
g_free (cOneBookmark); |
| 465 |
} |
| 466 |
else |
| 467 |
{ |
| 468 |
g_free (cOneBookmark); |
| 469 |
} |
| 470 |
} |
| 471 |
g_free (cBookmarksList); |
| 472 |
} |
| 473 |
return pBookmarkIconList; |
| 474 |
} |