Implement DATASET commands.
[pspp-builds.git] / src / ui / gui / psppire-window.c
index 332abe37608df3477a1a8249451d99fddb909ed1..399b020b32cb3f767c6246c31538d5484143d493 100644 (file)
@@ -79,7 +79,8 @@ enum
 {
   PROP_0,
   PROP_FILENAME,
-  PROP_DESCRIPTION
+  PROP_DESCRIPTION,
+  PROP_ID
 };
 
 
@@ -94,18 +95,82 @@ psppire_window_set_title (PsppireWindow *window)
 {
   GString *title = g_string_sized_new (80);
 
-  g_string_printf (title, "%s ", window->basename ? window->basename : "");
-  g_string_append_unichar (title, 0x2014); /* em dash */
-  g_string_printf (title, " PSPPIRE %s", window->description);
-
   if (window->dirty)
-    g_string_prepend_c (title, '*');
+    g_string_append_c (title, '*');
+
+  if (window->basename || window->id)
+    {
+      if (window->basename)
+        g_string_append_printf (title, "%s ", window->basename);
+
+      if (window->id != '\0')
+        g_string_append_printf (title, "[%s] ", window->id);
+
+      g_string_append_unichar (title, 0x2014); /* em dash */
+      g_string_append_c (title, ' '); /* em dash */
+    }
+
+  g_string_append_printf (title, "PSPPIRE %s", window->description);
 
   gtk_window_set_title (GTK_WINDOW (window), title->str);
 
   g_string_free (title, TRUE);
 }
 
