OSDN Git Service

OTBaseForm.InvokeAsyncメソッドをなるべく使用する
[opentween/open-tween.git] / OpenTween / ApiInfoDialog.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 spx (@5px)
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.Data;
26 using System.Drawing;
27 using System.Linq;
28 using System.Text;
29 using System.Threading.Tasks;
30 using System.Windows.Forms;
31 using OpenTween.Api;
32
33 namespace OpenTween
34 {
35     public partial class ApiInfoDialog : OTBaseForm
36     {
37         public ApiInfoDialog()
38         {
39             InitializeComponent();
40         }
41
42         private readonly List<string> _tlEndpoints = new List<string>
43         {
44             "/statuses/home_timeline",
45             "/statuses/mentions_timeline",
46             "/statuses/show/:id",
47             "/statuses/user_timeline",
48             "/favorites/list",
49             "/direct_messages",
50             "/direct_messages/sent",
51             "/lists/statuses",
52             "/search/tweets",
53         };
54
55         private void ApiInfoDialog_Shown(object sender, EventArgs e)
56         {
57             // TL更新用エンドポイントの追加
58             var group = this.ListViewApi.Groups[0];
59             foreach (var endpoint in _tlEndpoints)
60             {
61                 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
62                 AddListViewItem(endpoint, apiLimit, group);
63             }
64
65             // その他
66             group = this.ListViewApi.Groups[1];
67             var apiStatuses = MyCommon.TwitterApiInfo.AccessLimit.Where(x => !_tlEndpoints.Contains(x.Key)).OrderBy(x => x.Key);
68             foreach (var pair in apiStatuses)
69             {
70                 AddListViewItem(pair.Key, pair.Value, group);
71             }
72
73             MyCommon.TwitterApiInfo.AccessLimitUpdated += this.TwitterApiStatus_AccessLimitUpdated;
74         }
75
76         private void AddListViewItem(string endpoint, ApiLimit apiLimit, ListViewGroup group)
77         {
78             var item = new ListViewItem(
79                 new string[] {
80                     endpoint,
81                     apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount,
82                     apiLimit.AccessLimitResetDate.ToString()
83                 });
84             item.Group = group;
85             this.ListViewApi.Items.Add(item);
86         }
87
88         private void UpdateEndpointLimit(string endpoint)
89         {
90             var item = this.ListViewApi.Items.Cast<ListViewItem>().FirstOrDefault(x => x.SubItems[0].Text == endpoint);
91             if (item != null)
92             {
93                 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
94                 item.SubItems[1].Text = apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount;
95                 item.SubItems[2].Text = apiLimit.AccessLimitResetDate.ToString();
96             }
97         }
98
99         private async void TwitterApiStatus_AccessLimitUpdated(object sender, EventArgs e)
100         {
101             try
102             {
103                 if (this.InvokeRequired && !this.IsDisposed)
104                 {
105                     await this.InvokeAsync(() => this.TwitterApiStatus_AccessLimitUpdated(sender, e));
106                 }
107                 else
108                 {
109                     var endpoint = (e as TwitterApiStatus.AccessLimitUpdatedEventArgs).EndpointName;
110                     UpdateEndpointLimit(endpoint);
111                 }
112             }
113             catch (ObjectDisposedException)
114             {
115                 return;
116             }
117             catch (InvalidOperationException)
118             {
119                 return;
120             }
121         }
122
123         private void ApiInfoDialog_FormClosing(object sender, FormClosingEventArgs e)
124         {
125             MyCommon.TwitterApiInfo.AccessLimitUpdated -= this.TwitterApiStatus_AccessLimitUpdated;
126         }
127
128         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
129         {
130             base.ScaleControl(factor, specified);
131             ScaleChildControl(this.ListViewApi, factor);
132         }
133     }
134
135     // ちらつき軽減用
136     public class BufferedListView : ListView
137     {
138         public BufferedListView()
139         {
140             DoubleBuffered = true;
141         }
142     }
143 }