aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-non-intrusive-error-dialog.c
blob: b2a841756782924bacad9309594deaf1af2696f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 *  Authors: Ashish Shrivastava <shashish@novell.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib/gstdio.h>
#include "e-non-intrusive-error-dialog.h"

/* eni - non intrusive error */

static gboolean
eni_query_tooltip_cb (GtkTreeView *view,
                  gint x,
                  gint y,
                  gboolean keyboard_mode,
                  GtkTooltip *tooltip)
{
        GtkTreeViewColumn *column;
        GtkTreeModel *model;
        GtkTreePath *path;
        GtkTreeIter iter;
        gint level;

        if (!gtk_tree_view_get_tooltip_context (
                view, &x, &y, keyboard_mode, NULL, &path, &iter))
                return FALSE;

        /* Figure out which column we're pointing at. */
        if (keyboard_mode)
                gtk_tree_view_get_cursor (view, NULL, &column);
        else
                gtk_tree_view_get_path_at_pos (
                        view, x, y, NULL, &column, NULL, NULL);

        /* Restrict the tip area to a single cell. */
        gtk_tree_view_set_tooltip_cell (view, tooltip, path, column, NULL);

        /* This only works if the tree view is NOT reorderable. */
        if (column != gtk_tree_view_get_column (view, 0))
                return FALSE;

        model = gtk_tree_view_get_model (view);
        gtk_tree_model_get (model, &iter, COL_LEVEL, &level, -1);
        gtk_tooltip_set_text (tooltip, ldata[level].key);

        return TRUE;
}

