summaryrefslogtreecommitdiff
path: root/src/io/contacts-io-import-main.vala
blob: 75a6559765bccaca9c895ed853dde5a23bd24632 (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
/*
 * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using Folks;

int main (string[] args) {
  if (args.length != 3)
    error ("Expected exactly 2 arguments, but got %d", args.length - 1);

  unowned var import_type = args[1];
  if (import_type == "")
    error ("Invalid import type: got empty import type");

  unowned var path = args[2];
  if (path == "")
    error ("Invalid path: path is empty");

  Contacts.Io.Importer importer;
  switch (import_type) {
    case "vcard":
      importer = new Contacts.Io.VCardImporter ();
      break;
    default:
      error ("Unknown import type '%s'", import_type);
  }

  HashTable<string, Value?> details;
  try {
    var file = File.new_for_path (path);
    details = importer.import_file (file);
  } catch (Error err) {
    error ("Error while importing file '%s': %s", path, err.message);
  }

  // Serialize
  var serialized = Contacts.Io.serialize_to_gvariant (details);

  // TODO: raw bytes (performance) or variant.print/parse?
  // var bytes = serialized.get_data_as_bytes ();
  // stdout.write (bytes.get_data (), bytes.get_size ());
  stdout.write (serialized.print (false).data);

  return 0;
}