OSDN Git Service

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