summaryrefslogtreecommitdiff
path: root/mate-panel/mate-desktop-item-edit.c
blob: 4860cf84715963a99a9d9e98929e7e70738c1eac (plain)
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
59
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
141
142
143
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
#include "config.h"

#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gtk/gtk.h>

#include <libpanel-util/panel-error.h>
#include <libpanel-util/panel-keyfile.h>

#include "panel-ditem-editor.h"
#include "panel-icon-names.h"
#include "panel-util.h"

/* FIXME Symbols needed by panel-util.c - sucky */
#include "applet.h"
GSList *mate_panel_applet_list_applets (void) { return NULL; }
#include "panel-config-global.h"
gboolean panel_global_config_get_tooltips_enabled (void) { return FALSE; }
#include "panel-lockdown.h"
gboolean panel_lockdown_get_disable_lock_screen (void) { return FALSE; }

static int dialogs = 0;
static gboolean create_new = FALSE;
static char **desktops = NULL;

static GOptionEntry options[] = {
	{ "create-new", 0, 0, G_OPTION_ARG_NONE, &create_new, N_("Create new file in the given directory"), NULL },
	{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &desktops, NULL, N_("[FILE...]") },
	{ NULL }
};

static void
dialog_destroyed (GtkWidget *dialog, gpointer data)
{
	dialogs --;

	if (dialogs <= 0)
		gtk_main_quit ();
}

static void
validate_for_filename (char *file)
{
	char *ptr;

	g_return_if_fail (file != NULL);
	
	ptr = file;
	while (*ptr != '\0') {
		if (*ptr == '/')
			*ptr = '_';
		ptr++;
	}
}

static char *
find_uri_on_save (PanelDItemEditor *dialog,
		  gpointer          data)
{
	GKeyFile *keyfile;
	char     *name;
	char     *filename;
	char     *uri;
	char     *dir;

	keyfile = panel_ditem_editor_get_key_file (dialog);
	name = panel_key_file_get_string (keyfile, "Name");

	validate_for_filename (name);
	filename = g_filename_from_utf8 (name, -1, NULL, NULL, NULL);
	g_free (name);

	if (filename == NULL)
		filename = g_strdup ("foo");

	dir = g_object_get_data (G_OBJECT (dialog), "dir");
	uri = panel_make_unique_desktop_path_from_name (dir, filename);

	g_free (filename);

	return uri;
}

static void
error_reported (GtkWidget  *dialog,
		const char *primary,
		const char *secondary,
		gpointer    data)
{
	panel_error_dialog (GTK_WINDOW (dialog), NULL,
			    "error_editing_launcher", TRUE,
			    primary, secondary);
}

int
main (int argc, char * argv[])
{
	GError *error = NULL;
	int i;

	bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	if (!gtk_init_with_args (&argc, &argv,
	                         _("- Edit .desktop files"),
	                         options,
	                         GETTEXT_PACKAGE,
	                         &error)) {
		g_printerr ("%s\n", error->message);
		g_error_free (error);
		return 1;
	}

	gtk_window_set_default_icon_name (PANEL_ICON_LAUNCHER);

	if (desktops == NULL ||
	    desktops[0] == NULL) {
		g_printerr ("mate-desktop-item-edit: no file to edit\n");
		return 0;
	}

	for (i = 0; desktops[i] != NULL; i++) {
		GFile     *file;
		GFileInfo *info;
		GFileType  type;
		char      *uri;
		char      *path;
		GtkWidget *dlg = NULL;

		file = g_file_new_for_commandline_arg (desktops[i]);
		uri  = g_file_get_uri (file);
		path = g_file_get_path (file);
		info = g_file_query_info (file, "standard::type",
					  G_FILE_QUERY_INFO_NONE, NULL, NULL);
		g_object_unref (file);

		if (info) {
			type = g_file_info_get_file_type (info);

			if (type == G_FILE_TYPE_DIRECTORY && create_new) {

				dlg = panel_ditem_editor_new (NULL, NULL, NULL,
							     _("Create Launcher"));
				g_object_set_data_full (G_OBJECT (dlg), "dir",
							g_strdup (path),
							(GDestroyNotify)g_free);

				panel_ditem_register_save_uri_func (PANEL_DITEM_EDITOR (dlg),
								    find_uri_on_save,
								    NULL);

			} else if (type == G_FILE_TYPE_DIRECTORY) {
				/* Rerun this iteration with the .directory
				 * file
				 * Note: No need to free, for one we can't free
				 * an individual member of desktops and
				 * secondly we will soon exit */
			        desktops[i] = g_build_path ("/", uri,
							    ".directory", NULL);
				i--;
			} else if (type == G_FILE_TYPE_REGULAR
				   && g_str_has_suffix (desktops [i],
					   		".directory")
				   && !create_new) {
				dlg = panel_ditem_editor_new_directory (NULL,
									NULL,
									uri,
									_("Directory Properties"));
			} else if (type == G_FILE_TYPE_REGULAR
				   && g_str_has_suffix (desktops [i],
					   		".desktop")
				   && !create_new) {
				dlg = panel_ditem_editor_new (NULL, NULL, uri,
							      _("Launcher Properties"));
			} else if (type == G_FILE_TYPE_REGULAR
				   && create_new) {
				g_printerr ("mate-desktop-item-edit: %s "
					    "already exists\n", uri);
			} else {
				g_printerr ("mate-desktop-item-edit: %s "
					    "does not look like a desktop "
					    "item\n", uri);
			}

			g_object_unref (info);

		} else if (g_str_has_suffix (desktops [i], ".directory")) {
			/* a non-existant file.  Well we can still edit that
			 * sort of.  We will just create it new */
			dlg = panel_ditem_editor_new_directory (NULL, NULL, uri,
								_("Directory Properties"));

		} else if (g_str_has_suffix (desktops [i], ".desktop")) {
			/* a non-existant file.  Well we can still edit that
			 * sort of.  We will just create it new */
			dlg = panel_ditem_editor_new (NULL, NULL, uri,
						      _("Create Launcher"));

		} else {
			g_printerr ("mate-desktop-item-edit: %s does not "
				    "have a .desktop or .directory "
				    "suffix\n", uri);
		}

		if (dlg != NULL) {
			dialogs ++;
			g_signal_connect (G_OBJECT (dlg), "destroy",
					  G_CALLBACK (dialog_destroyed), NULL);
			g_signal_connect (G_OBJECT (dlg), "error_reported",
					  G_CALLBACK (error_reported), NULL);
			gtk_widget_show (dlg);
		}

		g_free (uri);
		g_free (path);
	}

	if (dialogs > 0)
		gtk_main ();

        return 0;
}