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