bookmark-agent.c 29.8 KB
Newer Older
Jim Krehl's avatar
Jim Krehl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
 * This file is part of the Main Menu.
 *
 * Copyright (c) 2007 Novell, Inc.
 *
 * The Main Menu 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.
 *
 * The Main Menu 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
 * the Main Menu; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "bookmark-agent.h"

#ifdef HAVE_CONFIG_H
#	include <config.h>
#else
#	define PACKAGE "gnome-main-menu"
#endif

#if ! GLIB_CHECK_VERSION (2, 12, 0)
#	include "bookmark-agent-libslab.h"
#endif

#include <gtk/gtkversion.h>
#if GTK_CHECK_VERSION (2, 10, 0)
#	define USE_GTK_RECENT_MANAGER
#	include <gtk/gtkrecentmanager.h>
#else
#	define EGG_ENABLE_RECENT_FILES
#	include "egg-recent-model.h"
#endif

#include <string.h>
#include <stdlib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <libgnomevfs/gnome-vfs.h>

#include "libslab-utils.h"

#define MODIFIABLE_APPS_GCONF_KEY "/desktop/gnome/applications/main-menu/lock-down/user_modifiable_apps"
#define MODIFIABLE_DOCS_GCONF_KEY "/desktop/gnome/applications/main-menu/lock-down/user_modifiable_docs"
#define MODIFIABLE_DIRS_GCONF_KEY "/desktop/gnome/applications/main-menu/lock-down/user_modifiable_dirs"
#define MODIFIABLE_SYS_GCONF_KEY  "/desktop/gnome/applications/main-menu/lock-down/user_modifiable_system_area"

#define USER_APPS_STORE_FILE_NAME "applications.xbel"
#define USER_DOCS_STORE_FILE_NAME "documents.xbel"
#define USER_DIRS_STORE_FILE_NAME "places.xbel"
#define SYSTEM_STORE_FILE_NAME    "system-items.xbel"
Jim Krehl's avatar
Jim Krehl committed
59
#define CALC_TEMPLATE_FILE_NAME   "empty.ods"
Jim Krehl's avatar
Jim Krehl committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 133 134 135 136 137 138 139 140

#define GTK_BOOKMARKS_FILE ".gtk-bookmarks"

#define TYPE_IS_RECENT(type) ((type) == BOOKMARK_STORE_RECENT_APPS || (type) == BOOKMARK_STORE_RECENT_DOCS)

typedef struct {
	BookmarkStoreType        type;

	BookmarkItem           **items;
	gint                     n_items;
	BookmarkStoreStatus      status;

	GBookmarkFile           *store;
	gboolean                 needs_sync;

	gchar                   *store_path;
	gchar                   *user_store_path;
	gboolean                 user_modifiable;
	gboolean                 reorderable;
	const gchar             *store_filename;
	const gchar             *lockdown_key;

	GnomeVFSMonitorHandle   *store_monitor;
	GnomeVFSMonitorHandle   *user_store_monitor;
	guint                    gconf_monitor;

	void                  (* update_path) (BookmarkAgent *);
	void                  (* load_store)  (BookmarkAgent *);
	void                  (* save_store)  (BookmarkAgent *);
	void                  (* create_item) (BookmarkAgent *, const gchar *);

	gchar                   *gtk_store_path;
	GnomeVFSMonitorHandle   *gtk_store_monitor;
} BookmarkAgentPrivate;

#define PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BOOKMARK_AGENT_TYPE, BookmarkAgentPrivate))

enum {
	PROP_0,
	PROP_ITEMS,
	PROP_STATUS
};

static BookmarkAgent *instances [BOOKMARK_STORE_N_TYPES];

static BookmarkAgentClass *bookmark_agent_parent_class = NULL;

static void           bookmark_agent_base_init  (BookmarkAgentClass *);
static void           bookmark_agent_class_init (BookmarkAgentClass *);
static void           bookmark_agent_init       (BookmarkAgent      *);
static BookmarkAgent *bookmark_agent_new        (BookmarkStoreType   );

static void get_property (GObject *, guint, GValue *, GParamSpec *);
static void set_property (GObject *, guint, const GValue *, GParamSpec *);
static void finalize     (GObject *);

static void update_agent (BookmarkAgent *);
static void update_items (BookmarkAgent *);
static void save_store   (BookmarkAgent *);
static gint get_rank     (BookmarkAgent *, const gchar *);
static void set_rank     (BookmarkAgent *, const gchar *, gint);

