gtkbookmarksmanager.c 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* GTK - The GIMP Toolkit
 * gtkbookmarksmanager.c: Utilities to manage and monitor ~/.gtk-bookmarks
 * Copyright (C) 2003, Red Hat, Inc.
 * Copyright (C) 2007-2008 Carlos Garnacho
 * Copyright (C) 2011 Suse
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
10
 * published by the Free Software Foundation; either version 2.1 of the
11 12 13 14 15
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * Authors: Federico Mena Quintero <federico@gnome.org>
 */

#include "config.h"

#include <string.h>

#include <glib/gi18n-lib.h>

#include "gtkbookmarksmanager.h"
#include "gtkfilechooser.h" /* for the GError types */

static void
Matthias Clasen's avatar
Matthias Clasen committed
34
_gtk_bookmark_free (gpointer data)
35
{
Matthias Clasen's avatar
Matthias Clasen committed
36 37
  GtkBookmark *bookmark = data;

38 39 40 41 42
  g_object_unref (bookmark->file);
  g_free (bookmark->label);
  g_slice_free (GtkBookmark, bookmark);
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56
static void
set_error_bookmark_doesnt_exist (GFile *file, GError **error)
{
  gchar *uri = g_file_get_uri (file);

  g_set_error (error,
               GTK_FILE_CHOOSER_ERROR,
               GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
               _("%s does not exist in the bookmarks list"),
               uri);

  g_free (uri);
}

57
static GFile *
58
get_legacy_bookmarks_file (void)
59 60 61 62 63 64 65 66 67 68 69
{
  GFile *file;
  gchar *filename;

  filename = g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
  file = g_file_new_for_path (filename);
  g_free (filename);

  return file;
}

70 71 72 73 74 75 76 77 78 79 80 81 82
static GFile *
get_bookmarks_file (void)
{
  GFile *file;
  gchar *filename;

  filename = g_build_filename (g_get_user_config_dir (), "gtk-3.0", "bookmarks", NULL);
  file = g_file_new_for_path (filename);
  g_free (filename);

  return file;
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static GSList *
read_bookmarks (GFile *file)
{
  gchar *contents;
  gchar **lines, *space;
  GSList *bookmarks = NULL;
  gint i;

  if (!g_file_load_contents (file, NULL, &contents,
			     NULL, NULL, NULL))
    return NULL;

  lines = g_strsplit (contents, "\n", -1);

  for (i = 0; lines[i]; i++)
    {
      GtkBookmark *bookmark;

      if (!*lines[i])
	continue;

      if (!g_utf8_validate (lines[i], -1, NULL))
	continue;

      bookmark = g_slice_new0 (GtkBookmark);

      if ((space = strchr (lines[i], ' ')) != NULL)
	{
	  space[0] = '\0';
	  bookmark->label = g_strdup (space + 1);
	}

      bookmark->file = g_file_new_for_uri (lines[i]);
      bookmarks = g_slist_prepend (bookmarks, bookmark);
    }

  bookmarks = g_slist_reverse (bookmarks);
  g_strfreev (lines);
  g_free (contents);

  return bookmarks;
}

static void
save_bookmarks (GFile  *bookmarks_file,
		GSList *bookmarks)
{
  GError *error = NULL;
  GString *contents;
  GSList *l;
133
  GFile *parent = NULL;
134 135 136 137 138 139 140 141 142 143 144 145 146 147

  contents = g_string_new ("");

  for (l = bookmarks; l; l = l->next)
    {
      GtkBookmark *bookmark = l->data;
      gchar *uri;

      uri = g_file_get_uri (bookmark->file);
      if (!uri)
	continue;

      g_string_append (contents, uri);

148
      if (bookmark->label && g_utf8_validate (bookmark->label, -1, NULL))
149 150 151 152 153 154
	g_string_append_printf (contents, " %s", bookmark->label);

      g_string_append_c (contents, '\n');
      g_free (uri);
    }

155 156 157 158 159 160 161 162
  parent = g_file_get_parent (bookmarks_file);
  if (!g_file_make_directory_with_parents (parent, NULL, &error))
    {
       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
	 g_clear_error (&error);
       else
	 goto out;
    }
163 164
  if (!g_file_replace_contents (bookmarks_file,
				contents->str,
165
				contents->len,
166 167
				NULL, FALSE, 0, NULL,
				NULL, &error))
168 169 170 171
    goto out;

 out:
  if (error)
172 173 174 175
    {
      g_critical ("%s", error->message);
      g_error_free (error);
    }
176
  g_clear_object (&parent);
177 178 179 180 181 182
  g_string_free (contents, TRUE);
}

static void
notify_changed (GtkBookmarksManager *manager)
{
183 184
  if (manager->changed_func)
    manager->changed_func (manager->changed_func_data);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}

static void
bookmarks_file_changed (GFileMonitor      *monitor,
			GFile             *file,
			GFile             *other_file,
			GFileMonitorEvent  event,
			gpointer           data)
{
  GtkBookmarksManager *manager = data;

  switch (event)
    {
    case G_FILE_MONITOR_EVENT_CHANGED:
    case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
    case G_FILE_MONITOR_EVENT_CREATED:
    case G_FILE_MONITOR_EVENT_DELETED:
Matthias Clasen's avatar
Matthias Clasen committed
202
      g_slist_free_full (manager->bookmarks, _gtk_bookmark_free);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
      manager->bookmarks = read_bookmarks (file);

      gdk_threads_enter ();
      notify_changed (manager);
      gdk_threads_leave ();
      break;

    default:
      /* ignore at the moment */
      break;
    }
}

GtkBookmarksManager *
_gtk_bookmarks_manager_new (GtkBookmarksChangedFunc changed_func, gpointer changed_func_data)
{
  GtkBookmarksManager *manager;
  GFile *bookmarks_file;
  GError *error;

  manager = g_new0 (GtkBookmarksManager, 1);

  manager->changed_func = changed_func;
  manager->changed_func_data = changed_func_data;

  bookmarks_file = get_bookmarks_file ();
  manager->bookmarks = read_bookmarks (bookmarks_file);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
230
  if (!manager->bookmarks)
231 232 233 234 235
    {
      GFile *legacy_bookmarks_file;

      /* Read the legacy one and write it to the new one */
      legacy_bookmarks_file = get_legacy_bookmarks_file ();
Federico Mena Quintero's avatar
Federico Mena Quintero committed
236
      manager->bookmarks = read_bookmarks (legacy_bookmarks_file);
237 238
      if (manager->bookmarks)
	save_bookmarks (bookmarks_file, manager->bookmarks);
239 240 241

      g_object_unref (legacy_bookmarks_file);
    }
242 243 244 245 246 247 248 249 250 251 252

  error = NULL;
  manager->bookmarks_monitor = g_file_monitor_file (bookmarks_file,
						    G_FILE_MONITOR_NONE,
						    NULL, &error);
  if (error)
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }
  else
253 254
    manager->bookmarks_monitor_changed_id = g_signal_connect (manager->bookmarks_monitor, "changed",
							      G_CALLBACK (bookmarks_file_changed), manager);
255 256 257 258 259 260 261 262 263 264 265 266

  g_object_unref (bookmarks_file);

  return manager;
}

void
_gtk_bookmarks_manager_free (GtkBookmarksManager *manager)
{
  g_return_if_fail (manager != NULL);

  if (manager->bookmarks_monitor)
267 268 269 270 271 272
    {
      g_file_monitor_cancel (manager->bookmarks_monitor);
      g_signal_handler_disconnect (manager->bookmarks_monitor, manager->bookmarks_monitor_changed_id);
      manager->bookmarks_monitor_changed_id = 0;
      g_object_unref (manager->bookmarks_monitor);
    }
273

Matthias Clasen's avatar
Matthias Clasen committed
274
  g_slist_free_full (manager->bookmarks, _gtk_bookmark_free);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

  g_free (manager);
}

GSList *
_gtk_bookmarks_manager_list_bookmarks (GtkBookmarksManager *manager)
{
  GSList *bookmarks, *files = NULL;

  g_return_val_if_fail (manager != NULL, NULL);

  bookmarks = manager->bookmarks;

  while (bookmarks)
    {
      GtkBookmark *bookmark;

      bookmark = bookmarks->data;
      bookmarks = bookmarks->next;

      files = g_slist_prepend (files, g_object_ref (bookmark->file));
    }

  return g_slist_reverse (files);
}

301
static GSList *
302
find_bookmark_link_for_file (GSList *bookmarks, GFile *file, int *position_ret)
303
{
304 305 306
  int pos;

  pos = 0;
307 308 309 310 311
  for (; bookmarks; bookmarks = bookmarks->next)
    {
      GtkBookmark *bookmark = bookmarks->data;

      if (g_file_equal (file, bookmark->file))
312 313 314 315 316 317 318 319
	{
	  if (position_ret)
	    *position_ret = pos;

	  return bookmarks;
	}

      pos++;
320 321
    }

322 323 324
  if (position_ret)
    *position_ret = -1;

325 326 327
  return NULL;
}

328 329 330 331 332 333 334 335 336 337
gboolean
_gtk_bookmarks_manager_has_bookmark (GtkBookmarksManager *manager,
                                     GFile               *file)
{
  GSList *link;

  link = find_bookmark_link_for_file (manager->bookmarks, file, NULL);
  return (link != NULL);
}

338 339 340 341 342 343
gboolean
_gtk_bookmarks_manager_insert_bookmark (GtkBookmarksManager *manager,
					GFile               *file,
					gint                 position,
					GError             **error)
{
344
  GSList *link;
345 346 347 348 349 350
  GtkBookmark *bookmark;
  GFile *bookmarks_file;

  g_return_val_if_fail (manager != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

351
  link = find_bookmark_link_for_file (manager->bookmarks, file, NULL);
352

353
  if (link)
354
    {
355
      gchar *uri;
356
      bookmark = link->data;
357
      uri = g_file_get_uri (bookmark->file);
358 359 360 361

      g_set_error (error,
		   GTK_FILE_CHOOSER_ERROR,
		   GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
362
		   _("%s already exists in the bookmarks list"),
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
		   uri);

      g_free (uri);

      return FALSE;
    }

  bookmark = g_slice_new0 (GtkBookmark);
  bookmark->file = g_object_ref (file);

  manager->bookmarks = g_slist_insert (manager->bookmarks, bookmark, position);

  bookmarks_file = get_bookmarks_file ();
  save_bookmarks (bookmarks_file, manager->bookmarks);
  g_object_unref (bookmarks_file);

  notify_changed (manager);

  return TRUE;
}

gboolean
_gtk_bookmarks_manager_remove_bookmark (GtkBookmarksManager *manager,
					GFile               *file,
					GError             **error)
{
389
  GSList *link;
390 391 392 393 394 395 396 397
  GFile *bookmarks_file;

  g_return_val_if_fail (manager != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (!manager->bookmarks)
    return FALSE;

398
  link = find_bookmark_link_for_file (manager->bookmarks, file, NULL);
399
  if (link)
400
    {
401
      GtkBookmark *bookmark = link->data;
402

403 404 405
      manager->bookmarks = g_slist_remove_link (manager->bookmarks, link);
      _gtk_bookmark_free (bookmark);
      g_slist_free_1 (link);
406
    }
407
  else
408
    {
409
      set_error_bookmark_doesnt_exist (file, error);
410 411 412 413 414 415 416 417 418 419 420 421
      return FALSE;
    }

  bookmarks_file = get_bookmarks_file ();
  save_bookmarks (bookmarks_file, manager->bookmarks);
  g_object_unref (bookmarks_file);

  notify_changed (manager);

  return TRUE;
}

422 423 424 425 426 427 428 429
gboolean
_gtk_bookmarks_manager_reorder_bookmark (GtkBookmarksManager *manager,
					 GFile               *file,
					 gint                 new_position,
					 GError             **error)
{
  GSList *link;
  GFile *bookmarks_file;
430
  int old_position;
431 432 433

  g_return_val_if_fail (manager != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
434
  g_return_val_if_fail (new_position >= 0, FALSE);
435 436 437 438

  if (!manager->bookmarks)
    return FALSE;

439 440 441 442
  link = find_bookmark_link_for_file (manager->bookmarks, file, &old_position);
  if (new_position == old_position)
    return TRUE;

443 444 445 446 447 448 449
  if (link)
    {
      GtkBookmark *bookmark = link->data; 

      manager->bookmarks = g_slist_remove_link (manager->bookmarks, link);
      g_slist_free_1 (link);

450 451 452
      if (new_position > old_position)
	new_position--;

453 454 455 456
      manager->bookmarks = g_slist_insert (manager->bookmarks, bookmark, new_position);
    }
  else
    {
457
      set_error_bookmark_doesnt_exist (file, error);
458 459 460 461 462 463 464 465 466 467 468 469
      return FALSE;
    }

  bookmarks_file = get_bookmarks_file ();
  save_bookmarks (bookmarks_file, manager->bookmarks);
  g_object_unref (bookmarks_file);

  notify_changed (manager);

  return TRUE;
}

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
gchar *
_gtk_bookmarks_manager_get_bookmark_label (GtkBookmarksManager *manager,
					   GFile               *file)
{
  GSList *bookmarks;
  gchar *label = NULL;

  g_return_val_if_fail (manager != NULL, NULL);
  g_return_val_if_fail (file != NULL, NULL);

  bookmarks = manager->bookmarks;

  while (bookmarks)
    {
      GtkBookmark *bookmark;

      bookmark = bookmarks->data;
      bookmarks = bookmarks->next;

      if (g_file_equal (file, bookmark->file))
	{
	  label = g_strdup (bookmark->label);
	  break;
	}
    }

  return label;
}

499
gboolean
500 501
_gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager,
					   GFile               *file,
502 503
					   const gchar         *label,
					   GError             **error)
504 505
{
  GFile *bookmarks_file;
506
  GSList *link;
507

508 509
  g_return_val_if_fail (manager != NULL, FALSE);
  g_return_val_if_fail (file != NULL, FALSE);
510

511
  link = find_bookmark_link_for_file (manager->bookmarks, file, NULL);
512 513 514
  if (link)
    {
      GtkBookmark *bookmark = link->data;
515

516 517 518 519
      g_free (bookmark->label);
      bookmark->label = g_strdup (label);
    }
  else
520
    {
521
      set_error_bookmark_doesnt_exist (file, error);
522
      return FALSE;
523 524 525 526 527 528
    }

  bookmarks_file = get_bookmarks_file ();
  save_bookmarks (bookmarks_file, manager->bookmarks);
  g_object_unref (bookmarks_file);

529 530 531
  notify_changed (manager);

  return TRUE;
532
}
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

gboolean
_gtk_bookmarks_manager_get_xdg_type (GtkBookmarksManager *manager,
                                     GFile               *file,
                                     GUserDirectory      *directory)
{
  GSList *link;
  gboolean match;
  GFile *location;
  const gchar *path;
  GUserDirectory dir;
  GtkBookmark *bookmark;

  link = find_bookmark_link_for_file (manager->bookmarks, file, NULL);
  if (!link)
    return FALSE;

  match = FALSE;
  bookmark = link->data;

  for (dir = 0; dir < G_USER_N_DIRECTORIES; dir++)
    {
      path = g_get_user_special_dir (dir);
      if (!path)
        continue;

      location = g_file_new_for_path (path);
      match = g_file_equal (location, bookmark->file);
      g_object_unref (location);

      if (match)
        break;
    }

  if (match && directory != NULL)
    *directory = dir;

  return match;
}

gboolean
_gtk_bookmarks_manager_get_is_builtin (GtkBookmarksManager *manager,
                                       GFile               *file)
{
  GUserDirectory xdg_type;

  /* if this is not an XDG dir, it's never builtin */
  if (!_gtk_bookmarks_manager_get_xdg_type (manager, file, &xdg_type))
    return FALSE;

  /* exclude XDG locations we don't display by default */
  return _gtk_bookmarks_manager_get_is_xdg_dir_builtin (xdg_type);
}

gboolean
_gtk_bookmarks_manager_get_is_xdg_dir_builtin (GUserDirectory xdg_type)
{
  return (xdg_type != G_USER_DIRECTORY_DESKTOP) &&
    (xdg_type != G_USER_DIRECTORY_TEMPLATES) &&
    (xdg_type != G_USER_DIRECTORY_PUBLIC_SHARE);
}