| 1 |
/* |
| 2 |
* ConnMan GTK GUI |
| 3 |
* |
| 4 |
* Copyright (C) 2015 Intel Corporation. All rights reserved. |
| 5 |
* Author: Jaakko Hannikainen <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <glib.h> |
| 22 |
#include <glib/gi18n.h> |
| 23 |
#include <gtk/gtk.h> |
| 24 |
|
| 25 |
#include "dialog.h" |
| 26 |
#include "main.h" |
| 27 |
#include "style.h" |
| 28 |
|
| 29 |
static struct token_element *create_new(enum token_element_type type, |
| 30 |
const gchar *name, GtkWidget *content) |
| 31 |
{ |
| 32 |
struct token_element *elem = g_malloc(sizeof(*elem)); |
| 33 |
|
| 34 |
elem->type = type; |
| 35 |
elem->value = NULL; |
| 36 |
elem->name = g_strdup(name); |
| 37 |
|
| 38 |
if(strchr(name, '_')) { |
| 39 |
elem->label = gtk_label_new_with_mnemonic(name); |
| 40 |
gtk_label_set_mnemonic_widget(GTK_LABEL(elem->label), content); |
| 41 |
} |
| 42 |
else |
| 43 |
elem->label = gtk_label_new(name); |
| 44 |
elem->content = content; |
| 45 |
|
| 46 |
style_set_margin(elem->label, MARGIN_LARGE); |
| 47 |
style_set_margin(elem->content, MARGIN_LARGE); |
| 48 |
gtk_widget_set_margin_top(elem->label, MARGIN_MEDIUM); |
| 49 |
gtk_widget_set_margin_bottom(elem->label, MARGIN_MEDIUM); |
| 50 |
gtk_widget_set_margin_top(elem->content, MARGIN_MEDIUM); |
| 51 |
gtk_widget_set_margin_bottom(elem->content, MARGIN_MEDIUM); |
| 52 |
|
| 53 |
return elem; |
| 54 |
} |
| 55 |
|
| 56 |
struct token_element *token_new_text(const gchar *name, const gchar *value) |
| 57 |
{ |
| 58 |
GtkWidget *content; |
| 59 |
struct token_element *elem; |
| 60 |
|
| 61 |
content = gtk_label_new(value); |
| 62 |
elem = create_new(TOKEN_ELEMENT_TEXT, name, content); |
| 63 |
|
| 64 |
return elem; |
| 65 |
} |
| 66 |
|
| 67 |
static void check_entry(GtkWidget *entry, gpointer user_data) |
| 68 |
{ |
| 69 |
gboolean valid = TRUE; |
| 70 |
gboolean (*check)(GtkWidget *entry); |
| 71 |
GtkDialog *window; |
| 72 |
GtkStyleContext *context; |
| 73 |
|
| 74 |
check = g_object_get_data(G_OBJECT(entry), "check"); |
| 75 |
window = g_object_get_data(G_OBJECT(entry), "window"); |
| 76 |
context = gtk_widget_get_style_context(entry); |
| 77 |
if(check) |
| 78 |
valid = check(entry); |
| 79 |
|
| 80 |
if(!valid) { |
| 81 |
gtk_style_context_add_class(context, "error"); |
| 82 |
gtk_dialog_set_response_sensitive(window, |
| 83 |
GTK_RESPONSE_ACCEPT, FALSE); |
| 84 |
} else { |
| 85 |
gtk_style_context_remove_class(context, "error"); |
| 86 |
gtk_dialog_set_response_sensitive(window, |
| 87 |
GTK_RESPONSE_ACCEPT, TRUE); |
| 88 |
} |
| 89 |
} |
| 90 |
struct token_element *token_new_entry_full(const gchar *name, gboolean secret, |
| 91 |
const gchar *value, |
| 92 |
gboolean (*check)(GtkWidget *entry)) |
| 93 |
{ |
| 94 |
GtkWidget *content; |
| 95 |
struct token_element *elem; |
| 96 |
|
| 97 |
content = gtk_entry_new(); |
| 98 |
elem = create_new(TOKEN_ELEMENT_ENTRY, name, content); |
| 99 |
gtk_entry_set_activates_default(GTK_ENTRY(content), TRUE); |
| 100 |
|
| 101 |
if(secret && strcmp(name, "Name") && strcmp(name, "Identity") && |
| 102 |
strcmp(name, "WPS") && strcmp(name, "Username") && |
| 103 |
strcmp(name, "Host") && strcmp(name, "OpenConnect.CACert") && |
| 104 |
strcmp(name, "OpenConnect.ServerCert") && |
| 105 |
strcmp(name, "OpenConnect.VPNHost")) |
| 106 |
gtk_entry_set_visibility(GTK_ENTRY(content), FALSE); |
| 107 |
|
| 108 |
if(value) |
| 109 |
gtk_entry_set_text(GTK_ENTRY(content), value); |
| 110 |
|
| 111 |
g_object_set_data(G_OBJECT(content), "check", check); |
| 112 |
g_signal_connect(content, "changed", G_CALLBACK(check_entry), NULL); |
| 113 |
|
| 114 |
return elem; |
| 115 |
} |
| 116 |
|
| 117 |
struct token_element *token_new_entry(const gchar *name, gboolean secret) |
| 118 |
{ |
| 119 |
return token_new_entry_full(name, secret, NULL, NULL); |
| 120 |
} |
| 121 |
|
| 122 |
struct token_element *token_new_list(const gchar *name, GPtrArray *options) |
| 123 |
{ |
| 124 |
GtkComboBoxText *box; |
| 125 |
GtkWidget *content; |
| 126 |
struct token_element *elem; |
| 127 |
int i; |
| 128 |
|
| 129 |
content = gtk_combo_box_text_new(); |
| 130 |
elem = create_new(TOKEN_ELEMENT_LIST, name, content); |
| 131 |
box = GTK_COMBO_BOX_TEXT(content); |
| 132 |
|
| 133 |
for(i = 0; i < options->len; i++) |
| 134 |
gtk_combo_box_text_append_text(box, options->pdata[i]); |
| 135 |
|
| 136 |
return elem; |
| 137 |
} |
| 138 |
|
| 139 |
struct token_element *token_new_checkbox(const gchar *name, gboolean state) |
| 140 |
{ |
| 141 |
GtkWidget *content; |
| 142 |
struct token_element *elem; |
| 143 |
|
| 144 |
content = gtk_check_button_new(); |
| 145 |
elem = create_new(TOKEN_ELEMENT_CHECKBOX, name, content); |
| 146 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(content), state); |
| 147 |
|
| 148 |
return elem; |
| 149 |
} |
| 150 |
|
| 151 |
gchar *get_element_value(struct token_element *elem) |
| 152 |
{ |
| 153 |
gchar *value = NULL; |
| 154 |
if(elem->type == TOKEN_ELEMENT_ENTRY) { |
| 155 |
GtkEntryBuffer *buf; |
| 156 |
|
| 157 |
buf = gtk_entry_get_buffer(GTK_ENTRY(elem->content)); |
| 158 |
value = g_strdup(gtk_entry_buffer_get_text(buf)); |
| 159 |
} else if(elem->type == TOKEN_ELEMENT_LIST) { |
| 160 |
GtkComboBoxText *box = GTK_COMBO_BOX_TEXT(elem->content); |
| 161 |
value = gtk_combo_box_text_get_active_text(box); |
| 162 |
} else if(elem->type == TOKEN_ELEMENT_CHECKBOX) { |
| 163 |
GtkToggleButton *button = GTK_TOGGLE_BUTTON(elem->content); |
| 164 |
value = GINT_TO_POINTER(gtk_toggle_button_get_active(button)); |
| 165 |
} |
| 166 |
|
| 167 |
return value; |
| 168 |
} |
| 169 |
|
| 170 |
struct token_window_params { |
| 171 |
GPtrArray *elements; |
| 172 |
const gchar *title; |
| 173 |
GCond *cond; |
| 174 |
GMutex *mutex; |
| 175 |
gboolean returned; |
| 176 |
gint value; |
| 177 |
}; |
| 178 |
|
| 179 |
static gboolean ask_tokens_sync(gpointer data) |
| 180 |
{ |
| 181 |
struct token_window_params *params = data; |
| 182 |
GPtrArray *elements = params->elements; |
| 183 |
|
| 184 |
GtkDialog *dialog; |
| 185 |
GtkWidget *grid, *window; |
| 186 |
int i; |
| 187 |
int flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL; |
| 188 |
|
| 189 |
grid = gtk_grid_new(); |
| 190 |
window = gtk_dialog_new_with_buttons(params->title, |
| 191 |
GTK_WINDOW(main_window), flags, |
| 192 |
_("_OK"), GTK_RESPONSE_ACCEPT, |
| 193 |
_("_Cancel"), GTK_RESPONSE_CANCEL, |
| 194 |
NULL); |
| 195 |
gtk_dialog_set_default_response(GTK_DIALOG(window), |
| 196 |
GTK_RESPONSE_ACCEPT); |
| 197 |
for(i = 0; i < elements->len; i++) { |
| 198 |
struct token_element *elem = g_ptr_array_index(elements, i); |
| 199 |
gtk_grid_attach(GTK_GRID(grid), elem->label, 0, i, 1, 1); |
| 200 |
gtk_grid_attach(GTK_GRID(grid), elem->content, 1, i, 1, 1); |
| 201 |
g_object_set_data(G_OBJECT(elem->content), "window", window); |
| 202 |
if(elem->type == TOKEN_ELEMENT_ENTRY) |
| 203 |
check_entry(elem->content, NULL); |
| 204 |
} |
| 205 |
|
| 206 |
gtk_widget_show_all(grid); |
| 207 |
dialog = GTK_DIALOG(window); |
| 208 |
gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(dialog)), |
| 209 |
grid); |
| 210 |
|
| 211 |
i = gtk_dialog_run(GTK_DIALOG(window)); |
| 212 |
|
| 213 |
params->value = i == GTK_RESPONSE_ACCEPT; |
| 214 |
if(i != GTK_RESPONSE_ACCEPT) |
| 215 |
goto out; |
| 216 |
|
| 217 |
for(i = 0; i < elements->len; i++) { |
| 218 |
struct token_element *elem = elements->pdata[i]; |
| 219 |
|
| 220 |
elem->value = get_element_value(elem); |
| 221 |
} |
| 222 |
|
| 223 |
out: |
| 224 |
gtk_widget_destroy(window); |
| 225 |
g_mutex_lock(params->mutex); |
| 226 |
params->returned = TRUE; |
| 227 |
g_cond_signal(params->cond); |
| 228 |
g_mutex_unlock(params->mutex); |
| 229 |
return FALSE; |
| 230 |
} |
| 231 |
|
| 232 |
gboolean dialog_ask_tokens(const gchar *title, GPtrArray *elements) |
| 233 |
{ |
| 234 |
struct token_window_params params = {}; |
| 235 |
GMutex mutex; |
| 236 |
GCond cond; |
| 237 |
|
| 238 |
params.elements = elements; |
| 239 |
params.title = title; |
| 240 |
params.mutex = &mutex; |
| 241 |
params.cond = &cond; |
| 242 |
g_mutex_init(&mutex); |
| 243 |
g_cond_init(&cond); |
| 244 |
g_main_context_invoke(NULL, (GSourceFunc)ask_tokens_sync, ¶ms); |
| 245 |
g_mutex_lock(&mutex); |
| 246 |
while(!params.returned) |
| 247 |
g_cond_wait(&cond, &mutex); |
| 248 |
g_mutex_unlock(&mutex); |
| 249 |
g_mutex_clear(&mutex); |
| 250 |
g_cond_clear(&cond); |
| 251 |
return !!params.value; |
| 252 |
} |
| 253 |
|
| 254 |
void free_token_element(struct token_element *elem) |
| 255 |
{ |
| 256 |
g_free(elem->name); |
| 257 |
if(elem->type != TOKEN_ELEMENT_CHECKBOX) |
| 258 |
g_free(elem->value); |
| 259 |
g_free(elem); |
| 260 |
} |
| 261 |
|
| 262 |
struct error_params { |
| 263 |
gchar *text; |
| 264 |
gchar *log; |
| 265 |
}; |
| 266 |
|
| 267 |
static gboolean show_error_sync(gpointer data) |
| 268 |
{ |
| 269 |
struct error_params *params = data; |
| 270 |
|
| 271 |
static const int max_log_length = 100; |
| 272 |
|
| 273 |
const gchar *title; |
| 274 |
int flags; |
| 275 |
GtkWidget *window, *area, *grid, *text, *log, *box = NULL; |
| 276 |
|
| 277 |
grid = gtk_grid_new(); |
| 278 |
|
| 279 |
if(params->log) |
| 280 |
g_strstrip(params->log); |
| 281 |
|
| 282 |
text = gtk_label_new(params->text); |
| 283 |
title = _("Operation failed"); |
| 284 |
flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL; |
| 285 |
window = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_window), |
| 286 |
flags, |
| 287 |
_("_OK"), GTK_RESPONSE_NONE, NULL); |
| 288 |
gtk_dialog_set_default_response(GTK_DIALOG(window), GTK_RESPONSE_NONE); |
| 289 |
|
| 290 |
gtk_widget_set_halign(text, GTK_ALIGN_START); |
| 291 |
style_set_margin(text, MARGIN_LARGE); |
| 292 |
gtk_grid_attach(GTK_GRID(grid), text, 0, 0, 1, 1); |
| 293 |
|
| 294 |
if(params->log) { |
| 295 |
gtk_widget_set_margin_bottom(text, 0); |
| 296 |
|
| 297 |
log = gtk_label_new(params->log); |
| 298 |
style_set_margin(log, MARGIN_LARGE); |
| 299 |
gtk_widget_set_hexpand(log, TRUE); |
| 300 |
gtk_widget_set_vexpand(log, TRUE); |
| 301 |
|
| 302 |
if(strlen(params->log) >= max_log_length) { |
| 303 |
label_align_text(GTK_LABEL(log), 0, 0.5); |
| 304 |
GtkStyleContext *context; |
| 305 |
GtkScrolledWindow *scroll; |
| 306 |
|
| 307 |
box = gtk_scrolled_window_new(NULL, NULL); |
| 308 |
scroll = GTK_SCROLLED_WINDOW(box); |
| 309 |
gtk_scrolled_window_set_policy(scroll, |
| 310 |
GTK_POLICY_NEVER, |
| 311 |
GTK_POLICY_AUTOMATIC); |
| 312 |
style_set_margin(log, 0); |
| 313 |
style_set_margin(box, MARGIN_LARGE); |
| 314 |
gtk_widget_set_margin_top(box, 0); |
| 315 |
style_add_context(log); |
| 316 |
context = gtk_widget_get_style_context(log); |
| 317 |
gtk_style_context_add_class(context, "cm-log"); |
| 318 |
gtk_scrolled_window_set_shadow_type(scroll, |
| 319 |
GTK_SHADOW_IN); |
| 320 |
|
| 321 |
gtk_container_add(GTK_CONTAINER(box), log); |
| 322 |
gtk_grid_attach(GTK_GRID(grid), box, 0, 1, 1, 1); |
| 323 |
} else |
| 324 |
gtk_grid_attach(GTK_GRID(grid), log, 0, 1, 1, 1); |
| 325 |
} |
| 326 |
else |
| 327 |
log = NULL; |
| 328 |
|
| 329 |
gtk_widget_show_all(grid); |
| 330 |
|
| 331 |
area = gtk_dialog_get_content_area(GTK_DIALOG(window)); |
| 332 |
gtk_container_add(GTK_CONTAINER(area), grid); |
| 333 |
|
| 334 |
if(box) { |
| 335 |
int mwidth, width, mheight, height; |
| 336 |
const int max_height = 300; |
| 337 |
GtkScrolledWindow *scroll; |
| 338 |
|
| 339 |
scroll = GTK_SCROLLED_WINDOW(box); |
| 340 |
gtk_widget_get_preferred_width(log, &mwidth, &width); |
| 341 |
if(mwidth > width) |
| 342 |
width = mwidth; |
| 343 |
gtk_widget_get_preferred_height(log, &mheight, &height); |
| 344 |
if(mheight > height) |
| 345 |
height = mheight; |
| 346 |
if(height > max_height) |
| 347 |
height = max_height; |
| 348 |
gtk_scrolled_window_set_min_content_width(scroll, width); |
| 349 |
gtk_scrolled_window_set_min_content_height(scroll, height); |
| 350 |
} else |
| 351 |
gtk_window_set_resizable(GTK_WINDOW(window), FALSE); |
| 352 |
|
| 353 |
gtk_dialog_run(GTK_DIALOG(window)); |
| 354 |
|
| 355 |
gtk_widget_destroy(window); |
| 356 |
|
| 357 |
g_free(params->text); |
| 358 |
g_free(params->log); |
| 359 |
g_free(params); |
| 360 |
|
| 361 |
return FALSE; |
| 362 |
} |
| 363 |
|
| 364 |
void show_error(const gchar *text, const gchar *log) |
| 365 |
{ |
| 366 |
struct error_params *params = g_malloc(sizeof(*params)); |
| 367 |
params->text = g_strdup(text); |
| 368 |
if(log) |
| 369 |
params->log = g_strdup(log); |
| 370 |
else |
| 371 |
params->log = NULL; |
| 372 |
g_main_context_invoke(NULL, show_error_sync, params); |
| 373 |
} |