[gtk/wip/otte/undo: 12/17] undocommand: Add a timestamp property



commit fc17f25117890e8891dc86b067a8e12c48ec19dd
Author: Benjamin Otte <otte redhat com>
Date:   Mon Aug 17 06:51:46 2015 +0200

    undocommand: Add a timestamp property

 gtk/gtkentryundocommand.c   | 12 ++++--
 gtk/gtkundocommand.c        | 91 +++++++++++++++++++++++++++++++++++++++++++--
 gtk/gtkundocommandprivate.h |  2 +
 3 files changed, 99 insertions(+), 6 deletions(-)
---
diff --git a/gtk/gtkentryundocommand.c b/gtk/gtkentryundocommand.c
index 0709a86764..eefc78ce22 100644
--- a/gtk/gtkentryundocommand.c
+++ b/gtk/gtkentryundocommand.c
@@ -105,13 +105,16 @@ gtk_entry_undo_command_redo (GtkUndoCommand *command)
 
 static GtkUndoCommand *
 gtk_entry_undo_command_new_from_snapshots (GtkEntry               *entry,
+                                           gint64                  timestamp,
                                            const GtkEntrySnapshot *before,
                                            const GtkEntrySnapshot *after)
 {
   GtkEntryUndoCommand *command;
   GtkEntryUndoCommandPrivate *priv;
 
-  command = g_object_new (GTK_TYPE_ENTRY_UNDO_COMMAND, NULL);
+  command = g_object_new (GTK_TYPE_ENTRY_UNDO_COMMAND,
+                          "timestamp", timestamp,
+                          NULL);
   priv = gtk_entry_undo_command_get_instance_private (command);
 
   priv->entry = entry;
@@ -134,7 +137,10 @@ gtk_entry_undo_command_merge (GtkUndoCommand *command,
   if (command_priv->entry != followup_priv->entry)
     return NULL;
 
-  return gtk_entry_undo_command_new_from_snapshots (command_priv->entry, &command_priv->before, 
&followup_priv->after);
+  return gtk_entry_undo_command_new_from_snapshots (command_priv->entry,
+                                                    gtk_undo_command_get_timestamp (followup),
+                                                    &command_priv->before,
+                                                    &followup_priv->after);
 }
 
 gboolean
@@ -262,7 +268,7 @@ gtk_entry_undo_command_new (GtkEntry               *entry,
 
   gtk_entry_snapshot_init_from_entry (&after, entry);
 
-  result = gtk_entry_undo_command_new_from_snapshots (entry, before, &after);
+  result = gtk_entry_undo_command_new_from_snapshots (entry, 0, before, &after);
   
   gtk_entry_snapshot_clear (&after);
 
diff --git a/gtk/gtkundocommand.c b/gtk/gtkundocommand.c
index 08da66f3d0..c3688178a0 100644
--- a/gtk/gtkundocommand.c
+++ b/gtk/gtkundocommand.c
@@ -28,9 +28,58 @@ struct _GtkUndoCommandPrivate {
   gint64 timestamp;
 };
 
+enum {
+  PROP_0,
+  PROP_TIMESTAMP,
+  /* add more */
+  NUM_PROPERTIES
+};
+
+static GParamSpec *properties[NUM_PROPERTIES];
+
 G_DEFINE_TYPE_WITH_CODE (GtkUndoCommand, gtk_undo_command, G_TYPE_OBJECT,
                          G_ADD_PRIVATE (GtkUndoCommand))
 
+static void
+gtk_undo_command_get_property (GObject    *object,
+                               guint       property_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
+{
+  GtkUndoCommandPrivate *priv = gtk_undo_command_get_instance_private (GTK_UNDO_COMMAND (object));
+
+  switch (property_id)
+    {
+    case PROP_TIMESTAMP:
+      g_value_set_int64 (value, priv->timestamp);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
+}
+
+static void
+gtk_undo_command_set_property (GObject      *object,
+                               guint         property_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+  GtkUndoCommandPrivate *priv = gtk_undo_command_get_instance_private (GTK_UNDO_COMMAND (object));
+
+  switch (property_id)
+    {
+    case PROP_TIMESTAMP:
+      priv->timestamp = g_value_get_int64 (value);
+      if (priv->timestamp == 0)
+        priv->timestamp = g_get_real_time ();
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
+}
+
 static gboolean
 gtk_undo_command_real_undo (GtkUndoCommand *command)
 {
@@ -75,19 +124,35 @@ gtk_undo_command_real_describe (GtkUndoCommand *command)
 static void
 gtk_undo_command_class_init (GtkUndoCommandClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->set_property = gtk_undo_command_set_property;
+  object_class->get_property = gtk_undo_command_get_property;
+
   klass->undo = gtk_undo_command_real_undo;
   klass->redo = gtk_undo_command_real_redo;
   klass->merge = gtk_undo_command_real_merge;
   klass->should_merge = gtk_undo_command_real_should_merge;
   klass->describe = gtk_undo_command_real_describe;
+
+  /*
+   * GtkUndoCommand:timestamp:
+   *
+   * Timestamp this command was recorded at. See
+   * gtk_undo_command_get_timestamp() for details.
+   */
+  properties[PROP_TIMESTAMP] = g_param_spec_int64 ("timestamp", "Timestamp",
+                                                   "Time at which this command was recorded",
+                                                   0, G_MAXINT64, 0,
+                                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
+                                                   G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
 }
 
 static void
 gtk_undo_command_init (GtkUndoCommand *command)
 {
-  GtkUndoCommandPrivate *priv = gtk_undo_command_get_instance_private (command);
-
-  priv->timestamp = g_get_real_time ();
 }
 
 gboolean
@@ -145,3 +210,23 @@ gtk_undo_command_describe (GtkUndoCommand *command)
 
   return GTK_UNDO_COMMAND_GET_CLASS (command)->describe (command);
 }
+
+/*
+ * gtk_undo_command_get_timestamp:
+ * @command: The command
+ *
+ * Returns the timestamp when this command was recorded. If multiple
+ * commands get combined into one, the timestamp will be the timestamp
+ * of the newest command.
+ *
+ * Returns: Timestamp as returned by g_get_real_time()
+ **/
+gint64
+gtk_undo_command_get_timestamp (GtkUndoCommand *command)
+{
+  GtkUndoCommandPrivate *priv = gtk_undo_command_get_instance_private (command);
+
+  g_return_val_if_fail (GTK_IS_UNDO_COMMAND (command), 0);
+
+  return priv->timestamp;
+}
diff --git a/gtk/gtkundocommandprivate.h b/gtk/gtkundocommandprivate.h
index 9968c6c074..bfe982e47a 100644
--- a/gtk/gtkundocommandprivate.h
+++ b/gtk/gtkundocommandprivate.h
@@ -59,6 +59,8 @@ struct _GtkUndoCommandClass
 
 GType                   gtk_undo_command_get_type       (void) G_GNUC_CONST;
 
+gint64                  gtk_undo_command_get_timestamp  (GtkUndoCommand                *command);
+
 gboolean                gtk_undo_command_undo           (GtkUndoCommand                *command);
 gboolean                gtk_undo_command_redo           (GtkUndoCommand                *command);
 GtkUndoCommand *        gtk_undo_command_merge          (GtkUndoCommand                *command,


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