aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/tools/evolution-addressbook-export.c
blob: 8aa9e732cdeb4ab83820f6c4afb385e55492736a (plain) (blame)
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
/*
 * This program 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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Authors:
 *      Gilbert Fang <gilbert.fang@sun.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <glib/gi18n.h>

#include <libebook/libebook.h>

#include "e-util/e-util-private.h"

#include "evolution-addressbook-export.h"

#ifdef G_OS_WIN32
#ifdef DATADIR
#undef DATADIR
#endif
#include <libedataserver/libedataserver.h>
#endif

/* Command-Line Options */
static gchar *opt_output_file = NULL;
static gboolean opt_list_folders_mode = FALSE;
static gchar *opt_output_format = NULL;
static gchar *opt_addressbook_source_uid = NULL;
static gchar **opt_remaining = NULL;

static GOptionEntry entries[] = {
    { "output", '\0', 0,
      G_OPTION_ARG_STRING, &opt_output_file,
      N_("Specify the output file instead of standard output"),
      N_("OUTPUTFILE") },
    { "list-addressbook-folders", 'l', 0,
      G_OPTION_ARG_NONE, &opt_list_folders_mode,
      N_("List local address book folders") },
    { "format", '\0', 0,
      G_OPTION_ARG_STRING, &opt_output_format,
      N_("Show cards as vcard or csv file"),
      N_("[vcard|csv]") },
    { G_OPTION_REMAINING, '\0', 0,
      G_OPTION_ARG_STRING_ARRAY, &opt_remaining },
    { NULL }
};

gint
main (gint argc,
      gchar **argv)
{
    ActionContext actctx;
    GOptionContext *context;
    GError *error = NULL;

    gint current_action = ACTION_NOTHING;
    gint IsCSV = FALSE;
    gint IsVCard = FALSE;

#ifdef G_OS_WIN32
    e_util_win32_initialize ();
#endif

    /*i18n-lize */
    bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    textdomain (GETTEXT_PACKAGE);

    context = g_option_context_new (NULL);
    g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
    if (!g_option_context_parse (context, &argc, &argv, &error)) {
        g_printerr ("%s\n", error->message);
        g_error_free (error);
        exit (-1);
    }

    actctx.registry = e_source_registry_new_sync (NULL, &error);
    if (error != NULL) {
        g_printerr ("%s\n", error->message);
        g_error_free (error);
        exit (-1);
    }

    /* Parsing Parameter */
    if (opt_remaining && g_strv_length (opt_remaining) > 0)
        opt_addressbook_source_uid = g_strdup (opt_remaining[0]);

    if (opt_list_folders_mode != FALSE) {
        current_action = ACTION_LIST_FOLDERS;
        if (opt_addressbook_source_uid != NULL || opt_output_format != NULL) {
            g_warning (_("Command line arguments error, please use --help option to see the usage."));
            exit (-1);
        }
    } else {

        current_action = ACTION_LIST_CARDS;

        /* check the output format */
        if (opt_output_format == NULL) {
            IsVCard = TRUE;
        } else {
            IsCSV = !strcmp (opt_output_format, "csv");
            IsVCard = !strcmp (opt_output_format, "vcard");
            if (IsCSV == FALSE && IsVCard == FALSE) {
                g_warning (_("Only support csv or vcard format."));
                exit (-1);
            }
        }
    }

    /* do actions */
    if (current_action == ACTION_LIST_FOLDERS) {
        actctx.action_type = current_action;
        if (opt_output_file == NULL) {
            actctx.output_file = NULL;
        } else {
            actctx.output_file = g_strdup (opt_output_file);
        }
        action_list_folders_init (&actctx);

    } else if (current_action == ACTION_LIST_CARDS) {
        actctx.action_type = current_action;
        if (opt_output_file == NULL) {
            actctx.output_file = NULL;
        } else {
            actctx.output_file = g_strdup (opt_output_file);
        }
        actctx.IsCSV = IsCSV;
        actctx.IsVCard = IsVCard;
        actctx.addressbook_source_uid =
            g_strdup (opt_addressbook_source_uid);

        action_list_cards_init (&actctx);

    } else {
        g_warning (_("Unhandled error"));
        exit (-1);
    }

    g_object_unref (actctx.registry);

    /*FIXME:should free actctx's some gchar * field, such as output_file! but since the program will end, so that will not cause mem leak.  */

    return 0;
}