source: trunk/gtk/msgwin.c @ 1587

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

Set roles for non-dialog windows to help WMs out a bit.

  • Property svn:keywords set to Date Rev Author Id
File size: 7.9 KB
Line 
1/******************************************************************************
2 * $Id: msgwin.c 1587 2007-03-24 10:20:00Z joshe $
3 *
4 * Copyright (c) 2006-2007 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 <string.h>
27
28#include <gtk/gtk.h>
29#include <glib/gi18n.h>
30
31#include "conf.h"
32#include "msgwin.h"
33#include "tr_prefs.h"
34#include "transmission.h"
35#include "util.h"
36
37#define MAX_MSGCOUNT 5000
38
39#define COL_LVL 0
40#define COL_MSG 1
41
42static void
43changelevel( GtkWidget * widget, gpointer data );
44static void
45asksave( GtkWidget * widget, gpointer data );
46static void
47dosave( GtkWidget * widget, gint resp, gpointer gdata );
48static void
49doclear( GtkWidget * widget, gpointer data );
50
51static GtkTextBuffer * textbuf = NULL;
52
53static struct { char * label; char * pref; char * text; int id; } levels[] = {
54  { N_("Error"), "error", "ERR", TR_MSG_ERR },
55  { N_("Info"),  "info",  "INF", TR_MSG_INF },
56  { N_("Debug"), "debug", "DBG", TR_MSG_DBG },
57};
58
59GtkWidget *
60msgwin_create( void ) {
61  GtkWidget * win, * vbox, * scroll, * text;
62  GtkWidget * frame, * bbox, * save, * clear, * menu;
63  PangoFontDescription * desc;
64  int ii, curlevel;
65
66  if( NULL == textbuf )
67    textbuf = gtk_text_buffer_new( NULL );
68
69  win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
70  vbox = gtk_vbox_new( FALSE, 0 );
71  scroll = gtk_scrolled_window_new( NULL, NULL );
72  text = gtk_text_view_new_with_buffer( textbuf );
73  frame = gtk_frame_new( NULL );
74  bbox = gtk_hbutton_box_new();
75  save = gtk_button_new_from_stock( GTK_STOCK_SAVE );
76  clear = gtk_button_new_from_stock( GTK_STOCK_CLEAR );
77  menu = gtk_combo_box_new_text();
78
79  gtk_text_view_set_editable( GTK_TEXT_VIEW( text ), FALSE );
80  desc = pango_font_description_new();
81  pango_font_description_set_family( desc, "Monospace" );
82  gtk_widget_modify_font( text, desc );
83  pango_font_description_free( desc );
84
85  gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scroll ),
86                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
87
88  gtk_container_add( GTK_CONTAINER( scroll ), text );
89  gtk_container_add( GTK_CONTAINER( frame ), scroll );
90
91  gtk_frame_set_shadow_type( GTK_FRAME( frame ), GTK_SHADOW_IN );
92  gtk_box_pack_start( GTK_BOX( vbox ), frame, TRUE, TRUE, 0 );
93
94  gtk_button_box_set_layout( GTK_BUTTON_BOX( bbox), GTK_BUTTONBOX_SPREAD );
95
96  curlevel = tr_getMessageLevel();
97  for( ii = 0; ALEN( levels ) > ii; ii++ ) {
98    gtk_combo_box_append_text( GTK_COMBO_BOX( menu ),
99                               gettext( levels[ii].label ) );
100    if( levels[ii].id == curlevel )
101      gtk_combo_box_set_active( GTK_COMBO_BOX( menu ), ii );
102  }
103
104  gtk_container_add( GTK_CONTAINER( bbox ), clear );
105  gtk_container_add( GTK_CONTAINER( bbox ), save );
106  gtk_container_add( GTK_CONTAINER( bbox ), menu );
107  gtk_box_pack_start( GTK_BOX( vbox ), bbox, FALSE, FALSE, 0 );
108
109  gtk_container_add( GTK_CONTAINER( win ), vbox );
110
111  g_signal_connect( save, "clicked", G_CALLBACK( asksave ), win );
112  g_signal_connect( clear, "clicked", G_CALLBACK( doclear ), NULL );
113  g_signal_connect( menu, "changed", G_CALLBACK( changelevel ), NULL );
114
115  gtk_window_set_role( GTK_WINDOW( win ), "tr-messages" );
116
117  gtk_widget_show_all( win );
118
119  return win;
120}
121
122static void
123changelevel( GtkWidget * widget, gpointer data SHUTUP ) {
124  int    index;
125  char * ignored;
126
127  index = gtk_combo_box_get_active( GTK_COMBO_BOX( widget ) );
128  if( 0 <= index && (int) ALEN( levels ) > index &&
129      tr_getMessageLevel() != levels[index].id ) {
130    tr_setMessageLevel( levels[index].id );
131    cf_setpref( tr_prefs_name( PREF_ID_MSGLEVEL ), levels[index].pref );
132    cf_saveprefs( &ignored );
133    g_free( ignored );
134    msgwin_update();
135  }
136}
137
138static void
139asksave( GtkWidget * widget SHUTUP, gpointer data ) {
140  GtkWidget * wind;
141
142  wind = gtk_file_chooser_dialog_new( _("Save Log"), GTK_WINDOW( data ),
143                                      GTK_FILE_CHOOSER_ACTION_SAVE,
144                                      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
145                                      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
146                                      NULL );
147  g_signal_connect( G_OBJECT( wind ), "response", G_CALLBACK( dosave ), NULL );
148  gtk_widget_show( wind );
149}
150
151static void
152dosave( GtkWidget * widget, gint resp, gpointer gdata SHUTUP ) {
153  char      * path, * buf;
154  FILE      * fptr;
155  GtkTextIter front, back;
156  size_t      len;
157
158  if( GTK_RESPONSE_ACCEPT == resp ) {
159    path = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( widget ) );
160    if( NULL != path ) {
161      fptr = fopen( path, "w" );
162      if( NULL == fptr ) {
163        errmsg( GTK_WINDOW( widget ),
164                _("Failed to open the file %s for writing:\n%s"),
165                path, strerror( errno ) );
166      }
167      else {
168        gtk_text_buffer_get_start_iter( textbuf, &front );
169        gtk_text_buffer_get_end_iter( textbuf, &back );
170        buf = gtk_text_buffer_get_text( textbuf, &front, &back, FALSE );
171        if( NULL != buf ) {
172          len = strlen( buf );
173          if( len > fwrite( buf, 1, len, fptr ) ) {
174            errmsg( GTK_WINDOW( widget ),
175                    _("Error while writing to the file %s:\n%s"),
176                    path, strerror( errno ) );
177          }
178          g_free( buf );
179        }
180        fclose( fptr );
181      }
182    }
183    g_free( path );
184  }
185
186  gtk_widget_destroy( widget );
187}
188
189static void
190doclear( GtkWidget * widget SHUTUP, gpointer data SHUTUP ) {
191  GtkTextIter front, back;
192
193  gtk_text_buffer_get_start_iter( textbuf, &front );
194  gtk_text_buffer_get_end_iter( textbuf, &back );
195  gtk_text_buffer_delete( textbuf, &front, &back );
196}
197
198void
199msgwin_loadpref( void ) {
200  const char * pref;
201  int ii;
202
203  tr_setMessageQueuing( 1 );
204  pref = tr_prefs_get( PREF_ID_MSGLEVEL );
205  if( NULL == pref )
206    return;
207
208  for( ii = 0; ALEN( levels ) > ii; ii++ ) {
209    if( 0 == strcmp( pref, levels[ii].pref ) ) {
210      tr_setMessageLevel( levels[ii].id );
211      break;
212    }
213  }
214}
215
216void
217msgwin_update( void ) {
218  tr_msg_list_t * msgs, * ii;
219  GtkTextIter     iter, front;
220  char          * label, * line;
221  int             count, jj;
222  struct tm     * tm;
223
224  if( NULL == textbuf )
225    return;
226
227  msgs = tr_getQueuedMessages();
228  for( ii = msgs; NULL != ii; ii = ii->next ) {
229    label = _("???");
230    for( jj = 0; ALEN( levels ) > jj; jj++ ) {
231      if( levels[jj].id == ii->level ) {
232        label = levels[jj].text;
233        break;
234      }
235    }
236    tm = localtime( &ii->when );
237    line = g_strdup_printf( "%02i:%02i:%02i %s %s\n", tm->tm_hour, tm->tm_min,
238                            tm->tm_sec, label, ii->message );
239    gtk_text_buffer_get_end_iter( textbuf, &iter );
240    gtk_text_buffer_insert( textbuf, &iter, line, -1 );
241    g_free( line );
242  }
243  tr_freeMessageList( msgs );
244
245  count = gtk_text_buffer_get_line_count( textbuf );
246  if( MAX_MSGCOUNT < count ) {
247    gtk_text_buffer_get_iter_at_line( textbuf, &front, 0 );
248    gtk_text_buffer_get_iter_at_line( textbuf, &iter, count - MAX_MSGCOUNT );
249    gtk_text_buffer_delete( textbuf, &front, &iter );
250  }
251}
Note: See TracBrowser for help on using the repository browser.