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) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
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)
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
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.
30 using System.Collections.Generic;
31 using System.ComponentModel;
34 using System.Globalization;
37 using System.Windows.Forms;
41 public partial class HashtagManage : OTBaseForm
43 public string UseHash { get; private set; } = "";
44 public bool IsPermanent { get; private set; } = false;
45 public bool IsHead { get; private set; } = false;
46 public bool IsNotAddToAtReply { get; private set; } = true;
49 /// エラー時にダイアログを表示させない (ユニットテストなどで使用)
51 public bool RunSilent { get; set; }
54 private readonly AtIdSupplement _hashSupl;
57 private bool _isAdd = false;
59 private void ChangeMode(bool isEdit)
61 this.GroupHashtag.Enabled = !isEdit;
62 this.GroupDetail.Enabled = isEdit;
63 this.TableLayoutButtons.Enabled = !isEdit;
65 this.UseHashText.Focus();
67 this.HistoryHashList.Focus();
70 private void Cancel_Button_Click(object sender, EventArgs e)
72 this.DialogResult = DialogResult.Cancel;
76 private void AddButton_Click(object sender, EventArgs e)
78 this.UseHashText.Text = "";
83 private void EditButton_Click(object sender, EventArgs e)
85 if (this.HistoryHashList.SelectedIndices.Count == 0) return;
86 this.UseHashText.Text = this.HistoryHashList.SelectedItems[0].ToString();
91 private void DeleteButton_Click(object sender, EventArgs e)
93 if (this.HistoryHashList.SelectedIndices.Count == 0) return;
94 if (!this.RunSilent &&
95 MessageBox.Show(Properties.Resources.DeleteHashtagsMessage1,
97 MessageBoxButtons.OKCancel,
98 MessageBoxIcon.Question) == DialogResult.Cancel)
103 // 削除によってインデックス番号が変わらないように逆順に処理する
104 var selectedIndices = this.HistoryHashList.SelectedIndices.Cast<int>()
105 .OrderByDescending(x => x).ToArray();
107 foreach (var idx in selectedIndices)
109 if (UseHashText.Text == HistoryHashList.Items[idx].ToString()) UseHashText.Text = "";
110 HistoryHashList.Items.RemoveAt(idx);
112 if (HistoryHashList.Items.Count > 0)
114 HistoryHashList.SelectedIndex = 0;
118 private void UnSelectButton_Click(object sender, EventArgs e)
122 HistoryHashList.SelectedIndices.Clear();
123 } while (HistoryHashList.SelectedIndices.Count > 0);
126 private int GetIndexOf(ListBox.ObjectCollection list, string value)
128 if (string.IsNullOrEmpty(value)) return -1;
132 foreach (var l in list)
135 if (string.IsNullOrEmpty(src))
140 if (string.Compare(src, value, StringComparison.OrdinalIgnoreCase) == 0)
151 public void AddHashToHistory(string hash, bool isIgnorePermanent)
154 if (!string.IsNullOrEmpty(hash))
156 if (isIgnorePermanent || !this.IsPermanent)
159 var idx = GetIndexOf(HistoryHashList.Items, hash);
161 if (idx != -1) HistoryHashList.Items.RemoveAt(idx);
162 HistoryHashList.Items.Insert(0, hash);
167 var idx = GetIndexOf(HistoryHashList.Items, hash);
168 if (this.IsPermanent)
172 //重複アイテムが2行目以降にあれば2行目へ
173 HistoryHashList.Items.RemoveAt(idx);
174 HistoryHashList.Items.Insert(1, hash);
179 if (HistoryHashList.Items.Count == 0)
182 HistoryHashList.Items.Add(hash);
187 HistoryHashList.Items.Insert(1, hash);
195 private void HashtagManage_Shown(object sender, EventArgs e)
198 this.CheckPermanent.Checked = this.IsPermanent;
199 this.RadioHead.Checked = this.IsHead;
200 this.RadioLast.Checked = !this.IsHead;
202 if (this.HistoryHashList.Items.Contains(this.UseHash))
204 this.HistoryHashList.SelectedItem = this.UseHash;
208 if (this.HistoryHashList.Items.Count > 0)
209 this.HistoryHashList.SelectedIndex = 0;
211 this.ChangeMode(false);
214 public HashtagManage(AtIdSupplement hashSuplForm, string[] history, string permanentHash, bool IsPermanent, bool IsHead, bool IsNotAddToAtReply)
216 // この呼び出しは、Windows フォーム デザイナで必要です。
217 InitializeComponent();
219 // InitializeComponent() 呼び出しの後で初期化を追加します。
221 _hashSupl = hashSuplForm;
222 HistoryHashList.Items.AddRange(history);
223 this.UseHash = permanentHash;
224 this.IsPermanent = IsPermanent;
225 this.IsHead = IsHead;
226 this.IsNotAddToAtReply = IsNotAddToAtReply;
229 private void UseHashText_KeyPress(object sender, KeyPressEventArgs e)
231 if (e.KeyChar == '#')
233 _hashSupl.ShowDialog();
234 if (!string.IsNullOrEmpty(_hashSupl.inputText))
238 var selStart = UseHashText.SelectionStart;
241 fHalf = UseHashText.Text.Substring(0, selStart);
243 if (selStart < UseHashText.Text.Length)
245 eHalf = UseHashText.Text.Substring(selStart);
247 UseHashText.Text = fHalf + _hashSupl.inputText + eHalf;
248 UseHashText.SelectionStart = selStart + _hashSupl.inputText.Length;
254 private void HistoryHashList_DoubleClick(object sender, EventArgs e)
255 => this.OK_Button_Click(this.OK_Button, EventArgs.Empty);
257 public void ToggleHash()
259 if (string.IsNullOrEmpty(this.UseHash))
261 if (this.HistoryHashList.Items.Count > 0)
262 this.UseHash = this.HistoryHashList.Items[0].ToString();
270 public List<string> HashHistories
274 var hash = new List<string>();
275 foreach (string item in HistoryHashList.Items)
283 public void ClearHashtag()
284 => this.UseHash = "";
286 public void SetPermanentHash(string hash)
289 this.UseHash = hash.Trim();
290 this.AddHashToHistory(UseHash, false);
291 this.IsPermanent = true;
294 private void PermOK_Button_Click(object sender, EventArgs e)
297 var hashStr = UseHashText.Text;
298 if (!this.AdjustHashtags(ref hashStr, !this.RunSilent)) return;
300 UseHashText.Text = hashStr;
301 if (!this._isAdd && this.HistoryHashList.SelectedIndices.Count > 0)
303 var idx = this.HistoryHashList.SelectedIndices[0];
304 this.HistoryHashList.Items.RemoveAt(idx);
307 this.HistoryHashList.SelectedIndices.Clear();
308 } while (this.HistoryHashList.SelectedIndices.Count > 0);
309 this.HistoryHashList.Items.Insert(idx, hashStr);
310 this.HistoryHashList.SelectedIndex = idx;
314 this.AddHashToHistory(hashStr, false);
317 this.HistoryHashList.SelectedIndices.Clear();
318 } while (this.HistoryHashList.SelectedIndices.Count > 0);
319 this.HistoryHashList.SelectedIndex = this.HistoryHashList.Items.IndexOf(hashStr);
325 private void PermCancel_Button_Click(object sender, EventArgs e)
327 if (this.HistoryHashList.Items.Count > 0 && this.HistoryHashList.SelectedIndices.Count > 0)
328 this.UseHashText.Text = this.HistoryHashList.Items[this.HistoryHashList.SelectedIndices[0]].ToString();
330 this.UseHashText.Text = "";
335 private void HistoryHashList_KeyDown(object sender, KeyEventArgs e)
337 if (e.KeyCode == Keys.Delete)
338 this.DeleteButton_Click(this.DeleteButton, EventArgs.Empty);
339 else if (e.KeyCode == Keys.Insert)
340 this.AddButton_Click(this.AddButton, EventArgs.Empty);
343 private bool AdjustHashtags(ref string hashtag, bool isShowWarn)
346 hashtag = hashtag.Trim();
347 if (string.IsNullOrEmpty(hashtag))
349 if (isShowWarn) MessageBox.Show("emply hashtag.", "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
352 hashtag = hashtag.Replace("#", "#");
353 hashtag = hashtag.Replace(" ", " ");
355 foreach (var hash in hashtag.Split(' '))
359 if (!hash.StartsWith("#", StringComparison.Ordinal))
361 if (isShowWarn) MessageBox.Show("Invalid hashtag. -> " + hash, "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
364 if (hash.Length == 1)
366 if (isShowWarn) MessageBox.Show("empty hashtag.", "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
370 adjust += hash + " ";
373 hashtag = adjust.Trim();
377 private void OK_Button_Click(object sender, EventArgs e)
380 foreach (string hs in this.HistoryHashList.SelectedItems)
385 if (!string.IsNullOrEmpty(hash))
387 this.AddHashToHistory(hash, true);
388 this.IsPermanent = this.CheckPermanent.Checked;
392 //使用ハッシュが未選択ならば、固定オプション外す
393 this.IsPermanent = false;
395 this.IsHead = this.RadioHead.Checked;
398 this.DialogResult = DialogResult.OK;
402 private void HashtagManage_KeyDown(object sender, KeyEventArgs e)
404 if (e.KeyCode == Keys.Enter)
407 if (this.GroupDetail.Enabled)
408 this.PermOK_Button_Click(this.PermOK_Button, EventArgs.Empty);
410 this.OK_Button_Click(this.OK_Button, EventArgs.Empty);
412 else if (e.KeyCode == Keys.Escape)
415 if (this.GroupDetail.Enabled)
416 this.PermCancel_Button_Click(this.PermCancel_Button, EventArgs.Empty);
418 this.Cancel_Button_Click(this.Cancel_Button, EventArgs.Empty);
422 private void CheckNotAddToAtReply_CheckedChanged(object sender, EventArgs e)
423 => this.IsNotAddToAtReply = CheckNotAddToAtReply.Checked;