Roxen Application Launcher 0.4.5
Okey, here comes an update of my Roxen Application Launcher (come again?) for Linux.
There’s no major changes to this release. The connection to the Roxen server is now stored in a shared object so that it can use a “keep-alive” connection. Not that I think it matters a great deal.
There’s now an option to change the behavior of the applications window close button so that it hides the application to the tray – or notification area as it’s called in Gnome – rather than closes the application.
More Vala programming to the people – Sources at Github.
Roxen Appliction Launcher 0.4.5 23:00, Tue 13 April 2010 :: 375.9 kB
Roxen Application Launcher 0.4.4
So, here’s a new release of the Roxen Application Launcher for Linux (RAL). The previous versions used my home made (sloppy so) HTTP client which didn’t handle redirects or secure connections – thank you tec for the feed back – since I had some major problems getting libsoup working with binary files like images and such. Binary files was heavily scrambled when read from or written to disk so I made my own simple HTTP client that kept the data as a byte array to prevent some underlying libraries (GLib) from fiddling with it.
But I solved the libsoup issue so now the RAL handles redirects and secure connections. This is how I solved it:
The libsoup issue
When uploading a file back to the Roxen server I use IOChannel (g_io_channel in plain C) instead of Gio. So the upload works like this:
- var sess = new Soup.SessionSync();
- var mess = new Soup.Message("PUT", get_uri());
- mess.request_headers.append("Cookie", get_cookie());
- mess.request_headers.append("Translate", "f");
- IOChannel ch = new IOChannel.file(local_file, "r");
- ch.set_encoding(null); // Enables reading of binary data
- string data;
- size_t len;
- ch.read_to_end(out data, out len);
- mess.request_body.append(Soup.MemoryUse.COPY, data, len);
- sess.send_message(mess);
And that seems to work like a charm!
When downloading data it’s a bit more tricky! Of course I tried using IOChannel in this case also but that made no difference. Downloaded images ended up 4 bytes long! But then I thought: You can make your own C bindings in Vala (remember the Vala compiler generates C code) through what is called Vapi files. So what I did was writing a C function that takes a SoupMessageBody object/struct passed from Vala and writes the data part to a file given as argument.
- gboolean save_soup_data(SoupMessageBody *data, const char *file)
- {
- FILE *fh;
- if ((fh = fopen(file, "w")) == NULL) {
- fprintf(stderr, "Unable to open file \"%s\" for writing!\n", file);
- return FALSE;
- }
- int wrote = fwrite(data->data, 1, data->length, fh);
- if (wrote != (int)data->length) {
- fprintf(stderr, "wrote (%d) != data->length (%d). Data may have been "
- "truncated", wrote, (int)data->length);
- }
- fclose(fh);
- return TRUE;
- }
And this was then made available to Vala by the following Vapi file:
- [CCode (cprefix = "", lower_case_cprefix = "", cheader_filename = "")]
- namespace Soppa // Soppa is Swedish for Soup ;)
- {
- [CCode (cname = "save_soup_data")]
- public bool save_soup_data(Soup.MessageBody data, string file);
- }
And this is how the actual Vala code downloading the files looks like:
- var sess = new Soup.SessionSync();
- var mess = new Soup.Message("GET", get_uri());
- mess.request_headers.append("Cookie", get_cookie());
- mess.request_headers.append("Translate", "f");
- sess.send_message(mess);
- if (mess.status_code == Soup.KnownStatusCode.OK) {
- // Here I call the C function made available through the Vapi file
- if (Soppa.save_soup_data(mess.response_body, local_file)) {
- message("The file was downloaded and written to disk OK");
- }
- else {
- message("Failed writing data to disk!");
- }
- }
So that’s that on that!
The notification
I also – just for fun – implemented a notification mechanism through libnotify. Since I believe that can be rather annoying it’s not activated by default but can easily be activated by a checkbox in the user interface.
The packages
The Roxen Application Launcher for Linux can be downloaded at the download page at Github where also the work in progress sources is available or downloaded below!
Roxen Application Launcher 0.4.4 23:06, Wed 13 January 2010 :: 373.5 kB
Stay black!
Bitlyfier – A Bit.ly client for GNOME
For those of us tweeting – or sharing web addresses in general – these long addresses with extensive query strings you wan’t to share isn’t too user friendly. So we have Bit.ly, among others, that lets you shorten a URL – or give it an alias if you like – and also gives you statistics on how many clicks it has and if it’s shared on Twitter and what not.
Since I’m on the quest of learning the programming language Vala I though why not making a Bit.ly desktop client for GNOME. So I did!
The desktop client
There’s really nothing extraordinary about it, in fact it’s quite simple. Put a long URL in the input field and hit “OK”. You’ll get the shortened URL back in the same input field.
NOTE! The screenshots is showing the Swedish translation but the interface is orginally in English.
Shortening a long URL

