Source code upload
[framework/connectivity/libgphoto2.git] / examples / config.c
1 #include "samples.h"
2
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 /*
8  * This function looks up a label or key entry of
9  * a configuration widget.
10  * The functions descend recursively, so you can just
11  * specify the last component.
12  */
13
14 static int
15 _lookup_widget(CameraWidget*widget, const char *key, CameraWidget **child) {
16         int ret;
17         ret = gp_widget_get_child_by_name (widget, key, child);
18         if (ret < GP_OK)
19                 ret = gp_widget_get_child_by_label (widget, key, child);
20         return ret;
21 }
22
23 /* Gets a string configuration value.
24  * This can be:
25  *  - A Text widget
26  *  - The current selection of a Radio Button choice
27  *  - The current selection of a Menu choice
28  *
29  * Sample (for Canons eg):
30  *   get_config_value_string (camera, "owner", &ownerstr, context);
31  */
32 int
33 get_config_value_string (Camera *camera, const char *key, char **str, GPContext *context) {
34         CameraWidget            *widget = NULL, *child = NULL;
35         CameraWidgetType        type;
36         int                     ret;
37         char                    *val;
38
39         ret = gp_camera_get_config (camera, &widget, context);
40         if (ret < GP_OK) {
41                 fprintf (stderr, "camera_get_config failed: %d\n", ret);
42                 return ret;
43         }
44         ret = _lookup_widget (widget, key, &child);
45         if (ret < GP_OK) {
46                 fprintf (stderr, "lookup widget failed: %d\n", ret);
47                 goto out;
48         }
49
50         /* This type check is optional, if you know what type the label
51          * has already. If you are not sure, better check. */
52         ret = gp_widget_get_type (child, &type);
53         if (ret < GP_OK) {
54                 fprintf (stderr, "widget get type failed: %d\n", ret);
55                 goto out;
56         }
57         switch (type) {
58         case GP_WIDGET_MENU:
59         case GP_WIDGET_RADIO:
60         case GP_WIDGET_TEXT:
61                 break;
62         default:
63                 fprintf (stderr, "widget has bad type %d\n", type);
64                 ret = GP_ERROR_BAD_PARAMETERS;
65                 goto out;
66         }
67
68         /* This is the actual query call. Note that we just
69          * a pointer reference to the string, not a copy... */
70         ret = gp_widget_get_value (child, &val);
71         if (ret < GP_OK) {
72                 fprintf (stderr, "could not query widget value: %d\n", ret);
73                 goto out;
74         }
75         /* Create a new copy for our caller. */
76         *str = strdup (val);
77 out:
78         gp_widget_free (widget);
79         return ret;
80 }
81
82
83 /* Sets a string configuration value.
84  * This can set for:
85  *  - A Text widget
86  *  - The current selection of a Radio Button choice
87  *  - The current selection of a Menu choice
88  *
89  * Sample (for Canons eg):
90  *   get_config_value_string (camera, "owner", &ownerstr, context);
91  */
92 int
93 set_config_value_string (Camera *camera, const char *key, const char *val, GPContext *context) {
94         CameraWidget            *widget = NULL, *child = NULL;
95         CameraWidgetType        type;
96         int                     ret;
97
98         ret = gp_camera_get_config (camera, &widget, context);
99         if (ret < GP_OK) {
100                 fprintf (stderr, "camera_get_config failed: %d\n", ret);
101                 return ret;
102         }
103         ret = _lookup_widget (widget, key, &child);
104         if (ret < GP_OK) {
105                 fprintf (stderr, "lookup widget failed: %d\n", ret);
106                 goto out;
107         }
108
109         /* This type check is optional, if you know what type the label
110          * has already. If you are not sure, better check. */
111         ret = gp_widget_get_type (child, &type);
112         if (ret < GP_OK) {
113                 fprintf (stderr, "widget get type failed: %d\n", ret);
114                 goto out;
115         }
116         switch (type) {
117         case GP_WIDGET_MENU:
118         case GP_WIDGET_RADIO:
119         case GP_WIDGET_TEXT:
120                 break;
121         default:
122                 fprintf (stderr, "widget has bad type %d\n", type);
123                 ret = GP_ERROR_BAD_PARAMETERS;
124                 goto out;
125         }
126
127         /* This is the actual set call. Note that we keep
128          * ownership of the string and have to free it if necessary.
129          */
130         ret = gp_widget_set_value (child, val);
131         if (ret < GP_OK) {
132                 fprintf (stderr, "could not set widget value: %d\n", ret);
133                 goto out;
134         }
135         /* This stores it on the camera again */
136         ret = gp_camera_set_config (camera, widget, context);
137         if (ret < GP_OK) {
138                 fprintf (stderr, "camera_set_config failed: %d\n", ret);
139                 return ret;
140         }
141 out:
142         gp_widget_free (widget);
143         return ret;
144 }
145
146
147 /*
148  * This enables/disables the specific canon capture mode.
149  * 
150  * For non canons this is not required, and will just return
151  * with an error (but without negative effects).
152  */
153 int
154 canon_enable_capture (Camera *camera, int onoff, GPContext *context) {
155         CameraWidget            *widget = NULL, *child = NULL;
156         CameraWidgetType        type;
157         int                     ret;
158
159         ret = gp_camera_get_config (camera, &widget, context);
160         if (ret < GP_OK) {
161                 fprintf (stderr, "camera_get_config failed: %d\n", ret);
162                 return ret;
163         }
164         ret = _lookup_widget (widget, "capture", &child);
165         if (ret < GP_OK) {
166                 /*fprintf (stderr, "lookup widget failed: %d\n", ret);*/
167                 goto out;
168         }
169
170         ret = gp_widget_get_type (child, &type);
171         if (ret < GP_OK) {
172                 fprintf (stderr, "widget get type failed: %d\n", ret);
173                 goto out;
174         }
175         switch (type) {
176         case GP_WIDGET_TOGGLE:
177                 break;
178         default:
179                 fprintf (stderr, "widget has bad type %d\n", type);
180                 ret = GP_ERROR_BAD_PARAMETERS;
181                 goto out;
182         }
183         /* Now set the toggle to the wanted value */
184         ret = gp_widget_set_value (child, &onoff);
185         if (ret < GP_OK) {
186                 fprintf (stderr, "toggling Canon capture to %d failed with %d\n", onoff, ret);
187                 goto out;
188         }
189         /* OK */
190         ret = gp_camera_set_config (camera, widget, context);
191         if (ret < GP_OK) {
192                 fprintf (stderr, "camera_set_config failed: %d\n", ret);
193                 return ret;
194         }
195 out:
196         gp_widget_free (widget);
197         return ret;
198 }