summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-customization-data.c
blob: 0f8e38cf9edb817a51580f61246c3908578d2303 (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
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/*
 * Nautilus
 *
 * Copyright (C) 2000 Eazel, Inc.
 *
 * Nautilus 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.
 *
 * Nautilus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Rebecca Schulman <rebecka@eazel.com>
 */

/* nautilus-customization-data.c - functions to collect and load customization
   names and imges */

#include <ctype.h>

#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtk.h>
#include <libgnomevfs/gnome-vfs.h>
#include <string.h>

#include "nautilus-customization-data.h"
#include "nautilus-gdk-extensions.h"
#include "nautilus-gdk-pixbuf-extensions.h"
#include "nautilus-file-utilities.h"
#include "nautilus-gdk-extensions.h"
#include "nautilus-gtk-extensions.h"
#include "nautilus-image.h"
#include "nautilus-label.h"
#include "nautilus-string.h"

typedef enum {
	READ_PUBLIC_CUSTOMIZATIONS,
	READ_PRIVATE_CUSTOMIZATIONS
} CustomizationReadingMode;

struct NautilusCustomizationData {
	char *customization_name;
	CustomizationReadingMode reading_mode;

	GnomeVFSDirectoryList *public_file_list;	
	GnomeVFSDirectoryList *private_file_list;
	GnomeVFSDirectoryList *current_file_list;

	GdkPixbuf *background_frame;

	gboolean started_reading_current_file_list;
	gboolean private_data_was_displayed;
	gboolean data_is_for_a_menu;
	int maximum_icon_height;
	int maximum_icon_width;
};


/* The Property here should be one of "emblems", "colors" or "backgrounds" */
static char *            get_global_customization_uri        (const char *customization_name);
static char *            get_private_customization_uri       (const char *customization_name);
static char *            get_file_path_for_mode              (const NautilusCustomizationData *data,
							      const char *file_name);
static char*             format_name_for_display             (const char *name);
static char*             strip_extension                     (const char* string_to_strip);

NautilusCustomizationData* 
nautilus_customization_data_new (const char *customization_name,
				 gboolean show_public_customizations,
				 gboolean data_is_for_a_menu,
				 int maximum_icon_height,
				 int maximum_icon_width)
{
	NautilusCustomizationData *data;
	char *public_directory_uri, *private_directory_uri;
	char *temp_str;
	GnomeVFSResult public_result, private_result;

	data = g_new0 (NautilusCustomizationData, 1);

	public_result = GNOME_VFS_OK;

	if (show_public_customizations) {
		public_directory_uri = get_global_customization_uri (customization_name);
		
		
		public_result = gnome_vfs_directory_list_load (&data->public_file_list,
							       public_directory_uri,
							       GNOME_VFS_FILE_INFO_GET_MIME_TYPE, NULL);
		g_free (public_directory_uri);
	}

	private_directory_uri = get_private_customization_uri (customization_name);
	private_result = gnome_vfs_directory_list_load (&data->private_file_list,
							private_directory_uri,
							GNOME_VFS_FILE_INFO_GET_MIME_TYPE, NULL);
	g_free (private_directory_uri);
	if (public_result != GNOME_VFS_OK && 
	    private_result != GNOME_VFS_OK) {
		g_warning ("Couldn't read any of the emblem directories\n");
		g_free (data);
		return NULL;
	}
	if (private_result == GNOME_VFS_OK) {
		data->reading_mode = READ_PRIVATE_CUSTOMIZATIONS;
		data->current_file_list = data->private_file_list;
	}
	if (show_public_customizations && public_result == GNOME_VFS_OK) {
		data->reading_mode = READ_PUBLIC_CUSTOMIZATIONS;	
		data->current_file_list = data->public_file_list;
	}

	/* load the frame if necessary */
	if (!strcmp(customization_name, "backgrounds")) {
		temp_str = nautilus_pixmap_file ("chit_frame.png");
		data->background_frame = gdk_pixbuf_new_from_file (temp_str);
		g_free (temp_str);
	} else {
		data->background_frame = NULL;
	}

	
	data->started_reading_current_file_list = FALSE;
	data->private_data_was_displayed = FALSE;
	data->data_is_for_a_menu = data_is_for_a_menu;
	data->customization_name = g_strdup (customization_name);

	data->maximum_icon_height = maximum_icon_height;
	data->maximum_icon_width = maximum_icon_width;

	return data;
}


GnomeVFSResult
nautilus_customization_data_get_next_element_for_display (NautilusCustomizationData *data,
							  char **emblem_name,
							  GtkWidget **pixmap_widget,
							  GtkWidget **label)
{
	GnomeVFSFileInfo *current_file_info;

	char *image_file_name, *filtered_name, *truncated_name;
	GdkPixbuf *pixbuf;
	GdkPixbuf *orig_pixbuf;

	if (!data->started_reading_current_file_list) {
		current_file_info = gnome_vfs_directory_list_first (data->current_file_list);
		data->started_reading_current_file_list = TRUE;
	}
	else {
		current_file_info = gnome_vfs_directory_list_next (data->current_file_list);
	}
	if (current_file_info == NULL) {
		if (data->reading_mode == READ_PUBLIC_CUSTOMIZATIONS) {
			if (data->private_file_list == NULL) {
				return GNOME_VFS_ERROR_EOF;
			}
			data->reading_mode = READ_PRIVATE_CUSTOMIZATIONS;
			data->current_file_list = data->private_file_list;
			data->started_reading_current_file_list = FALSE;
			return nautilus_customization_data_get_next_element_for_display (data,
											 emblem_name,
											 pixmap_widget,
											 label);
		}
		else {
				return GNOME_VFS_ERROR_EOF;
		}
	}

	
	if (!nautilus_istr_has_prefix (current_file_info->mime_type, "image/") ||
	    nautilus_istr_has_prefix (current_file_info->name, ".")) {
		
		return nautilus_customization_data_get_next_element_for_display (data,
										 emblem_name,
										 pixmap_widget,
										 label);
	}

	image_file_name = get_file_path_for_mode (data,
						  current_file_info->name);
	orig_pixbuf = gdk_pixbuf_new_from_file(image_file_name);
	g_free(image_file_name);

	*emblem_name = g_strdup (current_file_info->name);
	
	if (!strcmp(data->customization_name, "backgrounds")) {
		pixbuf = nautilus_customization_make_background_chit (orig_pixbuf, data->background_frame, FALSE);
	} else {
		pixbuf = nautilus_gdk_pixbuf_scale_down_to_fit (orig_pixbuf, 
								data->maximum_icon_width, 
								data->maximum_icon_height);
		gdk_pixbuf_unref (orig_pixbuf);
	}
	
	*pixmap_widget = nautilus_image_new ();
	nautilus_image_set_pixbuf (NAUTILUS_IMAGE (*pixmap_widget), pixbuf);
	gdk_pixbuf_unref (pixbuf);
	
	filtered_name = format_name_for_display (current_file_info->name);
	/* If the data is for a menu,
	   we want to truncate it and not use the nautilus
	   label because anti-aliased text doesn't look right
	   in menus */
	if (data->data_is_for_a_menu) {
		truncated_name = nautilus_truncate_text_for_menu_item (filtered_name);
		*label = gtk_label_new (truncated_name);
		g_free (truncated_name);
	}
	else {
		*label = nautilus_label_new (filtered_name);
		nautilus_label_set_font_size (NAUTILUS_LABEL (*label), 12);
	}
	
	g_free (filtered_name);

	if (data->reading_mode == READ_PRIVATE_CUSTOMIZATIONS) {
		data->private_data_was_displayed = TRUE;
	}
	return GNOME_VFS_OK;
}

gboolean                   
nautilus_customization_data_private_data_was_displayed (NautilusCustomizationData *data)
{
	return data->private_data_was_displayed;
}

void                       
nautilus_customization_data_destroy (NautilusCustomizationData *data)
{
	g_assert (data->public_file_list != NULL ||
		  data->private_file_list != NULL);

	if (data->background_frame != NULL) {
		gdk_pixbuf_unref (data->background_frame);
	}

	if (data->public_file_list != NULL) {
		gnome_vfs_directory_list_destroy (data->public_file_list);
	}
	if (data->private_file_list != NULL) {
		gnome_vfs_directory_list_destroy (data->private_file_list);
	}

	g_free (data->customization_name);
	g_free (data);
}


/* get_global_customization_directory
   Get the path where a property's pixmaps are stored 
   @customization_name : the name of the customization to get.
   Should be one of "emblems", "colors", or "backgrounds" 

   Return value: The directory name where the customization's 
   public pixmaps are stored */
static char *                  
get_global_customization_uri (const char *customization_name)
{
	char *directory_path, *directory_uri;
	
	directory_path = nautilus_make_path (NAUTILUS_DATADIR,
					     customization_name);
	directory_uri = gnome_vfs_get_uri_from_local_path (directory_path);
	
	g_free (directory_path);

	return directory_uri;
	
}


/* get_private_customization_directory
   Get the path where a customization's pixmaps are stored 
   @customization_name : the name of the customization to get.
   Should be one of "emblems", "colors", or "backgrounds" 

   Return value: The directory name where the customization's 
   user-specific pixmaps are stored */
static char *                  
get_private_customization_uri (const char *customization_name)
{
	char *user_directory;
	char *directory_path, *directory_uri;

	user_directory = nautilus_get_user_directory ();
	directory_path = nautilus_make_path (user_directory,
					     customization_name);
	g_free (user_directory);
	directory_uri = gnome_vfs_get_uri_from_local_path (directory_path);
	g_free (directory_path);
	
	return directory_uri;
}


static char *            
get_file_path_for_mode (const NautilusCustomizationData *data,
			const char *file_name)
{
	char *directory_uri, *uri, *directory_name;
	if (data->reading_mode == READ_PUBLIC_CUSTOMIZATIONS) {
		directory_uri = get_global_customization_uri (data->customization_name);
	}
	else {
		directory_uri = get_private_customization_uri (data->customization_name);
	}
	
	uri = nautilus_make_path (directory_uri, file_name);
	g_free (directory_uri);
	directory_name = gnome_vfs_get_local_path_from_uri (uri);
	g_free (uri);

	return directory_name;
}

/* utility to make an attractive background image by compositing with a frame */
GdkPixbuf*
nautilus_customization_make_background_chit (GdkPixbuf *background_tile, GdkPixbuf *frame, gboolean dragging)
{
	GdkPixbuf *pixbuf, *temp_pixbuf;
	int frame_width, frame_height;
	
	
	frame_width = gdk_pixbuf_get_width (frame);
	frame_height = gdk_pixbuf_get_height (frame);
	
	/* scale the background tile to the proper size */
	pixbuf = gdk_pixbuf_scale_simple (background_tile, frame_width, frame_height, GDK_INTERP_BILINEAR);
			
	/* composite the mask on top of it */
	gdk_pixbuf_composite (frame, pixbuf, 0, 0, frame_width, frame_height,
			      0.0, 0.0, 1.0, 1.0, GDK_INTERP_BILINEAR, 255);
	
	/* if we're dragging, get rid of the light-colored halo */
	if (dragging) {
		temp_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, frame_width - 6, frame_height - 6);
		gdk_pixbuf_copy_area (pixbuf, 2, 2, frame_width - 6, frame_height - 6, temp_pixbuf, 0, 0);
		gdk_pixbuf_unref (pixbuf);
		pixbuf = temp_pixbuf;
	}
			      
	gdk_pixbuf_unref (background_tile);
	return pixbuf;
}


