aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-main_window_adv_pseudonym.c
blob: 77ae7353827faf8d9fe22dc0e4176ea9f57d1f81 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
     This file is part of GNUnet
     (C) 2005, 2006, 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-main_window_adv_pseudonym.c
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk-common.h"
#include "gnunet-fs-gtk.h"
#include "gnunet-fs-gtk-edit_publish_dialog.h"

/**
 * Callback with information about local (!) namespaces.
 * Contains the names of the local namespace and the global
 * ID.  Adds the to the list store.
 *
 * @param cls closure, the list store.
 * @param name human-readable identifier of the namespace
 * @param id hash identifier for the namespace
 */
static void
add_to_list (void *cls, const char *name, const GNUNET_HashCode * id)
{
  GtkListStore *ls = cls;
  GtkTreeIter iter;

  gtk_list_store_insert_with_values (ls, &iter, -1, 0, name, 1,
                                     GNUNET_FS_namespace_create
                                     (GNUNET_FS_GTK_get_fs_handle (), name),
                                     -1);

}


static void
selection_changed_cb (GtkTreeSelection * ts, gpointer user_data)
{
  GtkTreeIter iter;
  GtkWidget *ok_button;
  GtkBuilder *builder;
  builder = GTK_BUILDER (user_data);

  ok_button =
      GTK_WIDGET (gtk_builder_get_object
                  (builder, "GNUNET_GTK_select_pseudonym_ok_button"));
  if (TRUE == gtk_tree_selection_get_selected (ts, NULL, &iter))
    gtk_widget_set_sensitive (ok_button, TRUE);
  else
    gtk_widget_set_sensitive (ok_button, FALSE);
}

struct NamespaceAdvertisementContext
{
  struct GNUNET_FS_Namespace *ns;
  int do_index;
  const char *short_fn;
  guint anonymity_level;
  guint priority;
  const char *root;
  struct GNUNET_FS_FileInformation *fip;
};


/**
 * Function called on entries in a GNUNET_FS_FileInformation publish-structure.
 *
 * @param cls closure, a 'struct NamespaceAdvertisementContext *'
 * @param fi the entry in the publish-structure
 * @param length length of the file or directory
 * @param meta metadata for the file or directory (can be modified)
 * @param uri pointer to the keywords that will be used for this entry (can be modified)
 * @param bo block options (can be modified)
 * @param do_index should we index (can be modified)
 * @param client_info pointer to client context set upon creation (can be modified)
 * @return GNUNET_OK to continue, GNUNET_NO to remove
 *         this entry from the directory, GNUNET_SYSERR
 *         to abort the iteration
 */
static int
advertise_namespace (void *cls, struct GNUNET_FS_FileInformation *fi,
		     uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
		     struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
		     int *do_index, void **client_info)
{
  struct NamespaceAdvertisementContext *nds = cls;

  GNUNET_FS_namespace_advertise (GNUNET_FS_GTK_get_fs_handle (), *uri,
      nds->ns, meta, bo, nds->root, NULL, NULL);
  return GNUNET_SYSERR;
}


static void
adv_pseudonym_edit_publish_dialog_cb (gpointer cls, 
				      int do_index, 
				      const char *short_fn,
				      guint anonymity_level, 
				      guint priority, 
				      const char *root, 			      
				      gint ret)
{
  struct NamespaceAdvertisementContext *nds = cls;

  if (ret == GTK_RESPONSE_OK)
  {
    nds->do_index = do_index;
    nds->short_fn = short_fn;
    nds->anonymity_level = anonymity_level;
    nds->priority = priority;
    nds->root = root;
    GNUNET_FS_file_information_inspect (nds->fip,
					&advertise_namespace, nds);
  }
  GNUNET_FS_namespace_delete (nds->ns, GNUNET_NO);
  GNUNET_FS_file_information_destroy (nds->fip, NULL, NULL);
  GNUNET_free (nds);
}

