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.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
22 // You should have received a copy of the GNU General public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
30 using System.Collections.Generic;
31 using System.ComponentModel;
37 using System.Net.Http;
39 using System.Text.RegularExpressions;
40 using System.Threading;
41 using System.Threading.Tasks;
42 using System.Windows.Forms;
43 using OpenTween.Models;
44 using OpenTween.Setting;
48 public partial class TweetDetailsView : UserControl
50 public TweenMain Owner { get; set; } = null!;
52 /// <summary>プロフィール画像のキャッシュ</summary>
53 public ImageCache IconCache { get; set; } = null!;
55 /// <summary><see cref="PostClass"/> のダンプを表示するか</summary>
56 public bool DumpPostClass { get; set; }
58 /// <summary>現在表示中の発言</summary>
59 public PostClass? CurrentPost { get; private set; }
62 public new bool TabStop
65 set => base.TabStop = value;
68 /// <summary>ステータスバーに表示するテキストの変化を通知するイベント</summary>
69 public event EventHandler<TweetDetailsViewStatusChengedEventArgs>? StatusChanged;
71 /// <summary><see cref="ContextMenuPostBrowser"/> 展開時の <see cref="PostBrowser"/>.StatusText を保持するフィールド</summary>
72 private string _postBrowserStatusText = "";
74 public TweetDetailsView()
76 this.InitializeComponent();
82 DateTimeLabel.Text = "";
83 SourceLinkLabel.Text = "";
85 new InternetSecurityManager(PostBrowser);
86 this.PostBrowser.AllowWebBrowserDrop = false; // COMException を回避するため、ActiveX の初期化が終わってから設定する
89 public async Task ShowPostDetails(PostClass post)
91 this.CurrentPost = post;
93 var loadTasks = new List<Task>();
95 using (ControlTransaction.Update(this.TableLayoutPanel1))
97 SourceLinkLabel.Text = post.Source;
98 SourceLinkLabel.TabStop = false; // Text を更新すると勝手に true にされる
104 nameText = "DM FROM <- ";
106 nameText = "DM TO -> ";
112 nameText += post.ScreenName + "/" + post.Nickname;
113 if (post.RetweetedId != null)
114 nameText += " (RT:" + post.RetweetedBy + ")";
116 NameLabel.Text = nameText;
118 var nameForeColor = SystemColors.ControlText;
119 if (post.IsOwl && (SettingManager.Common.OneWayLove || post.IsDm))
120 nameForeColor = SettingManager.Local.ColorOWL;
121 if (post.RetweetedId != null)
122 nameForeColor = SettingManager.Local.ColorRetweet;
124 nameForeColor = SettingManager.Local.ColorFav;
125 NameLabel.ForeColor = nameForeColor;
127 loadTasks.Add(this.SetUserPictureAsync(post.ImageUrl));
129 DateTimeLabel.Text = post.CreatedAt.ToLocalTimeString();
132 if (this.DumpPostClass)
134 var sb = new StringBuilder(512);
136 sb.Append("-----Start PostClass Dump<br>");
137 sb.AppendFormat("TextFromApi : {0}<br>", post.TextFromApi);
138 sb.AppendFormat("(PlainText) : <xmp>{0}</xmp><br>", post.TextFromApi);
139 sb.AppendFormat("StatusId : {0}<br>", post.StatusId);
140 sb.AppendFormat("ImageUrl : {0}<br>", post.ImageUrl);
141 sb.AppendFormat("InReplyToStatusId : {0}<br>", post.InReplyToStatusId);
142 sb.AppendFormat("InReplyToUser : {0}<br>", post.InReplyToUser);
143 sb.AppendFormat("IsDM : {0}<br>", post.IsDm);
144 sb.AppendFormat("IsFav : {0}<br>", post.IsFav);
145 sb.AppendFormat("IsMark : {0}<br>", post.IsMark);
146 sb.AppendFormat("IsMe : {0}<br>", post.IsMe);
147 sb.AppendFormat("IsOwl : {0}<br>", post.IsOwl);
148 sb.AppendFormat("IsProtect : {0}<br>", post.IsProtect);
149 sb.AppendFormat("IsRead : {0}<br>", post.IsRead);
150 sb.AppendFormat("IsReply : {0}<br>", post.IsReply);
152 foreach (var nm in post.ReplyToList.Select(x => x.ScreenName))
154 sb.AppendFormat("ReplyToList : {0}<br>", nm);
157 sb.AppendFormat("ScreenName : {0}<br>", post.ScreenName);
158 sb.AppendFormat("NickName : {0}<br>", post.Nickname);
159 sb.AppendFormat("Text : {0}<br>", post.Text);
160 sb.AppendFormat("(PlainText) : <xmp>{0}</xmp><br>", post.Text);
161 sb.AppendFormat("CreatedAt : {0}<br>", post.CreatedAt.ToLocalTimeString());
162 sb.AppendFormat("Source : {0}<br>", post.Source);
163 sb.AppendFormat("UserId : {0}<br>", post.UserId);
164 sb.AppendFormat("FilterHit : {0}<br>", post.FilterHit);
165 sb.AppendFormat("RetweetedBy : {0}<br>", post.RetweetedBy);
166 sb.AppendFormat("RetweetedId : {0}<br>", post.RetweetedId);
168 sb.AppendFormat("Media.Count : {0}<br>", post.Media.Count);
169 if (post.Media.Count > 0)
171 for (var i = 0; i < post.Media.Count; i++)
173 var info = post.Media[i];
174 sb.AppendFormat("Media[{0}].Url : {1}<br>", i, info.Url);
175 sb.AppendFormat("Media[{0}].VideoUrl : {1}<br>", i, info.VideoUrl ?? "---");
178 sb.Append("-----End PostClass Dump<br>");
180 PostBrowser.DocumentText = this.Owner.createDetailHtml(sb.ToString());
184 using (ControlTransaction.Update(this.PostBrowser))
186 this.PostBrowser.DocumentText =
187 this.Owner.createDetailHtml(post.IsDeleted ? "(DELETED)" : post.Text);
189 this.PostBrowser.Document.Window.ScrollTo(0, 0);
192 loadTasks.Add(this.AppendQuoteTweetAsync(post));
194 await Task.WhenAll(loadTasks);
197 public void ScrollDownPostBrowser(bool forward)
199 var doc = PostBrowser.Document;
200 if (doc == null) return;
202 var tags = doc.GetElementsByTagName("html");
206 tags[0].ScrollTop += SettingManager.Local.FontDetail.Height;
208 tags[0].ScrollTop -= SettingManager.Local.FontDetail.Height;
212 public void PageDownPostBrowser(bool forward)
214 var doc = PostBrowser.Document;
215 if (doc == null) return;
217 var tags = doc.GetElementsByTagName("html");
221 tags[0].ScrollTop += PostBrowser.ClientRectangle.Height - SettingManager.Local.FontDetail.Height;
223 tags[0].ScrollTop -= PostBrowser.ClientRectangle.Height - SettingManager.Local.FontDetail.Height;
227 public HtmlElement[] GetLinkElements()
229 return this.PostBrowser.Document.Links.Cast<HtmlElement>()
230 .Where(x => x.GetAttribute("className") != "tweet-quote-link") // 引用ツイートで追加されたリンクを除く
234 private async Task SetUserPictureAsync(string imageUrl, bool force = false)
236 if (MyCommon.IsNullOrEmpty(imageUrl))
239 if (this.IconCache == null)
242 this.ClearUserPicture();
244 await this.UserPicture.SetImageFromTask(async () =>
246 var image = await this.IconCache.DownloadImageAsync(imageUrl, force)
247 .ConfigureAwait(false);
249 return await image.CloneAsync()
250 .ConfigureAwait(false);
255 /// UserPicture.Image に設定されている画像を破棄します。
257 private void ClearUserPicture()
259 if (this.UserPicture.Image != null)
261 var oldImage = this.UserPicture.Image;
262 this.UserPicture.Image = null;
268 /// 発言詳細欄のツイートURLを展開する
270 private async Task AppendQuoteTweetAsync(PostClass post)
272 var quoteStatusIds = post.QuoteStatusIds;
273 if (quoteStatusIds.Length == 0 && post.InReplyToStatusId == null)
277 var loadingQuoteHtml = quoteStatusIds.Select(x => FormatQuoteTweetHtml(x, Properties.Resources.LoadingText, isReply: false));
279 var loadingReplyHtml = string.Empty;
280 if (post.InReplyToStatusId != null)
281 loadingReplyHtml = FormatQuoteTweetHtml(post.InReplyToStatusId.Value, Properties.Resources.LoadingText, isReply: true);
283 var body = post.Text + string.Concat(loadingQuoteHtml) + loadingReplyHtml;
285 using (ControlTransaction.Update(this.PostBrowser))
286 this.PostBrowser.DocumentText = this.Owner.createDetailHtml(body);
289 var loadTweetTasks = quoteStatusIds.Select(x => this.CreateQuoteTweetHtml(x, isReply: false)).ToList();
291 if (post.InReplyToStatusId != null)
292 loadTweetTasks.Add(this.CreateQuoteTweetHtml(post.InReplyToStatusId.Value, isReply: true));
294 var quoteHtmls = await Task.WhenAll(loadTweetTasks);
296 // 非同期処理中に表示中のツイートが変わっていたらキャンセルされたものと扱う
297 if (this.CurrentPost != post || this.CurrentPost.IsDeleted)
300 body = post.Text + string.Concat(quoteHtmls);
302 using (ControlTransaction.Update(this.PostBrowser))
303 this.PostBrowser.DocumentText = this.Owner.createDetailHtml(body);
306 private async Task<string> CreateQuoteTweetHtml(long statusId, bool isReply)
308 var post = TabInformations.GetInstance()[statusId];
313 post = await this.Owner.TwitterInstance.GetStatusApi(false, statusId)
314 .ConfigureAwait(false);
316 catch (WebApiException ex)
318 return FormatQuoteTweetHtml(statusId, WebUtility.HtmlEncode($"Err:{ex.Message}(GetStatus)"), isReply);
322 if (!TabInformations.GetInstance().AddQuoteTweet(post))
323 return FormatQuoteTweetHtml(statusId, "This Tweet is unavailable.", isReply);
326 return FormatQuoteTweetHtml(post, isReply);
329 internal static string FormatQuoteTweetHtml(PostClass post, bool isReply)
331 var innerHtml = "<p>" + StripLinkTagHtml(post.Text) + "</p>" +
332 " — " + WebUtility.HtmlEncode(post.Nickname) +
333 " (@" + WebUtility.HtmlEncode(post.ScreenName) + ") " +
334 WebUtility.HtmlEncode(post.CreatedAt.ToLocalTimeString());
336 return FormatQuoteTweetHtml(post.StatusId, innerHtml, isReply);
339 internal static string FormatQuoteTweetHtml(long statusId, string innerHtml, bool isReply)
341 var blockClassName = "quote-tweet";
344 blockClassName += " reply";
346 return "<a class=\"quote-tweet-link\" href=\"//opentween/status/" + statusId + "\">" +
347 $"<blockquote class=\"{blockClassName}\">{innerHtml}</blockquote>" +
352 /// 指定されたHTMLからリンクを除去します
354 internal static string StripLinkTagHtml(string html)
355 => Regex.Replace(html, @"<a[^>]*>(.*?)</a>", "$1"); // a 要素はネストされていない前提の正規表現パターン
357 public async Task DoTranslation()
359 if (this.CurrentPost == null || this.CurrentPost.IsDeleted)
362 await this.DoTranslation(this.CurrentPost.TextFromApi);
365 private async Task DoTranslation(string str)
367 if (MyCommon.IsNullOrEmpty(str))
370 var bing = new Bing();
373 var translatedText = await bing.TranslateAsync(str,
375 langTo: SettingManager.Common.TranslateLanguage);
377 this.PostBrowser.DocumentText = this.Owner.createDetailHtml(translatedText);
379 catch (WebApiException e)
381 this.RaiseStatusChanged("Err:" + e.Message);
383 catch (OperationCanceledException)
385 this.RaiseStatusChanged("Err:Timeout");
389 private async Task DoSearchToolStrip(string url)
391 // 発言詳細で「選択文字列で検索」(選択文字列取得)
392 var _selText = this.PostBrowser.GetSelectedText();
394 if (_selText != null)
396 if (url == Properties.Resources.SearchItem4Url)
399 this.Owner.AddNewTabForSearch(_selText);
403 var tmp = string.Format(url, Uri.EscapeDataString(_selText));
404 await MyCommon.OpenInBrowserAsync(this, tmp);
408 private string? GetUserId()
410 var m = Regex.Match(this._postBrowserStatusText, @"^https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)(/status(es)?/[0-9]+)?$");
411 if (m.Success && this.Owner.IsTwitterId(m.Result("${ScreenName}")))
412 return m.Result("${ScreenName}");
417 protected void RaiseStatusChanged(string statusText)
418 => this.StatusChanged?.Invoke(this, new TweetDetailsViewStatusChengedEventArgs(statusText));
420 private void TweetDetailsView_FontChanged(object sender, EventArgs e)
422 // OTBaseForm.GlobalFont による UI フォントの変更に対応
423 var origFont = this.NameLabel.Font;
424 this.NameLabel.Font = new Font(this.Font.Name, origFont.Size, origFont.Style);
427 #region TableLayoutPanel1
429 private async void UserPicture_DoubleClick(object sender, EventArgs e)
431 if (this.CurrentPost == null)
434 await MyCommon.OpenInBrowserAsync(this, MyCommon.TwitterUrl + this.CurrentPost.ScreenName);
437 private void UserPicture_MouseEnter(object sender, EventArgs e)
438 => this.UserPicture.Cursor = Cursors.Hand;
440 private void UserPicture_MouseLeave(object sender, EventArgs e)
441 => this.UserPicture.Cursor = Cursors.Default;
443 private async void PostBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
445 if (e.Url.AbsoluteUri != "about:blank")
447 await this.ShowPostDetails(this.CurrentPost!); // 現在の発言を表示し直す (Navigated の段階ではキャンセルできない)
448 await MyCommon.OpenInBrowserAsync(this, e.Url.OriginalString);
452 private async void PostBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
454 if (e.Url.Scheme == "data")
456 this.RaiseStatusChanged(this.PostBrowser.StatusText.Replace("&", "&&"));
458 else if (e.Url.AbsoluteUri != "about:blank")
461 // Ctrlを押しながらリンクを開いた場合は、設定と逆の動作をするフラグを true としておく
462 await this.Owner.OpenUriAsync(e.Url, MyCommon.IsKeyDown(Keys.Control));
466 private async void PostBrowser_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
468 var KeyRes = this.Owner.CommonKeyDown(e.KeyData, FocusedControl.PostBrowser, out var asyncTask);
475 if (Enum.IsDefined(typeof(Shortcut), (Shortcut)e.KeyData))
477 var shortcut = (Shortcut)e.KeyData;
482 case Shortcut.CtrlIns:
486 // その他のショートカットキーは無効にする
493 if (asyncTask != null)
497 private void PostBrowser_StatusTextChanged(object sender, EventArgs e)
501 if (PostBrowser.StatusText.StartsWith("http", StringComparison.Ordinal)
502 || PostBrowser.StatusText.StartsWith("ftp", StringComparison.Ordinal)
503 || PostBrowser.StatusText.StartsWith("data", StringComparison.Ordinal))
505 this.RaiseStatusChanged(this.PostBrowser.StatusText.Replace("&", "&&"));
507 if (MyCommon.IsNullOrEmpty(PostBrowser.StatusText))
509 this.RaiseStatusChanged(statusText: "");
517 private async void SourceLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
519 var sourceUri = this.CurrentPost?.SourceUri;
520 if (sourceUri != null && e.Button == MouseButtons.Left)
522 await MyCommon.OpenInBrowserAsync(this, sourceUri.AbsoluteUri);
526 private void SourceLinkLabel_MouseEnter(object sender, EventArgs e)
528 var sourceUri = this.CurrentPost?.SourceUri;
529 if (sourceUri != null)
531 this.RaiseStatusChanged(MyCommon.ConvertToReadableUrl(sourceUri.AbsoluteUri));
535 private void SourceLinkLabel_MouseLeave(object sender, EventArgs e)
536 => this.RaiseStatusChanged(statusText: "");
540 #region ContextMenuUserPicture
542 private void ContextMenuUserPicture_Opening(object sender, CancelEventArgs e)
544 // 発言詳細のアイコン右クリック時のメニュー制御
545 if (this.CurrentPost != null)
547 var name = this.CurrentPost.ImageUrl;
548 if (!MyCommon.IsNullOrEmpty(name))
550 var idx = name.LastIndexOf('/');
553 name = Path.GetFileName(name.Substring(idx));
554 if (name.Contains("_normal.") || name.EndsWith("_normal", StringComparison.Ordinal))
556 name = name.Replace("_normal", "");
557 this.IconNameToolStripMenuItem.Text = name;
558 this.IconNameToolStripMenuItem.Enabled = true;
562 this.IconNameToolStripMenuItem.Enabled = false;
563 this.IconNameToolStripMenuItem.Text = Properties.Resources.ContextMenuStrip3_OpeningText1;
568 this.IconNameToolStripMenuItem.Enabled = false;
569 this.IconNameToolStripMenuItem.Text = Properties.Resources.ContextMenuStrip3_OpeningText1;
572 this.ReloadIconToolStripMenuItem.Enabled = true;
574 if (this.IconCache.TryGetFromCache(this.CurrentPost.ImageUrl) != null)
576 this.SaveIconPictureToolStripMenuItem.Enabled = true;
580 this.SaveIconPictureToolStripMenuItem.Enabled = false;
585 this.IconNameToolStripMenuItem.Enabled = false;
586 this.ReloadIconToolStripMenuItem.Enabled = false;
587 this.SaveIconPictureToolStripMenuItem.Enabled = false;
588 this.IconNameToolStripMenuItem.Text = Properties.Resources.ContextMenuStrip3_OpeningText1;
593 this.IconNameToolStripMenuItem.Enabled = false;
594 this.ReloadIconToolStripMenuItem.Enabled = false;
595 this.SaveIconPictureToolStripMenuItem.Enabled = false;
596 this.IconNameToolStripMenuItem.Text = Properties.Resources.ContextMenuStrip3_OpeningText2;
598 if (this.CurrentPost != null)
600 if (this.CurrentPost.UserId == this.Owner.TwitterInstance.UserId)
602 FollowToolStripMenuItem.Enabled = false;
603 UnFollowToolStripMenuItem.Enabled = false;
604 ShowFriendShipToolStripMenuItem.Enabled = false;
605 ShowUserStatusToolStripMenuItem.Enabled = true;
606 SearchPostsDetailNameToolStripMenuItem.Enabled = true;
607 SearchAtPostsDetailNameToolStripMenuItem.Enabled = false;
608 ListManageUserContextToolStripMenuItem3.Enabled = true;
612 FollowToolStripMenuItem.Enabled = true;
613 UnFollowToolStripMenuItem.Enabled = true;
614 ShowFriendShipToolStripMenuItem.Enabled = true;
615 ShowUserStatusToolStripMenuItem.Enabled = true;
616 SearchPostsDetailNameToolStripMenuItem.Enabled = true;
617 SearchAtPostsDetailNameToolStripMenuItem.Enabled = true;
618 ListManageUserContextToolStripMenuItem3.Enabled = true;
623 FollowToolStripMenuItem.Enabled = false;
624 UnFollowToolStripMenuItem.Enabled = false;
625 ShowFriendShipToolStripMenuItem.Enabled = false;
626 ShowUserStatusToolStripMenuItem.Enabled = false;
627 SearchPostsDetailNameToolStripMenuItem.Enabled = false;
628 SearchAtPostsDetailNameToolStripMenuItem.Enabled = false;
629 ListManageUserContextToolStripMenuItem3.Enabled = false;
633 private async void FollowToolStripMenuItem_Click(object sender, EventArgs e)
635 if (this.CurrentPost == null)
638 if (this.CurrentPost.UserId == this.Owner.TwitterInstance.UserId)
641 await this.Owner.FollowCommand(this.CurrentPost.ScreenName);
644 private async void UnFollowToolStripMenuItem_Click(object sender, EventArgs e)
646 if (this.CurrentPost == null)
649 if (this.CurrentPost.UserId == this.Owner.TwitterInstance.UserId)
652 await this.Owner.RemoveCommand(this.CurrentPost.ScreenName, false);
655 private async void ShowFriendShipToolStripMenuItem_Click(object sender, EventArgs e)
657 if (this.CurrentPost == null)
660 if (this.CurrentPost.UserId == this.Owner.TwitterInstance.UserId)
663 await this.Owner.ShowFriendship(this.CurrentPost.ScreenName);
666 // ListManageUserContextToolStripMenuItem3.Click は ListManageUserContextToolStripMenuItem_Click を共用
668 private async void ShowUserStatusToolStripMenuItem_Click(object sender, EventArgs e)
670 if (this.CurrentPost == null)
673 await this.Owner.ShowUserStatus(this.CurrentPost.ScreenName, false);
676 private async void SearchPostsDetailNameToolStripMenuItem_Click(object sender, EventArgs e)
678 if (this.CurrentPost == null)
681 await this.Owner.AddNewTabForUserTimeline(this.CurrentPost.ScreenName);
684 private void SearchAtPostsDetailNameToolStripMenuItem_Click(object sender, EventArgs e)
686 if (this.CurrentPost == null)
689 this.Owner.AddNewTabForSearch("@" + this.CurrentPost.ScreenName);
692 private async void IconNameToolStripMenuItem_Click(object sender, EventArgs e)
694 var imageUrl = this.CurrentPost?.ImageUrl;
695 if (MyCommon.IsNullOrEmpty(imageUrl))
698 await MyCommon.OpenInBrowserAsync(this, imageUrl.Remove(imageUrl.LastIndexOf("_normal", StringComparison.Ordinal), 7)); // "_normal".Length
701 private async void ReloadIconToolStripMenuItem_Click(object sender, EventArgs e)
703 var imageUrl = this.CurrentPost?.ImageUrl;
704 if (MyCommon.IsNullOrEmpty(imageUrl))
707 await this.SetUserPictureAsync(imageUrl, force: true);
710 private void SaveIconPictureToolStripMenuItem_Click(object sender, EventArgs e)
712 var imageUrl = this.CurrentPost?.ImageUrl;
713 if (MyCommon.IsNullOrEmpty(imageUrl))
716 var memoryImage = this.IconCache.TryGetFromCache(imageUrl);
717 if (memoryImage == null)
720 this.Owner.SaveFileDialog1.FileName = imageUrl.Substring(imageUrl.LastIndexOf('/') + 1);
722 if (this.Owner.SaveFileDialog1.ShowDialog() == DialogResult.OK)
726 using var orgBmp = new Bitmap(memoryImage.Image);
727 using var bmp2 = new Bitmap(orgBmp.Size.Width, orgBmp.Size.Height);
729 using (var g = Graphics.FromImage(bmp2))
731 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
732 g.DrawImage(orgBmp, 0, 0, orgBmp.Size.Width, orgBmp.Size.Height);
734 bmp2.Save(this.Owner.SaveFileDialog1.FileName);
738 // 処理中にキャッシュアウトする可能性あり
745 #region ContextMenuPostBrowser
747 private void ContextMenuPostBrowser_Opening(object ender, CancelEventArgs e)
750 if (PostBrowser.StatusText.StartsWith("http", StringComparison.Ordinal))
752 this._postBrowserStatusText = PostBrowser.StatusText;
753 var name = GetUserId();
754 UrlCopyContextMenuItem.Enabled = true;
757 FollowContextMenuItem.Enabled = true;
758 RemoveContextMenuItem.Enabled = true;
759 FriendshipContextMenuItem.Enabled = true;
760 ShowUserStatusContextMenuItem.Enabled = true;
761 SearchPostsDetailToolStripMenuItem.Enabled = true;
762 IdFilterAddMenuItem.Enabled = true;
763 ListManageUserContextToolStripMenuItem.Enabled = true;
764 SearchAtPostsDetailToolStripMenuItem.Enabled = true;
768 FollowContextMenuItem.Enabled = false;
769 RemoveContextMenuItem.Enabled = false;
770 FriendshipContextMenuItem.Enabled = false;
771 ShowUserStatusContextMenuItem.Enabled = false;
772 SearchPostsDetailToolStripMenuItem.Enabled = false;
773 IdFilterAddMenuItem.Enabled = false;
774 ListManageUserContextToolStripMenuItem.Enabled = false;
775 SearchAtPostsDetailToolStripMenuItem.Enabled = false;
778 if (Regex.IsMatch(this._postBrowserStatusText, @"^https?://twitter.com/search\?q=%23"))
779 UseHashtagMenuItem.Enabled = true;
781 UseHashtagMenuItem.Enabled = false;
785 this._postBrowserStatusText = "";
786 UrlCopyContextMenuItem.Enabled = false;
787 FollowContextMenuItem.Enabled = false;
788 RemoveContextMenuItem.Enabled = false;
789 FriendshipContextMenuItem.Enabled = false;
790 ShowUserStatusContextMenuItem.Enabled = false;
791 SearchPostsDetailToolStripMenuItem.Enabled = false;
792 SearchAtPostsDetailToolStripMenuItem.Enabled = false;
793 UseHashtagMenuItem.Enabled = false;
794 IdFilterAddMenuItem.Enabled = false;
795 ListManageUserContextToolStripMenuItem.Enabled = false;
797 // 文字列選択されていないときは選択文字列関係の項目を非表示に
798 var _selText = this.PostBrowser.GetSelectedText();
799 if (_selText == null)
801 SelectionSearchContextMenuItem.Enabled = false;
802 SelectionCopyContextMenuItem.Enabled = false;
803 SelectionTranslationToolStripMenuItem.Enabled = false;
807 SelectionSearchContextMenuItem.Enabled = true;
808 SelectionCopyContextMenuItem.Enabled = true;
809 SelectionTranslationToolStripMenuItem.Enabled = true;
811 // 発言内に自分以外のユーザーが含まれてればフォロー状態全表示を有効に
812 var ma = Regex.Matches(this.PostBrowser.DocumentText, @"href=""https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)(/status(es)?/[0-9]+)?""");
813 var fAllFlag = false;
814 foreach (Match mu in ma)
816 if (!mu.Result("${ScreenName}").Equals(this.Owner.TwitterInstance.Username, StringComparison.InvariantCultureIgnoreCase))
822 this.FriendshipAllMenuItem.Enabled = fAllFlag;
824 if (this.CurrentPost == null)
825 TranslationToolStripMenuItem.Enabled = false;
827 TranslationToolStripMenuItem.Enabled = true;
832 private async void SearchGoogleContextMenuItem_Click(object sender, EventArgs e)
833 => await this.DoSearchToolStrip(Properties.Resources.SearchItem2Url);
835 private async void SearchWikipediaContextMenuItem_Click(object sender, EventArgs e)
836 => await this.DoSearchToolStrip(Properties.Resources.SearchItem1Url);
838 private async void SearchPublicSearchContextMenuItem_Click(object sender, EventArgs e)
839 => await this.DoSearchToolStrip(Properties.Resources.SearchItem4Url);
841 private void CurrentTabToolStripMenuItem_Click(object sender, EventArgs e)
843 // 発言詳細の選択文字列で現在のタブを検索
844 var _selText = this.PostBrowser.GetSelectedText();
846 if (_selText != null)
848 var searchOptions = new SearchWordDialog.SearchOptions(
849 SearchWordDialog.SearchType.Timeline,
852 caseSensitive: false,
855 this.Owner.SearchDialog.ResultOptions = searchOptions;
857 this.Owner.DoTabSearch(
859 searchOptions.CaseSensitive,
860 searchOptions.UseRegex,
861 TweenMain.SEARCHTYPE.NextSearch);
865 private void SelectionCopyContextMenuItem_Click(object sender, EventArgs e)
868 var _selText = this.PostBrowser.GetSelectedText();
871 Clipboard.SetDataObject(_selText, false, 5, 100);
875 MessageBox.Show(ex.Message);
879 private void UrlCopyContextMenuItem_Click(object sender, EventArgs e)
883 foreach (var link in this.PostBrowser.Document.Links.Cast<HtmlElement>())
885 if (link.GetAttribute("href") == this._postBrowserStatusText)
887 var linkStr = link.GetAttribute("title");
888 if (MyCommon.IsNullOrEmpty(linkStr))
889 linkStr = link.GetAttribute("href");
891 Clipboard.SetDataObject(linkStr, false, 5, 100);
896 Clipboard.SetDataObject(this._postBrowserStatusText, false, 5, 100);
900 MessageBox.Show(ex.Message);
904 private void SelectionAllContextMenuItem_Click(object sender, EventArgs e)
905 => this.PostBrowser.Document.ExecCommand("SelectAll", false, null); // 発言詳細ですべて選択
907 private async void FollowContextMenuItem_Click(object sender, EventArgs e)
909 var name = GetUserId();
911 await this.Owner.FollowCommand(name);
914 private async void RemoveContextMenuItem_Click(object sender, EventArgs e)
916 var name = GetUserId();
918 await this.Owner.RemoveCommand(name, false);
921 private async void FriendshipContextMenuItem_Click(object sender, EventArgs e)
923 var name = GetUserId();
925 await this.Owner.ShowFriendship(name);
928 private async void FriendshipAllMenuItem_Click(object sender, EventArgs e)
930 var ma = Regex.Matches(this.PostBrowser.DocumentText, @"href=""https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)(/status(es)?/[0-9]+)?""");
931 var ids = new List<string>();
932 foreach (Match mu in ma)
934 if (!mu.Result("${ScreenName}").Equals(this.Owner.TwitterInstance.Username, StringComparison.InvariantCultureIgnoreCase))
936 ids.Add(mu.Result("${ScreenName}"));
940 await this.Owner.ShowFriendship(ids.ToArray());
943 private async void ShowUserStatusContextMenuItem_Click(object sender, EventArgs e)
945 var name = GetUserId();
947 await this.Owner.ShowUserStatus(name);
950 private async void SearchPostsDetailToolStripMenuItem_Click(object sender, EventArgs e)
952 var name = GetUserId();
954 await this.Owner.AddNewTabForUserTimeline(name);
957 private void SearchAtPostsDetailToolStripMenuItem_Click(object sender, EventArgs e)
959 var name = GetUserId();
960 if (name != null) this.Owner.AddNewTabForSearch("@" + name);
963 private void IdFilterAddMenuItem_Click(object sender, EventArgs e)
965 var name = GetUserId();
967 this.Owner.AddFilterRuleByScreenName(name);
970 private void ListManageUserContextToolStripMenuItem_Click(object sender, EventArgs e)
972 var menuItem = (ToolStripMenuItem)sender;
975 if (menuItem.Owner == this.ContextMenuPostBrowser)
978 if (user == null) return;
980 else if (this.CurrentPost != null)
982 user = this.CurrentPost.ScreenName;
989 this.Owner.ListManageUserContext(user);
992 private void UseHashtagMenuItem_Click(object sender, EventArgs e)
994 var m = Regex.Match(this._postBrowserStatusText, @"^https?://twitter.com/search\?q=%23(?<hash>.+)$");
996 this.Owner.SetPermanentHashtag(Uri.UnescapeDataString(m.Groups["hash"].Value));
999 private async void SelectionTranslationToolStripMenuItem_Click(object sender, EventArgs e)
1001 var text = this.PostBrowser.GetSelectedText();
1002 await this.DoTranslation(text);
1005 private async void TranslationToolStripMenuItem_Click(object sender, EventArgs e)
1006 => await this.DoTranslation();
1010 #region ContextMenuSource
1012 private void ContextMenuSource_Opening(object sender, CancelEventArgs e)
1014 if (this.CurrentPost == null || this.CurrentPost.IsDeleted || this.CurrentPost.IsDm)
1016 SourceCopyMenuItem.Enabled = false;
1017 SourceUrlCopyMenuItem.Enabled = false;
1021 SourceCopyMenuItem.Enabled = true;
1022 SourceUrlCopyMenuItem.Enabled = true;
1026 private void SourceCopyMenuItem_Click(object sender, EventArgs e)
1028 if (this.CurrentPost == null)
1033 Clipboard.SetDataObject(this.CurrentPost.Source, false, 5, 100);
1035 catch (Exception ex)
1037 MessageBox.Show(ex.Message);
1041 private void SourceUrlCopyMenuItem_Click(object sender, EventArgs e)
1043 var sourceUri = this.CurrentPost?.SourceUri;
1044 if (sourceUri == null)
1049 Clipboard.SetDataObject(sourceUri.AbsoluteUri, false, 5, 100);
1051 catch (Exception ex)
1053 MessageBox.Show(ex.Message);
1060 public class TweetDetailsViewStatusChengedEventArgs : EventArgs
1062 /// <summary>ステータスバーに表示するテキスト</summary>
1064 /// 空文字列の場合は <see cref="TweenMain"/> の既定のテキストを表示する
1066 public string StatusText { get; }
1068 public TweetDetailsViewStatusChengedEventArgs(string statusText)
1069 => this.StatusText = statusText;