OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween / Setting / Panel / NotifyPanel.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) 2014      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 //
10 // This file is part of OpenTween.
11 //
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 #nullable enable
28
29 using System;
30 using System.Collections.Generic;
31 using System.ComponentModel;
32 using System.Drawing;
33 using System.Data;
34 using System.Linq;
35 using System.Text;
36 using System.Windows.Forms;
37 using System.IO;
38
39 namespace OpenTween.Setting.Panel
40 {
41     public partial class NotifyPanel : SettingPanelBase
42     {
43         public NotifyPanel()
44             => this.InitializeComponent();
45
46         public void LoadConfig(SettingCommon settingCommon)
47         {
48             this.ApplyEventNotifyFlag(settingCommon.EventNotifyEnabled, settingCommon.EventNotifyFlag, settingCommon.IsMyEventNotifyFlag);
49             this.CheckForceEventNotify.Checked = settingCommon.ForceEventNotify;
50             this.CheckFavEventUnread.Checked = settingCommon.FavEventUnread;
51
52             this.SoundFileListup();
53
54             var soundFile = settingCommon.EventSoundFile ?? "";
55             var soundFileIdx = this.ComboBoxEventNotifySound.Items.IndexOf(soundFile);
56             if (soundFileIdx != -1)
57                 this.ComboBoxEventNotifySound.SelectedIndex = soundFileIdx;
58
59             this.IsRemoveSameFavEventCheckBox.Checked = settingCommon.IsRemoveSameEvent;
60         }
61
62         public void SaveConfig(SettingCommon settingCommon)
63         {
64             settingCommon.EventNotifyEnabled = this.CheckEventNotify.Checked;
65             this.GetEventNotifyFlag(ref settingCommon.EventNotifyFlag, ref settingCommon.IsMyEventNotifyFlag);
66             settingCommon.ForceEventNotify = this.CheckForceEventNotify.Checked;
67             settingCommon.FavEventUnread = this.CheckFavEventUnread.Checked;
68             settingCommon.EventSoundFile = (string)this.ComboBoxEventNotifySound.SelectedItem;
69             settingCommon.IsRemoveSameEvent = this.IsRemoveSameFavEventCheckBox.Checked;
70         }
71
72         private void SoundFileListup()
73         {
74             this.ComboBoxEventNotifySound.Items.Clear();
75             this.ComboBoxEventNotifySound.Items.Add("");
76             var oDir = new DirectoryInfo(Application.StartupPath + Path.DirectorySeparatorChar);
77             if (Directory.Exists(Path.Combine(Application.StartupPath, "Sounds")))
78             {
79                 oDir = oDir.GetDirectories("Sounds")[0];
80             }
81             foreach (var oFile in oDir.GetFiles("*.wav"))
82             {
83                 this.ComboBoxEventNotifySound.Items.Add(oFile.Name);
84             }
85         }
86
87         private class EventCheckboxTblElement
88         {
89             public CheckBox CheckBox = null!;
90             public MyCommon.EVENTTYPE Type;
91         }
92
93         private EventCheckboxTblElement[] GetEventCheckboxTable()
94         {
95             return new[]
96             {
97                 new EventCheckboxTblElement
98                 {
99                     CheckBox = this.CheckFavoritesEvent,
100                     Type = MyCommon.EVENTTYPE.Favorite,
101                 },
102                 new EventCheckboxTblElement
103                 {
104                     CheckBox = this.CheckUnfavoritesEvent,
105                     Type = MyCommon.EVENTTYPE.Unfavorite,
106                 },
107                 new EventCheckboxTblElement
108                 {
109                     CheckBox = this.CheckFollowEvent,
110                     Type = MyCommon.EVENTTYPE.Follow,
111                 },
112                 new EventCheckboxTblElement
113                 {
114                     CheckBox = this.CheckListMemberAddedEvent,
115                     Type = MyCommon.EVENTTYPE.ListMemberAdded,
116                 },
117                 new EventCheckboxTblElement
118                 {
119                     CheckBox = this.CheckListMemberRemovedEvent,
120                     Type = MyCommon.EVENTTYPE.ListMemberRemoved,
121                 },
122                 new EventCheckboxTblElement
123                 {
124                     CheckBox = this.CheckBlockEvent,
125                     Type = MyCommon.EVENTTYPE.Block,
126                 },
127                 new EventCheckboxTblElement
128                 {
129                     CheckBox = this.CheckUserUpdateEvent,
130                     Type = MyCommon.EVENTTYPE.UserUpdate,
131                 },
132                 new EventCheckboxTblElement
133                 {
134                     CheckBox = this.CheckListCreatedEvent,
135                     Type = MyCommon.EVENTTYPE.ListCreated,
136                 },
137                 new EventCheckboxTblElement
138                 {
139                     CheckBox = this.CheckQuotedTweetEvent,
140                     Type = MyCommon.EVENTTYPE.QuotedTweet,
141                 },
142                 new EventCheckboxTblElement
143                 {
144                     CheckBox = this.CheckRetweetEvent,
145                     Type = MyCommon.EVENTTYPE.Retweet,
146                 },
147             };
148         }
149
150         private void GetEventNotifyFlag(ref MyCommon.EVENTTYPE eventnotifyflag, ref MyCommon.EVENTTYPE isMyeventnotifyflag)
151         {
152             var evt = MyCommon.EVENTTYPE.None;
153             var myevt = MyCommon.EVENTTYPE.None;
154
155             foreach (var tbl in GetEventCheckboxTable())
156             {
157                 switch (tbl.CheckBox.CheckState)
158                 {
159                     case CheckState.Checked:
160                         evt |= tbl.Type;
161                         myevt |= tbl.Type;
162                         break;
163                     case CheckState.Indeterminate:
164                         evt |= tbl.Type;
165                         break;
166                     case CheckState.Unchecked:
167                         break;
168                 }
169             }
170             eventnotifyflag = evt;
171             isMyeventnotifyflag = myevt;
172         }
173
174         private void ApplyEventNotifyFlag(bool rootEnabled, MyCommon.EVENTTYPE eventnotifyflag, MyCommon.EVENTTYPE isMyeventnotifyflag)
175         {
176             var evt = eventnotifyflag;
177             var myevt = isMyeventnotifyflag;
178
179             this.CheckEventNotify.Checked = rootEnabled;
180
181             foreach (var tbl in GetEventCheckboxTable())
182             {
183                 if ((evt & tbl.Type) != 0)
184                 {
185                     if ((myevt & tbl.Type) != 0)
186                     {
187                         tbl.CheckBox.CheckState = CheckState.Checked;
188                     }
189                     else
190                     {
191                         tbl.CheckBox.CheckState = CheckState.Indeterminate;
192                     }
193                 }
194                 else
195                 {
196                     tbl.CheckBox.CheckState = CheckState.Unchecked;
197                 }
198                 tbl.CheckBox.Enabled = rootEnabled;
199             }
200         }
201
202         private void CheckEventNotify_CheckedChanged(object sender, EventArgs e)
203         {
204             foreach (var tbl in this.GetEventCheckboxTable())
205             {
206                 tbl.CheckBox.Enabled = this.CheckEventNotify.Checked;
207             }
208         }
209     }
210 }