0

as was suggested by the other post, i'll ask new question, more specific one.

GtkWidget *button[] /*to be a dynamic array */

void file_folder(GtkWidget *widget,  gpointer data)
{
  GtkWidget* dialog;

  dialog = gtk_file_chooser_dialog_new("Choose a file", GTK_WINDOW(data),
          GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_OK, GTK_RESPONSE_OK,
          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);

  gtk_widget_show_all(dialog);
  gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), "/home/demo/Downloads");
  gint resp = gtk_dialog_run(GTK_DIALOG(dialog));


  if(resp == GTK_RESPONSE_OK)
  {
      /*select file to execute*/
      g_print("Success\n");
      gtk_widget_destroy(dialog);
  }
  else
  {
      gtk_widget_destroy(dialog);
  }

}

How can I execute the file selected

and how can I create this dynamic array which will contain:

app name, app icon and executable (assuming that I am developing on ubuntu).

Barr J
  • 10,636
  • 1
  • 28
  • 46
  • I would say the next step would be to print the actual file name selected, rather than the plain `"Success"`, and to ensure you have a valid copy before closing the dialog. – Weather Vane May 06 '15 at 14:32
  • the success is for test issues, instead of the print the file should execute. – Barr J May 06 '15 at 15:15

2 Answers2

0

First get the file name of the selected file using gtk_file_chooser_get_filename().

gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));

After obtaining the file name of the file, you can refer to this answer and use fork() and execv() to execute the file.

As for your dynamic array, you need to use an array of a custom-defined structure. For example:

struct my_button {
    const char *app_name;
    GtkWidget *app_icon;
    const char *exec_path;
}

Then you can create a dynamic array of struct my_button. You can implement this yourself, and I'm sure you can find many tutorials online. Alternatively, you can use GLib (which GTK+ uses) and it provides a lot of dynamic data structures that you can use, e.g. GArray. Example:

struct my_button *b = malloc(sizeof *b);
b->app_name = "whatever"; // etc
GArray *arr = g_array_new(TRUE, TRUE, sizeof *b);
g_array_append_vals(arr, b, 1);

Remember to free the used memory after use.

For more details on GArray please refer to the documentation.

Community
  • 1
  • 1
user12205
  • 2,684
  • 1
  • 20
  • 40
  • Thanks for the quick response! when I use system, it gets the path and file but returns "permission denied". – Barr J May 07 '15 at 06:04
  • @user4702146 In a terminal, type `ls -l /path/to/file`, check the first column, it should be a combination of `-`, `r`, `w` and `x`. See if there is any `x`. If no, run `chmod +x /path/to/file` in the terminal, then run your program again. – user12205 May 07 '15 at 07:12
  • I mean, when I choose the file I want to run from the dialog (jpg, png image fop exmple) it throws Permission denied. – Barr J May 07 '15 at 10:56
  • @user4702146 That's because image files are not executable files. – user12205 May 07 '15 at 12:34
0

I found my solution!

execl("/usr/bin/xdg-open", "xdg-open", filename, (char *)0);

exit(1);

did the job.

Barr J
  • 10,636
  • 1
  • 28
  • 46