2006-08-20 22:45:29 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-02-19 22:09:05 +00:00
|
|
|
* Copyright (c) 2006-2007 Transmission authors and contributors
|
2006-08-20 22:45:29 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2006-09-18 01:26:49 +00:00
|
|
|
#include <errno.h>
|
2007-07-19 03:55:00 +00:00
|
|
|
#include <stdio.h>
|
2006-08-20 22:45:29 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
|
2006-08-20 22:45:29 +00:00
|
|
|
#include "conf.h"
|
2007-12-27 16:21:14 +00:00
|
|
|
#include "hig.h"
|
2006-08-20 22:45:29 +00:00
|
|
|
#include "msgwin.h"
|
2007-02-19 22:09:05 +00:00
|
|
|
#include "tr_prefs.h"
|
2006-08-20 22:45:29 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2006-09-18 01:26:49 +00:00
|
|
|
#define MAX_MSGCOUNT 5000
|
|
|
|
|
2006-08-20 22:45:29 +00:00
|
|
|
#define COL_LVL 0
|
|
|
|
#define COL_MSG 1
|
|
|
|
|
|
|
|
static GtkTextBuffer * textbuf = NULL;
|
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
static GtkTextTag*
|
|
|
|
get_or_create_tag (GtkTextTagTable * table, const char * key)
|
|
|
|
{
|
2007-07-06 16:25:24 +00:00
|
|
|
GtkTextTag * tag;
|
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
g_assert (table);
|
|
|
|
g_assert (key && *key);
|
|
|
|
|
2007-07-06 16:25:24 +00:00
|
|
|
tag = gtk_text_tag_table_lookup (table, key);
|
2007-06-22 20:59:23 +00:00
|
|
|
if (!tag) {
|
|
|
|
tag = gtk_text_tag_new (key);
|
|
|
|
gtk_text_tag_table_add (table, tag);
|
2007-06-23 00:00:19 +00:00
|
|
|
g_object_unref (tag); /* table refs it */
|
2007-06-22 20:59:23 +00:00
|
|
|
}
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
return tag;
|
|
|
|
}
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
static GtkTextBuffer*
|
|
|
|
debug_window_text_buffer_new ( void )
|
|
|
|
{
|
|
|
|
GtkTextBuffer * buffer = gtk_text_buffer_new ( NULL );
|
|
|
|
|
|
|
|
GtkTextTagTable * table = gtk_text_buffer_get_tag_table (buffer);
|
|
|
|
|
|
|
|
g_object_set (get_or_create_tag(table,"bold"),
|
|
|
|
"weight", PANGO_WEIGHT_BOLD,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_object_set (get_or_create_tag (table, "info"),
|
|
|
|
"foreground", "black",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_object_set (get_or_create_tag (table, "error"),
|
|
|
|
"foreground", "red",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_object_set (get_or_create_tag (table, "debug"),
|
|
|
|
"foreground", "gray",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
void
|
|
|
|
msgwin_update( void )
|
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_msg_list * msgs, * ii;
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
g_assert( textbuf != NULL );
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
msgs = tr_getQueuedMessages();
|
|
|
|
for( ii = msgs; NULL != ii; ii = ii->next )
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char * line;
|
|
|
|
const char * tag = NULL;
|
|
|
|
struct tm * tm = localtime( &ii->when );
|
|
|
|
GtkTextIter mark_start, mark_end;
|
|
|
|
|
|
|
|
switch( ii->level ) {
|
|
|
|
case TR_MSG_ERR: tag = "error"; break;
|
|
|
|
case TR_MSG_INF: tag = "info"; break;
|
|
|
|
case TR_MSG_DBG: tag = "debug"; break;
|
|
|
|
}
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
line = g_strdup_printf( "%02i:%02i:%02i %s\n", tm->tm_hour, tm->tm_min, tm->tm_sec, ii->message );
|
|
|
|
len = strlen( line );
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
gtk_text_buffer_get_end_iter( textbuf, &mark_end );
|
|
|
|
gtk_text_buffer_insert( textbuf, &mark_end, line, len );
|
|
|
|
mark_start = mark_end;
|
|
|
|
gtk_text_iter_backward_chars( &mark_start, len );
|
|
|
|
gtk_text_buffer_apply_tag_by_name (textbuf, tag, &mark_start, &mark_end);
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
g_free( line );
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
2007-06-22 20:59:23 +00:00
|
|
|
tr_freeMessageList( msgs );
|
2006-09-18 01:26:49 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
#if 0
|
|
|
|
count = gtk_text_buffer_get_line_count( textbuf );
|
|
|
|
if( MAX_MSGCOUNT < count ) {
|
|
|
|
gtk_text_buffer_get_iter_at_line( textbuf, &front, 0 );
|
|
|
|
gtk_text_buffer_get_iter_at_line( textbuf, &iter, count - MAX_MSGCOUNT );
|
|
|
|
gtk_text_buffer_delete( textbuf, &front, &iter );
|
2006-08-20 22:45:29 +00:00
|
|
|
}
|
2007-06-22 20:59:23 +00:00
|
|
|
#endif
|
2006-08-20 22:45:29 +00:00
|
|
|
}
|
|
|
|
|
2006-09-18 01:26:49 +00:00
|
|
|
static void
|
2007-06-22 20:59:23 +00:00
|
|
|
level_combo_changed_cb( GtkWidget * w, TrCore * core )
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
if( gtk_combo_box_get_active_iter( GTK_COMBO_BOX(w), &iter ) ) {
|
|
|
|
int id = 0;
|
|
|
|
GtkTreeModel * m = gtk_combo_box_get_model( GTK_COMBO_BOX(w) );
|
|
|
|
gtk_tree_model_get( m, &iter, 1, &id, -1 );
|
|
|
|
tr_setMessageLevel( id );
|
2007-09-27 20:57:58 +00:00
|
|
|
tr_core_set_pref_int( core, PREF_KEY_MSGLEVEL, id );
|
2007-06-22 20:59:23 +00:00
|
|
|
msgwin_update( );
|
|
|
|
}
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-06-22 20:59:23 +00:00
|
|
|
save_dialog_response_cb( GtkWidget * d, int response, GtkTextBuffer * textbuf )
|
|
|
|
{
|
|
|
|
if( response == GTK_RESPONSE_ACCEPT )
|
|
|
|
{
|
|
|
|
char * filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( d ) );
|
|
|
|
FILE * fp = fopen( filename, "w+" );
|
|
|
|
if( !fp )
|
|
|
|
{
|
|
|
|
errmsg( GTK_WINDOW(d),
|
2007-10-13 17:33:53 +00:00
|
|
|
_("Error writing to \"%s\": %s"),
|
2007-06-22 20:59:23 +00:00
|
|
|
filename, g_strerror( errno ) );
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
2007-06-22 20:59:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char * buf;
|
|
|
|
GtkTextIter front, back;
|
|
|
|
gtk_text_buffer_get_start_iter( textbuf, &front );
|
|
|
|
gtk_text_buffer_get_end_iter( textbuf, &back );
|
|
|
|
buf = gtk_text_buffer_get_text( textbuf, &front, &back, FALSE );
|
|
|
|
if( buf ) {
|
|
|
|
const size_t len = strlen( buf );
|
|
|
|
if( len > fwrite( buf, 1, len, fp ) ) {
|
|
|
|
errmsg( GTK_WINDOW( d ),
|
|
|
|
_("Error writing to \"%s\": %s"),
|
2007-10-13 17:33:53 +00:00
|
|
|
filename, g_strerror( errno ) );
|
2007-06-22 20:59:23 +00:00
|
|
|
}
|
|
|
|
g_free( buf );
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
2007-06-22 20:59:23 +00:00
|
|
|
fclose( fp );
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
2007-06-22 20:59:23 +00:00
|
|
|
g_free( filename );
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
gtk_widget_destroy( d );
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-06-22 20:59:23 +00:00
|
|
|
save_cb( GtkWidget * w, GtkTextBuffer * textbuf )
|
|
|
|
{
|
|
|
|
GtkWindow * window = GTK_WINDOW( gtk_widget_get_toplevel( w ) );
|
2007-11-18 02:32:46 +00:00
|
|
|
GtkWidget * d = gtk_file_chooser_dialog_new( _("Save Log"), window,
|
2007-06-22 20:59:23 +00:00
|
|
|
GTK_FILE_CHOOSER_ACTION_SAVE,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL );
|
|
|
|
g_signal_connect( d, "response",
|
|
|
|
G_CALLBACK( save_dialog_response_cb ), textbuf );
|
|
|
|
gtk_widget_show( d );
|
|
|
|
}
|
2006-09-18 01:26:49 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
static void
|
|
|
|
clear_cb( GtkWidget * w UNUSED, GtkTextBuffer * textbuf )
|
|
|
|
{
|
|
|
|
GtkTextIter front, back;
|
2006-09-18 01:26:49 +00:00
|
|
|
gtk_text_buffer_get_start_iter( textbuf, &front );
|
|
|
|
gtk_text_buffer_get_end_iter( textbuf, &back );
|
|
|
|
gtk_text_buffer_delete( textbuf, &front, &back );
|
|
|
|
}
|
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
static struct {
|
|
|
|
const char * label;
|
2006-08-20 22:45:29 +00:00
|
|
|
const char * pref;
|
2007-06-22 20:59:23 +00:00
|
|
|
const char * text;
|
|
|
|
int id;
|
|
|
|
} trLevels[] = {
|
|
|
|
{ N_("Error"), "error", "ERR", TR_MSG_ERR },
|
|
|
|
{ N_("Info"), "info", "INF", TR_MSG_INF },
|
|
|
|
{ N_("Debug"), "debug", "DBG", TR_MSG_DBG },
|
|
|
|
};
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
GtkWidget *
|
|
|
|
msgwin_create( TrCore * core )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
GtkListStore * store;
|
|
|
|
GtkWidget * win, * vbox, * scroll, * text;
|
|
|
|
GtkWidget * levels;
|
|
|
|
GtkWidget * toolbar;
|
2007-12-27 16:21:14 +00:00
|
|
|
GtkWidget * w;
|
2007-06-22 20:59:23 +00:00
|
|
|
GtkCellRenderer * renderer;
|
|
|
|
int ii, curlevel;
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
|
2007-11-18 02:32:46 +00:00
|
|
|
gtk_window_set_title( GTK_WINDOW( win ), _( "Message Log" ) );
|
2007-06-27 18:34:38 +00:00
|
|
|
gtk_window_set_default_size( GTK_WINDOW( win ), 600, 400 );
|
2007-11-18 02:32:46 +00:00
|
|
|
gtk_window_set_role( GTK_WINDOW( win ), "message-log" );
|
2007-06-22 20:59:23 +00:00
|
|
|
vbox = gtk_vbox_new( FALSE, 0 );
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
/**
|
|
|
|
*** toolbar
|
|
|
|
**/
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
toolbar = gtk_toolbar_new ();
|
|
|
|
gtk_toolbar_set_style( GTK_TOOLBAR( toolbar), GTK_TOOLBAR_BOTH_HORIZ );
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar), GTK_STOCK_SAVE,
|
2007-10-13 17:33:53 +00:00
|
|
|
NULL, NULL,
|
2007-06-22 20:59:23 +00:00
|
|
|
G_CALLBACK(save_cb), textbuf, -1);
|
2006-09-18 01:26:49 +00:00
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar), GTK_STOCK_CLEAR,
|
2007-10-13 17:33:53 +00:00
|
|
|
NULL, NULL,
|
2007-06-22 20:59:23 +00:00
|
|
|
G_CALLBACK(clear_cb), textbuf, -1);
|
|
|
|
|
|
|
|
gtk_toolbar_insert_space(GTK_TOOLBAR(toolbar), -1);
|
|
|
|
|
|
|
|
|
|
|
|
gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
|
|
|
|
GTK_TOOLBAR_CHILD_WIDGET, gtk_label_new(_("Level ")),
|
|
|
|
NULL, _("Select the debug filter level."),
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
|
2007-12-27 16:21:14 +00:00
|
|
|
w = gtk_alignment_new( 0.0f, 0.0f, 0.0f, 0.0f );
|
|
|
|
gtk_widget_set_usize( w, GUI_PAD_SMALL, GUI_PAD_SMALL );
|
|
|
|
gtk_toolbar_append_element( GTK_TOOLBAR(toolbar),
|
|
|
|
GTK_TOOLBAR_CHILD_WIDGET, w,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
2007-06-22 20:59:23 +00:00
|
|
|
store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
curlevel = pref_int_get( PREF_KEY_MSGLEVEL );
|
2007-06-22 20:59:23 +00:00
|
|
|
for( i=ii=0; i<G_N_ELEMENTS(trLevels); ++i ) {
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, _(trLevels[i].label),
|
|
|
|
1, trLevels[i].id,
|
|
|
|
-1);
|
|
|
|
if( trLevels[i].id == curlevel )
|
|
|
|
ii = i;
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
2007-06-22 20:59:23 +00:00
|
|
|
levels = gtk_combo_box_new_with_model (GTK_TREE_MODEL(store));
|
|
|
|
g_object_unref (G_OBJECT(store));
|
|
|
|
renderer = gtk_cell_renderer_text_new ();
|
|
|
|
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(levels), renderer, TRUE);
|
|
|
|
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(levels), renderer, "text", 0, NULL);
|
|
|
|
gtk_combo_box_set_active( GTK_COMBO_BOX( levels ), ii );
|
|
|
|
g_signal_connect( levels, "changed", G_CALLBACK(level_combo_changed_cb), core );
|
|
|
|
|
|
|
|
gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
|
|
|
|
GTK_TOOLBAR_CHILD_WIDGET, levels,
|
|
|
|
NULL, _("Select the debug filter level."),
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
gtk_box_pack_start( GTK_BOX( vbox ), toolbar, FALSE, FALSE, 0 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
*** text area
|
|
|
|
**/
|
|
|
|
|
|
|
|
text = gtk_text_view_new_with_buffer( textbuf );
|
|
|
|
gtk_text_view_set_editable( GTK_TEXT_VIEW( text ), FALSE );
|
|
|
|
|
|
|
|
scroll = gtk_scrolled_window_new( NULL, NULL );
|
|
|
|
gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scroll ),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC );
|
|
|
|
gtk_container_add( GTK_CONTAINER( scroll ), text );
|
|
|
|
|
|
|
|
gtk_box_pack_start( GTK_BOX( vbox ), scroll, TRUE, TRUE, 0 );
|
|
|
|
|
|
|
|
msgwin_update( );
|
|
|
|
gtk_container_add( GTK_CONTAINER( win ), vbox );
|
|
|
|
gtk_widget_show_all( win );
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
msgwin_loadpref( void )
|
|
|
|
{
|
|
|
|
textbuf = debug_window_text_buffer_new ( );
|
2006-08-20 22:45:29 +00:00
|
|
|
}
|