launcher.c 10 KB
Newer Older
1 2 3 4 5 6
/*
 * GNOME panel launcher module.
 * (C) 1997 The Free Software Foundation
 *
 * Authors: Miguel de Icaza
 *          Federico Mena
7
 * CORBAized by George Lebl
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
8
 * de-CORBAized by George Lebl
9 10 11 12 13 14 15 16 17
 */

#include <config.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include "gnome.h"
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
18
#include "panel.h"
19
#include "panel-util.h"
20

21 22
#define LAUNCHER_PROPERTIES "launcher_properties"

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
23
extern GtkTooltips *panel_tooltips;
24

25
static char *default_app_pixmap=NULL;
26 27

typedef struct {
28
	int                applet_id;
29
	GtkWidget         *button;
George Lebl's avatar
George Lebl committed
30
	int               signal_click_tag;
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
	GnomeDesktopEntry *dentry;
} Launcher;

typedef struct {
	GtkWidget *dialog;

	GtkWidget *name_entry;
	GtkWidget *comment_entry;
	GtkWidget *execute_entry;
	GtkWidget *icon_entry;
	GtkWidget *documentation_entry;
	GtkWidget *terminal_toggle;

	/*information about this launcher*/
	Launcher *launcher;

	GnomeDesktopEntry *dentry;
} Properties;

static void
launch (GtkWidget *widget, void *data)
{
	GnomeDesktopEntry *item = data;

	gnome_desktop_entry_launch (item);
}

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
58 59
Launcher *
create_launcher (char *parameters)
60 61 62 63 64 65
{
	GtkWidget *pixmap;
	GnomeDesktopEntry *dentry;
	Launcher *launcher;

	if (!default_app_pixmap)
66
		default_app_pixmap = gnome_pixmap_file ("gnome-unknown.png");
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

	if (*parameters == '/')
		dentry = gnome_desktop_entry_load (parameters);
	else {
		char *apps_par, *entry, *extension;

		if (strstr (parameters, ".desktop"))
			extension = NULL;
		else
			extension = ".desktop";
		
		apps_par = g_copy_strings ("apps/", parameters, extension, NULL);
		entry = gnome_datadir_file (apps_par);
		g_free (apps_par);
		
		if (!entry)
			return NULL;
		dentry = gnome_desktop_entry_load (entry);
		g_free (entry);
	}
	if (!dentry)
		return NULL; /*button is null*/

	launcher = g_new(Launcher,1);

	launcher->button = gtk_button_new ();
93 94 95 96 97
	pixmap = NULL;
	if(dentry->icon)
		pixmap = gnome_pixmap_new_from_file (dentry->icon);
	else
		pixmap = NULL;
98 99 100 101
	if (!pixmap) {
		if (default_app_pixmap)
			pixmap = gnome_pixmap_new_from_file (default_app_pixmap);
		else
102
			pixmap = gtk_label_new (_("App"));
103 104 105
	}
	gtk_container_add (GTK_CONTAINER(launcher->button), pixmap);
	gtk_widget_show (pixmap);
106 107 108 109 110 111
	/*gtk_widget_set_usize (launcher->button, pixmap->requisition.width,
			      pixmap->requisition.height);*/
	/*FIXME: we'll do this better alter, but this makes it look fine with
	  normal icons*/
	gtk_widget_set_usize (launcher->button, 48, 48);
	
112 113
	gtk_widget_show (launcher->button);

114 115
	launcher->signal_click_tag = gtk_signal_connect (GTK_OBJECT(launcher->button),
							 "clicked",
116 117 118 119 120 121 122
							 (GtkSignalFunc) launch,
							 dentry);

	gtk_object_set_user_data(GTK_OBJECT(launcher->button), launcher);

	launcher->dentry = dentry;

123 124
	launcher->applet_id = -1;

125 126 127
	gtk_object_set_data(GTK_OBJECT(launcher->button),
			    LAUNCHER_PROPERTIES,NULL);

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 159 160 161
	return launcher;
}

static void
check_dentry_save(GnomeDesktopEntry *dentry)
{
	FILE *file;
	char *pruned;
	char *new_name;
	char *appsdir;

	file = fopen(dentry->location, "w");
	if (file) {
		fclose(file);
		return;
	}

	pruned = strrchr(dentry->location, '/');
	if (pruned)
		pruned++; /* skip over slash */
	else
		pruned = dentry->location;

	appsdir = gnome_util_home_file ("apps");
	mkdir (appsdir, 0755);

	new_name = g_concat_dir_and_file(appsdir, pruned);
	g_free(dentry->location);
	dentry->location = new_name;
}

#define free_and_nullify(x) { g_free(x); x = NULL; }

