Ejemplo n.º 1
0
/* depending on type of filter, often only vtl or track will be given.
 * the other can be NULL.
 */
static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikDataSourceInterface *source_interface,
                      VikTrwLayer *vtl, VikTrack *track )
{
    /* for manual dialogs */
    GtkWidget *dialog = NULL;
    GtkWidget *status;
    gchar *cmd, *extra;
    gchar *cmd_off = NULL;
    gchar *extra_off = NULL;
    acq_dialog_widgets_t *w;
    gpointer user_data;

    /* for UI builder */
    gpointer pass_along_data;
    VikLayerParamData *paramdatas = NULL;

    w_and_interface_t *wi;

    /*** INIT AND CHECK EXISTENCE ***/
    if ( source_interface->init_func )
        user_data = source_interface->init_func();
    else
        user_data = NULL;
    pass_along_data = user_data;

    if ( source_interface->check_existence_func ) {
        gchar *error_str = source_interface->check_existence_func();
        if ( error_str ) {
            a_dialog_error_msg ( GTK_WINDOW(vw), error_str );
            g_free ( error_str );
            return;
        }
    }

    /* BUILD UI & GET OPTIONS IF NECESSARY. */

    /* POSSIBILITY 0: NO OPTIONS. DO NOTHING HERE. */
    /* POSSIBILITY 1: ADD_SETUP_WIDGETS_FUNC */
    if ( source_interface->add_setup_widgets_func ) {
        dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );

        gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
        GtkWidget *response_w = NULL;
#if GTK_CHECK_VERSION (2, 20, 0)
        response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
#endif

        source_interface->add_setup_widgets_func(dialog, vvp, user_data);
        gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );

        if ( response_w )
            gtk_widget_grab_focus ( response_w );

        if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
            source_interface->cleanup_func(user_data);
            gtk_widget_destroy(dialog);
            return;
        }
    }
    /* POSSIBILITY 2: UI BUILDER */
    else if ( source_interface->params ) {
        paramdatas = a_uibuilder_run_dialog ( source_interface->window_title, GTK_WINDOW(vw),
                                              source_interface->params, source_interface->params_count,
                                              source_interface->params_groups, source_interface->params_groups_count,
                                              source_interface->params_defaults );
        if ( paramdatas )
            pass_along_data = paramdatas;
        else
            return; /* TODO: do we have to free anything here? */
    }

    /* CREATE INPUT DATA & GET COMMAND STRING */

    if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER ) {
        gchar *name_src = write_tmp_trwlayer ( vtl );

        ((VikDataSourceGetCmdStringFuncWithInput) source_interface->get_cmd_string_func)
        ( pass_along_data, &cmd, &extra, name_src );

        g_free ( name_src );
        /* TODO: delete the tmp file? or delete it only after we're done with it? */
    } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK ) {
        gchar *name_src = write_tmp_trwlayer ( vtl );
        gchar *name_src_track = write_tmp_track ( track );

        ((VikDataSourceGetCmdStringFuncWithInputInput) source_interface->get_cmd_string_func)
        ( pass_along_data, &cmd, &extra, name_src, name_src_track );

        g_free ( name_src );
        g_free ( name_src_track );
    } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRACK ) {
        gchar *name_src_track = write_tmp_track ( track );

        ((VikDataSourceGetCmdStringFuncWithInput) source_interface->get_cmd_string_func)
        ( pass_along_data, &cmd, &extra, name_src_track );

        g_free ( name_src_track );
    } else
        source_interface->get_cmd_string_func ( pass_along_data, &cmd, &extra );

    /* Get data for Off command */
    if ( source_interface->off_func ) {
        source_interface->off_func ( pass_along_data, &cmd_off, &extra_off );
    }

    /* cleanup for option dialogs */
    if ( source_interface->add_setup_widgets_func ) {
        gtk_widget_destroy(dialog);
        dialog = NULL;
    } else if ( source_interface->params ) {
        a_uibuilder_free_paramdatas ( paramdatas, source_interface->params, source_interface->params_count );
    }

    /*** LET'S DO IT! ***/

    if ( ! cmd )
        return;

    w = g_malloc(sizeof(*w));
    wi = g_malloc(sizeof(*wi));
    wi->w = w;
    wi->w->source_interface = source_interface;
    wi->cmd = cmd;
    wi->extra = extra; /* usually input data type (?) */

    dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
    gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT, FALSE );
    gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );


    w->dialog = dialog;
    w->ok = TRUE;
    status = gtk_label_new (_("Status: detecting gpsbabel"));
    gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), status, FALSE, FALSE, 5 );
    gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
    gtk_widget_show_all(status);
    w->status = status;

    w->vw = vw;
    w->vlp = vlp;
    w->vvp = vvp;
    if ( source_interface->add_progress_widgets_func ) {
        source_interface->add_progress_widgets_func ( dialog, user_data );
    }
    w->user_data = user_data;


    g_thread_create((GThreadFunc)get_from_anything, wi, FALSE, NULL );

    gtk_dialog_run ( GTK_DIALOG(dialog) );
    if ( w->ok )
        w->ok = FALSE; /* tell thread to stop. TODO: add mutex */
    else {
        if ( cmd_off ) {
            /* Turn off */
            a_babel_convert_from (NULL, cmd_off, NULL, extra_off, NULL);
        }
        g_free ( w ); /* thread has finished; free w */
    }
    gtk_widget_destroy ( dialog );
}
Ejemplo n.º 2
0
/* depending on type of filter, often only vtl or track will be given.
 * the other can be NULL.
 */
