2011-09-29 14:24:57 +00:00
|
|
|
/* -*- 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
|
2015-11-19 12:11:05 +00:00
|
|
|
* published by the Free Software Foundation; either version 2.1 of the
|
2011-09-29 14:24:57 +00:00
|
|
|
* 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
|
2015-11-19 12:11:05 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2011-09-29 14:24:57 +00:00
|
|
|
*
|
2015-11-19 12:11:05 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-01-12 12:56:49 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2011-09-29 14:24:57 +00:00
|
|
|
*
|
|
|
|
* Authors: Federico Mena Quintero <federico@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib/gi18n-lib.h>
|
|
|
|
|
2019-09-10 11:11:03 +00:00
|
|
|
#include "gtkbookmarksmanagerprivate.h"
|
2011-09-29 14:24:57 +00:00
|
|
|
#include "gtkfilechooser.h" /* for the GError types */
|
|
|
|
|
|
|
|
static void
|
2015-07-31 21:57:04 +00:00
|
|
|
_gtk_bookmark_free (gpointer data)
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
2015-07-31 21:57:04 +00:00
|
|
|
GtkBookmark *bookmark = data;
|
|
|
|
|
2011-09-29 14:24:57 +00:00
|
|
|
g_object_unref (bookmark->file);
|
|
|
|
g_free (bookmark->label);
|
|
|
|
g_slice_free (GtkBookmark, bookmark);
|
|
|
|
}
|
|
|
|
|
2013-05-28 08:12:27 +00:00
|
|
|
static void
|
|
|
|
set_error_bookmark_doesnt_exist (GFile *file, GError **error)
|
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char *uri = g_file_get_uri (file);
|
2013-05-28 08:12:27 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-29 14:24:57 +00:00
|
|
|
static GFile *
|
2012-09-04 01:52:27 +00:00
|
|
|
get_legacy_bookmarks_file (void)
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
|
|
|
GFile *file;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *filename;
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
filename = g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
|
|
|
|
file = g_file_new_for_path (filename);
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2012-09-04 01:52:27 +00:00
|
|
|
static GFile *
|
|
|
|
get_bookmarks_file (void)
|
|
|
|
{
|
|
|
|
GFile *file;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *filename;
|
2012-09-04 01:52:27 +00:00
|
|
|
|
2018-02-15 12:29:38 +00:00
|
|
|
/* Use gtk-3.0's bookmarks file as the format didn't change.
|
|
|
|
* Add the 3.0 file format to get_legacy_bookmarks_file() when
|
|
|
|
* the format does change.
|
|
|
|
*/
|
|
|
|
filename = g_build_filename (g_get_user_config_dir (), "gtk-3.0", "bookmarks", NULL);
|
2012-09-04 01:52:27 +00:00
|
|
|
file = g_file_new_for_path (filename);
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2011-09-29 14:24:57 +00:00
|
|
|
static GSList *
|
2020-02-04 16:54:30 +00:00
|
|
|
parse_bookmarks (const char *contents)
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char **lines, *space;
|
2011-09-29 14:24:57 +00:00
|
|
|
GSList *bookmarks = NULL;
|
2020-07-24 13:54:49 +00:00
|
|
|
int i;
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
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);
|
2020-02-04 16:54:30 +00:00
|
|
|
|
|
|
|
return bookmarks;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSList *
|
|
|
|
read_bookmarks (GFile *file)
|
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char *contents;
|
2020-02-04 16:54:30 +00:00
|
|
|
GSList *bookmarks = NULL;
|
|
|
|
|
|
|
|
if (!g_file_load_contents (file, NULL, &contents,
|
|
|
|
NULL, NULL, NULL))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bookmarks = parse_bookmarks (contents);
|
|
|
|
|
2011-09-29 14:24:57 +00:00
|
|
|
g_free (contents);
|
|
|
|
|
|
|
|
return bookmarks;
|
|
|
|
}
|
|
|
|
|
2020-02-04 16:54:30 +00:00
|
|
|
static void
|
|
|
|
notify_changed (GtkBookmarksManager *manager)
|
|
|
|
{
|
|
|
|
if (manager->changed_func)
|
|
|
|
manager->changed_func (manager->changed_func_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_bookmarks_finish (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GFile *file = G_FILE (source);
|
|
|
|
GtkBookmarksManager *manager = data;
|
|
|
|
char *contents = NULL;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (!g_file_load_contents_finish (file, result, &contents, NULL, NULL, &error))
|
|
|
|
{
|
|
|
|
g_warning ("Failed to load '%s': %s", g_file_peek_path (file), error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_slist_free_full (manager->bookmarks, _gtk_bookmark_free);
|
|
|
|
manager->bookmarks = parse_bookmarks (contents);
|
|
|
|
|
|
|
|
g_free (contents);
|
|
|
|
|
|
|
|
notify_changed (manager);
|
|
|
|
}
|
|
|
|
|
2011-09-29 14:24:57 +00:00
|
|
|
static void
|
|
|
|
save_bookmarks (GFile *bookmarks_file,
|
|
|
|
GSList *bookmarks)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GString *contents;
|
|
|
|
GSList *l;
|
2013-05-09 20:48:03 +00:00
|
|
|
GFile *parent = NULL;
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
contents = g_string_new ("");
|
|
|
|
|
|
|
|
for (l = bookmarks; l; l = l->next)
|
|
|
|
{
|
|
|
|
GtkBookmark *bookmark = l->data;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *uri;
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
uri = g_file_get_uri (bookmark->file);
|
|
|
|
if (!uri)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_string_append (contents, uri);
|
|
|
|
|
2015-09-25 11:57:24 +00:00
|
|
|
if (bookmark->label && g_utf8_validate (bookmark->label, -1, NULL))
|
2011-09-29 14:24:57 +00:00
|
|
|
g_string_append_printf (contents, " %s", bookmark->label);
|
|
|
|
|
|
|
|
g_string_append_c (contents, '\n');
|
|
|
|
g_free (uri);
|
|
|
|
}
|
|
|
|
|
2013-05-09 20:48:03 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-09-29 14:24:57 +00:00
|
|
|
if (!g_file_replace_contents (bookmarks_file,
|
|
|
|
contents->str,
|
2014-06-28 05:51:24 +00:00
|
|
|
contents->len,
|
2011-09-29 14:24:57 +00:00
|
|
|
NULL, FALSE, 0, NULL,
|
|
|
|
NULL, &error))
|
2013-05-09 20:48:03 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (error)
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
|
|
|
g_critical ("%s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
2013-05-09 20:48:03 +00:00
|
|
|
g_clear_object (&parent);
|
2011-09-29 14:24:57 +00:00
|
|
|
g_string_free (contents, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2020-02-04 16:54:30 +00:00
|
|
|
g_file_load_contents_async (file, NULL, read_bookmarks_finish, manager);
|
2011-09-29 14:24:57 +00:00
|
|
|
break;
|
|
|
|
|
2017-10-06 19:19:42 +00:00
|
|
|
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
|
|
|
|
case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
|
|
|
|
case G_FILE_MONITOR_EVENT_UNMOUNTED:
|
|
|
|
case G_FILE_MONITOR_EVENT_MOVED:
|
|
|
|
case G_FILE_MONITOR_EVENT_RENAMED:
|
|
|
|
case G_FILE_MONITOR_EVENT_MOVED_IN:
|
|
|
|
case G_FILE_MONITOR_EVENT_MOVED_OUT:
|
2011-09-29 14:24:57 +00:00
|
|
|
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 ();
|
2020-02-04 16:54:30 +00:00
|
|
|
if (!g_file_query_exists (bookmarks_file, NULL))
|
2012-09-04 01:52:27 +00:00
|
|
|
{
|
|
|
|
GFile *legacy_bookmarks_file;
|
|
|
|
|
|
|
|
/* Read the legacy one and write it to the new one */
|
|
|
|
legacy_bookmarks_file = get_legacy_bookmarks_file ();
|
2012-09-04 02:03:12 +00:00
|
|
|
manager->bookmarks = read_bookmarks (legacy_bookmarks_file);
|
2013-05-09 20:48:38 +00:00
|
|
|
if (manager->bookmarks)
|
|
|
|
save_bookmarks (bookmarks_file, manager->bookmarks);
|
2012-09-04 01:52:27 +00:00
|
|
|
|
|
|
|
g_object_unref (legacy_bookmarks_file);
|
|
|
|
}
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
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
|
2011-09-30 14:43:59 +00:00
|
|
|
manager->bookmarks_monitor_changed_id = g_signal_connect (manager->bookmarks_monitor, "changed",
|
|
|
|
G_CALLBACK (bookmarks_file_changed), manager);
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
g_object_unref (bookmarks_file);
|
|
|
|
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_bookmarks_manager_free (GtkBookmarksManager *manager)
|
|
|
|
{
|
|
|
|
g_return_if_fail (manager != NULL);
|
|
|
|
|
|
|
|
if (manager->bookmarks_monitor)
|
2011-09-30 14:43:59 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2011-09-29 14:24:57 +00:00
|
|
|
|
2015-07-31 21:57:04 +00:00
|
|
|
g_slist_free_full (manager->bookmarks, _gtk_bookmark_free);
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-01-21 22:12:48 +00:00
|
|
|
static GSList *
|
2012-10-20 19:09:34 +00:00
|
|
|
find_bookmark_link_for_file (GSList *bookmarks, GFile *file, int *position_ret)
|
2012-09-11 20:02:01 +00:00
|
|
|
{
|
2012-10-20 19:09:34 +00:00
|
|
|
int pos;
|
|
|
|
|
|
|
|
pos = 0;
|
2012-09-11 20:02:01 +00:00
|
|
|
for (; bookmarks; bookmarks = bookmarks->next)
|
|
|
|
{
|
|
|
|
GtkBookmark *bookmark = bookmarks->data;
|
|
|
|
|
|
|
|
if (g_file_equal (file, bookmark->file))
|
2012-10-20 19:09:34 +00:00
|
|
|
{
|
|
|
|
if (position_ret)
|
|
|
|
*position_ret = pos;
|
|
|
|
|
|
|
|
return bookmarks;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos++;
|
2012-09-11 20:02:01 +00:00
|
|
|
}
|
|
|
|
|
2012-10-20 19:09:34 +00:00
|
|
|
if (position_ret)
|
|
|
|
*position_ret = -1;
|
|
|
|
|
2012-09-11 20:02:01 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-21 22:38:34 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-29 14:24:57 +00:00
|
|
|
gboolean
|
|
|
|
_gtk_bookmarks_manager_insert_bookmark (GtkBookmarksManager *manager,
|
|
|
|
GFile *file,
|
2020-07-24 13:54:49 +00:00
|
|
|
int position,
|
2011-09-29 14:24:57 +00:00
|
|
|
GError **error)
|
|
|
|
{
|
2012-09-11 20:02:01 +00:00
|
|
|
GSList *link;
|
2011-09-29 14:24:57 +00:00
|
|
|
GtkBookmark *bookmark;
|
|
|
|
GFile *bookmarks_file;
|
|
|
|
|
|
|
|
g_return_val_if_fail (manager != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
2012-10-20 19:09:34 +00:00
|
|
|
link = find_bookmark_link_for_file (manager->bookmarks, file, NULL);
|
2011-09-29 14:24:57 +00:00
|
|
|
|
2012-09-11 20:02:01 +00:00
|
|
|
if (link)
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char *uri;
|
2012-09-11 20:02:01 +00:00
|
|
|
bookmark = link->data;
|
2013-05-28 05:36:54 +00:00
|
|
|
uri = g_file_get_uri (bookmark->file);
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
g_set_error (error,
|
|
|
|
GTK_FILE_CHOOSER_ERROR,
|
|
|
|
GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
|
2013-05-28 08:12:27 +00:00
|
|
|
_("%s already exists in the bookmarks list"),
|
2011-09-29 14:24:57 +00:00
|
|
|
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)
|
|
|
|
{
|
2012-09-11 20:02:01 +00:00
|
|
|
GSList *link;
|
2011-09-29 14:24:57 +00:00
|
|
|
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;
|
|
|
|
|
2012-10-20 19:09:34 +00:00
|
|
|
link = find_bookmark_link_for_file (manager->bookmarks, file, NULL);
|
2012-09-11 20:02:01 +00:00
|
|
|
if (link)
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
2012-09-11 20:02:01 +00:00
|
|
|
GtkBookmark *bookmark = link->data;
|
2011-09-29 14:24:57 +00:00
|
|
|
|
2012-09-11 20:02:01 +00:00
|
|
|
manager->bookmarks = g_slist_remove_link (manager->bookmarks, link);
|
|
|
|
_gtk_bookmark_free (bookmark);
|
|
|
|
g_slist_free_1 (link);
|
2011-09-29 14:24:57 +00:00
|
|
|
}
|
2012-09-11 20:02:01 +00:00
|
|
|
else
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
2013-05-28 08:12:27 +00:00
|
|
|
set_error_bookmark_doesnt_exist (file, error);
|
2011-09-29 14:24:57 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmarks_file = get_bookmarks_file ();
|
|
|
|
save_bookmarks (bookmarks_file, manager->bookmarks);
|
|
|
|
g_object_unref (bookmarks_file);
|
|
|
|
|
|
|
|
notify_changed (manager);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-11 20:17:03 +00:00
|
|
|
gboolean
|
|
|
|
_gtk_bookmarks_manager_reorder_bookmark (GtkBookmarksManager *manager,
|
|
|
|
GFile *file,
|
2020-07-24 13:54:49 +00:00
|
|
|
int new_position,
|
2012-09-11 20:17:03 +00:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GSList *link;
|
|
|
|
GFile *bookmarks_file;
|
2012-10-20 19:09:34 +00:00
|
|
|
int old_position;
|
2012-09-11 20:17:03 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (manager != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
2012-10-20 19:09:34 +00:00
|
|
|
g_return_val_if_fail (new_position >= 0, FALSE);
|
2012-09-11 20:17:03 +00:00
|
|
|
|
|
|
|
if (!manager->bookmarks)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-10-20 19:09:34 +00:00
|
|
|
link = find_bookmark_link_for_file (manager->bookmarks, file, &old_position);
|
|
|
|
if (new_position == old_position)
|
|
|
|
return TRUE;
|
|
|
|
|
2012-09-11 20:17:03 +00:00
|
|
|
if (link)
|
|
|
|
{
|
|
|
|
GtkBookmark *bookmark = link->data;
|
|
|
|
|
|
|
|
manager->bookmarks = g_slist_remove_link (manager->bookmarks, link);
|
|
|
|
g_slist_free_1 (link);
|
|
|
|
|
2012-10-20 19:09:34 +00:00
|
|
|
if (new_position > old_position)
|
|
|
|
new_position--;
|
|
|
|
|
2012-09-11 20:17:03 +00:00
|
|
|
manager->bookmarks = g_slist_insert (manager->bookmarks, bookmark, new_position);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-28 08:12:27 +00:00
|
|
|
set_error_bookmark_doesnt_exist (file, error);
|
2012-09-11 20:17:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmarks_file = get_bookmarks_file ();
|
|
|
|
save_bookmarks (bookmarks_file, manager->bookmarks);
|
|
|
|
g_object_unref (bookmarks_file);
|
|
|
|
|
|
|
|
notify_changed (manager);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
char *
|
2011-09-29 14:24:57 +00:00
|
|
|
_gtk_bookmarks_manager_get_bookmark_label (GtkBookmarksManager *manager,
|
|
|
|
GFile *file)
|
|
|
|
{
|
|
|
|
GSList *bookmarks;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *label = NULL;
|
2011-09-29 14:24:57 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-09-11 20:02:01 +00:00
|
|
|
gboolean
|
2011-09-29 14:24:57 +00:00
|
|
|
_gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager,
|
|
|
|
GFile *file,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *label,
|
2012-09-11 20:02:01 +00:00
|
|
|
GError **error)
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
|
|
|
GFile *bookmarks_file;
|
2012-09-11 20:02:01 +00:00
|
|
|
GSList *link;
|
2011-09-29 14:24:57 +00:00
|
|
|
|
2012-09-11 20:02:01 +00:00
|
|
|
g_return_val_if_fail (manager != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (file != NULL, FALSE);
|
2011-09-29 14:24:57 +00:00
|
|
|
|
2012-10-20 19:09:34 +00:00
|
|
|
link = find_bookmark_link_for_file (manager->bookmarks, file, NULL);
|
2012-09-11 20:02:01 +00:00
|
|
|
if (link)
|
|
|
|
{
|
|
|
|
GtkBookmark *bookmark = link->data;
|
2011-09-29 14:24:57 +00:00
|
|
|
|
2012-09-11 20:02:01 +00:00
|
|
|
g_free (bookmark->label);
|
|
|
|
bookmark->label = g_strdup (label);
|
|
|
|
}
|
|
|
|
else
|
2011-09-29 14:24:57 +00:00
|
|
|
{
|
2013-05-28 08:12:27 +00:00
|
|
|
set_error_bookmark_doesnt_exist (file, error);
|
2012-09-11 20:02:01 +00:00
|
|
|
return FALSE;
|
2011-09-29 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bookmarks_file = get_bookmarks_file ();
|
|
|
|
save_bookmarks (bookmarks_file, manager->bookmarks);
|
|
|
|
g_object_unref (bookmarks_file);
|
|
|
|
|
2012-09-11 20:02:01 +00:00
|
|
|
notify_changed (manager);
|
|
|
|
|
|
|
|
return TRUE;
|
2011-09-29 14:24:57 +00:00
|
|
|
}
|
2013-01-21 22:32:02 +00:00
|
|
|
|
2019-09-10 11:08:42 +00:00
|
|
|
static gboolean
|
2013-01-21 22:32:02 +00:00
|
|
|
_gtk_bookmarks_manager_get_xdg_type (GtkBookmarksManager *manager,
|
|
|
|
GFile *file,
|
|
|
|
GUserDirectory *directory)
|
|
|
|
{
|
|
|
|
GSList *link;
|
|
|
|
gboolean match;
|
|
|
|
GFile *location;
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *path;
|
2013-01-21 22:32:02 +00:00
|
|
|
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);
|
|
|
|
}
|