GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to run external app and get the return data?

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Joel
Familiar Face


Joined: 06 Apr 2008
Posts: 32

PostPosted: Thu Jun 12, 2008 2:42 pm    Post subject: How to run external app and get the return data? Reply with quote

In perl-gtk I can run apps and get the return data:
Code: (Perl)
1
2
3
4
5

my $data = `md5sum somefile.deb`;
# text1 is an entry widget
text1->set_text($data);

Is there a function in gtk binary development with the same effect?
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 474
Location: Portland, OR USA

PostPosted: Thu Jun 12, 2008 3:59 pm    Post subject: Reply with quote

Do you mean in C programming? If so, check out Execute command into a GtkTextView (C and Libglade)
Back to top
Joel
Familiar Face


Joined: 06 Apr 2008
Posts: 32

PostPosted: Thu Jun 12, 2008 9:35 pm    Post subject: Reply with quote

Thanks..that help..this is my sub final code:
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#include <gtk/gtk.h>

typedef struct {
    GtkWidget *edit1;
    GtkWidget *edit2;
    GtkWidget *filesel;
} gData, *PGDATA;

gData myWidgets = {0};

void OnWindowDestroy(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
}

void OnFileSelCancel(GtkWidget *w, gpointer fs) {
    gtk_widget_destroy(GTK_WIDGET(fs));
}

void OnFileSelOk(GtkWidget *w, gpointer data) {
    gchar *file;
    gchar *command;
    gchar *outmd5;
   
    file = g_strdup_printf("%s", gtk_file_selection_get_filename(GTK_FILE_SELECTION(((PGDATA)data)->filesel)));
    gtk_entry_set_text(GTK_ENTRY(((PGDATA)data)->edit1), file);
    gtk_widget_destroy(GTK_WIDGET(((PGDATA)data)->filesel));
   
    command = g_strdup_printf("md5sum \"%s\"", file);
   
    g_spawn_command_line_sync(command, &outmd5, 0, 0, 0);
    gtk_entry_set_text(GTK_ENTRY(((PGDATA)data)->edit2), outmd5);
   
    g_free(outmd5);
    g_free(command);
    g_free(file);
}

void OnButton1Clicked(GtkWidget *widget, gpointer data) {
    ((gData*)data)->filesel = gtk_file_selection_new("");
    g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(((gData*)data)->filesel)), "destroy", G_CALLBACK(OnFileSelCancel), ((gData*)data)->filesel);
    g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(((gData*)data)->filesel)->cancel_button), "clicked", G_CALLBACK(OnFileSelCancel), ((gData*)data)->filesel);
    g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(((gData*)data)->filesel)->ok_button), "clicked", G_CALLBACK(OnFileSelOk), data);
    gtk_widget_show(((gData*)data)->filesel);
}

int main(int argc, char *argv[]) {
    GtkWidget *Window;
    GtkWidget *panel;
    GtkWidget *label1;
    GtkWidget *part1;
    GtkWidget *button1;
    GtkWidget *label2;
   
    gtk_init(&argc, &argv);
    Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(Window), GTK_WIN_POS_CENTER);
    //gtk_window_set_default_size(GTK_WINDOW(Window), 350, 120); // This doesn't worlk
   
gtk_window_set_resizable(GTK_WINDOW(Window), FALSE);
    gtk_window_set_title(GTK_WINDOW(Window), "md5 - chica");
    g_signal_connect(G_OBJECT(Window), "destroy", G_CALLBACK(OnWindowDestroy), NULL);
   
    panel = gtk_vbox_new(TRUE, 0);
    gtk_container_add(GTK_CONTAINER(Window), panel);
   
    label1 = gtk_label_new("Ingresa el fichero a obtener MD5:");
    gtk_box_pack_start(GTK_BOX(panel), label1, TRUE, TRUE, 0);
   
    part1 = gtk_hbox_new(TRUE, 0);
    gtk_box_pack_start(GTK_BOX(panel), part1, TRUE, TRUE, 0);
   
    myWidgets.edit1 = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(part1), myWidgets.edit1, TRUE, TRUE, 0);
   
    button1 = gtk_button_new_from_stock("gtk-find");
    g_signal_connect(G_OBJECT(button1), "clicked", G_CALLBACK(OnButton1Clicked), &myWidgets);
    gtk_box_pack_start(GTK_BOX(part1), button1, TRUE, FALSE, 0);
   
    label2 = gtk_label_new("Salida:");
    gtk_box_pack_start(GTK_BOX(panel), label2, FALSE, FALSE, 0);
   
    myWidgets.edit2 = gtk_entry_new_with_max_length(32);
    gtk_box_pack_start(GTK_BOX(panel), myWidgets.edit2, FALSE, FALSE, 0);
   
    gtk_widget_show_all(Window);
    gtk_main();
}

