OSDN Git Service

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