view subtly/soup.rb @ 40:61134827e004

Added libsoup interface
author unexist
date Sun, 24 Jan 2010 01:55:13 +0100
parents
children
line wrap: on
line source
##
# @package subtly
#
# @file Soup bindings
# @author Christoph Kappel <unexist@dorfelite.net>
# @version $Id: subtly/soup.rb,v 40 1264294513.0-3600 unexist $
#
# This program can be distributed under the terms of the GNU GPL.
##

require "ffi"
require "subtly/gobject"

module Subtly
  module Soup # {{{
    class Uri # {{{
      # Internal object
      attr_accessor :uri

      extend FFI::Library

      ffi_lib("libwebkit-1.0")

      ## initialize
      # Create soup uri
      #
      # @param [String, #read]  uri  Uri to load
      #
      # @return [Object] New Soup
      ##

      def initialize(uri)
        @uri = Subtly::Soup::Uri.ffi_init(uri)
      end

      ## finalize
      # Kill soup instance
      ##

      def finalize
        Subtly::Soup::Uri.ffi_finalize(@uri)
      end

      ## to_wid
      # Get real widget from object
      #
      # @return [Pointer] Real widget
      ##

      def to_wid
        @uri
      end

      private

      ## ffi_init
      # Create soup uri
      #
      # @param [String, #read]  uri_string  Uri to load
      #
      # @return [Pointer] A #SoupURI
      ##

      attach_function(:ffi_init,
        :soup_uri_new, [ :string ], :pointer
      )

      ## ffi_finalize
      # Free uri
      #
      # @param [String, #read]  urig  A #SoupURI
      ##

      attach_function(:ffi_finalize,
        :soup_uri_free, [ :pointer ], :void
      )
    end # }}}

    class Session # {{{
      # Internal sesson
      attr_accessor :session

      # Default name
      PROXY_URI = "proxy-uri"

      extend FFI::Library

      ffi_lib("libwebkit-1.0")

      ## initialize
      # Create session
      #
      # @param [Pointer, #read]  session  Session to wrap
      #
      # @return [Object] New Session
      ##

      def initialize(session)
        @session = session
      end

      ## proxy=
      # Set proxy
      #
      # @param [String, #read]  uri  Uri to proxy
      ##

      def proxy=(uri)
        if(uri.empty?)
          Subtly::Soup::Session.ffi_remove_feature(@session, PROXY_URI)
        else
          # Set uri
          suri = Subtly::Soup::Uri.new(uri)
          Subtly::GObject.set(@session, PROXY_URI, suri)
          suri.finalize
        end
      end

      private

      ## ffi_remove_feature
      # Removes a feature from session
      #
      # @param [Pointer, #read]  session       A #SoupSession
      # @param [String,  #read]  feature_type  Type of feature
      ##

      attach_function(:ffi_remove_feature,
        :soup_session_remove_feature_by_type, [ :pointer, :string ], :void
      )
    end # }}}
  end # }}}
end