GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Nov 28, 2014 2:35 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Problem with g_timeout_add
PostPosted: Mon Jun 09, 2008 4:31 pm 
Offline
Familiar Face

Joined: Mon Jun 09, 2008 4:28 pm
Posts: 6
I noticed a strange behavior: if I use g_timeout_add(240, time_handler, NULL), my function (time_handler) is called every 240 milliseconds, and it's ok. But after 242 seconds, the timeout function expires. If I set an interval of 100 milliseconds, after 102 seconds the timeout is destroyed and will not be called again. If I set an interval of 50 milliseconds, after 52 seconds the timeout is destroyed and will not be called again, and so on. It seems that timeout is destroyed after interval time transformed into seconds + 2.

Why this happens?

Scorpion77


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 6:47 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
Just a couple of questions:
what type of system are you using? (linux, windows etc.)
does your callback follow the GSourceFunc prototype? http://library.gnome.org/devel/glib/sta ... SourceFunc
does this simple example quit after 12 seconds?
Code:
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>

gboolean timeout(gpointer data)
{
   printf("Hello! - %d\n", rand());
   return TRUE;
}

int main(int argc, char **argv)
{
   GMainLoop *ml;

   g_timeout_add(10, timeout, NULL);

   ml = g_main_loop_new(NULL, TRUE);
   g_main_loop_run(ml);

   return 0;
}

Thank you...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 9:22 am 
Offline
Familiar Face

Joined: Mon Jun 09, 2008 4:28 pm
Posts: 6
dreblen wrote:
Just a couple of questions:
what type of system are you using? (linux, windows etc.)


I'm using Slackware Linux 12.1

dreblen wrote:
does your callback follow the GSourceFunc prototype? http://library.gnome.org/devel/glib/sta ... SourceFunc


Yes, it does.

dreblen wrote:
does this simple example quit after 12 seconds?


No, it doesn't.

My program is for installing additional packages that are lacking in slackware, creating different users and doing other useful things. I use it for 2 computers, one in my room and one in lounge. It launches a bash script in background, that, after so many installed packages, writes in a temporary file that point has arrived, so time_handler reads the contents of the file and thus displays an installation progress bar. For a better understanding this is the source code of the program (consider that I am not an expert programmer):

Code:
#include <gtk/gtk.h>
#include <string.h>

static gboolean on_window_delete_event (GtkWidget*, GdkEvent*, gpointer);
GtkResponseType confirm_quit_dialog();
void closekde();

GtkWidget *win, *fix, *label, *button, *image, *win2, *radio1, *radio2;
GSList *group;

FILE *progressfile;
int progress=0;

gboolean gtk_toggle_button_get_active   (GtkToggleButton *toggle_button);

