GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 02, 2014 12:29 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: How to PUT the filename from the file button [SOLVED]
PostPosted: Wed Jun 20, 2012 3:25 pm 
Offline

Joined: Wed Jun 20, 2012 2:37 pm
Posts: 4
Hello to everyone!

I've created a small and simple (but useful for me) program in GTK2 & C.
It's a simple GUI to gpg encrypt program.

It's working, but only if I use a static file path. So, that's no good :(

What I missed? I've tried some code, but unsuccessfully.
I would like to use this name of choosen file as part of command (I use "system" command).

Can anyone help me to catch an idea how to get this?
This would be a really big help to understand it & use it in my other projects.
I'm still learning the coding, but as You can see, I've worked out for himself a clear writing style.

This is my code (some parts of program are remained in my native polish language) and You can use it fully to Yours projects.
Code:
/*     compile with command:

gcc -o ./Encryptor `pkg-config --cflags --libs gtk+-2.0` `pkg-config --libs gtk+-2.0` ./Encryptor.c

*/

#include <gtk/gtk.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>


//void encrypt_by_key_one(GtkWidget *widget, gpointer data)
void encrypt_by_key_one()
{
system ("gpg --encrypt -r GPG_KEY_ONE '/home/iona/Encryptor.c' &");
printf("Encrypted via key one!\n");
return 0;
}


//void encrypt_by_key_two(GtkWidget *widget, gpointer data)
void encrypt_by_key_two()
{
system ("gpg --encrypt -r GPG_KEY_TWO '/home/iona/Encryptor.c' &");
   printf("PEncrypted via key two!\n");
      return 0;
}



int main( int argc, char *argv[])
{
   gtk_init(&argc, &argv);


    /*    MAIN WINDOW PROGRAM   */
    GtkWidget *okno_programu = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_position(GTK_WINDOW(okno_programu), GTK_WIN_POS_CENTER);
   gtk_window_set_default_size(GTK_WINDOW(okno_programu), 250, 255);
   gtk_window_set_title(GTK_WINDOW(okno_programu), "Encryptor");
   gtk_container_set_border_width(GTK_CONTAINER(okno_programu), 5);
   g_signal_connect_swapped(G_OBJECT(okno_programu), "destroy",
         G_CALLBACK(gtk_main_quit), G_OBJECT(okno_programu));


    /*    CONTAINER 'FIXED'    */
    GtkWidget *fixed = gtk_fixed_new();
        gtk_container_add(GTK_CONTAINER(okno_programu), fixed);


    /*    FRAME FOR FILE   */
    GtkWidget *controls_frame = gtk_frame_new("  Choose file to encrypt  ");
        gtk_widget_set_size_request(controls_frame, 220, 85);
           gtk_frame_set_shadow_type(GTK_FRAME(controls_frame), GTK_SHADOW_ETCHED_IN);
               gtk_fixed_put(GTK_FIXED(fixed), controls_frame, 10, 10);


    /*    FILE BUTTON    */
   GtkWidget *przycisk_wyboru_pliku = gtk_file_chooser_button_new("przycisk_wyboru_pliku", GTK_FILE_CHOOSER_ACTION_OPEN);
      gtk_widget_set_size_request(przycisk_wyboru_pliku, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), przycisk_wyboru_pliku, 25, 40);

   
    /*    FRAME FOR KEY BUTTONS     */
    GtkWidget *controls_frame_2 = gtk_frame_new("  GPG  ");
        gtk_widget_set_size_request(controls_frame_2, 220, 130);
           gtk_frame_set_shadow_type(GTK_FRAME(controls_frame_2), GTK_SHADOW_ETCHED_IN);
               gtk_fixed_put(GTK_FIXED(fixed), controls_frame_2, 10, 100);


    /*    BUTTON FOR KEY ONE   */
    GtkWidget *szyfruj_kluczem_one = gtk_button_new_with_label("Encrypt via key one");
      gtk_widget_set_size_request(szyfruj_kluczem_one, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), szyfruj_kluczem_one, 25, 130);
      gtk_signal_connect_object (GTK_OBJECT (szyfruj_kluczem_one), "clicked",
         GTK_SIGNAL_FUNC (encrypt_by_key_one), NULL);


    /*    BUTTON FOR KEY TWO   */
    GtkWidget *szyfruj_kluczem_two = gtk_button_new_with_label("Encrypt via key two");
      gtk_widget_set_size_request(szyfruj_kluczem_two, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), szyfruj_kluczem_two, 25, 180);
      gtk_signal_connect_object (GTK_OBJECT (szyfruj_kluczem_two), "clicked",
         GTK_SIGNAL_FUNC (encrypt_by_key_two), NULL);


   gtk_widget_show_all(okno_programu);

   gtk_main();

   return 0;
}


