Another day, another “head first” book

With pleasure I found out that Head First Design Patterns was in the bookstore I’m usually getting tech-books at (I like going to a store, buying the book and then immediately begin reading it – this is why I don’t order all books over the web). The book was hidden in the shelf full of UML-books where it should have been placed near the Java-books: It’s really Java-centric.

As I noted here, I really like the head first series and if you ask me, head first design patterns is the best so far which may be because the topic really, really interests me. Additionally, I so far found much less mistakes than in head first jsp (where there were quite some).

This new book of the series has something the others don’t: It has suspense. Always when one of the patterns is explained, I’m so much looking forward to learn what the next pattern will be and what the next example will be.

I’m not a theoretical guy, so it’s quite difficult to keep me reading when dry topics are to be explained. Not so with head first design patterns: They keep it interesting and they keep explaining by example (very good ones by the way). It’s really well done.

I’m now about in the middle of the book (the command pattern) and while I alreay knew some things, I was able to learn a good deal of new stuff (and the correct terminlogoy to use) and interestingly, it’s sticking in my brain. I can remember every single important thing (the rocket-powered rubber-duck, for example. Btw: Rubber-ducks do fly indeed: Just throw it out of the window and they fly – in one direction only, but they do fly. The fly()-method would have had to be overriden by many ducks anyways, but I agree, the strategy-pattern is the better solution).

Even if you are not interested in design-patterns: Go and get this book. Even during reading the very first chapter you’ll soon get interested and by the middle of the book you long for every second of free time to continue going on reading just to learn what the next pattern may be and what example may be used to explain it.

Incredibly great stuff.

Serendipity

Last sunday I somehow came across Serendipity.

Besides being the only project where hitting Ctrl-V is simpler than actually spelling the name (let alone pronouncing it), this blogging engine shows much promise for me.

It has some obvious advantages over MT for me:

  • It’s OpenSource. Hacking it isn’t a crime.
  • It’s written in PHP, one of the languages I’m really fluent in.
  • It has some great anti-spam-features (though forcing the preview here did help greatly).
  • It uses dynamically generated pages instead of statically generating each an every page.
  • It has many more features than MT does.

My only problem: It does not have an importer for Movable Type. Well, actually, the current CVS HEAD does, but it does not work either. But because of the first two points above, I could do something about that.

Now, this evening, I will be working on the comments importer and tomorrow you will be able to have a look at how well my patches work (at least with this blogs data)

And sometime later this year, I will be using Serendipity as my blogging engine (hopefully with many more patches by myself). That’s for sure!

What I hate about PHP

This is what I really hate about PHP:

pilif@galadriel ~ % cat test.php
<?
if (10 == '10ABC')
    echo "Gnegg!n";
?>
pilif@galadriel ~ % php test.php
Gnegg!

This is the reason for a pretty serious bug in my current i’m-loving-doing-that-as-it’s-the-greatest-ever-project

What happens is that PHP implicitly converts 10ABC to an integer (yielding 10) and then making an integer comparison.

In my oppinion, this is wrong as inplicitely converting a string to an integer can cause information to be lost. Would PHP have converted 10 to ’10’, the comparison would have worked like one expects because converting an intger to a string works without losing information.

