Archive for April, 2008

Drag and Drop Outlook Emails onto .NET Application

Wednesday, April 30th, 2008

I do talk a lot of trash about Microsoft on this blog, but since I work in a place which is a Windows shop I can’t really get away from it’s products. Today I dusted off my “C# Developer” hat, and started chugging away. Why .NET? Because it seemed like the right tool for the job. The task at hand was to figure out a way how someone could grab bunch of emails out of her Outlook, and then drag and drop them onto something so that:

  1. The emails get saved as .msg files in an appropriate folder on the network share
  2. Our internal intranet application becomes aware of these files, and display download links where applicable

So instead of forwarding emails to 4-5 people assigned to a given job, the scheduling person would simply put them in a central location on the server and appropriate people would see download links on their “project page” once they log into the web app. This would limit the “did you get that thing I sent you?” questions, and allow an administrator to add people to a project without the need of hunting down all the relevant emails and forwarding them to the new guy.

Communicating with the web application is not an issue - I can do that in any language. The tricky part the drag and drop functionality which is the whole point of this project. The whole exercise of saving the email as a .msg file and uploading it to the web app using a traditional HTML form was briefly considered for .5 seconds and then unanimously rejected because it would be a royal pain in the ass. On the other hand typing in a task number and then dragging/dropping relevant emails onto some sort of a form is kinda like forwarding them - only you just have to do it once.

I briefly considered Java, but the interaction between Outlook and the Java application looked like a really hairy business. On the other hand .NET seemed like a perfect choice. You’d think that Microsoft would have a relatively easy mechanism of scripting it’s flagship office product in their flagship programming environment. And it turns out that there is - it’s called MS Office Primary Interop Assemblies. These are bunch of COM objects which you can import into your Visual Studio project and they allow you to interact with Office applications at a somewhat intimate level. Unsurprisingly, they are dependent on the version of MS Office you are using (in my case Office 2003) making it hard to create a robust, catch-all solution. You can grab a distributable installer package for Office 2003 version from here.

I added the Microsoft Office 11 Core Library and Microsoft Outlook 11 Core Library as the references to my project. They automagically appeared under the COM tab of my reference dialog in Visual Studio after installing the redistributable package linked above.

Next on the menu was the Drag and Drop functionality. That was pretty easy. In fact, an article written by Tgueth on Code Project gives you a very good primer how to set things up. Just ignore his code, because it won’t work for what I’m trying to do. He was simply interested in dropping actual onto the form - so real files dragged over from Explorer, or Outlook attachments dragged from your email. The email themselves however are handled differently, and cannot be captured this way. I tried, and I failed. There is however an alternate solution using the Interop Assemblies I described above - and believe it or not, the code is actually much cleaner and more compact.

So I sat down and hacked up a quick prototype. Here is the relevant code:

namespace DragAndDrop
{
  public partial class Form1 : Form
  {
    private Microsoft.Office.Interop.Outlook.Application OL;
    private string my_dir = "C:\\";
 
    public Form1()
    {
      InitializeComponent();
      OL = new Microsoft.Office.Interop.Outlook.Application();
    }
 
    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
      e.Effect = DragDropEffects.Copy;
    }
 
    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
      for (int i = 1; i <= OL.ActiveExplorer().Selection.Count; i++)
      {
        Object temp = OL.ActiveExplorer().Selection[i];
 
        if (temp is Microsoft.Office.Interop.Outlook.MailItem)
        {
          Microsoft.Office.Interop.Outlook.MailItem mailitem = (temp as Microsoft.Office.Interop.Outlook.MailItem);
          string subject = mailitem.Subject.Replace(":", "");
          mailitem.SaveAs(my_dir + "\\" + subject + ".msg", Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);
        }
      }
    }
  }
}

Let’s break it down, shall we? The first method Form1_DragEnter describes what is supposed to happen when you drag an object onto your form. We simply want to change the Effect property of the event to Copy:

e.Effect = DragDropEffects.Copy;

If you don’t do this, then your code will detect drop effects but that’s about it - it won’t know or care what was dropped. This will ensure that the target of the drop will hold on to a copy of whatever you dragged over when you drop it.

Next, we need to handle the items that were dropped in the Form1_DragDrop method. The following statement returns an array of items which are currently selected with your mouse (ie. the stuff you are dragging right now):

OL.ActiveExplorer().Selection

Since I want to allow users to drag bunch of emails at a time, I’m going to iterate over this array and then grab each item. Since potentially a user could drag over something that is not an email, I’m temporarily saving it as an Object:

Object temp = OL.ActiveExplorer().Selection[i];

If it turns out to be an Outlook email object we cast it back to Microsoft.Office.Interop.Outlook.MailItem and save it. Note that I use the subject line as the filename (an quickly sanitize it removing the illegal “:” character) and save it in the root of the C: drive for now.

The SaveAs method of the MailItem object takes two parameters: first one is the path where you want to save the item, and the other is the file type which must be a member of the OlSaveAsType Enumeration which is a part of the Interop library. I’m using olMSG since that will save as the default Outlook message format.

This code works under Office 2003 but there is one small issue. For every mail item you drop, Outlook will throw this nasty security warning message:

Outlook Security Warning

The good news is that the user has the option to disable it for up to 10 minutes. The bad news is that 10 minutes is the maximum amount of time you can choose from that drop down box. I do see why they have this warning there, I just suspect it will become really annoying unless I find out a way to disable it. I did not cross that bridge yet though. Any suggestions? So far the bare bones functionality is working and I can drag and drop email messages onto my little form, and have them saved perfectly along with attachments which is precisely what I wanted.

Next step is to figure out how to make that thing talk to a PHP + MySQL backend of our web application, but that should not be that difficult. All I need to do is to send a HTTPS POST request back to the server, and then have something on the other end process that data and dump it into a database. It doesn’t really need to be much more complicated than that.

The Third Path…

Tuesday, April 29th, 2008

I found the following quote from Scott Adam’s blog a bit depressing:

Let’s say you have a typical life and try to live it in the healthiest way. You might allocate your 24-hour weekday this way:

Sleep: 8 hours
Exercise: 1 hour
Work: 8 hours
Eating: 2 hours (leisurely)
Hygiene: 1 hour
Travel: 1 (Commute, errands)

That leaves you three hours for family time, sex, shopping, food preparation, chores, household repair, volunteering in the school, and so on. If you have a dentist appointment, or your talkative relative calls, or American Idol has a two-hour special, you’re tapped out.

In other words, in 5 out of 7 days in a week you spend over 88% of the time doing things you must do, and only 12% of the time doing things you actually want to do. This pattern is how you will spend 70% of your working days. Note that Scott cleverly allocates those precious 3 hours to social endeavors (such as family time, and sex) and necessary chores (shopping, personal errands, etc..). What about the so called “me time”? This schedule does not necessarily include the time for verging out in front of the TV, reading a good book, playing video games, writing a blog, catching up on your backed up RSS feeds (although most people do that at work these days), responding to emails and hacking away on that personal project of yours. If you are planning to do any, or all of that something else is got to give.

Scott claims that there are two ways to subvert this evil cycle, and neither one of them is very good. You either slowly kill yourself by not sleeping and/or exercising, or you take a pay cut and work less hours. For a while now I have been doing option #1, accruing an impressive sleep debt over the years. But I do believe there is a third path.

A wise man once told me that if you find a job you love, you will never have to work again. In fact I think he was channeling Confucius, but never mind that. I think this the key to the third path.

The problem naturally is finding a job that is both rewarding, exciting, challenging, interesting and fulfilling. If you wake up on a Monday morning and you actually don’t mind going to work that much, you have probably found that job. If you feel passionate about what you do and you take a great deal of satisfaction in your work, you are probably already there. If you get to tie in your hobbies, and your personal interest into your professional career you are threading the third path. If you get paid to contribute to your favorite open source project, or to nurse your personal pet project to life under protectorate of your company you are a lucky guy.

