Saturday, May 5, 2007

Ruby and GTK2




I've been using for a while Zenity as a simple framework to provide user interaction on my local machine. Few of my family members also use my computer and it is easier for them to use UI, then to remember all the magic words, not to mention terminal. So, my Internet provider likes to drop the connection, maybe not very often but it happens. It's really suspicious when you are doing some research and it's 2:30 am and then suddenly your ISP declines your connections. Perhaps they are doing some cleaning at that hour, maybe it is worth investigating, but as for now never mind. Right, clearly I need a tool that will show what's the status of the connection - do I need to reconnect or I'm connected but the traffic is really bad. Zenity happens to be a great tool, it's easy to use and you can create some user interaction really easy. Because I like to do new things I've decided to check out ruby GTK2 library for that. And Yes I was inspired by the recent news that Microsoft is going to include Iron Ruby in .NET framework. Here is my impression of that.

Because it is my first post about ruby let me just remind you that there is a mod for GNU Emacs. Now, you have to install additional libraries in order to use GKT2 from a ruby. I'm using Ubuntu and dependencies are pretty big, but it's as easy to install additional stuff as typing


sudo apt-get install ruby-gnome2


If you are not familiar with ruby and GTK2 a great tutorial can be found here. And in terms of API documentation be sure to try rbbr or web API documentation.

In terms of code ruby bindings to GTK are like any other bindings to GTK. This is definitely not the best way of creating UI. In terms of UI i strongly agree with Steve Yegge. Now let's get back to the ruby. Here is a code that I came up with:


require 'gtk2'

class InternetConnection


def initialize()
@connection_status = "You a_re not connected to the Internet"
end

def checkStatus()
commandOutput = `ifconfig`
if commandOutput.index("ppp0") != nil then
@connection_status = "You a_re connected to the Internet"
else
@connection_status = "You a_re not connected to the Internet"
end
end

def disconnect()
command = `xterm -e "sudo killall pppd"`
checkStatus()
end

def connect()
command = `xterm -e "sudo pppd call speedtch"`
checkStatus()
end

def run()
table = Gtk::Table.new(3, 4, true);


image_apple_green = Gtk::Image.new("apple-green.png")
image_apple_red = Gtk::Image.new("apple-red.png")
image_status = Gtk::Image.new("tennis-ball.png")

checkStatus()
b_status = Gtk::Button.new(@connection_status, use_underline = true)
b_status.set_image(image_status)
b_status.signal_connect("clicked"){
checkStatus()
}

b_connect = Gtk::Button.new("C_onnect to the Internet", use_underline = true)
b_connect.set_image(image_apple_green)
b_connect.signal_connect("clicked") {
connect()
}

b_disconnect = Gtk::Button.new("_Disconnect from the Internet", use_underline = true)
b_disconnect.set_image(image_apple_red)
b_disconnect.signal_connect("clicked") {
disconnect()
}

b_close = Gtk::Button.new(Gtk::Stock::CLOSE)
b_close.signal_connect("clicked") {
Gtk.main_quit
}

window = Gtk::Window.new
window.signal_connect("delete_event") {
Gtk.main_quit
false
}

table.attach_defaults(b_status, 0, 4, 0, 1 )
table.attach_defaults(b_connect, 0, 2, 1, 2 )
table.attach_defaults(b_disconnect, 2, 4, 1, 2 )
table.attach_defaults(b_close, 1, 3, 2, 3 )

window.border_width = 10
window.add(table)
window.show_all

Gtk.main
end

end



And it looks like that:

No comments: