"Fossies" - the Fresh Open Source Software Archive

Member "jpilot-2_0_1/export_gui.c" (3 Apr 2021, 12981 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 "export_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  * export_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 <sys/stat.h>
   24 #include <gtk/gtk.h>
   25 #include <stdlib.h>
   26 #include <string.h>
   27 #include <pi-appinfo.h>
   28 
   29 #include "i18n.h"
   30 #include "utils.h"
   31 #include "log.h"
   32 #include "prefs.h"
   33 #include "export.h"
   34 
   35 /********************************* Constants **********************************/
   36 #define NUM_CAT_ITEMS 16
   37 
   38 #define BROWSE_OK     1
   39 #define BROWSE_CANCEL 2
   40 
   41 /******************************* Global vars **********************************/
   42 static GtkWidget *export_treeView;
   43 static int export_category;
   44 
   45 static int glob_export_browse_pressed;
   46 static int glob_pref_export;
   47 
   48 static GtkWidget *export_radio_type[10];
   49 static int glob_export_type;
   50 static GtkWidget *save_as_entry;
   51 GtkWidget *category_menu;
   52 
   53 /****************************** Prototypes ************************************/
   54 static void (*glob_cb_export_menu)(GtkWidget *treeView, int category);
   55 
   56 static GtkWidget *(*glob_cb_init_menu)();
   57 
   58 static void (*glob_cb_export_done)(GtkWidget *widget, const char *filename);
   59 
   60 static void (*glob_cb_export_ok)(GtkWidget *export_window,
   61                                  GtkWidget *treeView,
   62                                  int type,
   63                                  const char *filename);
   64 
   65 /****************************** Main Code *************************************/
   66 /* 
   67  * Browse GUI
   68  */
   69 static gboolean cb_export_browse_destroy(GtkWidget *widget) {
   70     gtk_main_quit();
   71     return FALSE;
   72 }
   73 
   74 static void cb_export_browse_cancel(GtkWidget *widget, gpointer data) {
   75     glob_export_browse_pressed = BROWSE_CANCEL;
   76     gtk_widget_destroy(widget);
   77 }
   78 
   79 static void cb_export_browse_ok(GtkWidget *widget, gpointer data) {
   80     char *sel;
   81     int pref = GPOINTER_TO_INT(data);
   82     glob_export_browse_pressed = BROWSE_OK;
   83     if (pref) {
   84         sel = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (widget));
   85         set_pref(pref, 0, sel, TRUE);
   86         gtk_entry_set_text(GTK_ENTRY(save_as_entry), sel);
   87     }
   88     gtk_widget_destroy(widget);
   89 }
   90 
   91 int export_browse(GtkWidget *main_window, int pref_export) {
   92     GtkWidget *fileChooserWidget;
   93     const char *svalue;
   94     char dir[MAX_PREF_LEN + 2];
   95     int i;
   96 
   97     glob_export_browse_pressed = 0;
   98     if (pref_export) {
   99         glob_pref_export = pref_export;
  100     } else {
  101         glob_pref_export = 0;
  102     }
  103 
  104     if (pref_export) {
  105         get_pref(pref_export, NULL, &svalue);
  106         g_strlcpy(dir, svalue, sizeof(dir));
  107         i = strlen(dir) - 1;
  108         if (i < 0) i = 0;
  109         if (dir[i] != '/') {
  110             for (i = strlen(dir); i >= 0; i--) {
  111                 if (dir[i] == '/') {
  112                     dir[i + 1] = '\0';
  113                     break;
  114                 }
  115             }
  116         }
  117 
  118         if (chdir(dir) < 0) {
  119             jp_logf(JP_LOG_WARN, "chdir failed %s %d\n", __FILE__, __LINE__);
  120         }
  121     }
  122     fileChooserWidget = gtk_file_chooser_dialog_new(_("File Browser"), GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_SAVE,
  123                                                     "Cancel", GTK_RESPONSE_CANCEL, "Open",
  124                                                     GTK_RESPONSE_ACCEPT, NULL);
  125     gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(fileChooserWidget), TRUE);
  126     //This blocks main thread until they close the dialog.
  127     if (gtk_dialog_run(GTK_DIALOG (fileChooserWidget)) == GTK_RESPONSE_ACCEPT) {
  128         cb_export_browse_ok(fileChooserWidget, GINT_TO_POINTER(pref_export));
  129         glob_export_browse_pressed = BROWSE_OK;
  130     } else {
  131         cb_export_browse_cancel(fileChooserWidget, NULL);
  132     }
  133     cb_export_browse_destroy(fileChooserWidget);
  134 
  135     gtk_main();
  136     return glob_export_browse_pressed;
  137 }
  138 
  139 /* End Export Browse */
  140 
  141 /*
  142  * Start Export code
  143  */
  144 static gboolean cb_export_destroy(GtkWidget *widget) {
  145     const char *filename;
  146 
  147     filename = gtk_entry_get_text(GTK_ENTRY(save_as_entry));
  148     if (glob_cb_export_done) {
  149         glob_cb_export_done(widget, filename);
  150     }
  151     gtk_list_store_clear(GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(export_treeView))));
  152     if (category_menu != NULL) {
  153         GtkTreeModel *clearingmodel = gtk_combo_box_get_model(GTK_COMBO_BOX(category_menu));
  154         gtk_list_store_clear(GTK_LIST_STORE(clearingmodel));
  155     }
  156     gtk_main_quit();
  157 
  158     return FALSE;
  159 }
  160 
  161 static void cb_ok(GtkWidget *widget, gpointer data) {
  162     const char *filename;
  163 
  164     filename = gtk_entry_get_text(GTK_ENTRY(save_as_entry));
  165 
  166     if (glob_cb_export_ok) {
  167         glob_cb_export_ok(data, export_treeView, glob_export_type, filename);
  168     }
  169 
  170     gtk_widget_destroy(data);
  171 }
  172 
  173 static void cb_export_browse(GtkWidget *widget, gpointer data) {
  174     export_browse(GTK_WIDGET(data), glob_pref_export);
  175 }
  176 
  177 static void cb_export_quit(GtkWidget *widget, gpointer data) {
  178     gtk_widget_destroy(data);
  179 }
  180 
  181 static void cb_export_type(GtkWidget *widget, gpointer data) {
  182     glob_export_type = GPOINTER_TO_INT(data);
  183 }
  184 
  185 static void cb_export_category(GtkComboBox *item, int selection) {
  186     if (!item) return;
  187     if (gtk_combo_box_get_active(GTK_COMBO_BOX(item)) < 0) {
  188         return;
  189     }
  190     int selectedItem = get_selected_category_from_combo_box(item);
  191     if (selectedItem == -1) {
  192         return;
  193     }
  194     export_category = selectedItem;
  195     jp_logf(JP_LOG_DEBUG, "cb_export_category() cat=%d\n", export_category);
  196     if (glob_cb_export_menu && export_treeView != NULL) {
  197         glob_cb_export_menu(export_treeView, export_category);
  198         gtk_tree_selection_select_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(export_treeView)));
  199     }
  200 
  201 
  202     jp_logf(JP_LOG_DEBUG, "Leaving cb_export_category()\n");
  203     //gtk_combo_box_set_active(GTK_COMBO_BOX(item), pos);
  204 
  205 }
  206 
  207 int export_gui(GtkWidget *main_window,
  208                int w, int h, int x, int y,
  209                int columns,
  210                struct sorted_cats *sort_l,
  211                int pref_export,
  212                char *type_text[],
  213                int type_int[],
  214                GtkWidget *(*cb_init_menu)(),
  215                void (*cb_export_menu)(GtkWidget *treeView, int category),
  216                void (*cb_export_done)(GtkWidget *widget,
  217                                       const char *filename),
  218                void (*cb_export_ok)(GtkWidget *export_window,
  219                                     GtkWidget *treeView,
  220                                     int type,
  221                                     const char *filename)
  222 ) {
  223     GtkWidget *export_window;
  224     GtkWidget *button;
  225     GtkWidget *vbox;
  226     GtkWidget *hbox;
  227 
  228 
  229     GtkWidget *scrolled_window;
  230     GtkWidget *label;
  231     char title[256];
  232     GSList *group;
  233     int i;
  234     const char *svalue;
  235 
  236     jp_logf(JP_LOG_DEBUG, "export_gui()\n");
  237 
  238     export_category = CATEGORY_ALL;
  239 
  240     /* Set the export type to the first type available */
  241     glob_export_type = type_int[0];
  242     glob_cb_init_menu = cb_init_menu;
  243     glob_cb_export_menu = cb_export_menu;
  244     glob_cb_export_done = cb_export_done;
  245     glob_cb_export_ok = cb_export_ok;
  246 
  247     glob_pref_export = pref_export;
  248 
  249     g_snprintf(title, sizeof(title), "%s %s", PN, _("Export"));
  250 
  251     export_window = gtk_widget_new(GTK_TYPE_WINDOW,
  252                                    "type", GTK_WINDOW_TOPLEVEL,
  253                                    "title", title,
  254                                    NULL);
  255 
  256     gtk_window_set_default_size(GTK_WINDOW(export_window), w, h);
  257     //gtk_widget_set_uposition(GTK_WIDGET(export_window), x, y);
  258 
  259     gtk_window_set_modal(GTK_WINDOW(export_window), TRUE);
  260     gtk_window_set_transient_for(GTK_WINDOW(export_window), GTK_WINDOW(main_window));
  261 
  262     gtk_container_set_border_width(GTK_CONTAINER(export_window), 5);
  263 
  264     g_signal_connect(G_OBJECT(export_window), "destroy",
  265                        G_CALLBACK(cb_export_destroy), export_window);
  266 
  267     vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  268     gtk_container_add(GTK_CONTAINER(export_window), vbox);
  269 
  270     /* Label for instructions */
  271     label = gtk_label_new(_("Select records to be exported"));
  272     gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  273     label = gtk_label_new(_("Use Ctrl and Shift Keys"));
  274     gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  275 
  276     /* Put the export category menu up */
  277     if (category_menu  && GTK_IS_COMBO_BOX(category_menu)) {
  278         GtkTreeModel *clearingmodel = gtk_combo_box_get_model(GTK_COMBO_BOX(category_menu));
  279         gtk_list_store_clear(GTK_LIST_STORE(clearingmodel));
  280     }
  281     make_category_menu(&category_menu, sort_l,
  282                        cb_export_category, TRUE, FALSE);
  283     gtk_combo_box_set_active(GTK_COMBO_BOX(category_menu), 0);
  284     gtk_box_pack_start(GTK_BOX(vbox), category_menu, FALSE, FALSE, 0);
  285 
  286     /* Put the record list window up */
  287     scrolled_window = gtk_scrolled_window_new(NULL, NULL);
  288     gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 0);
  289     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
  290                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  291     gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
  292 
  293 
  294     if (glob_cb_init_menu) {
  295         export_treeView = glob_cb_init_menu();
  296     } else {
  297         export_treeView = gtk_tree_view_new();
  298     }
  299     gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(export_treeView)),
  300                                 GTK_SELECTION_MULTIPLE);
  301     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(export_treeView), gtk_false());
  302 
  303     gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(export_treeView));
  304 
  305     /* Export Type buttons */
  306     group = NULL;
  307     for (i = 0; i < 100; i++) {
  308         if (type_text[i] == NULL) break;
  309         export_radio_type[i] = gtk_radio_button_new_with_label(group, _(type_text[i]));
  310         group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(export_radio_type[i]));
  311         gtk_box_pack_start(GTK_BOX(vbox), export_radio_type[i], FALSE, FALSE, 0);
  312         g_signal_connect(G_OBJECT(export_radio_type[i]), "pressed",
  313                            G_CALLBACK(cb_export_type),
  314                            GINT_TO_POINTER(type_int[i]));
  315     }
  316     export_radio_type[i] = NULL;
  317 
  318     /* Save As entry */
  319     hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
  320     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
  321     label = gtk_label_new(_("Save as"));
  322     gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
  323     save_as_entry = gtk_entry_new();
  324     gtk_entry_set_max_length(GTK_ENTRY(save_as_entry), 250);
  325     svalue = NULL;
  326     if (glob_pref_export) {
  327         get_pref(glob_pref_export, NULL, &svalue);
  328     }
  329     if (svalue) {
  330         gtk_entry_set_text(GTK_ENTRY(save_as_entry), svalue);
  331     }
  332     gtk_box_pack_start(GTK_BOX(hbox), save_as_entry, TRUE, TRUE, 0);
  333 
  334     /* Browse button */
  335     button = gtk_button_new_with_label(_("Browse"));
  336     gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
  337     g_signal_connect(G_OBJECT(button), "clicked",
  338                        G_CALLBACK(cb_export_browse), export_window);
  339 
  340     /* Cancel/OK buttons */
  341     hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
  342     gtk_container_set_border_width(GTK_CONTAINER(hbox), 12);
  343     gtk_button_box_set_layout(GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
  344      gtk_box_set_spacing(GTK_BOX(hbox), 6);
  345     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
  346 
  347     button = gtk_button_new_with_label("Cancel");
  348     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
  349     g_signal_connect(G_OBJECT(button), "clicked",
  350                        G_CALLBACK(cb_export_quit), export_window);
  351 
  352     button = gtk_button_new_with_label("OK");
  353     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
  354     g_signal_connect(G_OBJECT(button), "clicked",
  355                        G_CALLBACK(cb_ok), export_window);
  356 
  357     if (glob_cb_export_menu) {
  358         glob_cb_export_menu(GTK_WIDGET(export_treeView), export_category);
  359     }
  360 
  361     gtk_widget_show_all(export_window);
  362 
  363     gtk_tree_selection_select_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(export_treeView)));
  364 
  365     gtk_main();
  366 
  367     return EXIT_SUCCESS;
  368 }
  369