I think the third path is all about balance. It’s about taking that 8 hours of daily boredom and turning it into 8 hours of “fun”. It might sound silly, but I believe it is not impossible. And there are companies out there which seem to grok this concept. For example Google, which actually allows it’s developers to allocate portion of their work time to personal side projects. Of course I’m sure that no job is perfect, and that even working at Google has a dark side. I think that environment is just a part of the equation. The other important part is your attitude towards work.

Some people may find fulfillment and happiness wile launching some awesome startup based on some crazy idea they came up with. Others may find it working at a big, progressive company such as Google. Yet another may find personal fulfillment and serenity as a ditch digger.

Naturally, you still only get only 3 hours of personal time on a weekday. That part of equation doesn’t change. What changes is the setup:

Sleep: 8 hours
Exercise: 1 hour
Pursuing your dreams, achieving your goals and having fun: 8 hours
Eating: 2 hours (leisurely)
Hygiene: 1 hour
Travel: 1 (Commute, errands)

How does that look to you? Would you still say your life is slowly trickling through your fingers? Would you burn out as quickly? When you put things this way, the 3 hours of free time are merely just an icing on the cake. Easier said than done - I know. But I guess part of it is probably making choices, taking risks, and not settling for a job that merely “pays the bills”.

So how about you? Which path did you pick? Did you find that dream job yet? Are you looking? Or perhaps you had it and lost it? Share in the comments. :)

Evangelion: 1.0 You Are (Not) Alone

Monday, April 28th, 2008

I finally got my hands on a fansubbed copy of the first installation of the much hyped and much anticipated cinematic re-make of the cult TV series Neon Genesis Evangelion. And no, this is not yet another movie that attempts to translate the original, infuriatingly abstract ending into something more approachable that would give fans a sense of closure. This is a complete re-telling of the story from the very first episode, all the way to the end.

Evangelion: 1.0 You Are (Not) Alone

Rebuild of Evangelion is actually planned as a tetralogy in which the 4th and last part will be a brand new original ending which will differ from both the original, and the movie End of Evangelion.

Why to remake the cult TV series from 13 years ago now? I suppose money is a huge factor. Hollywood has proven time and time again that a glitzy remake of a time honored classic, or a cult title simply cannot fail at the box office. Especially if it is a super, special directors cut edition with never before seen footage. Take the Star Wars: Special Edition - you wouldn’t think that people would pay a ticket admission price to see the same old movie with few CGI shots of wompa rats and Banthas superimposed over the old footage. But they did.

So Rebuild of Evangelion is a little bit like that - same story, but with brand new CGI effects, and vastly improved animation and a new musical score. That and of course blatant product placement:

Product Placement

Naturally, this is not the only reason. There is also merchandising and tie-ins. :mrgreen: But officially speaking, the reasons are a legion. You can read the official statement from Hideaki Anno (the mastermind behind this project) to see them for yourself. In short, he wants to tell the story from the beginning in a way that is more comprehensible, and more approachable to regular audience - and to sort of create the ultimate, directors cut, true version of the story with a definite ending where there previously was none. But mostly, I think it is about the money. ;)

Sachiel on the Prowl

So how is the movie itself? In one word, unavailable in US. Or at least that’s what I summarized after reading the wikipedia entry and a cursory googe search. I could not find any information about US release schedule, or even if one is planned for the future. If you are more in the know than me, please post any and all available info in the comments. For me, this meant that I can proceed to procure a fan-subbed version of the show guilt free via the magical vehicle of bittorrent.

