[Frugalware-git] fwsetup-ng: add progress dialog system for newt

James Buren ryuo at frugalware.org
Fri Aug 31 06:37:25 CEST 2012


Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=4fa1368e2bc13885685b9b347a27fa57511d8237

commit 4fa1368e2bc13885685b9b347a27fa57511d8237
Author: James Buren <ryuo at frugalware.org>
Date:   Thu Aug 30 23:36:54 2012 -0500

add progress dialog system for newt

diff --git a/src/local.h b/src/local.h
index 3b20356..b282542 100644
--- a/src/local.h
+++ b/src/local.h
@@ -25,6 +25,8 @@
#define GIBIBYTE (1LL << 30LL)
#define TEBIBYTE (1LL << 40LL)

+typedef bool (*ui_dialog_progress_callback) (char *text,size_t n,int *percent,void *data);
+
extern bool mkdir_recurse(const char *path);
extern bool size_to_string(char *s,size_t n,long long size);
extern int get_text_screen_width(const char *s);
@@ -33,5 +35,6 @@ extern bool get_button_screen_size(const char *text,int *width,int *height);
extern int ui_main(int argc,char **argv);
extern void ui_dialog_text(const char *title,const char *text);
extern bool ui_dialog_yesno(const char *title,const char *text,bool defaultno);
+extern bool ui_dialog_progress(const char *title,ui_dialog_progress_callback cb,void *data);
extern FILE *logfile;
extern int main(int argc,char **argv);
diff --git a/src/ui_newt.c b/src/ui_newt.c
index 7076892..de4e60a 100644
--- a/src/ui_newt.c
+++ b/src/ui_newt.c
@@ -151,3 +151,76 @@ extern bool ui_dialog_yesno(const char *title,const char *text,bool defaultno)

return result;
}
+
+extern bool ui_dialog_progress(const char *title,ui_dialog_progress_callback cb,void *data)
+{
+  char text[NEWT_WIDTH + 1] = {0};
+  int percent = 0;
+  newtComponent label = 0;
+  newtComponent scale = 0;
+  newtComponent form = 0;
+  struct newtExitStruct es = {0};
+  bool result = false;
+
+  if(title == 0 || cb == 0)
+  {
+    errno = EINVAL;
+    fprintf(logfile,"%s: %s\n",__func__,strerror(errno));
+    return false;
+  }
+
+  if(!cb(text,NEWT_WIDTH + 1,&percent,data))
+  {
+    fprintf(logfile,_("Progress dialog callback canceled the operation.\n"));
+    return false;
+  }
+
+  if(newtCenteredWindow(NEWT_WIDTH,3,title) != 0)
+  {
+    fprintf(logfile,_("Failed to open a NEWT window.\n"));
+    return false;
+  }
+
+  label = newtLabel(0,0,text);
+
+  scale = newtScale(0,2,NEWT_WIDTH,100);
+
+  newtScaleSet(scale,percent);
+
+  form = newtForm(0,0,NEWT_FLAG_NOF12);
+
+  newtFormAddComponents(form,label,scale,(void *) 0);
+
+  newtFormSetTimer(form,100);
+
+  while(true)
+  {
+    newtFormRun(form,&es);
+
+    if(es.reason == NEWT_EXIT_TIMER)
+    {
+      if(!cb(text,NEWT_WIDTH + 1,&percent,data))
+      {
+        fprintf(logfile,_("Progress dialog callback canceled the operation.\n"));
+        result = false;
+        break;
+      }
+
+      newtLabelSetText(label,text);
+
+      newtScaleSet(scale,percent);
+    }
+
+    if(percent > 100)
+    {
+      result = true;
+      break;
+    }
+  }
+
+  newtFormDestroy(form);
+
+  newtPopWindow();
+
+  return result;
+}


More information about the Frugalware-git mailing list