OSDN Git Service

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