OSDN Git Service

プロフィール画面へのD&Dによるアイコン変更時に確認ダイアログを表示するように変更
[opentween/open-tween.git] / OpenTween / ImageListViewItem.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      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
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.Net.Http;
29 using System.Threading.Tasks;
30 using System.Windows.Forms;
31
32 namespace OpenTween
33 {
34     public class ImageListViewItem : ListViewItem
35     {
36         protected readonly ImageCache imageCache;
37         protected readonly string imageUrl;
38
39         /// <summary>
40         /// 状態表示に使用するアイコンのインデックスを取得・設定する。
41         /// </summary>
42         /// <remarks>
43         /// StateImageIndex は不必要な処理が挟まるため、使用しないようにする。
44         /// </remarks>
45         public int StateIndex { get; set; }
46
47         private WeakReference imageReference = new WeakReference(null);
48         private Task imageTask = null;
49
50         public event EventHandler ImageDownloaded;
51
52         public ImageListViewItem(string[] items)
53             : this(items, null, null)
54         {
55         }
56
57         public ImageListViewItem(string[] items, ImageCache imageCache, string imageUrl)
58             : base(items)
59         {
60             this.imageCache = imageCache;
61             this.imageUrl = imageUrl;
62             this.StateIndex = -1;
63
64             if (imageCache != null)
65             {
66                 var image = imageCache.TryGetFromCache(imageUrl);
67
68                 if (image != null)
69                     this.imageReference.Target = image;
70             }
71         }
72
73         public Task GetImageAsync(bool force = false)
74         {
75             if (this.imageTask == null || this.imageTask.IsCompleted)
76             {
77                 this.imageTask = this.GetImageAsyncInternal(force);
78             }
79
80             return this.imageTask;
81         }
82
83         private async Task GetImageAsyncInternal(bool force)
84         {
85             if (string.IsNullOrEmpty(this.imageUrl))
86                 return;
87
88             if (!force && this.imageReference.Target != null)
89                 return;
90
91             try
92             {
93                 var image = await this.imageCache.DownloadImageAsync(this.imageUrl, force);
94
95                 this.imageReference.Target = image;
96
97                 if (this.ListView == null || !this.ListView.Created || this.ListView.IsDisposed)
98                     return;
99
100                 if (this.Index < this.ListView.VirtualListSize)
101                 {
102                     this.ListView.RedrawItems(this.Index, this.Index, true);
103
104                     if (this.ImageDownloaded != null)
105                         this.ImageDownloaded(this, EventArgs.Empty);
106                 }
107             }
108             catch (HttpRequestException) { }
109             catch (InvalidImageException) { }
110             catch (TaskCanceledException) { }
111         }
112
113         public MemoryImage Image
114         {
115             get
116             {
117                 return (MemoryImage)this.imageReference.Target;
118             }
119         }
120
121         public Task RefreshImageAsync()
122         {
123             this.imageReference.Target = null;
124             return this.GetImageAsync(true);
125         }
126     }
127 }