source: trunk/gtk/msgwin.c @ 5159

Last change on this file since 5159 was 5159, checked in by charles, 14 years ago

(gtk) #742: gtk dialog buttons need to support `alternative button order' for KDE users

  • Property svn:keywords set to Date Rev Author Id
File size: 10.1 KB
Line 
1/******************************************************************************
2 * $Id: msgwin.c 5159 2008-02-28 14:48:23Z charles $
3 *
4 * Copyright (c) 2006-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <errno.h>
26#include <stdio.h>
27#include <string.h>
28
29#include <gtk/gtk.h>
30#include <glib/gi18n.h>
31
32#include <libtransmission/transmission.h>
33
34#include "conf.h"
35#include "hig.h"
36#include "msgwin.h"
37#include "tr-prefs.h"
38#include "util.h"
39
40#define MAX_MSGCOUNT 5000
41
42#define COL_LVL 0
43#define COL_MSG 1
44
45static GtkTextBuffer * textbuf = NULL;
46
47static GtkTextTag*
48get_or_create_tag (GtkTextTagTable * table, const char * key)
49{
50  GtkTextTag * tag;
51
52  g_assert (table);
53  g_assert (key && *key);
54
55  tag = gtk_text_tag_table_lookup (table, key);
56  if (!tag) {
57    tag = gtk_text_tag_new (key);
58    gtk_text_tag_table_add (table, tag);
59    g_object_unref (tag); /* table refs it */
60  }
61
62  return tag;
63}
64
65static GtkTextBuffer*
66debug_window_text_buffer_new ( void )
67{
68  GtkTextBuffer * buffer = gtk_text_buffer_new ( NULL );
69
70  GtkTextTagTable * table = gtk_text_buffer_get_tag_table (buffer);
71
72  g_object_set (get_or_create_tag(table,"bold"),
73      "weight", PANGO_WEIGHT_BOLD,
74      NULL);
75 
76  g_object_set (get_or_create_tag (table, "info"),
77      "foreground", "black",
78      NULL);
79 
80  g_object_set (get_or_create_tag (table, "error"),
81      "foreground", "red",
82      NULL);
83
84  g_object_set (get_or_create_tag (table, "debug"),
85      "foreground", "gray",
86      NULL);
87
88  return buffer;
89}
90
91void
92msgwin_update( void )
93{
94  tr_msg_list * msgs, * ii;
95
96  g_assert( textbuf != NULL );
97
98  msgs = tr_getQueuedMessages();
99  for( ii = msgs; NULL != ii; ii = ii->next )
100  {
101    int len;
102    char * line;
103    const char * tag = NULL;
104    struct tm * tm = localtime( &ii->when );
105    GtkTextIter mark_start, mark_end;
106
107    switch( ii->level ) {
108      case TR_MSG_ERR: tag = "error"; break;
109      case TR_MSG_INF: tag = "info"; break;
110      case TR_MSG_DBG: tag = "debug"; break;
111    }
112
113    line = g_strdup_printf( "%02i:%02i:%02i %s\n", tm->tm_hour, tm->tm_min, tm->tm_sec, ii->message );
114    len = strlen( line );
115
116    gtk_text_buffer_get_end_iter( textbuf, &mark_end );
117    gtk_text_buffer_insert( textbuf, &mark_end, line, len );
118    mark_start = mark_end;
119    gtk_text_iter_backward_chars( &mark_start, len );
120    gtk_text_buffer_apply_tag_by_name (textbuf, tag, &mark_start, &mark_end);
121
122    g_free( line );
123  }
124  tr_freeMessageList( msgs );
125
126#if 0
127  count = gtk_text_buffer_get_line_count( textbuf );
128  if( MAX_MSGCOUNT < count ) {
129    gtk_text_buffer_get_iter_at_line( textbuf, &front, 0 );
130    gtk_text_buffer_get_iter_at_line( textbuf, &iter, count - MAX_MSGCOUNT );
131    gtk_text_buffer_delete( textbuf, &front, &iter );
132  }
133#endif
134}
135
136static void
137level_combo_changed_cb( GtkWidget * w, TrCore * core )
138{
139    GtkTreeIter iter;
140    if( gtk_combo_box_get_active_iter( GTK_COMBO_BOX(w), &iter ) ) {
141        int id = 0;
142        GtkTreeModel * m = gtk_combo_box_get_model( GTK_COMBO_BOX(w) );
143        gtk_tree_model_get( m, &iter, 1, &id, -1 );
144        tr_setMessageLevel( id );
145        tr_core_set_pref_int( core, PREF_KEY_MSGLEVEL, id );
146        msgwin_update( );
147    }
148}
149
150static void
151save_dialog_response_cb( GtkWidget * d, int response, GtkTextBuffer * textbuf )
152{
153  if( response == GTK_RESPONSE_ACCEPT )
154  {
155      char * filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( d ) );
156      FILE * fp = fopen( filename, "w+" );
157      if( !fp )
158      {
159          errmsg( GTK_WINDOW(d),
160                  _("Error writing to \"%s\": %s"),
161                  filename, g_strerror( errno ) );
162      }
163      else
164      {
165          char * buf;
166          GtkTextIter front, back;
167          gtk_text_buffer_get_start_iter( textbuf, &front );
168          gtk_text_buffer_get_end_iter( textbuf, &back );
169          buf = gtk_text_buffer_get_text( textbuf, &front, &back, FALSE );
170          if( buf ) {
171              const size_t len = strlen( buf );
172              if( len > fwrite( buf, 1, len, fp ) ) {
173                  errmsg( GTK_WINDOW( d ),
174                          _("Error writing to \"%s\": %s"),
175                          filename, g_strerror( errno ) );
176              }
177              g_free( buf );
178          }
179          fclose( fp );
180      }
181      g_free( filename );
182  }
183
184  gtk_widget_destroy( d );
185}
186
187static void
188save_cb( GtkWidget * w, GtkTextBuffer * textbuf )
189{
190  GtkWindow * window = GTK_WINDOW( gtk_widget_get_toplevel( w ) );
191  GtkWidget * d = gtk_file_chooser_dialog_new( _("Save Log"), window,
192                                               GTK_FILE_CHOOSER_ACTION_SAVE,
193                                               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
194                                               GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
195                                               NULL );
196  gtk_dialog_set_alternative_button_order( GTK_DIALOG( d ),
197                                           GTK_RESPONSE_ACCEPT,
198                                           GTK_RESPONSE_CANCEL,
199                                           -1 );
200  g_signal_connect( d, "response",
201                    G_CALLBACK( save_dialog_response_cb ), textbuf );
202  gtk_widget_show( d );
203}
204
205static void
206clear_cb( GtkWidget * w UNUSED, GtkTextBuffer * textbuf )
207{
208  GtkTextIter front, back;
209  gtk_text_buffer_get_start_iter( textbuf, &front );
210  gtk_text_buffer_get_end_iter( textbuf, &back );
211  gtk_text_buffer_delete( textbuf, &front, &back );
212}
213
214static struct {
215  const char * label;
216  const char * pref;
217  const char * text;
218  int id;
219} trLevels[] = {
220  { N_("Error"), "error", "ERR", TR_MSG_ERR },
221  { N_("Info"),  "info",  "INF", TR_MSG_INF },
222  { N_("Debug"), "debug", "DBG", TR_MSG_DBG },
223};
224
225GtkWidget *
226msgwin_create( TrCore * core )
227{
228  unsigned int i;
229  GtkListStore * store;
230  GtkWidget * win, * vbox, * scroll, * text;
231  GtkWidget * levels;
232  GtkWidget * toolbar;
233  GtkWidget * w;
234  GtkCellRenderer * renderer;
235  int ii, curlevel;
236
237  win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
238  gtk_window_set_title( GTK_WINDOW( win ), _( "Message Log" ) );
239  gtk_window_set_default_size( GTK_WINDOW( win ), 600, 400 );
240  gtk_window_set_role( GTK_WINDOW( win ), "message-log" );
241  vbox = gtk_vbox_new( FALSE, 0 );
242
243  /**
244  ***  toolbar
245  **/
246
247  toolbar = gtk_toolbar_new ();
248  gtk_toolbar_set_style( GTK_TOOLBAR( toolbar), GTK_TOOLBAR_BOTH_HORIZ );
249
250  gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar), GTK_STOCK_SAVE,
251                           NULL, NULL,
252                           G_CALLBACK(save_cb), textbuf, -1);
253
254  gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar), GTK_STOCK_CLEAR,
255                           NULL, NULL,
256                           G_CALLBACK(clear_cb), textbuf, -1);
257
258  gtk_toolbar_insert_space(GTK_TOOLBAR(toolbar), -1);
259
260
261  gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
262                             GTK_TOOLBAR_CHILD_WIDGET, gtk_label_new(_("Level ")),
263                             NULL, _("Select the debug filter level."),
264                             NULL, NULL, NULL, NULL);
265
266  w = gtk_alignment_new( 0.0f, 0.0f, 0.0f, 0.0f );
267  gtk_widget_set_size_request( w, GUI_PAD_SMALL, GUI_PAD_SMALL );
268  gtk_toolbar_append_element( GTK_TOOLBAR(toolbar),
269                              GTK_TOOLBAR_CHILD_WIDGET, w,
270                              NULL, NULL, NULL, NULL, NULL, NULL);
271
272  store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
273
274  curlevel = pref_int_get( PREF_KEY_MSGLEVEL );
275  for( i=ii=0; i<G_N_ELEMENTS(trLevels); ++i ) {
276      GtkTreeIter iter;
277      gtk_list_store_append (store, &iter);
278      gtk_list_store_set (store, &iter, 0, _(trLevels[i].label),
279                                        1, trLevels[i].id,
280                                       -1);
281      if( trLevels[i].id == curlevel )
282          ii = i;
283  }
284  levels = gtk_combo_box_new_with_model (GTK_TREE_MODEL(store));
285  g_object_unref (G_OBJECT(store));
286  renderer = gtk_cell_renderer_text_new ();
287  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(levels), renderer, TRUE);
288  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(levels), renderer, "text", 0, NULL);
289  gtk_combo_box_set_active( GTK_COMBO_BOX( levels ), ii );
290  g_signal_connect( levels, "changed", G_CALLBACK(level_combo_changed_cb), core );
291
292  gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
293                             GTK_TOOLBAR_CHILD_WIDGET, levels,
294                             NULL, _("Select the debug filter level."),
295                             NULL, NULL, NULL, NULL);
296
297  gtk_box_pack_start( GTK_BOX( vbox ), toolbar, FALSE, FALSE, 0 );
298
299  /**
300  ***  text area
301  **/
302
303  text = gtk_text_view_new_with_buffer( textbuf );
304  gtk_text_view_set_editable( GTK_TEXT_VIEW( text ), FALSE );
305
306  scroll = gtk_scrolled_window_new( NULL, NULL );
307  gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scroll ),
308                                  GTK_POLICY_AUTOMATIC,
309                                  GTK_POLICY_AUTOMATIC );
310  gtk_container_add( GTK_CONTAINER( scroll ), text );
311
312  gtk_box_pack_start( GTK_BOX( vbox ), scroll, TRUE, TRUE, 0 );
313
314  msgwin_update( );
315  gtk_container_add( GTK_CONTAINER( win ), vbox );
316  gtk_widget_show_all( win );
317  return win;
318}
319
320void
321msgwin_loadpref( void )
322{
323    textbuf = debug_window_text_buffer_new ( );
324}
Note: See TracBrowser for help on using the repository browser.