From dcd507ad2b2c6bdef6c7f8588208122bbede826b Mon Sep 17 00:00:00 2001 From: jhs Date: Thu, 1 May 2008 10:16:13 +0000 Subject: [PATCH] 2008-05-01 Johannes Schmid * libanjuta/anjuta-utils.c (anjuta_util_uri_get_dirname), (anjuta_util_replace_home_dir_with_tilde), (anjuta_util_str_middle_truncate): * libanjuta/anjuta-utils.h: Added some utility methods (derived from gedit) * plugins/document-manager/plugin.c (get_directory_display_name), (update_title): Really fix #530033 now git-svn-id: http://svn.gnome.org/svn/anjuta/trunk@3907 1dbfb86a-d425-0410-a06b-cb591aac69f6 --- ChangeLog | 12 ++++ libanjuta/anjuta-utils.c | 118 ++++++++++++++++++++++++++++++++++++++ libanjuta/anjuta-utils.h | 8 ++- plugins/document-manager/plugin.c | 80 ++++++++++++-------------- 4 files changed, 174 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index 85d3801d..3b30ebc7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-05-01 Johannes Schmid + + * libanjuta/anjuta-utils.c (anjuta_util_uri_get_dirname), + (anjuta_util_replace_home_dir_with_tilde), + (anjuta_util_str_middle_truncate): + * libanjuta/anjuta-utils.h: + Added some utility methods (derived from gedit) + + * plugins/document-manager/plugin.c (get_directory_display_name), + (update_title): + Really fix #530033 now + 2008-05-01 Massimo Cora' * plugins/symbol-db/plugin.c (on_single_file_scan_end): diff --git a/libanjuta/anjuta-utils.c b/libanjuta/anjuta-utils.c index 628d0f78..391722be 100644 --- a/libanjuta/anjuta-utils.c +++ b/libanjuta/anjuta-utils.c @@ -1435,3 +1435,121 @@ anjuta_util_get_user_config_dir () return folder; } +/* The following functions are taken from gedit */ + +/* Note that this function replace home dir with ~ */ +gchar * +anjuta_util_uri_get_dirname (const gchar *uri) +{ + gchar *res; + gchar *str; + + // CHECK: does it work with uri chaining? - Paolo + str = g_path_get_dirname (uri); + g_return_val_if_fail (str != NULL, "."); + + if ((strlen (str) == 1) && (*str == '.')) + { + g_free (str); + + return NULL; + } + + res = anjuta_util_replace_home_dir_with_tilde (str); + + g_free (str); + + return res; +} + +gchar* +anjuta_util_replace_home_dir_with_tilde (const gchar *uri) +{ + gchar *tmp; + gchar *home; + + g_return_val_if_fail (uri != NULL, NULL); + + /* Note that g_get_home_dir returns a const string */ + tmp = (gchar *)g_get_home_dir (); + + if (tmp == 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); + + return g_strdup ("~"); + } + + tmp = home; + home = g_strdup_printf ("%s/", tmp); + g_free (tmp); + + if (g_str_has_prefix (uri, home)) + { + gchar *res; + + res = g_strdup_printf ("~/%s", uri + strlen (home)); + + g_free (home); + + return res; + } + + g_free (home); + + return g_strdup (uri); +} + +gchar * +anjuta_util_str_middle_truncate (const gchar *string, + guint truncate_length) +{ + 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); + + length = strlen (string); + + 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); + } + + 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); + } + + /* Find the 'middle' where the truncation will occur. */ + 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)); + + return g_string_free (truncated, FALSE); +} + diff --git a/libanjuta/anjuta-utils.h b/libanjuta/anjuta-utils.h index 6633c49d..d74a1138 100644 --- a/libanjuta/anjuta-utils.h +++ b/libanjuta/anjuta-utils.h @@ -111,13 +111,19 @@ gchar* anjuta_util_escape_quotes(const gchar* str); gboolean anjuta_util_path_has_extension (const gchar *path, const gchar *ext); gchar* anjuta_util_get_real_path (const gchar *path); +gchar* anjuta_util_uri_get_dirname (const gchar *uri); +gchar* anjuta_util_replace_home_dir_with_tilde (const gchar *uri); +gchar* anjuta_util_str_middle_truncate (const gchar *string, + guint truncate_length); + gchar* anjuta_util_get_uri_mime_type (const gchar *uri); void anjuta_util_help_display (GtkWindow *parent, const gchar *doc_id, const gchar *file_name); -gchar *anjuta_util_get_user_config_dir (); +gchar *anjuta_util_get_user_config_dir (void); + /* Temporarily copied here */ diff --git a/plugins/document-manager/plugin.c b/plugins/document-manager/plugin.c index 7ae7ef8e..4b1a918d 100644 --- a/plugins/document-manager/plugin.c +++ b/plugins/document-manager/plugin.c @@ -449,76 +449,70 @@ static struct ActionToggleGroupInfo action_toggle_groups[] = { #define VIEW_EOL "view.eol" #define VIEW_LINE_WRAP "view.line.wrap" +#define MAX_TITLE_LENGTH 80 + +static gchar* +get_directory_display_name (DocmanPlugin* plugin, + const gchar* uri) +{ + gchar* dir; + gchar* display_uri = display_uri = gnome_vfs_format_uri_for_display (uri); + gchar* display_dir; + dir = anjuta_util_uri_get_dirname (display_uri); + + display_dir = anjuta_util_str_middle_truncate (dir, + MAX (20, MAX_TITLE_LENGTH)); + g_free (display_uri); + g_free (dir); + return display_dir; +} + static void update_title (DocmanPlugin* doc_plugin) { IAnjutaDocument *doc; AnjutaStatus *status; - gchar *filename; gchar *title; doc = anjuta_docman_get_current_document (ANJUTA_DOCMAN (doc_plugin->docman)); if (doc) { + gchar* real_filename; + gchar *dir; gchar *uri; + const gchar *filename; + filename = ianjuta_document_get_filename (doc, NULL); uri = ianjuta_file_get_uri (IANJUTA_FILE (doc), NULL); - if (uri) + dir = get_directory_display_name (doc_plugin, uri); + g_free (uri); + if (ianjuta_file_savable_is_dirty(IANJUTA_FILE_SAVABLE (doc), NULL)) { - filename = gnome_vfs_get_local_path_from_uri (uri); - g_free (uri); + gchar* dirty_name = g_strconcat ("*", filename, NULL); + real_filename = dirty_name; } else - filename = NULL; - } - else - filename = NULL; - - if (filename && doc_plugin->project_name) - { - gchar *display_filename = NULL; - const gchar *home = g_get_home_dir(); - if (doc_plugin->project_path) + real_filename = g_strdup (filename); + + if (doc_plugin->project_name) { - if (g_str_has_prefix (filename, doc_plugin->project_path)) - { - /* the +1 is the '/' */ - display_filename = filename + strlen (doc_plugin->project_path) + 1; - } + title = g_strdup_printf ("%s (%s) - %s", real_filename, dir, + doc_plugin->project_name); } - if (!display_filename && - g_str_has_prefix (filename, home)) + else { - filename[strlen (home) - 1] = '~'; - display_filename = filename + strlen (home) - 1; + title = g_strdup_printf ("%s (%s)", real_filename, dir); } - if (!display_filename) - display_filename = filename; - title = g_strconcat (display_filename, " - ", doc_plugin->project_name, NULL); - } - else if (filename) - { - title = g_strdup (filename); - } - else if (doc_plugin->project_name) - { - title = g_strdup (doc_plugin->project_name); + g_free (real_filename); + g_free (dir); } else - title = NULL; - - if (title && doc && - ianjuta_file_savable_is_dirty(IANJUTA_FILE_SAVABLE (doc), NULL)) { - gchar* dirty_title = g_strconcat ("*", title, NULL); - g_free(title); - title = dirty_title; + title = g_strdup (doc_plugin->project_name); } - status = anjuta_shell_get_status (ANJUTA_PLUGIN (doc_plugin)->shell, NULL); /* NULL title is ok */ anjuta_status_set_title (status, title); - g_free (filename); g_free (title); } -- 2.11.4.GIT