File: import_gui.c

package info (click to toggle)
jpilot 0.99.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,808 kB
  • ctags: 1,967
  • sloc: ansic: 29,655; sh: 8,387; makefile: 352; sed: 93
file content (499 lines) | stat: -rw-r--r-- 14,096 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* import_gui.c
 * A module of J-Pilot http://jpilot.org
 *
 * Copyright (C) 1999-2001 by Judd Montgomery
 *
 * 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; version 2 of the License.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "config.h"
#include "i18n.h"
#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "utils.h"
#include "prefs.h"
#include "log.h"

static GtkWidget *radio_types[MAX_IMPORT_TYPES+1];
static int radio_file_types[MAX_IMPORT_TYPES+1];
static int line_selected;
static GtkWidget *filew=NULL;
static GtkWidget *import_record_ask_window=NULL;
static int glob_import_record_ask_button_pressed;
int (*glob_import_callback)(GtkWidget *parent_window, char *file_path, int type);
int glob_type_selected;


/*
 * This function reads until it finds a non separator character,
 * then reads a string.  Spaces, commas, etc. can be inside quotes.
 * Escaped quotes (double quotes) are converted to single.
 * Return value is size of text.
 *  -1 for EOL
 */
int read_csv_field(FILE *in, char *text, int size, int new_line)
{
   int n, c;
   char sep[]=",\t \r\n";
   int quoted;

   n=0;
   quoted=0;
   text[0]='\0';
   /* Read until a non separator character is found */
   while (1) {
      c=getc(in);
      if (feof(in)) {
	 return 0;
	 text[++n]='\0';
      }
      if (!strchr(sep, c)) {
	 ungetc(c, in);
	 break;
      }
   }
   /* Read the field */
   while (1) {
      c=fgetc(in);
      if (feof(in)) break;
      /* Look for quote */
      if (c=='"') {
	 if (quoted) {
	    c=fgetc(in);
	    if (c=='"') {
	       /* Found double quotes, convert to single */
	    } else {
	       quoted=(quoted&1)^1;
	       ungetc(c, in);
	       continue;
	    }
	 } else {
	    quoted=1;
	    continue;
	 }
      }
      /* Look for separators */
      if (strchr(sep, c)) {
	 if (!quoted) {
	    text[n++]='\0';
	    break;
	 }
      }
      text[n++]=c;
      if (n+1>size) return n;
   }
   text[n++]='\0';
   return n;
}

char *str_type(int type)
{
   switch (type) {
    case IMPORT_TYPE_UNKNOWN:
      return "IMPORT_TYPE_UNKNOWN";
    case IMPORT_TYPE_TEXT:
      return "IMPORT_TYPE_TEXT";
    case IMPORT_TYPE_DAT:
      return "IMPORT_TYPE_DAT";
    case IMPORT_TYPE_CSV:
      return "IMPORT_TYPE_CSV";
    case IMPORT_TYPE_XML:
      return "IMPORT_TYPE_XML";
   }
   return "?";
}

int guess_file_type(char *path)
{
   FILE *in;
   char text[256];

   if (!path) return IMPORT_TYPE_UNKNOWN;
   in=fopen(path, "r");
   if (!in) return IMPORT_TYPE_UNKNOWN;
   if (dat_check_if_dat_file(in)) {
      fclose(in);
      return IMPORT_TYPE_DAT;
   }
   fseek(in, 0, SEEK_SET);
   fread(text, 1, 15, in);
   if (!strncmp(text, "CSV ", 4)) {
      fclose(in);
      return IMPORT_TYPE_CSV;
   }
   fclose(in);
   return IMPORT_TYPE_TEXT;
}

/* Main import file selection window */
static gboolean cb_destroy(GtkWidget *widget)
{
   filew = NULL;
   return FALSE;
}

