Archive for the 'Programming' Category

Glade Catalog Tutorial

Tuesday, August 18th, 2009

For some time now Glade allows to add custom widgets and objects to an interface file by making a catalog. Many people are asking how exactly it can be done. I did not see any hands on guide, step by step howto, or just any plain example of a custom widget catalog yet. Recently I’ve found some spare hours to play with Glade and experiment with the feature. It happens to be not that hard after all, and I decided it would be beneficial to share the experience by writing this little tutorial.

Before anything I must note that sometimes all you need is to “fake” a class in the catalog. To get an idea you can read a little post in the blog of one of the Glade developers. It provides you a tiny example of a catalog. In general though, you need to build a shared library containing all your classes to make them available to Glade at the run time. Let’s take as an example the clock face widget from the ancient Gnome Journal February 16, 2006 issue and try add it to a Glade file. The sources ain’t clearly marked with any license, but I’m sure the author wouldn’t mind.

The first step is to make a working directory, download the files, give the sources appropriate names, and see if all of this works together:

mkdir eggclock
cd eggclock
wget http://www.gnome.org/~davyd/gnome-journal-cairo-article/clock-ex5.c
wget http://www.gnome.org/~davyd/gnome-journal-cairo-article/clock-ex5.h
wget http://www.gnome.org/~davyd/gnome-journal-cairo-article/main-ex5.c
wget http://www.gnome.org/~davyd/gnome-journal-cairo-article/clock-marshallers.list
wget http://www.gnome.org/~davyd/gnome-journal-cairo-article/Makefile
mv clock-ex5.c clock.c
mv clock-ex5.h clock.h
mv main-ex5.c main.c
make

I took the freedom to put it all in a single archive to ease your pain of going through the above steps. Assuming you have all appropriate development libraries installed you should have a working application now, and can run it in usual manner:

./clock

The second step is to modify the Makefile to create a shared library. The widget is named EggClockFace for a reason unclear to me, but whatever this “egg” stands for it is a namespace, so let’s give the library unsophisticated name libegg.so and replace the single rule for making executable with something like this:

clock: main.o libegg.so
gcc -g -rdynamic -o clock `pkg-config –libs gtk+-2.0` $^
main.o: main.c clock.h
gcc -g -c `pkg-config –cflags gtk+-2.0` $<
libegg.so: clock.o clock-marshallers.o
gcc -g -shared -o libegg.so `pkg-config –libs gtk+-2.0` $^
clock.o: clock.c clock.h clock-marshallers.h
gcc -fpic -g -c `pkg-config –cflags gtk+-2.0` $<
clock-marshallers.o: clock-marshallers.c
gcc -fpic -g -c `pkg-config –cflags gtk+-2.0` $<

Don’t forget that all command lines should start with a tab. Actually you should give your library proper soname, install it in in a proper location, and all that stuff, but I’m leaving that for you as an exercise. For the tutorial purposes this quick and dirty approach would do just fine, and it is sufficient to start the application setting the working directory as the library path directly from the command line:

LD_LIBRARY_PATH=. ./clock

The third step is to create the catalog file as simple as this:

<?xml version=”1.0″ encoding=”UTF-8″?>
<glade-catalog name=”egg” library=”libegg.so” depends=”gtk+”>
<glade-widget-classes>
<glade-widget-class name=”EggClockFace” generic-name=”clockface” title=”Clock Face” />
</glade-widget-classes>
<glade-widget-group name=”egg-widgets” title=”Egg Widgets”>
<glade-widget-class-ref name=”EggClockFace”/>
</glade-widget-group>
</glade-catalog>

Save it again in the working directory and invoke Glade in this manner:

GLADE_CATALOG_PATH=. GLADE_MODULE_PATH=. glade-3

Now you should see “Egg Widgets” group in the palette and a little iconless button under it. That would be our clock face widget. Create a top level window and put it in there:

Custom widget in Glade

All that is left for the final step is to is to save this file and modify the main.c to make use of it. Let’s set the “visible” property for the window to true, set gtk_main_quit as its “delete-event” handler and time_changed_cb as the handler for the clock face “time-changed” signal, give it a simple name clock.ui and replace the code of the main function in the main.c with the following:

GtkBuilder *builder;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, “clock.ui”, NULL);
gtk_builder_connect_signals (builder, NULL);
g_object_unref (builder);
gtk_main ();
return 0;

Also you’d need to remove “static” from time_changed_cb definition, so GtkBuilder would be able to find the handler. And that’s about it. Now you’re ready to clutter your user interface with multitude of random clocks in all sizes and alignments, and all of them going to be ticking right inside the Glade window. In case you need to see complete sources, just download and extract the final archive.

As you can see, you have to do very little to make your own Glade catalog, so start hacking, file any bugs you have found, and by all means share your work. Feel free to ask any questions on the mailing list, but read the documentation first. Mind though, that with general programming questions you probably would be better off at GTK forums. Any comments regarding this tutorial are also appreciated.

Who the Hell is a Hacker?

