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 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
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.Net.Http;
29 using System.Runtime.Serialization;
30 using System.Threading.Tasks;
31 using System.Windows.Forms;
36 public class ImageListViewItem : ListViewItem
38 protected readonly ImageCache imageCache;
39 protected readonly string imageUrl;
42 /// 状態表示に使用するアイコンのインデックスを取得・設定する。
45 /// StateImageIndex は不必要な処理が挟まるため、使用しないようにする。
47 public int StateIndex { get; set; }
49 private WeakReference imageReference = new WeakReference(null);
50 private Task imageTask = null;
52 public event EventHandler ImageDownloaded;
54 public ImageListViewItem(string[] items)
55 : this(items, null, null)
59 public ImageListViewItem(string[] items, ImageCache imageCache, string imageUrl)
62 this.imageCache = imageCache;
63 this.imageUrl = imageUrl;
66 var image = imageCache?.TryGetFromCache(imageUrl);
69 this.imageReference.Target = image;
72 protected ImageListViewItem(SerializationInfo info, StreamingContext context)
77 public Task GetImageAsync(bool force = false)
79 if (this.imageTask == null || this.imageTask.IsCompleted)
81 this.imageTask = this.GetImageAsyncInternal(force);
84 return this.imageTask;
87 private async Task GetImageAsyncInternal(bool force)
89 if (string.IsNullOrEmpty(this.imageUrl))
92 if (!force && this.imageReference.Target != null)
97 var image = await this.imageCache.DownloadImageAsync(this.imageUrl, force);
99 this.imageReference.Target = image;
101 if (this.ListView == null || !this.ListView.Created || this.ListView.IsDisposed)
104 if (this.Index < this.ListView.VirtualListSize)
106 this.ListView.RedrawItems(this.Index, this.Index, true);
108 this.ImageDownloaded?.Invoke(this, EventArgs.Empty);
111 catch (HttpRequestException) { }
112 catch (InvalidImageException) { }
113 catch (TaskCanceledException) { }
116 public MemoryImage Image
117 => (MemoryImage)this.imageReference.Target;
119 public Task RefreshImageAsync()
121 this.imageReference.Target = null;
122 return this.GetImageAsync(true);