diff options
Diffstat (limited to 'src/fs/common.c')
-rw-r--r-- | src/fs/common.c | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/src/fs/common.c b/src/fs/common.c new file mode 100644 index 00000000..f586a3d4 --- /dev/null +++ b/src/fs/common.c | |||
@@ -0,0 +1,183 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 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/common.c | ||
23 | * @brief Common functions used in various places | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "common.h" | ||
27 | |||
28 | /** | ||
29 | * Add meta data to list store. | ||
30 | * | ||
31 | * @param cls closure (the GtkListStore) | ||
32 | * @param plugin_name name of the plugin that produced this value; | ||
33 | * special values can be used (i.e. '<zlib>' for zlib being | ||
34 | * used in the main libextractor library and yielding | ||
35 | * meta data). | ||
36 | * @param type libextractor-type describing the meta data | ||
37 | * @param format basic format information about data | ||
38 | * @param data_mime_type mime-type of data (not of the original file); | ||
39 | * can be NULL (if mime-type is not known) | ||
40 | * @param data actual meta-data found | ||
41 | * @param data_len number of bytes in data | ||
42 | * @return 0 to continue (always) | ||
43 | */ | ||
44 | int | ||
45 | GNUNET_FS_GTK_add_meta_data_to_list_store (void *cls, | ||
46 | const char *plugin_name, | ||
47 | enum EXTRACTOR_MetaType type, | ||
48 | enum EXTRACTOR_MetaFormat format, | ||
49 | const char *data_mime_type, | ||
50 | const char *data, | ||
51 | size_t data_len) | ||
52 | { | ||
53 | GtkListStore *ls = GTK_LIST_STORE (cls); | ||
54 | |||
55 | if ( (format == EXTRACTOR_METAFORMAT_UTF8) || | ||
56 | (format == EXTRACTOR_METAFORMAT_C_STRING) ) | ||
57 | gtk_list_store_insert_with_values (ls, | ||
58 | NULL, | ||
59 | G_MAXINT, | ||
60 | 0, type, | ||
61 | 1, format, | ||
62 | 2, EXTRACTOR_metatype_to_string (type), | ||
63 | 3, data, | ||
64 | -1); | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | |||
69 | /** | ||
70 | * Convert the year from the spin button to an expiration | ||
71 | * time (on midnight, January 1st of that year). | ||
72 | */ | ||
73 | struct GNUNET_TIME_Absolute | ||
74 | GNUNET_FS_GTK_get_expiration_time (GtkSpinButton *spin) | ||
75 | { | ||
76 | struct GNUNET_TIME_Absolute ret; | ||
77 | int year; | ||
78 | |||
79 | year = gtk_spin_button_get_value_as_int (spin); | ||
80 | GNUNET_assert (year >= 0); | ||
81 | ret = GNUNET_FS_year_to_time ( (unsigned int) year); | ||
82 | GNUNET_break (GNUNET_TIME_absolute_get ().abs_value < ret.abs_value); | ||
83 | return ret; | ||
84 | } | ||
85 | |||
86 | |||
87 | void | ||
88 | GNUNET_FS_GTK_setup_expiration_year_adjustment (GtkBuilder *builder) | ||
89 | { | ||
90 | GtkAdjustment *aj; | ||
91 | unsigned int year; | ||
92 | |||
93 | year = GNUNET_FS_get_current_year (); | ||
94 | aj = GTK_ADJUSTMENT (gtk_builder_get_object (builder, | ||
95 | "expiration_year_adjustment")); | ||
96 | gtk_adjustment_set_value (aj, year + 2); | ||
97 | gtk_adjustment_set_lower (aj, year + 1); | ||
98 | } | ||
99 | |||
100 | |||
101 | GdkPixbuf * | ||
102 | GNUNET_FS_GTK_get_thumbnail_from_meta_data (const struct GNUNET_CONTAINER_MetaData *meta) | ||
103 | { | ||
104 | GdkPixbuf *pixbuf; | ||
105 | GdkPixbufLoader *loader; | ||
106 | size_t ts; | ||
107 | unsigned char *thumb; | ||
108 | |||
109 | thumb = NULL; | ||
110 | ts = GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb); | ||
111 | if (ts == 0) | ||
112 | return NULL; | ||
113 | loader = gdk_pixbuf_loader_new (); | ||
114 | gdk_pixbuf_loader_write (loader, (const guchar *) thumb, ts, NULL); | ||
115 | pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); | ||
116 | gdk_pixbuf_loader_close (loader, NULL); | ||
117 | if (pixbuf != NULL) | ||
118 | g_object_ref (pixbuf); | ||
119 | g_object_unref (loader); | ||
120 | GNUNET_free (thumb); | ||
121 | return pixbuf; | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * mmap the given file and run the GNUNET_FS_directory_list_contents | ||
127 | * function on it. | ||
128 | */ | ||
129 | void | ||
130 | GNUNET_FS_GTK_mmap_and_scan (const char *filename, | ||
131 | GNUNET_FS_DirectoryEntryProcessor dep, | ||
132 | void *dep_cls) | ||
133 | { | ||
134 | struct GNUNET_DISK_FileHandle *fh; | ||
135 | struct GNUNET_DISK_MapHandle *mh; | ||
136 | uint64_t fsize; | ||
137 | void * ddata; | ||
138 | |||
139 | if (GNUNET_OK != | ||
140 | GNUNET_DISK_file_size (filename, | ||
141 | &fsize, | ||
142 | GNUNET_YES)) | ||
143 | { | ||
144 | GNUNET_break (0); | ||
145 | return; | ||
146 | } | ||
147 | fh = GNUNET_DISK_file_open (filename, | ||
148 | GNUNET_DISK_OPEN_READ, | ||
149 | GNUNET_DISK_PERM_NONE); | ||
150 | if (fh == NULL) | ||
151 | { | ||
152 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); | ||
153 | return; | ||
154 | } | ||
155 | ddata = GNUNET_DISK_file_map (fh, | ||
156 | &mh, | ||
157 | GNUNET_DISK_MAP_TYPE_READ, | ||
158 | (size_t) fsize); | ||
159 | if (ddata == NULL) | ||
160 | { | ||
161 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mmap", filename); | ||
162 | GNUNET_break (GNUNET_OK == | ||
163 | GNUNET_DISK_file_close (fh)); | ||
164 | return; | ||
165 | } | ||
166 | if (GNUNET_SYSERR == | ||
167 | GNUNET_FS_directory_list_contents ((size_t) fsize, | ||
168 | ddata, | ||
169 | 0, | ||
170 | dep, dep_cls)) | ||
171 | { | ||
172 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
173 | _("Selected file `%s' is not a GNUnet directory!\n"), | ||
174 | filename); | ||
175 | } | ||
176 | GNUNET_break (GNUNET_OK == | ||
177 | GNUNET_DISK_file_unmap (mh)); | ||
178 | GNUNET_break (GNUNET_OK == | ||
179 | GNUNET_DISK_file_close (fh)); | ||
180 | } | ||
181 | |||
182 | |||
183 | /* end of common.c */ | ||