static void load_xbel_store          (BookmarkAgent *);
static void load_places_store        (BookmarkAgent *);
static void load_recent_store        (BookmarkAgent *);
static void update_user_spec_path    (BookmarkAgent *);
static void update_recent_store_path (BookmarkAgent *);
static void save_xbel_store          (BookmarkAgent *);
static void create_app_item          (BookmarkAgent *, const gchar *);
static void create_doc_item          (BookmarkAgent *, const gchar *);
static void create_dir_item          (BookmarkAgent *, const gchar *);

static void store_monitor_cb (GnomeVFSMonitorHandle *, const gchar *, const gchar *,
                              GnomeVFSMonitorEventType, gpointer);
static void gconf_notify_cb  (GConfClient *, guint, GConfEntry *, gpointer);
static void weak_destroy_cb  (gpointer, GObject *);

#ifdef USE_GTK_RECENT_MANAGER
static gint recent_item_mru_comp_func (gconstpointer a, gconstpointer b);
#endif

Jim Krehl's avatar
Jim Krehl committed
141 142 143
static gchar *find_package_data_file (const gchar *filename);


Jim Krehl's avatar
Jim Krehl committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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 230 231 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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 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 304 305 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
GType
bookmark_agent_get_type ()
{
	static GType g_define_type_id = 0;

	if (G_UNLIKELY (g_define_type_id == 0)) {
		static const GTypeInfo info = {
			sizeof (BookmarkAgentClass),
			(GBaseInitFunc) bookmark_agent_base_init,
			NULL,
			(GClassInitFunc) bookmark_agent_class_init,
			NULL, NULL,
			sizeof (BookmarkAgent), 0,
			(GInstanceInitFunc) bookmark_agent_init,
			NULL
		};

		g_define_type_id = g_type_register_static (
			G_TYPE_OBJECT, "BookmarkAgent", & info, 0);
	}

	return g_define_type_id;
}

BookmarkAgent *
bookmark_agent_get_instance (BookmarkStoreType type)
{
	g_return_val_if_fail (0 <= type, NULL);
	g_return_val_if_fail (type < BOOKMARK_STORE_N_TYPES, NULL);

	if (! instances [type]) {
		instances [type] = bookmark_agent_new (type);
		g_object_weak_ref (G_OBJECT (instances [type]), weak_destroy_cb, GINT_TO_POINTER (type));
	}
	else
		g_object_ref (G_OBJECT (instances [type]));

	return instances [type];
}

gboolean
bookmark_agent_has_item (BookmarkAgent *this, const gchar *uri)
{
	return g_bookmark_file_has_item (PRIVATE (this)->store, uri);
}

void
bookmark_agent_add_item (BookmarkAgent *this, const BookmarkItem *item)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	if (! item)
		return;

	g_return_if_fail (priv->user_modifiable);
	g_return_if_fail (item->uri);
	g_return_if_fail (item->mime_type);

	g_bookmark_file_set_mime_type (priv->store, item->uri, item->mime_type);

	if (item->mtime)
		g_bookmark_file_set_modified (priv->store, item->uri, item->mtime);

	if (item->title)
		g_bookmark_file_set_title (priv->store, item->uri, item->title);

	g_bookmark_file_add_application (priv->store, item->uri, item->app_name, item->app_exec);

	set_rank (this, item->uri, g_bookmark_file_get_size (priv->store) - 1);

	save_store (this);
}

void
bookmark_agent_move_item (BookmarkAgent *this, const gchar *uri, const gchar *uri_new)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

#ifdef USE_GTK_RECENT_MANAGER
	GError *error = NULL;
#else
	EggRecentModel *model;
#endif


	if (! TYPE_IS_RECENT (priv->type))
		return;

#ifdef USE_GTK_RECENT_MANAGER
	gtk_recent_manager_move_item (
		gtk_recent_manager_get_default (), uri, uri_new, & error);

	if (error)
		libslab_handle_g_error (
			& error, "%s: unable to update %s with renamed file, [%s] -> [%s].",
			G_STRFUNC, priv->store_path, uri, uri_new);
#else
	model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_NONE);

	egg_recent_model_delete (model, uri);
	egg_recent_model_add    (model, uri_new);

	g_object_unref (model);
#endif
}

void
bookmark_agent_remove_item (BookmarkAgent *this, const gchar *uri)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gint rank;

#ifdef USE_GTK_RECENT_MANAGER
	GError *error = NULL;
#else
	EggRecentModel *model;
