OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween / SearchWordDialog.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      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.Data;
33 using System.Drawing;
34 using System.Linq;
35 using System.Text;
36 using System.Windows.Forms;
37
38 namespace OpenTween
39 {
40     public partial class SearchWordDialog : OTBaseForm
41     {
42         public enum SearchType
43         {
44             /// <summary>
45             /// タイムライン内を検索
46             /// </summary>
47             Timeline,
48             /// <summary>
49             /// Twitter検索
50             /// </summary>
51             Public,
52         }
53
54         public class SearchOptions
55         {
56             public SearchType Type { get; }
57             public string Query { get; }
58
59             // タイムライン内検索のみで使用する
60             public bool NewTab { get; }
61             public bool CaseSensitive { get; }
62             public bool UseRegex { get; }
63
64             public SearchOptions(SearchType type, string query, bool newTab, bool caseSensitive, bool useRegex)
65             {
66                 this.Type = type;
67                 this.Query = query;
68                 this.NewTab = newTab;
69                 this.CaseSensitive = caseSensitive;
70                 this.UseRegex = useRegex;
71             }
72         }
73
74         private SearchOptions? resultOptions = null;
75         public SearchOptions? ResultOptions
76         {
77             get => this.resultOptions;
78             set
79             {
80                 this.resultOptions = value;
81
82                 if (value == null)
83                 {
84                     this.Reset();
85                     return;
86                 }
87
88                 switch (value.Type)
89                 {
90                     case SearchType.Timeline:
91                         this.tabControl.SelectedTab = this.tabPageTimeline;
92                         this.textSearchTimeline.Text = value.Query;
93                         this.checkTimelineCaseSensitive.Checked = value.CaseSensitive;
94                         this.checkTimelineRegex.Checked = value.UseRegex;
95                         break;
96                     case SearchType.Public:
97                         this.tabControl.SelectedTab = this.tabPagePublic;
98                         this.textSearchTimeline.Text = value.Query;
99                         break;
100                     default:
101                         throw new InvalidEnumArgumentException("value", (int)value.Type, typeof(SearchType));
102                 }
103             }
104         }
105
106         private bool disableNetTabButton = false;
107         public bool DisableNewTabButton
108         {
109             get => this.disableNetTabButton;
110             set
111             {
112                 this.disableNetTabButton = value;
113                 this.buttonSearchTimelineNew.Enabled = !value;
114             }
115         }
116
117         public SearchWordDialog()
118             => this.InitializeComponent();
119
120         public void Reset()
121         {
122             this.tabControl.SelectedTab = this.tabPageTimeline;
123
124             this.textSearchTimeline.ResetText();
125             this.checkTimelineCaseSensitive.Checked = false;
126             this.checkTimelineRegex.Checked = false;
127
128             this.textSearchPublic.ResetText();
129         }
130
131         private void SearchWordDialog_Shown(object sender, EventArgs e)
132             => this.ActivateSelectedTabPage();
133
134         private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
135             => this.ActivateSelectedTabPage();
136
137         private void ActivateSelectedTabPage()
138         {
139             if (this.tabControl.SelectedTab == this.tabPageTimeline)
140             {
141                 this.AcceptButton = this.buttonSearchTimeline;
142                 this.textSearchTimeline.SelectAll();
143                 this.textSearchTimeline.Focus();
144             }
145             else
146             {
147                 this.AcceptButton = this.buttonSearchPublic;
148                 this.textSearchPublic.SelectAll();
149                 this.textSearchPublic.Focus();
150             }
151         }
152
153         private void buttonSearchTimeline_Click(object sender, EventArgs e)
154         {
155             if (string.IsNullOrEmpty(this.textSearchTimeline.Text))
156             {
157                 this.DialogResult = DialogResult.Cancel;
158                 return;
159             }
160
161             this.DialogResult = DialogResult.OK;
162
163             this.ResultOptions = new SearchOptions(
164                 SearchType.Timeline,
165                 this.textSearchTimeline.Text,
166                 false,
167                 this.checkTimelineCaseSensitive.Checked,
168                 this.checkTimelineRegex.Checked
169             );
170         }
171
172         private void buttonSearchTimelineNew_Click(object sender, EventArgs e)
173         {
174             if (string.IsNullOrEmpty(this.textSearchTimeline.Text))
175             {
176                 this.DialogResult = DialogResult.Cancel;
177                 return;
178             }
179
180             this.DialogResult = DialogResult.OK;
181
182             this.ResultOptions = new SearchOptions(
183                 SearchType.Timeline,
184                 this.textSearchTimeline.Text,
185                 true,
186                 this.checkTimelineCaseSensitive.Checked,
187                 this.checkTimelineRegex.Checked
188             );
189         }
190
191         private void buttonSearchPublic_Click(object sender, EventArgs e)
192         {
193             if (string.IsNullOrEmpty(this.textSearchPublic.Text))
194             {
195                 this.DialogResult = DialogResult.Cancel;
196                 return;
197             }
198
199             this.DialogResult = DialogResult.OK;
200
201             this.ResultOptions = new SearchOptions(
202                 SearchType.Public,
203                 this.textSearchPublic.Text,
204                 true,
205                 false,
206                 false
207             );
208         }
209
210         private async void linkLabelSearchHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
211         {
212             // 「検索オプションの使い方」ページのURL
213             const string PublicSearchHelpUrl = "https://support.twitter.com/articles/249059";
214
215             var tweenMain = (TweenMain)this.Owner;
216             await tweenMain.OpenUriInBrowserAsync(PublicSearchHelpUrl);
217         }
218
219         private void SearchWordDialog_KeyDown(object sender, KeyEventArgs e)
220         {
221             if (e.KeyCode == Keys.Escape)
222                 this.DialogResult = DialogResult.Cancel;
223         }
224     }
225 }