OSDN Git Service

自動プロパティを使用する (IDE0032)
[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 (endpoint, apiLimit) in apiStatuses)
69             {
70                 AddListViewItem(endpoint, apiLimit, 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 subitems = new[]
79             {
80                 endpoint,
81                 apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount,
82                 apiLimit.AccessLimitResetDate.ToLocalTimeString(),
83             };
84             var item = new ListViewItem(subitems)
85             {
86                 Group = group,
87             };
88
89             this.ListViewApi.Items.Add(item);
90         }
91
92         private void UpdateEndpointLimit(string endpoint)
93         {
94             var item = this.ListViewApi.Items.Cast<ListViewItem>().FirstOrDefault(x => x.SubItems[0].Text == endpoint);
95             if (item != null)
96             {
97                 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
98                 item.SubItems[1].Text = apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount;
99                 item.SubItems[2].Text = apiLimit.AccessLimitResetDate.ToLocalTimeString();
100             }
101         }
102
103         private async void TwitterApiStatus_AccessLimitUpdated(object sender, EventArgs e)
104         {
105             try
106             {
107                 if (this.InvokeRequired && !this.IsDisposed)
108                 {
109                     await this.InvokeAsync(() => this.TwitterApiStatus_AccessLimitUpdated(sender, e));
110                 }
111                 else
112                 {
113                     var endpoint = (e as TwitterApiStatus.AccessLimitUpdatedEventArgs).EndpointName;
114                     UpdateEndpointLimit(endpoint);
115                 }
116             }
117             catch (ObjectDisposedException)
118             {
119                 return;
120             }
121             catch (InvalidOperationException)
122             {
123                 return;
124             }
125         }
126
127         private void ApiInfoDialog_FormClosing(object sender, FormClosingEventArgs e)
128         {
129             MyCommon.TwitterApiInfo.AccessLimitUpdated -= this.TwitterApiStatus_AccessLimitUpdated;
130         }
131
132         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
133         {
134             base.ScaleControl(factor, specified);
135             ScaleChildControl(this.ListViewApi, factor);
136         }
137     }
138
139     // ちらつき軽減用
140     public class BufferedListView : ListView
141     {
142         public BufferedListView()
143         {
144             DoubleBuffered = true;
145         }
146     }
147 }