]> git.sur5r.net Git - glabels/blob - src/recent.c
Imported Upstream version 3.0.0
[glabels] / src / recent.c
1 /*
2  *  recent.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "recent.h"
24
25 #include <gtk/gtk.h>
26 #include <string.h>
27
28 #include "prefs.h"
29
30 #include "debug.h"
31
32 #define GLABELS_MIME_TYPE "application/x-glabels"
33
34 static GtkRecentManager *model;
35
36
37 /*****************************************************************************/
38 /* Initialize recent files model.                                            */
39 /*****************************************************************************/
40 void
41 gl_recent_init (void)
42 {
43         gl_debug (DEBUG_RECENT, "START");
44
45         model = gtk_recent_manager_get_default ();
46
47         gl_debug (DEBUG_RECENT, "END");
48 }
49
50
51 /*****************************************************************************/
52 /* Get UTF8 filename from GtkRecentInfo structure.                           */
53 /*****************************************************************************/
54 gchar *
55 gl_recent_get_utf8_filename (GtkRecentInfo *item)
56 {
57         const gchar *uri;
58         gchar       *filename;
59         gchar       *utf8_filename = NULL;
60
61         gl_debug (DEBUG_RECENT, "START");
62
63         uri = gtk_recent_info_get_uri (item);
64
65         filename = g_filename_from_uri (uri, NULL, NULL);
66         if ( filename != NULL )
67         {
68                 utf8_filename = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
69                 g_free (filename);
70         }
71
72         return utf8_filename;
73         gl_debug (DEBUG_RECENT, "END");
74 }
75
76
77 /*****************************************************************************/
78 /* Add file by UTF8 filename to recent model.                                */
79 /*****************************************************************************/
80 void
81 gl_recent_add_utf8_filename (gchar *utf8_filename)
82 {
83         GtkRecentData *recent_data;
84         gchar         *filename;
85         gchar         *uri;
86
87         static gchar *groups[2] = {
88                 "glabels",
89                 NULL
90         };
91
92         gl_debug (DEBUG_RECENT, "START");
93
94         recent_data = g_slice_new (GtkRecentData);
95
96         recent_data->display_name = NULL;
97         recent_data->description  = NULL;
98         recent_data->mime_type    = GLABELS_MIME_TYPE;
99         recent_data->app_name     = (gchar *) g_get_application_name ();
100         recent_data->app_exec     = g_strjoin (" ", g_get_prgname (), "%f", NULL);
101         recent_data->groups       = groups;
102         recent_data->is_private = FALSE;
103
104         filename = g_filename_from_utf8 (utf8_filename, -1, NULL, NULL, NULL);
105         if ( filename != NULL )
106         {
107
108                 uri = g_filename_to_uri (filename, NULL, NULL);
109                 if ( uri != NULL )
110                 {
111
112                         gtk_recent_manager_add_full (model, uri, recent_data);
113                         g_free (uri);
114
115                 }
116                 g_free (filename);
117
118         }
119
120         g_free (recent_data->app_exec);
121         g_slice_free (GtkRecentData, recent_data);
122
123         gl_debug (DEBUG_RECENT, "END");
124 }
125
126
127 /*****************************************************************************/
128 /* Create a menu of recent files.                                            */
129 /*****************************************************************************/
130 GtkWidget *
131 gl_recent_create_menu (void)
132 {
133         GtkWidget               *recent_menu;
134         GtkRecentFilter         *recent_filter;
135
136         gl_debug (DEBUG_RECENT, "START");
137
138         recent_menu  =
139                 gtk_recent_chooser_menu_new_for_manager (model);
140         gtk_recent_chooser_menu_set_show_numbers (GTK_RECENT_CHOOSER_MENU (recent_menu), FALSE);
141         gtk_recent_chooser_set_show_icons (GTK_RECENT_CHOOSER (recent_menu), TRUE);
142         gtk_recent_chooser_set_limit (GTK_RECENT_CHOOSER (recent_menu),
143                                       gl_prefs_model_get_max_recents (gl_prefs));
144         gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (recent_menu), GTK_RECENT_SORT_MRU);
145         gtk_recent_chooser_set_local_only (GTK_RECENT_CHOOSER (recent_menu), TRUE);
146
147         recent_filter = gtk_recent_filter_new ();
148         gtk_recent_filter_add_mime_type (recent_filter, GLABELS_MIME_TYPE);
149         gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (recent_menu), recent_filter);
150
151         gl_debug (DEBUG_RECENT, "END");
152         return recent_menu;
153 }
154
155
156
157
158 /*
159  * Local Variables:       -- emacs
160  * mode: C                -- emacs
161  * c-basic-offset: 8      -- emacs
162  * tab-width: 8           -- emacs
163  * indent-tabs-mode: nil  -- emacs
164  * End:                   -- emacs
165  */