+static void recent_open(GtkAction *action, gpointer data)
+{
+ struct gui *ui = (struct gui *) data;
+ int port, type, server_start;
+ struct gui_entry *ge;
+ GtkRecentInfo *info;
+ const gchar *uri;
+ gint cur_page;
+ char *host;
+ int ret, ge_is_new = 0;
+
+ /*
+ * Creates new tab if current tab is the main window, or the
+ * current tab already has a client.
+ */
+ cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(ui->notebook));
+ ge = get_ge_from_page(cur_page, &ge_is_new);
+ if (ge->client) {
+ ge = get_new_ge_with_tab("Untitled");
+ ge_is_new = 1;
+ }
+
+ gtk_notebook_set_current_page(GTK_NOTEBOOK(ui->notebook), ge->page_num);
+
+ info = g_object_get_data(G_OBJECT(action), "gtk-recent-info");
+ uri = gtk_recent_info_get_uri(info);
+
+ if (get_connection_details(&host, &port, &type, &server_start)) {
+ if (ge_is_new)
+ gtk_widget_destroy(ge->vbox);
+
+ return;
+ }
+
+ ret = do_file_open(ge, uri, host, type, port);
+
+ free(host);
+
+ if (!ret) {
+ if (server_start)
+ gfio_start_server();
+ } else {
+ if (ge_is_new)
+ gtk_widget_destroy(ge->vbox);
+ }
+}
+
+static gint compare_recent_items(GtkRecentInfo *a, GtkRecentInfo *b)
+{
+ time_t time_a = gtk_recent_info_get_visited(a);
+ time_t time_b = gtk_recent_info_get_visited(b);
+
+ return time_b - time_a;
+}
+
+static void add_recent_file_items(struct gui *ui)
+{
+ const gchar *gfio = g_get_application_name();
+ GList *items, *item;
+ int i = 0;
+
+ if (ui->recent_ui_id) {
+ gtk_ui_manager_remove_ui(ui->uimanager, ui->recent_ui_id);
+ gtk_ui_manager_ensure_update(ui->uimanager);
+ }
+ ui->recent_ui_id = gtk_ui_manager_new_merge_id(ui->uimanager);
+
+ if (ui->actiongroup) {
+ gtk_ui_manager_remove_action_group(ui->uimanager, ui->actiongroup);
+ g_object_unref(ui->actiongroup);
+ }
+ ui->actiongroup = gtk_action_group_new("RecentFileActions");
+
+ gtk_ui_manager_insert_action_group(ui->uimanager, ui->actiongroup, -1);
+
+ items = gtk_recent_manager_get_items(ui->recentmanager);
+ items = g_list_sort(items, (GCompareFunc) compare_recent_items);
+
+ for (item = items; item && item->data; item = g_list_next(item)) {
+ GtkRecentInfo *info = (GtkRecentInfo *) item->data;
+ gchar *action_name;
+ const gchar *label;
+ GtkAction *action;
+
+ if (!gtk_recent_info_has_application(info, gfio))
+ continue;
+
+ /*
+ * We only support local files for now
+ */
+ if (!gtk_recent_info_is_local(info) || !gtk_recent_info_exists(info))
+ continue;
+
+ action_name = g_strdup_printf("RecentFile%u", i++);
+ label = gtk_recent_info_get_display_name(info);
+
+ action = g_object_new(GTK_TYPE_ACTION,
+ "name", action_name,
+ "label", label, NULL);
+
+ g_object_set_data_full(G_OBJECT(action), "gtk-recent-info",
+ gtk_recent_info_ref(info),
+ (GDestroyNotify) gtk_recent_info_unref);
+
+
+ g_signal_connect(action, "activate", G_CALLBACK(recent_open), ui);
+
+ gtk_action_group_add_action(ui->actiongroup, action);
+ g_object_unref(action);
+
+ gtk_ui_manager_add_ui(ui->uimanager, ui->recent_ui_id,
+ "/MainMenu/FileMenu/FileRecentFiles",
+ label, action_name,
+ GTK_UI_MANAGER_MENUITEM, FALSE);
+
+ g_free(action_name);
+
+ if (i == 8)
+ break;
+ }
+
+ g_list_foreach(items, (GFunc) gtk_recent_info_unref, NULL);
+ g_list_free(items);
+}
+