Pingly – a Roxen module for automatic Twingly pinging

Published 9 July, 2010 in Roxen - 0 Comments

Twingly is a blog search engine that focus on indexing the “blogosphere” rather than being a generic search engine. Twingly has a ping service that let you ping Twingly when you have new content on your blog so that Twingly can head over there and index the new content asap.

Since Roxen CMS have event hooks this module listens for newly published files and when found automatically notifies Twingly about it.

The only thing needed is to set up a config file in the SiteBuilder’s workarea so that this module knows under which paths newly published content should notify Twingly and with which arguments. But all this is documented in the module.

One note! If you run a replicated environment install this module on one of the frontend servers, not the backend. If installed on a backend Twingly might head over to your site before the new content has been replicated.

The Pingly Roxen module 11:32, Fri 09 July 2010 :: 15.4 kB

JavaScript minifier filter module for Roxen

Published 28 June, 2010 in Misc - 0 Comments

Nowadays web sites and web applications tend to be more and more JavaScript driven which results in humongous JavaScript files. It’s not uncommon to have several 100 of bytes of JavaScript on a site. Of course web browsers cache stuff like JavaScript so that it only is requested from the server once. But judging from the visitor logs at work most people only visit our site once a month or so which means that cache will expire and all those scripts has to be requested upon the first visit.

Now, there are several ways to compact JavaScripts: Packer, YUI Compressor, Shrink Safe, jsmin and many more. Some of these just remove redundant white space and comments, some obfuscates the code and shortens variable and function names and what not. Many of these scripts and programs are very fine but they require you to manually minify your scripts, and that’s just a hassle!

But since we use Roxen CMS at work things get much easier if you write your own Roxen filter module which automatically minifies JavaScripts on the fly, given they meet certain criteria. And so I did!

I ported the original jsmin code written in C to Pike. Then it was just a matter of creating a simple filter module for Roxen. And then it was all done.

You can use two criteria to determine if a script should be minified or not:

  1. Path glob: In the module settings you can specify any number of directory globs or full paths. If a requested JavaScript either is in a directory matching a glob or is a direct match it will be minified.
  2. Query string variable: In the module settings you can define a variable name that if exists as a query string variable in the request the JavaScript will be minified. So:

    <script type="text/javascript" src="myscript.js?jsmin=1"></script>

    will minify myscript.js

And that’s that!

jsmin Roxen filter module 13:34, Mon 28 June 2010 :: 6.1 kB

Roxen Application Launcher 0.4.5

Published 13 April, 2010 in Applications , Linux , Roxen - 0 Comments
This is not the latest version of Roxen Application Launcher. You’ll find the latest version at the download page.

Screenshot of Roxen Application Launcher

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

Blue Screen of Death

Published 5 February, 2010 in Misc - 0 Comments

You don’t get surprised when you get to work one morning and your monitors looks like this, given your computer is running the bless from Redmond!

Man I wish I was running Linux at work as well!

(And no: I hardly drink any Coca Cola at all ;) )

GTK hacking in Pike

Published 19 January, 2010 in Applications , Linux , Programming - 0 Comments

Tweepi, the Twitter client written in PikeI’ve found out that it’s great fun programming desktop applications and of course it gets more fun the more you learn. Now I’m doing a Twitter client in Pike – my favorite programming language – mostly because I wanted to try out GTK programming in Pike. I use the good Twitter client Pino – written in Vala – and I have borrowed the concept and layout from it. I call it Tweepi.

The only major difference between Tweepi and Pino – besides they are written in different programming languages – is that Pino uses WebKit to draw the status messages where I am using good old GTK widgets – and I guess there are no bindings to WebKit in Pike for that matter ;)

One thing I noticed is that the Gtk.Label widget sucks at displaying longer texts that line wraps. Since the label widget handles some HTML formatting I thought that it would be suitable for displaying the status messages, but the text looked like shit, line wrapping where ever it felt like. And the Gtk.TextView widget doesn’t handle formatting per default so I Googled some and found that you can format text in Gtk.TextViews by inserting Gtk.TextTags at desired positions. And since Pike has the most awesome HTML parser It was just a matter of sending the text through the parser and create some Gtk.TextTags and inserting them at the same position in the text buffer. (Well, actually it wasn’t that easy but with some help from a Python class I found on the web it was doable).

