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.
28 using System.Collections.Generic;
29 using System.ComponentModel;
32 using System.Globalization;
35 using System.Windows.Forms;
39 public partial class HashtagManage : OTBaseForm
42 /// エラー時にダイアログを表示させない (ユニットテストなどで使用)
44 public bool RunSilent { get; set; }
47 private AtIdSupplement _hashSupl;
49 private string _useHash = "";
50 private bool _isPermanent = false;
51 private bool _isHead = false;
52 private bool _isNotAddToAtReply = true;
54 private bool _isAdd = false;
56 private void ChangeMode(bool isEdit)
58 this.GroupHashtag.Enabled = !isEdit;
59 this.GroupDetail.Enabled = isEdit;
60 this.TableLayoutButtons.Enabled = !isEdit;
62 this.UseHashText.Focus();
64 this.HistoryHashList.Focus();
67 private void Cancel_Button_Click(object sender, EventArgs e)
69 this.DialogResult = DialogResult.Cancel;
73 private void AddButton_Click(object sender, EventArgs e)
75 this.UseHashText.Text = "";
80 private void EditButton_Click(object sender, EventArgs e)
82 if (this.HistoryHashList.SelectedIndices.Count == 0) return;
83 this.UseHashText.Text = this.HistoryHashList.SelectedItems[0].ToString();
88 private void DeleteButton_Click(object sender, EventArgs e)
90 if (this.HistoryHashList.SelectedIndices.Count == 0) return;
91 if (!this.RunSilent &&
92 MessageBox.Show(Properties.Resources.DeleteHashtagsMessage1,
94 MessageBoxButtons.OKCancel,
95 MessageBoxIcon.Question) == DialogResult.Cancel)
100 // 削除によってインデックス番号が変わらないように逆順に処理する
101 var selectedIndices = this.HistoryHashList.SelectedIndices.Cast<int>()
102 .OrderByDescending(x => x).ToArray();
104 foreach (var idx in selectedIndices)
106 if (UseHashText.Text == HistoryHashList.Items[idx].ToString()) UseHashText.Text = "";
107 HistoryHashList.Items.RemoveAt(idx);
109 if (HistoryHashList.Items.Count > 0)
111 HistoryHashList.SelectedIndex = 0;
115 private void UnSelectButton_Click(object sender, EventArgs e)
119 HistoryHashList.SelectedIndices.Clear();
120 } while (HistoryHashList.SelectedIndices.Count > 0);
123 private int GetIndexOf(ListBox.ObjectCollection list, string value)
125 if (string.IsNullOrEmpty(value)) return -1;
129 foreach (object l in list)
131 string src = l as string;
132 if (string.IsNullOrEmpty(src))
137 if (string.Compare(src, value, StringComparison.OrdinalIgnoreCase) == 0)
148 public void AddHashToHistory(string hash, bool isIgnorePermanent)
151 if (!string.IsNullOrEmpty(hash))
153 if (isIgnorePermanent || !_isPermanent)
156 int idx = GetIndexOf(HistoryHashList.Items, hash);
158 if (idx != -1) HistoryHashList.Items.RemoveAt(idx);
159 HistoryHashList.Items.Insert(0, hash);
164 int idx = GetIndexOf(HistoryHashList.Items, hash);
169 //重複アイテムが2行目以降にあれば2行目へ
170 HistoryHashList.Items.RemoveAt(idx);
171 HistoryHashList.Items.Insert(1, hash);
176 if (HistoryHashList.Items.Count == 0)
179 HistoryHashList.Items.Add(hash);
184 HistoryHashList.Items.Insert(1, hash);
192 private void HashtagManage_Shown(object sender, EventArgs e)
195 this.CheckPermanent.Checked = this._isPermanent;
196 this.RadioHead.Checked = this._isHead;
197 this.RadioLast.Checked = !this._isHead;
199 if (this.HistoryHashList.Items.Contains(this._useHash))
201 this.HistoryHashList.SelectedItem = this._useHash;
205 if (this.HistoryHashList.Items.Count > 0)
206 this.HistoryHashList.SelectedIndex = 0;
208 this.ChangeMode(false);
211 public HashtagManage(AtIdSupplement hashSuplForm, string[] history, string permanentHash, bool IsPermanent, bool IsHead, bool IsNotAddToAtReply)
213 // この呼び出しは、Windows フォーム デザイナで必要です。
214 InitializeComponent();
216 // InitializeComponent() 呼び出しの後で初期化を追加します。
218 _hashSupl = hashSuplForm;
219 HistoryHashList.Items.AddRange(history);
220 _useHash = permanentHash;
221 _isPermanent = IsPermanent;
223 _isNotAddToAtReply = IsNotAddToAtReply;
226 private void UseHashText_KeyPress(object sender, KeyPressEventArgs e)
228 if (e.KeyChar == '#')
230 _hashSupl.ShowDialog();
231 if (!string.IsNullOrEmpty(_hashSupl.inputText))
235 int selStart = UseHashText.SelectionStart;
238 fHalf = UseHashText.Text.Substring(0, selStart);
240 if (selStart < UseHashText.Text.Length)
242 eHalf = UseHashText.Text.Substring(selStart);
244 UseHashText.Text = fHalf + _hashSupl.inputText + eHalf;
245 UseHashText.SelectionStart = selStart + _hashSupl.inputText.Length;
251 private void HistoryHashList_DoubleClick(object sender, EventArgs e)
253 this.OK_Button_Click(null, null);
256 public void ToggleHash()
258 if (string.IsNullOrEmpty(this._useHash))
260 if (this.HistoryHashList.Items.Count > 0)
261 this._useHash = this.HistoryHashList.Items[0].ToString();
269 public List<string> HashHistories
273 List<string> hash = new List<string>();
274 foreach (string item in HistoryHashList.Items)
282 public string UseHash
284 get { return _useHash; }
287 public void ClearHashtag()
292 public void SetPermanentHash(string hash)
295 _useHash = hash.Trim();
296 this.AddHashToHistory(_useHash, false);
297 this._isPermanent = true;
300 public bool IsPermanent
302 get { return _isPermanent; }
307 get { return _isHead; }
310 public bool IsNotAddToAtReply
312 get { return _isNotAddToAtReply; }
315 private void PermOK_Button_Click(object sender, EventArgs e)
318 string hashStr = UseHashText.Text;
319 if (!this.AdjustHashtags(ref hashStr, !this.RunSilent)) return;
321 UseHashText.Text = hashStr;
323 if (!this._isAdd && this.HistoryHashList.SelectedIndices.Count > 0)
325 idx = this.HistoryHashList.SelectedIndices[0];
326 this.HistoryHashList.Items.RemoveAt(idx);
329 this.HistoryHashList.SelectedIndices.Clear();
330 } while (this.HistoryHashList.SelectedIndices.Count > 0);
331 this.HistoryHashList.Items.Insert(idx, hashStr);
332 this.HistoryHashList.SelectedIndex = idx;
336 this.AddHashToHistory(hashStr, false);
339 this.HistoryHashList.SelectedIndices.Clear();
340 } while (this.HistoryHashList.SelectedIndices.Count > 0);
341 this.HistoryHashList.SelectedIndex = this.HistoryHashList.Items.IndexOf(hashStr);
347 private void PermCancel_Button_Click(object sender, EventArgs e)
349 if (this.HistoryHashList.Items.Count > 0 && this.HistoryHashList.SelectedIndices.Count > 0)
350 this.UseHashText.Text = this.HistoryHashList.Items[this.HistoryHashList.SelectedIndices[0]].ToString();
352 this.UseHashText.Text = "";
357 private void HistoryHashList_KeyDown(object sender, KeyEventArgs e)
359 if (e.KeyCode == Keys.Delete)
360 this.DeleteButton_Click(null, null);
361 else if (e.KeyCode == Keys.Insert)
362 this.AddButton_Click(null, null);
365 private bool AdjustHashtags(ref string hashtag, bool isShowWarn)
368 hashtag = hashtag.Trim();
369 if (string.IsNullOrEmpty(hashtag))
371 if (isShowWarn) MessageBox.Show("emply hashtag.", "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
374 hashtag = hashtag.Replace("#", "#");
375 hashtag = hashtag.Replace(" ", " ");
377 foreach (string hash in hashtag.Split(' '))
381 if (!hash.StartsWith("#", StringComparison.Ordinal))
383 if (isShowWarn) MessageBox.Show("Invalid hashtag. -> " + hash, "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
386 if (hash.Length == 1)
388 if (isShowWarn) MessageBox.Show("empty hashtag.", "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
392 adjust += hash + " ";
395 hashtag = adjust.Trim();
399 private void OK_Button_Click(object sender, EventArgs e)
402 foreach (string hs in this.HistoryHashList.SelectedItems)
407 if (!string.IsNullOrEmpty(hash))
409 this.AddHashToHistory(hash, true);
410 this._isPermanent = this.CheckPermanent.Checked;
414 //使用ハッシュが未選択ならば、固定オプション外す
415 this._isPermanent = false;
417 this._isHead = this.RadioHead.Checked;
418 this._useHash = hash;
420 this.DialogResult = DialogResult.OK;
424 private void HashtagManage_KeyDown(object sender, KeyEventArgs e)
426 if (e.KeyCode == Keys.Enter)
429 if (this.GroupDetail.Enabled)
430 this.PermOK_Button_Click(null, null);
432 this.OK_Button_Click(null, null);
434 else if (e.KeyCode == Keys.Escape)
437 if (this.GroupDetail.Enabled)
438 this.PermCancel_Button_Click(null, null);
440 this.Cancel_Button_Click(null, null);
444 private void CheckNotAddToAtReply_CheckedChanged(object sender, EventArgs e)
446 _isNotAddToAtReply = CheckNotAddToAtReply.Checked;