Avahify Your Application!

Post Syndicated from Lennart Poettering original https://0pointer.net/blog/projects/avahify-your-app.html

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:

  • Mail applications such as Evolution may use it to browse for POP3, POP3S, IMAP, IMAPS and SMTP servers.
  • VNC applications may use it to browse for VNC/RFB servers
  • Database clients such as Glom may use it to browse for PostrgreSQL servers
  • FTP clients may use it to browse for FTP servers
  • RSS readers may use it to browse for local RSS feeds

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.