So now I have a start at something that is a Gtk.HtmlTextView – actually it inherits Gtk.TextView but has an additional method insert_html_text(string text) – and albeit quite simple at the moment it’s worth continuing on. The code for the HtmlTextView is available at my Github repository.

In general I find the GTK implementation in Pike to be pretty OK, but there exist some verbose, and tedious, stuff like getting the text from a Gtk.TextView:

2 lines of Pike
  1. Gtk.TextBuffer b = my_textview>get_buffer();
  2. string text = b>get_text(b>get_start_iter(), b>get_end_iter(), 0);

which in Vala and C# would be done like:

5 lines of Vala
  1. // Vala
  2. string text = my_textview.get_buffer().text;
  3. // C#
  4. string text = myTextView.Buffer.Text;

Anyway! Tweepi isn’t done yet but I think I have solved the most tedious stuff and it’s starting to become useful. It’ll probably be done in a couple of weeks and I will of course release the sources then.

Roxen Application Launcher 0.4.4

Published 14 January, 2010 in Applications , Linux , Roxen - 0 Comments
This is not the latest version of Roxen Application Launcher. You’ll find the latest version at the download page.

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:

13 lines of Vala
  1. var sess = new Soup.SessionSync();
  2. var mess = new Soup.Message(“PUT”, get_uri());
  3. mess.request_headers.append(“Cookie”, get_cookie());
  4. mess.request_headers.append(“Translate”, “f”);
  5. IOChannel ch = new IOChannel.file(local_file, “r”);
  6. ch.set_encoding(null); // Enables reading of binary data
  7. string data;
  8. size_t len;
  9. ch.read_to_end(out data, out len);
  10. mess.request_body.append(Soup.MemoryUse.COPY, data, len);
  11. 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.

19 lines of C/C++
  1. gboolean save_soup_data(SoupMessageBody *data, const char *file)
  2. {
  3. FILE *fh;
  4. if ((fh = fopen(file, “w”)) == NULL) {
  5. fprintf(stderr, “Unable to open file \”%s\” for writing!\n”, file);
  6. return FALSE;
  7. }
  8. int wrote = fwrite(data>data, 1, data>length, fh);
  9. if (wrote != (int)data>length) {
  10. fprintf(stderr, “wrote (%d) != data->length (%d). Data may have been “
  11. “truncated”, wrote, (int)data>length);
  12. }
  13. fclose(fh);
  14. return TRUE;
  15. }

And this was then made available to Vala by the following Vapi file:

6 lines of Vala
  1. [CCode (cprefix = “”, lower_case_cprefix = “”, cheader_filename = “”)]
  2. namespace Soppa // Soppa is Swedish for Soup 😉
  3. {
  4. [CCode (cname = “save_soup_data”)]
  5. public bool save_soup_data(Soup.MessageBody data, string file);
  6. }

And this is how the actual Vala code downloading the files looks like:

15 lines of Vala
  1. var sess = new Soup.SessionSync();
  2. var mess = new Soup.Message(“GET”, get_uri());
  3. mess.request_headers.append(“Cookie”, get_cookie());
  4. mess.request_headers.append(“Translate”, “f”);
  5. sess.send_message(mess);
  6. if (mess.status_code == Soup.KnownStatusCode.OK) {
  7. // Here I call the C function made available through the Vapi file
  8. if (Soppa.save_soup_data(mess.response_body, local_file)) {
  9. message(“The file was downloaded and written to disk OK”);
  10. }
  11. else {
  12. message(“Failed writing data to disk!”);
  13. }
  14. }

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!

Extracting text from PDFs

Published 11 January, 2010 in Applications , Programming - 0 Comments

Unwanted line breaks in text copied from PDF
Unwanted line breaks in text copied from PDF

