File: command-set.vala

package info (click to toggle)
folks 0.15.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,600 kB
  • sloc: ansic: 6,382; python: 135; makefile: 25
file content (199 lines) | stat: -rw-r--r-- 5,547 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * 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
{
  private const string[] _valid_subcommands =
    {
      "alias",
    };

  public override string name
    {
      get { return "set"; }
    }

  public override string description
    {
      get
        {
          return "Set an individual's properties";
        }
    }

  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 int run (string? command_string)
    {
      string[] parts = {};

      if (command_string != null)
        {
          /* Parse subcommands */
          parts = command_string.split (" ");
        }

      if (!Utils.validate_subcommand (this.name, command_string, parts[0],
              Set._valid_subcommands))
          return 1;

      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 1;
            }

          /* 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 1;
            }

          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 1;
            }

          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 1;
            }
        }
      else
        {
          assert_not_reached ();
        }

      return 0;
    }

  /* 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 = Set._valid_subcommands;
              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: */