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 Sep 18, 2014 9:41 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: GTK WebKit URI Callback
PostPosted: Mon Sep 24, 2012 11:38 pm 
Offline
Familiar Face

Joined: Sat Mar 03, 2012 2:45 am
Posts: 18
Hello Everyone,

I am trying to get the Gtk app to close when redirected to a specific page. It's not closing. I have a callback when page finishes loading going to a function and have a if statement on the url if should close the app.

Any Suggestions?
Code:
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <webkit/webkit.h>
#include <stdio.h>
#include <string.h>

static WebKitWebView* web_view;

typedef struct _Private Private;
static struct _Private
{
   
};

static struct Private* priv = NULL;

/* #define UI_FILE PACKAGE_DATA_DIR"/ui/myapp.ui" */
#define UI_FILE "src/myapp.ui"
#define TOP_WINDOW "window"

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

static GtkWidget* create_window (void)
{
   GtkWidget *window;
   GtkBuilder *builder;
   GError* error = NULL;

   /* Load UI from file */
   builder = gtk_builder_new ();
   if (!gtk_builder_add_from_file (builder, UI_FILE, &error))
   {
      g_critical ("Couldn't load builder file: %s", error->message);
      g_error_free (error);
   }

   /* Auto-connect signal handlers */
   gtk_builder_connect_signals (builder, NULL);

   /* Get the window object from the ui file */
   window = GTK_WIDGET (gtk_builder_get_object (builder, TOP_WINDOW));
        if (!window)
        {
                g_critical ("Widget \"%s\" is missing in file %s.",
            TOP_WINDOW,
            UI_FILE);
        }

   priv = g_malloc (sizeof (struct _Private));

   g_object_unref (builder);
   
   return window;
}

static void
load_commit_cb (WebKitWebView* web_view, gpointer data)
{
   WebKitLoadStatus status = webkit_web_view_get_load_status (web_view);
   if(status == WEBKIT_LOAD_FINISHED)
   {
      const gchar* uri = webkit_web_view_get_uri (web_view);
      const gchar* uri_logout = "http://localhost/?i=logout";
      if(uri == uri_logout)
      {
         gtk_main_quit ();
      }
   }
}

void scrolledwindow_draw_cb (GtkWidget *scrolledwindow, GtkWidget *buttonbox)
{
   GtkWidget *scrolled_window;
   scrolled_window = GTK_WIDGET(scrolledwindow);

    web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
    gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
                 
    gchar* uri = (gchar*) ("http://localhost/?i=login");
    webkit_web_view_open (web_view, uri);

   gtk_widget_show_all (scrolled_window);
}

int main (int argc, char *argv[])
{
    GtkWidget  *window;
   
#ifdef ENABLE_NLS
   bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
   textdomain (GETTEXT_PACKAGE);
#endif
   
   gtk_init (&argc, &argv);

   window = create_window ();

   g_signal_connect (G_OBJECT (web_view), "load-progress-changed", G_CALLBACK (load_commit_cb), web_view);

   gtk_widget_show (window);
   gtk_main ();
   g_free (priv);
   return 0;
}


Top
 Profile  
 
 Post subject: Re: GTK WebKit URI Callback
PostPosted: Tue Sep 25, 2012 7:43 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

To what signal is that scrolledwindow_draw_cb() function connected to? That part of the code seems fishy.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: GTK WebKit URI Callback
PostPosted: Tue Sep 25, 2012 10:44 pm 
Offline
Never Seen the Sunlight

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

You do not say which version of webkit or GTK+ you are using.

Even just a quick look at your code reveals many errors.

- The signal "load-progress-changed" is deprecated and you should connect to the property "progress" use "notify::progress" instead.
- The function proto-type that defines the call-back (load_commit_cb) for the signal "load-progress-changed" is incorrect.
- In the same function you are comparing two constant string pointers. These pointers will always point to different areas of memory so the if statement will never be true and may be optimised out. In this case you are comparing the pointers, I expect you wanted to compare the strings that the pointers point to so you need to use g_strcmp0() see http://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-strcmp0

For more details on webkit see http://webkitgtk.org/?page=documentation

_________________
E.


Top
 Profile  
 
 Post subject: Re: GTK WebKit URI Callback
PostPosted: Wed Sep 26, 2012 2:55 pm 
Offline
Familiar Face

Joined: Sat Mar 03, 2012 2:45 am
Posts: 18
That Worked. Thank you Very Much. I guess there is still a few websites that don't say weather they are using GTK2 or GTK3 and that is why i was using the old signals.

I appreciate Everything.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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