The Stochastic Game

Ramblings of General Geekery

Poor man’s search engines sync for Google Chrome (version 2)

After receiving some feedback, I’ve updated my scripts for syncing the search engine settings in Google Chrome. You can grab the new ZIP file here.

Here are the changes:

  • I’ve written some native CMD.EXE scripts for Windows, which are easier to run than the Powershell ones for non dev people (or devs that never used Powershell).
  • The scripts have been renamed to “export” and “import” since most people don’t get the “push” and “pull”.
  • The “import” script creates a backup of the settings files (a file called “Web Data.backup” in the same directory) so you can revert to the previous version if something goes wrong.

Enjoy!


My new shiny toy: the Archos 70

Tablets, and especially Android tablets, are going to be all the rage this year, with more new tablets announced every week than Lindsay Lohan had rehabs. Because I’m a geek with money to waste on unneeded electronics, I’ve decided to be part of the early adopters with my new shiny toy, the Archos 70.

IMG_2022.jpg

I would have usually waited on more mature tablets, along with a more mature version of Android (hopefully Honeycomb), but there are a few reasons I jumped on this one:

  • I’ve been a relatively faithful user of Archos’ previous portable media players (I’ve owned 2 other devices of theirs in the past, the AV500 and the 1st gen Archos 5).
  • The Futureshop warranty on my Archos 5 was about to expire, so I returned it and cashed in its retail value as a gift card in order to upgrade to the latest model “for free”.
  • I need a portable video player first, and a tablet second, so I don’t care much if the tablet experience is not so good.
  • I’m not playing games on my phone, and I don’t intend to play games on my tablet either.

Reading through Engadget’s review of the Archos 70 and his big brother the Archos 101, all I can say is that it’s a pretty fair review. If you don’t want to read the whole thing, the conclusion is that although those devices are clearly not as good as the Galaxy Tab or the iPad, for less than half the price there’s really nothing you can complain about. I’d go as far as saying that the price/quality ratio is actually higher on the Archos 70. This is especially true when compared to the Galaxy Tab, because this one is bound to be obsolete very soon (if it’s not already by the time I finish writing this) – at least the iPad features an OS and apps that are very well designed for the tablet format and are already quite mature.

Now there’s a few things I’d like to add to Engadget’s review, because I think they didn’t highlight some of the things the Archos 70/101 are doing better than the competition.

Body Design

Archos has been until now mostly a portable media player manufacturer, and it shows in the Archos 70. First, the device has a widescreen aspect ratio, unlike the big shots out there. This means that when watching movies and recent TV series, you will have smaller black bars around the screen, if any at all. For example, this is it playing Dirty Harry… See? No bars, no cropping. Just full-screen bad-assery.

IMG_2037.jpg

Speaking of the screen, it’s bright enough that you can watch videos outside to some degree, but it’s not as good as the iPad or Galaxy Tab screens, obviously. The view angles are particularly pretty horrible, but I don’t think it’s a big deal because it’s not like you can cram several people in front of a 7 inches screen anyway. For personal use, it’s “good enough”.

Second, the device has a built in stand you can unfold in the back. It’s very useful if you’re watching something while doing something else, like eating or cleaning your camera lenses or whatever. Way better porn watching experience, too 🙂

IMG_2028.jpg

The stand itself is cheap plastic and feels a bit fragile, so I wouldn’t be surprised if some people broke it at some point (which would suck).

By the way, I was expecting something bad in terms of device body after reading Engadget’s review, but it turned out okay in my opinion. It sure doesn’t feel as nice and solid as an iPad, but it doesn’t feel like a Playskool toy either. And I dropped it a couple times already and it’s completely fine (unlike an iPhone 4…). True to Archos’ tradition, however, the power and volume buttons on the side are a bit wobbly, and I expect them to be the first thing that breaks down. I wish they would learn to make sturdier buttons…

Another thing Engadget mentioned already, but it’s good to mention it again, is how thin that tablet is. It’s almost 50% thinner than the iPad. See how it’s even thinner than my iPhone 3G:

IMG_2027.jpg

It’s also super light. Archos says it’s 300 grams, which is less than half the iPad’s weight (although that’s not fair since the iPad is way bigger, with a 10 inches screen). My point is that it’s just a bit heavier than a Kindle 3 (at 250 grams), so it feels pretty natural to hold the Archos 70 in one hand while riding the bus or the subway. This is perfect for me since that’s where I watch most of my videos with it.

Media Playback

Another thing Archos is famous for is releasing devices that can read all the major (and even some minor) audio and video codecs. These tablets are no different, and so far anything I’ve thrown at it has been playing fine without the need for transcoding. Also, and that’s a big feature for me, the device appears as an external USB drive when you connect it to your computer – no need to go through some painful piece of software like iTunes. And you don’t even have to connect it at all since you can put stuff on an external mini-SD card (not included). Like any other Android device, it will scan the SD card when you mount it back and all the media that’s on it will be available. Hell, you can even just push stuff on it wirelessly with FTP or any other Wi-Fi syncing app. You’re free! Yay!

Speaking of Wi-Fi, the Archos 70 does a good job of integrating with your home network. The custom Video Player app can not only read local media, but also media from your SMB shares or your UPnP servers.

IMG_2030.jpg