#endif

	gchar **uris = NULL;
	gint    rank_i;
	gint    i;


	g_return_if_fail (priv->user_modifiable);

	if (! bookmark_agent_has_item (this, uri))
		return;

	if (TYPE_IS_RECENT (priv->type)) {
#ifdef USE_GTK_RECENT_MANAGER
		gtk_recent_manager_remove_item (
			gtk_recent_manager_get_default (), uri, & error);

		if (error)
			libslab_handle_g_error (
				& error, "%s: unable to remove [%s] from %s.",
				G_STRFUNC, priv->store_path, uri);
#else
		model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_NONE);
		egg_recent_model_delete (model, uri);
		g_object_unref (model);
#endif
	}
	else {
		rank = get_rank (this, uri);

		g_bookmark_file_remove_item (priv->store, uri, NULL);

		if (rank >= 0) {
			uris = g_bookmark_file_get_uris (priv->store, NULL);
				 
			for (i =  0; uris && uris [i]; ++i) {
				rank_i = get_rank (this, uris [i]);

				if (rank_i > rank)
					set_rank (this, uris [i], rank_i - 1);
			}
		}

		save_store (this);
	}
}

void
bookmark_agent_reorder_items (BookmarkAgent *this, const gchar **uris)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gint i;


	g_return_if_fail (priv->reorderable);

	for (i = 0; uris && uris [i]; ++i)
		set_rank (this, uris [i], i);

	save_store (this);
}

void
bookmark_item_free (BookmarkItem *item)
{
	if (! item)
		return;

	g_free (item->uri);
	g_free (item->title);
	g_free (item->mime_type);
	g_free (item->icon);
	g_free (item->app_name);
	g_free (item->app_exec);
	g_free (item);
}

static void
bookmark_agent_base_init (BookmarkAgentClass *this_class)
{
	gint i;

	for (i = 0; i < BOOKMARK_STORE_N_TYPES; ++i)
		instances [i] = NULL;
}

static void
bookmark_agent_class_init (BookmarkAgentClass *this_class)
{
	GObjectClass *g_obj_class = G_OBJECT_CLASS (this_class);

	GParamSpec *items_pspec;
	GParamSpec *status_pspec;


	g_obj_class->get_property = get_property;
	g_obj_class->set_property = set_property;
	g_obj_class->finalize     = finalize;

	items_pspec = g_param_spec_pointer (
		BOOKMARK_AGENT_ITEMS_PROP, BOOKMARK_AGENT_ITEMS_PROP,
		"the null-terminated list which contains the bookmark items in this store",
		G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);

	status_pspec = g_param_spec_int (
		BOOKMARK_AGENT_STORE_STATUS_PROP, BOOKMARK_AGENT_STORE_STATUS_PROP, "the status of the store",
		BOOKMARK_STORE_DEFAULT_ONLY, BOOKMARK_STORE_USER, BOOKMARK_STORE_DEFAULT,
		G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);

	g_object_class_install_property (g_obj_class, PROP_ITEMS,  items_pspec);
	g_object_class_install_property (g_obj_class, PROP_STATUS, status_pspec);

	g_type_class_add_private (this_class, sizeof (BookmarkAgentPrivate));

	bookmark_agent_parent_class = g_type_class_peek_parent (this_class);
}

static void
bookmark_agent_init (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	priv->type                = -1;

 	priv->items               = NULL;
 	priv->n_items             = 0;
	priv->status              = BOOKMARK_STORE_ABSENT;

	priv->store               = NULL;
	priv->needs_sync          = FALSE;

	priv->store_path          = NULL;
	priv->user_store_path     = NULL;
	priv->user_modifiable     = FALSE;
	priv->reorderable         = FALSE;
	priv->store_filename      = NULL;
	priv->lockdown_key        = NULL;

	priv->store_monitor       = NULL;
	priv->user_store_monitor  = NULL;
	priv->gconf_monitor       = 0;

	priv->update_path         = NULL;
	priv->load_store          = NULL;
	priv->save_store          = NULL;
	priv->create_item         = NULL;

	priv->gtk_store_path      = NULL;
	priv->gtk_store_monitor   = NULL;
}

