]> www.fi.muni.cz Git - evince.git/blob - shell/ev-properties-fonts.c
Correctly disconnect from job signals on destroy.
[evince.git] / shell / ev-properties-fonts.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
3  *
4  *  Copyright (C) 2005 Red Hat, Inc
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "ev-properties-fonts.h"
26 #include "ev-document-fonts.h"
27 #include "ev-jobs.h"
28 #include "ev-job-queue.h"
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtktreeview.h>
32 #include <glade/glade.h>
33
34 struct _EvPropertiesFonts {
35         GtkVBox base_instance;
36
37         GladeXML *xml;
38
39         GtkWidget *fonts_treeview;
40         GtkWidget *fonts_progress_label;
41         EvJob     *fonts_job;
42
43         EvDocument *document;
44 };
45
46 struct _EvPropertiesFontsClass {
47         GtkVBoxClass base_class;
48 };
49
50 static void
51 job_fonts_finished_cb (EvJob *job, EvPropertiesFonts *properties);
52
53 G_DEFINE_TYPE (EvPropertiesFonts, ev_properties_fonts, GTK_TYPE_VBOX)
54
55 static void
56 ev_properties_fonts_dispose (GObject *object)
57 {
58         EvPropertiesFonts *properties = EV_PROPERTIES_FONTS (object);
59
60         if (properties->xml) {
61                 g_object_unref (properties->xml);
62                 properties->xml = NULL;
63         }
64         
65         if (properties->fonts_job) {
66
67                 g_signal_handlers_disconnect_by_func
68                                 (properties->fonts_job, 
69                                  job_fonts_finished_cb, 
70                                  properties);
71                 ev_job_queue_remove_job (properties->fonts_job);
72
73                 g_object_unref (properties->fonts_job);         
74                 properties->fonts_job = NULL;
75         }
76 }
77
78 static void
79 ev_properties_fonts_class_init (EvPropertiesFontsClass *properties_class)
80 {
81         GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
82
83         g_object_class->dispose = ev_properties_fonts_dispose;
84 }
85
86 static void
87 font_cell_data_func (GtkTreeViewColumn *col, GtkCellRenderer *renderer,
88                      GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
89 {
90         char *name;
91         char *details;
92         char *markup;
93
94         gtk_tree_model_get(model, iter,
95                            EV_DOCUMENT_FONTS_COLUMN_NAME, &name,
96                            EV_DOCUMENT_FONTS_COLUMN_DETAILS, &details,
97                            -1); 
98
99         if (details) {
100                 markup = g_strdup_printf ("<b><big>%s</big></b>\n<small>%s</small>",
101                                           name, details);
102         } else {
103                 markup = g_strdup_printf ("<b><big>%s</big></b>", name);
104         }
105
106         g_object_set (renderer, "markup", markup, NULL);
107         
108         g_free (markup);
109         g_free (details);
110         g_free (name);
111 }
112
113 static void
114 ev_properties_fonts_init (EvPropertiesFonts *properties)
115 {
116         GladeXML *xml;
117         GtkCellRenderer *renderer;
118         GtkTreeViewColumn *column;
119
120         /* Create a new GladeXML object from XML file glade_file */
121         xml = glade_xml_new (DATADIR "/evince-properties.glade", "fonts_page_root", NULL);
122         properties->xml = xml;
123         g_assert (xml != NULL);
124
125         gtk_box_pack_start (GTK_BOX (properties),
126                             glade_xml_get_widget (xml, "fonts_page_root"),
127                             TRUE, TRUE, 0);
128
129         properties->fonts_treeview = glade_xml_get_widget (xml, "fonts_treeview");
130         properties->fonts_progress_label = glade_xml_get_widget (xml, "font_progress_label");
131
132         column = gtk_tree_view_column_new ();
133         gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
134         gtk_tree_view_append_column (GTK_TREE_VIEW (properties->fonts_treeview), column);
135
136         renderer = GTK_CELL_RENDERER (g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
137                                                     "ypad", 6, NULL));
138         gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
139         gtk_tree_view_column_set_title (GTK_TREE_VIEW_COLUMN (column), _("Font"));
140         gtk_tree_view_column_set_cell_data_func (column, renderer,
141                                                  font_cell_data_func,
142                                                  NULL, NULL);
143 }
144
145 static void
146 update_progress_label (GtkWidget *label, double progress)
147 {
148         if (progress > 0) {
149                 char *progress_text;
150                 progress_text = g_strdup_printf (_("Gathering font information... %3d%%"),
151                                                  (int) (progress * 100));
152                 gtk_label_set_text (GTK_LABEL (label), progress_text);
153                 g_free (progress_text);
154                 gtk_widget_show (label);
155         } else {
156                 gtk_widget_hide (label);
157         }
158 }
159
160 static void
161 job_fonts_finished_cb (EvJob *job, EvPropertiesFonts *properties)
162 {       
163         EvDocumentFonts *document_fonts = EV_DOCUMENT_FONTS (job->document);
164         double progress;
165         
166         progress = ev_document_fonts_get_progress (document_fonts);
167         update_progress_label (properties->fonts_progress_label, progress);
168
169         if (EV_JOB_FONTS (job)->scan_completed) {
170                 g_signal_handlers_disconnect_by_func
171                                 (job, job_fonts_finished_cb, properties);
172                 g_object_unref (properties->fonts_job);
173                 properties->fonts_job = NULL;
174         } else {
175                 GtkTreeModel *model;
176
177                 model = gtk_tree_view_get_model
178                                 (GTK_TREE_VIEW (properties->fonts_treeview));
179                 ev_document_doc_mutex_lock ();
180                 ev_document_fonts_fill_model (document_fonts, model);
181                 ev_document_doc_mutex_unlock ();
182
183                 ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
184         }
185 }
186
187 void
188 ev_properties_fonts_set_document (EvPropertiesFonts *properties,
189                                   EvDocument        *document)
190 {
191         GtkTreeView *tree_view = GTK_TREE_VIEW (properties->fonts_treeview);
192         GtkListStore *list_store;
193
194         properties->document = document;
195
196         list_store = gtk_list_store_new (EV_DOCUMENT_FONTS_COLUMN_NUM_COLUMNS,
197                                          G_TYPE_STRING, G_TYPE_STRING);
198         gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (list_store));
199
200         properties->fonts_job = ev_job_fonts_new (properties->document);
201         g_signal_connect (properties->fonts_job, "finished",
202                           G_CALLBACK (job_fonts_finished_cb),
203                           properties);
204         ev_job_queue_add_job (properties->fonts_job, EV_JOB_PRIORITY_LOW);
205 }
206
207 GtkWidget *
208 ev_properties_fonts_new ()
209 {
210         EvPropertiesFonts *properties;
211
212         properties = g_object_new (EV_TYPE_PROPERTIES_FONTS, NULL);
213
214         return GTK_WIDGET (properties);
215 }