<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ColonelPanic &#187; General Development</title>
	<atom:link href="http://colonelpanic.net/category/general-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://colonelpanic.net</link>
	<description>Dumping our corps so you don&#039;t have to</description>
	<lastBuildDate>Mon, 30 Jan 2012 17:50:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>iOS and iCloud: overcoming &#8220;bad file descriptor&#8221; errors</title>
		<link>http://colonelpanic.net/2012/01/ios-and-icloud-overcoming-bad-file-descriptor-errors/</link>
		<comments>http://colonelpanic.net/2012/01/ios-and-icloud-overcoming-bad-file-descriptor-errors/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 06:49:48 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[icloud]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=638</guid>
		<description><![CDATA[Pretty quick one today. I beat my head against this for several hours before I found the obvious solution, so I thought I&#8217;d jot the info down here for reference, and maybe to help someone else out. NOTE: There is now a relevant answer on StackOverflow. Unfortunately, this answer wasn&#8217;t there when I needed it. [...]]]></description>
			<content:encoded><![CDATA[<p>Pretty quick one today. I beat my head against this for several hours before I found the obvious solution, so I thought I&#8217;d jot the info down here for reference, and maybe to help someone else out.</p>
<p><strong>NOTE:</strong> There is now <a href="http://stackoverflow.com/questions/8714525/cannot-sync-simple-text-file-with-icloud-bad-file-descriptor">a relevant answer</a> on StackOverflow. Unfortunately, this answer wasn&#8217;t there when I needed it.</p>
<h3>The problem</h3>
<p>Recently, while adding <a href="http://www.apple.com/icloud/">iCloud</a> support to the <a title="Day One - your premier journaling solution." href="http://dayoneapp.com/">Day One</a> iOS app, I ran into an issue copying files to and from the application&#8217;s Documents directory and its corresponding iCloud directory. Day One can run in one of three modes:</p>
<ol>
<li>Sync entries via iCloud</li>
<li>Sync entries via <a href="https://www.dropbox.com/">Dropbox</a></li>
<li>Don&#8217;t sync entries</li>
</ol>
<p>If you&#8217;ve developed for iCloud at all, you know that iCloud documents live within specific, &#8220;ubiquitous&#8221; directories on the user&#8217;s device (discovered via NSFileManager&#8217;s URLForUbiquityContainerIdentifier: method). So in order to enable iCloud sync, we needed to copy all files into the appropriate ubiquitous container. Conversely, if the user elects to disable iCloud then we need to copy all files from the ubiquitous container into the application&#8217;s Documents directory.</p>
<h3>The assumption</h3>
<p>We wrote a simple method to copy files from one directory to another, either going into or coming out of iCloud. It uses a basic NSDirectoryEnumerator to find all the files in the current source directory, check if they are present in the destination directory, and copy them there if they aren&#8217;t. Here&#8217;s a simplified example:</p>
<div class="wp_syntax">
<div class="code">
<pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>sourceDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self currentWorkingDir<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// current directory, ie ubiquitous iCloud directory</span>
<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>destDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self destDirForMovingFromDir<span style="color: #002200;">:</span>sourceDir<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// where should we copy to?</span>
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fm <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSDirectoryEnumerator</span> <span style="color: #002200;">*</span>dirEnum <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fm enumeratorAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>sourceDir path<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>mergeError;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>sourceFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dirEnum nextObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>sourceFileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>sourceDir URLByAppendingPathComponent<span style="color: #002200;">:</span>sourceFile<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>destFileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>destDir URLByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>sourceFile lastPathComponent<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fm fileExistsAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>destFileURL path<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fm copyItemAtURL<span style="color: #002200;">:</span>sourceFileURL toURL<span style="color: #002200;">:</span>destFileURL error<span style="color: #002200;">:&amp;</span>mergeError<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR (copy error): %@ -&gt; %@ - %@&quot;</span>, sourceFileURL, destFileURL, mergeError<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre>
</div>
</div>
<p>I frequently found &#8220;bad file descriptor&#8221; errors being logged out. We were assuming that the NSDirectoryEnumerator would only list files that were actually present on the device. As it turns out, that is not the case when enumerating a ubiquitous directory.</p>
<h3>The solution</h3>
<p>Once you realize that the NSDirectoryEnumerator will list all ubiquitous files that exist on any device, regardless of whether or not they have been downloaded to the current device, the solution becomes pretty obvious:</p>
<p><em>You can&#8217;t copy a file that hasn&#8217;t been downloaded.</em></p>
<p>Adding that check is fairly straight forward. Since our method doesn&#8217;t know if we are going into or out of iCloud, we implemented a pretty general solution. Modifying the above example, it looks something like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="objc" style="font-family:monospace;">...
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>sourceFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dirEnum nextObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    ...
    <span style="color: #11740a; font-style: italic;">// By default, assume all files are able to be copied.</span>
    <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>ableToBeCopied <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// However, ubiquitous items that are not downloaded CANNOT be copied</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fm isUbiquitousItemAtURL<span style="color: #002200;">:</span>sourceFileURL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>sourceFileURL getResourceValue<span style="color: #002200;">:&amp;</span>ableToBeCopied forKey<span style="color: #002200;">:</span>NSURLUbiquitousItemIsDownloadedKey error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Now copy the file if it doesn't exist and is able to be copied</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fm fileExistsAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>destFileURL path<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#91;</span>ableToBeCopied boolValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fm copyItemAtURL<span style="color: #002200;">:</span>sourceFileURL toURL<span style="color: #002200;">:</span>destFileURL error<span style="color: #002200;">:&amp;</span>mergeError<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR (copy error): %@ -&gt; %@ - %@&quot;</span>, sourceFileURL, destFileURL, mergeError<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre>
</div>
</div>
<h3>Conclusion</h3>
<p>See what I mean? Pretty obvious, and I can&#8217;t believe I lost so much time tracking this down. If the source file is ubiquitous, it might not be downloaded, and thus you might not be able to copy it. So check it first.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2012/01/ios-and-icloud-overcoming-bad-file-descriptor-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Deferred Objects &#8211; Part 2</title>
		<link>http://colonelpanic.net/2011/12/jquery-deferred-objects-part-2/</link>
		<comments>http://colonelpanic.net/2011/12/jquery-deferred-objects-part-2/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 16:47:58 +0000</pubDate>
		<dc:creator>Robin</dc:creator>
				<category><![CDATA[General Development]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=603</guid>
		<description><![CDATA[Deferred Objects &#8211; Part II As you recall from the first part jQuery Deferred Objects &#8211; Part I, deferred objects give you a way to organize asynchronous blocks of code in to a self-managed callback queue, while allowing you to attach 1..* callbacks. Also, you&#8217;ll recall that the basics of using jQuery deferred objects include [...]]]></description>
			<content:encoded><![CDATA[<h1> Deferred Objects &#8211; Part II</h1>
<p>As you recall from the first part <a href="http://colonelpanic.net/2011/11/jquery-deferred-objects/" target="_blank">jQuery Deferred Objects &#8211; Part I</a>, deferred objects give you a way to organize asynchronous blocks of code in to <a href="http://www.erichynds.com/jquery/using-deferreds-in-jquery/" target="_blank">a self-managed callback queue</a>, while allowing you to attach 1..* callbacks. Also, you&#8217;ll recall that the basics of using jQuery deferred objects include essentially the following &#8220;simplified&#8221; steps:<br />
1. Created a deferred object<br />
2. Attached .done and .fail callbacks<br />
3. Called resolve or reject, thereby triggering the appropriate respective callbacks</p>
<h2> Promises</h2>
<p>This was fine as a first step in understanding what&#8217;s going on, but in all likelyhood you&#8217;ll use a slightly different approach. What&#8217;s wrong with returning a deferred object itself? It gives the caller access to resolve or reject directly, and that means other callers could be affected as a result. What we really want to do is return a &#8220;promise&#8221;. From the <a href="http://api.jquery.com/Types/#Promise">jQuery Promise Documentation</a> we learn:<br />
&#8220;This object provides a subset of the methods of the Deferred object (then, done, fail, always, pipe. isResolved, and isRejected) to prevent users from changing the state of the Deferred.&#8221;</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span>
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/push_to_twitter&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/push_to_facebook&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/push_to_linkedin&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/generate_report&quot;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">then</span><span style="color: #009900;">&#40;</span> it_succeeded<span style="color: #339933;">,</span> it_failed<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> it_succeeded<span style="color: #009900;">&#40;</span>report_data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Now do something with the report data e.g. show</span>
    <span style="color: #006600; font-style: italic;">// an awesome summary with pretty bars and graphs!</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> it_failed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Treat user's depression, OCD and other ailments</span>
    <span style="color: #006600; font-style: italic;">// Prescribe prozac, etc., etc...</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<h2>Possible Uses</h2>
<p>Possible use cases are endless but here are some examples:<br />
1. You need to make multiple asynchronous Ajax requests &#8211; with Deferred objects you can handle the results together without concern for which order they finish in.<br />
2. You want to be able to make the call in one region of code, then pass the result as an argument to a different part, but the call may be asynchronous.<br />
3. Easily return data from a modal dialog<br />
4. Caching &#8211; an API properly supporting Deferred objects can implement caching <em>without any changes to the consumer of the API</em>.</p>
<h2>An Example: Caching</h2>
<p>For caching, jQuery&#8217;s when .. then provides us the tools we need to return both fresh and cached data from one interface. Take the following code:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cacheA <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> fetch_some_objectA<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cacheA<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> cacheA<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> cacheA<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/fooEndpoint'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// ajax options here</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        cacheA<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> res<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">var</span> cacheB <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> fetch_some_objectB<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cacheB<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> cacheB<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> cacheB<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/barEndpoint'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// ajax options here</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        cacheB<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> res<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span>fetch_some_objectA<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> fetch_some_objectB<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'bar'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dataA<span style="color: #339933;">,</span> dataB<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Do whatever you want with said data</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fail</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>errA<span style="color: #339933;">,</span> errB<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Deal with error handling</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<h3> The basics of caching with Deferred objects</h3>
<p>There are a couple of cases to consider here; the first case is that you are requesting object(&#8216;foo&#8217;) for the first time. In this case, cacheA["foo"] will not exist and so an ajax call is created. $.ajax returns a deferred object, which is saved in the cache. The reason this is important is that thereafter, future requests will get that same deferred object, thus preventing two requests from being made to the same endpoint. Once the ajax call completes the done handler we set updates the cache with the actual object. Any calls that ran fetch<em>some</em>objectA(&#8216;foo&#8217;) while the request was &#8220;in progress&#8221; will be notified with the data once the deferred is resolved. Calls made after the request has resolved will get the actual data straight away. So, how do you tell if you have a deferred or the real data? The answer is simple: you don&#8217;t need to.</p>
<p>This is where <a href="http://api.jquery.com/jQuery.when/">jQuery.when</a> neatly ties everything together. jQuery.when ($.when) accepts any number of arguments and returns a jquery.Deferred object. That object will be resolved once all of the arguments are resolved; however, if those objects are not jQuery.Deferred objects then they will be considered resolved immediately. From the documentation, jQuery.when takes either a Deferred or object: &#8220;If a single argument is passed to jQuery.when and it is not a Deferred, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately.&#8221;</p>
<p>After passing through a jQuery.when, a Deferred object that resolves to a string &#8220;A&#8221; and the string &#8220;A&#8221; become functionally equivalent &#8212; no matter which you passed in, you&#8217;ll still get the same thing:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fdsafdsa&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>This will immediately alert &#8220;fdsafdsa&#8221;. Now essentially the same using the deferred resolve idiom:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> dfd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> $.<span style="color: #660066;">Deferred</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span>dfd<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dfd.<span style="color: #660066;">resolve</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fdsafdsa&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>This will also alert the same thing; it doesn&#8217;t matter if it&#8217;s a real object or a deferred, once you make it into the when it&#8217;ll be a real object. This makes caching really slick because you don&#8217;t need to care if you have the object yet or not, you just let jQuery.when figure that out and tell it what should happen once it does.</p>
<h3>One small problem&#8230;</h3>
<p>If you actually copy in this code and play with it long enough, you&#8217;ll notice something that doesn&#8217;t quite work how you expect. When the jQuery.ajax call returns you&#8217;ll actually get a slightly different object in the jQuery.when callback; this actually makes sense if you look at it closer. From the jQuery codebase, when the ajax call completes it is either resolved or rejected with the following lines:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Success/Error</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> isSuccess <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    deferred.<span style="color: #660066;">resolveWith</span><span style="color: #009900;">&#40;</span> callbackContext<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span> success<span style="color: #339933;">,</span> statusText<span style="color: #339933;">,</span> jqXHR <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    deferred.<span style="color: #660066;">rejectWith</span><span style="color: #009900;">&#40;</span> callbackContext<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span> jqXHR<span style="color: #339933;">,</span> statusText<span style="color: #339933;">,</span> error <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Notice that there are actually three arguments in either case; this may surprise you, since most examples you&#8217;ll see of the callbacks accept just one, like the above code. Most don&#8217;t need the other two, but jQuery.when isn&#8217;t going to discard those extra values and yet it still needs to keep one argument per input in the callback function. Therefore, in the $.when block above you&#8217;ll actually have both dataA and dataB resolve to an array with three values (success, statusText, jqXHR); however, we normally would only cache one of those values (success) so when reading from the cache we don&#8217;t get that array. The solution is to make our functions a little bit more complicated by using the jquery pipe method to restructure the result a little bit:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cacheA <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> fetch_some_objectA<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cacheA<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> cacheA<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> cacheA<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/fooEndpoint'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// ajax options here</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pipe</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> cacheA<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> res<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">var</span> cacheB <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> fetch_some_objectB<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cacheB<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> cacheB<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> cacheB<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/barEndpoint'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// ajax options here</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pipe</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> cacheB<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> res<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span>fetch_some_objectA<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> fetch_some_objectB<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'bar'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dataA<span style="color: #339933;">,</span> dataB<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Do whatever you want with said data</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fail</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>errA<span style="color: #339933;">,</span> errB<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Deal with error handling</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>You may notice the assignment of the result to the cache happens twice. The reason is that you first want to assign the deferred object to the cache when you start the request &#8212; otherwise if you call the function again before the ajax request finishes it&#8217;ll start a new request rather than reusing the old one. </p>
<p>With this new object (the result of the .pipe) the resolved value will be as we expect it. This relatively simple pattern can get a little more complex if you&#8217;re doing a lot with pipe, but for the consumer the interface is really simple.</p>
<p>Admittedly, there is some complexity to using this pattern but the &#8220;win&#8221; is that your interface now has a &#8220;Single Point of Contact&#8221; (<a href="http://en.wikipedia.org/wiki/Point_of_contact">SPOC</a>)<br />
Let&#8217;s look at a convenient analogy&#8230;</p>
<h2>An Example: Placing a Special Order</h2>
<p>Let&#8217;s imagine that, as an active supporter of your local community businesses, you make it a point to go to your favorite little &#8220;mom and pop&#8221; bookstore to place orders for books. The lady at the counter asks for your phone number or email, and says she&#8217;ll contact you as soon as your books come in. Now obviously, there&#8217;s the small chance that, after the fact, the wholesaler, manufacturer, etc., will have ran out of the ordered book(s). Should this happen, you&#8217;d probably feel it was acceptable for the bookstore to contact you and give you the option to wait or cancel your order. However, you would not want to here directly from the wholesaler or manufacturer directly. This would probably confuse you and you may even complain to the bookstore that they should manage their purchases better should this happen! The expectation you have as a customer in this example is that you only have to deal with a single point of contact. In code, we can have a similar convenience using deferred objects:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Contrived ajax and random failure stubs so we can</span>
<span style="color: #006600; font-style: italic;">// pretend to have &quot;mostly successful&quot; requests</span>
<span style="color: #003366; font-weight: bold;">function</span> randomTrueFalse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>r<span style="color: #339933;">%</span>9 <span style="color: #339933;">!==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> ajaxStub<span style="color: #009900;">&#40;</span>endpoint<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> dfdResult <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> $.<span style="color: #660066;">Deferred</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> bool <span style="color: #339933;">=</span> randomTrueFalse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            dfdResult.<span style="color: #660066;">resolve</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'success: '</span><span style="color: #339933;">+</span>endpoint<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            dfdResult.<span style="color: #660066;">reject</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fail: '</span><span style="color: #339933;">+</span>endpoint<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> dfdResult.<span style="color: #660066;">promise</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> manufacturer <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        orderBook<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>bookName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span>ajaxStub<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/book/'</span><span style="color: #339933;">+</span>bookName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pipe</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>respObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Manufacturer-DONE: &quot;</span><span style="color: #339933;">,</span> respObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> respObj<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>errObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Manufacturer-FAIL: &quot;</span><span style="color: #339933;">,</span> errObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">return</span> errObj<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> wholesaler <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> m <span style="color: #339933;">=</span> manufacturer<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
        orderBook<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>bookName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span>m.<span style="color: #660066;">orderBook</span><span style="color: #009900;">&#40;</span>bookName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pipe</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>respObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Wholesaler-DONE: &quot;</span><span style="color: #339933;">,</span> respObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> respObj<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>errObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Wholesaler-FAIL: &quot;</span><span style="color: #339933;">,</span> errObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">return</span> errObj<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> retailer <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> w <span style="color: #339933;">=</span> wholesaler<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
        orderBook<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>bookName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span>w.<span style="color: #660066;">orderBook</span><span style="color: #009900;">&#40;</span>bookName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pipe</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>respObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Retailer-DONE: &quot;</span><span style="color: #339933;">,</span> respObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                dfdResult.<span style="color: #660066;">resolve</span><span style="color: #009900;">&#40;</span>respObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>errObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Retailer-FAIL: &quot;</span><span style="color: #339933;">,</span> errObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">return</span> errObj<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Notice that, although we have multiple potential points of failure above (e.g.</span>
<span style="color: #006600; font-style: italic;">// Manufacturer, Wholesaler, Retailer, the customer only needs to deal with the</span>
<span style="color: #006600; font-style: italic;">// retailer directly.)</span>
<span style="color: #003366; font-weight: bold;">function</span> makePurchase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Place an order for two books</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span><span style="color: #CC0000;">3</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span>retailer.<span style="color: #660066;">orderBook</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Great Book - Part &quot;</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>respObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Purchaser-DONE... respObj: &quot;</span><span style="color: #339933;">,</span> respObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fail</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Purchaser-FAIL... err: &quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
makePurchase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/*
Random output for placing order of two books might look like:
Manufacturer-FAIL: fail: /book/Great Book - Part 1
Wholesaler-FAIL: fail: /book/Great Book - Part 1
Retailer-FAIL: fail: /book/Great Book - Part 1
Purchaser-FAIL... err: fail: /book/Great Book - Part 1
Manufacturer-DONE: success: /book/Great Book - Part 2
Wholesaler-DONE: success: /book/Great Book - Part 2
Retailer-DONE: success: /book/Great Book - Part 2
Purchaser-DONE... respObj: success: /book/Great Book - Part 2
*/</span></pre>
</div>
</div>
<p>Start at the bottom at the makePurchase() call and you should see that we&#8217;ve essentially modeled our story of a customer (purchaser) placing an order for two books. Should the purchase succeed or fail, the purchaser will only ever have to deal with the retailer directly (as expected), because the fact that deferred promises can sort of &#8220;bubble back up&#8221; the call chain. This keeps the interface quite clean, and the caller&#8217;s required knowledge of inner working to a minimum.</p>
<h2> Conclusion</h2>
<p>I&#8217;ve purposely kept the examples contrived so you can focus on the principles of deferred objects. If you&#8217;d like to see a variety of other example implementations, <a href="http://jaubourg.net/">Julian Aubourg</a> and <a href="http://addyosmani.com/">Addy Osmani</a> have written a <a href="http://msdn.microsoft.com/en-us/scriptjunkie/gg723713">wonderful article</a> you should definitey have a look at. jQuery deferred objects take a bit to get used to, but provide some very compelling advantages for web applications that require heavy ajax or graphics.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2011/12/jquery-deferred-objects-part-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>jQuery Deferred Objects</title>
		<link>http://colonelpanic.net/2011/11/jquery-deferred-objects/</link>
		<comments>http://colonelpanic.net/2011/11/jquery-deferred-objects/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 18:46:48 +0000</pubDate>
		<dc:creator>Robin</dc:creator>
				<category><![CDATA[General Development]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=575</guid>
		<description><![CDATA[If you&#8217;re a web developer, you may be asking yourself, &#8216;what are these &#8220;deferred objects&#8221; I keep hearing about?&#8217; Hopefully, this article will help explain that. Given the asynchronous nature of the web, and specifically JavaScript, a general asynchronous callback pattern has proliferated. So you have something you need to defer a bit so you [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re a web developer, you may be asking yourself, &#8216;what are these &#8220;deferred objects&#8221; I keep hearing about?&#8217; Hopefully, this article will help explain that.</p>
<p>Given the asynchronous nature of the web, and specifically JavaScript, a general asynchronous callback pattern has proliferated. So you have something you need to defer a bit so you use a timeout or an interval and you&#8217;re good to go&#8230;what&#8217;s the issue you may be asking? Well&#8230;web applications have become more and more complex, and what was once a nested callback or two, is turning into a hairy beast of nested callbacks.</p>
<p>The Deferred pattern addresses this nested nightmare by using using an idiom much like publish/subscribe (Observer) or Notifications (if you come from the land of iOS development). However, notification is a bit different because the caller has access to the Deferred object itself and can determine whether to call success or fail callbacks. This is sort of a forward reference, but will hopefully be clearer further down in this article. That said, let&#8217;s dig in&#8230;</p>
<p>If you&#8217;re familiar with stacks and queues then the underlying concept should be pretty straight forward. Essentially, the Deferred object implementation keeps a stack of callback function objects (similar to subscribers) for success or failure. Conventionally, these are called done callbacks or fail callbacks. Specifically, these function objects get pushed on the Deferred object when you call .then(myfunc), .done(myfunc), or .fail(myfunc):</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> deferred <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Deferred<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
deferred.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
setTimeout<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3000</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  deferred.<span style="color: #660066;">resolve</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hey buddy!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>First we create a deferred object. Immediately, the deferred object goes in to the &#8220;Pending&#8221; state. </p>
<p>Next, and here&#8217;s the important part to &#8220;grok&#8221;, by calling .done(&#8230; on the Deferred object, we are essentially asking it to add our function object to its array of callback functions. </p>
<p>Ok, so now we have 1..* functions queued (or stacked depending on implementation details) on the deferred object. Now one of two things will happen (at some point):<br />
1. We call deferred.resolve (or one of it&#8217;s variants)<br />
2. We call deferred.reject</p>
<p>If we do 1., this will &#8220;trigger&#8221; the deferred object to call all of the queued resolve category functions. These are the .done, .then, variant callbacks.</p>
<p>If we do 2., this will &#8220;trigger&#8221; the deferred object call all of the queued fail variant functions.</p>
<p>There are some semantic details to all of this, but that drives to the essence of the pattern. If you&#8217;re still not clear, I&#8217;ve drawn this extremely ugly diagram to help to visualize the essence of the deferred pattern (as described in both the <a href="http://api.jquery.com/category/deferred-object/">jQuery API docs</a> and <a href="http://wiki.commonjs.org/wiki/Promises/A">CommonJS Promises page)</a>. Open your browser wide and start on the left and work to the right:</a></p>
<p><img src="http://colonelpanic.net/wp-content/uploads/2011/11/Deferred.png" alt="Deferred Object Pattern" /></p>
<p>Cool! So how is this Deferred object doing it&#8217;s magic internally? Here&#8217;s a simplified example of how it may be implemented:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>JavaScript Deferred Objects Example<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Deferred<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> callbacks <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">done</span> <span style="color: #339933;">=</span>  <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>cb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        callbacks.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>cb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">resolve</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> callbacks.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            callbacks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Now we use the deferred object</span>
<span style="color: #003366; font-weight: bold;">var</span> deferred <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Deferred<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
deferred.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  deferred.<span style="color: #660066;">resolve</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>Hey buddy<span style="color: #339933;">!&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>What&#8217;s interesting about this pattern is that the owner of the deferred object determines whether we go in to the resolved or rejected state not the deferred object itself. This is slightly different from how a traditional Pub/Sub pattern would work, where the Publishing object itself would likely determine who/when to callback it&#8217;s subscribers. Because of this, it&#8217;s common to see usage where an intermediary function that is supplying some service (e.g. fetchResource, hitDatabase, etc.), will negotiate the initialization of the deferred object, determine whether to call resolve or reject, etc., and simply return the deferred object. So it looks like:</p>
<p><img src="http://colonelpanic.net/wp-content/uploads/2011/11/Deferred_Brokered.png" alt="Deferred Pattern using a Broker" /></p>
<p>The above diagram only shows part of the picture. Once the Broker object calls resolve or reject, the original caller&#8217;s respective .done, .fail, etc., will get called.</p>
<p>All of this is possible because of JavaScript&#8217;s ability to treat functions as first class citizens (a fancy way of saying you can pass functions around and call them later and treat them like good ol&#8217; objects!).</p>
<p>I hope this posting was somewhat helpful and revealed the magic behind Deferred Objects.</p>
<p><strong>EDIT</strong></p>
<p>My good friend Richard Bateman offers a point of clarification. There is actually a difference between .then and .done; .then allows you to provide two callbacks, the first for done and the second for fail.</p>
<h3>Next part</h3>
<p>For more on jquery deferred objects, see <a href="http://colonelpanic.net/2011/12/jquery-deferred-objects-part-2/">part 2 of this series</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2011/11/jquery-deferred-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>UPDATED: Terminal Nirvana with TotalTerminal (Snow Leopard, Lion)</title>
		<link>http://colonelpanic.net/2011/08/updated-terminal-nirvana-with-totalterminal-snow-leopard-lion/</link>
		<comments>http://colonelpanic.net/2011/08/updated-terminal-nirvana-with-totalterminal-snow-leopard-lion/#comments</comments>
		<pubDate>Tue, 30 Aug 2011 05:08:30 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[Systems Administration]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[visor]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=537</guid>
		<description><![CDATA[It&#8217;s been a while since I wrote up how I achieved Terminal Nirvana on Snow Leopard using SIMBL and Visor. Since then, some things have changed: TotalTerminal has replaced Visor Mac OS X Lion has been released TotalTerminal changes up the way it launches Terminal &#8212; instead of being automatically injected via SIMBL, it manually [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I wrote up how I achieved <a title="Terminal Nirvana on Snow Leopard with SIMBL + Visor" href="http://colonelpanic.net/2010/07/terminal-nirvana-on-snow-leopard-with-simbl-visor/">Terminal Nirvana on Snow Leopard using SIMBL and Visor</a>. Since then, some things have changed:</p>
<ul>
<li><a href="http://totalterminal.binaryage.com/">TotalTerminal</a> has replaced Visor</li>
<li>Mac OS X Lion has been released</li>
</ul>
<p>TotalTerminal changes up the way it launches Terminal &#8212; instead of being automatically injected via SIMBL, it manually injects the plugin into Terminal.app. Well, I wasn&#8217;t going to let this deter me.</p>
<p>Getting back to Nirvana after installing TotalTerminal is not all that bad. Most of the steps haven&#8217;t changed from my original post so I will only go into detail about the new steps here. Briefly, here are the steps I followed.</p>
<ol>
<li>Remove TerminalColours.bundle SIMBL plugin &#8212; per the <a href="http://totalterminal.binaryage.com/#faq">TotalTerminal FAQ</a>, it injects its own version and having TerminalColours.bundle installed creates a conflict. (~/Library/Application\ Support/SIMBL/Plugins)</li>
<li>Fix the Home / End keys (see original post)</li>
<li>Copy Terminal.app to VisorTerminal.app (see original post)</li>
<li>Install TotalTerminal</li>
<li>Modify TotalTerminal to use VisorTerminal.app instead of Terminal.app</li>
</ol>
<h3>That&#8217;s right, there&#8217;s really only one thing to change, and it&#8217;s very simple</h3>
<p>TotalTerminal.app has a simple, compiled AppleScript which launches Terminal and injects itself. The compiled file is located at /Applications/TotalTerminal.app/Contents/Resources/Scripts/main.scpt. But, we don&#8217;t want the compiled binary version, we want the original so we can make it use VisorTerminal.app.</p>
<p>Fortunately the <a href="https://raw.github.com/binaryage/totalterminal-installer/master/TotalTerminal.app/Contents/Resources/Scripts/main.applescript">uncompiled source file</a> is available in the <a href="https://github.com/binaryage/totalterminal-installer">TotalTerminal repo on Github</a>.</p>
<p>Only one change is necessary.</p>
<div class="wp_syntax">
<div class="code">
<pre class="diff" style="font-family:monospace;"><span style="color: #888822;">--- TotalTerminal.app/Contents/Resources/Scripts/main.applescript</span>
<span style="color: #888822;">+++ TotalTerminal.app/Contents/Resources/Scripts/main.applescript</span>
<span style="color: #440088;">@@ -1,4 +1,4 @@</span>
<span style="color: #991111;">-tell application &quot;Terminal&quot;</span>
<span style="color: #00b000;">+tell application &quot;VisorTerminal&quot;</span>
        delay <span style="">1</span> -- this delay is important to prevent random &quot;Connection is Invalid -<span style="">609</span>&quot; AppleScript errors
        try
                event BATTinit</pre>
</div>
</div>
<p>Once that is done, you just need to compile the script and replace the one that shipped with TotalTerminal.</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;">$ osacompile <span style="color: #660033;">-o</span> main.scpt main.applescript
$ <span style="color: #c20cb9; font-weight: bold;">mv</span> main.scpt <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>TotalTerminal.app<span style="color: #000000; font-weight: bold;">/</span>Contents<span style="color: #000000; font-weight: bold;">/</span>Resources<span style="color: #000000; font-weight: bold;">/</span>Scripts</pre>
</div>
</div>
<h3>That&#8217;s it.</h3>
<p>You should be good to go. Put TotalTerminal.app into your user startup applications if you wish (I do), or launch it manually by launching TotalTerminal.app.</p>
<p>Enjoy the restoration of terminal nirvana.</p>
<h3>For the lazy&#8230;</h3>
<p>My modified and compiled main.applescript is <a href="http://colonelpanic.net/wp-content/uploads/2011/08/modified-applescript-compiled.zip">available for download</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2011/08/updated-terminal-nirvana-with-totalterminal-snow-leopard-lion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enabling git access via password authentication with gitolite</title>
		<link>http://colonelpanic.net/2011/03/enabling-git-access-via-password-authentication-with-gitolite/</link>
		<comments>http://colonelpanic.net/2011/03/enabling-git-access-via-password-authentication-with-gitolite/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 23:46:17 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[gitolite]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=486</guid>
		<description><![CDATA[We recently started using gitolite at my workplace. Our previous git hosting setup involved manually managing linux users and groups on the Ubuntu server, which was needlessly time consuming and painful. There were times when file permissions got out of sync because the repo was deployed by user X from his workstation (where group permissions [...]]]></description>
			<content:encoded><![CDATA[<p>We recently started using <a title="gitolite" href="https://github.com/sitaramc/gitolite">gitolite</a> at my workplace. Our previous git hosting setup involved manually managing linux users and groups on the Ubuntu server, which was needlessly time consuming and painful. There were times when file permissions got out of sync because the repo was deployed by user X from his workstation (where group permissions were not setup correctly for a shared git repository), and other headaches.</p>
<p>We needed something easier. We did not want to go to a workflow where there is one git user on the system, every employee authenticates with their own public key to that user, and everyone with a key has access to everything. We wanted more granular control, but also an easier management workflow. While gitolite does use a single system user under the covers, it provides per-user access control (governed by public keys), easy repo management, and a couple other nice features like wildcard repositories and branch-level access restrictions.</p>
<p>However, one of our developers is in the habit of accessing a random server, cloning a repo (using his password), and pushing from environments other than his development environment. Obviously, his private key is not going to be on each of the machines he does this from. Gitolite does not allow password-based access for obvious reasons &#8212; it needs to know who you are to determine permissions &#8212; so gitolite was cramping his style.</p>
<p><span id="more-486"></span></p>
<p>There are a couple ways around this:</p>
<ol>
<li>Add a public key for each system he pushes changes from. Since he often logs in as &#8220;administrator&#8221; or &#8220;root&#8221; on individual systems, this obviously is not a good solution.</li>
<li>Have the developer enable SSH Agent Forwarding from his machine. This works well, as long as the user does not chain sessions (connect to server A, from there to server B, and clone on B) or is willing / able / allowed to enable Agent Forwarding on each system in the chain. (NOTE: on Mac OS X Snow Leopard, enabling SSH Agent Forwarding is as simple as running ssh-add and adding two lines to your ~/.ssh/config file:
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># add key to agent (I do this in ~/.profile)</span>
<span style="color: #c20cb9; font-weight: bold;">ssh-add</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa
&nbsp;
<span style="color: #666666; font-style: italic;"># ~/.ssh/config</span>
Host <span style="color: #000000; font-weight: bold;">*</span>
    ForwardAgent <span style="color: #c20cb9; font-weight: bold;">yes</span></pre>
</div>
</div>
</li>
<li>Come up with something else.</li>
</ol>
<p>What we really wanted was a way to allow password-based access to the repositories. Since gitolite does not support this itself, we had to come up with another way. As it turns out, doing so is not too bad, though it does involve a little bash shell black magic.</p>
<p>What we came up with is creating a system user on the git server for each developer who wants password access to git repositories, using a custom script as their defined shell (instead of /bin/sh or /bin/bash). Don&#8217;t forget to make your new script executable.</p>
<p>This script needs to look at what the user is trying to do (git action, gitolite ADC, some random shell command, nothing&#8230;) and act accordingly. Here&#8217;s the script we came up with, which will be dissected below to explain what each part does in more detail than the inline doc (<a href="https://gist.github.com/847755">gist</a>).</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #7a0874; font-weight: bold;">shift</span> <span style="color: #666666; font-style: italic;"># get rid of -c</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># if no commands, just open a shell</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-l</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># if the first arg is a git- command, that means it is something like git-push, etc... so forward it</span>
<span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$1</span> == git-<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">UserKnownHostsFile</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #660033;">-o</span> <span style="color: #007800;">StrictHostKeyChecking</span>=no <span style="color: #c20cb9; font-weight: bold;">git</span><span style="color: #000000; font-weight: bold;">@</span>localhost <span style="color: #007800;">$*</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># if the first arg is SET_ENV_ONLY, we sourced this file in order to set up the gitolite function</span>
<span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$1</span> == <span style="color: #ff0000;">&quot;SET_ENV_ONLY&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        gitolite <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">UserKnownHostsFile</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #660033;">-o</span> <span style="color: #007800;">StrictHostKeyChecking</span>=no <span style="color: #c20cb9; font-weight: bold;">git</span><span style="color: #000000; font-weight: bold;">@</span>localhost <span style="color: #007800;">$*</span>
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># if there is at least one non-git command, source this file in a new shell and create the 'gitolite' function</span>
<span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;source $0 shiftme SET_ENV_ONLY; $*&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre>
</div>
</div>
<p>If the user is not trying to <em>do</em> anything (just wants to open a shell), then there are no arguments and we should just open a bash prompt:</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-l</span></pre>
</div>
</div>
<p>Okay, the simple bit is out of the way. Now on to the black magic.</p>
<p>If the user is trying to perform a git command (via git push, etc.) then we create a new SSH session to localhost (remember, this is running on the same server as gitolite) and pass along the commands ($*). Since the user has already logged in, this ssh command will run from their account using their private / public key. The git commands are things like git-receive-pack, git-upload-pack, etc, so we match the first script argument against &#8220;git-&#8221;.</p>
<p>Finally, we don&#8217;t care about validating the host&#8217;s fingerprint (we are already on the host) and there can be no man in the middle (since we are going to localhost), so we can turn off StrictHostKeyChecking. We also don&#8217;t want to print out the various fingerprint messages that SSH will generate when you ignore host keys, so we make the new SSH connection quiet. That&#8217;s it for this block:</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$1</span> == git-<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">UserKnownHostsFile</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #660033;">-o</span> <span style="color: #007800;">StrictHostKeyChecking</span>=no <span style="color: #c20cb9; font-weight: bold;">git</span><span style="color: #000000; font-weight: bold;">@</span>localhost <span style="color: #007800;">$*</span></pre>
</div>
</div>
<p>I am going to go over the last two pieces in reverse order, because it is easier to follow.</p>
<p>If the user sent some SSH commands that were <strong>not</strong> native git commands, we need to process those commands. So, we will execute the remaining commands in a new bash process.</p>
<p>BUT, what if the user is trying to perform a gitolite command (such as <em>expand</em> or an ADC), what should we do? We can&#8217;t just trust that all commands the user sends will either be a git command or a gitolite command, so we need to set up some more black magic to handle that for us. So the first thing we will do when we open a new bash process to handle the remaining commands is source <strong>this custom script</strong> ($0) with a custom value we made up (SET_ENV_ONLY). That way, if this script executes again we will know it was done by ourselves and we can set up some more magic.</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;source $0 shiftme SET_ENV_ONLY; $*&quot;</span></pre>
</div>
</div>
<p>Finally for the last block.</p>
<p>If our new script is being executed, and the first argument is SET_ENV_ONLY (after <em>shift</em>ing), then it is because we sourced it from our new bash process from the previous step. What we do now is set up a <em>gitolite</em> command which does something very similar to what happens if the user is performing a native git command: it opens an SSH connection to git@host and performs the gitolite commands.</p>
<p>After this has been done, &#8220;gitolite expand&#8221; is the same as &#8220;ssh git@git.company.com expand&#8221;. This makes it so the developer can still run gitolite commands and ADCs, he just has to prefix each command with <em>gitolite</em>. Something like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>DEVELOPER<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">@</span>git.company.com <span style="color: #ff0000;">&quot;gitolite expand; echo <span style="color: #007800;">$PATH</span>; gitolite info&quot;</span></pre>
</div>
</div>
<p>is equivalent to this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #c20cb9; font-weight: bold;">git</span><span style="color: #000000; font-weight: bold;">@</span>git.company.com expand
$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>DEVELOPER<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">@</span>git.company.com <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$PATH</span>
$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #c20cb9; font-weight: bold;">git</span><span style="color: #000000; font-weight: bold;">@</span>git.company.com info</pre>
</div>
</div>
<p>Now you just need to create accounts that use this script as their shell. Each user can add whatever public keys they want to their authorized_keys file, or they can authenticate solely with their password. The only requirement is that their server account has a private / public key generated, and that the public key is added to gitolite.</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> root<span style="color: #000000; font-weight: bold;">@</span>git.company.com
$ useradd <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>custom_gitbash.sh <span style="color: #660033;">-m</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>DEVELOPER<span style="color: #7a0874; font-weight: bold;">&#93;</span>
$ <span style="color: #c20cb9; font-weight: bold;">su</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>DEVELOPER<span style="color: #7a0874; font-weight: bold;">&#93;</span>
$ <span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa
$ <span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa.pub ~<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>DEVELOPER<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">@</span>git.company.com.pub
<span style="color: #666666; font-style: italic;"># add the new public key to the gitolite keydir</span></pre>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2011/03/enabling-git-access-via-password-authentication-with-gitolite/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Resizing an image with GDI+ with C++</title>
		<link>http://colonelpanic.net/2010/11/resizing-an-image-with-gdi-with-cpp/</link>
		<comments>http://colonelpanic.net/2010/11/resizing-an-image-with-gdi-with-cpp/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 04:53:30 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[featured]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=432</guid>
		<description><![CDATA[Resizing images Resizing images is a fairly common need; there are lots of libraries around that do this.  However, I just needed to create thumbnails on-the-fly, and I certainly didn&#8217;t want to triple the size of my binary by linking against ImageMagick just to support resizing common image types. GDI+ has the capability to do [...]]]></description>
			<content:encoded><![CDATA[<h1>Resizing images</h1>
<p>Resizing images is a fairly common need; there are lots of libraries around that do this.  However, I just needed to create thumbnails on-the-fly, and I certainly didn&#8217;t want to triple the size of my binary by linking against ImageMagick just to support resizing common image types.</p>
<p>GDI+ has the capability to do this, but surprisingly I was unable to find a clear example of how to do this just using GDI+ in C++ &#8212; lots of examples in C#, but I needed to do it in C++.</p>
<p>In the hopes that the next person to attempt this will simply find this blog post, here is how to do it in 6 easy(ish) steps:<br />
<span id="more-432"></span></p>
<h2>Step 1: Initialize GDI+</h2>
<p>If you haven&#8217;t already done so, you need to initailize GDI+.  This is easy, and can be done with the following commands (also note the header file you need to include):</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// We'll use these headers:</span>
<span style="color: #339900;">#include &lt;Gdiplus.h&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
<span style="color: #666666;">// In your header file, class, etc:</span>
ULONG_PTR<span style="color: #008000;">&#40;</span>m_gdiplusToken<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// Somewhere where it will run once before you need to use GDI:</span>
GdiplusStartupInput gdiplusstartupinput<span style="color: #008080;">;</span>
GdiplusStartup<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>m_gdiplusToken, <span style="color: #000040;">&amp;</span>gdiplusstartupinput, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<h2>Step 2: Load the image:</h2>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">wstring</span> filename<span style="color: #008000;">&#40;</span>L<span style="color: #FF0000;">&quot;someImage.jpg&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
Gdiplus<span style="color: #008080;">::</span><span style="color: #007788;">Bitmap</span> bmp<span style="color: #008000;">&#40;</span>filename.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p>That was hard, wasn&#8217;t it?</p>
<h2>Step 3: Resize the image:</h2>
<p>I wrote a function from this.  I adapted it from one I found somewhere, but now I can&#8217;t find the link. Note that this will preserve the aspect ratio of the image so that it fits in the specified boundaries but does not stretch.  This means that if you specify 128&#215;128, the resulting image might actually be something like 128&#215;96, 102&#215;128, etc.</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">Gdiplus<span style="color: #008080;">::</span><span style="color: #007788;">Bitmap</span><span style="color: #000040;">*</span> GDIPlusImageProcessor<span style="color: #008080;">::</span><span style="color: #007788;">ResizeClone</span><span style="color: #008000;">&#40;</span>Bitmap <span style="color: #000040;">*</span>bmp, INT width, INT height<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    UINT o_height <span style="color: #000080;">=</span> bmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetHeight<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    UINT o_width <span style="color: #000080;">=</span> bmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetWidth<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    INT n_width <span style="color: #000080;">=</span> width<span style="color: #008080;">;</span>
    INT n_height <span style="color: #000080;">=</span> height<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> ratio <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span>o_width<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span>o_height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>o_width <span style="color: #000080;">&gt;</span> o_height<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #666666;">// Resize down by width</span>
        n_height <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>UINT<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span>n_width<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> ratio<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
        n_width <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>UINT<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>n_height <span style="color: #000040;">*</span> ratio<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    Gdiplus<span style="color: #008080;">::</span><span style="color: #007788;">Bitmap</span><span style="color: #000040;">*</span> newBitmap <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Gdiplus<span style="color: #008080;">::</span><span style="color: #007788;">Bitmap</span><span style="color: #008000;">&#40;</span>n_width, n_height, bmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetPixelFormat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Gdiplus<span style="color: #008080;">::</span><span style="color: #007788;">Graphics</span> graphics<span style="color: #008000;">&#40;</span>newBitmap<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    graphics.<span style="color: #007788;">DrawImage</span><span style="color: #008000;">&#40;</span>bmp, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span>, n_width, n_height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> newBitmap<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</div>
</div>
<p>And then later after you load the image into bmp:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">INT width <span style="color: #000080;">=</span> height <span style="color: #000080;">=</span> <span style="color: #0000dd;">128</span><span style="color: #008080;">;</span>
Gdiplus<span style="color: #008080;">::</span><span style="color: #007788;">Bitmap</span> <span style="color: #000040;">*</span>resizedBmp <span style="color: #000080;">=</span> ResizeClone<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>bmp, width, height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<h2>Step 4: Get the encoder:</h2>
<p>The Gdiplus::Bitmap object stores the data in raw format so that it can be manipulated.  At this point you get to decide what format to save it in. I just wanted to resize it, so I keep it in the original format.  I created a simple map to map the supported image filenames to the mimetype, which we can use to find the correct encoder.</p>
<p>Here is what I store in my map:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">    m_mtMap<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;.jpeg&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;image/jpeg&quot;</span><span style="color: #008080;">;</span>
    m_mtMap<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;.jpe&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;image/jpeg&quot;</span><span style="color: #008080;">;</span>
    m_mtMap<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;.jpg&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;image/jpeg&quot;</span><span style="color: #008080;">;</span>
    m_mtMap<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;.png&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;image/png&quot;</span><span style="color: #008080;">;</span>
    m_mtMap<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;.gif&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;image/gif&quot;</span><span style="color: #008080;">;</span>
    m_mtMap<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;.tiff&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;image/tiff&quot;</span><span style="color: #008080;">;</span>
    m_mtMap<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;.tif&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;image/tiff&quot;</span><span style="color: #008080;">;</span>
    m_mtMap<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;.bmp&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;image/bmp&quot;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p>I found a utility function for looking up the encoder from the mimetype:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> GDIPlusImageProcessor<span style="color: #008080;">::</span><span style="color: #007788;">GetEncoderClsid</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> WCHAR<span style="color: #000040;">*</span> form, CLSID<span style="color: #000040;">*</span> pClsid<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    UINT num<span style="color: #008080;">;</span>
    UINT size<span style="color: #008080;">;</span>
    ImageCodecInfo<span style="color: #000040;">*</span> pImageCodecInfo<span style="color: #000080;">=</span><span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
    GetImageEncodersSize<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>num,<span style="color: #000040;">&amp;</span>size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>size<span style="color: #000080;">==</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    pImageCodecInfo<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span>ImageCodecInfo<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">malloc</span><span style="color: #008000;">&#40;</span>size<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>pImageCodecInfo<span style="color: #000080;">==</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    GetImageEncoders<span style="color: #008000;">&#40;</span>num,size,pImageCodecInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>UINT j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;</span>num<span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>wcscmp<span style="color: #008000;">&#40;</span>pImageCodecInfo<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">MimeType</span>,form<span style="color: #008000;">&#41;</span><span style="color: #000080;">==</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #000040;">*</span>pClsid <span style="color: #000080;">=</span> pImageCodecInfo<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">Clsid</span><span style="color: #008080;">;</span>
            <span style="color: #0000dd;">free</span><span style="color: #008000;">&#40;</span>pImageCodecInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">return</span> j<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">free</span><span style="color: #008000;">&#40;</span>pImageCodecInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</div>
</div>
<h2>Step 5: Save the image:</h2>
<p>Now that we have a function to get the encoder CLSID, we can save the image.  If we want to save it as a file, we can do it like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">CLSID encId<span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">wstring</span> extension <span style="color: #000080;">=</span> getExtension<span style="color: #008000;">&#40;</span>filename<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">wstring</span> mimetype <span style="color: #000080;">=</span> getMimeTypeFrom<span style="color: #008000;">&#40;</span>extension<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>GetEncoderClsid<span style="color: #008000;">&#40;</span>mimetype, <span style="color: #000040;">&amp;</span>encId<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    resizedBmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Save<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">wstring</span><span style="color: #008000;">&#40;</span>L<span style="color: #FF0000;">&quot;Output&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> extension, <span style="color: #000040;">&amp;</span>encId<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</div>
</div>
<p>Now, in my case this wasn&#8217;t actually the goal; I needed the byte data for that file.  I guess I could have saved it and then opened it and read it back, but since I am using this in a <a href="http://firebreath.org" target="_blank">FireBreath</a> plugin for Facebook that can be problematic.  You can get the bytes directly like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">        <span style="color: #666666;">// If we were able to determine the filetype, save it to a stream</span>
        IStream <span style="color: #000040;">*</span>buffer<span style="color: #008080;">;</span>
        CreateStreamOnHGlobal<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">true</span>, <span style="color: #000040;">&amp;</span>buffer<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        resizedBmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Save<span style="color: #008000;">&#40;</span>buffer, <span style="color: #000040;">&amp;</span>encId<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Find the size of the resulting buffer</span>
        STATSTG statstg<span style="color: #008080;">;</span>
        buffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Stat<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>statstg, STATFLAG_DEFAULT<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        ULONG bmpBufferSize <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>ULONG<span style="color: #008000;">&#41;</span>statstg.<span style="color: #007788;">cbSize</span>.<span style="color: #007788;">LowPart</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">int</span> length <span style="color: #000080;">=</span> bmpBufferSize<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Seek to the beginning of the stream</span>
        LARGE_INTEGER li <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
        buffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Seek<span style="color: #008000;">&#40;</span>li, STREAM_SEEK_SET, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Copy the resulting data from the stream into our result object</span>
        <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>data <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#91;</span>bmpBufferSize<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        res.<span style="color: #007788;">content_type</span> <span style="color: #000080;">=</span> content_type<span style="color: #008080;">;</span>
        ULONG bytesRead<span style="color: #008080;">;</span>
        buffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Read<span style="color: #008000;">&#40;</span>data, bmpBufferSize, <span style="color: #000040;">&amp;</span>bytesRead<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        buffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<h2>Step 6: Deinitialize GDI+ properly</h2>
<p>You need the token you created in step 1:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">    GdiplusShutdown<span style="color: #008000;">&#40;</span>m_gdiplusToken<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<h1>That&#8217;s it!</h1>
<p>Hope it helps someone!</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/11/resizing-an-image-with-gdi-with-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integrating doxygen and confluence</title>
		<link>http://colonelpanic.net/2010/10/integrating-doxygen-and-confluence/</link>
		<comments>http://colonelpanic.net/2010/10/integrating-doxygen-and-confluence/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 18:15:44 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[confluence]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[doxygen]]></category>
		<category><![CDATA[firebreath]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=410</guid>
		<description><![CDATA[FireBreath has a new website This last week FireBreath has taken a big step &#8212; we have a new website at http://firebreath.org. The reasons for the move are varied, but what it comes down to is that Google Code&#8217;s wiki, while pretty decent for small projects, simply isn&#8217;t powerful enough to handle documentation for a [...]]]></description>
			<content:encoded><![CDATA[<h1>FireBreath has a new website</h1>
<p>This last week FireBreath has taken a big step &#8212; we have a new website at <a title="FireBreath - Cross-Platform Browser Plugin framework" href="http://firebreath.org" target="_blank">http://firebreath.org</a>. The reasons for the move are varied, but what it comes down to is that Google Code&#8217;s wiki, while pretty decent for small projects, simply isn&#8217;t powerful enough to handle documentation for a project like this well.  Granted, it can do it, but not as well as other options.</p>
<h2>Creating an integrated documentation system</h2>
<p>My primary goal with this website is to provide a completely integrated documentation system for users of FireBreath that is easy for both me and others to update.  I am fairly happy with my solution so far which relies on <a title="Atlassian Confluence" href="http://www.atlassian.com/software/confluence/" target="_blank">Atlassian Confluence</a> and <a title="Doxygen" href="http://www.doxygen.org/">Doxygen</a>.  To find out why and how I chose this combination, as well as the steps I took to get here, read on.  Also, if you&#8217;re in a hurry and just want to see the end result, skip to the last couple sections of this post under &#8220;<a href="http://colonelpanic.net/2010/10/integrating-doxygen-and-confluence/#nirvana">Doxygen/Confluence Nirvana</a>&#8221; =]</p>
<h2><span id="more-410"></span>Choosing Confluence</h2>
<p>I should add a quick plug here for <a title="Atlassian Confluence" href="http://www.atlassian.com/software/confluence/" target="_blank">Atlassian Confluence</a>. No, I don&#8217;t work for them, and previous to this I had only used their more well known solution <a href="http://www.atlassian.com/software/jira/">JIRA</a>, which is also not bad.  The main reason I had never really looked into them is that, well, I&#8217;m cheap, and those are commercial products.  However, as it turns out, Atlassian provides <a title="Confluence pricing" href="http://www.atlassian.com/software/confluence/pricing.jsp" target="_blank">free licenses</a> for open source projects!  Awesome!  It should also be mentioned that the commercial license for small organizations is pretty reasonable as well.</p>
<p>I installed it first just to test, and I did have to learn a bit of new wiki syntax; however, the syntax is easily understandable and powerful.  With the help of 4 or 5 vim search/replace regex strings, it probably took me all of an hour to convert 30+ wiki pages from google code format to confluence, and I can customize the theme and fonts!  When I found <a title="Gliffy confluence plugin" href="http://www.gliffy.com/confluence-plugin/" target="_blank">Gliffy</a> (also free for open source projects) I was hooked and immediately applied for the open source license.  Less than 48 hours later I had it.</p>
<h2>Creating doxygen docs</h2>
<p>In a recent survey that we ask FireBreath users to fill out, many users mentioned frustration with the lack of good class documentation in FireBreath, and a few suggested something generated from Source Code.  After doing a little research, I chose <a title="Doxygen" href="http://www.doxygen.org">Doxygen</a> as the tool to use; in fact, it&#8217;s the only real C++ documentation generation tool I found.  It took a little learning, but soon I had class documentation generated from FireBreath source code and started adding documentation comments.</p>
<p>Writing good documentation for use with doxygen is difficult, but <a href="http://blog.ezyang.com/2010/06/secret-of-autogenerated-docs/">if you work at it</a> you can come up with some <a title="FireBreath auto-generated docs - annotated class list" href="http://classdocs.firebreath.org/annotated.html" target="_blank">pretty good stuff</a>.  However, the index in doxygen isn&#8217;t quite what I wanted.</p>
<h2>Integrating the doxygen docs in confluence</h2>
<p>After I tweaked the stylesheet to make it more readable, I found that there was still something I didn&#8217;t like about the docs; we have lots of documentation written in our wiki, tutorials and explanations that don&#8217;t make sense to put in the code.  However, it&#8217;s in a completely different system from the class documentation generated by doxygen, so even if our docs link to each other, the experience is far from ideal!</p>
<p>We definitely need the docs integrated into the same system.</p>
<h3>The iframe approach</h3>
<p>The first thing I tried was embedding an iframe in a confluence wiki page.  This isn&#8217;t too hard; you just have to set up a user macro or enable the {iframe} macro.  I set a minimum height so that it was readable, and it wasn&#8217;t too bad.</p>
<p>The problem is, it doesn&#8217;t use the full available height, it has weird scroll bars, and different sized screens really throw off the look.</p>
<h3>The {html-include} macro</h3>
<p>The next thing I tried was <a href="http://confluence.atlassian.com/display/DOC/Enabling+the+html-include+Macro">enabling the html-include</a> macro, adding classdocs.firebreath.org into the whitelist, and then using the following code:</p>
<pre>{html-include:url=http://classdocs.firebreath.org/patched/classes.html}</pre>
<p>This actually looked pretty good!  Everything was integrated, my class list was there!  I was pretty excited!  Well, at least until I tried clicking on a link.  All the links are relative, and html-include doesn&#8217;t rewrite the links.  So, naturally, none of them worked!  Also, since the &lt;head&gt; section of the page was missing, we had no stylesheet.</p>
<p>The missing stylesheet issue wasn&#8217;t that hard to fix; I simply copied the majority of the stylesheet from doxygen (which I had tweaked anyway to match the color scheme) and put it into a user macro.</p>
<pre>&lt;style&gt;
/* contents of doxygen.css here */
&lt;/style&gt;</pre>
<p>I also added style tags to hide the tab navigation, since it looked just a bit out of place.</p>
<h3>sed to the rescue!</h3>
<p>So I now had class docs that I could put into a confluence page, but none of my links worked!  After a little thought, I came up with the following bash script:</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> fl <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*</span>.html; <span style="color: #000000; font-weight: bold;">do</span>
 <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$fl</span> <span style="color: #007800;">$fl</span>.old
 <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/href=&quot;\([^#]\)/href=&quot;http:\/\/classdocs.firebreath.org\/\1/g'</span> <span style="color: #007800;">$fl</span>.old <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #007800;">$fl</span>.stage2
 <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/http:\/\/classdocs.firebreath.org\/javascript:/javascript:/g'</span> <span style="color: #007800;">$fl</span>.stage2 <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #007800;">$fl</span>.stage3
 <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/src=&quot;/src=&quot;http:\/\/classdocs.firebreath.org\//g'</span> <span style="color: #007800;">$fl</span>.stage3 <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #007800;">$fl</span>
 <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$fl</span>.old
 <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$fl</span>.stage2
 <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$fl</span>.stage3
<span style="color: #000000; font-weight: bold;">done</span></pre>
</div>
</div>
<p>This script processes all of the links in the page and adds an absolute path to each!  Fantastic, now my links work!</p>
<p>Except&#8230;. well, I still wasn&#8217;t happy with it.  There had to be a better way!  Sure, the links work, but when you click on it you leave the page!</p>
<h3><a name="nirvana">Doxygen-Confluence nirvana</a></h3>
<p>I looked at a lot of options.  I downloaded the Atlassian plugin framework and looked at writing a plugin to help me.  I emailed Atlassian support.  I looked at a lot of things.  Finally, I gave up and broke out the python.  I have attached the resulting script.  Here is what it does:</p>
<p>Uses the following config options:</p>
<ul>
<li>Input path to doxygen docs</li>
<li>Output path</li>
<li>Parent pageId for:
<ul>
<li>Classes</li>
<li>Structs</li>
<li>Namespaces</li>
<li>Files</li>
</ul>
</li>
<li>Confluence URL</li>
<li>Username/password passed on the command line</li>
</ul>
<p>Performs the following general steps:</p>
<ol>
<li>Expects that doxygen has been run and has generated docs/html and docs/xml directories</li>
<li>Loads and parses docs/xml/index.xml</li>
<li>Loop through all entries in the XML file
<ol>
<li>Get the refid from the xml to figure out which HTML file exists for that entry</li>
<li>Generate a &#8220;readable name&#8221; for each entry (replace &#8220;::&#8221; with a space, since confluence doesn&#8217;t allow &#8220;:&#8221; in page names)</li>
<li>Add an entry to the search/replace list to map the html filename to the new page name</li>
</ol>
</li>
<li>Process all .html files found; search for .html paths and replace then with the new page name path</li>
<li>Loop through all found pages
<ol>
<li>use XML-RPC to get the page from confluence</li>
<li>If none was found, create a new page</li>
<li>Set the parent of the page (see config options above)</li>
<li>Set the content of the page with:
<ul>
<li>{doxygen_init} &#8211; injects the CSS</li>
<li>{html-include} &#8211; with a web path to the output .html files</li>
</ul>
</li>
<li>Store the page in Confluence</li>
</ol>
</li>
<li>Remove all pages that are descendants of one of the root pages that weren&#8217;t added this session</li>
</ol>
<p>And voila!  Confluence now has all of the doxygen pages in, and since we rewrote all of the links they all point to each other.</p>
<h2>Clean-up</h2>
<p>If you, like me, think that including <em><strong>every</strong></em> class in the project could be a bit overkill and only include the ones that have documentation, you may want to remove the .html files that doxygen generates for the &#8220;non-visible&#8221; classes.  These aren&#8217;t linked to, so you don&#8217;t normally see them, but they do show up in the .xml file. Since part of the check the script does is to see if the expected .html file exists, you can fix this by simply deleting all .html files in the directory that nothing links to.  The best way I have found to do this is with the following script:</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">for</span> fl <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*</span>.html; <span style="color: #000000; font-weight: bold;">do</span>
 <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #007800;">$fl</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
 <span style="color: #000000; font-weight: bold;">if</span>  <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #007800;">$?</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-v</span> <span style="color: #007800;">$fl</span>
 <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span></pre>
</div>
</div>
<h1>The code</h1>
<p>Attachment: <a href="http://colonelpanic.net/wp-content/uploads/2010/10/doc2confluence.py_.gz">doc2confluence.py.gz</a></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/10/integrating-doxygen-and-confluence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solr DisMax gotchas: fun with stopwords and punctuation</title>
		<link>http://colonelpanic.net/2010/09/solr-dismax-gotchas-fun-with-stopwords-and-punctuation/</link>
		<comments>http://colonelpanic.net/2010/09/solr-dismax-gotchas-fun-with-stopwords-and-punctuation/#comments</comments>
		<pubDate>Sat, 18 Sep 2010 05:16:42 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[dismax]]></category>
		<category><![CDATA[solr]]></category>
		<category><![CDATA[stopwords]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=405</guid>
		<description><![CDATA[I have been using Apache Solr on a recent project for my employer, and ran into some gotchas. Notably, I had issues with a DisMax query not working the way I expected because of inconsistent stopwords configuration on the fields being searched. I also ran into a problem with words being tokenized with their surrounding punctuation, [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using <a href="http://lucene.apache.org/solr/">Apache Solr</a> on a recent project for my employer, and ran into some gotchas. Notably, I had issues with a DisMax query not working the way I expected because of inconsistent stopwords configuration on the fields being searched.</p>
<p>I also ran into a problem with words being tokenized with their surrounding punctuation, rather than simply the word &#8212; searching for &#8220;something&#8221; was definitely NOT the same thing as searching for &#8220;something.&#8221; &#8212; note the included punctuation in the second search.</p>
<p>The bottom line:</p>
<ul>
<li>make sure stopwords is configured the same way for the fields you are searching (or at least be sure you understand the ramifications)</li>
<li>don&#8217;t be afraid to play with the &#8220;minimum-match&#8221; (mm) default configuration</li>
<li>when it makes sense, strip surrounding punctuation using PatternReplaceFilterFactory using an index tokenizer</li>
</ul>
<p>For the full, detailed write-up, head over to OpenSky&#8217;s engineering blog and read the <a href="http://engineering.shopopensky.com/post/creating-a-unified-search-experience">original blog post</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/09/solr-dismax-gotchas-fun-with-stopwords-and-punctuation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic HTML content in iTunes LP / iTunes Extras</title>
		<link>http://colonelpanic.net/2010/08/dynamic-html-content-in-itunes-lp-itunes-extras/</link>
		<comments>http://colonelpanic.net/2010/08/dynamic-html-content-in-itunes-lp-itunes-extras/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 03:58:41 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[itunes]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=332</guid>
		<description><![CDATA[Recently, I have been doing some iTunes LP projects for one of my clients. One of the things that immediately struck me as a huge potential time saver was automating the creation of HTML elements for lists of items, ie lists of song and video tracks. Often I can splice images such that their position [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I have been doing some iTunes LP projects for one of my clients. One of the things that immediately struck me as a huge potential time saver was automating the creation of HTML elements for lists of items, ie lists of song and video tracks. Often I can splice images such that their position can be controlled with a single CSS rule, and explicitly creating all the <em>image-fader</em> DIVs for the individual songs or videos in the HTML views is terribly redundant.</p>
<h2>The Goal</h2>
<p>Create HTML elements dynamically which transition appropriately when the view controller appears.</p>
<h2>The Problem</h2>
<p>The shipping version of TuneKit.js doesn&#8217;t give you a chance to append elements to the DOM before viewDidAppear is invoked (which is after transitionToController).</p>
<p><span id="more-332"></span></p>
<h2>Initial Attempts</h2>
<p>I first tried appending the child elements (using TKUtils.buildElement in a loop over appData.songs) inside the viewDidAppear callback. Obviously, the problem here is that the elements don&#8217;t exist the first time the view controller appears, so can&#8217;t nicely fade in via your defined CSS animations.</p>
<p>Next, I tried moving the logic that create the HTML elements into viewWillAppear. The problem here was that this method was called before the desired view controller is added to the DOM &#8212; so the element I need to append the children to doesn&#8217;t exist.</p>
<h2>The Solution</h2>
<p>The solution was actually pretty simple, and works like a charm; you just need to be willing to edit TuneKit.js.<br />
<!--more--><br />
Before:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// pre-transition methods</span>
controller._viewWillAppear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
controller.<span style="color: #660066;">viewWillAppear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// add it to the tree</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">view</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>controller.<span style="color: #660066;">view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// transition</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>previous_controller <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">transitionToController</span><span style="color: #009900;">&#40;</span>previous_controller<span style="color: #339933;">,</span> controller<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>After:</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// add it to the tree</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">view</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>controller.<span style="color: #660066;">view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// pre-transition methods</span>
controller._viewWillAppear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
controller.<span style="color: #660066;">viewWillAppear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// transition</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>previous_controller <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">transitionToController</span><span style="color: #009900;">&#40;</span>previous_controller<span style="color: #339933;">,</span> controller<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<h2>Conclusion</h2>
<p>Now that TuneKit appends the view controller to the DOM before calling the viewWillAppear callback, your dynamic elements can be created before the view appears and transitionToController is invoked.</p>
<p>Done.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/08/dynamic-html-content-in-itunes-lp-itunes-extras/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging PHP in Vim using VimDebugger</title>
		<link>http://colonelpanic.net/2010/08/debugging-php-in-vim-using-vimdebugger/</link>
		<comments>http://colonelpanic.net/2010/08/debugging-php-in-vim-using-vimdebugger/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 15:41:13 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[xdebug]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=298</guid>
		<description><![CDATA[Remote Debugging PHP from vim I&#8217;ve recently been using PHP a lot.  I&#8217;ve also been using VIM a lot.  So, it makes sense that I would start using the PHP remote debugger, right?  Well, the problem is (and don&#8217;t take this the wrong way), the php remote debugger plugin for vim was written in 2004, [...]]]></description>
			<content:encoded><![CDATA[<h2>Remote Debugging PHP from vim</h2>
<p>I&#8217;ve recently been using PHP a lot.  I&#8217;ve also been using VIM a lot.   So, it makes sense that I would start using the PHP remote debugger,  right?  Well, the problem is (and don&#8217;t take this the wrong way), the  php remote debugger plugin for vim was written in 2004, and there are a  few things missing from it.  So, being the overly-easily-amused  developer I am, I embarked on a quest to fix it.</p>
<p><span id="more-298"></span></p>
<h2>DBGp python library</h2>
<p>It didn&#8217;t take me long to realize that the python classes (vimscript  doesn&#8217;t do sockets, so you have to use something like python) provided  with the old plugin was not extensible enough for what I needed.  I  started re-factoring it, but fortunately before I got too far I found a  DBGp python library written by ActiveState.  I had to do some hacking to  get it all working together &#8212; vim doesn&#8217;t deal with import statements  in python well &#8212; but it turned out to be perfect for my needs.</p>
<h2>VimDebugger README</h2>
<p>Here is the README from the <a title="VimDebugger" href="http://github.com/taxilian/VimDebugger">github repo</a>:</p>
<div>
<h3>Vim Debugger</h3>
<p>VimDebugger is a dbgp client plugin for vim for debugging php   applications with xdebug.  It is currently in early stages, but is   already much more advanced than many other options.</p>
<h3>Special Thanks</h3>
<p>VimDebugger utilizes the ActiveState DBGp library. VimDebugger borrows code liberally from <a href="http://www.vim.org/scripts/script.php?script_id=1152">the remote PHP debugger</a> vim plugin by Seung Woo Shin.</p>
<h3>Setup</h3>
<p>At present, VimDebugger.py must be saved as   $VIMRUNTIME.&#8221;/plugin/VimDebugger.py&#8221; in order to work correctly.  I hope   to allow more flexible placement of this file in a future release.    VimDebugger.vim should generally be placed in the same directory.</p>
<h3>Remote Debugging</h3>
<p>VimDebugger will listen on localhost:9000.  This could be changed in   VimDebugger.vim, but currently is not easily configurable through your   .vimrc.  You need to have xdebug already configured to connect to that   port.</p>
<h3>Debugger functions</h3>
<p>The following commands are added by the debugger for all your remote debugging needs:</p>
<ul>
<li>:DbgRun  &#8211; Starts the listener (listens for 10 seconds) if your  script is not  already attached.  If it is, continues (will run to the  end or until  the next breakpoint)</li>
<li>:DbgDetach &#8211; Detaches the remote debugger and shuts down the listener</li>
<li>:DbgToggleBreakpoint &#8211; Toggles a breakpoint on the current line of the current file</li>
<li>:DbgStepInto &#8211; Steps into the next function or include</li>
<li>:DbgStepOver &#8211; Steps over the next function or include</li>
<li>:DbgStepOut &#8211; Steps out to the next step up in the stack</li>
</ul>
<h3>Key Bindings</h3>
<p>The debugger does not automatically bind any hotkeys, but leaves  that  to you to do in your own .vimrc.  I often use Visual Studio, so I  set  my key bindings up in a similar way:</p>
<pre>map &lt;F11&gt; :DbgStepInto&lt;CR&gt;
map &lt;F10&gt; :DbgStepOver&lt;CR&gt;
map &lt;S-F11&gt; :DbgStepOut&lt;CR&gt;
map &lt;F5&gt; :DbgRun&lt;CR&gt;
map &lt;S-F5&gt; :DbgDetach&lt;CR&gt;
map &lt;F8&gt; :DbgToggleBreakpoint&lt;CR&gt;</pre>
<h3>Watch</h3>
<p>Currently, there are no functions for adding your own watch items,   but that is planned for one of the next releases.  For now, the watch   window will automatically refresh every step with the current context.    The Watch window utilizes vim code folding on multi-line entries, so if   you have an object, be sure to expand it by double clicking, hitting   enter, or using the vim code folding keyboard commands to see the   introspection at work.  Only three levels are returned, to keep the   response from overwhelming the debugger.</p>
<p>For objects, a list of  the methods available on that object will be  returned with their  visibility.  Also, a list of properties will be  returned with their  visibility and their value.</p>
<p>For arrays, a list of values (up to three nested levels) will be returned.</p>
<h3>Stack</h3>
<p>The stack window updates every frame. To go to another part of the   stack, simply go to the line in question and double click or press   enter.</p>
<h2>Screenshots</h2>
<p>How well does it work?  Well, let me show you some screenshots, and then you decide.</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-8.39.21-AM2.png"><img class="alignnone size-full wp-image-301" title="Screen shot 2010-08-05 at 8.39.21 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-8.39.21-AM2.png" alt="" width="586" height="209" /></a></p>
<p>On the left, you see the code window; that looks almost identical to the old vim plugin.  That&#8217;s one thing it did very well, and I couldn&#8217;t improve on it; so I shamelessly stole a lot of the code, instead.  On the bottom right you see the first of the improvements I added.  Instead of just displaying a list of stack entries and requiring someone to type &#8220;:Up&#8221; and &#8220;:Dn&#8221; to traverse the stack, the list is formatted and set up so that you can double click or press enter on the line you want, and it will take you there in the left pane.</p>
<p>The real improvements can be seen in the watch window, on the top right.  You may notice that these are folded; there are three variables in the local scope, and as you can see they are all instances of objects.  Well, often when I&#8217;m debugging I need to know what kind of object that is!  Here it tells you.  That&#8217;s not even the best part, though; expand the fold, and you&#8217;ll see the following:</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.01.06-AM.png"><img class="alignnone size-full wp-image-302" title="Screen shot 2010-08-05 at 9.01.06 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.01.06-AM.png" alt="" width="473" height="71" /></a></p>
<p>The number of methods and properties!  Expand methods:</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.06.29-AM.png"><img class="alignnone size-full wp-image-303" title="Screen shot 2010-08-05 at 9.06.29 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.06.29-AM.png" alt="" width="441" height="152" /></a></p>
<p>I&#8217;ve truncated it for brevity, but trust me: the method names are all there.  Let&#8217;s look at properties.</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.07.34-AM.png"><img class="alignnone size-full wp-image-304" title="Screen shot 2010-08-05 at 9.07.34 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.07.34-AM.png" alt="" width="444" height="182" /></a></p>
<p>You&#8217;ll notice again; all of the properties, with their values, and&#8230; more folds?  Open one of them.</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.11.00-AM.png"><img class="alignnone size-full wp-image-305" title="Screen shot 2010-08-05 at 9.11.00 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.11.00-AM.png" alt="" width="610" height="187" /></a></p>
<p>As you can see, array values are also sent.  For practical reasons, there is a depth limit of 3 when fetching watch variables, but that is configurable.  The main challenge is getting the data back from PHP; I have the max data size set at 64KB, so that is the limit to how much can be sent. We could probably raise that limit, but that has other consequences.</p>
<h2>Installing it</h2>
<p>First, you may want to read <a title="How to debug PHP with Vim and XDebug" href="http://tech.blog.box.net/2007/06/20/how-to-debug-php-with-vim-and-xdebug-on-linux/" target="_blank">this blog post</a> to see how to set up xdebug.  Instead of using the vim plugin they link to, though, get mine from the <a title="VimDebugger on github" href="http://github.com/taxilian/VimDebugger">github repo</a>.  Put VimDebugger.py and VimDebugger.vim in your ~/.vim/plugins dir, and add the key bindings to your .vimrc.  Let me know how it goes!</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/08/debugging-php-in-vim-using-vimdebugger/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

