delicious orkut facebook technorati twitter

Thursday, May 28, 2009

How to add a Print This Post option in Blogger

When I read a good and informative article on someone's blog or any other website, if I really like it I usually look for options to keep the article for my reference in the future. Only bookmarking the link is not sufficient enough because in case the website itself goes down, the link is of no use anymore.

Some blogs/websites offer options to save the article as a PDF document. Another very common option is to allow the reader to print the article. But, because Blogger does not provide this option by default, we will have to find a way ourselves.

At first, I thought how hard its going to be. Hook an event here, write a script there, and voila! But, wait a minute (I thought to myself)...don't I have this sidebar showing quite a few widgets, the social networking icons in the header area, the post icons, the quickedit (pencil) icon under the post, comment form, and what not. I dint want any of this to show up on the page when someone prints my articles.

With a little bit of scripting and CSS knowledge, you can add this very useful option in your own blog that your users will really appreciate. Here's how:

  • Open your blog's Layout Settings and click Edit HTML.
  • Check Expand Widget Templates.
  • In your template code, under <head> element, add the following lines:
    <script type='text/javascript'>
    //<![CDATA[
    /* generate and use any GUID here */
    var NAME_PRINT_WINDOW = 'C7DE3BE342E8486AA77A19F1E0EFCEC1';
    function init() {
    if (window.name == NAME_PRINT_WINDOW)
    window.print();
    }
    function printPost(href) {
    window.open(href, NAME_PRINT_WINDOW,
    'scrollbars=yes,resizable=yes,width=640,height=480,menubar,toolbar,location');
    }
    //]]>
    </script>
  • Now, find <body> tag and change it to <body onload='init();'>.
  • Then, find <p><data:post.body/></p> and just above it, add the following:
    <span class='item-action'>
    <a expr:href='data:post.url' onclick='javascript:printPost(this.href);return false;'>
    <img class='icon-action' src='http://mail.google.com/mail/images/print_icon.gif'/>
    Print this post
    </a>
    </span>

That's all to it; in case you find any problem while making it work, leave me a comment. And, on how to make your blog more printer friendly, I'll discuss it in the next post.

Thursday, May 14, 2009

A non-generic IList implementation without any Boxing and Unboxing

While writing this post, I'm assuming that you know what boxing/unboxing means in the .NET context and in case you don't, read this MSDN article to know more about it.

In the default implementation of IList interface such as ArrayList, when you Add() a ValueType item to the list, the value is boxed inside a Object. Similarly, when a ValueType element is retrieved from the list, unboxing occurs and explicit casting must be performed.

Now, according to MSDN documentation, it can take upto 20 times longer than a simple reference assignment and the casting takes upto four times long as an assignment.

Provided below is my own implementation that gets rid of boxing/unboxing problem while still using the same old fashioned non-generic IList:

class VariantList : VariantListBase
{
#region Methods
public int Append<T>(T value) where T : struct {
return (this as IList).Add(new T[1] { value });
}

public T GetAt<T>(int index) where T : struct {
T[] value = (T[])(this[index]);
return value[0];
}
#endregion
}

VariantListBase is an abstract class implementing IList with a structure similar to the following:

abstract class VariantListBase : System.Collections.IList
{
private System.Collections.IList innerList = new System.Collections.ArrayList();

/*********
* IList, ICollection and IEnumerable method implementations
* simply forward the call to this.innerList
*********/
}

So, how does VariantList avoids boxing/unboxing? It achieves this by using arrays.

Remember that any type of array derives from System.Array base class, which itself, is a reference type. And this is exactly what is being done in the Append<T>(T value) method. When Append<T>() is used to add a ValueType element to the list, a new array of one single element of the same type T is created and added, thus completely avoiding the need for boxing. In the same way, when a value is retrieved using GetAt<T>(int index) method, it is retrieved without performing any unboxing process.

But here is the deal: After performing the 10 million iteration benchmarking and using Hi-Res Timer, I haven't found much difference (only a few milliseconds) between the time taken to insert as well as retrieve items using ArrayList and VariantList. And I'll be honest with you, while repeating benchmarks, a few times VariantList actually performed a little slower than the ArrayList.

Anyways, if you want to use this code, perform some benchmarks and see if its of any advantage to you. Also, if you have any comments, suggestions, criticisms, etc. on this article, please feel free to share.

Source code: VariantListBase.cs, VariantList.cs

Wednesday, May 6, 2009

Download Windows 7 Release Candidate with your favorite download manager

No, this is not another post telling you about Microsoft's making Windows 7 RC publicly available for download as I'm sure that almost everyone is aware of that. The reason behind this post, in fact, is to provide information on how to use your own favorite download manager instead of the one Microsoft wants you to use for downloading the ISO file.

Windows 7 RC Download Manager

Now, considering the fact that the maximum connection limit is set to only 4, the default download manager gives very slow download speeds (it gave me a speed of only 200 Kbps average when I usually get a speed of 768 Kbps). Even when I tried to paste the direct download links (32-bit and 64-bit) in Flashget, its still didn't work; I guess because the download server look for a cookie which, when not available, makes the server simply refuse the download request.

After trying quite a few methods, I have finally found a hack to it which is as follows:

  1. Open Mozilla Firefox
  2. Go to Tools | Options and click Content
  3. Uncheck Enable Java
  4. Click OK

Now visit the website Windows 7 Release Candidate Customer Preview Program, choose the platform (32-bit / 64-bit) and click Go. Sign in, edit the profile information if required and on the next page, note down the product key, and click Download Now. Now when Firefox asks where to save the ISO file, choose Flashgot instead.

That's all to it, you should get good download speeds now. Enjoy Windows 7 :)