source: trunk/gtk/msgwin.c @ 804

Last change on this file since 804 was 804, checked in by joshe, 15 years ago

Add a debug output window.

  • Property svn:keywords set to Date Rev Author Id
File size: 5.9 KB
Line 
1/******************************************************************************
2 * $Id: msgwin.c 804 2006-08-20 22:45:29Z joshe $
3 *
4 * Copyright (c) 2006 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 <string.h>
26
27#include <gtk/gtk.h>
28#include <glib/gi18n.h>
29
30#include "conf.h"
31#include "msgwin.h"
32#include "transmission.h"
33#include "util.h"
34
35#define COL_LVL 0
36#define COL_MSG 1
37
38static void
39changelevel( GtkToggleButton * button, gpointer data );
40static void
41addmsg( int level, const char * msg );
42
43
44static GMutex * listmutex = NULL;
45static GSList * messages = NULL;
46static GtkTextBuffer * textbuf = NULL;
47
48void
49msgwin_init( void ) {
50  if( !g_thread_supported() )
51    g_thread_init( NULL );
52  listmutex = g_mutex_new();
53  tr_setMessageFunction( addmsg );
54}
55
56GtkWidget *
57msgwin_create( void ) {
58  GtkWidget * win, * vbox, * scroll, * text;
59  GtkWidget * frame, * bbox, * err, * inf, * dbg;
60
61  if( NULL == textbuf )
62    textbuf = gtk_text_buffer_new( NULL );
63
64  win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
65  vbox = gtk_vbox_new( FALSE, 0 );
66  scroll = gtk_scrolled_window_new( NULL, NULL );
67  text = gtk_text_view_new_with_buffer( textbuf );
68  frame = gtk_frame_new( NULL );
69  bbox = gtk_hbutton_box_new();
70  err = gtk_radio_button_new_with_label( NULL, _( "Error" ) );
71  inf = gtk_radio_button_new_with_label_from_widget(
72    GTK_RADIO_BUTTON( err ), _( "Info" ) );
73  dbg = gtk_radio_button_new_with_label_from_widget(
74    GTK_RADIO_BUTTON( err ), _( "Debug" ) );
75
76  gtk_text_view_set_editable( GTK_TEXT_VIEW( text ), FALSE );
77
78  gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scroll ),
79                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
80
81  gtk_container_add( GTK_CONTAINER( scroll ), text );
82  gtk_container_add( GTK_CONTAINER( frame ), scroll );
83
84  gtk_frame_set_shadow_type( GTK_FRAME( frame ), GTK_SHADOW_IN );
85  gtk_box_pack_start( GTK_BOX( vbox ), frame, TRUE, TRUE, 0 );
86
87  gtk_button_box_set_layout( GTK_BUTTON_BOX( bbox), GTK_BUTTONBOX_SPREAD );
88
89  gtk_container_add( GTK_CONTAINER( bbox ), err );
90  gtk_container_add( GTK_CONTAINER( bbox ), inf );
91  gtk_container_add( GTK_CONTAINER( bbox ), dbg );
92  gtk_box_pack_start( GTK_BOX( vbox ), bbox, FALSE, FALSE, 0 );
93
94  gtk_container_add( GTK_CONTAINER( win ), vbox );
95
96  switch( tr_getMessageLevel() ) {
97    case TR_MSG_ERR:
98      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( err ), TRUE );
99      break;
100    case TR_MSG_INF:
101      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( inf ), TRUE );
102      break;
103    case TR_MSG_DBG:
104      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( dbg ), TRUE );
105      break;
106  }
107
108  g_signal_connect( err, "toggled", G_CALLBACK( changelevel ),
109                    GINT_TO_POINTER( TR_MSG_ERR ) );
110  g_signal_connect( inf, "toggled", G_CALLBACK( changelevel ),
111                    GINT_TO_POINTER( TR_MSG_INF ) );
112  g_signal_connect( dbg, "toggled", G_CALLBACK( changelevel ),
113                    GINT_TO_POINTER( TR_MSG_DBG ) );
114
115  gtk_widget_show_all( win );
116
117  return win;
118}
119
120static void
121changelevel( GtkToggleButton * button, gpointer data ) {
122  int    level;
123  char * ignored;
124
125  if( gtk_toggle_button_get_active( button ) ) {
126    level = GPOINTER_TO_INT( data );
127    tr_setMessageLevel( level );
128    switch( level ) {
129      case TR_MSG_ERR:
130        cf_setpref( PREF_MSGLEVEL, "error" );
131        break;
132      case TR_MSG_INF:
133        cf_setpref( PREF_MSGLEVEL, "info" );
134        break;
135      case TR_MSG_DBG:
136        cf_setpref( PREF_MSGLEVEL, "debug" );
137        break;
138    }
139    cf_saveprefs( &ignored );
140    g_free( ignored );
141    msgwin_update();
142  }
143}
144
145void
146msgwin_loadpref( void ) {
147  const char * pref;
148
149  pref = cf_getpref( PREF_MSGLEVEL );
150  if( NULL == pref )
151    return;
152
153  if( 0 == strcmp( "error", pref ) )
154    tr_setMessageLevel( TR_MSG_ERR );
155  else if( 0 == strcmp( "info", pref ) )
156    tr_setMessageLevel( TR_MSG_INF );
157  else if( 0 == strcmp( "debug", pref ) )
158    tr_setMessageLevel( TR_MSG_DBG );
159}
160
161void
162msgwin_update( void ) {
163  GSList    * ii;
164  GtkTextIter iter;
165
166  if( NULL == textbuf )
167    return;
168
169  g_mutex_lock( listmutex );
170
171  if( NULL != messages ) {
172    for( ii = messages; NULL != ii; ii = ii->next ) {
173      gtk_text_buffer_get_end_iter( textbuf, &iter );
174      gtk_text_buffer_insert( textbuf, &iter, ii->data, -1 );
175      g_free( ii->data );
176    }
177    g_slist_free( messages );
178    messages = NULL;
179  }
180
181  g_mutex_unlock( listmutex );
182}
183
184static void
185addmsg( int level, const char * msg ) {
186  char * str;
187
188  g_mutex_lock( listmutex );
189
190  switch( level )
191  {
192    case TR_MSG_ERR:
193      str = _( "ERR" );
194      break;
195    case TR_MSG_INF:
196      str = _( "INF" );
197      break;
198    case TR_MSG_DBG:
199      str = _( "DBG" );
200      break;
201    default:
202      str = _( "???" );
203      break;
204  }
205
206  str = g_strdup_printf( "%s: %s\n", str, msg );
207  messages = g_slist_append( messages, str );
208
209  g_mutex_unlock( listmutex );
210}
Note: See TracBrowser for help on using the repository browser.