And this is it:
Image



Cheers!

~ Iona


Last edited by Iona on Sat Jun 23, 2012 1:21 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: How to get filename from the gtk_file_chooser_button
PostPosted: Thu Jun 21, 2012 2:21 pm 
Offline

Joined: Wed Jun 20, 2012 2:37 pm
Posts: 4
Continued struggle...

Code:
/*     compile with command:

gcc -o ./Encryptor `pkg-config --cflags --libs gtk+-2.0` `pkg-config --libs gtk+-2.0` ./Encryptor.c

*/

#include <gtk/gtk.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>




   GtkWidget *przycisk_wyboru_pliku;

//void encrypt_by_key_one(GtkWidget *widget, gpointer data)       I don't shure, is it better than this line below?:
void encrypt_by_key_one()
{

if (gtk_dialog_run (GTK_DIALOG (przycisk_wyboru_pliku)) == GTK_RESPONSE_ACCEPT)
    {
        char *nazwa_pliku;

        nazwa_pliku = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (przycisk_wyboru_pliku));
        g_free (nazwa_pliku);
    }

    gtk_widget_destroy (przycisk_wyboru_pliku);


system ("gpg --encrypt -r GPG_KEY_ONE 'nazwa_pliku%' &")
   printf("Encrypted via key one!\n");
return 0;
}









//void encrypt_by_key_two(GtkWidget *widget, gpointer data)
void encrypt_by_key_two()
{
system ("gpg --encrypt -r GPG_KEY_TWO '/home/iona/test.c' &");
   printf("Encrypted via key two!\n");
      return 0;
}



int main( int argc, char *argv[])
{
   gtk_init(&argc, &argv);


    /*    MAIN WINDOW PROGRAM   */
    GtkWidget *okno_programu = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_position(GTK_WINDOW(okno_programu), GTK_WIN_POS_CENTER);
   gtk_window_set_default_size(GTK_WINDOW(okno_programu), 250, 255);
   gtk_window_set_title(GTK_WINDOW(okno_programu), "Encryptor");
   gtk_container_set_border_width(GTK_CONTAINER(okno_programu), 5);
   g_signal_connect_swapped(G_OBJECT(okno_programu), "destroy",
         G_CALLBACK(gtk_main_quit), G_OBJECT(okno_programu));


    /*    CONTAINER 'FIXED'    */
    GtkWidget *fixed = gtk_fixed_new();
        gtk_container_add(GTK_CONTAINER(okno_programu), fixed);


    /*    FRAME FOR FILE   */
    GtkWidget *controls_frame = gtk_frame_new("  Choose file to encrypt  ");
        gtk_widget_set_size_request(controls_frame, 220, 85);
           gtk_frame_set_shadow_type(GTK_FRAME(controls_frame), GTK_SHADOW_ETCHED_IN);
               gtk_fixed_put(GTK_FIXED(fixed), controls_frame, 10, 10);


    /*    FILE BUTTON    */
   GtkWidget *przycisk_wyboru_pliku = gtk_file_chooser_button_new("przycisk_wyboru_pliku", GTK_FILE_CHOOSER_ACTION_OPEN);
      gtk_widget_set_size_request(przycisk_wyboru_pliku, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), przycisk_wyboru_pliku, 25, 40);
