[folks: 1/2] inspect: Add set command with set alias capability.



commit 161b67e100bb9e161c1c05818463a4c90921bc2e
Author: Jeremy Whiting <jpwhiting kde org>
Date:   Wed Aug 8 13:48:40 2012 -0600

    inspect: Add set command with set alias capability.

 NEWS                           |    1 +
 tools/inspect/Makefile.am      |    1 +
 tools/inspect/command-set.vala |  198 ++++++++++++++++++++++++++++++++++++++++
 tools/inspect/inspect.vala     |    1 +
 4 files changed, 201 insertions(+), 0 deletions(-)
---
diff --git a/NEWS b/NEWS
index c1df5c5..ca23da1 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,7 @@ Bugs fixed:
   _tpf_persona_contact_weak_notify_cb()
 â Bug 681726 â empathy crashed with SIGABRT in g_assertion_message()
 â Bug 683452 â gnome-contacts starts up with an empty address book
+â Bug 681476 â folks-inspect could use some methods to set values
 
 API changes:
 â Add PropertyError.UNAVAILABLE
diff --git a/tools/inspect/Makefile.am b/tools/inspect/Makefile.am
index a809d01..947706e 100644
--- a/tools/inspect/Makefile.am
+++ b/tools/inspect/Makefile.am
@@ -22,6 +22,7 @@ folks_inspect_SOURCES = \
 	command-persona-stores.vala \
 	command-personas.vala \
 	command-quit.vala \
+	command-set.vala \
 	command-signals.vala \
 	signal-manager.vala \
 	utils.vala \
diff --git a/tools/inspect/command-set.vala b/tools/inspect/command-set.vala
new file mode 100644
index 0000000..670161c
--- /dev/null
+++ b/tools/inspect/command-set.vala
@@ -0,0 +1,198 @@
+/*
+ * Copyright (C) 2012 Jeremy Whiting <jeremy whiting collabora com>
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *       Jeremy Whiting <jeremy whiting collabora com>
+ */
+
+using Folks;
+using Gee;
+using GLib;
+
+private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
+{
+  public override string name
+    {
+      get { return "set"; }
+    }
+
+  public override string description
+    {
+      get
+        {
+          return "set an individual's attributes";
+        }
+    }
+
+  public override string help
+    {
+      get
+        {
+          return "set alias [individual UID] [new alias]" +
+                  "   Set the alias of the given individual.";
+        }
+    }
+
+  public Set (Client client)
+    {
+      base (client);
+    }
+
+  public override async void run (string? command_string)
+    {
+      string[] parts = {};
+
+      if (command_string != null)
+        {
+          /* Parse subcommands */
+          parts = command_string.split (" ");
+        }
+
+      if (parts.length < 1 ||
+          (parts[0] != "alias"))
+        {
+          Utils.print_line ("Unrecognised 'set' command '%s'.",
+            command_string);
+          return;
+        }
+
+      if (parts[0] == "alias")
+        {
+          if (parts.length < 3)
+            {
+              Utils.print_line ("Must pass at least one individual ID and a new alias to an " +
+                  "'alias' subcommand.");
+
+              return;
+            }
+
+          /* To set an attribute on an individual, we must have at least one. */
+          if (parts[1] == null || parts[1].strip () == "")
+            {
+              Utils.print_line ("Unrecognised individual ID '%s'.",
+                  parts[1]);
+
+              return;
+            }
+
+          var id = parts[1].strip ();
+
+          var individual = this.client.aggregator.individuals.get (id);
+          if (individual == null)
+            {
+              Utils.print_line ("Unrecognized individual ID '%s'.", id);
+              return;
+            }
+            
+          try
+            {
+              var persona = yield this.client.aggregator.ensure_individual_property_writeable (individual, "alias");
+              
+              /* Since the individual may have changed, use the individual from the new persona. */
+              persona.individual.alias = parts[2];
+              Utils.print_line ("Setting of individual's alias to '%s' was successful.",
+                  parts[2]);
+            }
+          catch (Folks.IndividualAggregatorError e)
+            {
+              Utils.print_line ("Setting of individual's alias to '%s' failed.",
+                  parts[2]);
+            }
+
+          return;
+        }
+      else
+        {
+          assert_not_reached ();
+        }
+    }
+
+  /* FIXME: These can't be in the subcommand_name_completion_cb() function
+   * because Vala doesn't allow static local variables. Erk. */
+  [CCode (array_length = false, array_null_terminated = true)]
+  private static string[] subcommand_completions;
+  private static uint completion_count;
+  private static string prefix;
+
+  /* Complete a subcommand name (âaliasâ), starting with @word. */
+  public static string? subcommand_name_completion_cb (string word,
+      int state)
+    {
+      if (state == 0)
+        {
+          string[] parts = word.split (" ");
+
+          if (parts.length > 0 &&
+              (parts[0] == "alias"))
+            {
+              var last_part = parts[parts.length - 1];
+
+              if (parts[0] == "alias")
+                {
+                  subcommand_completions =
+                      Readline.completion_matches (last_part,
+                          Utils.individual_id_completion_cb);
+                }
+
+              if (last_part == "")
+                {
+                  prefix = word;
+                }
+              else
+                {
+                  prefix = word[0:-last_part.length];
+                }
+            }
+          else
+            {
+              subcommand_completions =
+                  { "alias", null };
+              prefix = "";
+            }
+
+          completion_count = 0;
+        }
+
+      while (completion_count < subcommand_completions.length)
+        {
+          var completion = subcommand_completions[completion_count];
+          var candidate = prefix + completion;
+          completion_count++;
+
+          if (completion != null && completion != "" &&
+              candidate.has_prefix (word))
+            {
+              return completion;
+            }
+        }
+
+      /* Clean up */
+      subcommand_completions = null;
+      completion_count = 0;
+      prefix = "";
+
+      return null;
+    }
+  
+  public override string[]? complete_subcommand (string subcommand)
+    {
+      /* @subcommand should be âaliasâ */
+      return Readline.completion_matches (subcommand,
+          subcommand_name_completion_cb);
+    }
+}
+
+/* vim: filetype=vala textwidth=80 tabstop=2 expandtab: */
diff --git a/tools/inspect/inspect.vala b/tools/inspect/inspect.vala
index f480151..d3a81e0 100644
--- a/tools/inspect/inspect.vala
+++ b/tools/inspect/inspect.vala
@@ -139,6 +139,7 @@ public class Folks.Inspect.Client : Object
       this.commands.set ("personas", new Commands.Personas (this));
       this.commands.set ("backends", new Commands.Backends (this));
       this.commands.set ("persona-stores", new Commands.PersonaStores (this));
+      this.commands.set ("set", new Commands.Set (this));
       this.commands.set ("signals", new Commands.Signals (this));
       this.commands.set ("debug", new Commands.Debug (this));
 



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