inspect: Add set command with set alias capability.
authorJeremy Whiting <jpwhiting@kde.org>
Wed, 8 Aug 2012 19:48:40 +0000 (13:48 -0600)
committerJeremy Whiting <jpwhiting@kde.org>
Thu, 6 Sep 2012 20:12:55 +0000 (14:12 -0600)
NEWS
tools/inspect/Makefile.am
tools/inspect/command-set.vala [new file with mode: 0644]
tools/inspect/inspect.vala

diff --git a/NEWS b/NEWS
index c1df5c5..ca23da1 100644 (file)
--- 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
index a809d01..947706e 100644 (file)
@@ -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 (file)
index 0000000..670161c
--- /dev/null
@@ -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: */
index f480151..d3a81e0 100644 (file)
@@ -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));