I’d love to give credit to the subber but the video came without an NFO and there are no credits in the subtitle track itself. The only clue I have about the identity of whoever did this was that the file was labeled with the [NF_ITK] tag which is I suppose either a scene group or a fansubbing group - but google didn’t really return any results for them either. The subs are serviceable but not perfect. A lot of the background chatter (especially during the Eva launch sequence) is simply missing. Fortunately I didn’t catch any “mass naked child events” type blunders which is a good thing. That’s as much I can say. I don’t actually know a lick of Japanese so validity of the sub remains to be determined. It seemed ok to me - and I recognized some of the memorable dialogs from the series (eg. the hedgehog’s dilemma and etc..).

vlcsnap-4255742-custom.png

As for the movie itself, I am somewhat underwhelmed. The plot is essentially a condensed version of the first 3 episodes which depict the attacks of the first 3 angels (Sachiel, Shamshel and Ramiel). While all the scenes were re-drawn and animated from scratch, with much more modern style of shading, and much more fluid movement, it seems that the team pretty-much re-used a lot of story boards from the series. Watching You Are (Not) Alone is very much like watching A New Hope: Special Edition. It’s the very same movie with a healthy dose of shiny new CGI and that one new really cool scene that was not in the original. The main difference is that while the Special Edition of Star Wars actually ran slightly longer than the original, the new Eva movie actually tries to condense events that originally spanned 3 hour episodes down into a 90 minute feature. I believe that some of the high school sequences depicting Shinji’s and Rei’s daily life ended up being cut. This means that we are seeing a bit less of our main character moping around and being miserable, but we also miss out on the little interactions that give his relationship with his classmates a bit more depth. It also slightly distorts your sense of time passage. The time between angel attacks seems much shorter now.

vlcsnap-4256086-custom.png

In fact, the whole thing felt a bit rushed. Since I’m familiar with the series, I could easily fill in the blanks, and I knew exactly what was going on at any given time. Perhaps this is due to the fansub quality, but I felt like we were not getting as much info as in the original. Perhaps, they intentionally decided to turn down the level of details and technobabble to meet Hideaki’s goal of increased approachability. If that’s the case, then I would say this was a wrong decision. Such a move would make the movie even less comprehensible to a new viewer. But it is hard to say without actually seeing an official translation. Remember - not a leak of Japanese here. ;)

The new CGI is nice, but I’m not really someone who swoons over graphical fireworks. I saw Evangelion for the first time only few years ago so it was already a dated show. Still, I loved it despite the fact that it did not have flashy 3d effects which are so common in the contemporary anime. The combat is surely more dynamic and exciting, the angels have a new and improved look and you can observe the Tokyo-3 buildings rising out of the ground in painstaking detail. The original series had a tight budget, but this remake seems to be taking the “no expense spared” approach and it shows. Still, to me, visuals are secondary to the story itself. And this is where the movie is slightly underwhelming. Not that the story is bad in itself - after all it is the same store as before, which was excellent to begin with. It’s just that this is part of the problem - a brand new movie, with the same old story only in a more condensed mode. It’s a bit like watching that amateur Phantom Menace cut in which they remove all the stupid Jar Jar Brinks scenes - only this time, actually meaningful stuff gets cut or changed. There are notable exceptions though, which actually add to the story - some very ominous foreshadowing, and interesting tidbits here and there which almost make up for the loss of detail elsewhere.

vlcsnap-4244176-custom.png

What are these exceptions? Well, for one, Rei has nipples this time around. Yeah, I was surprised too. If I can remember correctly, the TV series had all the ladies sporting busts akin to those of a Barbie doll - ie. perfectly smooth featureless orbs. I guess Rei fans will love this upgrade especially since she is showing plenty of skin. But that’s largely irrelevant. There is more.

I spotted several very striking, and important departures from the source. If you don’t want to be spoiled, just scroll down to the very last paragraph of the post real quick and close your eyes not to see the screen shots. Mega spoilers lurk below.

vlcsnap-4256806-custom.png

