www.pudn.com > Fetion.rar > ChannelManagerImp.cs


namespace Imps.Client.Pc 
{ 
    using Imps.Client.Base; 
    using Imps.Client.Core; 
    using Imps.Client.Pc.BizControls; 
    using Imps.Client.Pc.Channel; 
    using Imps.Client.Utils; 
    using Imps.Utils; 
    using System; 
    using System.Collections.Generic; 
    using System.Drawing; 
    using System.IO; 
    using System.Net; 
    using System.Reflection; 
    using System.Runtime.CompilerServices; 
    using System.Text; 
    using System.Threading; 
    using System.Windows.Forms; 
    using System.Xml; 
 
    public class ChannelManagerImp : IChannelManager 
    { 
        private Dictionary _cachedChannels; 
        internal Queue _downloadLogoQueue; 
        private IFrameworkWindow _frameworkWnd; 
        private ChannelItemCollection _items; 
        private static object _syncObjChannels = new object(); 
        private const string GetChannelCode = "{0}?ClientType={1}&Province={2}&OEMTag={3}"; 
        private const string SsiCookieKey = "ssic"; 
        private static Imps.Client.Pc.BizControls.WebBrowserContainer webBrowser; 
 
        public event EventHandler OnChannelLogoUpdated; 
 
        public event EventHandler OnChannelRetrived; 
 
        public ChannelManagerImp(IFrameworkWindow framework) 
        { 
            this._frameworkWnd = framework; 
            this._downloadLogoQueue = new Queue(); 
            this._items = new ChannelItemCollection(); 
        } 
 
        public IChannel CreateChannel(ChannelItem oi) 
        { 
            IChannel channel = null; 
            if (this.CachedChannels.ContainsKey(oi.ChannelType)) 
            { 
                channel = this.CachedChannels[oi.ChannelType]; 
                channel.Reset(); 
            } 
            else 
            { 
                ConstructorInfo constructor = oi.ChannelType.GetConstructor(new System.Type[] { typeof(IFrameworkWindow) }); 
                if (constructor == null) 
                { 
                    channel = (IChannel) Activator.CreateInstance(oi.ChannelType); 
                } 
                else 
                { 
                    channel = (IChannel) constructor.Invoke(new object[] { this._frameworkWnd }); 
                } 
                InitChannelControl(channel.ChannelControl); 
                this.CachedChannels.Add(oi.ChannelType, channel); 
            } 
            if (channel is WebPageChannel) 
            { 
                ((WebPageChannel) channel).InitUrl = oi.StrUri; 
            } 
            return channel; 
        } 
 
        private void DownLoadChannelLogos(ChannelManagerImp cmi) 
        { 
            Queue queue = cmi._downloadLogoQueue; 
            if (queue != null) 
            { 
                while (queue.Count > 0) 
                { 
                    ChannelItem sender = queue.Dequeue(); 
                    sender.TryCounter--; 
                    Image image = this.GetImage(sender.LogoUrl); 
                    Image image2 = this.GetImage(sender.LogoHoverUrl); 
                    if (image != null) 
                    { 
                        sender.Logo = image; 
                        sender.LogoHover = (image2 == null) ? image : image2; 
                        if (this.OnChannelLogoUpdated != null) 
                        { 
                            FuncDispatcher.InvokeEventHandlerInUiThread(sender, this.OnChannelLogoUpdated, EventArgs.Empty); 
                        } 
                    } 
                    else 
                    { 
                        if (image2 != null) 
                        { 
                            image2.Dispose(); 
                        } 
                        if (sender.TryCounter > 0) 
                        { 
                            queue.Enqueue(sender); 
                        } 
                    } 
                    Thread.Sleep(200); 
                } 
            } 
        } 
 
        public void GetChannels(object states) 
        { 
            lock (_syncObjChannels) 
            { 
                this._items.Clear(); 
                ChannelManagerImp sender = states as ChannelManagerImp; 
                if (sender != null) 
                { 
                    string str = string.Empty; 
                    string uriString = string.Format("{0}?ClientType={1}&Province={2}&OEMTag={3}", new object[] { this.User.Configuration.SystemSetting.ServerUriSetting.UriGetTabInfo, "PC", HttpWebRequestHelper.UrlEncode((string) this.User.PersonalInfo.Province), HttpWebRequestHelper.UrlEncode(OemSetting.Instance.OemTag) }); 
                    int num = 3; 
                    while (num > 0) 
                    { 
                        num--; 
                        try 
                        { 
                            string host = new Uri(uriString).Host; 
                            ClientLogger.WriteGeneral("获取频道信息", uriString, 0); 
                            HttpWebRequest request = ConnectionFactory.CreateHttpWebRequest(uriString, null, false, false); 
                            if (request.CookieContainer == null) 
                            { 
                                request.CookieContainer = new CookieContainer(); 
                            } 
                            request.CookieContainer.Add(new Cookie("ssic", this.User.SsiCredential, "/", host)); 
                            using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) 
                            { 
                                using (Stream stream = response.GetResponseStream()) 
                                { 
                                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) 
                                    { 
                                        str = reader.ReadToEnd(); 
                                    } 
                                } 
                            } 
                            break; 
                        } 
                        catch (WebException exception) 
                        { 
                            ClientLogger.WriteGeneral("获取频道信息失败!", exception.ToString(), 20); 
                            this._downloadLogoQueue.Clear(); 
                        } 
                        catch (Exception exception2) 
                        { 
                            ClientLogger.WriteGeneral("获取频道信息失败!", exception2.ToString(), 20); 
                            this._downloadLogoQueue.Clear(); 
                        } 
                        Thread.Sleep(0x7d0); 
                    } 
                    if (!string.IsNullOrEmpty(str)) 
                    { 
                        try 
                        { 
                            XmlDocument document = new XmlDocument(); 
                            document.LoadXml(str); 
                            foreach (XmlNode node2 in document.DocumentElement.SelectSingleNode("tabs").ChildNodes) 
                            { 
                                if (!(node2 is XmlComment)) 
                                { 
                                    string name = node2.Attributes["id"].Value; 
                                    string text1 = node2.Attributes["uri"].Value; 
                                    ChannelItem item = new ChannelItem(this, name, typeof(WebPageChannel)); 
                                    foreach (XmlAttribute attribute in node2.Attributes) 
                                    { 
                                        switch (attribute.Name) 
                                        { 
                                            case "id": 
                                            { 
                                                item.ID = attribute.Value; 
                                                continue; 
                                            } 
                                            case "text": 
                                            { 
                                                item.Text = (string) new ProposedData(attribute.Value); 
                                                continue; 
                                            } 
                                            case "tip": 
                                            { 
                                                item.Tip = attribute.Value; 
                                                continue; 
                                            } 
                                            case "desc": 
                                            { 
                                                item.Description = (string) new ProposedData(attribute.Value); 
                                                continue; 
                                            } 
                                            case "uri": 
                                            { 
                                                item.StrUri = attribute.Value; 
                                                continue; 
                                            } 
                                            case "ordinal": 
                                            { 
                                                item.Ordinal = attribute.Value; 
                                                continue; 
                                            } 
                                            case "logo": 
                                            { 
                                                item.LogoUrl = attribute.Value; 
                                                continue; 
                                            } 
                                            case "logo-hover": 
                                            { 
                                                item.LogoHoverUrl = attribute.Value; 
                                                continue; 
                                            } 
                                            case "ex-args": 
                                            { 
                                                item.InitArgs = attribute.Value; 
                                                continue; 
                                            } 
                                        } 
                                    } 
                                    sender.Items.Add(item); 
                                    this._downloadLogoQueue.Enqueue(item); 
                                } 
                            } 
                            if (this.OnChannelRetrived != null) 
                            { 
                                FuncDispatcher.InvokeEventHandlerInUiThread(sender, this.OnChannelRetrived, EventArgs.Empty); 
                            } 
                            this.DownLoadChannelLogos(sender); 
                        } 
                        catch (XmlException exception3) 
                        { 
                            ClientLogger.WriteGeneral("获取频道信息失败!", string.Format("XmlException:\r\n{0}\r\nRaw Html Body:\r\n{1}", exception3.ToString(), str), 20); 
                            this._downloadLogoQueue.Clear(); 
                        } 
                        catch (Exception exception4) 
                        { 
                            ClientLogger.WriteGeneral("获取频道信息失败!", exception4.ToString(), 20); 
                            this._downloadLogoQueue.Clear(); 
                        } 
                    } 
                } 
            } 
        } 
 
        public void GetChannelsFromServer() 
        { 
            ThreadPool.QueueUserWorkItem(new WaitCallback(this.GetChannels), this); 
        } 
 
        private Image GetImage(string url) 
        { 
            if (!string.IsNullOrEmpty(url)) 
            { 
                try 
                { 
                    using (HttpWebResponse response = (HttpWebResponse) ConnectionFactory.CreateHttpWebRequest(url, null, false, false).GetResponse()) 
                    { 
                        return Image.FromStream(response.GetResponseStream()); 
                    } 
                } 
                catch (WebException exception) 
                { 
                    ClientLogger.WriteGeneral("下载频道图片过程中出现错误", exception.ToString()); 
                } 
                catch (Exception exception2) 
                { 
                    ClientLogger.WriteGeneral("下载频道图片过程中出现错误", exception2.ToString()); 
                } 
            } 
            return null; 
        } 
 
        private static void InitChannelControl(Control ctrl) 
        { 
            ctrl.Location = new System.Drawing.Point(0, 0); 
            ctrl.Dock = DockStyle.Fill; 
            ctrl.Visible = true; 
        } 
 
        private Dictionary CachedChannels 
        { 
            get 
            { 
                if (this._cachedChannels == null) 
                { 
                    this._cachedChannels = new Dictionary(2); 
                } 
                return this._cachedChannels; 
            } 
        } 
 
        public static Imps.Client.Pc.BizControls.WebBrowserContainer ChannelWebBrowser 
        { 
            get 
            { 
                if ((webBrowser != null) && !webBrowser.IsHandleCreated) 
                { 
                    webBrowser = null; 
                } 
                if (webBrowser == null) 
                { 
                    webBrowser = new Imps.Client.Pc.BizControls.WebBrowserContainer(); 
                    webBrowser.Dock = DockStyle.Fill; 
                    webBrowser.MinimumSize = new Size(20, 20); 
                    webBrowser.Name = "webBrowser"; 
                } 
                return webBrowser; 
            } 
        } 
 
        public ChannelItemCollection Items 
        { 
            get 
            { 
                return this._items; 
            } 
        } 
 
        public Imps.Client.Core.User User 
        { 
            get 
            { 
                return this._frameworkWnd.AccountManager.CurrentUser; 
            } 
        } 
    } 
}