/* utility to format the passed-in name for display by stripping the extension, mapping underscore
   and capitalizing as necessary */

static char*
format_name_for_display (const char* name)
{
	gboolean need_to_cap;
	int index, length;
	char *formatted_str;

	/* don't display a name for the "reset" property, since it's name is
	   contained in its image and also to help distinguish it */  
	if (!nautilus_strcmp(name, RESET_IMAGE_NAME)) {
		return g_strdup("");
	}
		
	formatted_str = strip_extension (name);
	
	/* FIXME bugzilla.eazel.com 5046: Not appropriate to upper-case the letter of the name
	 * after a space for all languages. A potential translation
	 * nightmare.
	 */
	need_to_cap = TRUE;
	length = strlen (formatted_str);
	for (index = 0; index < length; index++) {
		if (need_to_cap) {
			formatted_str[index] = toupper ((guchar) formatted_str[index]);
		}
		
		if (formatted_str[index] == '_') {
			formatted_str[index] = ' ';
		}
		need_to_cap = formatted_str[index] == ' ';
	}
	
	return formatted_str;	
}


/* utility routine to strip the extension from the passed in string */
static char*
strip_extension (const char* string_to_strip)
{
	char *result_str, *temp_str;
	if (string_to_strip == NULL)
		return NULL;
	
	result_str = g_strdup(string_to_strip);
	temp_str = strrchr(result_str, '.');
	if (temp_str)
		*temp_str = '\0';
	return result_str;
}