First minor detail I noticed was that Shinji was actually at one point training in one of the unarmored simulation Eva bodies which were dormant and seemingly abandoned in the original. This is interesting, but not very significant. Yet it shows that they are fleshing out these little details, possibly in order to make the story more cohesive.

There are bigger surprises ahead though. For example, Misato not only knows what lies hidden in the Central Dogma but also has access to it. To convince Shinji to fight Ramiel, she takes him all the way down there, and shows him the crucified angel correctly identifying it as Lilith. In the original Misato did not know about it until it was revealed to her by Kanji near the end of the series. Furthermore, both of them assumed the angel on the cross was Adam not Lilith.

Furthermore, the new Lilith does not have the characteristic, symbolic mask with the 7 eyes. She is now wearing the plain, smooth gray mask similar to those seen on all the other angels. Perhaps this is done to establish her identity as one of the angels more firmly in the eyes of the audience? She is also not bleeding LCL. To that tune I do not recall Shinji ever saying that the LCL smells like blood in this version, but this once again might just be the fansubber dropping the ball. Still, this may suggest that they dropped the idea of LCL being the blood of Lilith.

vlcsnap-4198928-custom.png

Another huge departure from the original story is the scene in which Kaworu wakes up on the surface of the moon which seems to be stained with blood or something like that. He says something cryptic about “the third one” never changing, and expresses that he looks forward to meeting Shinji. In the original this character didn’t show up till the very end of the series and we initially did not know about his un-natural origins. Furthermore he seems to be standing over a body of yet another angel who looks strikingly similar to Lilith, and in fact is wearing a mask that looks very similar to the one Lilith wore in the original. Is that Adam by chance?

vlcsnap-4182997-custom.png

Overall, the movie by itself does not really live up to Hideaki Anno’s promises of being the ultimate anime of all times. It feels much more like watching Star Wars: Special Edition. But it is not all just a condensed carbon copy of the original with shiny new CGI and animated nipples either. The last act of the film seems to suggest that the creators are taking the story in a very different direction this time around. The next 3 movies that will soon follow You Are (Not) Alone are likely to become progressively more original leading up to a brand new and unexpected conclusion. There are also rumored new characters on the horizon that are yet to be introduced. So I suggest adopting a wait and see approach here. I will reserve my final judgment until I see the whole tetralogy in it’s entirety. While this first installment is not totally mind blowing, it seems like a good start. What remains is to hope that someone out there will at some point secure US distribution rights, and do a popper translation so we can all enjoy fully and support the whole project with our dollars without worrying about region codes, and the ever-present language barrier.

URL’s In Printed Media

Friday, April 25th, 2008

I was reading an article in a magazine last week and while I don’t exactly recall the magazine or the topic right now I distinctly remember mild annoyance at the editor’s decision to put list of URL’s at the end of the piece. Not that it’s a bad practice in itself but some of the listed links were actually pointing to Youtube videos. Youtube URL’s are not that long or difficult, but I don’t think they were designed to be used in printed media. For one, they use a cryptic case sensitive argument at the end which is a pain in the ass to type. I mean, look at it - the thing is ugly and awkward to type:

http://www.youtube.com/watch?v=oHg5SJYRHA0

It’s actually easier and quicker to simply google the videos in question by their title rather than even attempt to type the address by hand. It reminds me of the tricky exercise typing Microsoft’s CD product keys during activation. You hardly ever get them right on the first try. Not that I don’t appreciate the gesture but there is this dissonance between print media and electronic media. When you print URL’s you ought to keep their typability (is that a word?) in mind.

A lot of magazines do something different. They simply refer the reader to the online version of the article or specially prepared page on their website which contains the relevant links. This is an improvement, but you still usually end up with a fairly long URL’ which includes stuff like the publication date, the volume number, author’s name or all of the above. Sometimes the damn thing is to long, and they simply print a blurb to visit their site, and leave locating the right page as an exercise to the reader.