static BookmarkAgent *
bookmark_agent_new (BookmarkStoreType type)
{
	BookmarkAgent        *this;
	BookmarkAgentPrivate *priv;

	this = g_object_new (BOOKMARK_AGENT_TYPE, NULL);
	priv = PRIVATE (this);

	priv->type  = type;
	priv->store = g_bookmark_file_new ();

	switch (type) {
		case BOOKMARK_STORE_USER_APPS:
			priv->lockdown_key   = MODIFIABLE_APPS_GCONF_KEY;
			priv->store_filename = USER_APPS_STORE_FILE_NAME;
			priv->create_item    = create_app_item;

			break;

		case BOOKMARK_STORE_USER_DOCS:
			priv->lockdown_key   = MODIFIABLE_DOCS_GCONF_KEY;
			priv->store_filename = USER_DOCS_STORE_FILE_NAME;
			priv->create_item    = create_doc_item;

			break;

		case BOOKMARK_STORE_USER_DIRS:
			priv->lockdown_key   = MODIFIABLE_DIRS_GCONF_KEY;
			priv->store_filename = USER_DIRS_STORE_FILE_NAME;
			priv->create_item    = create_dir_item;

			priv->user_modifiable = GPOINTER_TO_INT (libslab_get_gconf_value (priv->lockdown_key));
			priv->reorderable     = FALSE;

			priv->load_store = load_places_store;

			priv->gtk_store_path = g_build_filename (g_get_home_dir (), GTK_BOOKMARKS_FILE, NULL);
			gnome_vfs_monitor_add (
				& priv->gtk_store_monitor, priv->gtk_store_path,
				GNOME_VFS_MONITOR_FILE, store_monitor_cb, this);

			break;

		case BOOKMARK_STORE_RECENT_APPS:
		case BOOKMARK_STORE_RECENT_DOCS:
			priv->user_modifiable = TRUE;
			priv->reorderable     = FALSE;

#ifdef USE_GTK_RECENT_MANAGER
			priv->store_path = g_build_filename (g_get_home_dir (), ".recently-used.xbel", NULL);
#else
			priv->store_path = g_build_filename (g_get_home_dir (), ".recently-used", NULL);
#endif

			gnome_vfs_monitor_add (
				& priv->store_monitor, priv->store_path,
				GNOME_VFS_MONITOR_FILE, store_monitor_cb, this);

			priv->update_path = update_recent_store_path;
			priv->load_store  = load_recent_store;

			break;

		case BOOKMARK_STORE_SYSTEM:
			priv->lockdown_key   = MODIFIABLE_SYS_GCONF_KEY;
			priv->store_filename = SYSTEM_STORE_FILE_NAME;
			priv->create_item    = create_app_item;

			break;

		default:
			break;
	}

	if (
		type == BOOKMARK_STORE_USER_APPS || type == BOOKMARK_STORE_USER_DOCS ||
		type == BOOKMARK_STORE_USER_DIRS || type == BOOKMARK_STORE_SYSTEM)
	{
		priv->user_modifiable = GPOINTER_TO_INT (libslab_get_gconf_value (priv->lockdown_key));

		priv->user_store_path = g_build_filename (
			g_get_user_data_dir (), PACKAGE, priv->store_filename, NULL);

		priv->update_path = update_user_spec_path;

		priv->gconf_monitor = libslab_gconf_notify_add (
			priv->lockdown_key, gconf_notify_cb, this);
	}

	if (type == BOOKMARK_STORE_USER_APPS || type == BOOKMARK_STORE_USER_DOCS || type == BOOKMARK_STORE_SYSTEM) {
		priv->reorderable = TRUE;
		priv->load_store  = load_xbel_store;
		priv->save_store  = save_xbel_store;
	}

	update_agent (this);

	return this;
}

static void
get_property (GObject *g_obj, guint prop_id, GValue *value, GParamSpec *pspec)
{
	BookmarkAgent        *this = BOOKMARK_AGENT (g_obj);
	BookmarkAgentPrivate *priv = PRIVATE        (this);


	switch (prop_id) {
		case PROP_ITEMS:
			g_value_set_pointer (value, priv->items);
			break;

		case PROP_STATUS:
			g_value_set_int (value, priv->status);
			break;
	}
}

static void
set_property (GObject *g_obj, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	/* no writeable properties */
}

static void
finalize (GObject *g_obj)
{
	BookmarkAgentPrivate *priv = PRIVATE (g_obj);

	gint i;


	for (i = 0; priv->items && priv->items [i]; ++i)
		bookmark_item_free (priv->items [i]);

	g_free (priv->items);
	g_free (priv->store_path);
	g_free (priv->user_store_path);
	g_free (priv->gtk_store_path);

	if (priv->store_monitor)
		gnome_vfs_monitor_cancel (priv->store_monitor);

	if (priv->user_store_monitor)
		gnome_vfs_monitor_cancel (priv->user_store_monitor);

	if (priv->gtk_store_monitor)
		gnome_vfs_monitor_cancel (priv->gtk_store_monitor);

	libslab_gconf_notify_remove (priv->gconf_monitor);

	g_bookmark_file_free (priv->store);

	G_OBJECT_CLASS (bookmark_agent_parent_class)->finalize (g_obj);
}

static void
update_agent (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	priv->update_path (this);
	priv->load_store  (this);

	update_items (this);
}