static void
cb_quit(GtkWidget *widget,
	gpointer   data)
{
   char *sel;
   char dir[MAX_PREF_VALUE+2];
   int i;

   jpilot_logf(LOG_DEBUG, "Quit\n");

   sel = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filew));
   strncpy(dir, sel, MAX_PREF_VALUE);
   dir[MAX_PREF_VALUE]='\0';
   i=strlen(dir)-1;
   if (i<0) i=0;
   if (dir[i]!='/') {
      for (i=strlen(dir); i>=0; i--) {
	 if (dir[i]=='/') {
	    dir[i+1]='\0';
	    break;
	 }
      }
   }

   set_pref(PREF_MEMO_IMPORT_PATH, 0, dir);

   filew = NULL;
   gtk_widget_destroy(data);
}

static void
cb_type(GtkWidget *widget,
	gpointer   data)
{
   glob_type_selected=GPOINTER_TO_INT(data);
}

static void
cb_import(GtkWidget *widget,
	  gpointer   filesel)
{
   char *sel;
   struct stat statb;

   jpilot_logf(LOG_DEBUG, "cb_import\n");
   sel = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
   jpilot_logf(LOG_DEBUG, "file selected [%s]\n", sel);

   /*Check to see if its a regular file */
   if (stat(sel, &statb)) {
      jpilot_logf(LOG_DEBUG, "File selected was not stat-able\n");
      return;
   }
   if (!S_ISREG(statb.st_mode)) {
      jpilot_logf(LOG_DEBUG, "File selected was not a regular file\n");
      return;
   }
   glob_import_callback(filesel, sel, glob_type_selected);
}

static void cb_import_select_row(GtkWidget *file_clist,
				 gint row,
				 gint column,
				 GdkEventButton *bevent,
				 gpointer data)
{
   char *sel;
   struct stat statb;
   int guessed_type;
   int i;

   jpilot_logf(LOG_DEBUG, "cb_import_select_row\n");
   sel = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filew));

   /*Check to see if its a regular file */
   if (stat(sel, &statb)) {
      jpilot_logf(LOG_DEBUG, "File selected was not stat-able\n");
      return;
   }
   if (!S_ISREG(statb.st_mode)) {
      jpilot_logf(LOG_DEBUG, "File selected was not a regular file\n");
      return;
   }

   guessed_type=guess_file_type(sel);
   for (i=0; i<MAX_IMPORT_TYPES; i++) {
      if (radio_types[i]==NULL) break;
      if (guessed_type==radio_file_types[i]) {
	 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_types[i]), TRUE);
	 break;
      }
   }

   return;
}

