From 4ef02d4837560edd3ad5770c4c72617a92819833 Mon Sep 17 00:00:00 2001 From: JosephMcc Date: Sun, 1 Jan 2017 13:31:02 -0800 Subject: [PATCH] xed-utils: Clean up code styling --- xed/xed-utils.c | 1921 ++++++++++++++++++++++++----------------------- 1 file changed, 999 insertions(+), 922 deletions(-) diff --git a/xed/xed-utils.c b/xed/xed-utils.c index 5fb81c1..984eeff 100644 --- a/xed/xed-utils.c +++ b/xed/xed-utils.c @@ -69,148 +69,143 @@ gboolean xed_utils_uri_has_file_scheme (const gchar *uri) { - GFile *gfile; - gboolean res; + GFile *gfile; + gboolean res; - gfile = g_file_new_for_uri (uri); - res = g_file_has_uri_scheme (gfile, "file"); + gfile = g_file_new_for_uri (uri); + res = g_file_has_uri_scheme (gfile, "file"); - g_object_unref (gfile); - return res; + g_object_unref (gfile); + return res; } /* FIXME: we should check for chained URIs */ gboolean xed_utils_uri_has_writable_scheme (const gchar *uri) { - GFile *gfile; - gchar *scheme; - GSList *writable_schemes; - gboolean res; + GFile *gfile; + gchar *scheme; + GSList *writable_schemes; + gboolean res; - gfile = g_file_new_for_uri (uri); - scheme = g_file_get_uri_scheme (gfile); + gfile = g_file_new_for_uri (uri); + scheme = g_file_get_uri_scheme (gfile); - g_return_val_if_fail (scheme != NULL, FALSE); + g_return_val_if_fail (scheme != NULL, FALSE); - g_object_unref (gfile); + g_object_unref (gfile); - writable_schemes = xed_prefs_manager_get_writable_vfs_schemes (); + writable_schemes = xed_prefs_manager_get_writable_vfs_schemes (); - /* CHECK: should we use g_ascii_strcasecmp? - Paolo (Nov 6, 2005) */ - res = (g_slist_find_custom (writable_schemes, - scheme, - (GCompareFunc)strcmp) != NULL); + /* CHECK: should we use g_ascii_strcasecmp? - Paolo (Nov 6, 2005) */ + res = (g_slist_find_custom (writable_schemes, scheme, (GCompareFunc)strcmp) != NULL); - g_slist_foreach (writable_schemes, (GFunc)g_free, NULL); - g_slist_free (writable_schemes); + g_slist_foreach (writable_schemes, (GFunc)g_free, NULL); + g_slist_free (writable_schemes); - g_free (scheme); + g_free (scheme); - return res; + return res; } static void -widget_get_origin (GtkWidget *widget, gint *x, gint *y) +widget_get_origin (GtkWidget *widget, + gint *x, + gint *y) { - GdkWindow *window; + GdkWindow *window; - window = gtk_widget_get_window (widget); - gdk_window_get_origin (window, x, y); + window = gtk_widget_get_window (widget); + gdk_window_get_origin (window, x, y); } void xed_utils_menu_position_under_widget (GtkMenu *menu, - gint *x, - gint *y, - gboolean *push_in, - gpointer user_data) + gint *x, + gint *y, + gboolean *push_in, + gpointer user_data) { - GtkWidget *widget; - GtkRequisition requisition; - GtkAllocation allocation; + GtkWidget *widget; + GtkRequisition requisition; + GtkAllocation allocation; - widget = GTK_WIDGET (user_data); - widget_get_origin (widget, x, y); + widget = GTK_WIDGET (user_data); + widget_get_origin (widget, x, y); - gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &requisition); + gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &requisition); - gtk_widget_get_allocation (widget, &allocation); + gtk_widget_get_allocation (widget, &allocation); - if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) - { - *x += allocation.x + allocation.width - requisition.width; - } - else - { - *x += allocation.x; - } + if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) + { + *x += allocation.x + allocation.width - requisition.width; + } + else + { + *x += allocation.x; + } - *y += allocation.y + allocation.height; + *y += allocation.y + allocation.height; - *push_in = TRUE; + *push_in = TRUE; } void xed_utils_menu_position_under_tree_view (GtkMenu *menu, - gint *x, - gint *y, - gboolean *push_in, - gpointer user_data) + gint *x, + gint *y, + gboolean *push_in, + gpointer user_data) { - GtkTreeView *tree = GTK_TREE_VIEW (user_data); - GtkTreeModel *model; - GtkTreeSelection *selection; - GtkTreeIter iter; + GtkTreeView *tree = GTK_TREE_VIEW (user_data); + GtkTreeModel *model; + GtkTreeSelection *selection; + GtkTreeIter iter; - model = gtk_tree_view_get_model (tree); - g_return_if_fail (model != NULL); + model = gtk_tree_view_get_model (tree); + g_return_if_fail (model != NULL); - selection = gtk_tree_view_get_selection (tree); - g_return_if_fail (selection != NULL); + selection = gtk_tree_view_get_selection (tree); + g_return_if_fail (selection != NULL); - if (gtk_tree_selection_get_selected (selection, NULL, &iter)) - { - GtkTreePath *path; - GdkRectangle rect; + if (gtk_tree_selection_get_selected (selection, NULL, &iter)) + { + GtkTreePath *path; + GdkRectangle rect; - widget_get_origin (GTK_WIDGET (tree), x, y); + widget_get_origin (GTK_WIDGET (tree), x, y); - path = gtk_tree_model_get_path (model, &iter); - gtk_tree_view_get_cell_area (tree, path, - gtk_tree_view_get_column (tree, 0), /* FIXME 0 for RTL ? */ - &rect); - gtk_tree_path_free (path); + path = gtk_tree_model_get_path (model, &iter); + gtk_tree_view_get_cell_area (tree, path, + gtk_tree_view_get_column (tree, 0), /* FIXME 0 for RTL ? */ + &rect); + gtk_tree_path_free (path); - *x += rect.x; - *y += rect.y + rect.height; + *x += rect.x; + *y += rect.y + rect.height; - if (gtk_widget_get_direction (GTK_WIDGET (tree)) == GTK_TEXT_DIR_RTL) - { - GtkRequisition requisition; - gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &requisition); + if (gtk_widget_get_direction (GTK_WIDGET (tree)) == GTK_TEXT_DIR_RTL) + { + GtkRequisition requisition; + gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &requisition); - *x += rect.width - requisition.width; - } - } - else - { - /* no selection -> regular "under widget" positioning */ - xed_utils_menu_position_under_widget (menu, - x, y, push_in, - tree); - } + *x += rect.width - requisition.width; + } + } + else + { + /* no selection -> regular "under widget" positioning */ + xed_utils_menu_position_under_widget (menu, x, y, push_in, tree); + } } /* FIXME: remove this with gtk 2.12, it has gdk_color_to_string */ gchar * xed_gdk_color_to_string (GdkColor color) { - return g_strdup_printf ("#%04x%04x%04x", - color.red, - color.green, - color.blue); + return g_strdup_printf ("#%04x%04x%04x", color.red, color.green, color.blue); } /** @@ -222,16 +217,14 @@ xed_gdk_color_to_string (GdkColor color) */ GtkWidget * xed_gtk_button_new_with_stock_icon (const gchar *label, - const gchar *stock_id) + const gchar *stock_id) { - GtkWidget *button; + GtkWidget *button; - button = gtk_button_new_with_mnemonic (label); - gtk_button_set_image (GTK_BUTTON (button), - gtk_image_new_from_stock (stock_id, - GTK_ICON_SIZE_BUTTON)); + button = gtk_button_new_with_mnemonic (label); + gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON)); - return button; + return button; } /** @@ -245,67 +238,72 @@ xed_gtk_button_new_with_stock_icon (const gchar *label, */ GtkWidget * xed_dialog_add_button (GtkDialog *dialog, - const gchar *text, - const gchar *stock_id, - gint response_id) + const gchar *text, + const gchar *stock_id, + gint response_id) { - GtkWidget *button; + GtkWidget *button; - g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL); - g_return_val_if_fail (text != NULL, NULL); - g_return_val_if_fail (stock_id != NULL, NULL); + g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL); + g_return_val_if_fail (text != NULL, NULL); + g_return_val_if_fail (stock_id != NULL, NULL); - button = xed_gtk_button_new_with_stock_icon (text, stock_id); - g_return_val_if_fail (button != NULL, NULL); + button = xed_gtk_button_new_with_stock_icon (text, stock_id); + g_return_val_if_fail (button != NULL, NULL); - gtk_widget_set_can_default (button, TRUE); + gtk_widget_set_can_default (button, TRUE); - gtk_widget_show (button); + gtk_widget_show (button); - gtk_dialog_add_action_widget (dialog, button, response_id); + gtk_dialog_add_action_widget (dialog, button, response_id); - return button; + return button; } /* * n: len of the string in bytes */ gboolean -g_utf8_caselessnmatch (const char *s1, const char *s2, gssize n1, gssize n2) +g_utf8_caselessnmatch (const char *s1, + const char *s2, + gssize n1, + gssize n2) { - gchar *casefold; - gchar *normalized_s1; - gchar *normalized_s2; - gint len_s1; - gint len_s2; - gboolean ret = FALSE; + gchar *casefold; + gchar *normalized_s1; + gchar *normalized_s2; + gint len_s1; + gint len_s2; + gboolean ret = FALSE; - g_return_val_if_fail (s1 != NULL, FALSE); - g_return_val_if_fail (s2 != NULL, FALSE); - g_return_val_if_fail (n1 > 0, FALSE); - g_return_val_if_fail (n2 > 0, FALSE); + g_return_val_if_fail (s1 != NULL, FALSE); + g_return_val_if_fail (s2 != NULL, FALSE); + g_return_val_if_fail (n1 > 0, FALSE); + g_return_val_if_fail (n2 > 0, FALSE); - casefold = g_utf8_casefold (s1, n1); - normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD); - g_free (casefold); + casefold = g_utf8_casefold (s1, n1); + normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD); + g_free (casefold); - casefold = g_utf8_casefold (s2, n2); - normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD); - g_free (casefold); + casefold = g_utf8_casefold (s2, n2); + normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD); + g_free (casefold); - len_s1 = strlen (normalized_s1); - len_s2 = strlen (normalized_s2); + len_s1 = strlen (normalized_s1); + len_s2 = strlen (normalized_s2); - if (len_s1 < len_s2) - goto finally_2; + if (len_s1 < len_s2) + { + goto finally_2; + } - ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0); + ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0); finally_2: - g_free (normalized_s1); - g_free (normalized_s2); + g_free (normalized_s1); + g_free (normalized_s2); - return ret; + return ret; } /** @@ -318,22 +316,28 @@ finally_2: * for a specified gtk widget. */ void -xed_utils_set_atk_name_description (GtkWidget *widget, - const gchar *name, - const gchar *description) +xed_utils_set_atk_name_description (GtkWidget *widget, + const gchar *name, + const gchar *description) { - AtkObject *aobj; + AtkObject *aobj; - aobj = gtk_widget_get_accessible (widget); + aobj = gtk_widget_get_accessible (widget); - if (!(GTK_IS_ACCESSIBLE (aobj))) - return; + if (!(GTK_IS_ACCESSIBLE (aobj))) + { + return; + } - if(name) - atk_object_set_name (aobj, name); + if (name) + { + atk_object_set_name (aobj, name); + } - if(description) - atk_object_set_description (aobj, description); + if (description) + { + atk_object_set_description (aobj, description); + } } /** @@ -346,219 +350,230 @@ xed_utils_set_atk_name_description (GtkWidget *widget, * between 2 specified widgets. */ void -xed_utils_set_atk_relation (GtkWidget *obj1, - GtkWidget *obj2, - AtkRelationType rel_type ) +xed_utils_set_atk_relation (GtkWidget *obj1, + GtkWidget *obj2, + AtkRelationType rel_type ) { - AtkObject *atk_obj1, *atk_obj2; - AtkRelationSet *relation_set; - AtkObject *targets[1]; - AtkRelation *relation; + AtkObject *atk_obj1, *atk_obj2; + AtkRelationSet *relation_set; + AtkObject *targets[1]; + AtkRelation *relation; - atk_obj1 = gtk_widget_get_accessible (obj1); - atk_obj2 = gtk_widget_get_accessible (obj2); + atk_obj1 = gtk_widget_get_accessible (obj1); + atk_obj2 = gtk_widget_get_accessible (obj2); - if (!(GTK_IS_ACCESSIBLE (atk_obj1)) || !(GTK_IS_ACCESSIBLE (atk_obj2))) - return; + if (!(GTK_IS_ACCESSIBLE (atk_obj1)) || !(GTK_IS_ACCESSIBLE (atk_obj2))) + { + return; + } - relation_set = atk_object_ref_relation_set (atk_obj1); - targets[0] = atk_obj2; + relation_set = atk_object_ref_relation_set (atk_obj1); + targets[0] = atk_obj2; - relation = atk_relation_new (targets, 1, rel_type); - atk_relation_set_add (relation_set, relation); + relation = atk_relation_new (targets, 1, rel_type); + atk_relation_set_add (relation_set, relation); - g_object_unref (G_OBJECT (relation)); + g_object_unref (G_OBJECT (relation)); } gboolean xed_utils_uri_exists (const gchar* text_uri) { - GFile *gfile; - gboolean res; + GFile *gfile; + gboolean res; - g_return_val_if_fail (text_uri != NULL, FALSE); + g_return_val_if_fail (text_uri != NULL, FALSE); - xed_debug_message (DEBUG_UTILS, "text_uri: %s", text_uri); + xed_debug_message (DEBUG_UTILS, "text_uri: %s", text_uri); - gfile = g_file_new_for_uri (text_uri); - res = g_file_query_exists (gfile, NULL); + gfile = g_file_new_for_uri (text_uri); + res = g_file_query_exists (gfile, NULL); - g_object_unref (gfile); + g_object_unref (gfile); - xed_debug_message (DEBUG_UTILS, res ? "TRUE" : "FALSE"); + xed_debug_message (DEBUG_UTILS, res ? "TRUE" : "FALSE"); - return res; + return res; } gchar * xed_utils_escape_search_text (const gchar* text) { - GString *str; - gint length; - const gchar *p; - const gchar *end; + GString *str; + gint length; + const gchar *p; + const gchar *end; - if (text == NULL) - return NULL; + if (text == NULL) + { + return NULL; + } - xed_debug_message (DEBUG_SEARCH, "Text: %s", text); + xed_debug_message (DEBUG_SEARCH, "Text: %s", text); - length = strlen (text); + length = strlen (text); - /* no escape when typing. - * The short circuit works only for ascii, but we only - * care about not escaping a single '\' */ - if (length == 1) - return g_strdup (text); + /* no escape when typing. + * The short circuit works only for ascii, but we only + * care about not escaping a single '\' */ + if (length == 1) + { + return g_strdup (text); + } - str = g_string_new (""); + str = g_string_new (""); - p = text; - end = text + length; + p = text; + end = text + length; - while (p != end) - { - const gchar *next; - next = g_utf8_next_char (p); + while (p != end) + { + const gchar *next; + next = g_utf8_next_char (p); - switch (*p) - { - case '\n': - g_string_append (str, "\\n"); - break; - case '\r': - g_string_append (str, "\\r"); - break; - case '\t': - g_string_append (str, "\\t"); - break; - case '\\': - g_string_append (str, "\\\\"); - break; - default: - g_string_append_len (str, p, next - p); - break; - } + switch (*p) + { + case '\n': + g_string_append (str, "\\n"); + break; + case '\r': + g_string_append (str, "\\r"); + break; + case '\t': + g_string_append (str, "\\t"); + break; + case '\\': + g_string_append (str, "\\\\"); + break; + default: + g_string_append_len (str, p, next - p); + break; + } - p = next; - } + p = next; + } - return g_string_free (str, FALSE); + return g_string_free (str, FALSE); } gchar * xed_utils_unescape_search_text (const gchar *text) { - GString *str; - gint length; - gboolean drop_prev = FALSE; - const gchar *cur; - const gchar *end; - const gchar *prev; + GString *str; + gint length; + gboolean drop_prev = FALSE; + const gchar *cur; + const gchar *end; + const gchar *prev; - if (text == NULL) - return NULL; + if (text == NULL) + { + return NULL; + } - length = strlen (text); + length = strlen (text); - str = g_string_new (""); + str = g_string_new (""); - cur = text; - end = text + length; - prev = NULL; + cur = text; + end = text + length; + prev = NULL; - while (cur != end) - { - const gchar *next; - next = g_utf8_next_char (cur); + while (cur != end) + { + const gchar *next; + next = g_utf8_next_char (cur); - if (prev && (*prev == '\\')) - { - switch (*cur) - { - case 'n': - str = g_string_append (str, "\n"); - break; - case 'r': - str = g_string_append (str, "\r"); - break; - case 't': - str = g_string_append (str, "\t"); - break; - case '\\': - str = g_string_append (str, "\\"); - drop_prev = TRUE; - break; - default: - str = g_string_append (str, "\\"); - str = g_string_append_len (str, cur, next - cur); - break; - } - } - else if (*cur != '\\') - { - str = g_string_append_len (str, cur, next - cur); - } - else if ((next == end) && (*cur == '\\')) - { - str = g_string_append (str, "\\"); - } + if (prev && (*prev == '\\')) + { + switch (*cur) + { + case 'n': + str = g_string_append (str, "\n"); + break; + case 'r': + str = g_string_append (str, "\r"); + break; + case 't': + str = g_string_append (str, "\t"); + break; + case '\\': + str = g_string_append (str, "\\"); + drop_prev = TRUE; + break; + default: + str = g_string_append (str, "\\"); + str = g_string_append_len (str, cur, next - cur); + break; + } + } + else if (*cur != '\\') + { + str = g_string_append_len (str, cur, next - cur); + } + else if ((next == end) && (*cur == '\\')) + { + str = g_string_append (str, "\\"); + } - if (!drop_prev) - { - prev = cur; - } - else - { - prev = NULL; - drop_prev = FALSE; - } + if (!drop_prev) + { + prev = cur; + } + else + { + prev = NULL; + drop_prev = FALSE; + } - cur = next; - } + cur = next; + } - return g_string_free (str, FALSE); + return g_string_free (str, FALSE); } void -xed_warning (GtkWindow *parent, const gchar *format, ...) +xed_warning (GtkWindow *parent, + const gchar *format, + ...) { - va_list args; - gchar *str; - GtkWidget *dialog; - GtkWindowGroup *wg = NULL; + va_list args; + gchar *str; + GtkWidget *dialog; + GtkWindowGroup *wg = NULL; - g_return_if_fail (format != NULL); + g_return_if_fail (format != NULL); - if (parent != NULL) - wg = gtk_window_get_group (parent); + if (parent != NULL) + { + wg = gtk_window_get_group (parent); + } - va_start (args, format); - str = g_strdup_vprintf (format, args); - va_end (args); + va_start (args, format); + str = g_strdup_vprintf (format, args); + va_end (args); - dialog = gtk_message_dialog_new_with_markup ( - parent, - GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, - GTK_MESSAGE_ERROR, - GTK_BUTTONS_OK, - "%s", str); + dialog = gtk_message_dialog_new_with_markup (parent, + GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_OK, + "%s", str); - g_free (str); + g_free (str); - if (wg != NULL) - gtk_window_group_add_window (wg, GTK_WINDOW (dialog)); + if (wg != NULL) + { + gtk_window_group_add_window (wg, GTK_WINDOW (dialog)); + } - gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK); + gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK); - gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE); + gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE); - g_signal_connect (G_OBJECT (dialog), - "response", - G_CALLBACK (gtk_widget_destroy), - NULL); + g_signal_connect (G_OBJECT (dialog), "response", + G_CALLBACK (gtk_widget_destroy), NULL); - gtk_widget_show (dialog); + gtk_widget_show (dialog); } /* @@ -566,155 +581,161 @@ xed_warning (GtkWindow *parent, const gchar *format, ...) */ gchar * xed_utils_escape_underscores (const gchar* text, - gssize length) + gssize length) { - GString *str; - const gchar *p; - const gchar *end; + GString *str; + const gchar *p; + const gchar *end; - g_return_val_if_fail (text != NULL, NULL); + g_return_val_if_fail (text != NULL, NULL); - if (length < 0) - length = strlen (text); + if (length < 0) + { + length = strlen (text); + } - str = g_string_sized_new (length); + str = g_string_sized_new (length); - p = text; - end = text + length; + p = text; + end = text + length; - while (p != end) - { - const gchar *next; - next = g_utf8_next_char (p); + while (p != end) + { + const gchar *next; + next = g_utf8_next_char (p); - switch (*p) - { - case '_': - g_string_append (str, "__"); - break; - default: - g_string_append_len (str, p, next - p); - break; - } + switch (*p) + { + case '_': + g_string_append (str, "__"); + break; + default: + g_string_append_len (str, p, next - p); + break; + } - p = next; - } + p = next; + } - return g_string_free (str, FALSE); + return g_string_free (str, FALSE); } /* the following functions are taken from eel */ static gchar * xed_utils_str_truncate (const gchar *string, - guint truncate_length, - gboolean middle) + guint truncate_length, + gboolean middle) { - GString *truncated; - guint length; - guint n_chars; - guint num_left_chars; - guint right_offset; - guint delimiter_length; - const gchar *delimiter = "\342\200\246"; + GString *truncated; + guint length; + guint n_chars; + guint num_left_chars; + guint right_offset; + guint delimiter_length; + const gchar *delimiter = "\342\200\246"; - g_return_val_if_fail (string != NULL, NULL); + g_return_val_if_fail (string != NULL, NULL); - length = strlen (string); + length = strlen (string); - g_return_val_if_fail (g_utf8_validate (string, length, NULL), NULL); + g_return_val_if_fail (g_utf8_validate (string, length, NULL), NULL); - /* It doesnt make sense to truncate strings to less than - * the size of the delimiter plus 2 characters (one on each - * side) - */ - delimiter_length = g_utf8_strlen (delimiter, -1); - if (truncate_length < (delimiter_length + 2)) { - return g_strdup (string); - } + /* It doesnt make sense to truncate strings to less than + * the size of the delimiter plus 2 characters (one on each + * side) + */ + delimiter_length = g_utf8_strlen (delimiter, -1); + if (truncate_length < (delimiter_length + 2)) + { + return g_strdup (string); + } - n_chars = g_utf8_strlen (string, length); + n_chars = g_utf8_strlen (string, length); - /* Make sure the string is not already small enough. */ - if (n_chars <= truncate_length) { - return g_strdup (string); - } + /* Make sure the string is not already small enough. */ + if (n_chars <= truncate_length) + { + return g_strdup (string); + } - /* Find the 'middle' where the truncation will occur. */ - if (middle) - { - num_left_chars = (truncate_length - delimiter_length) / 2; - right_offset = n_chars - truncate_length + num_left_chars + delimiter_length; + /* Find the 'middle' where the truncation will occur. */ + if (middle) + { + num_left_chars = (truncate_length - delimiter_length) / 2; + right_offset = n_chars - truncate_length + num_left_chars + delimiter_length; - truncated = g_string_new_len (string, - g_utf8_offset_to_pointer (string, num_left_chars) - string); - g_string_append (truncated, delimiter); - g_string_append (truncated, g_utf8_offset_to_pointer (string, right_offset)); - } - else - { - num_left_chars = truncate_length - delimiter_length; - truncated = g_string_new_len (string, - g_utf8_offset_to_pointer (string, num_left_chars) - string); - g_string_append (truncated, delimiter); - } + truncated = g_string_new_len (string, g_utf8_offset_to_pointer (string, num_left_chars) - string); + g_string_append (truncated, delimiter); + g_string_append (truncated, g_utf8_offset_to_pointer (string, right_offset)); + } + else + { + num_left_chars = truncate_length - delimiter_length; + truncated = g_string_new_len (string, g_utf8_offset_to_pointer (string, num_left_chars) - string); + g_string_append (truncated, delimiter); + } - return g_string_free (truncated, FALSE); + return g_string_free (truncated, FALSE); } gchar * xed_utils_str_middle_truncate (const gchar *string, - guint truncate_length) + guint truncate_length) { - return xed_utils_str_truncate (string, truncate_length, TRUE); + return xed_utils_str_truncate (string, truncate_length, TRUE); } gchar * xed_utils_str_end_truncate (const gchar *string, - guint truncate_length) + guint truncate_length) { - return xed_utils_str_truncate (string, truncate_length, FALSE); + return xed_utils_str_truncate (string, truncate_length, FALSE); } gchar * xed_utils_make_valid_utf8 (const char *name) { - GString *string; - const char *remainder, *invalid; - int remaining_bytes, valid_bytes; + GString *string; + const char *remainder, *invalid; + int remaining_bytes, valid_bytes; - g_return_val_if_fail (name != NULL, NULL); + g_return_val_if_fail (name != NULL, NULL); - string = NULL; - remainder = name; - remaining_bytes = strlen (name); + string = NULL; + remainder = name; + remaining_bytes = strlen (name); - while (remaining_bytes != 0) { - if (g_utf8_validate (remainder, remaining_bytes, &invalid)) { - break; - } - valid_bytes = invalid - remainder; + while (remaining_bytes != 0) + { + if (g_utf8_validate (remainder, remaining_bytes, &invalid)) + { + break; + } + valid_bytes = invalid - remainder; - if (string == NULL) { - string = g_string_sized_new (remaining_bytes); - } - g_string_append_len (string, remainder, valid_bytes); - /* append U+FFFD REPLACEMENT CHARACTER */ - g_string_append (string, "\357\277\275"); + if (string == NULL) + { + string = g_string_sized_new (remaining_bytes); + } + g_string_append_len (string, remainder, valid_bytes); + /* append U+FFFD REPLACEMENT CHARACTER */ + g_string_append (string, "\357\277\275"); - remaining_bytes -= valid_bytes + 1; - remainder = invalid + 1; - } + remaining_bytes -= valid_bytes + 1; + remainder = invalid + 1; + } - if (string == NULL) { - return g_strdup (name); - } + if (string == NULL) + { + return g_strdup (name); + } - g_string_append (string, remainder); + g_string_append (string, remainder); - g_assert (g_utf8_validate (string->str, -1, NULL)); + g_assert (g_utf8_validate (string->str, -1, NULL)); - return g_string_free (string, FALSE); + return g_string_free (string, FALSE); } /** @@ -725,27 +746,27 @@ xed_utils_make_valid_utf8 (const char *name) gchar * xed_utils_uri_get_dirname (const gchar *uri) { - gchar *res; - gchar *str; + gchar *res; + gchar *str; - g_return_val_if_fail (uri != NULL, NULL); + g_return_val_if_fail (uri != NULL, NULL); - /* CHECK: does it work with uri chaining? - Paolo */ - str = g_path_get_dirname (uri); - g_return_val_if_fail (str != NULL, g_strdup (".")); + /* CHECK: does it work with uri chaining? - Paolo */ + str = g_path_get_dirname (uri); + g_return_val_if_fail (str != NULL, g_strdup (".")); - if ((strlen (str) == 1) && (*str == '.')) - { - g_free (str); + if ((strlen (str) == 1) && (*str == '.')) + { + g_free (str); - return NULL; - } + return NULL; + } - res = xed_utils_replace_home_dir_with_tilde (str); + res = xed_utils_replace_home_dir_with_tilde (str); - g_free (str); + g_free (str); - return res; + return res; } /** @@ -762,109 +783,113 @@ xed_utils_uri_get_dirname (const gchar *uri) gchar * xed_utils_location_get_dirname_for_display (GFile *location) { - gchar *uri; - gchar *res; - GMount *mount; + gchar *uri; + gchar *res; + GMount *mount; - g_return_val_if_fail (location != NULL, NULL); + g_return_val_if_fail (location != NULL, NULL); - /* we use the parse name, that is either the local path - * or an uri but which is utf8 safe */ - uri = g_file_get_parse_name (location); + /* we use the parse name, that is either the local path + * or an uri but which is utf8 safe */ + uri = g_file_get_parse_name (location); - /* FIXME: this is sync... is it a problem? */ - mount = g_file_find_enclosing_mount (location, NULL, NULL); - if (mount != NULL) - { - gchar *mount_name; - gchar *path = NULL; - gchar *dirname; + /* FIXME: this is sync... is it a problem? */ + mount = g_file_find_enclosing_mount (location, NULL, NULL); + if (mount != NULL) + { + gchar *mount_name; + gchar *path = NULL; + gchar *dirname; - mount_name = g_mount_get_name (mount); - g_object_unref (mount); + mount_name = g_mount_get_name (mount); + g_object_unref (mount); - /* obtain the "path" part of the uri */ - xed_utils_decode_uri (uri, - NULL, NULL, - NULL, NULL, - &path); + /* obtain the "path" part of the uri */ + xed_utils_decode_uri (uri, + NULL, NULL, + NULL, NULL, + &path); - if (path == NULL) - { - dirname = xed_utils_uri_get_dirname (uri); - } - else - { - dirname = xed_utils_uri_get_dirname (path); - } + if (path == NULL) + { + dirname = xed_utils_uri_get_dirname (uri); + } + else + { + dirname = xed_utils_uri_get_dirname (path); + } - if (dirname == NULL || strcmp (dirname, ".") == 0) - { - res = mount_name; - } - else - { - res = g_strdup_printf ("%s %s", mount_name, dirname); - g_free (mount_name); - } + if (dirname == NULL || strcmp (dirname, ".") == 0) + { + res = mount_name; + } + else + { + res = g_strdup_printf ("%s %s", mount_name, dirname); + g_free (mount_name); + } - g_free (path); - g_free (dirname); - } - else - { - /* fallback for local files or uris without mounts */ - res = xed_utils_uri_get_dirname (uri); - } + g_free (path); + g_free (dirname); + } + else + { + /* fallback for local files or uris without mounts */ + res = xed_utils_uri_get_dirname (uri); + } - g_free (uri); + g_free (uri); - return res; + return res; } gchar * xed_utils_replace_home_dir_with_tilde (const gchar *uri) { - gchar *tmp; - gchar *home; + gchar *tmp; + gchar *home; - g_return_val_if_fail (uri != NULL, NULL); + g_return_val_if_fail (uri != NULL, NULL); - /* Note that g_get_home_dir returns a const string */ - tmp = (gchar *)g_get_home_dir (); + /* Note that g_get_home_dir returns a const string */ + tmp = (gchar *)g_get_home_dir (); - if (tmp == NULL) - return g_strdup (uri); + if (tmp == NULL) + { + return g_strdup (uri); + } - home = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL); - if (home == NULL) - return g_strdup (uri); + home = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL); + if (home == NULL) + { + return g_strdup (uri); + } - if (strcmp (uri, home) == 0) - { - g_free (home); + if (strcmp (uri, home) == 0) + { + g_free (home); - return g_strdup ("~"); - } + return g_strdup ("~"); + } - tmp = home; - home = g_strdup_printf ("%s/", tmp); - g_free (tmp); + tmp = home; + home = g_strdup_printf ("%s/", tmp); + g_free (tmp); - if (g_str_has_prefix (uri, home)) - { - gchar *res; + if (g_str_has_prefix (uri, home)) + { + gchar *res; - res = g_strdup_printf ("~/%s", uri + strlen (home)); + res = g_strdup_printf ("~/%s", uri + strlen (home)); - g_free (home); + g_free (home); - return res; - } + return res; + } - g_free (home); + g_free (home); - return g_strdup (uri); + return g_strdup (uri); } /* the following two functions are courtesy of galeon */ @@ -883,40 +908,44 @@ guint xed_utils_get_current_workspace (GdkScreen *screen) { #ifdef GDK_WINDOWING_X11 - GdkWindow *root_win; - GdkDisplay *display; - Atom type; - gint format; - gulong nitems; - gulong bytes_after; - guint *current_desktop; - gint err, result; - guint ret = 0; + GdkWindow *root_win; + GdkDisplay *display; + Atom type; + gint format; + gulong nitems; + gulong bytes_after; + guint *current_desktop; + gint err, result; + guint ret = 0; - g_return_val_if_fail (GDK_IS_SCREEN (screen), 0); + g_return_val_if_fail (GDK_IS_SCREEN (screen), 0); - root_win = gdk_screen_get_root_window (screen); - display = gdk_screen_get_display (screen); + root_win = gdk_screen_get_root_window (screen); + display = gdk_screen_get_display (screen); - gdk_error_trap_push (); - result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (root_win), - gdk_x11_get_xatom_by_name_for_display (display, "_NET_CURRENT_DESKTOP"), - 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems, - &bytes_after, (gpointer) ¤t_desktop); - err = gdk_error_trap_pop (); + gdk_error_trap_push (); + result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (root_win), + gdk_x11_get_xatom_by_name_for_display (display, "_NET_CURRENT_DESKTOP"), + 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems, + &bytes_after, (gpointer) ¤t_desktop); + err = gdk_error_trap_pop (); - if (err != Success || result != Success) - return ret; + if (err != Success || result != Success) + { + return ret; + } - if (type == XA_CARDINAL && format == 32 && nitems > 0) - ret = current_desktop[0]; + if (type == XA_CARDINAL && format == 32 && nitems > 0) + { + ret = current_desktop[0]; + } - XFree (current_desktop); - return ret; + XFree (current_desktop); + return ret; #else - /* FIXME: on mac etc proably there are native APIs - * to get the current workspace etc */ - return 0; + /* FIXME: on mac etc proably there are native APIs + * to get the current workspace etc */ + return 0; #endif } @@ -933,41 +962,45 @@ guint xed_utils_get_window_workspace (GtkWindow *gtkwindow) { #ifdef GDK_WINDOWING_X11 - GdkWindow *window; - GdkDisplay *display; - Atom type; - gint format; - gulong nitems; - gulong bytes_after; - guint *workspace; - gint err, result; - guint ret = XED_ALL_WORKSPACES; + GdkWindow *window; + GdkDisplay *display; + Atom type; + gint format; + gulong nitems; + gulong bytes_after; + guint *workspace; + gint err, result; + guint ret = XED_ALL_WORKSPACES; - g_return_val_if_fail (GTK_IS_WINDOW (gtkwindow), 0); - g_return_val_if_fail (gtk_widget_get_realized (GTK_WIDGET (gtkwindow)), 0); + g_return_val_if_fail (GTK_IS_WINDOW (gtkwindow), 0); + g_return_val_if_fail (gtk_widget_get_realized (GTK_WIDGET (gtkwindow)), 0); - window = gtk_widget_get_window (GTK_WIDGET (gtkwindow)); - display = gdk_window_get_display (window); + window = gtk_widget_get_window (GTK_WIDGET (gtkwindow)); + display = gdk_window_get_display (window); - gdk_error_trap_push (); - result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window), - gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"), - 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems, - &bytes_after, (gpointer) &workspace); - err = gdk_error_trap_pop (); + gdk_error_trap_push (); + result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window), + gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"), + 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems, + &bytes_after, (gpointer) &workspace); + err = gdk_error_trap_pop (); - if (err != Success || result != Success) - return ret; + if (err != Success || result != Success) + { + return ret; + } - if (type == XA_CARDINAL && format == 32 && nitems > 0) - ret = workspace[0]; + if (type == XA_CARDINAL && format == 32 && nitems > 0) + { + ret = workspace[0]; + } - XFree (workspace); - return ret; + XFree (workspace); + return ret; #else - /* FIXME: on mac etc proably there are native APIs - * to get the current workspace etc */ - return 0; + /* FIXME: on mac etc proably there are native APIs + * to get the current workspace etc */ + return 0; #endif } @@ -982,144 +1015,160 @@ xed_utils_get_window_workspace (GtkWindow *gtkwindow) */ void xed_utils_get_current_viewport (GdkScreen *screen, - gint *x, - gint *y) + gint *x, + gint *y) { #ifdef GDK_WINDOWING_X11 - GdkWindow *root_win; - GdkDisplay *display; - Atom type; - gint format; - gulong nitems; - gulong bytes_after; - gulong *coordinates; - gint err, result; + GdkWindow *root_win; + GdkDisplay *display; + Atom type; + gint format; + gulong nitems; + gulong bytes_after; + gulong *coordinates; + gint err, result; - g_return_if_fail (GDK_IS_SCREEN (screen)); - g_return_if_fail (x != NULL && y != NULL); + g_return_if_fail (GDK_IS_SCREEN (screen)); + g_return_if_fail (x != NULL && y != NULL); - /* Default values for the viewport origin */ - *x = 0; - *y = 0; + /* Default values for the viewport origin */ + *x = 0; + *y = 0; - root_win = gdk_screen_get_root_window (screen); - display = gdk_screen_get_display (screen); + root_win = gdk_screen_get_root_window (screen); + display = gdk_screen_get_display (screen); - gdk_error_trap_push (); - result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (root_win), - gdk_x11_get_xatom_by_name_for_display (display, "_NET_DESKTOP_VIEWPORT"), - 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems, - &bytes_after, (void*) &coordinates); - err = gdk_error_trap_pop (); + gdk_error_trap_push (); + result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (root_win), + gdk_x11_get_xatom_by_name_for_display (display, "_NET_DESKTOP_VIEWPORT"), + 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems, + &bytes_after, (void*) &coordinates); + err = gdk_error_trap_pop (); - if (err != Success || result != Success) - return; + if (err != Success || result != Success) + { + return; + } - if (type != XA_CARDINAL || format != 32 || nitems < 2) - { - XFree (coordinates); - return; - } + if (type != XA_CARDINAL || format != 32 || nitems < 2) + { + XFree (coordinates); + return; + } - *x = coordinates[0]; - *y = coordinates[1]; - XFree (coordinates); + *x = coordinates[0]; + *y = coordinates[1]; + XFree (coordinates); #else - /* FIXME: on mac etc proably there are native APIs - * to get the current workspace etc */ - *x = 0; - *y = 0; + /* FIXME: on mac etc proably there are native APIs + * to get the current workspace etc */ + *x = 0; + *y = 0; #endif } static gboolean is_valid_scheme_character (gchar c) { - return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.'; + return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.'; } static gboolean has_valid_scheme (const gchar *uri) { - const gchar *p; + const gchar *p; - p = uri; + p = uri; - if (!is_valid_scheme_character (*p)) { - return FALSE; - } + if (!is_valid_scheme_character (*p)) + { + return FALSE; + } - do { - p++; - } while (is_valid_scheme_character (*p)); + do + { + p++; + } while (is_valid_scheme_character (*p)); - return *p == ':'; + return *p == ':'; } gboolean xed_utils_is_valid_uri (const gchar *uri) { - const guchar *p; + const guchar *p; - if (uri == NULL) - return FALSE; + if (uri == NULL) + { + return FALSE; + } - if (!has_valid_scheme (uri)) - return FALSE; + if (!has_valid_scheme (uri)) + { + return FALSE; + } - /* We expect to have a fully valid set of characters */ - for (p = (const guchar *)uri; *p; p++) { - if (*p == '%') - { - ++p; - if (!g_ascii_isxdigit (*p)) - return FALSE; + /* We expect to have a fully valid set of characters */ + for (p = (const guchar *)uri; *p; p++) + { + if (*p == '%') + { + ++p; + if (!g_ascii_isxdigit (*p)) + { + return FALSE; + } - ++p; - if (!g_ascii_isxdigit (*p)) - return FALSE; - } - else - { - if (*p <= 32 || *p >= 128) - return FALSE; - } - } + ++p; + if (!g_ascii_isxdigit (*p)) + { + return FALSE; + } + } + else + { + if (*p <= 32 || *p >= 128) + { + return FALSE; + } + } + } - return TRUE; + return TRUE; } static GtkWidget * -handle_builder_error (const gchar *message, ...) +handle_builder_error (const gchar *message, + ...) { - GtkWidget *label; - gchar *msg; - gchar *msg_plain; - va_list args; + GtkWidget *label; + gchar *msg; + gchar *msg_plain; + va_list args; - va_start (args, message); - msg_plain = g_strdup_vprintf (message, args); - va_end (args); + va_start (args, message); + msg_plain = g_strdup_vprintf (message, args); + va_end (args); - label = gtk_label_new (NULL); - gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); + label = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); - msg = g_strconcat ("", - msg_plain, "\n\n", - _("Please check your installation."), - NULL); + msg = g_strconcat ("", + msg_plain, "\n\n", + _("Please check your installation."), + NULL); - gtk_label_set_markup (GTK_LABEL (label), msg); + gtk_label_set_markup (GTK_LABEL (label), msg); - g_free (msg_plain); - g_free (msg); + g_free (msg_plain); + g_free (msg); - gtk_widget_set_margin_left (label, 5); - gtk_widget_set_margin_right (label, 5); - gtk_widget_set_margin_top (label, 5); - gtk_widget_set_margin_bottom (label, 5); + gtk_widget_set_margin_left (label, 5); + gtk_widget_set_margin_right (label, 5); + gtk_widget_set_margin_top (label, 5); + gtk_widget_set_margin_bottom (label, 5); - return label; + return label; } /* FIXME this is an issue for introspection */ @@ -1141,129 +1190,126 @@ handle_builder_error (const gchar *message, ...) */ gboolean xed_utils_get_ui_objects (const gchar *filename, - gchar **root_objects, - GtkWidget **error_widget, - const gchar *object_name, - ...) + gchar **root_objects, + GtkWidget **error_widget, + const gchar *object_name, + ...) { - GtkBuilder *builder; - va_list args; - const gchar *name; - GError *error = NULL; - gchar *filename_markup; - gboolean ret = TRUE; + GtkBuilder *builder; + va_list args; + const gchar *name; + GError *error = NULL; + gchar *filename_markup; + gboolean ret = TRUE; - g_return_val_if_fail (filename != NULL, FALSE); - g_return_val_if_fail (error_widget != NULL, FALSE); - g_return_val_if_fail (object_name != NULL, FALSE); + g_return_val_if_fail (filename != NULL, FALSE); + g_return_val_if_fail (error_widget != NULL, FALSE); + g_return_val_if_fail (object_name != NULL, FALSE); - filename_markup = g_markup_printf_escaped ("%s", filename); - *error_widget = NULL; + filename_markup = g_markup_printf_escaped ("%s", filename); + *error_widget = NULL; - builder = gtk_builder_new (); + builder = gtk_builder_new (); - if (root_objects != NULL) - { - gtk_builder_add_objects_from_file (builder, - filename, - root_objects, - &error); - } - else - { - gtk_builder_add_from_file (builder, - filename, - &error); - } + if (root_objects != NULL) + { + gtk_builder_add_objects_from_file (builder, filename, root_objects, &error); + } + else + { + gtk_builder_add_from_file (builder, filename, &error); + } - if (error != NULL) - { - *error_widget = handle_builder_error (_("Unable to open UI file %s. Error: %s"), - filename_markup, - error->message); - g_error_free (error); - g_free (filename_markup); - g_object_unref (builder); + if (error != NULL) + { + *error_widget = handle_builder_error (_("Unable to open UI file %s. Error: %s"), + filename_markup, + error->message); + g_error_free (error); + g_free (filename_markup); + g_object_unref (builder); - return FALSE; - } + return FALSE; + } - va_start (args, object_name); - for (name = object_name; name; name = va_arg (args, const gchar *) ) - { - GObject **gobj; + va_start (args, object_name); + for (name = object_name; name; name = va_arg (args, const gchar *) ) + { + GObject **gobj; - gobj = va_arg (args, GObject **); - *gobj = gtk_builder_get_object (builder, name); + gobj = va_arg (args, GObject **); + *gobj = gtk_builder_get_object (builder, name); - if (!*gobj) - { - *error_widget = handle_builder_error (_("Unable to find the object '%s' inside file %s."), - name, - filename_markup), - ret = FALSE; - break; - } + if (!*gobj) + { + *error_widget = handle_builder_error (_("Unable to find the object '%s' inside file %s."), + name, + filename_markup), + ret = FALSE; + break; + } - /* we return a new ref for the root objects, - * the others are already reffed by their parent root object */ - if (root_objects != NULL) - { - gint i; + /* we return a new ref for the root objects, + * the others are already reffed by their parent root object */ + if (root_objects != NULL) + { + gint i; - for (i = 0; root_objects[i] != NULL; ++i) - { - if ((strcmp (name, root_objects[i]) == 0)) - { - g_object_ref (*gobj); - } - } - } - } - va_end (args); + for (i = 0; root_objects[i] != NULL; ++i) + { + if ((strcmp (name, root_objects[i]) == 0)) + { + g_object_ref (*gobj); + } + } + } + } + va_end (args); - g_free (filename_markup); - g_object_unref (builder); + g_free (filename_markup); + g_object_unref (builder); - return ret; + return ret; } gchar * xed_utils_make_canonical_uri_from_shell_arg (const gchar *str) { - GFile *gfile; - gchar *uri; + GFile *gfile; + gchar *uri; - g_return_val_if_fail (str != NULL, NULL); - g_return_val_if_fail (*str != '\0', NULL); + g_return_val_if_fail (str != NULL, NULL); + g_return_val_if_fail (*str != '\0', NULL); - /* Note for the future: - * FIXME: is still still relevant? - * - * paolo: and flame whoever tells - * you that file:///mate/test_files/hëllò - * doesn't work --- that's not a valid URI - * - * federico: well, another solution that - * does not requires patch to _from_shell_args - * is to check that the string returned by it - * contains only ASCII chars - * paolo: hmmmm, isn't there - * mate_vfs_is_uri_valid() or something? - * : I will use xed_utils_is_valid_uri () - * - */ + /* Note for the future: + * FIXME: is still still relevant? + * + * paolo: and flame whoever tells + * you that file:///mate/test_files/hëllò + * doesn't work --- that's not a valid URI + * + * federico: well, another solution that + * does not requires patch to _from_shell_args + * is to check that the string returned by it + * contains only ASCII chars + * paolo: hmmmm, isn't there + * mate_vfs_is_uri_valid() or something? + * : I will use xed_utils_is_valid_uri () + * + */ - gfile = g_file_new_for_commandline_arg (str); - uri = g_file_get_uri (gfile); - g_object_unref (gfile); + gfile = g_file_new_for_commandline_arg (str); + uri = g_file_get_uri (gfile); + g_object_unref (gfile); - if (xed_utils_is_valid_uri (uri)) - return uri; + if (xed_utils_is_valid_uri (uri)) + { + return uri; + } - g_free (uri); - return NULL; + g_free (uri); + return NULL; } /** @@ -1276,16 +1322,18 @@ xed_utils_make_canonical_uri_from_shell_arg (const gchar *str) gboolean xed_utils_file_has_parent (GFile *gfile) { - GFile *parent; - gboolean ret; + GFile *parent; + gboolean ret; - parent = g_file_get_parent (gfile); - ret = parent != NULL; + parent = g_file_get_parent (gfile); + ret = parent != NULL; - if (parent) - g_object_unref (parent); + if (parent) + { + g_object_unref (parent); + } - return ret; + return ret; } /** @@ -1297,77 +1345,81 @@ xed_utils_file_has_parent (GFile *gfile) gchar * xed_utils_basename_for_display (gchar const *uri) { - gchar *name; - GFile *gfile; - gchar *hn; + gchar *name; + GFile *gfile; + gchar *hn; - g_return_val_if_fail (uri != NULL, NULL); + g_return_val_if_fail (uri != NULL, NULL); - gfile = g_file_new_for_uri (uri); + gfile = g_file_new_for_uri (uri); - /* First, try to query the display name, but only on local files */ - if (g_file_has_uri_scheme (gfile, "file")) - { - GFileInfo *info; - info = g_file_query_info (gfile, - G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, - G_FILE_QUERY_INFO_NONE, - NULL, - NULL); + /* First, try to query the display name, but only on local files */ + if (g_file_has_uri_scheme (gfile, "file")) + { + GFileInfo *info; + info = g_file_query_info (gfile, + G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, + G_FILE_QUERY_INFO_NONE, + NULL, + NULL); - if (info) - { - /* Simply get the display name to use as the basename */ - name = g_strdup (g_file_info_get_display_name (info)); - g_object_unref (info); - } - else - { - /* This is a local file, and therefore we will use - * g_filename_display_basename on the local path */ - gchar *local_path; + if (info) + { + /* Simply get the display name to use as the basename */ + name = g_strdup (g_file_info_get_display_name (info)); + g_object_unref (info); + } + else + { + /* This is a local file, and therefore we will use + * g_filename_display_basename on the local path */ + gchar *local_path; - local_path = g_file_get_path (gfile); - name = g_filename_display_basename (local_path); - g_free (local_path); - } - } - else if (xed_utils_file_has_parent (gfile) || !xed_utils_decode_uri (uri, NULL, NULL, &hn, NULL, NULL)) - { - /* For remote files with a parent (so not just http://foo.com) - or remote file for which the decoding of the host name fails, - use the _parse_name and take basename of that */ - gchar *parse_name; - gchar *base; + local_path = g_file_get_path (gfile); + name = g_filename_display_basename (local_path); + g_free (local_path); + } + } + else if (xed_utils_file_has_parent (gfile) || !xed_utils_decode_uri (uri, NULL, NULL, &hn, NULL, NULL)) + { + /* For remote files with a parent (so not just http://foo.com) + or remote file for which the decoding of the host name fails, + use the _parse_name and take basename of that */ + gchar *parse_name; + gchar *base; - parse_name = g_file_get_parse_name (gfile); - base = g_filename_display_basename (parse_name); - name = g_uri_unescape_string (base, NULL); + parse_name = g_file_get_parse_name (gfile); + base = g_filename_display_basename (parse_name); + name = g_uri_unescape_string (base, NULL); - g_free (base); - g_free (parse_name); - } - else - { - /* display '/ on ' using the decoded host */ - gchar *hn_utf8; + g_free (base); + g_free (parse_name); + } + else + { + /* display '/ on ' using the decoded host */ + gchar *hn_utf8; - if (hn != NULL) - hn_utf8 = xed_utils_make_valid_utf8 (hn); - else - /* we should never get here */ - hn_utf8 = g_strdup ("?"); + if (hn != NULL) + { + hn_utf8 = xed_utils_make_valid_utf8 (hn); + } + else + { + /* we should never get here */ + hn_utf8 = g_strdup ("?"); + } - /* Translators: '/ on ' */ - name = g_strdup_printf (_("/ on %s"), hn_utf8); + /* Translators: '/ on ' */ + name = g_strdup_printf (_("/ on %s"), hn_utf8); - g_free (hn_utf8); - g_free (hn); - } + g_free (hn_utf8); + g_free (hn); + } - g_object_unref (gfile); + g_object_unref (gfile); - return name; + return name; } /** @@ -1384,14 +1436,14 @@ xed_utils_basename_for_display (gchar const *uri) gchar * xed_utils_uri_for_display (const gchar *uri) { - GFile *gfile; - gchar *parse_name; + GFile *gfile; + gchar *parse_name; - gfile = g_file_new_for_uri (uri); - parse_name = g_file_get_parse_name (gfile); - g_object_unref (gfile); + gfile = g_file_new_for_uri (uri); + parse_name = g_file_get_parse_name (gfile); + g_object_unref (gfile); - return parse_name; + return parse_name; } /** @@ -1401,47 +1453,51 @@ xed_utils_uri_for_display (const gchar *uri) * Create a list of valid uri's from a uri-list drop. * * Return value: (transfer full): a string array which will hold the uris or %NULL if there - * were no valid uris. g_strfreev should be used when the - * string array is no longer used + * were no valid uris. g_strfreev should be used when the + * string array is no longer used */ gchar ** xed_utils_drop_get_uris (GtkSelectionData *selection_data) { - gchar **uris; - gint i; - gint p = 0; - gchar **uri_list; + gchar **uris; + gint i; + gint p = 0; + gchar **uri_list; - uris = g_uri_list_extract_uris ((gchar *) gtk_selection_data_get_data (selection_data)); - uri_list = g_new0(gchar *, g_strv_length (uris) + 1); + uris = g_uri_list_extract_uris ((gchar *) gtk_selection_data_get_data (selection_data)); + uri_list = g_new0(gchar *, g_strv_length (uris) + 1); - for (i = 0; uris[i] != NULL; i++) - { - gchar *uri; + for (i = 0; uris[i] != NULL; i++) + { + gchar *uri; - uri = xed_utils_make_canonical_uri_from_shell_arg (uris[i]); + uri = xed_utils_make_canonical_uri_from_shell_arg (uris[i]); - /* Silently ignore malformed URI/filename */ - if (uri != NULL) - uri_list[p++] = uri; - } + /* Silently ignore malformed URI/filename */ + if (uri != NULL) + { + uri_list[p++] = uri; + } + } - g_strfreev (uris); + g_strfreev (uris); - if (*uri_list == NULL) - { - g_free(uri_list); - return NULL; - } + if (*uri_list == NULL) + { + g_free(uri_list); + return NULL; + } - return uri_list; + return uri_list; } static void null_ptr (gchar **ptr) { - if (ptr) - *ptr = NULL; + if (ptr) + { + *ptr = NULL; + } } /** @@ -1463,128 +1519,149 @@ null_ptr (gchar **ptr) */ gboolean xed_utils_decode_uri (const gchar *uri, - gchar **scheme, - gchar **user, - gchar **host, - gchar **port, - gchar **path + gchar **scheme, + gchar **user, + gchar **host, + gchar **port, + gchar **path ) { - /* Largely copied from glib/gio/gdummyfile.c:_g_decode_uri. This - * functionality should be in glib/gio, but for now we implement it - * ourselves (see bug #546182) */ + /* Largely copied from glib/gio/gdummyfile.c:_g_decode_uri. This + * functionality should be in glib/gio, but for now we implement it + * ourselves (see bug #546182) */ - const char *p, *in, *hier_part_start, *hier_part_end; - char *out; - char c; + const char *p, *in, *hier_part_start, *hier_part_end; + char *out; + char c; - /* From RFC 3986 Decodes: - * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] - */ + /* From RFC 3986 Decodes: + * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] + */ - p = uri; + p = uri; - null_ptr (scheme); - null_ptr (user); - null_ptr (port); - null_ptr (host); - null_ptr (path); + null_ptr (scheme); + null_ptr (user); + null_ptr (port); + null_ptr (host); + null_ptr (path); - /* Decode scheme: - * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) - */ + /* Decode scheme: + * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) + */ - if (!g_ascii_isalpha (*p)) - return FALSE; + if (!g_ascii_isalpha (*p)) + { + return FALSE; + } - while (1) - { - c = *p++; + while (1) + { + c = *p++; - if (c == ':') - break; + if (c == ':') + { + break; + } - if (!(g_ascii_isalnum(c) || - c == '+' || - c == '-' || - c == '.')) - return FALSE; - } + if (!(g_ascii_isalnum(c) || c == '+' || c == '-' || c == '.')) + { + return FALSE; + } + } - if (scheme) - { - *scheme = g_malloc (p - uri); - out = *scheme; + if (scheme) + { + *scheme = g_malloc (p - uri); + out = *scheme; - for (in = uri; in < p - 1; in++) - *out++ = g_ascii_tolower (*in); + for (in = uri; in < p - 1; in++) + { + *out++ = g_ascii_tolower (*in); + } - *out = '\0'; - } + *out = '\0'; + } - hier_part_start = p; - hier_part_end = p + strlen (p); + hier_part_start = p; + hier_part_end = p + strlen (p); - if (hier_part_start[0] == '/' && hier_part_start[1] == '/') - { - const char *authority_start, *authority_end; - const char *userinfo_start, *userinfo_end; - const char *host_start, *host_end; - const char *port_start; + if (hier_part_start[0] == '/' && hier_part_start[1] == '/') + { + const char *authority_start, *authority_end; + const char *userinfo_start, *userinfo_end; + const char *host_start, *host_end; + const char *port_start; - authority_start = hier_part_start + 2; - /* authority is always followed by / or nothing */ - authority_end = memchr (authority_start, '/', hier_part_end - authority_start); + authority_start = hier_part_start + 2; + /* authority is always followed by / or nothing */ + authority_end = memchr (authority_start, '/', hier_part_end - authority_start); - if (authority_end == NULL) - authority_end = hier_part_end; + if (authority_end == NULL) + { + authority_end = hier_part_end; + } - /* 3.2: - * authority = [ userinfo "@" ] host [ ":" port ] - */ + /* 3.2: + * authority = [ userinfo "@" ] host [ ":" port ] + */ - userinfo_end = memchr (authority_start, '@', authority_end - authority_start); + userinfo_end = memchr (authority_start, '@', authority_end - authority_start); - if (userinfo_end) - { - userinfo_start = authority_start; + if (userinfo_end) + { + userinfo_start = authority_start; - if (user) - *user = g_uri_unescape_segment (userinfo_start, userinfo_end, NULL); + if (user) + { + *user = g_uri_unescape_segment (userinfo_start, userinfo_end, NULL); + } - if (user && *user == NULL) - { - if (scheme) - g_free (*scheme); + if (user && *user == NULL) + { + if (scheme) + { + g_free (*scheme); + } - return FALSE; - } + return FALSE; + } - host_start = userinfo_end + 1; - } - else - host_start = authority_start; + host_start = userinfo_end + 1; + } + else + { + host_start = authority_start; + } - port_start = memchr (host_start, ':', authority_end - host_start); + port_start = memchr (host_start, ':', authority_end - host_start); - if (port_start) - { - host_end = port_start++; + if (port_start) + { + host_end = port_start++; - if (port) - *port = g_strndup (port_start, authority_end - port_start); - } - else - host_end = authority_end; + if (port) + { + *port = g_strndup (port_start, authority_end - port_start); + } + } + else + { + host_end = authority_end; + } - if (host) - *host = g_strndup (host_start, host_end - host_start); + if (host) + { + *host = g_strndup (host_start, host_end - host_start); + } - hier_part_start = authority_end; - } + hier_part_start = authority_end; + } - if (path) - *path = g_uri_unescape_segment (hier_part_start, hier_part_end, "/"); + if (path) + { + *path = g_uri_unescape_segment (hier_part_start, hier_part_end, "/"); + } - return TRUE; + return TRUE; }