static void
162
properties_apply_callback(GtkWidget *widget, int page, gpointer data)
163 164 165 166
{
	Properties        *prop;
	GnomeDesktopEntry *dentry;
	GtkWidget         *pixmap;
167 168
	int i, n_args;
	char **exec;
169

170
	if (page != -1)
171 172
		return;
	
173 174 175 176 177
	prop = data;
	dentry = prop->dentry;

	free_and_nullify(dentry->name);
	free_and_nullify(dentry->comment);
178 179
	gnome_string_array_free (dentry->exec);
	dentry->exec = NULL;
180 181
	free_and_nullify(dentry->exec);
	free_and_nullify(dentry->tryexec);
182
	free_and_nullify(dentry->icon);
183 184 185 186 187
	free_and_nullify(dentry->docpath);
	free_and_nullify(dentry->type);

	dentry->name      = g_strdup(gtk_entry_get_text(GTK_ENTRY(prop->name_entry)));
	dentry->comment   = g_strdup(gtk_entry_get_text(GTK_ENTRY(prop->comment_entry)));
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	/* Handle exec specially: split at spaces.  Multiple spaces
	   must be compacted, hence the weirdness.  This
	   implementation wastes a little memory in some cases.  Big
	   deal.  */
	exec = gnome_string_split (gtk_entry_get_text(GTK_ENTRY(prop->execute_entry)),
				   " ", -1);
	for (n_args = i = 0; exec[i]; ++i) {
		if (! exec[i][0]) {
			/* Empty item means multiple spaces found.  Remove
			   it.  */
			g_free (exec[i]);
		} else {
			exec[n_args++] = exec[i];
		}
	}
	exec[n_args] = NULL;
	dentry->exec_length = n_args;
	dentry->exec = exec;

208
	dentry->icon      = g_strdup(gtk_entry_get_text(GTK_ENTRY(prop->icon_entry)));
209 210 211 212 213 214 215 216
	dentry->docpath   = g_strdup(gtk_entry_get_text(GTK_ENTRY(prop->documentation_entry)));
	dentry->type      = g_strdup("Application"); /* FIXME: should handle more cases */
	dentry->terminal  = GTK_TOGGLE_BUTTON(prop->terminal_toggle)->active;

	check_dentry_save(dentry);
	gnome_desktop_entry_save(dentry);

	dentry=gnome_desktop_entry_load(dentry->location);
217

218 219 220
	gnome_desktop_entry_free(prop->dentry);
	prop->dentry = dentry;

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
221 222
	gtk_tooltips_set_tip (panel_tooltips,prop->launcher->button->parent,
			      dentry->comment,NULL);
223 224 225 226 227
	
	pixmap=GTK_BUTTON(prop->launcher->button)->child;

	gtk_container_remove(GTK_CONTAINER(prop->launcher->button),pixmap);

228
	pixmap = gnome_pixmap_new_from_file (dentry->icon);
229 230 231 232 233 234 235
	if (!pixmap) {
		if (default_app_pixmap)
			pixmap = gnome_pixmap_new_from_file (default_app_pixmap);
		else
			pixmap = gtk_label_new (_("App"));
	}
	gtk_container_add (GTK_CONTAINER(prop->launcher->button), pixmap);
236
	gtk_widget_show(pixmap);
237

238 239 240 241 242
	/*FIXME: a bad hack to keep it all 48x48*/
	gtk_widget_set_usize (prop->launcher->button, 48, 48);

	/*gtk_widget_set_usize (prop->launcher->button, pixmap->requisition.width,
			      pixmap->requisition.height);*/
243 244 245 246 247 248 249 250 251 252

	gtk_signal_disconnect(GTK_OBJECT(prop->launcher->button),
			      prop->launcher->signal_click_tag);

	prop->launcher->signal_click_tag = gtk_signal_connect (GTK_OBJECT(prop->launcher->button), "clicked",
							       (GtkSignalFunc) launch,
							       dentry);

	/*replace the dentry in launcher structure with the new one */
	gnome_desktop_entry_free(prop->launcher->dentry);
253

254 255 256 257 258
	prop->launcher->dentry=dentry;
}

#undef free_and_nullify

George Lebl's avatar
George Lebl committed
259
static int
260
properties_close_callback(GtkWidget *widget, gpointer data)
261
{
262 263 264
	Properties *prop = data;
	gtk_object_set_data(GTK_OBJECT(prop->launcher->button),
			    LAUNCHER_PROPERTIES,NULL);
265 266 267 268 269 270
	gtk_signal_disconnect_by_data(GTK_OBJECT(prop->name_entry),widget);
	gtk_signal_disconnect_by_data(GTK_OBJECT(prop->comment_entry),widget);
	gtk_signal_disconnect_by_data(GTK_OBJECT(prop->execute_entry),widget);
	gtk_signal_disconnect_by_data(GTK_OBJECT(prop->icon_entry),widget);
	gtk_signal_disconnect_by_data(GTK_OBJECT(prop->documentation_entry),widget);
	gtk_signal_disconnect_by_data(GTK_OBJECT(prop->terminal_toggle),widget);
271
	g_free (prop);
272
	return FALSE;
273 274
}