void import_gui(GtkWidget *main_window, GtkWidget *main_pane,
		char *type_desc[], int type_int[],
		int (*import_callback)(GtkWidget *parent_window,
				       char *file_path, int type))
{
   GtkWidget *button;
   GtkWidget *vbox, *hbox;
   GtkWidget *label;
   char temp[256];
   const char *svalue;
   GSList *group;
   int i;
   int pw, ph, px, py;

   if (filew) {
      return;
   }

   line_selected = -1;

   gdk_window_get_size(main_window->window, &pw, &ph);
   gdk_window_get_root_origin(main_window->window, &px, &py);

   pw = GTK_PANED(main_pane)->handle_xpos;
   px+=40;

   g_snprintf(temp, 255, "%s %s", PN, _("Import"));
   temp[255]='\0';

   filew = gtk_widget_new(GTK_TYPE_FILE_SELECTION,
			  "type", GTK_WINDOW_DIALOG,
			  "x", px, "y", py,
			  "width", pw, "height", ph,
			  "title", temp,
			  NULL);

   get_pref(PREF_MEMO_IMPORT_PATH, NULL, &svalue);
   if (svalue && svalue[0]) {
      gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), svalue);
   }

   glob_import_callback=import_callback;

   /* Set the type to match the first button, which will be set */
   glob_type_selected=type_int[0];

   gtk_widget_hide((GTK_FILE_SELECTION(filew)->cancel_button));
   gtk_signal_connect(GTK_OBJECT(filew), "destroy",
                      GTK_SIGNAL_FUNC(cb_destroy), filew);

   /*Even though I hide the ok button I still want to connect its signal */
   /*because a double click on the file name also calls this callback */
   gtk_widget_hide(GTK_WIDGET(GTK_FILE_SELECTION(filew)->ok_button));   
   gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filew)->ok_button),
		      "clicked", GTK_SIGNAL_FUNC(cb_import), filew);

   label = gtk_label_new(_("To change to a hidden directory type it below and hit TAB"));
   gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->main_vbox),
		      label, FALSE, FALSE, 0);
   gtk_widget_show(label);


   button = gtk_button_new_with_label(_("Import"));
   gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->ok_button->parent),
		      button, TRUE, TRUE, 0);
   gtk_signal_connect(GTK_OBJECT(button),
		      "clicked", GTK_SIGNAL_FUNC(cb_import), filew);
   gtk_widget_show(button);

   button = gtk_button_new_with_label(_("Done"));
   gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->ok_button->parent),
		      button, TRUE, TRUE, 0);
   gtk_signal_connect(GTK_OBJECT(button),
		      "clicked", GTK_SIGNAL_FUNC(cb_quit), filew);
   gtk_widget_show(button);

   /* File Type */
   vbox=gtk_vbox_new(FALSE, 0);
   hbox=gtk_hbox_new(FALSE, 0);
   gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->action_area),
		      vbox, TRUE, TRUE, 0);
   gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
   label = gtk_label_new(_("Import File Type"));
   gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);

   group = NULL;
   for (i=0; i<MAX_IMPORT_TYPES; i++) {
      if (type_desc[i]==NULL) break;
      radio_types[i] = gtk_radio_button_new_with_label(group, type_desc[i]);
      radio_file_types[i] = type_int[i];
      group = gtk_radio_button_group(GTK_RADIO_BUTTON(radio_types[i]));
      gtk_box_pack_start(GTK_BOX(vbox), radio_types[i], TRUE, TRUE, 0);
      gtk_signal_connect(GTK_OBJECT(radio_types[i]), "clicked",
			 GTK_SIGNAL_FUNC(cb_type), GINT_TO_POINTER(type_int[i]));
   }
   radio_types[i]=NULL;
   radio_file_types[i]=0;

   /* This callback is for a file guess algorithm and to pre-push
    * the type buttons */
   gtk_signal_connect(GTK_OBJECT(GTK_CLIST(GTK_FILE_SELECTION(filew)->file_list)),
		      "select_row", GTK_SIGNAL_FUNC(cb_import_select_row), NULL);

   gtk_widget_show_all(vbox);

   gtk_widget_show(filew);
}


/* 
 * Import record display and choice.
 * Put up the record for viewing and ask if it should be imported.
 */
/* Main import file selection window */
static gboolean cb_import_record_ask_destroy(GtkWidget *widget)
{
   import_record_ask_window = NULL;
   gtk_main_quit();
   return FALSE;
}

static void
cb_import_record_ask_quit(GtkWidget *widget,
			  gpointer   data)
{
   glob_import_record_ask_button_pressed=GPOINTER_TO_INT(data);
   gtk_widget_destroy(import_record_ask_window);
}

