gedit-recent.c 7.82 KB
Newer Older
1
/*
2
 * gedit-recent.c
3 4 5
 * This file is part of gedit
 *
 * Copyright (C) 2005 - Paolo Maggi
6 7
 * Copyright (C) 2014 - Paolo Borelli
 * Copyright (C) 2014 - Jesse van den Kieboom
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

23 24
#include "gedit-recent.h"

25 26
#include <gtk/gtk.h>
#include <gedit/gedit-document.h>
27 28 29
#include <string.h>

#include "gedit-settings.h"
30 31 32 33

void
gedit_recent_add_document (GeditDocument *document)
{
34
	GtkSourceFile *file;
35
	GFile *location;
36 37
	GtkRecentManager *recent_manager;
	GtkRecentData recent_data;
38
	gchar *uri;
39
	static gchar *groups[2];
40 41 42

	g_return_if_fail (GEDIT_IS_DOCUMENT (document));

43 44 45
	file = gedit_document_get_file (document);
	location = gtk_source_file_get_location (file);

46
	if (location == NULL)
47
	{
48 49
		return;
	}
50

51
	recent_manager = gtk_recent_manager_get_default ();
52

53 54 55
	groups[0] = (gchar *) g_get_application_name ();
	groups[1] = NULL;

56 57 58 59 60 61 62
	recent_data.display_name = NULL;
	recent_data.description = NULL;
	recent_data.mime_type = gedit_document_get_mime_type (document);
	recent_data.app_name = (gchar *) g_get_application_name ();
	recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
	recent_data.groups = groups;
	recent_data.is_private = FALSE;
63

64
	uri = g_file_get_uri (location);
65

66 67 68
	if (!gtk_recent_manager_add_full (recent_manager, uri, &recent_data))
	{
		g_warning ("Failed to add uri '%s' to the recent manager.", uri);
69
	}
70 71 72 73

	g_free (uri);
	g_free (recent_data.app_exec);
	g_free (recent_data.mime_type);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
}

void
gedit_recent_remove_if_local (GFile *location)
{
	g_return_if_fail (G_IS_FILE (location));

	/* If a file is local chances are that if load/save fails the file has
	 * beed removed and the failure is permanent so we remove it from the
	 * list of recent files. For remote files the failure may be just
	 * transitory and we keep the file in the list.
	 */
	if (g_file_has_uri_scheme (location, "file"))
	{
		GtkRecentManager *recent_manager;
		gchar *uri;

91
		recent_manager = gtk_recent_manager_get_default ();
92 93 94 95 96 97 98

		uri = g_file_get_uri (location);
		gtk_recent_manager_remove_item (recent_manager, uri, NULL);
		g_free (uri);
	}
}

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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
static gint
sort_recent_items_mru (GtkRecentInfo *a,
                       GtkRecentInfo *b,
                       gpointer       unused)
{
	g_assert (a != NULL && b != NULL);
	return gtk_recent_info_get_modified (b) - gtk_recent_info_get_modified (a);
}

static void
populate_filter_info (GtkRecentInfo        *info,
                      GtkRecentFilterInfo  *filter_info,
                      GtkRecentFilterFlags  needed)
{
	filter_info->uri = gtk_recent_info_get_uri (info);
	filter_info->mime_type = gtk_recent_info_get_mime_type (info);

	filter_info->contains = GTK_RECENT_FILTER_URI | GTK_RECENT_FILTER_MIME_TYPE;

	if (needed & GTK_RECENT_FILTER_DISPLAY_NAME)
	{
		filter_info->display_name = gtk_recent_info_get_display_name (info);
		filter_info->contains |= GTK_RECENT_FILTER_DISPLAY_NAME;
	}
	else
	{
		filter_info->uri = NULL;
	}

	if (needed & GTK_RECENT_FILTER_APPLICATION)
	{
		filter_info->applications = (const gchar **) gtk_recent_info_get_applications (info, NULL);
		filter_info->contains |= GTK_RECENT_FILTER_APPLICATION;
	}
	else
	{
		filter_info->applications = NULL;
	}

	if (needed & GTK_RECENT_FILTER_GROUP)
	{
		filter_info->groups = (const gchar **) gtk_recent_info_get_groups (info, NULL);
		filter_info->contains |= GTK_RECENT_FILTER_GROUP;
	}
	else
	{
		filter_info->groups = NULL;
	}

	if (needed & GTK_RECENT_FILTER_AGE)
	{
		filter_info->age = gtk_recent_info_get_age (info);
		filter_info->contains |= GTK_RECENT_FILTER_AGE;
	}
	else
	{
		filter_info->age = -1;
	}
}

