OSDN Git Service

ドキュメントコメントの誤りを修正
[opentween/open-tween.git] / OpenTween / Tween.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 //コンパイル後コマンド
28 //"c:\Program Files\Microsoft.NET\SDK\v2.0\Bin\sgen.exe" /f /a:"$(TargetPath)"
29 //"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" /f /a:"$(TargetPath)"
30
31 using System;
32 using System.Collections.Concurrent;
33 using System.Collections.Generic;
34 using System.ComponentModel;
35 using System.Diagnostics;
36 using System.Drawing;
37 using System.Globalization;
38 using System.IO;
39 using System.Linq;
40 using System.Media;
41 using System.Net;
42 using System.Net.Http;
43 using System.Reflection;
44 using System.Text;
45 using System.Text.RegularExpressions;
46 using System.Threading;
47 using System.Threading.Tasks;
48 using System.Windows.Forms;
49 using OpenTween.Api;
50 using OpenTween.Api.DataModel;
51 using OpenTween.Connection;
52 using OpenTween.Models;
53 using OpenTween.OpenTweenCustomControl;
54 using OpenTween.Thumbnail;
55
56 namespace OpenTween
57 {
58     public partial class TweenMain : OTBaseForm
59     {
60         //各種設定
61         private Size _mySize;           //画面サイズ
62         private Point _myLoc;           //画面位置
63         private int _mySpDis;           //区切り位置
64         private int _mySpDis2;          //発言欄区切り位置
65         private int _mySpDis3;          //プレビュー区切り位置
66         private int _iconSz;            //アイコンサイズ(現在は16、24、48の3種類。将来直接数字指定可能とする 注:24x24の場合に26と指定しているのはMSゴシック系フォントのための仕様)
67         private bool _iconCol;          //1列表示の時true(48サイズのとき)
68
69         //雑多なフラグ類
70         private bool _initial;         //true:起動時処理中
71         private bool _initialLayout = true;
72         private bool _ignoreConfigSave;         //true:起動時処理中
73         private bool _tabDrag;           //タブドラッグ中フラグ(DoDragDropを実行するかの判定用)
74         private TabPage _beforeSelectedTab; //タブが削除されたときに前回選択されていたときのタブを選択する為に保持
75         private Point _tabMouseDownPoint;
76         private string _rclickTabName;      //右クリックしたタブの名前(Tabコントロール機能不足対応)
77         private readonly object _syncObject = new object();    //ロック用
78
79         private const string detailHtmlFormatHeaderMono = 
80             "<html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\">"
81             + "<style type=\"text/css\"><!-- "
82             + "body, p, pre {margin: 0;} "
83             + "pre {font-family: \"%FONT_FAMILY%\", sans-serif; font-size: %FONT_SIZE%pt; background-color:rgb(%BG_COLOR%); word-wrap: break-word; color:rgb(%FONT_COLOR%);} "
84             + "a:link, a:visited, a:active, a:hover {color:rgb(%LINK_COLOR%); } "
85             + "img.emoji {width: 1em; height: 1em; margin: 0 .05em 0 .1em; vertical-align: -0.1em; border: none;} "
86             + ".quote-tweet {border: 1px solid #ccc; margin: 1em; padding: 0.5em;} "
87             + ".quote-tweet.reply {border-color: #f33;} "
88             + ".quote-tweet-link {color: inherit !important; text-decoration: none;}"
89             + "--></style>"
90             + "</head><body><pre>";
91         private const string detailHtmlFormatFooterMono = "</pre></body></html>";
92         private const string detailHtmlFormatHeaderColor = 
93             "<html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\">"
94             + "<style type=\"text/css\"><!-- "
95             + "body, p, pre {margin: 0;} "
96             + "body {font-family: \"%FONT_FAMILY%\", sans-serif; font-size: %FONT_SIZE%pt; background-color:rgb(%BG_COLOR%); margin: 0; word-wrap: break-word; color:rgb(%FONT_COLOR%);} "
97             + "a:link, a:visited, a:active, a:hover {color:rgb(%LINK_COLOR%); } "
98             + "img.emoji {width: 1em; height: 1em; margin: 0 .05em 0 .1em; vertical-align: -0.1em; border: none;} "
99             + ".quote-tweet {border: 1px solid #ccc; margin: 1em; padding: 0.5em;} "
100             + ".quote-tweet.reply {border-color: rgb(%BG_REPLY_COLOR%);} "
101             + ".quote-tweet-link {color: inherit !important; text-decoration: none;}"
102             + "--></style>"
103             + "</head><body><p>";
104         private const string detailHtmlFormatFooterColor = "</p></body></html>";
105         private string detailHtmlFormatHeader;
106         private string detailHtmlFormatFooter;
107
108         private bool _myStatusError = false;
109         private bool _myStatusOnline = false;
110         private bool soundfileListup = false;
111         private FormWindowState _formWindowState = FormWindowState.Normal; // フォームの状態保存用 通知領域からアイコンをクリックして復帰した際に使用する
112
113         //設定ファイル関連
114         //private SettingToConfig _cfg; //旧
115         internal SettingLocal _cfgLocal;
116         private SettingCommon _cfgCommon;
117
118         //twitter解析部
119         private TwitterApi twitterApi = new TwitterApi();
120         private Twitter tw;
121
122         //Growl呼び出し部
123         private GrowlHelper gh = new GrowlHelper(Application.ProductName);
124
125         //サブ画面インスタンス
126         internal SearchWordDialog SearchDialog = new SearchWordDialog();     //検索画面インスタンス
127         private OpenURL UrlDialog = new OpenURL();
128         public AtIdSupplement AtIdSupl;     //@id補助
129         public AtIdSupplement HashSupl;    //Hashtag補助
130         public HashtagManage HashMgr;
131         private EventViewerDialog evtDialog;
132
133         //表示フォント、色、アイコン
134         private Font _fntUnread;            //未読用フォント
135         private Color _clUnread;            //未読用文字色
136         private Font _fntReaded;            //既読用フォント
137         private Color _clReaded;            //既読用文字色
138         private Color _clFav;               //Fav用文字色
139         private Color _clOWL;               //片思い用文字色
140         private Color _clRetweet;               //Retweet用文字色
141         private Color _clHighLight = Color.FromKnownColor(KnownColor.HighlightText);         //選択中の行用文字色
142         private Font _fntDetail;            //発言詳細部用フォント
143         private Color _clDetail;              //発言詳細部用色
144         private Color _clDetailLink;          //発言詳細部用リンク文字色
145         private Color _clDetailBackcolor;     //発言詳細部用背景色
146         private Color _clSelf;              //自分の発言用背景色
147         private Color _clAtSelf;            //自分宛返信用背景色
148         private Color _clTarget;            //選択発言者の他の発言用背景色
149         private Color _clAtTarget;          //選択発言中の返信先用背景色
150         private Color _clAtFromTarget;      //選択発言者への返信発言用背景色
151         private Color _clAtTo;              //選択発言の唯一@先
152         private Color _clListBackcolor;       //リスト部通常発言背景色
153         private Color _clInputBackcolor;      //入力欄背景色
154         private Color _clInputFont;           //入力欄文字色
155         private Font _fntInputFont;           //入力欄フォント
156         private ImageCache IconCache;        //アイコン画像リスト
157         private Icon NIconAt;               //At.ico             タスクトレイアイコン:通常時
158         private Icon NIconAtRed;            //AtRed.ico          タスクトレイアイコン:通信エラー時
159         private Icon NIconAtSmoke;          //AtSmoke.ico        タスクトレイアイコン:オフライン時
160         private Icon[] NIconRefresh = new Icon[4];       //Refresh.ico        タスクトレイアイコン:更新中(アニメーション用に4種類を保持するリスト)
161         private Icon TabIcon;               //Tab.ico            未読のあるタブ用アイコン
162         private Icon MainIcon;              //Main.ico           画面左上のアイコン
163         private Icon ReplyIcon;               //5g
164         private Icon ReplyIconBlink;          //6g
165
166         private ImageList _listViewImageList = new ImageList();    //ListViewItemの高さ変更用
167
168         private PostClass _anchorPost;
169         private bool _anchorFlag;        //true:関連発言移動中(関連移動以外のオペレーションをするとfalseへ。trueだとリスト背景色をアンカー発言選択中として描画)
170
171         private List<PostingStatus> _history = new List<PostingStatus>();   //発言履歴
172         private int _hisIdx;                  //発言履歴カレントインデックス
173
174         //発言投稿時のAPI引数(発言編集時に設定。手書きreplyでは設定されない)
175         private Tuple<long, string> inReplyTo = null; // リプライ先のステータスID・スクリーン名
176
177         //時速表示用
178         private List<DateTime> _postTimestamps = new List<DateTime>();
179         private List<DateTime> _favTimestamps = new List<DateTime>();
180
181         // 以下DrawItem関連
182         private SolidBrush _brsHighLight = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
183         private SolidBrush _brsBackColorMine;
184         private SolidBrush _brsBackColorAt;
185         private SolidBrush _brsBackColorYou;
186         private SolidBrush _brsBackColorAtYou;
187         private SolidBrush _brsBackColorAtFromTarget;
188         private SolidBrush _brsBackColorAtTo;
189         private SolidBrush _brsBackColorNone;
190         private SolidBrush _brsDeactiveSelection = new SolidBrush(Color.FromKnownColor(KnownColor.ButtonFace)); //Listにフォーカスないときの選択行の背景色
191         private StringFormat sfTab = new StringFormat();
192
193         //////////////////////////////////////////////////////////////////////////////////////////////////////////
194         private TabInformations _statuses;
195
196         /// <summary>
197         /// 現在表示している発言一覧の <see cref="ListView"/> に対するキャッシュ
198         /// </summary>
199         /// <remarks>
200         /// キャッシュクリアのために null が代入されることがあるため、
201         /// 使用する場合には <see cref="_listItemCache"/> に対して直接メソッド等を呼び出さずに
202         /// 一旦ローカル変数に代入してから参照すること。
203         /// </remarks>
204         private ListViewItemCache _listItemCache = null;
205
206         internal class ListViewItemCache
207         {
208             /// <summary>アイテムをキャッシュする対象の <see cref="ListView"/></summary>
209             public ListView TargetList { get; set; }
210
211             /// <summary>キャッシュする範囲の開始インデックス</summary>
212             public int StartIndex { get; set; }
213
214             /// <summary>キャッシュする範囲の終了インデックス</summary>
215             public int EndIndex { get; set; }
216
217             /// <summary>キャッシュされた <see cref="ListViewItem"/> インスタンス</summary>
218             public ListViewItem[] ListItem { get; set; }
219
220             /// <summary>キャッシュされた範囲に対応する <see cref="PostClass"/> インスタンス</summary>
221             public PostClass[] Post { get; set; }
222
223             /// <summary>キャッシュされたアイテムの件数</summary>
224             public int Count
225                 => this.EndIndex - this.StartIndex + 1;
226
227             /// <summary>指定されたインデックスがキャッシュの範囲内であるか判定します</summary>
228             /// <returns><paramref name="index"/> がキャッシュの範囲内であれば true、それ以外は false</returns>
229             public bool Contains(int index)
230                 => index >= this.StartIndex && index <= this.EndIndex;
231
232             /// <summary>指定されたインデックスの範囲が全てキャッシュの範囲内であるか判定します</summary>
233             /// <returns><paramref name="rangeStart"/> から <paramref name="rangeEnd"/> の範囲が全てキャッシュの範囲内であれば true、それ以外は false</returns>
234             public bool IsSupersetOf(int rangeStart, int rangeEnd)
235                 => rangeStart >= this.StartIndex && rangeEnd <= this.EndIndex;
236
237             /// <summary>指定されたインデックスの <see cref="ListViewItem"/> と <see cref="PostClass"/> をキャッシュから取得することを試みます</summary>
238             /// <returns>取得に成功すれば true、それ以外は false</returns>
239             public bool TryGetValue(int index, out ListViewItem item, out PostClass post)
240             {
241                 if (this.Contains(index))
242                 {
243                     item = this.ListItem[index - this.StartIndex];
244                     post = this.Post[index - this.StartIndex];
245                     return true;
246                 }
247                 else
248                 {
249                     item = null;
250                     post = null;
251                     return false;
252                 }
253             }
254         }
255
256         private TabPage _curTab;
257         private int _curItemIndex;
258         private DetailsListView _curList;
259         private PostClass _curPost;
260         private bool _isColumnChanged = false;
261
262         private const int MAX_WORKER_THREADS = 20;
263         private SemaphoreSlim workerSemaphore = new SemaphoreSlim(MAX_WORKER_THREADS);
264         private CancellationTokenSource workerCts = new CancellationTokenSource();
265         private IProgress<string> workerProgress;
266
267         private int UnreadCounter = -1;
268         private int UnreadAtCounter = -1;
269
270         private string[] ColumnOrgText = new string[9];
271         private string[] ColumnText = new string[9];
272
273         private bool _DoFavRetweetFlags = false;
274         private bool osResumed = false;
275
276         //////////////////////////////////////////////////////////////////////////////////////////////////////////
277         private bool _colorize = false;
278
279         private System.Timers.Timer TimerTimeline = new System.Timers.Timer();
280
281         private string recommendedStatusFooter;
282
283         //URL短縮のUndo用
284         private struct urlUndo
285         {
286             public string Before;
287             public string After;
288         }
289
290         private List<urlUndo> urlUndoBuffer = null;
291
292         private struct ReplyChain
293         {
294             public long OriginalId;
295             public long InReplyToId;
296             public TabPage OriginalTab;
297
298             public ReplyChain(long originalId, long inReplyToId, TabPage originalTab)
299             {
300                 this.OriginalId = originalId;
301                 this.InReplyToId = inReplyToId;
302                 this.OriginalTab = originalTab;
303             }
304         }
305
306         private Stack<ReplyChain> replyChains; //[, ]でのリプライ移動の履歴
307         private Stack<Tuple<TabPage, PostClass>> selectPostChains = new Stack<Tuple<TabPage, PostClass>>(); //ポスト選択履歴
308
309         //検索処理タイプ
310         internal enum SEARCHTYPE
311         {
312             DialogSearch,
313             NextSearch,
314             PrevSearch,
315         }
316
317         private class PostingStatus
318         {
319             public string status = "";
320             public long? inReplyToId = null;
321             public string inReplyToName = null;
322             public string imageService = "";      //画像投稿サービス名
323             public IMediaItem[] mediaItems = null;
324             public PostingStatus()
325             {
326             }
327             public PostingStatus(string status, long? replyToId, string replyToName)
328             {
329                 this.status = status;
330                 this.inReplyToId = replyToId;
331                 this.inReplyToName = replyToName;
332             }
333         }
334
335         private void TweenMain_Activated(object sender, EventArgs e)
336         {
337             //画面がアクティブになったら、発言欄の背景色戻す
338             if (StatusText.Focused)
339             {
340                 this.StatusText_Enter(this.StatusText, System.EventArgs.Empty);
341             }
342         }
343
344         private bool disposed = false;
345
346         /// <summary>
347         /// 使用中のリソースをすべてクリーンアップします。
348         /// </summary>
349         /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
350         protected override void Dispose(bool disposing)
351         {
352             base.Dispose(disposing);
353
354             if (this.disposed)
355                 return;
356
357             if (disposing)
358             {
359                 this.components?.Dispose();
360
361                 //後始末
362                 SearchDialog.Dispose();
363                 UrlDialog.Dispose();
364                 NIconAt?.Dispose();
365                 NIconAtRed?.Dispose();
366                 NIconAtSmoke?.Dispose();
367                 foreach (var iconRefresh in this.NIconRefresh)
368                 {
369                     iconRefresh?.Dispose();
370                 }
371                 TabIcon?.Dispose();
372                 MainIcon?.Dispose();
373                 ReplyIcon?.Dispose();
374                 ReplyIconBlink?.Dispose();
375                 _listViewImageList.Dispose();
376                 _brsHighLight.Dispose();
377                 _brsBackColorMine?.Dispose();
378                 _brsBackColorAt?.Dispose();
379                 _brsBackColorYou?.Dispose();
380                 _brsBackColorAtYou?.Dispose();
381                 _brsBackColorAtFromTarget?.Dispose();
382                 _brsBackColorAtTo?.Dispose();
383                 _brsBackColorNone?.Dispose();
384                 _brsDeactiveSelection?.Dispose();
385                 //sf.Dispose();
386                 sfTab.Dispose();
387
388                 this.workerCts.Cancel();
389
390                 if (IconCache != null)
391                 {
392                     this.IconCache.CancelAsync();
393                     this.IconCache.Dispose();
394                 }
395
396                 this.thumbnailTokenSource?.Dispose();
397
398                 this.tw.Dispose();
399                 this.twitterApi.Dispose();
400                 this._hookGlobalHotkey.Dispose();
401             }
402
403             // 終了時にRemoveHandlerしておかないとメモリリークする
404             // http://msdn.microsoft.com/ja-jp/library/microsoft.win32.systemevents.powermodechanged.aspx
405             Microsoft.Win32.SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
406
407             this.disposed = true;
408         }
409
410         private void LoadIcons()
411         {
412             // Icons フォルダ以下のアイコンを読み込み(着せ替えアイコン対応)
413             var iconsDir = Path.Combine(Application.StartupPath, "Icons");
414
415             // ウィンドウ左上のアイコン
416             var iconMain = this.LoadIcon(Path.Combine(iconsDir, "MIcon.ico"));
417
418             // タブ見出し未読表示アイコン
419             var iconTab = this.LoadIcon(Path.Combine(iconsDir, "Tab.ico"));
420
421             // タスクトレイ: 通常時アイコン
422             var iconAt = this.LoadIcon(Path.Combine(iconsDir, "At.ico"));
423
424             // タスクトレイ: エラー時アイコン
425             var iconAtRed = this.LoadIcon(Path.Combine(iconsDir, "AtRed.ico"));
426
427             // タスクトレイ: オフライン時アイコン
428             var iconAtSmoke = this.LoadIcon(Path.Combine(iconsDir, "AtSmoke.ico"));
429
430             // タスクトレイ: Reply通知アイコン (最大2枚でアニメーション可能)
431             var iconReply = this.LoadIcon(Path.Combine(iconsDir, "Reply.ico"));
432             var iconReplyBlink = this.LoadIcon(Path.Combine(iconsDir, "ReplyBlink.ico"));
433
434             // タスクトレイ: 更新中アイコン (最大4枚でアニメーション可能)
435             var iconRefresh1 = this.LoadIcon(Path.Combine(iconsDir, "Refresh.ico"));
436             var iconRefresh2 = this.LoadIcon(Path.Combine(iconsDir, "Refresh2.ico"));
437             var iconRefresh3 = this.LoadIcon(Path.Combine(iconsDir, "Refresh3.ico"));
438             var iconRefresh4 = this.LoadIcon(Path.Combine(iconsDir, "Refresh4.ico"));
439
440             // 読み込んだアイコンを設定 (不足するアイコンはデフォルトのものを設定)
441
442             this.MainIcon = iconMain ?? Properties.Resources.MIcon;
443             this.TabIcon = iconTab ?? Properties.Resources.TabIcon;
444             this.NIconAt = iconAt ?? iconMain ?? Properties.Resources.At;
445             this.NIconAtRed = iconAtRed ?? Properties.Resources.AtRed;
446             this.NIconAtSmoke = iconAtSmoke ?? Properties.Resources.AtSmoke;
447
448             if (iconReply != null && iconReplyBlink != null)
449             {
450                 this.ReplyIcon = iconReply;
451                 this.ReplyIconBlink = iconReplyBlink;
452             }
453             else
454             {
455                 this.ReplyIcon = iconReply ?? iconReplyBlink ?? Properties.Resources.Reply;
456                 this.ReplyIconBlink = this.NIconAt;
457             }
458
459             if (iconRefresh1 == null)
460             {
461                 this.NIconRefresh = new[] {
462                     Properties.Resources.Refresh, Properties.Resources.Refresh2,
463                     Properties.Resources.Refresh3, Properties.Resources.Refresh4,
464                 };
465             }
466             else if (iconRefresh2 == null)
467             {
468                 this.NIconRefresh = new[] { iconRefresh1 };
469             }
470             else if (iconRefresh3 == null)
471             {
472                 this.NIconRefresh = new[] { iconRefresh1, iconRefresh2 };
473             }
474             else if (iconRefresh4 == null)
475             {
476                 this.NIconRefresh = new[] { iconRefresh1, iconRefresh2, iconRefresh3 };
477             }
478             else // iconRefresh1 から iconRefresh4 まで全て揃っている
479             {
480                 this.NIconRefresh = new[] { iconRefresh1, iconRefresh2, iconRefresh3, iconRefresh4 };
481             }
482         }
483
484         private Icon LoadIcon(string filePath)
485         {
486             if (!File.Exists(filePath))
487                 return null;
488
489             try
490             {
491                 return new Icon(filePath);
492             }
493             catch (Exception)
494             {
495                 return null;
496             }
497         }
498
499         private void InitColumns(ListView list, bool startup)
500         {
501             this.InitColumnText();
502
503             ColumnHeader[] columns = null;
504             try
505             {
506                 if (this._iconCol)
507                 {
508                     columns = new[]
509                     {
510                         new ColumnHeader(), // アイコン
511                         new ColumnHeader(), // 本文
512                     };
513
514                     columns[0].Text = this.ColumnText[0];
515                     columns[1].Text = this.ColumnText[2];
516
517                     if (startup)
518                     {
519                         var widthScaleFactor = this.CurrentAutoScaleDimensions.Width / this._cfgLocal.ScaleDimension.Width;
520
521                         columns[0].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width1);
522                         columns[1].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width3);
523                         columns[0].DisplayIndex = 0;
524                         columns[1].DisplayIndex = 1;
525                     }
526                     else
527                     {
528                         var idx = 0;
529                         foreach (var curListColumn in this._curList.Columns.Cast<ColumnHeader>())
530                         {
531                             columns[idx].Width = curListColumn.Width;
532                             columns[idx].DisplayIndex = curListColumn.DisplayIndex;
533                             idx++;
534                         }
535                     }
536                 }
537                 else
538                 {
539                     columns = new[]
540                     {
541                         new ColumnHeader(), // アイコン
542                         new ColumnHeader(), // ニックネーム
543                         new ColumnHeader(), // 本文
544                         new ColumnHeader(), // 日付
545                         new ColumnHeader(), // ユーザID
546                         new ColumnHeader(), // 未読
547                         new ColumnHeader(), // マーク&プロテクト
548                         new ColumnHeader(), // ソース
549                     };
550
551                     foreach (var i in Enumerable.Range(0, columns.Length))
552                         columns[i].Text = this.ColumnText[i];
553
554                     if (startup)
555                     {
556                         var widthScaleFactor = this.CurrentAutoScaleDimensions.Width / this._cfgLocal.ScaleDimension.Width;
557
558                         columns[0].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width1);
559                         columns[1].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width2);
560                         columns[2].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width3);
561                         columns[3].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width4);
562                         columns[4].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width5);
563                         columns[5].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width6);
564                         columns[6].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width7);
565                         columns[7].Width = ScaleBy(widthScaleFactor, _cfgLocal.Width8);
566
567                         var displayIndex = new[] {
568                             this._cfgLocal.DisplayIndex1, this._cfgLocal.DisplayIndex2,
569                             this._cfgLocal.DisplayIndex3, this._cfgLocal.DisplayIndex4,
570                             this._cfgLocal.DisplayIndex5, this._cfgLocal.DisplayIndex6,
571                             this._cfgLocal.DisplayIndex7, this._cfgLocal.DisplayIndex8
572                         };
573
574                         foreach (var i in Enumerable.Range(0, displayIndex.Length))
575                         {
576                             columns[i].DisplayIndex = displayIndex[i];
577                         }
578                     }
579                     else
580                     {
581                         var idx = 0;
582                         foreach (var curListColumn in this._curList.Columns.Cast<ColumnHeader>())
583                         {
584                             columns[idx].Width = curListColumn.Width;
585                             columns[idx].DisplayIndex = curListColumn.DisplayIndex;
586                             idx++;
587                         }
588                     }
589                 }
590
591                 list.Columns.AddRange(columns);
592
593                 columns = null;
594             }
595             finally
596             {
597                 if (columns != null)
598                 {
599                     foreach (var column in columns)
600                         column?.Dispose();
601                 }
602             }
603         }
604
605         private void InitColumnText()
606         {
607             ColumnText[0] = "";
608             ColumnText[1] = Properties.Resources.AddNewTabText2;
609             ColumnText[2] = Properties.Resources.AddNewTabText3;
610             ColumnText[3] = Properties.Resources.AddNewTabText4_2;
611             ColumnText[4] = Properties.Resources.AddNewTabText5;
612             ColumnText[5] = "";
613             ColumnText[6] = "";
614             ColumnText[7] = "Source";
615
616             ColumnOrgText[0] = "";
617             ColumnOrgText[1] = Properties.Resources.AddNewTabText2;
618             ColumnOrgText[2] = Properties.Resources.AddNewTabText3;
619             ColumnOrgText[3] = Properties.Resources.AddNewTabText4_2;
620             ColumnOrgText[4] = Properties.Resources.AddNewTabText5;
621             ColumnOrgText[5] = "";
622             ColumnOrgText[6] = "";
623             ColumnOrgText[7] = "Source";
624
625             int c = 0;
626             switch (_statuses.SortMode)
627             {
628                 case ComparerMode.Nickname:  //ニックネーム
629                     c = 1;
630                     break;
631                 case ComparerMode.Data:  //本文
632                     c = 2;
633                     break;
634                 case ComparerMode.Id:  //時刻=発言Id
635                     c = 3;
636                     break;
637                 case ComparerMode.Name:  //名前
638                     c = 4;
639                     break;
640                 case ComparerMode.Source:  //Source
641                     c = 7;
642                     break;
643             }
644
645             if (_iconCol)
646             {
647                 if (_statuses.SortOrder == SortOrder.Descending)
648                 {
649                     // U+25BE BLACK DOWN-POINTING SMALL TRIANGLE
650                     ColumnText[2] = ColumnOrgText[2] + "▾";
651                 }
652                 else
653                 {
654                     // U+25B4 BLACK UP-POINTING SMALL TRIANGLE
655                     ColumnText[2] = ColumnOrgText[2] + "▴";
656                 }
657             }
658             else
659             {
660                 if (_statuses.SortOrder == SortOrder.Descending)
661                 {
662                     // U+25BE BLACK DOWN-POINTING SMALL TRIANGLE
663                     ColumnText[c] = ColumnOrgText[c] + "▾";
664                 }
665                 else
666                 {
667                     // U+25B4 BLACK UP-POINTING SMALL TRIANGLE
668                     ColumnText[c] = ColumnOrgText[c] + "▴";
669                 }
670             }
671         }
672
673         private void InitializeTraceFrag()
674         {
675 #if DEBUG
676             TraceOutToolStripMenuItem.Checked = true;
677             MyCommon.TraceFlag = true;
678 #endif
679             if (!MyCommon.FileVersion.EndsWith("0", StringComparison.Ordinal))
680             {
681                 TraceOutToolStripMenuItem.Checked = true;
682                 MyCommon.TraceFlag = true;
683             }
684         }
685
686         private void TweenMain_Load(object sender, EventArgs e)
687         {
688             _ignoreConfigSave = true;
689             this.Visible = false;
690
691             if (MyApplication.StartupOptions.ContainsKey("d"))
692                 MyCommon.TraceFlag = true;
693
694             InitializeTraceFrag();
695
696             //Win32Api.SetProxy(HttpConnection.ProxyType.Specified, "127.0.0.1", 8080, "user", "pass")
697
698             MyCommon.TwitterApiInfo.AccessLimitUpdated += TwitterApiStatus_AccessLimitUpdated;
699             Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
700
701             Regex.CacheSize = 100;
702
703             //発言保持クラス
704             _statuses = TabInformations.GetInstance();
705
706             //アイコン設定
707             LoadIcons();
708             this.Icon = MainIcon;              //メインフォーム(TweenMain)
709             NotifyIcon1.Icon = NIconAt;      //タスクトレイ
710             TabImage.Images.Add(TabIcon);    //タブ見出し
711
712             //<<<<<<<<<設定関連>>>>>>>>>
713             ////設定読み出し
714             LoadConfig();
715
716             // 現在の DPI と設定保存時の DPI との比を取得する
717             var configScaleFactor = this._cfgLocal.GetConfigScaleFactor(this.CurrentAutoScaleDimensions);
718
719             // UIフォント設定
720             var fontUIGlobal = this._cfgLocal.FontUIGlobal;
721             if (fontUIGlobal != null)
722             {
723                 OTBaseForm.GlobalFont = fontUIGlobal;
724                 this.Font = fontUIGlobal;
725             }
726
727             //不正値チェック
728             if (!MyApplication.StartupOptions.ContainsKey("nolimit"))
729             {
730                 if (this._cfgCommon.TimelinePeriod < 15 && this._cfgCommon.TimelinePeriod > 0)
731                     this._cfgCommon.TimelinePeriod = 15;
732
733                 if (this._cfgCommon.ReplyPeriod < 15 && this._cfgCommon.ReplyPeriod > 0)
734                     this._cfgCommon.ReplyPeriod = 15;
735
736                 if (this._cfgCommon.DMPeriod < 15 && this._cfgCommon.DMPeriod > 0)
737                     this._cfgCommon.DMPeriod = 15;
738
739                 if (this._cfgCommon.PubSearchPeriod < 30 && this._cfgCommon.PubSearchPeriod > 0)
740                     this._cfgCommon.PubSearchPeriod = 30;
741
742                 if (this._cfgCommon.UserTimelinePeriod < 15 && this._cfgCommon.UserTimelinePeriod > 0)
743                     this._cfgCommon.UserTimelinePeriod = 15;
744
745                 if (this._cfgCommon.ListsPeriod < 15 && this._cfgCommon.ListsPeriod > 0)
746                     this._cfgCommon.ListsPeriod = 15;
747             }
748
749             if (!Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.Timeline, this._cfgCommon.CountApi))
750                 this._cfgCommon.CountApi = 60;
751             if (!Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.Reply, this._cfgCommon.CountApiReply))
752                 this._cfgCommon.CountApiReply = 40;
753
754             if (this._cfgCommon.MoreCountApi != 0 && !Twitter.VerifyMoreApiResultCount(this._cfgCommon.MoreCountApi))
755                 this._cfgCommon.MoreCountApi = 200;
756             if (this._cfgCommon.FirstCountApi != 0 && !Twitter.VerifyFirstApiResultCount(this._cfgCommon.FirstCountApi))
757                 this._cfgCommon.FirstCountApi = 100;
758
759             if (this._cfgCommon.FavoritesCountApi != 0 && !Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.Favorites, this._cfgCommon.FavoritesCountApi))
760                 this._cfgCommon.FavoritesCountApi = 40;
761             if (this._cfgCommon.ListCountApi != 0 && !Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.List, this._cfgCommon.ListCountApi))
762                 this._cfgCommon.ListCountApi = 100;
763             if (this._cfgCommon.SearchCountApi != 0 && !Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.PublicSearch, this._cfgCommon.SearchCountApi))
764                 this._cfgCommon.SearchCountApi = 100;
765             if (this._cfgCommon.UserTimelineCountApi != 0 && !Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.UserTimeline, this._cfgCommon.UserTimelineCountApi))
766                 this._cfgCommon.UserTimelineCountApi = 20;
767
768             //廃止サービスが選択されていた場合ux.nuへ読み替え
769             if (this._cfgCommon.AutoShortUrlFirst < 0)
770                 this._cfgCommon.AutoShortUrlFirst = MyCommon.UrlConverter.Uxnu;
771
772             TwitterApiConnection.RestApiHost = this._cfgCommon.TwitterApiHost;
773             this.tw = new Twitter(this.twitterApi);
774
775             //認証関連
776             if (string.IsNullOrEmpty(this._cfgCommon.Token)) this._cfgCommon.UserName = "";
777             tw.Initialize(this._cfgCommon.Token, this._cfgCommon.TokenSecret, this._cfgCommon.UserName, this._cfgCommon.UserId);
778
779             _initial = true;
780
781             Networking.Initialize();
782
783             bool saveRequired = false;
784             bool firstRun = false;
785
786             //ユーザー名、パスワードが未設定なら設定画面を表示(初回起動時など)
787             if (string.IsNullOrEmpty(tw.Username))
788             {
789                 saveRequired = true;
790                 firstRun = true;
791
792                 //設定せずにキャンセルされたか、設定されたが依然ユーザー名が未設定ならプログラム終了
793                 if (ShowSettingDialog(showTaskbarIcon: true) != DialogResult.OK ||
794                     string.IsNullOrEmpty(tw.Username))
795                 {
796                     Application.Exit();  //強制終了
797                     return;
798                 }
799             }
800
801             //Twitter用通信クラス初期化
802             Networking.DefaultTimeout = TimeSpan.FromSeconds(this._cfgCommon.DefaultTimeOut);
803             Networking.SetWebProxy(this._cfgLocal.ProxyType,
804                 this._cfgLocal.ProxyAddress, this._cfgLocal.ProxyPort,
805                 this._cfgLocal.ProxyUser, this._cfgLocal.ProxyPassword);
806             Networking.ForceIPv4 = this._cfgCommon.ForceIPv4;
807
808             TwitterApiConnection.RestApiHost = this._cfgCommon.TwitterApiHost;
809             tw.RestrictFavCheck = this._cfgCommon.RestrictFavCheck;
810             tw.ReadOwnPost = this._cfgCommon.ReadOwnPost;
811             tw.TrackWord = this._cfgCommon.TrackWord;
812             TrackToolStripMenuItem.Checked = !String.IsNullOrEmpty(tw.TrackWord);
813             tw.AllAtReply = this._cfgCommon.AllAtReply;
814             AllrepliesToolStripMenuItem.Checked = tw.AllAtReply;
815             ShortUrl.Instance.DisableExpanding = !this._cfgCommon.TinyUrlResolve;
816             ShortUrl.Instance.BitlyId = this._cfgCommon.BilyUser;
817             ShortUrl.Instance.BitlyKey = this._cfgCommon.BitlyPwd;
818
819             // アクセストークンが有効であるか確認する
820             // ここが Twitter API への最初のアクセスになるようにすること
821             try
822             {
823                 this.tw.VerifyCredentials();
824             }
825             catch (WebApiException ex)
826             {
827                 MessageBox.Show(this, string.Format(Properties.Resources.StartupAuthError_Text, ex.Message),
828                     Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
829             }
830
831             //サムネイル関連の初期化
832             //プロキシ設定等の通信まわりの初期化が済んでから処理する
833             ThumbnailGenerator.InitializeGenerator();
834
835             var imgazyobizinet = ThumbnailGenerator.ImgAzyobuziNetInstance;
836             imgazyobizinet.Enabled = this._cfgCommon.EnableImgAzyobuziNet;
837             imgazyobizinet.DisabledInDM = this._cfgCommon.ImgAzyobuziNetDisabledInDM;
838
839             Thumbnail.Services.TonTwitterCom.GetApiConnection = () => this.twitterApi.Connection;
840
841             //画像投稿サービス
842             ImageSelector.Initialize(tw, this.tw.Configuration, _cfgCommon.UseImageServiceName, _cfgCommon.UseImageService);
843
844             //ハッシュタグ/@id関連
845             AtIdSupl = new AtIdSupplement(SettingAtIdList.Load().AtIdList, "@");
846             HashSupl = new AtIdSupplement(_cfgCommon.HashTags, "#");
847             HashMgr = new HashtagManage(HashSupl,
848                                     _cfgCommon.HashTags.ToArray(),
849                                     _cfgCommon.HashSelected,
850                                     _cfgCommon.HashIsPermanent,
851                                     _cfgCommon.HashIsHead,
852                                     _cfgCommon.HashIsNotAddToAtReply);
853             if (!string.IsNullOrEmpty(HashMgr.UseHash) && HashMgr.IsPermanent) HashStripSplitButton.Text = HashMgr.UseHash;
854
855             //アイコンリスト作成
856             this.IconCache = new ImageCache();
857             this.tweetDetailsView.IconCache = this.IconCache;
858
859             //フォント&文字色&背景色保持
860             _fntUnread = this._cfgLocal.FontUnread;
861             _clUnread = this._cfgLocal.ColorUnread;
862             _fntReaded = this._cfgLocal.FontRead;
863             _clReaded = this._cfgLocal.ColorRead;
864             _clFav = this._cfgLocal.ColorFav;
865             _clOWL = this._cfgLocal.ColorOWL;
866             _clRetweet = this._cfgLocal.ColorRetweet;
867             _fntDetail = this._cfgLocal.FontDetail;
868             _clDetail = this._cfgLocal.ColorDetail;
869             _clDetailLink = this._cfgLocal.ColorDetailLink;
870             _clDetailBackcolor = this._cfgLocal.ColorDetailBackcolor;
871             _clSelf = this._cfgLocal.ColorSelf;
872             _clAtSelf = this._cfgLocal.ColorAtSelf;
873             _clTarget = this._cfgLocal.ColorTarget;
874             _clAtTarget = this._cfgLocal.ColorAtTarget;
875             _clAtFromTarget = this._cfgLocal.ColorAtFromTarget;
876             _clAtTo = this._cfgLocal.ColorAtTo;
877             _clListBackcolor = this._cfgLocal.ColorListBackcolor;
878             _clInputBackcolor = this._cfgLocal.ColorInputBackcolor;
879             _clInputFont = this._cfgLocal.ColorInputFont;
880             _fntInputFont = this._cfgLocal.FontInputFont;
881
882             _brsBackColorMine = new SolidBrush(_clSelf);
883             _brsBackColorAt = new SolidBrush(_clAtSelf);
884             _brsBackColorYou = new SolidBrush(_clTarget);
885             _brsBackColorAtYou = new SolidBrush(_clAtTarget);
886             _brsBackColorAtFromTarget = new SolidBrush(_clAtFromTarget);
887             _brsBackColorAtTo = new SolidBrush(_clAtTo);
888             //_brsBackColorNone = new SolidBrush(Color.FromKnownColor(KnownColor.Window));
889             _brsBackColorNone = new SolidBrush(_clListBackcolor);
890
891             // StringFormatオブジェクトへの事前設定
892             //sf.Alignment = StringAlignment.Near;             // Textを近くへ配置(左から右の場合は左寄せ)
893             //sf.LineAlignment = StringAlignment.Near;         // Textを近くへ配置(上寄せ)
894             //sf.FormatFlags = StringFormatFlags.LineLimit;    // 
895             sfTab.Alignment = StringAlignment.Center;
896             sfTab.LineAlignment = StringAlignment.Center;
897
898             InitDetailHtmlFormat();
899
900             //Regex statregex = new Regex("^0*");
901             this.recommendedStatusFooter = " [TWNv" + Regex.Replace(MyCommon.FileVersion.Replace(".", ""), "^0*", "") + "]";
902
903             _history.Add(new PostingStatus());
904             _hisIdx = 0;
905             this.inReplyTo = null;
906
907             //各種ダイアログ設定
908             SearchDialog.Owner = this;
909             UrlDialog.Owner = this;
910
911             //新着バルーン通知のチェック状態設定
912             NewPostPopMenuItem.Checked = _cfgCommon.NewAllPop;
913             this.NotifyFileMenuItem.Checked = NewPostPopMenuItem.Checked;
914
915             //新着取得時のリストスクロールをするか。trueならスクロールしない
916             ListLockMenuItem.Checked = _cfgCommon.ListLock;
917             this.LockListFileMenuItem.Checked = _cfgCommon.ListLock;
918             //サウンド再生(タブ別設定より優先)
919             this.PlaySoundMenuItem.Checked = this._cfgCommon.PlaySound;
920             this.PlaySoundFileMenuItem.Checked = this._cfgCommon.PlaySound;
921
922             this.IdeographicSpaceToSpaceToolStripMenuItem.Checked = _cfgCommon.WideSpaceConvert;
923             this.ToolStripFocusLockMenuItem.Checked = _cfgCommon.FocusLockToStatusText;
924
925             //ウィンドウ設定
926             this.ClientSize = ScaleBy(configScaleFactor, _cfgLocal.FormSize);
927             _mySize = this.ClientSize; // サイズ保持(最小化・最大化されたまま終了した場合の対応用)
928             _myLoc = _cfgLocal.FormLocation;
929             //タイトルバー領域
930             if (this.WindowState != FormWindowState.Minimized)
931             {
932                 this.DesktopLocation = _cfgLocal.FormLocation;
933                 Rectangle tbarRect = new Rectangle(this.Location, new Size(_mySize.Width, SystemInformation.CaptionHeight));
934                 bool outOfScreen = true;
935                 if (Screen.AllScreens.Length == 1)    //ハングするとの報告
936                 {
937                     foreach (Screen scr in Screen.AllScreens)
938                     {
939                         if (!Rectangle.Intersect(tbarRect, scr.Bounds).IsEmpty)
940                         {
941                             outOfScreen = false;
942                             break;
943                         }
944                     }
945                     if (outOfScreen)
946                     {
947                         this.DesktopLocation = new Point(0, 0);
948                         _myLoc = this.DesktopLocation;
949                     }
950                 }
951             }
952             this.TopMost = this._cfgCommon.AlwaysTop;
953             _mySpDis = ScaleBy(configScaleFactor.Height, _cfgLocal.SplitterDistance);
954             _mySpDis2 = ScaleBy(configScaleFactor.Height, _cfgLocal.StatusTextHeight);
955             if (_cfgLocal.PreviewDistance == -1)
956             {
957                 _mySpDis3 = _mySize.Width - ScaleBy(this.CurrentScaleFactor.Width, 150);
958                 if (_mySpDis3 < 1) _mySpDis3 = ScaleBy(this.CurrentScaleFactor.Width, 50);
959                 _cfgLocal.PreviewDistance = _mySpDis3;
960             }
961             else
962             {
963                 _mySpDis3 = ScaleBy(configScaleFactor.Width, _cfgLocal.PreviewDistance);
964             }
965             MultiLineMenuItem.Checked = _cfgLocal.StatusMultiline;
966             //this.Tween_ClientSizeChanged(this, null);
967             this.PlaySoundMenuItem.Checked = this._cfgCommon.PlaySound;
968             this.PlaySoundFileMenuItem.Checked = this._cfgCommon.PlaySound;
969             //入力欄
970             StatusText.Font = _fntInputFont;
971             StatusText.ForeColor = _clInputFont;
972
973             // SplitContainer2.Panel2MinSize を一行表示の入力欄の高さに合わせる (MS UI Gothic 12pt (96dpi) の場合は 19px)
974             this.StatusText.Multiline = false; // _cfgLocal.StatusMultiline の設定は後で反映される
975             this.SplitContainer2.Panel2MinSize = this.StatusText.Height;
976
977             // 必要であれば、発言一覧と発言詳細部・入力欄の上下を入れ替える
978             SplitContainer1.IsPanelInverted = !this._cfgCommon.StatusAreaAtBottom;
979
980             //全新着通知のチェック状態により、Reply&DMの新着通知有効無効切り替え(タブ別設定にするため削除予定)
981             if (this._cfgCommon.UnreadManage == false)
982             {
983                 ReadedStripMenuItem.Enabled = false;
984                 UnreadStripMenuItem.Enabled = false;
985             }
986
987             //リンク先URL表示部の初期化(画面左下)
988             StatusLabelUrl.Text = "";
989             //状態表示部の初期化(画面右下)
990             StatusLabel.Text = "";
991             StatusLabel.AutoToolTip = false;
992             StatusLabel.ToolTipText = "";
993             //文字カウンタ初期化
994             lblLen.Text = this.GetRestStatusCount(this.FormatStatusText("")).ToString();
995
996             this.JumpReadOpMenuItem.ShortcutKeyDisplayString = "Space";
997             CopySTOTMenuItem.ShortcutKeyDisplayString = "Ctrl+C";
998             CopyURLMenuItem.ShortcutKeyDisplayString = "Ctrl+Shift+C";
999             CopyUserIdStripMenuItem.ShortcutKeyDisplayString = "Shift+Alt+C";
1000
1001             // SourceLinkLabel のテキストが SplitContainer2.Panel2.AccessibleName にセットされるのを防ぐ
1002             // (タブオーダー順で SourceLinkLabel の次にある PostBrowser が TabStop = false となっているため、
1003             // さらに次のコントロールである SplitContainer2.Panel2 の AccessibleName がデフォルトで SourceLinkLabel のテキストになってしまう)
1004             this.SplitContainer2.Panel2.AccessibleName = "";
1005
1006             ////////////////////////////////////////////////////////////////////////////////
1007             var sortOrder = (SortOrder)_cfgCommon.SortOrder;
1008             var mode = ComparerMode.Id;
1009             switch (_cfgCommon.SortColumn)
1010             {
1011                 case 0:    //0:アイコン,5:未読マーク,6:プロテクト・フィルターマーク
1012                 case 5:
1013                 case 6:
1014                     //ソートしない
1015                     mode = ComparerMode.Id;  //Idソートに読み替え
1016                     break;
1017                 case 1:  //ニックネーム
1018                     mode = ComparerMode.Nickname;
1019                     break;
1020                 case 2:  //本文
1021                     mode = ComparerMode.Data;
1022                     break;
1023                 case 3:  //時刻=発言Id
1024                     mode = ComparerMode.Id;
1025                     break;
1026                 case 4:  //名前
1027                     mode = ComparerMode.Name;
1028                     break;
1029                 case 7:  //Source
1030                     mode = ComparerMode.Source;
1031                     break;
1032             }
1033             _statuses.SetSortMode(mode, sortOrder);
1034             ////////////////////////////////////////////////////////////////////////////////
1035
1036             ApplyListViewIconSize(this._cfgCommon.IconSize);
1037
1038             //<<<<<<<<タブ関連>>>>>>>
1039             // タブの位置を調整する
1040             SetTabAlignment();
1041
1042             //デフォルトタブの存在チェック、ない場合には追加
1043             if (this._statuses.GetTabByType<HomeTabModel>() == null)
1044                 this._statuses.AddTab(new HomeTabModel());
1045
1046             if (this._statuses.GetTabByType<MentionsTabModel>() == null)
1047                 this._statuses.AddTab(new MentionsTabModel());
1048
1049             if (this._statuses.GetTabByType<DirectMessagesTabModel>() == null)
1050                 this._statuses.AddTab(new DirectMessagesTabModel());
1051
1052             if (this._statuses.GetTabByType<FavoritesTabModel>() == null)
1053                 this._statuses.AddTab(new FavoritesTabModel());
1054
1055             if (this._statuses.GetTabByType<MuteTabModel>() == null)
1056                 this._statuses.AddTab(new MuteTabModel());
1057
1058             foreach (var tab in _statuses.Tabs.Values)
1059             {
1060                 // ミュートタブは表示しない
1061                 if (tab.TabType == MyCommon.TabUsageType.Mute)
1062                     continue;
1063
1064                 if (!AddNewTab(tab, startup: true))
1065                     throw new TabException(Properties.Resources.TweenMain_LoadText1);
1066             }
1067
1068             _curTab = ListTab.SelectedTab;
1069             _curItemIndex = -1;
1070             _curList = (DetailsListView)_curTab.Tag;
1071
1072             if (this._cfgCommon.TabIconDisp)
1073             {
1074                 ListTab.DrawMode = TabDrawMode.Normal;
1075             }
1076             else
1077             {
1078                 ListTab.DrawMode = TabDrawMode.OwnerDrawFixed;
1079                 ListTab.DrawItem += ListTab_DrawItem;
1080                 ListTab.ImageList = null;
1081             }
1082
1083             if (this._cfgCommon.HotkeyEnabled)
1084             {
1085                 //////グローバルホットキーの登録
1086                 HookGlobalHotkey.ModKeys modKey = HookGlobalHotkey.ModKeys.None;
1087                 if ((this._cfgCommon.HotkeyModifier & Keys.Alt) == Keys.Alt)
1088                     modKey |= HookGlobalHotkey.ModKeys.Alt;
1089                 if ((this._cfgCommon.HotkeyModifier & Keys.Control) == Keys.Control)
1090                     modKey |= HookGlobalHotkey.ModKeys.Ctrl;
1091                 if ((this._cfgCommon.HotkeyModifier & Keys.Shift) == Keys.Shift)
1092                     modKey |= HookGlobalHotkey.ModKeys.Shift;
1093                 if ((this._cfgCommon.HotkeyModifier & Keys.LWin) == Keys.LWin)
1094                     modKey |= HookGlobalHotkey.ModKeys.Win;
1095
1096                 _hookGlobalHotkey.RegisterOriginalHotkey(this._cfgCommon.HotkeyKey, this._cfgCommon.HotkeyValue, modKey);
1097             }
1098
1099             if (this._cfgCommon.IsUseNotifyGrowl)
1100                 gh.RegisterGrowl();
1101
1102             StatusLabel.Text = Properties.Resources.Form1_LoadText1;       //画面右下の状態表示を変更
1103
1104             SetMainWindowTitle();
1105             SetNotifyIconText();
1106
1107             if (!this._cfgCommon.MinimizeToTray || this.WindowState != FormWindowState.Minimized)
1108             {
1109                 this.Visible = true;
1110             }
1111
1112             //タイマー設定
1113             TimerTimeline.AutoReset = true;
1114             TimerTimeline.SynchronizingObject = this;
1115             //Recent取得間隔
1116             TimerTimeline.Interval = 1000;
1117             TimerTimeline.Enabled = true;
1118             //更新中アイコンアニメーション間隔
1119             TimerRefreshIcon.Interval = 200;
1120             TimerRefreshIcon.Enabled = true;
1121
1122             _ignoreConfigSave = false;
1123             this.TweenMain_Resize(null, null);
1124             if (saveRequired) SaveConfigsAll(false);
1125
1126             foreach (var ua in this._cfgCommon.UserAccounts)
1127             {
1128                 if (ua.UserId == 0 && ua.Username.ToLowerInvariant() == tw.Username.ToLowerInvariant())
1129                 {
1130                     ua.UserId = tw.UserId;
1131                     break;
1132                 }
1133             }
1134
1135             if (firstRun)
1136             {
1137                 // 初回起動時だけ右下のメニューを目立たせる
1138                 HashStripSplitButton.ShowDropDown();
1139             }
1140         }
1141
1142         private void InitDetailHtmlFormat()
1143         {
1144             if (this._cfgCommon.IsMonospace)
1145             {
1146                 detailHtmlFormatHeader = detailHtmlFormatHeaderMono;
1147                 detailHtmlFormatFooter = detailHtmlFormatFooterMono;
1148             }
1149             else
1150             {
1151                 detailHtmlFormatHeader = detailHtmlFormatHeaderColor;
1152                 detailHtmlFormatFooter = detailHtmlFormatFooterColor;
1153             }
1154
1155             detailHtmlFormatHeader = detailHtmlFormatHeader
1156                     .Replace("%FONT_FAMILY%", _fntDetail.Name)
1157                     .Replace("%FONT_SIZE%", _fntDetail.Size.ToString())
1158                     .Replace("%FONT_COLOR%", $"{_clDetail.R},{_clDetail.G},{_clDetail.B}")
1159                     .Replace("%LINK_COLOR%", $"{_clDetailLink.R},{_clDetailLink.G},{_clDetailLink.B}")
1160                     .Replace("%BG_COLOR%", $"{_clDetailBackcolor.R},{_clDetailBackcolor.G},{_clDetailBackcolor.B}")
1161                     .Replace("%BG_REPLY_COLOR%", $"{_clAtTo.R}, {_clAtTo.G}, {_clAtTo.B}");
1162         }
1163
1164         private void ListTab_DrawItem(object sender, DrawItemEventArgs e)
1165         {
1166             string txt;
1167             try
1168             {
1169                 txt = ListTab.TabPages[e.Index].Text;
1170             }
1171             catch (Exception)
1172             {
1173                 return;
1174             }
1175
1176             e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Control, e.Bounds);
1177             if (e.State == DrawItemState.Selected)
1178             {
1179                 e.DrawFocusRectangle();
1180             }
1181             Brush fore;
1182             try
1183             {
1184                 if (_statuses.Tabs[txt].UnreadCount > 0)
1185                     fore = Brushes.Red;
1186                 else
1187                     fore = System.Drawing.SystemBrushes.ControlText;
1188             }
1189             catch (Exception)
1190             {
1191                 fore = System.Drawing.SystemBrushes.ControlText;
1192             }
1193             e.Graphics.DrawString(txt, e.Font, fore, e.Bounds, sfTab);
1194         }
1195
1196         private void LoadConfig()
1197         {
1198             _cfgCommon = SettingCommon.Load();
1199             SettingCommon.Instance = this._cfgCommon;
1200             if (_cfgCommon.UserAccounts == null || _cfgCommon.UserAccounts.Count == 0)
1201             {
1202                 _cfgCommon.UserAccounts = new List<UserAccount>();
1203                 if (!string.IsNullOrEmpty(_cfgCommon.UserName))
1204                 {
1205                     UserAccount account = new UserAccount();
1206                     account.Username = _cfgCommon.UserName;
1207                     account.UserId = _cfgCommon.UserId;
1208                     account.Token = _cfgCommon.Token;
1209                     account.TokenSecret = _cfgCommon.TokenSecret;
1210
1211                     _cfgCommon.UserAccounts.Add(account);
1212                 }
1213             }
1214
1215             _cfgLocal = SettingLocal.Load();
1216
1217             // v1.2.4 以前の設定には ScaleDimension の項目がないため、現在の DPI と同じとして扱う
1218             if (_cfgLocal.ScaleDimension.IsEmpty)
1219                 _cfgLocal.ScaleDimension = this.CurrentAutoScaleDimensions;
1220
1221             var tabsSetting = SettingTabs.Load().Tabs;
1222             foreach (var tabSetting in tabsSetting)
1223             {
1224                 TabModel tab;
1225                 switch (tabSetting.TabType)
1226                 {
1227                     case MyCommon.TabUsageType.Home:
1228                         tab = new HomeTabModel(tabSetting.TabName);
1229                         break;
1230                     case MyCommon.TabUsageType.Mentions:
1231                         tab = new MentionsTabModel(tabSetting.TabName);
1232                         break;
1233                     case MyCommon.TabUsageType.DirectMessage:
1234                         tab = new DirectMessagesTabModel(tabSetting.TabName);
1235                         break;
1236                     case MyCommon.TabUsageType.Favorites:
1237                         tab = new FavoritesTabModel(tabSetting.TabName);
1238                         break;
1239                     case MyCommon.TabUsageType.UserDefined:
1240                         tab = new FilterTabModel(tabSetting.TabName);
1241                         break;
1242                     case MyCommon.TabUsageType.UserTimeline:
1243                         tab = new UserTimelineTabModel(tabSetting.TabName, tabSetting.User);
1244                         break;
1245                     case MyCommon.TabUsageType.PublicSearch:
1246                         tab = new PublicSearchTabModel(tabSetting.TabName)
1247                         {
1248                             SearchWords = tabSetting.SearchWords,
1249                             SearchLang = tabSetting.SearchLang,
1250                         };
1251                         break;
1252                     case MyCommon.TabUsageType.Lists:
1253                         tab = new ListTimelineTabModel(tabSetting.TabName, tabSetting.ListInfo);
1254                         break;
1255                     case MyCommon.TabUsageType.Mute:
1256                         tab = new MuteTabModel(tabSetting.TabName);
1257                         break;
1258                     default:
1259                         continue;
1260                 }
1261
1262                 tab.UnreadManage = tabSetting.UnreadManage;
1263                 tab.Protected = tabSetting.Protected;
1264                 tab.Notify = tabSetting.Notify;
1265                 tab.SoundFile = tabSetting.SoundFile;
1266
1267                 if (tab.IsDistributableTabType)
1268                 {
1269                     var filterTab = (FilterTabModel)tab;
1270                     filterTab.FilterArray = tabSetting.FilterArray;
1271                     filterTab.FilterModified = false;
1272                 }
1273
1274                 if (this._statuses.ContainsTab(tab.TabName))
1275                     tab.TabName = this._statuses.MakeTabName("MyTab");
1276
1277                 this._statuses.AddTab(tab);
1278             }
1279             if (_statuses.Tabs.Count == 0)
1280             {
1281                 _statuses.AddTab(new HomeTabModel());
1282                 _statuses.AddTab(new MentionsTabModel());
1283                 _statuses.AddTab(new DirectMessagesTabModel());
1284                 _statuses.AddTab(new FavoritesTabModel());
1285             }
1286         }
1287
1288         private void TimerInterval_Changed(object sender, IntervalChangedEventArgs e) //Handles SettingDialog.IntervalChanged
1289         {
1290             if (!TimerTimeline.Enabled) return;
1291             ResetTimers = e;
1292         }
1293
1294         private IntervalChangedEventArgs ResetTimers = IntervalChangedEventArgs.ResetAll;
1295
1296         private static int homeCounter = 0;
1297         private static int mentionCounter = 0;
1298         private static int dmCounter = 0;
1299         private static int pubSearchCounter = 0;
1300         private static int userTimelineCounter = 0;
1301         private static int listsCounter = 0;
1302         private static int usCounter = 0;
1303         private static int ResumeWait = 0;
1304         private static int refreshFollowers = 0;
1305
1306         private async void TimerTimeline_Elapsed(object sender, EventArgs e)
1307         {
1308             if (homeCounter > 0) Interlocked.Decrement(ref homeCounter);
1309             if (mentionCounter > 0) Interlocked.Decrement(ref mentionCounter);
1310             if (dmCounter > 0) Interlocked.Decrement(ref dmCounter);
1311             if (pubSearchCounter > 0) Interlocked.Decrement(ref pubSearchCounter);
1312             if (userTimelineCounter > 0) Interlocked.Decrement(ref userTimelineCounter);
1313             if (listsCounter > 0) Interlocked.Decrement(ref listsCounter);
1314             if (usCounter > 0) Interlocked.Decrement(ref usCounter);
1315             Interlocked.Increment(ref refreshFollowers);
1316
1317             var refreshTasks = new List<Task>();
1318
1319             ////タイマー初期化
1320             if (ResetTimers.Timeline || homeCounter <= 0 && this._cfgCommon.TimelinePeriod > 0)
1321             {
1322                 Interlocked.Exchange(ref homeCounter, this._cfgCommon.TimelinePeriod);
1323                 if (!tw.IsUserstreamDataReceived && !ResetTimers.Timeline)
1324                     refreshTasks.Add(this.GetHomeTimelineAsync());
1325                 ResetTimers.Timeline = false;
1326             }
1327             if (ResetTimers.Reply || mentionCounter <= 0 && this._cfgCommon.ReplyPeriod > 0)
1328             {
1329                 Interlocked.Exchange(ref mentionCounter, this._cfgCommon.ReplyPeriod);
1330                 if (!tw.IsUserstreamDataReceived && !ResetTimers.Reply)
1331                     refreshTasks.Add(this.GetReplyAsync());
1332                 ResetTimers.Reply = false;
1333             }
1334             if (ResetTimers.DirectMessage || dmCounter <= 0 && this._cfgCommon.DMPeriod > 0)
1335             {
1336                 Interlocked.Exchange(ref dmCounter, this._cfgCommon.DMPeriod);
1337                 if (!tw.IsUserstreamDataReceived && !ResetTimers.DirectMessage)
1338                     refreshTasks.Add(this.GetDirectMessagesAsync());
1339                 ResetTimers.DirectMessage = false;
1340             }
1341             if (ResetTimers.PublicSearch || pubSearchCounter <= 0 && this._cfgCommon.PubSearchPeriod > 0)
1342             {
1343                 Interlocked.Exchange(ref pubSearchCounter, this._cfgCommon.PubSearchPeriod);
1344                 if (!ResetTimers.PublicSearch)
1345                     refreshTasks.Add(this.GetPublicSearchAllAsync());
1346                 ResetTimers.PublicSearch = false;
1347             }
1348             if (ResetTimers.UserTimeline || userTimelineCounter <= 0 && this._cfgCommon.UserTimelinePeriod > 0)
1349             {
1350                 Interlocked.Exchange(ref userTimelineCounter, this._cfgCommon.UserTimelinePeriod);
1351                 if (!ResetTimers.UserTimeline)
1352                     refreshTasks.Add(this.GetUserTimelineAllAsync());
1353                 ResetTimers.UserTimeline = false;
1354             }
1355             if (ResetTimers.Lists || listsCounter <= 0 && this._cfgCommon.ListsPeriod > 0)
1356             {
1357                 Interlocked.Exchange(ref listsCounter, this._cfgCommon.ListsPeriod);
1358                 if (!ResetTimers.Lists)
1359                     refreshTasks.Add(this.GetListTimelineAllAsync());
1360                 ResetTimers.Lists = false;
1361             }
1362             if (ResetTimers.UserStream || usCounter <= 0 && this._cfgCommon.UserstreamPeriod > 0)
1363             {
1364                 Interlocked.Exchange(ref usCounter, this._cfgCommon.UserstreamPeriod);
1365                 if (this.tw.UserStreamActive)
1366                     this.RefreshTimeline();
1367                 ResetTimers.UserStream = false;
1368             }
1369             if (refreshFollowers > 6 * 3600)
1370             {
1371                 Interlocked.Exchange(ref refreshFollowers, 0);
1372                 refreshTasks.AddRange(new[]
1373                 {
1374                     this.doGetFollowersMenu(),
1375                     this.RefreshNoRetweetIdsAsync(),
1376                     this.RefreshTwitterConfigurationAsync(),
1377                 });
1378             }
1379             if (osResumed)
1380             {
1381                 Interlocked.Increment(ref ResumeWait);
1382                 if (ResumeWait > 30)
1383                 {
1384                     osResumed = false;
1385                     Interlocked.Exchange(ref ResumeWait, 0);
1386                     refreshTasks.AddRange(new[]
1387                     {
1388                         this.GetHomeTimelineAsync(),
1389                         this.GetReplyAsync(),
1390                         this.GetDirectMessagesAsync(),
1391                         this.GetPublicSearchAllAsync(),
1392                         this.GetUserTimelineAllAsync(),
1393                         this.GetListTimelineAllAsync(),
1394                         this.doGetFollowersMenu(),
1395                         this.RefreshTwitterConfigurationAsync(),
1396                     });
1397                 }
1398             }
1399
1400             await Task.WhenAll(refreshTasks);
1401         }
1402
1403         private void RefreshTimeline()
1404         {
1405             // 現在表示中のタブのスクロール位置を退避
1406             var curListScroll = this.SaveListViewScroll(this._curList, this._statuses.Tabs[this._curTab.Text]);
1407
1408             // 各タブのリスト上の選択位置などを退避
1409             var listSelections = this.SaveListViewSelection();
1410
1411             //更新確定
1412             PostClass[] notifyPosts;
1413             string soundFile;
1414             int addCount;
1415             bool newMentionOrDm;
1416             bool isDelete;
1417             addCount = _statuses.SubmitUpdate(out soundFile, out notifyPosts, out newMentionOrDm, out isDelete);
1418
1419             if (MyCommon._endingFlag) return;
1420
1421             //リストに反映&選択状態復元
1422             try
1423             {
1424                 foreach (TabPage tab in ListTab.TabPages)
1425                 {
1426                     DetailsListView lst = (DetailsListView)tab.Tag;
1427                     TabModel tabInfo = _statuses.Tabs[tab.Text];
1428                     if (isDelete || lst.VirtualListSize != tabInfo.AllCount)
1429                     {
1430                         using (ControlTransaction.Update(lst))
1431                         {
1432                             if (lst.Equals(_curList))
1433                             {
1434                                 this.PurgeListViewItemCache();
1435                             }
1436                             try
1437                             {
1438                                 lst.VirtualListSize = tabInfo.AllCount; //リスト件数更新
1439                             }
1440                             catch (Exception)
1441                             {
1442                                 //アイコン描画不具合あり?
1443                             }
1444
1445                             // 選択位置などを復元
1446                             this.RestoreListViewSelection(lst, tabInfo, listSelections[tabInfo.TabName]);
1447                         }
1448                     }
1449                     if (tabInfo.UnreadCount > 0)
1450                         if (this._cfgCommon.TabIconDisp)
1451                             if (tab.ImageIndex == -1) tab.ImageIndex = 0; //タブアイコン
1452                 }
1453                 if (!this._cfgCommon.TabIconDisp) ListTab.Refresh();
1454             }
1455             catch (Exception)
1456             {
1457                 //ex.Data["Msg"] = "Ref1, UseAPI=" + SettingDialog.UseAPI.ToString();
1458                 //throw;
1459             }
1460
1461             // スクロール位置を復元
1462             this.RestoreListViewScroll(this._curList, this._statuses.Tabs[this._curTab.Text], curListScroll);
1463
1464             //新着通知
1465             NotifyNewPosts(notifyPosts, soundFile, addCount, newMentionOrDm);
1466
1467             SetMainWindowTitle();
1468             if (!StatusLabelUrl.Text.StartsWith("http", StringComparison.Ordinal)) SetStatusLabelUrl();
1469
1470             HashSupl.AddRangeItem(tw.GetHashList());
1471
1472         }
1473
1474         internal struct ListViewScroll
1475         {
1476             public ScrollLockMode ScrollLockMode { get; set; }
1477             public long? TopItemStatusId { get; set; }
1478         }
1479
1480         internal enum ScrollLockMode
1481         {
1482             /// <summary>固定しない</summary>
1483             None,
1484
1485             /// <summary>最上部に固定する</summary>
1486             FixedToTop,
1487
1488             /// <summary>最下部に固定する</summary>
1489             FixedToBottom,
1490
1491             /// <summary><see cref="ListViewScroll.TopItemStatusId"/> の位置に固定する</summary>
1492             FixedToItem,
1493         }
1494
1495         /// <summary>
1496         /// <see cref="ListView"/> のスクロール位置に関する情報を <see cref="ListViewScroll"/> として返します
1497         /// </summary>
1498         private ListViewScroll SaveListViewScroll(DetailsListView listView, TabModel tab)
1499         {
1500             var listScroll = new ListViewScroll
1501             {
1502                 ScrollLockMode = this.GetScrollLockMode(listView),
1503             };
1504
1505             if (listScroll.ScrollLockMode == ScrollLockMode.FixedToItem)
1506             {
1507                 var topItem = listView.TopItem;
1508                 if (topItem != null)
1509                     listScroll.TopItemStatusId = tab.GetStatusIdAt(topItem.Index);
1510             }
1511
1512             return listScroll;
1513         }
1514
1515         private ScrollLockMode GetScrollLockMode(DetailsListView listView)
1516         {
1517             if (this._statuses.SortMode == ComparerMode.Id)
1518             {
1519                 if (this._statuses.SortOrder == SortOrder.Ascending)
1520                 {
1521                     // Id昇順
1522                     if (this.ListLockMenuItem.Checked)
1523                         return ScrollLockMode.None;
1524
1525                     // 最下行が表示されていたら、最下行へ強制スクロール。最下行が表示されていなかったら制御しない
1526
1527                     // 一番下に表示されているアイテム
1528                     var bottomItem = listView.GetItemAt(0, listView.ClientSize.Height - 1);
1529                     if (bottomItem == null || bottomItem.Index == listView.VirtualListSize - 1)
1530                         return ScrollLockMode.FixedToBottom;
1531                     else
1532                         return ScrollLockMode.None;
1533                 }
1534                 else
1535                 {
1536                     // Id降順
1537                     if (this.ListLockMenuItem.Checked)
1538                         return ScrollLockMode.FixedToItem;
1539
1540                     // 最上行が表示されていたら、制御しない。最上行が表示されていなかったら、現在表示位置へ強制スクロール
1541                     var topItem = listView.TopItem;
1542                     if (topItem == null || topItem.Index == 0)
1543                         return ScrollLockMode.FixedToTop;
1544                     else
1545                         return ScrollLockMode.FixedToItem;
1546                 }
1547             }
1548             else
1549             {
1550                 return ScrollLockMode.FixedToItem;
1551             }
1552         }
1553
1554         internal struct ListViewSelection
1555         {
1556             public long[] SelectedStatusIds { get; set; }
1557             public long? SelectionMarkStatusId { get; set; }
1558             public long? FocusedStatusId { get; set; }
1559         }
1560
1561         /// <summary>
1562         /// <see cref="ListView"/> の選択状態を <see cref="ListViewSelection"/> として返します
1563         /// </summary>
1564         private IReadOnlyDictionary<string, ListViewSelection> SaveListViewSelection()
1565         {
1566             var listsDict = new Dictionary<string, ListViewSelection>();
1567
1568             foreach (var tabPage in this.ListTab.TabPages.Cast<TabPage>())
1569             {
1570                 var listView = (DetailsListView)tabPage.Tag;
1571                 var tab = _statuses.Tabs[tabPage.Text];
1572
1573                 ListViewSelection listStatus;
1574                 if (listView.VirtualListSize != 0)
1575                 {
1576                     listStatus = new ListViewSelection
1577                     {
1578                         SelectedStatusIds = this.GetSelectedStatusIds(listView, tab),
1579                         FocusedStatusId = this.GetFocusedStatusId(listView, tab),
1580                         SelectionMarkStatusId = this.GetSelectionMarkStatusId(listView, tab),
1581                     };
1582                 }
1583                 else
1584                 {
1585                     listStatus = new ListViewSelection
1586                     {
1587                         SelectedStatusIds = new long[0],
1588                         SelectionMarkStatusId = null,
1589                         FocusedStatusId = null,
1590                     };
1591                 }
1592
1593                 listsDict[tab.TabName] = listStatus;
1594             }
1595
1596             return listsDict;
1597         }
1598
1599         private long[] GetSelectedStatusIds(DetailsListView listView, TabModel tab)
1600         {
1601             var selectedIndices = listView.SelectedIndices;
1602             if (selectedIndices.Count > 0 && selectedIndices.Count < 61)
1603                 return tab.GetStatusIdAt(selectedIndices.Cast<int>());
1604             else
1605                 return null;
1606         }
1607
1608         private long? GetFocusedStatusId(DetailsListView listView, TabModel tab)
1609         {
1610             var focusedItem = listView.FocusedItem;
1611
1612             return focusedItem != null ? tab.GetStatusIdAt(focusedItem.Index) : (long?)null;
1613         }
1614
1615         private long? GetSelectionMarkStatusId(DetailsListView listView, TabModel tab)
1616         {
1617             var selectionMarkIndex = listView.SelectionMark;
1618
1619             return selectionMarkIndex != -1 ? tab.GetStatusIdAt(selectionMarkIndex) : (long?)null;
1620         }
1621
1622         /// <summary>
1623         /// <see cref="SaveListViewScroll"/> によって保存されたスクロール位置を復元します
1624         /// </summary>
1625         private void RestoreListViewScroll(DetailsListView listView, TabModel tab, ListViewScroll listScroll)
1626         {
1627             if (listView.VirtualListSize == 0)
1628                 return;
1629
1630             switch (listScroll.ScrollLockMode)
1631             {
1632                 case ScrollLockMode.FixedToTop:
1633                     listView.EnsureVisible(0);
1634                     break;
1635                 case ScrollLockMode.FixedToBottom:
1636                     listView.EnsureVisible(listView.VirtualListSize - 1);
1637                     break;
1638                 case ScrollLockMode.FixedToItem:
1639                     var topIndex = listScroll.TopItemStatusId != null ? tab.IndexOf(listScroll.TopItemStatusId.Value) : -1;
1640                     if (topIndex != -1)
1641                         listView.TopItem = listView.Items[topIndex];
1642                     break;
1643                 case ScrollLockMode.None:
1644                 default:
1645                     break;
1646             }
1647         }
1648
1649         /// <summary>
1650         /// <see cref="SaveListViewSelection"/> によって保存された選択状態を復元します
1651         /// </summary>
1652         private void RestoreListViewSelection(DetailsListView listView, TabModel tab, ListViewSelection listSelection)
1653         {
1654             // status_id から ListView 上のインデックスに変換
1655             int[] selectedIndices = null;
1656             if (listSelection.SelectedStatusIds != null)
1657                 selectedIndices = tab.IndexOf(listSelection.SelectedStatusIds).Where(x => x != -1).ToArray();
1658
1659             var focusedIndex = -1;
1660             if (listSelection.FocusedStatusId != null)
1661                 focusedIndex = tab.IndexOf(listSelection.FocusedStatusId.Value);
1662
1663             var selectionMarkIndex = -1;
1664             if (listSelection.SelectionMarkStatusId != null)
1665                 selectionMarkIndex = tab.IndexOf(listSelection.SelectionMarkStatusId.Value);
1666
1667             this.SelectListItem(listView, selectedIndices, focusedIndex, selectionMarkIndex);
1668         }
1669
1670         private bool BalloonRequired()
1671         {
1672             Twitter.FormattedEvent ev = new Twitter.FormattedEvent();
1673             ev.Eventtype = MyCommon.EVENTTYPE.None;
1674
1675             return BalloonRequired(ev);
1676         }
1677
1678         private bool IsEventNotifyAsEventType(MyCommon.EVENTTYPE type)
1679         {
1680             if (type == MyCommon.EVENTTYPE.None)
1681                 return true;
1682
1683             if (!this._cfgCommon.EventNotifyEnabled)
1684                 return false;
1685
1686             return this._cfgCommon.EventNotifyFlag.HasFlag(type);
1687         }
1688
1689         private bool IsMyEventNotityAsEventType(Twitter.FormattedEvent ev)
1690         {
1691             if (!ev.IsMe)
1692                 return true;
1693
1694             return this._cfgCommon.IsMyEventNotifyFlag.HasFlag(ev.Eventtype);
1695         }
1696
1697         private bool BalloonRequired(Twitter.FormattedEvent ev)
1698         {
1699             if (this._initial)
1700                 return false;
1701
1702             if (NativeMethods.IsScreenSaverRunning())
1703                 return false;
1704
1705             // 「新着通知」が無効
1706             if (!this.NewPostPopMenuItem.Checked)
1707             {
1708                 // 「新着通知が無効でもイベントを通知する」にも該当しない
1709                 if (!this._cfgCommon.ForceEventNotify || ev.Eventtype == MyCommon.EVENTTYPE.None)
1710                     return false;
1711             }
1712
1713             // 「画面最小化・アイコン時のみバルーンを表示する」が有効
1714             if (this._cfgCommon.LimitBalloon)
1715             {
1716                 if (this.WindowState != FormWindowState.Minimized && this.Visible && Form.ActiveForm != null)
1717                     return false;
1718             }
1719
1720             return this.IsEventNotifyAsEventType(ev.Eventtype) && this.IsMyEventNotityAsEventType(ev);
1721         }
1722
1723         private void NotifyNewPosts(PostClass[] notifyPosts, string soundFile, int addCount, bool newMentions)
1724         {
1725             if (this._cfgCommon.ReadOwnPost)
1726             {
1727                 if (notifyPosts != null && notifyPosts.Length > 0 && notifyPosts.All(x => x.UserId == tw.UserId))
1728                     return;
1729             }
1730
1731             //新着通知
1732             if (BalloonRequired())
1733             {
1734                 if (notifyPosts != null && notifyPosts.Length > 0)
1735                 {
1736                     //Growlは一個ずつばらして通知。ただし、3ポスト以上あるときはまとめる
1737                     if (this._cfgCommon.IsUseNotifyGrowl)
1738                     {
1739                         StringBuilder sb = new StringBuilder();
1740                         bool reply = false;
1741                         bool dm = false;
1742
1743                         foreach (PostClass post in notifyPosts)
1744                         {
1745                             if (!(notifyPosts.Length > 3))
1746                             {
1747                                 sb.Clear();
1748                                 reply = false;
1749                                 dm = false;
1750                             }
1751                             if (post.IsReply && !post.IsExcludeReply) reply = true;
1752                             if (post.IsDm) dm = true;
1753                             if (sb.Length > 0) sb.Append(System.Environment.NewLine);
1754                             switch (this._cfgCommon.NameBalloon)
1755                             {
1756                                 case MyCommon.NameBalloonEnum.UserID:
1757                                     sb.Append(post.ScreenName).Append(" : ");
1758                                     break;
1759                                 case MyCommon.NameBalloonEnum.NickName:
1760                                     sb.Append(post.Nickname).Append(" : ");
1761                                     break;
1762                             }
1763                             sb.Append(post.TextFromApi);
1764                             if (notifyPosts.Length > 3)
1765                             {
1766                                 if (notifyPosts.Last() != post) continue;
1767                             }
1768
1769                             StringBuilder title = new StringBuilder();
1770                             GrowlHelper.NotifyType nt;
1771                             if (this._cfgCommon.DispUsername)
1772                             {
1773                                 title.Append(tw.Username);
1774                                 title.Append(" - ");
1775                             }
1776                             else
1777                             {
1778                                 //title.Clear();
1779                             }
1780                             if (dm)
1781                             {
1782                                 //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
1783                                 //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [DM] " + Properties.Resources.RefreshDirectMessageText1 + " " + addCount.ToString() + Properties.Resources.RefreshDirectMessageText2;
1784                                 title.Append(Application.ProductName);
1785                                 title.Append(" [DM] ");
1786                                 title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1787                                 nt = GrowlHelper.NotifyType.DirectMessage;
1788                             }
1789                             else if (reply)
1790                             {
1791                                 //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
1792                                 //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [Reply!] " + Properties.Resources.RefreshTimelineText1 + " " + addCount.ToString() + Properties.Resources.RefreshTimelineText2;
1793                                 title.Append(Application.ProductName);
1794                                 title.Append(" [Reply!] ");
1795                                 title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1796                                 nt = GrowlHelper.NotifyType.Reply;
1797                             }
1798                             else
1799                             {
1800                                 //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
1801                                 //NotifyIcon1.BalloonTipTitle += Application.ProductName + " " + Properties.Resources.RefreshTimelineText1 + " " + addCount.ToString() + Properties.Resources.RefreshTimelineText2;
1802                                 title.Append(Application.ProductName);
1803                                 title.Append(" ");
1804                                 title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1805                                 nt = GrowlHelper.NotifyType.Notify;
1806                             }
1807                             string bText = sb.ToString();
1808                             if (string.IsNullOrEmpty(bText)) return;
1809
1810                             var image = this.IconCache.TryGetFromCache(post.ImageUrl);
1811                             gh.Notify(nt, post.StatusId.ToString(), title.ToString(), bText, image == null ? null : image.Image, post.ImageUrl);
1812                         }
1813                     }
1814                     else
1815                     {
1816                         StringBuilder sb = new StringBuilder();
1817                         bool reply = false;
1818                         bool dm = false;
1819                         foreach (PostClass post in notifyPosts)
1820                         {
1821                             if (post.IsReply && !post.IsExcludeReply) reply = true;
1822                             if (post.IsDm) dm = true;
1823                             if (sb.Length > 0) sb.Append(System.Environment.NewLine);
1824                             switch (this._cfgCommon.NameBalloon)
1825                             {
1826                                 case MyCommon.NameBalloonEnum.UserID:
1827                                     sb.Append(post.ScreenName).Append(" : ");
1828                                     break;
1829                                 case MyCommon.NameBalloonEnum.NickName:
1830                                     sb.Append(post.Nickname).Append(" : ");
1831                                     break;
1832                             }
1833                             sb.Append(post.TextFromApi);
1834
1835                         }
1836                         //if (SettingDialog.DispUsername) { NotifyIcon1.BalloonTipTitle = tw.Username + " - "; } else { NotifyIcon1.BalloonTipTitle = ""; }
1837                         StringBuilder title = new StringBuilder();
1838                         ToolTipIcon ntIcon;
1839                         if (this._cfgCommon.DispUsername)
1840                         {
1841                             title.Append(tw.Username);
1842                             title.Append(" - ");
1843                         }
1844                         else
1845                         {
1846                             //title.Clear();
1847                         }
1848                         if (dm)
1849                         {
1850                             //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
1851                             //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [DM] " + Properties.Resources.RefreshDirectMessageText1 + " " + addCount.ToString() + Properties.Resources.RefreshDirectMessageText2;
1852                             ntIcon = ToolTipIcon.Warning;
1853                             title.Append(Application.ProductName);
1854                             title.Append(" [DM] ");
1855                             title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1856                         }
1857                         else if (reply)
1858                         {
1859                             //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
1860                             //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [Reply!] " + Properties.Resources.RefreshTimelineText1 + " " + addCount.ToString() + Properties.Resources.RefreshTimelineText2;
1861                             ntIcon = ToolTipIcon.Warning;
1862                             title.Append(Application.ProductName);
1863                             title.Append(" [Reply!] ");
1864                             title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1865                         }
1866                         else
1867                         {
1868                             //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
1869                             //NotifyIcon1.BalloonTipTitle += Application.ProductName + " " + Properties.Resources.RefreshTimelineText1 + " " + addCount.ToString() + Properties.Resources.RefreshTimelineText2;
1870                             ntIcon = ToolTipIcon.Info;
1871                             title.Append(Application.ProductName);
1872                             title.Append(" ");
1873                             title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1874                         }
1875                         string bText = sb.ToString();
1876                         if (string.IsNullOrEmpty(bText)) return;
1877                         //NotifyIcon1.BalloonTipText = sb.ToString();
1878                         //NotifyIcon1.ShowBalloonTip(500);
1879                         NotifyIcon1.BalloonTipTitle = title.ToString();
1880                         NotifyIcon1.BalloonTipText = bText;
1881                         NotifyIcon1.BalloonTipIcon = ntIcon;
1882                         NotifyIcon1.ShowBalloonTip(500);
1883                     }
1884                 }
1885             }
1886
1887             //サウンド再生
1888             if (!_initial && this._cfgCommon.PlaySound && !string.IsNullOrEmpty(soundFile))
1889             {
1890                 try
1891                 {
1892                     string dir = Application.StartupPath;
1893                     if (Directory.Exists(Path.Combine(dir, "Sounds")))
1894                     {
1895                         dir = Path.Combine(dir, "Sounds");
1896                     }
1897                     using (SoundPlayer player = new SoundPlayer(Path.Combine(dir, soundFile)))
1898                     {
1899                         player.Play();
1900                     }
1901                 }
1902                 catch (Exception)
1903                 {
1904                 }
1905             }
1906
1907             //mentions新着時に画面ブリンク
1908             if (!_initial && this._cfgCommon.BlinkNewMentions && newMentions && Form.ActiveForm == null)
1909             {
1910                 NativeMethods.FlashMyWindow(this.Handle, NativeMethods.FlashSpecification.FlashTray, 3);
1911             }
1912         }
1913
1914         private void MyList_SelectedIndexChanged(object sender, EventArgs e)
1915         {
1916             if (_curList == null || !_curList.Equals(sender) || _curList.SelectedIndices.Count != 1) return;
1917
1918             _curItemIndex = _curList.SelectedIndices[0];
1919             if (_curItemIndex > _curList.VirtualListSize - 1) return;
1920
1921             try
1922             {
1923                 this._curPost = GetCurTabPost(_curItemIndex);
1924             }
1925             catch (ArgumentException)
1926             {
1927                 return;
1928             }
1929
1930             this.PushSelectPostChain();
1931
1932             this._statuses.SetReadAllTab(_curPost.StatusId, read: true);
1933             //キャッシュの書き換え
1934             ChangeCacheStyleRead(true, _curItemIndex);   //既読へ(フォント、文字色)
1935
1936             ColorizeList();
1937             _colorize = true;
1938         }
1939
1940         private void ChangeCacheStyleRead(bool Read, int Index)
1941         {
1942             var tabInfo = _statuses.Tabs[_curTab.Text];
1943             //Read:true=既読 false=未読
1944             //未読管理していなかったら既読として扱う
1945             if (!tabInfo.UnreadManage ||
1946                !this._cfgCommon.UnreadManage) Read = true;
1947
1948             var listCache = this._listItemCache;
1949             if (listCache == null)
1950                 return;
1951
1952             // キャッシュに含まれていないアイテムは対象外
1953             ListViewItem itm;
1954             PostClass post;
1955             if (!listCache.TryGetValue(Index, out itm, out post))
1956                 return;
1957
1958             ChangeItemStyleRead(Read, itm, post, ((DetailsListView)_curTab.Tag));
1959         }
1960
1961         private void ChangeItemStyleRead(bool Read, ListViewItem Item, PostClass Post, DetailsListView DList)
1962         {
1963             Font fnt;
1964             //フォント
1965             if (Read)
1966             {
1967                 fnt = _fntReaded;
1968                 Item.SubItems[5].Text = "";
1969             }
1970             else
1971             {
1972                 fnt = _fntUnread;
1973                 Item.SubItems[5].Text = "★";
1974             }
1975             //文字色
1976             Color cl;
1977             if (Post.IsFav)
1978                 cl = _clFav;
1979             else if (Post.RetweetedId != null)
1980                 cl = _clRetweet;
1981             else if (Post.IsOwl && (Post.IsDm || this._cfgCommon.OneWayLove))
1982                 cl = _clOWL;
1983             else if (Read || !this._cfgCommon.UseUnreadStyle)
1984                 cl = _clReaded;
1985             else
1986                 cl = _clUnread;
1987
1988             if (DList == null || Item.Index == -1)
1989             {
1990                 Item.ForeColor = cl;
1991                 if (this._cfgCommon.UseUnreadStyle)
1992                     Item.Font = fnt;
1993             }
1994             else
1995             {
1996                 DList.Update();
1997                 if (this._cfgCommon.UseUnreadStyle)
1998                     DList.ChangeItemFontAndColor(Item.Index, cl, fnt);
1999                 else
2000                     DList.ChangeItemForeColor(Item.Index, cl);
2001                 //if (_itemCache != null) DList.RedrawItems(_itemCacheIndex, _itemCacheIndex + _itemCache.Length - 1, false);
2002             }
2003         }
2004
2005         private void ColorizeList()
2006         {
2007             //Index:更新対象のListviewItem.Index。Colorを返す。
2008             //-1は全キャッシュ。Colorは返さない(ダミーを戻す)
2009             PostClass _post;
2010             if (_anchorFlag)
2011                 _post = _anchorPost;
2012             else
2013                 _post = _curPost;
2014
2015             if (_post == null) return;
2016
2017             var listCache = this._listItemCache;
2018             if (listCache == null)
2019                 return;
2020
2021             var index = listCache.StartIndex;
2022             foreach (var cachedPost in listCache.Post)
2023             {
2024                 var backColor = this.JudgeColor(_post, cachedPost);
2025                 this._curList.ChangeItemBackColor(index++, backColor);
2026             }
2027         }
2028
2029         private void ColorizeList(ListViewItem Item, int Index)
2030         {
2031             //Index:更新対象のListviewItem.Index。Colorを返す。
2032             //-1は全キャッシュ。Colorは返さない(ダミーを戻す)
2033             PostClass _post;
2034             if (_anchorFlag)
2035                 _post = _anchorPost;
2036             else
2037                 _post = _curPost;
2038
2039             PostClass tPost = GetCurTabPost(Index);
2040
2041             if (_post == null) return;
2042
2043             if (Item.Index == -1)
2044                 Item.BackColor = JudgeColor(_post, tPost);
2045             else
2046                 _curList.ChangeItemBackColor(Item.Index, JudgeColor(_post, tPost));
2047         }
2048
2049         private Color JudgeColor(PostClass BasePost, PostClass TargetPost)
2050         {
2051             Color cl;
2052             if (TargetPost.StatusId == BasePost.InReplyToStatusId)
2053                 //@先
2054                 cl = _clAtTo;
2055             else if (TargetPost.IsMe)
2056                 //自分=発言者
2057                 cl = _clSelf;
2058             else if (TargetPost.IsReply)
2059                 //自分宛返信
2060                 cl = _clAtSelf;
2061             else if (BasePost.ReplyToList.Contains(TargetPost.ScreenName.ToLowerInvariant()))
2062                 //返信先
2063                 cl = _clAtFromTarget;
2064             else if (TargetPost.ReplyToList.Contains(BasePost.ScreenName.ToLowerInvariant()))
2065                 //その人への返信
2066                 cl = _clAtTarget;
2067             else if (TargetPost.ScreenName.Equals(BasePost.ScreenName, StringComparison.OrdinalIgnoreCase))
2068                 //発言者
2069                 cl = _clTarget;
2070             else
2071                 //その他
2072                 cl = _clListBackcolor;
2073
2074             return cl;
2075         }
2076
2077         private async void PostButton_Click(object sender, EventArgs e)
2078         {
2079             if (StatusText.Text.Trim().Length == 0)
2080             {
2081                 if (!ImageSelector.Enabled)
2082                 {
2083                     await this.DoRefresh();
2084                     return;
2085                 }
2086             }
2087
2088             if (this.ExistCurrentPost && StatusText.Text.Trim() == string.Format("RT @{0}: {1}", _curPost.ScreenName, _curPost.TextFromApi))
2089             {
2090                 DialogResult rtResult = MessageBox.Show(string.Format(Properties.Resources.PostButton_Click1, Environment.NewLine),
2091                                                                "Retweet",
2092                                                                MessageBoxButtons.YesNoCancel,
2093                                                                MessageBoxIcon.Question);
2094                 switch (rtResult)
2095                 {
2096                     case DialogResult.Yes:
2097                         StatusText.Text = "";
2098                         await this.doReTweetOfficial(false);
2099                         return;
2100                     case DialogResult.Cancel:
2101                         return;
2102                 }
2103             }
2104
2105             var inReplyToStatusId = this.inReplyTo?.Item1;
2106             var inReplyToScreenName = this.inReplyTo?.Item2;
2107             _history[_history.Count - 1] = new PostingStatus(StatusText.Text, inReplyToStatusId, inReplyToScreenName);
2108
2109             if (this._cfgCommon.Nicoms)
2110             {
2111                 StatusText.SelectionStart = StatusText.Text.Length;
2112                 await UrlConvertAsync(MyCommon.UrlConverter.Nicoms);
2113             }
2114             //if (SettingDialog.UrlConvertAuto)
2115             //{
2116             //    StatusText.SelectionStart = StatusText.Text.Length;
2117             //    UrlConvertAutoToolStripMenuItem_Click(null, null);
2118             //}
2119             //else if (SettingDialog.Nicoms)
2120             //{
2121             //    StatusText.SelectionStart = StatusText.Text.Length;
2122             //    UrlConvert(UrlConverter.Nicoms);
2123             //}
2124             StatusText.SelectionStart = StatusText.Text.Length;
2125             CheckReplyTo(StatusText.Text);
2126
2127             var statusText = this.FormatStatusText(this.StatusText.Text);
2128
2129             if (this.GetRestStatusCount(statusText) < 0)
2130             {
2131                 // 文字数制限を超えているが強制的に投稿するか
2132                 var ret = MessageBox.Show(Properties.Resources.PostLengthOverMessage1, Properties.Resources.PostLengthOverMessage2, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
2133                 if (ret != DialogResult.OK)
2134                     return;
2135             }
2136
2137             var status = new PostingStatus();
2138             status.status = statusText;
2139
2140             status.inReplyToId = this.inReplyTo?.Item1;
2141             status.inReplyToName = this.inReplyTo?.Item2;
2142             if (ImageSelector.Visible)
2143             {
2144                 //画像投稿
2145                 if (!ImageSelector.TryGetSelectedMedia(out status.imageService, out status.mediaItems))
2146                     return;
2147             }
2148
2149             this.inReplyTo = null;
2150             StatusText.Text = "";
2151             _history.Add(new PostingStatus());
2152             _hisIdx = _history.Count - 1;
2153             if (!ToolStripFocusLockMenuItem.Checked)
2154                 ((Control)ListTab.SelectedTab.Tag).Focus();
2155             urlUndoBuffer = null;
2156             UrlUndoToolStripMenuItem.Enabled = false;  //Undoをできないように設定
2157
2158             //Google検索(試験実装)
2159             if (StatusText.Text.StartsWith("Google:", StringComparison.OrdinalIgnoreCase) && StatusText.Text.Trim().Length > 7)
2160             {
2161                 string tmp = string.Format(Properties.Resources.SearchItem2Url, Uri.EscapeDataString(StatusText.Text.Substring(7)));
2162                 await this.OpenUriInBrowserAsync(tmp);
2163             }
2164
2165             await this.PostMessageAsync(status);
2166         }
2167
2168         private void EndToolStripMenuItem_Click(object sender, EventArgs e)
2169         {
2170             MyCommon._endingFlag = true;
2171             this.Close();
2172         }
2173
2174         private void TweenMain_FormClosing(object sender, FormClosingEventArgs e)
2175         {
2176             if (!this._cfgCommon.CloseToExit && e.CloseReason == CloseReason.UserClosing && MyCommon._endingFlag == false)
2177             {
2178                 //_endingFlag=false:フォームの×ボタン
2179                 e.Cancel = true;
2180                 this.Visible = false;
2181             }
2182             else
2183             {
2184                 _hookGlobalHotkey.UnregisterAllOriginalHotkey();
2185                 _ignoreConfigSave = true;
2186                 MyCommon._endingFlag = true;
2187                 TimerTimeline.Enabled = false;
2188                 TimerRefreshIcon.Enabled = false;
2189             }
2190         }
2191
2192         private void NotifyIcon1_BalloonTipClicked(object sender, EventArgs e)
2193         {
2194             this.Visible = true;
2195             if (this.WindowState == FormWindowState.Minimized)
2196             {
2197                 this.WindowState = FormWindowState.Normal;
2198             }
2199             this.Activate();
2200             this.BringToFront();
2201         }
2202
2203         private static int errorCount = 0;
2204
2205         private static bool CheckAccountValid()
2206         {
2207             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2208             {
2209                 errorCount += 1;
2210                 if (errorCount > 5)
2211                 {
2212                     errorCount = 0;
2213                     Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
2214                     return true;
2215                 }
2216                 return false;
2217             }
2218             errorCount = 0;
2219             return true;
2220         }
2221
2222         private Task GetHomeTimelineAsync()
2223         {
2224             return this.GetHomeTimelineAsync(loadMore: false);
2225         }
2226
2227         private async Task GetHomeTimelineAsync(bool loadMore)
2228         {
2229             await this.workerSemaphore.WaitAsync();
2230
2231             try
2232             {
2233                 var homeTab = this._statuses.GetTabByType<HomeTabModel>();
2234                 await homeTab.RefreshAsync(this.tw, loadMore, this._initial, this.workerProgress);
2235
2236                 this.RefreshTimeline();
2237             }
2238             catch (WebApiException ex)
2239             {
2240                 this._myStatusError = true;
2241                 this.StatusLabel.Text = $"Err:{ex.Message}(GetTimeline)";
2242             }
2243             finally
2244             {
2245                 this.workerSemaphore.Release();
2246             }
2247         }
2248
2249         private Task GetReplyAsync()
2250         {
2251             return this.GetReplyAsync(loadMore: false);
2252         }
2253
2254         private async Task GetReplyAsync(bool loadMore)
2255         {
2256             await this.workerSemaphore.WaitAsync();
2257
2258             try
2259             {
2260                 var replyTab = this._statuses.GetTabByType<MentionsTabModel>();
2261                 await replyTab.RefreshAsync(this.tw, loadMore, this._initial, this.workerProgress);
2262
2263                 this.RefreshTimeline();
2264             }
2265             catch (WebApiException ex)
2266             {
2267                 this._myStatusError = true;
2268                 this.StatusLabel.Text = $"Err:{ex.Message}(GetTimeline)";
2269             }
2270             finally
2271             {
2272                 this.workerSemaphore.Release();
2273             }
2274         }
2275
2276         private Task GetDirectMessagesAsync()
2277         {
2278             return this.GetDirectMessagesAsync(loadMore: false);
2279         }
2280
2281         private async Task GetDirectMessagesAsync(bool loadMore)
2282         {
2283             await this.workerSemaphore.WaitAsync();
2284
2285             try
2286             {
2287                 var dmTab = this._statuses.GetTabByType<DirectMessagesTabModel>();
2288                 await dmTab.RefreshAsync(this.tw, loadMore, this._initial, this.workerProgress);
2289
2290                 this.RefreshTimeline();
2291             }
2292             catch (WebApiException ex)
2293             {
2294                 this._myStatusError = true;
2295                 this.StatusLabel.Text = $"Err:{ex.Message}(GetDirectMessage)";
2296             }
2297             finally
2298             {
2299                 this.workerSemaphore.Release();
2300             }
2301         }
2302
2303         private Task GetFavoritesAsync()
2304         {
2305             return this.GetFavoritesAsync(loadMore: false);
2306         }
2307
2308         private async Task GetFavoritesAsync(bool loadMore)
2309         {
2310             await this.workerSemaphore.WaitAsync();
2311
2312             try
2313             {
2314                 var favTab = this._statuses.GetTabByType<FavoritesTabModel>();
2315                 await favTab.RefreshAsync(this.tw, loadMore, this._initial, this.workerProgress);
2316
2317                 this.RefreshTimeline();
2318             }
2319             catch (WebApiException ex)
2320             {
2321                 this._myStatusError = true;
2322                 this.StatusLabel.Text = ex.Message;
2323             }
2324             finally
2325             {
2326                 this.workerSemaphore.Release();
2327             }
2328         }
2329
2330         private Task GetPublicSearchAllAsync()
2331         {
2332             var tabs = this._statuses.GetTabsByType<PublicSearchTabModel>();
2333
2334             return this.GetPublicSearchAsync(tabs, loadMore: false);
2335         }
2336
2337         private Task GetPublicSearchAsync(PublicSearchTabModel tab)
2338         {
2339             return this.GetPublicSearchAsync(tab, loadMore: false);
2340         }
2341
2342         private Task GetPublicSearchAsync(PublicSearchTabModel tab, bool loadMore)
2343         {
2344             return this.GetPublicSearchAsync(new[] { tab }, loadMore);
2345         }
2346
2347         private async Task GetPublicSearchAsync(IEnumerable<PublicSearchTabModel> tabs, bool loadMore)
2348         {
2349             await this.workerSemaphore.WaitAsync();
2350
2351             try
2352             {
2353                 foreach (var tab in tabs)
2354                 {
2355                     try
2356                     {
2357                         await tab.RefreshAsync(this.tw, loadMore, this._initial, this.workerProgress);
2358                     }
2359                     catch (WebApiException ex)
2360                     {
2361                         this._myStatusError = true;
2362                         this.StatusLabel.Text = $"Err:{ex.Message}(GetSearch)";
2363                     }
2364                 }
2365
2366                 this.RefreshTimeline();
2367             }
2368             finally
2369             {
2370                 this.workerSemaphore.Release();
2371             }
2372         }
2373
2374         private Task GetUserTimelineAllAsync()
2375         {
2376             var tabs = this._statuses.GetTabsByType<UserTimelineTabModel>();
2377
2378             return this.GetUserTimelineAsync(tabs, loadMore: false);
2379         }
2380
2381         private Task GetUserTimelineAsync(UserTimelineTabModel tab)
2382         {
2383             return this.GetUserTimelineAsync(tab, loadMore: false);
2384         }
2385
2386         private Task GetUserTimelineAsync(UserTimelineTabModel tab, bool loadMore)
2387         {
2388             return this.GetUserTimelineAsync(new[] { tab }, loadMore);
2389         }
2390
2391         private async Task GetUserTimelineAsync(IEnumerable<UserTimelineTabModel> tabs, bool loadMore)
2392         {
2393             await this.workerSemaphore.WaitAsync();
2394
2395             try
2396             {
2397                 foreach (var tab in tabs)
2398                 {
2399                     try
2400                     {
2401                         await tab.RefreshAsync(this.tw, loadMore, this._initial, this.workerProgress);
2402                     }
2403                     catch (WebApiException ex)
2404                     {
2405                         this._myStatusError = true;
2406                         this.StatusLabel.Text = $"Err:{ex.Message}(GetUserTimeline)";
2407                     }
2408                 }
2409
2410                 this.RefreshTimeline();
2411             }
2412             finally
2413             {
2414                 this.workerSemaphore.Release();
2415             }
2416         }
2417
2418         private Task GetListTimelineAllAsync()
2419         {
2420             var tabs = this._statuses.GetTabsByType<ListTimelineTabModel>();
2421
2422             return this.GetListTimelineAsync(tabs, loadMore: false);
2423         }
2424
2425         private Task GetListTimelineAsync(ListTimelineTabModel tab)
2426         {
2427             return this.GetListTimelineAsync(tab, loadMore: false);
2428         }
2429
2430         private Task GetListTimelineAsync(ListTimelineTabModel tab, bool loadMore)
2431         {
2432             return this.GetListTimelineAsync(new[] { tab }, loadMore);
2433         }
2434
2435         private async Task GetListTimelineAsync(IEnumerable<ListTimelineTabModel> tabs, bool loadMore)
2436         {
2437             await this.workerSemaphore.WaitAsync();
2438
2439             try
2440             {
2441                 foreach (var tab in tabs)
2442                 {
2443                     try
2444                     {
2445                         await tab.RefreshAsync(this.tw, loadMore, this._initial, this.workerProgress);
2446                     }
2447                     catch (WebApiException ex)
2448                     {
2449                         this._myStatusError = true;
2450                         this.StatusLabel.Text = $"Err:{ex.Message}(GetListStatus)";
2451                     }
2452                 }
2453
2454                 this.RefreshTimeline();
2455             }
2456             finally
2457             {
2458                 this.workerSemaphore.Release();
2459             }
2460         }
2461
2462         private async Task GetRelatedTweetsAsync(RelatedPostsTabModel tab)
2463         {
2464             await this.workerSemaphore.WaitAsync();
2465
2466             try
2467             {
2468                 await tab.RefreshAsync(this.tw, this._initial, this.workerProgress);
2469
2470                 this.RefreshTimeline();
2471             }
2472             catch (WebApiException ex)
2473             {
2474                 this._myStatusError = true;
2475                 this.StatusLabel.Text = $"Err:{ex.Message}(GetRelatedTweets)";
2476             }
2477             finally
2478             {
2479                 this.workerSemaphore.Release();
2480             }
2481         }
2482
2483         private async Task FavAddAsync(long statusId, TabModel tab)
2484         {
2485             await this.workerSemaphore.WaitAsync();
2486
2487             try
2488             {
2489                 var progress = new Progress<string>(x => this.StatusLabel.Text = x);
2490
2491                 await this.FavAddAsyncInternal(progress, this.workerCts.Token, statusId, tab);
2492             }
2493             catch (WebApiException ex)
2494             {
2495                 this._myStatusError = true;
2496                 this.StatusLabel.Text = $"Err:{ex.Message}(PostFavAdd)";
2497             }
2498             finally
2499             {
2500                 this.workerSemaphore.Release();
2501             }
2502         }
2503
2504         private async Task FavAddAsyncInternal(IProgress<string> p, CancellationToken ct, long statusId, TabModel tab)
2505         {
2506             if (ct.IsCancellationRequested)
2507                 return;
2508
2509             if (!CheckAccountValid())
2510                 throw new WebApiException("Auth error. Check your account");
2511
2512             PostClass post;
2513             if (!tab.Posts.TryGetValue(statusId, out post))
2514                 return;
2515
2516             if (post.IsFav)
2517                 return;
2518
2519             await Task.Run(async () =>
2520             {
2521                 p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText15, 0, 1, 0));
2522
2523                 try
2524                 {
2525                     await this.twitterApi.FavoritesCreate(post.RetweetedId ?? post.StatusId)
2526                         .IgnoreResponse()
2527                         .ConfigureAwait(false);
2528
2529                     if (this._cfgCommon.RestrictFavCheck)
2530                     {
2531                         var status = await this.twitterApi.StatusesShow(post.RetweetedId ?? post.StatusId)
2532                             .ConfigureAwait(false);
2533
2534                         if (status.Favorited != true)
2535                             throw new WebApiException("NG(Restricted?)");
2536                     }
2537
2538                     this._favTimestamps.Add(DateTime.Now);
2539
2540                     // TLでも取得済みならfav反映
2541                     if (this._statuses.ContainsKey(statusId))
2542                     {
2543                         var postTl = this._statuses[statusId];
2544                         postTl.IsFav = true;
2545
2546                         var favTab = this._statuses.GetTabByType(MyCommon.TabUsageType.Favorites);
2547                         favTab.AddPostQueue(postTl);
2548                     }
2549
2550                     // 検索,リスト,UserTimeline,Relatedの各タブに反映
2551                     foreach (var tb in this._statuses.GetTabsInnerStorageType())
2552                     {
2553                         if (tb.Contains(statusId))
2554                             tb.Posts[statusId].IsFav = true;
2555                     }
2556
2557                     p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText15, 1, 1, 0));
2558                 }
2559                 catch (WebApiException)
2560                 {
2561                     p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText15, 1, 1, 1));
2562                     throw;
2563                 }
2564
2565                 // 時速表示用
2566                 var oneHour = DateTime.Now - TimeSpan.FromHours(1);
2567                 foreach (var i in MyCommon.CountDown(this._favTimestamps.Count - 1, 0))
2568                 {
2569                     if (this._favTimestamps[i] < oneHour)
2570                         this._favTimestamps.RemoveAt(i);
2571                 }
2572
2573                 this._statuses.DistributePosts();
2574             });
2575
2576             if (ct.IsCancellationRequested)
2577                 return;
2578
2579             this.RefreshTimeline();
2580
2581             if (this._curList != null && this._curTab != null && this._curTab.Text == tab.TabName)
2582             {
2583                 using (ControlTransaction.Update(this._curList))
2584                 {
2585                     var idx = tab.IndexOf(statusId);
2586                     if (idx != -1)
2587                         this.ChangeCacheStyleRead(post.IsRead, idx);
2588                 }
2589
2590                 if (statusId == this._curPost.StatusId)
2591                     await this.DispSelectedPost(true); // 選択アイテム再表示
2592             }
2593         }
2594
2595         private async Task FavRemoveAsync(IReadOnlyList<long> statusIds, TabModel tab)
2596         {
2597             await this.workerSemaphore.WaitAsync();
2598
2599             try
2600             {
2601                 var progress = new Progress<string>(x => this.StatusLabel.Text = x);
2602
2603                 await this.FavRemoveAsyncInternal(progress, this.workerCts.Token, statusIds, tab);
2604             }
2605             catch (WebApiException ex)
2606             {
2607                 this._myStatusError = true;
2608                 this.StatusLabel.Text = $"Err:{ex.Message}(PostFavRemove)";
2609             }
2610             finally
2611             {
2612                 this.workerSemaphore.Release();
2613             }
2614         }
2615
2616         private async Task FavRemoveAsyncInternal(IProgress<string> p, CancellationToken ct, IReadOnlyList<long> statusIds, TabModel tab)
2617         {
2618             if (ct.IsCancellationRequested)
2619                 return;
2620
2621             if (!CheckAccountValid())
2622                 throw new WebApiException("Auth error. Check your account");
2623
2624             var successIds = new List<long>();
2625
2626             await Task.Run(async () =>
2627             {
2628                 //スレッド処理はしない
2629                 var allCount = 0;
2630                 var failedCount = 0;
2631                 foreach (var statusId in statusIds)
2632                 {
2633                     allCount++;
2634
2635                     var post = tab.Posts[statusId];
2636
2637                     p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText17, allCount, statusIds.Count, failedCount));
2638
2639                     if (!post.IsFav)
2640                         continue;
2641
2642                     try
2643                     {
2644                         await this.twitterApi.FavoritesDestroy(post.RetweetedId ?? post.StatusId)
2645                             .IgnoreResponse()
2646                             .ConfigureAwait(false);
2647                     }
2648                     catch (WebApiException)
2649                     {
2650                         failedCount++;
2651                         continue;
2652                     }
2653
2654                     successIds.Add(statusId);
2655                     post.IsFav = false; // リスト再描画必要
2656
2657                     if (this._statuses.ContainsKey(statusId))
2658                     {
2659                         this._statuses[statusId].IsFav = false;
2660                     }
2661
2662                     // 検索,リスト,UserTimeline,Relatedの各タブに反映
2663                     foreach (var tb in this._statuses.GetTabsInnerStorageType())
2664                     {
2665                         if (tb.Contains(statusId))
2666                             tb.Posts[statusId].IsFav = false;
2667                     }
2668                 }
2669             });
2670
2671             if (ct.IsCancellationRequested)
2672                 return;
2673
2674             var favTab = this._statuses.GetTabByType(MyCommon.TabUsageType.Favorites);
2675             foreach (var statusId in successIds)
2676             {
2677                 // ツイートが削除された訳ではないので IsDeleted はセットしない
2678                 favTab.EnqueueRemovePost(statusId, setIsDeleted: false);
2679             }
2680
2681             this.RefreshTimeline();
2682
2683             if (this._curList != null && this._curTab != null && this._curTab.Text == tab.TabName)
2684             {
2685                 if (tab.TabType == MyCommon.TabUsageType.Favorites)
2686                 {
2687                     // 色変えは不要
2688                 }
2689                 else
2690                 {
2691                     using (ControlTransaction.Update(this._curList))
2692                     {
2693                         foreach (var statusId in successIds)
2694                         {
2695                             var idx = tab.IndexOf(statusId);
2696                             if (idx == -1)
2697                                 continue;
2698
2699                             var post = tab.Posts[statusId];
2700                             this.ChangeCacheStyleRead(post.IsRead, idx);
2701                         }
2702                     }
2703
2704                     if (successIds.Contains(this._curPost.StatusId))
2705                         await this.DispSelectedPost(true); // 選択アイテム再表示
2706                 }
2707             }
2708         }
2709
2710         private async Task PostMessageAsync(PostingStatus status)
2711         {
2712             await this.workerSemaphore.WaitAsync();
2713
2714             try
2715             {
2716                 var progress = new Progress<string>(x => this.StatusLabel.Text = x);
2717
2718                 await this.PostMessageAsyncInternal(progress, this.workerCts.Token, status);
2719             }
2720             catch (WebApiException ex)
2721             {
2722                 this._myStatusError = true;
2723                 this.StatusLabel.Text = $"Err:{ex.Message}(PostMessage)";
2724             }
2725             finally
2726             {
2727                 this.workerSemaphore.Release();
2728             }
2729         }
2730
2731         private async Task PostMessageAsyncInternal(IProgress<string> p, CancellationToken ct, PostingStatus status)
2732         {
2733             if (ct.IsCancellationRequested)
2734                 return;
2735
2736             if (!CheckAccountValid())
2737                 throw new WebApiException("Auth error. Check your account");
2738
2739             p.Report("Posting...");
2740
2741             var errMsg = "";
2742
2743             try
2744             {
2745                 await Task.Run(async () =>
2746                 {
2747                     if (status.mediaItems == null || status.mediaItems.Length == 0)
2748                     {
2749                         await this.tw.PostStatus(status.status, status.inReplyToId)
2750                             .ConfigureAwait(false);
2751                     }
2752                     else
2753                     {
2754                         var service = ImageSelector.GetService(status.imageService);
2755                         await service.PostStatusAsync(status.status, status.inReplyToId, status.mediaItems)
2756                             .ConfigureAwait(false);
2757                     }
2758                 });
2759
2760                 p.Report(Properties.Resources.PostWorker_RunWorkerCompletedText4);
2761             }
2762             catch (WebApiException ex)
2763             {
2764                 // 処理は中断せずエラーの表示のみ行う
2765                 errMsg = $"Err:{ex.Message}(PostMessage)";
2766                 p.Report(errMsg);
2767                 this._myStatusError = true;
2768             }
2769             finally
2770             {
2771                 // 使い終わった MediaItem は破棄する
2772                 if (status.mediaItems != null)
2773                 {
2774                     foreach (var disposableItem in status.mediaItems.OfType<IDisposable>())
2775                     {
2776                         disposableItem.Dispose();
2777                     }
2778                 }
2779             }
2780
2781             if (ct.IsCancellationRequested)
2782                 return;
2783
2784             if (!string.IsNullOrEmpty(errMsg) &&
2785                 !errMsg.StartsWith("OK:", StringComparison.Ordinal) &&
2786                 !errMsg.StartsWith("Warn:", StringComparison.Ordinal))
2787             {
2788                 var ret = MessageBox.Show(
2789                     string.Format(
2790                         "{0}   --->   [ " + errMsg + " ]" + Environment.NewLine +
2791                         "\"" + status.status + "\"" + Environment.NewLine +
2792                         "{1}",
2793                         Properties.Resources.StatusUpdateFailed1,
2794                         Properties.Resources.StatusUpdateFailed2),
2795                     "Failed to update status",
2796                     MessageBoxButtons.RetryCancel,
2797                     MessageBoxIcon.Question);
2798
2799                 if (ret == DialogResult.Retry)
2800                 {
2801                     await this.PostMessageAsync(status);
2802                 }
2803                 else
2804                 {
2805                     // 連投モードのときだけEnterイベントが起きないので強制的に背景色を戻す
2806                     if (this.ToolStripFocusLockMenuItem.Checked)
2807                         this.StatusText_Enter(this.StatusText, EventArgs.Empty);
2808                 }
2809                 return;
2810             }
2811
2812             this._postTimestamps.Add(DateTime.Now);
2813
2814             var oneHour = DateTime.Now - TimeSpan.FromHours(1);
2815             foreach (var i in MyCommon.CountDown(this._postTimestamps.Count - 1, 0))
2816             {
2817                 if (this._postTimestamps[i] < oneHour)
2818                     this._postTimestamps.RemoveAt(i);
2819             }
2820
2821             if (!this.HashMgr.IsPermanent && !string.IsNullOrEmpty(this.HashMgr.UseHash))
2822             {
2823                 this.HashMgr.ClearHashtag();
2824                 this.HashStripSplitButton.Text = "#[-]";
2825                 this.HashToggleMenuItem.Checked = false;
2826                 this.HashToggleToolStripMenuItem.Checked = false;
2827             }
2828
2829             this.SetMainWindowTitle();
2830
2831             if (this._cfgCommon.PostAndGet)
2832             {
2833                 if (this.tw.UserStreamActive)
2834                     this.RefreshTimeline();
2835                 else
2836                     await this.GetHomeTimelineAsync();
2837             }
2838         }
2839
2840         private async Task RetweetAsync(IReadOnlyList<long> statusIds)
2841         {
2842             await this.workerSemaphore.WaitAsync();
2843
2844             try
2845             {
2846                 var progress = new Progress<string>(x => this.StatusLabel.Text = x);
2847
2848                 await this.RetweetAsyncInternal(progress, this.workerCts.Token, statusIds);
2849             }
2850             catch (WebApiException ex)
2851             {
2852                 this._myStatusError = true;
2853                 this.StatusLabel.Text = $"Err:{ex.Message}(PostRetweet)";
2854             }
2855             finally
2856             {
2857                 this.workerSemaphore.Release();
2858             }
2859         }
2860
2861         private async Task RetweetAsyncInternal(IProgress<string> p, CancellationToken ct, IReadOnlyList<long> statusIds)
2862         {
2863             if (ct.IsCancellationRequested)
2864                 return;
2865
2866             if (!CheckAccountValid())
2867                 throw new WebApiException("Auth error. Check your account");
2868
2869             bool read;
2870             if (!this._cfgCommon.UnreadManage)
2871                 read = true;
2872             else
2873                 read = this._initial && this._cfgCommon.Read;
2874
2875             p.Report("Posting...");
2876
2877             var retweetTasks = from statusId in statusIds
2878                                select this.tw.PostRetweet(statusId, read);
2879
2880             await Task.WhenAll(retweetTasks)
2881                 .ConfigureAwait(false);
2882
2883             if (ct.IsCancellationRequested)
2884                 return;
2885
2886             p.Report(Properties.Resources.PostWorker_RunWorkerCompletedText4);
2887
2888             this._postTimestamps.Add(DateTime.Now);
2889
2890             var oneHour = DateTime.Now - TimeSpan.FromHours(1);
2891             foreach (var i in MyCommon.CountDown(this._postTimestamps.Count - 1, 0))
2892             {
2893                 if (this._postTimestamps[i] < oneHour)
2894                     this._postTimestamps.RemoveAt(i);
2895             }
2896
2897             if (this._cfgCommon.PostAndGet && !this.tw.UserStreamActive)
2898                 await this.GetHomeTimelineAsync();
2899         }
2900
2901         private async Task RefreshFollowerIdsAsync()
2902         {
2903             await this.workerSemaphore.WaitAsync();
2904             try
2905             {
2906                 this.StatusLabel.Text = Properties.Resources.UpdateFollowersMenuItem1_ClickText1;
2907
2908                 await this.tw.RefreshFollowerIds();
2909
2910                 this.StatusLabel.Text = Properties.Resources.UpdateFollowersMenuItem1_ClickText3;
2911
2912                 this.RefreshTimeline();
2913                 this.PurgeListViewItemCache();
2914                 this._curList?.Refresh();
2915             }
2916             catch (WebApiException ex)
2917             {
2918                 this.StatusLabel.Text = $"Err:{ex.Message}(RefreshFollowersIds)";
2919             }
2920             finally
2921             {
2922                 this.workerSemaphore.Release();
2923             }
2924         }
2925
2926         private async Task RefreshNoRetweetIdsAsync()
2927         {
2928             await this.workerSemaphore.WaitAsync();
2929             try
2930             {
2931                 await this.tw.RefreshNoRetweetIds();
2932
2933                 this.StatusLabel.Text = "NoRetweetIds refreshed";
2934             }
2935             catch (WebApiException ex)
2936             {
2937                 this.StatusLabel.Text = $"Err:{ex.Message}(RefreshNoRetweetIds)";
2938             }
2939             finally
2940             {
2941                 this.workerSemaphore.Release();
2942             }
2943         }
2944
2945         private async Task RefreshBlockIdsAsync()
2946         {
2947             await this.workerSemaphore.WaitAsync();
2948             try
2949             {
2950                 this.StatusLabel.Text = Properties.Resources.UpdateBlockUserText1;
2951
2952                 await this.tw.RefreshBlockIds();
2953
2954                 this.StatusLabel.Text = Properties.Resources.UpdateBlockUserText3;
2955             }
2956             catch (WebApiException ex)
2957             {
2958                 this.StatusLabel.Text = $"Err:{ex.Message}(RefreshBlockIds)";
2959             }
2960             finally
2961             {
2962                 this.workerSemaphore.Release();
2963             }
2964         }
2965
2966         private async Task RefreshTwitterConfigurationAsync()
2967         {
2968             await this.workerSemaphore.WaitAsync();
2969             try
2970             {
2971                 await this.tw.RefreshConfiguration();
2972
2973                 if (this.tw.Configuration.PhotoSizeLimit != 0)
2974                 {
2975                     foreach (var service in this.ImageSelector.GetServices())
2976                     {
2977                         service.UpdateTwitterConfiguration(this.tw.Configuration);
2978                     }
2979                 }
2980
2981                 this.PurgeListViewItemCache();
2982
2983                 this._curList?.Refresh();
2984             }
2985             catch (WebApiException ex)
2986             {
2987                 this.StatusLabel.Text = $"Err:{ex.Message}(RefreshConfiguration)";
2988             }
2989             finally
2990             {
2991                 this.workerSemaphore.Release();
2992             }
2993         }
2994
2995         private async Task RefreshMuteUserIdsAsync()
2996         {
2997             this.StatusLabel.Text = Properties.Resources.UpdateMuteUserIds_Start;
2998
2999             try
3000             {
3001                 await tw.RefreshMuteUserIdsAsync();
3002             }
3003             catch (WebApiException ex)
3004             {
3005                 this.StatusLabel.Text = string.Format(Properties.Resources.UpdateMuteUserIds_Error, ex.Message);
3006                 return;
3007             }
3008
3009             this.StatusLabel.Text = Properties.Resources.UpdateMuteUserIds_Finish;
3010         }
3011
3012         private void NotifyIcon1_MouseClick(object sender, MouseEventArgs e)
3013         {
3014             if (e.Button == MouseButtons.Left)
3015             {
3016                 this.Visible = true;
3017                 if (this.WindowState == FormWindowState.Minimized)
3018                 {
3019                     this.WindowState = _formWindowState;
3020                 }
3021                 this.Activate();
3022                 this.BringToFront();
3023             }
3024         }
3025
3026         private async void MyList_MouseDoubleClick(object sender, MouseEventArgs e)
3027         {
3028             switch (this._cfgCommon.ListDoubleClickAction)
3029             {
3030                 case 0:
3031                     MakeReplyOrDirectStatus();
3032                     break;
3033                 case 1:
3034                     await this.FavoriteChange(true);
3035                     break;
3036                 case 2:
3037                     if (_curPost != null)
3038                         await this.ShowUserStatus(_curPost.ScreenName, false);
3039                     break;
3040                 case 3:
3041                     ShowUserTimeline();
3042                     break;
3043                 case 4:
3044                     ShowRelatedStatusesMenuItem_Click(null, null);
3045                     break;
3046                 case 5:
3047                     MoveToHomeToolStripMenuItem_Click(null, null);
3048                     break;
3049                 case 6:
3050                     StatusOpenMenuItem_Click(null, null);
3051                     break;
3052                 case 7:
3053                     //動作なし
3054                     break;
3055             }
3056         }
3057
3058         private async void FavAddToolStripMenuItem_Click(object sender, EventArgs e)
3059         {
3060             await this.FavoriteChange(true);
3061         }
3062
3063         private async void FavRemoveToolStripMenuItem_Click(object sender, EventArgs e)
3064         {
3065             await this.FavoriteChange(false);
3066         }
3067
3068
3069         private async void FavoriteRetweetMenuItem_Click(object sender, EventArgs e)
3070         {
3071             await this.FavoritesRetweetOfficial();
3072         }
3073
3074         private async void FavoriteRetweetUnofficialMenuItem_Click(object sender, EventArgs e)
3075         {
3076             await this.FavoritesRetweetUnofficial();
3077         }
3078
3079         private async Task FavoriteChange(bool FavAdd, bool multiFavoriteChangeDialogEnable = true)
3080         {
3081             TabModel tab;
3082             if (!this._statuses.Tabs.TryGetValue(this._curTab.Text, out tab))
3083                 return;
3084
3085             //trueでFavAdd,falseでFavRemove
3086             if (tab.TabType == MyCommon.TabUsageType.DirectMessage || _curList.SelectedIndices.Count == 0
3087                 || !this.ExistCurrentPost) return;
3088
3089             if (this._curList.SelectedIndices.Count > 1)
3090             {
3091                 if (FavAdd)
3092                 {
3093                     // 複数ツイートの一括ふぁぼは禁止
3094                     // https://support.twitter.com/articles/76915#favoriting
3095                     MessageBox.Show(string.Format(Properties.Resources.FavoriteLimitCountText, 1));
3096                     _DoFavRetweetFlags = false;
3097                     return;
3098                 }
3099                 else
3100                 {
3101                     if (multiFavoriteChangeDialogEnable)
3102                     {
3103                         var confirm = MessageBox.Show(Properties.Resources.FavRemoveToolStripMenuItem_ClickText1,
3104                             Properties.Resources.FavRemoveToolStripMenuItem_ClickText2,
3105                             MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
3106
3107                         if (confirm == DialogResult.Cancel)
3108                             return;
3109                     }
3110                 }
3111             }
3112
3113             if (FavAdd)
3114             {
3115                 var selectedPost = this.GetCurTabPost(_curList.SelectedIndices[0]);
3116                 if (selectedPost.IsFav)
3117                 {
3118                     this.StatusLabel.Text = Properties.Resources.FavAddToolStripMenuItem_ClickText4;
3119                     return;
3120                 }
3121
3122                 await this.FavAddAsync(selectedPost.StatusId, tab);
3123             }
3124             else
3125             {
3126                 var selectedPosts = this._curList.SelectedIndices.Cast<int>()
3127                     .Select(x => this.GetCurTabPost(x))
3128                     .Where(x => x.IsFav);
3129
3130                 var statusIds = selectedPosts.Select(x => x.StatusId).ToArray();
3131                 if (statusIds.Length == 0)
3132                 {
3133                     this.StatusLabel.Text = Properties.Resources.FavRemoveToolStripMenuItem_ClickText4;
3134                     return;
3135                 }
3136
3137                 await this.FavRemoveAsync(statusIds, tab);
3138             }
3139         }
3140
3141         private PostClass GetCurTabPost(int Index)
3142         {
3143             var listCache = this._listItemCache;
3144             if (listCache != null)
3145             {
3146                 ListViewItem item;
3147                 PostClass post;
3148                 if (listCache.TryGetValue(Index, out item, out post))
3149                     return post;
3150             }
3151
3152             return _statuses.Tabs[_curTab.Text][Index];
3153         }
3154
3155         private async void MoveToHomeToolStripMenuItem_Click(object sender, EventArgs e)
3156         {
3157             if (_curList.SelectedIndices.Count > 0)
3158                 await this.OpenUriInBrowserAsync(MyCommon.TwitterUrl + GetCurTabPost(_curList.SelectedIndices[0]).ScreenName);
3159             else if (_curList.SelectedIndices.Count == 0)
3160                 await this.OpenUriInBrowserAsync(MyCommon.TwitterUrl);
3161         }
3162
3163         private async void MoveToFavToolStripMenuItem_Click(object sender, EventArgs e)
3164         {
3165             if (_curList.SelectedIndices.Count > 0)
3166                 await this.OpenUriInBrowserAsync(MyCommon.TwitterUrl + "#!/" + GetCurTabPost(_curList.SelectedIndices[0]).ScreenName + "/favorites");
3167         }
3168
3169         private void TweenMain_ClientSizeChanged(object sender, EventArgs e)
3170         {
3171             if ((!_initialLayout) && this.Visible)
3172             {
3173                 if (this.WindowState == FormWindowState.Normal)
3174                 {
3175                     _mySize = this.ClientSize;
3176                     _mySpDis = this.SplitContainer1.SplitterDistance;
3177                     _mySpDis3 = this.SplitContainer3.SplitterDistance;
3178                     if (StatusText.Multiline) _mySpDis2 = this.StatusText.Height;
3179                     ModifySettingLocal = true;
3180                 }
3181             }
3182         }
3183
3184         private void MyList_ColumnClick(object sender, ColumnClickEventArgs e)
3185         {
3186             var comparerMode = this.GetComparerModeByColumnIndex(e.Column);
3187             if (comparerMode == null)
3188                 return;
3189
3190             this.SetSortColumn(comparerMode.Value);
3191         }
3192
3193         /// <summary>
3194         /// 列インデックスからソートを行う ComparerMode を求める
3195         /// </summary>
3196         /// <param name="columnIndex">ソートを行うカラムのインデックス (表示上の順序とは異なる)</param>
3197         /// <returns>ソートを行う ComparerMode。null であればソートを行わない</returns>
3198         private ComparerMode? GetComparerModeByColumnIndex(int columnIndex)
3199         {
3200             if (this._iconCol)
3201                 return ComparerMode.Id;
3202
3203             switch (columnIndex)
3204             {
3205                 case 1: // ニックネーム
3206                     return ComparerMode.Nickname;
3207                 case 2: // 本文
3208                     return ComparerMode.Data;
3209                 case 3: // 時刻=発言Id
3210                     return ComparerMode.Id;
3211                 case 4: // 名前
3212                     return ComparerMode.Name;
3213                 case 7: // Source
3214                     return ComparerMode.Source;
3215                 default:
3216                     // 0:アイコン, 5:未読マーク, 6:プロテクト・フィルターマーク
3217                     return null;
3218             }
3219         }
3220
3221         /// <summary>
3222         /// 発言一覧の指定した位置の列でソートする
3223         /// </summary>
3224         /// <param name="columnIndex">ソートする列の位置 (表示上の順序で指定)</param>
3225         private void SetSortColumnByDisplayIndex(int columnIndex)
3226         {
3227             // 表示上の列の位置から ColumnHeader を求める
3228             var col = this._curList.Columns.Cast<ColumnHeader>()
3229                 .FirstOrDefault(x => x.DisplayIndex == columnIndex);
3230
3231             if (col == null)
3232                 return;
3233
3234             var comparerMode = this.GetComparerModeByColumnIndex(col.Index);
3235             if (comparerMode == null)
3236                 return;
3237
3238             this.SetSortColumn(comparerMode.Value);
3239         }
3240
3241         /// <summary>
3242         /// 発言一覧の最後列の項目でソートする
3243         /// </summary>
3244         private void SetSortLastColumn()
3245         {
3246             // 表示上の最後列にある ColumnHeader を求める
3247             var col = this._curList.Columns.Cast<ColumnHeader>()
3248                 .OrderByDescending(x => x.DisplayIndex)
3249                 .First();
3250
3251             var comparerMode = this.GetComparerModeByColumnIndex(col.Index);
3252             if (comparerMode == null)
3253                 return;
3254
3255             this.SetSortColumn(comparerMode.Value);
3256         }
3257
3258         /// <summary>
3259         /// 発言一覧を指定された ComparerMode に基づいてソートする
3260         /// </summary>
3261         private void SetSortColumn(ComparerMode sortColumn)
3262         {
3263             if (this._cfgCommon.SortOrderLock)
3264                 return;
3265
3266             this._statuses.ToggleSortOrder(sortColumn);
3267             this.InitColumnText();
3268
3269             var list = this._curList;
3270             if (_iconCol)
3271             {
3272                 list.Columns[0].Text = this.ColumnText[0];
3273                 list.Columns[1].Text = this.ColumnText[2];
3274             }
3275             else
3276             {
3277                 for (var i = 0; i <= 7; i++)
3278                 {
3279                     list.Columns[i].Text = this.ColumnText[i];
3280                 }
3281             }
3282
3283             this.PurgeListViewItemCache();
3284
3285             var tab = this._statuses.Tabs[this._curTab.Text];
3286             if (tab.AllCount > 0 && this._curPost != null)
3287             {
3288                 var idx = tab.IndexOf(this._curPost.StatusId);
3289                 if (idx > -1)
3290                 {
3291                     this.SelectListItem(list, idx);
3292                     list.EnsureVisible(idx);
3293                 }
3294             }
3295             list.Refresh();
3296
3297             this.ModifySettingCommon = true;
3298         }
3299
3300         private void TweenMain_LocationChanged(object sender, EventArgs e)
3301         {
3302             if (this.WindowState == FormWindowState.Normal && !_initialLayout)
3303             {
3304                 _myLoc = this.DesktopLocation;
3305                 ModifySettingLocal = true;
3306             }
3307         }
3308
3309         private void ContextMenuOperate_Opening(object sender, CancelEventArgs e)
3310         {
3311             if (ListTab.SelectedTab == null) return;
3312             if (_statuses == null || _statuses.Tabs == null || !_statuses.Tabs.ContainsKey(ListTab.SelectedTab.Text)) return;
3313             if (!this.ExistCurrentPost)
3314             {
3315                 ReplyStripMenuItem.Enabled = false;
3316                 ReplyAllStripMenuItem.Enabled = false;
3317                 DMStripMenuItem.Enabled = false;
3318                 ShowProfileMenuItem.Enabled = false;
3319                 ShowUserTimelineContextMenuItem.Enabled = false;
3320                 ListManageUserContextToolStripMenuItem2.Enabled = false;
3321                 MoveToFavToolStripMenuItem.Enabled = false;
3322                 TabMenuItem.Enabled = false;
3323                 IDRuleMenuItem.Enabled = false;
3324                 SourceRuleMenuItem.Enabled = false;
3325                 ReadedStripMenuItem.Enabled = false;
3326                 UnreadStripMenuItem.Enabled = false;
3327             }
3328             else
3329             {
3330                 ShowProfileMenuItem.Enabled = true;
3331                 ListManageUserContextToolStripMenuItem2.Enabled = true;
3332                 ReplyStripMenuItem.Enabled = true;
3333                 ReplyAllStripMenuItem.Enabled = true;
3334                 DMStripMenuItem.Enabled = true;
3335                 ShowUserTimelineContextMenuItem.Enabled = true;
3336                 MoveToFavToolStripMenuItem.Enabled = true;
3337                 TabMenuItem.Enabled = true;
3338                 IDRuleMenuItem.Enabled = true;
3339                 SourceRuleMenuItem.Enabled = true;
3340                 ReadedStripMenuItem.Enabled = true;
3341                 UnreadStripMenuItem.Enabled = true;
3342             }
3343             if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.DirectMessage || !this.ExistCurrentPost || _curPost.IsDm)
3344             {
3345                 FavAddToolStripMenuItem.Enabled = false;
3346                 FavRemoveToolStripMenuItem.Enabled = false;
3347                 StatusOpenMenuItem.Enabled = false;
3348                 FavorareMenuItem.Enabled = false;
3349                 ShowRelatedStatusesMenuItem.Enabled = false;
3350
3351                 ReTweetStripMenuItem.Enabled = false;
3352                 ReTweetUnofficialStripMenuItem.Enabled = false;
3353                 QuoteStripMenuItem.Enabled = false;
3354                 FavoriteRetweetContextMenu.Enabled = false;
3355                 FavoriteRetweetUnofficialContextMenu.Enabled = false;
3356             }
3357             else
3358             {
3359                 FavAddToolStripMenuItem.Enabled = true;
3360                 FavRemoveToolStripMenuItem.Enabled = true;
3361                 StatusOpenMenuItem.Enabled = true;
3362                 FavorareMenuItem.Enabled = true;
3363                 ShowRelatedStatusesMenuItem.Enabled = true;  //PublicSearchの時問題出るかも
3364
3365                 if (_curPost.IsMe)
3366                 {
3367                     ReTweetStripMenuItem.Enabled = false;  //公式RTは無効に
3368                     ReTweetUnofficialStripMenuItem.Enabled = true;
3369                     QuoteStripMenuItem.Enabled = true;
3370                     FavoriteRetweetContextMenu.Enabled = false;  //公式RTは無効に
3371                     FavoriteRetweetUnofficialContextMenu.Enabled = true;
3372                 }
3373                 else
3374                 {
3375                     if (_curPost.IsProtect)
3376                     {
3377                         ReTweetStripMenuItem.Enabled = false;
3378                         ReTweetUnofficialStripMenuItem.Enabled = false;
3379                         QuoteStripMenuItem.Enabled = false;
3380                         FavoriteRetweetContextMenu.Enabled = false;
3381                         FavoriteRetweetUnofficialContextMenu.Enabled = false;
3382                     }
3383                     else
3384                     {
3385                         ReTweetStripMenuItem.Enabled = true;
3386                         ReTweetUnofficialStripMenuItem.Enabled = true;
3387                         QuoteStripMenuItem.Enabled = true;
3388                         FavoriteRetweetContextMenu.Enabled = true;
3389                         FavoriteRetweetUnofficialContextMenu.Enabled = true;
3390                     }
3391                 }
3392             }
3393             //if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType != MyCommon.TabUsageType.Favorites)
3394             //{
3395             //    RefreshMoreStripMenuItem.Enabled = true;
3396             //}
3397             //else
3398             //{
3399             //    RefreshMoreStripMenuItem.Enabled = false;
3400             //}
3401             if (!this.ExistCurrentPost
3402                 || _curPost.InReplyToStatusId == null)
3403             {
3404                 RepliedStatusOpenMenuItem.Enabled = false;
3405             }
3406             else
3407             {
3408                 RepliedStatusOpenMenuItem.Enabled = true;
3409             }
3410             if (!this.ExistCurrentPost || string.IsNullOrEmpty(_curPost.RetweetedBy))
3411             {
3412                 MoveToRTHomeMenuItem.Enabled = false;
3413             }
3414             else
3415             {
3416                 MoveToRTHomeMenuItem.Enabled = true;
3417             }
3418
3419             if (this.ExistCurrentPost)
3420             {
3421                 this.DeleteStripMenuItem.Enabled = this._curPost.CanDeleteBy(this.tw.UserId);
3422                 if (this._curPost.RetweetedByUserId == this.tw.UserId)
3423                     this.DeleteStripMenuItem.Text = Properties.Resources.DeleteMenuText2;
3424                 else
3425                     this.DeleteStripMenuItem.Text = Properties.Resources.DeleteMenuText1;
3426             }
3427         }
3428
3429         private void ReplyStripMenuItem_Click(object sender, EventArgs e)
3430         {
3431             MakeReplyOrDirectStatus(false, true);
3432         }
3433
3434         private void DMStripMenuItem_Click(object sender, EventArgs e)
3435         {
3436             MakeReplyOrDirectStatus(false, false);
3437         }
3438
3439         private async Task doStatusDelete()
3440         {
3441             if (this._curTab == null || this._curList == null)
3442                 return;
3443
3444             if (this._curList.SelectedIndices.Count == 0)
3445                 return;
3446
3447             var posts = this._curList.SelectedIndices.Cast<int>()
3448                 .Select(x => this.GetCurTabPost(x))
3449                 .ToArray();
3450
3451             // 選択されたツイートの中に削除可能なものが一つでもあるか
3452             if (!posts.Any(x => x.CanDeleteBy(this.tw.UserId)))
3453                 return;
3454
3455             var ret = MessageBox.Show(this,
3456                 string.Format(Properties.Resources.DeleteStripMenuItem_ClickText1, Environment.NewLine),
3457                 Properties.Resources.DeleteStripMenuItem_ClickText2,
3458                 MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
3459
3460             if (ret != DialogResult.OK)
3461                 return;
3462
3463             var focusedIndex = this._curList.FocusedItem?.Index ?? this._curList.TopItem?.Index ?? 0;
3464
3465             using (ControlTransaction.Cursor(this, Cursors.WaitCursor))
3466             {
3467                 Exception lastException = null;
3468                 foreach (var post in posts)
3469                 {
3470                     if (!post.CanDeleteBy(this.tw.UserId))
3471                         continue;
3472
3473                     try
3474                     {
3475                         if (post.IsDm)
3476                         {
3477                             await this.twitterApi.DirectMessagesDestroy(post.StatusId)
3478                                 .IgnoreResponse();
3479                         }
3480                         else
3481                         {
3482                             if (post.RetweetedId != null && post.UserId == this.tw.UserId)
3483                                 // 他人に RT された自分のツイート
3484                                 await this.twitterApi.StatusesDestroy(post.RetweetedId.Value)
3485                                     .IgnoreResponse();
3486                             else
3487                                 // 自分のツイート or 自分が RT したツイート
3488                                 await this.twitterApi.StatusesDestroy(post.StatusId)
3489                                     .IgnoreResponse();
3490                         }
3491                     }
3492                     catch (WebApiException ex)
3493                     {
3494                         lastException = ex;
3495                         continue;
3496                     }
3497
3498                     this._statuses.RemovePostFromAllTabs(post.StatusId, setIsDeleted: true);
3499                 }
3500
3501                 if (lastException == null)
3502                     this.StatusLabel.Text = Properties.Resources.DeleteStripMenuItem_ClickText4; // 成功
3503                 else
3504                     this.StatusLabel.Text = Properties.Resources.DeleteStripMenuItem_ClickText3; // 失敗
3505
3506                 this.PurgeListViewItemCache();
3507                 this._curPost = null;
3508                 this._curItemIndex = -1;
3509
3510                 foreach (var tabPage in this.ListTab.TabPages.Cast<TabPage>())
3511                 {
3512                     var listView = (DetailsListView)tabPage.Tag;
3513                     var tab = this._statuses.Tabs[tabPage.Text];
3514
3515                     using (ControlTransaction.Update(listView))
3516                     {
3517                         listView.VirtualListSize = tab.AllCount;
3518
3519                         if (tabPage == this._curTab)
3520                         {
3521                             listView.SelectedIndices.Clear();
3522
3523                             if (tab.AllCount != 0)
3524                             {
3525                                 int selectedIndex;
3526                                 if (tab.AllCount - 1 > focusedIndex && focusedIndex > -1)
3527                                     selectedIndex = focusedIndex;
3528                                 else
3529                                     selectedIndex = tab.AllCount - 1;
3530
3531                                 listView.SelectedIndices.Add(selectedIndex);
3532                                 listView.EnsureVisible(selectedIndex);
3533                                 listView.FocusedItem = listView.Items[selectedIndex];
3534                             }
3535                         }
3536                     }
3537
3538                     if (this._cfgCommon.TabIconDisp && tab.UnreadCount == 0)
3539                     {
3540                         if (tabPage.ImageIndex == 0)
3541                             tabPage.ImageIndex = -1; // タブアイコン
3542                     }
3543                 }
3544
3545                 if (!this._cfgCommon.TabIconDisp)
3546                     this.ListTab.Refresh();
3547             }
3548         }
3549
3550         private async void DeleteStripMenuItem_Click(object sender, EventArgs e)
3551         {
3552             await this.doStatusDelete();
3553         }
3554
3555         private void ReadedStripMenuItem_Click(object sender, EventArgs e)
3556         {
3557             using (ControlTransaction.Update(this._curList))
3558             {
3559                 foreach (int idx in _curList.SelectedIndices)
3560                 {
3561                     var post = this._statuses.Tabs[this._curTab.Text][idx];
3562                     this._statuses.SetReadAllTab(post.StatusId, read: true);
3563                     ChangeCacheStyleRead(true, idx);
3564                 }
3565                 ColorizeList();
3566             }
3567             foreach (TabPage tb in ListTab.TabPages)
3568             {
3569                 if (_statuses.Tabs[tb.Text].UnreadCount == 0)
3570                 {
3571                     if (this._cfgCommon.TabIconDisp)
3572                     {
3573                         if (tb.ImageIndex == 0) tb.ImageIndex = -1; //タブアイコン
3574                     }
3575                 }
3576             }
3577             if (!this._cfgCommon.TabIconDisp) ListTab.Refresh();
3578         }
3579
3580         private void UnreadStripMenuItem_Click(object sender, EventArgs e)
3581         {
3582             using (ControlTransaction.Update(this._curList))
3583             {
3584                 foreach (int idx in _curList.SelectedIndices)
3585                 {
3586                     var post = this._statuses.Tabs[this._curTab.Text][idx];
3587                     this._statuses.SetReadAllTab(post.StatusId, read: false);
3588                     ChangeCacheStyleRead(false, idx);
3589                 }
3590                 ColorizeList();
3591             }
3592             foreach (TabPage tb in ListTab.TabPages)
3593             {
3594                 if (_statuses.Tabs[tb.Text].UnreadCount > 0)
3595                 {
3596                     if (this._cfgCommon.TabIconDisp)
3597                     {
3598                         if (tb.ImageIndex == -1) tb.ImageIndex = 0; //タブアイコン
3599                     }
3600                 }
3601             }
3602             if (!this._cfgCommon.TabIconDisp) ListTab.Refresh();
3603         }
3604
3605         private async void RefreshStripMenuItem_Click(object sender, EventArgs e)
3606         {
3607             await this.DoRefresh();
3608         }
3609
3610         private async Task DoRefresh()
3611         {
3612             if (_curTab != null)
3613             {
3614                 TabModel tab;
3615                 if (!this._statuses.Tabs.TryGetValue(this._curTab.Text, out tab))
3616                     return;
3617
3618                 switch (_statuses.Tabs[_curTab.Text].TabType)
3619                 {
3620                     case MyCommon.TabUsageType.Mentions:
3621                         await this.GetReplyAsync();
3622                         break;
3623                     case MyCommon.TabUsageType.DirectMessage:
3624                         await this.GetDirectMessagesAsync();
3625                         break;
3626                     case MyCommon.TabUsageType.Favorites:
3627                         await this.GetFavoritesAsync();
3628                         break;
3629                     //case MyCommon.TabUsageType.Profile:
3630                         //// TODO
3631                     case MyCommon.TabUsageType.PublicSearch:
3632                         var searchTab = (PublicSearchTabModel)tab;
3633                         if (string.IsNullOrEmpty(searchTab.SearchWords)) return;
3634                         await this.GetPublicSearchAsync(searchTab);
3635                         break;
3636                     case MyCommon.TabUsageType.UserTimeline:
3637                         await this.GetUserTimelineAsync((UserTimelineTabModel)tab);
3638                         break;
3639                     case MyCommon.TabUsageType.Lists:
3640                         var listTab = (ListTimelineTabModel)tab;
3641                         if (listTab.ListInfo == null || listTab.ListInfo.Id == 0) return;
3642                         await this.GetListTimelineAsync(listTab);
3643                         break;
3644                     default:
3645                         await this.GetHomeTimelineAsync();
3646                         break;
3647                 }
3648             }
3649             else
3650             {
3651                 await this.GetHomeTimelineAsync();
3652             }
3653         }
3654
3655         private async Task DoRefreshMore()
3656         {
3657             //ページ指定をマイナス1に
3658             if (_curTab != null)
3659             {
3660                 TabModel tab;
3661                 if (!this._statuses.Tabs.TryGetValue(this._curTab.Text, out tab))
3662                     return;
3663
3664                 switch (_statuses.Tabs[_curTab.Text].TabType)
3665                 {
3666                     case MyCommon.TabUsageType.Mentions:
3667                         await this.GetReplyAsync(loadMore: true);
3668                         break;
3669                     case MyCommon.TabUsageType.DirectMessage:
3670                         await this.GetDirectMessagesAsync(loadMore: true);
3671                         break;
3672                     case MyCommon.TabUsageType.Favorites:
3673                         await this.GetFavoritesAsync(loadMore: true);
3674                         break;
3675                     case MyCommon.TabUsageType.Profile:
3676                         //// TODO
3677                         break;
3678                     case MyCommon.TabUsageType.PublicSearch:
3679                         var searchTab = (PublicSearchTabModel)tab;
3680                         if (string.IsNullOrEmpty(searchTab.SearchWords)) return;
3681                         await this.GetPublicSearchAsync(searchTab, loadMore: true);
3682                         break;
3683                     case MyCommon.TabUsageType.UserTimeline:
3684                         await this.GetUserTimelineAsync((UserTimelineTabModel)tab, loadMore: true);
3685                         break;
3686                     case MyCommon.TabUsageType.Lists:
3687                         var listTab = (ListTimelineTabModel)tab;
3688                         if (listTab.ListInfo == null || listTab.ListInfo.Id == 0) return;
3689                         await this.GetListTimelineAsync(listTab, loadMore: true);
3690                         break;
3691                     default:
3692                         await this.GetHomeTimelineAsync(loadMore: true);
3693                         break;
3694                 }
3695             }
3696             else
3697             {
3698                 await this.GetHomeTimelineAsync(loadMore: true);
3699             }
3700         }
3701
3702         private DialogResult ShowSettingDialog(bool showTaskbarIcon = false)
3703         {
3704             DialogResult result = DialogResult.Abort;
3705
3706             using (var settingDialog = new AppendSettingDialog())
3707             {
3708                 settingDialog.Icon = this.MainIcon;
3709                 settingDialog.Owner = this;
3710                 settingDialog.ShowInTaskbar = showTaskbarIcon;
3711                 settingDialog.IntervalChanged += this.TimerInterval_Changed;
3712
3713                 settingDialog.tw = this.tw;
3714                 settingDialog.twitterApi = this.twitterApi;
3715
3716                 settingDialog.LoadConfig(this._cfgCommon, this._cfgLocal);
3717
3718                 try
3719                 {
3720                     result = settingDialog.ShowDialog(this);
3721                 }
3722                 catch (Exception)
3723                 {
3724                     return DialogResult.Abort;
3725                 }
3726
3727                 if (result == DialogResult.OK)
3728                 {
3729                     lock (_syncObject)
3730                     {
3731                         settingDialog.SaveConfig(this._cfgCommon, this._cfgLocal);
3732                     }
3733                 }
3734             }
3735
3736             return result;
3737         }
3738
3739         private async void SettingStripMenuItem_Click(object sender, EventArgs e)
3740         {
3741             // 設定画面表示前のユーザー情報
3742             var oldUser = new { tw.AccessToken, tw.AccessTokenSecret, tw.Username, tw.UserId };
3743
3744             var oldIconSz = this._cfgCommon.IconSize;
3745
3746             if (ShowSettingDialog() == DialogResult.OK)
3747             {
3748                 lock (_syncObject)
3749                 {
3750                     tw.RestrictFavCheck = this._cfgCommon.RestrictFavCheck;
3751                     tw.ReadOwnPost = this._cfgCommon.ReadOwnPost;
3752                     ShortUrl.Instance.DisableExpanding = !this._cfgCommon.TinyUrlResolve;
3753                     ShortUrl.Instance.BitlyId = this._cfgCommon.BilyUser;
3754                     ShortUrl.Instance.BitlyKey = this._cfgCommon.BitlyPwd;
3755                     TwitterApiConnection.RestApiHost = this._cfgCommon.TwitterApiHost;
3756
3757                     Networking.DefaultTimeout = TimeSpan.FromSeconds(this._cfgCommon.DefaultTimeOut);
3758                     Networking.SetWebProxy(this._cfgLocal.ProxyType,
3759                         this._cfgLocal.ProxyAddress, this._cfgLocal.ProxyPort,
3760                         this._cfgLocal.ProxyUser, this._cfgLocal.ProxyPassword);
3761                     Networking.ForceIPv4 = this._cfgCommon.ForceIPv4;
3762
3763                     ImageSelector.Reset(tw, this.tw.Configuration);
3764
3765                     try
3766                     {
3767                         if (this._cfgCommon.TabIconDisp)
3768                         {
3769                             ListTab.DrawItem -= ListTab_DrawItem;
3770                             ListTab.DrawMode = TabDrawMode.Normal;
3771                             ListTab.ImageList = this.TabImage;
3772                         }
3773                         else
3774                         {
3775                             ListTab.DrawItem -= ListTab_DrawItem;
3776                             ListTab.DrawItem += ListTab_DrawItem;
3777                             ListTab.DrawMode = TabDrawMode.OwnerDrawFixed;
3778                             ListTab.ImageList = null;
3779                         }
3780                     }
3781                     catch (Exception ex)
3782                     {
3783                         ex.Data["Instance"] = "ListTab(TabIconDisp)";
3784                         ex.Data["IsTerminatePermission"] = false;
3785                         throw;
3786                     }
3787
3788                     try
3789                     {
3790                         if (!this._cfgCommon.UnreadManage)
3791                         {
3792                             ReadedStripMenuItem.Enabled = false;
3793                             UnreadStripMenuItem.Enabled = false;
3794                             if (this._cfgCommon.TabIconDisp)
3795                             {
3796                                 foreach (TabPage myTab in ListTab.TabPages)
3797                                 {
3798                                     myTab.ImageIndex = -1;
3799                                 }
3800                             }
3801                         }
3802                         else
3803                         {
3804                             ReadedStripMenuItem.Enabled = true;
3805                             UnreadStripMenuItem.Enabled = true;
3806                         }
3807                     }
3808                     catch (Exception ex)
3809                     {
3810                         ex.Data["Instance"] = "ListTab(UnreadManage)";
3811                         ex.Data["IsTerminatePermission"] = false;
3812                         throw;
3813                     }
3814
3815                     // タブの表示位置の決定
3816                     SetTabAlignment();
3817
3818                     SplitContainer1.IsPanelInverted = !this._cfgCommon.StatusAreaAtBottom;
3819
3820                     var imgazyobizinet = ThumbnailGenerator.ImgAzyobuziNetInstance;
3821                     imgazyobizinet.Enabled = this._cfgCommon.EnableImgAzyobuziNet;
3822                     imgazyobizinet.DisabledInDM = this._cfgCommon.ImgAzyobuziNetDisabledInDM;
3823
3824                     this.PlaySoundMenuItem.Checked = this._cfgCommon.PlaySound;
3825                     this.PlaySoundFileMenuItem.Checked = this._cfgCommon.PlaySound;
3826                     _fntUnread = this._cfgLocal.FontUnread;
3827                     _clUnread = this._cfgLocal.ColorUnread;
3828                     _fntReaded = this._cfgLocal.FontRead;
3829                     _clReaded = this._cfgLocal.ColorRead;
3830                     _clFav = this._cfgLocal.ColorFav;
3831                     _clOWL = this._cfgLocal.ColorOWL;
3832                     _clRetweet = this._cfgLocal.ColorRetweet;
3833                     _fntDetail = this._cfgLocal.FontDetail;
3834                     _clDetail = this._cfgLocal.ColorDetail;
3835                     _clDetailLink = this._cfgLocal.ColorDetailLink;
3836                     _clDetailBackcolor = this._cfgLocal.ColorDetailBackcolor;
3837                     _clSelf = this._cfgLocal.ColorSelf;
3838                     _clAtSelf = this._cfgLocal.ColorAtSelf;
3839                     _clTarget = this._cfgLocal.ColorTarget;
3840                     _clAtTarget = this._cfgLocal.ColorAtTarget;
3841                     _clAtFromTarget = this._cfgLocal.ColorAtFromTarget;
3842                     _clAtTo = this._cfgLocal.ColorAtTo;
3843                     _clListBackcolor = this._cfgLocal.ColorListBackcolor;
3844                     _clInputBackcolor = this._cfgLocal.ColorInputBackcolor;
3845                     _clInputFont = this._cfgLocal.ColorInputFont;
3846                     _fntInputFont = this._cfgLocal.FontInputFont;
3847                     _brsBackColorMine.Dispose();
3848                     _brsBackColorAt.Dispose();
3849                     _brsBackColorYou.Dispose();
3850                     _brsBackColorAtYou.Dispose();
3851                     _brsBackColorAtFromTarget.Dispose();
3852                     _brsBackColorAtTo.Dispose();
3853                     _brsBackColorNone.Dispose();
3854                     _brsBackColorMine = new SolidBrush(_clSelf);
3855                     _brsBackColorAt = new SolidBrush(_clAtSelf);
3856                     _brsBackColorYou = new SolidBrush(_clTarget);
3857                     _brsBackColorAtYou = new SolidBrush(_clAtTarget);
3858                     _brsBackColorAtFromTarget = new SolidBrush(_clAtFromTarget);
3859                     _brsBackColorAtTo = new SolidBrush(_clAtTo);
3860                     _brsBackColorNone = new SolidBrush(_clListBackcolor);
3861
3862                     try
3863                     {
3864                         if (StatusText.Focused) StatusText.BackColor = _clInputBackcolor;
3865                         StatusText.Font = _fntInputFont;
3866                         StatusText.ForeColor = _clInputFont;
3867                     }
3868                     catch (Exception ex)
3869                     {
3870                         MessageBox.Show(ex.Message);
3871                     }
3872
3873                     try
3874                     {
3875                         InitDetailHtmlFormat();
3876                     }
3877                     catch (Exception ex)
3878                     {
3879                         ex.Data["Instance"] = "Font";
3880                         ex.Data["IsTerminatePermission"] = false;
3881                         throw;
3882                     }
3883
3884                     try
3885                     {
3886                         foreach (TabPage tb in ListTab.TabPages)
3887                         {
3888                             if (this._cfgCommon.TabIconDisp)
3889                             {
3890                                 if (_statuses.Tabs[tb.Text].UnreadCount == 0)
3891                                     tb.ImageIndex = -1;
3892                                 else
3893                                     tb.ImageIndex = 0;
3894                             }
3895                         }
3896                     }
3897                     catch (Exception ex)
3898                     {
3899                         ex.Data["Instance"] = "ListTab(TabIconDisp no2)";
3900                         ex.Data["IsTerminatePermission"] = false;
3901                         throw;
3902                     }
3903
3904                     try
3905                     {
3906                         var oldIconCol = _iconCol;
3907
3908                         if (this._cfgCommon.IconSize != oldIconSz)
3909                             ApplyListViewIconSize(this._cfgCommon.IconSize);
3910
3911                         foreach (TabPage tp in ListTab.TabPages)
3912                         {
3913                             DetailsListView lst = (DetailsListView)tp.Tag;
3914
3915                             using (ControlTransaction.Update(lst))
3916                             {
3917                                 lst.GridLines = this._cfgCommon.ShowGrid;
3918                                 lst.Font = _fntReaded;
3919                                 lst.BackColor = _clListBackcolor;
3920
3921                                 if (_iconCol != oldIconCol)
3922                                     ResetColumns(lst);
3923                             }
3924                         }
3925                     }
3926                     catch (Exception ex)
3927                     {
3928                         ex.Data["Instance"] = "ListView(IconSize)";
3929                         ex.Data["IsTerminatePermission"] = false;
3930                         throw;
3931                     }
3932
3933                     SetMainWindowTitle();
3934                     SetNotifyIconText();
3935
3936                     this.PurgeListViewItemCache();
3937                     _curList?.Refresh();
3938                     ListTab.Refresh();
3939
3940                     _hookGlobalHotkey.UnregisterAllOriginalHotkey();
3941                     if (this._cfgCommon.HotkeyEnabled)
3942                     {
3943                         ///グローバルホットキーの登録。設定で変更可能にするかも
3944                         HookGlobalHotkey.ModKeys modKey = HookGlobalHotkey.ModKeys.None;
3945                         if ((this._cfgCommon.HotkeyModifier & Keys.Alt) == Keys.Alt)
3946                             modKey |= HookGlobalHotkey.ModKeys.Alt;
3947                         if ((this._cfgCommon.HotkeyModifier & Keys.Control) == Keys.Control)
3948                             modKey |= HookGlobalHotkey.ModKeys.Ctrl;
3949                         if ((this._cfgCommon.HotkeyModifier & Keys.Shift) == Keys.Shift)
3950                             modKey |=  HookGlobalHotkey.ModKeys.Shift;
3951                         if ((this._cfgCommon.HotkeyModifier & Keys.LWin) == Keys.LWin)
3952                             modKey |= HookGlobalHotkey.ModKeys.Win;
3953
3954                         _hookGlobalHotkey.RegisterOriginalHotkey(this._cfgCommon.HotkeyKey, this._cfgCommon.HotkeyValue, modKey);
3955                     }
3956
3957                     if (this._cfgCommon.IsUseNotifyGrowl) gh.RegisterGrowl();
3958                     try
3959                     {
3960                         StatusText_TextChanged(null, null);
3961                     }
3962                     catch (Exception)
3963                     {
3964                     }
3965                 }
3966             }
3967             else
3968             {
3969                 // キャンセル時は Twitter クラスの認証情報を画面表示前の状態に戻す
3970                 this.tw.Initialize(oldUser.AccessToken, oldUser.AccessTokenSecret, oldUser.Username, oldUser.UserId);
3971             }
3972
3973             Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3974
3975             this.TopMost = this._cfgCommon.AlwaysTop;
3976             SaveConfigsAll(false);
3977
3978             if (tw.Username != oldUser.Username)
3979                 await this.doGetFollowersMenu();
3980         }
3981
3982         /// <summary>
3983         /// タブの表示位置を設定する
3984         /// </summary>
3985         private void SetTabAlignment()
3986         {
3987             var newAlignment = this._cfgCommon.ViewTabBottom ? TabAlignment.Bottom : TabAlignment.Top;
3988             if (ListTab.Alignment == newAlignment) return;
3989
3990             // 各タブのリスト上の選択位置などを退避
3991             var listSelections = this.SaveListViewSelection();
3992
3993             ListTab.Alignment = newAlignment;
3994
3995             foreach (TabPage tab in ListTab.TabPages)
3996             {
3997                 DetailsListView lst = (DetailsListView)tab.Tag;
3998                 TabModel tabInfo = _statuses.Tabs[tab.Text];
3999                 using (ControlTransaction.Update(lst))
4000                 {
4001                     // 選択位置などを復元
4002                     this.RestoreListViewSelection(lst, tabInfo, listSelections[tabInfo.TabName]);
4003                 }
4004             }
4005         }
4006
4007         private void ApplyListViewIconSize(MyCommon.IconSizes iconSz)
4008         {
4009             // アイコンサイズの再設定
4010             _iconCol = false;
4011             switch (iconSz)
4012             {
4013                 case MyCommon.IconSizes.IconNone:
4014                     _iconSz = 0;
4015                     break;
4016                 case MyCommon.IconSizes.Icon16:
4017                     _iconSz = 16;
4018                     break;
4019                 case MyCommon.IconSizes.Icon24:
4020                     _iconSz = 26;
4021                     break;
4022                 case MyCommon.IconSizes.Icon48:
4023                     _iconSz = 48;
4024                     break;
4025                 case MyCommon.IconSizes.Icon48_2:
4026                     _iconSz = 48;
4027                     _iconCol = true;
4028                     break;
4029             }
4030
4031             if (_iconSz > 0)
4032             {
4033                 // ディスプレイの DPI 設定を考慮したサイズを設定する
4034                 _listViewImageList.ImageSize = new Size(
4035                     1,
4036                     (int)Math.Ceiling(this._iconSz * this.CurrentScaleFactor.Height));
4037             }
4038             else
4039             {
4040                 _listViewImageList.ImageSize = new Size(1, 1);
4041             }
4042         }
4043
4044         private void ResetColumns(DetailsListView list)
4045         {
4046             using (ControlTransaction.Update(list))
4047             using (ControlTransaction.Layout(list, false))
4048             {
4049                 // カラムヘッダの再設定
4050                 list.ColumnClick -= MyList_ColumnClick;
4051                 list.DrawColumnHeader -= MyList_DrawColumnHeader;
4052                 list.ColumnReordered -= MyList_ColumnReordered;
4053                 list.ColumnWidthChanged -= MyList_ColumnWidthChanged;
4054
4055                 var cols = list.Columns.Cast<ColumnHeader>().ToList();
4056                 list.Columns.Clear();
4057                 cols.ForEach(col => col.Dispose());
4058                 cols.Clear();
4059
4060                 InitColumns(list, true);
4061
4062                 list.ColumnClick += MyList_ColumnClick;
4063                 list.DrawColumnHeader += MyList_DrawColumnHeader;
4064                 list.ColumnReordered += MyList_ColumnReordered;
4065                 list.ColumnWidthChanged += MyList_ColumnWidthChanged;
4066             }
4067         }
4068
4069         public void AddNewTabForSearch(string searchWord)
4070         {
4071             //同一検索条件のタブが既に存在すれば、そのタブアクティブにして終了
4072             foreach (var tb in _statuses.GetTabsByType<PublicSearchTabModel>())
4073             {
4074                 if (tb.SearchWords == searchWord && string.IsNullOrEmpty(tb.SearchLang))
4075                 {
4076                     foreach (TabPage tp in ListTab.TabPages)
4077                     {
4078                         if (tb.TabName == tp.Text)
4079                         {
4080                             ListTab.SelectedTab = tp;
4081                             return;
4082                         }
4083                     }
4084                 }
4085             }
4086             //ユニークなタブ名生成
4087             string tabName = searchWord;
4088             for (int i = 0; i <= 100; i++)
4089             {
4090                 if (_statuses.ContainsTab(tabName))
4091                     tabName += "_";
4092                 else
4093                     break;
4094             }
4095             //タブ追加
4096             var tab = new PublicSearchTabModel(tabName);
4097             _statuses.AddTab(tab);
4098             AddNewTab(tab, startup: false);
4099             //追加したタブをアクティブに
4100             ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
4101             //検索条件の設定
4102             ComboBox cmb = (ComboBox)ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"];
4103             cmb.Items.Add(searchWord);
4104             cmb.Text = searchWord;
4105             SaveConfigsTabs();
4106             //検索実行
4107             this.SearchButton_Click(ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"], null);
4108         }
4109
4110         private void ShowUserTimeline()
4111         {
4112             if (!this.ExistCurrentPost) return;
4113             AddNewTabForUserTimeline(_curPost.ScreenName);
4114         }
4115
4116         private void SearchComboBox_KeyDown(object sender, KeyEventArgs e)
4117         {
4118             if (e.KeyCode == Keys.Escape)
4119             {
4120                 TabPage relTp = ListTab.SelectedTab;
4121                 RemoveSpecifiedTab(relTp.Text, false);
4122                 SaveConfigsTabs();
4123                 e.SuppressKeyPress = true;
4124             }
4125         }
4126
4127         public void AddNewTabForUserTimeline(string user)
4128         {
4129             //同一検索条件のタブが既に存在すれば、そのタブアクティブにして終了
4130             foreach (var tb in _statuses.GetTabsByType<UserTimelineTabModel>())
4131             {
4132                 if (tb.ScreenName == user)
4133                 {
4134                     foreach (TabPage tp in ListTab.TabPages)
4135                     {
4136                         if (tb.TabName == tp.Text)
4137                         {
4138                             ListTab.SelectedTab = tp;
4139                             return;
4140                         }
4141                     }
4142                 }
4143             }
4144             //ユニークなタブ名生成
4145             string tabName = "user:" + user;
4146             while (_statuses.ContainsTab(tabName))
4147             {
4148                 tabName += "_";
4149             }
4150             //タブ追加
4151             var tab = new UserTimelineTabModel(tabName, user);
4152             this._statuses.AddTab(tab);
4153             this.AddNewTab(tab, startup: false);
4154             //追加したタブをアクティブに
4155             ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
4156             SaveConfigsTabs();
4157             //検索実行
4158             this.GetUserTimelineAsync(tab);
4159         }
4160
4161         public bool AddNewTab(TabModel tab, bool startup)
4162         {
4163             //重複チェック
4164             foreach (TabPage tb in ListTab.TabPages)
4165             {
4166                 if (tb.Text == tab.TabName) return false;
4167             }
4168
4169             //新規タブ名チェック
4170             if (tab.TabName == Properties.Resources.AddNewTabText1) return false;
4171
4172             var _tabPage = new TabPage();
4173             var _listCustom = new DetailsListView();
4174
4175             int cnt = ListTab.TabPages.Count;
4176
4177             ///ToDo:Create and set controls follow tabtypes
4178
4179             using (ControlTransaction.Update(_listCustom))
4180             using (ControlTransaction.Layout(this.SplitContainer1.Panel1, false))
4181             using (ControlTransaction.Layout(this.SplitContainer1.Panel2, false))
4182             using (ControlTransaction.Layout(this.SplitContainer1, false))
4183             using (ControlTransaction.Layout(this.ListTab, false))
4184             using (ControlTransaction.Layout(this))
4185             using (ControlTransaction.Layout(_tabPage, false))
4186             {
4187                 _tabPage.Controls.Add(_listCustom);
4188
4189                 /// UserTimeline関連
4190                 var userTab = tab as UserTimelineTabModel;
4191                 var listTab = tab as ListTimelineTabModel;
4192                 var searchTab = tab as PublicSearchTabModel;
4193
4194                 if (userTab != null || listTab != null)
4195                 {
4196                     var label = new Label();
4197                     label.Dock = DockStyle.Top;
4198                     label.Name = "labelUser";
4199                     label.TabIndex = 0;
4200
4201                     if (listTab != null)
4202                     {
4203                         label.Text = listTab.ListInfo.ToString();
4204                     }
4205                     else if (userTab != null)
4206                     {
4207                         label.Text = userTab.ScreenName + "'s Timeline";
4208                     }
4209                     label.TextAlign = ContentAlignment.MiddleLeft;
4210                     using (ComboBox tmpComboBox = new ComboBox())
4211                     {
4212                         label.Height = tmpComboBox.Height;
4213                     }
4214                     _tabPage.Controls.Add(label);
4215                 }
4216                 /// 検索関連の準備
4217                 else if (searchTab != null)
4218                 {
4219                     var pnl = new Panel();
4220
4221                     var lbl = new Label();
4222                     var cmb = new ComboBox();
4223                     var btn = new Button();
4224                     var cmbLang = new ComboBox();
4225
4226                     using (ControlTransaction.Layout(pnl, false))
4227                     {
4228                         pnl.Controls.Add(cmb);
4229                         pnl.Controls.Add(cmbLang);
4230                         pnl.Controls.Add(btn);
4231                         pnl.Controls.Add(lbl);
4232                         pnl.Name = "panelSearch";
4233                         pnl.TabIndex = 0;
4234                         pnl.Dock = DockStyle.Top;
4235                         pnl.Height = cmb.Height;
4236                         pnl.Enter += SearchControls_Enter;
4237                         pnl.Leave += SearchControls_Leave;
4238
4239                         cmb.Text = "";
4240                         cmb.Anchor = AnchorStyles.Left | AnchorStyles.Right;
4241                         cmb.Dock = DockStyle.Fill;
4242                         cmb.Name = "comboSearch";
4243                         cmb.DropDownStyle = ComboBoxStyle.DropDown;
4244                         cmb.ImeMode = ImeMode.NoControl;
4245                         cmb.TabStop = false;
4246                         cmb.TabIndex = 1;
4247                         cmb.AutoCompleteMode = AutoCompleteMode.None;
4248                         cmb.KeyDown += SearchComboBox_KeyDown;
4249
4250                         cmbLang.Text = "";
4251                         cmbLang.Anchor = AnchorStyles.Left | AnchorStyles.Right;
4252                         cmbLang.Dock = DockStyle.Right;
4253                         cmbLang.Width = 50;
4254                         cmbLang.Name = "comboLang";
4255                         cmbLang.DropDownStyle = ComboBoxStyle.DropDownList;
4256                         cmbLang.TabStop = false;
4257                         cmbLang.TabIndex = 2;
4258                         cmbLang.Items.Add("");
4259                         cmbLang.Items.Add("ja");
4260                         cmbLang.Items.Add("en");
4261                         cmbLang.Items.Add("ar");
4262                         cmbLang.Items.Add("da");
4263                         cmbLang.Items.Add("nl");
4264                         cmbLang.Items.Add("fa");
4265                         cmbLang.Items.Add("fi");
4266                         cmbLang.Items.Add("fr");
4267                         cmbLang.Items.Add("de");
4268                         cmbLang.Items.Add("hu");
4269                         cmbLang.Items.Add("is");
4270                         cmbLang.Items.Add("it");
4271                         cmbLang.Items.Add("no");
4272                         cmbLang.Items.Add("pl");
4273                         cmbLang.Items.Add("pt");
4274                         cmbLang.Items.Add("ru");
4275                         cmbLang.Items.Add("es");
4276                         cmbLang.Items.Add("sv");
4277                         cmbLang.Items.Add("th");
4278
4279                         lbl.Text = "Search(C-S-f)";
4280                         lbl.Name = "label1";
4281                         lbl.Dock = DockStyle.Left;
4282                         lbl.Width = 90;
4283                         lbl.Height = cmb.Height;
4284                         lbl.TextAlign = ContentAlignment.MiddleLeft;
4285                         lbl.TabIndex = 0;
4286
4287                         btn.Text = "Search";
4288                         btn.Name = "buttonSearch";
4289                         btn.UseVisualStyleBackColor = true;
4290                         btn.Dock = DockStyle.Right;
4291                         btn.TabStop = false;
4292                         btn.TabIndex = 3;
4293                         btn.Click += SearchButton_Click;
4294
4295                         if (!string.IsNullOrEmpty(searchTab.SearchWords))
4296                         {
4297                             cmb.Items.Add(searchTab.SearchWords);
4298                             cmb.Text = searchTab.SearchWords;
4299                         }
4300
4301                         cmbLang.Text = searchTab.SearchLang;
4302
4303                         _tabPage.Controls.Add(pnl);
4304                     }
4305                 }
4306
4307                 _tabPage.Tag = _listCustom;
4308                 this.ListTab.Controls.Add(_tabPage);
4309
4310                 _tabPage.Location = new Point(4, 4);
4311                 _tabPage.Name = "CTab" + cnt;
4312                 _tabPage.Size = new Size(380, 260);
4313                 _tabPage.TabIndex = 2 + cnt;
4314                 _tabPage.Text = tab.TabName;
4315                 _tabPage.UseVisualStyleBackColor = true;
4316                 _tabPage.AccessibleRole = AccessibleRole.PageTab;
4317
4318                 _listCustom.AccessibleName = Properties.Resources.AddNewTab_ListView_AccessibleName;
4319                 _listCustom.TabIndex = 1;
4320                 _listCustom.AllowColumnReorder = true;
4321                 _listCustom.ContextMenuStrip = this.ContextMenuOperate;
4322                 _listCustom.ColumnHeaderContextMenuStrip = this.ContextMenuColumnHeader;
4323                 _listCustom.Dock = DockStyle.Fill;
4324                 _listCustom.FullRowSelect = true;
4325                 _listCustom.HideSelection = false;
4326                 _listCustom.Location = new Point(0, 0);
4327                 _listCustom.Margin = new Padding(0);
4328                 _listCustom.Name = "CList" + Environment.TickCount;
4329                 _listCustom.ShowItemToolTips = true;
4330                 _listCustom.Size = new Size(380, 260);
4331                 _listCustom.UseCompatibleStateImageBehavior = false;
4332                 _listCustom.View = View.Details;
4333                 _listCustom.OwnerDraw = true;
4334                 _listCustom.VirtualMode = true;
4335                 _listCustom.Font = _fntReaded;
4336                 _listCustom.BackColor = _clListBackcolor;
4337
4338                 _listCustom.GridLines = this._cfgCommon.ShowGrid;
4339                 _listCustom.AllowDrop = true;
4340
4341                 _listCustom.SmallImageList = _listViewImageList;
4342
4343                 InitColumns(_listCustom, startup);
4344
4345                 _listCustom.SelectedIndexChanged += MyList_SelectedIndexChanged;
4346                 _listCustom.MouseDoubleClick += MyList_MouseDoubleClick;
4347                 _listCustom.ColumnClick += MyList_ColumnClick;
4348                 _listCustom.DrawColumnHeader += MyList_DrawColumnHeader;
4349                 _listCustom.DragDrop += TweenMain_DragDrop;
4350                 _listCustom.DragEnter += TweenMain_DragEnter;
4351                 _listCustom.DragOver += TweenMain_DragOver;
4352                 _listCustom.DrawItem += MyList_DrawItem;
4353                 _listCustom.MouseClick += MyList_MouseClick;
4354                 _listCustom.ColumnReordered += MyList_ColumnReordered;
4355                 _listCustom.ColumnWidthChanged += MyList_ColumnWidthChanged;
4356                 _listCustom.CacheVirtualItems += MyList_CacheVirtualItems;
4357                 _listCustom.RetrieveVirtualItem += MyList_RetrieveVirtualItem;
4358                 _listCustom.DrawSubItem += MyList_DrawSubItem;
4359                 _listCustom.HScrolled += MyList_HScrolled;
4360             }
4361
4362             return true;
4363         }
4364
4365         public bool RemoveSpecifiedTab(string TabName, bool confirm)
4366         {
4367             var tabInfo = _statuses.GetTabByName(TabName);
4368             if (tabInfo.IsDefaultTabType || tabInfo.Protected) return false;
4369
4370             if (confirm)
4371             {
4372                 string tmp = string.Format(Properties.Resources.RemoveSpecifiedTabText1, Environment.NewLine);
4373                 if (MessageBox.Show(tmp, TabName + " " + Properties.Resources.RemoveSpecifiedTabText2,
4374                                  MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
4375                 {
4376                     return false;
4377                 }
4378             }
4379
4380             var _tabPage = ListTab.TabPages.Cast<TabPage>().FirstOrDefault(tp => tp.Text == TabName);
4381             if (_tabPage == null) return false;
4382
4383             SetListProperty();   //他のタブに列幅等を反映
4384
4385             //オブジェクトインスタンスの削除
4386             DetailsListView _listCustom = (DetailsListView)_tabPage.Tag;
4387             _tabPage.Tag = null;
4388
4389             using (ControlTransaction.Layout(this.SplitContainer1.Panel1, false))
4390             using (ControlTransaction.Layout(this.SplitContainer1.Panel2, false))
4391             using (ControlTransaction.Layout(this.SplitContainer1, false))
4392             using (ControlTransaction.Layout(this.ListTab, false))
4393             using (ControlTransaction.Layout(this))
4394             using (ControlTransaction.Layout(_tabPage, false))
4395             {
4396                 if (this.ListTab.SelectedTab == _tabPage)
4397                 {
4398                     this.ListTab.SelectTab((this._beforeSelectedTab != null && this.ListTab.TabPages.Contains(this._beforeSelectedTab)) ? this._beforeSelectedTab : this.ListTab.TabPages[0]);
4399                     this._beforeSelectedTab = null;
4400                 }
4401                 this.ListTab.Controls.Remove(_tabPage);
4402
4403                 // 後付けのコントロールを破棄
4404                 if (tabInfo.TabType == MyCommon.TabUsageType.UserTimeline || tabInfo.TabType == MyCommon.TabUsageType.Lists)
4405                 {
4406                     using (Control label = _tabPage.Controls["labelUser"])
4407                     {
4408                         _tabPage.Controls.Remove(label);
4409                     }
4410                 }
4411                 else if (tabInfo.TabType == MyCommon.TabUsageType.PublicSearch)
4412                 {
4413                     using (Control pnl = _tabPage.Controls["panelSearch"])
4414                     {
4415                         pnl.Enter -= SearchControls_Enter;
4416                         pnl.Leave -= SearchControls_Leave;
4417                         _tabPage.Controls.Remove(pnl);
4418
4419                         foreach (Control ctrl in pnl.Controls)
4420                         {
4421                             if (ctrl.Name == "buttonSearch")
4422                             {
4423                                 ctrl.Click -= SearchButton_Click;
4424                             }
4425                             else if (ctrl.Name == "comboSearch")
4426                             {
4427                                 ctrl.KeyDown -= SearchComboBox_KeyDown;
4428                             }
4429                             pnl.Controls.Remove(ctrl);
4430                             ctrl.Dispose();
4431                         }
4432                     }
4433                 }
4434
4435                 _tabPage.Controls.Remove(_listCustom);
4436
4437                 _listCustom.SelectedIndexChanged -= MyList_SelectedIndexChanged;
4438                 _listCustom.MouseDoubleClick -= MyList_MouseDoubleClick;
4439                 _listCustom.ColumnClick -= MyList_ColumnClick;
4440                 _listCustom.DrawColumnHeader -= MyList_DrawColumnHeader;
4441                 _listCustom.DragDrop -= TweenMain_DragDrop;
4442                 _listCustom.DragEnter -= TweenMain_DragEnter;
4443                 _listCustom.DragOver -= TweenMain_DragOver;
4444                 _listCustom.DrawItem -= MyList_DrawItem;
4445                 _listCustom.MouseClick -= MyList_MouseClick;
4446                 _listCustom.ColumnReordered -= MyList_ColumnReordered;
4447                 _listCustom.ColumnWidthChanged -= MyList_ColumnWidthChanged;
4448                 _listCustom.CacheVirtualItems -= MyList_CacheVirtualItems;
4449                 _listCustom.RetrieveVirtualItem -= MyList_RetrieveVirtualItem;
4450                 _listCustom.DrawSubItem -= MyList_DrawSubItem;
4451                 _listCustom.HScrolled -= MyList_HScrolled;
4452
4453                 var cols = _listCustom.Columns.Cast<ColumnHeader>().ToList<ColumnHeader>();
4454                 _listCustom.Columns.Clear();
4455                 cols.ForEach(col => col.Dispose());
4456                 cols.Clear();
4457
4458                 _listCustom.ContextMenuStrip = null;
4459                 _listCustom.ColumnHeaderContextMenuStrip = null;
4460                 _listCustom.Font = null;
4461
4462                 _listCustom.SmallImageList = null;
4463                 _listCustom.ListViewItemSorter = null;
4464
4465                 //キャッシュのクリア
4466                 if (_curTab.Equals(_tabPage))
4467                 {
4468                     _curTab = null;
4469                     _curItemIndex = -1;
4470                     _curList = null;
4471                     _curPost = null;
4472                 }
4473                 this.PurgeListViewItemCache();
4474             }
4475
4476             _tabPage.Dispose();
4477             _listCustom.Dispose();
4478             _statuses.RemoveTab(TabName);
4479
4480             foreach (TabPage tp in ListTab.TabPages)
4481             {
4482                 DetailsListView lst = (DetailsListView)tp.Tag;
4483                 var count = _statuses.Tabs[tp.Text].AllCount;
4484                 lst.VirtualListSize = count;
4485             }
4486
4487             return true;
4488         }
4489
4490         private void ListTab_Deselected(object sender, TabControlEventArgs e)
4491         {
4492             this.PurgeListViewItemCache();
4493             _beforeSelectedTab = e.TabPage;
4494         }
4495
4496         private void ListTab_MouseMove(object sender, MouseEventArgs e)
4497         {
4498             //タブのD&D
4499
4500             if (!this._cfgCommon.TabMouseLock && e.Button == MouseButtons.Left && _tabDrag)
4501             {
4502                 string tn = "";
4503                 Rectangle dragEnableRectangle = new Rectangle(_tabMouseDownPoint.X - (SystemInformation.DragSize.Width / 2), _tabMouseDownPoint.Y - (SystemInformation.DragSize.Height / 2), SystemInformation.DragSize.Width, SystemInformation.DragSize.Height);
4504                 if (!dragEnableRectangle.Contains(e.Location))
4505                 {
4506                     //タブが多段の場合にはMouseDownの前の段階で選択されたタブの段が変わっているので、このタイミングでカーソルの位置からタブを判定出来ない。
4507                     tn = ListTab.SelectedTab.Text;
4508                 }
4509
4510                 if (string.IsNullOrEmpty(tn)) return;
4511
4512                 foreach (TabPage tb in ListTab.TabPages)
4513                 {
4514                     if (tb.Text == tn)
4515                     {
4516                         ListTab.DoDragDrop(tb, DragDropEffects.All);
4517                         break;
4518                     }
4519                 }
4520             }
4521             else
4522             {
4523                 _tabDrag = false;
4524             }
4525
4526             Point cpos = new Point(e.X, e.Y);
4527             for (int i = 0; i < ListTab.TabPages.Count; i++)
4528             {
4529                 Rectangle rect = ListTab.GetTabRect(i);
4530                 if (rect.Left <= cpos.X & cpos.X <= rect.Right &
4531                    rect.Top <= cpos.Y & cpos.Y <= rect.Bottom)
4532                 {
4533                     _rclickTabName = ListTab.TabPages[i].Text;
4534                     break;
4535                 }
4536             }
4537         }
4538
4539         private async void ListTab_SelectedIndexChanged(object sender, EventArgs e)
4540         {
4541             //_curList.Refresh();
4542             SetMainWindowTitle();
4543             SetStatusLabelUrl();
4544             SetApiStatusLabel();
4545             if (ListTab.Focused || ((Control)ListTab.SelectedTab.Tag).Focused) this.Tag = ListTab.Tag;
4546             TabMenuControl(ListTab.SelectedTab.Text);
4547             this.PushSelectPostChain();
4548             await DispSelectedPost();
4549         }
4550
4551         private void SetListProperty()
4552         {
4553             //削除などで見つからない場合は処理せず
4554             if (_curList == null) return;
4555             if (!_isColumnChanged) return;
4556
4557             int[] dispOrder = new int[_curList.Columns.Count];
4558             for (int i = 0; i < _curList.Columns.Count; i++)
4559             {
4560                 for (int j = 0; j < _curList.Columns.Count; j++)
4561                 {
4562                     if (_curList.Columns[j].DisplayIndex == i)
4563                     {
4564                         dispOrder[i] = j;
4565                         break;
4566                     }
4567                 }
4568             }
4569
4570             //列幅、列並びを他のタブに設定
4571             foreach (TabPage tb in ListTab.TabPages)
4572             {
4573                 if (!tb.Equals(_curTab))
4574                 {
4575                     if (tb.Tag != null && tb.Controls.Count > 0)
4576                     {
4577                         DetailsListView lst = (DetailsListView)tb.Tag;
4578                         for (int i = 0; i < lst.Columns.Count; i++)
4579                         {
4580                             lst.Columns[dispOrder[i]].DisplayIndex = i;
4581                             lst.Columns[i].Width = _curList.Columns[i].Width;
4582                         }
4583                     }
4584                 }
4585             }
4586
4587             _isColumnChanged = false;
4588         }
4589
4590         private void StatusText_KeyPress(object sender, KeyPressEventArgs e)
4591         {
4592             if (e.KeyChar == '@')
4593             {
4594                 if (!this._cfgCommon.UseAtIdSupplement) return;
4595                 //@マーク
4596                 int cnt = AtIdSupl.ItemCount;
4597                 ShowSuplDialog(StatusText, AtIdSupl);
4598                 if (cnt != AtIdSupl.ItemCount) ModifySettingAtId = true;
4599                 e.Handled = true;
4600             }
4601             else if (e.KeyChar == '#')
4602             {
4603                 if (!this._cfgCommon.UseHashSupplement) return;
4604                 ShowSuplDialog(StatusText, HashSupl);
4605                 e.Handled = true;
4606             }
4607         }
4608
4609         public void ShowSuplDialog(TextBox owner, AtIdSupplement dialog)
4610         {
4611             ShowSuplDialog(owner, dialog, 0, "");
4612         }
4613
4614         public void ShowSuplDialog(TextBox owner, AtIdSupplement dialog, int offset)
4615         {
4616             ShowSuplDialog(owner, dialog, offset, "");
4617         }
4618
4619         public void ShowSuplDialog(TextBox owner, AtIdSupplement dialog, int offset, string startswith)
4620         {
4621             dialog.StartsWith = startswith;
4622             if (dialog.Visible)
4623             {
4624                 dialog.Focus();
4625             }
4626             else
4627             {
4628                 dialog.ShowDialog();
4629             }
4630             this.TopMost = this._cfgCommon.AlwaysTop;
4631             int selStart = owner.SelectionStart;
4632             string fHalf = "";
4633             string eHalf = "";
4634             if (dialog.DialogResult == DialogResult.OK)
4635             {
4636                 if (!string.IsNullOrEmpty(dialog.inputText))
4637                 {
4638                     if (selStart > 0)
4639                     {
4640                         fHalf = owner.Text.Substring(0, selStart - offset);
4641                     }
4642                     if (selStart < owner.Text.Length)
4643                     {
4644                         eHalf = owner.Text.Substring(selStart);
4645                     }
4646                     owner.Text = fHalf + dialog.inputText + eHalf;
4647                     owner.SelectionStart = selStart + dialog.inputText.Length;
4648                 }
4649             }
4650             else
4651             {
4652                 if (selStart > 0)
4653                 {
4654                     fHalf = owner.Text.Substring(0, selStart);
4655                 }
4656                 if (selStart < owner.Text.Length)
4657                 {
4658                     eHalf = owner.Text.Substring(selStart);
4659                 }
4660                 owner.Text = fHalf + eHalf;
4661                 if (selStart > 0)
4662                 {
4663                     owner.SelectionStart = selStart;
4664                 }
4665             }
4666             owner.Focus();
4667         }
4668
4669         private void StatusText_KeyUp(object sender, KeyEventArgs e)
4670         {
4671             //スペースキーで未読ジャンプ
4672             if (!e.Alt && !e.Control && !e.Shift)
4673             {
4674                 if (e.KeyCode == Keys.Space || e.KeyCode == Keys.ProcessKey)
4675                 {
4676                     bool isSpace = false;
4677                     foreach (char c in StatusText.Text)
4678                     {
4679                         if (c == ' ' || c == ' ')
4680                         {
4681                             isSpace = true;
4682                         }
4683                         else
4684                         {
4685                             isSpace = false;
4686                             break;
4687                         }
4688                     }
4689                     if (isSpace)
4690                     {
4691                         e.Handled = true;
4692                         StatusText.Text = "";
4693                         JumpUnreadMenuItem_Click(null, null);
4694                     }
4695                 }
4696             }
4697             this.StatusText_TextChanged(null, null);
4698         }
4699
4700         private void StatusText_TextChanged(object sender, EventArgs e)
4701         {
4702             //文字数カウント
4703             int pLen = this.GetRestStatusCount(this.FormatStatusText(this.StatusText.Text));
4704             lblLen.Text = pLen.ToString();
4705             if (pLen < 0)
4706             {
4707                 StatusText.ForeColor = Color.Red;
4708             }
4709             else
4710             {
4711                 StatusText.ForeColor = _clInputFont;
4712             }
4713             if (string.IsNullOrEmpty(StatusText.Text))
4714             {
4715                 this.inReplyTo = null;
4716             }
4717         }
4718
4719         /// <summary>
4720         /// ツイート投稿前のフッター付与などの前処理を行います
4721         /// </summary>
4722         private string FormatStatusText(string statusText)
4723         {
4724             statusText = statusText.Replace("\r\n", "\n");
4725
4726             if (this.ToolStripMenuItemUrlMultibyteSplit.Checked)
4727             {
4728                 // URLと全角文字の切り離し
4729                 statusText = Regex.Replace(statusText, @"https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#^]+", "$& ");
4730             }
4731
4732             if (this.IdeographicSpaceToSpaceToolStripMenuItem.Checked)
4733             {
4734                 // 文中の全角スペースを半角スペース1個にする
4735                 statusText = statusText.Replace(" ", " ");
4736             }
4737
4738             // DM の場合はこれ以降の処理を行わない
4739             if (statusText.StartsWith("D ", StringComparison.OrdinalIgnoreCase))
4740                 return statusText;
4741
4742             bool disableFooter;
4743             if (this._cfgCommon.PostShiftEnter)
4744             {
4745                 disableFooter = MyCommon.IsKeyDown(Keys.Control);
4746             }
4747             else
4748             {
4749                 if (this.StatusText.Multiline && !this._cfgCommon.PostCtrlEnter)
4750                     disableFooter = MyCommon.IsKeyDown(Keys.Control);
4751                 else
4752                     disableFooter = MyCommon.IsKeyDown(Keys.Shift);
4753             }
4754
4755             if (statusText.Contains("RT @"))
4756                 disableFooter = true;
4757
4758             var header = "";
4759             var footer = "";
4760
4761             var hashtag = this.HashMgr.UseHash;
4762             if (!string.IsNullOrEmpty(hashtag) && !(this.HashMgr.IsNotAddToAtReply && this.inReplyTo != null))
4763             {
4764                 if (HashMgr.IsHead)
4765                     header = HashMgr.UseHash + " ";
4766                 else
4767                     footer = " " + HashMgr.UseHash;
4768             }
4769
4770             if (!disableFooter)
4771             {
4772                 if (this._cfgLocal.UseRecommendStatus)
4773                 {
4774                     // 推奨ステータスを使用する
4775                     footer += this.recommendedStatusFooter;
4776                 }
4777                 else if (!string.IsNullOrEmpty(this._cfgLocal.StatusText))
4778                 {
4779                     // テキストボックスに入力されている文字列を使用する
4780                     footer += " " + this._cfgLocal.StatusText.Trim();
4781                 }
4782             }
4783
4784             statusText = header + statusText + footer;
4785
4786             if (this.ToolStripMenuItemPreventSmsCommand.Checked)
4787             {
4788                 // ツイートが意図せず SMS コマンドとして解釈されることを回避 (D, DM, M のみ)
4789                 // 参照: https://support.twitter.com/articles/14020
4790
4791                 if (Regex.IsMatch(statusText, @"^[+\-\[\]\s\\.,*/(){}^~|='&%$#""<>?]*(d|dm|m)([+\-\[\]\s\\.,*/(){}^~|='&%$#""<>?]+|$)", RegexOptions.IgnoreCase)
4792                     && !Twitter.DMSendTextRegex.IsMatch(statusText))
4793                 {
4794                     // U+200B (ZERO WIDTH SPACE) を先頭に加えて回避
4795                     statusText = '\u200b' + statusText;
4796                 }
4797             }
4798
4799             return statusText;
4800         }
4801
4802         /// <summary>
4803         /// 投稿欄に表示する入力可能な文字数を計算します
4804         /// </summary>
4805         private int GetRestStatusCount(string statusText)
4806         {
4807             //文字数カウント
4808             var remainCount = this.tw.GetTextLengthRemain(statusText);
4809
4810             if (this.ImageSelector.Visible && !string.IsNullOrEmpty(this.ImageSelector.ServiceName))
4811             {
4812                 remainCount -= this.tw.Configuration.CharactersReservedPerMedia;
4813             }
4814
4815             return remainCount;
4816         }
4817
4818         private void MyList_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
4819         {
4820             if (sender != this._curList)
4821                 return;
4822
4823             var listCache = this._listItemCache;
4824             if (listCache != null && listCache.IsSupersetOf(e.StartIndex, e.EndIndex))
4825             {
4826                 // If the newly requested cache is a subset of the old cache,
4827                 // no need to rebuild everything, so do nothing.
4828                 return;
4829             }
4830
4831             // Now we need to rebuild the cache.
4832             this.CreateCache(e.StartIndex, e.EndIndex);
4833         }
4834
4835         private void MyList_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
4836         {
4837             var listCache = this._listItemCache;
4838             if (listCache != null && listCache.TargetList == sender)
4839             {
4840                 ListViewItem item;
4841                 PostClass cacheItemPost;
4842                 if (listCache.TryGetValue(e.ItemIndex, out item, out cacheItemPost))
4843                 {
4844                     e.Item = item;
4845                     return;
4846                 }
4847             }
4848
4849             // A cache miss, so create a new ListViewItem and pass it back.
4850             TabPage tb = (TabPage)((DetailsListView)sender).Parent;
4851             try
4852             {
4853                 e.Item = this.CreateItem(tb, _statuses.Tabs[tb.Text][e.ItemIndex], e.ItemIndex);
4854             }
4855             catch (Exception)
4856             {
4857                 // 不正な要求に対する間に合わせの応答
4858                 string[] sitem = {"", "", "", "", "", "", "", ""};
4859                 e.Item = new ImageListViewItem(sitem);
4860             }
4861         }
4862
4863         private void CreateCache(int startIndex, int endIndex)
4864         {
4865             var tabInfo = this._statuses.Tabs[this._curTab.Text];
4866
4867             if (tabInfo.AllCount == 0)
4868                 return;
4869
4870             // キャッシュ要求(要求範囲±30を作成)
4871             startIndex = Math.Max(startIndex - 30, 0);
4872             endIndex = Math.Min(endIndex + 30, tabInfo.AllCount - 1);
4873
4874             var cacheLength = endIndex - startIndex + 1;
4875
4876             var posts = tabInfo[startIndex, endIndex]; //配列で取得
4877             var listItems = Enumerable.Range(0, cacheLength)
4878                 .Select(x => this.CreateItem(this._curTab, posts[x], startIndex + x))
4879                 .ToArray();
4880
4881             var listCache = new ListViewItemCache
4882             {
4883                 TargetList = this._curList,
4884                 StartIndex = startIndex,
4885                 EndIndex = endIndex,
4886                 Post = posts,
4887                 ListItem = listItems,
4888             };
4889
4890             Interlocked.Exchange(ref this._listItemCache, listCache);
4891         }
4892
4893         /// <summary>
4894         /// DetailsListView のための ListViewItem のキャッシュを消去する
4895         /// </summary>
4896         private void PurgeListViewItemCache()
4897         {
4898             Interlocked.Exchange(ref this._listItemCache, null);
4899         }
4900
4901         private ListViewItem CreateItem(TabPage Tab, PostClass Post, int Index)
4902         {
4903             StringBuilder mk = new StringBuilder();
4904             //if (Post.IsDeleted) mk.Append("×");
4905             //if (Post.IsMark) mk.Append("♪");
4906             //if (Post.IsProtect) mk.Append("Ю");
4907             //if (Post.InReplyToStatusId != null) mk.Append("⇒");
4908             if (Post.FavoritedCount > 0) mk.Append("+" + Post.FavoritedCount);
4909             ImageListViewItem itm;
4910             if (Post.RetweetedId == null)
4911             {
4912                 string[] sitem= {"",
4913                                  Post.Nickname,
4914                                  Post.IsDeleted ? "(DELETED)" : Post.TextSingleLine,
4915                                  Post.CreatedAt.ToString(this._cfgCommon.DateTimeFormat),
4916                                  Post.ScreenName,
4917                                  "",
4918                                  mk.ToString(),
4919                                  Post.Source};
4920                 itm = new ImageListViewItem(sitem, this.IconCache, Post.ImageUrl);
4921             }
4922             else
4923             {
4924                 string[] sitem = {"",
4925                                   Post.Nickname,
4926                                   Post.IsDeleted ? "(DELETED)" : Post.TextSingleLine,
4927                                   Post.CreatedAt.ToString(this._cfgCommon.DateTimeFormat),
4928                                   Post.ScreenName + Environment.NewLine + "(RT:" + Post.RetweetedBy + ")",
4929                                   "",
4930                                   mk.ToString(),
4931                                   Post.Source};
4932                 itm = new ImageListViewItem(sitem, this.IconCache, Post.ImageUrl);
4933             }
4934             itm.StateIndex = Post.StateIndex;
4935
4936             bool read = Post.IsRead;
4937             //未読管理していなかったら既読として扱う
4938             if (!_statuses.Tabs[Tab.Text].UnreadManage || !this._cfgCommon.UnreadManage) read = true;
4939             ChangeItemStyleRead(read, itm, Post, null);
4940             if (Tab.Equals(_curTab)) ColorizeList(itm, Index);
4941             return itm;
4942         }
4943
4944         /// <summary>
4945         /// 全てのタブの振り分けルールを反映し直します
4946         /// </summary>
4947         private void ApplyPostFilters()
4948         {
4949             using (ControlTransaction.Cursor(this, Cursors.WaitCursor))
4950             {
4951                 this.PurgeListViewItemCache();
4952                 this._curPost = null;
4953                 this._curItemIndex = -1;
4954                 this._statuses.FilterAll();
4955
4956                 foreach (TabPage tabPage in this.ListTab.TabPages)
4957                 {
4958                     var tab = this._statuses.Tabs[tabPage.Text];
4959
4960                     var listview = (DetailsListView)tabPage.Tag;
4961                     using (ControlTransaction.Update(listview))
4962                     {
4963                         listview.VirtualListSize = tab.AllCount;
4964                     }
4965
4966                     if (this._cfgCommon.TabIconDisp)
4967                     {
4968                         if (tab.UnreadCount > 0)
4969                             tabPage.ImageIndex = 0;
4970                         else
4971                             tabPage.ImageIndex = -1;
4972                     }
4973                 }
4974
4975                 if (!this._cfgCommon.TabIconDisp)
4976                     this.ListTab.Refresh();
4977
4978                 SetMainWindowTitle();
4979                 SetStatusLabelUrl();
4980             }
4981         }
4982
4983         private void MyList_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
4984         {
4985             e.DrawDefault = true;
4986         }
4987
4988         private void MyList_HScrolled(object sender, EventArgs e)
4989         {
4990             DetailsListView listView = (DetailsListView)sender;
4991             listView.Refresh();
4992         }
4993
4994         private void MyList_DrawItem(object sender, DrawListViewItemEventArgs e)
4995         {
4996             if (e.State == 0) return;
4997             e.DrawDefault = false;
4998
4999             SolidBrush brs2 = null;
5000             if (!e.Item.Selected)     //e.ItemStateでうまく判定できない???
5001             {
5002                 if (e.Item.BackColor == _clSelf)
5003                     brs2 = _brsBackColorMine;
5004                 else if (e.Item.BackColor == _clAtSelf)
5005                     brs2 = _brsBackColorAt;
5006                 else if (e.Item.BackColor == _clTarget)
5007                     brs2 = _brsBackColorYou;
5008                 else if (e.Item.BackColor == _clAtTarget)
5009                     brs2 = _brsBackColorAtYou;
5010                 else if (e.Item.BackColor == _clAtFromTarget)
5011                     brs2 = _brsBackColorAtFromTarget;
5012                 else if (e.Item.BackColor == _clAtTo)
5013                     brs2 = _brsBackColorAtTo;
5014                 else
5015                     brs2 = _brsBackColorNone;
5016             }
5017             else
5018             {
5019                 //選択中の行
5020                 if (((Control)sender).Focused)
5021                     brs2 = _brsHighLight;
5022                 else
5023                     brs2 = _brsDeactiveSelection;
5024             }
5025             e.Graphics.FillRectangle(brs2, e.Bounds);
5026             e.DrawFocusRectangle();
5027             this.DrawListViewItemIcon(e);
5028         }
5029
5030         private void MyList_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
5031         {
5032             if (e.ItemState == 0) return;
5033
5034             if (e.ColumnIndex > 0)
5035             {
5036                 //アイコン以外の列
5037                 RectangleF rct = e.Bounds;
5038                 rct.Width = e.Header.Width;
5039                 int fontHeight = e.Item.Font.Height;
5040                 if (_iconCol)
5041                 {
5042                     rct.Y += fontHeight;
5043                     rct.Height -= fontHeight;
5044                 }
5045
5046                 int heightDiff;
5047                 int drawLineCount = Math.Max(1, Math.DivRem((int)rct.Height, fontHeight, out heightDiff));
5048
5049                 //if (heightDiff > fontHeight * 0.7)
5050                 //{
5051                 //    rct.Height += fontHeight;
5052                 //    drawLineCount += 1;
5053                 //}
5054
5055                 //フォントの高さの半分を足してるのは保険。無くてもいいかも。
5056                 if (!_iconCol && drawLineCount <= 1)
5057                 {
5058                     //rct.Inflate(0, heightDiff / -2);
5059                     //rct.Height += fontHeight / 2;
5060                 }
5061                 else if (heightDiff < fontHeight * 0.7)
5062                 {
5063                     //最終行が70%以上欠けていたら、最終行は表示しない
5064                     //rct.Height = (float)((fontHeight * drawLineCount) + (fontHeight / 2));
5065                     rct.Height = (fontHeight * drawLineCount) - 1;
5066                 }
5067                 else
5068                 {
5069                     drawLineCount += 1;
5070                 }
5071
5072                 //if (!_iconCol && drawLineCount > 1)
5073                 //{
5074                 //    rct.Y += fontHeight * 0.2;
5075                 //    if (heightDiff >= fontHeight * 0.8) rct.Height -= fontHeight * 0.2;
5076                 //}
5077
5078                 if (rct.Width > 0)
5079                 {
5080                     Color color = (!e.Item.Selected) ? e.Item.ForeColor :   //選択されていない行
5081                         (((Control)sender).Focused) ? _clHighLight :        //選択中の行
5082                         _clUnread;
5083
5084                     if (_iconCol)
5085                     {
5086                         Rectangle rctB = e.Bounds;
5087                         rctB.Width = e.Header.Width;
5088                         rctB.Height = fontHeight;
5089
5090                         using (Font fnt = new Font(e.Item.Font, FontStyle.Bold))
5091                         {
5092                             TextRenderer.DrawText(e.Graphics,
5093                                                     e.Item.SubItems[2].Text,
5094                                                     e.Item.Font,
5095                                                     Rectangle.Round(rct),
5096                                                     color,
5097                                                     TextFormatFlags.WordBreak |
5098                                                     TextFormatFlags.EndEllipsis |
5099                                                     TextFormatFlags.GlyphOverhangPadding |
5100                                                     TextFormatFlags.NoPrefix);
5101                             TextRenderer.DrawText(e.Graphics,
5102                                                     e.Item.SubItems[4].Text + " / " + e.Item.SubItems[1].Text + " (" + e.Item.SubItems[3].Text + ") " + e.Item.SubItems[5].Text + e.Item.SubItems[6].Text + " [" + e.Item.SubItems[7].Text + "]",
5103                                                     fnt,
5104                                                     rctB,
5105                                                     color,
5106                                                     TextFormatFlags.SingleLine |
5107                                                     TextFormatFlags.EndEllipsis |
5108                                                     TextFormatFlags.GlyphOverhangPadding |
5109                                                     TextFormatFlags.NoPrefix);
5110                         }
5111                     }
5112                     else if (drawLineCount == 1)
5113                     {
5114                         TextRenderer.DrawText(e.Graphics,
5115                                                 e.SubItem.Text,
5116                                                 e.Item.Font,
5117                                                 Rectangle.Round(rct),
5118                                                 color,
5119                                                 TextFormatFlags.SingleLine |
5120                                                 TextFormatFlags.EndEllipsis |
5121                                                 TextFormatFlags.GlyphOverhangPadding |
5122                                                 TextFormatFlags.NoPrefix |
5123                                                 TextFormatFlags.VerticalCenter);
5124                     }
5125                     else
5126                     {
5127                         TextRenderer.DrawText(e.Graphics,
5128                                                 e.SubItem.Text,
5129                                                 e.Item.Font,
5130                                                 Rectangle.Round(rct),
5131                                                 color,
5132                                                 TextFormatFlags.WordBreak |
5133                                                 TextFormatFlags.EndEllipsis |
5134                                                 TextFormatFlags.GlyphOverhangPadding |
5135                                                 TextFormatFlags.NoPrefix);
5136                     }
5137                     //if (e.ColumnIndex == 6) this.DrawListViewItemStateIcon(e, rct);
5138                 }
5139             }
5140         }
5141
5142         private void DrawListViewItemIcon(DrawListViewItemEventArgs e)
5143         {
5144             if (_iconSz == 0) return;
5145
5146             ImageListViewItem item = (ImageListViewItem)e.Item;
5147
5148             //e.Bounds.Leftが常に0を指すから自前で計算
5149             Rectangle itemRect = item.Bounds;
5150             var col0 = e.Item.ListView.Columns[0];
5151             itemRect.Width = col0.Width;
5152
5153             if (col0.DisplayIndex > 0)
5154             {
5155                 foreach (ColumnHeader clm in e.Item.ListView.Columns)
5156                 {
5157                     if (clm.DisplayIndex < col0.DisplayIndex)
5158                         itemRect.X += clm.Width;
5159                 }
5160             }
5161
5162             // ディスプレイの DPI 設定を考慮したアイコンサイズ
5163             var realIconSize = new SizeF(this._iconSz * this.CurrentScaleFactor.Width, this._iconSz * this.CurrentScaleFactor.Height).ToSize();
5164             var realStateSize = new SizeF(16 * this.CurrentScaleFactor.Width, 16 * this.CurrentScaleFactor.Height).ToSize();
5165
5166             Rectangle iconRect;
5167             var img = item.Image;
5168             if (img != null)
5169             {
5170                 iconRect = Rectangle.Intersect(new Rectangle(e.Item.GetBounds(ItemBoundsPortion.Icon).Location, realIconSize), itemRect);
5171                 iconRect.Offset(0, Math.Max(0, (itemRect.Height - realIconSize.Height) / 2));
5172
5173                 if (iconRect.Width > 0)
5174                 {
5175                     e.Graphics.FillRectangle(Brushes.White, iconRect);
5176                     e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
5177                     try
5178                     {
5179                         e.Graphics.DrawImage(img.Image, iconRect);
5180                     }
5181                     catch (ArgumentException)
5182                     {
5183                         item.RefreshImageAsync();
5184                     }
5185                 }
5186             }
5187             else
5188             {
5189                 iconRect = Rectangle.Intersect(new Rectangle(e.Item.GetBounds(ItemBoundsPortion.Icon).Location, new Size(1, 1)), itemRect);
5190                 //iconRect.Offset(0, Math.Max(0, (itemRect.Height - realIconSize.Height) / 2));
5191
5192                 item.GetImageAsync();
5193             }
5194
5195             if (item.StateIndex > -1)
5196             {
5197                 Rectangle stateRect = Rectangle.Intersect(new Rectangle(new Point(iconRect.X + realIconSize.Width + 2, iconRect.Y), realStateSize), itemRect);
5198                 if (stateRect.Width > 0)
5199                 {
5200                     //e.Graphics.FillRectangle(Brushes.White, stateRect);
5201                     //e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.High;
5202                     e.Graphics.DrawImage(this.PostStateImageList.Images[item.StateIndex], stateRect);
5203                 }
5204             }
5205         }
5206
5207         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
5208         {
5209             base.ScaleControl(factor, specified);
5210
5211             ScaleChildControl(this.TabImage, factor);
5212
5213             var tabpages = this.ListTab.TabPages.Cast<TabPage>();
5214             var listviews = tabpages.Select(x => x.Tag).Cast<ListView>();
5215
5216             foreach (var listview in listviews)
5217             {
5218                 ScaleChildControl(listview, factor);
5219             }
5220         }
5221
5222         //private void DrawListViewItemStateIcon(DrawListViewSubItemEventArgs e, RectangleF rct)
5223         //{
5224         //    ImageListViewItem item = (ImageListViewItem)e.Item;
5225         //    if (item.StateImageIndex > -1)
5226         //    {
5227         //        ////e.Bounds.Leftが常に0を指すから自前で計算
5228         //        //Rectangle itemRect = item.Bounds;
5229         //        //itemRect.Width = e.Item.ListView.Columns[4].Width;
5230
5231         //        //foreach (ColumnHeader clm in e.Item.ListView.Columns)
5232         //        //{
5233         //        //    if (clm.DisplayIndex < e.Item.ListView.Columns[4].DisplayIndex)
5234         //        //    {
5235         //        //        itemRect.X += clm.Width;
5236         //        //    }
5237         //        //}
5238
5239         //        //Rectangle iconRect = Rectangle.Intersect(new Rectangle(e.Item.GetBounds(ItemBoundsPortion.Icon).Location, new Size(_iconSz, _iconSz)), itemRect);
5240         //        //iconRect.Offset(0, Math.Max(0, (itemRect.Height - _iconSz) / 2));
5241
5242         //        if (rct.Width > 0)
5243         //        {
5244         //            RectangleF stateRect = RectangleF.Intersect(rct, new RectangleF(rct.Location, new Size(18, 16)));
5245         //            //e.Graphics.FillRectangle(Brushes.White, rct);
5246         //            //e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.High;
5247         //            e.Graphics.DrawImage(this.PostStateImageList.Images(item.StateImageIndex), stateRect);
5248         //        }
5249         //    }
5250         //}
5251
5252         internal void DoTabSearch(string searchWord, bool caseSensitive, bool useRegex, SEARCHTYPE searchType)
5253         {
5254             var tab = this._statuses.Tabs[this._curTab.Text];
5255
5256             if (tab.AllCount == 0)
5257             {
5258                 MessageBox.Show(Properties.Resources.DoTabSearchText2, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Information);
5259                 return;
5260             }
5261
5262             var selectedIndex = this._curList.SelectedIndices.Count != 0 ? this._curList.SelectedIndices[0] : -1;
5263
5264             int startIndex;
5265             switch (searchType)
5266             {
5267                 case SEARCHTYPE.NextSearch: // 次を検索
5268                     if (selectedIndex != -1)
5269                         startIndex = Math.Min(selectedIndex + 1, tab.AllCount - 1);
5270                     else
5271                         startIndex = 0;
5272                     break;
5273                 case SEARCHTYPE.PrevSearch: // 前を検索
5274                     if (selectedIndex != -1)
5275                         startIndex = Math.Max(selectedIndex - 1, 0);
5276                     else
5277                         startIndex = tab.AllCount - 1;
5278                     break;
5279                 case SEARCHTYPE.DialogSearch: // ダイアログからの検索
5280                 default:
5281                     if (selectedIndex != -1)
5282                         startIndex = selectedIndex;
5283                     else
5284                         startIndex = 0;
5285                     break;
5286             }
5287
5288             Func<string, bool> stringComparer;
5289             try
5290             {
5291                 stringComparer = this.CreateSearchComparer(searchWord, useRegex, caseSensitive);
5292             }
5293             catch (ArgumentException)
5294             {
5295                 MessageBox.Show(Properties.Resources.DoTabSearchText1, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Error);
5296                 return;
5297             }
5298
5299             var reverse = searchType == SEARCHTYPE.PrevSearch;
5300             var foundIndex = tab.SearchPostsAll(stringComparer, startIndex, reverse)
5301                 .DefaultIfEmpty(-1).First();
5302
5303             if (foundIndex == -1)
5304             {
5305                 MessageBox.Show(Properties.Resources.DoTabSearchText2, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Information);
5306                 return;
5307             }
5308
5309             this.SelectListItem(this._curList, foundIndex);
5310             this._curList.EnsureVisible(foundIndex);
5311         }
5312
5313         private void MenuItemSubSearch_Click(object sender, EventArgs e)
5314         {
5315             // 検索メニュー
5316             this.ShowSearchDialog();
5317         }
5318
5319         private void MenuItemSearchNext_Click(object sender, EventArgs e)
5320         {
5321             var previousSearch = this.SearchDialog.ResultOptions;
5322             if (previousSearch == null || previousSearch.Type != SearchWordDialog.SearchType.Timeline)
5323             {
5324                 this.SearchDialog.Reset();
5325                 this.ShowSearchDialog();
5326                 return;
5327             }
5328
5329             // 次を検索
5330             this.DoTabSearch(
5331                 previousSearch.Query,
5332                 previousSearch.CaseSensitive,
5333                 previousSearch.UseRegex,
5334                 SEARCHTYPE.NextSearch);
5335         }
5336
5337         private void MenuItemSearchPrev_Click(object sender, EventArgs e)
5338         {
5339             var previousSearch = this.SearchDialog.ResultOptions;
5340             if (previousSearch == null || previousSearch.Type != SearchWordDialog.SearchType.Timeline)
5341             {
5342                 this.SearchDialog.Reset();
5343                 this.ShowSearchDialog();
5344                 return;
5345             }
5346
5347             // 前を検索
5348             this.DoTabSearch(
5349                 previousSearch.Query,
5350                 previousSearch.CaseSensitive,
5351                 previousSearch.UseRegex,
5352                 SEARCHTYPE.PrevSearch);
5353         }
5354
5355         /// <summary>
5356         /// 検索ダイアログを表示し、検索を実行します
5357         /// </summary>
5358         private void ShowSearchDialog()
5359         {
5360             if (this.SearchDialog.ShowDialog(this) != DialogResult.OK)
5361             {
5362                 this.TopMost = this._cfgCommon.AlwaysTop;
5363                 return;
5364             }
5365             this.TopMost = this._cfgCommon.AlwaysTop;
5366
5367             var searchOptions = this.SearchDialog.ResultOptions;
5368             if (searchOptions.Type == SearchWordDialog.SearchType.Timeline)
5369             {
5370                 if (searchOptions.NewTab)
5371                 {
5372                     var tabName = Properties.Resources.SearchResults_TabName;
5373
5374                     try
5375                     {
5376                         tabName = this._statuses.MakeTabName(tabName);
5377                     }
5378                     catch (TabException ex)
5379                     {
5380                         MessageBox.Show(this, ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
5381                     }
5382
5383                     var resultTab = new LocalSearchTabModel(tabName);
5384                     this.AddNewTab(resultTab, startup: false);
5385                     this._statuses.AddTab(resultTab);
5386
5387                     var targetTab = this._statuses.Tabs[this._curTab.Text];
5388
5389                     Func<string, bool> stringComparer;
5390                     try
5391                     {
5392                         stringComparer = this.CreateSearchComparer(searchOptions.Query, searchOptions.UseRegex, searchOptions.CaseSensitive);
5393                     }
5394                     catch (ArgumentException)
5395                     {
5396                         MessageBox.Show(Properties.Resources.DoTabSearchText1, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Error);
5397                         return;
5398                     }
5399
5400                     var foundIndices = targetTab.SearchPostsAll(stringComparer).ToArray();
5401                     if (foundIndices.Length == 0)
5402                     {
5403                         MessageBox.Show(Properties.Resources.DoTabSearchText2, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Information);
5404                         return;
5405                     }
5406
5407                     var foundPosts = foundIndices.Select(x => targetTab[x]);
5408                     foreach (var post in foundPosts)
5409                     {
5410                         resultTab.AddPostQueue(post);
5411                     }
5412
5413                     this._statuses.DistributePosts();
5414                     this.RefreshTimeline();
5415
5416                     var tabPage = this.ListTab.TabPages.Cast<TabPage>()
5417                         .First(x => x.Text == tabName);
5418
5419                     this.ListTab.SelectedTab = tabPage;
5420                 }
5421                 else
5422                 {
5423                     this.DoTabSearch(
5424                         searchOptions.Query,
5425                         searchOptions.CaseSensitive,
5426                         searchOptions.UseRegex,
5427                         SEARCHTYPE.DialogSearch);
5428                 }
5429             }
5430             else if (searchOptions.Type == SearchWordDialog.SearchType.Public)
5431             {
5432                 this.AddNewTabForSearch(searchOptions.Query);
5433             }
5434         }
5435
5436         /// <summary>発言検索に使用するメソッドを生成します</summary>
5437         /// <exception cref="ArgumentException">
5438         /// <paramref name="useRegex"/> が true かつ、<paramref name="query"/> が不正な正規表現な場合
5439         /// </exception>
5440         private Func<string, bool> CreateSearchComparer(string query, bool useRegex, bool caseSensitive)
5441         {
5442             if (useRegex)
5443             {
5444                 var regexOption = caseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase;
5445                 var regex = new Regex(query, regexOption);
5446
5447                 return x => regex.IsMatch(x);
5448             }
5449             else
5450             {
5451                 var comparisonType = caseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
5452
5453                 return x => x.IndexOf(query, comparisonType) != -1;
5454             }
5455         }
5456
5457         private void AboutMenuItem_Click(object sender, EventArgs e)
5458         {
5459             using (TweenAboutBox about = new TweenAboutBox())
5460             {
5461                 about.ShowDialog(this);
5462             }
5463             this.TopMost = this._cfgCommon.AlwaysTop;
5464         }
5465
5466         private void JumpUnreadMenuItem_Click(object sender, EventArgs e)
5467         {
5468             int bgnIdx = ListTab.TabPages.IndexOf(_curTab);
5469             int idx = -1;
5470             DetailsListView lst = null;
5471
5472             if (ImageSelector.Enabled)
5473                 return;
5474
5475             //現在タブから最終タブまで探索
5476             for (int i = bgnIdx; i < ListTab.TabPages.Count; i++)
5477             {
5478                 //未読Index取得
5479                 idx = _statuses.Tabs[ListTab.TabPages[i].Text].NextUnreadIndex;
5480                 if (idx > -1)
5481                 {
5482                     ListTab.SelectedIndex = i;
5483                     lst = (DetailsListView)ListTab.TabPages[i].Tag;
5484                     //_curTab = ListTab.TabPages[i];
5485                     break;
5486                 }
5487             }
5488
5489             //未読みつからず&現在タブが先頭ではなかったら、先頭タブから現在タブの手前まで探索
5490             if (idx == -1 && bgnIdx > 0)
5491             {
5492                 for (int i = 0; i < bgnIdx; i++)
5493                 {
5494                     idx = _statuses.Tabs[ListTab.TabPages[i].Text].NextUnreadIndex;
5495                     if (idx > -1)
5496                     {
5497                         ListTab.SelectedIndex = i;
5498                         lst = (DetailsListView)ListTab.TabPages[i].Tag;
5499                         //_curTab = ListTab.TabPages[i];
5500                         break;
5501                     }
5502                 }
5503             }
5504
5505             //全部調べたが未読見つからず→先頭タブの最新発言へ
5506             if (idx == -1)
5507             {
5508                 ListTab.SelectedIndex = 0;
5509                 lst = (DetailsListView)ListTab.TabPages[0].Tag;
5510                 //_curTab = ListTab.TabPages[0];
5511                 if (_statuses.SortOrder == SortOrder.Ascending)
5512                     idx = lst.VirtualListSize - 1;
5513                 else
5514                     idx = 0;
5515             }
5516
5517             if (lst.VirtualListSize > 0 && idx > -1 && lst.VirtualListSize > idx)
5518             {
5519                 SelectListItem(lst, idx);
5520                 if (_statuses.SortMode == ComparerMode.Id)
5521                 {
5522                     if (_statuses.SortOrder == SortOrder.Ascending && lst.Items[idx].Position.Y > lst.ClientSize.Height - _iconSz - 10 ||
5523                        _statuses.SortOrder == SortOrder.Descending && lst.Items[idx].Position.Y < _iconSz + 10)
5524                     {
5525                         MoveTop();
5526                     }
5527                     else
5528                     {
5529                         lst.EnsureVisible(idx);
5530                     }
5531                 }
5532                 else
5533                 {
5534                     lst.EnsureVisible(idx);
5535                 }
5536             }
5537             lst.Focus();
5538         }
5539
5540         private async void StatusOpenMenuItem_Click(object sender, EventArgs e)
5541         {
5542             if (_curList.SelectedIndices.Count > 0 && _statuses.Tabs[_curTab.Text].TabType != MyCommon.TabUsageType.DirectMessage)
5543             {
5544                 var post = _statuses.Tabs[_curTab.Text][_curList.SelectedIndices[0]];
5545                 await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(post));
5546             }
5547         }
5548
5549         private async void FavorareMenuItem_Click(object sender, EventArgs e)
5550         {
5551             if (_curList.SelectedIndices.Count > 0)
5552             {
5553                 PostClass post = _statuses.Tabs[_curTab.Text][_curList.SelectedIndices[0]];
5554                 await this.OpenUriInBrowserAsync(Properties.Resources.FavstarUrl + "users/" + post.ScreenName + "/recent");
5555             }
5556         }
5557
5558         private async void VerUpMenuItem_Click(object sender, EventArgs e)
5559         {
5560             await this.CheckNewVersion(false);
5561         }
5562
5563         private void RunTweenUp()
5564         {
5565             ProcessStartInfo pinfo = new ProcessStartInfo();
5566             pinfo.UseShellExecute = true;
5567             pinfo.WorkingDirectory = MyCommon.settingPath;
5568             pinfo.FileName = Path.Combine(MyCommon.settingPath, "TweenUp3.exe");
5569             pinfo.Arguments = "\"" + Application.StartupPath + "\"";
5570             try
5571             {
5572                 Process.Start(pinfo);
5573             }
5574             catch (Exception)
5575             {
5576                 MessageBox.Show("Failed to execute TweenUp3.exe.");
5577             }
5578         }
5579
5580         public class VersionInfo
5581         {
5582             public Version Version { get; set; }
5583             public Uri DownloadUri { get; set; }
5584             public string ReleaseNote { get; set; }
5585         }
5586
5587         /// <summary>
5588         /// OpenTween の最新バージョンの情報を取得します
5589         /// </summary>
5590         public async Task<VersionInfo> GetVersionInfoAsync()
5591         {
5592             var versionInfoUrl = new Uri(ApplicationSettings.VersionInfoUrl + "?" +
5593                 DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount);
5594
5595             var responseText = await Networking.Http.GetStringAsync(versionInfoUrl)
5596                 .ConfigureAwait(false);
5597
5598             // 改行2つで前後パートを分割(前半がバージョン番号など、後半が詳細テキスト)
5599             var msgPart = responseText.Split(new[] { "\n\n", "\r\n\r\n" }, 2, StringSplitOptions.None);
5600
5601             var msgHeader = msgPart[0].Split(new[] { "\n", "\r\n" }, StringSplitOptions.None);
5602             var msgBody = msgPart.Length == 2 ? msgPart[1] : "";
5603
5604             msgBody = Regex.Replace(msgBody, "(?<!\r)\n", "\r\n"); // LF -> CRLF
5605
5606             return new VersionInfo
5607             {
5608                 Version = Version.Parse(msgHeader[0]),
5609                 DownloadUri = new Uri(msgHeader[1]),
5610                 ReleaseNote = msgBody,
5611             };
5612         }
5613
5614         private async Task CheckNewVersion(bool startup = false)
5615         {
5616             if (ApplicationSettings.VersionInfoUrl == null)
5617                 return; // 更新チェック無効化
5618
5619             try
5620             {
5621                 var versionInfo = await this.GetVersionInfoAsync();
5622
5623                 if (versionInfo.Version <= Version.Parse(MyCommon.FileVersion))
5624                 {
5625                     // 更新不要
5626                     if (!startup)
5627                     {
5628                         var msgtext = string.Format(Properties.Resources.CheckNewVersionText7,
5629                             MyCommon.GetReadableVersion(), MyCommon.GetReadableVersion(versionInfo.Version));
5630                         msgtext = MyCommon.ReplaceAppName(msgtext);
5631
5632                         MessageBox.Show(msgtext,
5633                             MyCommon.ReplaceAppName(Properties.Resources.CheckNewVersionText2),
5634                             MessageBoxButtons.OK, MessageBoxIcon.Information);
5635                     }
5636                     return;
5637                 }
5638
5639                 using (var dialog = new UpdateDialog())
5640                 {
5641                     dialog.SummaryText = string.Format(Properties.Resources.CheckNewVersionText3,
5642                         MyCommon.GetReadableVersion(versionInfo.Version));
5643                     dialog.DetailsText = versionInfo.ReleaseNote;
5644
5645                     if (dialog.ShowDialog(this) == DialogResult.Yes)
5646                     {
5647                         await this.OpenUriInBrowserAsync(versionInfo.DownloadUri.OriginalString);
5648                     }
5649                 }
5650             }
5651             catch (Exception)
5652             {
5653                 this.StatusLabel.Text = Properties.Resources.CheckNewVersionText9;
5654                 if (!startup)
5655                 {
5656                     MessageBox.Show(Properties.Resources.CheckNewVersionText10,
5657                         MyCommon.ReplaceAppName(Properties.Resources.CheckNewVersionText2),
5658                         MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
5659                 }
5660             }
5661         }
5662
5663         private async Task Colorize()
5664         {
5665             _colorize = false;
5666             await this.DispSelectedPost();
5667             //件数関連の場合、タイトル即時書き換え
5668             if (this._cfgCommon.DispLatestPost != MyCommon.DispTitleEnum.None &&
5669                this._cfgCommon.DispLatestPost != MyCommon.DispTitleEnum.Post &&
5670                this._cfgCommon.DispLatestPost != MyCommon.DispTitleEnum.Ver &&
5671                this._cfgCommon.DispLatestPost != MyCommon.DispTitleEnum.OwnStatus)
5672             {
5673                 SetMainWindowTitle();
5674             }
5675             if (!StatusLabelUrl.Text.StartsWith("http")) SetStatusLabelUrl();
5676             foreach (TabPage tb in ListTab.TabPages)
5677             {
5678                 if (_statuses.Tabs[tb.Text].UnreadCount == 0)
5679                 {
5680                     if (this._cfgCommon.TabIconDisp)
5681                     {
5682                         if (tb.ImageIndex == 0) tb.ImageIndex = -1;
5683                     }
5684                 }
5685             }
5686             if (!this._cfgCommon.TabIconDisp) ListTab.Refresh();
5687         }
5688
5689         public string createDetailHtml(string orgdata)
5690         {
5691             if (this._cfgLocal.UseTwemoji)
5692                 orgdata = EmojiFormatter.ReplaceEmojiToImg(orgdata);
5693
5694             return detailHtmlFormatHeader + orgdata + detailHtmlFormatFooter;
5695         }
5696
5697         private Task DispSelectedPost()
5698         {
5699             return this.DispSelectedPost(false);
5700         }
5701
5702         private PostClass displayPost = new PostClass();
5703
5704         /// <summary>
5705         /// サムネイル表示に使用する CancellationToken の生成元
5706         /// </summary>
5707         private CancellationTokenSource thumbnailTokenSource = null;
5708
5709         private async Task DispSelectedPost(bool forceupdate)
5710         {
5711             if (_curList.SelectedIndices.Count == 0 || _curPost == null)
5712                 return;
5713
5714             var oldDisplayPost = this.displayPost;
5715             this.displayPost = this._curPost;
5716
5717             if (!forceupdate && this._curPost.Equals(oldDisplayPost))
5718                 return;
5719
5720             var loadTasks = new List<Task>
5721             {
5722                 this.tweetDetailsView.ShowPostDetails(this._curPost),
5723             };
5724
5725             this.SplitContainer3.Panel2Collapsed = true;
5726
5727             if (this._cfgCommon.PreviewEnable)
5728             {
5729                 var oldTokenSource = Interlocked.Exchange(ref this.thumbnailTokenSource, new CancellationTokenSource());
5730                 oldTokenSource?.Cancel();
5731
5732                 var token = this.thumbnailTokenSource.Token;
5733                 loadTasks.Add(this.tweetThumbnail1.ShowThumbnailAsync(_curPost, token));
5734             }
5735
5736             try
5737             {
5738                 await Task.WhenAll(loadTasks);
5739             }
5740             catch (OperationCanceledException) { }
5741         }
5742
5743         private async void MatomeMenuItem_Click(object sender, EventArgs e)
5744         {
5745             await this.OpenApplicationWebsite();
5746         }
5747
5748         private async Task OpenApplicationWebsite()
5749         {
5750             await this.OpenUriInBrowserAsync(ApplicationSettings.WebsiteUrl);
5751         }
5752
5753         private async void ShortcutKeyListMenuItem_Click(object sender, EventArgs e)
5754         {
5755             await this.OpenUriInBrowserAsync(ApplicationSettings.ShortcutKeyUrl);
5756         }
5757
5758         private async void ListTab_KeyDown(object sender, KeyEventArgs e)
5759         {
5760             if (ListTab.SelectedTab != null)
5761             {
5762                 if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.PublicSearch)
5763                 {
5764                     Control pnl = ListTab.SelectedTab.Controls["panelSearch"];
5765                     if (pnl.Controls["comboSearch"].Focused ||
5766                         pnl.Controls["comboLang"].Focused ||
5767                         pnl.Controls["buttonSearch"].Focused) return;
5768                 }
5769
5770                 if (e.Control || e.Shift || e.Alt)
5771                     this._anchorFlag = false;
5772
5773                 Task asyncTask;
5774                 if (CommonKeyDown(e.KeyData, FocusedControl.ListTab, out asyncTask))
5775                 {
5776                     e.Handled = true;
5777                     e.SuppressKeyPress = true;
5778                 }
5779
5780                 if (asyncTask != null)
5781                     await asyncTask;
5782             }
5783         }
5784
5785         private ShortcutCommand[] shortcutCommands = new ShortcutCommand[0];
5786
5787         private void InitializeShortcuts()
5788         {
5789             this.shortcutCommands = new[]
5790             {
5791                 // リストのカーソル移動関係(上下キー、PageUp/Downに該当)
5792                 ShortcutCommand.Create(Keys.J, Keys.Control | Keys.J, Keys.Shift | Keys.J, Keys.Control | Keys.Shift | Keys.J)
5793                     .FocusedOn(FocusedControl.ListTab)
5794                     .Do(() => SendKeys.Send("{DOWN}")),
5795
5796                 ShortcutCommand.Create(Keys.K, Keys.Control | Keys.K, Keys.Shift | Keys.K, Keys.Control | Keys.Shift | Keys.K)
5797                     .FocusedOn(FocusedControl.ListTab)
5798                     .Do(() => SendKeys.Send("{UP}")),
5799
5800                 ShortcutCommand.Create(Keys.F, Keys.Shift | Keys.F)
5801                     .FocusedOn(FocusedControl.ListTab)
5802                     .Do(() => SendKeys.Send("{PGDN}")),
5803
5804                 ShortcutCommand.Create(Keys.B, Keys.Shift | Keys.B)
5805                     .FocusedOn(FocusedControl.ListTab)
5806                     .Do(() => SendKeys.Send("{PGUP}")),
5807
5808                 ShortcutCommand.Create(Keys.F1)
5809                     .Do(() => this.OpenApplicationWebsite()),
5810
5811                 ShortcutCommand.Create(Keys.F3)
5812                     .Do(() => this.MenuItemSearchNext_Click(null, null)),
5813
5814                 ShortcutCommand.Create(Keys.F5)
5815                     .Do(() => this.DoRefresh()),
5816
5817                 ShortcutCommand.Create(Keys.F6)
5818                     .Do(() => this.GetReplyAsync()),
5819
5820                 ShortcutCommand.Create(Keys.F7)
5821                     .Do(() => this.GetDirectMessagesAsync()),
5822
5823                 ShortcutCommand.Create(Keys.Space, Keys.ProcessKey)
5824                     .NotFocusedOn(FocusedControl.StatusText)
5825                     .Do(() => { this._anchorFlag = false; this.JumpUnreadMenuItem_Click(null, null); }),
5826
5827                 ShortcutCommand.Create(Keys.G)
5828                     .NotFocusedOn(FocusedControl.StatusText)
5829                     .Do(() => { this._anchorFlag = false; this.ShowRelatedStatusesMenuItem_Click(null, null); }),
5830
5831                 ShortcutCommand.Create(Keys.Right, Keys.N)
5832                     .FocusedOn(FocusedControl.ListTab)
5833                     .Do(() => this.GoRelPost(forward: true)),
5834
5835                 ShortcutCommand.Create(Keys.Left, Keys.P)
5836                     .FocusedOn(FocusedControl.ListTab)
5837                     .Do(() => this.GoRelPost(forward: false)),
5838
5839                 ShortcutCommand.Create(Keys.OemPeriod)
5840                     .FocusedOn(FocusedControl.ListTab)
5841                     .Do(() => this.GoAnchor()),
5842
5843                 ShortcutCommand.Create(Keys.I)
5844                     .FocusedOn(FocusedControl.ListTab)
5845                     .OnlyWhen(() => this.StatusText.Enabled)
5846                     .Do(() => this.StatusText.Focus()),
5847
5848                 ShortcutCommand.Create(Keys.Enter)
5849                     .FocusedOn(FocusedControl.ListTab)
5850                     .Do(() => this.MakeReplyOrDirectStatus()),
5851
5852                 ShortcutCommand.Create(Keys.R)
5853                     .FocusedOn(FocusedControl.ListTab)
5854                     .Do(() => this.DoRefresh()),
5855
5856                 ShortcutCommand.Create(Keys.L)
5857                     .FocusedOn(FocusedControl.ListTab)
5858                     .Do(() => { this._anchorFlag = false; this.GoPost(forward: true); }),
5859
5860                 ShortcutCommand.Create(Keys.H)
5861                     .FocusedOn(FocusedControl.ListTab)
5862                     .Do(() => { this._anchorFlag = false; this.GoPost(forward: false); }),
5863
5864                 ShortcutCommand.Create(Keys.Z, Keys.Oemcomma)
5865                     .FocusedOn(FocusedControl.ListTab)
5866                     .Do(() => { this._anchorFlag = false; this.MoveTop(); }),
5867
5868                 ShortcutCommand.Create(Keys.S)
5869                     .FocusedOn(FocusedControl.ListTab)
5870                     .Do(() => { this._anchorFlag = false; this.GoNextTab(forward: true); }),
5871
5872                 ShortcutCommand.Create(Keys.A)
5873                     .FocusedOn(FocusedControl.ListTab)
5874                     .Do(() => { this._anchorFlag = false; this.GoNextTab(forward: false); }),
5875
5876                 // ] in_reply_to参照元へ戻る
5877                 ShortcutCommand.Create(Keys.Oem4)
5878                     .FocusedOn(FocusedControl.ListTab)
5879                     .Do(() => { this._anchorFlag = false; return this.GoInReplyToPostTree(); }),
5880
5881                 // [ in_reply_toへジャンプ
5882                 ShortcutCommand.Create(Keys.Oem6)
5883                     .FocusedOn(FocusedControl.ListTab)
5884                     .Do(() => { this._anchorFlag = false; this.GoBackInReplyToPostTree(); }),
5885
5886                 ShortcutCommand.Create(Keys.Escape)
5887                     .FocusedOn(FocusedControl.ListTab)
5888                     .Do(() => {
5889                         this._anchorFlag = false;
5890                         if (ListTab.SelectedTab != null)
5891                         {
5892                             var tabtype = _statuses.Tabs[ListTab.SelectedTab.Text].TabType;
5893                             if (tabtype == MyCommon.TabUsageType.Related || tabtype == MyCommon.TabUsageType.UserTimeline || tabtype == MyCommon.TabUsageType.PublicSearch || tabtype == MyCommon.TabUsageType.SearchResults)
5894                             {
5895                                 var relTp = ListTab.SelectedTab;
5896                                 RemoveSpecifiedTab(relTp.Text, false);
5897                                 SaveConfigsTabs();
5898                             }
5899                         }
5900                     }),
5901
5902                 // 上下キー, PageUp/Downキー, Home/Endキー は既定の動作を残しつつアンカー初期化
5903                 ShortcutCommand.Create(Keys.Up, Keys.Down, Keys.PageUp, Keys.PageDown, Keys.Home, Keys.End)
5904                     .FocusedOn(FocusedControl.ListTab)
5905                     .Do(() => this._anchorFlag = false, preventDefault: false),
5906
5907                 // PreviewKeyDownEventArgs.IsInputKey を true にしてスクロールを発生させる
5908                 ShortcutCommand.Create(Keys.Up, Keys.Down)
5909                     .FocusedOn(FocusedControl.PostBrowser)
5910                     .Do(() => { }),
5911
5912                 ShortcutCommand.Create(Keys.Control | Keys.R)
5913                     .Do(() => this.MakeReplyOrDirectStatus(isAuto: false, isReply: true)),
5914
5915                 ShortcutCommand.Create(Keys.Control | Keys.D)
5916                     .Do(() => this.doStatusDelete()),
5917
5918                 ShortcutCommand.Create(Keys.Control | Keys.M)
5919                     .Do(() => this.MakeReplyOrDirectStatus(isAuto: false, isReply: false)),
5920
5921                 ShortcutCommand.Create(Keys.Control | Keys.S)
5922                     .Do(() => this.FavoriteChange(FavAdd: true)),
5923
5924                 ShortcutCommand.Create(Keys.Control | Keys.I)
5925                     .Do(() => this.doRepliedStatusOpen()),
5926
5927                 ShortcutCommand.Create(Keys.Control | Keys.Q)
5928                     .Do(() => this.doQuoteOfficial()),
5929
5930                 ShortcutCommand.Create(Keys.Control | Keys.B)
5931                     .Do(() => this.ReadedStripMenuItem_Click(null, null)),
5932
5933                 ShortcutCommand.Create(Keys.Control | Keys.T)
5934                     .Do(() => this.HashManageMenuItem_Click(null, null)),
5935
5936                 ShortcutCommand.Create(Keys.Control | Keys.L)
5937                     .Do(() => this.UrlConvertAutoToolStripMenuItem_Click(null, null)),
5938
5939                 ShortcutCommand.Create(Keys.Control | Keys.Y)
5940                     .NotFocusedOn(FocusedControl.PostBrowser)
5941                     .Do(() => this.MultiLineMenuItem_Click(null, null)),
5942
5943                 ShortcutCommand.Create(Keys.Control | Keys.F)
5944                     .Do(() => this.MenuItemSubSearch_Click(null, null)),
5945
5946                 ShortcutCommand.Create(Keys.Control | Keys.U)
5947                     .Do(() => this.ShowUserTimeline()),
5948
5949                 ShortcutCommand.Create(Keys.Control | Keys.H)
5950                     .Do(() => this.MoveToHomeToolStripMenuItem_Click(null, null)),
5951
5952                 ShortcutCommand.Create(Keys.Control | Keys.G)
5953                     .Do(() => this.MoveToFavToolStripMenuItem_Click(null, null)),
5954
5955                 ShortcutCommand.Create(Keys.Control | Keys.O)
5956                     .Do(() => this.StatusOpenMenuItem_Click(null, null)),
5957
5958                 ShortcutCommand.Create(Keys.Control | Keys.E)
5959                     .Do(() => this.OpenURLMenuItem_Click(null, null)),
5960
5961                 ShortcutCommand.Create(Keys.Control | Keys.Home, Keys.Control | Keys.End)
5962                     .FocusedOn(FocusedControl.ListTab)
5963                     .Do(() => this._colorize = true, preventDefault: false),
5964
5965                 ShortcutCommand.Create(Keys.Control | Keys.N)
5966                     .FocusedOn(FocusedControl.ListTab)
5967                     .Do(() => this.GoNextTab(forward: true)),
5968
5969                 ShortcutCommand.Create(Keys.Control | Keys.P)
5970                     .FocusedOn(FocusedControl.ListTab)
5971                     .Do(() => this.GoNextTab(forward: false)),
5972
5973                 ShortcutCommand.Create(Keys.Control | Keys.C, Keys.Control | Keys.Insert)
5974                     .FocusedOn(FocusedControl.ListTab)
5975                     .Do(() => this.CopyStot()),
5976
5977                 // タブダイレクト選択(Ctrl+1~8,Ctrl+9)
5978                 ShortcutCommand.Create(Keys.Control | Keys.D1)
5979                     .FocusedOn(FocusedControl.ListTab)
5980                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 1)
5981                     .Do(() => this.ListTab.SelectedIndex = 0),
5982
5983                 ShortcutCommand.Create(Keys.Control | Keys.D2)
5984                     .FocusedOn(FocusedControl.ListTab)
5985                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 2)
5986                     .Do(() => this.ListTab.SelectedIndex = 1),
5987
5988                 ShortcutCommand.Create(Keys.Control | Keys.D3)
5989                     .FocusedOn(FocusedControl.ListTab)
5990                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 3)
5991                     .Do(() => this.ListTab.SelectedIndex = 2),
5992
5993                 ShortcutCommand.Create(Keys.Control | Keys.D4)
5994                     .FocusedOn(FocusedControl.ListTab)
5995                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 4)
5996                     .Do(() => this.ListTab.SelectedIndex = 3),
5997
5998                 ShortcutCommand.Create(Keys.Control | Keys.D5)
5999                     .FocusedOn(FocusedControl.ListTab)
6000                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 5)
6001                     .Do(() => this.ListTab.SelectedIndex = 4),
6002
6003                 ShortcutCommand.Create(Keys.Control | Keys.D6)
6004                     .FocusedOn(FocusedControl.ListTab)
6005                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 6)
6006                     .Do(() => this.ListTab.SelectedIndex = 5),
6007
6008                 ShortcutCommand.Create(Keys.Control | Keys.D7)
6009                     .FocusedOn(FocusedControl.ListTab)
6010                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 7)
6011                     .Do(() => this.ListTab.SelectedIndex = 6),
6012
6013                 ShortcutCommand.Create(Keys.Control | Keys.D8)
6014                     .FocusedOn(FocusedControl.ListTab)
6015                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 8)
6016                     .Do(() => this.ListTab.SelectedIndex = 7),
6017
6018                 ShortcutCommand.Create(Keys.Control | Keys.D9)
6019                     .FocusedOn(FocusedControl.ListTab)
6020                     .Do(() => this.ListTab.SelectedIndex = this.ListTab.TabPages.Count - 1),
6021
6022                 ShortcutCommand.Create(Keys.Control | Keys.A)
6023                     .FocusedOn(FocusedControl.StatusText)
6024                     .Do(() => this.StatusText.SelectAll()),
6025
6026                 ShortcutCommand.Create(Keys.Control | Keys.V)
6027                     .FocusedOn(FocusedControl.StatusText)
6028                     .Do(() => this.ProcClipboardFromStatusTextWhenCtrlPlusV()),
6029
6030                 ShortcutCommand.Create(Keys.Control | Keys.Up)
6031                     .FocusedOn(FocusedControl.StatusText)
6032                     .Do(() => {
6033                         if (!string.IsNullOrWhiteSpace(StatusText.Text))
6034                         {
6035                             var inReplyToStatusId = this.inReplyTo?.Item1;
6036                             var inReplyToScreenName = this.inReplyTo?.Item2;
6037                             _history[_hisIdx] = new PostingStatus(StatusText.Text, inReplyToStatusId, inReplyToScreenName);
6038                         }
6039                         _hisIdx -= 1;
6040                         if (_hisIdx < 0) _hisIdx = 0;
6041
6042                         var historyItem = this._history[this._hisIdx];
6043                         StatusText.Text = historyItem.status;
6044                         StatusText.SelectionStart = StatusText.Text.Length;
6045                         if (historyItem.inReplyToId != null)
6046                             this.inReplyTo = Tuple.Create(historyItem.inReplyToId.Value, historyItem.inReplyToName);
6047                         else
6048                             this.inReplyTo = null;
6049                     }),
6050
6051                 ShortcutCommand.Create(Keys.Control | Keys.Down)
6052                     .FocusedOn(FocusedControl.StatusText)
6053                     .Do(() => {
6054                         if (!string.IsNullOrWhiteSpace(StatusText.Text))
6055                         {
6056                             var inReplyToStatusId = this.inReplyTo?.Item1;
6057                             var inReplyToScreenName = this.inReplyTo?.Item2;
6058                             _history[_hisIdx] = new PostingStatus(StatusText.Text, inReplyToStatusId, inReplyToScreenName);
6059                         }
6060                         _hisIdx += 1;
6061                         if (_hisIdx > _history.Count - 1) _hisIdx = _history.Count - 1;
6062
6063                         var historyItem = this._history[this._hisIdx];
6064                         StatusText.Text = historyItem.status;
6065                         StatusText.SelectionStart = StatusText.Text.Length;
6066                         if (historyItem.inReplyToId != null)
6067                             this.inReplyTo = Tuple.Create(historyItem.inReplyToId.Value, historyItem.inReplyToName);
6068                         else
6069                             this.inReplyTo = null;
6070                     }),
6071
6072                 ShortcutCommand.Create(Keys.Control | Keys.PageUp, Keys.Control | Keys.P)
6073                     .FocusedOn(FocusedControl.StatusText)
6074                     .Do(() => {
6075                         if (ListTab.SelectedIndex == 0)
6076                         {
6077                             ListTab.SelectedIndex = ListTab.TabCount - 1;
6078                         }
6079                         else
6080                         {
6081                             ListTab.SelectedIndex -= 1;
6082                         }
6083                         StatusText.Focus();
6084                     }),
6085
6086                 ShortcutCommand.Create(Keys.Control | Keys.PageDown, Keys.Control | Keys.N)
6087                     .FocusedOn(FocusedControl.StatusText)
6088                     .Do(() => {
6089                         if (ListTab.SelectedIndex == ListTab.TabCount - 1)
6090                         {
6091                             ListTab.SelectedIndex = 0;
6092                         }
6093                         else
6094                         {
6095                             ListTab.SelectedIndex += 1;
6096                         }
6097                         StatusText.Focus();
6098                     }),
6099
6100                 ShortcutCommand.Create(Keys.Control | Keys.Y)
6101                     .FocusedOn(FocusedControl.PostBrowser)
6102                     .Do(() => {
6103                         MultiLineMenuItem.Checked = !MultiLineMenuItem.Checked;
6104                         MultiLineMenuItem_Click(null, null);
6105                     }),
6106
6107                 ShortcutCommand.Create(Keys.Shift | Keys.F3)
6108                     .Do(() => this.MenuItemSearchPrev_Click(null, null)),
6109
6110                 ShortcutCommand.Create(Keys.Shift | Keys.F5)
6111                     .Do(() => this.DoRefreshMore()),
6112
6113                 ShortcutCommand.Create(Keys.Shift | Keys.F6)
6114                     .Do(() => this.GetReplyAsync(loadMore: true)),
6115
6116                 ShortcutCommand.Create(Keys.Shift | Keys.F7)
6117                     .Do(() => this.GetDirectMessagesAsync(loadMore: true)),
6118
6119                 ShortcutCommand.Create(Keys.Shift | Keys.R)
6120                     .NotFocusedOn(FocusedControl.StatusText)
6121                     .Do(() => this.DoRefreshMore()),
6122
6123                 ShortcutCommand.Create(Keys.Shift | Keys.H)
6124                     .FocusedOn(FocusedControl.ListTab)
6125                     .Do(() => this.GoTopEnd(GoTop: true)),
6126
6127                 ShortcutCommand.Create(Keys.Shift | Keys.L)
6128                     .FocusedOn(FocusedControl.ListTab)
6129                     .Do(() => this.GoTopEnd(GoTop: false)),
6130
6131                 ShortcutCommand.Create(Keys.Shift | Keys.M)
6132                     .FocusedOn(FocusedControl.ListTab)
6133                     .Do(() => this.GoMiddle()),
6134
6135                 ShortcutCommand.Create(Keys.Shift | Keys.G)
6136                     .FocusedOn(FocusedControl.ListTab)
6137                     .Do(() => this.GoLast()),
6138
6139                 ShortcutCommand.Create(Keys.Shift | Keys.Z)
6140                     .FocusedOn(FocusedControl.ListTab)
6141                     .Do(() => this.MoveMiddle()),
6142
6143                 ShortcutCommand.Create(Keys.Shift | Keys.Oem4)
6144                     .FocusedOn(FocusedControl.ListTab)
6145                     .Do(() => this.GoBackInReplyToPostTree(parallel: true, isForward: false)),
6146
6147                 ShortcutCommand.Create(Keys.Shift | Keys.Oem6)
6148                     .FocusedOn(FocusedControl.ListTab)
6149                     .Do(() => this.GoBackInReplyToPostTree(parallel: true, isForward: true)),
6150
6151                 // お気に入り前後ジャンプ(SHIFT+N←/P→)
6152                 ShortcutCommand.Create(Keys.Shift | Keys.Right, Keys.Shift | Keys.N)
6153                     .FocusedOn(FocusedControl.ListTab)
6154                     .Do(() => this.GoFav(forward: true)),
6155
6156                 // お気に入り前後ジャンプ(SHIFT+N←/P→)
6157                 ShortcutCommand.Create(Keys.Shift | Keys.Left, Keys.Shift | Keys.P)
6158                     .FocusedOn(FocusedControl.ListTab)
6159                     .Do(() => this.GoFav(forward: false)),
6160
6161                 ShortcutCommand.Create(Keys.Shift | Keys.Space)
6162                     .FocusedOn(FocusedControl.ListTab)
6163                     .Do(() => this.GoBackSelectPostChain()),
6164
6165                 ShortcutCommand.Create(Keys.Alt | Keys.R)
6166                     .Do(() => this.doReTweetOfficial(isConfirm: true)),
6167
6168                 ShortcutCommand.Create(Keys.Alt | Keys.P)
6169                     .OnlyWhen(() => this._curPost != null)
6170                     .Do(() => this.doShowUserStatus(_curPost.ScreenName, ShowInputDialog: false)),
6171
6172                 ShortcutCommand.Create(Keys.Alt | Keys.Up)
6173                     .Do(() => this.tweetDetailsView.ScrollDownPostBrowser(forward: false)),
6174
6175                 ShortcutCommand.Create(Keys.Alt | Keys.Down)
6176                     .Do(() => this.tweetDetailsView.ScrollDownPostBrowser(forward: true)),
6177
6178                 ShortcutCommand.Create(Keys.Alt | Keys.PageUp)
6179                     .Do(() => this.tweetDetailsView.PageDownPostBrowser(forward: false)),
6180
6181                 ShortcutCommand.Create(Keys.Alt | Keys.PageDown)
6182                     .Do(() => this.tweetDetailsView.PageDownPostBrowser(forward: true)),
6183
6184                 // 別タブの同じ書き込みへ(ALT+←/→)
6185                 ShortcutCommand.Create(Keys.Alt | Keys.Right)
6186                     .FocusedOn(FocusedControl.ListTab)
6187                     .Do(() => this.GoSamePostToAnotherTab(left: false)),
6188
6189                 ShortcutCommand.Create(Keys.Alt | Keys.Left)
6190                     .FocusedOn(FocusedControl.ListTab)
6191                     .Do(() => this.GoSamePostToAnotherTab(left: true)),
6192
6193                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.R)
6194                     .Do(() => this.MakeReplyOrDirectStatus(isAuto: false, isReply: true, isAll: true)),
6195
6196                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.C, Keys.Control | Keys.Shift | Keys.Insert)
6197                     .Do(() => this.CopyIdUri()),
6198
6199                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.F)
6200                     .OnlyWhen(() => this.ListTab.SelectedTab != null &&
6201                         this._statuses.Tabs[this.ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.PublicSearch)
6202                     .Do(() => this.ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"].Focus()),
6203
6204                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.S)
6205                     .Do(() => this.FavoriteChange(FavAdd: false)),
6206
6207                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.B)
6208                     .Do(() => this.UnreadStripMenuItem_Click(null, null)),
6209
6210                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.T)
6211                     .Do(() => this.HashToggleMenuItem_Click(null, null)),
6212
6213                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.P)
6214                     .Do(() => this.ImageSelectMenuItem_Click(null, null)),
6215
6216                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.H)
6217                     .Do(() => this.doMoveToRTHome()),
6218
6219                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.O)
6220                     .Do(() => this.FavorareMenuItem_Click(null, null)),
6221
6222                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.Up)
6223                     .FocusedOn(FocusedControl.StatusText)
6224                     .Do(() => {
6225                         if (_curList != null && _curList.VirtualListSize != 0 &&
6226                                     _curList.SelectedIndices.Count > 0 && _curList.SelectedIndices[0] > 0)
6227                         {
6228                             var idx = _curList.SelectedIndices[0] - 1;
6229                             SelectListItem(_curList, idx);
6230                             _curList.EnsureVisible(idx);
6231                         }
6232                     }),
6233
6234                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.Down)
6235                     .FocusedOn(FocusedControl.StatusText)
6236                     .Do(() => {
6237                         if (_curList != null && _curList.VirtualListSize != 0 && _curList.SelectedIndices.Count > 0
6238                                     && _curList.SelectedIndices[0] < _curList.VirtualListSize - 1)
6239                         {
6240                             var idx = _curList.SelectedIndices[0] + 1;
6241                             SelectListItem(_curList, idx);
6242                             _curList.EnsureVisible(idx);
6243                         }
6244                     }),
6245
6246                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.Space)
6247                     .FocusedOn(FocusedControl.StatusText)
6248                     .Do(() => {
6249                         if (StatusText.SelectionStart > 0)
6250                         {
6251                             int endidx = StatusText.SelectionStart - 1;
6252                             string startstr = "";
6253                             for (int i = StatusText.SelectionStart - 1; i >= 0; i--)
6254                             {
6255                                 char c = StatusText.Text[i];
6256                                 if (Char.IsLetterOrDigit(c) || c == '_')
6257                                 {
6258                                     continue;
6259                                 }
6260                                 if (c == '@')
6261                                 {
6262                                     startstr = StatusText.Text.Substring(i + 1, endidx - i);
6263                                     int cnt = AtIdSupl.ItemCount;
6264                                     ShowSuplDialog(StatusText, AtIdSupl, startstr.Length + 1, startstr);
6265                                     if (AtIdSupl.ItemCount != cnt) ModifySettingAtId = true;
6266                                 }
6267                                 else if (c == '#')
6268                                 {
6269                                     startstr = StatusText.Text.Substring(i + 1, endidx - i);
6270                                     ShowSuplDialog(StatusText, HashSupl, startstr.Length + 1, startstr);
6271                                 }
6272                                 else
6273                                 {
6274                                     break;
6275                                 }
6276                             }
6277                         }
6278                     }),
6279
6280                 // ソートダイレクト選択(Ctrl+Shift+1~8,Ctrl+Shift+9)
6281                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D1)
6282                     .FocusedOn(FocusedControl.ListTab)
6283                     .Do(() => this.SetSortColumnByDisplayIndex(0)),
6284
6285                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D2)
6286                     .FocusedOn(FocusedControl.ListTab)
6287                     .Do(() => this.SetSortColumnByDisplayIndex(1)),
6288
6289                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D3)
6290                     .FocusedOn(FocusedControl.ListTab)
6291                     .Do(() => this.SetSortColumnByDisplayIndex(2)),
6292
6293                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D4)
6294                     .FocusedOn(FocusedControl.ListTab)
6295                     .Do(() => this.SetSortColumnByDisplayIndex(3)),
6296
6297                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D5)
6298                     .FocusedOn(FocusedControl.ListTab)
6299                     .Do(() => this.SetSortColumnByDisplayIndex(4)),
6300
6301                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D6)
6302                     .FocusedOn(FocusedControl.ListTab)
6303                     .Do(() => this.SetSortColumnByDisplayIndex(5)),
6304
6305                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D7)
6306                     .FocusedOn(FocusedControl.ListTab)
6307                     .Do(() => this.SetSortColumnByDisplayIndex(6)),
6308
6309                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D8)
6310                     .FocusedOn(FocusedControl.ListTab)
6311                     .Do(() => this.SetSortColumnByDisplayIndex(7)),
6312
6313                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D9)
6314                     .FocusedOn(FocusedControl.ListTab)
6315                     .Do(() => this.SetSortLastColumn()),
6316
6317                 ShortcutCommand.Create(Keys.Control | Keys.Alt | Keys.S)
6318                     .Do(() => this.FavoritesRetweetOfficial()),
6319
6320                 ShortcutCommand.Create(Keys.Control | Keys.Alt | Keys.R)
6321                     .Do(() => this.FavoritesRetweetUnofficial()),
6322
6323                 ShortcutCommand.Create(Keys.Control | Keys.Alt | Keys.H)
6324                     .Do(() => this.OpenUserAppointUrl()),
6325
6326                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.R)
6327                     .FocusedOn(FocusedControl.PostBrowser)
6328                     .Do(() => this.doReTweetUnofficial()),
6329
6330                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.T)
6331                     .OnlyWhen(() => this.ExistCurrentPost)
6332                     .Do(() => this.tweetDetailsView.DoTranslation()),
6333
6334                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.R)
6335                     .Do(() => this.doReTweetUnofficial()),
6336
6337                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.C, Keys.Alt | Keys.Shift | Keys.Insert)
6338                     .Do(() => this.CopyUserId()),
6339
6340                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.Up)
6341                     .Do(() => this.tweetThumbnail1.ScrollUp()),
6342
6343                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.Down)
6344                     .Do(() => this.tweetThumbnail1.ScrollDown()),
6345
6346                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.Enter)
6347                     .FocusedOn(FocusedControl.ListTab)
6348                     .OnlyWhen(() => !this.SplitContainer3.Panel2Collapsed)
6349                     .Do(() => this.OpenThumbnailPicture(this.tweetThumbnail1.Thumbnail)),
6350             };
6351         }
6352
6353         internal bool CommonKeyDown(Keys keyData, FocusedControl focusedOn, out Task asyncTask)
6354         {
6355             // Task を返す非同期処理があれば asyncTask に代入する
6356             asyncTask = null;
6357
6358             // ShortcutCommand に対応しているコマンドはここで処理される
6359             foreach (var command in this.shortcutCommands)
6360             {
6361                 if (command.IsMatch(keyData, focusedOn))
6362                 {
6363                     asyncTask = command.RunCommand();
6364                     return command.PreventDefault;
6365                 }
6366             }
6367
6368             return false;
6369         }
6370
6371         private void GoNextTab(bool forward)
6372         {
6373             int idx = ListTab.SelectedIndex;
6374             if (forward)
6375             {
6376                 idx += 1;
6377                 if (idx > ListTab.TabPages.Count - 1) idx = 0;
6378             }
6379             else
6380             {
6381                 idx -= 1;
6382                 if (idx < 0) idx = ListTab.TabPages.Count - 1;
6383             }
6384             ListTab.SelectedIndex = idx;
6385         }
6386
6387         private void CopyStot()
6388         {
6389             string clstr = "";
6390             StringBuilder sb = new StringBuilder();
6391             bool IsProtected = false;
6392             bool isDm = false;
6393             if (this._curTab != null && this._statuses.GetTabByName(this._curTab.Text) != null) isDm = this._statuses.GetTabByName(this._curTab.Text).TabType == MyCommon.TabUsageType.DirectMessage;
6394             foreach (int idx in _curList.SelectedIndices)
6395             {
6396                 PostClass post = _statuses.Tabs[_curTab.Text][idx];
6397                 if (post.IsProtect)
6398                 {
6399                     IsProtected = true;
6400                     continue;
6401                 }
6402                 if (post.IsDeleted) continue;
6403                 if (!isDm)
6404                 {
6405                     if (post.RetweetedId != null)
6406                         sb.AppendFormat("{0}:{1} [https://twitter.com/{0}/status/{2}]{3}", post.ScreenName, post.TextSingleLine, post.RetweetedId, Environment.NewLine);
6407                     else
6408                         sb.AppendFormat("{0}:{1} [https://twitter.com/{0}/status/{2}]{3}", post.ScreenName, post.TextSingleLine, post.StatusId, Environment.NewLine);
6409                 }
6410                 else
6411                 {
6412                     sb.AppendFormat("{0}:{1} [{2}]{3}", post.ScreenName, post.TextSingleLine, post.StatusId, Environment.NewLine);
6413                 }
6414             }
6415             if (IsProtected)
6416             {
6417                 MessageBox.Show(Properties.Resources.CopyStotText1);
6418             }
6419             if (sb.Length > 0)
6420             {
6421                 clstr = sb.ToString();
6422                 try
6423                 {
6424                     Clipboard.SetDataObject(clstr, false, 5, 100);
6425                 }
6426                 catch (Exception ex)
6427                 {
6428                     MessageBox.Show(ex.Message);
6429                 }
6430             }
6431         }
6432
6433         private void CopyIdUri()
6434         {
6435             string clstr = "";
6436             StringBuilder sb = new StringBuilder();
6437             if (this._curTab == null) return;
6438             if (this._statuses.GetTabByName(this._curTab.Text) == null) return;
6439             if (this._statuses.GetTabByName(this._curTab.Text).TabType == MyCommon.TabUsageType.DirectMessage) return;
6440             foreach (int idx in _curList.SelectedIndices)
6441             {
6442                 var post = _statuses.Tabs[_curTab.Text][idx];
6443                 sb.Append(MyCommon.GetStatusUrl(post));
6444                 sb.Append(Environment.NewLine);
6445             }
6446             if (sb.Length > 0)
6447             {
6448                 clstr = sb.ToString();
6449                 try
6450                 {
6451                     Clipboard.SetDataObject(clstr, false, 5, 100);
6452                 }
6453                 catch (Exception ex)
6454                 {
6455                     MessageBox.Show(ex.Message);
6456                 }
6457             }
6458         }
6459
6460         private void GoFav(bool forward)
6461         {
6462             if (_curList.VirtualListSize == 0) return;
6463             int fIdx = 0;
6464             int toIdx = 0;
6465             int stp = 1;
6466
6467             if (forward)
6468             {
6469                 if (_curList.SelectedIndices.Count == 0)
6470                 {
6471                     fIdx = 0;
6472                 }
6473                 else
6474                 {
6475                     fIdx = _curList.SelectedIndices[0] + 1;
6476                     if (fIdx > _curList.VirtualListSize - 1) return;
6477                 }
6478                 toIdx = _curList.VirtualListSize;
6479                 stp = 1;
6480             }
6481             else
6482             {
6483                 if (_curList.SelectedIndices.Count == 0)
6484                 {
6485                     fIdx = _curList.VirtualListSize - 1;
6486                 }
6487                 else
6488                 {
6489                     fIdx = _curList.SelectedIndices[0] - 1;
6490                     if (fIdx < 0) return;
6491                 }
6492                 toIdx = -1;
6493                 stp = -1;
6494             }
6495
6496             for (int idx = fIdx; idx != toIdx; idx += stp)
6497             {
6498                 if (_statuses.Tabs[_curTab.Text][idx].IsFav)
6499                 {
6500                     SelectListItem(_curList, idx);
6501                     _curList.EnsureVisible(idx);
6502                     break;
6503                 }
6504             }
6505         }
6506
6507         private void GoSamePostToAnotherTab(bool left)
6508         {
6509             if (_curList.VirtualListSize == 0) return;
6510             int fIdx = 0;
6511             int toIdx = 0;
6512             int stp = 1;
6513             long targetId = 0;
6514
6515             if (_statuses.Tabs[_curTab.Text].TabType == MyCommon.TabUsageType.DirectMessage) return; // Directタブは対象外(見つかるはずがない)
6516             if (_curList.SelectedIndices.Count == 0) return; //未選択も処理しない
6517
6518             targetId = GetCurTabPost(_curList.SelectedIndices[0]).StatusId;
6519
6520             if (left)
6521             {
6522                 // 左のタブへ
6523                 if (ListTab.SelectedIndex == 0)
6524                 {
6525                     return;
6526                 }
6527                 else
6528                 {
6529                     fIdx = ListTab.SelectedIndex - 1;
6530                 }
6531                 toIdx = -1;
6532                 stp = -1;
6533             }
6534             else
6535             {
6536                 // 右のタブへ
6537                 if (ListTab.SelectedIndex == ListTab.TabCount - 1)
6538                 {
6539                     return;
6540                 }
6541                 else
6542                 {
6543                     fIdx = ListTab.SelectedIndex + 1;
6544                 }
6545                 toIdx = ListTab.TabCount;
6546                 stp = 1;
6547             }
6548
6549             bool found = false;
6550             for (int tabidx = fIdx; tabidx != toIdx; tabidx += stp)
6551             {
6552                 if (_statuses.Tabs[ListTab.TabPages[tabidx].Text].TabType == MyCommon.TabUsageType.DirectMessage) continue; // Directタブは対象外
6553                 for (int idx = 0; idx < ((DetailsListView)ListTab.TabPages[tabidx].Tag).VirtualListSize; idx++)
6554                 {
6555                     if (_statuses.Tabs[ListTab.TabPages[tabidx].Text][idx].StatusId == targetId)
6556                     {
6557                         ListTab.SelectedIndex = tabidx;
6558                         SelectListItem(_curList, idx);
6559                         _curList.EnsureVisible(idx);
6560                         found = true;
6561                         break;
6562                     }
6563                 }
6564                 if (found) break;
6565             }
6566         }
6567
6568         private void GoPost(bool forward)
6569         {
6570             if (_curList.SelectedIndices.Count == 0 || _curPost == null) return;
6571             int fIdx = 0;
6572             int toIdx = 0;
6573             int stp = 1;
6574
6575             if (forward)
6576             {
6577                 fIdx = _curList.SelectedIndices[0] + 1;
6578                 if (fIdx > _curList.VirtualListSize - 1) return;
6579                 toIdx = _curList.VirtualListSize;
6580                 stp = 1;
6581             }
6582             else
6583             {
6584                 fIdx = _curList.SelectedIndices[0] - 1;
6585                 if (fIdx < 0) return;
6586                 toIdx = -1;
6587                 stp = -1;
6588             }
6589
6590             string name = "";
6591             if (_curPost.RetweetedId == null)
6592             {
6593                 name = _curPost.ScreenName;
6594             }
6595             else
6596             {
6597                 name = _curPost.RetweetedBy;
6598             }
6599             for (int idx = fIdx; idx != toIdx; idx += stp)
6600             {
6601                 if (_statuses.Tabs[_curTab.Text][idx].RetweetedId == null)
6602                 {
6603                     if (_statuses.Tabs[_curTab.Text][idx].ScreenName == name)
6604                     {
6605                         SelectListItem(_curList, idx);
6606                         _curList.EnsureVisible(idx);
6607                         break;
6608                     }
6609                 }
6610                 else
6611                 {
6612                     if (_statuses.Tabs[_curTab.Text][idx].RetweetedBy == name)
6613                     {
6614                         SelectListItem(_curList, idx);
6615                         _curList.EnsureVisible(idx);
6616                         break;
6617                     }
6618                 }
6619             }
6620         }
6621
6622         private void GoRelPost(bool forward)
6623         {
6624             if (_curList.SelectedIndices.Count == 0) return;
6625
6626             int fIdx = 0;
6627             int toIdx = 0;
6628             int stp = 1;
6629             if (forward)
6630             {
6631                 fIdx = _curList.SelectedIndices[0] + 1;
6632                 if (fIdx > _curList.VirtualListSize - 1) return;
6633                 toIdx = _curList.VirtualListSize;
6634                 stp = 1;
6635             }
6636             else
6637             {
6638                 fIdx = _curList.SelectedIndices[0] - 1;
6639                 if (fIdx < 0) return;
6640                 toIdx = -1;
6641                 stp = -1;
6642             }
6643
6644             if (!_anchorFlag)
6645             {
6646                 if (_curPost == null) return;
6647                 _anchorPost = _curPost;
6648                 _anchorFlag = true;
6649             }
6650             else
6651             {
6652                 if (_anchorPost == null) return;
6653             }
6654
6655             for (int idx = fIdx; idx != toIdx; idx += stp)
6656             {
6657                 PostClass post = _statuses.Tabs[_curTab.Text][idx];
6658                 if (post.ScreenName == _anchorPost.ScreenName ||
6659                     post.RetweetedBy == _anchorPost.ScreenName ||
6660                     post.ScreenName == _anchorPost.RetweetedBy ||
6661                     (!string.IsNullOrEmpty(post.RetweetedBy) && post.RetweetedBy == _anchorPost.RetweetedBy) ||
6662                     _anchorPost.ReplyToList.Contains(post.ScreenName.ToLowerInvariant()) ||
6663                     _anchorPost.ReplyToList.Contains(post.RetweetedBy.ToLowerInvariant()) ||
6664                     post.ReplyToList.Contains(_anchorPost.ScreenName.ToLowerInvariant()) ||
6665                     post.ReplyToList.Contains(_anchorPost.RetweetedBy.ToLowerInvariant()))
6666                 {
6667                     SelectListItem(_curList, idx);
6668                     _curList.EnsureVisible(idx);
6669                     break;
6670                 }
6671             }
6672         }
6673
6674         private void GoAnchor()
6675         {
6676             if (_anchorPost == null) return;
6677             int idx = _statuses.Tabs[_curTab.Text].IndexOf(_anchorPost.StatusId);
6678             if (idx == -1) return;
6679
6680             SelectListItem(_curList, idx);
6681             _curList.EnsureVisible(idx);
6682         }
6683
6684         private void GoTopEnd(bool GoTop)
6685         {
6686             if (_curList.VirtualListSize == 0)
6687                 return;
6688
6689             ListViewItem _item;
6690             int idx;
6691
6692             if (GoTop)
6693             {
6694                 _item = _curList.GetItemAt(0, 25);
6695                 if (_item == null)
6696                     idx = 0;
6697                 else
6698                     idx = _item.Index;
6699             }
6700             else
6701             {
6702                 _item = _curList.GetItemAt(0, _curList.ClientSize.Height - 1);
6703                 if (_item == null)
6704                     idx = _curList.VirtualListSize - 1;
6705                 else
6706                     idx = _item.Index;
6707             }
6708             SelectListItem(_curList, idx);
6709         }
6710
6711         private void GoMiddle()
6712         {
6713             if (_curList.VirtualListSize == 0)
6714                 return;
6715
6716             ListViewItem _item;
6717             int idx1;
6718             int idx2;
6719             int idx3;
6720
6721             _item = _curList.GetItemAt(0, 0);
6722             if (_item == null)
6723             {
6724                 idx1 = 0;
6725             }
6726             else
6727             {
6728                 idx1 = _item.Index;
6729             }
6730
6731             _item = _curList.GetItemAt(0, _curList.ClientSize.Height - 1);
6732             if (_item == null)
6733             {
6734                 idx2 = _curList.VirtualListSize - 1;
6735             }
6736             else
6737             {
6738                 idx2 = _item.Index;
6739             }
6740             idx3 = (idx1 + idx2) / 2;
6741
6742             SelectListItem(_curList, idx3);
6743         }
6744
6745         private void GoLast()
6746         {
6747             if (_curList.VirtualListSize == 0) return;
6748
6749             if (_statuses.SortOrder == SortOrder.Ascending)
6750             {
6751                 SelectListItem(_curList, _curList.VirtualListSize - 1);
6752                 _curList.EnsureVisible(_curList.VirtualListSize - 1);
6753             }
6754             else
6755             {
6756                 SelectListItem(_curList, 0);
6757                 _curList.EnsureVisible(0);
6758             }
6759         }
6760
6761         private void MoveTop()
6762         {
6763             if (_curList.SelectedIndices.Count == 0) return;
6764             int idx = _curList.SelectedIndices[0];
6765             if (_statuses.SortOrder == SortOrder.Ascending)
6766             {
6767                 _curList.EnsureVisible(_curList.VirtualListSize - 1);
6768             }
6769             else
6770             {
6771                 _curList.EnsureVisible(0);
6772             }
6773             _curList.EnsureVisible(idx);
6774         }
6775
6776         private async Task GoInReplyToPostTree()
6777         {
6778             if (_curPost == null) return;
6779
6780             TabModel curTabClass = _statuses.Tabs[_curTab.Text];
6781
6782             if (curTabClass.TabType == MyCommon.TabUsageType.PublicSearch && _curPost.InReplyToStatusId == null && _curPost.TextFromApi.Contains("@"))
6783             {
6784                 try
6785                 {
6786                     var post = await tw.GetStatusApi(false, _curPost.StatusId);
6787
6788                     _curPost.InReplyToStatusId = post.InReplyToStatusId;
6789                     _curPost.InReplyToUser = post.InReplyToUser;
6790                     _curPost.IsReply = post.IsReply;
6791                     this.PurgeListViewItemCache();
6792                     _curList.RedrawItems(_curItemIndex, _curItemIndex, false);
6793                 }
6794                 catch (WebApiException ex)
6795                 {
6796                     this.StatusLabel.Text = $"Err:{ex.Message}(GetStatus)";
6797                 }
6798             }
6799
6800             if (!(this.ExistCurrentPost && _curPost.InReplyToUser != null && _curPost.InReplyToStatusId != null)) return;
6801
6802             if (replyChains == null || (replyChains.Count > 0 && replyChains.Peek().InReplyToId != _curPost.StatusId))
6803             {
6804                 replyChains = new Stack<ReplyChain>();
6805             }
6806             replyChains.Push(new ReplyChain(_curPost.StatusId, _curPost.InReplyToStatusId.Value, _curTab));
6807
6808             int inReplyToIndex;
6809             string inReplyToTabName;
6810             long inReplyToId = _curPost.InReplyToStatusId.Value;
6811             string inReplyToUser = _curPost.InReplyToUser;
6812             //Dictionary<long, PostClass> curTabPosts = curTabClass.Posts;
6813
6814             var inReplyToPosts = from tab in _statuses.Tabs.Values
6815                                  orderby tab != curTabClass
6816                                  from post in tab.Posts.Values
6817                                  where post.StatusId == inReplyToId
6818                                  let index = tab.IndexOf(post.StatusId)
6819                                  where index != -1
6820                                  select new {Tab = tab, Index = index};
6821
6822             var inReplyPost = inReplyToPosts.FirstOrDefault();
6823             if (inReplyPost == null)
6824             {
6825                 try
6826                 {
6827                     await Task.Run(async () =>
6828                     {
6829                         var post = await tw.GetStatusApi(false, _curPost.InReplyToStatusId.Value)
6830                             .ConfigureAwait(false);
6831                         post.IsRead = true;
6832
6833                         _statuses.AddPost(post);
6834                         _statuses.DistributePosts();
6835                     });
6836                 }
6837                 catch (WebApiException ex)
6838                 {
6839                     this.StatusLabel.Text = $"Err:{ex.Message}(GetStatus)";
6840                     await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(inReplyToUser, inReplyToId));
6841                     return;
6842                 }
6843
6844                 this.RefreshTimeline();
6845
6846                 inReplyPost = inReplyToPosts.FirstOrDefault();
6847                 if (inReplyPost == null)
6848                 {
6849                     await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(inReplyToUser, inReplyToId));
6850                     return;
6851                 }
6852             }
6853             inReplyToTabName = inReplyPost.Tab.TabName;
6854             inReplyToIndex = inReplyPost.Index;
6855
6856             TabPage tabPage = this.ListTab.TabPages.Cast<TabPage>().First((tp) => { return tp.Text == inReplyToTabName; });
6857             DetailsListView listView = (DetailsListView)tabPage.Tag;
6858
6859             if (_curTab != tabPage)
6860             {
6861                 this.ListTab.SelectTab(tabPage);
6862             }
6863
6864             this.SelectListItem(listView, inReplyToIndex);
6865             listView.EnsureVisible(inReplyToIndex);
6866         }
6867
6868         private void GoBackInReplyToPostTree(bool parallel = false, bool isForward = true)
6869         {
6870             if (_curPost == null) return;
6871
6872             TabModel curTabClass = _statuses.Tabs[_curTab.Text];
6873             //Dictionary<long, PostClass> curTabPosts = curTabClass.Posts;
6874
6875             if (parallel)
6876             {
6877                 if (_curPost.InReplyToStatusId != null)
6878                 {
6879                     var posts = from t in _statuses.Tabs
6880                                 from p in t.Value.Posts
6881                                 where p.Value.StatusId != _curPost.StatusId && p.Value.InReplyToStatusId == _curPost.InReplyToStatusId
6882                                 let indexOf = t.Value.IndexOf(p.Value.StatusId)
6883                                 where indexOf > -1
6884                                 orderby isForward ? indexOf : indexOf * -1
6885                                 orderby t.Value != curTabClass
6886                                 select new {Tab = t.Value, Post = p.Value, Index = indexOf};
6887                     try
6888                     {
6889                         var postList = posts.ToList();
6890                         for (int i = postList.Count - 1; i >= 0; i--)
6891                         {
6892                             int index = i;
6893                             if (postList.FindIndex((pst) => { return pst.Post.StatusId == postList[index].Post.StatusId; }) != index)
6894                             {
6895                                 postList.RemoveAt(index);
6896                             }
6897                         }
6898                         var post = postList.FirstOrDefault((pst) => { return pst.Tab == curTabClass && isForward ? pst.Index > _curItemIndex : pst.Index < _curItemIndex; });
6899                         if (post == null) post = postList.FirstOrDefault((pst) => { return pst.Tab != curTabClass; });
6900                         if (post == null) post = postList.First();
6901                         this.ListTab.SelectTab(this.ListTab.TabPages.Cast<TabPage>().First((tp) => { return tp.Text == post.Tab.TabName; }));
6902                         DetailsListView listView = (DetailsListView)this.ListTab.SelectedTab.Tag;
6903                         SelectListItem(listView, post.Index);
6904                         listView.EnsureVisible(post.Index);
6905                     }
6906                     catch (InvalidOperationException)
6907                     {
6908                         return;
6909                     }
6910                 }
6911             }
6912             else
6913             {
6914                 if (replyChains == null || replyChains.Count < 1)
6915                 {
6916                     var posts = from t in _statuses.Tabs
6917                                 from p in t.Value.Posts
6918                                 where p.Value.InReplyToStatusId == _curPost.StatusId
6919                                 let indexOf = t.Value.IndexOf(p.Value.StatusId)
6920                                 where indexOf > -1
6921                                 orderby indexOf
6922                                 orderby t.Value != curTabClass
6923                                 select new {Tab = t.Value, Index = indexOf};
6924                     try
6925                     {
6926                         var post = posts.First();
6927                         this.ListTab.SelectTab(this.ListTab.TabPages.Cast<TabPage>().First((tp) => { return tp.Text == post.Tab.TabName; }));
6928                         DetailsListView listView = (DetailsListView)this.ListTab.SelectedTab.Tag;
6929                         SelectListItem(listView, post.Index);
6930                         listView.EnsureVisible(post.Index);
6931                     }
6932                     catch (InvalidOperationException)
6933                     {
6934                         return;
6935                     }
6936                 }
6937                 else
6938                 {
6939                     ReplyChain chainHead = replyChains.Pop();
6940                     if (chainHead.InReplyToId == _curPost.StatusId)
6941                     {
6942                         int idx = _statuses.Tabs[chainHead.OriginalTab.Text].IndexOf(chainHead.OriginalId);
6943                         if (idx == -1)
6944                         {
6945                             replyChains = null;
6946                         }
6947                         else
6948                         {
6949                             try
6950                             {
6951                                 ListTab.SelectTab(chainHead.OriginalTab);
6952                             }
6953                             catch (Exception)
6954                             {
6955                                 replyChains = null;
6956                             }
6957                             SelectListItem(_curList, idx);
6958                             _curList.EnsureVisible(idx);
6959                         }
6960                     }
6961                     else
6962                     {
6963                         replyChains = null;
6964                         this.GoBackInReplyToPostTree(parallel);
6965                     }
6966                 }
6967             }
6968         }
6969
6970         private void GoBackSelectPostChain()
6971         {
6972             if (this.selectPostChains.Count > 1)
6973             {
6974                 var idx = -1;
6975                 TabPage tp = null;
6976
6977                 do
6978                 {
6979                     try
6980                     {
6981                         this.selectPostChains.Pop();
6982                         var tabPostPair = this.selectPostChains.Peek();
6983
6984                         if (!this.ListTab.TabPages.Contains(tabPostPair.Item1)) continue;  //該当タブが存在しないので無視
6985
6986                         if (tabPostPair.Item2 != null)
6987                         {
6988                             idx = this._statuses.Tabs[tabPostPair.Item1.Text].IndexOf(tabPostPair.Item2.StatusId);
6989                             if (idx == -1) continue;  //該当ポストが存在しないので無視
6990                         }
6991
6992                         tp = tabPostPair.Item1;
6993
6994                         this.selectPostChains.Pop();
6995                     }
6996                     catch (InvalidOperationException)
6997                     {
6998                     }
6999
7000                     break;
7001                 }
7002                 while (this.selectPostChains.Count > 1);
7003
7004                 if (tp == null)
7005                 {
7006                     //状態がおかしいので処理を中断
7007                     //履歴が残り1つであればクリアしておく
7008                     if (this.selectPostChains.Count == 1)
7009                         this.selectPostChains.Clear();
7010                     return;
7011                 }
7012
7013                 DetailsListView lst = (DetailsListView)tp.Tag;
7014                 this.ListTab.SelectedTab = tp;
7015                 if (idx > -1)
7016                 {
7017                     SelectListItem(lst, idx);
7018                     lst.EnsureVisible(idx);
7019                 }
7020                 lst.Focus();
7021             }
7022         }
7023
7024         private void PushSelectPostChain()
7025         {
7026             int count = this.selectPostChains.Count;
7027             if (count > 0)
7028             {
7029                 var p = this.selectPostChains.Peek();
7030                 if (p.Item1 == this._curTab)
7031                 {
7032                     if (p.Item2 == this._curPost) return;  //最新の履歴と同一
7033                     if (p.Item2 == null) this.selectPostChains.Pop();  //置き換えるため削除
7034                 }
7035             }
7036             if (count >= 2500) TrimPostChain();
7037             this.selectPostChains.Push(Tuple.Create(this._curTab, this._curPost));
7038         }
7039
7040         private void TrimPostChain()
7041         {
7042             if (this.selectPostChains.Count <= 2000) return;
7043             var p = new Stack<Tuple<TabPage, PostClass>>(2000);
7044             for (int i = 0; i < 2000; i++)
7045             {
7046                 p.Push(this.selectPostChains.Pop());
7047             }
7048             this.selectPostChains.Clear();
7049             for (int i = 0; i < 2000; i++)
7050             {
7051                 this.selectPostChains.Push(p.Pop());
7052             }
7053         }
7054
7055         private bool GoStatus(long statusId)
7056         {
7057             if (statusId == 0) return false;
7058             for (int tabidx = 0; tabidx < ListTab.TabCount; tabidx++)
7059             {
7060                 if (_statuses.Tabs[ListTab.TabPages[tabidx].Text].TabType != MyCommon.TabUsageType.DirectMessage && _statuses.Tabs[ListTab.TabPages[tabidx].Text].Contains(statusId))
7061                 {
7062                     int idx = _statuses.Tabs[ListTab.TabPages[tabidx].Text].IndexOf(statusId);
7063                     ListTab.SelectedIndex = tabidx;
7064                     SelectListItem(_curList, idx);
7065                     _curList.EnsureVisible(idx);
7066                     return true;
7067                 }
7068             }
7069             return false;
7070         }
7071
7072         private bool GoDirectMessage(long statusId)
7073         {
7074             if (statusId == 0) return false;
7075             for (int tabidx = 0; tabidx < ListTab.TabCount; tabidx++)
7076             {
7077                 if (_statuses.Tabs[ListTab.TabPages[tabidx].Text].TabType == MyCommon.TabUsageType.DirectMessage && _statuses.Tabs[ListTab.TabPages[tabidx].Text].Contains(statusId))
7078                 {
7079                     int idx = _statuses.Tabs[ListTab.TabPages[tabidx].Text].IndexOf(statusId);
7080                     ListTab.SelectedIndex = tabidx;
7081                     SelectListItem(_curList, idx);
7082                     _curList.EnsureVisible(idx);
7083                     return true;
7084                 }
7085             }
7086             return false;
7087         }
7088
7089         private void MyList_MouseClick(object sender, MouseEventArgs e)
7090         {
7091             _anchorFlag = false;
7092         }
7093
7094         private void StatusText_Enter(object sender, EventArgs e)
7095         {
7096             // フォーカスの戻り先を StatusText に設定
7097             this.Tag = StatusText;
7098             StatusText.BackColor = _clInputBackcolor;
7099         }
7100
7101         public Color InputBackColor
7102         {
7103             get { return _clInputBackcolor; }
7104             set { _clInputBackcolor = value; }
7105         }
7106
7107         private void StatusText_Leave(object sender, EventArgs e)
7108         {
7109             // フォーカスがメニューに遷移しないならばフォーカスはタブに移ることを期待
7110             if (ListTab.SelectedTab != null && MenuStrip1.Tag == null) this.Tag = ListTab.SelectedTab.Tag;
7111             StatusText.BackColor = Color.FromKnownColor(KnownColor.Window);
7112         }
7113
7114         private async void StatusText_KeyDown(object sender, KeyEventArgs e)
7115         {
7116             Task asyncTask;
7117             if (CommonKeyDown(e.KeyData, FocusedControl.StatusText, out asyncTask))
7118             {
7119                 e.Handled = true;
7120                 e.SuppressKeyPress = true;
7121             }
7122
7123             this.StatusText_TextChanged(null, null);
7124
7125             if (asyncTask != null)
7126                 await asyncTask;
7127         }
7128
7129         private void SaveConfigsAll(bool ifModified)
7130         {
7131             if (!ifModified)
7132             {
7133                 SaveConfigsCommon();
7134                 SaveConfigsLocal();
7135                 SaveConfigsTabs();
7136                 SaveConfigsAtId();
7137             }
7138             else
7139             {
7140                 if (ModifySettingCommon) SaveConfigsCommon();
7141                 if (ModifySettingLocal) SaveConfigsLocal();
7142                 if (ModifySettingAtId) SaveConfigsAtId();
7143             }
7144         }
7145
7146         private void SaveConfigsAtId()
7147         {
7148             if (_ignoreConfigSave || !this._cfgCommon.UseAtIdSupplement && AtIdSupl == null) return;
7149
7150             ModifySettingAtId = false;
7151             SettingAtIdList cfgAtId = new SettingAtIdList(AtIdSupl.GetItemList());
7152             cfgAtId.Save();
7153         }
7154
7155         private void SaveConfigsCommon()
7156         {
7157             if (_ignoreConfigSave) return;
7158
7159             ModifySettingCommon = false;
7160             lock (_syncObject)
7161             {
7162                 _cfgCommon.UserName = tw.Username;
7163                 _cfgCommon.UserId = tw.UserId;
7164                 _cfgCommon.Token = tw.AccessToken;
7165                 _cfgCommon.TokenSecret = tw.AccessTokenSecret;
7166
7167                 if (IdeographicSpaceToSpaceToolStripMenuItem != null &&
7168                    IdeographicSpaceToSpaceToolStripMenuItem.IsDisposed == false)
7169                 {
7170                     _cfgCommon.WideSpaceConvert = this.IdeographicSpaceToSpaceToolStripMenuItem.Checked;
7171                 }
7172
7173                 _cfgCommon.SortOrder = (int)_statuses.SortOrder;
7174                 switch (_statuses.SortMode)
7175                 {
7176                     case ComparerMode.Nickname:  //ニックネーム
7177                         _cfgCommon.SortColumn = 1;
7178                         break;
7179                     case ComparerMode.Data:  //本文
7180                         _cfgCommon.SortColumn = 2;
7181                         break;
7182                     case ComparerMode.Id:  //時刻=発言Id
7183                         _cfgCommon.SortColumn = 3;
7184                         break;
7185                     case ComparerMode.Name:  //名前
7186                         _cfgCommon.SortColumn = 4;
7187                         break;
7188                     case ComparerMode.Source:  //Source
7189                         _cfgCommon.SortColumn = 7;
7190                         break;
7191                 }
7192
7193                 _cfgCommon.HashTags = HashMgr.HashHistories;
7194                 if (HashMgr.IsPermanent)
7195                 {
7196                     _cfgCommon.HashSelected = HashMgr.UseHash;
7197                 }
7198                 else
7199                 {
7200                     _cfgCommon.HashSelected = "";
7201                 }
7202                 _cfgCommon.HashIsHead = HashMgr.IsHead;
7203                 _cfgCommon.HashIsPermanent = HashMgr.IsPermanent;
7204                 _cfgCommon.HashIsNotAddToAtReply = HashMgr.IsNotAddToAtReply;
7205                 if (ToolStripFocusLockMenuItem != null &&
7206                         ToolStripFocusLockMenuItem.IsDisposed == false)
7207                 {
7208                     _cfgCommon.FocusLockToStatusText = this.ToolStripFocusLockMenuItem.Checked;
7209                 }
7210                 _cfgCommon.TrackWord = tw.TrackWord;
7211                 _cfgCommon.AllAtReply = tw.AllAtReply;
7212                 _cfgCommon.UseImageService = ImageSelector.ServiceIndex;
7213                 _cfgCommon.UseImageServiceName = ImageSelector.ServiceName;
7214
7215                 _cfgCommon.Save();
7216             }
7217         }
7218
7219         private void SaveConfigsLocal()
7220         {
7221             if (_ignoreConfigSave) return;
7222             lock (_syncObject)
7223             {
7224                 ModifySettingLocal = false;
7225                 _cfgLocal.ScaleDimension = this.CurrentAutoScaleDimensions;
7226                 _cfgLocal.FormSize = _mySize;
7227                 _cfgLocal.FormLocation = _myLoc;
7228                 _cfgLocal.SplitterDistance = _mySpDis;
7229                 _cfgLocal.PreviewDistance = _mySpDis3;
7230                 _cfgLocal.StatusMultiline = StatusText.Multiline;
7231                 _cfgLocal.StatusTextHeight = _mySpDis2;
7232
7233                 _cfgLocal.FontUnread = _fntUnread;
7234                 _cfgLocal.ColorUnread = _clUnread;
7235                 _cfgLocal.FontRead = _fntReaded;
7236                 _cfgLocal.ColorRead = _clReaded;
7237                 _cfgLocal.FontDetail = _fntDetail;
7238                 _cfgLocal.ColorDetail = _clDetail;
7239                 _cfgLocal.ColorDetailBackcolor = _clDetailBackcolor;
7240                 _cfgLocal.ColorDetailLink = _clDetailLink;
7241                 _cfgLocal.ColorFav = _clFav;
7242                 _cfgLocal.ColorOWL = _clOWL;
7243                 _cfgLocal.ColorRetweet = _clRetweet;
7244                 _cfgLocal.ColorSelf = _clSelf;
7245                 _cfgLocal.ColorAtSelf = _clAtSelf;
7246                 _cfgLocal.ColorTarget = _clTarget;
7247                 _cfgLocal.ColorAtTarget = _clAtTarget;
7248                 _cfgLocal.ColorAtFromTarget = _clAtFromTarget;
7249                 _cfgLocal.ColorAtTo = _clAtTo;
7250                 _cfgLocal.ColorListBackcolor = _clListBackcolor;
7251                 _cfgLocal.ColorInputBackcolor = _clInputBackcolor;
7252                 _cfgLocal.ColorInputFont = _clInputFont;
7253                 _cfgLocal.FontInputFont = _fntInputFont;
7254
7255                 if (_ignoreConfigSave) return;
7256                 _cfgLocal.Save();
7257             }
7258         }
7259
7260         private void SaveConfigsTabs()
7261         {
7262             var tabsSetting = new SettingTabs();
7263
7264             var tabs = this.ListTab.TabPages.Cast<TabPage>()
7265                 .Select(x => this._statuses.Tabs[x.Text])
7266                 .Concat(new[] { this._statuses.GetTabByType(MyCommon.TabUsageType.Mute) });
7267
7268             foreach (var tab in tabs)
7269             {
7270                 if (!tab.IsPermanentTabType)
7271                     continue;
7272
7273                 var tabSetting = new SettingTabs.SettingTabItem
7274                 {
7275                     TabName = tab.TabName,
7276                     TabType = tab.TabType,
7277                     UnreadManage = tab.UnreadManage,
7278                     Protected = tab.Protected,
7279                     Notify = tab.Notify,
7280                     SoundFile = tab.SoundFile,
7281                 };
7282
7283                 var filterTab = tab as FilterTabModel;
7284                 if (filterTab != null)
7285                     tabSetting.FilterArray = filterTab.FilterArray;
7286
7287                 var userTab = tab as UserTimelineTabModel;
7288                 if (userTab != null)
7289                     tabSetting.User = userTab.ScreenName;
7290
7291                 var searchTab = tab as PublicSearchTabModel;
7292                 if (searchTab != null)
7293                 {
7294                     tabSetting.SearchWords = searchTab.SearchWords;
7295                     tabSetting.SearchLang = searchTab.SearchLang;
7296                 }
7297
7298                 var listTab = tab as ListTimelineTabModel;
7299                 if (listTab != null)
7300                     tabSetting.ListInfo = listTab.ListInfo;
7301
7302                 tabsSetting.Tabs.Add(tabSetting);
7303             }
7304
7305             tabsSetting.Save();
7306         }
7307
7308         private async void OpenURLFileMenuItem_Click(object sender, EventArgs e)
7309         {
7310             string inputText;
7311             var ret = InputDialog.Show(this, Properties.Resources.OpenURL_InputText, Properties.Resources.OpenURL_Caption, out inputText);
7312             if (ret != DialogResult.OK)
7313                 return;
7314
7315             var match = Twitter.StatusUrlRegex.Match(inputText);
7316             if (!match.Success)
7317             {
7318                 MessageBox.Show(this, Properties.Resources.OpenURL_InvalidFormat,
7319                     Properties.Resources.OpenURL_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
7320                 return;
7321             }
7322
7323             try
7324             {
7325                 var statusId = long.Parse(match.Groups["StatusId"].Value);
7326                 await this.OpenRelatedTab(statusId);
7327             }
7328             catch (TabException ex)
7329             {
7330                 MessageBox.Show(this, ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
7331             }
7332         }
7333
7334         private void SaveLogMenuItem_Click(object sender, EventArgs e)
7335         {
7336             DialogResult rslt = MessageBox.Show(string.Format(Properties.Resources.SaveLogMenuItem_ClickText1, Environment.NewLine),
7337                     Properties.Resources.SaveLogMenuItem_ClickText2,
7338                     MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
7339             if (rslt == DialogResult.Cancel) return;
7340
7341             SaveFileDialog1.FileName = MyCommon.GetAssemblyName() + "Posts" + DateTime.Now.ToString("yyMMdd-HHmmss") + ".tsv";
7342             SaveFileDialog1.InitialDirectory = Application.ExecutablePath;
7343             SaveFileDialog1.Filter = Properties.Resources.SaveLogMenuItem_ClickText3;
7344             SaveFileDialog1.FilterIndex = 0;
7345             SaveFileDialog1.Title = Properties.Resources.SaveLogMenuItem_ClickText4;
7346             SaveFileDialog1.RestoreDirectory = true;
7347
7348             if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
7349             {
7350                 if (!SaveFileDialog1.ValidateNames) return;
7351                 using (StreamWriter sw = new StreamWriter(SaveFileDialog1.FileName, false, Encoding.UTF8))
7352                 {
7353                     if (rslt == DialogResult.Yes)
7354                     {
7355                         //All
7356                         for (int idx = 0; idx < _curList.VirtualListSize; idx++)
7357                         {
7358                             PostClass post = _statuses.Tabs[_curTab.Text][idx];
7359                             string protect = "";
7360                             if (post.IsProtect) protect = "Protect";
7361                             sw.WriteLine(post.Nickname + "\t" +
7362                                      "\"" + post.TextFromApi.Replace("\n", "").Replace("\"", "\"\"") + "\"" + "\t" +
7363                                      post.CreatedAt + "\t" +
7364                                      post.ScreenName + "\t" +
7365                                      post.StatusId + "\t" +
7366                                      post.ImageUrl + "\t" +
7367                                      "\"" + post.Text.Replace("\n", "").Replace("\"", "\"\"") + "\"" + "\t" +
7368                                      protect);
7369                         }
7370                     }
7371                     else
7372                     {
7373                         foreach (int idx in _curList.SelectedIndices)
7374                         {
7375                             PostClass post = _statuses.Tabs[_curTab.Text][idx];
7376                             string protect = "";
7377                             if (post.IsProtect) protect = "Protect";
7378                             sw.WriteLine(post.Nickname + "\t" +
7379                                      "\"" + post.TextFromApi.Replace("\n", "").Replace("\"", "\"\"") + "\"" + "\t" +
7380                                      post.CreatedAt + "\t" +
7381                                      post.ScreenName + "\t" +
7382                                      post.StatusId + "\t" +
7383                                      post.ImageUrl + "\t" +
7384                                      "\"" + post.Text.Replace("\n", "").Replace("\"", "\"\"") + "\"" + "\t" +
7385                                      protect);
7386                         }
7387                     }
7388                 }
7389             }
7390             this.TopMost = this._cfgCommon.AlwaysTop;
7391         }
7392
7393         public bool TabRename(ref string tabName)
7394         {
7395             //タブ名変更
7396             string newTabText = null;
7397             using (InputTabName inputName = new InputTabName())
7398             {
7399                 inputName.TabName = tabName;
7400                 inputName.ShowDialog();
7401                 if (inputName.DialogResult == DialogResult.Cancel) return false;
7402                 newTabText = inputName.TabName;
7403             }
7404             this.TopMost = this._cfgCommon.AlwaysTop;
7405             if (!string.IsNullOrEmpty(newTabText))
7406             {
7407                 //新タブ名存在チェック
7408                 for (int i = 0; i < ListTab.TabCount; i++)
7409                 {
7410                     if (ListTab.TabPages[i].Text == newTabText)
7411                     {
7412                         string tmp = string.Format(Properties.Resources.Tabs_DoubleClickText1, newTabText);
7413                         MessageBox.Show(tmp, Properties.Resources.Tabs_DoubleClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
7414                         return false;
7415                     }
7416                 }
7417                 //タブ名を変更
7418                 for (int i = 0; i < ListTab.TabCount; i++)
7419                 {
7420                     if (ListTab.TabPages[i].Text == tabName)
7421                     {
7422                         ListTab.TabPages[i].Text = newTabText;
7423                         break;
7424                     }
7425                 }
7426                 _statuses.RenameTab(tabName, newTabText);
7427
7428                 SaveConfigsCommon();
7429                 SaveConfigsTabs();
7430                 _rclickTabName = newTabText;
7431                 tabName = newTabText;
7432                 return true;
7433             }
7434             else
7435             {
7436                 return false;
7437             }
7438         }
7439
7440         private void ListTab_MouseClick(object sender, MouseEventArgs e)
7441         {
7442             if (e.Button == MouseButtons.Middle)
7443             {
7444                 for (int i = 0; i < this.ListTab.TabPages.Count; i++)
7445                 {
7446                     if (this.ListTab.GetTabRect(i).Contains(e.Location))
7447                     {
7448                         this.RemoveSpecifiedTab(this.ListTab.TabPages[i].Text, true);
7449                         this.SaveConfigsTabs();
7450                         break;
7451                     }
7452                 }
7453             }
7454         }
7455
7456         private void ListTab_DoubleClick(object sender, MouseEventArgs e)
7457         {
7458             string tn = ListTab.SelectedTab.Text;
7459             TabRename(ref tn);
7460         }
7461
7462         private void ListTab_MouseDown(object sender, MouseEventArgs e)
7463         {
7464             if (this._cfgCommon.TabMouseLock) return;
7465             if (e.Button == MouseButtons.Left)
7466             {
7467                 for (int i = 0; i < ListTab.TabPages.Count; i++)
7468                 {
7469                     if (this.ListTab.GetTabRect(i).Contains(e.Location))
7470                     {
7471                         _tabDrag = true;
7472                         _tabMouseDownPoint = e.Location;
7473                         break;
7474                     }
7475                 }
7476             }
7477             else
7478             {
7479                 _tabDrag = false;
7480             }
7481         }
7482
7483         private void ListTab_DragEnter(object sender, DragEventArgs e)
7484         {
7485             if (e.Data.GetDataPresent(typeof(TabPage)))
7486                 e.Effect = DragDropEffects.Move;
7487             else
7488                 e.Effect = DragDropEffects.None;
7489         }
7490
7491         private void ListTab_DragDrop(object sender, DragEventArgs e)
7492         {
7493             if (!e.Data.GetDataPresent(typeof(TabPage))) return;
7494
7495             _tabDrag = false;
7496             string tn = "";
7497             bool bef = false;
7498             Point cpos = new Point(e.X, e.Y);
7499             Point spos = ListTab.PointToClient(cpos);
7500             int i;
7501             for (i = 0; i < ListTab.TabPages.Count; i++)
7502             {
7503                 Rectangle rect = ListTab.GetTabRect(i);
7504                 if (rect.Left <= spos.X && spos.X <= rect.Right &&
7505                     rect.Top <= spos.Y && spos.Y <= rect.Bottom)
7506                 {
7507                     tn = ListTab.TabPages[i].Text;
7508                     if (spos.X <= (rect.Left + rect.Right) / 2)
7509                         bef = true;
7510                     else
7511                         bef = false;
7512
7513                     break;
7514                 }
7515             }
7516
7517             //タブのないところにドロップ->最後尾へ移動
7518             if (string.IsNullOrEmpty(tn))
7519             {
7520                 tn = ListTab.TabPages[ListTab.TabPages.Count - 1].Text;
7521                 bef = false;
7522                 i = ListTab.TabPages.Count - 1;
7523             }
7524
7525             TabPage tp = (TabPage)e.Data.GetData(typeof(TabPage));
7526             if (tp.Text == tn) return;
7527
7528             ReOrderTab(tp.Text, tn, bef);
7529         }
7530
7531         public void ReOrderTab(string targetTabText, string baseTabText, bool isBeforeBaseTab)
7532         {
7533             var baseIndex = this.GetTabPageIndex(baseTabText);
7534             if (baseIndex == -1)
7535                 return;
7536
7537             var targetIndex = this.GetTabPageIndex(targetTabText);
7538             if (targetIndex == -1)
7539                 return;
7540
7541             using (ControlTransaction.Layout(this.ListTab))
7542             {
7543                 var mTp = this.ListTab.TabPages[targetIndex];
7544                 this.ListTab.TabPages.Remove(mTp);
7545
7546                 if (targetIndex < baseIndex)
7547                     baseIndex--;
7548
7549                 if (isBeforeBaseTab)
7550                     ListTab.TabPages.Insert(baseIndex, mTp);
7551                 else
7552                     ListTab.TabPages.Insert(baseIndex + 1, mTp);
7553             }
7554
7555             SaveConfigsTabs();
7556         }
7557
7558         private void MakeReplyOrDirectStatus(bool isAuto = true, bool isReply = true, bool isAll = false)
7559         {
7560             //isAuto:true=先頭に挿入、false=カーソル位置に挿入
7561             //isReply:true=@,false=DM
7562             if (!StatusText.Enabled) return;
7563             if (_curList == null) return;
7564             if (_curTab == null) return;
7565             if (!this.ExistCurrentPost) return;
7566
7567             // 複数あてリプライはReplyではなく通常ポスト
7568             //↑仕様変更で全部リプライ扱いでOK(先頭ドット付加しない)
7569             //090403暫定でドットを付加しないようにだけ修正。単独と複数の処理は統合できると思われる。
7570             //090513 all @ replies 廃止の仕様変更によりドット付加に戻し(syo68k)
7571
7572             if (_curList.SelectedIndices.Count > 0)
7573             {
7574                 // アイテムが1件以上選択されている
7575                 if (_curList.SelectedIndices.Count == 1 && !isAll && this.ExistCurrentPost)
7576                 {
7577                     // 単独ユーザー宛リプライまたはDM
7578                     if ((_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.DirectMessage && isAuto) || (!isAuto && !isReply))
7579                     {
7580                         // ダイレクトメッセージ
7581                         StatusText.Text = "D " + _curPost.ScreenName + " " + StatusText.Text;
7582                         StatusText.SelectionStart = StatusText.Text.Length;
7583                         StatusText.Focus();
7584                         this.inReplyTo = null;
7585                         return;
7586                     }
7587                     if (string.IsNullOrEmpty(StatusText.Text))
7588                     {
7589                         //空の場合
7590
7591                         // ステータステキストが入力されていない場合先頭に@ユーザー名を追加する
7592                         StatusText.Text = "@" + _curPost.ScreenName + " ";
7593
7594                         var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
7595                         var inReplyToScreenName = this._curPost.ScreenName;
7596                         this.inReplyTo = Tuple.Create(inReplyToStatusId, inReplyToScreenName);
7597                     }
7598                     else
7599                     {
7600                         //何か入力済の場合
7601
7602                         if (isAuto)
7603                         {
7604                             //1件選んでEnter or DoubleClick
7605                             if (StatusText.Text.Contains("@" + _curPost.ScreenName + " "))
7606                             {
7607                                 if (this.inReplyTo?.Item2 == _curPost.ScreenName)
7608                                 {
7609                                     //返信先書き換え
7610                                     var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
7611                                     var inReplyToScreenName = this._curPost.ScreenName;
7612                                     this.inReplyTo = Tuple.Create(inReplyToStatusId, inReplyToScreenName);
7613                                 }
7614                                 return;
7615                             }
7616                             if (!StatusText.Text.StartsWith("@", StringComparison.Ordinal))
7617                             {
7618                                 //文頭@以外
7619                                 if (StatusText.Text.StartsWith(". ", StringComparison.Ordinal))
7620                                 {
7621                                     // 複数リプライ
7622                                     StatusText.Text = StatusText.Text.Insert(2, "@" + _curPost.ScreenName + " ");
7623                                     this.inReplyTo = null;
7624                                 }
7625                                 else
7626                                 {
7627                                     // 単独リプライ
7628                                     StatusText.Text = "@" + _curPost.ScreenName + " " + StatusText.Text;
7629                                     var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
7630                                     var inReplyToScreenName = this._curPost.ScreenName;
7631                                     this.inReplyTo = Tuple.Create(inReplyToStatusId, inReplyToScreenName);
7632                                 }
7633                             }
7634                             else
7635                             {
7636                                 //文頭@
7637                                 // 複数リプライ
7638                                 StatusText.Text = ". @" + _curPost.ScreenName + " " + StatusText.Text;
7639                                 //StatusText.Text = "@" + _curPost.ScreenName + " " + StatusText.Text;
7640                                 this.inReplyTo = null;
7641                             }
7642                         }
7643                         else
7644                         {
7645                             //1件選んでCtrl-Rの場合(返信先操作せず)
7646                             int sidx = StatusText.SelectionStart;
7647                             string id = "@" + _curPost.ScreenName + " ";
7648                             if (sidx > 0)
7649                             {
7650                                 if (StatusText.Text.Substring(sidx - 1, 1) != " ")
7651                                 {
7652                                     id = " " + id;
7653                                 }
7654                             }
7655                             StatusText.Text = StatusText.Text.Insert(sidx, id);
7656                             sidx += id.Length;
7657                             //if (StatusText.Text.StartsWith("@"))
7658                             //{
7659                             //    //複数リプライ
7660                             //    StatusText.Text = ". " + StatusText.Text.Insert(sidx, " @" + _curPost.ScreenName + " ");
7661                             //    sidx += 5 + _curPost.ScreenName.Length;
7662                             //}
7663                             //else
7664                             //{
7665                             //    // 複数リプライ
7666                             //    StatusText.Text = StatusText.Text.Insert(sidx, " @" + _curPost.ScreenName + " ");
7667                             //    sidx += 3 + _curPost.ScreenName.Length;
7668                             //}
7669                             StatusText.SelectionStart = sidx;
7670                             StatusText.Focus();
7671                             //_reply_to_id = 0;
7672                             //_reply_to_name = null;
7673                             return;
7674                         }
7675                     }
7676                 }
7677                 else
7678                 {
7679                     // 複数リプライ
7680                     if (!isAuto && !isReply) return;
7681
7682                     //C-S-rか、複数の宛先を選択中にEnter/DoubleClick/C-r/C-S-r
7683
7684                     if (isAuto)
7685                     {
7686                         //Enter or DoubleClick
7687
7688                         string sTxt = StatusText.Text;
7689                         if (!sTxt.StartsWith(". ", StringComparison.Ordinal))
7690                         {
7691                             sTxt = ". " + sTxt;
7692                             this.inReplyTo = null;
7693                         }
7694                         for (int cnt = 0; cnt < _curList.SelectedIndices.Count; cnt++)
7695                         {
7696                             PostClass post = _statuses.Tabs[_curTab.Text][_curList.SelectedIndices[cnt]];
7697                             if (!sTxt.Contains("@" + post.ScreenName + " "))
7698                             {
7699                                 sTxt = sTxt.Insert(2, "@" + post.ScreenName + " ");
7700                                 //sTxt = "@" + post.ScreenName + " " + sTxt;
7701                             }
7702                         }
7703                         StatusText.Text = sTxt;
7704                     }
7705                     else
7706                     {
7707                         //C-S-r or C-r
7708                         if (_curList.SelectedIndices.Count > 1)
7709                         {
7710                             //複数ポスト選択
7711
7712                             string ids = "";
7713                             int sidx = StatusText.SelectionStart;
7714                             for (int cnt = 0; cnt < _curList.SelectedIndices.Count; cnt++)
7715                             {
7716                                 PostClass post = _statuses.Tabs[_curTab.Text][_curList.SelectedIndices[cnt]];
7717                                 if (!ids.Contains("@" + post.ScreenName + " ") &&
7718                                     !post.ScreenName.Equals(tw.Username, StringComparison.CurrentCultureIgnoreCase))
7719                                 {
7720                                     ids += "@" + post.ScreenName + " ";
7721                                 }
7722                                 if (isAll)
7723                                 {
7724                                     foreach (string nm in post.ReplyToList)
7725                                     {
7726                                         if (!ids.Contains("@" + nm + " ") &&
7727                                             !nm.Equals(tw.Username, StringComparison.CurrentCultureIgnoreCase))
7728                                         {
7729                                             Match m = Regex.Match(post.TextFromApi, "[@@](?<id>" + nm + ")([^a-zA-Z0-9]|$)", RegexOptions.IgnoreCase);
7730                                             if (m.Success)
7731                                                 ids += "@" + m.Result("${id}") + " ";
7732                                             else
7733                                                 ids += "@" + nm + " ";
7734                                         }
7735                                     }
7736                                 }
7737                             }
7738                             if (ids.Length == 0) return;
7739                             if (!StatusText.Text.StartsWith(". ", StringComparison.Ordinal))
7740                             {
7741                                 StatusText.Text = ". " + StatusText.Text;
7742                                 sidx += 2;
7743                                 this.inReplyTo = null;
7744                             }
7745                             if (sidx > 0)
7746                             {
7747                                 if (StatusText.Text.Substring(sidx - 1, 1) != " ")
7748                                 {
7749                                     ids = " " + ids;
7750                                 }
7751                             }
7752                             StatusText.Text = StatusText.Text.Insert(sidx, ids);
7753                             sidx += ids.Length;
7754                             //if (StatusText.Text.StartsWith("@"))
7755                             //{
7756                             //    StatusText.Text = ". " + StatusText.Text.Insert(sidx, ids);
7757                             //    sidx += 2 + ids.Length;
7758                             //}
7759                             //else
7760                             //{
7761                             //    StatusText.Text = StatusText.Text.Insert(sidx, ids);
7762                             //    sidx += 1 + ids.Length;
7763                             //}
7764                             StatusText.SelectionStart = sidx;
7765                             StatusText.Focus();
7766                             return;
7767                         }
7768                         else
7769                         {
7770                             //1件のみ選択のC-S-r(返信元付加する可能性あり)
7771
7772                             string ids = "";
7773                             int sidx = StatusText.SelectionStart;
7774                             PostClass post = _curPost;
7775                             if (!ids.Contains("@" + post.ScreenName + " ") &&
7776                                 !post.ScreenName.Equals(tw.Username, StringComparison.CurrentCultureIgnoreCase))
7777                             {
7778                                 ids += "@" + post.ScreenName + " ";
7779                             }
7780                             foreach (string nm in post.ReplyToList)
7781                             {
7782                                 if (!ids.Contains("@" + nm + " ") &&
7783                                     !nm.Equals(tw.Username, StringComparison.CurrentCultureIgnoreCase))
7784                                 {
7785                                     Match m = Regex.Match(post.TextFromApi, "[@@](?<id>" + nm + ")([^a-zA-Z0-9]|$)", RegexOptions.IgnoreCase);
7786                                     if (m.Success)
7787                                         ids += "@" + m.Result("${id}") + " ";
7788                                     else
7789                                         ids += "@" + nm + " ";
7790                                 }
7791                             }
7792                             if (!string.IsNullOrEmpty(post.RetweetedBy))
7793                             {
7794                                 if (!ids.Contains("@" + post.RetweetedBy + " ") &&
7795                                    !post.RetweetedBy.Equals(tw.Username, StringComparison.CurrentCultureIgnoreCase))
7796                                 {
7797                                     ids += "@" + post.RetweetedBy + " ";
7798                                 }
7799                             }
7800                             if (ids.Length == 0) return;
7801                             if (string.IsNullOrEmpty(StatusText.Text))
7802                             {
7803                                 //未入力の場合のみ返信先付加
7804                                 StatusText.Text = ids;
7805                                 StatusText.SelectionStart = ids.Length;
7806                                 StatusText.Focus();
7807
7808                                 var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
7809                                 var inReplyToScreenName = this._curPost.ScreenName;
7810                                 this.inReplyTo = Tuple.Create(inReplyToStatusId, inReplyToScreenName);
7811                                 return;
7812                             }
7813
7814                             if (sidx > 0)
7815                             {
7816                                 if (StatusText.Text.Substring(sidx - 1, 1) != " ")
7817                                 {
7818                                     ids = " " + ids;
7819                                 }
7820                             }
7821                             StatusText.Text = StatusText.Text.Insert(sidx, ids);
7822                             sidx += ids.Length;
7823                             StatusText.SelectionStart = sidx;
7824                             StatusText.Focus();
7825                             return;
7826                         }
7827                     }
7828                 }
7829                 StatusText.SelectionStart = StatusText.Text.Length;
7830                 StatusText.Focus();
7831             }
7832         }
7833
7834         private void ListTab_MouseUp(object sender, MouseEventArgs e)
7835         {
7836             _tabDrag = false;
7837         }
7838
7839         private static int iconCnt = 0;
7840         private static int blinkCnt = 0;
7841         private static bool blink = false;
7842         private static bool idle = false;
7843
7844         private async Task RefreshTasktrayIcon()
7845         {
7846             if (_colorize)
7847                 await this.Colorize();
7848
7849             if (!TimerRefreshIcon.Enabled) return;
7850             //Static usCheckCnt As int = 0
7851
7852             //Static iconDlListTopItem As ListViewItem = null
7853
7854             //if (((ListView)ListTab.SelectedTab.Tag).TopItem == iconDlListTopItem)
7855             //    ((ImageDictionary)this.TIconDic).PauseGetImage = false;
7856             //else
7857             //    ((ImageDictionary)this.TIconDic).PauseGetImage = true;
7858             //
7859             //iconDlListTopItem = ((ListView)ListTab.SelectedTab.Tag).TopItem;
7860
7861             iconCnt += 1;
7862             blinkCnt += 1;
7863             //usCheckCnt += 1;
7864
7865             //if (usCheckCnt > 300)    //1min
7866             //{
7867             //    usCheckCnt = 0;
7868             //    if (!this.IsReceivedUserStream)
7869             //    {
7870             //        TraceOut("ReconnectUserStream");
7871             //        tw.ReconnectUserStream();
7872             //    }
7873             //}
7874
7875             var busy = this.workerSemaphore.CurrentCount != MAX_WORKER_THREADS;
7876
7877             if (iconCnt >= this.NIconRefresh.Length)
7878             {
7879                 iconCnt = 0;
7880             }
7881             if (blinkCnt > 10)
7882             {
7883                 blinkCnt = 0;
7884                 //未保存の変更を保存
7885                 SaveConfigsAll(true);
7886             }
7887
7888             if (busy)
7889             {
7890                 NotifyIcon1.Icon = NIconRefresh[iconCnt];
7891                 idle = false;
7892                 _myStatusError = false;
7893                 return;
7894             }
7895
7896             TabModel tb = _statuses.GetTabByType(MyCommon.TabUsageType.Mentions);
7897             if (this._cfgCommon.ReplyIconState != MyCommon.REPLY_ICONSTATE.None && tb != null && tb.UnreadCount > 0)
7898             {
7899                 if (blinkCnt > 0) return;
7900                 blink = !blink;
7901                 if (blink || this._cfgCommon.ReplyIconState == MyCommon.REPLY_ICONSTATE.StaticIcon)
7902                 {
7903                     NotifyIcon1.Icon = ReplyIcon;
7904                 }
7905                 else
7906                 {
7907                     NotifyIcon1.Icon = ReplyIconBlink;
7908                 }
7909                 idle = false;
7910                 return;
7911             }
7912
7913             if (idle) return;
7914             idle = true;
7915             //優先度:エラー→オフライン→アイドル
7916             //エラーは更新アイコンでクリアされる
7917             if (_myStatusError)
7918             {
7919                 NotifyIcon1.Icon = NIconAtRed;
7920                 return;
7921             }
7922             if (_myStatusOnline)
7923             {
7924                 NotifyIcon1.Icon = NIconAt;
7925             }
7926             else
7927             {
7928                 NotifyIcon1.Icon = NIconAtSmoke;
7929             }
7930         }
7931
7932         private async void TimerRefreshIcon_Tick(object sender, EventArgs e)
7933         {
7934             //200ms
7935             await this.RefreshTasktrayIcon();
7936         }
7937
7938         private void ContextMenuTabProperty_Opening(object sender, CancelEventArgs e)
7939         {
7940             //右クリックの場合はタブ名が設定済。アプリケーションキーの場合は現在のタブを対象とする
7941             if (string.IsNullOrEmpty(_rclickTabName) || sender != ContextMenuTabProperty)
7942             {
7943                 if (ListTab != null && ListTab.SelectedTab != null)
7944                     _rclickTabName = ListTab.SelectedTab.Text;
7945                 else
7946                     return;
7947             }
7948
7949             if (_statuses == null) return;
7950             if (_statuses.Tabs == null) return;
7951
7952             TabModel tb = _statuses.Tabs[_rclickTabName];
7953             if (tb == null) return;
7954
7955             NotifyDispMenuItem.Checked = tb.Notify;
7956             this.NotifyTbMenuItem.Checked = tb.Notify;
7957
7958             soundfileListup = true;
7959             SoundFileComboBox.Items.Clear();
7960             this.SoundFileTbComboBox.Items.Clear();
7961             SoundFileComboBox.Items.Add("");
7962             this.SoundFileTbComboBox.Items.Add("");
7963             DirectoryInfo oDir = new DirectoryInfo(Application.StartupPath + Path.DirectorySeparatorChar);
7964             if (Directory.Exists(Path.Combine(Application.StartupPath, "Sounds")))
7965             {
7966                 oDir = oDir.GetDirectories("Sounds")[0];
7967             }
7968             foreach (FileInfo oFile in oDir.GetFiles("*.wav"))
7969             {
7970                 SoundFileComboBox.Items.Add(oFile.Name);
7971                 this.SoundFileTbComboBox.Items.Add(oFile.Name);
7972             }
7973             int idx = SoundFileComboBox.Items.IndexOf(tb.SoundFile);
7974             if (idx == -1) idx = 0;
7975             SoundFileComboBox.SelectedIndex = idx;
7976             this.SoundFileTbComboBox.SelectedIndex = idx;
7977             soundfileListup = false;
7978             UreadManageMenuItem.Checked = tb.UnreadManage;
7979             this.UnreadMngTbMenuItem.Checked = tb.UnreadManage;
7980
7981             TabMenuControl(_rclickTabName);
7982         }
7983
7984         private void TabMenuControl(string tabName)
7985         {
7986             var tabInfo = _statuses.GetTabByName(tabName);
7987
7988             this.FilterEditMenuItem.Enabled = true;
7989             this.EditRuleTbMenuItem.Enabled = true;
7990
7991             if (tabInfo.IsDefaultTabType)
7992             {
7993                 this.ProtectTabMenuItem.Enabled = false;
7994                 this.ProtectTbMenuItem.Enabled = false;
7995             }
7996             else
7997             {
7998                 this.ProtectTabMenuItem.Enabled = true;
7999                 this.ProtectTbMenuItem.Enabled = true;
8000             }
8001
8002             if (tabInfo.IsDefaultTabType || tabInfo.Protected)
8003             {
8004                 this.ProtectTabMenuItem.Checked = true;
8005                 this.ProtectTbMenuItem.Checked = true;
8006                 this.DeleteTabMenuItem.Enabled = false;
8007                 this.DeleteTbMenuItem.Enabled = false;
8008             }
8009             else
8010             {
8011                 this.ProtectTabMenuItem.Checked = false;
8012                 this.ProtectTbMenuItem.Checked = false;
8013                 this.DeleteTabMenuItem.Enabled = true;
8014                 this.DeleteTbMenuItem.Enabled = true;
8015             }
8016         }
8017
8018         private void ProtectTabMenuItem_Click(object sender, EventArgs e)
8019         {
8020             var checkState = ((ToolStripMenuItem)sender).Checked;
8021
8022             // チェック状態を同期
8023             this.ProtectTbMenuItem.Checked = checkState;
8024             this.ProtectTabMenuItem.Checked = checkState;
8025
8026             // ロック中はタブの削除を無効化
8027             this.DeleteTabMenuItem.Enabled = !checkState;
8028             this.DeleteTbMenuItem.Enabled = !checkState;
8029
8030             if (string.IsNullOrEmpty(_rclickTabName)) return;
8031             _statuses.Tabs[_rclickTabName].Protected = checkState;
8032
8033             SaveConfigsTabs();
8034         }
8035
8036         private void UreadManageMenuItem_Click(object sender, EventArgs e)
8037         {
8038             UreadManageMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
8039             this.UnreadMngTbMenuItem.Checked = UreadManageMenuItem.Checked;
8040
8041             if (string.IsNullOrEmpty(_rclickTabName)) return;
8042             ChangeTabUnreadManage(_rclickTabName, UreadManageMenuItem.Checked);
8043
8044             SaveConfigsTabs();
8045         }
8046
8047         public void ChangeTabUnreadManage(string tabName, bool isManage)
8048         {
8049             var idx = this.GetTabPageIndex(tabName);
8050             if (idx == -1)
8051                 return;
8052
8053             _statuses.Tabs[tabName].UnreadManage = isManage;
8054             if (this._cfgCommon.TabIconDisp)
8055             {
8056                 if (_statuses.Tabs[tabName].UnreadCount > 0)
8057                     ListTab.TabPages[idx].ImageIndex = 0;
8058                 else
8059                     ListTab.TabPages[idx].ImageIndex = -1;
8060             }
8061
8062             if (_curTab.Text == tabName)
8063             {
8064                 this.PurgeListViewItemCache();
8065                 _curList.Refresh();
8066             }
8067
8068             SetMainWindowTitle();
8069             SetStatusLabelUrl();
8070             if (!this._cfgCommon.TabIconDisp) ListTab.Refresh();
8071         }
8072
8073         private void NotifyDispMenuItem_Click(object sender, EventArgs e)
8074         {
8075             NotifyDispMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
8076             this.NotifyTbMenuItem.Checked = NotifyDispMenuItem.Checked;
8077
8078             if (string.IsNullOrEmpty(_rclickTabName)) return;
8079
8080             _statuses.Tabs[_rclickTabName].Notify = NotifyDispMenuItem.Checked;
8081
8082             SaveConfigsTabs();
8083         }
8084
8085         private void SoundFileComboBox_SelectedIndexChanged(object sender, EventArgs e)
8086         {
8087             if (soundfileListup || string.IsNullOrEmpty(_rclickTabName)) return;
8088
8089             _statuses.Tabs[_rclickTabName].SoundFile = (string)((ToolStripComboBox)sender).SelectedItem;
8090
8091             SaveConfigsTabs();
8092         }
8093
8094         private void DeleteTabMenuItem_Click(object sender, EventArgs e)
8095         {
8096             if (string.IsNullOrEmpty(_rclickTabName) || sender == this.DeleteTbMenuItem) _rclickTabName = ListTab.SelectedTab.Text;
8097
8098             RemoveSpecifiedTab(_rclickTabName, true);
8099             SaveConfigsTabs();
8100         }
8101
8102         private void FilterEditMenuItem_Click(object sender, EventArgs e)
8103         {
8104             if (string.IsNullOrEmpty(_rclickTabName)) _rclickTabName = _statuses.GetTabByType(MyCommon.TabUsageType.Home).TabName;
8105
8106             using (var fltDialog = new FilterDialog())
8107             {
8108                 fltDialog.Owner = this;
8109                 fltDialog.SetCurrent(_rclickTabName);
8110                 fltDialog.ShowDialog(this);
8111             }
8112             this.TopMost = this._cfgCommon.AlwaysTop;
8113
8114             this.ApplyPostFilters();
8115             SaveConfigsTabs();
8116         }
8117
8118         private void AddTabMenuItem_Click(object sender, EventArgs e)
8119         {
8120             string tabName = null;
8121             MyCommon.TabUsageType tabUsage;
8122             using (InputTabName inputName = new InputTabName())
8123             {
8124                 inputName.TabName = _statuses.MakeTabName("MyTab");
8125                 inputName.IsShowUsage = true;
8126                 inputName.ShowDialog();
8127                 if (inputName.DialogResult == DialogResult.Cancel) return;
8128                 tabName = inputName.TabName;
8129                 tabUsage = inputName.Usage;
8130             }
8131             this.TopMost = this._cfgCommon.AlwaysTop;
8132             if (!string.IsNullOrEmpty(tabName))
8133             {
8134                 //List対応
8135                 ListElement list = null;
8136                 if (tabUsage == MyCommon.TabUsageType.Lists)
8137                 {
8138                     using (ListAvailable listAvail = new ListAvailable())
8139                     {
8140                         if (listAvail.ShowDialog(this) == DialogResult.Cancel) return;
8141                         if (listAvail.SelectedList == null) return;
8142                         list = listAvail.SelectedList;
8143                     }
8144                 }
8145
8146                 TabModel tab;
8147                 switch (tabUsage)
8148                 {
8149                     case MyCommon.TabUsageType.UserDefined:
8150                         tab = new FilterTabModel(tabName);
8151                         break;
8152                     case MyCommon.TabUsageType.PublicSearch:
8153                         tab = new PublicSearchTabModel(tabName);
8154                         break;
8155                     case MyCommon.TabUsageType.Lists:
8156                         tab = new ListTimelineTabModel(tabName, list);
8157                         break;
8158                     default:
8159                         return;
8160                 }
8161
8162                 if (!_statuses.AddTab(tab) || !AddNewTab(tab, startup: false))
8163                 {
8164                     string tmp = string.Format(Properties.Resources.AddTabMenuItem_ClickText1, tabName);
8165                     MessageBox.Show(tmp, Properties.Resources.AddTabMenuItem_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
8166                 }
8167                 else
8168                 {
8169                     //成功
8170                     SaveConfigsTabs();
8171                     if (tabUsage == MyCommon.TabUsageType.PublicSearch)
8172                     {
8173                         ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
8174                         ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"].Focus();
8175                     }
8176                     if (tabUsage == MyCommon.TabUsageType.Lists)
8177                     {
8178                         ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
8179                         var listTab = (ListTimelineTabModel)this._statuses.Tabs[this._curTab.Text];
8180                         this.GetListTimelineAsync(listTab);
8181                     }
8182                 }
8183             }
8184         }
8185
8186         private void TabMenuItem_Click(object sender, EventArgs e)
8187         {
8188             using (var fltDialog = new FilterDialog())
8189             {
8190                 fltDialog.Owner = this;
8191
8192                 //選択発言を元にフィルタ追加
8193                 foreach (int idx in _curList.SelectedIndices)
8194                 {
8195                     string tabName;
8196                     //タブ選択(or追加)
8197                     if (!SelectTab(out tabName)) return;
8198
8199                     fltDialog.SetCurrent(tabName);
8200                     if (_statuses.Tabs[_curTab.Text][idx].RetweetedId == null)
8201                     {
8202                         fltDialog.AddNewFilter(_statuses.Tabs[_curTab.Text][idx].ScreenName, _statuses.Tabs[_curTab.Text][idx].TextFromApi);
8203                     }
8204                     else
8205                     {
8206                         fltDialog.AddNewFilter(_statuses.Tabs[_curTab.Text][idx].RetweetedBy, _statuses.Tabs[_curTab.Text][idx].TextFromApi);
8207                     }
8208                     fltDialog.ShowDialog(this);
8209                     this.TopMost = this._cfgCommon.AlwaysTop;
8210                 }
8211             }
8212
8213             this.ApplyPostFilters();
8214             SaveConfigsTabs();
8215             if (this.ListTab.SelectedTab != null &&
8216                 ((DetailsListView)this.ListTab.SelectedTab.Tag).SelectedIndices.Count > 0)
8217             {
8218                 _curPost = _statuses.Tabs[this.ListTab.SelectedTab.Text][((DetailsListView)this.ListTab.SelectedTab.Tag).SelectedIndices[0]];
8219             }
8220         }
8221
8222         protected override bool ProcessDialogKey(Keys keyData)
8223         {
8224             //TextBox1でEnterを押してもビープ音が鳴らないようにする
8225             if ((keyData & Keys.KeyCode) == Keys.Enter)
8226             {
8227                 if (StatusText.Focused)
8228                 {
8229                     bool _NewLine = false;
8230                     bool _Post = false;
8231
8232                     if (this._cfgCommon.PostCtrlEnter) //Ctrl+Enter投稿時
8233                     {
8234                         if (StatusText.Multiline)
8235                         {
8236                             if ((keyData & Keys.Shift) == Keys.Shift && (keyData & Keys.Control) != Keys.Control) _NewLine = true;
8237
8238                             if ((keyData & Keys.Control) == Keys.Control) _Post = true;
8239                         }
8240                         else
8241                         {
8242                             if (((keyData & Keys.Control) == Keys.Control)) _Post = true;
8243                         }
8244
8245                     }
8246                     else if (this._cfgCommon.PostShiftEnter) //SHift+Enter投稿時
8247                     {
8248                         if (StatusText.Multiline)
8249                         {
8250                             if ((keyData & Keys.Control) == Keys.Control && (keyData & Keys.Shift) != Keys.Shift) _NewLine = true;
8251
8252                             if ((keyData & Keys.Shift) == Keys.Shift) _Post = true;
8253                         }
8254                         else
8255                         {
8256                             if (((keyData & Keys.Shift) == Keys.Shift)) _Post = true;
8257                         }
8258
8259                     }
8260                     else //Enter投稿時
8261                     {
8262                         if (StatusText.Multiline)
8263                         {
8264                             if ((keyData & Keys.Shift) == Keys.Shift && (keyData & Keys.Control) != Keys.Control) _NewLine = true;
8265
8266                             if (((keyData & Keys.Control) != Keys.Control && (keyData & Keys.Shift) != Keys.Shift) ||
8267                                 ((keyData & Keys.Control) == Keys.Control && (keyData & Keys.Shift) == Keys.Shift)) _Post = true;
8268                         }
8269                         else
8270                         {
8271                             if (((keyData & Keys.Shift) == Keys.Shift) ||
8272                                 (((keyData & Keys.Control) != Keys.Control) &&
8273                                 ((keyData & Keys.Shift) != Keys.Shift))) _Post = true;
8274                         }
8275                     }
8276
8277                     if (_NewLine)
8278                     {
8279                         int pos1 = StatusText.SelectionStart;
8280                         if (StatusText.SelectionLength > 0)
8281                         {
8282                             StatusText.Text = StatusText.Text.Remove(pos1, StatusText.SelectionLength);  //選択状態文字列削除
8283                         }
8284                         StatusText.Text = StatusText.Text.Insert(pos1, Environment.NewLine);  //改行挿入
8285                         StatusText.SelectionStart = pos1 + Environment.NewLine.Length;    //カーソルを改行の次の文字へ移動
8286                         return true;
8287                     }
8288                     else if (_Post)
8289                     {
8290                         PostButton_Click(null, null);
8291                         return true;
8292                     }
8293                 }
8294                 else if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.PublicSearch &&
8295                          (ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"].Focused ||
8296                          ListTab.SelectedTab.Controls["panelSearch"].Controls["comboLang"].Focused))
8297                 {
8298                     this.SearchButton_Click(ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"], null);
8299                     return true;
8300                 }
8301             }
8302
8303             return base.ProcessDialogKey(keyData);
8304         }
8305
8306         private void ReplyAllStripMenuItem_Click(object sender, EventArgs e)
8307         {
8308             MakeReplyOrDirectStatus(false, true, true);
8309         }
8310
8311         private void IDRuleMenuItem_Click(object sender, EventArgs e)
8312         {
8313             //未選択なら処理終了
8314             if (_curList.SelectedIndices.Count == 0) return;
8315
8316             var tab = this._statuses.Tabs[this._curTab.Text];
8317             var screenNameArray = this._curList.SelectedIndices.Cast<int>()
8318                 .Select(x => tab[x])
8319                 .Select(x => x.RetweetedId != null ? x.RetweetedBy : x.ScreenName)
8320                 .ToArray();
8321
8322             this.AddFilterRuleByScreenName(screenNameArray);
8323
8324             if (screenNameArray.Length != 0)
8325             {
8326                 List<string> atids = new List<string>();
8327                 foreach (var screenName in screenNameArray)
8328                 {
8329                     atids.Add("@" + screenName);
8330                 }
8331                 int cnt = AtIdSupl.ItemCount;
8332                 AtIdSupl.AddRangeItem(atids.ToArray());
8333                 if (AtIdSupl.ItemCount != cnt) ModifySettingAtId = true;
8334             }
8335         }
8336
8337         private void SourceRuleMenuItem_Click(object sender, EventArgs e)
8338         {
8339             if (this._curList.SelectedIndices.Count == 0)
8340                 return;
8341
8342             var tab = this._statuses.Tabs[this._curTab.Text];
8343             var sourceArray = this._curList.SelectedIndices.Cast<int>()
8344                 .Select(x => tab[x].Source).ToArray();
8345
8346             this.AddFilterRuleBySource(sourceArray);
8347         }
8348
8349         public void AddFilterRuleByScreenName(params string[] screenNameArray)
8350         {
8351             //タブ選択(or追加)
8352             string tabName;
8353             if (!SelectTab(out tabName)) return;
8354
8355             var tab = (FilterTabModel)this._statuses.Tabs[tabName];
8356
8357             bool mv;
8358             bool mk;
8359             if (tab.TabType != MyCommon.TabUsageType.Mute)
8360             {
8361                 this.MoveOrCopy(out mv, out mk);
8362             }
8363             else
8364             {
8365                 // ミュートタブでは常に MoveMatches を true にする
8366                 mv = true;
8367                 mk = false;
8368             }
8369
8370             foreach (var screenName in screenNameArray)
8371             {
8372                 tab.AddFilter(new PostFilterRule
8373                 {
8374                     FilterName = screenName,
8375                     UseNameField = true,
8376                     MoveMatches = mv,
8377                     MarkMatches = mk,
8378                     UseRegex = false,
8379                     FilterByUrl = false,
8380                 });
8381             }
8382
8383             this.ApplyPostFilters();
8384             SaveConfigsTabs();
8385         }
8386
8387         public void AddFilterRuleBySource(params string[] sourceArray)
8388         {
8389             // タブ選択ダイアログを表示(or追加)
8390             string tabName;
8391             if (!this.SelectTab(out tabName))
8392                 return;
8393
8394             var filterTab = (FilterTabModel)this._statuses.Tabs[tabName];
8395
8396             bool mv;
8397             bool mk;
8398             if (filterTab.TabType != MyCommon.TabUsageType.Mute)
8399             {
8400                 // フィルタ動作選択ダイアログを表示(移動/コピー, マーク有無)
8401                 this.MoveOrCopy(out mv, out mk);
8402             }
8403             else
8404             {
8405                 // ミュートタブでは常に MoveMatches を true にする
8406                 mv = true;
8407                 mk = false;
8408             }
8409
8410             // 振り分けルールに追加するSource
8411             foreach (var source in sourceArray)
8412             {
8413                 filterTab.AddFilter(new PostFilterRule
8414                 {
8415                     FilterSource = source,
8416                     MoveMatches = mv,
8417                     MarkMatches = mk,
8418                     UseRegex = false,
8419                     FilterByUrl = false,
8420                 });
8421             }
8422
8423             this.ApplyPostFilters();
8424             this.SaveConfigsTabs();
8425         }
8426
8427         private bool SelectTab(out string tabName)
8428         {
8429             do
8430             {
8431                 tabName = null;
8432
8433                 //振り分け先タブ選択
8434                 using (var dialog = new TabsDialog(_statuses))
8435                 {
8436                     if (dialog.ShowDialog(this) == DialogResult.Cancel) return false;
8437
8438                     var selectedTab = dialog.SelectedTab;
8439                     tabName = selectedTab == null ? null : selectedTab.TabName;
8440                 }
8441
8442                 ListTab.SelectedTab.Focus();
8443                 //新規タブを選択→タブ作成
8444                 if (tabName == null)
8445                 {
8446                     using (InputTabName inputName = new InputTabName())
8447                     {
8448                         inputName.TabName = _statuses.MakeTabName("MyTab");
8449                         inputName.ShowDialog();
8450                         if (inputName.DialogResult == DialogResult.Cancel) return false;
8451                         tabName = inputName.TabName;
8452                     }
8453                     this.TopMost = this._cfgCommon.AlwaysTop;
8454                     if (!string.IsNullOrEmpty(tabName))
8455                     {
8456                         var tab = new FilterTabModel(tabName);
8457                         if (!_statuses.AddTab(tab) || !AddNewTab(tab, startup: false))
8458                         {
8459                             string tmp = string.Format(Properties.Resources.IDRuleMenuItem_ClickText2, tabName);
8460                             MessageBox.Show(tmp, Properties.Resources.IDRuleMenuItem_ClickText3, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
8461                             //もう一度タブ名入力
8462                         }
8463                         else
8464                         {
8465                             return true;
8466                         }
8467                     }
8468                 }
8469                 else
8470                 {
8471                     //既存タブを選択
8472                     return true;
8473                 }
8474             }
8475             while (true);
8476         }
8477
8478         private void MoveOrCopy(out bool move, out bool mark)
8479         {
8480             {
8481                 //移動するか?
8482                 string _tmp = string.Format(Properties.Resources.IDRuleMenuItem_ClickText4, Environment.NewLine);
8483                 if (MessageBox.Show(_tmp, Properties.Resources.IDRuleMenuItem_ClickText5, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
8484                     move = false;
8485                 else
8486                     move = true;
8487             }
8488             if (!move)
8489             {
8490                 //マークするか?
8491                 string _tmp = string.Format(Properties.Resources.IDRuleMenuItem_ClickText6, Environment.NewLine);
8492                 if (MessageBox.Show(_tmp, Properties.Resources.IDRuleMenuItem_ClickText7, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
8493                     mark = true;
8494                 else
8495                     mark = false;
8496             }
8497             else
8498             {
8499                 mark = false;
8500             }
8501         }
8502         private void CopySTOTMenuItem_Click(object sender, EventArgs e)
8503         {
8504             this.CopyStot();
8505         }
8506
8507         private void CopyURLMenuItem_Click(object sender, EventArgs e)
8508         {
8509             this.CopyIdUri();
8510         }
8511
8512         private void SelectAllMenuItem_Click(object sender, EventArgs e)
8513         {
8514             if (StatusText.Focused)
8515             {
8516                 // 発言欄でのCtrl+A
8517                 StatusText.SelectAll();
8518             }
8519             else
8520             {
8521                 // ListView上でのCtrl+A
8522                 NativeMethods.SelectAllItems(this._curList);
8523             }
8524         }
8525
8526         private void MoveMiddle()
8527         {
8528             ListViewItem _item;
8529             int idx1;
8530             int idx2;
8531
8532             if (_curList.SelectedIndices.Count == 0) return;
8533
8534             int idx = _curList.SelectedIndices[0];
8535
8536             _item = _curList.GetItemAt(0, 25);
8537             if (_item == null)
8538                 idx1 = 0;
8539             else
8540                 idx1 = _item.Index;
8541
8542             _item = _curList.GetItemAt(0, _curList.ClientSize.Height - 1);
8543             if (_item == null)
8544                 idx2 = _curList.VirtualListSize - 1;
8545             else
8546                 idx2 = _item.Index;
8547
8548             idx -= Math.Abs(idx1 - idx2) / 2;
8549             if (idx < 0) idx = 0;
8550
8551             _curList.EnsureVisible(_curList.VirtualListSize - 1);
8552             _curList.EnsureVisible(idx);
8553         }
8554
8555         private async void OpenURLMenuItem_Click(object sender, EventArgs e)
8556         {
8557             var linkElements = this.tweetDetailsView.GetLinkElements();
8558
8559             if (linkElements.Length > 0)
8560             {
8561                 UrlDialog.ClearUrl();
8562
8563                 string openUrlStr = "";
8564
8565                 if (linkElements.Length == 1)
8566                 {
8567                     // ツイートに含まれる URL が 1 つのみの場合
8568                     //   => OpenURL ダイアログを表示せずにリンクを開く
8569
8570                     string urlStr = "";
8571                     try
8572                     {
8573                         urlStr = MyCommon.IDNEncode(linkElements[0].GetAttribute("href"));
8574                     }
8575                     catch (ArgumentException)
8576                     {
8577                         //変なHTML?
8578                         return;
8579                     }
8580                     catch (Exception)
8581                     {
8582                         return;
8583                     }
8584                     if (string.IsNullOrEmpty(urlStr)) return;
8585                     openUrlStr = MyCommon.urlEncodeMultibyteChar(urlStr);
8586
8587                     // Ctrl+E で呼ばれた場合を考慮し isReverseSettings の判定を行わない
8588                     await this.OpenUriAsync(new Uri(openUrlStr));
8589                 }
8590                 else
8591                 {
8592                     // ツイートに含まれる URL が複数ある場合
8593                     //   => OpenURL を表示しユーザーが選択したリンクを開く
8594
8595                     foreach (var linkElm in linkElements)
8596                     {
8597                         string urlStr = "";
8598                         string linkText = "";
8599                         string href = "";
8600                         try
8601                         {
8602                             urlStr = linkElm.GetAttribute("title");
8603                             href = MyCommon.IDNEncode(linkElm.GetAttribute("href"));
8604                             if (string.IsNullOrEmpty(urlStr)) urlStr = href;
8605                             linkText = linkElm.InnerText;
8606                         }
8607                         catch (ArgumentException)
8608                         {
8609                             //変なHTML?
8610                             return;
8611                         }
8612                         catch (Exception)
8613                         {
8614                             return;
8615                         }
8616                         if (string.IsNullOrEmpty(urlStr)) continue;
8617                         UrlDialog.AddUrl(new OpenUrlItem(linkText, MyCommon.urlEncodeMultibyteChar(urlStr), href));
8618                     }
8619                     try
8620                     {
8621                         if (UrlDialog.ShowDialog() == DialogResult.OK)
8622                         {
8623                             openUrlStr = UrlDialog.SelectedUrl;
8624
8625                             // Ctrlを押しながらリンクを開いた場合は、設定と逆の動作をするフラグを true としておく
8626                             await this.OpenUriAsync(new Uri(openUrlStr), MyCommon.IsKeyDown(Keys.Control));
8627                         }
8628                     }
8629                     catch (Exception)
8630                     {
8631                         return;
8632                     }
8633                     this.TopMost = this._cfgCommon.AlwaysTop;
8634                 }
8635             }
8636         }
8637
8638         private void ClearTabMenuItem_Click(object sender, EventArgs e)
8639         {
8640             if (string.IsNullOrEmpty(_rclickTabName)) return;
8641             ClearTab(_rclickTabName, true);
8642         }
8643
8644         private void ClearTab(string tabName, bool showWarning)
8645         {
8646             if (showWarning)
8647             {
8648                 string tmp = string.Format(Properties.Resources.ClearTabMenuItem_ClickText1, Environment.NewLine);
8649                 if (MessageBox.Show(tmp, tabName + " " + Properties.Resources.ClearTabMenuItem_ClickText2, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
8650                 {
8651                     return;
8652                 }
8653             }
8654
8655             _statuses.ClearTabIds(tabName);
8656             if (ListTab.SelectedTab.Text == tabName)
8657             {
8658                 _anchorPost = null;
8659                 _anchorFlag = false;
8660                 this.PurgeListViewItemCache();
8661                 _curItemIndex = -1;
8662                 _curPost = null;
8663             }
8664             foreach (TabPage tb in ListTab.TabPages)
8665             {
8666                 if (tb.Text == tabName)
8667                 {
8668                     tb.ImageIndex = -1;
8669                     ((DetailsListView)tb.Tag).VirtualListSize = 0;
8670                     break;
8671                 }
8672             }
8673             if (!this._cfgCommon.TabIconDisp) ListTab.Refresh();
8674
8675             SetMainWindowTitle();
8676             SetStatusLabelUrl();
8677         }
8678
8679         private static long followers = 0;
8680
8681         private void SetMainWindowTitle()
8682         {
8683             //メインウインドウタイトルの書き換え
8684             StringBuilder ttl = new StringBuilder(256);
8685             int ur = 0;
8686             int al = 0;
8687             if (this._cfgCommon.DispLatestPost != MyCommon.DispTitleEnum.None &&
8688                 this._cfgCommon.DispLatestPost != MyCommon.DispTitleEnum.Post &&
8689                 this._cfgCommon.DispLatestPost != MyCommon.DispTitleEnum.Ver &&
8690                 this._cfgCommon.DispLatestPost != MyCommon.DispTitleEnum.OwnStatus)
8691             {
8692                 foreach (var tab in _statuses.Tabs.Values)
8693                 {
8694                     ur += tab.UnreadCount;
8695                     al += tab.AllCount;
8696                 }
8697             }
8698
8699             if (this._cfgCommon.DispUsername) ttl.Append(tw.Username).Append(" - ");
8700             ttl.Append(Application.ProductName);
8701             ttl.Append("  ");
8702             switch (this._cfgCommon.DispLatestPost)
8703             {
8704                 case MyCommon.DispTitleEnum.Ver:
8705                     ttl.Append("Ver:").Append(MyCommon.GetReadableVersion());
8706                     break;
8707                 case MyCommon.DispTitleEnum.Post:
8708                     if (_history != null && _history.Count > 1)
8709                         ttl.Append(_history[_history.Count - 2].status.Replace("\r\n", " "));
8710                     break;
8711                 case MyCommon.DispTitleEnum.UnreadRepCount:
8712                     ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText1, _statuses.GetTabByType(MyCommon.TabUsageType.Mentions).UnreadCount + _statuses.GetTabByType(MyCommon.TabUsageType.DirectMessage).UnreadCount);
8713                     break;
8714                 case MyCommon.DispTitleEnum.UnreadAllCount:
8715                     ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText2, ur);
8716                     break;
8717                 case MyCommon.DispTitleEnum.UnreadAllRepCount:
8718                     ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText3, ur, _statuses.GetTabByType(MyCommon.TabUsageType.Mentions).UnreadCount + _statuses.GetTabByType(MyCommon.TabUsageType.DirectMessage).UnreadCount);
8719                     break;
8720                 case MyCommon.DispTitleEnum.UnreadCountAllCount:
8721                     ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText4, ur, al);
8722                     break;
8723                 case MyCommon.DispTitleEnum.OwnStatus:
8724                     if (followers == 0 && tw.FollowersCount > 0) followers = tw.FollowersCount;
8725                     ttl.AppendFormat(Properties.Resources.OwnStatusTitle, tw.StatusesCount, tw.FriendsCount, tw.FollowersCount, tw.FollowersCount - followers);
8726                     break;
8727             }
8728
8729             try
8730             {
8731                 this.Text = ttl.ToString();
8732             }
8733             catch (AccessViolationException)
8734             {
8735                 //原因不明。ポスト内容に依存か?たまーに発生するが再現せず。
8736             }
8737         }
8738
8739         private string GetStatusLabelText()
8740         {
8741             //ステータス欄にカウント表示
8742             //タブ未読数/タブ発言数 全未読数/総発言数 (未読@+未読DM数)
8743             if (_statuses == null) return "";
8744             TabModel tbRep = _statuses.GetTabByType(MyCommon.TabUsageType.Mentions);
8745             TabModel tbDm = _statuses.GetTabByType(MyCommon.TabUsageType.DirectMessage);
8746             if (tbRep == null || tbDm == null) return "";
8747             int urat = tbRep.UnreadCount + tbDm.UnreadCount;
8748             int ur = 0;
8749             int al = 0;
8750             int tur = 0;
8751             int tal = 0;
8752             StringBuilder slbl = new StringBuilder(256);
8753             try
8754             {
8755                 foreach (var tab in _statuses.Tabs.Values)
8756                 {
8757                     ur += tab.UnreadCount;
8758                     al += tab.AllCount;
8759                     if (_curTab != null && tab.TabName.Equals(_curTab.Text))
8760                     {
8761                         tur = tab.UnreadCount;
8762                         tal = tab.AllCount;
8763                     }
8764                 }
8765             }
8766             catch (Exception)
8767             {
8768                 return "";
8769             }
8770
8771             UnreadCounter = ur;
8772             UnreadAtCounter = urat;
8773
8774             var homeTab = this._statuses.GetTabByType<HomeTabModel>();
8775
8776             slbl.AppendFormat(Properties.Resources.SetStatusLabelText1, tur, tal, ur, al, urat, _postTimestamps.Count, _favTimestamps.Count, homeTab.TweetsPerHour);
8777             if (this._cfgCommon.TimelinePeriod == 0)
8778             {
8779                 slbl.Append(Properties.Resources.SetStatusLabelText2);
8780             }
8781             else
8782             {
8783                 slbl.Append(this._cfgCommon.TimelinePeriod + Properties.Resources.SetStatusLabelText3);
8784             }
8785             return slbl.ToString();
8786         }
8787
8788         private async void TwitterApiStatus_AccessLimitUpdated(object sender, EventArgs e)
8789         {
8790             try
8791             {
8792                 if (this.InvokeRequired && !this.IsDisposed)
8793                 {
8794                     await this.InvokeAsync(() => this.TwitterApiStatus_AccessLimitUpdated(sender, e));
8795                 }
8796                 else
8797                 {
8798                     var endpointName = (e as TwitterApiStatus.AccessLimitUpdatedEventArgs).EndpointName;
8799                     SetApiStatusLabel(endpointName);
8800                 }
8801             }
8802             catch (ObjectDisposedException)
8803             {
8804                 return;
8805             }
8806             catch (InvalidOperationException)
8807             {
8808                 return;
8809             }
8810         }
8811
8812         private void SetApiStatusLabel(string endpointName = null)
8813         {
8814             if (_curTab == null)
8815             {
8816                 this.toolStripApiGauge.ApiEndpoint = null;
8817             }
8818             else
8819             {
8820                 var tabType = _statuses.Tabs[_curTab.Text].TabType;
8821
8822                 if (endpointName == null)
8823                 {
8824                     // 表示中のタブに応じて更新
8825                     switch (tabType)
8826                     {
8827                         case MyCommon.TabUsageType.Home:
8828                         case MyCommon.TabUsageType.UserDefined:
8829                             endpointName = "/statuses/home_timeline";
8830                             break;
8831
8832                         case MyCommon.TabUsageType.Mentions:
8833                             endpointName = "/statuses/mentions_timeline";
8834                             break;
8835
8836                         case MyCommon.TabUsageType.Favorites:
8837                             endpointName = "/favorites/list";
8838                             break;
8839
8840                         case MyCommon.TabUsageType.DirectMessage:
8841                             endpointName = "/direct_messages";
8842                             break;
8843
8844                         case MyCommon.TabUsageType.UserTimeline:
8845                             endpointName = "/statuses/user_timeline";
8846                             break;
8847
8848                         case MyCommon.TabUsageType.Lists:
8849                             endpointName = "/lists/statuses";
8850                             break;
8851
8852                         case MyCommon.TabUsageType.PublicSearch:
8853                             endpointName = "/search/tweets";
8854                             break;
8855
8856                         case MyCommon.TabUsageType.Related:
8857                             endpointName = "/statuses/show/:id";
8858                             break;
8859
8860                         default:
8861                             break;
8862                     }
8863
8864                     this.toolStripApiGauge.ApiEndpoint = endpointName;
8865                 }
8866                 else
8867                 {
8868                     // 表示中のタブに関連する endpoint であれば更新
8869                     var update = false;
8870
8871                     switch (endpointName)
8872                     {
8873                         case "/statuses/home_timeline":
8874                             update = tabType == MyCommon.TabUsageType.Home ||
8875                                      tabType == MyCommon.TabUsageType.UserDefined;
8876                             break;
8877
8878                         case "/statuses/mentions_timeline":
8879                             update = tabType == MyCommon.TabUsageType.Mentions;
8880                             break;
8881
8882                         case "/favorites/list":
8883                             update = tabType == MyCommon.TabUsageType.Favorites;
8884                             break;
8885
8886                         case "/direct_messages:":
8887                             update = tabType == MyCommon.TabUsageType.DirectMessage;
8888                             break;
8889
8890                         case "/statuses/user_timeline":
8891                             update = tabType == MyCommon.TabUsageType.UserTimeline;
8892                             break;
8893
8894                         case "/lists/statuses":
8895                             update = tabType == MyCommon.TabUsageType.Lists;
8896                             break;
8897
8898                         case "/search/tweets":
8899                             update = tabType == MyCommon.TabUsageType.PublicSearch;
8900                             break;
8901
8902                         case "/statuses/show/:id":
8903                             update = tabType == MyCommon.TabUsageType.Related;
8904                             break;
8905
8906                         default:
8907                             break;
8908                     }
8909
8910                     if (update)
8911                     {
8912                         this.toolStripApiGauge.ApiEndpoint = endpointName;
8913                     }
8914                 }
8915             }
8916         }
8917
8918         private void SetStatusLabelUrl()
8919         {
8920             StatusLabelUrl.Text = GetStatusLabelText();
8921         }
8922
8923         public void SetStatusLabel(string text)
8924         {
8925             StatusLabel.Text = text;
8926         }
8927
8928         private static StringBuilder ur = new StringBuilder(64);
8929
8930         private void SetNotifyIconText()
8931         {
8932             // タスクトレイアイコンのツールチップテキスト書き換え
8933             // Tween [未読/@]
8934             ur.Remove(0, ur.Length);
8935             if (this._cfgCommon.DispUsername)
8936             {
8937                 ur.Append(tw.Username);
8938                 ur.Append(" - ");
8939             }
8940             ur.Append(Application.ProductName);
8941 #if DEBUG
8942             ur.Append("(Debug Build)");
8943 #endif
8944             if (UnreadCounter != -1 && UnreadAtCounter != -1)
8945             {
8946                 ur.Append(" [");
8947                 ur.Append(UnreadCounter);
8948                 ur.Append("/@");
8949                 ur.Append(UnreadAtCounter);
8950                 ur.Append("]");
8951             }
8952             NotifyIcon1.Text = ur.ToString();
8953         }
8954
8955         internal void CheckReplyTo(string StatusText)
8956         {
8957             MatchCollection m;
8958             //ハッシュタグの保存
8959             m = Regex.Matches(StatusText, Twitter.HASHTAG, RegexOptions.IgnoreCase);
8960             string hstr = "";
8961             foreach (Match hm in m)
8962             {
8963                 if (!hstr.Contains("#" + hm.Result("$3") + " "))
8964                 {
8965                     hstr += "#" + hm.Result("$3") + " ";
8966                     HashSupl.AddItem("#" + hm.Result("$3"));
8967                 }
8968             }
8969             if (!string.IsNullOrEmpty(HashMgr.UseHash) && !hstr.Contains(HashMgr.UseHash + " "))
8970             {
8971                 hstr += HashMgr.UseHash;
8972             }
8973             if (!string.IsNullOrEmpty(hstr)) HashMgr.AddHashToHistory(hstr.Trim(), false);
8974
8975             // 本当にリプライ先指定すべきかどうかの判定
8976             m = Regex.Matches(StatusText, "(^|[ -/:-@[-^`{-~])(?<id>@[a-zA-Z0-9_]+)");
8977
8978             if (this._cfgCommon.UseAtIdSupplement)
8979             {
8980                 int bCnt = AtIdSupl.ItemCount;
8981                 foreach (Match mid in m)
8982                 {
8983                     AtIdSupl.AddItem(mid.Result("${id}"));
8984                 }
8985                 if (bCnt != AtIdSupl.ItemCount) ModifySettingAtId = true;
8986             }
8987
8988             // リプライ先ステータスIDの指定がない場合は指定しない
8989             if (this.inReplyTo == null)
8990                 return;
8991
8992             // 通常Reply
8993             // 次の条件を満たす場合に in_reply_to_status_id 指定
8994             // 1. Twitterによりリンクと判定される @idが文中に1つ含まれる (2009/5/28 リンク化される@IDのみカウントするように修正)
8995             // 2. リプライ先ステータスIDが設定されている(リストをダブルクリックで返信している)
8996             // 3. 文中に含まれた@idがリプライ先のポスト者のIDと一致する
8997
8998             if (m != null)
8999             {
9000                 var inReplyToScreenName = this.inReplyTo.Item2;
9001                 if (StatusText.StartsWith("@", StringComparison.Ordinal))
9002                 {
9003                     if (StatusText.StartsWith("@" + inReplyToScreenName, StringComparison.Ordinal)) return;
9004                 }
9005                 else
9006                 {
9007                     foreach (Match mid in m)
9008                     {
9009                         if (StatusText.Contains("RT " + mid.Result("${id}") + ":") && mid.Result("${id}") == "@" + inReplyToScreenName) return;
9010                     }
9011                 }
9012             }
9013
9014             this.inReplyTo = null;
9015         }
9016
9017         private void TweenMain_Resize(object sender, EventArgs e)
9018         {
9019             if (!_initialLayout && this._cfgCommon.MinimizeToTray && WindowState == FormWindowState.Minimized)
9020             {
9021                 this.Visible = false;
9022             }
9023             if (_initialLayout && _cfgLocal != null && this.WindowState == FormWindowState.Normal && this.Visible)
9024             {
9025                 // 現在の DPI と設定保存時の DPI との比を取得する
9026                 var configScaleFactor = this._cfgLocal.GetConfigScaleFactor(this.CurrentAutoScaleDimensions);
9027
9028                 this.ClientSize = ScaleBy(configScaleFactor, _cfgLocal.FormSize);
9029                 //_mySize = this.ClientSize;                     //サイズ保持(最小化・最大化されたまま終了した場合の対応用)
9030                 this.DesktopLocation = _cfgLocal.FormLocation;
9031                 //_myLoc = this.DesktopLocation;                        //位置保持(最小化・最大化されたまま終了した場合の対応用)
9032
9033                 // Splitterの位置設定
9034                 var splitterDistance = ScaleBy(configScaleFactor.Height, _cfgLocal.SplitterDistance);
9035                 if (splitterDistance > this.SplitContainer1.Panel1MinSize &&
9036                     splitterDistance < this.SplitContainer1.Height - this.SplitContainer1.Panel2MinSize - this.SplitContainer1.SplitterWidth)
9037                 {
9038                     this.SplitContainer1.SplitterDistance = splitterDistance;
9039                 }
9040
9041                 //発言欄複数行
9042                 StatusText.Multiline = _cfgLocal.StatusMultiline;
9043                 if (StatusText.Multiline)
9044                 {
9045                     var statusTextHeight = ScaleBy(configScaleFactor.Height, _cfgLocal.StatusTextHeight);
9046                     int dis = SplitContainer2.Height - statusTextHeight - SplitContainer2.SplitterWidth;
9047                     if (dis > SplitContainer2.Panel1MinSize && dis < SplitContainer2.Height - SplitContainer2.Panel2MinSize - SplitContainer2.SplitterWidth)
9048                     {
9049                         SplitContainer2.SplitterDistance = SplitContainer2.Height - statusTextHeight - SplitContainer2.SplitterWidth;
9050                     }
9051                     StatusText.Height = statusTextHeight;
9052                 }
9053                 else
9054                 {
9055                     if (SplitContainer2.Height - SplitContainer2.Panel2MinSize - SplitContainer2.SplitterWidth > 0)
9056                     {
9057                         SplitContainer2.SplitterDistance = SplitContainer2.Height - SplitContainer2.Panel2MinSize - SplitContainer2.SplitterWidth;
9058                     }
9059                 }
9060
9061                 var previewDistance = ScaleBy(configScaleFactor.Width, _cfgLocal.PreviewDistance);
9062                 if (previewDistance > this.SplitContainer3.Panel1MinSize && previewDistance < this.SplitContainer3.Width - this.SplitContainer3.Panel2MinSize - this.SplitContainer3.SplitterWidth)
9063                 {
9064                     this.SplitContainer3.SplitterDistance = previewDistance;
9065                 }
9066
9067                 // Panel2Collapsed は SplitterDistance の設定を終えるまで true にしない
9068                 this.SplitContainer3.Panel2Collapsed = true;
9069
9070                 _initialLayout = false;
9071             }
9072             if (this.WindowState != FormWindowState.Minimized)
9073             {
9074                 _formWindowState = this.WindowState;
9075             }
9076         }
9077
9078         private void PlaySoundMenuItem_CheckedChanged(object sender, EventArgs e)
9079         {
9080             PlaySoundMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
9081             this.PlaySoundFileMenuItem.Checked = PlaySoundMenuItem.Checked;
9082             if (PlaySoundMenuItem.Checked)
9083             {
9084                 this._cfgCommon.PlaySound = true;
9085             }
9086             else
9087             {
9088                 this._cfgCommon.PlaySound = false;
9089             }
9090             ModifySettingCommon = true;
9091         }
9092
9093         private void SplitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
9094         {
9095             if (this._initialLayout)
9096                 return;
9097
9098             int splitterDistance;
9099             switch (this.WindowState)
9100             {
9101                 case FormWindowState.Normal:
9102                     splitterDistance = this.SplitContainer1.SplitterDistance;
9103                     break;
9104                 case FormWindowState.Maximized:
9105                     // 最大化時は、通常時のウィンドウサイズに換算した SplitterDistance を算出する
9106                     var normalContainerHeight = this._mySize.Height - this.ToolStripContainer1.TopToolStripPanel.Height - this.ToolStripContainer1.BottomToolStripPanel.Height;
9107                     splitterDistance = this.SplitContainer1.SplitterDistance - (this.SplitContainer1.Height - normalContainerHeight);
9108                     splitterDistance = Math.Min(splitterDistance, normalContainerHeight - this.SplitContainer1.SplitterWidth - this.SplitContainer1.Panel2MinSize);
9109                     break;
9110                 default:
9111                     return;
9112             }
9113
9114             this._mySpDis = splitterDistance;
9115             this.ModifySettingLocal = true;
9116         }
9117
9118         private async Task doRepliedStatusOpen()
9119         {
9120             if (this.ExistCurrentPost && _curPost.InReplyToUser != null && _curPost.InReplyToStatusId != null)
9121             {
9122                 if (MyCommon.IsKeyDown(Keys.Shift))
9123                 {
9124                     await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(_curPost.InReplyToUser, _curPost.InReplyToStatusId.Value));
9125                     return;
9126                 }
9127                 if (_statuses.ContainsKey(_curPost.InReplyToStatusId.Value))
9128                 {
9129                     PostClass repPost = _statuses[_curPost.InReplyToStatusId.Value];
9130                     MessageBox.Show($"{repPost.ScreenName} / {repPost.Nickname}   ({repPost.CreatedAt})" + Environment.NewLine + repPost.TextFromApi);
9131                 }
9132                 else
9133                 {
9134                     foreach (TabModel tb in _statuses.GetTabsByType(MyCommon.TabUsageType.Lists | MyCommon.TabUsageType.PublicSearch))
9135                     {
9136                         if (tb == null || !tb.Contains(_curPost.InReplyToStatusId.Value)) break;
9137                         PostClass repPost = _statuses[_curPost.InReplyToStatusId.Value];
9138                         MessageBox.Show($"{repPost.ScreenName} / {repPost.Nickname}   ({repPost.CreatedAt})" + Environment.NewLine + repPost.TextFromApi);
9139                         return;
9140                     }
9141                     await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(_curPost.InReplyToUser, _curPost.InReplyToStatusId.Value));
9142                 }
9143             }
9144         }
9145
9146         private async void RepliedStatusOpenMenuItem_Click(object sender, EventArgs e)
9147         {
9148             await this.doRepliedStatusOpen();
9149         }
9150
9151         private void SplitContainer2_Panel2_Resize(object sender, EventArgs e)
9152         {
9153             var multiline = this.SplitContainer2.Panel2.Height > this.SplitContainer2.Panel2MinSize + 2;
9154             if (multiline != this.StatusText.Multiline)
9155             {
9156                 this.StatusText.Multiline = multiline;
9157                 MultiLineMenuItem.Checked = multiline;
9158                 ModifySettingLocal = true;
9159             }
9160         }
9161
9162         private void StatusText_MultilineChanged(object sender, EventArgs e)
9163         {
9164             if (this.StatusText.Multiline)
9165                 this.StatusText.ScrollBars = ScrollBars.Vertical;
9166             else
9167                 this.StatusText.ScrollBars = ScrollBars.None;
9168
9169             ModifySettingLocal = true;
9170         }
9171
9172         private void MultiLineMenuItem_Click(object sender, EventArgs e)
9173         {
9174             //発言欄複数行
9175             StatusText.Multiline = MultiLineMenuItem.Checked;
9176             _cfgLocal.StatusMultiline = MultiLineMenuItem.Checked;
9177             if (MultiLineMenuItem.Checked)
9178             {
9179                 if (SplitContainer2.Height - _mySpDis2 - SplitContainer2.SplitterWidth < 0)
9180                     SplitContainer2.SplitterDistance = 0;
9181                 else
9182                     SplitContainer2.SplitterDistance = SplitContainer2.Height - _mySpDis2 - SplitContainer2.SplitterWidth;
9183             }
9184             else
9185             {
9186                 SplitContainer2.SplitterDistance = SplitContainer2.Height - SplitContainer2.Panel2MinSize - SplitContainer2.SplitterWidth;
9187             }
9188             ModifySettingLocal = true;
9189         }
9190
9191         private async Task<bool> UrlConvertAsync(MyCommon.UrlConverter Converter_Type)
9192         {
9193             //t.coで投稿時自動短縮する場合は、外部サービスでの短縮禁止
9194             //if (SettingDialog.UrlConvertAuto && SettingDialog.ShortenTco) return;
9195
9196             //Converter_Type=Nicomsの場合は、nicovideoのみ短縮する
9197             //参考資料 RFC3986 Uniform Resource Identifier (URI): Generic Syntax
9198             //Appendix A.  Collected ABNF for URI
9199             //http://www.ietf.org/rfc/rfc3986.txt
9200
9201             string result = "";
9202
9203             const string nico = @"^https?://[a-z]+\.(nicovideo|niconicommons|nicolive)\.jp/[a-z]+/[a-z0-9]+$";
9204
9205             if (StatusText.SelectionLength > 0)
9206             {
9207                 string tmp = StatusText.SelectedText;
9208                 // httpから始まらない場合、ExcludeStringで指定された文字列で始まる場合は対象としない
9209                 if (tmp.StartsWith("http"))
9210                 {
9211                     // 文字列が選択されている場合はその文字列について処理
9212
9213                     //nico.ms使用、nicovideoにマッチしたら変換
9214                     if (this._cfgCommon.Nicoms && Regex.IsMatch(tmp, nico))
9215                     {
9216                         result = nicoms.Shorten(tmp);
9217                     }
9218                     else if (Converter_Type != MyCommon.UrlConverter.Nicoms)
9219                     {
9220                         //短縮URL変換 日本語を含むかもしれないのでURLエンコードする
9221                         try
9222                         {
9223                             var srcUri = new Uri(MyCommon.urlEncodeMultibyteChar(tmp));
9224                             var resultUri = await ShortUrl.Instance.ShortenUrlAsync(Converter_Type, srcUri);
9225                             result = resultUri.AbsoluteUri;
9226                         }
9227                         catch (WebApiException e)
9228                         {
9229                             this.StatusLabel.Text = Converter_Type + ":" + e.Message;
9230                             return false;
9231                         }
9232                         catch (UriFormatException e)
9233                         {
9234                             this.StatusLabel.Text = Converter_Type + ":" + e.Message;
9235                             return false;
9236                         }
9237                     }
9238                     else
9239                     {
9240                         return true;
9241                     }
9242
9243                     if (!string.IsNullOrEmpty(result))
9244                     {
9245                         urlUndo undotmp = new urlUndo();
9246
9247                         StatusText.Select(StatusText.Text.IndexOf(tmp, StringComparison.Ordinal), tmp.Length);
9248                         StatusText.SelectedText = result;
9249
9250                         //undoバッファにセット
9251                         undotmp.Before = tmp;
9252                         undotmp.After = result;
9253
9254                         if (urlUndoBuffer == null)
9255                         {
9256                             urlUndoBuffer = new List<urlUndo>();
9257                             UrlUndoToolStripMenuItem.Enabled = true;
9258                         }
9259
9260                         urlUndoBuffer.Add(undotmp);
9261                     }
9262                 }
9263             }
9264             else
9265             {
9266                 const string url = @"(?<before>(?:[^\""':!=]|^|\:))" +
9267                                    @"(?<url>(?<protocol>https?://)" +
9268                                    @"(?<domain>(?:[\.-]|[^\p{P}\s])+\.[a-z]{2,}(?::[0-9]+)?)" +
9269                                    @"(?<path>/[a-z0-9!*//();:&=+$/%#\-_.,~@]*[a-z0-9)=#/]?)?" +
9270                                    @"(?<query>\?[a-z0-9!*//();:&=+$/%#\-_.,~@?]*[a-z0-9_&=#/])?)";
9271                 // 正規表現にマッチしたURL文字列をtinyurl化
9272                 foreach (Match mt in Regex.Matches(StatusText.Text, url, RegexOptions.IgnoreCase))
9273                 {
9274                     if (StatusText.Text.IndexOf(mt.Result("${url}"), StringComparison.Ordinal) == -1) continue;
9275                     string tmp = mt.Result("${url}");
9276                     if (tmp.StartsWith("w", StringComparison.OrdinalIgnoreCase)) tmp = "http://" + tmp;
9277                     urlUndo undotmp = new urlUndo();
9278
9279                     //選んだURLを選択(?)
9280                     StatusText.Select(StatusText.Text.IndexOf(mt.Result("${url}"), StringComparison.Ordinal), mt.Result("${url}").Length);
9281
9282                     //nico.ms使用、nicovideoにマッチしたら変換
9283                     if (this._cfgCommon.Nicoms && Regex.IsMatch(tmp, nico))
9284                     {
9285                         result = nicoms.Shorten(tmp);
9286                     }
9287                     else if (Converter_Type != MyCommon.UrlConverter.Nicoms)
9288                     {
9289                         //短縮URL変換 日本語を含むかもしれないのでURLエンコードする
9290                         try
9291                         {
9292                             var srcUri = new Uri(MyCommon.urlEncodeMultibyteChar(tmp));
9293                             var resultUri = await ShortUrl.Instance.ShortenUrlAsync(Converter_Type, srcUri);
9294                             result = resultUri.AbsoluteUri;
9295                         }
9296                         catch (HttpRequestException e)
9297                         {
9298                             // 例外のメッセージが「Response status code does not indicate success: 500 (Internal Server Error).」
9299                             // のように長いので「:」が含まれていればそれ以降のみを抽出する
9300                             var message = e.Message.Split(new[] { ':' }, count: 2).Last();
9301
9302                             this.StatusLabel.Text = Converter_Type + ":" + message;
9303                             continue;
9304                         }
9305                         catch (WebApiException e)
9306                         {
9307                             this.StatusLabel.Text = Converter_Type + ":" + e.Message;
9308                             continue;
9309                         }
9310                         catch (UriFormatException e)
9311                         {
9312                             this.StatusLabel.Text = Converter_Type + ":" + e.Message;
9313                             continue;
9314                         }
9315                     }
9316                     else
9317                     {
9318                         continue;
9319                     }
9320
9321                     if (!string.IsNullOrEmpty(result))
9322                     {
9323                         StatusText.Select(StatusText.Text.IndexOf(mt.Result("${url}"), StringComparison.Ordinal), mt.Result("${url}").Length);
9324                         StatusText.SelectedText = result;
9325                         //undoバッファにセット
9326                         undotmp.Before = mt.Result("${url}");
9327                         undotmp.After = result;
9328
9329                         if (urlUndoBuffer == null)
9330                         {
9331                             urlUndoBuffer = new List<urlUndo>();
9332                             UrlUndoToolStripMenuItem.Enabled = true;
9333                         }
9334
9335                         urlUndoBuffer.Add(undotmp);
9336                     }
9337                 }
9338             }
9339
9340             return true;
9341         }
9342
9343         private void doUrlUndo()
9344         {
9345             if (urlUndoBuffer != null)
9346             {
9347                 string tmp = StatusText.Text;
9348                 foreach (urlUndo data in urlUndoBuffer)
9349                 {
9350                     tmp = tmp.Replace(data.After, data.Before);
9351                 }
9352                 StatusText.Text = tmp;
9353                 urlUndoBuffer = null;
9354                 UrlUndoToolStripMenuItem.Enabled = false;
9355                 StatusText.SelectionStart = 0;
9356                 StatusText.SelectionLength = 0;
9357             }
9358         }
9359
9360         private async void TinyURLToolStripMenuItem_Click(object sender, EventArgs e)
9361         {
9362             await UrlConvertAsync(MyCommon.UrlConverter.TinyUrl);
9363         }
9364
9365         private async void IsgdToolStripMenuItem_Click(object sender, EventArgs e)
9366         {
9367             await UrlConvertAsync(MyCommon.UrlConverter.Isgd);
9368         }
9369
9370         private async void TwurlnlToolStripMenuItem_Click(object sender, EventArgs e)
9371         {
9372             await UrlConvertAsync(MyCommon.UrlConverter.Twurl);
9373         }
9374
9375         private async void UxnuMenuItem_Click(object sender, EventArgs e)
9376         {
9377             await UrlConvertAsync(MyCommon.UrlConverter.Uxnu);
9378         }
9379
9380         private async void UrlConvertAutoToolStripMenuItem_Click(object sender, EventArgs e)
9381         {
9382             if (!await UrlConvertAsync(this._cfgCommon.AutoShortUrlFirst))
9383             {
9384                 MyCommon.UrlConverter svc = this._cfgCommon.AutoShortUrlFirst;
9385                 Random rnd = new Random();
9386                 // 前回使用した短縮URLサービス以外を選択する
9387                 do
9388                 {
9389                     svc = (MyCommon.UrlConverter)rnd.Next(System.Enum.GetNames(typeof(MyCommon.UrlConverter)).Length);
9390                 }
9391                 while (svc == this._cfgCommon.AutoShortUrlFirst || svc == MyCommon.UrlConverter.Nicoms || svc == MyCommon.UrlConverter.Unu);
9392                 await UrlConvertAsync(svc);
9393             }
9394         }
9395
9396         private void UrlUndoToolStripMenuItem_Click(object sender, EventArgs e)
9397         {
9398             doUrlUndo();
9399         }
9400
9401         private void NewPostPopMenuItem_CheckStateChanged(object sender, EventArgs e)
9402         {
9403             this.NotifyFileMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
9404             this.NewPostPopMenuItem.Checked = this.NotifyFileMenuItem.Checked;
9405             _cfgCommon.NewAllPop = NewPostPopMenuItem.Checked;
9406             ModifySettingCommon = true;
9407         }
9408
9409         private void ListLockMenuItem_CheckStateChanged(object sender, EventArgs e)
9410         {
9411             ListLockMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
9412             this.LockListFileMenuItem.Checked = ListLockMenuItem.Checked;
9413             _cfgCommon.ListLock = ListLockMenuItem.Checked;
9414             ModifySettingCommon = true;
9415         }
9416
9417         private void MenuStrip1_MenuActivate(object sender, EventArgs e)
9418         {
9419             // フォーカスがメニューに移る (MenuStrip1.Tag フラグを立てる)
9420             MenuStrip1.Tag = new Object();
9421             MenuStrip1.Select(); // StatusText がフォーカスを持っている場合 Leave が発生
9422         }
9423
9424         private void MenuStrip1_MenuDeactivate(object sender, EventArgs e)
9425         {
9426             if (this.Tag != null) // 設定された戻り先へ遷移
9427             {
9428                 if (this.Tag == this.ListTab.SelectedTab)
9429                     ((Control)this.ListTab.SelectedTab.Tag).Select();
9430                 else
9431                     ((Control)this.Tag).Select();
9432             }
9433             else // 戻り先が指定されていない (初期状態) 場合はタブに遷移
9434             {
9435                 if (ListTab.SelectedIndex > -1 && ListTab.SelectedTab.HasChildren)
9436                 {
9437                     this.Tag = ListTab.SelectedTab.Tag;
9438                     ((Control)this.Tag).Select();
9439                 }
9440             }
9441             // フォーカスがメニューに遷移したかどうかを表すフラグを降ろす
9442             MenuStrip1.Tag = null;
9443         }
9444
9445         private void MyList_ColumnReordered(object sender, ColumnReorderedEventArgs e)
9446         {
9447             DetailsListView lst = (DetailsListView)sender;
9448             if (_cfgLocal == null) return;
9449
9450             if (_iconCol)
9451             {
9452                 _cfgLocal.Width1 = lst.Columns[0].Width;
9453                 _cfgLocal.Width3 = lst.Columns[1].Width;
9454             }
9455             else
9456             {
9457                 int[] darr = new int[lst.Columns.Count];
9458                 for (int i = 0; i < lst.Columns.Count; i++)
9459                 {
9460                     darr[lst.Columns[i].DisplayIndex] = i;
9461                 }
9462                 MyCommon.MoveArrayItem(darr, e.OldDisplayIndex, e.NewDisplayIndex);
9463
9464                 for (int i = 0; i < lst.Columns.Count; i++)
9465                 {
9466                     switch (darr[i])
9467                     {
9468                         case 0:
9469                             _cfgLocal.DisplayIndex1 = i;
9470                             break;
9471                         case 1:
9472                             _cfgLocal.DisplayIndex2 = i;
9473                             break;
9474                         case 2:
9475                             _cfgLocal.DisplayIndex3 = i;
9476                             break;
9477                         case 3:
9478                             _cfgLocal.DisplayIndex4 = i;
9479                             break;
9480                         case 4:
9481                             _cfgLocal.DisplayIndex5 = i;
9482                             break;
9483                         case 5:
9484                             _cfgLocal.DisplayIndex6 = i;
9485                             break;
9486                         case 6:
9487                             _cfgLocal.DisplayIndex7 = i;
9488                             break;
9489                         case 7:
9490                             _cfgLocal.DisplayIndex8 = i;
9491                             break;
9492                     }
9493                 }
9494                 _cfgLocal.Width1 = lst.Columns[0].Width;
9495                 _cfgLocal.Width2 = lst.Columns[1].Width;
9496                 _cfgLocal.Width3 = lst.Columns[2].Width;
9497                 _cfgLocal.Width4 = lst.Columns[3].Width;
9498                 _cfgLocal.Width5 = lst.Columns[4].Width;
9499                 _cfgLocal.Width6 = lst.Columns[5].Width;
9500                 _cfgLocal.Width7 = lst.Columns[6].Width;
9501                 _cfgLocal.Width8 = lst.Columns[7].Width;
9502             }
9503             ModifySettingLocal = true;
9504             _isColumnChanged = true;
9505         }
9506
9507         private void MyList_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
9508         {
9509             DetailsListView lst = (DetailsListView)sender;
9510             if (_cfgLocal == null) return;
9511             if (_iconCol)
9512             {
9513                 if (_cfgLocal.Width1 != lst.Columns[0].Width)
9514                 {
9515                     _cfgLocal.Width1 = lst.Columns[0].Width;
9516                     ModifySettingLocal = true;
9517                     _isColumnChanged = true;
9518                 }
9519                 if (_cfgLocal.Width3 != lst.Columns[1].Width)
9520                 {
9521                     _cfgLocal.Width3 = lst.Columns[1].Width;
9522                     ModifySettingLocal = true;
9523                     _isColumnChanged = true;
9524                 }
9525             }
9526             else
9527             {
9528                 if (_cfgLocal.Width1 != lst.Columns[0].Width)
9529                 {
9530                     _cfgLocal.Width1 = lst.Columns[0].Width;
9531                     ModifySettingLocal = true;
9532                     _isColumnChanged = true;
9533                 }
9534                 if (_cfgLocal.Width2 != lst.Columns[1].Width)
9535                 {
9536                     _cfgLocal.Width2 = lst.Columns[1].Width;
9537                     ModifySettingLocal = true;
9538                     _isColumnChanged = true;
9539                 }
9540                 if (_cfgLocal.Width3 != lst.Columns[2].Width)
9541                 {
9542                     _cfgLocal.Width3 = lst.Columns[2].Width;
9543                     ModifySettingLocal = true;
9544                     _isColumnChanged = true;
9545                 }
9546                 if (_cfgLocal.Width4 != lst.Columns[3].Width)
9547                 {
9548                     _cfgLocal.Width4 = lst.Columns[3].Width;
9549                     ModifySettingLocal = true;
9550                     _isColumnChanged = true;
9551                 }
9552                 if (_cfgLocal.Width5 != lst.Columns[4].Width)
9553                 {
9554                     _cfgLocal.Width5 = lst.Columns[4].Width;
9555                     ModifySettingLocal = true;
9556                     _isColumnChanged = true;
9557                 }
9558                 if (_cfgLocal.Width6 != lst.Columns[5].Width)
9559                 {
9560                     _cfgLocal.Width6 = lst.Columns[5].Width;
9561                     ModifySettingLocal = true;
9562                     _isColumnChanged = true;
9563                 }
9564                 if (_cfgLocal.Width7 != lst.Columns[6].Width)
9565                 {
9566                     _cfgLocal.Width7 = lst.Columns[6].Width;
9567                     ModifySettingLocal = true;
9568                     _isColumnChanged = true;
9569                 }
9570                 if (_cfgLocal.Width8 != lst.Columns[7].Width)
9571                 {
9572                     _cfgLocal.Width8 = lst.Columns[7].Width;
9573                     ModifySettingLocal = true;
9574                     _isColumnChanged = true;
9575                 }
9576             }
9577             // 非表示の時にColumnChangedが呼ばれた場合はForm初期化処理中なので保存しない
9578             //if (changed)
9579             //{
9580             //    SaveConfigsLocal();
9581             //}
9582         }
9583
9584         private void SplitContainer2_SplitterMoved(object sender, SplitterEventArgs e)
9585         {
9586             if (StatusText.Multiline) _mySpDis2 = StatusText.Height;
9587             ModifySettingLocal = true;
9588         }
9589
9590         private void TweenMain_DragDrop(object sender, DragEventArgs e)
9591         {
9592             if (e.Data.GetDataPresent(DataFormats.FileDrop))
9593             {
9594                 if (!e.Data.GetDataPresent(DataFormats.Html, false))  // WebBrowserコントロールからの絵文字画像Drag&Dropは弾く
9595                 {
9596                     SelectMedia_DragDrop(e);
9597                 }
9598             }
9599             else if (e.Data.GetDataPresent("UniformResourceLocatorW"))
9600             {
9601                 var url = GetUrlFromDataObject(e.Data);
9602
9603                 string appendText;
9604                 if (url.Item2 == null)
9605                     appendText = url.Item1;
9606                 else
9607                     appendText = url.Item2 + " " + url.Item1;
9608
9609                 if (this.StatusText.TextLength == 0)
9610                     this.StatusText.Text = appendText;
9611                 else
9612                     this.StatusText.Text += " " + appendText;
9613             }
9614             else if (e.Data.GetDataPresent(DataFormats.UnicodeText))
9615             {
9616                 var text = (string)e.Data.GetData(DataFormats.UnicodeText);
9617                 if (text != null)
9618                     this.StatusText.Text += text;
9619             }
9620             else if (e.Data.GetDataPresent(DataFormats.StringFormat))
9621             {
9622                 string data = (string)e.Data.GetData(DataFormats.StringFormat, true);
9623                 if (data != null) StatusText.Text += data;
9624             }
9625         }
9626
9627         /// <summary>
9628         /// IDataObject から URL とタイトルの対を取得します
9629         /// </summary>
9630         /// <remarks>
9631         /// タイトルのみ取得できなかった場合は Value2 が null のタプルを返すことがあります。
9632         /// </remarks>
9633         /// <exception cref="ArgumentException">不正なフォーマットが入力された場合</exception>
9634         /// <exception cref="NotSupportedException">サポートされていないデータが入力された場合</exception>
9635         internal static Tuple<string, string> GetUrlFromDataObject(IDataObject data)
9636         {
9637             if (data.GetDataPresent("text/x-moz-url"))
9638             {
9639                 // Firefox, Google Chrome で利用可能
9640                 // 参照: https://developer.mozilla.org/ja/docs/DragDrop/Recommended_Drag_Types
9641
9642                 using (var stream = (MemoryStream)data.GetData("text/x-moz-url"))
9643                 {
9644                     var lines = Encoding.Unicode.GetString(stream.ToArray()).TrimEnd('\0').Split('\n');
9645                     if (lines.Length < 2)
9646                         throw new ArgumentException("不正な text/x-moz-url フォーマットです", nameof(data));
9647
9648                     return new Tuple<string, string>(lines[0], lines[1]);
9649                 }
9650             }
9651             else if (data.GetDataPresent("IESiteModeToUrl"))
9652             {
9653                 // Internet Exproler 用
9654                 // 保護モードが有効なデフォルトの IE では DragDrop イベントが発火しないため使えない
9655
9656                 using (var stream = (MemoryStream)data.GetData("IESiteModeToUrl"))
9657                 {
9658                     var lines = Encoding.Unicode.GetString(stream.ToArray()).TrimEnd('\0').Split('\0');
9659                     if (lines.Length < 2)
9660                         throw new ArgumentException("不正な IESiteModeToUrl フォーマットです", nameof(data));
9661
9662                     return new Tuple<string, string>(lines[0], lines[1]);
9663                 }
9664             }
9665             else if (data.GetDataPresent("UniformResourceLocatorW"))
9666             {
9667                 // それ以外のブラウザ向け
9668
9669                 using (var stream = (MemoryStream)data.GetData("UniformResourceLocatorW"))
9670                 {
9671                     var url = Encoding.Unicode.GetString(stream.ToArray()).TrimEnd('\0');
9672                     return new Tuple<string, string>(url, null);
9673                 }
9674             }
9675
9676             throw new NotSupportedException("サポートされていないデータ形式です: " + data.GetFormats()[0]);
9677         }
9678
9679         private void TweenMain_DragEnter(object sender, DragEventArgs e)
9680         {
9681             if (e.Data.GetDataPresent(DataFormats.FileDrop))
9682             {
9683                 if (!e.Data.GetDataPresent(DataFormats.Html, false))  // WebBrowserコントロールからの絵文字画像Drag&Dropは弾く
9684                 {
9685                     SelectMedia_DragEnter(e);
9686                     return;
9687                 }
9688             }
9689             else if (e.Data.GetDataPresent("UniformResourceLocatorW"))
9690             {
9691                 e.Effect = DragDropEffects.Copy;
9692                 return;
9693             }
9694             else if (e.Data.GetDataPresent(DataFormats.UnicodeText))
9695             {
9696                 e.Effect = DragDropEffects.Copy;
9697                 return;
9698             }
9699             else if (e.Data.GetDataPresent(DataFormats.StringFormat))
9700             {
9701                 e.Effect = DragDropEffects.Copy;
9702                 return;
9703             }
9704
9705             e.Effect = DragDropEffects.None;
9706         }
9707
9708         private void TweenMain_DragOver(object sender, DragEventArgs e)
9709         {
9710         }
9711
9712         public bool IsNetworkAvailable()
9713         {
9714             bool nw = true;
9715             nw = MyCommon.IsNetworkAvailable();
9716             _myStatusOnline = nw;
9717             return nw;
9718         }
9719
9720         public async Task OpenUriAsync(Uri uri, bool isReverseSettings = false)
9721         {
9722             var uriStr = uri.AbsoluteUri;
9723
9724             // OpenTween 内部で使用する URL
9725             if (uri.Authority == "opentween")
9726             {
9727                 await this.OpenInternalUriAsync(uri);
9728                 return;
9729             }
9730
9731             // ハッシュタグを含む Twitter 検索
9732             if (uri.Host == "twitter.com" && uri.AbsolutePath == "/search" && uri.Query.Contains("q=%23"))
9733             {
9734                 // ハッシュタグの場合は、タブで開く
9735                 var unescapedQuery = Uri.UnescapeDataString(uri.Query);
9736                 var pos = unescapedQuery.IndexOf('#');
9737                 if (pos == -1) return;
9738
9739                 var hash = unescapedQuery.Substring(pos);
9740                 this.HashSupl.AddItem(hash);
9741                 this.HashMgr.AddHashToHistory(hash.Trim(), false);
9742                 this.AddNewTabForSearch(hash);
9743                 return;
9744             }
9745
9746             // ユーザープロフィールURL
9747             // フラグが立っている場合は設定と逆の動作をする
9748             if( this._cfgCommon.OpenUserTimeline && !isReverseSettings ||
9749                 !this._cfgCommon.OpenUserTimeline && isReverseSettings )
9750             {
9751                 var userUriMatch = Regex.Match(uriStr, "^https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)$");
9752                 if (userUriMatch.Success)
9753                 {
9754                     var screenName = userUriMatch.Groups["ScreenName"].Value;
9755                     if (this.IsTwitterId(screenName))
9756                     {
9757                         this.AddNewTabForUserTimeline(screenName);
9758                         return;
9759                     }
9760                 }
9761             }
9762
9763             // どのパターンにも該当しないURL
9764             await this.OpenUriInBrowserAsync(uriStr);
9765         }
9766
9767         /// <summary>
9768         /// OpenTween 内部の機能を呼び出すための URL を開きます
9769         /// </summary>
9770         private async Task OpenInternalUriAsync(Uri uri)
9771         {
9772             // ツイートを開く (//opentween/status/:status_id)
9773             var match = Regex.Match(uri.AbsolutePath, @"^/status/(\d+)$");
9774             if (match.Success)
9775             {
9776                 var statusId = long.Parse(match.Groups[1].Value);
9777                 await this.OpenRelatedTab(statusId);
9778                 return;
9779             }
9780         }
9781
9782         public Task OpenUriInBrowserAsync(string UriString)
9783         {
9784             return Task.Run(() =>
9785             {
9786                 string myPath = UriString;
9787
9788                 try
9789                 {
9790                     var configBrowserPath = this._cfgLocal.BrowserPath;
9791                     if (!string.IsNullOrEmpty(configBrowserPath))
9792                     {
9793                         if (configBrowserPath.StartsWith("\"", StringComparison.Ordinal) && configBrowserPath.Length > 2 && configBrowserPath.IndexOf("\"", 2, StringComparison.Ordinal) > -1)
9794                         {
9795                             int sep = configBrowserPath.IndexOf("\"", 2, StringComparison.Ordinal);
9796                             string browserPath = configBrowserPath.Substring(1, sep - 1);
9797                             string arg = "";
9798                             if (sep < configBrowserPath.Length - 1)
9799                             {
9800                                 arg = configBrowserPath.Substring(sep + 1);
9801                             }
9802                             myPath = arg + " " + myPath;
9803                             System.Diagnostics.Process.Start(browserPath, myPath);
9804                         }
9805                         else
9806                         {
9807                             System.Diagnostics.Process.Start(configBrowserPath, myPath);
9808                         }
9809                     }
9810                     else
9811                     {
9812                         System.Diagnostics.Process.Start(myPath);
9813                     }
9814                 }
9815                 catch (Exception)
9816                 {
9817                     //MessageBox.Show("ブラウザの起動に失敗、またはタイムアウトしました。" + ex.ToString());
9818                 }
9819             });
9820         }
9821
9822         private void ListTabSelect(TabPage _tab)
9823         {
9824             SetListProperty();
9825
9826             this.PurgeListViewItemCache();
9827
9828             _curTab = _tab;
9829             _curList = (DetailsListView)_tab.Tag;
9830             if (_curList.SelectedIndices.Count > 0)
9831             {
9832                 _curItemIndex = _curList.SelectedIndices[0];
9833                 _curPost = GetCurTabPost(_curItemIndex);
9834             }
9835             else
9836             {
9837                 _curItemIndex = -1;
9838                 _curPost = null;
9839             }
9840
9841             _anchorPost = null;
9842             _anchorFlag = false;
9843
9844             if (_iconCol)
9845             {
9846                 ((DetailsListView)_tab.Tag).Columns[1].Text = ColumnText[2];
9847             }
9848             else
9849             {
9850                 for (int i = 0; i < _curList.Columns.Count; i++)
9851                 {
9852                     ((DetailsListView)_tab.Tag).Columns[i].Text = ColumnText[i];
9853                 }
9854             }
9855         }
9856
9857         private void ListTab_Selecting(object sender, TabControlCancelEventArgs e)
9858         {
9859             ListTabSelect(e.TabPage);
9860         }
9861
9862         private void SelectListItem(DetailsListView LView, int Index)
9863         {
9864             //単一
9865             Rectangle bnd = new Rectangle();
9866             bool flg = false;
9867             var item = LView.FocusedItem;
9868             if (item != null)
9869             {
9870                 bnd = item.Bounds;
9871                 flg = true;
9872             }
9873
9874             do
9875             {
9876                 LView.SelectedIndices.Clear();
9877             }
9878             while (LView.SelectedIndices.Count > 0);
9879             item = LView.Items[Index];
9880             item.Selected = true;
9881             item.Focused = true;
9882
9883             if (flg) LView.Invalidate(bnd);
9884         }
9885
9886         private void SelectListItem(DetailsListView LView , int[] Index, int focusedIndex, int selectionMarkIndex)
9887         {
9888             //複数
9889             Rectangle bnd = new Rectangle();
9890             bool flg = false;
9891             var item = LView.FocusedItem;
9892             if (item != null)
9893             {
9894                 bnd = item.Bounds;
9895                 flg = true;
9896             }
9897
9898             if (Index != null)
9899             {
9900                 do
9901                 {
9902                     LView.SelectedIndices.Clear();
9903                 }
9904                 while (LView.SelectedIndices.Count > 0);
9905                 LView.SelectItems(Index);
9906             }
9907             if (selectionMarkIndex > -1 && LView.VirtualListSize > selectionMarkIndex)
9908             {
9909                 LView.SelectionMark = selectionMarkIndex;
9910             }
9911             if (focusedIndex > -1 && LView.VirtualListSize > focusedIndex)
9912             {
9913                 LView.Items[focusedIndex].Focused = true;
9914             }
9915             else if (Index != null && Index.Length != 0)
9916             {
9917                 LView.Items[Index.Last()].Focused = true;
9918             }
9919
9920             if (flg) LView.Invalidate(bnd);
9921         }
9922
9923         private void StartUserStream()
9924         {
9925             tw.NewPostFromStream += tw_NewPostFromStream;
9926             tw.UserStreamStarted += tw_UserStreamStarted;
9927             tw.UserStreamStopped += tw_UserStreamStopped;
9928             tw.PostDeleted += tw_PostDeleted;
9929             tw.UserStreamEventReceived += tw_UserStreamEventArrived;
9930
9931             this.RefreshUserStreamsMenu();
9932
9933             if (this._cfgCommon.UserstreamStartup)
9934                 tw.StartUserStream();
9935         }
9936
9937         private async void TweenMain_Shown(object sender, EventArgs e)
9938         {
9939             NotifyIcon1.Visible = true;
9940
9941             if (this.IsNetworkAvailable())
9942             {
9943                 StartUserStream();
9944
9945                 var loadTasks = new List<Task>
9946                 {
9947                     this.RefreshMuteUserIdsAsync(),
9948                     this.RefreshBlockIdsAsync(),
9949                     this.RefreshNoRetweetIdsAsync(),
9950                     this.RefreshTwitterConfigurationAsync(),
9951                     this.GetHomeTimelineAsync(),
9952                     this.GetReplyAsync(),
9953                     this.GetDirectMessagesAsync(),
9954                     this.GetPublicSearchAllAsync(),
9955                     this.GetUserTimelineAllAsync(),
9956                     this.GetListTimelineAllAsync(),
9957                 };
9958
9959                 if (this._cfgCommon.StartupFollowers)
9960                     loadTasks.Add(this.RefreshFollowerIdsAsync());
9961
9962                 if (this._cfgCommon.GetFav)
9963                     loadTasks.Add(this.GetFavoritesAsync());
9964
9965                 var allTasks = Task.WhenAll(loadTasks);
9966
9967                 var i = 0;
9968                 while (true)
9969                 {
9970                     var timeout = Task.Delay(5000);
9971                     if (await Task.WhenAny(allTasks, timeout) != timeout)
9972                         break;
9973
9974                     i += 1;
9975                     if (i > 24) break; // 120秒間初期処理が終了しなかったら強制的に打ち切る
9976
9977                     if (MyCommon._endingFlag)
9978                         return;
9979                 }
9980
9981                 if (MyCommon._endingFlag) return;
9982
9983                 if (ApplicationSettings.VersionInfoUrl != null)
9984                 {
9985                     //バージョンチェック(引数:起動時チェックの場合はtrue・・・チェック結果のメッセージを表示しない)
9986                     if (this._cfgCommon.StartupVersion)
9987                         await this.CheckNewVersion(true);
9988                 }
9989                 else
9990                 {
9991                     // ApplicationSetting.cs の設定により更新チェックが無効化されている場合
9992                     this.VerUpMenuItem.Enabled = false;
9993                     this.VerUpMenuItem.Available = false;
9994                     this.ToolStripSeparator16.Available = false; // VerUpMenuItem の一つ上にあるセパレータ
9995                 }
9996
9997                 // 権限チェック read/write権限(xAuthで取得したトークン)の場合は再認証を促す
9998                 if (MyCommon.TwitterApiInfo.AccessLevel == TwitterApiAccessLevel.ReadWrite)
9999                 {
10000                     MessageBox.Show(Properties.Resources.ReAuthorizeText);
10001                     SettingStripMenuItem_Click(null, null);
10002                 }
10003
10004                 // 取得失敗の場合は再試行する
10005                 var reloadTasks = new List<Task>();
10006
10007                 if (!tw.GetFollowersSuccess && this._cfgCommon.StartupFollowers)
10008                     reloadTasks.Add(this.RefreshFollowerIdsAsync());
10009
10010                 if (!tw.GetNoRetweetSuccess)
10011                     reloadTasks.Add(this.RefreshNoRetweetIdsAsync());
10012
10013                 if (this.tw.Configuration.PhotoSizeLimit == 0)
10014                     reloadTasks.Add(this.RefreshTwitterConfigurationAsync());
10015
10016                 await Task.WhenAll(reloadTasks);
10017             }
10018
10019             _initial = false;
10020
10021             TimerTimeline.Enabled = true;
10022         }
10023
10024         private async Task doGetFollowersMenu()
10025         {
10026             await this.RefreshFollowerIdsAsync();
10027             await this.DispSelectedPost(true);
10028         }
10029
10030         private async void GetFollowersAllToolStripMenuItem_Click(object sender, EventArgs e)
10031         {
10032             await this.doGetFollowersMenu();
10033         }
10034
10035         private void ReTweetUnofficialStripMenuItem_Click(object sender, EventArgs e)
10036         {
10037             doReTweetUnofficial();
10038         }
10039
10040         private async Task doReTweetOfficial(bool isConfirm)
10041         {
10042             //公式RT
10043             if (this.ExistCurrentPost)
10044             {
10045                 if (_curPost.IsProtect)
10046                 {
10047                     MessageBox.Show("Protected.");
10048                     _DoFavRetweetFlags = false;
10049                     return;
10050                 }
10051                 if (_curList.SelectedIndices.Count > 15)
10052                 {
10053                     MessageBox.Show(Properties.Resources.RetweetLimitText);
10054                     _DoFavRetweetFlags = false;
10055                     return;
10056                 }
10057                 else if (_curList.SelectedIndices.Count > 1)
10058                 {
10059                     string QuestionText = Properties.Resources.RetweetQuestion2;
10060                     if (_DoFavRetweetFlags) QuestionText = Properties.Resources.FavoriteRetweetQuestionText1;
10061                     switch (MessageBox.Show(QuestionText, "Retweet", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
10062                     {
10063                         case DialogResult.Cancel:
10064                         case DialogResult.No:
10065                             _DoFavRetweetFlags = false;
10066                             return;
10067                     }
10068                 }
10069                 else
10070                 {
10071                     if (_curPost.IsDm || _curPost.IsMe)
10072                     {
10073                         _DoFavRetweetFlags = false;
10074                         return;
10075                     }
10076                     if (!this._cfgCommon.RetweetNoConfirm)
10077                     {
10078                         string Questiontext = Properties.Resources.RetweetQuestion1;
10079                         if (_DoFavRetweetFlags) Questiontext = Properties.Resources.FavoritesRetweetQuestionText2;
10080                         if (isConfirm && MessageBox.Show(Questiontext, "Retweet", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
10081                         {
10082                             _DoFavRetweetFlags = false;
10083                             return;
10084                         }
10085                     }
10086                 }
10087
10088                 var statusIds = new List<long>();
10089                 foreach (int idx in _curList.SelectedIndices)
10090                 {
10091                     PostClass post = GetCurTabPost(idx);
10092                     if (!post.IsMe && !post.IsProtect && !post.IsDm)
10093                         statusIds.Add(post.StatusId);
10094                 }
10095
10096                 await this.RetweetAsync(statusIds);
10097             }
10098         }
10099
10100         private async void ReTweetStripMenuItem_Click(object sender, EventArgs e)
10101         {
10102             await this.doReTweetOfficial(true);
10103         }
10104
10105         private async Task FavoritesRetweetOfficial()
10106         {
10107             if (!this.ExistCurrentPost) return;
10108             _DoFavRetweetFlags = true;
10109             var retweetTask = this.doReTweetOfficial(true);
10110             if (_DoFavRetweetFlags)
10111             {
10112                 _DoFavRetweetFlags = false;
10113                 var favoriteTask = this.FavoriteChange(true, false);
10114
10115                 await Task.WhenAll(retweetTask, favoriteTask);
10116             }
10117             else
10118             {
10119                 await retweetTask;
10120             }
10121         }
10122
10123         private async Task FavoritesRetweetUnofficial()
10124         {
10125             if (this.ExistCurrentPost && !_curPost.IsDm)
10126             {
10127                 _DoFavRetweetFlags = true;
10128                 var favoriteTask = this.FavoriteChange(true);
10129                 if (!_curPost.IsProtect && _DoFavRetweetFlags)
10130                 {
10131                     _DoFavRetweetFlags = false;
10132                     doReTweetUnofficial();
10133                 }
10134
10135                 await favoriteTask;
10136             }
10137         }
10138
10139         /// <summary>
10140         /// TweetFormatterクラスによって整形された状態のHTMLを、非公式RT用に元のツイートに復元します
10141         /// </summary>
10142         /// <param name="statusHtml">TweetFormatterによって整形された状態のHTML</param>
10143         /// <param name="multiline">trueであればBRタグを改行に、falseであればスペースに変換します</param>
10144         /// <returns>復元されたツイート本文</returns>
10145         internal static string CreateRetweetUnofficial(string statusHtml, bool multiline)
10146         {
10147             // TweetFormatterクラスによって整形された状態のHTMLを元のツイートに復元します
10148
10149             // 通常の URL
10150             statusHtml = Regex.Replace(statusHtml, "<a href=\"(?<href>.+?)\" title=\"(?<title>.+?)\">(?<text>.+?)</a>", "${title}");
10151             // メンション
10152             statusHtml = Regex.Replace(statusHtml, "<a class=\"mention\" href=\"(?<href>.+?)\">(?<text>.+?)</a>", "${text}");
10153             // ハッシュタグ
10154             statusHtml = Regex.Replace(statusHtml, "<a class=\"hashtag\" href=\"(?<href>.+?)\">(?<text>.+?)</a>", "${text}");
10155
10156             // <br> 除去
10157             if (multiline)
10158                 statusHtml = statusHtml.Replace("<br>", Environment.NewLine);
10159             else
10160                 statusHtml = statusHtml.Replace("<br>", " ");
10161
10162             // &nbsp; は本来であれば U+00A0 (NON-BREAK SPACE) に置換すべきですが、
10163             // 現状では半角スペースの代用として &nbsp; を使用しているため U+0020 に置換します
10164             statusHtml = statusHtml.Replace("&nbsp;", " ");
10165
10166             return WebUtility.HtmlDecode(statusHtml);
10167         }
10168
10169         private async void DumpPostClassToolStripMenuItem_Click(object sender, EventArgs e)
10170         {
10171             this.tweetDetailsView.DumpPostClass = this.DumpPostClassToolStripMenuItem.Checked;
10172
10173             if (_curPost != null)
10174                 await this.DispSelectedPost(true);
10175         }
10176
10177         private void MenuItemHelp_DropDownOpening(object sender, EventArgs e)
10178         {
10179             if (MyCommon.DebugBuild || MyCommon.IsKeyDown(Keys.CapsLock, Keys.Control, Keys.Shift))
10180                 DebugModeToolStripMenuItem.Visible = true;
10181             else
10182                 DebugModeToolStripMenuItem.Visible = false;
10183         }
10184
10185         private void ToolStripMenuItemUrlAutoShorten_CheckedChanged(object sender, EventArgs e)
10186         {
10187             this._cfgCommon.UrlConvertAuto = ToolStripMenuItemUrlAutoShorten.Checked;
10188         }
10189
10190         private void ContextMenuPostMode_Opening(object sender, CancelEventArgs e)
10191         {
10192             ToolStripMenuItemUrlAutoShorten.Checked = this._cfgCommon.UrlConvertAuto;
10193         }
10194
10195         private void TraceOutToolStripMenuItem_Click(object sender, EventArgs e)
10196         {
10197             if (TraceOutToolStripMenuItem.Checked)
10198                 MyCommon.TraceFlag = true;
10199             else
10200                 MyCommon.TraceFlag = false;
10201         }
10202
10203         private void TweenMain_Deactivate(object sender, EventArgs e)
10204         {
10205             //画面が非アクティブになったら、発言欄の背景色をデフォルトへ
10206             this.StatusText_Leave(StatusText, System.EventArgs.Empty);
10207         }
10208
10209         private void TabRenameMenuItem_Click(object sender, EventArgs e)
10210         {
10211             if (string.IsNullOrEmpty(_rclickTabName)) return;
10212             TabRename(ref _rclickTabName);
10213         }
10214
10215         private async void BitlyToolStripMenuItem_Click(object sender, EventArgs e)
10216         {
10217             await UrlConvertAsync(MyCommon.UrlConverter.Bitly);
10218         }
10219
10220         private async void JmpToolStripMenuItem_Click(object sender, EventArgs e)
10221         {
10222             await UrlConvertAsync(MyCommon.UrlConverter.Jmp);
10223         }
10224
10225         private async void ApiUsageInfoMenuItem_Click(object sender, EventArgs e)
10226         {
10227             TwitterApiStatus apiStatus;
10228
10229             using (var dialog = new WaitingDialog(Properties.Resources.ApiInfo6))
10230             {
10231                 var cancellationToken = dialog.EnableCancellation();
10232
10233                 try
10234                 {
10235                     var task = this.tw.GetInfoApi();
10236                     apiStatus = await dialog.WaitForAsync(this, task);
10237                 }
10238                 catch (WebApiException)
10239                 {
10240                     apiStatus = null;
10241                 }
10242
10243                 if (cancellationToken.IsCancellationRequested)
10244                     return;
10245
10246                 if (apiStatus == null)
10247                 {
10248                     MessageBox.Show(Properties.Resources.ApiInfo5, Properties.Resources.ApiInfo4, MessageBoxButtons.OK, MessageBoxIcon.Information);
10249                     return;
10250                 }
10251             }
10252
10253             using (var apiDlg = new ApiInfoDialog())
10254             {
10255                 apiDlg.ShowDialog(this);
10256             }
10257         }
10258
10259         private async void FollowCommandMenuItem_Click(object sender, EventArgs e)
10260         {
10261             var id = _curPost?.ScreenName ?? "";
10262
10263             await this.FollowCommand(id);
10264         }
10265
10266         internal async Task FollowCommand(string id)
10267         {
10268             using (var inputName = new InputTabName())
10269             {
10270                 inputName.FormTitle = "Follow";
10271                 inputName.FormDescription = Properties.Resources.FRMessage1;
10272                 inputName.TabName = id;
10273
10274                 if (inputName.ShowDialog(this) != DialogResult.OK)
10275                     return;
10276                 if (string.IsNullOrWhiteSpace(inputName.TabName))
10277                     return;
10278
10279                 id = inputName.TabName.Trim();
10280             }
10281
10282             using (var dialog = new WaitingDialog(Properties.Resources.FollowCommandText1))
10283             {
10284                 try
10285                 {
10286                     var task = this.twitterApi.FriendshipsCreate(id);
10287                     await dialog.WaitForAsync(this, task);
10288                 }
10289                 catch (WebApiException ex)
10290                 {
10291                     MessageBox.Show(Properties.Resources.FRMessage2 + ex.Message);
10292                     return;
10293                 }
10294             }
10295
10296             MessageBox.Show(Properties.Resources.FRMessage3);
10297         }
10298
10299         private async void RemoveCommandMenuItem_Click(object sender, EventArgs e)
10300         {
10301             var id = _curPost?.ScreenName ?? "";
10302
10303             await this.RemoveCommand(id, false);
10304         }
10305
10306         internal async Task RemoveCommand(string id, bool skipInput)
10307         {
10308             if (!skipInput)
10309             {
10310                 using (var inputName = new InputTabName())
10311                 {
10312                     inputName.FormTitle = "Unfollow";
10313                     inputName.FormDescription = Properties.Resources.FRMessage1;
10314                     inputName.TabName = id;
10315
10316                     if (inputName.ShowDialog(this) != DialogResult.OK)
10317                         return;
10318                     if (string.IsNullOrWhiteSpace(inputName.TabName))
10319                         return;
10320
10321                     id = inputName.TabName.Trim();
10322                 }
10323             }
10324
10325             using (var dialog = new WaitingDialog(Properties.Resources.RemoveCommandText1))
10326             {
10327                 try
10328                 {
10329                     var task = this.twitterApi.FriendshipsDestroy(id);
10330                     await dialog.WaitForAsync(this, task);
10331                 }
10332                 catch (WebApiException ex)
10333                 {
10334                     MessageBox.Show(Properties.Resources.FRMessage2 + ex.Message);
10335                     return;
10336                 }
10337             }
10338
10339             MessageBox.Show(Properties.Resources.FRMessage3);
10340         }
10341
10342         private async void FriendshipMenuItem_Click(object sender, EventArgs e)
10343         {
10344             var id = _curPost?.ScreenName ?? "";
10345
10346             await this.ShowFriendship(id);
10347         }
10348
10349         internal async Task ShowFriendship(string id)
10350         {
10351             using (var inputName = new InputTabName())
10352             {
10353                 inputName.FormTitle = "Show Friendships";
10354                 inputName.FormDescription = Properties.Resources.FRMessage1;
10355                 inputName.TabName = id;
10356
10357                 if (inputName.ShowDialog(this) != DialogResult.OK)
10358                     return;
10359                 if (string.IsNullOrWhiteSpace(inputName.TabName))
10360                     return;
10361
10362                 id = inputName.TabName.Trim();
10363             }
10364
10365             bool isFollowing, isFollowed;
10366
10367             using (var dialog = new WaitingDialog(Properties.Resources.ShowFriendshipText1))
10368             {
10369                 var cancellationToken = dialog.EnableCancellation();
10370
10371                 try
10372                 {
10373                     var task = this.twitterApi.FriendshipsShow(this.twitterApi.CurrentScreenName, id);
10374                     var friendship = await dialog.WaitForAsync(this, task);
10375
10376                     isFollowing = friendship.Relationship.Source.Following;
10377                     isFollowed = friendship.Relationship.Source.FollowedBy;
10378                 }
10379                 catch (WebApiException ex)
10380                 {
10381                     if (!cancellationToken.IsCancellationRequested)
10382                         MessageBox.Show($"Err:{ex.Message}(FriendshipsShow)");
10383                     return;
10384                 }
10385
10386                 if (cancellationToken.IsCancellationRequested)
10387                     return;
10388             }
10389
10390             string result = "";
10391             if (isFollowing)
10392             {
10393                 result = Properties.Resources.GetFriendshipInfo1 + System.Environment.NewLine;
10394             }
10395             else
10396             {
10397                 result = Properties.Resources.GetFriendshipInfo2 + System.Environment.NewLine;
10398             }
10399             if (isFollowed)
10400             {
10401                 result += Properties.Resources.GetFriendshipInfo3;
10402             }
10403             else
10404             {
10405                 result += Properties.Resources.GetFriendshipInfo4;
10406             }
10407             result = id + Properties.Resources.GetFriendshipInfo5 + System.Environment.NewLine + result;
10408             MessageBox.Show(result);
10409         }
10410
10411         internal async Task ShowFriendship(string[] ids)
10412         {
10413             foreach (string id in ids)
10414             {
10415                 bool isFollowing, isFollowed;
10416
10417                 using (var dialog = new WaitingDialog(Properties.Resources.ShowFriendshipText1))
10418                 {
10419                     var cancellationToken = dialog.EnableCancellation();
10420
10421                     try
10422                     {
10423                         var task = this.twitterApi.FriendshipsShow(this.twitterApi.CurrentScreenName, id);
10424                         var friendship = await dialog.WaitForAsync(this, task);
10425
10426                         isFollowing = friendship.Relationship.Source.Following;
10427                         isFollowed = friendship.Relationship.Source.FollowedBy;
10428                     }
10429                     catch (WebApiException ex)
10430                     {
10431                         if (!cancellationToken.IsCancellationRequested)
10432                             MessageBox.Show($"Err:{ex.Message}(FriendshipsShow)");
10433                         return;
10434                     }
10435
10436                     if (cancellationToken.IsCancellationRequested)
10437                         return;
10438                 }
10439
10440                 string result = "";
10441                 string ff = "";
10442
10443                 ff = "  ";
10444                 if (isFollowing)
10445                 {
10446                     ff += Properties.Resources.GetFriendshipInfo1;
10447                 }
10448                 else
10449                 {
10450                     ff += Properties.Resources.GetFriendshipInfo2;
10451                 }
10452
10453                 ff += System.Environment.NewLine + "  ";
10454                 if (isFollowed)
10455                 {
10456                     ff += Properties.Resources.GetFriendshipInfo3;
10457                 }
10458                 else
10459                 {
10460                     ff += Properties.Resources.GetFriendshipInfo4;
10461                 }
10462                 result += id + Properties.Resources.GetFriendshipInfo5 + System.Environment.NewLine + ff;
10463                 if (isFollowing)
10464                 {
10465                     if (MessageBox.Show(
10466                         Properties.Resources.GetFriendshipInfo7 + System.Environment.NewLine + result, Properties.Resources.GetFriendshipInfo8,
10467                         MessageBoxButtons.YesNo,
10468                         MessageBoxIcon.Question,
10469                         MessageBoxDefaultButton.Button2) == DialogResult.Yes)
10470                     {
10471                         await this.RemoveCommand(id, true);
10472                     }
10473                 }
10474                 else
10475                 {
10476                     MessageBox.Show(result);
10477                 }
10478             }
10479         }
10480
10481         private async void OwnStatusMenuItem_Click(object sender, EventArgs e)
10482         {
10483             await this.doShowUserStatus(tw.Username, false);
10484             //if (!string.IsNullOrEmpty(tw.UserInfoXml))
10485             //{
10486             //    doShowUserStatus(tw.Username, false);
10487             //}
10488             //else
10489             //{
10490             //    MessageBox.Show(Properties.Resources.ShowYourProfileText1, "Your status", MessageBoxButtons.OK, MessageBoxIcon.Information);
10491             //    return;
10492             //}
10493         }
10494
10495         // TwitterIDでない固定文字列を調べる(文字列検証のみ 実際に取得はしない)
10496         // URLから切り出した文字列を渡す
10497
10498         public bool IsTwitterId(string name)
10499         {
10500             if (this.tw.Configuration.NonUsernamePaths == null || this.tw.Configuration.NonUsernamePaths.Length == 0)
10501                 return !Regex.Match(name, @"^(about|jobs|tos|privacy|who_to_follow|download|messages)$", RegexOptions.IgnoreCase).Success;
10502             else
10503                 return !this.tw.Configuration.NonUsernamePaths.Contains(name.ToLowerInvariant());
10504         }
10505
10506         private void IdeographicSpaceToSpaceToolStripMenuItem_Click(object sender, EventArgs e)
10507         {
10508             ModifySettingCommon = true;
10509         }
10510
10511         private void ToolStripFocusLockMenuItem_CheckedChanged(object sender, EventArgs e)
10512         {
10513             ModifySettingCommon = true;
10514         }
10515
10516         private void doQuoteOfficial()
10517         {
10518             if (this.ExistCurrentPost)
10519             {
10520                 if (_curPost.IsDm ||
10521                     !StatusText.Enabled) return;
10522
10523                 if (_curPost.IsProtect)
10524                 {
10525                     MessageBox.Show("Protected.");
10526                     return;
10527                 }
10528
10529                 StatusText.Text = " " + MyCommon.GetStatusUrl(_curPost);
10530
10531                 this.inReplyTo = null;
10532
10533                 StatusText.SelectionStart = 0;
10534                 StatusText.Focus();
10535             }
10536         }
10537
10538         private void doReTweetUnofficial()
10539         {
10540             //RT @id:内容
10541             if (this.ExistCurrentPost)
10542             {
10543                 if (_curPost.IsDm || !StatusText.Enabled)
10544                     return;
10545
10546                 if (_curPost.IsProtect)
10547                 {
10548                     MessageBox.Show("Protected.");
10549                     return;
10550                 }
10551                 string rtdata = _curPost.Text;
10552                 rtdata = CreateRetweetUnofficial(rtdata, this.StatusText.Multiline);
10553
10554                 StatusText.Text = " RT @" + _curPost.ScreenName + ": " + rtdata;
10555
10556                 // 投稿時に in_reply_to_status_id を付加する
10557                 var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
10558                 var inReplyToScreenName = this._curPost.ScreenName;
10559                 this.inReplyTo = Tuple.Create(inReplyToStatusId, inReplyToScreenName);
10560
10561                 StatusText.SelectionStart = 0;
10562                 StatusText.Focus();
10563             }
10564         }
10565
10566         private void QuoteStripMenuItem_Click(object sender, EventArgs e) // Handles QuoteStripMenuItem.Click, QtOpMenuItem.Click
10567         {
10568             doQuoteOfficial();
10569         }
10570
10571         private void SearchButton_Click(object sender, EventArgs e)
10572         {
10573             //公式検索
10574             Control pnl = ((Control)sender).Parent;
10575             if (pnl == null) return;
10576             string tbName = pnl.Parent.Text;
10577             var tb = (PublicSearchTabModel)_statuses.Tabs[tbName];
10578             ComboBox cmb = (ComboBox)pnl.Controls["comboSearch"];
10579             ComboBox cmbLang = (ComboBox)pnl.Controls["comboLang"];
10580             cmb.Text = cmb.Text.Trim();
10581             // 検索式演算子 OR についてのみ大文字しか認識しないので強制的に大文字とする
10582             bool Quote = false;
10583             StringBuilder buf = new StringBuilder();
10584             char[] c = cmb.Text.ToCharArray();
10585             for (int cnt = 0; cnt < cmb.Text.Length; cnt++)
10586             {
10587                 if (cnt > cmb.Text.Length - 4)
10588                 {
10589                     buf.Append(cmb.Text.Substring(cnt));
10590                     break;
10591                 }
10592                 if (c[cnt] == '"')
10593                 {
10594                     Quote = !Quote;
10595                 }
10596                 else
10597                 {
10598                     if (!Quote && cmb.Text.Substring(cnt, 4).Equals(" or ", StringComparison.OrdinalIgnoreCase))
10599                     {
10600                         buf.Append(" OR ");
10601                         cnt += 3;
10602                         continue;
10603                     }
10604                 }
10605                 buf.Append(c[cnt]);
10606             }
10607             cmb.Text = buf.ToString();
10608
10609             var listView = (DetailsListView)pnl.Parent.Tag;
10610
10611             var queryChanged = tb.SearchWords != cmb.Text || tb.SearchLang != cmbLang.Text;
10612
10613             tb.SearchWords = cmb.Text;
10614             tb.SearchLang = cmbLang.Text;
10615             if (string.IsNullOrEmpty(cmb.Text))
10616             {
10617                 listView.Focus();
10618                 SaveConfigsTabs();
10619                 return;
10620             }
10621             if (queryChanged)
10622             {
10623                 int idx = cmb.Items.IndexOf(tb.SearchWords);
10624                 if (idx > -1) cmb.Items.RemoveAt(idx);
10625                 cmb.Items.Insert(0, tb.SearchWords);
10626                 cmb.Text = tb.SearchWords;
10627                 cmb.SelectAll();
10628                 this.PurgeListViewItemCache();
10629                 listView.VirtualListSize = 0;
10630                 _statuses.ClearTabIds(tbName);
10631                 SaveConfigsTabs();   //検索条件の保存
10632             }
10633
10634             this.GetPublicSearchAsync(tb);
10635             listView.Focus();
10636         }
10637
10638         private async void RefreshMoreStripMenuItem_Click(object sender, EventArgs e)
10639         {
10640             //もっと前を取得
10641             await this.DoRefreshMore();
10642         }
10643
10644         /// <summary>
10645         /// 指定されたタブのListTabにおける位置を返します
10646         /// </summary>
10647         /// <remarks>
10648         /// 非表示のタブについて -1 が返ることを常に考慮して下さい
10649         /// </remarks>
10650         public int GetTabPageIndex(string tabName)
10651         {
10652             var index = 0;
10653             foreach (var tabPage in this.ListTab.TabPages.Cast<TabPage>())
10654             {
10655                 if (tabPage.Text == tabName)
10656                     return index;
10657
10658                 index++;
10659             }
10660
10661             return -1;
10662         }
10663
10664         private void UndoRemoveTabMenuItem_Click(object sender, EventArgs e)
10665         {
10666             if (_statuses.RemovedTab.Count == 0)
10667             {
10668                 MessageBox.Show("There isn't removed tab.", "Undo", MessageBoxButtons.OK, MessageBoxIcon.Information);
10669                 return;
10670             }
10671             else
10672             {
10673                 DetailsListView listView = null;
10674
10675                 TabModel tb = _statuses.RemovedTab.Pop();
10676                 if (tb.TabType == MyCommon.TabUsageType.Related)
10677                 {
10678                     var relatedTab = _statuses.GetTabByType(MyCommon.TabUsageType.Related);
10679                     if (relatedTab != null)
10680                     {
10681                         // 関連発言なら既存のタブを置き換える
10682                         tb.TabName = relatedTab.TabName;
10683                         this.ClearTab(tb.TabName, false);
10684                         _statuses.Tabs[tb.TabName] = tb;
10685
10686                         for (int i = 0; i < ListTab.TabPages.Count; i++)
10687                         {
10688                             var tabPage = ListTab.TabPages[i];
10689                             if (tb.TabName == tabPage.Text)
10690                             {
10691                                 listView = (DetailsListView)tabPage.Tag;
10692                                 ListTab.SelectedIndex = i;
10693                                 break;
10694                             }
10695                         }
10696                     }
10697                     else
10698                     {
10699                         const string TabName = "Related Tweets";
10700                         string renamed = TabName;
10701                         for (int i = 2; i <= 100; i++)
10702                         {
10703                             if (!_statuses.ContainsTab(renamed)) break;
10704                             renamed = TabName + i;
10705                         }
10706                         tb.TabName = renamed;
10707
10708                         _statuses.AddTab(tb);
10709                         AddNewTab(tb, startup: false);
10710
10711                         var tabPage = ListTab.TabPages[ListTab.TabPages.Count - 1];
10712                         listView = (DetailsListView)tabPage.Tag;
10713                         ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
10714                     }
10715                 }
10716                 else
10717                 {
10718                     string renamed = tb.TabName;
10719                     for (int i = 1; i < int.MaxValue; i++)
10720                     {
10721                         if (!_statuses.ContainsTab(renamed)) break;
10722                         renamed = tb.TabName + "(" + i + ")";
10723                     }
10724                     tb.TabName = renamed;
10725
10726                     _statuses.AddTab(tb);
10727                     AddNewTab(tb, startup: false);
10728
10729                     var tabPage = ListTab.TabPages[ListTab.TabPages.Count - 1];
10730                     listView = (DetailsListView)tabPage.Tag;
10731                     ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
10732                 }
10733                 SaveConfigsTabs();
10734
10735                 if (listView != null)
10736                 {
10737                     using (ControlTransaction.Update(listView))
10738                     {
10739                         listView.VirtualListSize = tb.AllCount;
10740                     }
10741                 }
10742             }
10743         }
10744
10745         private async Task doMoveToRTHome()
10746         {
10747             if (_curList.SelectedIndices.Count > 0)
10748             {
10749                 PostClass post = GetCurTabPost(_curList.SelectedIndices[0]);
10750                 if (post.RetweetedId != null)
10751                 {
10752                     await this.OpenUriInBrowserAsync("https://twitter.com/" + GetCurTabPost(_curList.SelectedIndices[0]).RetweetedBy);
10753                 }
10754             }
10755         }
10756
10757         private async void MoveToRTHomeMenuItem_Click(object sender, EventArgs e)
10758         {
10759             await this.doMoveToRTHome();
10760         }
10761
10762         private async void ListManageUserContextToolStripMenuItem_Click(object sender, EventArgs e)
10763         {
10764             var screenName = this._curPost?.ScreenName;
10765             if (screenName != null)
10766                 await this.ListManageUserContext(screenName);
10767         }
10768
10769         public async Task ListManageUserContext(string screenName)
10770         {
10771             if (this._statuses.SubscribableLists.Count == 0)
10772             {
10773                 try
10774                 {
10775                     using (var dialog = new WaitingDialog(Properties.Resources.ListsGetting))
10776                     {
10777                         var cancellationToken = dialog.EnableCancellation();
10778
10779                         var task = this.tw.GetListsApi();
10780                         await dialog.WaitForAsync(this, task);
10781
10782                         cancellationToken.ThrowIfCancellationRequested();
10783                     }
10784                 }
10785                 catch (OperationCanceledException) { return; }
10786                 catch (WebApiException ex)
10787                 {
10788                     MessageBox.Show("Failed to get lists. (" + ex.Message + ")");
10789                     return;
10790                 }
10791             }
10792
10793             using (MyLists listSelectForm = new MyLists(screenName, this.tw))
10794             {
10795                 listSelectForm.ShowDialog(this);
10796             }
10797         }
10798
10799         private void SearchControls_Enter(object sender, EventArgs e)
10800         {
10801             Control pnl = (Control)sender;
10802             foreach (Control ctl in pnl.Controls)
10803             {
10804                 ctl.TabStop = true;
10805             }
10806         }
10807
10808         private void SearchControls_Leave(object sender, EventArgs e)
10809         {
10810             Control pnl = (Control)sender;
10811             foreach (Control ctl in pnl.Controls)
10812             {
10813                 ctl.TabStop = false;
10814             }
10815         }
10816
10817         private void PublicSearchQueryMenuItem_Click(object sender, EventArgs e)
10818         {
10819             if (ListTab.SelectedTab != null)
10820             {
10821                 if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType != MyCommon.TabUsageType.PublicSearch) return;
10822                 ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"].Focus();
10823             }
10824         }
10825
10826         private void StatusLabel_DoubleClick(object sender, EventArgs e)
10827         {
10828             MessageBox.Show(StatusLabel.TextHistory, "Logs", MessageBoxButtons.OK, MessageBoxIcon.None);
10829         }
10830
10831         private void HashManageMenuItem_Click(object sender, EventArgs e)
10832         {
10833             DialogResult rslt = DialogResult.Cancel;
10834             try
10835             {
10836                 rslt = HashMgr.ShowDialog();
10837             }
10838             catch (Exception)
10839             {
10840                 return;
10841             }
10842             this.TopMost = this._cfgCommon.AlwaysTop;
10843             if (rslt == DialogResult.Cancel) return;
10844             if (!string.IsNullOrEmpty(HashMgr.UseHash))
10845             {
10846                 HashStripSplitButton.Text = HashMgr.UseHash;
10847                 HashToggleMenuItem.Checked = true;
10848                 HashToggleToolStripMenuItem.Checked = true;
10849             }
10850             else
10851             {
10852                 HashStripSplitButton.Text = "#[-]";
10853                 HashToggleMenuItem.Checked = false;
10854                 HashToggleToolStripMenuItem.Checked = false;
10855             }
10856             //if (HashMgr.IsInsert && HashMgr.UseHash != "")
10857             //{
10858             //    int sidx = StatusText.SelectionStart;
10859             //    string hash = HashMgr.UseHash + " ";
10860             //    if (sidx > 0)
10861             //    {
10862             //        if (StatusText.Text.Substring(sidx - 1, 1) != " ")
10863             //            hash = " " + hash;
10864             //    }
10865             //    StatusText.Text = StatusText.Text.Insert(sidx, hash);
10866             //    sidx += hash.Length;
10867             //    StatusText.SelectionStart = sidx;
10868             //    StatusText.Focus();
10869             //}
10870             ModifySettingCommon = true;
10871             this.StatusText_TextChanged(null, null);
10872         }
10873
10874         private void HashToggleMenuItem_Click(object sender, EventArgs e)
10875         {
10876             HashMgr.ToggleHash();
10877             if (!string.IsNullOrEmpty(HashMgr.UseHash))
10878             {
10879                 HashStripSplitButton.Text = HashMgr.UseHash;
10880                 HashToggleMenuItem.Checked = true;
10881                 HashToggleToolStripMenuItem.Checked = true;
10882             }
10883             else
10884             {
10885                 HashStripSplitButton.Text = "#[-]";
10886                 HashToggleMenuItem.Checked = false;
10887                 HashToggleToolStripMenuItem.Checked = false;
10888             }
10889             ModifySettingCommon = true;
10890             this.StatusText_TextChanged(null, null);
10891         }
10892
10893         private void HashStripSplitButton_ButtonClick(object sender, EventArgs e)
10894         {
10895             HashToggleMenuItem_Click(null, null);
10896         }
10897
10898         public void SetPermanentHashtag(string hashtag)
10899         {
10900             HashMgr.SetPermanentHash("#" + hashtag);
10901             HashStripSplitButton.Text = HashMgr.UseHash;
10902             HashToggleMenuItem.Checked = true;
10903             HashToggleToolStripMenuItem.Checked = true;
10904             //使用ハッシュタグとして設定
10905             ModifySettingCommon = true;
10906         }
10907
10908         private void MenuItemOperate_DropDownOpening(object sender, EventArgs e)
10909         {
10910             if (ListTab.SelectedTab == null) return;
10911             if (_statuses == null || _statuses.Tabs == null || !_statuses.Tabs.ContainsKey(ListTab.SelectedTab.Text)) return;
10912             if (!this.ExistCurrentPost)
10913             {
10914                 this.ReplyOpMenuItem.Enabled = false;
10915                 this.ReplyAllOpMenuItem.Enabled = false;
10916                 this.DmOpMenuItem.Enabled = false;
10917                 this.ShowProfMenuItem.Enabled = false;
10918                 this.ShowUserTimelineToolStripMenuItem.Enabled = false;
10919                 this.ListManageMenuItem.Enabled = false;
10920                 this.OpenFavOpMenuItem.Enabled = false;
10921                 this.CreateTabRuleOpMenuItem.Enabled = false;
10922                 this.CreateIdRuleOpMenuItem.Enabled = false;
10923                 this.CreateSourceRuleOpMenuItem.Enabled = false;
10924                 this.ReadOpMenuItem.Enabled = false;
10925                 this.UnreadOpMenuItem.Enabled = false;
10926             }
10927             else
10928             {
10929                 this.ReplyOpMenuItem.Enabled = true;
10930                 this.ReplyAllOpMenuItem.Enabled = true;
10931                 this.DmOpMenuItem.Enabled = true;
10932                 this.ShowProfMenuItem.Enabled = true;
10933                 this.ShowUserTimelineToolStripMenuItem.Enabled = true;
10934                 this.ListManageMenuItem.Enabled = true;
10935                 this.OpenFavOpMenuItem.Enabled = true;
10936                 this.CreateTabRuleOpMenuItem.Enabled = true;
10937                 this.CreateIdRuleOpMenuItem.Enabled = true;
10938                 this.CreateSourceRuleOpMenuItem.Enabled = true;
10939                 this.ReadOpMenuItem.Enabled = true;
10940                 this.UnreadOpMenuItem.Enabled = true;
10941             }
10942
10943             if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.DirectMessage || !this.ExistCurrentPost || _curPost.IsDm)
10944             {
10945                 this.FavOpMenuItem.Enabled = false;
10946                 this.UnFavOpMenuItem.Enabled = false;
10947                 this.OpenStatusOpMenuItem.Enabled = false;
10948                 this.OpenFavotterOpMenuItem.Enabled = false;
10949                 this.ShowRelatedStatusesMenuItem2.Enabled = false;
10950                 this.RtOpMenuItem.Enabled = false;
10951                 this.RtUnOpMenuItem.Enabled = false;
10952                 this.QtOpMenuItem.Enabled = false;
10953                 this.FavoriteRetweetMenuItem.Enabled = false;
10954                 this.FavoriteRetweetUnofficialMenuItem.Enabled = false;
10955             }
10956             else
10957             {
10958                 this.FavOpMenuItem.Enabled = true;
10959                 this.UnFavOpMenuItem.Enabled = true;
10960                 this.OpenStatusOpMenuItem.Enabled = true;
10961                 this.OpenFavotterOpMenuItem.Enabled = true;
10962                 this.ShowRelatedStatusesMenuItem2.Enabled = true;  //PublicSearchの時問題出るかも
10963
10964                 if (_curPost.IsMe)
10965                 {
10966                     this.RtOpMenuItem.Enabled = false;  //公式RTは無効に
10967                     this.RtUnOpMenuItem.Enabled = true;
10968                     this.QtOpMenuItem.Enabled = true;
10969                     this.FavoriteRetweetMenuItem.Enabled = false;  //公式RTは無効に
10970                     this.FavoriteRetweetUnofficialMenuItem.Enabled = true;
10971                 }
10972                 else
10973                 {
10974                     if (_curPost.IsProtect)
10975                     {
10976                         this.RtOpMenuItem.Enabled = false;
10977                         this.RtUnOpMenuItem.Enabled = false;
10978                         this.QtOpMenuItem.Enabled = false;
10979                         this.FavoriteRetweetMenuItem.Enabled = false;
10980                         this.FavoriteRetweetUnofficialMenuItem.Enabled = false;
10981                     }
10982                     else
10983                     {
10984                         this.RtOpMenuItem.Enabled = true;
10985                         this.RtUnOpMenuItem.Enabled = true;
10986                         this.QtOpMenuItem.Enabled = true;
10987                         this.FavoriteRetweetMenuItem.Enabled = true;
10988                         this.FavoriteRetweetUnofficialMenuItem.Enabled = true;
10989                     }
10990                 }
10991             }
10992
10993             if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType != MyCommon.TabUsageType.Favorites)
10994             {
10995                 this.RefreshPrevOpMenuItem.Enabled = true;
10996             }
10997             else
10998             {
10999                 this.RefreshPrevOpMenuItem.Enabled = false;
11000             }
11001             if (!this.ExistCurrentPost
11002                 || _curPost.InReplyToStatusId == null)
11003             {
11004                 OpenRepSourceOpMenuItem.Enabled = false;
11005             }
11006             else
11007             {
11008                 OpenRepSourceOpMenuItem.Enabled = true;
11009             }
11010             if (!this.ExistCurrentPost || string.IsNullOrEmpty(_curPost.RetweetedBy))
11011             {
11012                 OpenRterHomeMenuItem.Enabled = false;
11013             }
11014             else
11015             {
11016                 OpenRterHomeMenuItem.Enabled = true;
11017             }
11018
11019             if (this.ExistCurrentPost)
11020             {
11021                 this.DelOpMenuItem.Enabled = this._curPost.CanDeleteBy(this.tw.UserId);
11022             }
11023         }
11024
11025         private void MenuItemTab_DropDownOpening(object sender, EventArgs e)
11026         {
11027             ContextMenuTabProperty_Opening(sender, null);
11028         }
11029
11030         public Twitter TwitterInstance
11031         {
11032             get { return tw; }
11033         }
11034
11035         private void SplitContainer3_SplitterMoved(object sender, SplitterEventArgs e)
11036         {
11037             if (this._initialLayout)
11038                 return;
11039
11040             int splitterDistance;
11041             switch (this.WindowState)
11042             {
11043                 case FormWindowState.Normal:
11044                     splitterDistance = this.SplitContainer3.SplitterDistance;
11045                     break;
11046                 case FormWindowState.Maximized:
11047                     // 最大化時は、通常時のウィンドウサイズに換算した SplitterDistance を算出する
11048                     var normalContainerWidth = this._mySize.Width - SystemInformation.Border3DSize.Width * 2;
11049                     splitterDistance = this.SplitContainer3.SplitterDistance - (this.SplitContainer3.Width - normalContainerWidth);
11050                     splitterDistance = Math.Min(splitterDistance, normalContainerWidth - this.SplitContainer3.SplitterWidth - this.SplitContainer3.Panel2MinSize);
11051                     break;
11052                 default:
11053                     return;
11054             }
11055
11056             this._mySpDis3 = splitterDistance;
11057             this.ModifySettingLocal = true;
11058         }
11059
11060         private void MenuItemEdit_DropDownOpening(object sender, EventArgs e)
11061         {
11062             if (_statuses.RemovedTab.Count == 0)
11063             {
11064                 UndoRemoveTabMenuItem.Enabled = false;
11065             }
11066             else
11067             {
11068                 UndoRemoveTabMenuItem.Enabled = true;
11069             }
11070             if (ListTab.SelectedTab != null)
11071             {
11072                 if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.PublicSearch)
11073                     PublicSearchQueryMenuItem.Enabled = true;
11074                 else
11075                     PublicSearchQueryMenuItem.Enabled = false;
11076             }
11077             else
11078             {
11079                 PublicSearchQueryMenuItem.Enabled = false;
11080             }
11081             if (!this.ExistCurrentPost)
11082             {
11083                 this.CopySTOTMenuItem.Enabled = false;
11084                 this.CopyURLMenuItem.Enabled = false;
11085                 this.CopyUserIdStripMenuItem.Enabled = false;
11086             }
11087             else
11088             {
11089                 this.CopySTOTMenuItem.Enabled = true;
11090                 this.CopyURLMenuItem.Enabled = true;
11091                 this.CopyUserIdStripMenuItem.Enabled = true;
11092                 if (_curPost.IsDm) this.CopyURLMenuItem.Enabled = false;
11093                 if (_curPost.IsProtect) this.CopySTOTMenuItem.Enabled = false;
11094             }
11095         }
11096
11097         private void NotifyIcon1_MouseMove(object sender, MouseEventArgs e)
11098         {
11099             SetNotifyIconText();
11100         }
11101
11102         private async void UserStatusToolStripMenuItem_Click(object sender, EventArgs e)
11103         {
11104             var id = _curPost?.ScreenName ?? "";
11105
11106             await this.ShowUserStatus(id);
11107         }
11108
11109         private async Task doShowUserStatus(string id, bool ShowInputDialog)
11110         {
11111             TwitterUser user = null;
11112
11113             if (ShowInputDialog)
11114             {
11115                 using (var inputName = new InputTabName())
11116                 {
11117                     inputName.FormTitle = "Show UserStatus";
11118                     inputName.FormDescription = Properties.Resources.FRMessage1;
11119                     inputName.TabName = id;
11120
11121                     if (inputName.ShowDialog(this) != DialogResult.OK)
11122                         return;
11123                     if (string.IsNullOrWhiteSpace(inputName.TabName))
11124                         return;
11125
11126                     id = inputName.TabName.Trim();
11127                 }
11128             }
11129
11130             using (var dialog = new WaitingDialog(Properties.Resources.doShowUserStatusText1))
11131             {
11132                 var cancellationToken = dialog.EnableCancellation();
11133
11134                 try
11135                 {
11136                     var task = this.twitterApi.UsersShow(id);
11137                     user = await dialog.WaitForAsync(this, task);
11138                 }
11139                 catch (WebApiException ex)
11140                 {
11141                     if (!cancellationToken.IsCancellationRequested)
11142                         MessageBox.Show($"Err:{ex.Message}(UsersShow)");
11143                     return;
11144                 }
11145
11146                 if (cancellationToken.IsCancellationRequested)
11147                     return;
11148             }
11149
11150             await this.doShowUserStatus(user);
11151         }
11152
11153         private async Task doShowUserStatus(TwitterUser user)
11154         {
11155             using (var userDialog = new UserInfoDialog(this, this.twitterApi))
11156             {
11157                 var showUserTask = userDialog.ShowUserAsync(user);
11158                 userDialog.ShowDialog(this);
11159
11160                 this.Activate();
11161                 this.BringToFront();
11162
11163                 // ユーザー情報の表示が完了するまで userDialog を破棄しない
11164                 await showUserTask;
11165             }
11166         }
11167
11168         internal Task ShowUserStatus(string id, bool ShowInputDialog)
11169         {
11170             return this.doShowUserStatus(id, ShowInputDialog);
11171         }
11172
11173         internal Task ShowUserStatus(string id)
11174         {
11175             return this.doShowUserStatus(id, true);
11176         }
11177
11178         private async void ShowProfileMenuItem_Click(object sender, EventArgs e)
11179         {
11180             if (_curPost != null)
11181             {
11182                 await this.ShowUserStatus(_curPost.ScreenName, false);
11183             }
11184         }
11185
11186         private async void RtCountMenuItem_Click(object sender, EventArgs e)
11187         {
11188             if (!this.ExistCurrentPost)
11189                 return;
11190
11191             var statusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
11192             TwitterStatus status;
11193
11194             using (var dialog = new WaitingDialog(Properties.Resources.RtCountMenuItem_ClickText1))
11195             {
11196                 var cancellationToken = dialog.EnableCancellation();
11197
11198                 try
11199                 {
11200                     var task = this.twitterApi.StatusesShow(statusId);
11201                     status = await dialog.WaitForAsync(this, task);
11202                 }
11203                 catch (WebApiException ex)
11204                 {
11205                     if (!cancellationToken.IsCancellationRequested)
11206                         MessageBox.Show(Properties.Resources.RtCountText2 + Environment.NewLine + "Err:" + ex.Message);
11207                     return;
11208                 }
11209
11210                 if (cancellationToken.IsCancellationRequested)
11211                     return;
11212             }
11213
11214             MessageBox.Show(status.RetweetCount + Properties.Resources.RtCountText1);
11215         }
11216
11217         private HookGlobalHotkey _hookGlobalHotkey;
11218         public TweenMain()
11219         {
11220             _hookGlobalHotkey = new HookGlobalHotkey(this);
11221
11222             // この呼び出しは、Windows フォーム デザイナで必要です。
11223             InitializeComponent();
11224
11225             // InitializeComponent() 呼び出しの後で初期化を追加します。
11226
11227             if (!this.DesignMode)
11228             {
11229                 // デザイナでの編集時にレイアウトが縦方向に数pxずれる問題の対策
11230                 this.StatusText.Dock = DockStyle.Fill;
11231             }
11232
11233             this.tweetDetailsView.Owner = this;
11234
11235             this.TimerTimeline.Elapsed += this.TimerTimeline_Elapsed;
11236             this._hookGlobalHotkey.HotkeyPressed += _hookGlobalHotkey_HotkeyPressed;
11237             this.gh.NotifyClicked += GrowlHelper_Callback;
11238
11239             // メイリオフォント指定時にタブの最小幅が広くなる問題の対策
11240             this.ListTab.HandleCreated += (s, e) => NativeMethods.SetMinTabWidth((TabControl)s, 40);
11241
11242             this.ImageSelector.Visible = false;
11243             this.ImageSelector.Enabled = false;
11244             this.ImageSelector.FilePickDialog = OpenFileDialog1;
11245
11246             this.workerProgress = new Progress<string>(x => this.StatusLabel.Text = x);
11247
11248             this.ReplaceAppName();
11249             this.InitializeShortcuts();
11250         }
11251
11252         private void _hookGlobalHotkey_HotkeyPressed(object sender, KeyEventArgs e)
11253         {
11254             if ((this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized) && this.Visible && Form.ActiveForm == this)
11255             {
11256                 //アイコン化
11257                 this.Visible = false;
11258             }
11259             else if (Form.ActiveForm == null)
11260             {
11261                 this.Visible = true;
11262                 if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;
11263                 this.Activate();
11264                 this.BringToFront();
11265                 this.StatusText.Focus();
11266             }
11267         }
11268
11269         private void SplitContainer2_MouseDoubleClick(object sender, MouseEventArgs e)
11270         {
11271             this.MultiLineMenuItem.PerformClick();
11272         }
11273
11274         public PostClass CurPost
11275         {
11276             get { return _curPost; }
11277         }
11278
11279 #region "画像投稿"
11280         private void ImageSelectMenuItem_Click(object sender, EventArgs e)
11281         {
11282             if (ImageSelector.Visible)
11283                 ImageSelector.EndSelection();
11284             else
11285                 ImageSelector.BeginSelection();
11286         }
11287
11288         private void SelectMedia_DragEnter(DragEventArgs e)
11289         {
11290             if (ImageSelector.HasUploadableService(((string[])e.Data.GetData(DataFormats.FileDrop, false))[0], true))
11291             {
11292                 e.Effect = DragDropEffects.Copy;
11293                 return;
11294             }
11295             e.Effect = DragDropEffects.None;
11296         }
11297
11298         private void SelectMedia_DragDrop(DragEventArgs e)
11299         {
11300             this.Activate();
11301             this.BringToFront();
11302             ImageSelector.BeginSelection((string[])e.Data.GetData(DataFormats.FileDrop, false));
11303             StatusText.Focus();
11304         }
11305
11306         private void ImageSelector_BeginSelecting(object sender, EventArgs e)
11307         {
11308             TimelinePanel.Visible = false;
11309             TimelinePanel.Enabled = false;
11310         }
11311
11312         private void ImageSelector_EndSelecting(object sender, EventArgs e)
11313         {
11314             TimelinePanel.Visible = true;
11315             TimelinePanel.Enabled = true;
11316             ((DetailsListView)ListTab.SelectedTab.Tag).Focus();
11317         }
11318
11319         private void ImageSelector_FilePickDialogOpening(object sender, EventArgs e)
11320         {
11321             this.AllowDrop = false;
11322         }
11323
11324         private void ImageSelector_FilePickDialogClosed(object sender, EventArgs e)
11325         {
11326             this.AllowDrop = true;
11327         }
11328
11329         private void ImageSelector_SelectedServiceChanged(object sender, EventArgs e)
11330         {
11331             if (ImageSelector.Visible)
11332             {
11333                 ModifySettingCommon = true;
11334                 SaveConfigsAll(true);
11335
11336                 if (ImageSelector.ServiceName.Equals("Twitter"))
11337                     this.StatusText_TextChanged(null, null);
11338             }
11339         }
11340
11341         private void ImageSelector_VisibleChanged(object sender, EventArgs e)
11342         {
11343             this.StatusText_TextChanged(null, null);
11344         }
11345
11346         /// <summary>
11347         /// StatusTextでCtrl+Vが押下された時の処理
11348         /// </summary>
11349         private void ProcClipboardFromStatusTextWhenCtrlPlusV()
11350         {
11351             if (Clipboard.ContainsText())
11352             {
11353                 // clipboardにテキストがある場合は貼り付け処理
11354                 this.StatusText.Paste(Clipboard.GetText());
11355             }
11356             else if (Clipboard.ContainsImage())
11357             {
11358                 // 画像があるので投稿処理を行う
11359                 if (MessageBox.Show(Properties.Resources.PostPictureConfirm3,
11360                                    Properties.Resources.PostPictureWarn4,
11361                                    MessageBoxButtons.OKCancel,
11362                                    MessageBoxIcon.Question,
11363                                    MessageBoxDefaultButton.Button2)
11364                                == DialogResult.OK)
11365                 {
11366                     // clipboardから画像を取得
11367                     using (var image = Clipboard.GetImage())
11368                     {
11369                         this.ImageSelector.BeginSelection(image);
11370                     }
11371                 }
11372             }
11373         }
11374 #endregion
11375
11376         private void ListManageToolStripMenuItem_Click(object sender, EventArgs e)
11377         {
11378             using (ListManage form = new ListManage(tw))
11379             {
11380                 form.ShowDialog(this);
11381             }
11382         }
11383
11384         public bool ModifySettingCommon { get; set; }
11385         public bool ModifySettingLocal { get; set; }
11386         public bool ModifySettingAtId { get; set; }
11387
11388         private void MenuItemCommand_DropDownOpening(object sender, EventArgs e)
11389         {
11390             if (this.ExistCurrentPost && !_curPost.IsDm)
11391                 RtCountMenuItem.Enabled = true;
11392             else
11393                 RtCountMenuItem.Enabled = false;
11394
11395             //if (SettingDialog.UrlConvertAuto && SettingDialog.ShortenTco)
11396             //    TinyUrlConvertToolStripMenuItem.Enabled = false;
11397             //else
11398             //    TinyUrlConvertToolStripMenuItem.Enabled = true;
11399         }
11400
11401         private void CopyUserIdStripMenuItem_Click(object sender, EventArgs e)
11402         {
11403             CopyUserId();
11404         }
11405
11406         private void CopyUserId()
11407         {
11408             if (_curPost == null) return;
11409             string clstr = _curPost.ScreenName;
11410             try
11411             {
11412                 Clipboard.SetDataObject(clstr, false, 5, 100);
11413             }
11414             catch (Exception ex)
11415             {
11416                 MessageBox.Show(ex.Message);
11417             }
11418         }
11419
11420         private async void ShowRelatedStatusesMenuItem_Click(object sender, EventArgs e)
11421         {
11422             if (this.ExistCurrentPost && !_curPost.IsDm)
11423             {
11424                 try
11425                 {
11426                     await this.OpenRelatedTab(this._curPost);
11427                 }
11428                 catch (TabException ex)
11429                 {
11430                     MessageBox.Show(this, ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
11431                 }
11432             }
11433         }
11434
11435         /// <summary>
11436         /// 指定されたツイートに対する関連発言タブを開きます
11437         /// </summary>
11438         /// <param name="statusId">表示するツイートのID</param>
11439         /// <exception cref="TabException">名前の重複が多すぎてタブを作成できない場合</exception>
11440         private async Task OpenRelatedTab(long statusId)
11441         {
11442             var post = this._statuses[statusId];
11443             if (post == null)
11444             {
11445                 try
11446                 {
11447                     post = await this.tw.GetStatusApi(false, statusId);
11448                 }
11449                 catch (WebApiException ex)
11450                 {
11451                     this.StatusLabel.Text = $"Err:{ex.Message}(GetStatus)";
11452                     return;
11453                 }
11454             }
11455
11456             await this.OpenRelatedTab(post);
11457         }
11458
11459         /// <summary>
11460         /// 指定されたツイートに対する関連発言タブを開きます
11461         /// </summary>
11462         /// <param name="post">表示する対象となるツイート</param>
11463         /// <exception cref="TabException">名前の重複が多すぎてタブを作成できない場合</exception>
11464         private async Task OpenRelatedTab(PostClass post)
11465         {
11466             var tabRelated = this._statuses.GetTabByType<RelatedPostsTabModel>();
11467             if (tabRelated != null)
11468             {
11469                 this.RemoveSpecifiedTab(tabRelated.TabName, confirm: false);
11470             }
11471
11472             var tabName = this._statuses.MakeTabName("Related Tweets");
11473
11474             tabRelated = new RelatedPostsTabModel(tabName, post);
11475             tabRelated.UnreadManage = false;
11476             tabRelated.Notify = false;
11477
11478             this._statuses.AddTab(tabRelated);
11479             this.AddNewTab(tabRelated, startup: false);
11480
11481             TabPage tabPage;
11482             for (int i = 0; i < this.ListTab.TabPages.Count; i++)
11483             {
11484                 tabPage = this.ListTab.TabPages[i];
11485                 if (tabName == tabPage.Text)
11486                 {
11487                     this.ListTab.SelectedIndex = i;
11488                     break;
11489                 }
11490             }
11491
11492             await this.GetRelatedTweetsAsync(tabRelated);
11493
11494             tabPage = this.ListTab.TabPages.Cast<TabPage>()
11495                 .FirstOrDefault(x => x.Text == tabRelated.TabName);
11496
11497             if (tabPage != null)
11498             {
11499                 // TODO: 非同期更新中にタブが閉じられている場合を厳密に考慮したい
11500
11501                 var listView = (DetailsListView)tabPage.Tag;
11502                 var targetPost = tabRelated.TargetPost;
11503                 var index = tabRelated.IndexOf(targetPost.RetweetedId ?? targetPost.StatusId);
11504
11505                 if (index != -1 && index < listView.Items.Count)
11506                 {
11507                     listView.SelectedIndices.Add(index);
11508                     listView.Items[index].Focused = true;
11509                 }
11510             }
11511         }
11512
11513         private void CacheInfoMenuItem_Click(object sender, EventArgs e)
11514         {
11515             StringBuilder buf = new StringBuilder();
11516             //buf.AppendFormat("キャッシュメモリ容量         : {0}bytes({1}MB)" + Environment.NewLine, IconCache.CacheMemoryLimit, ((ImageDictionary)IconCache).CacheMemoryLimit / 1048576);
11517             //buf.AppendFormat("物理メモリ使用割合           : {0}%" + Environment.NewLine, IconCache.PhysicalMemoryLimit);
11518             buf.AppendFormat("キャッシュエントリ保持数     : {0}" + Environment.NewLine, IconCache.CacheCount);
11519             buf.AppendFormat("キャッシュエントリ破棄数     : {0}" + Environment.NewLine, IconCache.CacheRemoveCount);
11520             MessageBox.Show(buf.ToString(), "アイコンキャッシュ使用状況");
11521         }
11522
11523         private void tw_UserIdChanged()
11524         {
11525             this.ModifySettingCommon = true;
11526         }
11527
11528 #region "Userstream"
11529         private void tw_PostDeleted(object sender, PostDeletedEventArgs e)
11530         {
11531             try
11532             {
11533                 if (InvokeRequired && !IsDisposed)
11534                 {
11535                     Invoke((Action) (async () =>
11536                            {
11537                                this._statuses.RemovePostFromAllTabs(e.StatusId, setIsDeleted: true);
11538                                if (_curTab != null && _statuses.Tabs[_curTab.Text].Contains(e.StatusId))
11539                                {
11540                                    this.PurgeListViewItemCache();
11541                                    ((DetailsListView)_curTab.Tag).Update();
11542                                    if (_curPost != null && _curPost.StatusId == e.StatusId)
11543                                        await this.DispSelectedPost(true);
11544                                }
11545                            }));
11546                     return;
11547                 }
11548             }
11549             catch (ObjectDisposedException)
11550             {
11551                 return;
11552             }
11553             catch (InvalidOperationException)
11554             {
11555                 return;
11556             }
11557         }
11558
11559         private int userStreamsRefreshing = 0;
11560
11561         private async void tw_NewPostFromStream(object sender, EventArgs e)
11562         {
11563             if (this._cfgCommon.ReadOldPosts)
11564             {
11565                 _statuses.SetReadHomeTab(); //新着時未読クリア
11566             }
11567
11568             this._statuses.DistributePosts();
11569
11570             if (this._cfgCommon.UserstreamPeriod > 0) return;
11571
11572             // userStreamsRefreshing が 0 (インクリメント後は1) であれば RefreshTimeline を実行
11573             if (Interlocked.Increment(ref this.userStreamsRefreshing) == 1)
11574             {
11575                 try
11576                 {
11577                     await this.InvokeAsync(() => this.RefreshTimeline())
11578                         .ConfigureAwait(false);
11579                 }
11580                 finally
11581                 {
11582                     Interlocked.Exchange(ref this.userStreamsRefreshing, 0);
11583                 }
11584             }
11585         }
11586
11587         private void tw_UserStreamStarted(object sender, EventArgs e)
11588         {
11589             try
11590             {
11591                 if (InvokeRequired && !IsDisposed)
11592                 {
11593                     Invoke((Action)(() => this.tw_UserStreamStarted(sender, e)));
11594                     return;
11595                 }
11596             }
11597             catch (ObjectDisposedException)
11598             {
11599                 return;
11600             }
11601             catch (InvalidOperationException)
11602             {
11603                 return;
11604             }
11605
11606             this.RefreshUserStreamsMenu();
11607             this.MenuItemUserStream.Enabled = true;
11608
11609             StatusLabel.Text = "UserStream Started.";
11610         }
11611
11612         private void tw_UserStreamStopped(object sender, EventArgs e)
11613         {
11614             try
11615             {
11616                 if (InvokeRequired && !IsDisposed)
11617                 {
11618                     Invoke((Action)(() => this.tw_UserStreamStopped(sender, e)));
11619                     return;
11620                 }
11621             }
11622             catch (ObjectDisposedException)
11623             {
11624                 return;
11625             }
11626             catch (InvalidOperationException)
11627             {
11628                 return;
11629             }
11630
11631             this.RefreshUserStreamsMenu();
11632             this.MenuItemUserStream.Enabled = true;
11633
11634             StatusLabel.Text = "UserStream Stopped.";
11635         }
11636
11637         private void RefreshUserStreamsMenu()
11638         {
11639             if (this.tw.UserStreamActive)
11640             {
11641                 this.MenuItemUserStream.Text = "&UserStream ▶";
11642                 this.StopToolStripMenuItem.Text = "&Stop";
11643             }
11644             else
11645             {
11646                 this.MenuItemUserStream.Text = "&UserStream ■";
11647                 this.StopToolStripMenuItem.Text = "&Start";
11648             }
11649         }
11650
11651         private void tw_UserStreamEventArrived(object sender, UserStreamEventReceivedEventArgs e)
11652         {
11653             try
11654             {
11655                 if (InvokeRequired && !IsDisposed)
11656                 {
11657                     Invoke((Action)(() => this.tw_UserStreamEventArrived(sender, e)));
11658                     return;
11659                 }
11660             }
11661             catch (ObjectDisposedException)
11662             {
11663                 return;
11664             }
11665             catch (InvalidOperationException)
11666             {
11667                 return;
11668             }
11669             var ev = e.EventData;
11670             StatusLabel.Text = "Event: " + ev.Event;
11671             //if (ev.Event == "favorite")
11672             //{
11673             //    NotifyFavorite(ev);
11674             //}
11675             NotifyEvent(ev);
11676             if (ev.Event == "favorite" || ev.Event == "unfavorite")
11677             {
11678                 if (_curTab != null && _statuses.Tabs[_curTab.Text].Contains(ev.Id))
11679                 {
11680                     this.PurgeListViewItemCache();
11681                     ((DetailsListView)_curTab.Tag).Update();
11682                 }
11683                 if (ev.Event == "unfavorite" && ev.Username.ToLowerInvariant().Equals(tw.Username.ToLowerInvariant()))
11684                 {
11685                     var favTab = this._statuses.GetTabByType(MyCommon.TabUsageType.Favorites);
11686                     favTab.EnqueueRemovePost(ev.Id, setIsDeleted: false);
11687                 }
11688             }
11689         }
11690
11691         private void NotifyEvent(Twitter.FormattedEvent ev)
11692         {
11693             //新着通知 
11694             if (BalloonRequired(ev))
11695             {
11696                 NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
11697                 //if (SettingDialog.DispUsername) NotifyIcon1.BalloonTipTitle = tw.Username + " - "; else NotifyIcon1.BalloonTipTitle = "";
11698                 //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [" + ev.Event.ToUpper() + "] by " + ((string)(!string.IsNullOrEmpty(ev.Username) ? ev.Username : ""), string);
11699                 StringBuilder title = new StringBuilder();
11700                 if (this._cfgCommon.DispUsername)
11701                 {
11702                     title.Append(tw.Username);
11703                     title.Append(" - ");
11704                 }
11705                 else
11706                 {
11707                     //title.Clear();
11708                 }
11709                 title.Append(Application.ProductName);
11710                 title.Append(" [");
11711                 title.Append(ev.Event.ToUpper(CultureInfo.CurrentCulture));
11712                 title.Append("] by ");
11713                 if (!string.IsNullOrEmpty(ev.Username))
11714                 {
11715                     title.Append(ev.Username);
11716                 }
11717                 else
11718                 {
11719                     //title.Append("");
11720                 }
11721                 string text;
11722                 if (!string.IsNullOrEmpty(ev.Target))
11723                 {
11724                     //NotifyIcon1.BalloonTipText = ev.Target;
11725                     text = ev.Target;
11726                 }
11727                 else
11728                 {
11729                     //NotifyIcon1.BalloonTipText = " ";
11730                     text = " ";
11731                 }
11732                 //NotifyIcon1.ShowBalloonTip(500);
11733                 if (this._cfgCommon.IsUseNotifyGrowl)
11734                 {
11735                     gh.Notify(GrowlHelper.NotifyType.UserStreamEvent,
11736                               ev.Id.ToString(), title.ToString(), text);
11737                 }
11738                 else
11739                 {
11740                     NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
11741                     NotifyIcon1.BalloonTipTitle = title.ToString();
11742                     NotifyIcon1.BalloonTipText = text;
11743                     NotifyIcon1.ShowBalloonTip(500);
11744                 }
11745             }
11746
11747             //サウンド再生
11748             string snd = this._cfgCommon.EventSoundFile;
11749             if (!_initial && this._cfgCommon.PlaySound && !string.IsNullOrEmpty(snd))
11750             {
11751                 if ((ev.Eventtype & this._cfgCommon.EventNotifyFlag) != 0 && IsMyEventNotityAsEventType(ev))
11752                 {
11753                     try
11754                     {
11755                         string dir = Application.StartupPath;
11756                         if (Directory.Exists(Path.Combine(dir, "Sounds")))
11757                         {
11758                             dir = Path.Combine(dir, "Sounds");
11759                         }
11760                         using (SoundPlayer player = new SoundPlayer(Path.Combine(dir, snd)))
11761                         {
11762                             player.Play();
11763                         }
11764                     }
11765                     catch (Exception)
11766                     {
11767                     }
11768                 }
11769             }
11770         }
11771
11772         private void StopToolStripMenuItem_Click(object sender, EventArgs e)
11773         {
11774             MenuItemUserStream.Enabled = false;
11775             if (StopRefreshAllMenuItem.Checked)
11776             {
11777                 StopRefreshAllMenuItem.Checked = false;
11778                 return;
11779             }
11780             if (this.tw.UserStreamActive)
11781             {
11782                 tw.StopUserStream();
11783             }
11784             else
11785             {
11786                 tw.StartUserStream();
11787             }
11788         }
11789
11790         private static string inputTrack = "";
11791
11792         private void TrackToolStripMenuItem_Click(object sender, EventArgs e)
11793         {
11794             if (TrackToolStripMenuItem.Checked)
11795             {
11796                 using (InputTabName inputForm = new InputTabName())
11797                 {
11798                     inputForm.TabName = inputTrack;
11799                     inputForm.FormTitle = "Input track word";
11800                     inputForm.FormDescription = "Track word";
11801                     if (inputForm.ShowDialog() != DialogResult.OK)
11802                     {
11803                         TrackToolStripMenuItem.Checked = false;
11804                         return;
11805                     }
11806                     inputTrack = inputForm.TabName.Trim();
11807                 }
11808                 if (!inputTrack.Equals(tw.TrackWord))
11809                 {
11810                     tw.TrackWord = inputTrack;
11811                     this.ModifySettingCommon = true;
11812                     TrackToolStripMenuItem.Checked = !string.IsNullOrEmpty(inputTrack);
11813                     tw.ReconnectUserStream();
11814                 }
11815             }
11816             else
11817             {
11818                 tw.TrackWord = "";
11819                 tw.ReconnectUserStream();
11820             }
11821             this.ModifySettingCommon = true;
11822         }
11823
11824         private void AllrepliesToolStripMenuItem_Click(object sender, EventArgs e)
11825         {
11826             tw.AllAtReply = AllrepliesToolStripMenuItem.Checked;
11827             this.ModifySettingCommon = true;
11828             tw.ReconnectUserStream();
11829         }
11830
11831         private void EventViewerMenuItem_Click(object sender, EventArgs e)
11832         {
11833             if (evtDialog == null || evtDialog.IsDisposed)
11834             {
11835                 evtDialog = null;
11836                 evtDialog = new EventViewerDialog();
11837                 evtDialog.Owner = this;
11838                 //親の中央に表示
11839                 Point pos = evtDialog.Location;
11840                 pos.X = Convert.ToInt32(this.Location.X + this.Size.Width / 2 - evtDialog.Size.Width / 2);
11841                 pos.Y = Convert.ToInt32(this.Location.Y + this.Size.Height / 2 - evtDialog.Size.Height / 2);
11842                 evtDialog.Location = pos;
11843             }
11844             evtDialog.EventSource = tw.StoredEvent;
11845             if (!evtDialog.Visible)
11846             {
11847                 evtDialog.Show(this);
11848             }
11849             else
11850             {
11851                 evtDialog.Activate();
11852             }
11853             this.TopMost = this._cfgCommon.AlwaysTop;
11854         }
11855 #endregion
11856
11857         private void TweenRestartMenuItem_Click(object sender, EventArgs e)
11858         {
11859             MyCommon._endingFlag = true;
11860             try
11861             {
11862                 this.Close();
11863                 Application.Restart();
11864             }
11865             catch (Exception)
11866             {
11867                 MessageBox.Show("Failed to restart. Please run " + Application.ProductName + " manually.");
11868             }
11869         }
11870
11871         private async void OpenOwnFavedMenuItem_Click(object sender, EventArgs e)
11872         {
11873             if (!string.IsNullOrEmpty(tw.Username))
11874                 await this.OpenUriInBrowserAsync(Properties.Resources.FavstarUrl + "users/" + tw.Username + "/recent");
11875         }
11876
11877         private async void OpenOwnHomeMenuItem_Click(object sender, EventArgs e)
11878         {
11879             await this.OpenUriInBrowserAsync(MyCommon.TwitterUrl + tw.Username);
11880         }
11881
11882         private bool ExistCurrentPost
11883         {
11884             get
11885             {
11886                 if (_curPost == null) return false;
11887                 if (_curPost.IsDeleted) return false;
11888                 return true;
11889             }
11890         }
11891
11892         private void ShowUserTimelineToolStripMenuItem_Click(object sender, EventArgs e)
11893         {
11894             ShowUserTimeline();
11895         }
11896
11897         private string GetUserIdFromCurPostOrInput(string caption)
11898         {
11899             var id = _curPost?.ScreenName ?? "";
11900
11901             using (InputTabName inputName = new InputTabName())
11902             {
11903                 inputName.FormTitle = caption;
11904                 inputName.FormDescription = Properties.Resources.FRMessage1;
11905                 inputName.TabName = id;
11906                 if (inputName.ShowDialog() == DialogResult.OK &&
11907                     !string.IsNullOrEmpty(inputName.TabName.Trim()))
11908                 {
11909                     id = inputName.TabName.Trim();
11910                 }
11911                 else
11912                 {
11913                     id = "";
11914                 }
11915             }
11916             return id;
11917         }
11918
11919         private void UserTimelineToolStripMenuItem_Click(object sender, EventArgs e)
11920         {
11921             string id = GetUserIdFromCurPostOrInput("Show UserTimeline");
11922             if (!string.IsNullOrEmpty(id))
11923             {
11924                 AddNewTabForUserTimeline(id);
11925             }
11926         }
11927
11928         private async void UserFavorareToolStripMenuItem_Click(object sender, EventArgs e)
11929         {
11930             string id = GetUserIdFromCurPostOrInput("Show Favstar");
11931             if (!string.IsNullOrEmpty(id))
11932             {
11933                 await this.OpenUriInBrowserAsync(Properties.Resources.FavstarUrl + "users/" + id + "/recent");
11934             }
11935         }
11936
11937         private void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
11938         {
11939             if (e.Mode == Microsoft.Win32.PowerModes.Resume) osResumed = true;
11940         }
11941
11942         private void TimelineRefreshEnableChange(bool isEnable)
11943         {
11944             if (isEnable)
11945             {
11946                 tw.StartUserStream();
11947             }
11948             else
11949             {
11950                 tw.StopUserStream();
11951             }
11952             TimerTimeline.Enabled = isEnable;
11953         }
11954
11955         private void StopRefreshAllMenuItem_CheckedChanged(object sender, EventArgs e)
11956         {
11957             TimelineRefreshEnableChange(!StopRefreshAllMenuItem.Checked);
11958         }
11959
11960         private async Task OpenUserAppointUrl()
11961         {
11962             if (this._cfgCommon.UserAppointUrl != null)
11963             {
11964                 if (this._cfgCommon.UserAppointUrl.Contains("{ID}") || this._cfgCommon.UserAppointUrl.Contains("{STATUS}"))
11965                 {
11966                     if (_curPost != null)
11967                     {
11968                         string xUrl = this._cfgCommon.UserAppointUrl;
11969                         xUrl = xUrl.Replace("{ID}", _curPost.ScreenName);
11970
11971                         var statusId = _curPost.RetweetedId ?? _curPost.StatusId;
11972                         xUrl = xUrl.Replace("{STATUS}", statusId.ToString());
11973
11974                         await this.OpenUriInBrowserAsync(xUrl);
11975                     }
11976                 }
11977                 else
11978                 {
11979                     await this.OpenUriInBrowserAsync(this._cfgCommon.UserAppointUrl);
11980                 }
11981             }
11982         }
11983
11984         private async void OpenUserSpecifiedUrlMenuItem_Click(object sender, EventArgs e)
11985         {
11986             await this.OpenUserAppointUrl();
11987         }
11988
11989         private void GrowlHelper_Callback(object sender, GrowlHelper.NotifyCallbackEventArgs e)
11990         {
11991             if (Form.ActiveForm == null)
11992             {
11993                 this.BeginInvoke((Action) (() =>
11994                 {
11995                     this.Visible = true;
11996                     if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;
11997                     this.Activate();
11998                     this.BringToFront();
11999                     if (e.NotifyType == GrowlHelper.NotifyType.DirectMessage)
12000                     {
12001                         if (!this.GoDirectMessage(e.StatusId)) this.StatusText.Focus();
12002                     }
12003                     else
12004                     {
12005                         if (!this.GoStatus(e.StatusId)) this.StatusText.Focus();
12006                     }
12007                 }));
12008             }
12009         }
12010
12011         private void ReplaceAppName()
12012         {
12013             MatomeMenuItem.Text = MyCommon.ReplaceAppName(MatomeMenuItem.Text);
12014             AboutMenuItem.Text = MyCommon.ReplaceAppName(AboutMenuItem.Text);
12015         }
12016
12017         private void tweetThumbnail1_ThumbnailLoading(object sender, EventArgs e)
12018         {
12019             this.SplitContainer3.Panel2Collapsed = false;
12020         }
12021
12022         private async void tweetThumbnail1_ThumbnailDoubleClick(object sender, ThumbnailDoubleClickEventArgs e)
12023         {
12024             await this.OpenThumbnailPicture(e.Thumbnail);
12025         }
12026
12027         private async void tweetThumbnail1_ThumbnailImageSearchClick(object sender, ThumbnailImageSearchEventArgs e)
12028         {
12029             await this.OpenUriInBrowserAsync(e.ImageUrl);
12030         }
12031
12032         private async Task OpenThumbnailPicture(ThumbnailInfo thumbnail)
12033         {
12034             var url = thumbnail.FullSizeImageUrl ?? thumbnail.MediaPageUrl;
12035
12036             await this.OpenUriInBrowserAsync(url);
12037         }
12038
12039         private async void TwitterApiStatusToolStripMenuItem_Click(object sender, EventArgs e)
12040         {
12041             await this.OpenUriInBrowserAsync(Twitter.ServiceAvailabilityStatusUrl);
12042         }
12043
12044         private void PostButton_KeyDown(object sender, KeyEventArgs e)
12045         {
12046             if (e.KeyCode == Keys.Space)
12047             {
12048                 this.JumpUnreadMenuItem_Click(null, null);
12049
12050                 e.SuppressKeyPress = true;
12051             }
12052         }
12053
12054         private void ContextMenuColumnHeader_Opening(object sender, CancelEventArgs e)
12055         {
12056             this.IconSizeNoneToolStripMenuItem.Checked = this._cfgCommon.IconSize == MyCommon.IconSizes.IconNone;
12057             this.IconSize16ToolStripMenuItem.Checked = this._cfgCommon.IconSize == MyCommon.IconSizes.Icon16;
12058             this.IconSize24ToolStripMenuItem.Checked = this._cfgCommon.IconSize == MyCommon.IconSizes.Icon24;
12059             this.IconSize48ToolStripMenuItem.Checked = this._cfgCommon.IconSize == MyCommon.IconSizes.Icon48;
12060             this.IconSize48_2ToolStripMenuItem.Checked = this._cfgCommon.IconSize == MyCommon.IconSizes.Icon48_2;
12061
12062             this.LockListSortOrderToolStripMenuItem.Checked = this._cfgCommon.SortOrderLock;
12063         }
12064
12065         private void IconSizeNoneToolStripMenuItem_Click(object sender, EventArgs e)
12066         {
12067             ChangeListViewIconSize(MyCommon.IconSizes.IconNone);
12068         }
12069
12070         private void IconSize16ToolStripMenuItem_Click(object sender, EventArgs e)
12071         {
12072             ChangeListViewIconSize(MyCommon.IconSizes.Icon16);
12073         }
12074
12075         private void IconSize24ToolStripMenuItem_Click(object sender, EventArgs e)
12076         {
12077             ChangeListViewIconSize(MyCommon.IconSizes.Icon24);
12078         }
12079
12080         private void IconSize48ToolStripMenuItem_Click(object sender, EventArgs e)
12081         {
12082             ChangeListViewIconSize(MyCommon.IconSizes.Icon48);
12083         }
12084
12085         private void IconSize48_2ToolStripMenuItem_Click(object sender, EventArgs e)
12086         {
12087             ChangeListViewIconSize(MyCommon.IconSizes.Icon48_2);
12088         }
12089
12090         private void ChangeListViewIconSize(MyCommon.IconSizes iconSize)
12091         {
12092             if (this._cfgCommon.IconSize == iconSize) return;
12093
12094             var oldIconCol = _iconCol;
12095
12096             this._cfgCommon.IconSize = iconSize;
12097             ApplyListViewIconSize(iconSize);
12098
12099             if (_iconCol != oldIconCol)
12100             {
12101                 foreach (TabPage tp in ListTab.TabPages)
12102                 {
12103                     ResetColumns((DetailsListView)tp.Tag);
12104                 }
12105             }
12106
12107             _curList?.Refresh();
12108
12109             ModifySettingCommon = true;
12110         }
12111
12112         private void LockListSortToolStripMenuItem_Click(object sender, EventArgs e)
12113         {
12114             var state = this.LockListSortOrderToolStripMenuItem.Checked;
12115             if (this._cfgCommon.SortOrderLock == state) return;
12116
12117             this._cfgCommon.SortOrderLock = state;
12118
12119             ModifySettingCommon = true;
12120         }
12121
12122         private void tweetDetailsView_StatusChanged(object sender, TweetDetailsViewStatusChengedEventArgs e)
12123         {
12124             if (!string.IsNullOrEmpty(e.StatusText))
12125             {
12126                 this.StatusLabelUrl.Text = e.StatusText;
12127             }
12128             else
12129             {
12130                 this.SetStatusLabelUrl();
12131             }
12132         }
12133     }
12134 }