Tuesday, August 17, 2010

Adventures with QEmu

Setting up QEmu with a Windows XP host and a Ubuntu Server guest is the topic for another blog post.

This is the story of what happened when I tried to move it to another directory.

1. Decide that having QEmuManager sitting in your root My Documents folder is ugly and move it to a folder called Applications so it can snuggle up with PuttyPortable and ChromePortable.
2. Move the folder.
3. Start up QemuManager. Receive error stating it is unable to write to the original location.
4. Try to close QemuManager. Receive write error again. Eventually kill process using Task Manager.
5. Sigh; move folder back.
6. Start up QemuManager. Receive error stating it is unable to write to the new location (where it is no longer located).
7. Make "..." face. Repeat steps 4 and 5.
8. Spend a long time poking through the folder and menu options trying to find any path location.
9. Make a backup of your images folder.
10. Delete the application folder.
11. Unzip the original zip file to your preferred location.
12. Add a 'new' VM, only instead of creating a new disk image use your existing one.
13. It works. Cheer.
...
14. Discover networking no longer works on the Linux server.
15. Reboot a few times and fiddle with network settings. Discover that
16. Google "error while getting interface flags: no such device".
17. Find a post on Ubuntu forum describing similar issue. Descend into config file hell.
18. Follow instructions.
19. Everything works! *knocks on wood*

In summary:

To fix the "unable to write to [location]" error when you've moved Qemu Manager to a new location:
1. Make a copy of your VM image. In my case, it was the .qcow2 file in /Images. Make a note of what VM options you've chosen.
2. Delete the Qemu folder entirely.
3. Unzip qman70.zip to your new location.
4. Copy the image to the new /Images folder
5. Add a new VM in QemuManager, choosing the image file you saved.

To fix the networking problem (caused by the MAC address changing), as discovered by modifiedmind.
1. Locate /etc/udev/rules.d/70-persistent-net.rules. Make a backup, write down the module name (in brackets on the #PCI Device line)
2. Delete the #PCI Device line
3. sudo /etc/init.d/udev restart
4. sudo modprobe -r module
sudo modprobe module

Everything seems to work now.

Monday, April 26, 2010

Field Prefix in custom Model Binder

I'm doing some model validation and adding errors to the ModelState at OnModelUpdated in a custom model binder overriding DefaultModelBinder.

However, since the model can be updated either as one of many children in another Model, or on its own, I needed to get the field "prefix" out so that it would highlight the invalid field correctly in the view.

After a lot of unsuccessful searching, it hit upon me to look up the source code for TryUpdateModel to see what it does with the prefix parameter.

Turns out that the ModelName property on ModelBindingContext is the field prefix.

So, I was able to add my model error successfully for both usage cases like this:
string fieldPrefix = (!String.IsNullOrEmpty(bindingContext.ModelName)) ? bindingContext.ModelName + "." : String.Empty; controllerContext.Controller.ViewData.ModelState.AddModelError(fieldPrefix + "PropertyName", "Error Description");

Works for me.

PS: I wanted to use the new DataAnnotation validators for this validation, but since it involves comparing two (or more) fields on the model I was unable to get it working with the correct ModelState error keys. If anyone knows a better way, please let me (and this person on StackOverflow with a similar problem) know!

Friday, April 23, 2010

Making Visual Studio copy .config files

I'd created a few XML configuration files and given them the extension .config, since it was a descriptive name with the added bonus of IIS refusing to serve them.

However, using the Publish function in Visual Studio 2010 (right click -> "Publish...") to copy to the local file system, my custom .config files were never in my output directory. However, the .config files I'd created using the web.config file template were there.

I'd tried setting their "copy to output" setting to "always copy" and "copy if newer", all to no avail.

Eventually, it occurred to me to compare the properties of the ones that did work to the ones that didn't.

Screenshot of properties dialogue, 'Build Action' highlightedSo, the solution:

Bring up the Properties window for the file (right click -> Properties or alt+enter) and set the Build Action to "Content".

Now my files are copied successfully.

Wednesday, April 14, 2010

A pet peeve: Switch vs. If/Else If

One of my pet peeves is seeing an if/else tree where there could be a switch statement. I get even more annoyed when I see it in official sample code from Microsoft.

From the MSDN description of System.Web.UI.WebControls.MailDefinition
if (sourcePriority.SelectedValue == "Normal")
{
md.Priority = MailPriority.Normal;
}
else if (sourcePriority.SelectedValue == "High")
{
md.Priority = MailPriority.High;
}
else if (sourcePriority.SelectedValue == "Low")
{
md.Priority = MailPriority.Low;
}


I admit that my reasoning is largely aesthetic - switch statements just look neater, and I find them easier to read. Switch is generally held to be the more efficient of the two, but I will accept that for a 3-option split the performance gain is likely to be negligible.

Also, in this particular case, you could even use Enum.Parse().

Tuesday, April 13, 2010

It's been a while!

Some Visual Studio regular expressions I've found useful recently:

To replace your lazy Response.Write statements with something a little more correct:
Find: Response\.Write\({.#}\);
Replace: Results.Text += \1;
(Or stringBuilder.Append(\1); or whatever is appropriate.)

To find textboxes (Html.TextBox from the ASP.NET MVC helpers, and standard input elements) that don't have a maxlength set. Works on single lines only.
(Html\.TextBox|input type="text")~(.#maxlength)
(Replace maxlength with any other text for other properties.)

I found this Knol by Brian Lalonde very useful in creating these regular expressions (and I love the snarky ending paragraph).

Sunday, June 17, 2007

How to Create a Theme in Windows SharePoint Services v2

I was going to make a post about making SharePoint 2003 themes, but while I was searching for resources I came across this guide by Heather Solomon, which is far more comprehensive than anything I would have written.

Thanks, Heather!

Butterscotch ice cream

I had butter and brown sugar lying around the house, so I decided to search for a Butterscotch Icecream recipe. The only problem was that the recipe was all in Imperial (and measured sugar in grams, which is a pain for me as I don't have an electric scale).

Consequently, I present you with the same recipe, but with different measurements.

Butterscotch ice cream

60g Butter
6 tbsp Brown sugar; dark, soft
1 cup full cream milk; warm
2 Eggs
5 tbsp Caster sugar
1/4 tsp Vanilla essence
Whipping cream (10oz./283.5 g, which I estimate at about 300mL)

1. Melt the brown sugar and butter together in a pan over a gentle heat. Increase the heat until the mixture bubbles for 1 minute only.
2. Allow to cool slightly.
3. Add the warm milk.
4. Stir continuously over a gentle heat until thoroughly blended.
5. Allow to cool.
6. Beat together the eggs and the caster sugar in a bowl.
7. Pour the mixture from the saucepan on to the beaten eggs/sugar, add the vanilla essence and stir.
8. Strain back into the pan.
9. Stir over a low heat until the mixture thickens slightly; take care not to let it boil.
10. Cool the mixture.
11. Whip the cream lightly and fold into the cooled mixture.
12. Pour into a freezer container and freeze until mushy. Beat with a whisk and return to the freezer until the ice cream is firm. Put in icecream maker.
(Website notes recipe is from Scottish Tea-Time Recipes by Johanna Mathie, ISBN 1 898435 18 9.)

This is good with shortbread bits added, or macadamias, or with chocolate fudge sauce.

I'm not sure about the cream quantity, so it might come out a little intense, depending on your tastes.