159
/* The GeditRecentConfiguration struct is allocated and owned by the caller */
160 161 162 163 164 165 166 167 168 169 170 171 172 173
void
gedit_recent_configuration_init_default (GeditRecentConfiguration *config)
{
	GSettings *settings;

	config->manager = gtk_recent_manager_get_default ();

	if (config->filter != NULL)
	{
		g_object_unref (config->filter);
	}

	config->filter = gtk_recent_filter_new ();
	gtk_recent_filter_add_application (config->filter, g_get_application_name ());
174
	gtk_recent_filter_add_mime_type (config->filter, "text/plain");
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	g_object_ref_sink (config->filter);

	settings = g_settings_new ("org.gnome.gedit.preferences.ui");

	g_settings_get (settings,
	                GEDIT_SETTINGS_MAX_RECENTS,
	                "u",
	                &config->limit);

	g_object_unref (settings);

	config->show_not_found = TRUE;
	config->show_private = FALSE;
	config->local_only = FALSE;

	config->substring_filter = NULL;
}

193
/* The GeditRecentConfiguration struct is owned and destroyed by the caller */
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
void
gedit_recent_configuration_destroy (GeditRecentConfiguration *config)
{
	g_clear_object (&config->filter);
	config->manager = NULL;

	g_clear_pointer (&config->substring_filter, (GDestroyNotify)g_free);
}

GList *
gedit_recent_get_items (GeditRecentConfiguration *config)
{
	GtkRecentFilterFlags needed;
	GList *items;
	GList *retitems = NULL;
	gint length;
210
	char *substring_filter = NULL;
211 212 213 214 215 216 217 218 219 220 221 222 223 224

	if (config->limit == 0)
	{
		return NULL;
	}

	items = gtk_recent_manager_get_items (config->manager);

	if (!items)
	{
		return NULL;
	}

	needed = gtk_recent_filter_get_needed (config->filter);
225 226
	if (config->substring_filter && *config->substring_filter != '\0')
	{
227 228 229 230 231
		gchar *filter_normalized;

		filter_normalized = g_utf8_normalize (config->substring_filter, -1, G_NORMALIZE_ALL);
		substring_filter = g_utf8_casefold (filter_normalized, -1);
		g_free (filter_normalized);
232
	}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

	while (items)
	{
		GtkRecentInfo *info;
		GtkRecentFilterInfo filter_info;
		gboolean is_filtered;

		info = items->data;
		is_filtered = FALSE;

		if (config->local_only && !gtk_recent_info_is_local (info))
		{
			is_filtered = TRUE;
		}
		else if (!config->show_private && gtk_recent_info_get_private_hint (info))
		{
			is_filtered = TRUE;
		}
		else if (!config->show_not_found && !gtk_recent_info_exists (info))
		{
			is_filtered = TRUE;
		}
		else
		{
257
			if (substring_filter)
258
			{
259 260
				gchar *uri_normalized;
				gchar *uri_casefolded;
261

262 263 264
				uri_normalized = g_utf8_normalize (gtk_recent_info_get_uri_display (info), -1, G_NORMALIZE_ALL);
				uri_casefolded = g_utf8_casefold (uri_normalized, -1);
				g_free (uri_normalized);
265

266
				if (strstr (uri_casefolded, substring_filter) == NULL)
267 268 269 270
				{
					is_filtered = TRUE;
				}

271
				g_free (uri_casefolded);
272 273 274 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 301 302 303
			}

			if (!is_filtered)
			{
				populate_filter_info (info, &filter_info, needed);
				is_filtered = !gtk_recent_filter_filter (config->filter, &filter_info);

				/* these we own */
				if (filter_info.applications)
				{
					g_strfreev ((gchar **) filter_info.applications);
				}

				if (filter_info.groups)
				{
					g_strfreev ((gchar **) filter_info.groups);
				}
			}
		}

		if (!is_filtered)
		{
			retitems = g_list_prepend (retitems, info);
		}
		else
		{
			gtk_recent_info_unref (info);
		}

		items = g_list_delete_link (items, items);
	}

304 305
	g_free (substring_filter);

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	if (!retitems)
	{
		return NULL;
	}

	retitems = g_list_sort_with_data (retitems, (GCompareDataFunc) sort_recent_items_mru, NULL);
	length = g_list_length (retitems);

	if ((config->limit != -1) && (length > config->limit))
	{
		GList *clamp, *l;

		clamp = g_list_nth (retitems, config->limit - 1);

		if (!clamp)
		{
			return retitems;
		}

		l = clamp->next;
		clamp->next = NULL;

		g_list_free_full (l, (GDestroyNotify) gtk_recent_info_unref);
	}

	return retitems;
}

334
/* ex:set ts=8 noet: */