If I want to finish watching some TV series episode in bed before going to sleep, I can stream it from my NAS or my XBMC box downstairs. The only thing that wouldn’t play was uncompressed rips of Blu-Ray movies (we’re talking 25 to 30 Gb HD video files, here!). I would get this error message:

IMG_2031.jpg

The rest would play smoothly across Wi-Fi. Here’s Serenity streaming from my NAS. I can even switch/toggle the audio or subtitle tracks and all that jazz.

IMG_2035.jpg

Applications

The main thing that kinda sucks about applications on the Archos 70 is how Archos decided to not install the Google Market, or any of the Google applications. That was probably to avoid having to get the device certified by Google or something, but still, it’s pretty lame. Instead, they want the user to go through some other market, AppsLib, which is decent (it has quite a lot of applications) but, well, is only a subset of the main Google Market.

Thankfully, and because we’re talking about Android here, people hacked the device pretty quickly. Archosfans was, I think, the first to offer an APK of the official market app, and that’s what the Engadget guys point you to, but now I recommend using the one from XDA. In both cases, it’s super simple: you just download a file and launch it with the built-in file browser. Once that’s done, you can get GMail and Google Reader and whatever you feel like downloading. You can even run AppBrain and copy all the apps you already have on your phone, if you have an Android phone. You’re free! Yay!

Performance

I don’t care much about performance, since I’m not using the tablet for anything heavy like gaming or whatever, but in case you’re curious, here it is. I ran the Quadrant Benchmark on it and got a score of 1271 without tweaking anything on the device (e.g. it still had applications running in the background and all that).

IMG_2074.jpg

People around the web can get scores around 1500 and more (above the Nexus One running 2.2) by killing all running apps and setting the “Power Management” to “Overdrive” in the system settings. Now, benchmarks like these don’t mean much, I know. For example the OS version alone seems to play a big part in it (see the difference between the Nexus One running 2.1 and 2.2 on the previous screenshot?). But at least it gives a rough idea of where the Archos 70 falls in terms of performance.

Just for laughs I installed a few well known games like Angry Birds or X Construction Lite and they ran very smoothly. Some people reported, at first, that a few games were unplayable, but it looks like it got fixed when Archos released the 2.0.71 firmware. And if you’re looking for serious speed and power, the XDA guys have already released a few custom kernels for the Archos tablets, some of which feature CPU overclocking.

Conclusion

So far I’m quite happy with my new toy, and the only thing I’m worried about at this point is how long it’s going to last – Archos usually cuts costs with cheap builds that tends to break or malfunction after a couple years (but then again, my Apple devices haven’t lasted much longer in my experience, although it’s usually for different reasons). I originally expected to only use it as a portable video player, ignoring most of the other features like I did with the Archos 5 (it had a web browser and all that, but the horrible resistive touch screen and crappy performance made it unusable for anything else than media playback). But it turns out the Archos 70 is a very nice tablet, and I find myself browsing the web, checking my email and social networks, and all that stuff more and more. For example it’s a pretty nice way to catch up on saved Google Reader or Instapaper articles before going to sleep. It’s also a very decent eBook reader.

It’s the best gadget I’ve bought since my first iPhone, although it’s mostly awesome because it’s so cheap. But still, it’s awesome.


Poor man’s search engines sync for Google Chrome

Update: since Lifehacker featured this post on their home page, I released some simpler updated version of the scripts here.

The wonderful thing about open-source software is that whenever something’s missing, anybody can go ahead and fix it… that is, unless nobody cares enough about it. And that’s precisely what’s happening with the “search engines sync” feature in Google Chrome, which has been in Chromium’s bug database for more than a year and a half. Looks like Google is not so much in a hurry to let you use other search engines as soon as you install a fresh copy of their browser.

Oh, don’t look at me. I seriously don’t have the time to dive into such a big codebase to add the feature myself… but what I do have the time for is a little scripted hack to get around the problem quickly!

Google Chrome, like a lot of programs these days, is using SQLite to store its data. Your search engines are stored in a database called “Web Data”, in a table called “keywords”. It’s quite easy to use the SQLite command line shell to export and import that table across your computers… so that’s what I did. There are 2 scripts: one to export, and one to import, although I called them “push” and “pull” to sound cool like the Git/Mercurial guys.

Windows

On Windows, my scripts are written in Powershell, which ships by default with Windows 7. On previous versions of Windows, Powershell ships with some service packs, so if you’re up to date you should have it. Otherwise, go get it from Microsoft’s website.

Update: if you’ve never run any Powershell script before, you’ll have to change the security settings because I didn’t sign my scripts. Run Powershell as an administrator and type “Set-ExecutionPolicy RemoteSigned”.

You’ll need to also download the SQLite command line shell (no install required, it’s just one .exe file in a zip). Just keep it next to the script files.

Here’s the export script:

param (
    [String] $Destination = "keywords.sql"
)

$CurrentDir = Split-Path -Parent $MyInvocation.MyCommand.Path
if (![IO.Path]::IsPathRooted($Destination)) { $Destination = [IO.Path]::Combine($CurrentDir, $Destination) }
$Destination = $Destination.Replace('', '/')
$TempSqlScript = "$env:TEMPsync_chrome_sql_script"

