/[RomCheater]/trunk/RomCheater.Core/IWebBrowserProvider.cs
ViewVC logotype

Annotation of /trunk/RomCheater.Core/IWebBrowserProvider.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 733 - (hide annotations) (download)
Wed Jun 19 15:32:53 2013 UTC (7 years, 10 months ago) by william
File size: 4429 byte(s)
+ if no web browser provider is defined, return an EmptyProvider which uses the default system browser

1 william 696 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.Windows.Forms;
6 william 697 using RomCheater.Logging;
7 william 696
8     namespace RomCheater.Core
9     {
10     public interface IWebBrowserProvider
11     {
12     IWebBrowserInterface GetProvider();
13     }
14     public interface IWebBrowserInterface
15 william 697 {
16 william 696 void Navigate(string urlString);
17     void Navigate(Uri url);
18     }
19    
20     public class EmptyWebBrowserProvider : BaseWebBrowserProvider, IWebBrowserInterface
21     {
22 william 733 private WebBrowserProvider BackingProvider = new WebBrowserProvider();
23     public EmptyWebBrowserProvider()
24     : base()
25 william 696 {
26 william 733 this.BackingProvider = new WebBrowserProvider(this);
27 william 696 }
28 william 733 public IWebBrowserInterface Provider
29     {
30     get { return this.BackingProvider; }
31     }
32    
33     #region IWebBrowserInterface Members
34     public void Navigate(string urlString)
35     {
36     ////this.Provider.Navigate(urlString);
37     //this.webBrowser.Navigate(urlString);
38     System.Diagnostics.Process.Start(urlString);
39     }
40     public void Navigate(Uri url) { Navigate(url.ToString()); }
41     #endregion
42 william 696 }
43    
44     public class WebBrowserProvider : BaseWebBrowserProvider, IWebBrowserInterface
45     {
46 william 733 public static IWebBrowserInterface DefaultProvider { get { return WebBrowserProvider.EmptyProvider; } }
47     public static IWebBrowserInterface EmptyProvider { get { return WebBrowserProvider.Empty; } }
48     public static IWebBrowserInterface Empty { get { return new EmptyWebBrowserProvider(); } }
49 william 699 public WebBrowserProvider() { }
50     //public WebBrowserProvider(IWebBrowserInterface provider) { }
51     public WebBrowserProvider(IWebBrowserInterface provider)
52 william 696 : base(provider)
53     {
54     }
55     }
56    
57 william 699 public abstract class BaseWebBrowserProvider : IWebBrowserInterface, IWebBrowserProvider
58 william 696 {
59 william 699
60     public BaseWebBrowserProvider() { }
61     protected BaseWebBrowserProvider(IWebBrowserInterface provider)
62 william 696 {
63     this.Provider = provider;
64     }
65     #region IWebBrowserInterface Members
66 william 699 private IWebBrowserInterface _Provider;
67     public IWebBrowserInterface Provider { get { return _Provider; } protected set { _Provider = value; } }
68 william 696
69     #region Extensions to WebBrowser provider
70 william 697 public void Navigate(string urlString) { NavigateInternal(urlString); }
71 william 699 public void Navigate(Uri url) { NavigateInternal(url.ToString()); }
72 william 697
73     private void NavigateInternal(string url)
74     {
75     try
76     {
77 william 698 try
78     {
79     Uri uri = new Uri(url);
80     this.Provider.Navigate(uri);
81     }
82     catch (UriFormatException)
83     {
84     try
85     {
86     UriBuilder builder = new UriBuilder(url);
87     var proto = builder.Uri.Scheme;
88     this.Provider.Navigate(builder.Uri);
89     }
90     catch (Exception ex)
91     {
92     throw ex;
93     }
94     }
95 william 697 }
96     catch (UriFormatException ex)
97     {
98     MessageBox.Show(string.Format("Could not navigate to url: '{0}'", url), string.Format("A Uri Format Exception Occurred in '{0}'", this.GetType().Name), MessageBoxButtons.OK, MessageBoxIcon.Error);
99     logger.Error.WriteLine("Unable to navigate to url: '{0}'", url);
100     logger.VerboseError.WriteLine(ex.ToString());
101     }
102 william 698 catch (Exception ex)
103     {
104     MessageBox.Show(string.Format("Could not navigate to url: '{0}'", url), string.Format("An {0} Exception Occurred in '{1}'",ex.GetType().Name, this.GetType().Name), MessageBoxButtons.OK, MessageBoxIcon.Error);
105     logger.Error.WriteLine("Unable to navigate to url: '{0}'", url);
106     logger.VerboseError.WriteLine(ex.ToString());
107     }
108 william 697 }
109    
110 william 696 #endregion
111     #endregion
112    
113     #region IWebBrowserProvider Members
114     public IWebBrowserInterface GetProvider() { return (IWebBrowserInterface)this; }
115     #endregion
116     }
117    
118     }

  ViewVC Help
Powered by ViewVC 1.1.22