static gboolean
time_handler(GtkWidget *widget)
{

if ( ( progressfile = fopen( "/tmp/progress", "r" )) != NULL )
{
   fscanf(progressfile, "%d", &progress);
}

if ( progress==0 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress0.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==1 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress1.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==2 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress2.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==3 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress3.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==4 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress4.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==5 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress5.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==6 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress6.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==7 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress7.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==8 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress8.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress==9 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation status");
gtk_fixed_put (GTK_FIXED(fix), label, 140, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress9.png");
gtk_fixed_put (GTK_FIXED(fix), GTK_WIDGET(image), 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

else if ( progress>=10 )
{

gtk_container_remove (GTK_CONTAINER (win2), fix);
fix = gtk_fixed_new ();
label = gtk_label_new ("Installation complete successfully!");
gtk_fixed_put (GTK_FIXED(fix), label, 105, 50);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
image = gtk_image_new_from_file("/tmp/pictures/progress10.png");
gtk_fixed_put (GTK_FIXED(fix), image, 110, 120);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
label = gtk_label_new ("Click below to close the program");
gtk_fixed_put (GTK_FIXED(fix), label, 70, 170);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);
button = gtk_button_new_with_label ("Close");
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (chiudikde), (gpointer) win2);
gtk_fixed_put (GTK_FIXED(fix), button, 180, 210);
gtk_container_add (GTK_CONTAINER (win2), fix);
gtk_widget_show_all (win2);

}

}

void install()
{

  win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position (GTK_WINDOW(win2), GTK_WIN_POS_CENTER);
  gtk_window_set_title (GTK_WINDOW(win2), "Installing...");
  gtk_window_set_resizable (GTK_WINDOW(win2), FALSE);

  gtk_widget_set_size_request (win2, 420, 280);

  g_signal_connect (GTK_OBJECT(win), "destroy",
                    G_CALLBACK (gtk_main_quit), win2);

        g_signal_connect (G_OBJECT(win2), "delete-event",
                        G_CALLBACK (on_window_delete_event), NULL);


if (GTK_TOGGLE_BUTTON (radio1)->active)
{
   system( "touch /tmp/roompc" );
}

system( "rm /tmp/progress" );
system( "sh slackupdate.sh &" );

   g_timeout_add(240, time_handler, NULL);
   gtk_widget_show_all(win2);

}

void closekde()
{

fclose(progressfile);
exit(0);

}


int
main (int argc, char *argv[])
{
   FILE *fp;
   char ch, user[15];
   char ut[]="root";
   int x;
   int flag=0;

  gtk_init(&argc, &argv);


      system( "whoami > /tmp/user.txt" );

      if ( ( fp = fopen( "/tmp/user.txt", "r" )) != NULL )
      {
         fscanf(fp, "%s", &user);
         fclose(fp);
         system( "rm /tmp/user.txt" );
         x = strcmp( user, ut);
         if ( x == 0 )
         {
            flag=0;
         }
         else
         {
            flag=1;
         }

      }
      else
      {
         flag=1;
      }


if ( flag == 0 )
{

  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position (GTK_WINDOW(win), GTK_WIN_POS_CENTER);
  gtk_window_set_title (GTK_WINDOW(win), "Configurator post installation Slackware 12.1");
  gtk_window_set_resizable (GTK_WINDOW(win), FALSE);


  gtk_widget_set_size_request (win, 640, 480);

  g_signal_connect (GTK_OBJECT(win), "destroy",
                    G_CALLBACK (gtk_main_quit), win);

        g_signal_connect (G_OBJECT(win), "delete-event",
                        G_CALLBACK (on_window_delete_event), NULL);


      if ( ( fp = fopen( "/tmp/confslackoutput", "r" )) != NULL )
      {
         fix = gtk_fixed_new ();


         label = gtk_label_new ("THE INSTALLER HAS ALREADY EXECUTED");

         gtk_fixed_put (GTK_FIXED(fix), label, 210, 200);
         gtk_container_add (GTK_CONTAINER (win), fix);
         gtk_widget_show_all (win);

         button = gtk_button_new_with_label ("Close program");
         g_signal_connect (G_OBJECT (button), "clicked",
         G_CALLBACK (gtk_main_quit), (gpointer) win);

         gtk_fixed_put (GTK_FIXED(fix), button, 265, 240);
         gtk_container_add (GTK_CONTAINER (win), fix);
         gtk_widget_show_all (win);

      }
      else
      {


         fix = gtk_fixed_new ();


         label = gtk_label_new ("This program installs lacking packages in Slackware\n");
         "Choose on which computer to install packets:");

         gtk_fixed_put (GTK_FIXED(fix), label, 10, 150);
         gtk_container_add (GTK_CONTAINER (win), fix);
         gtk_widget_show_all (win);




         radio1 = gtk_radio_button_new_with_label (NULL, "Install on room computer");
         gtk_fixed_put (GTK_FIXED(fix), radio1, 230, 200);
         gtk_container_add (GTK_CONTAINER (win), fix);
         gtk_widget_show_all (win);

         group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio1));
         radio2 = gtk_radio_button_new_with_label (group, "Install on lounge computer");

         gtk_fixed_put (GTK_FIXED(fix), radio2, 230, 220);
         gtk_container_add (GTK_CONTAINER (win), fix);
         gtk_widget_show_all (win);


         button = gtk_button_new_with_label ("Install and configure");
         g_signal_connect (G_OBJECT (button), "clicked",
         G_CALLBACK (install), (gpointer) win);

         gtk_fixed_put (GTK_FIXED(fix), button, 265, 260);
         gtk_container_add (GTK_CONTAINER (win), fix);
         gtk_widget_show_all (win);
      }
}


