Can't get result from a dialog



Hello all,

I've plowed through the docs and through the archives of this mailing
list, google/google groups and what not, but I can't figure out where
I'm going wrong.

I'm writing a GTK+ application that shows logfiles in a "tail -f" kind
of way. It enables the user to also save these logfiles to a local
file for later analysing. Upon clicking the "save" button, the
GtkFileSelection dialog is opened to allow the user to select a file.
If the file already exists, I want the user to be able to specify
whether the file has to be appended to or overwritten, additionally,
there is a cancel option to fall back to the file selection dialog to
enable the user to select a new filename.

However, I can't seem to get the input from the "overwrite?" dialog.
In the code below, nothing below the response =
gtk_dialog_run(GTK_DIALOG(dialog)); is printed. Although the dialog
does dissapear and the user can use the file selection dialog to
select another filename, the application doesn't seem to be able to
return from gtk_dialog_run() (it must though, because I can select
another filename in the file selection after the dialog is
dissapeared). Also when I quit the application, it starts using 100%
cpu untill I kill it from the command line.

The "save_file()" function is the callback routine that is called when
the user clicks "save" to save the file currently before him (the
logfiles are arranged in a notebook widget, with each file dedicated
to a notebook page).

Any help or pointers to docs is greatly appreciated,

Ian.

#define RESPONSE_LOGFILE_OVERWRITE 100 /* user chooses to overwrite
existing file */
#define RESPONSE_LOGFILE_APPEND    101 /* user choosed to append to
existing file */
#define OVERWRITE_QUESTION "The file %s already exists.\nDo you " \
                           "wish to overwrite or append the current " \
                           "buffer to this file?\nClicking \"Cancel\" " \
                           "will return you to the file selection dialog."
#define OVERWRITE_BTN_TXT "Overwrite"
#define APPEND_BTN_TXT    "Append"

static void save_file(GtkWidget *w, gpointer data)
{
    GtkWidget *fileDialog;
    tLogFile *file;
    gchar* filename;

    filename = g_new(gchar, strlen(file->name) + strlen(".log") + 1);
    file = (tLogFile*)data;
    strcpy(filename, file->name);
    strcat(filename, ".log");

    fileDialog = gtk_file_selection_new("Save Logfile");
    gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(fileDialog)->ok_button),
                       "clicked", GTK_SIGNAL_FUNC(write_file), fileDialog);
    gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(fileDialog)->ok_button),
                       "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
                       (gpointer)fileDialog);
    gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(fileDialog)->cancel_button),
                       "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
                       (gpointer)fileDialog);

    gtk_file_selection_set_filename(GTK_FILE_SELECTION(fileDialog), filename);
    gtk_widget_show(fileDialog);
}

static void write_file(GtkWidget *w, gpointer data)
{
    FILE *fp;
    gchar *buf;
    G_CONST_RETURN gchar *filename;
    GtkTextIter start, end;
    GtkWidget *fileDialog;
    int page;
    tLogFile *file;

    fileDialog = (GtkWidget *)data;
    page = gtk_notebook_get_current_page(GTK_NOTEBOOK(gLogbook));

    /* Obtain the logfile entry in the gLogFiles hash table that is associated
       with this page. */

    file = (tLogFile*)g_hash_table_lookup(gLogFiles, &page);
    filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileDialog));

    gtk_text_buffer_get_bounds(file->buffer, &start, &end);
    buf = gtk_text_buffer_get_text(file->buffer, &start, &end, FALSE);

    if ((fp = open_file_for_buffer(filename, fileDialog)) != NULL) {
        fwrite(buf, sizeof(*buf), strlen(buf), fp);
        fclose(fp);
    } else {
        /* TODO: show a dialog that writing failed. Perhaps test if file
           already exists too. Although currently it is impossible to tell
           the difference between an error and the user pressing cancel
           in the "overwrite y/n" dialog in open_file_for_buffer(). */
        ;
    }
}

static FILE *open_file_for_buffer(G_CONST_RETURN gchar *filename,
                                  GtkWidget *fileDialog)
{
    struct stat buf;
    GtkWidget *dialog, *label;
    gchar *msg;
    gint response;
    FILE *fp = NULL;
    /* Check if the file exists. If it doesn't, then open it in "w" (write)
       mode. If the file exists, open a dialog to ask the user if the file
       needs to be appended to or overwritten. If the user clicks cancel,
       nothing is done. Open with "a" mode if the user wants to append.
    */
    if (stat(filename, &buf) == 0) { /* File exists, ask user what to do */
        dialog = gtk_dialog_new_with_buttons("File exists",
(GtkWindow*)fileDialog,
                    GTK_DIALOG_DESTROY_WITH_PARENT,
                    OVERWRITE_BTN_TXT, RESPONSE_LOGFILE_OVERWRITE,
                    APPEND_BTN_TXT, RESPONSE_LOGFILE_APPEND,
                    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);

        msg = g_new(gchar, strlen(OVERWRITE_QUESTION)+strlen(filename)+1);
        sprintf(msg, OVERWRITE_QUESTION, filename);
        label = gtk_label_new(msg);

        /* Make sure the dialog disappears after the user responds. */
        g_signal_connect_swapped(GTK_OBJECT (dialog),
                             "response", G_CALLBACK (gtk_widget_destroy),
                             GTK_OBJECT (dialog));

        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
        gtk_widget_show(label);
        response = gtk_dialog_run(GTK_DIALOG(dialog));
        g_print("Response: %d\n", response);
        switch (response) {
            case RESPONSE_LOGFILE_APPEND:
                g_print("Going to append\n");
                fp = fopen(filename, "a");
                break;
            case RESPONSE_LOGFILE_OVERWRITE:
                g_print("Going to overwrite\n ");
                fp = fopen(filename, "w");
                break;
            case GTK_RESPONSE_CANCEL: case GTK_RESPONSE_NONE:
                g_print("Doing nothing\n");
                break;
            default:
                g_print("Default action: nothing\n");
                break;
        }
        gtk_widget_destroy(dialog);
    } else { /* File doesn't exist yet. */
        g_print("Creating file\n");
        fp = fopen(filename, "w");
    }
    g_print("returning\n");
    return fp;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]