Push-Location
Write-Output "Exporting Chrome keywords to $Destination..."
cd "$HOMEAppDataLocalGoogleChromeUser DataDefault"
Write-Output ".output `"$Destination`"" | Out-File $TempSqlScript -Encoding ASCII
Write-Output ".dump keywords" | Out-File $TempSqlScript -Encoding ASCII -Append
& "$CurrentDirsqlite3.exe" -init $TempSqlScript "Web Data" .exit
Remove-Item $TempSqlScript
Pop-Location

And here’s the import script:

param (
    [String] $Source = "keywords.sql"
)

if ((Get-Process -Name chrome -ErrorAction SilentlyContinue | Measure-Object).Count -gt 0)
{
    throw "Close Chrome and try again..."
}

$Reply = Read-Host -Prompt "This will overwrite your Google Chrome search engines! Are you sure?  "
if (!($Reply -match "^(Y|y|YES|Yes|yes)$"))
{
    Write-Output "Cancelling operation."
    exit
}

$CurrentDir = Split-Path -Parent $MyInvocation.MyCommand.Path
if (![IO.Path]::IsPathRooted($Source)) { $Source = [IO.Path]::Combine($CurrentDir, $Source) }
$Source = $Source.Replace('', '/')
$TempSqlScript = "$env:TEMPsync_chrome_sql_script"

Push-Location
Write-Output "Importing Chrome keywords from $Source..."
cd "$HOMEAppDataLocalGoogleChromeUser DataDefault"
Write-Output "DROP TABLE IF EXISTS keywords;" | Out-File $TempSqlScript -Encoding ASCII
Write-Output ".read `"$Source`"" | Out-File $TempSqlScript -Encoding ASCII -Append
& "$CurrentDirsqlite3.exe" -init $TempSqlScript "Web Data" .exit
Remove-Item $TempSqlScript
Pop-Location

MacOS X

On MacOS X I’m using bash so all you need is to make sure you’ve got the SQLite command line shell. You can either download it directly, or use one of the package managers like MacPorts or Homebrew (I’m using the latter myself).

Here’s the export script:

#!/bin/sh

DESTINATION=${1:-./keywords.sql}
TEMP_SQL_SCRIPT=/tmp/sync_chrome_sql_script
echo "Exporting Chrome keywords to $DESTINATION..."
cd ~/Library/Application Support/Google/Chrome/Default
echo .output $DESTINATION > $TEMP_SQL_SCRIPT
echo .dump keywords >> $TEMP_SQL_SCRIPT
sqlite3 -init $TEMP_SQL_SCRIPT Web Data .exit
rm $TEMP_SQL_SCRIPT

And here’s the import script:

#!/bin/sh
if ps -x | grep -v grep | grep Google Chrome > /dev/null; then
    echo "Close Chrome and try again..."
    exit 1
fi

read -p "This will overwrite your Google Chrome search engines! Are you sure?  " -n 1
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Cancelling operation."
    exit 1
fi

SOURCE=${1:-./keywords.sql}
TEMP_SQL_SCRIPT=/tmp/sync_chrome_sql_script
echo
echo "Importing Chrome keywords from $SOURCE..."
cd ~/Library/Application Support/Google/Chrome/Default
echo DROP TABLE IF EXISTS keywords; > $TEMP_SQL_SCRIPT
echo .read $SOURCE >> $TEMP_SQL_SCRIPT
sqlite3 -init $TEMP_SQL_SCRIPT Web Data .exit
rm $TEMP_SQL_SCRIPT

Bringing it all together with Dropbox

Those scripts handle importing and exporting our search engines, but we’re missing the “sync” part so far. That’s where popular syncing service Dropbox comes into play (but you can do this with any other syncing service). Just put all this stuff (the SQLite command line shell, the scripts, and the database dump) in a Dropbox folder. All you have to do is run the “export/push” script after you’ve changed your search engines, and run the “import/pull” script next time you get on another computer. Dropbox will have synced the dumped database file (“keywords.sql” by default) in the meantime (unless you’re moving really super fast, in which case you’ll have to wait a second or two).

I’ve made a handy ZIP file with those scripts, a couple of bootstrap (double-clickeable) files for Windows, and the SQLite win32 command line shell. So download it and start syncing in a very hacky and lame way!


Using Twitter as a feed reader

Robert Scoble famously posted about ditching Google Reader for Twitter a bit more than a year ago, and ever since I’ve been baffled at people moving to Twitter or Facebook to read their news. Business Insider even said a few months ago that Twitter has killed RSS readers. I’ve always been wondering how Twitter would be any better than RSS… that is, until I’ve actually tried it.

Now I’m just thinking those people are crazy out of their minds.

First, let’s put Scoble aside. This guy is clearly not your average user, he’s really out there. I mean, his performance concerns with Google Reader are completely legitimate, but he must be the only guy on earth that actually follows more than 20,000 people… and still uses the term “friends” to define them, by the way. And I’m not even talking about the feasibility of keeping up with that much information — after all, if he’s clever, he’s using tools to consume it all and make the trending subjects bubble up to the surface, or maybe he does it the other way around by searching for topics he’s interesting in right now instead of reading the timeline in a linear fashion. But average people (or, well, at least me) are following real friends (either their blogs, or the links/videos/pictures they share), a few websites pertaining to their interests that may not be related to web technology (knitting? cat breeding?), and other things that can’t be processed by some “trending” tool.

