aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-common.c
blob: bd33d17a954b1d2d077fef82d0d85f68ed6399e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
     This file is part of GNUnet.
     (C) 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/fs/gnunet-fs-gtk-common.c
 * @brief Common functions used in various places
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk-common.h"

/**
 * Converts metadata specified by @data of size @data_len
 * and saved in format @format to UTF-8 encoded string.
 * Works only for C-string and UTF8 metadata formats
 * (returns NULL for everything else).
 * Verifies UTF-8 strings.
 *
 * @param format format of the @data
 * @param data data to convert
 * @param data_len length of the data buffer (in bytes)
 * @return NULL if can't be converted, allocated string otherwise,
 *         freeable with GNUNET_free* ().
 */
char *
GNUNET_FS_GTK_dubious_meta_to_utf8 (enum EXTRACTOR_MetaFormat format, const char *data, size_t data_len)
{
  gchar *result = NULL;

  if (format == EXTRACTOR_METAFORMAT_UTF8)
  {
    /* data must not contain NULLs (hence the -1) */
    if (g_utf8_validate (data, data_len - 1, NULL))
      result = GNUNET_strdup (data);
    else
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
          "Failed to validate supposedly utf-8 string `%s' of length %u, assuming it to be a C string\n",
          data, data_len);
      format = EXTRACTOR_METAFORMAT_C_STRING;
    }
  }
  if (format == EXTRACTOR_METAFORMAT_C_STRING)
  {
    if (data_len > 0)
    { /* There are no guarantees that data is NULL-terminated, AFAIU,
       * so let's play it safe, shall we?
       */
      char *data_copy = GNUNET_malloc (data_len + 1);
      memcpy (data_copy, data, data_len);
      data_copy[data_len] = '\0';
      result = GNUNET_GTK_from_loc_to_utf8 (data_copy);
      GNUNET_free (data_copy);
    }
  }
  return result;
}

/**
 * Add meta data to list store.
 *
 * @param cls closure (the GtkListStore)
 * @param plugin_name name of the plugin that produced this value;
 *        special values can be used (i.e. '<zlib>' for zlib being
 *        used in the main libextractor library and yielding
 *        meta data).
 * @param type libextractor-type describing the meta data
 * @param format basic format information about data
 * @param data_mime_type mime-type of data (not of the original file);
 *        can be NULL (if mime-type is not known)
 * @param data actual meta-data found
 * @param data_len number of bytes in data
 * @return 0 to continue (always)
 */
int
GNUNET_FS_GTK_add_meta_data_to_list_store (void *cls, const char *plugin_name,
                                           enum EXTRACTOR_MetaType type,
                                           enum EXTRACTOR_MetaFormat format,
                                           const char *data_mime_type,
                                           const char *data, size_t data_len)
{
  GtkListStore *ls = GTK_LIST_STORE (cls);
  gchar *data_to_insert = NULL;

  data_to_insert = GNUNET_FS_GTK_dubious_meta_to_utf8 (format, data, data_len);

  if (NULL != data_to_insert)  
  {
    gtk_list_store_insert_with_values (ls, NULL, G_MAXINT, 0, type, 1, format,
                                       2, EXTRACTOR_metatype_to_string (type),
                                       3, data_to_insert, -1);
    g_free (data_to_insert);
  }

  return 0;
}


/**
 * Convert the year from the spin button to an expiration
 * time (on midnight, January 1st of that year).
 */
struct GNUNET_TIME_Absolute
GNUNET_FS_GTK_get_expiration_time (GtkSpinButton * spin)
{
  struct GNUNET_TIME_Absolute ret;
  int year;

  year = gtk_spin_button_get_value_as_int (spin);
  GNUNET_assert (year >= 0);
  ret = GNUNET_FS_year_to_time ((unsigned int) year);
  GNUNET_break (GNUNET_TIME_absolute_get ().abs_value < ret.abs_value);
  return ret;
}


void
GNUNET_FS_GTK_setup_expiration_year_adjustment (GtkBuilder * builder)
{
  GtkAdjustment *aj;
  unsigned int year;

  year = GNUNET_FS_get_current_year ();
  aj = GTK_ADJUSTMENT (gtk_builder_get_object
                       (builder, "expiration_year_adjustment"));
  gtk_adjustment_set_value (aj, year + 2);
  gtk_adjustment_set_lower (aj, year + 1);
}


GdkPixbuf *
GNUNET_FS_GTK_get_thumbnail_from_meta_data (const struct
                                            GNUNET_CONTAINER_MetaData *meta)
{
  GdkPixbuf *pixbuf;
  GdkPixbufLoader *loader;
  size_t ts;
  unsigned char *thumb;

  thumb = NULL;
  ts = GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb);
  if (ts == 0)
    return NULL;
  loader = gdk_pixbuf_loader_new ();
  gdk_pixbuf_loader_write (loader, (const guchar *) thumb, ts, NULL);
  pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
  gdk_pixbuf_loader_close (loader, NULL);
  if (pixbuf != NULL)
    g_object_ref (pixbuf);
  g_object_unref (loader);
  GNUNET_free (thumb);
  return pixbuf;
}


/**
 * mmap the given file and run the GNUNET_FS_directory_list_contents
 * function on it.
 */
void
GNUNET_FS_GTK_mmap_and_scan (const char *filename,
                             GNUNET_FS_DirectoryEntryProcessor dep,
                             void *dep_cls)
{
  struct GNUNET_DISK_FileHandle *fh;
  struct GNUNET_DISK_MapHandle *mh;
  uint64_t fsize;
  void *ddata;

  if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fsize, GNUNET_YES))
  {
    GNUNET_break (0);
    return;
  }
  fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
                              GNUNET_DISK_PERM_NONE);
  if (fh == NULL)
  {
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
    return;
  }
  ddata =
      GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, (size_t) fsize);
  if (ddata == NULL)
  {
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mmap", filename);
    GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
    return;
  }
  if (GNUNET_SYSERR ==
      GNUNET_FS_directory_list_contents ((size_t) fsize, ddata, 0, dep,
                                         dep_cls))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Selected file `%s' is not a GNUnet directory!\n"), filename);
  }
  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
}

/* end of gnunet-fs-gtk-common.c */