00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include <photo/photo.h>
00040
00041 static int
00042 find_widget_by_name (Camera *photo, GPContext *context, const char *param, CameraWidget **child, CameraWidget **rootconfig) {
00043 int ret;
00044
00045 ret = gp_camera_get_config (photo, rootconfig, context);
00046 if (ret != GP_OK) return ret;
00047 ret = gp_widget_get_child_by_name (*rootconfig, param, child);
00048 if (ret != GP_OK)
00049 ret = gp_widget_get_child_by_label (*rootconfig, param, child);
00050 if (ret != GP_OK) {
00051 char *part, *s, *newname;
00052
00053 newname = strdup (param);
00054 if (!newname)
00055 return GP_ERROR_NO_MEMORY;
00056
00057 *child = *rootconfig;
00058 part = newname;
00059 while (part[0] == '/')
00060 part++;
00061 while (1) {
00062 CameraWidget *tmp;
00063
00064 s = strchr (part,'/');
00065 if (s)
00066 *s='\0';
00067 ret = gp_widget_get_child_by_name (*child, part, &tmp);
00068 if (ret != GP_OK)
00069 ret = gp_widget_get_child_by_label (*child, part, &tmp);
00070 if (ret != GP_OK)
00071 break;
00072 *child = tmp;
00073 if (!s)
00074 break;
00075 part = s+1;
00076 while (part[0] == '/')
00077 part++;
00078 }
00079 if (s) {
00080 gp_context_error (context,"%s not found in configuration tree.", newname);
00081 free (newname);
00082 gp_widget_free (*rootconfig);
00083 return GP_ERROR;
00084 }
00085 free (newname);
00086 }
00087 return GP_OK;
00088 }
00089
00090 int photo_set_config(photo_p photo, const char *param, const char *value)
00091 {
00092 CameraWidget *rootconfig, *child;
00093 int ret;
00094 const char *label;
00095 CameraWidgetType type;
00096
00097 ret = find_widget_by_name(photo->cam, photo->context, param, &child, &rootconfig);
00098 if (ret!=GP_OK)
00099 return 0;
00100
00101 ret = gp_widget_get_type(child, &type);
00102 if (ret!=GP_OK) {
00103 gp_widget_free(rootconfig);
00104 return 0;
00105 }
00106 ret = gp_widget_get_label(child, &label);
00107 if (ret!=GP_OK) {
00108 gp_widget_free(rootconfig);
00109 return 0;
00110 }
00111
00112 switch (type) {
00113 case GP_WIDGET_TEXT: {
00114 ret = gp_widget_set_value(child, value);
00115 if (ret!=GP_OK)
00116 gp_context_error(photo->context,"Failed to set the value of text widget %s to %s.", param, value);
00117 break;
00118 }
00119 case GP_WIDGET_RANGE: {
00120 float f, t, b, s;
00121
00122 ret = gp_widget_get_range(child, &b, &t, &s);
00123 if (ret!=GP_OK)
00124 break;
00125 if (!sscanf(value, "%f", &f)) {
00126 gp_context_error(photo->context,"The passed value %s is not a floating point value.", value);
00127 ret = GP_ERROR_BAD_PARAMETERS;
00128 break;
00129 }
00130 if ((f<b)||(f>t)) {
00131 gp_context_error(photo->context,"The passed value %f is not within the expected range %f - %f.", f, b, t);
00132 ret = GP_ERROR_BAD_PARAMETERS;
00133 break;
00134 }
00135 ret = gp_widget_set_value(child, &f);
00136 if (ret!=GP_OK)
00137 gp_context_error(photo->context,"Failed to set the value of range widget %s to %f.", param, f);
00138 break;
00139 }
00140 case GP_WIDGET_TOGGLE: {
00141 int t;
00142
00143 t = 2;
00144 if (!strcasecmp(value, "off")||
00145 !strcasecmp(value, "no")||
00146 !strcasecmp(value, "false")||
00147 !strcmp(value, "0"))
00148 t = 0;
00149 if (!strcasecmp(value, "on")||
00150 !strcasecmp(value, "yes")||
00151 !strcasecmp(value, "true")||
00152 !strcmp(value, "1"))
00153 t = 1;
00154
00155 if (t==2) {
00156 gp_context_error(photo->context,"The passed value %s is not a valid toggle value.", value);
00157 ret = GP_ERROR_BAD_PARAMETERS;
00158 break;
00159 }
00160 ret = gp_widget_set_value(child, &t);
00161 if (ret!=GP_OK)
00162 gp_context_error(photo->context,"Failed to set values %s of toggle widget %s.", value, param);
00163 break;
00164 }
00165 case GP_WIDGET_DATE: {
00166 int t = -1;
00167 #ifdef HAVE_STRPTIME
00168 struct tm xtm;
00169
00170 if (strptime (value, "%c", &xtm) || strptime (value, "%Ec", &xtm))
00171 t = mktime (&xtm);
00172 #endif
00173 if (t==-1) {
00174 if (!sscanf(value, "%d", &t)) {
00175 gp_context_error(photo->context,"The passed value %s is neither a valid time nor an integer.", value);
00176 ret = GP_ERROR_BAD_PARAMETERS;
00177 break;
00178 }
00179 }
00180 ret = gp_widget_set_value(child, &t);
00181 if (ret!=GP_OK)
00182 gp_context_error(photo->context,"Failed to set new time of date/time widget %s to %s.", param, value);
00183 break;
00184 }
00185 case GP_WIDGET_MENU:
00186 case GP_WIDGET_RADIO: {
00187 int cnt, i;
00188
00189 cnt = gp_widget_count_choices(child);
00190 if (cnt<GP_OK) {
00191 ret = cnt;
00192 break;
00193 }
00194 ret = GP_ERROR_BAD_PARAMETERS;
00195 for(i = 0; i<cnt; i++) {
00196 const char *choice;
00197
00198 ret = gp_widget_get_choice(child, i, &choice);
00199 if (ret!=GP_OK)
00200 continue;
00201 if (!strcmp(choice, value)) {
00202 ret = gp_widget_set_value(child, value);
00203 break;
00204 }
00205 }
00206 if (i!=cnt)
00207 break;
00208
00209 if (sscanf(value, "%d", &i)) {
00210 if ((i>=0)&&(i<cnt)) {
00211 const char *choice;
00212
00213 ret = gp_widget_get_choice(child, i, &choice);
00214 if (ret==GP_OK)
00215 ret = gp_widget_set_value(child, choice);
00216 break;
00217 }
00218 }
00219 gp_context_error(photo->context,"Choice %s not found within list of choices.", value);
00220 break;
00221 }
00222
00223
00224 case GP_WIDGET_WINDOW:
00225 case GP_WIDGET_SECTION:
00226 case GP_WIDGET_BUTTON:
00227 gp_context_error(photo->context,"The %s widget is not configurable.", param);
00228 ret = GP_ERROR_BAD_PARAMETERS;
00229 break;
00230 }
00231 if (ret==GP_OK) {
00232 ret = gp_camera_set_config(photo->cam, rootconfig, photo->context);
00233 if (ret!=GP_OK)
00234 gp_context_error(photo->context,"Failed to set new configuration value %s for configuration entry %s.", value, param);
00235 }
00236 gp_widget_free(rootconfig);
00237 return (ret==GP_OK);
00238 }
00239
00240 int photo_get_config(photo_p photo, const char *param, char **value)
00241 {
00242 CameraWidget *rootconfig, *child;
00243 int ret;
00244 const char *label;
00245 CameraWidgetType type;
00246
00247
00248 ret = find_widget_by_name(photo->cam, photo->context, param, &child, &rootconfig);
00249 if (ret != GP_OK)
00250 return 0;
00251
00252
00253 ret = gp_widget_get_type (child, &type);
00254 if (ret != GP_OK) {
00255 gp_widget_free (rootconfig);
00256 return 0;
00257 }
00258
00259
00260 ret = gp_widget_get_label (child, &label);
00261 if (ret != GP_OK) {
00262 gp_widget_free (rootconfig);
00263 return 0;
00264 }
00265
00266
00267 switch (type) {
00268 case GP_WIDGET_TEXT: {
00269 char *txt;
00270
00271 ret = gp_widget_get_value (child, &txt);
00272 if (ret != GP_OK) {
00273 gp_context_error (photo->context,"Failed to retrieve value of text widget %s.", param);
00274 }
00275 *value = txt;
00276 break;
00277 }
00278 case GP_WIDGET_RANGE: {
00279 float f, t,b,s;
00280
00281 ret = gp_widget_get_range (child, &b, &t, &s);
00282 if (ret != GP_OK){
00283 gp_context_error (photo->context,"Failed to retrieve values of range widget %s.", param);
00284 }
00285 ret = gp_widget_get_value (child, &f);
00286 if (ret != GP_OK) {
00287 gp_context_error (photo->context,"Failed to value of range widget %s.", param);
00288 }
00289 sprintf(*value,"%f",f);
00290 break;
00291 }
00292 case GP_WIDGET_TOGGLE: {
00293 int t;
00294
00295 ret = gp_widget_get_value (child, &t);
00296 if (ret != GP_OK) {
00297 gp_context_error (photo->context,"Failed to retrieve values of toggle widget %s.", param);
00298 }
00299 sprintf(*value,"%d",t);
00300 break;
00301 }
00302 case GP_WIDGET_DATE: {
00303 int ret, t;
00304 time_t xtime;
00305 struct tm *xtm;
00306 char timebuf[200];
00307
00308 ret = gp_widget_get_value (child, &t);
00309 if (ret != GP_OK) {
00310 gp_context_error (photo->context,"Failed to retrieve values of date/time widget %s.", param);
00311 break;
00312 }
00313 xtime = t;
00314 xtm = localtime (&xtime);
00315 ret = strftime (timebuf, sizeof(timebuf), "%c", xtm);
00316 sprintf(*value,"%s",timebuf);
00317 break;
00318 }
00319 case GP_WIDGET_MENU:
00320 case GP_WIDGET_RADIO: {
00321 char *current;
00322 ret = gp_widget_get_value (child, ¤t);
00323 if (ret != GP_OK) {
00324 gp_context_error (photo->context,"Failed to retrieve values of radio widget %s.", param);
00325 }
00326 sprintf(*value,"%s",current);
00327 break;
00328 }
00329
00330
00331 case GP_WIDGET_WINDOW:
00332 case GP_WIDGET_SECTION:
00333 case GP_WIDGET_BUTTON:
00334 break;
00335 }
00336 gp_widget_free (rootconfig);
00337 return (ret==GP_OK);
00338 }
00339
00340