"Fossies" - the Fresh Open Source Software Archive

Member "jpilot-2_0_1/import_gui.c" (3 Apr 2021, 14802 Bytes) of package /linux/privat/jpilot-2_0_1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "import_gui.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.8.2_vs_2_0_1.

    1 /*******************************************************************************
    2  * import_gui.c
    3  * A module of J-Pilot http://jpilot.org
    4  *
    5  * Copyright (C) 1999-2014 by Judd Montgomery
    6  *
    7  * This program is free software; you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by
    9  * the Free Software Foundation; version 2 of the License.
   10  *
   11  * This program is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program; if not, write to the Free Software
   18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   19  ******************************************************************************/
   20 
   21 /********************************* Includes ***********************************/
   22 #include "config.h"
   23 #include <gtk/gtk.h>
   24 #include <string.h>
   25 #include <stdlib.h>
   26 #include <sys/types.h>
   27 #include <sys/stat.h>
   28 #include <unistd.h>
   29 
   30 #include "i18n.h"
   31 #include "utils.h"
   32 #include "prefs.h"
   33 #include "log.h"
   34 #include "export.h"
   35 
   36 /******************************* Global vars **********************************/
   37 static GtkWidget *radio_types[MAX_IMPORT_TYPES+1];
   38 static int radio_file_types[MAX_IMPORT_TYPES+1];
   39 static int line_selected;
   40 static GtkWidget *import_record_ask_window=NULL;
   41 static int glob_import_record_ask_button_pressed;
   42 static int glob_type_selected;
   43 
   44 /****************************** Prototypes ************************************/
   45 static int (*glob_import_callback)(GtkWidget *parent_window, const char *file_path, int type);
   46 
   47 /****************************** Main Code *************************************/
   48 
   49 /*
   50  * This function reads a CSV entry until it finds the next seperator(',').
   51  * Spaces, commas, newlines, etc. are allowed inside quotes.
   52  * Escaped quotes (double quotes) are converted to single occurrences.
   53  * Return value is size of text including null terminator.
   54  */
   55 int read_csv_field(FILE *in, char *text, int size)
   56 {
   57    int n, c;
   58    char sep[]=",\t \r\n";
   59    char whitespace[]="\t \r\n";
   60    int quoted;
   61 
   62    n=0;
   63    quoted=FALSE;
   64    text[0]='\0';
   65 
   66    /* Read the field */
   67    while (1) {
   68       c=fgetc(in);
   69 
   70       /* Look for EOF */
   71       if (feof(in)) 
   72          break;
   73       /* Look for quote */
   74       if (c=='"') {
   75          if (quoted) {
   76             c=fgetc(in);
   77             if (c=='"') {
   78                /* Found double quotes, convert to single */
   79             } else {
   80                quoted=(quoted&1)^1;
   81                ungetc(c, in);
   82                continue;
   83             }
   84          } else {
   85             quoted=TRUE;
   86             continue;
   87          }
   88       }
   89       /* Look for separators */
   90       if (strchr(sep, c)) {
   91          if (!quoted) {
   92             if (c != ',') {
   93                /* skip whitespace  */
   94                while (1) {
   95                   c=getc(in);
   96                   if (feof(in)) {
   97                      text[n++]='\0';
   98                      return n;
   99                   }
  100                   if (strchr(whitespace, c)) {
  101                      continue;
  102                   } else {
  103                      ungetc(c, in);
  104                      break;
  105                   }
  106                }
  107             }
  108             /* after sep processing, break out of reading field */
  109             break;   
  110          } /* end if !quoted */
  111       } /* end if separator */
  112 
  113       /* Ordinary character, add to field */
  114       text[n++]=c;
  115       if (n+1>=size)
  116       {
  117          text[n++]='\0';
  118          return n;
  119       }
  120    }   /* end while(1) reading field */
  121 
  122    /* Terminate string and return */
  123    text[n++]='\0';
  124    return n;
  125 }
  126 
  127 /*
  128 static int guess_file_type(const char *path)
  129 {
  130    FILE *in;
  131    char text[256];
  132 
  133    if (!path) return IMPORT_TYPE_UNKNOWN;
  134    in=fopen(path, "r");
  135    if (!in) return IMPORT_TYPE_UNKNOWN;
  136    if (dat_check_if_dat_file(in)) {
  137       fclose(in);
  138       return IMPORT_TYPE_DAT;
  139    }
  140    fseek(in, 0, SEEK_SET);
  141    if (fread(text, 1, 15, in) < 1) {
  142       jp_logf(JP_LOG_WARN, "fread failed %s %d\n", __FILE__, __LINE__);
  143    }
  144    if (!strncmp(text, "CSV ", 4)) {
  145       fclose(in);
  146       return IMPORT_TYPE_CSV;
  147    }
  148    fclose(in);
  149    return IMPORT_TYPE_TEXT;
  150 }*/
  151 
  152 /* Main import file selection window */
  153 static gboolean cb_destroy(GtkWidget *widget)
  154 {
  155     widget = NULL;
  156     return FALSE;
  157 }
  158 
  159 static void cb_quit(GtkWidget *widget, gpointer data)
  160 {
  161    const char *sel;
  162    char dir[MAX_PREF_LEN+2];
  163    int i;
  164 
  165    jp_logf(JP_LOG_DEBUG, "Quit\n");
  166 
  167    sel = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
  168    strncpy(dir, sel, MAX_PREF_LEN);
  169    dir[MAX_PREF_LEN]='\0';
  170    i=strlen(dir)-1;
  171    if (i<0) i=0;
  172    if (dir[i]!='/') {
  173       for (i=strlen(dir); i>=0; i--) {
  174          if (dir[i]=='/') {
  175             dir[i+1]='\0';
  176             break;
  177          }
  178       }
  179    }
  180 
  181    set_pref(PREF_MEMO_IMPORT_PATH, 0, dir, TRUE);
  182 
  183    gtk_widget_destroy(widget);
  184 }
  185 
  186 static void cb_type(GtkWidget *widget, gpointer data)
  187 {
  188    glob_type_selected=GPOINTER_TO_INT(data);
  189 }
  190 
  191 static void cb_import(GtkWidget *widget, gpointer filesel)
  192 {
  193    char *sel;
  194    struct stat statb;
  195 
  196    jp_logf(JP_LOG_DEBUG, "cb_import\n");
  197    sel = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
  198    jp_logf(JP_LOG_DEBUG, "file selected [%s]\n", sel);
  199 
  200    /* Check to see if its a regular file */
  201    if (stat(sel, &statb)) {
  202       jp_logf(JP_LOG_DEBUG, "File selected was not stat-able\n");
  203       g_free(sel);
  204       return;
  205    }
  206    if (!S_ISREG(statb.st_mode)) {
  207       jp_logf(JP_LOG_DEBUG, "File selected was not a regular file\n");
  208       g_free(sel);
  209       return;
  210    }
  211    glob_import_callback(widget, sel, glob_type_selected);
  212    g_free(sel);
  213 
  214    cb_quit(widget, widget);
  215 }
  216 
  217 static gboolean cb_import_record_ask_destroy(GtkWidget *widget)
  218 {
  219    import_record_ask_window = NULL;
  220    gtk_main_quit();
  221    return FALSE;
  222 }
  223 
  224 static void
  225 cb_import_record_ask_quit(GtkWidget *widget,
  226                           gpointer   data)
  227 {
  228    glob_import_record_ask_button_pressed=GPOINTER_TO_INT(data);
  229    gtk_widget_destroy(import_record_ask_window);
  230 }
  231 
  232 /*
  233  * Import record display and choice.
  234  * Put up the record for viewing and ask if it should be imported.
  235  */
  236 int import_record_ask(GtkWidget *main_window, GtkWidget *pane,
  237                       char *text, struct CategoryAppInfo *cai,
  238                       char *old_cat_name,
  239                       int priv, int suggested_cat_num, int *new_cat_num)
  240 {
  241    GtkWidget *button;
  242    GtkWidget *vbox;
  243    GtkWidget *temp_hbox;
  244    GtkWidget *textw;
  245    GObject   *textw_buffer;
  246    GtkWidget *label;
  247    GtkWidget *scrolled_window;
  248    int pw, ph;
  249    gint px, py;
  250    char str[100];
  251    char *l;
  252    long char_set;
  253 
  254    /* There is no support yet for changing the suggested category */
  255    /* A menu for selecting cat to be imported into is desirable */
  256    *new_cat_num = suggested_cat_num;
  257 
  258    glob_import_record_ask_button_pressed = DIALOG_SAID_IMPORT_QUIT;
  259 
  260     pw = gdk_window_get_width(gtk_widget_get_window(main_window));
  261     ph = gdk_window_get_height(gtk_widget_get_window(main_window));
  262    gdk_window_get_root_origin(gtk_widget_get_window(main_window), &px, &py);
  263    pw = gtk_paned_get_position(GTK_PANED(pane));
  264    px+=40;
  265 
  266    import_record_ask_window = gtk_widget_new(GTK_TYPE_WINDOW,
  267                                              "type", GTK_WINDOW_TOPLEVEL,
  268                                              "title", _("Import"),
  269                                              NULL);
  270    gtk_window_set_default_size(GTK_WINDOW(import_record_ask_window), pw, ph);
  271    //gtk_widget_set_uposition(GTK_WIDGET(import_record_ask_window), px, py);
  272    gtk_container_set_border_width(GTK_CONTAINER(import_record_ask_window), 5);
  273    gtk_window_set_modal(GTK_WINDOW(import_record_ask_window), TRUE);
  274    gtk_window_set_transient_for(GTK_WINDOW(import_record_ask_window), GTK_WINDOW(main_window));
  275 
  276    g_signal_connect(G_OBJECT(import_record_ask_window), "destroy",
  277                       G_CALLBACK(cb_import_record_ask_destroy),
  278                       import_record_ask_window);
  279 
  280    vbox=gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  281    gtk_container_add(GTK_CONTAINER(import_record_ask_window), vbox);
  282 
  283    /* Private */
  284    if (priv) {
  285       g_snprintf(str, sizeof(str), _("Record was marked as private"));
  286    } else {
  287       g_snprintf(str, sizeof(str), _("Record was not marked as private"));
  288    }
  289    label = gtk_label_new(str);
  290    gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START);
  291    gtk_widget_set_valign(GTK_WIDGET(label), GTK_ALIGN_START);
  292    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  293 
  294    /* Category */
  295    get_pref(PREF_CHAR_SET, &char_set, NULL);
  296    l = charset_p2newj(old_cat_name, 16, char_set);
  297    g_snprintf(str, sizeof(str), _("Category before import was: [%s]"), l);
  298    g_free(l);
  299    label = gtk_label_new(str);
  300    gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START);
  301    gtk_widget_set_valign(GTK_WIDGET(label), GTK_ALIGN_START);
  302    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  303 
  304 
  305    l = charset_p2newj(cai->name[suggested_cat_num], 16, char_set);
  306    g_snprintf(str, sizeof(str), _("Record will be put in category [%s]"), l);
  307    g_free(l);
  308    label = gtk_label_new(str);
  309    gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START);
  310    gtk_widget_set_valign(GTK_WIDGET(label), GTK_ALIGN_START);
  311    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  312 
  313    /* Text window with scrollbar to display record */
  314    temp_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
  315    gtk_box_pack_start(GTK_BOX(vbox), temp_hbox, TRUE, TRUE, 0);
  316 
  317    textw = gtk_text_view_new();
  318    textw_buffer = G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textw)));
  319    gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textw), FALSE);
  320    gtk_text_view_set_editable(GTK_TEXT_VIEW(textw), FALSE);
  321    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textw), GTK_WRAP_WORD);
  322 
  323    scrolled_window = gtk_scrolled_window_new(NULL, NULL);
  324    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
  325                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  326    gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 1);
  327    gtk_container_add(GTK_CONTAINER(scrolled_window), textw);
  328    gtk_box_pack_start(GTK_BOX(temp_hbox), scrolled_window,TRUE,TRUE,0);
  329 
  330    if (text) {
  331       gtk_text_buffer_set_text(GTK_TEXT_BUFFER(textw_buffer), text, -1);
  332    }
  333 
  334    temp_hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
  335     gtk_box_set_spacing(GTK_BOX(temp_hbox), 6);
  336    gtk_container_set_border_width(GTK_CONTAINER(temp_hbox), 6);
  337    gtk_box_pack_start(GTK_BOX(vbox), temp_hbox, FALSE, FALSE, 0);
  338 
  339    /* Import button */
  340    button = gtk_button_new_with_label(_("Import"));
  341    gtk_box_pack_start(GTK_BOX(temp_hbox), button, TRUE, TRUE, 0);
  342    g_signal_connect(G_OBJECT(button), "clicked",
  343                       G_CALLBACK(cb_import_record_ask_quit),
  344                       GINT_TO_POINTER(DIALOG_SAID_IMPORT_YES));
  345 
  346    /* Import All button */
  347    button = gtk_button_new_with_label(_("Import All"));
  348    gtk_box_pack_start(GTK_BOX(temp_hbox), button, TRUE, TRUE, 0);
  349    g_signal_connect(G_OBJECT(button), "clicked",
  350                       G_CALLBACK(cb_import_record_ask_quit),
  351                       GINT_TO_POINTER(DIALOG_SAID_IMPORT_ALL));
  352 
  353    /* Skip button */
  354    button = gtk_button_new_with_label(_("Skip"));
  355    gtk_box_pack_start(GTK_BOX(temp_hbox), button, TRUE, TRUE, 0);
  356    g_signal_connect(G_OBJECT(button), "clicked",
  357                       G_CALLBACK(cb_import_record_ask_quit),
  358                       GINT_TO_POINTER(DIALOG_SAID_IMPORT_SKIP));
  359 
  360    /* Quit button */
  361    button = gtk_button_new_with_label("Close");
  362    gtk_box_pack_start(GTK_BOX(temp_hbox), button, TRUE, TRUE, 0);
  363    g_signal_connect(G_OBJECT(button), "clicked",
  364                       G_CALLBACK(cb_import_record_ask_quit),
  365                       GINT_TO_POINTER(DIALOG_SAID_IMPORT_QUIT));
  366 
  367    gtk_widget_show_all(import_record_ask_window);
  368 
  369    gtk_main();
  370 
  371    return glob_import_record_ask_button_pressed;
  372 }
  373 
  374 void import_gui(GtkWidget *main_window, GtkWidget *main_pane,
  375                 char *type_desc[], int type_int[],
  376                 int (*import_callback)(GtkWidget *parent_window,
  377                 const char *file_path, int type)) {
  378     GtkWidget *vbox, *hbox;
  379     GtkWidget *label;
  380     char title[256];
  381     const char *svalue;
  382     GSList *group;
  383     int res, i;
  384     GtkWidget *fileChooserWidget;
  385 
  386 
  387     line_selected = -1;
  388 
  389 
  390     g_snprintf(title, sizeof(title), "%s %s", PN, _("Import"));
  391     fileChooserWidget = gtk_file_chooser_dialog_new(_("Import"), GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_OPEN,
  392                                                     "Close", GTK_RESPONSE_CANCEL, "Import", GTK_RESPONSE_ACCEPT,
  393                                                     NULL);
  394     gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(fileChooserWidget), TRUE);
  395     get_pref(PREF_MEMO_IMPORT_PATH, NULL, &svalue);
  396     if (svalue && svalue[0]) {
  397        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fileChooserWidget), svalue);
  398     }
  399     GtkBox *extraWidget = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
  400 
  401     /* File Type radio buttons */
  402     vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  403     hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
  404     gtk_box_pack_start(extraWidget,
  405                        vbox, TRUE, TRUE, 0);
  406     gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
  407     label = gtk_label_new(_("Import File Type"));
  408     gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
  409     group = NULL;
  410     for (i = 0; i < MAX_IMPORT_TYPES; i++) {
  411         if (type_desc[i] == NULL) break;
  412         radio_types[i] = gtk_radio_button_new_with_label(group, _(type_desc[i]));
  413         radio_file_types[i] = type_int[i];
  414         group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_types[i]));
  415         gtk_box_pack_start(GTK_BOX(vbox), radio_types[i], TRUE, TRUE, 0);
  416         g_signal_connect(G_OBJECT(radio_types[i]), "clicked",
  417                            G_CALLBACK(cb_type), GINT_TO_POINTER(type_int[i]));
  418     }
  419     radio_types[i] = NULL;
  420     radio_file_types[i] = 0;
  421     gtk_widget_show_all(vbox);
  422     glob_import_callback = import_callback;
  423 
  424     /* Set the type to match the first button, which will be set */
  425     glob_type_selected = type_int[0];
  426     gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(fileChooserWidget), GTK_WIDGET(extraWidget));
  427     g_signal_connect(G_OBJECT(fileChooserWidget), "destroy",
  428                        G_CALLBACK(cb_destroy), fileChooserWidget);
  429 
  430     res = gtk_dialog_run(GTK_DIALOG(fileChooserWidget));
  431     if (res == GTK_RESPONSE_ACCEPT) {
  432         cb_import(fileChooserWidget, NULL);
  433     } else {
  434         cb_quit(fileChooserWidget, fileChooserWidget);
  435     }
  436 }