[evince] shell: Use display and edit name for filename



commit fc9370419720915953c5e97fe44c561ea78bb6fa
Author: Jason Crain <jcrain src gnome org>
Date:   Sun Apr 1 11:17:39 2018 -0500

    shell: Use display and edit name for filename
    
    Files on Google Drive have randomly generated URIs. Instead of using the
    URI, show the display-name in the UI for the password view and window
    title, and use the edit-name when saving and printing.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=756354

 shell/ev-password-view.c |   39 +++++++++------------------
 shell/ev-password-view.h |    4 +-
 shell/ev-window-title.c  |   39 ++++++++-------------------
 shell/ev-window-title.h  |    4 +-
 shell/ev-window.c        |   65 ++++++++++++++++++++++++++++++++++-----------
 5 files changed, 77 insertions(+), 74 deletions(-)
---
diff --git a/shell/ev-password-view.c b/shell/ev-password-view.c
index 181102c..1d7d4d4 100644
--- a/shell/ev-password-view.c
+++ b/shell/ev-password-view.c
@@ -42,7 +42,7 @@ struct _EvPasswordViewPrivate {
        gchar        *password;
        GPasswordSave password_save;
 
-       GFile        *uri_file;
+       char         *filename;
 };
 
 #define EV_PASSWORD_VIEW_GET_PRIVATE(object) \
@@ -65,10 +65,7 @@ ev_password_view_finalize (GObject *object)
 
        password_view->priv->parent_window = NULL;
 
-       if (password_view->priv->uri_file) {
-               g_object_unref (password_view->priv->uri_file);
-               password_view->priv->uri_file = NULL;
-       }
+       g_clear_pointer (&password_view->priv->filename, g_free);
 
        G_OBJECT_CLASS (ev_password_view_parent_class)->finalize (object);
 }
@@ -158,30 +155,22 @@ ev_password_view_init (EvPasswordView *password_view)
 
 /* Public functions */
 void
-ev_password_view_set_uri (EvPasswordView *password_view,
-                         const char     *uri)
+ev_password_view_set_filename (EvPasswordView *password_view,
+                              const char     *filename)
 {
-       gchar *markup, *file_name;
-       GFile *file;
+       gchar *markup;
 
        g_return_if_fail (EV_IS_PASSWORD_VIEW (password_view));
-       g_return_if_fail (uri != NULL);
+       g_return_if_fail (filename != NULL);
 
-       file = g_file_new_for_uri (uri);
-       if (password_view->priv->uri_file &&
-           g_file_equal (file, password_view->priv->uri_file)) {
-               g_object_unref (file);
+       if (g_strcmp0 (password_view->priv->filename, filename) == 0)
                return;
-       }
-       if (password_view->priv->uri_file)
-               g_object_unref (password_view->priv->uri_file);
-       password_view->priv->uri_file = file;
 
-       file_name = g_file_get_basename (password_view->priv->uri_file);
-       markup = g_markup_printf_escaped ("<span size=\"x-large\" weight=\"bold\">%s</span>",
-                                         file_name);
-       g_free (file_name);
+       g_free (password_view->priv->filename);
+       password_view->priv->filename = g_strdup (filename);
 
+       markup = g_markup_printf_escaped ("<span size=\"x-large\" weight=\"bold\">%s</span>",
+                                         filename);
        gtk_label_set_markup (GTK_LABEL (password_view->priv->label), markup);
        g_free (markup);
 }
@@ -244,7 +233,7 @@ ev_password_view_ask_password (EvPasswordView *password_view)
        GtkWidget *hbox, *main_vbox, *vbox, *icon;
        GtkWidget *grid;
        GtkWidget *label;
-       gchar     *text, *markup, *file_name;
+       gchar     *text, *markup;
 
        gtk_widget_set_sensitive (GTK_WIDGET (password_view), FALSE);
        
@@ -296,16 +285,14 @@ ev_password_view_ask_password (EvPasswordView *password_view)
        label = gtk_label_new (NULL);
        gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
        gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
-       file_name = g_file_get_basename (password_view->priv->uri_file);
         text = g_markup_printf_escaped (_("The document ā€œ%sā€ is locked and requires a password before it can 
be opened."),
-                                        file_name);
+                                        password_view->priv->filename);
         markup = g_strdup_printf ("<span size=\"larger\" weight=\"bold\">%s</span>\n\n%s",
                                  _("Password required"),
                                   text);
        gtk_label_set_markup (GTK_LABEL (label), markup);
        g_free (text);
        g_free (markup);
