OSDN Git Service

261247e785cabecf1f49d6862cc347e2ef62703e
[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 object uiLockObj = new object();
48
49         public ThumbnailInfo Thumbnail
50         {
51             get { return this.pictureBox[this.scrollBar.Value].Tag as ThumbnailInfo; }
52         }
53
54         public TweetThumbnail()
55         {
56             InitializeComponent();
57         }
58
59         public Task ShowThumbnailAsync(PostClass post)
60         {
61             return this.ShowThumbnailAsync(post, CancellationToken.None);
62         }
63
64         public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
65         {
66             var loadTasks = new List<Task>();
67
68             this.scrollBar.Enabled = false;
69
70             var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
71                 .ToArray();
72
73             cancelToken.ThrowIfCancellationRequested();
74
75             lock (this.uiLockObj)
76             {
77                 this.SetThumbnailCount(thumbnails.Length);
78                 if (thumbnails.Length == 0) return;
79
80                 for (int i = 0; i < thumbnails.Length; i++)
81                 {
82                     var thumb = thumbnails[i];
83                     var picbox = this.pictureBox[i];
84
85                     picbox.Tag = thumb;
86                     picbox.ContextMenu = CreateContextMenu(thumb);
87
88                     var loadTask = this.SetThumbnailImageAsync(picbox, thumb, cancelToken);
89                     loadTasks.Add(loadTask);
90
91                     var tooltipText = thumb.TooltipText;
92                     if (!string.IsNullOrEmpty(tooltipText))
93                     {
94                         this.toolTip.SetToolTip(picbox, tooltipText);
95                     }
96
97                     cancelToken.ThrowIfCancellationRequested();
98                 }
99
100                 if (thumbnails.Length > 1)
101                     this.scrollBar.Enabled = true;
102             }
103
104             if (this.ThumbnailLoading != null)
105                 this.ThumbnailLoading(this, EventArgs.Empty);
106
107             await Task.WhenAll(loadTasks).ConfigureAwait(false);
108         }
109
110         private async Task SetThumbnailImageAsync(OTPictureBox picbox, ThumbnailInfo thumbInfo,
111             CancellationToken cancelToken)
112         {
113             try
114             {
115                 picbox.ShowInitialImage();
116                 picbox.Image = await thumbInfo.LoadThumbnailImageAsync(cancelToken);
117             }
118             catch (Exception)
119             {
120                 picbox.ShowErrorImage();
121                 try
122                 {
123                     throw;
124                 }
125                 catch (HttpRequestException) { }
126                 catch (InvalidImageException) { }
127                 catch (TaskCanceledException) { }
128             }
129         }
130
131         private ContextMenu CreateContextMenu(ThumbnailInfo thumb)
132         {
133             var contextMenu = new ContextMenu();
134             contextMenu.MenuItems.Add(CreateImageSearchMenuItem(thumb));
135             return contextMenu;
136         }
137
138         private MenuItem CreateImageSearchMenuItem(ThumbnailInfo thumb)
139         {
140             var item = new MenuItem();
141             item.Text = Properties.Resources.SearchSimilarImageText;
142             var search_targe_url = thumb.FullSizeImageUrl ?? thumb.ThumbnailUrl ?? null;
143
144             if (search_targe_url != null)
145             {
146                 item.Click += (sender, e) =>
147                 {
148                     string uri = GetImageSearchUri(search_targe_url);
149                     if (this.ThumbnailImageSearchClick != null)
150                     {
151                         this.ThumbnailImageSearchClick(this, new ThumbnailImageSearchEventArgs(uri));
152                     }
153                 };
154             }
155             else
156             {
157                 item.Enabled = false;
158             }
159
160             return item;
161         }
162
163         private string GetImageSearchUri(string image_uri)
164         {
165             return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
166         }
167
168         protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
169         {
170             return ThumbnailGenerator.GetThumbnailsAsync(post, token);
171         }
172
173         /// <summary>
174         /// 表示するサムネイルの数を設定する
175         /// </summary>
176         /// <param name="count">表示するサムネイルの数</param>
177         protected void SetThumbnailCount(int count)
178         {
179             using (ControlTransaction.Layout(this, false))
180             {
181                 this.panelPictureBox.Controls.Clear();
182                 foreach (var picbox in this.pictureBox)
183                 {
184                     var memoryImage = picbox.Image;
185                     var contextMenu = picbox.ContextMenu;
186                     picbox.Dispose();
187
188                     if (memoryImage != null)
189                         memoryImage.Dispose();
190                     if (contextMenu != null)
191                         contextMenu.Dispose();
192                 }
193                 this.pictureBox.Clear();
194
195                 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
196                 this.scrollBar.Value = 0;
197
198                 for (int i = 0; i < count; i++)
199                 {
200                     var picbox = CreatePictureBox("pictureBox" + i);
201                     picbox.Visible = (i == 0);
202                     picbox.DoubleClick += this.pictureBox_DoubleClick;
203
204                     this.panelPictureBox.Controls.Add(picbox);
205                     this.pictureBox.Add(picbox);
206                 }
207             }
208         }
209
210         protected virtual OTPictureBox CreatePictureBox(string name)
211         {
212             return new OTPictureBox()
213             {
214                 Name = name,
215                 SizeMode = PictureBoxSizeMode.Zoom,
216                 WaitOnLoad = false,
217                 Dock = DockStyle.Fill,
218             };
219         }
220
221         public void ScrollUp()
222         {
223             var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
224
225             if (newval > this.scrollBar.Maximum)
226                 newval = this.scrollBar.Maximum;
227
228             this.scrollBar.Value = newval;
229         }
230
231         public void ScrollDown()
232         {
233             var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
234
235             if (newval < this.scrollBar.Minimum)
236                 newval = this.scrollBar.Minimum;
237
238             this.scrollBar.Value = newval;
239         }
240
241         private void scrollBar_ValueChanged(object sender, EventArgs e)
242         {
243             using (ControlTransaction.Layout(this, false))
244             {
245                 var value = this.scrollBar.Value;
246                 for (var i = 0; i < this.pictureBox.Count; i++)
247                 {
248                     this.pictureBox[i].Visible = (i == value);
249                 }
250             }
251         }
252
253         private void pictureBox_DoubleClick(object sender, EventArgs e)
254         {
255             var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
256
257             if (thumb == null) return;
258
259             if (this.ThumbnailDoubleClick != null)
260             {
261                 this.ThumbnailDoubleClick(this, new ThumbnailDoubleClickEventArgs(thumb));
262             }
263         }
264     }
265
266     public class ThumbnailDoubleClickEventArgs : EventArgs
267     {
268         public ThumbnailInfo Thumbnail { get; private set; }
269
270         public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
271         {
272             this.Thumbnail = thumbnail;
273         }
274     }
275
276     public class ThumbnailImageSearchEventArgs : EventArgs
277     {
278         public string ImageUrl { get; private set; }
279
280         public ThumbnailImageSearchEventArgs(string url)
281         {
282             this.ImageUrl = url;
283         }
284     }
285 }