Changeset 3313 for trunk/jons-gtk2-gui

Show
Ignore:
Timestamp:
02/26/03 11:14:31 (6 years ago)
Author:
bostjanlah
Message:

Make sure all messages inserted into log window are first converted to UTF-8
Make saving of log to file work

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/jons-gtk2-gui/src/log_window.cpp

    r3164 r3313  
    2020 
    2121#include "licq_gtk.h" 
     22#include "utilities.h" 
    2223 
    2324#include "licq_icqd.h" 
     
    2627 
    2728#include <gtk/gtk.h> 
     29#include <iostream> 
    2830 
    2931using namespace std; // for ofstream 
    3032 
    31 struct network_window *nw; 
     33struct network_window *nw = NULL; 
    3234gboolean nw_shown = FALSE; 
    3335gboolean hidden = FALSE; 
    3436 
    35 void new_log_window() 
    36 { 
    37     if(nw_shown) 
    38     { 
     37void  
     38new_log_window() 
     39{ 
     40    if (nw_shown) 
    3941        return; 
    40     } 
    41  
    42     if(hidden) 
    43     { 
     42 
     43    if (hidden) { 
    4444        gtk_widget_show_all(nw->window); 
    4545        hidden = FALSE; 
     
    8484 
    8585    gtk_table_attach(GTK_TABLE(table), box_text, 0, 2, 0, 2, 
    86              GtkAttachOptions(GTK_FILL | GTK_EXPAND), 
    87              GtkAttachOptions(GTK_FILL | GTK_EXPAND), 
    88                          3, 3); 
     86            GtkAttachOptions(GTK_FILL | GTK_EXPAND), 
     87            GtkAttachOptions(GTK_FILL | GTK_EXPAND), 
     88      3, 3); 
    8989 
    9090    /* The "OK" button */ 
     
    114114} 
    115115 
    116 void log_window_show(GtkWidget *widget, gpointer data) 
    117 { 
    118     if(nw == 0) 
    119     { 
     116void  
     117log_window_show(GtkWidget *widget, gpointer data) 
     118{ 
     119    if (nw == NULL) { 
    120120        new_log_window(); 
    121121        log_window_show(0, 0); 
    122122    } 
    123     else if(!nw_shown) 
    124     { 
     123    else if (!nw_shown) { 
    125124        gtk_widget_show_all(nw->window); 
    126125        GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nw->text)); 
     
    132131        hidden = FALSE; 
    133132    } 
    134  
    135 } 
    136  
    137 void log_pipe_callback(gpointer data, gint pipe, GdkInputCondition condition) 
    138 { 
    139     /* If the window doesn't exist, wait for it to exist */ 
    140     if(nw == 0) 
     133} 
     134 
     135void  
     136log_pipe_callback(gpointer data, gint pipe, GdkInputCondition condition) 
     137{ 
     138    /* If the window doesn't exist, create it */ 
     139    if (nw == NULL) 
    141140        new_log_window(); 
    142141 
    143142    gchar buf[4]; 
    144     gchar *for_user; /* The text for the window */ 
    145  
    146143    read(pipe, buf, 1); 
    147144 
    148145    /* Get the message */ 
    149     for_user = logg->NextLogMsg(); 
    150  
    151     /* Insert the message */ 
    152     GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nw->text)); 
    153     GtkTextIter iter; 
    154     gtk_text_buffer_get_end_iter(tb, &iter); 
    155     gtk_text_buffer_insert(tb, &iter, for_user, -1); 
    156  
    157     /* Scroll down to the new bottom if not hidden */ 
    158     if (hidden == FALSE) 
    159     { 
     146    gchar *for_user = convert_to_utf8(logg->NextLogMsg()); 
     147    if (for_user != NULL) { 
    160148        GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nw->text)); 
    161         GtkTextIter end; 
     149        GtkTextIter iter, end; 
     150 
     151    // when we're over 510 lines, we'll cut off the first 500 
    162152        gtk_text_buffer_get_end_iter(tb, &end); 
    163         GtkTextMark *tm = gtk_text_buffer_create_mark(tb, NULL, &end, TRUE); 
    164         gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(nw->text), tm, 0, FALSE, 0, 0); 
    165     } 
    166  
     153    if (gtk_text_iter_get_line(&end) > 510) { 
     154      gtk_text_buffer_get_start_iter(tb, &iter); 
     155        gtk_text_buffer_get_iter_at_line(tb, &end, 500); 
     156      gtk_text_buffer_delete(tb, &iter, &end); 
     157    } 
     158 
     159        /* Insert the message */ 
     160        gtk_text_buffer_get_end_iter(tb, &iter); 
     161        gtk_text_buffer_insert(tb, &iter, for_user, -1); 
     162 
     163        /* Scroll down to the new bottom if not hidden */ 
     164        if (!hidden) { 
     165            gtk_text_buffer_get_end_iter(tb, &end); 
     166            GtkTextMark *tm = gtk_text_buffer_create_mark(tb, NULL, &end, TRUE); 
     167            gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(nw->text), tm, 0, FALSE, 0, 0); 
     168        } 
     169         
     170    g_free(for_user); 
     171    } 
     172     
    167173    /* Get rid of this message */ 
    168174    logg->ClearLog(); 
     
    189195    GtkWidget *fileSelect = (GtkWidget *)_fs; 
    190196     
    191     const gchar *szFileName = gtk_file_selection_get_filename(GTK_FILE_SELECTION( 
    192         fileSelect)); 
    193  
    194     ofstream strmFileOut(szFileName, ios::out); 
    195  
    196     if (!strmFileOut.fail()) 
    197     { 
    198         strmFileOut << gtk_editable_get_chars(GTK_EDITABLE(nw->text), 0, -1); 
    199     } 
     197    const gchar *filename =  
     198      gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileSelect)); 
     199 
     200    ofstream strmFileOut(filename, ios::out); 
     201    if (strmFileOut) { 
     202        GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nw->text)); 
     203    GtkTextIter start, end; 
     204    gtk_text_buffer_get_start_iter(tb, &start); 
     205    gtk_text_buffer_get_end_iter(tb, &end); 
     206        strmFileOut << gtk_text_buffer_get_text(tb, &start, &end, FALSE); 
     207  } 
     208  else { 
     209    GtkWidget *dialog =  
     210        gtk_message_dialog_new(GTK_WINDOW(nw->window),  
     211                               GTK_DIALOG_DESTROY_WITH_PARENT, 
     212                               GTK_MESSAGE_ERROR,  
     213                               GTK_BUTTONS_CLOSE,  
     214                               "Couldn't save log to file: %s",  
     215                               filename); 
     216    gtk_dialog_run(GTK_DIALOG(dialog)); 
     217    gtk_widget_destroy(dialog); 
     218  } 
    200219 
    201220    strmFileOut.close();