GChildWatchFunc function called when the child is still outputting data



Hi,

I run unzip -vl archive_550mb.zip with g_spawn_async_with_pipes and I read its output line by line with g_io_channel_read_line. To know when unzip executable has finished I register a GChildWatchFunc with g_child_watch_add(). The data is to be inserted in a liststore. To speed up the process I use this trick found on scentic.net:

 model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview1));
    g_object_ref(model);
    gtk_tree_view_set_model(GTK_TREE_VIEW(treeview1), NULL);

//fill the liststore
gtk_tree_view_set_model (GTK_TREE_VIEW(treeview1), model);
    g_object_unref(model);


The problem is that the GChildWatchFunc function is called when the child is still outputting data and this is not what I want because the status bar outputs "Operation successfully completed" but the progress bar is still pulsing. Maybe it's a matter of synchonization but I really don't know how to fix the weird behaviour. Some code:

//output_fd is the file descriptor g_spawn_async_with_pipes gives me
SetIOChannel (output_fd, G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL,ZipOpen, (gpointer) mode );

GIOChannel *SetIOChannel (gint fd, GIOCondition cond, GIOFunc func, gpointer data)
{
        GIOChannel *ioc;
        ioc = g_io_channel_unix_new ( fd );
        g_io_add_watch (ioc, cond, func, data);
        g_io_channel_set_encoding (ioc, "ISO8859-1" , NULL);
        g_io_channel_set_flags ( ioc , G_IO_FLAG_NONBLOCK , NULL );
        return ioc;
}

static gboolean ZipOpen (GIOChannel *ioc, GIOCondition cond, gpointer data)
{
        gchar **fields = NULL;
        gchar *filename = NULL;
        gchar *line = NULL;
        if (cond & (G_IO_IN | G_IO_PRI) )
        {
                g_io_channel_read_line ( ioc, &line, NULL, NULL, NULL );
if (line != NULL && data ) gtk_text_buffer_insert (textbuf, &enditer, line, strlen ( line ) );
                fields = split_line (line , 7);
        if ( fields == NULL) return TRUE;
                filename = get_last_field (line , 8);
        if ( filename != NULL )
        {
            gtk_list_store_append (liststore, &iter);
                for ( x = 0; x < 7; x++)
                        gtk_list_store_set (liststore, &iter,x+1,fields[x],-1);
                gtk_list_store_set (liststore, &iter,0,filename,-1);
        }
        gtk_progress_bar_pulse ( GTK_PROGRESS_BAR (progressbar) );
        while (gtk_events_pending() )
                        gtk_main_iteration();
                g_strfreev ( fields );
                g_free (line);
                return TRUE;
        }
        else if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL) )
        {
                g_io_channel_shutdown ( ioc,TRUE,NULL );
                g_io_channel_unref (ioc);
                g_spawn_close_pid ( child_pid );
                return FALSE;
        }
}

Hope someone can helps,
--
Colossus
Xarchiver, a GTK2 only archive manager - http://xarchiver.sourceforge.net
Cpsed, a Linux OpenGL 3D scene editor - http://cpsed.sourceforge.net
Mizio, a QT proxy hunter scanner tool - http://mizio.sourceforge.net



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]