Exemplo n.º 1
0
gchar *
gedit_convert_from_utf8 (const gchar          *content, 
		         gsize                 len,
		         const GeditEncoding  *encoding,
			 gsize                *new_len,
			 GError 	     **error)
{
	GError *conv_error         = NULL;
	gchar  *converted_contents = NULL;
	gsize   bytes_written = 0;

	gedit_debug (DEBUG_UTILS);
	
	g_return_val_if_fail (content != NULL, NULL);
	g_return_val_if_fail (g_utf8_validate (content, len, NULL), NULL);
	g_return_val_if_fail (encoding != NULL, NULL);

	if (len < 0)
		len = strlen (content);

	if (encoding == gedit_encoding_get_utf8 ())
		return g_strndup (content, len);

	converted_contents = g_convert (content, 
					len, 
					gedit_encoding_get_charset (encoding), 
					"UTF-8",
					NULL, 
					&bytes_written,
					&conv_error); 

	if (conv_error != NULL)
	{
		gedit_debug_message (DEBUG_UTILS, "Cannot convert from UTF-8 to %s",
				     gedit_encoding_get_charset (encoding));

		if (converted_contents != NULL)
		{
			g_free (converted_contents);
			converted_contents = NULL;
		}

		g_propagate_error (error, conv_error);
	}
	else
	{
		if (new_len != NULL)
			*new_len = bytes_written;
	}

	return converted_contents;
}
static GCharsetConverter *
guess_encoding (GeditDocumentOutputStream *stream,
		const void                *inbuf,
		gsize                      inbuf_size)
{
	GCharsetConverter *conv = NULL;

	if (inbuf == NULL || inbuf_size == 0)
	{
		stream->priv->is_utf8 = TRUE;
		return NULL;
	}

	if (stream->priv->encodings != NULL &&
	    stream->priv->encodings->next == NULL)
	{
		stream->priv->use_first = TRUE;
	}

	/* We just check the first block */
	while (TRUE)
	{
		const GeditEncoding *enc;

		if (conv != NULL)
		{
			g_object_unref (conv);
			conv = NULL;
		}

		/* We get an encoding from the list */
		enc = get_encoding (stream);

		/* if it is NULL we didn't guess anything */
		if (enc == NULL)
		{
			break;
		}

		gedit_debug_message (DEBUG_UTILS, "trying charset: %s",
				     gedit_encoding_get_charset (stream->priv->current_encoding->data));

		if (enc == gedit_encoding_get_utf8 ())
		{
			gsize remainder;
			const gchar *end;
			
			if (g_utf8_validate (inbuf, inbuf_size, &end) ||
			    stream->priv->use_first)
			{
				stream->priv->is_utf8 = TRUE;
				break;
			}

			/* Check if the end is less than one char */
			remainder = inbuf_size - (end - (gchar *)inbuf);
			if (remainder < 6)
			{
				stream->priv->is_utf8 = TRUE;
				break;
			}

			continue;
		}

		conv = g_charset_converter_new ("UTF-8",
						gedit_encoding_get_charset (enc),
						NULL);

		/* If we tried all encodings we use the first one */
		if (stream->priv->use_first)
		{
			break;
		}

		/* Try to convert */
		if (try_convert (conv, inbuf, inbuf_size))
		{
			break;
		}
	}

	if (conv != NULL)
	{
		g_converter_reset (G_CONVERTER (conv));
	}

	return conv;
}
static void
update_menu (GeditEncodingsComboBox *menu)
{
    GtkListStore *store;
    GtkTreeIter iter;
    GSList *encodings, *l;
    gchar *str;
    const GeditEncoding *utf8_encoding;
    const GeditEncoding *current_encoding;

    store = menu->priv->store;

    /* Unset the previous model */
    g_signal_handler_block (menu, menu->priv->changed_id);
    gtk_list_store_clear (store);
    gtk_combo_box_set_model (GTK_COMBO_BOX (menu),
                             NULL);

    utf8_encoding = gedit_encoding_get_utf8 ();
    current_encoding = gedit_encoding_get_current ();

    if (!menu->priv->save_mode)
    {
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            NAME_COLUMN, _("Automatically Detected"),
                            ENCODING_COLUMN, NULL,
                            ADD_COLUMN, FALSE,
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            NAME_COLUMN, "",
                            ENCODING_COLUMN, NULL,
                            ADD_COLUMN, FALSE,
                            -1);
    }

    if (current_encoding != utf8_encoding)
        str = gedit_encoding_to_string (utf8_encoding);
    else
        str = g_strdup_printf (_("Current Locale (%s)"),
                               gedit_encoding_get_charset (utf8_encoding));

    gtk_list_store_append (store, &iter);
    gtk_list_store_set (store, &iter,
                        NAME_COLUMN, str,
                        ENCODING_COLUMN, utf8_encoding,
                        ADD_COLUMN, FALSE,
                        -1);

    g_free (str);

    if ((utf8_encoding != current_encoding) &&
            (current_encoding != NULL))
    {
        str = g_strdup_printf (_("Current Locale (%s)"),
                               gedit_encoding_get_charset (current_encoding));

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            NAME_COLUMN, str,
                            ENCODING_COLUMN, current_encoding,
                            ADD_COLUMN, FALSE,
                            -1);

        g_free (str);
    }

    encodings = gedit_prefs_manager_get_shown_in_menu_encodings ();

    for (l = encodings; l != NULL; l = g_slist_next (l))
    {
        const GeditEncoding *enc = (const GeditEncoding *)l->data;

        if ((enc != current_encoding) &&
                (enc != utf8_encoding) &&
                (enc != NULL))
        {
            str = gedit_encoding_to_string (enc);

            gtk_list_store_append (store, &iter);
            gtk_list_store_set (store, &iter,
                                NAME_COLUMN, str,
                                ENCODING_COLUMN, enc,
                                ADD_COLUMN, FALSE,
                                -1);

            g_free (str);
        }
    }

    g_slist_free (encodings);

    if (gedit_prefs_manager_shown_in_menu_encodings_can_set ())
    {
        gtk_list_store_append (store, &iter);
        /* separator */
        gtk_list_store_set (store, &iter,
                            NAME_COLUMN, "",
                            ENCODING_COLUMN, NULL,
                            ADD_COLUMN, FALSE,
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            NAME_COLUMN, _("Add or Remove..."),
                            ENCODING_COLUMN, NULL,
                            ADD_COLUMN, TRUE,
                            -1);
    }

    /* set the model back */
    gtk_combo_box_set_model (GTK_COMBO_BOX (menu),
                             GTK_TREE_MODEL (menu->priv->store));
    gtk_combo_box_set_active (GTK_COMBO_BOX (menu), 0);

    g_signal_handler_unblock (menu, menu->priv->changed_id);
}
Exemplo n.º 4
0
gchar *
gedit_convert_to_utf8 (const gchar          *content,
		       gsize                 len,
		       const GeditEncoding **encoding,
		       gsize                *new_len,
		       GError              **error)
{
	gedit_debug (DEBUG_UTILS);

	g_return_val_if_fail (content != NULL, NULL);
	g_return_val_if_fail (encoding != NULL, NULL);

	if (len < 0)
		len = strlen (content);

	if (*encoding != NULL)
	{
		const gchar* charset;
		
		charset = gedit_encoding_get_charset (*encoding);

		g_return_val_if_fail (charset != NULL, NULL);

		return gedit_convert_to_utf8_from_charset (content, 
							   len,
							   charset,
							   new_len,
							   error);
	}
	else
	{
		/* Automatically detect the encoding used */

		GSList *encodings;
		GSList *start;
		gchar *ret = NULL;

		gedit_debug_message (DEBUG_UTILS,
				     "Automatically detect the encoding used");

		encodings = gedit_prefs_manager_get_auto_detected_encodings ();

		if (encodings == NULL)
		{
			gedit_debug_message (DEBUG_UTILS, "encodings == NULL");

			if (g_utf8_validate (content, len, NULL))
			{
				gedit_debug_message (DEBUG_UTILS, "validate OK.");

				if (new_len != NULL)
					*new_len = len;
				
				return g_strndup (content, len);
			}
			else
			{
				gedit_debug_message (DEBUG_UTILS, "validate failed.");

				g_set_error (error, GEDIT_CONVERT_ERROR, 
					     GEDIT_CONVERT_ERROR_AUTO_DETECTION_FAILED,
				 	     "gedit was not able to automatically determine "
					     "the encoding of the file you want to open.");

				return NULL;
			}
		}

		gedit_debug_message (DEBUG_UTILS, "encodings != NULL");

		start = encodings;

		while (encodings != NULL) 
		{
			const GeditEncoding *enc;
			const gchar *charset;
			gchar *utf8_content;

			enc = (const GeditEncoding *)encodings->data;

			gedit_debug_message (DEBUG_UTILS, "Get charset");

			charset = gedit_encoding_get_charset (enc);
			g_return_val_if_fail (charset != NULL, NULL);

			gedit_debug_message (DEBUG_UTILS, "Trying to convert %lu bytes of data from '%s' to 'UTF-8'.", 
					(unsigned long) len, charset);

			utf8_content = gedit_convert_to_utf8_from_charset (content, 
									   len, 
									   charset, 
									   new_len,
									   NULL);

			if (utf8_content != NULL) 
			{
				*encoding = enc;
				ret = utf8_content;

				break;
			}

			encodings = g_slist_next (encodings);
		}

		if (ret == NULL)
		{
			gedit_debug_message (DEBUG_UTILS, "gedit has not been able to automatically determine the encoding of "
					     "the file you want to open.");

			g_set_error (error, GEDIT_CONVERT_ERROR,
				     GEDIT_CONVERT_ERROR_AUTO_DETECTION_FAILED,
			 	     "gedit was not able to automatically determine "
				     "the encoding of the file you want to open.");
		}

		g_slist_free (start);

		return ret;
	}

	g_return_val_if_reached (NULL);
}