evolution-3.6.4/addressbook/tools/evolution-addressbook-export.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *		Gilbert Fang <gilbert.fang@sun.com>
 18  *
 19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 20  *
 21  */
 22 
 23 #ifdef HAVE_CONFIG_H
 24 #include <config.h>
 25 #endif
 26 
 27 #include <stdlib.h>
 28 #include <string.h>
 29 #include <glib/gi18n.h>
 30 
 31 #include <libebook/libebook.h>
 32 
 33 #include "evolution-addressbook-export.h"
 34 
 35 #ifdef G_OS_WIN32
 36 #ifdef DATADIR
 37 #undef DATADIR
 38 #endif
 39 #include <windows.h>
 40 #include <conio.h>
 41 #ifndef PROCESS_DEP_ENABLE
 42 #define PROCESS_DEP_ENABLE 0x00000001
 43 #endif
 44 #ifndef PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION
 45 #define PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION 0x00000002
 46 #endif
 47 #endif
 48 
 49 /* Command-Line Options */
 50 static gchar *opt_output_file = NULL;
 51 static gboolean opt_list_folders_mode = FALSE;
 52 static gchar *opt_output_format = NULL;
 53 static gchar *opt_addressbook_source_uid = NULL;
 54 static gboolean opt_async_mode = FALSE;
 55 static gint opt_file_size = 0;
 56 static gchar **opt_remaining = NULL;
 57 
 58 static GOptionEntry entries[] = {
 59 	{ "output", '\0', 0,
 60 	  G_OPTION_ARG_STRING, &opt_output_file,
 61 	  N_("Specify the output file instead of standard output"),
 62 	  N_("OUTPUTFILE") },
 63 	{ "list-addressbook-folders", 'l', 0,
 64 	  G_OPTION_ARG_NONE, &opt_list_folders_mode,
 65 	  N_("List local address book folders") },
 66 	{ "format", '\0', 0,
 67 	  G_OPTION_ARG_STRING, &opt_output_format,
 68 	  N_("Show cards as vcard or csv file"),
 69 	  N_("[vcard|csv]") },
 70 	{ "async", 'a', 0,
 71 	  G_OPTION_ARG_NONE, &opt_async_mode,
 72 	  N_("Export in asynchronous mode") },
 73 	{ "size", '\0', 0,
 74 	  G_OPTION_ARG_INT, &opt_file_size,
 75 	  N_("The number of cards in one output file in asynchronous mode, "
 76 	     "default size 100."),
 77 	  N_("NUMBER") },
 78 	{ G_OPTION_REMAINING, '\0', 0,
 79 	  G_OPTION_ARG_STRING_ARRAY, &opt_remaining },
 80 	{ NULL }
 81 };
 82 
 83 gint
 84 main (gint argc,
 85       gchar **argv)
 86 {
 87 	ESourceRegistry *registry;
 88 	ActionContext actctx;
 89 	GOptionContext *context;
 90 	GError *error = NULL;
 91 
 92 	gint current_action = ACTION_NOTHING;
 93 	gint IsCSV = FALSE;
 94 	gint IsVCard = FALSE;
 95 
 96 #ifdef G_OS_WIN32
 97 	/* Reduce risks */
 98 	{
 99 		typedef BOOL (WINAPI *t_SetDllDirectoryA) (LPCSTR lpPathName);
100 		t_SetDllDirectoryA p_SetDllDirectoryA;
101 
102 		p_SetDllDirectoryA = GetProcAddress (GetModuleHandle ("kernel32.dll"), "SetDllDirectoryA");
103 		if (p_SetDllDirectoryA)
104 			(*p_SetDllDirectoryA) ("");
105 	}
106 #ifndef _WIN64
107 	{
108 		typedef BOOL (WINAPI *t_SetProcessDEPPolicy) (DWORD dwFlags);
109 		t_SetProcessDEPPolicy p_SetProcessDEPPolicy;
110 
111 		p_SetProcessDEPPolicy = GetProcAddress (GetModuleHandle ("kernel32.dll"), "SetProcessDEPPolicy");
112 		if (p_SetProcessDEPPolicy)
113 			(*p_SetProcessDEPPolicy) (PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION);
114 	}
115 #endif
116 #endif
117 
118 	g_type_init ();
119 
120 	/*i18n-lize */
121 	bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
122 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
123 	textdomain (GETTEXT_PACKAGE);
124 
125 	context = g_option_context_new (NULL);
126 	g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
127 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
128 		g_printerr ("%s\n", error->message);
129 		g_error_free (error);
130 		exit (-1);
131 	}
132 
133 	registry = e_source_registry_new_sync (NULL, &error);
134 	if (error != NULL) {
135 		g_printerr ("%s\n", error->message);
136 		g_error_free (error);
137 		exit (-1);
138 	}
139 
140 	/* Parsing Parameter */
141 	if (opt_remaining && g_strv_length (opt_remaining) > 0)
142 		opt_addressbook_source_uid = g_strdup (opt_remaining[0]);
143 
144 	if (opt_list_folders_mode != FALSE) {
145 		current_action = ACTION_LIST_FOLDERS;
146 		/* check there should not be addressbook-source-uid,
147 		 * and async and size, output_format */
148 		if (opt_addressbook_source_uid != NULL || opt_async_mode != FALSE || opt_output_format != NULL || opt_file_size != 0) {
149 			g_warning (_("Command line arguments error, please use --help option to see the usage."));
150 			exit (-1);
151 		}
152 	} else {
153 
154 		current_action = ACTION_LIST_CARDS;
155 
156 		/* check the output format */
157 		if (opt_output_format == NULL) {
158 			IsVCard = TRUE;
159 		} else {
160 			IsCSV = !strcmp (opt_output_format, "csv");
161 			IsVCard = !strcmp (opt_output_format, "vcard");
162 			if (IsCSV == FALSE && IsVCard == FALSE) {
163 				g_warning (_("Only support csv or vcard format."));
164 				exit (-1);
165 			}
166 		}
167 
168 		/*check async and output file */
169 		if (opt_async_mode == TRUE) {
170 			/* check have to output file , set default file_size */
171 			if (opt_output_file == NULL) {
172 				g_warning (_("In async mode, output must be file."));
173 				exit (-1);
174 			}
175 			if (opt_file_size == 0)
176 				opt_file_size = DEFAULT_SIZE_NUMBER;
177 		} else {
178 			/*check no file_size */
179 			if (opt_file_size != 0) {
180 				g_warning (_("In normal mode, there is no need for the size option."));
181 				exit (-1);
182 			}
183 		}
184 	}
185 
186 	/* do actions */
187 	if (current_action == ACTION_LIST_FOLDERS) {
188 		actctx.action_type = current_action;
189 		if (opt_output_file == NULL) {
190 			actctx.action_list_folders.output_file = NULL;
191 		} else {
192 			actctx.action_list_folders.output_file = g_strdup (opt_output_file);
193 		}
194 		action_list_folders_init (registry, &actctx);
195 
196 	} else if (current_action == ACTION_LIST_CARDS) {
197 		actctx.action_type = current_action;
198 		if (opt_output_file == NULL) {
199 			actctx.action_list_cards.output_file = NULL;
200 		} else {
201 			actctx.action_list_cards.output_file = g_strdup (opt_output_file);
202 		}
203 		actctx.action_list_cards.IsCSV = IsCSV;
204 		actctx.action_list_cards.IsVCard = IsVCard;
205 		actctx.action_list_cards.addressbook_source_uid =
206 			g_strdup (opt_addressbook_source_uid);
207 		actctx.action_list_cards.async_mode = opt_async_mode;
208 		actctx.action_list_cards.file_size = opt_file_size;
209 
210 		action_list_cards_init (registry, &actctx);
211 
212 	} else {
213 		g_warning (_("Unhandled error"));
214 		exit (-1);
215 	}
216 
217 	/*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.  */
218 
219 	return 0;
220 }