static void
update_items (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gchar    **uris         = NULL;
	gchar    **uris_ordered = NULL;
	gsize      n_uris       = 0;
	gint       rank         = -1;
	gboolean   needs_update = FALSE;

	gint i;


	uris = g_bookmark_file_get_uris (priv->store, & n_uris);
	uris_ordered = g_new0 (gchar *, n_uris + 1);
	uris_ordered [n_uris] = NULL;

	for (i = 0; uris && uris [i]; ++i) {
		rank = get_rank (this, uris [i]);

		if (rank < 0 || rank >= n_uris)
			rank = i;

		if (uris_ordered [rank])
			g_warning (
				"store corruption - multiple uris with same rank: [%s] rank = %d, [%s] [%s]",
				priv->store_path, rank, uris_ordered [rank], uris [i]);

		uris_ordered [rank] = uris [i];
	}

	if (priv->n_items != n_uris)
		needs_update = TRUE;

	for (i = 0; ! needs_update && uris_ordered && uris_ordered [i]; ++i)
		if (strcmp (priv->items [i]->uri, uris_ordered [i]))
			needs_update = TRUE;

	if (needs_update) {
		for (i = 0; priv->items && priv->items [i]; ++i)
			bookmark_item_free (priv->items [i]);

		g_free (priv->items);

		priv->n_items = n_uris;
		priv->items = g_new0 (BookmarkItem *, priv->n_items + 1);

		for (i = 0; uris_ordered && uris_ordered [i]; ++i) {
			priv->items [i]            = g_new0 (BookmarkItem, 1);
			priv->items [i]->uri       = g_strdup (uris_ordered [i]);
			priv->items [i]->title     = g_bookmark_file_get_title     (priv->store, uris_ordered [i], NULL);
			priv->items [i]->mime_type = g_bookmark_file_get_mime_type (priv->store, uris_ordered [i], NULL);
			priv->items [i]->mtime     = g_bookmark_file_get_modified  (priv->store, uris_ordered [i], NULL);
			priv->items [i]->app_name  = NULL;
			priv->items [i]->app_exec  = NULL;

			g_bookmark_file_get_icon (priv->store, uris_ordered [i], & priv->items [i]->icon, NULL, NULL);
		}

		g_object_notify (G_OBJECT (this), BOOKMARK_AGENT_ITEMS_PROP);
	}

	g_strfreev (uris);
	g_free (uris_ordered);
}

static void
save_store (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gchar *dir;


	g_return_if_fail (priv->user_modifiable);

	priv->needs_sync = TRUE;
	priv->update_path (this);

	dir = g_path_get_dirname (priv->store_path);
	g_mkdir_with_parents (dir, 0700);
	g_free (dir);

	priv->save_store (this);
	update_items (this);
}

static gint
get_rank (BookmarkAgent *this, const gchar *uri)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gchar **groups;
	gint    rank;

	gint i;


	if (! priv->reorderable)
		return -1;

	groups = g_bookmark_file_get_groups (priv->store, uri, NULL, NULL);
	rank   = -1;

	for (i = 0; groups && groups [i]; ++i) {
		if (g_str_has_prefix (groups [i], "rank-")) {
			if (rank >= 0)
				g_warning (
					"store corruption - multiple ranks for same uri: [%s] [%s]",
					priv->store_path, uri);

			rank = atoi (& groups [i] [5]);
		}
	}

	g_strfreev (groups);

	return rank;
}

static void
set_rank (BookmarkAgent *this, const gchar *uri, gint rank)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gchar **groups;
	gchar  *group;

	gint i;


	if (! (priv->reorderable && bookmark_agent_has_item (this, uri)))
		return;

	groups = g_bookmark_file_get_groups (priv->store, uri, NULL, NULL);

	for (i = 0; groups && groups [i]; ++i)
		if (g_str_has_prefix (groups [i], "rank-"))
			g_bookmark_file_remove_group (priv->store, uri, groups [i], NULL);

	g_strfreev (groups);

	group = g_strdup_printf ("rank-%d", rank);
	g_bookmark_file_add_group (priv->store, uri, group);
	g_free (group);
}

static void
load_xbel_store (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gchar **uris = NULL;

	GError *error = NULL;

	gint i;


	if (! (priv->store_path && g_bookmark_file_load_from_file (priv->store, priv->store_path, & error))) {
		g_bookmark_file_free (priv->store);
		priv->store = g_bookmark_file_new ();

		libslab_handle_g_error (
			& error, "%s: couldn't load bookmark file [%s]\n",
			G_STRFUNC, priv->store_path);

		return;
	}

	uris = g_bookmark_file_get_uris (priv->store, NULL);

	for (i = 0; uris && uris [i]; ++i)
		priv->create_item (this, uris [i]);
}