static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikDataSourceInterface *source_interface,
		      VikTrwLayer *vtl, VikTrack *track )
{
  /* for manual dialogs */
  GtkWidget *dialog = NULL;
  GtkWidget *status;
  gchar *cmd = NULL;
  gchar *extra = NULL;
  gchar *cmd_off = NULL;
  gchar *extra_off = NULL;
  acq_dialog_widgets_t *w;
  gpointer user_data;
  gpointer options = NULL;

  /* for UI builder */
  gpointer pass_along_data;
  VikLayerParamData *paramdatas = NULL;

  w_and_interface_t *wi;

  /*** INIT AND CHECK EXISTENCE ***/
  if ( source_interface->init_func )
    user_data = source_interface->init_func();
  else
    user_data = NULL;
  pass_along_data = user_data;

  if ( source_interface->check_existence_func ) {
    gchar *error_str = source_interface->check_existence_func();
    if ( error_str ) {
      a_dialog_error_msg ( GTK_WINDOW(vw), error_str );
      g_free ( error_str );
      return;
    }
  }    

  /* BUILD UI & GET OPTIONS IF NECESSARY. */

  /* POSSIBILITY 0: NO OPTIONS. DO NOTHING HERE. */
  /* POSSIBILITY 1: ADD_SETUP_WIDGETS_FUNC */
  if ( source_interface->add_setup_widgets_func ) {
    dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );

    gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
    GtkWidget *response_w = NULL;
#if GTK_CHECK_VERSION (2, 20, 0)
    response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
#endif

    source_interface->add_setup_widgets_func(dialog, vvp, user_data);
    gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );

    if ( response_w )
      gtk_widget_grab_focus ( response_w );

    if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
      source_interface->cleanup_func(user_data);
      gtk_widget_destroy(dialog);
      return;
    }
  }
  /* POSSIBILITY 2: UI BUILDER */
  else if ( source_interface->params ) {
    paramdatas = a_uibuilder_run_dialog ( source_interface->window_title, GTK_WINDOW(vw),
			source_interface->params, source_interface->params_count,
			source_interface->params_groups, source_interface->params_groups_count,
			source_interface->params_defaults );
    if ( paramdatas )
      pass_along_data = paramdatas;
    else
      return; /* TODO: do we have to free anything here? */
  }

  /* CREATE INPUT DATA & GET COMMAND STRING */

  if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER ) {
    gchar *name_src = write_tmp_trwlayer ( vtl );

    ((VikDataSourceGetCmdStringFuncWithInput) source_interface->get_cmd_string_func)
	( pass_along_data, &cmd, &extra, name_src );

    g_free ( name_src );
    /* TODO: delete the tmp file? or delete it only after we're done with it? */
  } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK ) {
    gchar *name_src = write_tmp_trwlayer ( vtl );
    gchar *name_src_track = write_tmp_track ( track );

    ((VikDataSourceGetCmdStringFuncWithInputInput) source_interface->get_cmd_string_func)
	( pass_along_data, &cmd, &extra, name_src, name_src_track );

    g_free ( name_src );
    g_free ( name_src_track );
  } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRACK ) {
    gchar *name_src_track = write_tmp_track ( track );

    ((VikDataSourceGetCmdStringFuncWithInput) source_interface->get_cmd_string_func)
	( pass_along_data, &cmd, &extra, name_src_track );

    g_free ( name_src_track );
  } else if ( source_interface->get_cmd_string_func )
    source_interface->get_cmd_string_func ( pass_along_data, &cmd, &extra, &options );

  /* Get data for Off command */
  if ( source_interface->off_func ) {
    source_interface->off_func ( pass_along_data, &cmd_off, &extra_off );
  }

  /* cleanup for option dialogs */
  if ( source_interface->add_setup_widgets_func ) {
    gtk_widget_destroy(dialog);
    dialog = NULL;
  } else if ( source_interface->params ) {
    a_uibuilder_free_paramdatas ( paramdatas, source_interface->params, source_interface->params_count );
  }

  w = g_malloc(sizeof(*w));
  wi = g_malloc(sizeof(*wi));
  wi->w = w;
  wi->w->source_interface = source_interface;
  wi->cmd = cmd;
  wi->extra = extra; /* usually input data type (?) */
  wi->options = options;
  wi->vtl = vtl;
  wi->creating_new_layer = (!vtl);

  dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
  gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT, FALSE );
  gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );

  w->dialog = dialog;
  w->running = TRUE;
  status = gtk_label_new (_("Working..."));
  gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), status, FALSE, FALSE, 5 );
  gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
  // May not want to see the dialog at all
  if ( source_interface->is_thread || source_interface->keep_dialog_open )
    gtk_widget_show_all(dialog);
  w->status = status;

  w->vw = vw;
  w->vlp = vlp;
  w->vvp = vvp;
  if ( source_interface->add_progress_widgets_func ) {
    source_interface->add_progress_widgets_func ( dialog, user_data );
  }
  w->user_data = user_data;

  if (source_interface->mode == VIK_DATASOURCE_ADDTOLAYER) {
    VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
    if ( IS_VIK_TRW_LAYER(current_selected) ) {
      wi->vtl = VIK_TRW_LAYER(current_selected);
      wi->creating_new_layer = FALSE;
    }
  }
  else if ( source_interface->mode == VIK_DATASOURCE_MANUAL_LAYER_MANAGEMENT ) {
    // Don't create in acquire - as datasource will perform the necessary actions
    wi->creating_new_layer = FALSE;
    VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
    if ( IS_VIK_TRW_LAYER(current_selected) )
      wi->vtl = VIK_TRW_LAYER(current_selected);
  }
  if ( wi->creating_new_layer ) {
    wi->vtl = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, w->vvp, NULL, FALSE ) );
    vik_layer_rename ( VIK_LAYER ( wi->vtl ), _(source_interface->layer_title) );
  }

  if ( source_interface->is_thread ) {
    if ( cmd ) {
      g_thread_create((GThreadFunc)get_from_anything, wi, FALSE, NULL );
      gtk_dialog_run ( GTK_DIALOG(dialog) );
      if (w->running) {
        // Cancel and mark for thread to finish
        w->running = FALSE;
        // NB Thread will free memory
      } else {
        if ( cmd_off ) {
          /* Turn off */
          a_babel_convert_from (NULL, cmd_off, extra_off, NULL, NULL, NULL);
          g_free ( cmd_off );
        }
        if ( extra_off )
          g_free ( extra_off );

        // Thread finished by normal completion - free memory
        g_free ( w );
        g_free ( wi );
      }
    }
    else {
      // This shouldn't happen...
      gtk_label_set_text ( GTK_LABEL(w->status), _("Unable to create command\nAcquire method failed.") );
      gtk_dialog_run (GTK_DIALOG (dialog));
    }
  }
  else {
    // bypass thread method malarkly - you'll just have to wait...
    if ( source_interface->process_func ) {
      gboolean result = source_interface->process_func ( wi->vtl, cmd, extra, (BabelStatusFunc) progress_func, w, options );
      if ( !result )
        a_dialog_msg ( GTK_WINDOW(vw), GTK_MESSAGE_ERROR, _("Error: acquisition failed."), NULL );
    }
    g_free ( cmd );
    g_free ( extra );
    g_free ( options );

    on_complete_process ( wi );
    // Actually show it if necessary
    if ( wi->w->source_interface->keep_dialog_open )
      gtk_dialog_run ( GTK_DIALOG(dialog) );

    g_free ( w );
    g_free ( wi );
  }

  gtk_widget_destroy ( dialog );
}