Then again, integer-conversions are more accurate than string conversions, so I can understand PHP’s way. What I cannot understand is that a non-integer string is converted to something else than 0 or nothing (while causing a runtime-error). The comparison in my example should never have evaluated to a true value (which happened, because intval('10abc') == 10!

And converting to string if one argument of a comparison is a string is not the holy grail either – problems with locale-specific decimal points come to mind (is it . or ,?).

So perls idea of using a dedicated string comparison operator may not have been a bad idea after all…

Horde 3.0

Today Horde 3.0 along with some applications using it, the most noteworthy being IMP 4.0, has been released.

For me, horde has a long history of being a pain in the ass to install and extend. While installing the first versions has been quite easy (but not possible for me back then as I did not have access to my own server and the environement of our shared hoster did not have all the extensions needed – let alone shell access), it grew quite complicated with 3.0 onwards.

My main problem has been and is that Horde is not really a framework for application developement, but a frontend-container. It’s not possible to just install IMP. You’re always installing a kind-of groupware-application (Horde) and only them the webmail component

If you don’t do it right, you actually force your users to login in twice when checking their email (once in horde, once in IMP)

As always, I really had to take a look at those new releases.

As the horde main server is quite busy currently, I’ve downloaded from the mirror in the netherlands – the others where either not reachable or not current.

After downloading the horde framework, satisfying the very long list of dependencies took some time. Especially tricky was the fileinfo PECL-extension but this was because of a problem with my local PHP installation. Glad I found out now and could fix it

Then came the configuration. What a nicely done web interface! Unfortunately, I just managed to lock myself out (I chose “IMAP Server” as authentication source not knowing that this only works after IMP is installed and IMP cannot be installed without a working horde installation…)

After those things where setteled, I came to the installation of IMP. Easy procedure here – after getting used to with the framework itself before.

Then I’ve configured horde to use IMP as authentication source which did not work at first but after copying over the backup configuration file and trying again, it finally worked (don’t ask me what I did the second time).

My next problem was the preset settings for my users: Per default, it’s using a 12 hours time format, Arabia as location and somewhere in Africa for the time zone.

As I cannot ask my (possible) users to change those preferences, I looked for a way to do that and while doing that I began to understand how the Horde configuration system works.

Now, I’m quite impressed about how they are doing this: it’s generic, it’s configurable and every single feature can be locked down for the end users. Very nice.

Just make your configuration changes in config/prefs.php. If you need a list of possible values, either read the source, or easier: Just look at the HTML source of the preferences-screens.

If I had a whish for the next release: Provide a way for the administrator changing those settings via the Web-Frontend.

While I first just installed IMP, which worked flawslessly out-of-the box, I ventured further and installed kronolith, turba, nag and fiinally even chora. Additionally, I configured Horde to give access to chora only to me. Comfortable. Even more impressive, when I recall that the whole user-management is done via the XAMS environement (by using IMP to authenicate the users).

All in all, I still would whish to hide away horde and just install IMP (with a small, simple integrated addressbook), but as a) IMP really is the best (PHP-based – I don’t know no others) webmailer out there and as the other applications work really nicely (even with PHP5, though it’s not officially supported), I can live with that limitation.

Now, I have two tasks ahead of me:

  1. Provide support for changing the XAMS account password from within the web interface. This will be a great opportunity to learn how the preferences system really works.
  2. Teach Ingo how to create Exim-Filters as this is the filtering system that could most easily be integrated into XAMS. When I designed the initial draft of XAMS (then still called pmail), I took great pride that the mail delivery does not cause a non MTA-process to be forked an I want to keep it that way. It saves resources under high mail load.

After the christmas days, I certainly will know what the new Horde/IMP is made of. From an Administrators/Users perspective, it’s a great release.

Thank you guys!

Internet Explorer, File Downloads, PHP

Have you ever tried sending a file to Internet Explorer, for which an internal displaying plugin is installed? Take a .CSV-File for example (or a PDF for that matter).

If so, then maybe you have noticed that IE in some versions just displays an error-message about not being able to find the file just downloaded whenever you have a call to session_start() in your script.

The problem is with the Headers PHPs session management sends to the browser: It disallows any cahing and tells that the document expired somewhere around my year of birth (1981). It seems like IE takes that literaly and really does not cache the doument, but then naturally is unable to forward it to the plugin (or activex-control or whatever).

Fortunately, you may change PHPs default headers by just emitting some additional header()-calls:

    header('Content-Type:  application/csv');
    header('Pragma: cache');
    header('Cache-Control: public, must-revalidate, max-age=0');
    header('Connection: close');
    header('Expires: '.date('r', time()+60*60));
    header('Last-Modified: '.date('r', time()));

