app-shell.c 40.9 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
/*
 * This file is part of libslab.
 *
 * Copyright (c) 2006 Novell, Inc.
 *
 * Libslab is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * Libslab 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 Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libslab; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <libgnome/gnome-desktop-item.h>
#include <libgnomeui/libgnomeui.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "app-shell.h"
#include "shell-window.h"
#include "app-resizer.h"
#include "slab-section.h"
#include "slab-gnome-util.h"
#include "search-bar.h"

#include "application-tile.h"
#include "themed-icon.h"

#define TILE_EXEC_NAME "Tile_desktop_exec_name"
#define SECONDS_IN_DAY 86400
#define EXIT_SHELL_ON_ACTION_START "exit_shell_on_action_start"
#define EXIT_SHELL_ON_ACTION_HELP "exit_shell_on_action_help"
#define EXIT_SHELL_ON_ACTION_ADD_REMOVE "exit_shell_on_action_add_remove"
#define EXIT_SHELL_ON_ACTION_UPGRADE_UNINSTALL "exit_shell_on_action_upgrade_uninstall"
#define NEW_APPS_FILE_KEY "new_apps_file_key"

static void create_application_category_sections (AppShellData * app_data);
46 47 48 49 50 51 52
static GtkWidget *create_filter_section (AppShellData * app_data, const gchar * title);
static GtkWidget *create_groups_section (AppShellData * app_data, const gchar * title);
static GtkWidget *create_actions_section (AppShellData * app_data, const gchar * title,
	void (*actions_handler) (Tile *, TileEvent *, gpointer));

static void generate_launchers (GMenuTreeDirectory * root_dir, AppShellData * app_data,
	CategoryData * cat_data);
Jim Krehl's avatar
Jim Krehl committed
53
static void generate_new_apps (AppShellData * app_data);
54 55
static void insert_launcher_into_category (CategoryData * cat_data, GnomeDesktopItem * desktop_item,
	AppShellData * app_data);
Jim Krehl's avatar
Jim Krehl committed
56

57 58
static gboolean main_keypress_callback (GtkWidget * widget, GdkEventKey * event,
	AppShellData * app_data);
Jim Krehl's avatar
Jim Krehl committed
59
static gboolean main_delete_callback (GtkWidget * widget, GdkEvent * event,
60
	AppShellData * app_data);
Jim Krehl's avatar
Jim Krehl committed
61 62 63 64
static void launch_selected_app (AppShellData * app_data);
static void generate_potential_apps (gpointer catdata, gpointer user_data);

static void relayout_shell (AppShellData * app_data);
65 66 67
static gboolean handle_filter_changed (NldSearchBar * search_bar, int context, const char *text,
	gpointer user_data);
static void handle_group_clicked (Tile * tile, TileEvent * event, gpointer user_data);
Jim Krehl's avatar
Jim Krehl committed
68 69 70
static void set_state (AppShellData * app_data, GtkWidget * widget);
static void populate_groups_section (AppShellData * app_data);
static void generate_filtered_lists (gpointer catdata, gpointer user_data);
71
static void show_no_results_message (AppShellData * app_data, GtkWidget * containing_vbox);
Jim Krehl's avatar
Jim Krehl committed
72
static void populate_application_category_sections (AppShellData * app_data,
73 74 75 76
	GtkWidget * containing_vbox);
static void populate_application_category_section (AppShellData * app_data, SlabSection * section,
	GList * launcher_list);
static void tile_activated_cb (Tile * tile, TileEvent * event, gpointer user_data);
Jim Krehl's avatar
Jim Krehl committed
77
static void handle_launcher_single_clicked (Tile * launcher, gpointer data);
78 79
static void handle_menu_action_performed (Tile * launcher, TileEvent * event, TileAction * action,
	gpointer data);
Jim Krehl's avatar
Jim Krehl committed
80 81 82
static gint category_name_compare (gconstpointer a, gconstpointer b);
static gint category_data_compare (gconstpointer a, gconstpointer b);
static gint application_launcher_compare (gconstpointer a, gconstpointer b);
83
static void gmenu_tree_changed_callback (GMenuTree * tree, gpointer user_data);
Jim Krehl's avatar
Jim Krehl committed
84 85 86 87 88 89
gboolean regenerate_categories (AppShellData * app_data);

void
hide_shell (AppShellData * app_data)
{
	gtk_window_get_position (GTK_WINDOW (app_data->main_gnome_app),
90 91
		&app_data->main_gnome_app_window_x, &app_data->main_gnome_app_window_y);
	/* printf("x:%d, y:%d\n", app_data->main_gnome_app_window_x, app_data->main_gnome_app_window_y); */
Jim Krehl's avatar
Jim Krehl committed
92 93 94 95 96 97 98 99
	gtk_widget_hide (app_data->main_gnome_app);
}

void
show_shell (AppShellData * app_data)
{
	gtk_widget_show_all (app_data->main_gnome_app);
	if (!app_data->static_actions)
100
		gtk_widget_hide_all (app_data->actions_section);  /* don't show unless a launcher is selected */
Jim Krehl's avatar
Jim Krehl committed
101 102 103

	if (app_data->main_gnome_app_window_shown_once)
		gtk_window_move (GTK_WINDOW (app_data->main_gnome_app),
104 105 106
			app_data->main_gnome_app_window_x, app_data->main_gnome_app_window_y);

	/* if this is the first time shown, need to clear this handler */
Jim Krehl's avatar
Jim Krehl committed
107
	else
108
		shell_window_clear_resize_handler (SHELL_WINDOW (app_data->shell));
Jim Krehl's avatar
Jim Krehl committed
109 110 111 112
	app_data->main_gnome_app_window_shown_once = TRUE;
}

gboolean
113 114
create_main_window (AppShellData * app_data, const gchar * app_name, const gchar * title,
	const gchar * window_icon, gint width, gint height, gboolean hidden)
Jim Krehl's avatar
Jim Krehl committed
115 116 117
{
	GtkWidget *main_app = gnome_app_new (app_name, title);
	app_data->main_gnome_app = main_app;
118
	/* gtk_window_set_default_size(GTK_WINDOW(main_app), width, height); */
Jim Krehl's avatar
Jim Krehl committed
119 120 121
	gtk_window_set_icon_name (GTK_WINDOW (main_app), window_icon);
	gnome_app_set_contents (GNOME_APP (main_app), app_data->shell);

122 123 124
	g_signal_connect (main_app, "delete-event", G_CALLBACK (main_delete_callback), app_data);
	g_signal_connect (main_app, "key-press-event", G_CALLBACK (main_keypress_callback),
		app_data);
Jim Krehl's avatar
Jim Krehl committed
125

126
	gtk_window_set_position (GTK_WINDOW (app_data->main_gnome_app), GTK_WIN_POS_CENTER);
Jim Krehl's avatar
Jim Krehl committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	if (!hidden)
		show_shell (app_data);
	gtk_main ();

	return TRUE;
}

static void
generate_potential_apps (gpointer catdata, gpointer user_data)
{
	GHashTable *app_hash = (GHashTable *) user_data;
	CategoryData *data = (CategoryData *) catdata;
	gchar *uri;

	GList *launcher_list = data->filtered_launcher_list;

143 144
	while (launcher_list)
	{
Jim Krehl's avatar
Jim Krehl committed
145
		g_object_get (launcher_list->data, "tile-uri", &uri, NULL);
146 147 148
		/* eliminate dups of same app in multiple categories */
		if (!g_hash_table_lookup (app_hash, uri))
			g_hash_table_insert (app_hash, uri, launcher_list->data);
Jim Krehl's avatar
Jim Krehl committed
149 150 151 152 153 154 155 156 157
		else
			g_free (uri);
		launcher_list = g_list_next (launcher_list);
	}
}

