summaryrefslogtreecommitdiff
path: root/gnome-settings-daemon/gnome-settings-default-editor.c
blob: a257fe2463e8ff5a5005ed64f3d2fad16ca031e9 (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
/*
 * gnome-settings-default-editor.h: sync default editor changes to mime database
 *
 * Copyright 2002 Sun Microsystems, Inc.
 *
 * Author: jacob berkman  <jacob@ximian.com>
 *
 */

/*
 * WARNING: This is a hack.
 *
 * All it does is keep the "text / *" and "text/plain" mime type
 * handlers in sync with each other.  The reason we do this is because
 * there is no UI for editing the text / * handler, and this is probably
 * what the user actually wants to do.
 */

#include <config.h>

#include "gnome-settings-daemon.h"
#include "gnome-settings-default-editor.h"

#include <libgnomevfs/gnome-vfs-mime-handlers.h>
#include <libgnomevfs/gnome-vfs-mime-monitor.h>

#include <string.h>

/* #define DE_DEBUG */

#define SYNC_CHANGES_KEY "/apps/gnome_settings_daemon/default_editor/sync_text_types"

static gboolean sync_changes;

#if DE_DEBUG
static void
print_mime_app (const char *mime_type)
{
	GnomeVFSMimeApplication *mime_app;

	mime_app = gnome_vfs_mime_get_default_application (mime_type);

	g_message ("Default info for %s (%p):\n"
		   "\t        id: %s\n"
		   "\t      name: %s\n"
		   "\t   command: %s\n"
		   "\tneeds term: %s\n",
		   mime_type, mime_app,
		   mime_app->id, 
		   mime_app->name, 
		   mime_app->command,
		   mime_app->requires_terminal ? "Yes" : "No");
}

static void
print_state (void)
{
	if (sync_changes)
		g_message ("Synching changes.");
	else
		g_message ("Not synching changes.");

	print_mime_app ("text/*");
	print_mime_app ("text/plain");
}
#define PRINT_STATE print_state()
#else
#define PRINT_STATE
#endif

static void
sync_changes_cb (GConfEntry *entry)
{
	GConfValue *value = gconf_entry_get_value (entry);
	sync_changes = gconf_value_get_bool (value);

	PRINT_STATE;
}

static void
vfs_change_cb (GnomeVFSMIMEMonitor *monitor, GConfClient *client)
{
	GnomeVFSMimeApplication *star_app, *plain_app;
	GnomeVFSMimeActionType action;

	PRINT_STATE;

	if (!sync_changes)
		return;
	
	star_app  = gnome_vfs_mime_get_default_application ("text/*");
	plain_app = gnome_vfs_mime_get_default_application ("text/plain");

	if (star_app == NULL || plain_app == NULL) {
	        if (star_app != NULL) {
	            gnome_vfs_mime_application_free (star_app);
	        }
	        if (plain_app != NULL) {
	            gnome_vfs_mime_application_free (plain_app);
	        }
		return;
	}
	if (!strcmp (star_app->id, plain_app->id)) {
	        gnome_vfs_mime_application_free (star_app);
	        gnome_vfs_mime_application_free (plain_app);
		return;
	}

#if DE_DEBUG
	g_message ("Synching text/plain to text/*...");
#endif

	action = gnome_vfs_mime_get_default_action_type ("text/plain");

	gnome_vfs_mime_set_default_application ("text/*", plain_app->id);
	gnome_vfs_mime_application_free (plain_app);

	gnome_vfs_mime_set_default_action_type ("text/*", action);

	PRINT_STATE;
}

void
gnome_settings_default_editor_init (GConfClient *client)
{
	sync_changes = gconf_client_get_bool (client, SYNC_CHANGES_KEY, NULL);

	gnome_settings_daemon_register_callback (SYNC_CHANGES_KEY, sync_changes_cb);

	g_signal_connect (gnome_vfs_mime_monitor_get (), "data_changed",
			  G_CALLBACK (vfs_change_cb), client);			  
}

void
gnome_settings_default_editor_load (GConfClient *client)
{
	vfs_change_cb (NULL, client);
}