I want the window to be 350 X 120 and void the user to resize it, any ideas?
Also ideas to improve my above code are welcome :)
Back to top
dreblen
GTK+ Guru


Joined: 14 Jun 2007
Posts: 207
Location: Falun, WI USA

PostPosted: Fri Jun 13, 2008 12:11 am    Post subject: Reply with quote

you can try using gtk_window_resize and gtk_window_set_resizable:
http://library.gnome.org/devel/gtk/stable/GtkWindow.html#gtk-window-resize
http://library.gnome.org/devel/gtk/stable/GtkWindow.html#gtk-window-set-resizable
if gtk_window_set_default_size didn't work, you might need to call it after the widget is realized (either by showing it or by calling gtk_widget_realize)
Back to top
Joel
Familiar Face


Joined: 06 Apr 2008
Posts: 32

PostPosted: Fri Jun 13, 2008 12:32 am    Post subject: Reply with quote

dreblen wrote:
you can try using gtk_window_resize and gtk_window_set_resizable:
http://library.gnome.org/devel/gtk/stable/GtkWindow.html#gtk-window-resize
http://library.gnome.org/devel/gtk/stable/GtkWindow.html#gtk-window-set-resizable
if gtk_window_set_default_size didn't work, you might need to call it after the widget is realized (either by showing it or by calling gtk_widget_realize)

Yeah, using in this order:
Code: (C)
1
2
3
4

gtk_window_resize(GTK_WINDOW(Window), 350, 120);
gtk_window_set_resizable(GTK_WINDOW(Window), FALSE);

worked :D
Thanks!
Back to top
Joel
Familiar Face


Joined: 06 Apr 2008
Posts: 32

PostPosted: Fri Jun 13, 2008 1:04 am    Post subject: Reply with quote

No, it didn't.. :(

/edit:
But this did:
Code: (C)
1
2
3
4

gtk_widget_set_size_request(GTK_WIDGET(Window), 450, 120);
gtk_window_set_resizable(GTK_WINDOW(Window), FALSE);
Back to top
Vadi
GTK+ Geek


Joined: 28 May 2008
Posts: 68

PostPosted: Fri Jun 13, 2008 2:58 am    Post subject: Reply with quote

Won't the spawn_sync freeze up for app while it's doing the md5sum? I think a thread and an async is what you're looking for (if you want your app to be always responsive and whatnot).
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 474
Location: Portland, OR USA

PostPosted: Fri Jun 13, 2008 5:07 am    Post subject: Reply with quote

Yes, as the names imply, using g_spawn_sync your application has to wait for it to finish (synchronous) where as g_spawn_async is asynchronous but can be a bit more confusing to new programmers.

If even more functionality is needed, a GIOChannel can be quite usesful and is worth looking over.
Back to top
Joel
Familiar Face


Joined: 06 Apr 2008
Posts: 32

PostPosted: Fri Jun 13, 2008 1:40 pm    Post subject: Reply with quote

I have a problem and I don't think that is my substring function :\
My substr function:
Code: (C)
1
2
3
4
5
6
7
8

char* substr(const char *str, int start, int end) {
    if (end < 1) end = strlen(str) + 1;
    char *buffer = malloc(end+1);
    strncpy(buffer, str + start, end);
    return buffer;
}

Now the critical part of my code:
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12

gchar *command;
gchar *outmd5;
char *md5;
command = g_strdup_printf("md5sum \"%s\"", "some_file.deb");
g_spawn_command_line_sync(command, &outmd5, 0, 0, 0);
md5 = substr(outmd5, 0, 32);
gtk_entry_set_text(GTK_ENTRY(somentry), md5);
free(md5);
g_free(outmd5);
g_free(command);

The above code, md5 always returns the md5 code + garbage, something like:
11139cfd35b803ff1fb286e847f5761dBVkt
Any ideas?
Back to top
dreblen
GTK+ Guru


Joined: 14 Jun 2007
Posts: 207
Location: Falun, WI USA

PostPosted: Fri Jun 13, 2008 3:16 pm    Post subject: Reply with quote

I think that your problem might be fixed by this:
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
char* substr(const char *str, int start, int end)
{
    /* some compilers don't like putting code before variable declarations */
   
char *buffer;
    if(end < 1) end = strlen(str) + 1;
    /* you should typecast the void* that malloc returns */
   
buffer = (char*)malloc(end+1);
    strncpy(buffer, str+start, end);
    /* this is new */
   
buffer[end] = '\0';
    return buffer;
}
Back to top
Joel
Familiar Face


Joined: 06 Apr 2008
Posts: 32

PostPosted: Fri Jun 13, 2008 6:39 pm    Post subject: Reply with quote

Thanks, that worked :)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP