gwenhywfar  4.8.0beta
dlg_progress.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Tue Feb 16 2010
3  copyright : (C) 2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14 
15 #define DISABLE_DEBUGLOG
16 
17 
18 #include "dlg_progress_p.h"
19 
20 #include <gwenhywfar/gwenhywfar.h>
21 #include <gwenhywfar/pathmanager.h>
22 #include <gwenhywfar/debug.h>
23 #include <gwenhywfar/text.h>
24 
25 
26 
27 #define DIALOG_MINWIDTH 520
28 #define DIALOG_MINHEIGHT 400
29 #define DIALOG_MINHEIGHT_NOLOG 100
30 
31 
32 
33 GWEN_INHERIT(GWEN_DIALOG, GWEN_DLGPROGRESS)
34 
35 
36 
37 
38 
40  GWEN_DIALOG *dlg;
41  GWEN_DLGPROGRESS *xdlg;
42  GWEN_BUFFER *fbuf;
43  int rv;
44 
45  dlg=GWEN_Dialog_new("dlg_gwen_progress");
46  GWEN_NEW_OBJECT(GWEN_DLGPROGRESS, xdlg);
47 
48  GWEN_INHERIT_SETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg, xdlg,
50 
52 
53  /* get path of dialog description file */
54  fbuf=GWEN_Buffer_new(0, 256, 0, 1);
56  "gwenhywfar/dialogs/dlg_progress.dlg",
57  fbuf);
58  if (rv<0) {
59  DBG_INFO(GWEN_LOGDOMAIN, "Dialog description file not found (%d).", rv);
60  GWEN_Buffer_free(fbuf);
61  GWEN_Dialog_free(dlg);
62  return NULL;
63  }
64 
65  /* read dialog from dialog description file */
67  if (rv<0) {
68  DBG_INFO(GWEN_LOGDOMAIN, "here (%d).", rv);
69  GWEN_Buffer_free(fbuf);
70  GWEN_Dialog_free(dlg);
71  return NULL;
72  }
73  GWEN_Buffer_free(fbuf);
74 
75  xdlg->logBufferTxt=GWEN_Buffer_new(0, 256, 0, 1);
76  xdlg->logBufferHtml=GWEN_Buffer_new(0, 256, 0, 1);
77 
78  return dlg;
79 }
80 
81 
82 
83 void GWENHYWFAR_CB GWEN_DlgProgress_FreeData(void *bp, void *p) {
84  GWEN_DLGPROGRESS *xdlg;
85 
86  xdlg=(GWEN_DLGPROGRESS*) p;
87 
88  GWEN_Buffer_free(xdlg->logBufferHtml);
89  GWEN_Buffer_free(xdlg->logBufferTxt);
90 
91  GWEN_FREE_OBJECT(xdlg);
92 }
93 
94 
95 
97  GWEN_DLGPROGRESS *xdlg;
98 
99  assert(dlg);
100  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
101  assert(xdlg);
102 
103  xdlg->allowClose=b;
104  if (xdlg->wasInit) {
105  GWEN_Dialog_SetIntProperty(dlg, "abortButton", GWEN_DialogProperty_Enabled, 0, 0, 0);
106  GWEN_Dialog_SetIntProperty(dlg, "closeButton", GWEN_DialogProperty_Enabled, 0, 1, 0);
107  }
108 }
109 
110 
111 
113  GWEN_DLGPROGRESS *xdlg;
114 
115  assert(dlg);
116  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
117  assert(xdlg);
118 
119  xdlg->stayOpen=b;
120 }
121 
122 
123 
125  GWEN_DLGPROGRESS *xdlg;
126 
127  assert(dlg);
128  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
129  assert(xdlg);
130 
131  return xdlg->stayOpen;
132 }
133 
134 
135 
137  GWEN_DLGPROGRESS *xdlg;
138 
139  assert(dlg);
140  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
141  assert(xdlg);
142 
143  if (xdlg->showLog!=b) {
144  xdlg->showLog=b;
145  if (xdlg->wasInit) {
146 
148  if (b) {
149  int i;
150 
151  i=xdlg->withLogWidth;
152  if (i<DIALOG_MINWIDTH)
153  i=DIALOG_MINWIDTH;
155 
156  i=xdlg->withLogHeight;
157  if (i<DIALOG_MINHEIGHT)
160  }
161  }
162  }
163 }
164 
165 
166 
168  GWEN_DLGPROGRESS *xdlg;
169 
170  assert(dlg);
171  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
172  assert(xdlg);
173 
174  return xdlg->firstProgress;
175 }
176 
177 
178 
180  GWEN_DLGPROGRESS *xdlg;
181 
182  assert(dlg);
183  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
184  assert(xdlg);
185 
186  xdlg->firstProgress=pd;
187 
188  if (xdlg->wasInit) {
189  if (xdlg->firstProgress) {
190  const char *s;
191 
192  s=GWEN_ProgressData_GetTitle(xdlg->firstProgress);
193  if (s && *s)
195 
196  s=GWEN_ProgressData_GetText(xdlg->firstProgress);
197  if (s && *s)
198  GWEN_Dialog_SetCharProperty(dlg, "descrLabel", GWEN_DialogProperty_Title, 0, s, 0);
199 
200  GWEN_Dialog_SetIntProperty(dlg, "allProgress", GWEN_DialogProperty_Enabled, 0, 1, 0);
202  GWEN_ProgressData_GetTotal(xdlg->firstProgress), 0);
204  GWEN_ProgressData_GetCurrent(xdlg->firstProgress), 0);
205  }
206  else {
207  /* let it show 100 % */
208  GWEN_Dialog_SetIntProperty(dlg, "allProgress", GWEN_DialogProperty_MaxValue, 0, 100, 0);
209  GWEN_Dialog_SetIntProperty(dlg, "allProgress", GWEN_DialogProperty_Value, 0, 100, 0);
210  }
211  }
212 }
213 
214 
215 
217  GWEN_DLGPROGRESS *xdlg;
218 
219  assert(dlg);
220  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
221  assert(xdlg);
222 
223  return xdlg->secondProgress;
224 }
225 
226 
227 
229  GWEN_DLGPROGRESS *xdlg;
230 
231  assert(dlg);
232  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
233  assert(xdlg);
234 
235  xdlg->secondProgress=pd;
236 
237  if (xdlg->wasInit) {
238  if (xdlg->secondProgress) {
239  GWEN_Dialog_SetIntProperty(dlg, "currentProgress", GWEN_DialogProperty_Enabled, 0, 1, 0);
240  GWEN_Dialog_SetIntProperty(dlg, "currentProgress", GWEN_DialogProperty_MaxValue, 0,
241  GWEN_ProgressData_GetTotal(xdlg->secondProgress), 0);
242  GWEN_Dialog_SetIntProperty(dlg, "currentProgress", GWEN_DialogProperty_Value, 0,
243  GWEN_ProgressData_GetCurrent(xdlg->secondProgress), 0);
244  }
245  else {
246  GWEN_Dialog_SetIntProperty(dlg, "currentProgress", GWEN_DialogProperty_Value, 0, 0, 0);
247  GWEN_Dialog_SetIntProperty(dlg, "currentProgress", GWEN_DialogProperty_Enabled, 0, 0, 0);
248  }
249  }
250 }
251 
252 
253 
255  GWEN_LOGGER_LEVEL level,
256  const char *s) {
257  GWEN_DLGPROGRESS *xdlg;
258  GWEN_TIME *ti;
259  int rv;
260  GWEN_BUFFER *tbuf;
261  const char *col;
262 
263  assert(dlg);
264  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
265  assert(xdlg);
266 
267  ti=GWEN_CurrentTime();
268 
269  /* setup text string */
270  if (GWEN_Buffer_GetUsedBytes(xdlg->logBufferTxt))
271  GWEN_Buffer_AppendString(xdlg->logBufferTxt, "\n");
272  if (ti)
273  rv=GWEN_Time_toString(ti, "hh:mm:ss", xdlg->logBufferTxt);
274  else
276  if (rv<0)
277  GWEN_Buffer_AppendString(xdlg->logBufferTxt, "??:??:??");
278  GWEN_Buffer_AppendString(xdlg->logBufferTxt, " ");
279  if (s)
280  GWEN_Buffer_AppendString(xdlg->logBufferTxt, s);
281 
282  /* setup HTML string */
283  GWEN_Buffer_AppendString(xdlg->logBufferHtml, "<tr><td>");
284  if (ti)
285  rv=GWEN_Time_toString(ti, "hh:mm:ss", xdlg->logBufferHtml);
286  else
288  if (rv<0)
289  GWEN_Buffer_AppendString(xdlg->logBufferHtml, "??:??:??");
290  GWEN_Buffer_AppendString(xdlg->logBufferHtml, "</td><td>");
291 
292  if (level<=GWEN_LoggerLevel_Error)
293  col="red";
294  else if (level==GWEN_LoggerLevel_Warning)
295  col="blue";
296  else if (level==GWEN_LoggerLevel_Info)
297  col="green";
298  else
299  col=NULL;
300  if (col) {
301  GWEN_Buffer_AppendString(xdlg->logBufferHtml, "<font color=\"");
302  GWEN_Buffer_AppendString(xdlg->logBufferHtml, col);
303  GWEN_Buffer_AppendString(xdlg->logBufferHtml,"\">");
304  }
305  if (s)
306  GWEN_Text_EscapeXmlToBuffer(s, xdlg->logBufferHtml);
307  if (col)
308  GWEN_Buffer_AppendString(xdlg->logBufferHtml, "</font>");
309  GWEN_Buffer_AppendString(xdlg->logBufferHtml, "</td></tr>");
310 
311  /* assemble full string, containing HTML and text log */
312  tbuf=GWEN_Buffer_new(0,
313  GWEN_Buffer_GetUsedBytes(xdlg->logBufferHtml)+
314  GWEN_Buffer_GetUsedBytes(xdlg->logBufferTxt)+256,
315  0,
316  1);
317 
318  GWEN_Buffer_AppendString(tbuf, "<html><table>");
319  GWEN_Buffer_AppendString(tbuf, GWEN_Buffer_GetStart(xdlg->logBufferHtml));
320  GWEN_Buffer_AppendString(tbuf, "</table></html>");
321  GWEN_Buffer_AppendString(tbuf, GWEN_Buffer_GetStart(xdlg->logBufferTxt));
322 
324  GWEN_Buffer_GetStart(tbuf), 0);
325  GWEN_Buffer_free(tbuf);
326 }
327 
328 
329 
331  GWEN_DLGPROGRESS *xdlg;
332  const char *s;
333 
334  assert(dlg);
335  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
336  assert(xdlg);
337 
338  if (pd==xdlg->firstProgress)
339  s="allProgress";
340  else if (pd==xdlg->secondProgress)
341  s="currentProgress";
342  else {
343  DBG_ERROR(GWEN_LOGDOMAIN, "Progress %08x is neither primary nor secondary",
345  return;
346  }
347 
348  if (xdlg->wasInit) {
351  }
352 }
353 
354 
355 
357  GWEN_DLGPROGRESS *xdlg;
358  const char *s;
359 
360  assert(dlg);
361  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
362  assert(xdlg);
363 
364  if (pd==xdlg->firstProgress)
365  s="allProgress";
366  else if (pd==xdlg->secondProgress)
367  s="currentProgress";
368  else {
369  DBG_ERROR(GWEN_LOGDOMAIN, "Progress %08x is neither primary nor secondary",
371  return;
372  }
373 
374  if (xdlg->wasInit) {
377  }
378 }
379 
380 
381 
383  GWEN_DLGPROGRESS *xdlg;
384  int i;
385  GWEN_DB_NODE *dbParams;
386 
387  assert(dlg);
388  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
389  assert(xdlg);
390 
391  dbParams=GWEN_Dialog_GetPreferences(dlg);
392  assert(dbParams);
393 
394  xdlg->withLogWidth=GWEN_DB_GetIntValue(dbParams, "dialog_width", 0, -1);
395  xdlg->withLogHeight=GWEN_DB_GetIntValue(dbParams, "dialog_height", 0, -1);
396 
397  if (xdlg->showLog) {
398  int i;
399 
400  i=xdlg->withLogWidth;
401  if (i>=DIALOG_MINWIDTH)
403 
404  i=xdlg->withLogHeight;
405  if (i>=DIALOG_MINHEIGHT)
407 
409  }
410  else {
411  /* read width */
412  i=GWEN_DB_GetIntValue(dbParams, "dialog_width_nolog", 0, -1);
413  if (i>=DIALOG_MINWIDTH)
415 
416  /* read height */
417  i=GWEN_DB_GetIntValue(dbParams, "dialog_height_nolog", 0, -1);
418  if (i>=DIALOG_MINHEIGHT_NOLOG)
421  }
422 
423 
424  if (xdlg->firstProgress) {
425  const char *s;
426 
427  s=GWEN_ProgressData_GetTitle(xdlg->firstProgress);
428  if (s && *s)
430 
431  s=GWEN_ProgressData_GetText(xdlg->firstProgress);
432  if (s && *s)
433  GWEN_Dialog_SetCharProperty(dlg, "descrLabel", GWEN_DialogProperty_Title, 0, s, 0);
434 
436  GWEN_ProgressData_GetTotal(xdlg->firstProgress), 0);
438  GWEN_ProgressData_GetCurrent(xdlg->firstProgress), 0);
439  }
440 
441  if (xdlg->secondProgress) {
442  GWEN_Dialog_SetIntProperty(dlg, "currentProgress", GWEN_DialogProperty_MaxValue, 0,
443  GWEN_ProgressData_GetTotal(xdlg->secondProgress), 0);
444  GWEN_Dialog_SetIntProperty(dlg, "currentProgress", GWEN_DialogProperty_Value, 0,
445  GWEN_ProgressData_GetCurrent(xdlg->secondProgress), 0);
446  }
447 
448  GWEN_Dialog_SetIntProperty(dlg, "abortButton", GWEN_DialogProperty_Enabled, 0, 1, 0);
449  GWEN_Dialog_SetIntProperty(dlg, "closeButton", GWEN_DialogProperty_Enabled, 0, 0, 0);
450 
451  xdlg->wasInit=1;
452 }
453 
454 
455 
457  GWEN_DLGPROGRESS *xdlg;
458  int i;
459  GWEN_DB_NODE *dbParams;
460 
461  assert(dlg);
462  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
463  assert(xdlg);
464 
465  dbParams=GWEN_Dialog_GetPreferences(dlg);
466  assert(dbParams);
467 
468  /* store dialog width */
469  if (xdlg->showLog) {
471  GWEN_DB_SetIntValue(dbParams,
473  "dialog_width",
474  i);
475 
476  /* store dialog height */
478  GWEN_DB_SetIntValue(dbParams,
480  "dialog_height",
481  i);
482  }
483  else {
485  GWEN_DB_SetIntValue(dbParams,
487  "dialog_width_nolog",
488  i);
489 
490  /* store dialog height */
492  GWEN_DB_SetIntValue(dbParams,
494  "dialog_height_nolog",
495  i);
496  }
497 }
498 
499 
500 
501 
502 int GWEN_DlgProgress_HandleActivated(GWEN_DIALOG *dlg, const char *sender) {
503  GWEN_DLGPROGRESS *xdlg;
504 
505  assert(dlg);
506  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
507  assert(xdlg);
508 
509  if (strcasecmp(sender, "closeButton")==0) {
511  }
512  else if (strcasecmp(sender, "abortButton")==0) {
513  if (xdlg->firstProgress)
514  GWEN_ProgressData_SetAborted(xdlg->firstProgress, 1);
515  if (xdlg->secondProgress)
516  GWEN_ProgressData_SetAborted(xdlg->secondProgress, 1);
517 
518  xdlg->stayOpen=1;
519  GWEN_Dialog_SetIntProperty(dlg, "abortButton", GWEN_DialogProperty_Enabled, 0, 0, 0);
520  GWEN_Dialog_SetIntProperty(dlg, "closeButton", GWEN_DialogProperty_Enabled, 0, 1, 0);
521 
523  }
524 
526 }
527 
528 
529 
530 
533  const char *sender) {
534  GWEN_DLGPROGRESS *xdlg;
535 
536  assert(dlg);
537  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, GWEN_DLGPROGRESS, dlg);
538  assert(xdlg);
539 
540  switch(t) {
544 
548 
550  break;
551 
553  return GWEN_DlgProgress_HandleActivated(dlg, sender);
554 
557 
559  if (xdlg->allowClose==0)
561  else
563 
566  }
567 
569 
570 }
571 
572 
573 
574 
575 
576