Commit b0791cd2f7a25e7b9997b3ce5058abc336b33103

Authored by perry.werneck@gmail.com
1 parent 62238aa4

Incluindo conversão de charset ao salvar arquivo

Showing 1 changed file with 155 additions and 85 deletions   Show diff stats
src/gtk/dialog.c
@@ -34,11 +34,151 @@ @@ -34,11 +34,151 @@
34 34
35 /*--[ Implement ]------------------------------------------------------------------------------------*/ 35 /*--[ Implement ]------------------------------------------------------------------------------------*/
36 36
37 - static void save_text(GtkWindow *toplevel,const gchar *filename, const gchar *text, const gchar *errmsg) 37 + static void charset_changed(GtkComboBox *widget,gchar **encoding)
38 { 38 {
39 - GError *error = NULL; 39 + gchar *new_encoding = NULL;
  40 +
  41 +#if GTK_CHECK_VERSION(3,0,0)
  42 +
  43 + new_encoding = g_strdup(gtk_combo_box_get_active_id(GTK_COMBO_BOX(widget)));
  44 +
  45 +#else
  46 +
  47 + GValue value = { 0, };
  48 + GtkTreeIter iter;
  49 +
  50 + if(!gtk_combo_box_get_active_iter(widget,&iter))
  51 + return;
  52 +
  53 + gtk_tree_model_get_value(gtk_combo_box_get_model(widget),&iter,1,&value);
  54 + new_encoding = g_strdup(g_value_get_string(&value));
  55 +
  56 +#endif
  57 +
  58 + if(!new_encoding)
  59 + return;
  60 +
  61 + trace("%s: %s->%s",__FUNCTION__,*encoding,new_encoding);
  62 + if(*encoding)
  63 + g_free(*encoding);
  64 +
  65 + *encoding = new_encoding;
  66 + }
  67 +
  68 + static void add_option_menus(GtkWidget *widget, GtkAction *action, gchar **encoding)
  69 + {
  70 + GtkWidget *box = gtk_hbox_new(FALSE,6);
  71 + gchar *ptr = g_object_get_data(G_OBJECT(action),"charset");
  72 +
  73 + if(ptr)
  74 + {
  75 + *encoding = g_strdup(ptr);
  76 + }
  77 + else
  78 + {
  79 + // Add charset options
  80 + static const struct _list
  81 + {
  82 + const gchar *charset;
  83 + const gchar *text;
  84 + } list[] =
  85 + {
  86 + // http://en.wikipedia.org/wiki/Character_encoding
  87 + { "ISO-8859-1", N_( "Western Europe (ISO 8859-1)" ) },
  88 + { "CP1252", N_( "Windows Western languages (CP1252)" ) },
  89 +
  90 + { NULL, NULL }
  91 + };
  92 +
  93 + GtkWidget * label = gtk_label_new_with_mnemonic (_("C_haracter Coding:"));
  94 + const gchar * charset = NULL;
  95 +#if GTK_CHECK_VERSION(3,0,0)
  96 + GtkWidget * menu = gtk_combo_box_text_new();
  97 +#else
  98 + GtkTreeModel * model = (GtkTreeModel *) gtk_list_store_new(2,G_TYPE_STRING,G_TYPE_STRING);
  99 + GtkWidget * menu = gtk_combo_box_new_with_model(model);
  100 + GtkCellRenderer * renderer = gtk_cell_renderer_text_new();
  101 + GtkTreeIter iter;
  102 +#endif // GTK(3,0,0)
  103 + gchar * text;
  104 + int f;
  105 + int p = 0;
  106 +
  107 + g_get_charset(&charset);
  108 + *encoding = g_strdup(charset);
  109 +
  110 + text = g_strdup_printf(_("Current (%s)"),charset);
  111 +
  112 +#if GTK_CHECK_VERSION(3,0,0)
  113 +
  114 + gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(menu),p,charset,text);
  115 +
  116 +#else
  117 +
  118 + gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(menu), renderer, TRUE);
  119 + gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(menu), renderer, "text", 0, NULL);
  120 +
  121 + gtk_list_store_append((GtkListStore *) model,&iter);
  122 + gtk_list_store_set((GtkListStore *) model, &iter, 0, text, 1, charset, -1);
  123 +
  124 +#endif // GTK(3,0,0)
  125 +
  126 + g_free(text);
  127 +
  128 + gtk_combo_box_set_active(GTK_COMBO_BOX(menu),p++);
  129 +
  130 + for(f=0;list[f].charset;f++)
  131 + {
  132 + if(strcasecmp(charset,list[f].charset))
  133 + {
  134 +#if GTK_CHECK_VERSION(3,0,0)
  135 + gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(menu),p++,list[f].charset,gettext(list[f].text));
  136 +#else
  137 + gtk_list_store_append((GtkListStore *) model,&iter);
  138 + gtk_list_store_set((GtkListStore *) model, &iter, 0, gettext(list[f].text), 1, list[f].charset, -1);
  139 +#endif // GTK(3,0,0)
  140 + }
  141 + }
  142 +
  143 +
  144 + gtk_label_set_mnemonic_widget(GTK_LABEL(label), menu);
  145 +
  146 + gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,0);
  147 +
  148 + gtk_box_pack_start(GTK_BOX(box),menu,TRUE,TRUE,0);
  149 +
  150 + g_signal_connect(G_OBJECT(menu),"changed",G_CALLBACK(charset_changed),encoding);
  151 +
  152 + }
  153 +
  154 +
  155 + gtk_widget_show_all(box);
  156 + gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(widget),box);
  157 +
  158 +}
  159 +
  160 + static void save_text(GtkWindow *toplevel,const gchar *filename, const gchar *text, const gchar *encoding, const gchar *errmsg)
  161 + {
  162 + GError * error = NULL;
40 163
41 - if(!g_file_set_contents(filename,text,-1,&error)) 164 + if(g_strcasecmp(encoding,"UTF-8"))
  165 + {
  166 + // Convert to target charset and save
  167 + gsize bytes_written;
  168 + gchar * converted = g_convert_with_fallback(text,-1,encoding,"UTF-8",NULL,NULL,&bytes_written,&error);
  169 +
  170 + if(!error)
  171 + g_file_set_contents(filename,converted,-1,&error);
  172 +
  173 + g_free(converted);
  174 + }
  175 + else
  176 + {
  177 + // Same charset, save file
  178 + g_file_set_contents(filename,text,-1,&error);
  179 + }
  180 +
  181 + if(error)
42 { 182 {
43 GtkWidget *popup = gtk_message_dialog_new_with_markup( 183 GtkWidget *popup = gtk_message_dialog_new_with_markup(
44 toplevel, 184 toplevel,
@@ -60,7 +200,6 @@ @@ -60,7 +200,6 @@
60 200
61 static GtkFileChooserConfirmation confirm_overwrite(GtkFileChooser *chooser, GtkAction *action) 201 static GtkFileChooserConfirmation confirm_overwrite(GtkFileChooser *chooser, GtkAction *action)
62 { 202 {
63 - gchar * filename = gtk_file_chooser_get_filename(chooser);  
64 const gchar * attr = g_object_get_data(G_OBJECT(action),"overwrite"); 203 const gchar * attr = g_object_get_data(G_OBJECT(action),"overwrite");
65 GtkFileChooserConfirmation ret = GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME; 204 GtkFileChooserConfirmation ret = GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
66 GtkWidget * dialog; 205 GtkWidget * dialog;
@@ -90,16 +229,17 @@ @@ -90,16 +229,17 @@
90 const gchar * filename = g_object_get_data(G_OBJECT(action),"filename"); 229 const gchar * filename = g_object_get_data(G_OBJECT(action),"filename");
91 230
92 if(!text) 231 if(!text)
93 - return; 232 + return 0;
94 233
95 if(filename) 234 if(filename)
96 { 235 {
97 - save_text(toplevel,filename,text,errmsg); 236 + save_text(toplevel,filename,text,g_object_get_data(G_OBJECT(action),"encoding"),errmsg);
98 } 237 }
99 else 238 else
100 { 239 {
101 GtkWidget * dialog; 240 GtkWidget * dialog;
102 gchar * ptr; 241 gchar * ptr;
  242 + gchar * encattr = NULL;
103 243
104 dialog = gtk_file_chooser_dialog_new( gettext(user_title ? user_title : title), 244 dialog = gtk_file_chooser_dialog_new( gettext(user_title ? user_title : title),
105 toplevel, 245 toplevel,
@@ -111,6 +251,8 @@ @@ -111,6 +251,8 @@
111 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); 251 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
112 g_signal_connect(GTK_FILE_CHOOSER(dialog), "confirm-overwrite", G_CALLBACK(confirm_overwrite), action); 252 g_signal_connect(GTK_FILE_CHOOSER(dialog), "confirm-overwrite", G_CALLBACK(confirm_overwrite), action);
113 253
  254 + add_option_menus(dialog, action, &encattr);
  255 +
114 ptr = get_string_from_config("save",gtk_action_get_name(action),""); 256 ptr = get_string_from_config("save",gtk_action_get_name(action),"");
115 if(*ptr) 257 if(*ptr)
116 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),ptr); 258 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),ptr);
@@ -123,13 +265,18 @@ @@ -123,13 +265,18 @@
123 { 265 {
124 trace("Saving \"%s\"",ptr); 266 trace("Saving \"%s\"",ptr);
125 set_string_to_config("save",gtk_action_get_name(action),"%s",ptr); 267 set_string_to_config("save",gtk_action_get_name(action),"%s",ptr);
126 - save_text(toplevel,ptr,text,errmsg); 268 + save_text(toplevel,ptr,text,encattr,errmsg);
127 g_free(ptr); 269 g_free(ptr);
128 } 270 }
129 } 271 }
130 272
  273 + if(encattr)
  274 + g_free(encattr);
  275 +
131 gtk_widget_destroy(dialog); 276 gtk_widget_destroy(dialog);
132 } 277 }
  278 +
  279 + return 0;
133 } 280 }
134 281
135 void hostname_action(GtkAction *action, GtkWidget *widget) 282 void hostname_action(GtkAction *action, GtkWidget *widget)
@@ -312,83 +459,6 @@ @@ -312,83 +459,6 @@
312 459
313 } 460 }
314 461
315 - static void add_option_menus(GtkWidget *widget, GtkAction *action, gchar **encoding)  
316 - {  
317 - GtkWidget *box = gtk_hbox_new(FALSE,6);  
318 - gchar *ptr = g_object_get_data(G_OBJECT(action),"encoding");  
319 - int f;  
320 -  
321 - if(ptr)  
322 - {  
323 - *encoding = g_strdup(ptr);  
324 - }  
325 - else  
326 - {  
327 - // Add charset options  
328 - static const struct _list  
329 - {  
330 - const gchar *charset;  
331 - const gchar *text;  
332 - } list[] =  
333 - {  
334 - // http://en.wikipedia.org/wiki/Character_encoding  
335 - { "ISO-8859-1", N_( "Western Europe (ISO 8859-1)" ) },  
336 - { "CP1252", N_( "Windows Western languages (CP1252)" ) },  
337 -  
338 - { NULL, NULL }  
339 - };  
340 -  
341 - GtkWidget * label = gtk_label_new_with_mnemonic (_("C_haracter Coding:"));  
342 - const gchar * charset = NULL;  
343 -#if GTK_CHECK_VERSION(2,24,0)  
344 - GtkWidget * menu = gtk_combo_box_text_new();  
345 -#else  
346 - GtkWidget * menu = gtk_combo_box_new();  
347 -#endif // GTK(2,24)  
348 - gchar * text;  
349 - int f;  
350 - int p = 0;  
351 -  
352 - g_get_charset(&charset);  
353 - *encoding = g_strdup(charset);  
354 -  
355 - text = g_strdup_printf(_("Current (%s)"),charset);  
356 -  
357 -#if GTK_CHECK_VERSION(2,24,0)  
358 - gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(menu),p,charset,text);  
359 -#else  
360 -  
361 -#endif // GTK(2,24)  
362 - g_free(text);  
363 -  
364 - gtk_combo_box_set_active(GTK_COMBO_BOX(menu),p++);  
365 -  
366 - for(f=0;list[f].charset;f++)  
367 - {  
368 - if(strcasecmp(charset,list[f].charset))  
369 - {  
370 -#if GTK_CHECK_VERSION(2,24,0)  
371 - gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(menu),p++,list[f].charset,gettext(list[f].text));  
372 -#else  
373 -  
374 -#endif // GTK(2,24)  
375 - }  
376 - }  
377 -  
378 -  
379 - gtk_label_set_mnemonic_widget(GTK_LABEL(label), menu);  
380 -  
381 - gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,0);  
382 -  
383 - gtk_box_pack_start(GTK_BOX(box),menu,TRUE,TRUE,0);  
384 -  
385 - }  
386 -  
387 - gtk_widget_show_all(box);  
388 - gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(widget),box);  
389 -  
390 -}  
391 -  
392 void paste_file_action(GtkAction *action, GtkWidget *widget) 462 void paste_file_action(GtkAction *action, GtkWidget *widget)
393 { 463 {
394 const gchar * user_title = g_object_get_data(G_OBJECT(action),"title"); 464 const gchar * user_title = g_object_get_data(G_OBJECT(action),"title");
@@ -401,7 +471,7 @@ @@ -401,7 +471,7 @@
401 471
402 if(filename) 472 if(filename)
403 { 473 {
404 - ptr = g_object_get_data(G_OBJECT(action),"encoding"); 474 + ptr = g_object_get_data(G_OBJECT(action),"charset");
405 paste_filename(widget,filename,ptr); 475 paste_filename(widget,filename,ptr);
406 return; 476 return;
407 } 477 }