Anyway, I used Twitter instead of Google Reader for a couple months, and here’s what I found.

User Experience

The first thing that bugs me about Twitter, and all of its client applications, is that it effectively has a worse user experience than even the crappiest of RSS feed reader.

Remember why RSS feeds were supposed to improve our productivity, making it easier to get to the information that mattered? They were supposed to do this by bringing the information to us instead of forcing us to go to the information. RSS feeds would deliver the content, and only the content, right there in front of us, and they pretty much delivered on that promise. Sure, some websites only publish an abstract of the article, and you need to go to that website to read the rest, but that’s the worst case scenario. The best case scenario is that you read the whole thing in a distraction free environment.

Now what about Twitter? The best case scenario is the title of the article, with a link to the website. Yep. As far as I’m concerned, this is a step backwards in terms of productivity: I read some entry on Twitter, click a shortened URL, wait for some HTTP redirections, and then end up on the full website with animated ads and other distractions. Compare that to reading the article right now with only the content in front of me. And I’m not even talking about the worst case scenario, which is a shortened title of the article with a link, or some obscure smart-ass comment with a link.

Then there’s the problem of knowing what you’ve already read. All RSS clients track the items you’ve read. Most even synchronize that data over the cloud. Compare that to Twitter which doesn’t remember anything and for which I always have to look out for that point in the timeline where I’ve caught up with all the new stuff. And that’s for a naive, linear approach. Most of the time, I’m going to read stuff in a non-linear way, either by using lists or filtering by user (on Twitter) or filtering by tag or website (on Google Reader), so the stuff I’ve already read could end up mixed up with the stuff I haven’t read yet.

Q&A

There’s one thing I keep reading about Twitter being awesome, and that’s its ability to give you answers super quickly. Supposedly, you can tweet a question and get replies within a few minutes, if not seconds.

Well, that’s the biggest load of crap I’ve ever heard.

First, this obviously works only if you’ve got a sizeable following. A year and a half ago, the average Twitter user had 127 followers. It’s hard to tell how that number was calculated, but let’s assume it did take into account statistical distortions from the few internet celebrities having insane numbers of followers on one end, and all the inactive users (supposedly making up 3/4 of the whole user base) on the other end. Either way, that number is just big enough for a user to get a few answers a day. To really get to the point where you get better answers faster than your search engine of choice, I’m guessing you need to hit the 500 followers mark, maybe even 1000. I wouldn’t know, I’m far from being that popular… And even then, what? Twitter gets better for you as you get more people to follow you? Ok… what else sounds like this? Yeah: pyramid schemes. And that’s known to be non sustainable. There will be a lots of users down there with 20 followers trying to get some attention.

Second, what happened to all that concern about privacy? People start complaining the second Google introduces their Web Search History, but on the other hand it’s OK to search for information by posting stuff publicly on Twitter, which is then indexed a hundred times more?

Technical Stuff

Then there’s the problem of infrastructure. This part has a lot more to do with personal opinions about stuff you may not care about, but I always prefer a more open, distributed design over a more closed, constrained one. For example, if I can sign into a website using either my Facebook or Google account or OpenID, I’ll take OpenID because it’s obviously more open and doesn’t rely on the existence of a single company. Sure, unless you’re running your own OpenID identity server, you’re relying on some third party provider, but you can abstract that from OpenID consumers, and then switch the underlying implementation anytime you want. To pick a more mainstream example, between using a Hotmail or Gmail address and a custom domain address, I’ll choose the custom domain address for similar reasons.

Ok, so this is on the same level as people using open-source software vs .people who don’t care, or people eating organic food vs. people who also don’t care. And in this case, you probably don’t care, and that’s fine, but I personally dislike how Twitter and other similar services create this “siloization” effect on information and data on the web.

On Using Twitter

All of this makes Twitter a pretty horrible experience as a feed reader, at least for me. Does that mean it sucks? Well, kinda, but not necessarily. First, some of the new features indicate how the design team is trying to reduce my first productivity problem: if a link in a tweet points to a picture or a video from a known media service, said picture or video will be displayed in the sidebar right away. When you couple this with the new keyboard shortcuts and the lists, you have an experience that’s somewhat close to that of Google Reader, at least for some of the tweets. I can easily imagine that it’s going to get closer and closer in the near future… but right now it’s still lagging far behind IMHO.

For now I’m back to using Twitter the way it’s effective for me: following actual people’s status updates and link shares. Twitter is wonderful in the way it lets you get closer to what experts and other key people in society have to say on an everyday basis (you wouldn’t believe how “I had pasta today” inspires you when it’s been tweeted by a Nobel prize winner…). Arguably, that’s actually what Twitter is all about: information through people. RSS on the other hand is only for delivering content (although Google is trying to add social layers on top of it). The two may feel similar, but not quite.

Twitter is also a useful alternative search engine in some situations, but more and more search engines actually include tweets as search results (in Google, you need to click on the “Realtime” result type on the left).

But all things being equal, if I get the choice between following the same thing on Twitter or via RSS, I’ll choose RSS for sure.


Making WPF controls double-clickable

