source: trunk/gtk/msgwin.c @ 815

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

Rework the message stuff again to be easier on the frontend.

  • Property svn:keywords set to Date Rev Author Id
File size: 5.5 KB
Line 
1/******************************************************************************
2 * $Id: msgwin.c 815 2006-08-22 02:12:58Z 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 );
40
41static GtkTextBuffer * textbuf = NULL;
42
43GtkWidget *
44msgwin_create( void ) {
45  GtkWidget * win, * vbox, * scroll, * text;
46  GtkWidget * frame, * bbox, * err, * inf, * dbg;
47
48  if( NULL == textbuf )
49    textbuf = gtk_text_buffer_new( NULL );
50
51  win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
52  vbox = gtk_vbox_new( FALSE, 0 );
53  scroll = gtk_scrolled_window_new( NULL, NULL );
54  text = gtk_text_view_new_with_buffer( textbuf );
55  frame = gtk_frame_new( NULL );
56  bbox = gtk_hbutton_box_new();
57  err = gtk_radio_button_new_with_label( NULL, _( "Error" ) );
58  inf = gtk_radio_button_new_with_label_from_widget(
59    GTK_RADIO_BUTTON( err ), _( "Info" ) );
60  dbg = gtk_radio_button_new_with_label_from_widget(
61    GTK_RADIO_BUTTON( err ), _( "Debug" ) );
62
63  gtk_text_view_set_editable( GTK_TEXT_VIEW( text ), FALSE );
64
65  gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scroll ),
66                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
67
68  gtk_container_add( GTK_CONTAINER( scroll ), text );
69  gtk_container_add( GTK_CONTAINER( frame ), scroll );
70
71  gtk_frame_set_shadow_type( GTK_FRAME( frame ), GTK_SHADOW_IN );
72  gtk_box_pack_start( GTK_BOX( vbox ), frame, TRUE, TRUE, 0 );
73
74  gtk_button_box_set_layout( GTK_BUTTON_BOX( bbox), GTK_BUTTONBOX_SPREAD );
75
76  gtk_container_add( GTK_CONTAINER( bbox ), err );
77  gtk_container_add( GTK_CONTAINER( bbox ), inf );
78  gtk_container_add( GTK_CONTAINER( bbox ), dbg );
79  gtk_box_pack_start( GTK_BOX( vbox ), bbox, FALSE, FALSE, 0 );
80
81  gtk_container_add( GTK_CONTAINER( win ), vbox );
82
83  switch( tr_getMessageLevel() ) {
84    case TR_MSG_ERR:
85      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( err ), TRUE );
86      break;
87    case TR_MSG_INF:
88      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( inf ), TRUE );
89      break;
90    case TR_MSG_DBG:
91      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( dbg ), TRUE );
92      break;
93  }
94
95  g_signal_connect( err, "toggled", G_CALLBACK( changelevel ),
96                    GINT_TO_POINTER( TR_MSG_ERR ) );
97  g_signal_connect( inf, "toggled", G_CALLBACK( changelevel ),
98                    GINT_TO_POINTER( TR_MSG_INF ) );
99  g_signal_connect( dbg, "toggled", G_CALLBACK( changelevel ),
100                    GINT_TO_POINTER( TR_MSG_DBG ) );
101
102  gtk_widget_show_all( win );
103
104  return win;
105}
106
107static void
108changelevel( GtkToggleButton * button, gpointer data ) {
109  int    level;
110  char * ignored;
111
112  if( gtk_toggle_button_get_active( button ) ) {
113    level = GPOINTER_TO_INT( data );
114    tr_setMessageLevel( level );
115    switch( level ) {
116      case TR_MSG_ERR:
117        cf_setpref( PREF_MSGLEVEL, "error" );
118        break;
119      case TR_MSG_INF:
120        cf_setpref( PREF_MSGLEVEL, "info" );
121        break;
122      case TR_MSG_DBG:
123        cf_setpref( PREF_MSGLEVEL, "debug" );
124        break;
125    }
126    cf_saveprefs( &ignored );
127    g_free( ignored );
128    msgwin_update();
129  }
130}
131
132void
133msgwin_loadpref( void ) {
134  const char * pref;
135
136  tr_setMessageQueuing( 1 );
137  pref = cf_getpref( PREF_MSGLEVEL );
138  if( NULL == pref )
139    return;
140
141  if( 0 == strcmp( "error", pref ) )
142    tr_setMessageLevel( TR_MSG_ERR );
143  else if( 0 == strcmp( "info", pref ) )
144    tr_setMessageLevel( TR_MSG_INF );
145  else if( 0 == strcmp( "debug", pref ) )
146    tr_setMessageLevel( TR_MSG_DBG );
147}
148
149void
150msgwin_update( void ) {
151  tr_msg_list_t * msgs, * ii;
152  GtkTextIter     iter;
153  char          * label;
154
155  if( NULL == textbuf )
156    return;
157
158  msgs = tr_getQueuedMessages();
159  for( ii = msgs; NULL != ii; ii = ii->next ) {
160    switch( ii->level )
161    {
162      case TR_MSG_ERR:
163        label = _( "ERR " );
164        break;
165      case TR_MSG_INF:
166        label = _( "INF " );
167        break;
168      case TR_MSG_DBG:
169        label = _( "DBG " );
170        break;
171      default:
172        label = _( "??? " );
173        break;
174    }
175    gtk_text_buffer_get_end_iter( textbuf, &iter );
176    gtk_text_buffer_insert( textbuf, &iter, label, -1 );
177    gtk_text_buffer_insert( textbuf, &iter, ii->message, -1 );
178    gtk_text_buffer_insert( textbuf, &iter, "\n", -1 );
179  }
180  tr_freeMessageList( msgs );
181}
Note: See TracBrowser for help on using the repository browser.