A short explanation of the headers sent:

  1. The content-type tells the browser that there’s a CSV file coming
  2. Pragma is an old HTTP/1.0-Header. This one allows caching of the resource
  3. Cache-Control is the new HTTP/1.1 header to replace Pragma. “public” means: Public proxies may cache the document (private would also work and would mean: Cache in the Browsers cache). must-revalidate advises proxy servers (and browsers) to check if the resource is modified whenever the document is older than max_age seconds.
  4. The connection-header tells the server and browser what to do with the connection when the resource has been transmitted. The old HTTP/1.0 behaviour is close. keep-alive would be the newer behaviour. I’m not sure whether this really is necessary here, but with this header, it definitely works.
  5. The Expires-Header tells the browser when the document is going to expire. PHP default this to somewhere in 1981 and I think this is what causes the problem for IE. I set it to one hour in the future. If it were possible to just turn off those default-headers, I would simply send no Expires-header at all.
  6. Last-Modified tells the browser when the resource was last modified. I could actually get a timestamp of the underlying data representation and output that so the browser would not have to redownload the resource when the data has not changed, but it’s changing that often that this optimization is not worth the trouble, so I’m telling it just changed.

I have confirmation that this solves the problems some clients where expecting before. Very nice.

Delphi 2005

I got my hands on the demo-version of Delphi 2005 (download it here), and I actually have configured the beast already, so I have my usual environement to work on PopScan with it. These are my first impressions (I won’t talk about this File-Download-Window-Popping-Up Problem as all know it’s a nasty problem with a security-patch from Microsoft which will soon be fixed. Read about it here on Steves blog

  • It takes quite some time to start up. After removing the Delphi.NET and c# personalities (don’t need them), it starts about as fast, als my Delphi 7 did. Just a little bit slower
  • The compiler got faster, if you ask me.
  • Besides the great new features Borland is talking about, there are very nice usability-tunings everywhere which make working quite a bit easier.
  • The VCL form designer is extremely slow on my machine. Just displaying the PopScan Main Form within the designer takes nearly 10 Seconds. Delphi 7 does that instantly.
  • The debugger is slower too, which certainly has to do with the many great feature additions. I can live with that.
  • It’s extremely compatible to Delphi 7: I could install every single third party component without any problems. This is quite impressive considering Delphi 2005 is quite a rewrite.
  • While I like the new docked form designer, there’s one usability-problem with it: When you have components that use their own property editors (like Toolbar 2000), those editors are opened in their own window (understandable). Now, if you select a button in the component editor and then click the Project inspector to change a property, the Delphi Main Window will cover the property editor rendring it invisible. An easy fix would be to define the property editor always-on-top – a better fix would be integrating it somewhere in the IDE
  • Even JCLDebug could be compiled and installed (even the IDE Expert did work, though you have to manually install it)

All in all, this release of Delphi is a very great release providing the user with a ton of new features and fixes to long-standing usability problems (so long that you got used to them and now are missing them…). I have not expirienced any crashes so far (besides the one where the expat-parser of a debugged application took all the ram on my system, but I don’t blame Delphi for that), which is very nice.

Now, if only the beast could be made to run a bit faster (which will be done, I’d say, it’s the best Delphi since Delphi 2 which means quite a lot…

Thanks Borland.

PS: I know that it’s currently more in fashion to bash Borland and to whine about everything they do. And for the fourth consecutive year now I read posting about Delphi’s impending doom everywhere on the net. But consider this: Delphi still is the only RAD tool out there producing 100% native windows executables. And it still has one of the most lively communities I know of in the Windows-world. Even if Borland would kill off delphi, I’m quite certain, it will not go so easily. Not with this community.

On and speaking of killing off delphi: Seeing this great release of Delphi 2005, I am quite assured that Borland will continue supporting us.

So: Quit whining around!

XMLHTTP

Imagine, you are working on a webshop.

Imagine further, that you have a page displaying the users shoppingcart. Left of each entry, there’s an <input type="text"> for letting the use change the quantity of the article. Till now quite a common scenario, isn’t it?

Now in the time of DHTML and all that, you write some JavaScript to automatically recalculate the grand total of your shoppingcart on-the-fly, as the user is changing the quantities. This is very nice, as the user gets immediate response to her actions. No reloading the page is involved.

Now imagine further that the user has changed quite some quantities. The new cart is nothing like the old one was. The user is very happy with the total recalulating itself on every key she presses while the focus is in one of those editfieds. Very nice.

Now the user realizes that she needs another product. She clicks on the “Browse”-Link and …

What happens?

Well,… the link certainly works and she browses around in the shop looking for another product to order. But there’s a serious problem lurking around: As all the calculations were done on the client when the user changed the quantities, the server knows nothing about the changes. The server still thinks (provided something like a HTTP-Session-Emulation being at work – but how would you implement a shopping-cart without it?) the quantities are unchanged. When the user looks at the cart the next time (even after reloading the cart-page), she will see all the old values.

How to fix this? (Jonas, if you read this entry: This is about the solution to a problem we faced about a year ago while working on PopScan SMB). Most common today is one of the following:

  • Post the form on every change of the quantity. While this fixes the problem, it’s not very convinient for the user – especially if she uses a slow modem link. And even if the link is fast: Reloading the page whenever I’m tabbing out of an edit-field is very disturbing (though I’ve seen sites where the page even reloads on every key press).
  • Don’t recalculate anything, but provide an “Update values”-Button. This is what most users are used to as this is how the web worked so far: You enter something, you submit it to the server or you lose it.

Now this is where XMLHTTP comes to play.

While it has XML in it’s name, it has very less to do with XML. It’s a technology to send HTTP-Requests from JavaScript. And not only that: The requests are sent completely transparent to the end-user in the background. She doesn’t notice the slightest thing while the script is posting requests. As the API is asynchronous, there even is no waiting involved – not even over slow lines.

So.. how does it work?

I used this function to post back quantity-changes from my shoppingcart:

function updateToServer(quant,art){
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope
    // with old IE versions and security blocked creation of
    // the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
      xmlhttp = new XMLHttpRequest();
    }

    xmlhttp.open("GET", "/index.php/order/qchg?a="+encodeURI(art)+
           "&q="+encodeURI(quant),true);

    /* not interested in feedback. if it doesn't work, too bad. other
       methods provide fallback
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
        alert(xmlhttp.responseText)
      }

    }*/
 xmlhttp.send(null)
}

(disclaimer: much of the code comes from this page. If you know, what you are doing, copy&paste really is a timesaver.)

What does it do?

  1. It uses some IE-trickery with conditional code to instantiate the object.
  2. If the IE-code does not get run (on every standards-compliant browser), it uses the common way to instantiate the thing
  3. It prepares the request
  4. It sets up some event-handlers. As I’m not interested in the outcome, I’m not setting up anything.

As you can see, I created a special url for accessing my shop-system, just for updating the quantities.

This function is called from the onChange-event of the quantity-change-input-boxes. Now, whenever the user changes a quantity, /index.php/order/qchg is called, advising the server to update the quantity (if you find the URL strange – using PATH_INFO and all that: I will post something about a PHP-design-pattern that I’m using that has proven to be the most powerful in all those years I’ve been working with PHP).

Problem solved.

And just 30 minutes after implementing this method, I found out that for the purpose I’m using it, this whole XMLHTTP-thing would not be necessary:

While some trickery with FRAMEs could do the same thing, the really best method that even works with Netscape 4.x (even 3.x, if I remember correctly) would be to conditionally change the URL of a (transparent 1px2) image. This works always if no feedback from the script must be evaluated:

Pseudocode:

function updateToServer(quant, art){
 document.images['qposter'].src="/index.php/order/qchg?a="+encodeURI(art)+
             "&q="+encodeURI(quant);
}

A one-liner, no frame-trickery (frames are bad – even for such things), no finding out what object to instantiate, no problems with near-browsers,… very nice, but nowhere near structural markup, which is why I prefer the less hacky solution.

I hope, this was helpful for you. And as I’m progressing with this very interesting project I’m working on, I certianly will have more of such things to post.

Web Programming with CSS

For the first time in a very long time, I’m able to use a completely de-table-ized design in pure XHTML and CSS for creating a little web-application in PHP.

While many people just quote less bandwidth usage and better maintainable HTML-Code as the big advantages of using pure CSS layouts, let me add another one: Extremely increased productivity for programmers bringing interactivity to the layout

It’s a real pleasure: Never was it so easy to just concentrate on the functionality. No layout-information creeping into the business logic because it’s the only way to get some stupid placeholder GIF into the layout. No more error-prone stitching together immense and complicated HTML-snippets. No more debugging what went wrong when building one of those complicated layout tables.

And of course: No more pulling out hears when having a look at the size of the generated HTML-Code

I’ve never been as productive in coding a web application than in this case where the HTML-code is clean and the design is where it belongs to: In the CSS (which I don’t have to touch (anymore – the whole thing happens to be written by myself – Richard isn’t that good in CSS yet))

If only all future projects would be CSS-only. It would make live so much easier…

SQLite on .NET CF

SQLite just doesn’t stop to amaze me. First, we got it to compile on our small ucLinux based barcode scanner where it not only works flawlessly, but extremely fast too.

Now I thought about using SQLite in a little PocketPC application I intend to write using the .NET compact framework. This after some very bad expirience with SQL Server CE

  • There is no useful frontend to modify the data in the sdf-File: There is no tool for the desktop (besides using SQL-Server and then replicating the data to the device which I actually got to work this march or so, but it was a major pain in the ass to set up and is no solution to me. I mean: Why should I install a whole SQL-Server just to get some test-data to a smart device?) and the little frontend on the PocketPC suffers from the small screen and the lack of a keyboard.
  • Despite everyone claiming it’s fast, it isn’t (though this certainly is relative. I’m sure, the marketing departement of MS is still conviced that it’s fast). Where some operations may be, others are not. Searching for strings is an example of extreme slowness.
  • Starting an application using SQL Server CE takes about a minute on a usual 400 Mhz PocketPC. Way too long to be used in production with customers.

So, using a leightweight local SQL-engine which is fast even on a 66 Mhz CPU without MMU sounded quite appealing to me. Just: How much work would it be? How well would I be able to integrate SQLite into .NET?

Knowing about the lack of features in P/Invoke on the compact framework and knowing that the SQLite API uses callback functions, I feared the worst, but fortunately, I googled before getting to work.

So, I found this project.

They provide you with a full-fledged ADO.NET driver for SQLite, so you can use all the database classes and components you are used to, while still profiting from the advantages of SQLite

Compiling it was easy (while they provide pre-built binaries of sqlite.dll and the P/Invoke-Wrapper sqlite_wrapper.dll, they do so only for ARM and the desktop version of Windows for x86, so if you want to use the emulator for developement, you have to build those two DLLs yourself – using eVC4) and a quick look (it’s already late now – I got up more than 17 hours ago, so I’m quite tired now) using the sample project here was quite successful: The application started (instantly, no wait) and displayed the data inside the SQLite-File.

So, the speed-problem is solved. What about the frontend? While I don’t know any Windows GUI frontends for SQLite (though I know they exist), I have already worked with the SQLite ODBC driver (it’s funny to think of that: Usually ODBC-drivers are just a middleware between the Application and the dtabase, but in case of sqliteodbc, the database engine is linked into the ODBC-driver. Strange) and of course the command line tool and the PHP extension. So for my purposes, I’m going to create the database using a PHP-Script on Linux and copy the .db-File to the PocketPC. As seamless as possible. No replication, no installation of servers, no nothing. Just plain old copy.

More programmers fonts

In an older post of Urban Mainfraime, I read about some fonts for programmers. Jonathan recommends Anonymous and Bitstream Vera Sans Mono.

While both fonts are certainly better than the omnipresent Courier, I still think, ProFont, which I’ve already written about is much better than te other two alternatvies.

ProFont is a Bitmap font, optimized for 8px size, which is incredibly small. Because every character is hand-painted, the font is still very readable desite it’s small size, so it’s great if you want to have much text on little space – als always when programming ;-)

Maybe it takes a litte getting-used-to, but every Monospaced font I’ve seen since I switched to ProFont in Putty, Thunderbird, Delphi, jEdit and all other applications, looks clunky and too large for my eyes. Try it!