Windows 8 style apps in WPF

After building my first Windows 8 store app (using XAML and C#), I wondered whether it is doable to build desktop apps in the same style, without WINRT classes.
Since I preferred using XAML and C#, WPF was the application framework of choice.

Second step, how to build a Windows 8-style UI (formally known as Metro UI) without building my own controls form scratch?
Turns out there is an excellent solution for this: the mahapps.metro UI kit. Mahapps does the styling of common controls for you, and packs some Windows 8 specific controls, like a Panorama (sideways scrolling) view.

Last step was including Windows 8 components that make life as a programmer easier:

  • Asynchronous Programming with Async and Await, via Async Targeting Pack.
  • ASP.NET Web API. NuGet: `Install-Package Microsoft.AspNet.WebApi`.

Result is a personal reference app. What it does it simple: show current departure times for any bus stop and train station in the Netherlands, using the 9292ov (non-official) JSON API.



CodeIgniter – GAS ORM

CodeIgniter comes with an ActiveRecord implementation that is fairly straightforward. You can retrieve table rows easily, but you have to write your own logic if you need an extra layer of abstraction (models). Models can help for example to traverse relationships between entities easily, by lazy or eager-loading related entites when they are needed.

There are many PHP ORMs available that provide this abstraction, but none of them integrates nicely with CodeIgniter’s ActiveRecord. At least, I could find any.

I was looking for an ORM that:

  • is a layer on top of CodeIgniter’s ActiveRecord implementation. I don’t like importing a large 3th party library like Doctrine into my CodeIgniter projects, and trick CodeIgniter into using that instead of its own databases access layer.
  • Provides freedom in how and when to use it. I like the idea of convention over configuration, but in real world situations, using a framework that enforces too strict can be a burden. For example when using a legacy database.

Enter GAS ORM. Gas ORM is an excellent initiative by a single developer: Taufan Adity. GAS can be mixed with ActiveRecord calls, and supports convention as well as configuration. On top of that, the developer welcomes requests and questions. If you want to make handling database access with CodeIgniter easier: try it!

Gas code example:

$this->load->library('gas');
// to see available models :
// var_dump($this->gas->list_models());
$user = new User;

// Now you can use any available Gas method, or your User models public method

$user1 = $user->find(1);

$user1 = Gas::factory('user')->find(1);

I haven’t tested GAS extensively yet though, but the first version looks very promising.

Links

I’m developing a small console application in C# to convert .gde files – file format of the wonderful “The Guide outliner” – to .chm (Microsoft Compiled HTML Help).

First step is to convert a .gde file to XML. This can be done with gdeutil, a tool included with The Guide. However, gedeutil.exe does not create a valid XML files: the character ‘&’ in node titles is not escaped to ‘&’.

So, I had to incorporate an XML preprocessing step in my tool, in which unescaped charachters are replaced by their XML entities. Otherwise, the document can not be parsed by the .NET XML parser (or most other parsers).

This is the method I created for this purpose:

/// <summary>
/// Inserts '&amp;' for '&' character in XML text.
/// </summary>
/// <param name="xmlText"></param>
public static String PreProcess(String xmlText)
{
    if (String.IsNullOrEmpty(xmlText))
        return xmlText;
   
    bool ampersand = false;

    StringBuilder output = new StringBuilder();
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < xmlText.Length; i++)
    {
        char c = xmlText[i];
        if (c == '&')
        {
            // Maybe this is the start of an entity
            ampersand = true;
            buffer.Append(c);
        }
        else if (ampersand && c >= 64 && c <= 122)
        {                                        
            buffer.Append(c);
        }
        else if (ampersand && c == ';')
        {
            // Turns out to be an entity; don't change the output                
            output.Append(buffer.ToString());
            buffer.Clear();
            output.Append(c);
            ampersand = false;
        }
        else if (ampersand && (c < 64 || c > 122))
        {
            // Turns out not to be an entity                                      
            output.Append("&amp;" + c);
            buffer.Clear();
            ampersand = false;
        }
        else
        {
            output.Append(c);
        }                
    }
    return output.ToString();
}

Note that this is not a way of escaping entities in a text string (thats what HttpUtility.HtmlEncode is for), but a method of escaping characters in a complete XML document that includes tags The trick is to ignore already escaped characters; otherwise a simple search ‘&’ and replace with ‘&amp;’ would suffice.

I’m aware that this is not a fail-safe method. Nevertheless, I’m confident that this method is robust enough for use with XML files produced by gdeutil

Links

A simple captcha’s effect

Last July I implemented a simple captcha on this blog. Even though this type of captcha can be easily passed by an automated system (I wanted to keep comment posting as easy as possible for humans), the effect on received comment spam is very satisfactory as the image below shows.
(click for large version)


Aksimet stats screenshot.

Until this weekend, my last C++ program (more like a hello world “application”), dated from a few years ago. This weekend, I needed to write a small utility, that had to be very lightweight and fast. So I thought, why not try to write it C++? I must say, it was a bit of a challenge being a C# .NET programmer, but I succeeded. The biggest challenge though, turned out to be understanding strings in Visual C++.

As a C# programmer – used to just simply using the “String” class whenever text is involved – I was utterly confused to find many different constructs for strings in (Visual) C++. LPTSTR, LPCSTR, CString, TCHAR[], std:string… just to name a few. String types seem to live on different islands and in villages. There is an Unicode island, an ANSI island. On both islands there are “standard” and Microsoft villages.

It is very difficult to get a clear overview of Visual C++ string landscape, even by Googling. What I found where mainly forum threads with confusing answers, and MSDN articles shedding light only on Microsoft variants like LPTSTR.
But, I found a lighthouse, a guide that clearly explains the why and what of Visual C++ strings:

Unraveling Strings in Visual C++.

I hope this will help other .NET programmers who found themselves lost in a sting of C++ islands.

CodeIgniter query log hook

Whenever I use an ORM, such as CodeIgniter’s Active Record implementation, I find it very important to see the actual queries generated. Mainly because the danger of using an ORM is not knowing what happens in the background, which can introduce hard to find bugs and performance problems.

Below you’ll find a CodeIgniter hook that logs all database queries to a simple text file. I found this code useful in my first CodeIgniter project (since it’s from my first CI project, I think many revisions will follow, but you’ll get the idea).

/* config/hooks.php */
$hook['post_system'][] = array(
        'class' => 'QueryLogHook',
        'function' => 'log_queries',
        'filename' => 'QueryLogHook.php',
        'filepath' => 'hooks'

/* application/hooks/QueryLogHook.php */
class QueryLogHook {

    function log_queries() {   
        $CI =& get_instance();
        $times = $CI->db->query_times;
        $dbs    = array();
        $output = NULL;    
        $queries = $CI->db->queries;

        if (count($queries) == 0)
        {
            $output .= "no queries\n";
        }
        else
        {
            foreach ($queries as $key=>$query)
            {
                $output .= $query . "\n";
            }
            $took = round(doubleval($times[$key]), 3);
            $output .= "===[took:{$took}]\n\n";
        }

        $CI->load->helper('file');
        if ( ! write_file(APPPATH  . "/logs/queries.log.txt", $output, 'a+'))
        {
             log_message('debug','Unable to write query the file');
        }  
    }

}