275 276 277 278 279 280 281 282
static void
notify_entry_change (GtkWidget *widget, void *data)
{
	GnomePropertyBox *box = GNOME_PROPERTY_BOX (data);

	gnome_property_box_changed (box);
}

283 284 285 286 287 288 289
static GtkWidget *
create_properties_dialog(GnomeDesktopEntry *dentry, Launcher *launcher)
{
	Properties *prop;
	GtkWidget  *dialog;
	GtkWidget  *table;
	GtkWidget  *toggle;
George Lebl's avatar
George Lebl committed
290
	char *exec;
291 292 293 294 295 296

	prop = g_new(Properties, 1);
	prop->dentry = dentry;

	prop->launcher = launcher;

297
	prop->dialog = dialog = gnome_property_box_new();
298 299 300 301 302 303 304 305 306
	gtk_window_set_title(GTK_WINDOW(dialog), _("Launcher properties"));
	gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
	gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, TRUE);

	table = gtk_table_new(6, 2, FALSE);
	gtk_container_border_width(GTK_CONTAINER(table), 4);
	gtk_table_set_col_spacings(GTK_TABLE(table), 6);
	gtk_table_set_row_spacings(GTK_TABLE(table), 2);

307 308 309 310 311 312 313 314
	prop->name_entry          = create_text_entry(table,
						      "launcher_name", 0,
						      _("Name"), dentry->name,
						      dialog);
	prop->comment_entry       = create_text_entry(table,
						      "launcher_comment", 1,
						      _("Comment"),
						      dentry->comment, dialog);
315
	exec = gnome_string_joinv (" ", dentry->exec);
316 317 318 319
	prop->execute_entry       = create_file_entry(table,
						      "execute", 2,
						      _("Execute"), exec,
						      dialog);
320
	g_free (exec);
321 322 323 324 325 326 327 328
	prop->icon_entry          = create_file_entry(table,
						      "icon", 3,
						      _("Icon"), dentry->icon,
						      dialog);
	prop->documentation_entry = create_text_entry(table,
						      "launcher_document", 4,
						      _("Documentation"),
						      dentry->docpath, dialog);
329 330 331 332 333

	prop->terminal_toggle = toggle =
		gtk_check_button_new_with_label(_("Run inside terminal"));
	gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(toggle),
				    dentry->terminal ? TRUE : FALSE);
334 335 336
	gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
			    GTK_SIGNAL_FUNC (notify_entry_change), dialog);
				    
337
	gtk_table_attach(GTK_TABLE(table), toggle,
338
			 0, 2, 5, 6,
339 340 341 342
			 GTK_EXPAND | GTK_FILL | GTK_SHRINK,
			 GTK_FILL | GTK_SHRINK,
			 0, 0);

343
	gnome_property_box_append_page (GNOME_PROPERTY_BOX (prop->dialog),
344
					table, gtk_label_new (_("Item properties")));
345
	
346
	gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
347
			   (GtkSignalFunc) properties_close_callback,
348 349
			   prop);

350 351 352
	gtk_signal_connect(GTK_OBJECT(dialog), "apply",
			   GTK_SIGNAL_FUNC(properties_apply_callback), prop);

353 354 355
	return dialog;
}

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
356 357
void
launcher_properties(Launcher *launcher)
358 359 360 361 362
{
	GnomeDesktopEntry *dentry;
	char              *path;
	GtkWidget         *dialog;

363 364 365 366 367 368 369
	dialog = gtk_object_get_data(GTK_OBJECT(launcher->button),
				     LAUNCHER_PROPERTIES);
	if(dialog) {
		gdk_window_raise(dialog->window);
		return;
	}

370
	path = launcher->dentry->location;
371 372 373

	dentry = gnome_desktop_entry_load(path);
	if (!dentry) {
374 375 376
		g_warning("launcher properties: oops, "
			  "gnome_desktop_entry_load() returned NULL\n"
			  "                     on \"%s\"\n", path);
377 378 379 380
		return;
	}

	dialog = create_properties_dialog(dentry,launcher);
381 382
	gtk_object_set_data(GTK_OBJECT(launcher->button),
			    LAUNCHER_PROPERTIES,dialog);
383
	gtk_widget_show_all (dialog);
384
}