nautilus-view-identifier.c 6.85 KB
Newer Older
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-view-identifier.c: Unique ID/Human-readable name pairs for views
 
   Copyright (C) 2000 Eazel, Inc.
  
   This program 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 program 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: Maciej Stachowiak <mjs@eazel.com>
*/

#include <config.h>
#include "nautilus-view-identifier.h"

28 29 30
#include <libgnome/gnome-i18n.h>


Ramiro Estrugo's avatar
Ramiro Estrugo committed
31 32
#include <eel/eel-glib-extensions.h>
#include <eel/eel-string.h>
33 34 35
#include <glib.h>
#include <stdlib.h>

36 37 38 39 40 41 42 43
static NautilusViewIdentifier *
nautilus_view_identifier_new (const char *iid,
			      const char *name,
			      const char *view_as_label,
			      const char *label_viewer);



44
NautilusViewIdentifier *
45 46 47 48
nautilus_view_identifier_new (const char *iid, 
			      const char *name,
			      const char *view_as_label,
			      const char *viewer_label)
49 50 51 52 53 54 55 56 57
{
        NautilusViewIdentifier *new_identifier;
        
        g_return_val_if_fail (iid != NULL, NULL);
        g_return_val_if_fail (name != NULL, NULL);
        
        new_identifier = g_new0 (NautilusViewIdentifier, 1);
        new_identifier->iid = g_strdup (iid);
        new_identifier->name = g_strdup (name);
58 59 60 61 62 63

        new_identifier->view_as_label = view_as_label ? g_strdup (view_as_label) :
		g_strdup_printf (_("View as %s"), name);

        new_identifier->viewer_label = view_as_label ? g_strdup (viewer_label) :
		g_strdup_printf (_("%s Viewer"), name);
64 65 66 67
        
        return new_identifier;
}

68
NautilusViewIdentifier *
69
nautilus_view_identifier_copy (const NautilusViewIdentifier *identifier)
70
{
71 72 73 74
	if (identifier == NULL) {
		return NULL;
	}
	
75 76 77 78
	return nautilus_view_identifier_new (identifier->iid, 
					     identifier->name, 
					     identifier->view_as_label,
					     identifier->viewer_label);
79 80
}

81 82
/* Returns a list of languages, containing
   the LANG or LANGUAGE environment setting (with and without region code).
83
   The elements in the returned list must be freed */
84 85 86 87
static GSList *
get_lang_list (void)
{
        GSList *retval;
88 89
        char *lang, *lang_with_locale, *tmp, *org_pointer;
        char *equal_char;
90 91 92

        retval = NULL;

93
        tmp = g_getenv ("LANGUAGE");
94

95 96
        if (tmp == NULL) {
                tmp = g_getenv ("LANG");
97 98
        }

99 100
	lang = g_strdup (tmp);
	org_pointer = lang;
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	if (lang != NULL) {
		/* envs can be in NAME=VALUE form */
		equal_char = strchr (lang, '=');
		if (equal_char != NULL) {
			lang = equal_char + 1;
		}
		
		/* lang may be in form LANG_LOCALE */
		equal_char = strchr (lang, '_');
		if (equal_char != NULL) {
			lang_with_locale = g_strdup (lang);
			*equal_char = 0;
		} else {
			lang_with_locale = NULL;
		}
117

118 119
		/* Make sure we don't give oaf an empty
		   lang string */
Ramiro Estrugo's avatar
Ramiro Estrugo committed
120
		if (!eel_str_is_empty (lang_with_locale)) {
121
			retval = g_slist_prepend (retval, 
122
						  g_strdup (lang_with_locale));
123
		}
124
		g_free (lang_with_locale);
Ramiro Estrugo's avatar
Ramiro Estrugo committed
125
		if (!eel_str_is_empty (lang)) {
126 127
			retval = g_slist_prepend (retval, g_strdup (lang));
		}
128
        }
129
	g_free (org_pointer);
130 131 132 133 134
        
        return retval;
}

