All posts in "Programming"

Roxen Application Launcher 1.2.1

Published 22 November, 2011 in Applications , Linux , Programming , Roxen - 0 Comments

Roxen Application Launcher 1.2.1

There’s a new release of Roxen Application Launcher (come again?) for Linux.

Although the previous release, using GTK3, came quite recently this release has some new things.

GSettings

I dumped the “keyfile” solution for the application settings in favour to GSettings. So the settings is no longer stored in a file in the application directory but rather in the system’s application settings backend. GSettings is part of GIO – the GNOME networking library – and since RAL depends on GIO no new dependency is needed. The upside is that I could put a file of source code in the bin! Plus, it’s fun learning new stuff!

Editors and content types

Previously I have kept an editor – name and command line – for every content type. Anders at Roxen thought it’d be better if editors and content types were separated. I’ve thought about that before but never bothered to do anything about it.

But now, along with GTK3, there’s a new (I think) AppInfo class and the new AppChooserButton and AppChooserDialog widgets so I thought it’d be cool to use those. So selecting an editor for a new content type is way more simple now, and it also looks nicer. Plus we get the icon for the editor in the content type list under the “Applications” tab ;)

Simple logging

I also implemented some simple logging which can be viewed under the new “Logging” tab. This will be worked upon and at the moment not very useful information is written to the log, but at least it’s a start.

Default icons

The icons in the notification popup – which only are three to the number – is now fetched from the user’s default icon theme. They we’re bundled before.

SOUP all the way

Previously I have used a little hack for saving downloaded files to disk. The problem was that the Vapi bindings for libsoup casted the data to a string which totally scrambled binary content like images and such. My solution was to write a simple C-function which took a SoupMessageBody struct as argument and then wrote that to diskt always keeping the uint8[] type of the content.

I bug reported this way back and it’s now fixed in Vala so I dumped my solution and am now using Vala all the way. Gone is one C and one Vapi file.

While at it I changed from using blocking functions in libsoup to the async ones. You never really noticed blocking calls was used before, but right is right. Right?

And that’s that for this time I think!

Roxen Application Launcher 1.2.1

Sources is available at the Roxen Application Launcher Github repository

GTK TreeViewColumn with icon and text

Published 21 November, 2011 in Linux , Programming - 0 Comments

TreeViewColumn with icon and text

The other day I wanted to put both an icon and text in the same GTK+ TreeViewColumn, and I had absolutely no idea how to do that. So I Google’d and Google’d but had trouble finding any examples. I even downloaded the source code of the Gnome System Monitor – where exactly what I wanted exist – but that was mostly written in C++ which I know very little of.

But I’m stubborn, and after a while I found and example in Python which I managed to interpret. Even though I know very little Python it’s not that hard to follow, and the example was short.

In short what’s needed is packing two CellRenderers in the same TreeViewColumn. Quite logical when you know about it. The example below is in Vala:

15 lines of Vala
  1. var tree_view = new TreeView ();
  2. var col = new TreeViewColumn ();
  3. col.title = title;
  4. col.resizable = true;
  5. var crp = new CellRendererPixbuf ();
  6. col.pack_start (crp, false);
  7. col.add_attribute (crp, “pixbuf”, 0);
  8. var crt = new CellRendererText ();
  9. col.pack_start (crt, false);
  10. col.add_attribute (crt, “text”, 1);
  11. tree_view.insert_column (col, 1);

I hacked up a simple application that shows all installed programs – that has a .desktop entry I guess – in a list (the screenshot above). The sources is available at my Github repository.

Happy coding!

Variable length argument segfaults valac

Published 26 October, 2010 in Programming - 0 Comments

Hm, found a bug in valac (the Vala compiler) today. If an abstract or virtual method contains a variable length argument (va_list) valac will segmentation fault.