else
{

  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position (GTK_WINDOW(win), GTK_WIN_POS_CENTER);
  gtk_window_set_title (GTK_WINDOW(win), "Configurator post installation Slackware 12.1");
  gtk_window_set_resizable (GTK_WINDOW(win), FALSE);


  gtk_widget_set_size_request (win, 640, 480);

  g_signal_connect (GTK_OBJECT(win), "destroy",
                    G_CALLBACK (gtk_main_quit), win);

        g_signal_connect (G_OBJECT(win), "delete-event",
                        G_CALLBACK (on_window_delete_event), NULL);

  fix = gtk_fixed_new ();


  label = gtk_label_new ("YOU MUST BE ROOT TO RUN THE INSTALLER");

  gtk_fixed_put (GTK_FIXED(fix), label, 180, 200);
   gtk_container_add (GTK_CONTAINER (win), fix);
   gtk_widget_show_all (win);

   button = gtk_button_new_with_label ("Close program");
    g_signal_connect (G_OBJECT (button), "clicked",
            G_CALLBACK (gtk_main_quit), (gpointer) win);


  gtk_fixed_put (GTK_FIXED(fix), button, 265, 240);
   gtk_container_add (GTK_CONTAINER (win), fix);
   gtk_widget_show_all (win);


}

  gtk_main();

  return 0;

}

static gboolean
on_window_delete_event (GtkWidget *w, GdkEvent *e, gpointer user_data)
{
        if (confirm_quit_dialog() == GTK_RESPONSE_NO)
        {
                return TRUE;    /* do not quit */
        }
        else exit(0);      /* quit */
}

GtkResponseType
confirm_quit_dialog()
{
        GtkWidget *dialog;
        GtkResponseType response;
       
        /* create the dialog */
        dialog = gtk_message_dialog_new (NULL,
                    GTK_DIALOG_DESTROY_WITH_PARENT,
                        GTK_MESSAGE_QUESTION,
                        GTK_BUTTONS_YES_NO,
                        "Are you sure you want to quit?");

        gtk_window_set_title(GTK_WINDOW(dialog), "Really Quit?");
       
        /* run the dialog */
        response = gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);   
       
        return response;
}


Maybe my error is that I don't use g_main_loop_run(ml)?

dreblen wrote:
Thank you...


Thanks to you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 3:36 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
this might not be the problem, but sometimes not returning anything in a function that says it should can cause weird problems,
so you might want to try putting
Code:
return TRUE;

at the end of your time_handler function, because the timeout will keep running until your function returns FALSE (or you use g_source_remove)
Even if this isn't the problem, it's good practice to return something if the prototype says it should


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 9:23 am 
Offline
Familiar Face

Joined: Mon Jun 09, 2008 4:28 pm
Posts: 6
dreblen wrote:
this might not be the problem, but sometimes not returning anything in a function that says it should can cause weird problems,
so you might want to try putting
Code:
return TRUE;

at the end of your time_handler function, because the timeout will keep running until your function returns FALSE (or you use g_source_remove)
Even if this isn't the problem, it's good practice to return something if the prototype says it should

I used return TRUE; at the end of time_handler, but the problem is still the same. Can you try to compile my source code? If you put 10 ms timeout, you can see progress0.png but you can't see progress1.png, which should appear after 15 seconds, but after 12 seconds, the timeout expires and instead of progress image appears an image with a red x, like the usual image of file not found. You can use this bash script instead of slackupdate.sh:

Code:
#!/bin/bash

echo "0" > /tmp/progress
sleep 15s
echo "1" > /tmp/progress
sleep 15s
echo "2" > /tmp/progress
sleep 15s
echo "3" > /tmp/progress
sleep 15s
echo "4" > /tmp/progress
sleep 15s
echo "5" > /tmp/progress
sleep 15s
echo "6" > /tmp/progress
sleep 15s
echo "7" > /tmp/progress
sleep 15s
echo "8" > /tmp/progress
sleep 15s
echo "9" > /tmp/progress
sleep 15s
echo "10" > /tmp/progress

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 10:59 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
Okay, I've reworked your program so it uses less globals and repeats less code.
This works for me except for the fact that I don't have /tmp/pictures/*.png, you might want to use GtkProgressBar instead:
http://library.gnome.org/devel/gtk/stab ... ssBar.html
http://library.gnome.org/devel/gtk/stab ... t-fraction
Just so you know, both before and after I reworked it, the g_timeout problem didn't occur, but I don't use Slackware (kubuntu), so maybe the problem is on that end.

Here is the code, I've put comments in it regarding things I changed:
Code:
#include <gtk/gtk.h>
/* need to include <stdlib.h> for system() */
#include <stdlib.h>
#include <string.h>

gboolean on_window_delete_event (GtkWidget*, GdkEvent*, gpointer);
gint confirm_quit_dialog(void);
void closekde(GtkWidget *button, gpointer data);

