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.
28 using System.Collections.Generic;
29 using System.ComponentModel;
34 using System.Threading;
35 using System.Threading.Tasks;
36 using System.Windows.Forms;
37 using System.Text.RegularExpressions;
42 using OpenTween.Connection;
46 public partial class UserInfoDialog : OTBaseForm
48 private TwitterUser _displayUser;
49 private CancellationTokenSource cancellationTokenSource = null;
51 private readonly TweenMain mainForm;
52 private readonly Twitter twitter;
54 public UserInfoDialog(TweenMain mainForm, Twitter twitter)
56 this.mainForm = mainForm;
57 this.twitter = twitter;
59 InitializeComponent();
61 // LabelScreenName のフォントを OTBaseForm.GlobalFont に変更
62 this.LabelScreenName.Font = this.ReplaceToGlobalFont(this.LabelScreenName.Font);
65 private void CancelLoading()
67 var newTokenSource = new CancellationTokenSource();
68 var oldTokenSource = Interlocked.Exchange(ref this.cancellationTokenSource, newTokenSource);
70 if (oldTokenSource != null)
72 oldTokenSource.Cancel();
73 oldTokenSource.Dispose();
77 public async Task ShowUserAsync(TwitterUser user)
82 if (user == null || user == this._displayUser)
87 var cancellationToken = this.cancellationTokenSource.Token;
89 this._displayUser = user;
91 this.LabelId.Text = user.IdStr;
92 this.LabelScreenName.Text = user.ScreenName;
93 this.LabelName.Text = user.Name;
94 this.LabelLocation.Text = user.Location ?? "";
95 this.LabelCreatedAt.Text = MyCommon.DateTimeParse(user.CreatedAt).ToString();
98 this.LabelIsProtected.Text = Properties.Resources.Yes;
100 this.LabelIsProtected.Text = Properties.Resources.No;
103 this.LabelIsVerified.Text = Properties.Resources.Yes;
105 this.LabelIsVerified.Text = Properties.Resources.No;
107 var followingUrl = "https://twitter.com/" + user.ScreenName + "/following";
108 this.LinkLabelFollowing.Text = user.FriendsCount.ToString();
109 this.LinkLabelFollowing.Tag = followingUrl;
110 this.ToolTip1.SetToolTip(this.LinkLabelFollowing, followingUrl);
112 var followersUrl = "https://twitter.com/" + user.ScreenName + "/followers";
113 this.LinkLabelFollowers.Text = user.FollowersCount.ToString();
114 this.LinkLabelFollowers.Tag = followersUrl;
115 this.ToolTip1.SetToolTip(this.LinkLabelFollowers, followersUrl);
117 var favoritesUrl = "https://twitter.com/" + user.ScreenName + "/favorites";
118 this.LinkLabelFav.Text = user.FavouritesCount.ToString();
119 this.LinkLabelFav.Tag = favoritesUrl;
120 this.ToolTip1.SetToolTip(this.LinkLabelFav, favoritesUrl);
122 var profileUrl = "https://twitter.com/" + user.ScreenName;
123 this.LinkLabelTweet.Text = user.StatusesCount.ToString();
124 this.LinkLabelTweet.Tag = profileUrl;
125 this.ToolTip1.SetToolTip(this.LinkLabelTweet, profileUrl);
127 if (this.twitter.UserId == user.Id)
129 this.ButtonEdit.Enabled = true;
130 this.ChangeIconToolStripMenuItem.Enabled = true;
131 this.ButtonBlock.Enabled = false;
132 this.ButtonReportSpam.Enabled = false;
133 this.ButtonBlockDestroy.Enabled = false;
137 this.ButtonEdit.Enabled = false;
138 this.ChangeIconToolStripMenuItem.Enabled = false;
139 this.ButtonBlock.Enabled = true;
140 this.ButtonReportSpam.Enabled = true;
141 this.ButtonBlockDestroy.Enabled = true;
144 await Task.WhenAll(new[]
146 this.SetDescriptionAsync(user.Description, user.Entities.Description, cancellationToken),
147 this.SetRecentStatusAsync(user.Status, cancellationToken),
148 this.SetLinkLabelWebAsync(user.Url, user.Entities.Url, cancellationToken),
149 this.SetUserImageAsync(user.ProfileImageUrlHttps, cancellationToken),
150 this.LoadFriendshipAsync(user.ScreenName, cancellationToken),
154 private async Task SetDescriptionAsync(string descriptionText, TwitterEntities entities, CancellationToken cancellationToken)
156 if (descriptionText != null)
158 var atlist = new List<string>();
160 // description に含まれる < > " の記号のみエスケープを一旦解除する
161 var decodedText = descriptionText.Replace("<", "<").Replace(">", ">").Replace(""", "\"");
163 var html = WebUtility.HtmlEncode(decodedText);
164 html = await this.twitter.CreateHtmlAnchorAsync(html, atlist, entities, null);
165 html = this.mainForm.createDetailHtml(html);
167 if (cancellationToken.IsCancellationRequested)
170 this.DescriptionBrowser.DocumentText = html;
174 this.DescriptionBrowser.DocumentText = "";
178 private async Task SetUserImageAsync(string imageUri, CancellationToken cancellationToken)
180 var oldImage = this.UserPicture.Image;
181 this.UserPicture.Image = null;
184 // ProfileImageUrlHttps が null になる場合があるらしい
185 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=33871
186 if (imageUri == null)
189 await this.UserPicture.SetImageFromTask(async () =>
191 var uri = imageUri.Replace("_normal", "_bigger");
193 using (var imageStream = await Networking.Http.GetStreamAsync(uri).ConfigureAwait(false))
195 var image = await MemoryImage.CopyFromStreamAsync(imageStream)
196 .ConfigureAwait(false);
198 if (cancellationToken.IsCancellationRequested)
201 throw new OperationCanceledException(cancellationToken);
209 private async Task SetLinkLabelWebAsync(string uri, TwitterEntities entities, CancellationToken cancellationToken)
211 if (entities != null)
213 var expandedUrl = await ShortUrl.Instance.ExpandUrlAsync(entities.Urls[0].ExpandedUrl);
215 if (cancellationToken.IsCancellationRequested)
218 this.LinkLabelWeb.Text = expandedUrl;
219 this.LinkLabelWeb.Tag = expandedUrl;
220 this.ToolTip1.SetToolTip(this.LinkLabelWeb, expandedUrl);
222 else if (uri != null)
224 var expandedUrl = await ShortUrl.Instance.ExpandUrlAsync(uri);
226 if (cancellationToken.IsCancellationRequested)
229 this.LinkLabelWeb.Text = uri;
230 this.LinkLabelWeb.Tag = expandedUrl;
231 this.ToolTip1.SetToolTip(this.LinkLabelWeb, expandedUrl);
235 this.LinkLabelWeb.Text = "";
236 this.LinkLabelWeb.Tag = null;
237 this.ToolTip1.SetToolTip(this.LinkLabelWeb, null);
241 private async Task SetRecentStatusAsync(TwitterStatus status, CancellationToken cancellationToken)
243 var atlist = new List<string>();
247 var html = await this.twitter.CreateHtmlAnchorAsync(status.Text, atlist, status.MergedEntities, null);
248 html = this.mainForm.createDetailHtml(html +
249 " Posted at " + MyCommon.DateTimeParse(status.CreatedAt) +
250 " via " + status.Source);
252 if (cancellationToken.IsCancellationRequested)
255 this.RecentPostBrowser.DocumentText = html;
259 this.RecentPostBrowser.DocumentText = Properties.Resources.ShowUserInfo2;
263 private async Task LoadFriendshipAsync(string screenName, CancellationToken cancellationToken)
265 this.LabelIsFollowing.Text = "";
266 this.LabelIsFollowed.Text = "";
267 this.ButtonFollow.Enabled = false;
268 this.ButtonUnFollow.Enabled = false;
270 if (this.twitter.Username == screenName)
273 TwitterFriendship friendship;
276 friendship = await Task.Run(() => this.twitter.GetFriendshipInfo(screenName));
278 catch (WebApiException)
280 LabelIsFollowed.Text = Properties.Resources.GetFriendshipInfo6;
281 LabelIsFollowing.Text = Properties.Resources.GetFriendshipInfo6;
285 if (cancellationToken.IsCancellationRequested)
288 var isFollowing = friendship.Relationship.Source.Following;
289 var isFollowedBy = friendship.Relationship.Source.FollowedBy;
291 this.LabelIsFollowing.Text = isFollowing
292 ? Properties.Resources.GetFriendshipInfo1
293 : Properties.Resources.GetFriendshipInfo2;
295 this.LabelIsFollowed.Text = isFollowedBy
296 ? Properties.Resources.GetFriendshipInfo3
297 : Properties.Resources.GetFriendshipInfo4;
299 this.ButtonFollow.Enabled = !isFollowing;
300 this.ButtonUnFollow.Enabled = isFollowing;
303 private void ShowUserInfo_Load(object sender, EventArgs e)
305 this.TextBoxName.Location = this.LabelName.Location;
306 this.TextBoxName.Height = this.LabelName.Height;
307 this.TextBoxName.Width = this.LabelName.Width;
308 this.TextBoxName.BackColor = this.mainForm.InputBackColor;
309 this.TextBoxName.MaxLength = 20;
311 this.TextBoxLocation.Location = this.LabelLocation.Location;
312 this.TextBoxLocation.Height = this.LabelLocation.Height;
313 this.TextBoxLocation.Width = this.LabelLocation.Width;
314 this.TextBoxLocation.BackColor = this.mainForm.InputBackColor;
315 this.TextBoxLocation.MaxLength = 30;
317 this.TextBoxWeb.Location = this.LinkLabelWeb.Location;
318 this.TextBoxWeb.Height = this.LinkLabelWeb.Height;
319 this.TextBoxWeb.Width = this.LinkLabelWeb.Width;
320 this.TextBoxWeb.BackColor = this.mainForm.InputBackColor;
321 this.TextBoxWeb.MaxLength = 100;
323 this.TextBoxDescription.Location = this.DescriptionBrowser.Location;
324 this.TextBoxDescription.Height = this.DescriptionBrowser.Height;
325 this.TextBoxDescription.Width = this.DescriptionBrowser.Width;
326 this.TextBoxDescription.BackColor = this.mainForm.InputBackColor;
327 this.TextBoxDescription.MaxLength = 160;
328 this.TextBoxDescription.Multiline = true;
329 this.TextBoxDescription.ScrollBars = ScrollBars.Vertical;
332 private async void LinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
334 var linkLabel = (LinkLabel)sender;
336 var linkUrl = (string)linkLabel.Tag;
340 await this.mainForm.OpenUriInBrowserAsync(linkUrl);
343 private void ButtonFollow_Click(object sender, EventArgs e)
347 this.twitter.PostFollowCommand(this._displayUser.ScreenName);
349 catch (WebApiException ex)
351 MessageBox.Show(Properties.Resources.FRMessage2 + ex.Message);
355 MessageBox.Show(Properties.Resources.FRMessage3);
356 LabelIsFollowing.Text = Properties.Resources.GetFriendshipInfo1;
357 ButtonFollow.Enabled = false;
358 ButtonUnFollow.Enabled = true;
361 private void ButtonUnFollow_Click(object sender, EventArgs e)
363 if (MessageBox.Show(this._displayUser.ScreenName + Properties.Resources.ButtonUnFollow_ClickText1,
364 Properties.Resources.ButtonUnFollow_ClickText2,
365 MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
369 this.twitter.PostRemoveCommand(this._displayUser.ScreenName);
371 catch (WebApiException ex)
373 MessageBox.Show(Properties.Resources.FRMessage2 + ex.Message);
377 MessageBox.Show(Properties.Resources.FRMessage3);
378 LabelIsFollowing.Text = Properties.Resources.GetFriendshipInfo2;
379 ButtonFollow.Enabled = true;
380 ButtonUnFollow.Enabled = false;
384 private void ShowUserInfo_Activated(object sender, EventArgs e)
386 //画面が他画面の裏に隠れると、アイコン画像が再描画されない問題の対応
387 if (UserPicture.Image != null)
388 UserPicture.Invalidate(false);
391 private void ShowUserInfo_Shown(object sender, EventArgs e)
396 private async void WebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
398 if (e.Url.AbsoluteUri != "about:blank")
402 // Ctrlを押しながらリンクを開いた場合は、設定と逆の動作をするフラグを true としておく
403 await this.mainForm.OpenUriAsync(e.Url, MyCommon.IsKeyDown(Keys.Control));
407 private void SelectAllToolStripMenuItem_Click(object sender, EventArgs e)
409 var browser = (WebBrowser)this.ContextMenuRecentPostBrowser.SourceControl;
410 browser.Document.ExecCommand("SelectAll", false, null);
413 private void SelectionCopyToolStripMenuItem_Click(object sender, EventArgs e)
415 var browser = (WebBrowser)this.ContextMenuRecentPostBrowser.SourceControl;
416 var selectedText = browser.GetSelectedText();
417 if (selectedText != null)
421 Clipboard.SetDataObject(selectedText, false, 5, 100);
425 MessageBox.Show(ex.Message);
430 private void ContextMenuRecentPostBrowser_Opening(object sender, CancelEventArgs e)
432 var browser = (WebBrowser)this.ContextMenuRecentPostBrowser.SourceControl;
433 var selectedText = browser.GetSelectedText();
435 this.SelectionCopyToolStripMenuItem.Enabled = selectedText != null;
438 private async void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
440 await this.mainForm.OpenUriInBrowserAsync("https://support.twitter.com/groups/31-twitter-basics/topics/111-features/articles/268350-x8a8d-x8a3c-x6e08-x307f-x30a2-x30ab-x30a6-x30f3-x30c8-x306b-x3064-x3044-x3066");
443 private async void LinkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
445 await this.mainForm.OpenUriInBrowserAsync("https://support.twitter.com/groups/31-twitter-basics/topics/107-my-profile-account-settings/articles/243055-x516c-x958b-x3001-x975e-x516c-x958b-x30a2-x30ab-x30a6-x30f3-x30c8-x306b-x3064-x3044-x3066");
448 private void ButtonSearchPosts_Click(object sender, EventArgs e)
450 this.mainForm.AddNewTabForUserTimeline(this._displayUser.ScreenName);
453 private async void UserPicture_Click(object sender, EventArgs e)
455 var imageUrl = this._displayUser.ProfileImageUrlHttps;
456 imageUrl = imageUrl.Remove(imageUrl.LastIndexOf("_normal"), 7);
458 await this.mainForm.OpenUriInBrowserAsync(imageUrl);
461 private bool IsEditing = false;
462 private string ButtonEditText = "";
464 private async void ButtonEdit_Click(object sender, EventArgs e)
466 // 自分以外のプロフィールは変更できない
467 if (this.twitter.UserId != this._displayUser.Id)
470 using (ControlTransaction.Disabled(this.ButtonEdit))
474 ButtonEditText = ButtonEdit.Text;
475 ButtonEdit.Text = Properties.Resources.UserInfoButtonEdit_ClickText1;
477 TextBoxName.Text = LabelName.Text;
478 TextBoxName.Enabled = true;
479 TextBoxName.Visible = true;
480 LabelName.Visible = false;
482 TextBoxLocation.Text = LabelLocation.Text;
483 TextBoxLocation.Enabled = true;
484 TextBoxLocation.Visible = true;
485 LabelLocation.Visible = false;
487 TextBoxWeb.Text = this._displayUser.Url;
488 TextBoxWeb.Enabled = true;
489 TextBoxWeb.Visible = true;
490 LinkLabelWeb.Visible = false;
492 TextBoxDescription.Text = this._displayUser.Description;
493 TextBoxDescription.Enabled = true;
494 TextBoxDescription.Visible = true;
495 DescriptionBrowser.Visible = false;
498 TextBoxName.Select(TextBoxName.Text.Length, 0);
504 Task showUserTask = null;
506 if (TextBoxName.Modified ||
507 TextBoxLocation.Modified ||
508 TextBoxWeb.Modified ||
509 TextBoxDescription.Modified)
513 var user = await Task.Run(() =>
514 this.twitter.PostUpdateProfile(
515 this.TextBoxName.Text,
516 this.TextBoxWeb.Text,
517 this.TextBoxLocation.Text,
518 this.TextBoxDescription.Text));
520 showUserTask = this.ShowUserAsync(user);
522 catch (WebApiException ex)
524 MessageBox.Show(ex.Message);
529 TextBoxName.Enabled = false;
530 TextBoxName.Visible = false;
531 LabelName.Visible = true;
533 TextBoxLocation.Enabled = false;
534 TextBoxLocation.Visible = false;
535 LabelLocation.Visible = true;
537 TextBoxWeb.Enabled = false;
538 TextBoxWeb.Visible = false;
539 LinkLabelWeb.Visible = true;
541 TextBoxDescription.Enabled = false;
542 TextBoxDescription.Visible = false;
543 DescriptionBrowser.Visible = true;
545 ButtonEdit.Text = ButtonEditText;
549 if (showUserTask != null)
555 private async Task DoChangeIcon(string filename)
559 await Task.Run(() => this.twitter.PostUpdateProfileImage(filename));
561 catch (WebApiException ex)
563 // "Err:"が付いたエラーメッセージが返ってくる
564 MessageBox.Show(ex.Message + Environment.NewLine + Properties.Resources.ChangeIconToolStripMenuItem_ClickText4);
568 MessageBox.Show(Properties.Resources.ChangeIconToolStripMenuItem_ClickText5);
572 var user = await Task.Run(() => this.twitter.GetUserInfo(this._displayUser.ScreenName));
575 await this.ShowUserAsync(user);
577 catch (WebApiException ex)
579 MessageBox.Show(ex.Message);
583 private async void ChangeIconToolStripMenuItem_Click(object sender, EventArgs e)
585 OpenFileDialogIcon.Filter = Properties.Resources.ChangeIconToolStripMenuItem_ClickText1;
586 OpenFileDialogIcon.Title = Properties.Resources.ChangeIconToolStripMenuItem_ClickText2;
587 OpenFileDialogIcon.FileName = "";
589 DialogResult rslt = OpenFileDialogIcon.ShowDialog();
591 if (rslt != DialogResult.OK)
596 string fn = OpenFileDialogIcon.FileName;
597 if (this.IsValidIconFile(new FileInfo(fn)))
599 await this.DoChangeIcon(fn);
603 MessageBox.Show(Properties.Resources.ChangeIconToolStripMenuItem_ClickText6);
607 private void ButtonBlock_Click(object sender, EventArgs e)
609 if (MessageBox.Show(this._displayUser.ScreenName + Properties.Resources.ButtonBlock_ClickText1,
610 Properties.Resources.ButtonBlock_ClickText2,
611 MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
615 this.twitter.PostCreateBlock(this._displayUser.ScreenName);
617 catch (WebApiException ex)
619 MessageBox.Show(ex.Message + Environment.NewLine + Properties.Resources.ButtonBlock_ClickText3);
623 MessageBox.Show(Properties.Resources.ButtonBlock_ClickText4);
627 private void ButtonReportSpam_Click(object sender, EventArgs e)
629 if (MessageBox.Show(this._displayUser.ScreenName + Properties.Resources.ButtonReportSpam_ClickText1,
630 Properties.Resources.ButtonReportSpam_ClickText2,
631 MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
635 this.twitter.PostReportSpam(this._displayUser.ScreenName);
637 catch (WebApiException ex)
639 MessageBox.Show(ex.Message + Environment.NewLine + Properties.Resources.ButtonReportSpam_ClickText3);
643 MessageBox.Show(Properties.Resources.ButtonReportSpam_ClickText4);
647 private void ButtonBlockDestroy_Click(object sender, EventArgs e)
649 if (MessageBox.Show(this._displayUser.ScreenName + Properties.Resources.ButtonBlockDestroy_ClickText1,
650 Properties.Resources.ButtonBlockDestroy_ClickText2,
651 MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
655 this.twitter.PostDestroyBlock(this._displayUser.ScreenName);
657 catch (WebApiException ex)
659 MessageBox.Show(ex.Message + Environment.NewLine + Properties.Resources.ButtonBlockDestroy_ClickText3);
663 MessageBox.Show(Properties.Resources.ButtonBlockDestroy_ClickText4);
667 private bool IsValidExtension(string ext)
671 return ext.Equals(".jpg", StringComparison.Ordinal) ||
672 ext.Equals(".jpeg", StringComparison.Ordinal) ||
673 ext.Equals(".png", StringComparison.Ordinal) ||
674 ext.Equals(".gif", StringComparison.Ordinal);
677 private bool IsValidIconFile(FileInfo info)
679 return this.IsValidExtension(info.Extension) &&
680 info.Length < 700 * 1024 &&
681 !MyCommon.IsAnimatedGif(info.FullName);
684 private void ShowUserInfo_DragEnter(object sender, DragEventArgs e)
686 if (e.Data.GetDataPresent(DataFormats.FileDrop) &&
687 !e.Data.GetDataPresent(DataFormats.Html, false)) // WebBrowserコントロールからの絵文字画像D&Dは弾く
689 var files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
690 if (files.Length != 1)
693 var finfo = new FileInfo(files[0]);
694 if (this.IsValidIconFile(finfo))
696 e.Effect = DragDropEffects.Copy;
701 private async void ShowUserInfo_DragDrop(object sender, DragEventArgs e)
703 if (e.Data.GetDataPresent(DataFormats.FileDrop) &&
704 !e.Data.GetDataPresent(DataFormats.Html, false)) // WebBrowserコントロールからの絵文字画像D&Dは弾く
706 var ret = MessageBox.Show(this, Properties.Resources.ChangeIconToolStripMenuItem_Confirm,
707 Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
708 if (ret != DialogResult.OK)
711 string filename = ((string[])e.Data.GetData(DataFormats.FileDrop, false))[0];
712 await this.DoChangeIcon(filename);
716 protected override void Dispose(bool disposing)
720 var cts = this.cancellationTokenSource;
724 var oldImage = this.UserPicture.Image;
725 this.UserPicture.Image = null;
728 this.components?.Dispose();
731 base.Dispose(disposing);