A common UI pattern features the ability to double-click on a control to go in “edit” mode. So for example, you have a TextBlock that shows you the name of an object, and you can double-click it to change it into a TextBox where you can edit that name. At this point, it’s easy to hook up the MouseDoubleClick event, or some other mouse event, but that’s not very MVVM-like, is it?

Thankfully, WPF gives us the awesome attached dependency property API, which we already used for adding auto-complete to TextBoxes. The goal is to be able to write something like this:

<TextBlock Text="{Binding Name}" mi:ExtendedCommands.DoubleClickCommand="{Binding EditNameCommand}" mi:ExtendedCommands.DoubleClickCommandParameter="42" />

When the TextBlock would get double-clicked, the “EditNameCommandcommand object on the DataContext would get called with “42” as the parameter. This is super easy to do:

public static class ExtendedCommands
{
    public static readonly DependencyProperty DoubleClickCommandProperty;
    public static readonly DependencyProperty DoubleClickCommandParameterProperty;

    static ExtendedCommands()
    {
        DoubleClickCommandProperty = DependencyProperty.RegisterAttached("DoubleClickCommand", typeof(ICommand), typeof(ExtendedCommands), new UIPropertyMetadata(null, OnDoubleClickCommandPropertyChanged));
        DoubleClickCommandParameterProperty = DependencyProperty.RegisterAttached("DoubleClickCommandParameter", typeof(object), typeof(ExtendedCommands), new UIPropertyMetadata(null));
    }

    public static ICommand GetDoubleClickCommand(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(DoubleClickCommandProperty);
    }

    public static void SetDoubleClickCommand(DependencyObject obj, ICommand value)
    {
        obj.SetValue(DoubleClickCommandProperty, value);
    }

    public static object GetDoubleClickCommandParameter(DependencyObject obj)
    {
        return (object)obj.GetValue(DoubleClickCommandParameterProperty);
    }

    public static void SetDoubleClickCommandParameter(DependencyObject obj, object value)
    {
        obj.SetValue(DoubleClickCommandParameterProperty, value);
    }

    private static void OnDoubleClickCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var element = d as UIElement;
        if (element != null)
        {
            if (e.OldValue == null && e.NewValue != null)
            {
                element.MouseDown += new MouseButtonEventHandler(Control_MouseDown);
            }
            else if (e.OldValue != null && e.NewValue == null)
            {
                element.MouseDown -= new MouseButtonEventHandler(Control_MouseDown);
            }
        }
    }

    private static void Control_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount == 2)
        {
            var element = sender as UIElement;
            if (element != null)
            {
                var command = GetDoubleClickCommand(element);
                var parameter = GetDoubleClickCommandParameter(element);
                if (command != null && command.CanExecute(parameter))
                {
                    e.Handled = true;
                    command.Execute(parameter);
                }
            }
        }
    }
}

This code is pretty naive, but it gets the job done and illustrates quickly how to do it: just register 2 attached dependency properties in a new custom class, and whenever someone uses it on a UI element, start listening to the “MouseDown” event on that element and check against the “ClickCount” property to figure out if it’s a double-click. If it is indeed a double-click, just run the command that was specified for this control!

Obviously, you’ll need to handle error cases a bit better (like for instance if these properties are used on a non-UIElement), make it a bit better (for example register the MouseDoubleClick event instead of the MouseDown event if the target object is a Control), and other stuff like that. But, well, by now, you should know not to copy/paste code found on blogs without using your brain anyway.


The harpsichord and the clavichord!

Google recently released Google Scribe, an experiment on auto-completing words for the user in advance, instead of spell and grammar checking what he’s already written. Go try it for yourself.

I myself had some fun letting Google write stuff for me. Just start with an arbitrary word, and keep accepting whatever the first suggestion is. The UI slows down quite a lot after a dozen or so auto-completions, so sometimes you need to delete the last space and type it again to get some performance back.

For example, here’s what I got after seeding their algorithm with “I’m”:

I’m not sure if it is not a valid stream resource in C minor for Piano and Orchestra No. 1 in the world of the living room and dining room with a view to share videos with friends and family to enjoy a good meal and a drink for the first time in the future and the future of the world and the world of the living room and dining room with a view to share videos with friends and family to enjoy a good meal and a drink for the first time in the future and the future of the world and the world of the living room and dining room.

Obviously, the algorithm quickly loops like crazy, and the sentence doesn’t quite make sense, but it’s pretty funny (at least to me!). Now, here’s what I got when starting with “You”:

You can turn Autoshare off at any time and without notice to you and your family to enjoy a good meal and a drink for the first time in the future and the future of the world and the world of the living room and dining room with a view to share videos with friends and family to enjoy a good meal and a drink for the first time in the future and the future of the world and the world of the living room and dining room.

That’s pretty interesting, as the looping is very similar to the first one!

Now, here’s what I got when starting with “let’s”:

Let’s see if we can not guarantee each coupon ‘s authentication and authorization data between security domains and do not want to be related to the search result can be bookmarked to maintain the filter for future use at Recent Searches for neutral Higgs bosons in e minor for clavichord and the harpsichord and the clavichord and the harpsichord and the clavichord and the harpsichord and the clavichord and the harpsichord.

This time, the loop is short and kinda catchy. The harpsichord and the clavichord! The harpsichord and the clavichord! The harpsichord and the clavichord! Maybe that’s gonna be the next internet meme?


