レナート   TBFKAYIBYNYAAYB   ﻟﻴﻨﺎﺭﺕ

Wed, 07 Feb 2007

Avahify Your Application!

It has never been easier to add Zeroconf service discovery support to your GTK application!

The upcoming Avahi 0.6.18 will ship with a new library libavahi-ui which contains a GTK UI dialog AuiServiceDialog, a simple and easy-to-use dialog for selecting Zeroconf services, similar in style to GtkFileChooserDialog and friends. This dialog should be used whenever there is an IP server to enter in a GTK GUI. For example:

So, how does it look like? Here's a screenshot of a service dialog browsing for FTP, SFTP and WebDAV shares simultaneously:

Service Dialog

The dialog properly supports browsing in remote domains, browsing for multiple service types at the same time (i.e. POP3 and POP3S) and supports multi-homed services. It will also resolve the services if requested. Avahi will ship a (very useful!) example tool zssh.c which if started from the command line allows you to quickly browse for local SSH servers and connect to one of those available. (Short Theora screencast of zssh - Please excuse the strange cursor, seems to be a bug in Istanbul 0.2.1, which BTW is totally broken on multi-headed setups):

A simple application making use of this dialog might look like this:

#include <gtk/gtk.h>
#include <avahi-ui/avahi-ui.h>

int main(int argc, char*argv[]) {
    GtkWidget *d;

    gtk_init(&argc, &argv);

    d = aui_service_dialog_new("Choose Web Service");
    aui_service_dialog_set_browse_service_types(AUI_SERVICE_DIALOG(d), "_http._tcp", "_https._tcp", NULL);

    if (gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_OK)
        g_message("Selected service name: %s; service type: %s; host name: %s; port: %u",
		aui_service_dialog_get_service_name(AUI_SERVICE_DIALOG(d)),
		aui_service_dialog_get_service_type(AUI_SERVICE_DIALOG(d)),
		aui_service_dialog_get_host_name(AUI_SERVICE_DIALOG(d)),
		aui_service_dialog_get_port(AUI_SERVICE_DIALOG(d)));
    else
        g_message("Canceled.");
    
    gtk_widget_destroy(d);
    
    return 0;
}

A more elaborate example is zssh.c. You may browse the full API online.

AuiServiceDialog is not perfect yet. It still lacks i18n and a11y support. In addition it follows the HIG only very roughly. Patches welcome! I am also very interested in feedback from more experienced GTK programmers, since my experience with implementing GTK controls is rather limited. This is my first GTK library which should really feel like a GTK API. So please, read through the API and the implementation and send me your comments! Thank you!

If you want to integrate AuiServiceDialog into your application and don't want to wait for Avahi 0.6.18, just copy avahi-ui.h and avahi-ui.c into your sources and make sure to add avahi-client, avahi-glib, gtk+-2.0 to your pkg-config dependencies.

posted at: 13:56 | path: /projects | permanent link to this entry | 6 comments


Posted by Mathias Hasselmann at Wed Feb 7 15:41:18 2007
Guess you've finally decided to hack this thing out of the following feeling:

Wenn der Prophet nicht zum Berg kommt, kommt der Berg halt zum Propheten...

Well, for me the dialog looks quite ok. Regarding the API: You definitly should add a parent window parameter to the constructor and it might help if you customizing dialog buttons within the constructor. Look at gtk_file_chooser_dialog_new for some example. Speaking of GtkFileChooser: Maybe AuiServiceChooser would be a more consistent name? If you choose not to allow custom response buttons from the ctor I'd change the response ids to the more generic GTK_RESPONSE_REJECT and GTK_RESPONSE_ACCEPT.

Posted by Tester at Wed Feb 7 16:25:39 2007
I don't see the manual input field? Like the one that has been added to gtkfilechooser.

Posted by Lennart at Wed Feb 7 16:34:48 2007
Mathias: thanks for the advice! The GTk file selector dialog itself is called GtkFileChooserDialog. Following this the name for this dialog should probably be AuiServiceChooserDialog and not just AuiServiceChooser. However, I prefer the shorter AuiServiceDialog in this case.

I will certainly follow your advice on the parent window and on the buttons.

Posted by Lennart at Wed Feb 7 16:37:21 2007
Tester: It doesn't make any sense to have a manual input field, because noone would want to enter a zeroconf service name. Those names are only useful for humans identifying what they are looking for. They are mostly irrelevant when actually connecting to a service.

Remember that the service name is usually resolved to acquire a host name and an IP port. That host name/port should be editable by the user - however in a the parent window.

Posted by Murray Cumming at Sun Feb 11 16:44:53 2007
Very useful.

For the server side of this story, does Avahi have some API to help me dynamically pick an available port number, optionally within a range?

Posted by Lennart at Wed Apr 18 23:22:22 2007
Murray: There is now a new FAQ entry on http://avahi.org/wiki/Avah4users#FAQ which tries to explain how to do this best.

Leave a Comment:

Your Name:


Your E-mail (optional):


Comment:


As a protection against comment spam, please type the following number into the field on the right:
Secret Number Image

Please note that this is neither a support forum nor a bug tracker! Support questions or bug reports posted here will be ignored and not responded to!


It should be obvious but in case it isn't: the opinions reflected here are my own. They are not the views of my employer, or Ronald McDonald, or anyone else.

Please note that I take the liberty to delete any comments posted here that I deem inappropriate, off-topic, or insulting. And I excercise this liberty quite agressively. So yes, if you comment here, I might censor you. If you don't want to be censored your are welcome to comment on your own blog instead.


Lennart Poettering <mzoybt (at) 0pointer (dot) net>
Syndicated on Planet GNOME, Planet Fedora, planet.freedesktop.org, Planet Debian Upstream. feed RSS 0.91, RSS 2.0
Archives: 2005, 2006, 2007, 2008, 2009, 2010

Valid XHTML 1.0 Strict!   Valid CSS!