OSDN Git Service

読み取り専用のフィールドにreadonlyを追加する (IDE0044)
[opentween/open-tween.git] / OpenTween / Models / FilterTabModel.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 //           (c) 2012      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
10 //
11 // This file is part of OpenTween.
12 //
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
16 // any later version.
17 //
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
27
28 using System;
29 using System.Collections.Generic;
30 using System.ComponentModel;
31 using System.Linq;
32 using System.Text;
33 using System.Threading.Tasks;
34
35 namespace OpenTween.Models
36 {
37     public class FilterTabModel : TabModel
38     {
39         public override MyCommon.TabUsageType TabType
40             => MyCommon.TabUsageType.UserDefined;
41
42         public bool FilterModified { get; set; }
43
44         private readonly List<PostFilterRule> _filters = new List<PostFilterRule>();
45         private readonly object lockObjFilters = new object();
46
47         public FilterTabModel(string tabName) : base(tabName)
48         {
49         }
50
51         //フィルタに合致したら追加
52         public MyCommon.HITRESULT AddFiltered(PostClass post, bool immediately = false)
53         {
54             if (this.IsInnerStorageTabType)
55                 return MyCommon.HITRESULT.None;
56
57             var rslt = MyCommon.HITRESULT.None;
58
59             //全フィルタ評価(優先順位あり)
60             lock (this.lockObjFilters)
61             {
62                 foreach (var ft in _filters)
63                 {
64                     try
65                     {
66                         switch (ft.ExecFilter(post))   //フィルタクラスでヒット判定
67                         {
68                             case MyCommon.HITRESULT.None:
69                                 break;
70                             case MyCommon.HITRESULT.Copy:
71                                 if (rslt != MyCommon.HITRESULT.CopyAndMark) rslt = MyCommon.HITRESULT.Copy;
72                                 break;
73                             case MyCommon.HITRESULT.CopyAndMark:
74                                 rslt = MyCommon.HITRESULT.CopyAndMark;
75                                 break;
76                             case MyCommon.HITRESULT.Move:
77                                 rslt = MyCommon.HITRESULT.Move;
78                                 break;
79                             case MyCommon.HITRESULT.Exclude:
80                                 rslt = MyCommon.HITRESULT.Exclude;
81                                 goto exit_for;
82                         }
83                     }
84                     catch (NullReferenceException)
85                     {
86                         // ExecFilterでNullRef出る場合あり。暫定対応
87                         MyCommon.TraceOut("ExecFilterでNullRef: " + ft);
88                         rslt = MyCommon.HITRESULT.None;
89                     }
90                 }
91                 exit_for:
92                 ;
93             }
94
95             if (this.TabType != MyCommon.TabUsageType.Mute &&
96                 rslt != MyCommon.HITRESULT.None && rslt != MyCommon.HITRESULT.Exclude)
97             {
98                 if (immediately)
99                     this.AddPostImmediately(post.StatusId, post.IsRead);
100                 else
101                     this.AddPostQueue(post);
102             }
103
104             return rslt; //マーク付けは呼び出し元で行うこと
105         }
106
107
108         public PostFilterRule[] GetFilters()
109         {
110             lock (this.lockObjFilters)
111             {
112                 return _filters.ToArray();
113             }
114         }
115
116         public void RemoveFilter(PostFilterRule filter)
117         {
118             lock (this.lockObjFilters)
119             {
120                 _filters.Remove(filter);
121                 filter.PropertyChanged -= this.OnFilterModified;
122                 this.FilterModified = true;
123             }
124         }
125
126         public bool AddFilter(PostFilterRule filter)
127         {
128             lock (this.lockObjFilters)
129             {
130                 if (_filters.Contains(filter)) return false;
131                 filter.PropertyChanged += this.OnFilterModified;
132                 _filters.Add(filter);
133                 this.FilterModified = true;
134                 return true;
135             }
136         }
137
138         private void OnFilterModified(object sender, PropertyChangedEventArgs e)
139             => this.FilterModified = true;
140
141         public PostFilterRule[] FilterArray
142         {
143             get
144             {
145                 lock (this.lockObjFilters)
146                 {
147                     return _filters.ToArray();
148                 }
149             }
150             set
151             {
152                 lock (this.lockObjFilters)
153                 {
154                     foreach (var oldFilter in this._filters)
155                     {
156                         oldFilter.PropertyChanged -= this.OnFilterModified;
157                     }
158
159                     this._filters.Clear();
160                     this.FilterModified = true;
161
162                     foreach (var newFilter in value)
163                     {
164                         _filters.Add(newFilter);
165                         newFilter.PropertyChanged += this.OnFilterModified;
166                     }
167                 }
168             }
169         }
170
171         public override Task RefreshAsync(Twitter tw, bool backward, bool startup, IProgress<string> progress)
172         {
173             var homeTab = TabInformations.GetInstance().GetTabByType<HomeTabModel>();
174
175             return homeTab.RefreshAsync(tw, backward, startup, progress);
176         }
177     }
178 }