//      g_signal_connect(G_OBJECT(przycisk_wyboru_pliku), "clicked", G_CALLBACK(przycisk_wyboru_pliku), (gpointer)okno_programu);

   
    /*    FRAME FOR KEY BUTTONS     */
    GtkWidget *controls_frame_2 = gtk_frame_new("  GPG  ");
        gtk_widget_set_size_request(controls_frame_2, 220, 130);
           gtk_frame_set_shadow_type(GTK_FRAME(controls_frame_2), GTK_SHADOW_ETCHED_IN);
               gtk_fixed_put(GTK_FIXED(fixed), controls_frame_2, 10, 100);


    /*    BUTTON FOR KEY ONE   */
    GtkWidget *szyfruj_kluczem_one = gtk_button_new_with_label("Encrypt via key one");
      gtk_widget_set_size_request(szyfruj_kluczem_one, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), szyfruj_kluczem_one, 25, 130);
      gtk_signal_connect_object (GTK_OBJECT (szyfruj_kluczem_one), "clicked",
         GTK_SIGNAL_FUNC (encrypt_by_key_one), NULL);


    /*    BUTTON FOR KEY TWO   */
    GtkWidget *szyfruj_kluczem_two = gtk_button_new_with_label("Encrypt via key two");
      gtk_widget_set_size_request(szyfruj_kluczem_two, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), szyfruj_kluczem_two, 25, 180);
      gtk_signal_connect_object (GTK_OBJECT (szyfruj_kluczem_two), "clicked",
         GTK_SIGNAL_FUNC (encrypt_by_key_two), NULL);


   gtk_widget_show_all(okno_programu);

   gtk_main();

   return 0;
}


As result, still get this:
Code:
gpg: filename%: encryption failed: no public key


Still have no idea how to put a file name from the chooser button into gpg command to replace
Quote:
filename%
by the file name path of choosen file...


Top
 Profile  
 
 Post subject: Re: How to PUT the filename from the gtk_file_chooser_button
PostPosted: Fri Jun 22, 2012 6:09 pm 
Offline

Joined: Fri Jun 22, 2012 5:49 pm
Posts: 3
Hi,

I'm new here so I hope you don't mind if I dive in and try and answer this.
I think you need to build your command using sprintf before calling the system command.

e.g.
Code:
char cmd[100];
sprintf(cmd, "gpg --encrypt -r GPG_KEY_ONE %s &", nazwa_pliku);
system(cmd);


Hope that helps.

John


Top
 Profile  
 
 Post subject: Re: How to PUT the filename from the gtk_file_chooser_button
PostPosted: Sat Jun 23, 2012 3:40 am 
Offline

Joined: Wed Jun 20, 2012 2:37 pm
Posts: 4
Hi John!
Absolutely, I don't mine :) Thank's for the answer!

Guided by your advice (and results of further research), I've modyfied the code:
Code:
/*     compile it with command:

gcc -o ./Encryptor `pkg-config --cflags --libs gtk+-2.0` `pkg-config --libs gtk+-2.0` ./Encryptor.c

*/

#include <gtk/gtk.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>




   GtkWidget *przycisk_wyboru_pliku;

  //void encrypt_by_key_one(GtkWidget *widget, gpointer data)    /*   <-- I don't shure is it better than this line below:  */

void encrypt_by_key_one()
{
   if (gtk_dialog_run (GTK_DIALOG (przycisk_wyboru_pliku)) == GTK_RESPONSE_ACCEPT)
      {
      char *nazwa_pliku;
      char cmd[100];
      nazwa_pliku = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (przycisk_wyboru_pliku));
      sprintf(cmd, "gpg --encrypt -r GPG_KEY_ONE '%s' &", nazwa_pliku);      //taken %s into apostro should be correct
      system(cmd);
      g_free (nazwa_pliku);                      /*   <-- without this I have segfault    */
      printf ("File encrypted!\n");
      }

   else
      {
      printf ("Encryption failed!\n");
      }

      gtk_widget_destroy (przycisk_wyboru_pliku);
}






void encrypt_by_key_two()
{
   printf("Encrypted via key two!\n");
}



