// Copyright 2005 by Omar Al Zabir. All rights are reserved.
//
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
//
// website http://www.oazabir.com, email OmarAlZabir@gmail.com, msn oazabir@hotmail.com
using System;
using System.Diagnostics;
namespace RSSCommon.Helper{
/// <summary>
/// Helps with launching browser
/// </summary>
public class BrowserHelper
{
public static bool IsURLFile( string url )
{
return new Uri( url ).IsFile;
}
public static bool BrowseURL( string url )
{
if( null != url )
{
/*
// Make sure this is not the preview XML file being browsed
if( url.IndexOf( ApplicationSettings.ApplicationDataPath ) > 0 ) return false;
// We are interested to browse only to HTTP URLs
if( !url.StartsWith("http://") && !url.StartsWith("ftp://") && !url.StartsWith("mailto:") ) return false;
*/
using( Process p = Process.Start( new ProcessStartInfo( url ) ) )
{
}
return true;
}
return false;
}
public static string GetDroppedURL(System.Windows.Forms.DragEventArgs e)
{
string hyperLinkUrl = null;
string hyperLinkText = null;
try
{
hyperLinkUrl = e.Data.GetData(typeof(string)) as string;
// some browser deliver url and text
// in UniformResourceLocator (Firebird)
string[] tokens = hyperLinkUrl.Split('\\');
if(tokens.Length>1)
{
hyperLinkUrl = tokens[0];
hyperLinkText = tokens[1];
}
// we have to read FILEGROUPDESCRIPTOR to get the text (IE)
else
{
System.IO.Stream ioStream=
(System.IO.Stream)e.Data.GetData("FileGroupDescriptor");
byte[] contents = new Byte[512];
ioStream.Read(contents,0,512);
ioStream.Close();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//The magic number 76 is the size of that part of the
//FILEGROUPDESCRIPTOR structure before
// the filename starts - cribbed
//from another usenet post.
for (int i=76; contents[i] != 0; i++)
{
sb.Append((char)contents[i]);
}
if (!sb.ToString(sb.Length-4,4).ToLower().Equals(".url"))
{
throw new Exception("filename does not end in '.url'");
}
hyperLinkText = sb.ToString(0,sb.Length-4);
}
// do what ever you wanna do with the hyperlink
return hyperLinkUrl;
}
catch (System.Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
return null;
}
}
public static void DragEnter( System.Windows.Forms.DragEventArgs e )
{
if(e.Data.GetData("UniformResourceLocator",true)!=null)
{
e.Effect = System.Windows.Forms.DragDropEffects.Link;
}
else
{
e.Effect = System.Windows.Forms.DragDropEffects.None;
}
}
}
}
|