Facebook’s privacy issues

Everybody knows, or at least says without really knowing, that Facebook has a few privacy issues, but there’s one thing I never quite realized until recently…

It all started with my friend Bertrand opening his new personal blog. He wanted a dedicated place to post personal stuff, which he previously posted alternatively on his professional blog or on Facebook. I’m pretty sure he also wanted an excuse to play around with his new cool baby, Orchard… Anyway. In order not to start completely from scratch, he imported those previous articles he wrote, along with their comments, to the new blog.

I immediately sent him an email to tell him he could work at Google Buzz, seeing how he just disclosed Facebook comments to the public… but as I was writing it, I wondered… were his notes already public in the first place? You see, Facebook gives you a lot of control over your content’s visibility. You can specify exactly who gets access to what, who gets to comment on what, etc. The problem is that, although the content creator knows what the privacy settings are, the content consumers don’t. Being on Facebook, I just assumed that those notes were, at best, visible to friends of friends… but of course, it wasn’t the case. Bertrand’s notes were public all this time, and as I commented on them, I unknowingly posted completely public stuff.

None of those comments being public bothers me, mind you. But it’s just annoying how, when you participate in your friends’ online social life, you don’t really know what kind of privacy level you’re dealing with. Obviously, Facebook should add some kind of indicator whenever you post a comment, telling you who will be able to see it. I’m surprised this hasn’t been such a big deal so far. Maybe people are too busy with “Company XYZ is evil!!”-type generalities to bother with actual details.


Xaml serialization quirks and gotchas

I recently had to build a little tool that would read its configuration from a XAML file (because XAML serialization is, most of the time, better and more customizable than standard XML serialization). The trick was that this tool had to be built on top of .NET 3.0 – not 3.5 or 4.0. And I discovered that there are a few little gotchas in .NET 3.0’s XAML serializer that I, somehow, never ran into before.

Gotcha #1: Automatic properties

