summaryrefslogtreecommitdiff
path: root/tests/eds/helper-create-many-contacts.vala
blob: 000348ffb0a8588a258be3bed6346f4cca323d12 (plain)
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
/*
 * Copyright © 2013 Intel Corporation
 *
 * 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:
 *    Simon McVittie <simon.mcvittie@collabora.co.uk>
 */

/**
 * helper-create-many-contacts --uid=UID [--n-contacts=N]
 *
 * Add N contacts (default 2000) to the Evolution Data Server address book
 * with the given UID.
 *
 * This utility must be called in a Folks test environment, with
 * DBUS_SESSION_BUS_ADDRESS pointing to a temporary D-Bus session and
 * XDG_CONFIG_HOME, XDG_DATA_HOME, XDG_CACHE_HOME pointing into a temporary
 * directory.
 *
 * By default, we use N numbered contacts with some easy test data (currently
 * full name, one email address and one ICQ number).
 *
 * If the environment variable $FOLKS_TESTS_REAL_VCARDS is set, it is a file
 * containing multiple vCards in text format. Put a source of test data there
 * (e.g. a copy of your personal address book). The first contacts in that
 * file will be used as the first contacts in the address book: if there
 * are more than N only the first N will be used, and if there are fewer than
 * N, the difference will be made up with numbered contacts.
 */
public class Main
{
  private static void _add_many (uint n_contacts, string uid) throws GLib.Error
    {
      var registry = new E.SourceRegistry.sync ();
      var source = registry.ref_source (uid);
      assert (source.uid == uid);
      var book_client = E.BookClient.connect_sync (source);
      SList<E.Contact> contacts = null;

      var envvar = Environment.get_variable ("FOLKS_TESTS_REAL_VCARDS");

      uint n = 0;

      if (envvar != null)
        {
          /* Split at the boundaries between END:VCARD and BEGIN_VCARD,
           * without removing those tokens from the segments. */
          string[] cards;

          try
            {
              string text;
              FileUtils.get_contents ((!) envvar, out text);

              var regex = new Regex ("(?<=\r\nEND:VCARD)\r\n+(?=BEGIN:VCARD\r\n)",
                  RegexCompileFlags.OPTIMIZE | RegexCompileFlags.CASELESS |
                  RegexCompileFlags.NEWLINE_LF);
              cards = regex.split_full (text);
            }
          catch (Error e)
            {
              error ("%s", e.message);
            }

          foreach (var card in cards)
            {
              if (n >= n_contacts)
                break;

              var contact = new E.Contact.from_vcard (card);

              /* If we got the contact from an e-d-s export, give it a new
               * unique uid. */
              contact.id = null;

              contacts.prepend (contact);
              n++;
            }
        }

      while (n < n_contacts)
        {
          var contact = new E.Contact ();

          contact.full_name = "Contact %u".printf (n);
          contact.email_1 = "contact%u@example.com".printf (n);
          contact.im_icq_home_1 = "%u".printf (n);

          contacts.prepend (contact);
          n++;
        }

      debug ("Importing %u contacts", n);

      SList<string> uids;
      try
        {
          book_client.add_contacts_sync (contacts, out uids, null);
        }
      catch (Error e)
        {
          error ("%s", e.message);
        }

      debug ("Imported %u contacts", uids.length ());
    }

  private static int _n_contacts = 2000;
  private static string _uid = "";
  private const GLib.OptionEntry[] _options = {
      { "n-contacts", 'n', 0, OptionArg.INT, ref Main._n_contacts,
          "Number of contacts", "CONTACTS" },
      { "uid", 'u', 0, OptionArg.STRING, ref Main._uid,
          "Address book uid", "UID" },
      { null }
  };

  public static int main (string[] args)
    {
      Intl.setlocale (LocaleCategory.ALL, "");

      if (Environment.get_variable ("FOLKS_TESTS_SANDBOXED_DBUS") != "eds")
        error ("e-d-s helpers must be run in a private D-Bus session with " +
            "e-d-s services");

      try
        {
          var context = new OptionContext ("- Create many e-d-s contacts");
          context.set_help_enabled (true);
          context.add_main_entries (Main._options, null);
          context.parse (ref args);
        }
      catch (OptionError e)
        {
          stderr.printf ("Error parsing arguments: %s\n", e.message);
          return 2;
        }

      if (Main._uid == "")
        {
          stderr.printf ("The --uid=UID option is required\n");
          return 2;
        }

      try
        {
          Main._add_many (Main._n_contacts, Main._uid);
        }
      catch (Error e)
        {
          stderr.printf ("Error: %s\n", e.message);
          return 1;
        }

      return 0;
    }
}