OSDN Git Service

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