-       g_free (file_name);
        gtk_box_pack_start (GTK_BOX (main_vbox), label,
                            FALSE, FALSE, 0);
        gtk_widget_show (label);
diff --git a/shell/ev-password-view.h b/shell/ev-password-view.h
index 03e8907..81f6864 100644
--- a/shell/ev-password-view.h
+++ b/shell/ev-password-view.h
@@ -48,8 +48,8 @@ struct _EvPasswordViewClass {
 
 GType        ev_password_view_get_type                (void) G_GNUC_CONST;
 GtkWidget    *ev_password_view_new                     (GtkWindow      *parent);
-void          ev_password_view_set_uri                 (EvPasswordView *password_view,
-                                                       const char     *uri);
+void          ev_password_view_set_filename            (EvPasswordView *password_view,
+                                                       const char     *filename);
 void          ev_password_view_ask_password            (EvPasswordView *password_view);
 const gchar  *ev_password_view_get_password            (EvPasswordView *password_view);
 GPasswordSave ev_password_view_get_password_save_flags (EvPasswordView *password_view);
diff --git a/shell/ev-window-title.c b/shell/ev-window-title.c
index c09da93..eba2e9e 100644
--- a/shell/ev-window-title.c
+++ b/shell/ev-window-title.c
@@ -40,7 +40,7 @@ struct _EvWindowTitle
        EvWindow *window;
        EvWindowTitleType type;
        EvDocument *document;
-       char *uri;
+       char *filename;
         char *doc_title;
 };
 
@@ -57,19 +57,6 @@ static const BadTitleEntry bad_prefixes[] = {
        { EV_BACKEND_PDF, "Microsoft PowerPoint - " }
 };
 
-static char *
-get_filename_from_uri (const char *uri)
-{
-       char *filename;
-       char *basename;
-       
-       filename = g_uri_unescape_string (uri, NULL);
-       basename = g_path_get_basename (filename);
-       g_free(filename);
-
-       return basename;
-}
-
 /* Some docs report titles with confusing extensions (ex. .doc for pdf).
           Erase the confusing extension of the title */
 static void
@@ -83,13 +70,10 @@ ev_window_title_sanitize_title (EvWindowTitle *window_title, char **title) {
                if (g_ascii_strcasecmp (bad_extensions[i].backend, backend) == 0 && 
                    g_str_has_suffix (*title, bad_extensions[i].text)) {
                        char *new_title;
-                       char *filename = get_filename_from_uri (window_title->uri);
 
                        new_title = g_strndup (*title, strlen(*title) - strlen(bad_extensions[i].text));
                        g_free (*title);
                        *title = new_title;
-
-                       g_free (filename);
                }
        }
        for (i = 0; i < G_N_ELEMENTS (bad_prefixes); i++) {
@@ -119,11 +103,11 @@ ev_window_title_update (EvWindowTitle *window_title)
                 return;
         }
 
-       if (window_title->doc_title && window_title->uri) {
+       if (window_title->doc_title && window_title->filename) {
                 title = g_strdup (window_title->doc_title);
                 ev_window_title_sanitize_title (window_title, &title);
 
-               subtitle = get_filename_from_uri (window_title->uri);
+               subtitle = window_title->filename;
 
                title_header = title;
                title = g_strdup_printf ("%s ā€” %s", subtitle, title);
@@ -133,8 +117,8 @@ ev_window_title_update (EvWindowTitle *window_title)
                         if (*p == '\n')
                                 *p = ' ';
                 }
-       } else if (window_title->uri) {
-               title = get_filename_from_uri (window_title->uri);
+       } else if (window_title->filename) {
+               title = g_strdup (window_title->filename);
        } else if (!title) {
                title = g_strdup (_("Document Viewer"));
        }
@@ -164,7 +148,6 @@ ev_window_title_update (EvWindowTitle *window_title)
        }
 
        g_free (title);
-       g_free (subtitle);
        g_free (title_header);
 }
 
@@ -233,14 +216,14 @@ ev_window_title_set_document (EvWindowTitle *window_title,
 }
 
 void