NautilusViewIdentifier *
135
nautilus_view_identifier_new_from_bonobo_activation_server_info (Bonobo_ServerInfo *server, char *name_attribute)
136
{
137
        const char *view_as_name;       
138 139
        const char *view_as_label;       
        const char *viewer_label;       
140 141 142
        GSList *langs;

        langs = get_lang_list ();
143

144 145 146
        view_as_name = bonobo_activation_server_info_prop_lookup (server, name_attribute, langs);
	view_as_label = bonobo_activation_server_info_prop_lookup (server, "nautilus:view_as_label", langs);
	viewer_label = bonobo_activation_server_info_prop_lookup (server, "nautilus:viewer_label", langs);
147

148
        if (view_as_name == NULL) {
149
                view_as_name = bonobo_activation_server_info_prop_lookup (server, "name", langs);
150 151 152 153
        }
        if (view_as_name == NULL) {
                view_as_name = server->iid;
        }
154

Ramiro Estrugo's avatar
Ramiro Estrugo committed
155
	eel_g_slist_free_deep (langs);
156

157
	/* if the name is an OAFIID, clean it up for display */
Ramiro Estrugo's avatar
Ramiro Estrugo committed
158
	if (eel_str_has_prefix (view_as_name, "OAFIID:")) {
159 160 161 162 163 164 165 166 167
		char *display_name, *colon_ptr;
		NautilusViewIdentifier *new_identifier;
		
		display_name = g_strdup (view_as_name + 7);
		colon_ptr = strchr (display_name, ':');
		if (colon_ptr) {
			*colon_ptr = '\0';
		}
		
168 169
		new_identifier = nautilus_view_identifier_new (server->iid, display_name,
							       view_as_label, viewer_label);
170 171 172 173
		g_free(display_name);
		return new_identifier;					
	}
		
174 175
        return nautilus_view_identifier_new (server->iid, view_as_name,
					     view_as_label, viewer_label);
176 177 178
}

NautilusViewIdentifier *
179
nautilus_view_identifier_new_from_content_view (Bonobo_ServerInfo *server)
180
{
181
	return nautilus_view_identifier_new_from_bonobo_activation_server_info
182
		(server, "nautilus:view_as_name");
183 184
}

185
NautilusViewIdentifier *
186
nautilus_view_identifier_new_from_property_page (Bonobo_ServerInfo *server)
187
{
188
	return nautilus_view_identifier_new_from_bonobo_activation_server_info
189 190 191
		(server, "nautilus:property_page_name");
}

192
NautilusViewIdentifier *
193
nautilus_view_identifier_new_from_sidebar_panel (Bonobo_ServerInfo *server)
194
{
195
	return nautilus_view_identifier_new_from_bonobo_activation_server_info
196
		(server, "nautilus:sidebar_panel_name");
197 198 199 200 201 202 203 204
}

void
nautilus_view_identifier_free (NautilusViewIdentifier *identifier)
{
        if (identifier != NULL) {
                g_free (identifier->iid);
                g_free (identifier->name);
205 206
                g_free (identifier->view_as_label);
                g_free (identifier->viewer_label);
207 208 209 210
                g_free (identifier);
        }
}

211 212 213 214 215 216 217 218 219 220 221 222 223
GList *
nautilus_view_identifier_list_copy (GList *list)
{
	GList *copy, *node;

	copy = NULL;
	for (node = list; node != NULL; node = node->next) {
		copy = g_list_prepend
			(copy, nautilus_view_identifier_copy (node->data));
	}
	return g_list_reverse (copy);
}

224
static void
225
nautilus_view_identifier_free_callback (gpointer identifier, gpointer ignore)
226
{
Darin Adler's avatar
Darin Adler committed
227
	g_assert (ignore == NULL);
228 229 230 231
	nautilus_view_identifier_free (identifier);
}

void
232
nautilus_view_identifier_list_free (GList *list)
233
{
Ramiro Estrugo's avatar
Ramiro Estrugo committed
234
	eel_g_list_free_deep_custom
235
		(list, nautilus_view_identifier_free_callback, NULL);
236 237
}

238
int
239 240
nautilus_view_identifier_compare (const NautilusViewIdentifier *a,
				  const NautilusViewIdentifier *b)
241
{
242 243 244 245 246 247 248
	int result;

	result = strcmp (a->iid, b->iid);
	if (result != 0) {
		return result;
	}
	return strcmp (a->name, b->name);
249
}