static void
load_places_store (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gchar **uris;
	gchar **groups;
	gchar **folders = NULL;

	gchar  *buf;

	gint i, j;


	load_xbel_store (this);

	uris = g_bookmark_file_get_uris (priv->store, NULL);

	for (i = 0; uris && uris [i]; ++i) {
		groups = g_bookmark_file_get_groups (priv->store, uris [i], NULL, NULL);

		for (j = 0; groups && groups [j]; ++j) {
			if (! strcmp (groups [j], "gtk-bookmarks")) {
				g_bookmark_file_remove_item (priv->store, uris [i], NULL);

				break;
			}
		}

		g_strfreev (groups);
	}

	g_strfreev (uris);

	g_file_get_contents (priv->gtk_store_path, & buf, NULL, NULL);

	if (buf) {
		folders = g_strsplit (buf, "\n", -1);
		g_free (buf);
	}

	for (i = 0; folders && folders [i]; ++i) {
		if (strlen (folders [i]) > 0) {
			g_bookmark_file_add_group (priv->store, folders [i], "gtk-bookmarks");
			priv->create_item (this, folders [i]);
		}
	}

	g_strfreev (folders);
}

static void
load_recent_store (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

#ifdef USE_GTK_RECENT_MANAGER
	GtkRecentInfo *info;
	gboolean       include;
#else
	EggRecentModel *model;
	EggRecentItem  *info;
#endif

	GList *items;
	GList *items_ordered = NULL;

	BookmarkItem *item;

	GList *node;


#ifdef USE_GTK_RECENT_MANAGER
	items = gtk_recent_manager_get_items (gtk_recent_manager_get_default ());
#else
	model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);

	if (priv->type == BOOKMARK_STORE_RECENT_APPS)
		egg_recent_model_set_filter_groups (model, "recently-used-apps", NULL);
	else
		egg_recent_model_set_filter_groups (model, NULL);

	items = egg_recent_model_get_list (model);
#endif

	for (node = items; node; node = node->next) {
#ifdef USE_GTK_RECENT_MANAGER
		info = (GtkRecentInfo *) node->data;

		if (priv->type == BOOKMARK_STORE_RECENT_APPS)
			include = gtk_recent_info_has_group (info, "recently-used-apps");
		else
			include = ! gtk_recent_info_get_private_hint (info);

		if (include) {
			item = g_new0 (BookmarkItem, 1);

			item->uri       = g_strdup (gtk_recent_info_get_uri       (info));
			item->mime_type = g_strdup (gtk_recent_info_get_mime_type (info));
			item->mtime     = gtk_recent_info_get_modified            (info);

			items_ordered = g_list_insert_sorted (items_ordered, item, recent_item_mru_comp_func);
		}

		gtk_recent_info_unref (info);
#else
		info = (EggRecentItem *) node->data;

		item = g_new0 (BookmarkItem, 1);

		item->uri       = egg_recent_item_get_uri       (info);
		item->mime_type = egg_recent_item_get_mime_type (info);
		item->mtime     = egg_recent_item_get_timestamp (info);

		items_ordered = g_list_append (items_ordered, item);

		egg_recent_item_unref (info);
#endif
	}

	g_list_free (items);

#ifndef USE_GTK_RECENT_MANAGER
	g_object_unref (model);
#endif

	g_bookmark_file_free (priv->store);
	priv->store = g_bookmark_file_new ();

	for (node = items_ordered; node; node = node->next) {
		item = (BookmarkItem *) node->data;

		g_bookmark_file_set_mime_type (priv->store, item->uri, item->mime_type);
		g_bookmark_file_set_modified  (priv->store, item->uri, item->mtime);

		bookmark_item_free (item);
	}
}

Jim Krehl's avatar
Jim Krehl committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
static gchar *
find_package_data_file (const gchar *filename)
{
	const gchar * const *dirs = NULL;
	gchar    *path = NULL;
	gint                 i;

	dirs = g_get_system_data_dirs ();
	
	for (i = 0; ! path && dirs && dirs [i]; ++i) {
		path = g_build_filename (dirs [i], PACKAGE, filename, NULL);
		
		if (! g_file_test (path, G_FILE_TEST_EXISTS)) {
			g_free (path);
			path = NULL;
		}
	}

	return path;
}

Jim Krehl's avatar
Jim Krehl committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
static void
update_user_spec_path (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gboolean  use_user_path;
	gchar    *path = NULL;

	BookmarkStoreStatus status;

	use_user_path = priv->user_modifiable &&
		(priv->needs_sync || g_file_test (priv->user_store_path, G_FILE_TEST_EXISTS));

	if (use_user_path)
		path = g_strdup (priv->user_store_path);
	else {
Jim Krehl's avatar
Jim Krehl committed
934
		path = find_package_data_file (priv->store_filename);
Jim Krehl's avatar
Jim Krehl committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	}

	if (use_user_path)
		status = BOOKMARK_STORE_USER;
	else if (path && priv->user_modifiable)
		status = BOOKMARK_STORE_DEFAULT;
	else if (path)
		status = BOOKMARK_STORE_DEFAULT_ONLY;
	else
		status = BOOKMARK_STORE_ABSENT;
	
	if (priv->status != status) {
		priv->status = status;
		g_object_notify (G_OBJECT (this), BOOKMARK_AGENT_STORE_STATUS_PROP);

		if (priv->user_store_monitor) {
			gnome_vfs_monitor_cancel (priv->user_store_monitor);
			priv->user_store_monitor = NULL;
		}

		if (priv->status == BOOKMARK_STORE_DEFAULT)
			gnome_vfs_monitor_add (
				& priv->user_store_monitor, priv->user_store_path,
				GNOME_VFS_MONITOR_FILE, store_monitor_cb, this);
	}

	if (libslab_strcmp (priv->store_path, path)) {
		g_free (priv->store_path);
		priv->store_path = path;

		if (priv->store_monitor)
			gnome_vfs_monitor_cancel (priv->store_monitor);

		if (priv->store_path)
			gnome_vfs_monitor_add (
				& priv->store_monitor, priv->store_path,
				GNOME_VFS_MONITOR_FILE, store_monitor_cb, this);
	}
	else
		g_free (path);
}

static void
update_recent_store_path (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	BookmarkStoreStatus status;


	if (g_file_test (priv->store_path, G_FILE_TEST_EXISTS))
		status = BOOKMARK_STORE_USER;
	else
		status = BOOKMARK_STORE_USER;

	if (priv->status != status) {
		priv->status = status;
		g_object_notify (G_OBJECT (this), BOOKMARK_AGENT_STORE_STATUS_PROP);
	}
}

static void
save_xbel_store (BookmarkAgent *this)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	GError *error = NULL;


	if (! g_bookmark_file_to_file (priv->store, priv->store_path, & error))
		libslab_handle_g_error (
			& error, "%s: couldn't save bookmark file [%s]\n", G_STRFUNC, priv->store_path);
}

static void
create_app_item (BookmarkAgent *this, const gchar *uri)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	GnomeDesktopItem *ditem;
	gchar *uri_new = NULL;


	ditem = libslab_gnome_desktop_item_new_from_unknown_id (uri);

	if (ditem) {
		uri_new = g_strdup (gnome_desktop_item_get_location (ditem));
		gnome_desktop_item_unref (ditem);
	}

	if (! uri_new)
		return;

	if (libslab_strcmp (uri, uri_new)) {
		g_bookmark_file_move_item (priv->store, uri, uri_new, NULL);
	}
}

static void
create_doc_item (BookmarkAgent *this, const gchar *uri)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gchar *uri_new = NULL;
	gchar *path;
	gchar *dir;
	gchar *file;
Jim Krehl's avatar
Jim Krehl committed
1042 1043 1044
	gchar *template = NULL;
	gsize  length;
	gchar *contents;
Jim Krehl's avatar
Jim Krehl committed
1045 1046 1047 1048 1049


	if (! (strcmp (uri, "BLANK_SPREADSHEET") && strcmp (uri, "BLANK_DOCUMENT"))) {
		dir = g_build_filename (g_get_home_dir (), "Documents", NULL);

Jim Krehl's avatar
Jim Krehl committed
1050
		if (! strcmp (uri, "BLANK_SPREADSHEET")) {
Jim Krehl's avatar
Jim Krehl committed
1051
			file = g_strconcat (_("New Spreadsheet"), ".ods", NULL);
Jim Krehl's avatar
Jim Krehl committed
1052 1053
			template = find_package_data_file (CALC_TEMPLATE_FILE_NAME);
		}
Jim Krehl's avatar
Jim Krehl committed
1054 1055 1056 1057 1058 1059 1060
		else
			file = g_strconcat (_("New Document"), ".odt", NULL);

		path = g_build_filename (dir, file, NULL);

		if (! g_file_test (path, G_FILE_TEST_EXISTS)) {
			g_mkdir_with_parents (dir, 0700);
Jim Krehl's avatar
Jim Krehl committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070

			if (template != NULL) {
				if (g_file_get_contents (template, & contents, & length, NULL))
					g_file_set_contents (path, contents, length, NULL);

				g_free (contents);
				g_free (template);
			}
			else
				fclose (g_fopen (path, "w"));
Jim Krehl's avatar
Jim Krehl committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
		}

		uri_new = g_filename_to_uri (path, NULL, NULL);

		g_free (dir);
		g_free (file);
		g_free (path);
	}

	if (! uri_new)
		return;

	if (libslab_strcmp (uri, uri_new))
		g_bookmark_file_move_item (priv->store, uri, uri_new, NULL);
}

