OSDN Git Service

ShowUserInfoクラスのMyOwnerを削除、Owner, Twitterプロパティに置き換え
[opentween/open-tween.git] / OpenTween / ShowUserInfo.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 using System;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Data;
31 using System.Drawing;
32 using System.Linq;
33 using System.Text;
34 using System.Threading.Tasks;
35 using System.Windows.Forms;
36 using System.Text.RegularExpressions;
37 using System.Web;
38 using System.IO;
39 using System.Net;
40
41 namespace OpenTween
42 {
43     public partial class ShowUserInfo : OTBaseForm
44     {
45         private new TweenMain Owner
46         {
47             get { return (TweenMain)base.Owner; }
48         }
49
50         private Twitter Twitter
51         {
52             get { return this.Owner.TwitterInstance; }
53         }
54
55         public ShowUserInfo()
56         {
57             InitializeComponent();
58         }
59         private TwitterDataModel.User userInfo = null;
60         private UserInfo _info = new UserInfo();
61         private Image icondata = null;
62         private List<string> atlist = new List<string>();
63         private string recentPostTxt;
64
65         private const string Mainpath = "https://twitter.com/";
66         private const string Followingpath = "/following";
67         private const string Followerspath = "/followers";
68         private const string Favpath = "/favorites";
69
70         private string Home;
71         private string Following;
72         private string Followers;
73         private string Favorites;
74         private string FriendshipResult = "";
75
76         private void InitPath()
77         {
78             Home = Mainpath + _info.ScreenName;
79             Following = Home + Followingpath;
80             Followers = Home + Followerspath;
81             Favorites = Home + Favpath;
82         }
83
84         private void InitTooltip()
85         {
86             ToolTip1.SetToolTip(LinkLabelTweet, Home);
87             ToolTip1.SetToolTip(LinkLabelFollowing, Following);
88             ToolTip1.SetToolTip(LinkLabelFollowers, Followers);
89             ToolTip1.SetToolTip(LinkLabelFav, Favorites);
90         }
91
92         private bool AnalizeUserInfo(TwitterDataModel.User user)
93         {
94             if (user == null) return false;
95
96             try
97             {
98                 _info.Id = user.Id;
99                 _info.Name = WebUtility.HtmlDecode(user.Name).Trim();
100                 _info.ScreenName = user.ScreenName;
101                 _info.Location = WebUtility.HtmlDecode(user.Location);
102                 _info.Description = WebUtility.HtmlDecode(user.Description);
103                 _info.ImageUrl = new Uri(user.ProfileImageUrlHttps);
104                 _info.Url = user.Url;
105                 _info.Protect = user.Protected;
106                 _info.FriendsCount = user.FriendsCount;
107                 _info.FollowersCount = user.FollowersCount;
108                 _info.FavoriteCount = user.FavouritesCount;
109                 _info.CreatedAt = MyCommon.DateTimeParse(user.CreatedAt);
110                 _info.StatusesCount = user.StatusesCount;
111                 _info.Verified = user.Verified;
112                 try
113                 {
114                     _info.RecentPost = user.Status.Text;
115                     _info.PostCreatedAt = MyCommon.DateTimeParse(user.Status.CreatedAt);
116                     _info.PostSource = user.Status.Source;
117                     if (!_info.PostSource.Contains("</a>"))
118                     {
119                         _info.PostSource += "</a>";
120                     }
121                 }
122                 catch (Exception)
123                 {
124                     _info.RecentPost = null;
125                     _info.PostCreatedAt = new DateTime();
126                     _info.PostSource = null;
127                 }
128             }
129             catch (Exception)
130             {
131                 return false;
132             }
133             return true;
134         }
135
136         private async Task SetLinklabelWebAsync(string data)
137         {
138             string webtext;
139             string jumpto;
140             webtext = this.Twitter.PreProcessUrl("<a href=\"" + data + "\">Dummy</a>");
141             webtext = await ShortUrl.Instance.ExpandUrlHtmlAsync(webtext);
142             jumpto = Regex.Match(webtext, @"<a href=""(?<url>.*?)""").Groups["url"].Value;
143             ToolTip1.SetToolTip(LinkLabelWeb, jumpto);
144             LinkLabelWeb.Tag = jumpto;
145             LinkLabelWeb.Text = data;
146         }
147
148         private async Task SetDescriptionAsync(string descriptionText)
149         {
150             var html = WebUtility.HtmlEncode(descriptionText);
151             html = await this.Twitter.CreateHtmlAnchorAsync(html, this.atlist, null);
152             html = this.Owner.createDetailHtml(html);
153
154             this.DescriptionBrowser.DocumentText = html;
155         }
156
157         private void ShowUserInfo_FormClosed(object sender, FormClosedEventArgs e)
158         {
159             //TweenMain.TopMost = !TweenMain.TopMost;
160             //TweenMain.TopMost = !TweenMain.TopMost;
161         }
162
163         private async void ShowUserInfo_Load(object sender, EventArgs e)
164         {
165             if (!AnalizeUserInfo(userInfo))
166             {
167                 MessageBox.Show(Properties.Resources.ShowUserInfo1);
168                 this.Close();
169                 return;
170             }
171
172             // LabelScreenName のフォントを OTBaseForm.GlobalFont に変更
173             this.LabelScreenName.Font = this.ReplaceToGlobalFont(this.LabelScreenName.Font);
174
175             //アイコンロード
176             BackgroundWorkerImageLoader.RunWorkerAsync();
177
178             InitPath();
179             InitTooltip();
180             this.Text = this.Text.Insert(0, _info.ScreenName + " ");
181             LabelId.Text = _info.Id.ToString();
182             LabelScreenName.Text = _info.ScreenName;
183             LabelName.Text = _info.Name;
184
185             LabelLocation.Text = _info.Location;
186
187             var linkTask = this.SetLinklabelWebAsync(this._info.Url);
188
189             RecentPostBrowser.Visible = false;
190             if (_info.RecentPost != null)
191             {
192                 recentPostTxt = this.Owner.createDetailHtml(
193                      this.Twitter.CreateHtmlAnchor(_info.RecentPost, atlist, userInfo.Status.Entities, null) +
194                      " Posted at " + _info.PostCreatedAt.ToString() +
195                      " via " + _info.PostSource);
196             }
197
198             LinkLabelFollowing.Text = _info.FriendsCount.ToString();
199             LinkLabelFollowers.Text = _info.FollowersCount.ToString();
200             LinkLabelFav.Text = _info.FavoriteCount.ToString();
201             LinkLabelTweet.Text = _info.StatusesCount.ToString();
202
203             LabelCreatedAt.Text = _info.CreatedAt.ToString();
204
205             if (_info.Protect)
206                 LabelIsProtected.Text = Properties.Resources.Yes;
207             else
208                 LabelIsProtected.Text = Properties.Resources.No;
209
210             if (_info.Verified)
211                 LabelIsVerified.Text = Properties.Resources.Yes;
212             else
213                 LabelIsVerified.Text = Properties.Resources.No;
214
215             if (this.Twitter.Username == _info.ScreenName)
216             {
217                 ButtonEdit.Enabled = true;
218                 ChangeIconToolStripMenuItem.Enabled = true;
219                 ButtonBlock.Enabled = false;
220                 ButtonReportSpam.Enabled = false;
221                 ButtonBlockDestroy.Enabled = false;
222             }
223             else
224             {
225                 ButtonEdit.Enabled = false;
226                 ChangeIconToolStripMenuItem.Enabled = false;
227                 ButtonBlock.Enabled = true;
228                 ButtonReportSpam.Enabled = true;
229                 ButtonBlockDestroy.Enabled = true;
230             }
231
232             await linkTask;
233         }
234
235         private void ButtonClose_Click(object sender, EventArgs e)
236         {
237             this.Close();
238         }
239
240         public TwitterDataModel.User User
241         {
242             set { this.userInfo = value; }
243         }
244
245         private void LinkLabelWeb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
246         {
247             if (_info.Url != null)
248                 this.Owner.OpenUriAsync(LinkLabelWeb.Text);
249         }
250
251         private void LinkLabelFollowing_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
252         {
253             this.Owner.OpenUriAsync(Following);
254         }
255
256         private void LinkLabelFollowers_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
257         {
258             this.Owner.OpenUriAsync(Followers);
259         }
260
261         private void LinkLabelTweet_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
262         {
263             this.Owner.OpenUriAsync(Home);
264         }
265
266         private void LinkLabelFav_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
267         {
268             this.Owner.OpenUriAsync(Favorites);
269         }
270
271         private void ButtonFollow_Click(object sender, EventArgs e)
272         {
273             string ret = this.Twitter.PostFollowCommand(_info.ScreenName);
274             if (!string.IsNullOrEmpty(ret))
275             {
276                 MessageBox.Show(Properties.Resources.FRMessage2 + ret);
277             }
278             else
279             {
280                 MessageBox.Show(Properties.Resources.FRMessage3);
281                 LabelIsFollowing.Text = Properties.Resources.GetFriendshipInfo1;
282                 ButtonFollow.Enabled = false;
283                 ButtonUnFollow.Enabled = true;
284             }
285         }
286
287         private void ButtonUnFollow_Click(object sender, EventArgs e)
288         {
289             if (MessageBox.Show(_info.ScreenName + Properties.Resources.ButtonUnFollow_ClickText1,
290                                Properties.Resources.ButtonUnFollow_ClickText2,
291                                MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
292             {
293                 string ret = this.Twitter.PostRemoveCommand(_info.ScreenName);
294                 if (!string.IsNullOrEmpty(ret))
295                 {
296                     MessageBox.Show(Properties.Resources.FRMessage2 + ret);
297                 }
298                 else
299                 {
300                     MessageBox.Show(Properties.Resources.FRMessage3);
301                     LabelIsFollowing.Text = Properties.Resources.GetFriendshipInfo2;
302                     ButtonFollow.Enabled = true;
303                     ButtonUnFollow.Enabled = false;
304                 }
305             }
306         }
307
308         private void ShowUserInfo_Activated(object sender, EventArgs e)
309         {
310             //画面が他画面の裏に隠れると、アイコン画像が再描画されない問題の対応
311             if (UserPicture.Image != null)
312                 UserPicture.Invalidate(false);
313         }
314
315         private void ShowUserInfo_FormClosing(object sender, FormClosingEventArgs e)
316         {
317             UserPicture.Image = null;
318             if (icondata != null)
319                 icondata.Dispose();
320         }
321
322         private void BackgroundWorkerImageLoader_DoWork(object sender, DoWorkEventArgs e)
323         {
324             string name = _info.ImageUrl.ToString();
325             icondata = (new HttpVarious()).GetImage(name.Replace("_normal", "_bigger"));
326             if (this.Twitter.Username == _info.ScreenName) return;
327
328             _info.isFollowing = false;
329             _info.isFollowed = false;
330             FriendshipResult = this.Twitter.GetFriendshipInfo(_info.ScreenName, ref _info.isFollowing, ref _info.isFollowed);
331         }
332
333         private void BackgroundWorkerImageLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
334         {
335             try
336             {
337                 if (icondata != null)
338                     UserPicture.Image = icondata;
339             }
340             catch (Exception)
341             {
342                 UserPicture.Image = null;
343             }
344
345             if (this.Twitter.Username == _info.ScreenName)
346             {
347                 // 自分の場合
348                 LabelIsFollowing.Text = "";
349                 LabelIsFollowed.Text = "";
350                 ButtonFollow.Enabled = false;
351                 ButtonUnFollow.Enabled = false;
352             }
353             else
354             {
355                 if (string.IsNullOrEmpty(FriendshipResult))
356                 {
357                     if (_info.isFollowing)
358                     {
359                         LabelIsFollowing.Text = Properties.Resources.GetFriendshipInfo1;
360                     }
361                     else
362                     {
363                         LabelIsFollowing.Text = Properties.Resources.GetFriendshipInfo2;
364                     }
365                     ButtonFollow.Enabled = !_info.isFollowing;
366                     if (_info.isFollowed)
367                     {
368                         LabelIsFollowed.Text = Properties.Resources.GetFriendshipInfo3;
369                     }
370                     else
371                     {
372                         LabelIsFollowed.Text = Properties.Resources.GetFriendshipInfo4;
373                     }
374                     ButtonUnFollow.Enabled = _info.isFollowing;
375                 }
376                 else
377                 {
378                     MessageBox.Show(FriendshipResult);
379                     ButtonUnFollow.Enabled = false;
380                     ButtonFollow.Enabled = false;
381                     LabelIsFollowed.Text = Properties.Resources.GetFriendshipInfo6;
382                     LabelIsFollowing.Text = Properties.Resources.GetFriendshipInfo6;
383                 }
384             }
385
386         }
387
388         private async void ShowUserInfo_Shown(object sender, EventArgs e)
389         {
390             var descriptionTask = this.SetDescriptionAsync(this._info.Description);
391
392             if (_info.RecentPost != null)
393             {
394                 RecentPostBrowser.DocumentText = recentPostTxt;
395                 RecentPostBrowser.Visible = true;
396             }
397             else
398             {
399                 LabelRecentPost.Text = Properties.Resources.ShowUserInfo2;
400             }
401             ButtonClose.Focus();
402
403             await descriptionTask;
404         }
405
406         private void WebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
407         {
408             if (e.Url.AbsoluteUri != "about:blank")
409             {
410                 e.Cancel = true;
411
412                 if (e.Url.AbsoluteUri.StartsWith("http://twitter.com/search?q=%23") ||
413                     e.Url.AbsoluteUri.StartsWith("https://twitter.com/search?q=%23"))
414                 {
415                     //ハッシュタグの場合は、タブで開く
416                     string urlStr = Uri.UnescapeDataString(e.Url.AbsoluteUri);
417                     string hash = urlStr.Substring(urlStr.IndexOf("#"));
418                     this.Owner.HashSupl.AddItem(hash);
419                     this.Owner.HashMgr.AddHashToHistory(hash.Trim(), false);
420                     this.Owner.AddNewTabForSearch(hash);
421                     return;
422                 }
423                 else
424                 {
425                     Match m = Regex.Match(e.Url.AbsoluteUri, @"^https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)$");
426                     if (AppendSettingDialog.Instance.OpenUserTimeline && m.Success && this.Owner.IsTwitterId(m.Result("${ScreenName}")))
427                     {
428                         this.Owner.AddNewTabForUserTimeline(m.Result("${ScreenName}"));
429                     }
430                     else
431                     {
432                         this.Owner.OpenUriAsync(e.Url.OriginalString);
433                     }
434                 }
435             }
436         }
437
438         private void WebBrowser_StatusTextChanged(object sender, EventArgs e)
439         {
440             WebBrowser ComponentInstance = (WebBrowser)sender;
441             if (ComponentInstance.StatusText.StartsWith("http"))
442             {
443                 ToolTip1.Show(ComponentInstance.StatusText, this, PointToClient(MousePosition));
444             }
445             else if (string.IsNullOrEmpty(DescriptionBrowser.StatusText))
446             {
447                 ToolTip1.Hide(this);
448             }
449         }
450
451         private void SelectAllToolStripMenuItem_Click(object sender, EventArgs e)
452         {
453             WebBrowser sc = ContextMenuRecentPostBrowser.SourceControl as WebBrowser;
454             if (sc != null)
455                 sc.Document.ExecCommand("SelectAll", false, null);
456         }
457
458         private void SelectionCopyToolStripMenuItem_Click(object sender, EventArgs e)
459         {
460             WebBrowser sc = ContextMenuRecentPostBrowser.SourceControl as WebBrowser;
461             if (sc != null)
462             {
463                 string _selText = this.Owner.WebBrowser_GetSelectionText(ref sc);
464                 if (_selText != null)
465                 {
466                     try
467                     {
468                         Clipboard.SetDataObject(_selText, false, 5, 100);
469                     }
470                     catch (Exception ex)
471                     {
472                         MessageBox.Show(ex.Message);
473                     }
474                 }
475             }
476         }
477
478         private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
479         {
480             WebBrowser sc = ContextMenuRecentPostBrowser.SourceControl as WebBrowser;
481             if (sc != null)
482             {
483                 string _selText = this.Owner.WebBrowser_GetSelectionText(ref sc);
484                 if (_selText == null)
485                     SelectionCopyToolStripMenuItem.Enabled = false;
486                 else
487                     SelectionCopyToolStripMenuItem.Enabled = true;
488             }
489         }
490
491         private void ShowUserInfo_MouseEnter(object sender, EventArgs e)
492         {
493             ToolTip1.Hide(this);
494         }
495
496         private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
497         {
498             this.Owner.OpenUriAsync("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");
499         }
500
501         private void LinkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
502         {
503             this.Owner.OpenUriAsync("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");
504         }
505
506         private void ButtonSearchPosts_Click(object sender, EventArgs e)
507         {
508             this.Owner.AddNewTabForUserTimeline(_info.ScreenName);
509         }
510
511         private void UserPicture_DoubleClick(object sender, EventArgs e)
512         {
513             if (UserPicture.Image != null)
514             {
515                 string name = _info.ImageUrl.ToString();
516                 this.Owner.OpenUriAsync(name.Remove(name.LastIndexOf("_normal"), 7));
517             }
518         }
519
520         private void UserPicture_MouseEnter(object sender, EventArgs e)
521         {
522             UserPicture.Cursor = Cursors.Hand;
523         }
524
525         private void UserPicture_MouseLeave(object sender, EventArgs e)
526         {
527             UserPicture.Cursor = Cursors.Default;
528         }
529
530         private class UpdateProfileArgs
531         {
532             public Twitter tw;
533             public string name;
534             public string location;
535             public string url;
536             public string description;
537         }
538
539         private void UpdateProfile_Dowork(object sender, DoWorkEventArgs e)
540         {
541             UpdateProfileArgs arg = (UpdateProfileArgs)e.Argument;
542             e.Result = arg.tw.PostUpdateProfile(arg.name,
543                                                 arg.url,
544                                                 arg.location,
545                                                 arg.description);
546         }
547
548         private void UpddateProfile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
549         {
550             string res = (string)e.Result;
551             if (res.StartsWith("err:", StringComparison.CurrentCultureIgnoreCase))
552             {
553                 MessageBox.Show(res);
554             }
555         }
556
557         private bool IsEditing = false;
558         private string ButtonEditText = "";
559
560         private async void ButtonEdit_Click(object sender, EventArgs e)
561         {
562             // 自分以外のプロフィールは変更できない
563             if (this.Twitter.Username != _info.ScreenName) return;
564
565             this.ButtonEdit.Enabled = false;
566
567             if (!IsEditing)
568             {
569                 ButtonEditText = ButtonEdit.Text;
570                 ButtonEdit.Text = Properties.Resources.UserInfoButtonEdit_ClickText1;
571
572                 //座標初期化,プロパティ設定
573                 TextBoxName.Location = LabelName.Location;
574                 TextBoxName.Height = LabelName.Height;
575                 TextBoxName.Width = LabelName.Width;
576                 TextBoxName.BackColor = this.Owner.InputBackColor;
577                 TextBoxName.MaxLength = 20;
578                 TextBoxName.Text = LabelName.Text;
579                 TextBoxName.TabStop = true;
580                 TextBoxName.Visible = true;
581                 LabelName.Visible = false;
582
583                 TextBoxLocation.Location = LabelLocation.Location;
584                 TextBoxLocation.Height = LabelLocation.Height;
585                 TextBoxLocation.Width = LabelLocation.Width;
586                 TextBoxLocation.BackColor = this.Owner.InputBackColor;
587                 TextBoxLocation.MaxLength = 30;
588                 TextBoxLocation.Text = LabelLocation.Text;
589                 TextBoxLocation.TabStop = true;
590                 TextBoxLocation.Visible = true;
591                 LabelLocation.Visible = false;
592
593                 TextBoxWeb.Location = LinkLabelWeb.Location;
594                 TextBoxWeb.Height = LinkLabelWeb.Height;
595                 TextBoxWeb.Width = LinkLabelWeb.Width;
596                 TextBoxWeb.BackColor = this.Owner.InputBackColor;
597                 TextBoxWeb.MaxLength = 100;
598                 TextBoxWeb.Text = _info.Url;
599                 TextBoxWeb.TabStop = true;
600                 TextBoxWeb.Visible = true;
601                 LinkLabelWeb.Visible = false;
602
603                 TextBoxDescription.Location = DescriptionBrowser.Location;
604                 TextBoxDescription.Height = DescriptionBrowser.Height;
605                 TextBoxDescription.Width = DescriptionBrowser.Width;
606                 TextBoxDescription.BackColor = this.Owner.InputBackColor;
607                 TextBoxDescription.MaxLength = 160;
608                 TextBoxDescription.Text = _info.Description;
609                 TextBoxDescription.Multiline = true;
610                 TextBoxDescription.ScrollBars = ScrollBars.Vertical;
611                 TextBoxDescription.TabStop = true;
612                 TextBoxDescription.Visible = true;
613                 DescriptionBrowser.Visible = false;
614
615                 TextBoxName.Focus();
616                 TextBoxName.Select(TextBoxName.Text.Length, 0);
617
618                 IsEditing = true;
619             }
620             else
621             {
622                 UpdateProfileArgs arg = new UpdateProfileArgs();
623
624                 if (TextBoxName.Modified ||
625                     TextBoxLocation.Modified ||
626                     TextBoxWeb.Modified ||
627                     TextBoxDescription.Modified)
628                 {
629                     arg.tw = this.Twitter;
630                     arg.name = TextBoxName.Text.Trim();
631                     arg.url = TextBoxWeb.Text.Trim();
632                     arg.location = TextBoxLocation.Text.Trim();
633                     arg.description = TextBoxDescription.Text.Trim();
634
635                     using (FormInfo dlg = new FormInfo(this, Properties.Resources.UserInfoButtonEdit_ClickText2,
636                                                        UpdateProfile_Dowork,
637                                                        UpddateProfile_RunWorkerCompleted,
638                                                        arg))
639                     {
640                         dlg.ShowDialog();
641                         if (!string.IsNullOrEmpty(dlg.Result.ToString()))
642                         {
643                             return;
644                         }
645                     }
646                 }
647
648
649                 LabelName.Text = TextBoxName.Text;
650                 _info.Name = LabelName.Text;
651                 TextBoxName.TabStop = false;
652                 TextBoxName.Visible = false;
653                 LabelName.Visible = true;
654
655                 LabelLocation.Text = TextBoxLocation.Text;
656                 _info.Location = LabelLocation.Text;
657                 TextBoxLocation.TabStop = false;
658                 TextBoxLocation.Visible = false;
659                 LabelLocation.Visible = true;
660
661                 var linkTask = this.SetLinklabelWebAsync(this.TextBoxWeb.Text);
662                 _info.Url = TextBoxWeb.Text;
663                 TextBoxWeb.TabStop = false;
664                 TextBoxWeb.Visible = false;
665                 LinkLabelWeb.Visible = true;
666
667                 var descriptionTask = this.SetDescriptionAsync(this.TextBoxDescription.Text);
668
669                 _info.Description = TextBoxDescription.Text;
670                 TextBoxDescription.TabStop = false;
671                 TextBoxDescription.Visible = false;
672                 DescriptionBrowser.Visible = true;
673
674                 ButtonEdit.Text = ButtonEditText;
675
676                 IsEditing = false;
677
678                 await Task.WhenAll(new[] { linkTask, descriptionTask });
679             }
680
681             this.ButtonEdit.Enabled = true;
682         }
683
684         class UpdateProfileImageArgs
685         {
686             public Twitter tw;
687             public string FileName;
688         }
689
690         private void UpdateProfileImage_Dowork(object sender, DoWorkEventArgs e)
691         {
692             UpdateProfileImageArgs arg = (UpdateProfileImageArgs)e.Argument;
693             e.Result = arg.tw.PostUpdateProfileImage(arg.FileName);
694         }
695
696         private void UpdateProfileImage_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
697         {
698             string res = "";
699             TwitterDataModel.User user = null;
700
701             if (e.Result == null)
702             {
703                 return;
704             }
705
706
707             // アイコンを取得してみる
708             // が、古いアイコンのユーザーデータが返ってくるため反映/判断できない
709
710             try
711             {
712                 res = this.Twitter.GetUserInfo(_info.ScreenName, ref user);
713                 Image img = (new HttpVarious()).GetImage(user.ProfileImageUrlHttps);
714                 if (img != null)
715                 {
716                     UserPicture.Image = img;
717                 }
718             }
719             catch (Exception)
720             {
721             }
722         }
723
724         private void doChangeIcon(string filename)
725         {
726             string res = "";
727             UpdateProfileImageArgs arg = new UpdateProfileImageArgs() { tw = this.Twitter, FileName = filename };
728
729             using (FormInfo dlg = new FormInfo(this, Properties.Resources.ChangeIconToolStripMenuItem_ClickText3,
730                                                UpdateProfileImage_Dowork,
731                                                UpdateProfileImage_RunWorkerCompleted,
732                                                arg))
733             {
734                 dlg.ShowDialog();
735                 res = dlg.Result as string;
736                 if (!string.IsNullOrEmpty(res))
737                 {
738                     // "Err:"が付いたエラーメッセージが返ってくる
739                     MessageBox.Show(res + "\r\n" + Properties.Resources.ChangeIconToolStripMenuItem_ClickText4);
740                 }
741                 else
742                 {
743                     MessageBox.Show(Properties.Resources.ChangeIconToolStripMenuItem_ClickText5);
744                 }
745             }
746         }
747
748         private void ChangeIconToolStripMenuItem_Click(object sender, EventArgs e)
749         {
750             OpenFileDialogIcon.Filter = Properties.Resources.ChangeIconToolStripMenuItem_ClickText1;
751             OpenFileDialogIcon.Title = Properties.Resources.ChangeIconToolStripMenuItem_ClickText2;
752             OpenFileDialogIcon.FileName = "";
753
754             DialogResult rslt = OpenFileDialogIcon.ShowDialog();
755
756             if (rslt != DialogResult.OK)
757             {
758                 return;
759             }
760
761             string fn = OpenFileDialogIcon.FileName;
762             if (isValidIconFile(new FileInfo(fn)))
763             {
764                 doChangeIcon(fn);
765             }
766             else
767             {
768                 MessageBox.Show(Properties.Resources.ChangeIconToolStripMenuItem_ClickText6);
769             }
770         }
771
772         private void ButtonBlock_Click(object sender, EventArgs e)
773         {
774             if (MessageBox.Show(_info.ScreenName + Properties.Resources.ButtonBlock_ClickText1,
775                                 Properties.Resources.ButtonBlock_ClickText2,
776                                 MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
777             {
778                 string res = this.Twitter.PostCreateBlock(_info.ScreenName);
779                 if (!string.IsNullOrEmpty(res))
780                 {
781                     MessageBox.Show(res + Environment.NewLine + Properties.Resources.ButtonBlock_ClickText3);
782                 }
783                 else
784                 {
785                     MessageBox.Show(Properties.Resources.ButtonBlock_ClickText4);
786                 }
787             }
788         }
789
790         private void ButtonReportSpam_Click(object sender, EventArgs e)
791         {
792             if (MessageBox.Show(_info.ScreenName + Properties.Resources.ButtonReportSpam_ClickText1,
793                                 Properties.Resources.ButtonReportSpam_ClickText2,
794                                 MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
795             {
796                 string res = this.Twitter.PostReportSpam(_info.ScreenName);
797                 if (!string.IsNullOrEmpty(res))
798                 {
799                     MessageBox.Show(res + Environment.NewLine + Properties.Resources.ButtonReportSpam_ClickText3);
800                 }
801                 else
802                 {
803                     MessageBox.Show(Properties.Resources.ButtonReportSpam_ClickText4);
804                 }
805             }
806         }
807
808         private void ButtonBlockDestroy_Click(object sender, EventArgs e)
809         {
810             if (MessageBox.Show(_info.ScreenName + Properties.Resources.ButtonBlockDestroy_ClickText1,
811                                 Properties.Resources.ButtonBlockDestroy_ClickText2,
812                                 MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
813             {
814                 string res = this.Twitter.PostDestroyBlock(_info.ScreenName);
815                 if (!string.IsNullOrEmpty(res))
816                 {
817                     MessageBox.Show(res + Environment.NewLine + Properties.Resources.ButtonBlockDestroy_ClickText3);
818                 }
819                 else
820                 {
821                     MessageBox.Show(Properties.Resources.ButtonBlockDestroy_ClickText4);
822                 }
823             }
824         }
825
826         private bool isValidExtension(string ext)
827         {
828             return ext.Equals(".jpg") || ext.Equals(".jpeg") || ext.Equals(".png") || ext.Equals(".gif");
829         }
830
831         private bool isValidIconFile(FileInfo info)
832         {
833             string ext = info.Extension.ToLower();
834             return isValidExtension(ext) && info.Length < 700 * 1024 && !MyCommon.IsAnimatedGif(info.FullName);
835         }
836
837         private void ShowUserInfo_DragOver(object sender, DragEventArgs e)
838         {
839             if (e.Data.GetDataPresent(DataFormats.FileDrop))
840             {
841                 string filename = ((string[])e.Data.GetData(DataFormats.FileDrop, false))[0];
842                 FileInfo fl = new FileInfo(filename);
843
844                 e.Effect = DragDropEffects.None;
845                 if (isValidIconFile(fl))
846                 {
847                     e.Effect = DragDropEffects.Copy;
848                 }
849             }
850             else
851             {
852                 e.Effect = DragDropEffects.None;
853             }
854         }
855
856         private void ShowUserInfo_DragDrop(object sender, DragEventArgs e)
857         {
858             if (e.Data.GetDataPresent(DataFormats.FileDrop))
859             {
860                 string filename = ((string[])e.Data.GetData(DataFormats.FileDrop, false))[0];
861                 doChangeIcon(filename);
862             }
863         }
864     }
865 }