+static void
+psppire_window_update_list_name (PsppireWindow *window)
+{
+  PsppireWindowRegister *reg = psppire_window_register_new ();
+  GString *candidate = g_string_sized_new (80);
+  int n;
+
+  n = 1;
+  do
+    {
+      /* Compose a name. */
+      g_string_truncate (candidate, 0);
+      if (window->filename)
+        {
+          gchar *display_filename = g_filename_display_name (window->filename);
+          g_string_append (candidate, display_filename);
+          g_free (display_filename);
+
+          if (window->id)
+            g_string_append_printf (candidate, " [%s]", window->id);
+        }
+      else if (window->id)
+        g_string_append_printf (candidate, "[%s]", window->id);
+      else
+        g_string_append (candidate, window->description);
+
+      if (n++ > 1)
+        g_string_append_printf (candidate, " #%d", n);
+
+      if (window->list_name && !strcmp (candidate->str, window->list_name))
+        {
+          /* Keep the existing name. */
+          g_string_free (candidate, TRUE);
+          return;
+        }
+    }
+  while (psppire_window_register_lookup (reg, candidate->str));
+
+  if (window->list_name)
+    psppire_window_register_remove (reg, window->list_name);
+
+  g_free (window->list_name);
+  window->list_name = g_string_free (candidate, FALSE);
+
+  psppire_window_register_insert (reg, window, window->list_name);
+}
+
+static void
+psppire_window_name_changed (PsppireWindow *window)
+{
+  psppire_window_set_title (window);
+  psppire_window_update_list_name (window);
+}
+
 static void
 psppire_window_set_property (GObject         *object,
                             guint            prop_id,
@@ -117,50 +182,24 @@ psppire_window_set_property (GObject         *object,
   switch (prop_id)
     {
     case PROP_DESCRIPTION:
+      g_free (window->description);
       window->description = g_value_dup_string (value);
       psppire_window_set_title (window);
       break;
     case PROP_FILENAME:
-      {
-       PsppireWindowRegister *reg = psppire_window_register_new ();
-
-       gchar *candidate_name ;
-
-       {
-         const gchar *name = g_value_get_string (value);
-         int x = 0;
-         GValue def = {0};
-         g_value_init (&def, pspec->value_type);
-
-         if ( NULL == name)
-           {
-             g_param_value_set_default (pspec, &def);
-             name = g_value_get_string (&def);
-           }
-
-         candidate_name = xstrdup (name);
-
-         while ( psppire_window_register_lookup (reg, candidate_name))
-           {
-             free (candidate_name);
-             candidate_name = uniquify (name, &x);
-           }
-
-         window->basename = g_filename_display_basename (candidate_name);
-
-         g_value_unset (&def);
-       }
-
-       psppire_window_set_title (window);
-
-       if ( window->name)
-         psppire_window_register_remove (reg, window->name);
-
-       free (window->name);
-       window->name = candidate_name;
-
-       psppire_window_register_insert (reg, window, window->name);
-      }
+      g_free (window->filename);
+      window->filename = g_value_dup_string (value);
+      g_free (window->basename);
+      window->basename = (window->filename
+                          ? g_filename_display_basename (window->filename)
+                          : NULL);
+      psppire_window_name_changed (window);
+      break;
+      break;
+    case PROP_ID:
+      g_free (window->id);
+      window->id = g_value_dup_string (value);
+      psppire_window_name_changed (window);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -180,11 +219,14 @@ psppire_window_get_property (GObject         *object,
   switch (prop_id)
     {
     case PROP_FILENAME:
-      g_value_set_string (value, window->name);
+      g_value_set_string (value, window->filename);
       break;
     case PROP_DESCRIPTION:
       g_value_set_string (value, window->description);
       break;
+    case PROP_ID:
+      g_value_set_string (value, window->id);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -210,9 +252,12 @@ psppire_window_finalize (GObject *object)
 
   PsppireWindowRegister *reg = psppire_window_register_new ();
 
-  psppire_window_register_remove (reg, window->name);
-  free (window->name);
-  free (window->description);
+  psppire_window_register_remove (reg, window->list_name);
+  g_free (window->filename);
+  g_free (window->basename);
+  g_free (window->id);
+  g_free (window->description);
+  g_free (window->list_name);
 
   g_signal_handler_disconnect (psppire_window_register_new (),
                               window->remove_handler);
@@ -226,6 +271,17 @@ psppire_window_finalize (GObject *object)
     G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
+static GParamSpec *
+null_if_empty_param (const gchar *name, const gchar *nick,
+                     const gchar *blurb, const gchar *default_value,
+                     GParamFlags flags)
+{
+  GParamSpec *param;
+
+  param = g_param_spec_string (name, nick, blurb, default_value, flags);
+  ((GParamSpecString *) param)->null_fold_if_empty = TRUE;
+  return param;
+}
 
 static void
 psppire_window_class_init (PsppireWindowClass *class)
@@ -233,18 +289,25 @@ psppire_window_class_init (PsppireWindowClass *class)
   GObjectClass *object_class = G_OBJECT_CLASS (class);
 
   GParamSpec *description_spec =
-    g_param_spec_string ("description",
+    null_if_empty_param ("description",
                       "Description",
                       "A string describing the usage of the window",
-                        "??????", /*Should be overridden by derived classes */
+                        NULL, /*Should be overridden by derived classes */
                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
 
   GParamSpec *filename_spec =
-    g_param_spec_string ("filename",
+    null_if_empty_param ("filename",
                       "File name",
                       "The name of the file associated with this window, if any",
-                        /* TRANSLATORS: This will form a filename.  Please avoid whitespace. */
-                        _("Untitled"),
+                        NULL,
+                        G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
+
+  GParamSpec *id_spec =
+    null_if_empty_param ("id",
+                         "Identifier",
+                         "The PSPP language identifier for the data associated "
+                         "with this window (e.g. dataset name)",
+                        NULL,
                         G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
 
   object_class->set_property = psppire_window_set_property;
@@ -258,6 +321,10 @@ psppire_window_class_init (PsppireWindowClass *class)
                                    PROP_FILENAME,
                                    filename_spec);
 
+  g_object_class_install_property (object_class,
+                                   PROP_ID,
+                                   id_spec);
+
   parent_class = g_type_class_peek_parent (class);
 }
 
@@ -398,6 +465,11 @@ on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
          break;
        case GTK_RESPONSE_APPLY:
          psppire_window_save (w);
+          if (w->dirty)
+            {
+              /* Save failed, or user exited Save As dialog with Cancel. */
+              return TRUE;
+            }
          break;
        case GTK_RESPONSE_REJECT:
          break;
@@ -414,9 +486,12 @@ on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
 static void
 psppire_window_init (PsppireWindow *window)
 {
-  window->name = NULL;
   window->menu = NULL;
-  window->description = xstrdup ("");
+  window->filename = NULL;
+  window->basename = NULL;
+  window->id = NULL;
+  window->description = NULL;
+  window->list_name = NULL;
 
   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
 
@@ -450,33 +525,30 @@ psppire_window_init (PsppireWindow *window)
 gint
 psppire_window_query_save (PsppireWindow *se)
 {
-  gchar *fn;
   gint response;
   GtkWidget *dialog;
   GtkWidget *cancel_button;
 
-  const gchar *description;
-  const gchar *filename = psppire_window_get_filename (se);
+  gchar *description;
 
   GTimeVal time;
 
   g_get_current_time (&time);
 
-  g_object_get (se, "description", &description, NULL);
-
-  g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
-
-
-  fn = g_filename_display_basename (filename);
-
+  if (se->filename)
+    description = g_filename_display_basename (se->filename);
+  else if (se->id)
+    description = g_strdup (se->id);
+  else
+    description = g_strdup (se->description);
   dialog =
     gtk_message_dialog_new (GTK_WINDOW (se),
                            GTK_DIALOG_MODAL,
                            GTK_MESSAGE_WARNING,
                            GTK_BUTTONS_NONE,
                            _("Save the changes to `%s' before closing?"),
-                           fn);
-  g_free (fn);
+                           description);
+  g_free (description);
 
   g_object_set (dialog, "icon-name", "psppicon", NULL);
 
@@ -506,7 +578,7 @@ psppire_window_query_save (PsppireWindow *se)
 }
 
 
-
+/* The return value is encoded in the glib filename encoding. */
 const gchar *
 psppire_window_get_filename (PsppireWindow *w)
 {
@@ -514,6 +586,7 @@ psppire_window_get_filename (PsppireWindow *w)
 }
 
 
+/* FILENAME must be encoded in the glib filename encoding. */
 void
 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
 {
@@ -595,16 +668,39 @@ psppire_window_save (PsppireWindow *w)
 {
   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
 
-  g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
-
   g_assert (i);
-
   g_return_if_fail (i->save);
 
-  i->save (w);
+  if (w->filename == NULL)
+    psppire_window_save_as (w);
+  else
+    {
+      i->save (w);
+      w->dirty = FALSE;
+      psppire_window_set_title (w);
+    }
+}
 
-  w->dirty = FALSE;
-  psppire_window_set_title (w);
+void
+psppire_window_save_as (PsppireWindow *w)
+{
+  PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
+  gchar *old_filename;
+
+  g_assert (i);
+  g_return_if_fail (i->pick_filename);
+
+  old_filename = w->filename;
+  w->filename = NULL;
+
+  i->pick_filename (w);
+  if (w->filename == NULL)
+    w->filename = old_filename;
+  else
+    {
+      g_free (old_filename);
+      psppire_window_save (w);
+    }
 }
 
 extern GtkRecentManager *the_recent_mgr;
@@ -686,26 +782,26 @@ psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
   gtk_file_filter_add_pattern (filter, "*");
   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
 
-  {
-    gchar *dir_name;
-    gchar *filename = NULL;
-    g_object_get (toplevel, "filename", &filename, NULL);
-
-    if ( ! g_path_is_absolute (filename))
-      {
-       gchar *path =
-         g_build_filename (g_get_current_dir (), filename, NULL);
-       dir_name = g_path_get_dirname (path);
-       g_free (path);
-      }
-    else
-      {
-       dir_name = g_path_get_dirname (filename);
-      }
-    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
-                                        dir_name);
-    free (dir_name);
-  }
+  if (toplevel->filename)
+    {
+      const gchar *filename = toplevel->filename;
+      gchar *dir_name;
+
+      if ( ! g_path_is_absolute (filename))
+        {
+          gchar *path =
+            g_build_filename (g_get_current_dir (), filename, NULL);
+          dir_name = g_path_get_dirname (path);
+          g_free (path);
+        }
+      else
+        {
+          dir_name = g_path_get_dirname (filename);
+        }
+      gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
+                                           dir_name);
+      free (dir_name);
+    }
 
   return dialog;
 }