19 lines of Vala
  1. // valac -o test sample.vala
  2. int main(string[] args)
  3. {
  4. return 0;
  5. }
  6. public abstract class Base : Object
  7. {
  8. public abstract int query(string query, ... );
  9. }
  10. public class Child : Base
  11. {
  12. public override int query(string query, ... )
  13. {
  14. return 1;
  15. }
  16. }

I’ve filed a bug about it so we’ll see what the problem is and if it can be fixed rapidly.

Pike project – module stub creator

Published 20 August, 2010 in Programming - 0 Comments

Screenshot of Pike project

I recently began learning how to create Pike modules in C. The Pike module C API seems great and once you’ve sorted things out the modules are easy to build and install. Non the less, when creating a C module from scratch there’s a couple of files you need and some configurations of those before everything is set for go. And here comes “pike-project” into play.

Pike-project is a simple GTK program (works as command line tool also) written in Pike it self. The program will create the basics for a running Pike C module, or a plain installable Pike module. Then it’s just starting programming.

The program is available at my Github repository.

BTW! At the moment it only works on Linux I suppose.

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.

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

Vala – the new programming language for Gnome

Published 25 October, 2009 in Linux , Programming - 0 Comments

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

239 lines of Vala
  1. /* main.vala
  2. *
  3. * Copyright (C) 2009 Pontus Östlund <spam@poppa.se>
  4. *
  5. * No license what so ever. Do what ever you like…
  6. *
  7. * Author:
  8. * Pontus Östlund <spam@poppa.se>
  9. */
  10. // GLib isn’t really neccessary since it’s imported automatically
  11. using GLib, Gee, GConf;
  12. /**
  13. * User defined exception types
  14. */
  15. errordomain IOError {
  16. FILE_NOT_FOUND,
  17. NOT_A_DIRECTORY
  18. }
  19. /**
  20. * Main class
  21. */
  22. public class Main
  23. {
  24. /**
  25. * Used as reference in option parser
  26. */
  27. static int arg_delay;
  28. /**
  29. * Application command line options
  30. */
  31. const OptionEntry[] options = {
  32. { “delay”, ‘d’, 0, OptionArg.INT, ref arg_delay,
  33. “Number of minutes to wait before swapping background”, null },
  34. { null }
  35. };
  36. /**
  37. * Main method
  38. *
  39. * @param args
  40. * Command line arguments
  41. */
  42. public static int main (string[] args)
  43. {
  44. try {
  45. var opt = new OptionContext(“\”/path/to/wallpapers\””);
  46. opt.set_help_enabled(true);
  47. opt.add_main_entries(options, null);
  48. opt.parse(ref args);
  49. }
  50. catch (GLib.Error e) {
  51. stderr.printf(“Error: %s\n”, e.message);
  52. stderr.printf(“Run ‘%s –help’ to see a full list of available “+
  53. “options\n”, args[0]);
  54. return 1;
  55. }
  56. if (args.length < 2) {
  57. stderr.printf(“Missing argument!\n”);
  58. stderr.printf(“Run ‘%s –help’ for usage\n”, args[0]);
  59. return 1;
  60. }
  61. // Default time before changing background is 30 minutes
  62. int delay = arg_delay > 0 ? arg_delay*60 : 60*30;
  63. try {
  64. WallpaperIterator bg = new WallpaperIterator(args[1], delay);
  65. bg.run();
  66. bg.stop();
  67. }
  68. catch (IOError e) {
  69. stderr.printf(“Error: %s\n”, e.message);
  70. return 1;
  71. }
  72. finally {
  73. stderr.printf(“Finally block reached!\n”);
  74. }
  75. return 0;
  76. }
  77. }
  78. /**
  79. * Class that handles changeing of desktop wallpapers on a per interval way
  80. */
  81. public class WallpaperIterator : GLib.Object
  82. {
  83. /**
  84. * Number of seconds to wait before changeing wallpaper
  85. */
  86. private int delay = 0;
  87. /**
  88. * Current index in the images list
  89. */
  90. private int index = 0;
  91. /**
  92. * List of images to change between
  93. */
  94. private ArrayList<string> files = new ArrayList<string>();
  95. /**
  96. * List of allowed content types
  97. */
  98. private ArrayList<string> allow = new ArrayList<string>();
  99. /**
  100. * GConf client object
  101. */
  102. private GConf.Client gclient = null;
  103. /**
  104. * GConf registry where the background image is set
  105. */
  106. private string gconf_key = “/desktop/gnome/background/picture_filename”;
  107. /**
  108. * Background being used when the application is started
  109. */
  110. private string default_bg = null;
  111. /**
  112. * Constructor
  113. *
  114. * @param dir
  115. * The directory to collect wallpapers in
  116. * @param delay
  117. * Time to wait – in seconds – before swapping wallpaper
  118. */
  119. public WallpaperIterator(string dir, int delay)
  120. throws IOError
  121. {
  122. assert(delay > 0);
  123. allow.add(“image/jpeg”);
  124. allow.add(“image/png”);
  125. var f = File.new_for_path(dir);
  126. if (!f.query_exists(null))
  127. throw new IOError.FILE_NOT_FOUND(” \”%s\” doesn’t exits!”.printf(dir));
  128. if (f.query_file_type(0, null) != FileType.DIRECTORY) {
  129. throw new IOError.NOT_A_DIRECTORY(” \”%s\” is not a directory!”
  130. .printf(dir));
  131. }
  132. collect(f.get_path());
  133. if (files.size < 2) {
  134. warning(“Not enough images found for this application to be useful!\n”);
  135. return;
  136. }
  137. this.delay = delay;
  138. }
  139. /**
  140. * Run the application. Starts a MainLoop
  141. */
  142. public void run()
  143. {
  144. gclient = GConf.Client.get_default();
  145. default_bg = gclient.get_string(gconf_key);
  146. MainLoop loop = new MainLoop(null, false);
  147. var time = new TimeoutSource(delay*1000);
  148. time.set_callback(() => { swap(); });
  149. time.attach(loop.get_context());
  150. loop.run();
  151. }
  152. /**
  153. * Stop the application. Tries to reset the background
  154. */
  155. public void stop()
  156. {
  157. try {
  158. if (default_bg != null)
  159. gclient.set_string(gconf_key, default_bg);
  160. }
  161. catch (GLib.Error e) {
  162. warning(“Failed to restore background!\n”);
  163. }
  164. }
  165. /**
  166. * Does the actual background swapping
  167. */
  168. private void swap()
  169. {
  170. if (index >= files.size) {
  171. debug(“Restart…\n”);
  172. index = 0;
  173. }
  174. debug(“Callback…%-2d (%s)\n”, index, files[index]);
  175. try {
  176. gclient.set_string(gconf_key, files[index]);
  177. }
  178. catch (GLib.Error e) {
  179. warning(“Failed to set background: %s\n”, e.message);
  180. }
  181. index++;
  182. }
  183. /**
  184. * Collect images
  185. *
  186. * @param path
  187. */
  188. private void collect(string path)
  189. {
  190. try {
  191. var dir = File.new_for_path(path).enumerate_children(
  192. “standard::name,standard::type,standard::content-type”,
  193. 0, null
  194. );
  195. FileInfo fi;
  196. string fp;
  197. while ((fi = dir.next_file(null)) != null) {
  198. fp = path + “/” + fi.get_name();
  199. if (fi.get_file_type() == FileType.DIRECTORY)
  200. collect(fp);
  201. else {
  202. if (fi.get_content_type() in allow)
  203. files.add(fp);
  204. else
  205. warning(“Skipping \”%s\” due to unallowed content type\n”, fp);
  206. }
  207. }
  208. }
  209. catch (GLib.Error e) {
  210. warning(“Error: %s\n”, e.message);
  211. }
  212. }
  213. }

I think Vala looks promising and I will try it out trying to write a more complex application when I find the time.