aboutsummaryrefslogtreecommitdiff
path: root/src/common/helper.c
blob: 59c10bf8580a75b95c24f165746c192806b5f8d5 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
     This file is part of GNUnet
     (C) 2003, 2004, 2005 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/common/helper.c
 * @brief This file contains some GUI helper functions
 * @author Igor Wronsky
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunetgtk_common.h"

#define HELPER_DEBUG NO

static GladeXML * mainXML;

static char * gladeFile;

static GtkWidget * infoWindow;

static GtkWidget * infoText;

/**
 * the main thread 
 */
static PTHREAD_T mainThread;

typedef struct {
  Semaphore * sem;
  void * args;
  SimpleCallback func;
} SaveCall;

static SaveCall ** psc;

static unsigned int pscCount;

static Mutex sclock;

static int saveCallsUp;

static gboolean saveCallWrapper(gpointer data) {
  SaveCall * call = data;

  call->func(call->args);
  if (call->sem != NULL)
    SEMAPHORE_UP(call->sem);
  return FALSE;
}

/**
 * Call a callback function from the mainloop/main thread ("SaveCall").
 * Since GTK doesn't work with multi-threaded applications under Windows,
 * all GTK operations have to be done in the main thread
 */
void gtkSaveCall(SimpleCallback func, 
		 void * args) {
  SaveCall call;
  int i;

  MUTEX_LOCK(&sclock);
  if ( (saveCallsUp == NO) ||
       (! PTHREAD_SELF_TEST(&mainThread)) ) {
    call.args = args;
    call.func = func;
    call.sem  = SEMAPHORE_NEW(0);
    GROW(psc,
	 pscCount,
	 pscCount+1);
    psc[pscCount-1] = &call;
    MUTEX_UNLOCK(&sclock);
    gtk_idle_add(&saveCallWrapper, 
		 &call);
    SEMAPHORE_DOWN(call.sem);
    /* remove from psc list */
    MUTEX_LOCK(&sclock);
    for (i=0;i<pscCount;i++) {
      if (psc[i] == &call) {
	psc[i] = psc[pscCount-1];
	break;
      }
    }
    GNUNET_ASSERT(i != pscCount);
    GROW(psc,
	 pscCount,
	 pscCount-1);
    MUTEX_UNLOCK(&sclock);
    SEMAPHORE_FREE(call.sem);
  } else {
    MUTEX_UNLOCK(&sclock);
    func(args);
  }
}

int gtkRunSomeSaveCalls() {
  int i;

  if (! PTHREAD_SELF_TEST(&mainThread))
    return NO;
  MUTEX_LOCK(&sclock);
  if (pscCount == 0) {
    MUTEX_UNLOCK(&sclock);
    return NO;
  }
  i = randomi(pscCount);
  if (TRUE == g_idle_remove_by_data(psc[i]))
    psc[i]->func(psc[i]);
  MUTEX_UNLOCK(&sclock);
  gnunet_util_sleep(50 * cronMILLIS);
  /* sleep here is somewhat important, first of
     all, after completion we need to give the
     semaphore-mechanism time to remove the save-call
     from the list to avoid running it twice; 
     also, this function might be called in a tight
     loop (see search.c), so we should give the
     other threads some time to run.  */

  return YES;
}

/**
 * Callback for handling "delete_event": close the window 
 */
static gint 
deleteEvent(GtkWidget * widget,
	    GdkEvent * event,
	    gpointer data) {
  return FALSE;
}

static void hideWindow(GtkWidget * widget,
		       gpointer data) {
  if (widget != NULL)
    gtk_widget_hide(widget);
}

/**
 * Closure for doInfoMessage.
 */
typedef struct {
  int doPopup;
  char * note;
} InfoMessage;

/**
 * Callback for infoMessage()
 *
 * FIXME: use GLADE (or direct GTK functionality) here!
 */
static void doInfoMessage(void * args) {
  const InfoMessage * info = args;
  GtkTextIter iter;
  GtkTextBuffer * buffer;

  if (! infoWindow) {
    GtkWidget * box1;
    GtkWidget * button;
    GtkWidget * scrolled_window;

    infoWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_signal_connect(GTK_OBJECT(infoWindow),
                       "delete_event",
                       GTK_SIGNAL_FUNC(deleteEvent),
                       NULL);

    gtk_window_set_title(GTK_WINDOW(infoWindow),
                         _("Messages"));
    gtk_widget_set_usize(GTK_WIDGET(infoWindow),
                         780,
                         300);

    box1 = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER (infoWindow),
                      box1);
    gtk_widget_show(box1);
    
    /* create a scrollable window */
    scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
    				   GTK_POLICY_AUTOMATIC,
				   GTK_POLICY_ALWAYS);
    gtk_box_pack_start(GTK_BOX(box1),
    		       scrolled_window,
		       TRUE,
		       TRUE,
		       0);
    gtk_widget_show(scrolled_window);

    /* create a text widget */
    infoText = gtk_text_view_new();
    
    gtk_text_view_set_editable(GTK_TEXT_VIEW (infoText),
			       FALSE);
    gtk_container_add(GTK_CONTAINER(scrolled_window),
    		      infoText);
    gtk_widget_show(infoText);
    gtk_widget_realize(infoText);
  
    /* finish with a close button */
    button = gtk_button_new_with_label(_("Close"));
    gtk_box_pack_start(GTK_BOX (box1),
                       button,
                       FALSE,
                       FALSE,
                       0);
    gtk_signal_connect_object(GTK_OBJECT(button),
                              "clicked",
                              GTK_SIGNAL_FUNC(hideWindow),
                              GTK_OBJECT(infoWindow));
    gtk_signal_connect_object(GTK_OBJECT(infoWindow), 
			      "delete_event",
                              GTK_SIGNAL_FUNC(hideWindow),
                              GTK_OBJECT(infoWindow));
    gtk_signal_connect_object(GTK_OBJECT(infoWindow), 
			      "destroy",
                              GTK_SIGNAL_FUNC(hideWindow),
                              GTK_OBJECT(infoWindow));
    gtk_widget_show(button);
  }
  if (info->doPopup==YES)
    gtk_widget_show(infoWindow);

  /* append the text */
  buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(infoText));
  gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
  gtk_text_buffer_insert(buffer,
			 &iter,
			 info->note, 
			 -1);
}

/** 
 * Appends a message to the info window
 *
 * @param doPopup do we open the window, YES or NO
 */
void infoMessage(int doPopup,
		 const char * format,
		 ...) {
  va_list args;
  InfoMessage info;

  va_start(args, format);
  info.note = g_strdup_vprintf(format, args);
  va_end(args);
  info.doPopup = doPopup;
  gtkSaveCall(&doInfoMessage, 
	      &info);
  g_free(info.note);
}

static void saveAddLogEntry(void * args) {
  static GtkWidget * s = NULL;
  static int once = 1;
  static guint id;

  if (once) {
    once = 0;
    s = glade_xml_get_widget(mainXML,
			     "statusbar");
    id = gtk_statusbar_get_context_id(GTK_STATUSBAR(s),
				      "LOG");
  } else
    gtk_statusbar_pop(GTK_STATUSBAR(s),
		      id);
  gtk_statusbar_push(GTK_STATUSBAR(s),
		     id,
		     (const char*) args);
}

/** 
 * Appends a log entry to the info window
 *
 * @param txt the log entry
 *
 */
void addLogEntry(const char * txt) {
  infoMessage(NO, txt);
  gtkSaveCall(&saveAddLogEntry,
	      (void*) txt);
}

/**
 * Simple accessor method.
 */
const char * getGladeFileName() {
  return gladeFile;
}

/**
 * Simple accessor method.
 */
GladeXML * getMainXML() {
  return mainXML;
}

void initGNUnetGTKCommon() {
  MUTEX_CREATE_RECURSIVE(&sclock);
  PTHREAD_GET_SELF(&mainThread);
  saveCallsUp = YES;

  /* load the interface */
#ifdef MINGW
  gladeFile = MALLOC(_MAX_PATH + 1);
  plibc_conv_to_win_path(PACKAGE_DATA_DIR"/gnunet-gtk.glade", gladeFile);
#else
  gladeFile = STRDUP(PACKAGE_DATA_DIR"/gnunet-gtk.glade");
#endif

  mainXML = glade_xml_new(gladeFile,
			  "mainWindow",
			  NULL);
}

void doneGNUnetGTKCommon() {
  int i;

  UNREF(mainXML);
  mainXML = NULL;
  FREE(gladeFile);
  gladeFile = NULL;

  saveCallsUp = NO;
  PTHREAD_REL_SELF(&mainThread);
  MUTEX_LOCK(&sclock);
  for (i=0;i<pscCount;i++) 
    psc[i]->func(psc[i]);
  i = pscCount;
  MUTEX_UNLOCK(&sclock);  
  /* wait until all PSC-jobs have left
     the gtkSaveCall method before destroying
     the mutex! */
  while (i != 0) {
    gnunet_util_sleep(50 * cronMILLIS);    
    MUTEX_LOCK(&sclock);
    i = pscCount;
    MUTEX_UNLOCK(&sclock);
  }
  MUTEX_DESTROY(&sclock);

}

/* end of helper.c */