void
GNUNET_GTK_select_pseudonym_dialog_response_cb (GtkDialog *dialog,
   gint response_id, gpointer user_data)
{
  GtkBuilder *builder;
  GtkWidget *ad;
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  GtkTreeModel *tm;
  GtkTreeIter iter;
  struct GNUNET_FS_Namespace *ns;
  struct GNUNET_FS_Namespace *nso;
  GtkWindow *transient;
  struct NamespaceAdvertisementContext *nds;
  struct GNUNET_CONTAINER_MetaData *meta;
  struct GNUNET_FS_BlockOptions bo;

  builder = GTK_BUILDER (user_data);
  ad = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_select_pseudonym_dialog"));

  if (response_id != -5)
  {
    gtk_widget_hide (ad);
    return;
  }
  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_select_pseudonym_tree_view"));
  sel = gtk_tree_view_get_selection (tv);

  GNUNET_assert (TRUE == gtk_tree_selection_get_selected (sel, &tm, &iter));
  gtk_tree_model_get (tm, &iter, 1, &ns, -1);
  /* free all namespaces from list store except "ns" */
  if (TRUE == gtk_tree_model_get_iter_first (tm, &iter))
  {
    do
    {
      gtk_tree_model_get (tm, &iter, 1, &nso, -1);
      if (ns != nso)
        GNUNET_FS_namespace_delete (nso, GNUNET_NO);
    }
    while (TRUE == gtk_tree_model_iter_next (tm, &iter));
  }
  transient = gtk_window_get_transient_for (GTK_WINDOW (ad));
  gtk_widget_hide (ad);

  meta = GNUNET_CONTAINER_meta_data_create ();

  nds = GNUNET_malloc (sizeof (struct NamespaceAdvertisementContext));
  nds->ns = ns;
  nds->do_index = FALSE;
  nds->short_fn = NULL;
  nds->anonymity_level = 1;
  nds->priority = 1000;
  nds->root = NULL;
  /* This is a bogus fileinfo. It's needed because edit_publish_dialog
   * was written to work with fileinfo, and return a fileinfo.
   */
  nds->fip = GNUNET_FS_file_information_create_empty_directory (NULL, NULL,
								NULL, meta, &bo);
    memset (&bo, 0, sizeof (bo));
  GNUNET_CONTAINER_meta_data_destroy (meta);
  GNUNET_FS_GTK_edit_publish_dialog (builder, transient, 
				     nds->do_index,
				     nds->short_fn, 
				     nds->anonymity_level, 
				     nds->priority,
				     nds->fip, FALSE, &adv_pseudonym_edit_publish_dialog_cb, nds);
}

void
GNUNET_GTK_select_pseudonym_dialog_realize_cb (GtkWidget *widget,
    gpointer user_data)
{
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  GtkBuilder *builder;

  builder = GTK_BUILDER (user_data);

  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder, "GNUNET_GTK_select_pseudonym_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  g_signal_connect (G_OBJECT (sel), "changed",
                    G_CALLBACK (selection_changed_cb), user_data);
}

/**
 * Advertise pseudonym selected.
 */
void
GNUNET_GTK_main_menu_file_advertise_pseudonym_activate_cb (GtkWidget * dummy,
                                                           gpointer data)
{
  GtkWidget *ad;
  GtkListStore *ls;
  GtkWidget *toplevel;
  GtkBuilder *builder;

  builder = GTK_BUILDER (data);

  ad = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_select_pseudonym_dialog"));
  ls = GTK_LIST_STORE (gtk_builder_get_object
                       (builder, "GNUNET_GTK_select_pseudonym_liststore"));
  gtk_list_store_clear (ls);
  GNUNET_FS_namespace_list (GNUNET_FS_GTK_get_fs_handle (), &add_to_list, ls);
  toplevel = gtk_widget_get_toplevel (dummy);
  if (GTK_IS_WINDOW (toplevel))
    gtk_window_set_transient_for (GTK_WINDOW (ad), GTK_WINDOW (toplevel));
  gtk_window_present (GTK_WINDOW (ad));
}


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