aboutsummaryrefslogtreecommitdiff
path: root/src/setup/gnunet-setup-hostlist-server.c
blob: 0a9523e16fb3dc0fc5ce9b0fdbf1f5224f7c92ae (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
/*
     This file is part of GNUnet.
     Copyright (C) 2012 GNUnet e.V.

     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 3, 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., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file src/setup/gnunet-setup-hostlist-server.c
 * @brief (de)sensitize UI elements related to hostlist server configuration
 *        based on the build configuration of the hostlist daemon
 * @author Christian Grothoff
 */
#include "gnunet-setup.h"
#include <gnunet/gnunet_util_lib.h>


/**
 * Timeout for hostlist daemon to print help information
 */
#define CMD_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)


/**
 * Function called to report the result of testing the hostlist daemon.
 *
 * @param cls closure
 * @param result GNUNET_YES if the hostlist daemon has an integrated hostlist
 *               server, GNUNET_NO otherwise
 */
typedef void (*TestHostlistDaemonCallback) (void *cls,
                                            int result);


/**
 * Context for running hostlist daemon and processing its output.
 */
struct CommandContext
{
  /**
   * Handle to the command.
   */
  struct GNUNET_OS_CommandHandle *cmd;

  /**
   * Where to pass the result.
   */
  TestHostlistDaemonCallback callback;

  /**
   * Closure for the callback.
   */
  void *callback_cls;

  /**
   * GNUNET_YES if the target argument was found, GNUNET_NO otherwise.
   */
  int found;

};


static void
set_checkbutton_status (void *cls,
                        int result)
{
  GtkWidget * widget = cls;
  GtkToggleButton *button;

  if (GNUNET_YES != result)
  {
    gtk_widget_set_sensitive (widget, FALSE);
    button = GTK_TOGGLE_BUTTON (widget);
    if (button == NULL)
    {
      GNUNET_break (0);
      return;
    }
    /* also deactivate the checkbutton since this option could be enabled
       by editing the config file manually */
    gtk_toggle_button_set_active (button, FALSE);
  }
}


static void
set_spinbutton_status (void *cls,
                       int result)
{
  GtkWidget * widget = cls;

  if (GNUNET_YES != result)
    gtk_widget_set_sensitive (widget, FALSE);
}


/**
 * Process the output from the 'gnunet-daemon-hostlist -h' command
 * to find out whether the hostlist daemon can provide a hostlist server.
 *
 * @param cls the 'struct CommandContext'
 * @param line line of output, NULL at the end
 */
static void
process_hostlist_daemon_output (void *cls, const char *line)
{
  struct CommandContext *ctx = cls;
  char *t;
  char *w;

  if (NULL == line)
  {
    ctx->callback (ctx->callback_cls, ctx->found);
    GNUNET_OS_command_stop (ctx->cmd);
    GNUNET_free (ctx);
    return;
  }

  t = GNUNET_strdup (line);
  w = strtok (t, " ");
  while (w != NULL)
  {
    if (0 == strcmp (w, "--provide-hostlist"))
    {
      ctx->found = GNUNET_YES;
      break;
    }
    w = strtok (NULL, " ");
  }
  GNUNET_free (t);
}


/**
 * Run 'gnunet-daemon-hostlist -h'. Output of this command will let us
 * know whether the hostlist daemon can provide a hostlist server.
 *
 * @param callback function to call with the result
 * @param cls closure for the callback
 */
static void
test_hostlist_daemon (TestHostlistDaemonCallback callback,
                      void *cls)
{
  struct CommandContext *ctx;
  char *binary;

  ctx = GNUNET_new (struct CommandContext);
  ctx->callback = callback;
  ctx->callback_cls = cls;
  ctx->found = GNUNET_NO;
  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-daemon-hostlist");
  ctx->cmd = GNUNET_OS_command_run (&process_hostlist_daemon_output,
                                    ctx,
                                    CMD_TIMEOUT,
                                    binary,
                                    "gnunet-daemon-hostlist",
                                    "-h",
                                    NULL);
  GNUNET_free (binary);
  if (NULL == ctx->cmd)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Could not determine whether the hostlist daemon has"
                  " an integrated hostlist server!\n"));
    GNUNET_free (ctx);
  }
}


void
GNUNET_setup_hostlist_offer_hostlist_checkbutton_realize_cb (GtkWidget * widget,
                                                             gpointer user_data)
{
  test_hostlist_daemon (&set_checkbutton_status, widget);
}


void
GNUNET_setup_hostlist_advertise_checkbutton_realize_cb (GtkWidget * widget,
                                                        gpointer user_data)
{
  test_hostlist_daemon (&set_checkbutton_status, widget);
}


void
GNUNET_setup_hostlist_server_port_spin_button_realize_cb (GtkWidget * widget,
                                                          gpointer user_data)
{
  test_hostlist_daemon (&set_spinbutton_status, widget);
}


/* end of gnunet-setup-hostlist-server.c */