/* these don't need to be global */
//GtkWidget *win, *fix, *label, *button, *image, *win2, *radio1, *radio2;
//GSList *group;
//FILE *progressfile;
//int progress=0;

/* this is already defined in <gtk/gtk.h> */
//gboolean gtk_toggle_button_get_active    (GtkToggleButton *toggle_button);

/* the argument to a GSourceFunc is a gpointer, and it doesn't return static */
gboolean time_handler(gpointer data)
{
   /* usage of GtkFixed is discouraged becaues fonts might cause alignment to be off */
   /* http://library.gnome.org/devel/gtk/stable/GtkFixed.html#GtkFixed.description */
   static GtkWidget *fix = NULL;
   static GtkWidget *win = NULL;
   static GtkWidget *label = NULL;
   static GtkWidget *image = NULL;
   GtkWidget *button;
   FILE *progressfile;
   gint progress;
   gchar *str;
   
   if((progressfile = fopen("/tmp/progress", "r")) != NULL)
      fscanf(progressfile, "%d", &progress);
   else
      return FALSE;
   /* this doesn't need to be done in closekde() */
   fclose(progressfile);
   
   str = (gchar*)g_malloc(strlen("/tmp/pictures/progressNN.png") + 1);
   if(str == NULL)
      return FALSE;
   sprintf(str, "/tmp/pictures/progress%d.png", progress);
   
   /* these lines were unnecessary, and in fact, caused Gtk runtime warnings */
   //gtk_container_remove(GTK_CONTAINER(win2), fix);
   /* these were called several times in each if(progress == %d) block */
   //gtk_container_add(GTK_CONTAINER(win2), fix);
   //gtk_widget_show_all(win2);
   
   if(win == NULL)
   {
      win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
      gtk_window_set_position (GTK_WINDOW(win), GTK_WIN_POS_CENTER);
      gtk_window_set_title (GTK_WINDOW(win), "Installing...");
      gtk_window_set_resizable (GTK_WINDOW(win), FALSE);
      gtk_widget_set_size_request (win, 420, 280);
      g_signal_connect (G_OBJECT(win), "delete-event", G_CALLBACK (on_window_delete_event), NULL);
      gtk_widget_show(win);
   }
   
   if(fix == NULL)
   {
      fix = gtk_fixed_new();
      gtk_container_add(GTK_CONTAINER(win), fix);
      gtk_widget_show(fix);
   }
   
   if(label == NULL)
   {
      label = gtk_label_new("Installation Status");
      gtk_fixed_put(GTK_FIXED(fix), label, 140, 50);
      gtk_widget_show(label);
   }
   
   if(image == NULL)
   {
      image = gtk_image_new_from_file(str);
      gtk_fixed_put(GTK_FIXED(fix), image, 110, 120);
      gtk_widget_show(image);
   }
   else
      gtk_image_set_from_file(GTK_IMAGE(image), str);
   
   if(progress >= 10)
   {
      gtk_label_set_text(GTK_LABEL(label), "Installation Completed Succcessfully!");
      gtk_fixed_move(GTK_FIXED(fix), label, 105, 50);
      
      label = gtk_label_new("Click Below to Close the Program");
      gtk_fixed_put(GTK_FIXED(fix), label, 70, 170);
      gtk_widget_show(label);
      
      button = gtk_button_new_with_label("Close");
      g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gtk_main_quit), NULL);
      gtk_fixed_put(GTK_FIXED(fix), button, 180, 210);
      gtk_widget_show(button);
      
      return FALSE;
   }
   
   g_free(str);
   
   return TRUE;
}

void install(GtkWidget *button, gpointer data)
{
   system("rm /tmp/progress");
   system("sh slackupdate.sh &");
   
   /* data is the 'Lounge' radio button */
   if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data)) == FALSE)
      system("touch /tmp/roompc");
   
   g_timeout_add(240, time_handler, NULL);
}

