aboutsummaryrefslogtreecommitdiff
path: root/src/fs.c
blob: 114efe0ffc35543756c0ca8c6c9f29e44141a3c5 (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
/*
     This file is part of GNUnet.
     (C) 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/fs.c
 * @brief main file-sharing code of gnunet-gtk
 * @author Christian Grothoff
 */

#include "fs.h"
#include "helper.h"
#include "download.h"
#include "search.h"
#include "upload.h"
#include <GNUnet/gnunet_fsui_lib.h>

struct FSUI_Context * ctx;

static gint saveEventProcessor(SaveCall * call) {
  const FSUI_Event * event;

  event = (const FSUI_Event *) call->args;
  switch (event->type) {
  case FSUI_search_result:
    displaySearchResult(&event->data.SearchResult.fi,
			event->data.SearchResult.searchURI,
			NULL);
    break;
  case FSUI_search_error:
    LOG(LOG_ERROR,
	_("Error while searching: %s\n"),
	event->data.message);
    break;
  case FSUI_download_aborted:
    /* ignore for now */
    break;
  case FSUI_download_progress:
    displayDownloadUpdate(event->data.DownloadProgress.uri,
			  event->data.DownloadProgress.completed,
			  event->data.DownloadProgress.last_block,
			  event->data.DownloadProgress.last_size);
    /* FIXME: 
       if directory, also update search view! */
    break;
  case FSUI_download_complete:
    displayDownloadComplete(event->data.DownloadProgress.uri,
			    event->data.DownloadProgress.filename);
    /* FIXME: 
       if directory, also update search view! */
    break;
  case FSUI_download_error:
    BREAK();
    LOG(LOG_ERROR,
	_("Error while downloading: %s\n"),
	event->data.message);
    break;
  case FSUI_upload_progress:
    displayUploadUpdate(event->data.UploadProgress.main_filename,
			event->data.UploadProgress.filename,
			event->data.UploadProgress.completed,
			event->data.UploadProgress.total);
    displayUploadUpdate(event->data.UploadProgress.main_filename,
			event->data.UploadProgress.main_filename,
			event->data.UploadProgress.main_completed,
			event->data.UploadProgress.main_total);
    break;
  case FSUI_upload_complete:
    displayUploadComplete(event->data.UploadComplete.main_filename,
			  event->data.UploadComplete.filename,
			  event->data.UploadComplete.uri);
    break;
  case FSUI_upload_error:
    LOG(LOG_ERROR,
	_("Error while uploading: %s\n"),
	event->data.message);
    break;
  case FSUI_gnunetd_connected:
    LOG(LOG_MESSAGE,
	_("Connected to gnunetd.\n"));
    break;
  case FSUI_gnunetd_disconnected:
    LOG(LOG_MESSAGE,
	_("Disconnected from gnunetd.\n"));
    break;
  default:
    BREAK();
    LOG(LOG_ERROR,
	_("Unhandled (unknown) FSUI event: %u.\n"),
	event->type);
    break;
  }

  gtkSaveCallDone(call->sem);
  return FALSE;
}

/**
 * FSUI event handler.
 */
static void eventProcessor(void * cls,
			   const FSUI_Event * event) {
  gtkSaveCall((GtkFunction) &saveEventProcessor,
	      (void*) event);
}

void gtk_fs_init() {
  ctx = FSUI_start("gnunet-gtk",
		   YES,
		   &eventProcessor,
		   NULL);
  fs_search_start();
  fs_download_start();
  fs_upload_start();
}

static void * shutdownCode(Semaphore * sig) {
  fs_download_stop();
  fs_search_stop();
  FSUI_stop(ctx);
  fs_upload_stop();
  SEMAPHORE_UP(sig);
  return NULL;
}

void gtk_fs_done() {
  PTHREAD_T doneThread;
  Semaphore * sig;
  void * unused;

  sig = SEMAPHORE_NEW(0);
  if (0 != PTHREAD_CREATE(&doneThread,
			  (PThreadMain)&shutdownCode,
			  sig,
			  8*1024))
    DIE_STRERROR("pthread_create");
  while (OK != SEMAPHORE_DOWN_NONBLOCKING(sig))
    gtkRunSomeSaveCalls();
  PTHREAD_JOIN(&doneThread,
	       &unused);
  SEMAPHORE_FREE(sig);
}

/* end of fs.c */