I have a Toshiba laptop and use Ubuntu primarily. Though the current Linux kernel has very nice support for my hardware, it still isn't too intelligent about when I'm using a docking station or when I'm not.
The only differences of my setup while docked is that when I have my HDMI monitor plugged in to my laptop, I want both sound and video to go through it. I also want my resolution to be adjusted. This script, assigned to a hotkey for convenience, takes care of this for me.
The general logic flow is:
Granted, Ubuntu usually does a pretty good job of setting the display without me telling it to. I found the trick is, with HDMI connected, use the Displays control panel to set the display to HDMI and the appropriate resolution. From there it will always pick up that setting when HDMI is connected, and degrade gracefully to my LCD screen (and proper resolution) when I disconnect HDMI. The audio doesn't switch over though, so I still have use for this script from time to time.