⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 roomfunc.cs

📁 是一款式CSDN阅读器,可以方便CSDN用户阅读自己感兴趣的内容!
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

using System.Threading;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using feiyun0112.cnblogs.com.CSDNReader.Model;

namespace feiyun0112.cnblogs.com.CSDNReader.Functions
{
    class RoomFunc
    {
        private Room _room = null;
        public Model.Room Room
        {
            get { return _room; }
            set { _room = value; }
        }

        public RoomFunc(Model.Room r)
        {
            this.Room = r;
        }

        public Room AddRoom()
        {
            frmRoom frm =new frmRoom();
            frm.Text = "添加";
            if (DialogResult.OK != frm.ShowDialog())
                return null;

             return frm.Room ;
        }
        public Room EditRoom()
        {
            frmRoom frm = new frmRoom();
            frm.Text = "修改";
            frm.Room = this.Room;
            if (DialogResult.OK != frm.ShowDialog())
                return null;

            return frm.Room;
        }

        public bool Equal(Room r)
        {
            if (this.Room.ID == r.ID)
                return true;

            return false;
        }

        public bool CheckURL(string strURL)
        {
            WEBFunc wFunc = new WEBFunc();
            try
            {                
                string strHTML = "";

                strHTML = wFunc.GetHTML(strURL);

                if (strHTML == "") return false;


                string strRegex = @"<a[^<]*TopicView1.asp[^<]*id=(?<ID>[^<]*)""[^<]*target[^<]*>(?<topic>[^<]*)</a>[^<]*</td>[^<]*<td[^>]*>(?<user>[^<]*)</td>[^<]*<td[^>]*>(?<Points>[^<]*)</td>[^<]*<td[^>]*>(?<Replies>[^<]*)</td>[^<]*<td[^>]*>(?<ReplayTime>[^<]*)</td>";
                
                
                Regex r;
                MatchCollection m;
                r = new Regex(strRegex, RegexOptions.IgnoreCase);
                m = r.Matches(strHTML);

                if (m.Count <= 0) return false;
            }
            catch
            { return false; }
            finally
            {
                wFunc = null;
            }


            return true;
        }

        /// <summary>
        /// 使用多线程读取帖子列表
        /// </summary>
        public void RefreshRoom()
        {

            if (!this.Room.ManuleRefresh && ((TimeSpan)(DateTime.Now - this.Room.RefreshTime)).TotalMinutes < Setting.RefreshMinutes)
            {
                return;
            }
            if (this.Room.threadRefresh != null && this.Room.threadRefresh.ThreadState == ThreadState.Running)
            {
                return;
            }
            if (this.Room.IsGroup)
            {
                //////foreach(Room r in this.Room.ChildRooms)
                //////{
                //////    r.ManuleRefresh = Room.ManuleRefresh;
                //////    RoomFunc rFunc = new RoomFunc(r);
                //////    rFunc.RefreshRoom();
                //////}
            }
            else
            {
                this.Room.RefreshTime = DateTime.Now;
                this.Room.threadRefresh = new Thread(new ThreadStart(RefreshProc));

                this.Room.threadRefresh.Start();
            }

        }