static gboolean
return_first_entry (gpointer key, gpointer value, gpointer unused)
{
158
	return TRUE;	/*better way to pull an entry out ? */
Jim Krehl's avatar
Jim Krehl committed
159 160 161 162 163
}

static void
launch_selected_app (AppShellData * app_data)
{
164 165
	GHashTable *app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	g_list_foreach (app_data->categories_list, generate_potential_apps, app_hash);
Jim Krehl's avatar
Jim Krehl committed
166
	guint num_apps = g_hash_table_size (app_hash);
167 168
	if (num_apps == 1)
	{
Jim Krehl's avatar
Jim Krehl committed
169
		ApplicationTile *launcher =
170
			APPLICATION_TILE (g_hash_table_find (app_hash, return_first_entry, NULL));
Jim Krehl's avatar
Jim Krehl committed
171 172 173 174 175 176 177 178 179
		g_hash_table_destroy (app_hash);
		handle_launcher_single_clicked (TILE (launcher), app_data);
		return;
	}

	g_hash_table_destroy (app_hash);
}

static gboolean
180
main_keypress_callback (GtkWidget * widget, GdkEventKey * event, AppShellData * app_data)
Jim Krehl's avatar
Jim Krehl committed
181
{
182 183 184 185 186 187 188 189 190 191 192 193
	if (event->keyval == GDK_Return)
	{
		SlabSection *section = SLAB_SECTION (app_data->filter_section);

		/* Make sure our implementation has not changed */
		g_assert (NLD_IS_SEARCH_BAR (section->contents));
		NldSearchBar *search_bar = NLD_SEARCH_BAR (section->contents);
		if (nld_search_bar_has_focus (search_bar))
		{
			launch_selected_app (app_data);
			return TRUE;
		}
Jim Krehl's avatar
Jim Krehl committed
194 195 196 197
	}

	/* quit on ESC or Ctl-W or Ctl-Q */
	if (event->keyval == GDK_Escape ||
198 199
		((event->keyval == GDK_w || event->keyval == GDK_W)	&& (event->state & GDK_CONTROL_MASK)) ||
		((event->keyval == GDK_q || event->keyval == GDK_Q) && (event->state & GDK_CONTROL_MASK)))
Jim Krehl's avatar
Jim Krehl committed
200 201 202 203 204 205 206 207
	{
		hide_shell (app_data);
		return TRUE;
	}
	return FALSE;
}

static gboolean
208
main_delete_callback (GtkWidget * widget, GdkEvent * event, AppShellData * app_data)
Jim Krehl's avatar
Jim Krehl committed
209 210 211 212 213 214
{
	hide_shell (app_data);
	return TRUE;		/* stop the processing of this event */
}

void
215 216 217
layout_shell (AppShellData * app_data, const gchar * filter_title, const gchar * groups_title,
	const gchar * actions_title, GSList * actions,
	void (*actions_handler) (Tile *, TileEvent *, gpointer))
Jim Krehl's avatar
Jim Krehl committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
{
	GtkWidget *filter_section;
	GtkWidget *groups_section;
	GtkWidget *actions_section;

	GtkWidget *left_vbox;
	GtkWidget *right_vbox;
	gint num_cols;

	app_data->shell = shell_window_new (app_data);
	app_data->static_actions = actions;

	right_vbox = gtk_vbox_new (FALSE, CATEGORY_SPACING);

	num_cols = SIZING_SCREEN_WIDTH_LARGE_NUMCOLS;
233 234
	if (gdk_screen_width () <= SIZING_SCREEN_WIDTH_LARGE)
	{
Jim Krehl's avatar
Jim Krehl committed
235 236 237 238 239 240
		if (gdk_screen_width () <= SIZING_SCREEN_WIDTH_MEDIUM)
			num_cols = SIZING_SCREEN_WIDTH_SMALL_NUMCOLS;
		else
			num_cols = SIZING_SCREEN_WIDTH_MEDIUM_NUMCOLS;
	}
	app_data->category_layout =
241
		app_resizer_new (GTK_VBOX (right_vbox), num_cols, TRUE, app_data);
Jim Krehl's avatar
Jim Krehl committed
242 243

	GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
244 245
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_AUTOMATIC,
		GTK_POLICY_AUTOMATIC);
Jim Krehl's avatar
Jim Krehl committed
246
	gtk_container_add (GTK_CONTAINER (sw), app_data->category_layout);
247
	GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
Jim Krehl's avatar
Jim Krehl committed
248 249 250 251 252
	g_object_set (adjustment, "step-increment", (double) 20, NULL);

	create_application_category_sections (app_data);
	populate_application_category_sections (app_data, right_vbox);
	app_resizer_set_table_cache (APP_RESIZER (app_data->category_layout),
253
		app_data->cached_tables_list);
Jim Krehl's avatar
Jim Krehl committed
254 255

	gtk_container_set_focus_vadjustment (GTK_CONTAINER (right_vbox),
256
		gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw)));
Jim Krehl's avatar
Jim Krehl committed
257 258 259 260 261

	left_vbox = gtk_vbox_new (FALSE, 15);

	filter_section = create_filter_section (app_data, filter_title);
	app_data->filter_section = filter_section;
262
	gtk_box_pack_start (GTK_BOX (left_vbox), filter_section, FALSE, FALSE, 0);
Jim Krehl's avatar
Jim Krehl committed
263 264 265 266

	groups_section = create_groups_section (app_data, groups_title);
	app_data->groups_section = groups_section;
	populate_groups_section (app_data);
267
	gtk_box_pack_start (GTK_BOX (left_vbox), groups_section, FALSE, FALSE, 0);
Jim Krehl's avatar
Jim Krehl committed
268

269
	actions_section = create_actions_section (app_data, actions_title, actions_handler);
Jim Krehl's avatar
Jim Krehl committed
270
	app_data->actions_section = actions_section;
271
	gtk_box_pack_start (GTK_BOX (left_vbox), actions_section, FALSE, FALSE, 0);
Jim Krehl's avatar
Jim Krehl committed
272

273
	shell_window_set_contents (SHELL_WINDOW (app_data->shell), left_vbox, sw);
Jim Krehl's avatar
Jim Krehl committed
274 275 276 277 278 279 280 281 282 283 284 285
}

static gboolean
relayout_shell_partial (gpointer user_data)
{
	AppShellData *app_data = (AppShellData *) user_data;
	GtkVBox *vbox = APP_RESIZER (app_data->category_layout)->child;
	CategoryData *data;

	if (app_data->stop_incremental_relayout)
		return FALSE;

286 287
	if (app_data->incremental_relayout_cat_list != NULL)
	{
Jim Krehl's avatar
Jim Krehl committed
288
		/* There are still categories to layout */
289 290 291 292 293 294 295
		data = (CategoryData *) app_data->incremental_relayout_cat_list->data;
		if (data->filtered_launcher_list != NULL)
		{
			populate_application_category_section (app_data, data->section,
				data->filtered_launcher_list);
			gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (data->section), TRUE, TRUE,
				0);
Jim Krehl's avatar
Jim Krehl committed
296 297 298 299 300 301 302 303 304 305 306 307 308
			app_data->filtered_out_everything = FALSE;
		}

		app_data->incremental_relayout_cat_list =
			g_list_next (app_data->incremental_relayout_cat_list);
		return TRUE;
	}

	/* We're done laying out the categories; finish up */
	if (app_data->filtered_out_everything)
		show_no_results_message (app_data, GTK_WIDGET (vbox));

	app_resizer_set_table_cache (APP_RESIZER (app_data->category_layout),
309
		app_data->cached_tables_list);
Jim Krehl's avatar
Jim Krehl committed
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
	populate_groups_section (app_data);

	gtk_widget_show_all (app_data->category_layout);
	gdk_window_set_cursor (app_data->shell->window, NULL);

	app_data->stop_incremental_relayout = TRUE;
	return FALSE;
}

static void
relayout_shell_incremental (AppShellData * app_data)
{
	GtkVBox *vbox = APP_RESIZER (app_data->category_layout)->child;

	app_data->stop_incremental_relayout = FALSE;
	app_data->filtered_out_everything = TRUE;
	app_data->incremental_relayout_cat_list = app_data->categories_list;

	if (app_data->cached_tables_list)
		g_list_free (app_data->cached_tables_list);
	app_data->cached_tables_list = NULL;

	remove_container_entries (GTK_CONTAINER (vbox));

	g_idle_add ((GSourceFunc) relayout_shell_partial, app_data);
}

static void
relayout_shell (AppShellData * app_data)
{
	GtkWidget *shell;

	shell = app_data->shell;
	GtkVBox *vbox = APP_RESIZER (app_data->category_layout)->child;

	populate_application_category_sections (app_data, GTK_WIDGET (vbox));
	app_resizer_set_table_cache (APP_RESIZER (app_data->category_layout),
347
		app_data->cached_tables_list);
Jim Krehl's avatar
Jim Krehl committed
348 349 350 351
	populate_groups_section (app_data);

	gtk_widget_show_all (shell);
	if (!app_data->static_actions && !app_data->last_clicked_launcher)
352
		gtk_widget_hide_all (app_data->actions_section);  /* don't show unless a launcher is selected */
Jim Krehl's avatar
Jim Krehl committed
353 354 355 356
}

static GtkWidget *
create_actions_section (AppShellData * app_data, const gchar * title,
357
	void (*actions_handler) (Tile *, TileEvent *, gpointer))
Jim Krehl's avatar
Jim Krehl committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371
{
	g_assert (app_data != NULL);

	GtkWidget *section, *launcher;
	GtkWidget *vbox;
	GSList *actions;
	AppAction *action;

	section = slab_section_new (title, Style1);
	g_object_ref (section);

	vbox = gtk_vbox_new (FALSE, 0);
	slab_section_set_contents (SLAB_SECTION (section), vbox);

372 373 374 375
	if (app_data->static_actions)
	{
		for (actions = app_data->static_actions; actions; actions = actions->next)
		{
Jim Krehl's avatar
Jim Krehl committed
376 377 378
			action = (AppAction *) actions->data;
			GtkWidget *header = gtk_label_new (action->name);
			gtk_misc_set_alignment (GTK_MISC (header), 0, 0.5);
379 380 381 382 383 384
			launcher = nameplate_tile_new (NULL, NULL, header, NULL);

			g_object_set_data (G_OBJECT (launcher), APP_ACTION_KEY, action->item);
			g_signal_connect (launcher, "tile-activated", G_CALLBACK (actions_handler),
				app_data);
			gtk_box_pack_start (GTK_BOX (vbox), launcher, FALSE, FALSE, 0);
Jim Krehl's avatar
Jim Krehl committed
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
		}
	}

	return section;
}

static GtkWidget *
create_groups_section (AppShellData * app_data, const gchar * title)
{
	g_assert (app_data != NULL);

	GtkWidget *section;
	GtkWidget *vbox;

	section = slab_section_new (title, Style1);
	g_object_ref (section);

	vbox = gtk_vbox_new (FALSE, 0);
	slab_section_set_contents (SLAB_SECTION (section), vbox);

	return section;
}

static void
populate_groups_section (AppShellData * app_data)
{
	SlabSection *section = SLAB_SECTION (app_data->groups_section);
412 413
	/* Make sure our implementation has not changed and it's still a GtkVBox */
	g_assert (GTK_IS_VBOX (section->contents));
Jim Krehl's avatar
Jim Krehl committed
414 415 416 417 418

	GtkVBox *vbox = GTK_VBOX (section->contents);
	remove_container_entries (GTK_CONTAINER (vbox));

	GList *cat_list = app_data->categories_list;
419 420
	do
	{
Jim Krehl's avatar
Jim Krehl committed
421
		CategoryData *data = (CategoryData *) cat_list->data;
422 423 424 425 426
		if (NULL != data->filtered_launcher_list)
		{
			gtk_widget_set_state (GTK_WIDGET (data->group_launcher), GTK_STATE_NORMAL);
			gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (data->group_launcher),
				FALSE, FALSE, 0);
Jim Krehl's avatar
Jim Krehl committed
427 428 429 430 431 432 433 434 435 436 437 438
		}
	}
	while (NULL != (cat_list = g_list_next (cat_list)));
}

static void
handle_group_clicked (Tile * tile, TileEvent * event, gpointer user_data)
{
	AppShellData *app_data = (AppShellData *) user_data;
	GtkWidget *section = NULL;

	gint clicked_pos =
439
		GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tile), GROUP_POSITION_NUMBER_KEY));
Jim Krehl's avatar
Jim Krehl committed
440 441 442 443

	GList *cat_list = app_data->categories_list;

	gint total = 0;
444 445
	do
	{
Jim Krehl's avatar
Jim Krehl committed
446 447
		CategoryData *cat_data = (CategoryData *) cat_list->data;
		gint pos =
448 449 450 451
			GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cat_data->group_launcher),
				GROUP_POSITION_NUMBER_KEY));
		if (pos == clicked_pos)
		{
Jim Krehl's avatar
Jim Krehl committed
452 453 454 455
			section = GTK_WIDGET (cat_data->section);
			break;
		}

456 457 458 459
		if (NULL != cat_data->filtered_launcher_list)
		{
			total += GTK_WIDGET (cat_data->section)->allocation.height +
				CATEGORY_SPACING;
Jim Krehl's avatar
Jim Krehl committed
460 461 462 463 464 465 466 467 468 469 470 471 472
		}
	}
	while (NULL != (cat_list = g_list_next (cat_list)));

	g_assert (section != NULL);
	set_state (app_data, section);

	app_resizer_set_vadjustment_value (app_data->category_layout, total);
}

static void
set_state (AppShellData * app_data, GtkWidget * widget)
{
473 474
	if (app_data->selected_group)
	{
Jim Krehl's avatar
Jim Krehl committed
475 476 477 478
		slab_section_set_selected (app_data->selected_group, FALSE);
		app_data->selected_group = NULL;
	}

479 480
	if (widget)
	{
Jim Krehl's avatar
Jim Krehl committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
		app_data->selected_group = SLAB_SECTION (widget);
		slab_section_set_selected (SLAB_SECTION (widget), TRUE);
	}
	gtk_widget_queue_draw (app_data->shell);
}

static GtkWidget *
create_filter_section (AppShellData * app_data, const gchar * title)
{
	GtkWidget *section;

	GtkWidget *search_bar;

	section = slab_section_new (title, Style1);
	g_object_ref (section);

	search_bar = nld_search_bar_new ();
	nld_search_bar_set_search_timeout (NLD_SEARCH_BAR (search_bar), 0);
	slab_section_set_contents (SLAB_SECTION (section), search_bar);

501 502
	g_signal_connect (G_OBJECT (search_bar), "search", G_CALLBACK (handle_filter_changed),
		app_data);
Jim Krehl's avatar
Jim Krehl committed
503 504 505 506 507 508 509 510 511 512

	return section;
}

static gboolean
handle_filter_changed_delayed (gpointer user_data)
{
	AppShellData *app_data = (AppShellData *) user_data;

	g_list_foreach (app_data->categories_list, generate_filtered_lists,
513 514 515 516 517 518 519 520 521 522
		(gpointer) app_data->filter_string);
	app_data->last_clicked_launcher = NULL;

	/*  showing the updates incremtally is very visually distracting. Much worse than just blanking until
	   the incremental work is done and then doing one show. It would be nice to optimize this though
	   somehow and not even show any change but the cursor change until all the work is done. But since
	   we do the work incrementally in an idle loop I don't know how else besides hiding to not show
	   incremental updates
	 */
	/* gdk_window_freeze_updates(app_data->category_layout->window); */
Jim Krehl's avatar
Jim Krehl committed
523 524
	gtk_widget_hide (app_data->category_layout);
	app_data->busy_cursor =
525 526
		gdk_cursor_new_for_display (gtk_widget_get_display (app_data->shell), GDK_WATCH);
	gdk_window_set_cursor (app_data->shell->window, app_data->busy_cursor);
Jim Krehl's avatar
Jim Krehl committed
527 528 529 530 531 532 533 534 535 536 537 538
	gdk_cursor_unref (app_data->busy_cursor);

	set_state (app_data, NULL);
	app_resizer_set_vadjustment_value (app_data->category_layout, 0);

	relayout_shell_incremental (app_data);

	app_data->filter_changed_timeout = 0;
	return FALSE;
}

static gboolean
539
handle_filter_changed (NldSearchBar * search_bar, int context, const char *text, gpointer data)
Jim Krehl's avatar
Jim Krehl committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
{
	AppShellData *app_data;

	app_data = (AppShellData *) data;

	if (app_data->filter_string)
		g_free (app_data->filter_string);
	app_data->filter_string = g_strdup (text);

	if (app_data->filter_changed_timeout)
		g_source_remove (app_data->filter_changed_timeout);

	app_data->filter_changed_timeout =
		g_timeout_add (75, handle_filter_changed_delayed, app_data);
	app_data->stop_incremental_relayout = TRUE;

	return FALSE;
}

static void
generate_filtered_lists (gpointer catdata, gpointer user_data)
{
	CategoryData *data = (CategoryData *) catdata;

564
	/* Fixme - everywhere you use ascii you need to fix up for multibyte */
Jim Krehl's avatar
Jim Krehl committed
565 566 567 568 569 570 571
	gchar *filter_string = g_ascii_strdown (user_data, -1);
	gchar *temp1, *temp2;
	GList *launcher_list = data->launcher_list;

	g_list_free (data->filtered_launcher_list);
	data->filtered_launcher_list = NULL;

572 573
	do
	{
Jim Krehl's avatar
Jim Krehl committed
574 575
		temp1 = NULL;
		temp2 = NULL;
576 577 578 579 580 581
		ApplicationTile *launcher = APPLICATION_TILE (launcher_list->data);

		/* Since the filter may remove this entry from the
		   container it will not get a mouse out event */
		gtk_widget_set_state (GTK_WIDGET (launcher), GTK_STATE_NORMAL);
		const gchar *filename = g_object_get_data (G_OBJECT (launcher), TILE_EXEC_NAME); /* do I need to free this */
Jim Krehl's avatar
Jim Krehl committed
582 583 584 585

		temp1 = g_ascii_strdown (launcher->name, -1);
		if (launcher->description)
			temp2 = g_ascii_strdown (launcher->description, -1);
586 587 588 589
		if (g_strrstr (temp1, filter_string) || (launcher->description
				&& g_strrstr (temp2, filter_string))
			|| g_strrstr (filename, filter_string))
		{
Jim Krehl's avatar
Jim Krehl committed
590
			data->filtered_launcher_list =
591
				g_list_append (data->filtered_launcher_list, launcher);
Jim Krehl's avatar
Jim Krehl committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
		}
		if (temp1)
			g_free (temp1);
		if (temp2)
			g_free (temp2);
	}
	while (NULL != (launcher_list = g_list_next (launcher_list)));
	g_free (filter_string);
}

static void
delete_old_data (AppShellData * app_data)
{
	g_assert (app_data != NULL);
	g_assert (app_data->categories_list != NULL);

	GList *temp;
	GList *cat_list = app_data->categories_list;

611 612
	do
	{
Jim Krehl's avatar
Jim Krehl committed
613 614 615
		CategoryData *data = (CategoryData *) cat_list->data;
		gtk_widget_destroy (GTK_WIDGET (data->section));
		gtk_widget_destroy (GTK_WIDGET (data->group_launcher));
616 617
		gtk_object_unref (GTK_OBJECT (data->section));
		gtk_object_unref (GTK_OBJECT (data->group_launcher));
Jim Krehl's avatar
Jim Krehl committed
618
		g_free (data->category);
619 620 621 622 623 624

		for (temp = data->launcher_list; temp; temp = g_list_next (temp))
		{
			g_free (g_object_get_data (G_OBJECT (temp->data), TILE_EXEC_NAME));
			gtk_object_unref (temp->data);
		}
Jim Krehl's avatar
Jim Krehl committed
625 626 627

		g_list_free (data->launcher_list);
		g_list_free (data->filtered_launcher_list);
628
		g_free (data);
Jim Krehl's avatar
Jim Krehl committed
629 630 631 632 633
	}
	while (NULL != (cat_list = g_list_next (cat_list)));

	g_list_free (app_data->categories_list);
	app_data->categories_list = NULL;
634
	app_data->selected_group = NULL;
Jim Krehl's avatar
Jim Krehl committed
635 636 637 638 639 640
}

static void
create_application_category_sections (AppShellData * app_data)
{
	g_assert (app_data != NULL);
641
	g_assert (app_data->categories_list != NULL);	/* Fixme - pop up a dialog box and then close */
Jim Krehl's avatar
Jim Krehl committed
642 643 644 645

	GList *cat_list = app_data->categories_list;
	gint pos = 0;

646 647
	do
	{
Jim Krehl's avatar
Jim Krehl committed
648 649 650 651
		CategoryData *data = (CategoryData *) cat_list->data;

		GtkWidget *header = gtk_label_new (data->category);
		gtk_misc_set_alignment (GTK_MISC (header), 0, 0.5);
652
		data->group_launcher = TILE (nameplate_tile_new (NULL, NULL, header, NULL));
Jim Krehl's avatar
Jim Krehl committed
653 654
		g_object_ref (data->group_launcher);

655 656
		g_object_set_data (G_OBJECT (data->group_launcher), GROUP_POSITION_NUMBER_KEY,
			GINT_TO_POINTER (pos));
Jim Krehl's avatar
Jim Krehl committed
657 658
		pos++;
		g_signal_connect (data->group_launcher, "tile-activated",
659
			G_CALLBACK (handle_group_clicked), app_data);
Jim Krehl's avatar
Jim Krehl committed
660 661

		gchar *markup =
662 663 664 665 666 667
			g_markup_printf_escaped ("<span size=\"x-large\">%s</span>",
			data->category);
		data->section = SLAB_SECTION (slab_section_new_with_markup (markup, Style2));

		/* as we filter these will be added/removed from parent container and we dont want them destroyed */
		g_object_ref (data->section);
Jim Krehl's avatar
Jim Krehl committed
668 669 670 671 672 673 674
		g_free (markup);

		GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
		GtkWidget *table = gtk_table_new (0, 0, TRUE);
		gtk_table_set_col_spacings (GTK_TABLE (table), 5);
		gtk_table_set_row_spacings (GTK_TABLE (table), 5);
		gtk_box_pack_start (GTK_BOX (hbox), table, FALSE, FALSE, 15);
675
		slab_section_set_contents (SLAB_SECTION (data->section), hbox);
Jim Krehl's avatar
Jim Krehl committed
676 677 678 679 680 681 682
	}
	while (NULL != (cat_list = g_list_next (cat_list)));
}

static void
show_no_results_message (AppShellData * app_data, GtkWidget * containing_vbox)
{
683 684 685
	if (!app_data->filtered_out_everything_widget)
	{
		app_data->filtered_out_everything_widget = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
Jim Krehl's avatar
Jim Krehl committed
686 687 688
		g_object_ref (app_data->filtered_out_everything_widget);

		GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
689
		GtkWidget *image = themed_icon_new ("face-surprise", GTK_ICON_SIZE_DIALOG);
Jim Krehl's avatar
Jim Krehl committed
690 691 692 693 694
		gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);

		GtkWidget *label = gtk_label_new (NULL);
		gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
		gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 15);
695
		app_data->filtered_out_everything_widget_label = GTK_LABEL (label);
Jim Krehl's avatar
Jim Krehl committed
696

697
		gtk_container_add (GTK_CONTAINER (app_data->filtered_out_everything_widget), hbox);
Jim Krehl's avatar
Jim Krehl committed
698 699
	}

700 701 702 703 704 705 706
	gchar *markup = g_strdup_printf (
		_("<span size=\"large\"><b>No matches found.</b> </span><span>\n\n Your filter \"<b>%s</b>\" does not match any items.</span>"),
		app_data->filter_string);
	gtk_label_set_text (app_data->filtered_out_everything_widget_label, markup);
	gtk_label_set_use_markup (app_data->filtered_out_everything_widget_label, TRUE);
	gtk_box_pack_start (GTK_BOX (containing_vbox), app_data->filtered_out_everything_widget,
		TRUE, TRUE, 0);
Jim Krehl's avatar
Jim Krehl committed
707 708 709 710
	g_free (markup);
}

static void
711
populate_application_category_sections (AppShellData * app_data, GtkWidget * containing_vbox)
Jim Krehl's avatar
Jim Krehl committed
712 713 714
{
	GList *cat_list = app_data->categories_list;
	gboolean filtered_out_everything = TRUE;
715 716
	if (app_data->cached_tables_list)
		g_list_free (app_data->cached_tables_list);
Jim Krehl's avatar
Jim Krehl committed
717 718 719
	app_data->cached_tables_list = NULL;

	remove_container_entries (GTK_CONTAINER (containing_vbox));
720 721
	do
	{
Jim Krehl's avatar
Jim Krehl committed
722
		CategoryData *data = (CategoryData *) cat_list->data;
723 724 725 726 727 728
		if (NULL != data->filtered_launcher_list)
		{
			populate_application_category_section (app_data, data->section,
				data->filtered_launcher_list);
			gtk_box_pack_start (GTK_BOX (containing_vbox), GTK_WIDGET (data->section),
				TRUE, TRUE, 0);
Jim Krehl's avatar
Jim Krehl committed
729 730 731 732 733 734 735 736 737 738
			filtered_out_everything = FALSE;
		}
	}
	while (NULL != (cat_list = g_list_next (cat_list)));

	if (TRUE == filtered_out_everything)
		show_no_results_message (app_data, containing_vbox);
}

static void
739 740
populate_application_category_section (AppShellData * app_data, SlabSection * section,
	GList * launcher_list)
Jim Krehl's avatar
Jim Krehl committed
741 742 743 744
{
	g_assert (GTK_IS_HBOX (section->contents));
	GtkWidget *hbox = GTK_WIDGET (section->contents);

745 746 747
	GtkTable *table = gtk_container_get_children (GTK_CONTAINER (hbox))->data;
	/* Make sure our implementation has not changed and it's still a GtkTable */
	g_assert (GTK_IS_TABLE (table));
Jim Krehl's avatar
Jim Krehl committed
748

749
	app_data->cached_tables_list = g_list_append (app_data->cached_tables_list, table);
Jim Krehl's avatar
Jim Krehl committed
750

751 752
	app_resizer_layout_table_default (APP_RESIZER (app_data->category_layout), table,
		launcher_list);
Jim Krehl's avatar
Jim Krehl committed
753 754 755 756 757 758 759 760 761 762
}

gboolean
regenerate_categories (AppShellData * app_data)
{
	delete_old_data (app_data);
	generate_categories (app_data);
	create_application_category_sections (app_data);
	relayout_shell (app_data);

763
	return FALSE;	/* remove this function from the list */
Jim Krehl's avatar
Jim Krehl committed
764 765 766 767 768
}

static void
gmenu_tree_changed_callback (GMenuTree * old_tree, gpointer user_data)
{
769 770 771 772 773 774 775 776 777
	/*
	This method only gets called on the first change (gmenu appears to ignore subsequent) until
	we reget the root dir which we can't do in this method because if we do for some reason this
	method then gets called multiple times for one actual change. This actually is okay because
	it's probably a good idea to wait a couple seconds to regenerate the categories in case there
	are multiple quick changes being made, no sense regenerating multiple times.
	*/
	g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE, 3000, (GSourceFunc) regenerate_categories,
		user_data, NULL);
Jim Krehl's avatar
Jim Krehl committed
778 779 780
}

AppShellData *
781 782
appshelldata_new (const gchar * menu_name, NewAppConfig * new_apps, const gchar * gconf_keys_prefix,
	GtkIconSize icon_size)
Jim Krehl's avatar
Jim Krehl committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
{
	AppShellData *app_data = g_new0 (AppShellData, 1);
	app_data->gconf_prefix = gconf_keys_prefix;
	app_data->new_apps = new_apps;
	app_data->menu_name = menu_name;
	app_data->icon_size = icon_size;
	app_data->stop_incremental_relayout = TRUE;
	return app_data;
}

/* FIXME If there are menu items at the top level we don't see them */
void
generate_categories (AppShellData * app_data)
{
	GMenuTreeDirectory *root_dir;
	GSList *contents, *l;

800 801 802 803
	if (!app_data->tree)
	{
		app_data->tree = gmenu_tree_lookup (app_data->menu_name, GMENU_TREE_FLAGS_NONE);
		gmenu_tree_add_monitor (app_data->tree, gmenu_tree_changed_callback, app_data);
Jim Krehl's avatar
Jim Krehl committed
804 805 806
	}
	root_dir = gmenu_tree_get_root_directory (app_data->tree);
	contents = gmenu_tree_directory_get_contents (root_dir);
807 808 809 810 811
	if (!contents)
	{
		GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_DESTROY_WITH_PARENT,
			GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Failure loading - %s",
			app_data->menu_name);
Jim Krehl's avatar
Jim Krehl committed
812 813
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
814
		exit (1);	/* Fixme - is there a GNOME/GTK way to do this. */
Jim Krehl's avatar
Jim Krehl committed
815 816
	}

817 818 819 820
	for (l = contents; l; l = l->next)
	{
		if (gmenu_tree_item_get_type (l->data) == GMENU_TREE_ITEM_DIRECTORY)
		{
Jim Krehl's avatar
Jim Krehl committed
821 822 823 824 825 826
			CategoryData *data = NULL;
			const char *category;
			GList *list_entry;

			category = gmenu_tree_directory_get_name (l->data);
			list_entry =
827 828 829 830
				g_list_find_custom (app_data->categories_list, category,
				category_name_compare);
			if (!list_entry)
			{
Jim Krehl's avatar
Jim Krehl committed
831 832 833
				data = g_new0 (CategoryData, 1);
				data->category = g_strdup (category);
				app_data->categories_list =
834 835 836
					/* use the gmenu order instead of alphabetical */
					g_list_insert (app_data->categories_list, data, -1);
					/* g_list_insert_sorted (app_data->categories_list, data, category_data_compare); */
Jim Krehl's avatar
Jim Krehl committed
837
			}
838 839
			else
			{
Jim Krehl's avatar
Jim Krehl committed
840 841 842
				data = list_entry->data;
			}

843
			if (app_data->hash)	/* used to eliminate dups on a per category basis. */
Jim Krehl's avatar
Jim Krehl committed
844
				g_hash_table_destroy (app_data->hash);
845
			app_data->hash = g_hash_table_new (g_str_hash, g_str_equal);
Jim Krehl's avatar
Jim Krehl committed
846 847 848
			generate_launchers (l->data, app_data, data);
		}
	}
849 850
	if (app_data->hash)
	{
Jim Krehl's avatar
Jim Krehl committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864
		g_hash_table_destroy (app_data->hash);
		app_data->hash = NULL;
	}
	gmenu_tree_item_unref (root_dir);

	if (app_data->new_apps && (app_data->new_apps->max_items > 0))
		generate_new_apps (app_data);
}

static gboolean
check_specific_apps_hack (GnomeDesktopItem * item)
{
	static const gchar *COMMAND_LINE_LOCKDOWN_GCONF_KEY =
		"/desktop/gnome/lockdown/disable_command_line";
865
	static const gchar *COMMAND_LINE_LOCKDOWN_DESKTOP_CATEGORY = "TerminalEmulator";
Jim Krehl's avatar
Jim Krehl committed
866 867 868
	static gboolean got_lockdown_value = FALSE;
	static gboolean command_line_lockdown;

869 870
	if (!got_lockdown_value)
	{
Jim Krehl's avatar
Jim Krehl committed
871
		got_lockdown_value = TRUE;
872
		command_line_lockdown = get_slab_gconf_bool (COMMAND_LINE_LOCKDOWN_GCONF_KEY);
Jim Krehl's avatar
Jim Krehl committed
873 874
	}

875
	/* This seems like an ugly hack but it's the way it's currently done in the old control center */
Jim Krehl's avatar
Jim Krehl committed
876
	gchar *path;
877
	const char *exec = gnome_desktop_item_get_string (item, GNOME_DESKTOP_ITEM_EXEC);
Jim Krehl's avatar
Jim Krehl committed
878 879 880

	/* discard xscreensaver if gnome-screensaver is installed */
	if ((exec && !strcmp (exec, "xscreensaver-demo"))
881 882
		&& (path = g_find_program_in_path ("gnome-screensaver-preferences")))
	{
Jim Krehl's avatar
Jim Krehl committed
883 884 885 886 887 888
		g_free (path);
		return TRUE;
	}

	/* discard gnome-keyring-manager if CASA is installed */
	if ((exec && !strcmp (exec, "gnome-keyring-manager"))
889 890
		&& (path = g_find_program_in_path ("CASAManager.sh")))
	{
Jim Krehl's avatar
Jim Krehl committed
891 892 893 894 895
		g_free (path);
		return TRUE;
	}

	/* discard terminals if lockdown key is set */
896 897
	if (command_line_lockdown)
	{
Jim Krehl's avatar
Jim Krehl committed
898
		const gchar *categories =
899 900 901 902
			gnome_desktop_item_get_string (item, GNOME_DESKTOP_ITEM_CATEGORIES);
		if (g_strrstr (categories, COMMAND_LINE_LOCKDOWN_DESKTOP_CATEGORY))
		{
			/* printf ("eliminating %s\n", gnome_desktop_item_get_location (item)); */
Jim Krehl's avatar
Jim Krehl committed
903 904 905 906 907 908 909 910
			return TRUE;
		}
	}

	return FALSE;
}

static void
911
generate_launchers (GMenuTreeDirectory * root_dir, AppShellData * app_data, CategoryData * cat_data)
Jim Krehl's avatar
Jim Krehl committed
912 913 914 915 916 917
{
	GnomeDesktopItem *desktop_item;
	const gchar *desktop_file;
	GSList *contents, *l;

	contents = gmenu_tree_directory_get_contents (root_dir);
918 919
	for (l = contents; l; l = l->next)
	{
Jim Krehl's avatar
Jim Krehl committed
920 921 922
		GMenuTreeItemType item_type;

		item_type = gmenu_tree_item_get_type (l->data);
923 924
		switch (item_type)
		{
Jim Krehl's avatar
Jim Krehl committed
925
		case GMENU_TREE_ITEM_DIRECTORY:
926
			/* g_message ("Found sub-category %s", gmenu_tree_directory_get_name (l->data)); */
Jim Krehl's avatar
Jim Krehl committed
927 928 929
			generate_launchers (l->data, app_data, cat_data);
			break;
		case GMENU_TREE_ITEM_ENTRY:
930 931 932 933 934 935
			/* g_message ("Found item name is:%s", gmenu_tree_entry_get_name (l->data)); */
			desktop_file = gmenu_tree_entry_get_desktop_file_path (l->data);
			if (desktop_file)
			{
				if (g_hash_table_lookup (app_data->hash, desktop_file))
				{
Jim Krehl's avatar
Jim Krehl committed
936 937
					break;	/* duplicate */
				}
938 939 940 941 942 943
				/* Fixme - make sure it's safe to store this without duping it. As far as I can tell it is
				   safe as long as I don't hang on to this anylonger than I hang on to the GMenuTreeEntry*
				   which brings up another point - am I supposed to free these or does freeing the top level recurse
				*/
				g_hash_table_insert (app_data->hash, (gpointer) desktop_file,
					(gpointer) desktop_file);
Jim Krehl's avatar
Jim Krehl committed
944
			}
945 946 947 948 949
			desktop_item = gnome_desktop_item_new_from_file (desktop_file, 0, NULL);
			if (!desktop_item)
			{
				gchar *error_msg = g_strdup_printf("Failure - gnome_desktop_item_new_from_file(%s)",
					desktop_file);
Jim Krehl's avatar
Jim Krehl committed
950 951 952 953 954
				g_critical (error_msg);
				g_free (error_msg);
				break;
			}
			if (!check_specific_apps_hack (desktop_item))
955
				insert_launcher_into_category (cat_data, desktop_item, app_data);
Jim Krehl's avatar
Jim Krehl committed
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
			gnome_desktop_item_unref (desktop_item);
			break;
		default:
			break;
		}
	}
}

static void
generate_new_apps (AppShellData * app_data)
{
	GHashTable *all_apps_cache = NULL;
	gchar *all_apps;
	GError *error = NULL;
	gchar *separator = "\n";
971
	gchar *gconf_key;
Jim Krehl's avatar
Jim Krehl committed
972

973 974 975 976 977
	gconf_key = g_strdup_printf ("%s%s", app_data->gconf_prefix, NEW_APPS_FILE_KEY);
	gchar *basename = get_slab_gconf_string (gconf_key);
	g_free (gconf_key);
	if (!basename)
	{
Jim Krehl's avatar
Jim Krehl committed
978 979 980 981
		g_warning ("Failure getting gconf key NEW_APPS_FILE_KEY");
		return;
	}

982
	gchar *all_apps_file_name = g_build_filename (g_get_home_dir (), basename, NULL);
Jim Krehl's avatar
Jim Krehl committed
983 984
	g_free (basename);

985 986 987
	if (!g_file_get_contents (all_apps_file_name, &all_apps, NULL, &error))
	{
		/* If file does not exist, this is the first time this user has run this, create the baseline file */
Jim Krehl's avatar
Jim Krehl committed
988 989 990 991
		g_error_free (error);
		error = NULL;
		GList *categories, *launchers;

992 993 994 995 996 997 998 999
		/* best initial size determined by running on a couple different platforms */
		GString *gstr = g_string_sized_new (10000);

		for (categories = app_data->categories_list; categories; categories = categories->next)
		{
			CategoryData *data = categories->data;
			for (launchers = data->launcher_list; launchers; launchers = launchers->next)
			{
Jim Krehl's avatar
Jim Krehl committed
1000 1001
				Tile *tile = TILE (launchers->data);
				GnomeDesktopItem *item =
1002 1003
					application_tile_get_desktop_item (APPLICATION_TILE (tile));
				const gchar *uri = gnome_desktop_item_get_location (item);
Jim Krehl's avatar
Jim Krehl committed
1004 1005 1006 1007 1008 1009
				g_string_append (gstr, uri);
				g_string_append (gstr, separator);
			}
		}

		gchar *dirname = g_path_get_dirname (all_apps_file_name);
1010
		g_mkdir_with_parents (dirname, 0700);	/* creates if does not exist */
Jim Krehl's avatar
Jim Krehl committed
1011 1012
		g_free (dirname);

1013 1014
		if (!g_file_set_contents (all_apps_file_name, gstr->str, -1, &error))
			g_warning ("Error setting all apps file:%s\n", error->message);
Jim Krehl's avatar
Jim Krehl committed
1015 1016 1017 1018 1019 1020 1021 1022 1023

		g_string_free (gstr, TRUE);
		g_free (all_apps_file_name);
		return;
	}

	all_apps_cache = g_hash_table_new (g_str_hash, g_str_equal);
	gchar **all_apps_split = g_strsplit (all_apps, separator, -1);
	gint x;
1024 1025 1026
	for (x = 0; all_apps_split[x]; x++)
	{
		g_hash_table_insert (all_apps_cache, all_apps_split[x], all_apps_split[x]);
Jim Krehl's avatar
Jim Krehl committed
1027 1028 1029 1030 1031
	}

	gboolean got_new_apps = FALSE;
	CategoryData *new_apps_category = NULL;
	GList *categories, *launchers;
1032 1033 1034
	GHashTable *new_apps_dups = g_hash_table_new (g_str_hash, g_str_equal);
	for (categories = app_data->categories_list; categories; categories = categories->next)
	{
Jim Krehl's avatar
Jim Krehl committed
1035
		CategoryData *cat_data = categories->data;
1036 1037
		for (launchers = cat_data->launcher_list; launchers; launchers = launchers->next)
		{
Jim Krehl's avatar
Jim Krehl committed
1038 1039
			Tile *tile = TILE (launchers->data);
			GnomeDesktopItem *item =
1040 1041 1042 1043 1044 1045 1046 1047
				application_tile_get_desktop_item (APPLICATION_TILE (tile));
			const gchar *uri = gnome_desktop_item_get_location (item);
			if (!g_hash_table_lookup (all_apps_cache, uri))
			{
				if (g_hash_table_lookup (new_apps_dups, uri))
				{
					/* if a desktop file is in 2 or more top level categories, only show it once */
					/* printf("Discarding Newapp duplicate:%s\n", uri); */
Jim Krehl's avatar
Jim Krehl committed
1048 1049
					break;
				}
1050
				g_hash_table_insert (new_apps_dups, (gpointer) uri, (gpointer) uri);
Jim Krehl's avatar
Jim Krehl committed
1051

1052 1053 1054
				if (!got_new_apps)
				{
					new_apps_category = g_new0 (CategoryData, 1);
Jim Krehl's avatar
Jim Krehl committed
1055
					new_apps_category->category =
1056
						g_strdup (app_data->new_apps->name);
Jim Krehl's avatar
Jim Krehl committed
1057
					app_data->new_apps->garray =
1058 1059 1060 1061 1062 1063
						g_array_sized_new (FALSE, TRUE,
						sizeof (NewAppData *),
						app_data->new_apps->max_items);

					/* should not need this, but a bug in glib does not actually clear the elements until you call this method */
					g_array_set_size (app_data->new_apps->garray, app_data->new_apps->max_items);
Jim Krehl's avatar
Jim Krehl committed
1064 1065 1066
					got_new_apps = TRUE;
				}

1067 1068 1069 1070 1071
				GnomeVFSFileInfo *info = gnome_vfs_file_info_new ();
				if (gnome_vfs_get_file_info (uri, info,
						GNOME_VFS_FILE_INFO_DEFAULT) != GNOME_VFS_OK
					|| !(info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MTIME))
				{
Jim Krehl's avatar
Jim Krehl committed
1072
					gnome_vfs_file_info_unref (info);
1073
					g_warning ("Cant get vfs info for %s\n", uri);
Jim Krehl's avatar
Jim Krehl committed
1074 1075 1076 1077 1078
					return;
				}
				long filetime = info->mtime;
				gnome_vfs_file_info_unref (info);

1079 1080 1081 1082 1083 1084 1085
				for (x = 0; x < app_data->new_apps->max_items; x++)
				{
					NewAppData *temp_data = (NewAppData *)
						g_array_index (app_data->new_apps->garray, NewAppData *, x);
					if (!temp_data || filetime > temp_data->time)	/* if this slot is empty or we are newer than this slot */
					{
						NewAppData *temp = g_new0 (NewAppData, 1);
Jim Krehl's avatar
Jim Krehl committed
1086 1087
						temp->time = filetime;
						temp->item = item;
1088 1089
						g_array_insert_val (app_data->new_apps->garray, x,
							temp);
Jim Krehl's avatar
Jim Krehl committed
1090 1091 1092 1093 1094 1095 1096 1097 1098
						break;
					}
				}
			}
		}
	}
	g_hash_table_destroy (new_apps_dups);
	g_hash_table_destroy (all_apps_cache);

1099 1100 1101 1102
	if (got_new_apps)
	{
		for (x = 0; x < app_data->new_apps->max_items; x++)
		{
Jim Krehl's avatar
Jim Krehl committed
1103
			NewAppData *data =
1104 1105 1106 1107 1108 1109 1110
				(NewAppData *) g_array_index (app_data->new_apps->garray,
				NewAppData *, x);
			if (data)
			{
				insert_launcher_into_category (new_apps_category, data->item,
					app_data);
				g_free (data);
Jim Krehl's avatar
Jim Krehl committed
1111 1112 1113 1114 1115
			}
			else
				break;
		}
		app_data->categories_list =
1116 1117 1118
			g_list_prepend (app_data->categories_list, new_apps_category);

		g_array_free (app_data->new_apps->garray, TRUE);
Jim Krehl's avatar
Jim Krehl committed
1119 1120 1121 1122 1123 1124 1125
	}
	g_free (all_apps);
	g_free (all_apps_file_name);
	g_strfreev (all_apps_split);
}

static void
1126 1127
insert_launcher_into_category (CategoryData * cat_data, GnomeDesktopItem * desktop_item,
	AppShellData * app_data)
Jim Krehl's avatar
Jim Krehl committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
{
	GtkWidget *launcher;
	static GtkSizeGroup *icon_group = NULL;

	GtkWidget *tile_icon;

	if (!icon_group)
		icon_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);

	launcher =