If I was distributing my content via printed media and had to cite online resources quite often, I’d probably look into URL shortening instead. I actually haven’t seen this being done on a bigger scale in magazines, but private people do it all the time. For example, Twitter users utilize services such as TinyURL, Snurl, is.gd or Twurl to post long addresses without exhausting the 140 character limit. So instead of a long ass Youtube link with a 10+ character alphanumeric, case sensitive argument you use something like this:

http://is.gd/AJ

Can you see the difference? First URL is extremely easy to mistype. The second one, almost impossible to get wrong.

Naturally, there is a downside to using a 3rd party URL shortening service - especially in print. The URL is no longer meaningful. In my first example, you at least know I’m sending you to a Youtube video. In the second one it’s not that clear. Furthermore, you are routing your visitor traffic through someone else’s service which may or may not be reliable or trustworthy. While you can get away with it in handouts for your class presentation or an informal documentation it is probably not recommended for official brochures, posters or other materials of that nature. It simply doesn’t look professional.

You want to use URL shortening though - just not the generic services open to anyone on the internet. What you ought to do is to roll your own. The added benefit is that you will get the first pickings on the really short 2 and 3 character URL’s which should last you for a while - which is usually not the case with services used by thousands of people.

How do you create your own URL shortening service? One way is to simply grab an off the shelf solution like Get Shorty. It is free, PHP + MySQL based donationware that does precisely what tinyurl and friends do, but on your own server. It actually gives you a choice on how you want to generate your shortened URL’s - it can do the semi-random 3-4 chacacter codes just like all the services out there, or it can prompt you for custom keywords - so you can actually construct your URL like so:

http://mydomain.com/my/keywords/here

This makes for nice readable URL’s that still retain your domain name (for the purpose of quality control, branding and etc..) but seamlessly redirect your users to relevant URL’s while giving them a hint of where they will be going as well.

Alternatively of course you could create your own system. It’s actually not that difficult to do this. First you will need a database. Let’s make it really easy and just use two fields - the ID and URL:

CREATE TABLE shorturl (
	id INT NOT NULL AUTO_INCREMENT, 
	url TEXT, 
	PRIMARY KEY(url));

If you didn’t notice, I just killed two birds with one stone here. The id is set to auto increment so it will generate unique values for us for free. Naturally this is tad limited as you will quickly run out of the nice 1, 2 and 3 character id’s but it’s simple and reliable.

Next, we need some .htaccess magic:

RewriteEngine on
RewriteBase /short/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forward.php [L]

This should forward every link to a non-existent file in the directory /short/ to a script called forward.php. Then inside our script we do the following:

$id = end(explode("/", $_SERVER['REQUEST_URI']));
$result = mysql_query("SELECT id FROM shorturl where url='$id'");
$resultarray = mysql_fetch_array($result);
header("Location:".$resultarray['id']);

The REQUEST_URI gives me the address from which the user was redirected to my forwarding script. Whatever is behind the last “/” is my shortened id value. Once I grab that, I just need to look it up in the database, and then redirect you by sending your browser the “Location” header. Let’s say someone goes to the following URL:

http://mydomain.com/short/123

The script will look up 123 in the database, and then redirect this person to relevant URL that if finds.

Naturally, it will blow up in a very un-graceful way if the id is not found. I’m not really doing any error checking or validation here - I’m just illustrating the concept here. So don’t be copying and pasting this into any production code because it is likely wrong. Also, if it doesn’t work at all, let me know and post corrections because I admit I didn’t really test this. As I’m typing this it’s 2am so give me a break. :P

If you look at the get-shorty code you will note that they do it slightly differently, but I didn’t want to blatantly rip them off. There is clearly more than one way to do it, and you can probably vastly improve my .htaccess code up there.

I really think this is the way to go for print friendly URL’s. It’s easy to implement (I just did it and I’m half asleep) and it gives the reader easy to type shortcut that will take him directly to the resource without unnecessary searching or pit stops along the way.