Let’s say I wanted to deserialize the following class:

   1:      public class Foo
   2:      {
   3:          [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
   4:          public IList<int> Values { get; private set; }
   5:  
   6:          public Foo()
   7:          {
   8:              Values = new List<int>();
   9:          }
  10:      }

Pretty straightforward… however, if I try to deserialize it from a XAML file, I get the following exception:

property ‘Foo.Values’ cannot be set because it does not have an accessible set accessor. Line ‘3’ Position ‘4’.

I thought the framework was high on drugs or something, as what I wrote was pretty much the main use-case for the DesignerSerializationVisibility attribute. But then I remembered this other little gotcha with property reflection: when you use the auto-property syntax from C# 3.0 for a read-only property, the PropertyInfo you get on that property says you can write to it. This is because there is a setter method – only it’s private. And that’s why the XAML serializer complains about accessibility.

Note that this has been fixed with the new XAML serialization in .NET 4.0, with the new System.Xaml namespace. But if you need to target a previous version of the framework, you’ll need to refactor your code like so:

   1:      public class Foo
   2:      {
   3:          private List<int> mValues = new List<int>();
   4:  
   5:          [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
   6:          public IList<int> Values
   7:          {
   8:              get { return mValues; }
   9:          }
  10:          public Foo()
  11:          {
  12:          }
  13:      }

Gotcha #2: Public interface

Now you may think that the XAML serializer would be happy, but no… this is a very picky serializer! The next exception you get is:

‘Values’ is a read-only IEnumerable property, so ‘ConsoleApplication2.Foo’ must implement IAddChild. Line ‘3’ Position ‘4’.

That’s obviously weird, because the property is actually a list. But my guess is that the serializer is only looking for the ICollection interface. And the problem is that IList<T> does not implement that interface – it implements ICollection<T>, sure, but neither of those generic interfaces implement the “legacy” interfaces. That why most of the time you’re better off writing a proper collection class that inherits from Collection<T>, or some other base class from the System.Collections.ObjectModel namespace, because they implement both the generic and “legacy” interfaces… but sometimes you may not feel like writing an “IntCollection” class, right? Well. Life sucks.

Oh, and don’t bother actually implementing the IAddChild interface, or making the list itself implement that interface, it’s useless, you’ll get the same exception. That interface is some kind of weird leftover from the beta phase of WPF, and is now half-deprecated, half-internalized, as mentioned on that MSDN forums thread.

So basically, in our case, a quick fix would be changing the public interface to a List<int>.

After those few changes, the XAML serializer is happy and running fine!

Hope this helps.


Spam your friends with Yahoo! Pipes

You know how it goes: you’re an internet hipster with blogs and Twitter feeds and all that kind of new age stuff, but only other internet hipsters read them. Your friends (at least the ones that are not internet hipsters) only stick to Facebook. So how can you bring your stuff to them?

At first, it seems easy: Facebook can pull “stories” from various websites and services. Go to your profile, and under the status update box, click “Options” and then “Settings”. You get the following interface:

That’s cool for most people. You bring in your Flickr photos, and your blog articles, and maybe a few other things… but what happens if you’ve got more than one blog? The “Blog/RSS” site can be only chosen once. Also, notice how you can choose Google Reader as a story source. Sure, this works, but the way your shared items appear on your wall is not optimal: they appear as “secondary activities”, presented like your comments or likes, sometimes merging several ones together which adds extra clicks to get to the actual article (e.g: “Ludovic shared 2 new items on Google Reader”, with only a link to your shared items page, which means you don’t know which 2 items were shared, as they may not be the last 2 ones if there have been other, newer ones since then). At least it was like that a few months ago (maybe they fixed it in the meantime, you tell me). That’s not proper spamming, so let’s do it the power user way.

A few years ago, Yahoo launched Yahoo! Pipes, a website that lets you build feed mashups. I decided to use that to aggregate all my stuff and post it on my Facebook wall.

It’s pretty simple, at first. You just go to the Yahoo! Pipes website, log-in, click “Create a pipe”, and add one source node for each site you want to pull data from. You’ll most probably use “Fetch Feed” nodes with the direct URL to your blogs and shared items RSS or Atom feeds, but you can also use some “Fetch Site Feed” nodes, too (they will find the actual feed URLs like any feed reader would do). Now pick the “Union” node from the “Operators” category, and plug all your sources into it. Plug the “Union” output into the “Pipe Output” node that should have been created by default for you. Voilà, you have an aggregated feed that you can import on your wall on Facebook!

Or do you?

One problem you’ll notice right away is that all the items from the first feed are displayed, and then all the items from the second feed, and so on… The “Union” node only built a, well, union of those feeds. You need to re-order them by date so that all the items from all the sources are correctly mixed together. For this, add a “Sort” node, as shown above, that will sort items by “item.pubDate”.

There. Fixed? Nope… not yet.

Now you have a user experience problem. All those items appear as “notes” on your Facebook wall, with their contents directly readable. If you’re pulling feeds from your own websites and feeds from elsewhere at the same time (e.g. your Google Reader’s shared items feed), it becomes difficult for your friends to figure out which stuff was written by you, and which stuff is just cool shit you found on the web and wanted to share. You need to create a differentiator, like for example prepend “Shared on Google Reader:” in front of each of your shared items’ titles.

I’m still evaluating different options but at the moment I’m using something a bit less user-friendly, although definitely more friendly to the websites from which I share stuff from: I completely replace the contents of the item with a link to the actual article on its original website. This means that people can’t read the item right there (they need to click on the link), but it also means the people who posted the cool shit in the first place get a visitor that will potentially start clicking around on links and ads if he linked whatever I shared.

For this I created a different pipe, although I could have hard-coded it in the first one.

This pipe basically gets my Google Reader shared items feed and processes it in a loop: for each item, I replace the contents with a link whose text is just the title of the item. Inject this new pipe into the first one (you can reference a pipe inside another pipe) and, at last, you’re done!

The only problem I’ve had so far is that, after adding my aggregated feed to Facebook for the first time, the mobile version of the website did something funky. Instead of weaving all those notes into my friends’ timelines, moving the older items behind the more recent updates from other people and applications, it put everything it could at the top. So basically, I really spammed all my friends with dozens and dozens of items that were several weeks or even several months old. This bug didn’t affect the normal website, neither did it affect the iPhone application, as far as I could tell, so I only got a couple complaints. And hopefully they fixed the bug since then.

That’s pretty much it. You can find the 2 pipes I showed here and here.


About unit testing Maya and MStatus macros

Programmers in the video games and movies industry rarely write unit tests for all kinds of reasons and excuses, but every now and then, it happens. And it can get a bit complicated when you want to test a plug-in hosted by a 3rd party application like Autodesk’s Maya.

Setting up the unit test project

The good thing is, unlike most other 3d modeling packages, Maya comes with built in “batch” and “library” modes. The batch mode effectively runs Maya as a command line program, and the library mode allows you to host most of the Maya core engine inside your own application. This means that, as long as you’re not using anything that depends on the existence of the UI, it’s possible to run automated tests.

Once you’ve created your unit test project using your favourite test framework (lately I’ve been using boost::test), you want to initialize Maya in a minimalist environment (most probably in a test fixture setup phase). You can do that by pointing the MAYA_APP_DIR environment variable to a custom folder of your choice, which would contain a custom Maya.env file, along with maybe a few custom startup scripts. The goal is obviously to not load your complete production environment with all your 3rd party plug-ins. You also probably want to set the MAYA_PLUG_IN_PATH and MAYA_SCRIPT_PATH variables to point to the output or deployment folder(s) of your test project. This can even be done programmatically with the Maya API using MCommonSystemUtils::putEnv, as long as it happens before you initialize the engine.

When you’ve got the correct environment, you can call MLibrary::initialize and wait for an excruciatingly long time (somewhere between 20 and 30 seconds). Ideally, I’d like to start with a fresh Maya engine for each test suite (or even each test!), but given how long it takes for the library to initialize, that’s not an option and I do it only once on start-up. Between tests, I merely clear the scene with a “file -force –new” MEL command. I know you can severely strip down Maya by telling it to not even load the default plug-ins (e.g. if you’re testing something that doesn’t need rendering and animation and skinning and all that, you can in theory not load those features), but I haven’t bothered looking into that yet. If you have, I’d love to hear about it.

Anyway, all this, along with some nice helper functions for checking things in the scene, is pretty much enough to get you going… until you write your first “fail” test, i.e. a test that ensures that a given situation returns the expected error code or throws the expected exception.

The MStatus problem

You see, the problem is that, if you’re a bit paranoid like me, you’re probably using a lot of CHECK_MSTATUS macros around your code to make sure you’ll notice early if anything is not going as expected (the Maya API, being old as it is, doesn’t use exceptions, so if you don’t check return codes regularly you could go a long way with invalid objects, which increases the chances it’ll do some harm to your users’ data). When these macros are passed a non successful MStatus, they print a message to the standard error output. This pretty much means you’ll see that message printed in the terminal window as your unit test runs. That’s fine, but in the case of your fail test you don’t want that to happen because you only want to see unexpected error messages printed in the console.

Looking at the code for those CHECK_MSTATUS macros, one can see that they use the STL’s std::cerr to print the beginning of the message, and then call MStatus::perror to print a user-friendly description of the error. Being naive, I’m thinking “good, I just need to temporarily capture std::cerr!”. So I wrote the little following structure:

struct StreamCapture
{
public:
    StreamCapture(std::ostream& str, const std::ostream& replacement);
    ~StreamCapture();

private:
    std::ostream& mStr;
    const std::ostream& mReplacement;
    std::streambuf* mPrev;
};

StreamCapture::StreamCapture(std::ostream& str, const std::ostream& replacement) :
    mStr(str),
    mReplacement(replacement)
{
    mStr.flush();
    mPrev = mStr.rdbuf();
    mStr.rdbuf(mReplacement.rdbuf());
}

StreamCapture::~StreamCapture()
{
    mStr.flush();
    mStr.rdbuf(mPrev);
}
#define CAPTURE_COUT()
    std::stringstream replace_cout_##__LINE__ ;
    StreamCapture capture_cout_##__LINE__ (std::cout, replace_cout_##__LINE__)

#define CAPTURE_CERR()
    std::stringstream replace_cerr_##__LINE__ ;
    StreamCapture capture_cerr_##__LINE__ (std::cerr, replace_cerr_##__LINE__)

#define CAPTURE_ALL()
    CAPTURE_COUT();
    CAPTURE_CERR()

It’s using the classic RAII pattern, and you can use it as such:

BOOST_AUTO_TEST_CASE(TestThatSomethingFailingDoesntScrewUpTheRest)
{
    // Do something
    initializeStuff();
    {
        CAPTURE_ALL();
        callSomethingThatWillFail();
    }
    // Check that even with the failure, the data in the scene is still ok.
    checkStuff();
}

It won’t take you long to realize that only the first half of any error message printed by CHECK_MSTATUS has been captured. “No worries”, I thought. “They probably print the other half with a good ol’ fprintf, so I’ll capture stderr too!”.

Well… I tried a few different things, from a few different sources, but none of them worked. At best, it didn’t do anything – the error code description would still get printed. At worst, it would seemingly work but would then render the whole program unstable (most of the time it would crash on the second attempt to capture the error output). I don’t know what Maya is using to print that MStatus out, but it sure isn’t a straightforward fprintf.

Now, before moving on to my (somewhat disappointing) solution, let’s look at another MStatus problem.

The CHECK_MSTATUS_AND_RETURN_IT problem

One of the CHECK_MSTATUS macros is CHECK_MSTATUS_AND_RETURN_IT, which checks for any error and, if found, will make the current function return that very same error. A typical use of it would be:

MStatus doSomethingSimple();

MStatus doSomethingMoreComplicated() { // Do stuff CHECK_MSTATUS_AND_RETURN_IT(doSomethingSimple()); // Do more stuff return MStatus::kSuccess; }

The problem is that this macro is implemented as such:

#define CHECK_MSTATUS_AND_RETURN_IT(_status)
    CHECK_MSTATUS_AND_RETURN((_status), (_status))

Do you see? It calls CHECK_MSTATUS_AND_RETURN which, if the first status is an error, returns the second status. This means that if the call to doSomethingSimple fails, the macro will call it a second time to get the return value!

This is obviously bad… (and another example of why C++ macros can be evil because it’s not obvious what you can and cannot pass into them).

At first I defined a CHECK_MSTATUS_AND_RETURN_IT_SAFE macro that did the correct thing, but for various reasons I decided to just redefine the original macro and prevent other programmers from making that mistake (at least as long as the my header file was included, which is easier to enforce, especially if you’re using things like pre-compiled headers):

#pragma warning(push)
#pragma warning(disable:4005)
#define CHECK_MSTATUS_AND_RETURN_IT(status)
    CHECK_MSTATUS_AND_RETURN_IT_SAFE(status);
#pragma warning(pop)

Back to the MStatus problem

Now what does this have to do with the error message problem?

Well, now that I was already redefining one of the CHECK_MSTATUS macros, I figured it wasn’t much more trouble to redefine them all (there’s only a handful of them). The only thing you need to do is replace the call to MStatus::perror with a line that sends the MStatus into std::cerr. I mean, MStatus already has operator<< defined for IO streams, so it’s not like I had to do anything more than some copy/paste and changing one line.

So there you have it: my crappy solution for having clean Maya unit tests was to redefine the CHECK_MSTATUS macros so they use the STL’s IO streams. Do you have a better or more elegant solution? I’d love to know about it!