1138 1139
		application_tile_new_full (gnome_desktop_item_get_location (desktop_item),
		app_data->icon_size);
Jim Krehl's avatar
Jim Krehl committed
1140 1141 1142
	gtk_widget_set_size_request (launcher, SIZING_TILE_WIDTH, -1);

	gchar *filepath =
1143 1144
		g_strdup (gnome_desktop_item_get_string (desktop_item, GNOME_DESKTOP_ITEM_EXEC));
	g_strdelimit (filepath, " ", '\0');	/* just want the file name - no args or replacements */
Jim Krehl's avatar
Jim Krehl committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	gchar *filename = g_strrstr (filepath, "/");
	if (filename)
		g_stpcpy (filepath, filename + 1);
	filename = g_ascii_strdown (filepath, -1);
	g_free (filepath);
	g_object_set_data (G_OBJECT (launcher), TILE_EXEC_NAME, filename);

	tile_icon = NAMEPLATE_TILE (launcher)->image;
	gtk_size_group_add_widget (icon_group, tile_icon);

1155
	g_signal_connect (launcher, "tile-activated", G_CALLBACK (tile_activated_cb), app_data);
Jim Krehl's avatar
Jim Krehl committed
1156

1157 1158 1159 1160
	/* Note that this will handle the case of the action being launched via the side panel as
	   well as directly from the context menu of an individual launcher, because they both
	   funnel through tile_button_action_activate.
	*/
Jim Krehl's avatar
Jim Krehl committed
1161
	g_signal_connect (launcher, "tile-action-triggered",
1162
		G_CALLBACK (handle_menu_action_performed), app_data);
Jim Krehl's avatar
Jim Krehl committed
1163

1164
	/* These will be inserted/removed from tables as the filter changes and we dont want them */
Jim Krehl's avatar
Jim Krehl committed
1165 1166 1167
	/* destroyed when they are removed */
	g_object_ref (launcher);

1168 1169
	/* use alphabetical order instead of the gmenu order. We group all sub items in each top level
	category together, ignoring sub menus, so we also ignore sub menu layout hints */
Jim Krehl's avatar
Jim Krehl committed
1170
	cat_data->launcher_list =
1171 1172
		/* g_list_insert (cat_data->launcher_list, launcher, -1); */
		g_list_insert_sorted (cat_data->launcher_list, launcher, application_launcher_compare);
Jim Krehl's avatar
Jim Krehl committed
1173
	cat_data->filtered_launcher_list =
1174 1175
		/* g_list_insert (cat_data->filtered_launcher_list, launcher, -1); */
		g_list_insert_sorted (cat_data->filtered_launcher_list, launcher, application_launcher_compare);
Jim Krehl's avatar
Jim Krehl committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
}

static gint
application_launcher_compare (gconstpointer a, gconstpointer b)
{
	ApplicationTile *launcher1 = APPLICATION_TILE (a);
	ApplicationTile *launcher2 = APPLICATION_TILE (b);

	gchar *val1 = launcher1->name;
	gchar *val2 = launcher2->name;

1187 1188
	if (val1 == NULL || val2 == NULL)
	{
Jim Krehl's avatar
Jim Krehl committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
		g_assert_not_reached ();
	}
	return g_ascii_strcasecmp (val1, val2);
}

static gint
category_name_compare (gconstpointer a, gconstpointer b)
{
	CategoryData *data = (CategoryData *) a;
	const gchar *category = b;

1200 1201
	if (category == NULL || data->category == NULL)
	{
Jim Krehl's avatar
Jim Krehl committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
		g_assert_not_reached ();
	}
	return g_ascii_strcasecmp (category, data->category);
}

static gint
category_data_compare (gconstpointer a, gconstpointer b)
{
	CategoryData *data1 = (CategoryData *) a;
	CategoryData *data2 = (CategoryData *) b;

1213 1214
	if (data1->category == NULL || data2->category == NULL)
	{
Jim Krehl's avatar
Jim Krehl committed
1215 1216 1217 1218 1219 1220 1221 1222
		g_assert_not_reached ();
	}
	return g_ascii_strcasecmp (data1->category, data2->category);
}

static void
tile_activated_cb (Tile * tile, TileEvent * event, gpointer user_data)
{
1223 1224
	switch (event->type)
	{
Jim Krehl's avatar
Jim Krehl committed
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
	case TILE_EVENT_ACTIVATED_SINGLE_CLICK:
	case TILE_EVENT_ACTIVATED_KEYBOARD:
		handle_launcher_single_clicked (tile, user_data);
		break;
	default:
		break;
	}

}

static void
handle_launcher_single_clicked (Tile * launcher, gpointer data)
{
1238
	tile_trigger_action (launcher, launcher->actions[APPLICATION_TILE_ACTION_START]);
Jim Krehl's avatar
Jim Krehl committed
1239 1240 1241 1242

	AppShellData *app_data = (AppShellData *) data;
	gchar *gconf_key;

1243
	gconf_key = g_strdup_printf ("%s%s", app_data->gconf_prefix, EXIT_SHELL_ON_ACTION_START);
Jim Krehl's avatar
Jim Krehl committed
1244 1245 1246 1247 1248 1249
	if (get_slab_gconf_bool (gconf_key))
		hide_shell (app_data);
	g_free (gconf_key);
}

static void
1250 1251
handle_menu_action_performed (Tile * launcher, TileEvent * event, TileAction * action,
	gpointer data)
Jim Krehl's avatar
Jim Krehl committed
1252 1253
{
	AppShellData *app_data = (AppShellData *) data;
1254
	gchar *temp;
Jim Krehl's avatar
Jim Krehl committed
1255

1256 1257 1258 1259
	temp = NULL;
	if (action == launcher->actions[APPLICATION_TILE_ACTION_START])
	{
		temp = g_strdup_printf ("%s%s", app_data->gconf_prefix, EXIT_SHELL_ON_ACTION_START);
Jim Krehl's avatar
Jim Krehl committed
1260 1261
	}

1262 1263 1264
	else if (action == launcher->actions[APPLICATION_TILE_ACTION_HELP])
	{
		temp = g_strdup_printf ("%s%s", app_data->gconf_prefix, EXIT_SHELL_ON_ACTION_HELP);
Jim Krehl's avatar
Jim Krehl committed
1265 1266
	}

1267 1268 1269 1270 1271
	else if (action == launcher->actions[APPLICATION_TILE_ACTION_UPDATE_MAIN_MENU]
		|| action == launcher->actions[APPLICATION_TILE_ACTION_UPDATE_STARTUP])
	{
		temp = g_strdup_printf ("%s%s", app_data->gconf_prefix,
			EXIT_SHELL_ON_ACTION_ADD_REMOVE);
Jim Krehl's avatar
Jim Krehl committed
1272 1273
	}

1274 1275 1276 1277 1278
	else if (action == launcher->actions[APPLICATION_TILE_ACTION_UPGRADE_PACKAGE]
		|| action == launcher->actions[APPLICATION_TILE_ACTION_UNINSTALL_PACKAGE])
	{
		temp = g_strdup_printf ("%s%s", app_data->gconf_prefix,
			EXIT_SHELL_ON_ACTION_UPGRADE_UNINSTALL);
Jim Krehl's avatar
Jim Krehl committed
1279 1280
	}

1281 1282 1283 1284 1285 1286
	if (temp)
	{
		if (get_slab_gconf_bool (temp))
			hide_shell (app_data);
		g_free (temp);
	}
Jim Krehl's avatar
Jim Krehl committed
1287 1288 1289
	else
		g_warning ("Unknown Action");
}