OSDN Git Service

Merge branch 'dotnet4.5.1'
[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 readonly SearchType Type;
55             public readonly string Query;
56
57             // タイムライン内検索のみで使用する
58             public readonly bool NewTab;
59             public readonly bool CaseSensitive;
60             public readonly bool UseRegex;
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 { return 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.Type", (int)value.Type, typeof(SearchType));
100                 }
101             }
102         }
103
104         private bool disableNetTabButton = false;
105         public bool DisableNewTabButton
106         {
107             get { return this.disableNetTabButton; }
108             set
109             {
110                 this.disableNetTabButton = value;
111                 this.buttonSearchTimelineNew.Enabled = !value;
112             }
113         }
114
115         public SearchWordDialog()
116         {
117             InitializeComponent();
118         }
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 textSearchTimeline_Shown(object sender, EventArgs e)
132         {
133             this.textSearchTimeline.SelectAll();
134             this.textSearchTimeline.Focus();
135         }
136
137         private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
138         {
139             if (this.tabControl.SelectedTab == this.tabPageTimeline)
140                 this.AcceptButton = this.buttonSearchTimeline;
141             else
142                 this.AcceptButton = this.buttonSearchPublic;
143         }
144
145         private void buttonSearchTimeline_Click(object sender, EventArgs e)
146         {
147             if (string.IsNullOrEmpty(this.textSearchTimeline.Text))
148             {
149                 this.DialogResult = DialogResult.Cancel;
150                 return;
151             }
152
153             this.DialogResult = DialogResult.OK;
154
155             this.ResultOptions = new SearchOptions(
156                 SearchType.Timeline,
157                 this.textSearchTimeline.Text,
158                 false,
159                 this.checkTimelineCaseSensitive.Checked,
160                 this.checkTimelineRegex.Checked
161             );
162         }
163
164         private void buttonSearchTimelineNew_Click(object sender, EventArgs e)
165         {
166             if (string.IsNullOrEmpty(this.textSearchTimeline.Text))
167             {
168                 this.DialogResult = DialogResult.Cancel;
169                 return;
170             }
171
172             this.DialogResult = DialogResult.OK;
173
174             this.ResultOptions = new SearchOptions(
175                 SearchType.Timeline,
176                 this.textSearchTimeline.Text,
177                 true,
178                 this.checkTimelineCaseSensitive.Checked,
179                 this.checkTimelineRegex.Checked
180             );
181         }
182
183         private void buttonSearchPublic_Click(object sender, EventArgs e)
184         {
185             if (string.IsNullOrEmpty(this.textSearchPublic.Text))
186             {
187                 this.DialogResult = DialogResult.Cancel;
188                 return;
189             }
190
191             this.DialogResult = DialogResult.OK;
192
193             this.ResultOptions = new SearchOptions(
194                 SearchType.Public,
195                 this.textSearchPublic.Text,
196                 true,
197                 false,
198                 false
199             );
200         }
201
202         private /* async */ void linkLabelSearchHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
203         {
204             // 「検索オプションの使い方」ページのURL
205             const string PublicSearchHelpUrl = "https://support.twitter.com/articles/249059";
206
207             var tweenMain = (TweenMain)this.Owner;
208             tweenMain.OpenUriAsync(PublicSearchHelpUrl);
209         }
210     }
211 }