-ev_window_title_set_uri (EvWindowTitle *window_title,
-                        const char    *uri)
+ev_window_title_set_filename (EvWindowTitle *window_title,
+                             const char    *filename)
 {
-        if (g_strcmp0 (uri, window_title->uri) == 0)
+        if (g_strcmp0 (filename, window_title->filename) == 0)
                 return;
 
-       g_free (window_title->uri);
-       window_title->uri = g_strdup (uri);
+       g_free (window_title->filename);
+       window_title->filename = g_strdup (filename);
 
        ev_window_title_update (window_title);
 }
@@ -251,6 +234,6 @@ ev_window_title_free (EvWindowTitle *window_title)
         if (window_title->document)
                 g_object_weak_unref (G_OBJECT (window_title->document), (GWeakNotify)document_destroyed_cb, 
window_title);
         g_free (window_title->doc_title);
-       g_free (window_title->uri);
+       g_free (window_title->filename);
        g_free (window_title);
 }
diff --git a/shell/ev-window-title.h b/shell/ev-window-title.h
index 8c53fe4..a25ace4 100644
--- a/shell/ev-window-title.h
+++ b/shell/ev-window-title.h
@@ -39,8 +39,8 @@ void         ev_window_title_set_type     (EvWindowTitle     *window_title,
                                             EvWindowTitleType  type);
 void           ev_window_title_set_document (EvWindowTitle     *window_title,
                                             EvDocument        *document);
-void          ev_window_title_set_uri      (EvWindowTitle     *window_title,
-                                            const char        *uri);
+void          ev_window_title_set_filename (EvWindowTitle     *window_title,
+                                            const char        *filename);
 void          ev_window_title_free         (EvWindowTitle     *window_title);
 
 G_END_DECLS
diff --git a/shell/ev-window.c b/shell/ev-window.c
index adc7fca..e2ba86a 100644
--- a/shell/ev-window.c
+++ b/shell/ev-window.c
@@ -200,6 +200,8 @@ struct _EvWindowPrivate {
        char *uri;
        glong uri_mtime;
        char *local_uri;
+       char *display_name;
+       char *edit_name;
        gboolean in_reload;
        EvFileMonitor *monitor;
        guint setup_document_idle;
@@ -1465,7 +1467,8 @@ ev_window_setup_document (EvWindow *ev_window)
 
        ev_window_set_page_mode (ev_window, PAGE_MODE_DOCUMENT);
        ev_window_title_set_document (ev_window->priv->title, document);
-       ev_window_title_set_uri (ev_window->priv->title, ev_window->priv->uri);
+       ev_window_title_set_filename (ev_window->priv->title,
+                                     ev_window->priv->display_name);
 
         ev_window_ensure_settings (ev_window);
 
@@ -1745,13 +1748,13 @@ ev_window_load_job_cb (EvJob *job,
                }
 
                /* We need to ask the user for a password */
-               ev_window_title_set_uri (ev_window->priv->title,
-                                        ev_window->priv->uri);
+               ev_window_title_set_filename (ev_window->priv->title,
+                                             ev_window->priv->display_name);
                ev_window_title_set_type (ev_window->priv->title,
                                          EV_WINDOW_TITLE_PASSWORD);
 
-               ev_password_view_set_uri (EV_PASSWORD_VIEW (ev_window->priv->password_view),
-                                         job_load->uri);
+               ev_password_view_set_filename (EV_PASSWORD_VIEW (ev_window->priv->password_view),
+                                              ev_window->priv->display_name);
 
                ev_window_set_page_mode (ev_window, PAGE_MODE_PASSWORD);
 
@@ -2017,6 +2020,8 @@ window_open_file_copy_ready_cb (GFile        *source,
                ev_window_clear_local_uri (ev_window);
                g_free (ev_window->priv->uri);
                ev_window->priv->uri = NULL;
+               g_clear_pointer (&ev_window->priv->display_name, g_free);
+               g_clear_pointer (&ev_window->priv->edit_name, g_free);
                g_object_unref (source);
 
                ev_window_hide_loading_message (ev_window);
@@ -2068,9 +2073,8 @@ ev_window_load_file_remote (EvWindow *ev_window,
                /* We'd like to keep extension of source uri since
                 * it helps to resolve some mime types, say cbz.
                  */
-               base_name = g_file_get_basename (source_file);
+               base_name = ev_window->priv->edit_name;
                 template = g_strdup_printf ("document.XXXXXX-%s", base_name);
-                g_free (base_name);
 
                 tmp_file = ev_mkstemp_file (template, &err);
                g_free (template);
@@ -2105,6 +2109,35 @@ ev_window_load_file_remote (EvWindow *ev_window,
                                         (GSourceFunc)show_loading_progress);
 }
 
+static void
+set_filenames (EvWindow *ev_window, GFile *f)
+{
+       EvWindowPrivate *priv = ev_window->priv;
+       GFileInfo       *info;
+       GError          *error = NULL;
+
+       g_clear_pointer (&priv->display_name, g_free);
+       g_clear_pointer (&priv->edit_name, g_free);
+
+       info = g_file_query_info (f,
+                                 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
+                                 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME,
+                                 G_FILE_QUERY_INFO_NONE, NULL, &error);
+       if (info) {
+               priv->display_name = g_strdup (g_file_info_get_display_name (info));
+               priv->edit_name = g_strdup (g_file_info_get_edit_name (info));
+               g_object_unref (info);
+       } else {
+               g_warning ("%s: %s", G_STRFUNC, error->message);
+               g_error_free (error);
+       }
+
+       if (!priv->display_name)
+               priv->display_name = g_file_get_basename (f);
+       if (!priv->edit_name)
+               priv->edit_name = g_file_get_basename (f);
+}
+
 void
 ev_window_open_uri (EvWindow       *ev_window,
                    const char     *uri,
@@ -2170,6 +2203,7 @@ ev_window_open_uri (EvWindow       *ev_window,
                g_object_unref (ev_window->priv->dest);
        ev_window->priv->dest = dest ? g_object_ref (dest) : NULL;
 
+       set_filenames (ev_window, source_file);
        setup_size_from_metadata (ev_window);
        setup_model_from_metadata (ev_window);
 
@@ -2593,6 +2627,8 @@ ev_window_open_copy_at_dest (EvWindow   *window,
 
        if (window->priv->metadata)
                new_window->priv->metadata = g_object_ref (window->priv->metadata);
+       new_window->priv->display_name = g_strdup (window->priv->display_name);
+       new_window->priv->edit_name = g_strdup (window->priv->edit_name);
        ev_window_open_document (new_window,
                                 window->priv->document,
                                 dest, 0, NULL);
@@ -2855,13 +2891,12 @@ ev_window_save_as (EvWindow *ev_window)
        gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
 
        file = g_file_new_for_uri (ev_window->priv->uri);
-       base_name = g_file_get_basename (file);
+       base_name = ev_window->priv->edit_name;
        parent = g_file_get_parent (file);
        dir_name = g_file_get_path (parent);
        g_object_unref (parent);
 
        gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc), base_name);
-       g_free (base_name);
 
        documents_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
        default_dir = g_file_test (documents_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ?
@@ -3400,8 +3435,6 @@ ev_window_print_range (EvWindow *ev_window,
        gint              document_last_page;
        gboolean          embed_page_setup;
        gchar            *output_basename;
-       gchar            *unescaped_basename;
-       const gchar      *document_uri;
        gchar            *dot;
 
        g_return_if_fail (EV_IS_WINDOW (ev_window));
@@ -3450,18 +3483,15 @@ ev_window_print_range (EvWindow *ev_window,
                                                    &range, 1);
        }
 
-       document_uri = ev_document_get_uri (ev_window->priv->document);
-       output_basename = g_path_get_basename (document_uri);
+       output_basename = g_strdup (ev_window->priv->edit_name);
        dot = g_strrstr (output_basename, ".");
        if (dot)
                dot[0] = '\0';
 
-       unescaped_basename = g_uri_unescape_string (output_basename, NULL);
        /* Set output basename for printing to file */
        gtk_print_settings_set (print_settings,
                                GTK_PRINT_SETTINGS_OUTPUT_BASENAME,
-                               unescaped_basename);
-       g_free (unescaped_basename);
+                               output_basename);
        g_free (output_basename);
 
        ev_print_operation_set_job_name (op, gtk_window_get_title (GTK_WINDOW (ev_window)));
@@ -5586,6 +5616,9 @@ ev_window_dispose (GObject *object)
                priv->uri = NULL;
        }
 
+       g_clear_pointer (&priv->display_name, g_free);
+       g_clear_pointer (&priv->edit_name, g_free);
+
        if (priv->search_string) {
                g_free (priv->search_string);
                priv->search_string = NULL;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]