OSDN Git Service

C#7.0で追加されたTupleの構文を使用する
[opentween/open-tween.git] / OpenTween / HashtagManage.cs
index c557b20..8822349 100644 (file)
@@ -20,7 +20,7 @@
 // for more details. 
 // 
 // You should have received a copy of the GNU General public License along
-// with this program. if (not, see <http://www.gnu.org/licenses/>, or write to
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
 // Boston, MA 02110-1301, USA.
 
@@ -29,6 +29,7 @@ using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
+using System.Globalization;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
@@ -37,6 +38,11 @@ namespace OpenTween
 {
     public partial class HashtagManage : OTBaseForm
     {
+        /// <summary>
+        /// エラー時にダイアログを表示させない (ユニットテストなどで使用)
+        /// </summary>
+        public bool RunSilent { get; set; }
+
         //入力補助画面
         private AtIdSupplement _hashSupl;
         //I/F用
@@ -82,17 +88,23 @@ namespace OpenTween
         private void DeleteButton_Click(object sender, EventArgs e)
         {
             if (this.HistoryHashList.SelectedIndices.Count == 0) return;
-            if (MessageBox.Show(Properties.Resources.DeleteHashtagsMessage1,
+            if (!this.RunSilent &&
+                MessageBox.Show(Properties.Resources.DeleteHashtagsMessage1,
                                 "Delete Hashtags",
                                 MessageBoxButtons.OKCancel,
                                 MessageBoxIcon.Question) == DialogResult.Cancel)
             {
                 return;
             }
-            for (int i = 0; i < HistoryHashList.SelectedIndices.Count; i++)
+
+            // 削除によってインデックス番号が変わらないように逆順に処理する
+            var selectedIndices = this.HistoryHashList.SelectedIndices.Cast<int>()
+                .OrderByDescending(x => x).ToArray();
+
+            foreach (var idx in selectedIndices)
             {
-                if (UseHashText.Text == HistoryHashList.SelectedItems[0].ToString()) UseHashText.Text = "";
-                HistoryHashList.Items.RemoveAt(HistoryHashList.SelectedIndices[0]);
+                if (UseHashText.Text == HistoryHashList.Items[idx].ToString()) UseHashText.Text = "";
+                HistoryHashList.Items.RemoveAt(idx);
             }
             if (HistoryHashList.Items.Count > 0)
             {
@@ -122,7 +134,7 @@ namespace OpenTween
                     idx += 1;
                     continue;
                 }
-                if (string.Compare(src, value, true) == 0)
+                if (string.Compare(src, value, StringComparison.OrdinalIgnoreCase) == 0)
                 {
                     return idx;
                 }
@@ -304,7 +316,7 @@ namespace OpenTween
         {
             //ハッシュタグの整形
             string hashStr = UseHashText.Text;
-            if (!this.AdjustHashtags(ref hashStr, true)) return;
+            if (!this.AdjustHashtags(ref hashStr, !this.RunSilent)) return;
 
             UseHashText.Text = hashStr;
             int idx = 0;
@@ -366,7 +378,7 @@ namespace OpenTween
             {
                 if (hash.Length > 0)
                 {
-                    if (!hash.StartsWith("#"))
+                    if (!hash.StartsWith("#", StringComparison.Ordinal))
                     {
                         if (isShowWarn) MessageBox.Show("Invalid hashtag. -> " + hash, "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                         return false;