static void
create_dir_item (BookmarkAgent *this, const gchar *uri)
{
	BookmarkAgentPrivate *priv = PRIVATE (this);

	gchar *uri_new = NULL;
	gchar *path;
	gchar *name = NULL;
	gchar *icon = NULL;

	gchar *buf;
	gchar *tag_open_ptr  = NULL;
	gchar *tag_close_ptr = NULL;
	gchar *search_string = NULL;


	if (! strcmp (uri, "HOME")) {
		uri_new = g_filename_to_uri (g_get_home_dir (), NULL, NULL);
		name    = _("Home");
		icon    = "gnome-fs-home";
	}
	else if (! strcmp (uri, "DOCUMENTS")) {
		path = g_build_filename (g_get_home_dir (), "Documents", NULL);
		uri_new = g_filename_to_uri (path, NULL, NULL);
		g_free (path);
	}
	else if (! strcmp (uri, "DESKTOP")) {
		path = g_build_filename (g_get_home_dir (), "Desktop", NULL);
		uri_new = g_filename_to_uri (path, NULL, NULL);
		icon = "gnome-fs-desktop";
		g_free (path);
	}
	else if (! strcmp (uri, "file:///")) {
		icon = "drive-harddisk";
		name = _("File System");
	}
	else if (! strcmp (uri, "network:")) {
		icon = "network-workgroup";
		name = _("Network Servers");
	}
	else if (g_str_has_prefix (uri, "x-nautilus-search")) {
		icon = "system-search";

		path = g_build_filename (g_get_home_dir (), ".nautilus", "searches", & uri [21], NULL);

		if (g_file_test (path, G_FILE_TEST_EXISTS)) {
			g_file_get_contents (path, & buf, NULL, NULL);

			if (buf) {
				tag_open_ptr  = strstr (buf, "<text>");
				tag_close_ptr = strstr (buf, "</text>");
			}

			if (tag_open_ptr && tag_close_ptr) {
				tag_close_ptr [0] = '\0';

				search_string = g_strdup_printf ("\"%s\"", & tag_open_ptr [6]);

				tag_close_ptr [0] = 'a';
			}

			g_free (buf);
		}

		if (search_string)
			name = search_string;
		else
			name = _("Search");

		g_free (path);
	}

	if (icon)
		g_bookmark_file_set_icon (priv->store, uri, icon, "image/png");

	if (name)
		g_bookmark_file_set_title (priv->store, uri, name);

	g_bookmark_file_set_mime_type   (priv->store, uri, "inode/directory");
	g_bookmark_file_add_application (priv->store, uri, "nautilus", "nautilus --browser %u");

	if (uri_new && libslab_strcmp (uri, uri_new))
		g_bookmark_file_move_item (priv->store, uri, uri_new, NULL);
}

static void
store_monitor_cb (GnomeVFSMonitorHandle *handle, const gchar *monitor_uri,
                  const gchar *info_uri, GnomeVFSMonitorEventType type, gpointer user_data)
{
	update_agent (BOOKMARK_AGENT (user_data));
}

static void
gconf_notify_cb (GConfClient *client, guint conn_id,
                 GConfEntry *entry, gpointer user_data)
{
	BookmarkAgent        *this = BOOKMARK_AGENT (user_data);
	BookmarkAgentPrivate *priv = PRIVATE        (this);

	gboolean user_modifiable;


	user_modifiable = GPOINTER_TO_INT (libslab_get_gconf_value (priv->lockdown_key));

	if (priv->user_modifiable != user_modifiable) {
		priv->user_modifiable = user_modifiable;
		update_agent (this);
	}
}

static void
weak_destroy_cb (gpointer data, GObject *g_obj)
{
	instances [GPOINTER_TO_INT (data)] = NULL;
}

#ifdef USE_GTK_RECENT_MANAGER
static gint
recent_item_mru_comp_func (gconstpointer a, gconstpointer b)
{
	return ((BookmarkItem *) b)->mtime - ((BookmarkItem *) a)->mtime;
}
#endif