summaryrefslogtreecommitdiff
path: root/components/vcard/nautilus-vcard.c
blob: e22002c795cd2b29b65c96952a56db5f6bd8ee96 (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

/*
 *  Copyright (C) 2000 Eazel, Inc.
 *
 *  This library 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.
 *
 *  This library 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 library; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Author: Andy Hertzfeld <andy@eazel.com>
 *
 */

/* this is the implementation of the vcard component, which display a vcard graphically
 */
 
#include <config.h>
#include <gnome.h>
#include <liboaf/liboaf.h>

#include <bonobo.h>

#include "vcard.h"
#include "nautilus-vcard.h"

#include <gnome-xml/parser.h>
#include <gnome-xml/xmlmemory.h>

#include <libgnomevfs/gnome-vfs.h>

#include <libnautilus/nautilus-view.h>

#include <libnautilus-extensions/nautilus-file-utilities.h>
#include <eel/eel-gdk-extensions.h>
#include <eel/eel-gdk-pixbuf-extensions.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gtk-extensions.h>
#include <eel/eel-gtk-macros.h>
#include <eel/eel-scalable-font.h>
#include <eel/eel-string.h>
#include <eel/eel-xml-extensions.h>
#include <eel/eel-vfs-extensions.h>
#include <libnautilus-extensions/nautilus-font-factory.h>

/* private instance variables */
struct _NautilusVCardDetails {
	char *vcard_uri;
	char *vcard_data;

	EelReadFileHandle *load_file_handle;
	EelPixbufLoadHandle *load_image_handle;
	
	BonoboObject *control;
	EelScalableFont *font;	

	GdkPixbuf *logo;
};

static void nautilus_vcard_initialize_class (NautilusVCardClass *klass);
static void nautilus_vcard_initialize (NautilusVCard *view);
static void nautilus_vcard_destroy (GtkObject *object);

static void nautilus_vcard_draw (GtkWidget *widget, GdkRectangle *box);
static int  nautilus_vcard_expose (GtkWidget *widget, GdkEventExpose *event);
static gboolean nautilus_vcard_button_press_event (GtkWidget *widget, GdkEventButton *event);
static gboolean nautilus_vcard_motion_event (GtkWidget *widget, GdkEventMotion *event);
static gboolean nautilus_vcard_leave_event (GtkWidget *widget, GdkEventCrossing *event);
static void nautilus_vcard_size_request (GtkWidget *widget, GtkRequisition *request);

static void nautilus_vcard_set_uri (NautilusVCard *vcard, const char *uri);


EEL_DEFINE_CLASS_BOILERPLATE (NautilusVCard,
                                   nautilus_vcard,
                                   GTK_TYPE_EVENT_BOX)

#define MINIMUM_DRAW_SIZE 4

static void
nautilus_vcard_initialize_class (NautilusVCardClass *klass)
{
	GtkObjectClass *object_class;
	GtkWidgetClass *widget_class;
	
	object_class = GTK_OBJECT_CLASS (klass);
	widget_class = GTK_WIDGET_CLASS (klass);
	
	object_class->destroy = nautilus_vcard_destroy;
	
	widget_class->draw = nautilus_vcard_draw;
	widget_class->expose_event = nautilus_vcard_expose;
	widget_class->button_press_event = nautilus_vcard_button_press_event;
	widget_class->motion_notify_event = nautilus_vcard_motion_event;
	widget_class->leave_notify_event = nautilus_vcard_leave_event;
	widget_class->size_request = nautilus_vcard_size_request;
}

/* routines to handle setting and getting the configuration properties of the Bonobo control */

enum {
	CONFIGURATION
} MyArgs;


static void
get_bonobo_properties (BonoboPropertyBag *bag,
			BonoboArg *arg,
			guint arg_id,
			CORBA_Environment *ev,
			gpointer user_data)
{
	NautilusVCard *vcard = NAUTILUS_VCARD (user_data);

	switch (arg_id) {

		case CONFIGURATION:
		{
			BONOBO_ARG_SET_STRING (arg, vcard->details->vcard_uri);
			break;
		}

		default:
			g_warning ("Unhandled arg %d", arg_id);
			break;
	}
}

static void
set_bonobo_properties (BonoboPropertyBag *bag,
			const BonoboArg *arg,
			guint arg_id,
			CORBA_Environment *ev,
			gpointer user_data)
{
	NautilusVCard *vcard = NAUTILUS_VCARD (user_data);

	switch (arg_id) {

		case CONFIGURATION:
		{
			char *uri;

			uri = BONOBO_ARG_GET_STRING (arg);
			nautilus_vcard_set_uri (vcard, uri);
			
			break;
		}

		default:
			g_warning ("Unhandled arg %d", arg_id);
			break;
	}
}

/* initialize ourselves by connecting to the location change signal and allocating our subviews */
static void
nautilus_vcard_initialize (NautilusVCard *vcard)
{
	GtkWidget *frame;
	BonoboPropertyBag *property_bag;
		
	vcard->details = g_new0 (NautilusVCardDetails, 1);

	/* set up the font */
	vcard->details->font = eel_scalable_font_get_default_font ();
	
	/* receive mouse motion events */
	gtk_widget_add_events (GTK_WIDGET (vcard), GDK_POINTER_MOTION_MASK);

	/* embed it into a frame */		
	frame = gtk_frame_new (NULL);
  	gtk_frame_set_shadow_type(GTK_FRAME (frame), GTK_SHADOW_OUT);
  	gtk_widget_show (frame);
	gtk_container_add (GTK_CONTAINER (frame), GTK_WIDGET (vcard));
	
	/* make the bonobo control */
	vcard->details->control = (BonoboObject*) bonobo_control_new (GTK_WIDGET (frame));
	
	/* attach a property bag with the configure property */
	property_bag = bonobo_property_bag_new (get_bonobo_properties, set_bonobo_properties, vcard);
	bonobo_control_set_properties (BONOBO_CONTROL(vcard->details->control),property_bag);
	bonobo_object_unref (BONOBO_OBJECT (property_bag));

	bonobo_property_bag_add (property_bag, "configuration", CONFIGURATION, BONOBO_ARG_STRING, NULL,
				 "VCard Configuration", BONOBO_PROPERTY_WRITEABLE);
			 	
	/* show the view itself */	
	gtk_widget_show (GTK_WIDGET (vcard));
}


static void
nautilus_vcard_destroy (GtkObject *object)
{
	NautilusVCard *vcard;
	
	vcard = NAUTILUS_VCARD (object);
	g_free (vcard->details->vcard_uri);
	g_free (vcard->details->vcard_data);
	
	if (vcard->details->load_file_handle != NULL) {
		eel_read_file_cancel (vcard->details->load_file_handle);
	}
	
	if (vcard->details->load_image_handle != NULL) {
		eel_cancel_gdk_pixbuf_load (vcard->details->load_image_handle);
	}
			

	if (vcard->details->logo != NULL) {
		gdk_pixbuf_unref (vcard->details->logo);
	}	
	
	if (vcard->details->font) {
		gtk_object_unref (GTK_OBJECT (vcard->details->font));
	}
	
	g_free (vcard->details);

	/* Chain destroy */
	EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}

/* get associated Bonobo control */
BonoboObject *
nautilus_vcard_get_control (NautilusVCard *vcard)
{
	return vcard->details->control;
}

static void
vcard_logo_callback (GnomeVFSResult  error, GdkPixbuf *pixbuf, gpointer callback_data)
{
	NautilusVCard *vcard;
	GdkPixbuf *scaled_pixbuf;
	
	vcard = NAUTILUS_VCARD (callback_data);
	vcard->details->load_image_handle = NULL;
	
	if (vcard->details->logo) {
		gdk_pixbuf_unref (vcard->details->logo);
	}
	
	if (pixbuf != NULL) {
		gdk_pixbuf_ref (pixbuf);
		
		scaled_pixbuf = eel_gdk_pixbuf_scale_down_to_fit (pixbuf, 128, 80);
		gdk_pixbuf_unref (pixbuf);
		vcard->details->logo = scaled_pixbuf;
		gtk_widget_queue_draw (GTK_WIDGET (vcard));
	}
}

/* completion routine invoked when we've loaded the vcard file uri.  */

static void
vcard_read_done_callback (GnomeVFSResult result,
			 GnomeVFSFileSize file_size,
			 char *file_contents,
			 gpointer callback_data)
{
	char *logo_uri;
	NautilusVCard *vcard;
	
	vcard = NAUTILUS_VCARD (callback_data);
	vcard->details->load_file_handle = NULL;

	/* make sure the read was successful */
	if (result != GNOME_VFS_OK) {
		g_assert (file_contents == NULL);
		return;
	}

	/* free old data if any */
	if (vcard->details->vcard_data) {
		g_free (vcard->details->vcard_data);
	}
	
	/* set up the vcard data */
	vcard->details->vcard_data = g_realloc (file_contents, file_size + 1);
	vcard->details->vcard_data[file_size] = '\0';
			
	/* extract the image uri and, if found, load it asynchronously */
	logo_uri = vcard_logo (vcard->details->vcard_data);
	g_message ("logo uri is %s", logo_uri);
	if (logo_uri != NULL) {
		vcard->details->load_image_handle = eel_gdk_pixbuf_load_async (logo_uri, vcard_logo_callback, vcard);
		g_free (logo_uri);
	}
						
	/* schedule a redraw to reflect the new contents */
	gtk_widget_queue_draw (GTK_WIDGET (vcard));
}

/* load the vcard  asynchronously */
static void
load_vcard (NautilusVCard *vcard)
{
	/* load the uri asynchrounously, calling a completion routine when completed */
	vcard->details->load_file_handle = eel_read_entire_file_async (vcard->details->vcard_uri, vcard_read_done_callback, vcard);
}

/* set the uri and load it */
static void
nautilus_vcard_set_uri (NautilusVCard *vcard, const char *uri)
{

	if (eel_strcmp (vcard->details->vcard_uri, uri) == 0) {
		return;
	}
	
	if (vcard->details->vcard_uri != NULL) {
		g_free (vcard->details->vcard_uri);
		vcard->details->vcard_uri = NULL;
	}

	if (uri != NULL) {
		vcard->details->vcard_uri = g_strdup (uri);
		load_vcard (vcard);	
	}
}

/* convenience routine to composite an image with the proper clipping */
static void
vcard_pixbuf_composite (GdkPixbuf *source, GdkPixbuf *destination, int x_offset, int y_offset, int alpha)
{
	int source_width, source_height, dest_width, dest_height;
	double float_x_offset, float_y_offset;
	
	source_width  = gdk_pixbuf_get_width (source);
	source_height = gdk_pixbuf_get_height (source);
	dest_width  = gdk_pixbuf_get_width (destination);
	dest_height = gdk_pixbuf_get_height (destination);
	
	float_x_offset = x_offset;
	float_y_offset = y_offset;
	
	/* clip to the destination size */
	if ((x_offset + source_width) > dest_width) {
		source_width = dest_width - x_offset;
	}
	if ((y_offset + source_height) > dest_height) {
		source_height = dest_height - y_offset;
	}
	
	gdk_pixbuf_composite (source, destination, x_offset, y_offset, source_width, source_height,
					float_x_offset, float_y_offset, 1.0, 1.0, GDK_PIXBUF_ALPHA_BILEVEL, alpha);
}



/* draw the logo image */
static int
draw_vcard_logo_image (NautilusVCard *vcard, GdkPixbuf *pixbuf, int offset)
{
	GtkWidget *widget;
	int logo_width, logo_height;
	int v_offset;
	
	widget = GTK_WIDGET (vcard);
	v_offset = offset;
	
	if (vcard->details->logo != NULL) {
		logo_width = gdk_pixbuf_get_width (vcard->details->logo);
		logo_height = gdk_pixbuf_get_height (vcard->details->logo);

		vcard_pixbuf_composite (vcard->details->logo, pixbuf, 2, v_offset, 255);
		v_offset += logo_height + 2;
	}

	return v_offset;
}

/* draw the name and title */
static int
draw_vcard_name_and_title (NautilusVCard *vcard, GdkPixbuf *pixbuf, int v_offset)
{
	int name_len, title_len;
	char *name, *title;
	GtkWidget *widget;
        EelDimensions name_dimensions;
        EelDimensions title_dimensions;
	
	if (vcard->details->font == NULL) {
		return v_offset;
	}
	
	widget = GTK_WIDGET (vcard);

	/* extract the name and title */
	name = vcard_full_name (vcard->details->vcard_data);  
	title = vcard_title (vcard->details->vcard_data);  

	/* first, measure the name */
	if (name != NULL) {
		name_len = strlen (name);
		name_dimensions = eel_scalable_font_measure_text (vcard->details->font, 
                                                                  18,
                                                                  name, name_len);
	
		/* draw the name into the pixbuf using anti-aliased text */	
		eel_scalable_font_draw_text (vcard->details->font, pixbuf, 
					  4, v_offset,
					  NULL,
					  18,
					  name, name_len,
					  EEL_RGB_COLOR_BLACK,
					  EEL_OPACITY_FULLY_OPAQUE);
		v_offset += name_dimensions.height + 4;

		g_free (name);
		
		if (title != NULL) {
			title_len = strlen (title);
			title_dimensions = eel_scalable_font_measure_text (vcard->details->font, 
					     14,
					     title, title_len);
	
			/* draw the name into the pixbuf using anti-aliased text */	
			eel_scalable_font_draw_text (vcard->details->font, pixbuf, 
					  4, v_offset,
					  NULL,
					  14,
					  title, title_len,
					  EEL_RGB_COLOR_BLACK,
					  EEL_OPACITY_FULLY_OPAQUE);
			v_offset += title_dimensions.height + 4;
			
			g_free (title);
		}
	}
	return v_offset;
}

/* draw the addresses and phone numbers associated with the vcard */
static int
draw_vcard_addresses (NautilusVCard *vcard, GdkPixbuf *pixbuf, int v_offset)
{
	return v_offset;
}

/* handle drawing the control */
static void
nautilus_vcard_draw (GtkWidget *widget, GdkRectangle *box)
{
	NautilusVCard *control;
	GdkPixbuf *temp_pixbuf;
	int width, height, v_offset;

	/* allocate a pixbuf to draw into */
	width = widget->allocation.width;
	height = widget->allocation.height;
	
	
	temp_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
		
	g_return_if_fail (widget != NULL);
	g_return_if_fail (NAUTILUS_IS_VCARD (widget));

	control = NAUTILUS_VCARD (widget);

	/* draw the background */
	eel_gdk_pixbuf_fill_rectangle_with_color (temp_pixbuf, NULL, 0xFFEFEFEF);

	/* draw the logo, if any */
	v_offset = draw_vcard_logo_image (control, temp_pixbuf, 2);
	
	/* draw the name and title */
	v_offset = draw_vcard_name_and_title (control, temp_pixbuf, v_offset);
	
	v_offset += 6;
	/* draw the addresses */
	v_offset = draw_vcard_addresses (control, temp_pixbuf, v_offset);
	
	/* blit the resultingpixbuf to the drawable, then release it */
	gdk_pixbuf_render_to_drawable_alpha (temp_pixbuf,
					widget->window,
					0, 0,
					widget->allocation.x, widget->allocation.y,
					width, height,
					GDK_PIXBUF_ALPHA_BILEVEL, 128,
					GDK_RGB_DITHER_MAX,
					0, 0);

	gdk_pixbuf_unref (temp_pixbuf);
}

/* handle expose events */
static int
nautilus_vcard_expose (GtkWidget *widget, GdkEventExpose *event)
{
	GdkRectangle box;
	g_return_val_if_fail (widget != NULL, FALSE);
	g_return_val_if_fail (NAUTILUS_IS_VCARD (widget), FALSE);

	box.x = 0; box.y = 0;
	box.width = widget->allocation.width;
	box.height = widget->allocation.height;

	nautilus_vcard_draw (widget, &box);
	return FALSE;
}


/* handle mouse motion events by maintaining the prelight state */
static gboolean
nautilus_vcard_motion_event (GtkWidget *widget, GdkEventMotion *event)
{
	int x, y;
	int which_item, item_count;
	NautilusVCard *vcard;
	
	vcard = NAUTILUS_VCARD (widget);

	gtk_widget_get_pointer (widget, &x, &y);
	which_item = 0;
	item_count = 0;
	
	if (which_item < 0 || which_item >= item_count) {
		which_item = -1;
	}
	return TRUE;
}

/* handle size requests by requesting a fixed size */
static void
nautilus_vcard_size_request (GtkWidget *widget, GtkRequisition *request)
{
	request->width = 220;
	request->height = 140;
}

/* handle leave events by cancelling any prelighting */
static gboolean
nautilus_vcard_leave_event (GtkWidget *widget, GdkEventCrossing *event)
{
	NautilusVCard *vcard;
	
	vcard = NAUTILUS_VCARD (widget);
	return TRUE;
}

/* handle button press events */
static gboolean
nautilus_vcard_button_press_event (GtkWidget *widget, GdkEventButton *event)
{
	GList *selected_item;
	NautilusVCard *vcard;
	char *command;
	int result, which_item;

	vcard = NAUTILUS_VCARD (widget);
	if (event->y < widget->allocation.y) {
		command = g_strdup_printf ("nautilus %s", vcard->details->vcard_uri);
		result = system (command);
		g_free (command);
	} else {
		which_item = (event->y - widget->allocation.y ) / 16;
		if (which_item < 0) {
			selected_item = 0;
						
		}
	}
	
	return FALSE;
}