Debian Codegraph @

src/tools/dlgedit/gui_recent_files.cc

raw
1 /*
2 Copyright (C) 2011 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file common/gui_recent_files.cc
21 *
22 * @author Kai Sterker
23 * @brief Keep track of recently opened files.
24 */
25
26 #include "util.h"
27 #include "gui_recent_files.h"
28
29 static void on_recent_file_selected (GtkRecentChooser *chooser, gpointer user_data)
30 {
31 GuiRecentFiles *recent_files = (GuiRecentFiles *) user_data;
32 GuiRecentFileListener *listener = recent_files->getListener();
33
34 if (listener != NULL)
35 {
36 gchar* uri = gtk_recent_chooser_get_current_uri (chooser);
37 gchar* file = g_filename_from_uri (uri, NULL, NULL);
38
39 listener->OnRecentFileActivated(file);
40
41 g_free (file);
42 g_free (uri);
43 }
44 }
45
46 // ctor
47 GuiRecentFiles::GuiRecentFiles(const std::string & cmd, const std::string & args)
48 {
49 Listener = NULL;
50 Command = cmd;
51 Arguments = args;
52 Manager = gtk_recent_manager_get_default ();
53
54 // only show what's been opened by the current program
55 GtkRecentFilter *filter = gtk_recent_filter_new ();
56 gtk_recent_filter_add_application (filter, cmd.c_str());
57
58 // create menu to use in application
59 Menu = gtk_recent_chooser_menu_new_for_manager (Manager);
60 gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (Menu), filter);
61 gtk_recent_chooser_set_limit (GTK_RECENT_CHOOSER (Menu), 10);
62 gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (Menu), GTK_RECENT_SORT_MRU);
63
64 g_signal_connect (G_OBJECT(Menu), "item-activated", G_CALLBACK (on_recent_file_selected), this);
65 }
66
67 // dtor
68 GuiRecentFiles::~GuiRecentFiles()
69 {
70 }
71
72 // register new recent file
73 void GuiRecentFiles::registerFile (const std::string & file, const std::string & type)
74 {
75 GtkRecentData info;
76 info.display_name = NULL;
77 info.description = NULL;
78 info.mime_type = g_strdup (type.c_str());
79 info.app_name = g_strdup (Command.c_str());
80 info.app_exec = g_strdup (Arguments.c_str());
81 info.groups = NULL;
82 info.is_private = FALSE;
83
84 gchar* uri = g_filename_to_uri (util::get_absolute_path(file).c_str(), NULL, NULL);
85 gtk_recent_manager_add_full (Manager, uri, &info);
86 }