int import_record_ask(GtkWidget *main_window, GtkWidget *pane,
		      char *text, struct CategoryAppInfo *cai,
		      char *old_cat_name,
		      int priv, int suggested_cat_num, int *new_cat_num)
{
   GtkWidget *button;
   GtkWidget *vbox;
   GtkWidget *temp_hbox;
   GtkWidget *textw;
   GtkWidget *label;
   GtkWidget *vscrollbar;
   int pw, ph;
   gint px, py;
   char str[100];

   if (import_record_ask_window) {
      return 0;
   }

   /* There is no support yet for changing the suggested category */
   /* Like a menu for selecting cat to be imported into, etc. */
   *new_cat_num = suggested_cat_num;

   glob_import_record_ask_button_pressed = DIALOG_SAID_IMPORT_QUIT;

   gdk_window_get_size(main_window->window, &pw, &ph);

   gdk_window_get_root_origin(main_window->window, &px, &py);

   pw = GTK_PANED(pane)->handle_xpos;
   px+=40;

   import_record_ask_window = gtk_widget_new(GTK_TYPE_WINDOW,
					     "type", GTK_WINDOW_DIALOG,
					     "x", px, "y", py,
					     "width", pw, "height", ph,
					     "title", _("Import"),
					     NULL);

   gtk_container_set_border_width(GTK_CONTAINER(import_record_ask_window), 5);

   gtk_signal_connect(GTK_OBJECT(import_record_ask_window), "destroy",
                      GTK_SIGNAL_FUNC(cb_import_record_ask_destroy),
		      import_record_ask_window);

   vbox=gtk_vbox_new(FALSE, 0);
   gtk_container_add(GTK_CONTAINER(import_record_ask_window), vbox);

   /* Private */
   if (priv) {
      g_snprintf(str, 50, _("Record was marked as private"));
   } else {
      g_snprintf(str, 50, _("Record was not marked as private"));
   }
   label = gtk_label_new(str);
   gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
   gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);


   g_snprintf(str, 60, "Category before import was: [%s]", old_cat_name);
   label = gtk_label_new(str);
   gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
   gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);


   g_snprintf(str, 60, "Record will be put in category [%s]",
	      cai->name[suggested_cat_num]);
   label = gtk_label_new(str);
   gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
   gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);


   /* Make a text window with scrollbar */
   temp_hbox = gtk_hbox_new(FALSE, 0);
   gtk_box_pack_start(GTK_BOX(vbox), temp_hbox, TRUE, TRUE, 0);

   textw = gtk_text_new(NULL, NULL);
   gtk_text_set_editable(GTK_TEXT(textw), FALSE);
   gtk_text_set_word_wrap(GTK_TEXT(textw), TRUE);
   vscrollbar = gtk_vscrollbar_new(GTK_TEXT(textw)->vadj);
   gtk_box_pack_start(GTK_BOX(temp_hbox), textw, TRUE, TRUE, 0);
   gtk_box_pack_start(GTK_BOX(temp_hbox), vscrollbar, FALSE, FALSE, 0);

   if (text) {
      gtk_text_insert(GTK_TEXT(textw), NULL, NULL, NULL, text, -1);
   }

   /* Box for buttons  */
   temp_hbox = gtk_hbox_new(FALSE, 0);
   gtk_box_pack_start(GTK_BOX(vbox), temp_hbox, FALSE, FALSE, 0);

   /* Import button */
   button = gtk_button_new_with_label(_("Import"));
   gtk_box_pack_start(GTK_BOX(temp_hbox), button, TRUE, TRUE, 0);
   gtk_signal_connect(GTK_OBJECT(button), "clicked",
		      GTK_SIGNAL_FUNC(cb_import_record_ask_quit),
		      GINT_TO_POINTER(DIALOG_SAID_IMPORT_YES));

   /* Import All button */
   button = gtk_button_new_with_label(_("Import All"));
   gtk_box_pack_start(GTK_BOX(temp_hbox), button, TRUE, TRUE, 0);
   gtk_signal_connect(GTK_OBJECT(button), "clicked",
		      GTK_SIGNAL_FUNC(cb_import_record_ask_quit),
		      GINT_TO_POINTER(DIALOG_SAID_IMPORT_ALL));

   /* Skip button */
   button = gtk_button_new_with_label(_("Skip"));
   gtk_box_pack_start(GTK_BOX(temp_hbox), button, TRUE, TRUE, 0);
   gtk_signal_connect(GTK_OBJECT(button), "clicked",
		      GTK_SIGNAL_FUNC(cb_import_record_ask_quit),
		      GINT_TO_POINTER(DIALOG_SAID_IMPORT_SKIP));

   /* Quit button */
   button = gtk_button_new_with_label(_("Quit"));
   gtk_box_pack_start(GTK_BOX(temp_hbox), button, TRUE, TRUE, 0);
   gtk_signal_connect(GTK_OBJECT(button), "clicked",
		      GTK_SIGNAL_FUNC(cb_import_record_ask_quit),
		      GINT_TO_POINTER(DIALOG_SAID_IMPORT_QUIT));

   gtk_widget_show_all(import_record_ask_window);

   gtk_window_set_modal(GTK_WINDOW(import_record_ask_window), TRUE);

   gtk_main();

   return glob_import_record_ask_button_pressed;
}