URL Buddy

Published 31 October 06 09:38 AM | Coding4Fun 
  This is a utility that allows the user to determine what the URL is so we can concentrate on making fix-ups (incase of broken URL's), if required, and present the link to the user.

Difficulty: Intermediate
Time Required: 1-3 hours
Cost: Free
Software: Visual Studio Express Editions
Hardware:
Download: Download

     

    The ubiquitous URL has been around for what seems like an eternity, yet it doesn’t always find its way to true happiness as a hyperlink. Wrapped in online discussions, not easily parsed and even (gasp!) simply pasted as text into computer-related online publications, the promise of the hyperlink is stopped dead in its tracks as plain text, or even worse: A broken hyperlink. I can’t count the number of times I’ve had to select a URL with my mouse, paste it into my favorite text editor, fix line breaks, remove nonsense characters and the like, and then finally paste the URL into a Web browser. All of this instead of a single click. There must be a better way.

    Figure 1 – URLBuddy watches the clipboard for text strings that resemble URL’s.

    The Problem

    In my experience, two types of broken URLs are the most common:

    • Non-hyperlink URLs. These are everywhere: Web pages, email programs, text files, etc.
      Example: http://msdn.microsoft.com
    • URLs that aren’t correctly parsed. This is a common problem in email programs and content management systems. The content parser fails to correctly match the entire URL and either creates an invalid URL or none at all.
      Simple line wrap: http://msdn.microso/
      ft.com
      Line wrap with quote characters, usually found in email or online discussions:
      > > http://msdn.microso/
      > > ft.com

    The Solution

    The problems above exist because parsing a generic body of text and consistently, correctly pull out URLs is difficult. Some prefer to avoid the problem altogether and provide no links at all instead of potentially broken ones (first example), whereas others give usually correct hyperlinks with a few broken ones as an unfortunate side effect.

    The solution is simple: Let the user determine what the URL is so we can concentrate on making fix-ups, if required, and present the link to the user.

    Requirements

    The utility will:

    • Watch the clipboard for text that looks like a URL.
    • Run in the background in the notification area.
    • Present the user with a clickable “toast”-like form that appears only when a URL has been detected on the clipboard.
    • Display the URL and title of the Web page referred to by the URL on the “toast”.
    • Launch the URL when the “toast” is clicked.
    • Maintain a list of recent URLs between sessions and make them available via context menu.
    • Show the “toast” containing the most-recent URL when the notification icon is double-clicked.
    • Fade-in the “toast” to 80% opacity when a URL is found and 100% when the user hovers over it. Likewise, the “toast” will fade out after a few moments of inactivity or after the user clicks it.

    Implementation

    We will build this solution using Visual Studio 2005 Beta 2, specifically Visual C# Express, and I will point out some features that make Visual Studio 2005 a compelling choice over previous versions. To download Visual C# Express Edition Beta 2, or any of the other Express editions, visit http://msdn.microsoft.com/express.

    Startup

    When the application is launched, an instance of MainForm is created, but not visible, and its associated NotificationIcon is added to the notification area. During load, it uses PInvoke to call a Win32 API function instructing the operating system to notify it when content on the clipboard changes. The Settings are checked for recent URL XML; if it’s found it populates the Recent URLs menu.

    Normal operation

    The application is notified when a user copies something to the clipboard. CheckClipboard is called and determines if a new URL has been placed on the clipboard. If so, if fades in the “toast” and calls SetPageTitle, which downloads the Web page at the URL and attempts to pull out the title tag. If the request is successful and the title is found, it is displayed on the “toast.” If the user clicks on the toast, System.Diagnostics.Process.Start(URL) is called, which launches the user’s default browser and navigates to the URL.

    Shutdown

    The application notifies the operating system that it no longer wants to be notified of clipboard changes and XML serializes the URL/Title information in the UrlInfo objects. The serialized XML is then put into the settings and saved.

    Technology highlights

    Visual Studio and the .NET Framework have made rapid application development a reality, and the new version adds even more goodies to make the process as painless as possible. The first two below are new to Visual Studio 2005 and the third is new to C# 2.0.

    Visual Studio’s new Application Properties settings provide an interface to two very common tasks: User/application settings and resources. What used to be a tedious, but necessary, exercise in nearly every project is now something that can be done quickly so time can be spent where it’s most important: on the problem the software is solving.

    Built-in settings

    The developer can enter setting information directly into Visual Studio, choosing a name, scope (User/Application), type and optional value. Strongly-typed classes are generated that provide programmatic access to the settings, as well as load and save operations that manipulate settings in the user’s Local Settings\Application Data folder.

    When URL Buddy is shut down, it serializes information to XML about recent URLs (stored in UrlInfo objects) and stores the XML in the RecentUrls setting. At application startup, it checks for a value in this setting and re-creates the Recent URLs menu if any data is found.

    Figure 2 – Visual Studio 2005’s new settings manager

    The settings are accessed via the Properties.Settings class in C# and Settings class in Visual Basic.

    Built-in resources

    Resources are handled in much the same way. The developer can add or create all kinds of resources: Images, icons, strings, audio and so on. Visual Studio creates strongly-typed wrapper classes, so accessing the resources is as simple as accessing a property.

    URL Buddy stores the notification icon and the “toast” icon in the resources. The notification icon is assigned at application startup and the “toast” icon is accessed when the form is painted.

    Figure 3 – Visual Studio 2005’s new resource manager

    Resources are accessed via the Properties.Resources class in C# and My.Resources in Visual Basic.NET.

    Anonymous methods

    Anonymous methods are inline methods for delegate targets. They have a number of interesting uses, one of which should be particularly interesting to Windows Forms developers: Forwarding calls in event handlers.

    Classes with method names like exitMenuItem_Click, notifyIcon_DoubleClick, timer_Tick, and so on, aren’t self-descriptive and are more difficult to read and maintain. Many of these methods consist of a line or two of code. Anonymous methods provide a means to consolidate these calls inline. I prefer to put them in a single method and forward the calls to methods with better names (and signatures), if appropriate.

    Visual C#

    private void CreateEventHandlers()
    {
       exitMenuItem.Click += delegate
       {
         Close();
       };
    
       showLastUrlMenuItem.Click += delegate
       {
         ShowForm();
       };
    }

    More information is available on anonymous methods and other new C# features in an article titled Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes by Juval Lowy in the May 2004 issue of MSDN magazine.

    Filed under: ,

    Comment Notification

    If you would like to receive an email when updates are made to this post, please register here

    Subscribe to this post's comments using RSS

    Comments

    # Maru said on May 4, 2007 8:22 AM:

    I've basically been doing nothing worth mentioning. Not much on my mind to speak of. Not that it matters. More or less nothing seems important.

    # South said on May 15, 2007 10:02 PM:

    I haven't been up to anything today. I've more or less been doing nothing to speak of. More or less not much notable going on today. That's how it is.

    # doktor said on May 30, 2007 8:25 PM:

    Today was a complete loss. I feel like a complete blank. I don't care.

    # TIGER said on June 4, 2007 8:39 PM:

    I just don't have much to say. I haven't gotten much done today. Eh. Maybe tomorrow. I can't be bothered with anything recently, but I guess it doesn't bother me.

    # Gorod said on June 9, 2007 9:00 AM:

    I've just been letting everything pass me by lately. I haven't been up to much lately. My life's been dull recently, but that's how it is. I've just been sitting around doing nothing. It's not important. My mind is like a complete blank.

    # dart_bruise said on June 23, 2007 2:50 PM:

    These are all great comments and suggestions (and more are welcome). Thanks. I will follow up as soon as I can.

    Leave a Comment

    (required) 
    (optional)
    (required) 
    Page view tracker