| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <connman-ui-gtk.h> |
| 23 |
#include <gtkservice.h> |
| 24 |
|
| 25 |
struct _GtkServicePrivate { |
| 26 |
GtkBox *box; |
| 27 |
|
| 28 |
GtkLabel *name; |
| 29 |
|
| 30 |
GtkImage *state; |
| 31 |
GtkImage *security; |
| 32 |
GtkImage *signal; |
| 33 |
|
| 34 |
gboolean selected; |
| 35 |
}; |
| 36 |
|
| 37 |
static void gtk_service_destroy(GtkWidget *widget); |
| 38 |
static void gtk_service_class_init(GtkServiceClass *klass); |
| 39 |
static void gtk_service_init(GtkService *service); |
| 40 |
static void gtk_service_dispose(GObject *object); |
| 41 |
static gboolean gtk_service_button_release_event(GtkWidget *widget, |
| 42 |
GdkEventButton *event); |
| 43 |
|
| 44 |
G_DEFINE_TYPE(GtkService, gtk_service, GTK_TYPE_MENU_ITEM); |
| 45 |
|
| 46 |
static void gtk_service_class_init(GtkServiceClass *klass) |
| 47 |
{ |
| 48 |
GObjectClass *object_class; |
| 49 |
GtkWidgetClass *widget_class; |
| 50 |
GtkMenuItemClass *menu_item_class; |
| 51 |
|
| 52 |
object_class = G_OBJECT_CLASS(klass); |
| 53 |
widget_class = GTK_WIDGET_CLASS(klass); |
| 54 |
menu_item_class = GTK_MENU_ITEM_CLASS(klass); |
| 55 |
|
| 56 |
object_class->dispose = gtk_service_dispose; |
| 57 |
|
| 58 |
widget_class->destroy = gtk_service_destroy; |
| 59 |
widget_class->button_release_event = |
| 60 |
gtk_service_button_release_event; |
| 61 |
|
| 62 |
menu_item_class->hide_on_activate = FALSE; |
| 63 |
|
| 64 |
g_type_class_add_private(object_class, |
| 65 |
sizeof(GtkServicePrivate)); |
| 66 |
} |
| 67 |
|
| 68 |
static void gtk_service_init(GtkService *service) |
| 69 |
{ |
| 70 |
GtkServicePrivate *priv; |
| 71 |
|
| 72 |
priv = G_TYPE_INSTANCE_GET_PRIVATE(service, |
| 73 |
GTK_TYPE_SERVICE, |
| 74 |
GtkServicePrivate); |
| 75 |
service->priv = priv; |
| 76 |
|
| 77 |
priv->box = (GtkBox *) gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10); |
| 78 |
|
| 79 |
priv->name = (GtkLabel *) gtk_label_new(NULL); |
| 80 |
priv->state = (GtkImage *) gtk_image_new(); |
| 81 |
priv->security = (GtkImage *) gtk_image_new(); |
| 82 |
priv->signal = (GtkImage *) gtk_image_new(); |
| 83 |
|
| 84 |
priv->selected = FALSE; |
| 85 |
|
| 86 |
|
| 87 |
gtk_widget_set_halign((GtkWidget *)priv->name, GTK_ALIGN_START); |
| 88 |
|
| 89 |
gtk_widget_set_halign((GtkWidget *)priv->state, GTK_ALIGN_END); |
| 90 |
gtk_widget_set_halign((GtkWidget *)priv->security, GTK_ALIGN_END); |
| 91 |
gtk_widget_set_halign((GtkWidget *)priv->signal, GTK_ALIGN_END); |
| 92 |
|
| 93 |
gtk_box_set_homogeneous(priv->box, FALSE); |
| 94 |
|
| 95 |
gtk_box_pack_start(priv->box, |
| 96 |
(GtkWidget *)priv->name, TRUE, TRUE, 0); |
| 97 |
|
| 98 |
gtk_box_pack_start(priv->box, |
| 99 |
(GtkWidget *) priv->state, TRUE, TRUE, 0); |
| 100 |
gtk_box_pack_start(priv->box, |
| 101 |
(GtkWidget *) priv->security, TRUE, TRUE, 0); |
| 102 |
gtk_box_pack_start(priv->box, |
| 103 |
(GtkWidget *) priv->signal, TRUE, TRUE, 0); |
| 104 |
|
| 105 |
gtk_widget_set_visible((GtkWidget *)priv->box, TRUE); |
| 106 |
gtk_widget_set_visible((GtkWidget *)priv->name, TRUE); |
| 107 |
|
| 108 |
gtk_widget_set_visible((GtkWidget *)priv->state, FALSE); |
| 109 |
gtk_widget_set_visible((GtkWidget *)priv->security, FALSE); |
| 110 |
gtk_widget_set_visible((GtkWidget *)priv->signal, FALSE); |
| 111 |
|
| 112 |
gtk_container_add(GTK_CONTAINER(service), (GtkWidget *)priv->box); |
| 113 |
|
| 114 |
gtk_widget_set_can_focus((GtkWidget *)priv->box, TRUE); |
| 115 |
} |
| 116 |
|
| 117 |
static void gtk_service_dispose(GObject *object) |
| 118 |
{ |
| 119 |
(*G_OBJECT_CLASS(gtk_service_parent_class)->dispose)(object); |
| 120 |
} |
| 121 |
|
| 122 |
static void gtk_service_destroy(GtkWidget *widget) |
| 123 |
{ |
| 124 |
GtkService *service = GTK_SERVICE(widget); |
| 125 |
GtkServicePrivate *priv = service->priv; |
| 126 |
|
| 127 |
if (priv != NULL && priv->selected == FALSE) { |
| 128 |
|
| 129 |
connman_service_set_property_changed_callback(service->path, |
| 130 |
NULL, service); |
| 131 |
connman_technology_set_property_error_callback(service->path, |
| 132 |
NULL, service); |
| 133 |
} |
| 134 |
|
| 135 |
GTK_WIDGET_CLASS(gtk_service_parent_class)->destroy(widget); |
| 136 |
} |
| 137 |
|
| 138 |
static gboolean gtk_service_button_release_event(GtkWidget *widget, |
| 139 |
GdkEventButton *event) |
| 140 |
{ |
| 141 |
GtkService *service = GTK_SERVICE(widget); |
| 142 |
GtkWidget *parent; |
| 143 |
|
| 144 |
if (event->button != 1 && event->button != 3) |
| 145 |
goto activate; |
| 146 |
|
| 147 |
if (event->button == 1) { |
| 148 |
if (connman_service_is_connected(service->path) == TRUE) |
| 149 |
connman_service_disconnect(service->path); |
| 150 |
else { |
| 151 |
cui_agent_set_selected_service(service->path, |
| 152 |
connman_service_get_name(service->path)); |
| 153 |
connman_service_connect(service->path); |
| 154 |
} |
| 155 |
} else if (event->button == 3) { |
| 156 |
service->priv->selected = TRUE; |
| 157 |
|
| 158 |
cui_settings_popup(service->path); |
| 159 |
} |
| 160 |
|
| 161 |
activate: |
| 162 |
parent = gtk_widget_get_parent (widget); |
| 163 |
if (parent != NULL && GTK_IS_MENU_SHELL(parent) == TRUE) { |
| 164 |
GtkMenuShell *menu_shell = GTK_MENU_SHELL(parent); |
| 165 |
gtk_menu_shell_activate_item(menu_shell, widget, TRUE); |
| 166 |
} |
| 167 |
|
| 168 |
return TRUE; |
| 169 |
} |
| 170 |
|
| 171 |
static void service_property_changed_cb(const char *path, |
| 172 |
const char *property, void *user_data) |
| 173 |
{ |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
static void service_set_state(GtkService *service) |
| 178 |
{ |
| 179 |
GtkServicePrivate *priv = service->priv; |
| 180 |
const struct connman_ipv4 *ipv4; |
| 181 |
enum connman_state state; |
| 182 |
GdkPixbuf *image = NULL; |
| 183 |
const char *ip = NULL; |
| 184 |
const char *info; |
| 185 |
|
| 186 |
if (connman_service_is_connected(service->path) == FALSE) { |
| 187 |
gtk_widget_set_tooltip_text((GtkWidget *)priv->name, ""); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
ipv4 = connman_service_get_ipv4(service->path); |
| 192 |
if (ipv4 == NULL) { |
| 193 |
const struct connman_ipv6 *ipv6; |
| 194 |
|
| 195 |
ipv6 = connman_service_get_ipv6(service->path); |
| 196 |
if (ipv6 != NULL) |
| 197 |
ip = ipv6->address; |
| 198 |
} else |
| 199 |
ip = ipv4->address; |
| 200 |
|
| 201 |
if (ip == NULL) |
| 202 |
ip = ""; |
| 203 |
|
| 204 |
gtk_widget_set_tooltip_text((GtkWidget *)priv->name, ip); |
| 205 |
|
| 206 |
state = connman_service_get_state(service->path); |
| 207 |
cui_theme_get_state_icone_and_info(state, &image, &info); |
| 208 |
|
| 209 |
if (image == NULL) |
| 210 |
return; |
| 211 |
|
| 212 |
gtk_widget_set_visible((GtkWidget *)priv->state, TRUE); |
| 213 |
gtk_widget_set_tooltip_text((GtkWidget *)priv->state, info); |
| 214 |
gtk_image_set_from_pixbuf(priv->state, image); |
| 215 |
} |
| 216 |
|
| 217 |
static void service_set_signal(GtkService *service) |
| 218 |
{ |
| 219 |
GtkServicePrivate *priv = service->priv; |
| 220 |
GdkPixbuf *image = NULL; |
| 221 |
const char *type, *info; |
| 222 |
|
| 223 |
type = connman_service_get_type(service->path); |
| 224 |
|
| 225 |
if (g_strcmp0(type, "wifi") == 0) { |
| 226 |
uint8_t strength; |
| 227 |
|
| 228 |
strength = connman_service_get_strength(service->path); |
| 229 |
|
| 230 |
cui_theme_get_signal_icone_and_info(strength, &image, &info); |
| 231 |
} else |
| 232 |
cui_theme_get_type_icone_and_info(type, &image, &info); |
| 233 |
|
| 234 |
if (image == NULL) |
| 235 |
return; |
| 236 |
|
| 237 |
gtk_widget_set_visible((GtkWidget *)priv->signal, TRUE); |
| 238 |
gtk_widget_set_tooltip_text((GtkWidget *)priv->signal, info); |
| 239 |
gtk_image_set_from_pixbuf(priv->signal, image); |
| 240 |
} |
| 241 |
|
| 242 |
static void service_set_name(GtkService *service) |
| 243 |
{ |
| 244 |
const char *name; |
| 245 |
char *markup; |
| 246 |
|
| 247 |
name = connman_service_get_name(service->path); |
| 248 |
if (name == NULL) |
| 249 |
name = "- Hidden -"; |
| 250 |
|
| 251 |
if (connman_service_is_favorite(service->path) == TRUE) { |
| 252 |
if (g_strcmp0(connman_service_get_type(service->path), |
| 253 |
"wifi") == 0) { |
| 254 |
markup = g_markup_printf_escaped( |
| 255 |
"<b>%s</b> <i> (%s) </i>", |
| 256 |
name, |
| 257 |
connman_service_get_security(service->path)); |
| 258 |
} else |
| 259 |
markup = g_markup_printf_escaped("<b>%s</b>", name); |
| 260 |
} else { |
| 261 |
if (g_strcmp0(connman_service_get_type(service->path), |
| 262 |
"wifi") == 0) { |
| 263 |
markup = g_markup_printf_escaped( |
| 264 |
"%s <i> (%s) </i>", name, |
| 265 |
connman_service_get_security(service->path)); |
| 266 |
} else |
| 267 |
markup = g_markup_printf_escaped("%s", name); |
| 268 |
} |
| 269 |
|
| 270 |
gtk_label_set_markup(service->priv->name, markup); |
| 271 |
|
| 272 |
g_free(markup); |
| 273 |
} |
| 274 |
|
| 275 |
GtkService *gtk_service_new(const char *path) |
| 276 |
{ |
| 277 |
GtkService *service; |
| 278 |
char *path_copy; |
| 279 |
|
| 280 |
if (path == NULL) |
| 281 |
return NULL; |
| 282 |
|
| 283 |
path_copy = g_strdup(path); |
| 284 |
if (path_copy == NULL) |
| 285 |
return NULL; |
| 286 |
|
| 287 |
service = g_object_new(GTK_TYPE_SERVICE, NULL); |
| 288 |
if (service == NULL) { |
| 289 |
g_free(path_copy); |
| 290 |
return NULL; |
| 291 |
} |
| 292 |
|
| 293 |
service->path = path_copy; |
| 294 |
|
| 295 |
connman_service_set_property_changed_callback(path_copy, |
| 296 |
service_property_changed_cb, |
| 297 |
service); |
| 298 |
|
| 299 |
|
| 300 |
service_set_name(service); |
| 301 |
service_set_state(service); |
| 302 |
service_set_signal(service); |
| 303 |
|
| 304 |
return service; |
| 305 |
} |
| 306 |
|
| 307 |
|