int main( int argc, char *argv[])
{
   gtk_init(&argc, &argv);


    /*    MAIN WINDOW PROGRAM   */
    GtkWidget *okno_programu = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_position(GTK_WINDOW(okno_programu), GTK_WIN_POS_CENTER);
   gtk_window_set_default_size(GTK_WINDOW(okno_programu), 250, 255);
   gtk_window_set_title(GTK_WINDOW(okno_programu), "Encryptor");
   gtk_container_set_border_width(GTK_CONTAINER(okno_programu), 5);
   g_signal_connect_swapped(G_OBJECT(okno_programu), "destroy",
         G_CALLBACK(gtk_main_quit), G_OBJECT(okno_programu));


    /*    CONTAINER 'FIXED'    */
    GtkWidget *fixed = gtk_fixed_new();
        gtk_container_add(GTK_CONTAINER(okno_programu), fixed);


    /*    FRAME FOR FILE   */
    GtkWidget *controls_frame = gtk_frame_new("  Choose file to encrypt  ");
        gtk_widget_set_size_request(controls_frame, 220, 85);
           gtk_frame_set_shadow_type(GTK_FRAME(controls_frame), GTK_SHADOW_ETCHED_IN);
               gtk_fixed_put(GTK_FIXED(fixed), controls_frame, 10, 10);


    /*    FILE BUTTON    */
   GtkWidget *przycisk_wyboru_pliku = gtk_file_chooser_button_new("przycisk_wyboru_pliku", GTK_FILE_CHOOSER_ACTION_OPEN);
      gtk_widget_set_size_request(przycisk_wyboru_pliku, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), przycisk_wyboru_pliku, 25, 40);
      g_signal_connect(G_OBJECT(przycisk_wyboru_pliku), "file-set", G_CALLBACK(przycisk_wyboru_pliku), (gpointer)okno_programu);

   
    /*    FRAME FOR KEY BUTTONS     */
    GtkWidget *controls_frame_2 = gtk_frame_new("  GPG  ");
        gtk_widget_set_size_request(controls_frame_2, 220, 130);
           gtk_frame_set_shadow_type(GTK_FRAME(controls_frame_2), GTK_SHADOW_ETCHED_IN);
               gtk_fixed_put(GTK_FIXED(fixed), controls_frame_2, 10, 100);


    /*    BUTTON FOR KEY ONE   */
    GtkWidget *szyfruj_kluczem_one = gtk_button_new_with_label("Encrypt via key one");
      gtk_widget_set_size_request(szyfruj_kluczem_one, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), szyfruj_kluczem_one, 25, 130);
      gtk_signal_connect_object (GTK_OBJECT (szyfruj_kluczem_one), "clicked",
         GTK_SIGNAL_FUNC (encrypt_by_key_one), NULL);


    /*    BUTTON FOR KEY TWO   */
    GtkWidget *szyfruj_kluczem_two = gtk_button_new_with_label("Encrypt via key two");
      gtk_widget_set_size_request(szyfruj_kluczem_two, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), szyfruj_kluczem_two, 25, 180);
      gtk_signal_connect_object (GTK_OBJECT (szyfruj_kluczem_two), "clicked",
         GTK_SIGNAL_FUNC (encrypt_by_key_two), NULL);


   gtk_widget_show_all(okno_programu);

   gtk_main();

   return 0;
}


