[gnome-text-editor] document: add API to suggest filenames



commit 52580ea771787ed64cdb63a2bbe4e359604a38d8
Author: Christian Hergert <chergert redhat com>
Date:   Tue May 17 12:13:16 2022 -0700

    document: add API to suggest filenames
    
    Requires recent changes to GtkSourceView to be effective as that is where
    the properties are defined for suggested name or suffixes.
    
    Related #375

 src/editor-document-private.h |  3 +++
 src/editor-document.c         | 52 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
---
diff --git a/src/editor-document-private.h b/src/editor-document-private.h
index 05db4be..c934fc4 100644
--- a/src/editor-document-private.h
+++ b/src/editor-document-private.h
@@ -89,5 +89,8 @@ void                      _editor_document_use_admin               (EditorDocume
 gboolean                  _editor_document_had_error               (EditorDocument           *self);
 void                      _editor_document_persist_syntax_language (EditorDocument           *self,
                                                                     const char               *language_id);
+char                     *_editor_document_suggest_filename        (EditorDocument           *self);
+GFile                    *_editor_document_suggest_file            (EditorDocument           *self,
+                                                                    GFile                    *directory);
 
 G_END_DECLS
diff --git a/src/editor-document.c b/src/editor-document.c
index 8017e0a..6e9d4b9 100644
--- a/src/editor-document.c
+++ b/src/editor-document.c
@@ -2196,3 +2196,55 @@ _editor_document_had_error (EditorDocument *self)
 
   return self->load_failed;
 }
+
+char *
+_editor_document_suggest_filename (EditorDocument *self)
+{
+  g_autofree char *title = NULL;
+  GtkSourceLanguage *lang;
+  const char *suggested_name = NULL;
+  const char *suggested_suffix = NULL;
+
+  g_assert (EDITOR_IS_DOCUMENT (self));
+
+  if ((lang = gtk_source_buffer_get_language (GTK_SOURCE_BUFFER (self))))
+    {
+      suggested_suffix = gtk_source_language_get_metadata (lang, "suggested-suffix");
+      suggested_name = gtk_source_language_get_metadata (lang, "suggested-name");
+    }
+
+  if (suggested_name != NULL)
+    return g_strdup (suggested_name);
+
+  title = editor_document_dup_title (self);
+
+  if (suggested_suffix == NULL)
+    return g_strdup_printf ("%s.txt", title);
+
+  return g_strdup_printf ("%s.%s", title, suggested_suffix);
+}
+
+GFile *
+_editor_document_suggest_file (EditorDocument *self,
+                               GFile          *directory)
+{
+  static GFile *documents;
+  g_autofree char *name = NULL;
+
+  g_return_val_if_fail (EDITOR_IS_DOCUMENT (self), NULL);
+  g_return_val_if_fail (!directory || G_IS_FILE (directory), NULL);
+
+  if (directory == NULL)
+    {
+      if (documents == NULL)
+        documents = g_file_new_for_path (g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS));
+      directory = documents;
+    }
+
+  name = _editor_document_suggest_filename (self);
+
+  g_assert (G_IS_FILE (directory));
+  g_assert (name != NULL);
+
+  return g_file_get_child (directory, name);
+}


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