static void
eni_render_pixbuf (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
               GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
{
        gint level;

        gtk_tree_model_get (model, iter, COL_LEVEL, &level, -1);
        g_object_set (
                renderer, "stock-id", ldata[level].stock_id,
                "stock-size", GTK_ICON_SIZE_MENU, NULL);
}

static void
eni_render_date (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
              GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
{
        time_t t;
        gchar sdt[100]; /* Should be sufficient? */

        gtk_tree_model_get (model, iter, COL_TIME, &t, -1);
        strftime (sdt, 100, "%x %X", localtime (&t));
        g_object_set (renderer, "text", sdt, NULL);
}

static void
eni_append_logs (const gchar *txt, GtkListStore *store)
{
        gchar **str;

        str = g_strsplit (txt,  ":", 3);
        if (str[0] && str[1] && str[2]) {
                GtkTreeIter iter;

                gtk_list_store_append (store, &iter);
                gtk_list_store_set (
                        store, &iter,
                        COL_LEVEL, atoi (str[0]),
                        COL_TIME, atol (str[1]),
                        COL_DATA, g_strstrip (str[2]),
                        -1);
        } else
                g_printerr ("Unable to decode error log: %s\n", txt);

        g_strfreev (str);
}

static guint
eni_config_get_error_level (const gchar *path)
{
        GConfClient *gconf_client;
        guint error_level;

        gconf_client = gconf_client_get_default ();
        error_level = gconf_client_get_int (gconf_client, path, NULL);

        g_object_unref (gconf_client);
        return error_level;
}

guint
eni_config_get_error_timeout (const gchar *path)
{
        GConfClient *gconf_client;
        guint error_time;

        gconf_client = gconf_client_get_default ();
        error_time = gconf_client_get_int (gconf_client, path, NULL);

        g_object_unref (gconf_client);
        return error_time;
}

static void
eni_error_timeout_changed (GtkSpinButton *b, gpointer data)
{
        GConfClient *gconf_client;
        gint value = gtk_spin_button_get_value_as_int (b);

        gconf_client = gconf_client_get_default ();

        gconf_client_set_int (gconf_client, (gchar *) data, value, NULL);
        g_object_unref (gconf_client);
}

static void
eni_error_level_value_changed (GtkComboBox *w, gpointer *data)
{
        GConfClient *gconf_client;
        gint value = gtk_combo_box_get_active (w);

        gconf_client = gconf_client_get_default ();

        gconf_client_set_int (gconf_client, (gchar *) data, value, NULL);

        g_object_unref (gconf_client);
}

void
eni_show_logger(ELogger *logger, GtkWidget *top,const gchar *error_timeout_path, const gchar *error_level_path)
{
    GtkWidget *container;
    GtkWidget *label;
    GtkWidget *toplevel;
    GtkWidget *vbox;
    GtkWidget *widget;
    GtkWidget *window;
    GtkListStore *store;
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    gint i;

    toplevel = gtk_widget_get_toplevel (top);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);
    gtk_window_set_title (GTK_WINDOW (window), _("Debug Logs"));
    gtk_window_set_transient_for (
        GTK_WINDOW (window), GTK_WINDOW (toplevel));
    gtk_container_set_border_width (GTK_CONTAINER (window), 12);

    vbox = gtk_vbox_new (FALSE, 12);
    gtk_container_add (GTK_CONTAINER (window), vbox);

    container = gtk_hbox_new (FALSE, 6);
    gtk_box_pack_start (GTK_BOX (vbox), container, FALSE, FALSE, 0);

    /* Translators: This is the first part of the sentence
     * "Show _errors in the status bar for" - XXX - "second(s)." */
    widget = gtk_label_new_with_mnemonic (
        _("Show _errors in the status bar for"));
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    label = widget;

    widget = gtk_spin_button_new_with_range (1.0, 60.0, 1.0);
    gtk_spin_button_set_value (
        GTK_SPIN_BUTTON (widget),
        (gdouble) eni_config_get_error_timeout (error_timeout_path));
    g_signal_connect (
        widget, "value-changed",
        G_CALLBACK (eni_error_timeout_changed),
        (gpointer) error_timeout_path);

    gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);

    /* Translators: This is the second part of the sentence
     * "Show _errors in the status bar for" - XXX - "second(s)." */
    widget = gtk_label_new_with_mnemonic (_("second(s)."));
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);

    container = gtk_hbox_new (FALSE, 6);
    gtk_box_pack_start (GTK_BOX (vbox), container, FALSE, FALSE, 0);

    widget = gtk_label_new_with_mnemonic (_("Log Messages:"));
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    label = widget;

    widget = gtk_combo_box_new_text ();
    for (i = E_LOG_ERROR; i <= E_LOG_DEBUG; i++)
        gtk_combo_box_append_text (
                GTK_COMBO_BOX (widget), ldata[i].text);
    gtk_combo_box_set_active ((GtkComboBox *) widget, eni_config_get_error_level(error_level_path));

    g_signal_connect (
        widget, "changed",
        G_CALLBACK (eni_error_level_value_changed),
        (gpointer) error_level_path);

    gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);

    store = gtk_list_store_new (3, G_TYPE_INT, G_TYPE_LONG, G_TYPE_STRING);
    e_logger_get_logs (logger, (ELogFunction) eni_append_logs, store);
    gtk_tree_sortable_set_sort_column_id (
        GTK_TREE_SORTABLE (store), COL_TIME, GTK_SORT_DESCENDING);

    container = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (
        GTK_SCROLLED_WINDOW (container),
        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type (
        GTK_SCROLLED_WINDOW (container), GTK_SHADOW_IN);
    gtk_box_pack_start (GTK_BOX (vbox), container, TRUE, TRUE, 0);

    widget = gtk_tree_view_new();
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (widget), TRUE);
    gtk_tree_view_set_reorderable (GTK_TREE_VIEW (widget), FALSE);
    gtk_tree_view_set_model (GTK_TREE_VIEW (widget), GTK_TREE_MODEL (store));
    gtk_tree_view_set_search_column (GTK_TREE_VIEW (widget), COL_DATA);
    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (widget), TRUE);
    gtk_widget_set_has_tooltip (widget, TRUE);
    gtk_container_add (GTK_CONTAINER (container), widget);

    g_signal_connect (
        widget, "query-tooltip",
        G_CALLBACK (eni_query_tooltip_cb), NULL);

    column = gtk_tree_view_column_new ();
    gtk_tree_view_column_set_title (column, _("Log Level"));
    gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
    renderer = gtk_cell_renderer_pixbuf_new ();
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_column_set_cell_data_func (
        column, renderer, eni_render_pixbuf, NULL, NULL);

    column = gtk_tree_view_column_new ();
    gtk_tree_view_column_set_title (column, _("Time"));
    gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);

    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_column_pack_start (column, renderer, FALSE);
    gtk_tree_view_column_set_cell_data_func (
        column, renderer, eni_render_date, NULL, NULL);

    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_insert_column_with_attributes(
        GTK_TREE_VIEW (widget), -1, _("Messages"),
        renderer, "markup", COL_DATA, NULL);

    container = gtk_hbutton_box_new ();
    gtk_button_box_set_layout (
        GTK_BUTTON_BOX (container), GTK_BUTTONBOX_END);
    gtk_box_pack_start (GTK_BOX (vbox), container, FALSE, FALSE, 0);

    widget = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
    gtk_widget_set_tooltip_text (widget, _("Close this window"));
    g_signal_connect_swapped (
        widget, "clicked",
        G_CALLBACK (gtk_widget_destroy), window);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);

    gtk_widget_show_all (window);
}