Thursday, March 26th, 2009

I am a hacker. I do like this word. It is neat and sharp. Unfortunately general population understanding of the word is distorted. The narrow meaning of it, as it is commonly used and promoted by the media, describes a person who breaks into other people’s computers. I am nothing like that.

I am a programmer. Some of our kin claim that it is original and the only true meaning of the word hacker. To distinguish the “true hacker” from evil computer intruders they propose to call these wrongdoers “crackers” or something funny like that. I do not support that opinion. These computer security freaks, as evil as they are, can be considered hackers too.

It is like if the word “knife” would be almost exclusively used by public to describe a weapon. Now cooks and chiefs, frustrated by association with bloody murderers, would argue that the proper name for the weapon should be “dagger”. That neither would solve the problem, nor it is true. Knife is a knife and its purpose is to cut things, regardless of what these things are.

So, who the hell is a hacker? Hacker is obviously a person who does hacks. That is the root of all confusion. Average people are simply unaware of the very existence of this noun. The term hack can be defined as a crafty solution to a technical obstacle. Hacker is just a person who is willing to fiddle with things where other people would just give up and turn to other ways.

I would even argue that not all programmers can be considered hackers. Some programmers if not most of them are just that - skilled professionals. Hacking opportunities for a programmer in the closed source world are extremely limited. When some program does not do whatever you want it to do, your only option most of the time is to write another one from scratch. That may be a solution, but hardly a hack, regardless of how much better the new program is. Until you open the source code very little if any hacking is possible.

So to speak, when language is distorted, it leads to misunderstanding. Misunderstanding leads to frustration. Frustration leads to anger. Anger, as we all know, leads to dead kittens. When I use the word hacker and people are thinking about some criminal, it is very frustrating. My point is, however, all attempts to redefine things represent direct path to dead kittens. I am trying to educate people whenever possible instead, one at a time. I do not believe it would change overall situation one day. I just like to make things right at least around myself.

Bugzilla on Fedora 8

Saturday, April 19th, 2008

My boss asked me to install Bugzilla on our local server. The task happened to be relatively easy, however Bugzilla needs functioning mail server to be anyhow useful. I tried my SMTP server, but it did not work very well, probably because of our firewall which I have no control of. Sendmail is not functioning too, maybe for the same reason, and I have no clue how to troubleshot that animal. Anyway, mail servers are currently beyond my means, but I find it useful to document basic Bugzilla setup for historical purposes.

First of all I needed functioning web server. Apache already was there, so the only thing I had to do was starting the service:

# /sbin/service httpd start

It was immediately accessible at http://localhost.

Next step was to install MySQL server, since Bugzilla needs one. That went smoothly. The guide I googled up (do not remember which one exactly) instructed first off to secure MySQL with following commands:

$ mysql -u root mysql
mysql> UPDATE user SET password = password(’FryWucThyft4′) WHERE user = ‘root’;
mysql> FLUSH PRIVILEGES;

where “FryWucThyft4″ is some random password for database root account. To test it, disable anonymous user, and set up bugzilla user with password “WocEfcic5″:

$ mysql -u root -p mysql
mysql> DELETE FROM user WHERE user = ‘’;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, INDEX, ALTER, CREATE, LOCK TABLES, CREATE TEMPORARY TABLES, DROP, REFERENCES ON bugs.* TO bugs@localhost IDENTIFIED BY ‘WocEfcic5′;
mysql> FLUSH PRIVILEGES;

After that I’ve edited /etc/my.cnf to add this random stuff:

[mysqld]
# Prevent network access to MySQL.
skip-networking
# Allow packets up to 1M
max_allowed_packet=1M
# Allow small words in full-text indexes
ft_min_word_len=2

At that point I’ve installed Bugzilla per se. When it was there I’ve changed configuration file to accommodate for new fancy password in /etc/bugzilla/localconfig. The variable of interest was $db_pass. When it was done, I was ready to create the database:

# /usr/share/bugzilla/checksetup.pl

It asked to set up administrator email, real name, and password. When the database was created I followed advise to allow the table to grow to 20GB (it is said to be 4GB by default):

$ mysql -u bugs -p
mysql> use bugs
mysql> ALTER TABLE attachments AVG_ROW_LENGTH=1000000, MAX_ROWS=20000;

Finally I had to make bugzilla accessible from our web server, so I made a link in web server’s root:

# ln -s /usr/share/bugzilla/ /var/www/html/bugzilla

and adjusted directory settings in /etc/httpd/conf/httpd.conf to be:

AddHandler cgi-script .cgi
Options +Indexes +ExecCGI +FollowSymLinks
DirectoryIndex index.cgi
AllowOverride Limit

After I’ve restarted the web server to read new configuration:

# /sbin/service httpd restart

new shiny Bugzilla was at http://localhost/bugzilla/.

After first login it asks to change parameters: maintainer, urlbase, cookiepath, utf8, requirelogin, createemailregexp, and mail delivery method. The last one is the problem…