        private void RefreshProc()
        {
            bool blnNoRead = false;
            bool blnCountChanged = false;

            Topic Topic_old1st = null;
            Topic Topic_new1st = null;
            if (this.Room.Topics != null && this.Room.Topics.Count >0 && this.Room.Topics[0] != null)
            {
                Topic_old1st = new TopicFunc(this.Room.Topics[0]).Clone() ;                
            }

            this.Room.Items = -1;
            this.Room.UnreadItems = -1;

            int intOldItems = this.Room.Items;
            int intOldUnreadItems = this.Room.UnreadItems;

            if (Variant.CurrentRoom != null && Variant.CurrentRoom.URL == this.Room.URL)
                Variant.CurrentRoom.ManuleRefresh = true;

            WEBFunc wFunc = new WEBFunc();
            string strHTML = "";
            try
            {
                strHTML = wFunc.GetHTML(this.Room.URL);
            }
            catch
            {
                strHTML = "";
            }
            wFunc = null;

            if (strHTML == "") return;

            if (Variant.CurrentRoom != null && Variant.CurrentRoom.URL == this.Room.URL)
                Variant.CurrentRoom.ManuleRefresh = false;

            //使用正则表达式分析出帖子列表
            //格式为:
                //<a href="/Expert/TopicView1.asp?id=帖子ID" target="_blank">标题</a>
                //</td>
                //<td align="right">用户</td>
                //<td width="30" align="right">分数</td>
                //<td width="30" align="right">回复</td>
                //<td width="80" align="right">时间</td>
            string strRegex = @"<a[^<]*TopicView1.asp[^<]*id=(?<ID>[^<]*)""[^<]*target[^<]*>(?<topic>[^<]*)</a>[^<]*</td>[^<]*<td[^>]*>(?<user>[^<]*)</td>[^<]*<td[^>]*>(?<Points>[^<]*)</td>[^<]*<td[^>]*>(?<Replies>[^<]*)</td>[^<]*<td[^>]*>(?<ReplayTime>[^<]*)</td>";
            Regex r;
            MatchCollection m;
            r = new Regex(strRegex, RegexOptions.IgnoreCase);
            m = r.Matches(strHTML);

            if (m.Count <= 0) return;

            TopicFunc tFunc = new TopicFunc(null);

            List<Topic> OldTopicsClone = new List<Topic>(this.Room.Topics);

            List<Topic> NewTopics = new List<Topic>(m.Count + this.Room.Topics.Count);
            for (int i = 0; i < m.Count; i++)
            {
                //将帖子存入Topic对象
                Topic t = new Topic(this.Room,m[i].Groups["ID"].Value, m[i].Groups["topic"].Value, m[i].Groups["user"].Value, m[i].Groups["Points"].Value, m[i].Groups["Replies"].Value, m[i].Groups["ReplayTime"].Value);
                
                tFunc.Topic = t;

                DelegateFunc.OnOpenedTopic(t.Id);

                Topic topicOld = tFunc.Contains(OldTopicsClone);
                if (topicOld != null)
                {
                    //没有新回复,即从这帖开始后面都是旧帖
                    if ( t.Replies==topicOld.Replies && t.ReplyTime.CompareTo(topicOld.ReplyTime) <= 0 )
                    {
                        break;
                    }

 

                    OldTopicsClone.Remove(topicOld);

                    tFunc.Topic = topicOld;
                    if (tFunc.UpdateTopic(t))
                    {
                        t = topicOld;
                    }                    
                }
                else
                {
 
                }

                tFunc.Topic = t;
                //过滤帖子
                if (tFunc.Filter())
                {
                    NewTopics.Add(t);

                   
                }
                else
                {
   
                }
            }
            
            //旧帖还是显示
            for (int intTopic = 0; intTopic < OldTopicsClone.Count; intTopic++)
            {
                NewTopics.Add(OldTopicsClone[intTopic]);
            }
                       

            this.Room.Topics = NewTopics;
            this.Room.Items = -1;
            this.Room.UnreadItems = -1;

            if (intOldItems != this.Room.Items || intOldUnreadItems != this.Room.UnreadItems)
            {
                blnCountChanged=true;
            }
            if (this.Room.Topics != null && this.Room.Topics.Count > 0 && this.Room.Topics[0] != null)
            {
                Topic_new1st  = this.Room.Topics[0];
            }
            if(Topic_new1st!=null)
            {
                if (Topic_old1st == null)
                {
                    blnNoRead = true;
                }
                else if(!Topic_new1st.HaveRead)
                {
                    if (Topic_old1st.Replies != Topic_new1st.Replies || Topic_old1st.Id != Topic_new1st.Id || Topic_old1st.ReplyTime != Topic_new1st.ReplyTime || Topic_old1st.HaveRead != Topic_new1st.HaveRead)
                    {
                        blnNoRead = true;
                    }
                }
            }

            this.Room.ManuleRefresh = false;

            
            DelegateFunc.OnRefreshTopics(this.Room.ID, blnNoRead, blnCountChanged);
             
        }

        public  Room Clone()
        {
            Model.Room r = new Model.Room();

            r.ChildRooms = this.Room.ChildRooms;
            r.GroupID  = this.Room.GroupID ;
            r.ID  = this.Room.ID ;
            r.IsGroup  = this.Room.IsGroup ;
            r.Items  = this.Room.Items ;
            r.ManuleRefresh  = this.Room.ManuleRefresh ;
            r.Name  = this.Room.Name ;
            r.RefreshTime  = this.Room.RefreshTime ;
            r.Tag  = this.Room.Tag ;
            r.Topics  = this.Room.Topics ;
            r.UnreadItems  = this.Room.UnreadItems ;
            r.URL  = this.Room.URL ;
            return r; 
        }

        public void CheckTopicExist(Topic topic)
        {
            bool  blnExist=false;
            foreach (Topic t in this.Room.Topics)
            {
                if (t.Id ==topic.Id )
                {
                   blnExist=true ;
                    break;
                }
            }
            if (!blnExist)
            {
                this.Room.Topics.Add(topic);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -