diff options
Diffstat (limited to 'src/fs/gnunet-fs-gtk_open-directory.c')
-rw-r--r-- | src/fs/gnunet-fs-gtk_open-directory.c | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/src/fs/gnunet-fs-gtk_open-directory.c b/src/fs/gnunet-fs-gtk_open-directory.c new file mode 100644 index 00000000..33ca2003 --- /dev/null +++ b/src/fs/gnunet-fs-gtk_open-directory.c | |||
@@ -0,0 +1,180 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2005, 2006, 2010 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/fs/gnunet-fs-gtk_open-directory.c | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "gnunet-fs-gtk_common.h" | ||
26 | #include "gnunet-fs-gtk_event-handler.h" | ||
27 | |||
28 | |||
29 | /** | ||
30 | * Closure for 'add_child' function. | ||
31 | */ | ||
32 | struct AddChildContext | ||
33 | { | ||
34 | /** | ||
35 | * Name of the directory file. | ||
36 | */ | ||
37 | const char *filename; | ||
38 | |||
39 | /** | ||
40 | * Tree store where we will add entries. | ||
41 | */ | ||
42 | GtkTreeStore *ts; | ||
43 | |||
44 | /** | ||
45 | * Tab we've opened for the directory. | ||
46 | */ | ||
47 | struct SearchTab *tab; | ||
48 | |||
49 | /** | ||
50 | * Row reference to the directorie's parent entry. | ||
51 | */ | ||
52 | GtkTreeRowReference *prr; | ||
53 | |||
54 | }; | ||
55 | |||
56 | |||
57 | /** | ||
58 | * Function used to process entries in a directory. Adds each | ||
59 | * entry to our tab. | ||
60 | * | ||
61 | * @param cls closure, our 'struct AddChildContext*' | ||
62 | * @param filename name of the file in the directory | ||
63 | * @param uri URI of the file | ||
64 | * @param metadata metadata for the file; metadata for | ||
65 | * the directory if everything else is NULL/zero | ||
66 | * @param length length of the available data for the file | ||
67 | * (of type size_t since data must certainly fit | ||
68 | * into memory; if files are larger than size_t | ||
69 | * permits, then they will certainly not be | ||
70 | * embedded with the directory itself). | ||
71 | * @param data data available for the file (length bytes) | ||
72 | */ | ||
73 | static void | ||
74 | add_child (void *cls, const char *filename, const struct GNUNET_FS_Uri *uri, | ||
75 | const struct GNUNET_CONTAINER_MetaData *meta, size_t length, | ||
76 | const void *data) | ||
77 | { | ||
78 | struct AddChildContext *acc = cls; | ||
79 | GtkTreeIter iter; | ||
80 | |||
81 | if (NULL == uri) | ||
82 | { | ||
83 | /* directory meta data itself, create parent entry */ | ||
84 | struct GNUNET_CONTAINER_MetaData *dmeta; | ||
85 | GtkTreePath *tp; | ||
86 | |||
87 | dmeta = GNUNET_CONTAINER_meta_data_duplicate (meta); | ||
88 | GNUNET_CONTAINER_meta_data_insert (dmeta, "<user>", | ||
89 | EXTRACTOR_METATYPE_FILENAME, | ||
90 | EXTRACTOR_METAFORMAT_UTF8, "text/plain", | ||
91 | acc->filename, | ||
92 | strlen (acc->filename) + 1); | ||
93 | acc->tab = GNUNET_GTK_add_to_uri_tab (&iter, NULL, dmeta, NULL); | ||
94 | tp = gtk_tree_model_get_path (GTK_TREE_MODEL (acc->tab->ts), &iter); | ||
95 | acc->prr = gtk_tree_row_reference_new (GTK_TREE_MODEL (acc->tab->ts), tp); | ||
96 | gtk_tree_path_free (tp); | ||
97 | acc->ts = acc->tab->ts; | ||
98 | GNUNET_CONTAINER_meta_data_destroy (dmeta); | ||
99 | return; | ||
100 | } | ||
101 | if (NULL == acc->ts) | ||
102 | { | ||
103 | GNUNET_break (0); | ||
104 | return; | ||
105 | } | ||
106 | GNUNET_assert (NULL != | ||
107 | GNUNET_GTK_add_search_result (acc->tab, &iter, acc->prr, uri, | ||
108 | meta, NULL, 0)); | ||
109 | } | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Function called from the open-directory dialog upon completion. | ||
114 | * | ||
115 | * @param dialog the pseudonym selection dialog | ||
116 | * @param response_id response code from the dialog | ||
117 | * @param user_data the builder of the dialog | ||
118 | */ | ||
119 | void | ||
120 | GNUNET_GTK_open_directory_dialog_response_cb (GtkDialog * dialog, | ||
121 | gint response_id, | ||
122 | gpointer user_data) | ||
123 | { | ||
124 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
125 | char *filename; | ||
126 | struct AddChildContext acc; | ||
127 | |||
128 | if (-5 != response_id) | ||
129 | { | ||
130 | gtk_widget_destroy (GTK_WIDGET (dialog)); | ||
131 | g_object_unref (G_OBJECT (builder)); | ||
132 | return; | ||
133 | } | ||
134 | filename = GNUNET_GTK_filechooser_get_filename_utf8 (GTK_FILE_CHOOSER (dialog)); | ||
135 | gtk_widget_destroy (GTK_WIDGET (dialog)); | ||
136 | g_object_unref (G_OBJECT (builder)); | ||
137 | acc.filename = filename; | ||
138 | acc.ts = NULL; | ||
139 | GNUNET_FS_GTK_mmap_and_scan (filename, &add_child, &acc); | ||
140 | g_free (filename); | ||
141 | } | ||
142 | |||
143 | |||
144 | /** | ||
145 | * User selected "Open directory" in menu. Display dialog, open | ||
146 | * file and then display a new tab with its contents. | ||
147 | * | ||
148 | * @param dummy the menu entry | ||
149 | * @param user_data the main dialog builder, unused | ||
150 | */ | ||
151 | void | ||
152 | GNUNET_GTK_main_menu_file_open_gnunet_directory_activate_cb (GtkWidget * dummy, | ||
153 | gpointer data) | ||
154 | { | ||
155 | GtkWidget *ad; | ||
156 | GtkBuilder *builder; | ||
157 | GtkWidget *toplevel; | ||
158 | GtkFileFilter *ff; | ||
159 | |||
160 | builder = | ||
161 | GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_open_directory_dialog.glade", NULL); | ||
162 | if (NULL == builder) | ||
163 | { | ||
164 | GNUNET_break (0); | ||
165 | return; | ||
166 | } | ||
167 | ad = GTK_WIDGET (gtk_builder_get_object | ||
168 | (builder, "GNUNET_GTK_open_directory_dialog")); | ||
169 | ff = GTK_FILE_FILTER (gtk_builder_get_object | ||
170 | (builder, "gnunet_directory_filter")); | ||
171 | /* FIXME: some day, write a custom file filter for gnunet-directories... */ | ||
172 | gtk_file_filter_add_pattern (ff, "*" GNUNET_FS_DIRECTORY_EXT); | ||
173 | |||
174 | toplevel = gtk_widget_get_toplevel (dummy); | ||
175 | if (GTK_IS_WINDOW (toplevel)) | ||
176 | gtk_window_set_transient_for (GTK_WINDOW (ad), GTK_WINDOW (toplevel)); | ||
177 | gtk_window_present (GTK_WINDOW (ad)); | ||
178 | } | ||
179 | |||
180 | /* end of gnunet-fs-gtk_open-directory.c */ | ||