OSDN Git Service

複合代入演算子を使用する (IDE0054)
[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 using System;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Drawing;
31 using System.Data;
32 using System.Linq;
33 using System.Text;
34 using System.Windows.Forms;
35 using System.IO;
36
37 namespace OpenTween.Setting.Panel
38 {
39     public partial class NotifyPanel : SettingPanelBase
40     {
41         public NotifyPanel()
42             => this.InitializeComponent();
43
44         public void LoadConfig(SettingCommon settingCommon)
45         {
46             this.ApplyEventNotifyFlag(settingCommon.EventNotifyEnabled, settingCommon.EventNotifyFlag, settingCommon.IsMyEventNotifyFlag);
47             this.CheckForceEventNotify.Checked = settingCommon.ForceEventNotify;
48             this.CheckFavEventUnread.Checked = settingCommon.FavEventUnread;
49
50             this.SoundFileListup();
51
52             var soundFile = settingCommon.EventSoundFile ?? "";
53             var soundFileIdx = this.ComboBoxEventNotifySound.Items.IndexOf(soundFile);
54             if (soundFileIdx != -1)
55                 this.ComboBoxEventNotifySound.SelectedIndex = soundFileIdx;
56
57             this.IsRemoveSameFavEventCheckBox.Checked = settingCommon.IsRemoveSameEvent;
58         }
59
60         public void SaveConfig(SettingCommon settingCommon)
61         {
62             settingCommon.EventNotifyEnabled = this.CheckEventNotify.Checked;
63             this.GetEventNotifyFlag(ref settingCommon.EventNotifyFlag, ref settingCommon.IsMyEventNotifyFlag);
64             settingCommon.ForceEventNotify = this.CheckForceEventNotify.Checked;
65             settingCommon.FavEventUnread = this.CheckFavEventUnread.Checked;
66             settingCommon.EventSoundFile = (string)this.ComboBoxEventNotifySound.SelectedItem;
67             settingCommon.IsRemoveSameEvent = this.IsRemoveSameFavEventCheckBox.Checked;
68         }
69
70         private void SoundFileListup()
71         {
72             this.ComboBoxEventNotifySound.Items.Clear();
73             this.ComboBoxEventNotifySound.Items.Add("");
74             DirectoryInfo oDir = new DirectoryInfo(Application.StartupPath + Path.DirectorySeparatorChar);
75             if (Directory.Exists(Path.Combine(Application.StartupPath, "Sounds")))
76             {
77                 oDir = oDir.GetDirectories("Sounds")[0];
78             }
79             foreach (FileInfo oFile in oDir.GetFiles("*.wav"))
80             {
81                 this.ComboBoxEventNotifySound.Items.Add(oFile.Name);
82             }
83         }
84
85         private class EventCheckboxTblElement
86         {
87             public CheckBox CheckBox;
88             public MyCommon.EVENTTYPE Type;
89         }
90
91         private EventCheckboxTblElement[] GetEventCheckboxTable()
92         {
93             return new[]
94             {
95                 new EventCheckboxTblElement
96                 {
97                     CheckBox = this.CheckFavoritesEvent,
98                     Type = MyCommon.EVENTTYPE.Favorite,
99                 },
100                 new EventCheckboxTblElement
101                 {
102                     CheckBox = this.CheckUnfavoritesEvent,
103                     Type = MyCommon.EVENTTYPE.Unfavorite,
104                 },
105                 new EventCheckboxTblElement
106                 {
107                     CheckBox = this.CheckFollowEvent,
108                     Type = MyCommon.EVENTTYPE.Follow,
109                 },
110                 new EventCheckboxTblElement
111                 {
112                     CheckBox = this.CheckListMemberAddedEvent,
113                     Type = MyCommon.EVENTTYPE.ListMemberAdded,
114                 },
115                 new EventCheckboxTblElement
116                 {
117                     CheckBox = this.CheckListMemberRemovedEvent,
118                     Type = MyCommon.EVENTTYPE.ListMemberRemoved,
119                 },
120                 new EventCheckboxTblElement
121                 {
122                     CheckBox = this.CheckBlockEvent,
123                     Type = MyCommon.EVENTTYPE.Block,
124                 },
125                 new EventCheckboxTblElement
126                 {
127                     CheckBox = this.CheckUserUpdateEvent,
128                     Type = MyCommon.EVENTTYPE.UserUpdate,
129                 },
130                 new EventCheckboxTblElement
131                 {
132                     CheckBox = this.CheckListCreatedEvent,
133                     Type = MyCommon.EVENTTYPE.ListCreated,
134                 },
135                 new EventCheckboxTblElement
136                 {
137                     CheckBox = this.CheckQuotedTweetEvent,
138                     Type = MyCommon.EVENTTYPE.QuotedTweet,
139                 },
140                 new EventCheckboxTblElement
141                 {
142                     CheckBox = this.CheckRetweetEvent,
143                     Type = MyCommon.EVENTTYPE.Retweet,
144                 },
145             };
146         }
147
148         private void GetEventNotifyFlag(ref MyCommon.EVENTTYPE eventnotifyflag, ref MyCommon.EVENTTYPE isMyeventnotifyflag)
149         {
150             MyCommon.EVENTTYPE evt = MyCommon.EVENTTYPE.None;
151             MyCommon.EVENTTYPE myevt = MyCommon.EVENTTYPE.None;
152
153             foreach (EventCheckboxTblElement tbl in GetEventCheckboxTable())
154             {
155                 switch (tbl.CheckBox.CheckState)
156                 {
157                     case CheckState.Checked:
158                         evt |= tbl.Type;
159                         myevt |= tbl.Type;
160                         break;
161                     case CheckState.Indeterminate:
162                         evt |= tbl.Type;
163                         break;
164                     case CheckState.Unchecked:
165                         break;
166                 }
167             }
168             eventnotifyflag = evt;
169             isMyeventnotifyflag = myevt;
170         }
171
172         private void ApplyEventNotifyFlag(bool rootEnabled, MyCommon.EVENTTYPE eventnotifyflag, MyCommon.EVENTTYPE isMyeventnotifyflag)
173         {
174             MyCommon.EVENTTYPE evt = eventnotifyflag;
175             MyCommon.EVENTTYPE myevt = isMyeventnotifyflag;
176
177             this.CheckEventNotify.Checked = rootEnabled;
178
179             foreach (EventCheckboxTblElement tbl in GetEventCheckboxTable())
180             {
181                 if ((evt & tbl.Type) != 0)
182                 {
183                     if ((myevt & tbl.Type) != 0)
184                     {
185                         tbl.CheckBox.CheckState = CheckState.Checked;
186                     }
187                     else
188                     {
189                         tbl.CheckBox.CheckState = CheckState.Indeterminate;
190                     }
191                 }
192                 else
193                 {
194                     tbl.CheckBox.CheckState = CheckState.Unchecked;
195                 }
196                 tbl.CheckBox.Enabled = rootEnabled;
197             }
198         }
199
200         private void CheckEventNotify_CheckedChanged(object sender, EventArgs e)
201         {
202             foreach (var tbl in this.GetEventCheckboxTable())
203             {
204                 tbl.CheckBox.Enabled = this.CheckEventNotify.Checked;
205             }
206         }
207     }
208 }