gwenhywfar 4.0.3

dlg_input.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  begin       : Wed Feb 17 2010
00003  copyright   : (C) 2010 by Martin Preuss
00004  email       : martin@libchipcard.de
00005 
00006  ***************************************************************************
00007  *          Please see toplevel file COPYING for license details           *
00008  ***************************************************************************/
00009 
00010 
00011 #ifdef HAVE_CONFIG_H
00012 # include <config.h>
00013 #endif
00014 
00015 #define DISABLE_DEBUGLOG
00016 
00017 
00018 #include "dlg_input_p.h"
00019 
00020 #include <gwenhywfar/gwenhywfar.h>
00021 #include <gwenhywfar/pathmanager.h>
00022 #include <gwenhywfar/gui.h>
00023 #include <gwenhywfar/debug.h>
00024 
00025 
00026 
00027 
00028 GWEN_INHERIT(GWEN_DIALOG, GWEN_DLGINPUT)
00029 
00030 
00031 
00032 
00033 
00034 GWEN_DIALOG *GWEN_DlgInput_new(uint32_t flags,
00035                                const char *title,
00036                                const char *text,
00037                                int minLen,
00038                                int maxLen) {
00039   GWEN_DIALOG *dlg;
00040   GWEN_DLGINPUT *xdlg;
00041   GWEN_BUFFER *fbuf;
00042   int rv;
00043 
00044   if (flags & GWEN_GUI_INPUT_FLAGS_CONFIRM)
00045     dlg=GWEN_Dialog_new("dlg_gwen_input1");
00046   else
00047     dlg=GWEN_Dialog_new("dlg_gwen_input2");
00048   GWEN_NEW_OBJECT(GWEN_DLGINPUT, xdlg);
00049 
00050   GWEN_INHERIT_SETDATA(GWEN_DIALOG, GWEN_DLGINPUT, dlg, xdlg,
00051                        GWEN_DlgInput_FreeData);
00052 
00053   GWEN_Dialog_SetSignalHandler(dlg, GWEN_DlgInput_SignalHandler);
00054 
00055   /* get path of dialog description file */
00056   fbuf=GWEN_Buffer_new(0, 256, 0, 1);
00057   rv=GWEN_PathManager_FindFile(GWEN_PM_LIBNAME, GWEN_PM_SYSDATADIR,
00058                                "gwenhywfar/dialogs/dlg_input.dlg",
00059                                fbuf);
00060   if (rv<0) {
00061     DBG_INFO(GWEN_LOGDOMAIN, "Dialog description file not found (%d).", rv);
00062     GWEN_Buffer_free(fbuf);
00063     GWEN_Dialog_free(dlg);
00064     return NULL;
00065   }
00066 
00067   /* read dialog from dialog description file */
00068   rv=GWEN_Dialog_ReadXmlFile(dlg, GWEN_Buffer_GetStart(fbuf));
00069   if (rv<0) {
00070     DBG_INFO(GWEN_LOGDOMAIN, "here (%d).", rv);
00071     GWEN_Buffer_free(fbuf);
00072     GWEN_Dialog_free(dlg);
00073     return NULL;
00074   }
00075   GWEN_Buffer_free(fbuf);
00076 
00077   xdlg->flags=flags;
00078   if (title)
00079     xdlg->title=strdup(title);
00080   if (text)
00081     xdlg->text=strdup(text);
00082   xdlg->minLen=minLen;
00083   xdlg->maxLen=maxLen;
00084 
00085   if (!(flags & GWEN_GUI_INPUT_FLAGS_SHOW)) {
00086     GWEN_Dialog_AddWidgetFlags(dlg, "input1", GWEN_WIDGET_FLAGS_PASSWORD);
00087     GWEN_Dialog_AddWidgetFlags(dlg, "input2", GWEN_WIDGET_FLAGS_PASSWORD);
00088   }
00089 
00090   if (maxLen>32) {
00091     GWEN_Dialog_SetWidgetColumns(dlg, "input1", 64);
00092     GWEN_Dialog_SetWidgetColumns(dlg, "input2", 64);
00093   }
00094   else {
00095     GWEN_Dialog_SetWidgetColumns(dlg, "input1", 32);
00096     GWEN_Dialog_SetWidgetColumns(dlg, "input2", 32);
00097   }
00098 
00099   if (!(flags & GWEN_GUI_INPUT_FLAGS_CONFIRM)) {
00100     GWEN_Dialog_RemoveWidget(dlg, "input2");
00101     GWEN_Dialog_RemoveWidget(dlg, "label2");
00102   }
00103 
00104   return dlg;
00105 }
00106 
00107 
00108 
00109 void GWENHYWFAR_CB GWEN_DlgInput_FreeData(void *bp, void *p) {
00110   GWEN_DLGINPUT *xdlg;
00111 
00112   xdlg=(GWEN_DLGINPUT*) p;
00113 
00114   if (xdlg->response) {
00115     memset(xdlg->response, 0, strlen(xdlg->response));
00116     xdlg->response=NULL;
00117   }
00118   free(xdlg->title);
00119   free(xdlg->text);
00120 
00121   GWEN_FREE_OBJECT(xdlg);
00122 }
00123 
00124 
00125 
00126 int GWEN_DlgInput_CheckInput(GWEN_DIALOG *dlg) {
00127   GWEN_DLGINPUT *xdlg;
00128   const char *s1;
00129 
00130   assert(dlg);
00131   xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGINPUT, dlg);
00132   assert(xdlg);
00133 
00134   s1=GWEN_Dialog_GetCharProperty(dlg, "input1", GWEN_DialogProperty_Value, 0, NULL);
00135   if (xdlg->flags & GWEN_GUI_INPUT_FLAGS_CONFIRM) {
00136     const char *s2;
00137 
00138     s2=GWEN_Dialog_GetCharProperty(dlg, "input2", GWEN_DialogProperty_Value, 0, NULL);
00139 
00140     /* check for equality */
00141     if (!s1 || !s2 || strcasecmp(s1, s2)!=0)
00142       return -1;
00143   }
00144 
00145   if (!s1)
00146     return -1;
00147 
00148   if (xdlg->minLen>=0) {
00149     if (strlen(s1)<xdlg->minLen)
00150       return -1;
00151   }
00152 
00153   return 0;
00154 }
00155 
00156 
00157 
00158 void GWEN_DlgInput_Init(GWEN_DIALOG *dlg) {
00159   GWEN_DLGINPUT *xdlg;
00160   GWEN_DB_NODE *dbParams;
00161 
00162   assert(dlg);
00163   xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGINPUT, dlg);
00164   assert(xdlg);
00165 
00166   dbParams=GWEN_Dialog_GetPreferences(dlg);
00167   assert(dbParams);
00168 
00169 #if 0
00170   /* read width */
00171   i=GWEN_DB_GetIntValue(dbParams, "dialog_width", 0, -1);
00172   if (i>=DIALOG_MINWIDTH)
00173     GWEN_Dialog_SetIntProperty(dlg, "", GWEN_DialogProperty_Width, 0, i, 0);
00174 
00175   /* read height */
00176   i=GWEN_DB_GetIntValue(dbParams, "dialog_height", 0, -1);
00177   if (i>=DIALOG_MINHEIGHT)
00178     GWEN_Dialog_SetIntProperty(dlg, "", GWEN_DialogProperty_Height, 0, i, 0);
00179 #endif
00180 
00181   /* special stuff */
00182   if (xdlg->title)
00183     GWEN_Dialog_SetCharProperty(dlg, "", GWEN_DialogProperty_Title, 0, xdlg->title, 0);
00184 
00185   if (xdlg->text)
00186     GWEN_Dialog_SetCharProperty(dlg, "descrLabel", GWEN_DialogProperty_Title, 0, xdlg->text, 0);
00187 
00188 
00189   GWEN_Dialog_SetIntProperty(dlg, "okButton", GWEN_DialogProperty_Enabled, 0, 0, 0);
00190   GWEN_Dialog_SetIntProperty(dlg, "abortButton", GWEN_DialogProperty_Enabled, 0, 1, 0);
00191 
00192   GWEN_Dialog_SetIntProperty(dlg, "input1", GWEN_DialogProperty_Focus, 0, 1, 0);
00193 
00194 
00195   xdlg->wasInit=1;
00196 }
00197 
00198 
00199 
00200 void GWEN_DlgInput_Fini(GWEN_DIALOG *dlg) {
00201   GWEN_DLGINPUT *xdlg;
00202   GWEN_DB_NODE *dbParams;
00203 
00204   assert(dlg);
00205   xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGINPUT, dlg);
00206   assert(xdlg);
00207 
00208   dbParams=GWEN_Dialog_GetPreferences(dlg);
00209   assert(dbParams);
00210 
00211   if (xdlg->response) {
00212     memset(xdlg->response, 0, strlen(xdlg->response));
00213     xdlg->response=NULL;
00214   }
00215 
00216   if (GWEN_DlgInput_CheckInput(dlg)==0) {
00217     const char *s;
00218     s=GWEN_Dialog_GetCharProperty(dlg, "input1", GWEN_DialogProperty_Value, 0, NULL);
00219     if (s)
00220       xdlg->response=strdup(s);
00221   }
00222 
00223 #if 0
00224   /* store dialog width */
00225   i=GWEN_Dialog_GetIntProperty(dlg, "", GWEN_DialogProperty_Width, 0, -1);
00226   if (i<DIALOG_MINWIDTH)
00227     i=DIALOG_MINWIDTH;
00228   GWEN_DB_SetIntValue(dbParams,
00229                       GWEN_DB_FLAGS_OVERWRITE_VARS,
00230                       "dialog_width",
00231                       i);
00232 
00233   /* store dialog height */
00234   i=GWEN_Dialog_GetIntProperty(dlg, "", GWEN_DialogProperty_Height, 0, -1);
00235   if (i<DIALOG_MINHEIGHT)
00236     i=DIALOG_MINHEIGHT;
00237   GWEN_DB_SetIntValue(dbParams,
00238                       GWEN_DB_FLAGS_OVERWRITE_VARS,
00239                       "dialog_height",
00240                       i);
00241 #endif
00242 }
00243 
00244 
00245 
00246 
00247 int GWEN_DlgInput_HandleActivated(GWEN_DIALOG *dlg, const char *sender) {
00248   GWEN_DLGINPUT *xdlg;
00249 
00250   assert(dlg);
00251   xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGINPUT, dlg);
00252   assert(xdlg);
00253 
00254   if (strcasecmp(sender, "okButton")==0) {
00255     return GWEN_DialogEvent_ResultAccept;
00256   }
00257   else if (strcasecmp(sender, "abortButton")==0) {
00258     return GWEN_DialogEvent_ResultReject;
00259   }
00260   else if (strcasecmp(sender, "input1")==0 ||
00261            strcasecmp(sender, "input2")==0) {
00262     if (GWEN_DlgInput_CheckInput(dlg)==0)
00263       return GWEN_DialogEvent_ResultAccept;
00264     return GWEN_DialogEvent_ResultHandled;
00265   }
00266 
00267   return GWEN_DialogEvent_ResultNotHandled;
00268 }
00269 
00270 
00271 
00272 int GWEN_DlgInput_HandleValueChanged(GWEN_DIALOG *dlg, const char *sender) {
00273   GWEN_DLGINPUT *xdlg;
00274 
00275   assert(dlg);
00276   xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGINPUT, dlg);
00277   assert(xdlg);
00278 
00279   if (strcasecmp(sender, "input1")==0 ||
00280       strcasecmp(sender, "input2")==0) {
00281     if (GWEN_DlgInput_CheckInput(dlg))
00282       /* disable okButton */
00283       GWEN_Dialog_SetIntProperty(dlg, "okButton", GWEN_DialogProperty_Enabled, 0, 0, 0);
00284     else
00285       /* enable okButton */
00286       GWEN_Dialog_SetIntProperty(dlg, "okButton", GWEN_DialogProperty_Enabled, 0, 1, 0);
00287     return GWEN_DialogEvent_ResultHandled;
00288   }
00289 
00290   return GWEN_DialogEvent_ResultNotHandled;
00291 }
00292 
00293 
00294 
00295 int GWENHYWFAR_CB GWEN_DlgInput_SignalHandler(GWEN_DIALOG *dlg,
00296                                               GWEN_DIALOG_EVENTTYPE t,
00297                                               const char *sender) {
00298   GWEN_DLGINPUT *xdlg;
00299 
00300   assert(dlg);
00301   xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGINPUT, dlg);
00302   assert(xdlg);
00303 
00304   switch(t) {
00305   case GWEN_DialogEvent_TypeInit:
00306     GWEN_DlgInput_Init(dlg);
00307     return GWEN_DialogEvent_ResultHandled;;
00308 
00309   case GWEN_DialogEvent_TypeFini:
00310     GWEN_DlgInput_Fini(dlg);
00311     return GWEN_DialogEvent_ResultHandled;;
00312 
00313   case GWEN_DialogEvent_TypeValueChanged:
00314     return GWEN_DlgInput_HandleValueChanged(dlg, sender);
00315     break;
00316 
00317   case GWEN_DialogEvent_TypeActivated:
00318     return GWEN_DlgInput_HandleActivated(dlg, sender);
00319 
00320   case GWEN_DialogEvent_TypeEnabled:
00321   case GWEN_DialogEvent_TypeDisabled:
00322 
00323   case GWEN_DialogEvent_TypeClose:
00324     return GWEN_DialogEvent_ResultAccept;
00325 
00326   case GWEN_DialogEvent_TypeLast:
00327     return GWEN_DialogEvent_ResultNotHandled;
00328   }
00329 
00330   return GWEN_DialogEvent_ResultNotHandled;
00331 
00332 }
00333 
00334 
00335 
00336 int GWEN_DlgInput_CopyInput(GWEN_DIALOG *dlg, char *buffer, int size) {
00337   GWEN_DLGINPUT *xdlg;
00338 
00339   assert(dlg);
00340   xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGINPUT, dlg);
00341   assert(xdlg);
00342 
00343   if (xdlg->response) {
00344     int l;
00345 
00346     l=strlen(xdlg->response);
00347     if ((l+1)>size) {
00348       DBG_ERROR(GWEN_LOGDOMAIN, "Buffer too small");
00349       return GWEN_ERROR_BUFFER_OVERFLOW;
00350     }
00351     /* buffer ok, copy */
00352     memmove(buffer, xdlg->response, l+1);
00353     return 0;
00354   }
00355   return GWEN_ERROR_NO_DATA;
00356 }
00357 
00358 
00359 
00360