int main(int argc, char **argv)
{   
   GtkWidget *win;
   GtkWidget *fix;
   GtkWidget *label;
   GtkWidget *button;
   GtkWidget *radio;
   FILE *fp;
   gchar user[101];
   gboolean is_root;   /* this makes more sense than 'flag' */

   gtk_init(&argc, &argv);


   system("whoami > /tmp/user.txt");
   
   if((fp = fopen("/tmp/user.txt", "r")) != NULL)
   {
      fscanf(fp, "%s", &user);
      fclose(fp);
      system("rm /tmp/user.txt");
      if(!strcmp(user, "root"))
         is_root = TRUE;
      else
         is_root = FALSE;
      
   }
   else
      is_root = FALSE;
   
   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_position (GTK_WINDOW(win), GTK_WIN_POS_CENTER);
   gtk_window_set_title (GTK_WINDOW(win), "Configurator post installation Slackware 12.1");
   gtk_window_set_resizable (GTK_WINDOW(win), FALSE);
   gtk_widget_set_size_request (win, 640, 480);
   g_signal_connect (GTK_OBJECT(win), "destroy", G_CALLBACK (gtk_main_quit), win);
   g_signal_connect (G_OBJECT(win), "delete-event", G_CALLBACK (on_window_delete_event), NULL);

   fix = gtk_fixed_new ();
   gtk_container_add(GTK_CONTAINER(win), fix);
   gtk_widget_show(fix);
   
   if(is_root == TRUE)
   {
      if((fp = fopen("/tmp/confslackoutput", "r")) != NULL)
      {
         fclose(fp);
         
         label = gtk_label_new("THE INSTALLER HAS ALREADY EXECUTED");
         gtk_fixed_put(GTK_FIXED(fix), label, 210, 200);
         gtk_widget_show(label);
         
         button = gtk_button_new_with_label("Close Program");
         g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gtk_main_quit), NULL);
         gtk_fixed_put(GTK_FIXED(fix), button, 265, 240);
         gtk_widget_show(button);
      }
      else
      {
         label = gtk_label_new("This program installs lacking packages in Slackware\n"\
                  "Choose which computer to install packages to:");
         gtk_fixed_put(GTK_FIXED(fix), label, 10, 150);
         gtk_widget_show(label);
         
         radio = gtk_radio_button_new_with_label(NULL, "Install on Room Computer");
         gtk_fixed_put(GTK_FIXED(fix), radio, 230, 200);
         gtk_widget_show(radio);
         
         radio = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio), "Install on Lounge Computer");
         gtk_fixed_put(GTK_FIXED(fix), radio, 230, 220);
         gtk_widget_show(radio);
         
         button = gtk_button_new_with_label("Install and Configure");
         g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(install), (gpointer)radio);
         gtk_fixed_put(GTK_FIXED(fix), button, 265, 260);
         gtk_widget_show(button);
      }
   }
   else
   {
      label = gtk_label_new("YOU MUST BE ROOT TO RUN THE INSTALLER");
      gtk_fixed_put(GTK_FIXED(fix), label, 180, 200);
      gtk_widget_show(label);
      
      button = gtk_button_new_with_label("Close Program");
      g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gtk_main_quit), NULL);
      gtk_fixed_put(GTK_FIXED(fix), button, 265, 240);
      gtk_widget_show(button);
   }
   
   gtk_widget_show(win);

   gtk_main();

   return 0;

}

/* this doesn't need to be static */
gboolean on_window_delete_event(GtkWidget *w, GdkEvent *e, gpointer user_data)
{
   if(confirm_quit_dialog() == GTK_RESPONSE_YES)
      gtk_main_quit();
   
   return TRUE;
}

gint confirm_quit_dialog(void)
{
   GtkWidget *dialog;
   gint response;
   
   dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "Are You Sure You Want to Quit?");
   gtk_window_set_title(GTK_WINDOW(dialog), "Really Quit?");
   
   response = gtk_dialog_run(GTK_DIALOG(dialog));
   gtk_widget_destroy(dialog);
   
   return response;
}

Here's a link to the file with proper formatting:
http://tdreblen.googlepages.com/gtimeoutbug_reworked.c
I hope that this helps you...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 8:22 am 
Offline
Familiar Face

Joined: Mon Jun 09, 2008 4:28 pm
Posts: 6
dreblen wrote:
Okay, I've reworked your program so it uses less globals and repeats less code.
This works for me except for the fact that I don't have /tmp/pictures/*.png, you might want to use GtkProgressBar instead:
http://library.gnome.org/devel/gtk/stab ... ssBar.html
http://library.gnome.org/devel/gtk/stab ... t-fraction
Just so you know, both before and after I reworked it, the g_timeout problem didn't occur, but I don't use Slackware (kubuntu), so maybe the problem is on that end.

Here's a link to the file with proper formatting:
http://tdreblen.googlepages.com/gtimeoutbug_reworked.c
I hope that this helps you...


It works!

Thank you very much for having improved my code and dedicating your time to prove the real functioning of the program. You are a very good programmer. Thanks again.


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