OSDN Git Service

TweetThumbnail内でHttpClientインスタンスを保持する
[opentween/open-tween.git] / OpenTween / TweetThumbnail.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.ComponentModel;
25 using System.Drawing;
26 using System.Data;
27 using System.Linq;
28 using System.Net;
29 using System.Net.Http;
30 using System.Text;
31 using System.Windows.Forms;
32 using System.Text.RegularExpressions;
33 using System.Threading.Tasks;
34 using OpenTween.Thumbnail;
35 using System.Threading;
36
37 namespace OpenTween
38 {
39     public partial class TweetThumbnail : UserControl
40     {
41         protected internal List<OTPictureBox> pictureBox = new List<OTPictureBox>();
42
43         public event EventHandler ThumbnailLoading;
44         public event EventHandler<ThumbnailDoubleClickEventArgs> ThumbnailDoubleClick;
45         public event EventHandler<ThumbnailImageSearchEventArgs> ThumbnailImageSearchClick;
46
47         private readonly HttpClient http = MyCommon.CreateHttpClient();
48
49         private object uiLockObj = new object();
50
51         public ThumbnailInfo Thumbnail
52         {
53             get { return this.pictureBox[this.scrollBar.Value].Tag as ThumbnailInfo; }
54         }
55
56         public TweetThumbnail()
57         {
58             InitializeComponent();
59         }
60
61         public Task ShowThumbnailAsync(PostClass post)
62         {
63             return this.ShowThumbnailAsync(post, CancellationToken.None);
64         }
65
66         public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
67         {
68             var loadTasks = new List<Task>();
69
70             this.scrollBar.Enabled = false;
71
72             var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
73                 .ToArray();
74
75             cancelToken.ThrowIfCancellationRequested();
76
77             lock (this.uiLockObj)
78             {
79                 this.SetThumbnailCount(thumbnails.Length);
80                 if (thumbnails.Length == 0) return;
81
82                 for (int i = 0; i < thumbnails.Length; i++)
83                 {
84                     var thumb = thumbnails[i];
85                     var picbox = this.pictureBox[i];
86
87                     picbox.Tag = thumb;
88                     picbox.ContextMenuStrip = this.contextMenuStrip;
89
90                     var loadTask = picbox.SetImageFromTask(() => thumb.LoadThumbnailImageAsync(this.http, cancelToken));
91                     loadTasks.Add(loadTask);
92
93                     var tooltipText = thumb.TooltipText;
94                     if (!string.IsNullOrEmpty(tooltipText))
95                     {
96                         this.toolTip.SetToolTip(picbox, tooltipText);
97                     }
98
99                     cancelToken.ThrowIfCancellationRequested();
100                 }
101
102                 if (thumbnails.Length > 1)
103                     this.scrollBar.Enabled = true;
104             }
105
106             if (this.ThumbnailLoading != null)
107                 this.ThumbnailLoading(this, EventArgs.Empty);
108
109             await Task.WhenAll(loadTasks).ConfigureAwait(false);
110         }
111
112         private string GetImageSearchUri(string image_uri)
113         {
114             return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
115         }
116
117         protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
118         {
119             return ThumbnailGenerator.GetThumbnailsAsync(post, token);
120         }
121
122         /// <summary>
123         /// 表示するサムネイルの数を設定する
124         /// </summary>
125         /// <param name="count">表示するサムネイルの数</param>
126         protected void SetThumbnailCount(int count)
127         {
128             using (ControlTransaction.Layout(this, false))
129             {
130                 this.panelPictureBox.Controls.Clear();
131                 foreach (var picbox in this.pictureBox)
132                 {
133                     var memoryImage = picbox.Image;
134                     picbox.Dispose();
135
136                     if (memoryImage != null)
137                         memoryImage.Dispose();
138
139                     // メモリリーク対策 (http://stackoverflow.com/questions/2792427#2793714)
140                     picbox.ContextMenuStrip = null;
141                 }
142                 this.pictureBox.Clear();
143
144                 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
145                 this.scrollBar.Value = 0;
146
147                 for (int i = 0; i < count; i++)
148                 {
149                     var picbox = CreatePictureBox("pictureBox" + i);
150                     picbox.Visible = (i == 0);
151                     picbox.DoubleClick += this.pictureBox_DoubleClick;
152
153                     this.panelPictureBox.Controls.Add(picbox);
154                     this.pictureBox.Add(picbox);
155                 }
156             }
157         }
158
159         protected virtual OTPictureBox CreatePictureBox(string name)
160         {
161             return new OTPictureBox()
162             {
163                 Name = name,
164                 SizeMode = PictureBoxSizeMode.Zoom,
165                 WaitOnLoad = false,
166                 Dock = DockStyle.Fill,
167             };
168         }
169
170         public void ScrollUp()
171         {
172             var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
173
174             if (newval > this.scrollBar.Maximum)
175                 newval = this.scrollBar.Maximum;
176
177             this.scrollBar.Value = newval;
178         }
179
180         public void ScrollDown()
181         {
182             var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
183
184             if (newval < this.scrollBar.Minimum)
185                 newval = this.scrollBar.Minimum;
186
187             this.scrollBar.Value = newval;
188         }
189
190         private void scrollBar_ValueChanged(object sender, EventArgs e)
191         {
192             using (ControlTransaction.Layout(this, false))
193             {
194                 var value = this.scrollBar.Value;
195                 for (var i = 0; i < this.pictureBox.Count; i++)
196                 {
197                     this.pictureBox[i].Visible = (i == value);
198                 }
199             }
200         }
201
202         private void pictureBox_DoubleClick(object sender, EventArgs e)
203         {
204             var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
205
206             if (thumb == null) return;
207
208             if (this.ThumbnailDoubleClick != null)
209             {
210                 this.ThumbnailDoubleClick(this, new ThumbnailDoubleClickEventArgs(thumb));
211             }
212         }
213
214         private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
215         {
216             var picbox = (OTPictureBox)this.contextMenuStrip.SourceControl;
217             var thumb = (ThumbnailInfo)picbox.Tag;
218
219             var searchTargetUri = thumb.FullSizeImageUrl ?? thumb.ThumbnailUrl ?? null;
220             if (searchTargetUri != null)
221             {
222                 this.searchSimilarImageMenuItem.Enabled = true;
223                 this.searchSimilarImageMenuItem.Tag = searchTargetUri;
224             }
225             else
226             {
227                 this.searchSimilarImageMenuItem.Enabled = false;
228             }
229         }
230
231         private void searchSimilarImageMenuItem_Click(object sender, EventArgs e)
232         {
233             var searchTargetUri = (string)this.searchSimilarImageMenuItem.Tag;
234             var searchUri = this.GetImageSearchUri(searchTargetUri);
235
236             if (this.ThumbnailImageSearchClick != null)
237                 this.ThumbnailImageSearchClick(this, new ThumbnailImageSearchEventArgs(searchUri));
238         }
239     }
240
241     public class ThumbnailDoubleClickEventArgs : EventArgs
242     {
243         public ThumbnailInfo Thumbnail { get; private set; }
244
245         public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
246         {
247             this.Thumbnail = thumbnail;
248         }
249     }
250
251     public class ThumbnailImageSearchEventArgs : EventArgs
252     {
253         public string ImageUrl { get; private set; }
254
255         public ThumbnailImageSearchEventArgs(string url)
256         {
257             this.ImageUrl = url;
258         }
259     }
260 }