nautilus-view-identifier.c 4.36 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 28
/* -*- 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"

#include "nautilus-glib-extensions.h"
29
#include "nautilus-string.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <glib.h>
#include <stdlib.h>

NautilusViewIdentifier *
nautilus_view_identifier_new (const char *iid, const char *name)
{
        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);
        
        return new_identifier;
}

48 49 50
NautilusViewIdentifier *
nautilus_view_identifier_copy (NautilusViewIdentifier *identifier)
{
51 52 53 54
	if (identifier == NULL) {
		return NULL;
	}
	
55 56 57
	return nautilus_view_identifier_new (identifier->iid, identifier->name);
}

58 59 60 61 62 63 64 65 66 67

static GSList *
get_lang_list (void)
{
        GSList *retval;
        char *lang;
        char * equal_char;

        retval = NULL;

Darin Adler's avatar
Darin Adler committed
68
        lang = g_getenv ("LANGUAGE");
69 70

        if (!lang) {
Darin Adler's avatar
Darin Adler committed
71
                lang = g_getenv ("LANG");
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        }


        if (lang) {
                equal_char = strchr (lang, '=');
                if (equal_char != NULL) {
                        lang = equal_char + 1;
                }

                retval = g_slist_prepend (retval, lang);
        }
        
        return retval;
}

NautilusViewIdentifier *
nautilus_view_identifier_new_from_oaf_server_info (OAF_ServerInfo *server, char *name_attribute)
{
90
        const char *view_as_name;       
91 92 93 94
        GSList *langs;

        langs = get_lang_list ();
        view_as_name = oaf_server_info_attr_lookup (server, name_attribute, langs);
95
		
96 97 98 99 100 101 102 103 104 105
        if (view_as_name == NULL) {
                view_as_name = oaf_server_info_attr_lookup (server, "name", langs);
        }

        if (view_as_name == NULL) {
                view_as_name = server->iid;
        }
       
        g_slist_free (langs);

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	/* if the name is an OAFIID, clean it up for display */
	if (nautilus_str_has_prefix (view_as_name, "OAFIID:")) {
		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';
		}
		
		new_identifier = nautilus_view_identifier_new (server->iid, display_name);
		g_free(display_name);
		return new_identifier;					
	}
		
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
        return nautilus_view_identifier_new (server->iid, view_as_name);
}

NautilusViewIdentifier *
nautilus_view_identifier_new_from_content_view (OAF_ServerInfo *server)
{
	return nautilus_view_identifier_new_from_oaf_server_info (server, 
								  "nautilus:view_as_name");
}

NautilusViewIdentifier *
nautilus_view_identifier_new_from_sidebar_panel (OAF_ServerInfo *server)
{
	return nautilus_view_identifier_new_from_oaf_server_info (server, 
								  "nautilus:sidebar_panel_name");
}

void
nautilus_view_identifier_free (NautilusViewIdentifier *identifier)
{
        if (identifier != NULL) {
                g_free (identifier->iid);
                g_free (identifier->name);
                g_free (identifier);
        }
}

static void
nautilus_view_identifier_free_callback (NautilusViewIdentifier *identifier, gpointer ignore)
{
Darin Adler's avatar
Darin Adler committed
152
	g_assert (ignore == NULL);
153 154 155 156
	nautilus_view_identifier_free (identifier);
}

void
Darin Adler's avatar
Darin Adler committed
157
nautilus_view_identifier_list_free (GList *identifiers)
158
{
Darin Adler's avatar
Darin Adler committed
159 160 161 162
	nautilus_g_list_free_deep_custom
		(identifiers,
		 (GFunc) nautilus_view_identifier_free_callback,
		 NULL);
163 164
}