Diff of /trunk/src/geotoad.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 206 by harbaum, Tue Nov 24 12:42:31 2009 UTC revision 207 by harbaum, Tue Nov 24 14:02:35 2009 UTC
# Line 51  typedef struct { Line 51  typedef struct {
51    appdata_t *appdata;    appdata_t *appdata;
52    
53    GPtrArray *argv;    GPtrArray *argv;
54    gchar *info;    gchar *info, *color;
55    GMutex *update_mutex;    GMutex *update_mutex;
56    GCond *update_cond;    GCond *update_cond;
57    
# Line 76  static void arg_free(gpointer data, gpoi Line 76  static void arg_free(gpointer data, gpoi
76  }  }
77    
78  static void context_free(gt_context_t *context) {  static void context_free(gt_context_t *context) {
79    printf("freeing context\n");    //  printf("freeing context\n");
80    
81    if(context->info) g_free(context->info);    if(context->info) g_free(context->info);
82    
# Line 93  static void appendf(struct log_s *log, c Line 93  static void appendf(struct log_s *log, c
93    char *buf = g_strdup_vprintf(fmt, args);    char *buf = g_strdup_vprintf(fmt, args);
94    va_end( args );    va_end( args );
95    
96    //   printf("append: %s", buf);    //  printf("append: %s", buf);
97    
98    GtkTextTag *tag = NULL;    GtkTextTag *tag = NULL;
99    if(colname)    if(colname)
# Line 118  static void appendf(struct log_s *log, c Line 118  static void appendf(struct log_s *log, c
118  // update the gui with the required info.  // update the gui with the required info.
119  gboolean cb_update_job(gt_context_t *context) {  gboolean cb_update_job(gt_context_t *context) {
120    if (context->info) {    if (context->info) {
     char *color = NULL;  
     if(strstr(context->info, "Saved to ") != NULL) {  
       context->state = GT_STATE_SAVE_FOUND;  
       color = COLOR_OK;  
     }  
121    
122      if(strcasestr(context->info, "error") != NULL)      /* check if client reports "saved to ..." */
123        color = COLOR_ERR;      if(strstr(context->info, "Saved to "))
124          context->state = GT_STATE_SAVE_FOUND;
125    
126      appendf(&context->log, color, context->info);      appendf(&context->log, context->color, context->info);
127    
128      g_free(context->info);      g_free(context->info);
129      context->info = NULL;      context->info = NULL;
# Line 143  gboolean cb_update_job(gt_context_t *con Line 139  gboolean cb_update_job(gt_context_t *con
139    
140  // A helper function run in the job thread receiving the string that  // A helper function run in the job thread receiving the string that
141  // should be displayed in the textview.  // should be displayed in the textview.
142  void job_add_to_text_viewer(gt_context_t *context,  void append_parentf(gt_context_t *context, char *colname,
143                              const char *info)               const char *fmt, ...) {
144  {    va_list args;
145    /* TODO: make this cope with format strings */    va_start( args, fmt );
146    /* TODO: make this cope with colors */    context->info = g_strdup_vprintf(fmt, args);
147    context->info = g_strdup(info);    va_end( args );
148    
149      if(colname)
150        context->color = colname;
151      else
152        context->color = NULL;
153    
154    // Lock mutex to make sure that we will receive the condition signal    // Lock mutex to make sure that we will receive the condition signal
155    g_mutex_lock(context->update_mutex);    g_mutex_lock(context->update_mutex);
156    
157    g_idle_add((GSourceFunc)cb_update_job, context);    g_idle_add((GSourceFunc)cb_update_job, context);
158    
159    // Wait for cb_update_job to tell me that the update is done    // Wait for cb_update_job to tell me that the update is done
160    g_cond_wait(context->update_cond,    g_cond_wait(context->update_cond, context->update_mutex);
               context->update_mutex);  
161    g_mutex_unlock(context->update_mutex);    g_mutex_unlock(context->update_mutex);
162  }  }
163    
164  /* custom version of popen to get access to the pid and to be able */  /* custom version of popen to get access to the pid and to be able */
165  /* to slaughter the child process */  /* to slaughter the child process */
166  static FILE *gt_popen(char **args, const char *type, long *tid) {  static FILE *gt_popen(gt_context_t *context, char **args,
167                          const char *type, long *tid) {
168    int   p[2];    int   p[2];
169    FILE *fp;    FILE *fp;
170    
# Line 207  static FILE *gt_popen(char **args, const Line 209  static FILE *gt_popen(char **args, const
209    
210      execve(args[0], args, NULL);      execve(args[0], args, NULL);
211    
212      printf("gt_popen(): execve(%s) failed!\n", args[0]);      /* this printf will actually be redirected through the pipe */
213        printf("Error: Failed to execute!\n");
214    } else {         /* we're having major problems... */    } else {         /* we're having major problems... */
215      close(p[0]);      close(p[0]);
216      close(p[1]);      close(p[1]);
217      printf("gt_popen(): fork() failure!\n");  
218        /* this printf will actually be redirected through the pipe */
219        printf("Error: Failed to fork!\n");
220    }    }
221    
222    return NULL;    return NULL;
# Line 228  static gpointer thread_worker(gt_context Line 233  static gpointer thread_worker(gt_context
233    gsize terminator_pos;    gsize terminator_pos;
234    gchar *str_return;    gchar *str_return;
235    
236    fh = gt_popen((char**)(context->argv->pdata),"r", &context->pid);    fh = gt_popen(context, (char**)(context->argv->pdata),"r", &context->pid);
237    if(!fh) {    if(!fh) {
238      appendf(&context->log, COLOR_ERR, _("Failed to run GeoToad"));      append_parentf(context, COLOR_ERR, _("Failed to run GeoToad"));
239      return NULL;      return NULL;
240    }    }
241    
# Line 245  static gpointer thread_worker(gt_context Line 250  static gpointer thread_worker(gt_context
250                                            &length,                                            &length,
251                                            &terminator_pos,                                            &terminator_pos,
252                                            &error)) == G_IO_STATUS_NORMAL) {                                            &error)) == G_IO_STATUS_NORMAL) {
253      job_add_to_text_viewer(context, str_return);      char *color = NULL;
254        if(strstr(str_return, "Saved to "))  color = COLOR_OK;
255        if(strcasestr(str_return, "error"))  color = COLOR_ERR;
256    
257        append_parentf(context, color, str_return);
258      g_free(str_return);      g_free(str_return);
259    }    }
260    
261    g_io_channel_unref(gh);    g_io_channel_unref(gh);
262    pclose(fh);    pclose(fh);
263    job_add_to_text_viewer(context, "Job done!");    append_parentf(context, COLOR_SYSTEM, "Job done!");
264    
265    g_thread_exit(NULL);    g_thread_exit(NULL);
266    context_free(context);    context_free(context);
# Line 273  static void run(gt_context_t *context) { Line 282  static void run(gt_context_t *context) {
282    
283    /* build list of arguments to call geotoad */    /* build list of arguments to call geotoad */
284    context->argv = g_ptr_array_new();    context->argv = g_ptr_array_new();
285    g_ptr_array_add (context->argv, g_strdup_printf(GEOTOAD "a"));    g_ptr_array_add (context->argv, g_strdup_printf(GEOTOAD));
286    g_ascii_dtostr(str, sizeof(str), context->appdata->gt.distance);    g_ascii_dtostr(str, sizeof(str), context->appdata->gt.distance);
287    g_ptr_array_add (context->argv, g_strdup_printf("--distanceMax=%s", str));    g_ptr_array_add (context->argv, g_strdup_printf("--distanceMax=%s", str));
288    g_ptr_array_add (context->argv, g_strdup_printf("--output=%s", context->appdata->gt.filename));    g_ptr_array_add (context->argv, g_strdup_printf("--output=%s", context->appdata->gt.filename));

Legend:
Removed from v.206  
changed lines
  Added in v.207