After applying those changes (and of course with real gpg key ID) I've this as result:
Quote:
(Encryptor:2984): Gtk-CRITICAL **: gtk_dialog_run: assertion `GTK_IS_DIALOG (dialog)' failed
Encryption failed!

(Encryptor:2984): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed



It must be a simple bug, but I don't see it :(
Furthermore, this signall connect any from the choose button here is correct?:
Code:
    /*    FILE BUTTON    */
   GtkWidget *przycisk_wyboru_pliku = gtk_file_chooser_button_new("przycisk_wyboru_pliku", GTK_FILE_CHOOSER_ACTION_OPEN);
      gtk_widget_set_size_request(przycisk_wyboru_pliku, 190, 35);
   gtk_fixed_put(GTK_FIXED(fixed), przycisk_wyboru_pliku, 25, 40);
      g_signal_connect(G_OBJECT(przycisk_wyboru_pliku), "file-set"", G_CALLBACK(przycisk_wyboru_pliku), (gpointer)okno_programu);


With signal connect enabled I have segfault...

I am a beginner but I fight on :)


Top
 Profile  
 
 Post subject: Re: How to PUT the filename from the gtk_file_chooser_button
PostPosted: Sat Jun 23, 2012 7:34 am 
Offline

Joined: Fri Jun 22, 2012 5:49 pm
Posts: 3
Hi,

Your connect for the file chooser button is not correct because the G_CALLBACK is being passed the button object when it needs to point to a callback function which will be called when the file has been selected.

I changed the connect signal line to:
Code:
g_signal_connect(G_OBJECT(przycisk_wyboru_pliku), "file-set", G_CALLBACK(file_button_callback), (gpointer)okno_programu);


then added the callback funtion as:
Code:
void file_button_callback(GtkFileChooser *fc, gpointer data)
{
    filename = gtk_file_chooser_get_filename(fc);
    printf("you selected the file %s\n", filename);
}


and then defined:
Code:
char *filename;


as a global variable at the top of the program.

After these changes you should be able to select a file and it will print the file you selected.

As you say the other problem is when you click the button to encrypt the file.
The problem here is in the encrypt_by_key_one function. This is calling a dialog_run which I don't think is needed. It just needs to take the filename saved above and encrypt it.

I changed encrypt_by_key_one function to:
Code:
void encrypt_by_key_one()
{   
    char cmd[100];
   
    sprintf(cmd, "gpg --encrypt -r GPG_KEY_ONE '%s' &", filename);      //taken %s into apostro should be correct
    system(cmd);
    printf("command is %s\n", cmd);   
    printf ("File encrypted!\n");     
}


I attached my version of the program.
Hope that helps.

John


Attachments:
File comment: File encryptor program with some bug fixes.
Encryptor.c.tar.gz [1.32 KiB]
Downloaded 65 times
Top
 Profile  
 
 Post subject: Re: How to PUT the filename from the gtk_file_chooser_button
PostPosted: Sat Jun 23, 2012 1:17 pm 
Offline

Joined: Wed Jun 20, 2012 2:37 pm
Posts: 4
Thank you very much John!

I see links now.
Your help will be very helpful to create more complex projects in future.

After your changes and by enabling command execute with real gpg key ID instead of "GPG_KEY_ONE":

Code:
sprintf(cmd, "gpg --encrypt -r GPG_KEY_ONE '%s' &", filename);
    system(cmd);

my program works as expected.

Only thing I would get is a simple message about succesful of file encryption procces, so I've made this void:
Code:
void show_info(GtkWidget *widget, gpointer window)
{
  GtkWidget *dialog;
  dialog = gtk_message_dialog_new(GTK_WINDOW(window),
            GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            "Encryption succesful :)", "title");
  gtk_window_set_title(GTK_WINDOW(dialog), "Information");
  gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_destroy(dialog);
}

But I have no idea how to clip it into prgram to get info after executed command. Still working on it :)


~~~ EDIT after few minutes

Got it!
Code:
void szyfrowanie_kluczem_two()
{
   char cmd[100];
   
    sprintf(cmd, "gpg --encrypt -r HERE_IS_GPG_KEY '%s' &", nazwa_pliku);
    system(cmd);
    printf("Komenda szyfrująca: '%s'\n", cmd);
    printf ("Plik został zaszyfrowany!\n");

   GtkWidget *okno_programu = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *dialog;
  dialog = gtk_message_dialog_new(GTK_WINDOW(okno_programu),
            GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            "File encrypted  :)", "title");
  gtk_window_set_title(GTK_WINDOW(dialog), "Information");
  gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_destroy(dialog);
}


John, thank you so much again! :)
Cheers!

~ Iona


Top
 Profile  
 
 Post subject: Re: How to PUT the filename from the file button [SOLVED]
PostPosted: Sat Jun 23, 2012 6:07 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 750
Location: UK
Hi,

Just been looking at your code and have spotted many problems.

- You are using the function gtk_signal_connect_object() to connect your signals. This was deprecated about 10 years ago when GTK+ moved from version 1.xx to 2.0 and has now been removed from GTK+ 3. You should now be using g_signal_connect()

- You have a memory leak as the memory allocated for the string obtained from gtk_file_chooser_get_filename() is never released.

- You are using a fixed size buffer to create your command line. You only need a file name with about 75 character to have a buffer over flow and possibly cause a crash or a security problem. You should consider using g_strdup_printf() which will allocate a string long enough for the command line, you will need to free the allocated memory with g_free() once it is not needed any more. I would also have checks to make sure that the entered file is a real file.

- The use of the GtkFixed widget to contain the other widgets and setting the exact sizes should be avoid except under certain conditions. Problems could be that under other themes or other user settings the text could be cut off or displayed incorrectly.

- The function system() is defined under <stdlib.h> and not <unistd.h>. Note is may be better to use one of the g_spawn_*() functions such as g_spawn_command_line_async(). This will execute the programme directly without launching a command shell as well.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group