aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/bluez/e_mod_config.c
blob: 1514e62dd69ac764bdb4cb2d575af05b1617d253 (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
#include "e_mod_main.h"

extern const char _e_bluez_Name[];

struct _E_Config_Dialog_Data
{
   E_Bluez_Instance *inst;
   const char       *name;
   Eina_Bool         mode;
   unsigned int      timeout;
   struct
   {
      Evas_Object *label;
      Evas_Object *slider;
      Evas_Object *help;
   } gui;
};

/* Local Function Prototypes */
static void        *_create_data(E_Config_Dialog *dialog);
static void         _free_data(E_Config_Dialog      *dialog,
                               E_Config_Dialog_Data *cfdata);
static Evas_Object *_basic_create(E_Config_Dialog      *dialog,
                                  Evas                 *evas,
                                  E_Config_Dialog_Data *cfdata);
static int _basic_apply(E_Config_Dialog      *dialog,
                        E_Config_Dialog_Data *cfdata);

E_Config_Dialog *
e_bluez_config_dialog_new(E_Container      *con,
                          E_Bluez_Instance *inst)
{
   E_Config_Dialog *dialog;
   E_Config_Dialog_View *view;

   if (inst->conf_dialog)
     return inst->conf_dialog;

   view = E_NEW(E_Config_Dialog_View, 1);
   if (!view)
     return NULL;

   view->create_cfdata = _create_data;
   view->free_cfdata = _free_data;
   view->basic.create_widgets = _basic_create;
   view->basic.apply_cfdata = _basic_apply;

   dialog = e_config_dialog_new(con, _("Bluetooth Settings"),
                                _e_bluez_Name, "e_bluez_config_dialog_new",
                                e_bluez_theme_path(), 0, view, inst);

   return dialog;
}

static void *
_create_data(E_Config_Dialog *dialog)
{
   E_Config_Dialog_Data *cfdata;
   E_Bluez_Instance *inst;

   cfdata = E_NEW(E_Config_Dialog_Data, 1);
   if (!cfdata)
     return NULL;

   cfdata->inst = dialog->data;
   inst = cfdata->inst;
   if (!e_bluez_adapter_discoverable_get(inst->adapter, &cfdata->mode))
     cfdata->mode = 0;

   if (!e_bluez_adapter_discoverable_timeout_get
         (inst->adapter, &cfdata->timeout))
     cfdata->timeout = 0;
   cfdata->timeout /= 60;

   if (!e_bluez_adapter_name_get(inst->adapter, &cfdata->name))
     cfdata->name = NULL;
   else
     cfdata->name = strdup(cfdata->name);

   return cfdata;
}

static void
_free_data(E_Config_Dialog      *dialog,
           E_Config_Dialog_Data *cfdata)
{
   E_Bluez_Instance *inst = dialog->data;

   inst->conf_dialog = NULL;
   E_FREE(cfdata);
}

static void
_cb_disable_timeout(void            *data,
                    Evas_Object *obj __UNUSED__)
{
   E_Config_Dialog_Data *cfdata = data;
   Eina_Bool disable = !cfdata->mode;

   e_widget_disabled_set(cfdata->gui.label, disable);
   e_widget_disabled_set(cfdata->gui.slider, disable);
   e_widget_disabled_set(cfdata->gui.help, disable);
}

static Evas_Object *
_basic_create(E_Config_Dialog *dialog __UNUSED__,
              Evas                   *evas,
              E_Config_Dialog_Data   *cfdata)
{
   Evas_Object *o, *ob;
   Evas_Object *check;
   char buf[48];
   const char *address;

   o = e_widget_list_add(evas, 0, 0);

   if (!e_bluez_adapter_address_get(cfdata->inst->adapter, &address))
     address = NULL;

   ob = e_widget_label_add(evas, _("Name"));
   e_widget_list_object_append(o, ob, 1, 1, 0.5);

   ob = e_widget_entry_add(evas, (char **)&cfdata->name, NULL, NULL, NULL);
   e_widget_list_object_append(o, ob, 1, 1, 0.5);

   check = e_widget_check_add
       (evas, _("Discoverable mode"), (int *)&cfdata->mode);
   e_widget_list_object_append(o, check, 1, 1, 0.5);

   ob = e_widget_label_add(evas, _("Discovarable Timeout"));
   cfdata->gui.label = ob;
   e_widget_list_object_append(o, ob, 1, 1, 0.5);

   ob = e_widget_slider_add(evas, 1, 0, _("%1.0f minutes"), 0, 30, 1, 0,
                            NULL, (int *)&cfdata->timeout, 100);
   e_widget_slider_special_value_add(ob, 0.0, 0.0, _("Forever"));
   cfdata->gui.slider = ob;
   e_widget_list_object_append(o, ob, 1, 1, 0.5);

   e_widget_on_change_hook_set(check, _cb_disable_timeout, cfdata);
   _cb_disable_timeout(cfdata, NULL);

   snprintf(buf, sizeof(buf), _("MAC Address: %s"), address);
   ob = e_widget_label_add(evas, buf);
   e_widget_list_object_append(o, ob, 1, 1, 0.5);

   return o;
}

static void
_method_success_check(void            *data,
                      DBusMessage *msg __UNUSED__,
                      DBusError       *error)
{
   const char *name = data;

   if ((!error) || (!dbus_error_is_set(error)))
     return;

   ERR("method %s() finished with error: %s %s\n",
       name, error->name, error->message);
   dbus_error_free(error);
}

static int
_basic_apply(E_Config_Dialog *dialog __UNUSED__,
             E_Config_Dialog_Data   *cfdata)
{
   E_Bluez_Instance *inst = cfdata->inst;

   if (!e_bluez_adapter_discoverable_set
         (inst->adapter, cfdata->mode,
         _method_success_check, "e_bluez_adapter_discoverable_get"))
     ERR("Can't set Discoverable on adapter");

   if (!e_bluez_adapter_discoverable_timeout_set
         (inst->adapter, cfdata->timeout * 60,
         _method_success_check, "e_bluez_adapter_discoverable_timeout_get"))
     ERR("Can't set DiscoverableTimeout on adapter");

   if (!e_bluez_adapter_name_set
         (inst->adapter, cfdata->name,
         _method_success_check, "e_bluez_adapter_name_get"))
     ERR("Can't set Name on adapter");

   return 1;
}