Anybody working with information sooner or later have to copy and paste text from PDF-files. And anybody who has done that knows what a pain in the a** that is! You get actual line breaks from the visual line breaks in the PDF. The other day I began a job where I have to copy and paste text from a whole bunch of PDF files and it didn’t take long before I almost exploded with anger ;)

So I thought: Why not make a simple application that extracts the text from the PDF and – to the most possible degree – normalizes the unwanted line breaks.

And then there was Textifyer

So I fired up Visual C# Express and started hacking. I soon found the PDFbox component – using IKVM.NET – and it didn’t take long before I had some code that actually extracted the text from a PDF file. (a PDF extraction in C# howto)

I figured out how to detect unwanted line breaks: Each line with an unwanted line break ends with a space character. Lines with a wanted line break doesn’t (in 99% of the cases). So it is just a matter of of looping over the lines and if it ends with a space skip adding a line break and just append it to the previous text buffer.

Unwanted line breaks removed
Unwanted line breaks removed

So now I just have to clean up the interface and bug test the program – which will happen automatically since I’m copy and paste from a whole bunch of PDFs at the moment. When I feel it’s working alright I will release the program. It’s really nothing hardcore about it anyway ;)

Textifyer: Drag-n-drop enabled
Of course there’s drag-n-drop support!

Bitlyfier – A Bit.ly client for GNOME

Published 7 January, 2010 in Applications , Linux , Programming - 0 Comments

Bitlyfier 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
Shortening an URL with Bitlyfier

The shortened URL
The Bit.ly 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 bitlyfier settings dialog

The command line interface

For the hacker you, Bitlyfier can also be used as a command line tool. These are the options:

11 lines of Plain text
  1. Usage:
  2. bitlyfier [OPTION…] – Bitlyfier, URL shortener/expander
  3. Help Options:
  4. -h, –help Show help options
  5. Application Options:
  6. -e, –expand Expands the given URL
  7. -s, –shorten Shortens the given URL
  8. -n, –no-gui Sets the application in command line mode
  9. -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:

14 lines of Vala
  1. // main.vala
  2. // Compile: valac –pkg gee-1.0 –pkg json-glib-1.0 –pkg libsoup-2.4 -o main
  3. int main(string[] argv)
  4. {
  5. Bitly.Api api = new Bitly.Api(“username”, “R_the_api_key”);
  6. Bitly.Response response = api.shorten(“http://domain.com/the/long/url”);
  7. stdout.printf(“Short URL: %s\n”, response.get_string(“shortUrl”));
  8. response = api.stats(“A2ma2z”);
  9. stdout.printf(“Clicks: %d\n”, response.get_integer(“clicks”));
  10. return 0;
  11. }

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

Published 22 December, 2009 in Programming - 0 Comments

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:

5 lines of JavaScript
  1. var uri = new URI(“https://poppa.se/javascript-uri-class/”);
  2. console.log(uri.scheme); //-> http
  3. console.log(uri.host); //-> poppa.se
  4. console.log(uri.path); //-> /javascript-uri-class/
  5. console.log(uri.port); //-> 80

Now, if we want to alter the host so that it contains www we do:

2 lines of JavaScript
  1. uri.host = “www.poppa.se”;
  2. console.log(uri.toString()); //-> https://poppa.se/javascript-uri-class/

It’s also easy to alter query string variables:

4 lines of JavaScript
  1. var uri = new URI(“http://host.com/?name=poppa&lang=se”);
  2. uri.variables[“name”] = ‘Günther’;
  3. uri.variables[“lang”] = ‘de’;
  4. console.log(uri.toString()); //-> http://host.com/?name=Günther&lang=de

And I think that’s pretty smooth :)

Download the URI class

New Roxen Application Launcher for Linux written in Vala

Published 20 December, 2009 in Linux , Programming , Roxen - 0 Comments
This is not the latest version of Roxen Application Launcher. You’ll find the latest version at the download page.

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.

List of downloaded files
Screenshot 1 of the Roxen Application Launcher

Adding support for new file type
Screenshot 2 of the Roxen Application Launcher

The GNOME status icon
Screenshot 3 of the Roxen Application Launcher