The shortened URL

To use the application you will of course need a Bit.ly account. The first time Bitlyfier is launched it will ask for your Bit.ly account settings. Just fill in your username and API key (it’s found on your account page at http://bit.ly/account).
Bitlyfier account settings

The command line interface
For the hacker you, Bitlyfier can also be used as a command line tool. These are the options:
- Usage:
- bitlyfier [OPTION...] - Bitlyfier, URL shortener/expander
- Help Options:
- -h, --help Show help options
- Application Options:
- -e, --expand Expands the given URL
- -s, --shorten Shortens the given URL
- -n, --no-gui Sets the application in command line mode
- -g, --gconf Invokes setting username and apikey
NOTE! You should quote the value of the ‘-s’ flag. If the URL to be shortened
contains a querystring with ampersands the URL will be truncated if it’s not
quoted.
So to shorten a long URL do like:
user@machine:~$ bitlyfier -n -s "http://domain.com/long/url/to/shorten"
The Vala Bitly API classes
The Bitly API class I’ve written can of course be used standalone (it’s located in src/bitly.vala in the sources package downloadable below). Here’s an example of usage:
- // main.vala
- // Compile: valac --pkg gee-1.0 --pkg json-glib-1.0 --pkg libsoup-2.4 -o main
- int main(string[] argv)
- {
- Bitly.Api api = new Bitly.Api("username", "R_the_api_key");
- Bitly.Response response = api.shorten("http://domain.com/the/long/url");
- stdout.printf("Short URL: %s\n", response.get_string("shortUrl"));
- response = api.stats("A2ma2z");
- stdout.printf("Clicks: %d\n", response.get_integer("clicks"));
- return 0;
- }
More about the Bit.ly API and what the API methods do can be read about at http://bit.ly/6HIqjS.
The sources
The development sources of this application is available at Bitlyfier at Github. The current stable release can be found at the Download page.
JavaScript URI class
The other day I needed an URI class for JavaScript. I was doing some stuff where I needed to alter certain parts of an URI. I bet there’s a couple of URI classes for JavaScript out there but I can be a bit nit-picky about code and how it’s written
Anyway, I had a URI parser regexp lying which I wrote for a Vala class (before I found the Soup.URI class) and I thought that since that’s reusable I could hack up a JavaScript URI class myself. So I did!
Here’s some examples of usage:
- var uri = new URI("http://poppa.se/blog/javascript-uri-class/");
- console.log(uri.scheme); //-> http
- console.log(uri.host); //-> poppa.se
- console.log(uri.path); //-> /blog/javascript-uri-class/
- console.log(uri.port); //-> 80
Now, if we want to alter the host so that it contains www we do:
- uri.host = "www.poppa.se";
- console.log(uri.toString()); //-> http://www.poppa.se/blog/javascript-uri-class/
It’s also easy to alter query string variables:
- var uri = new URI("http://host.com/?name=poppa&lang=se");
- uri.variables["name"] = 'Günther';
- uri.variables["lang"] = 'de';
- console.log(uri.toString()); //-> http://host.com/?name=Günther&lang=de
And I think that’s pretty smooth
New Roxen Application Launcher for Linux written in Vala
A couple of weeks ago I stumbled upon a fairly new programming language named Vala. I thought it looked promising and since Vala is developed by the GNOME project – with the purpose of making software development for, primarily, GNOME easier – and I’m an avid GNOME user I wanted to look deeper into the world of Vala.
I, and most programmers I believe, work in that way that I need a real and useful project when learning a new programming language. So I thought why not re-writing the Roxen Application Launcher I wrote in C#/Mono a couple of years ago in Vala – which by the way is syntactically very, very similar to C# and Java. I’d gotten tired of always having to fiddle with the C# code with every new version of Mono since something always broke when Mono was updated so a re-write wasn’t going to be totally pointless. The good thing about Vala is that the Vala compiler generates C code and that’s what you compile the program from. Fast code and hopefully more mature and stable libraries that won’t break backwards compatibility with every new release.
What about Vala
So, on I went about it and I think that Vala is a really promising language. It’s still a very young language so some library bindings isn’t behaving exactly as expected and the documentation isn’t directly redundant – although the Vala reference documentation site isn’t half bad. But since Vala pretty much is a wrapper for, or binding to, the underlying C libraries you can find answers to your questions that way. All in all I think Vala has a promising future: Way more simple than C and almost as fast and light on memory (remember the Vala compiler generates C code) and way faster than C#/Mono and free from any Microsoft associations
.
What about the Roxen Application Launcher
In this new version I utilize GConf for storing application settings. I also made use of – for the first time – the GNU Build Tools for compilation which also makes it easier to distribute and for others to compile from the sources. This also means that the distributed version compiles from the C sources and not the Vala sources so there’s no need for the Vala compiler to build the program.
Other than that there’s nothing fancy about it. The Vala sources is available at my Github repository.
Roxen Appliction Launcher 0.4.2 19:38, Sun 20 December 2009 :: 374 kB
Screenshots
The screenshots is showing the Swedish translation.
Vala – the new programming language for Gnome
This Friday when I read the last issue (in Sweden) of Linux Format there was an article on a new programming language named Vala. The goal for Vala is to provide a modern programming language for, primarily, developing Gnome applications. There is of course Mono, but Vala doesn’t run in a virtual machine but is complied to machine code. But Vala resembles C# syntactically and has borrowed a lot of concepts from C#.
From what I understand Vala code is first translated into plain old C code, and then compiled with the ordinary GCC compiler. The benefit is that you don’t have to get head aches about memory management and so forth.
Vala seems pretty interesting and I downloaded it and compiled it without any difficulties. There are precompiled packages for most Linux distros – it’s available in the Ubuntu repository – but since Vala is new and still under development the distro packages is far behind in version.
There’s also a Vala IDE and plugins for GEdit, Anjuta and Eclipse.
Anyway, just to try Vala out I made a little program – using Val(a)ide – that changes the desktop wallpaper on a per interval basis. Send the program a path to a directory with images and the background will change among those images every *nth minutes.
This program needs libgee which at the moment needs to be added manually.
Compile: valac --pkg gconf-2.0 --pkg gee-1.0 --pkg gio-2.0 main.vala -o wallpaper-iterator
- /* main.vala
- *
- * Copyright (C) 2009 Pontus Östlund <spam@poppa.se>
- *
- * No license what so ever. Do what ever you like...
- *
- * Author:
- * Pontus Östlund <spam@poppa.se>
- */
- // GLib isn't really neccessary since it's imported automatically
- using GLib, Gee, GConf;
- /**
- * User defined exception types
- */
- errordomain IOError {
- FILE_NOT_FOUND,
- NOT_A_DIRECTORY
- }
- /**
- * Main class
- */
- public class Main
- {
- /**
- * Used as reference in option parser
- */
- static int arg_delay;
- /**
- * Application command line options
- */
- const OptionEntry[] options = {
- { "delay", 'd', 0, OptionArg.INT, ref arg_delay,
- "Number of minutes to wait before swapping background", null },
- { null }
- };
- /**
- * Main method
- *
- * @param args
- * Command line arguments
- */
- public static int main (string[] args)
- {
- try {
- var opt = new OptionContext("\"/path/to/wallpapers\"");
- opt.set_help_enabled(true);
- opt.add_main_entries(options, null);
- opt.parse(ref args);
- }
- catch (GLib.Error e) {
- stderr.printf("Error: %s\n", e.message);
- stderr.printf("Run '%s --help' to see a full list of available "+
- "options\n", args[0]);
- return 1;
- }
- if (args.length < 2) {
- stderr.printf("Missing argument!\n");
- stderr.printf("Run '%s --help' for usage\n", args[0]);
- return 1;
- }
- // Default time before changing background is 30 minutes
- int delay = arg_delay > 0 ? arg_delay*60 : 60*30;
- try {
- WallpaperIterator bg = new WallpaperIterator(args[1], delay);
- bg.run();
- bg.stop();
- }
- catch (IOError e) {
- stderr.printf("Error: %s\n", e.message);
- return 1;
- }
- finally {
- stderr.printf("Finally block reached!\n");
- }
- return 0;
- }
- }
- /**
- * Class that handles changeing of desktop wallpapers on a per interval way
- */
- public class WallpaperIterator : GLib.Object
- {
- /**
- * Number of seconds to wait before changeing wallpaper
- */
- private int delay = 0;
- /**
- * Current index in the images list
- */
- private int index = 0;
- /**
- * List of images to change between
- */
- private ArrayList<string> files = new ArrayList<string>();
- /**
- * List of allowed content types
- */
- private ArrayList<string> allow = new ArrayList<string>();
- /**
- * GConf client object
- */
- private GConf.Client gclient = null;
- /**
- * GConf registry where the background image is set
- */
- private string gconf_key = "/desktop/gnome/background/picture_filename";
- /**
- * Background being used when the application is started
- */
- private string default_bg = null;
- /**
- * Constructor
- *
- * @param dir
- * The directory to collect wallpapers in
- * @param delay
- * Time to wait - in seconds - before swapping wallpaper
- */
- public WallpaperIterator(string dir, int delay)
- throws IOError
- {
- assert(delay > 0);
- allow.add("image/jpeg");
- allow.add("image/png");
- var f = File.new_for_path(dir);
- if (!f.query_exists(null))
- throw new IOError.FILE_NOT_FOUND(" \"%s\" doesn't exits!".printf(dir));
- if (f.query_file_type(0, null) != FileType.DIRECTORY) {
- throw new IOError.NOT_A_DIRECTORY(" \"%s\" is not a directory!"
- .printf(dir));
- }
- collect(f.get_path());
- if (files.size < 2) {
- warning("Not enough images found for this application to be useful!\n");
- return;
- }
- this.delay = delay;
- }
- /**
- * Run the application. Starts a MainLoop
- */
- public void run()
- {
- gclient = GConf.Client.get_default();
- default_bg = gclient.get_string(gconf_key);
- MainLoop loop = new MainLoop(null, false);
- var time = new TimeoutSource(delay*1000);
- time.set_callback(() => { swap(); });
- time.attach(loop.get_context());
- loop.run();
- }
- /**
- * Stop the application. Tries to reset the background
- */
- public void stop()
- {
- try {
- if (default_bg != null)
- gclient.set_string(gconf_key, default_bg);
- }
- catch (GLib.Error e) {
- warning("Failed to restore background!\n");
- }
- }
- /**
- * Does the actual background swapping
- */
- private void swap()
- {
- if (index >= files.size) {
- debug("Restart...\n");
- index = 0;
- }
- debug("Callback...%-2d (%s)\n", index, files[index]);
- try {
- gclient.set_string(gconf_key, files[index]);
- }
- catch (GLib.Error e) {
- warning("Failed to set background: %s\n", e.message);
- }
- index++;
- }
- /**
- * Collect images
- *
- * @param path
- */
- private void collect(string path)
- {
- try {
- var dir = File.new_for_path(path).enumerate_children(
- "standard::name,standard::type,standard::content-type",
- 0, null
- );
- FileInfo fi;
- string fp;
- while ((fi = dir.next_file(null)) != null) {
- fp = path + "/" + fi.get_name();
- if (fi.get_file_type() == FileType.DIRECTORY)
- collect(fp);
- else {
- if (fi.get_content_type() in allow)
- files.add(fp);
- else
- warning("Skipping \"%s\" due to unallowed content type\n", fp);
- }
- }
- }
- catch (GLib.Error e) {
- warning("Error: %s\n", e.message);
- }
- }
- }
I think Vala looks promising and I will try it out trying to write a more complex application when I find the time.






