OSDN Git Service

ヘルプページ・Google検索・Wikipedia等をHTTPSに対応したURLに変更
[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             => this.InitializeComponent();
39
40         private readonly List<string> _tlEndpoints = new List<string>
41         {
42             "/statuses/home_timeline",
43             "/statuses/mentions_timeline",
44             "/statuses/show/:id",
45             "/statuses/user_timeline",
46             "/favorites/list",
47             "/direct_messages/events/list",
48             "/direct_messages",
49             "/direct_messages/sent",
50             "/lists/statuses",
51             "/search/tweets",
52         };
53
54         private void ApiInfoDialog_Shown(object sender, EventArgs e)
55         {
56             // TL更新用エンドポイントの追加
57             var group = this.ListViewApi.Groups[0];
58             foreach (var endpoint in _tlEndpoints)
59             {
60                 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
61                 AddListViewItem(endpoint, apiLimit, group);
62             }
63
64             // その他
65             group = this.ListViewApi.Groups[1];
66             var apiStatuses = MyCommon.TwitterApiInfo.AccessLimit.Where(x => !_tlEndpoints.Contains(x.Key)).OrderBy(x => x.Key);
67             foreach (var (endpoint, apiLimit) in apiStatuses)
68             {
69                 AddListViewItem(endpoint, apiLimit, group);
70             }
71
72             MyCommon.TwitterApiInfo.AccessLimitUpdated += this.TwitterApiStatus_AccessLimitUpdated;
73         }
74
75         private void AddListViewItem(string endpoint, ApiLimit apiLimit, ListViewGroup group)
76         {
77             var subitems = new[]
78             {
79                 endpoint,
80                 apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount,
81                 apiLimit.AccessLimitResetDate.ToLocalTimeString(),
82             };
83             var item = new ListViewItem(subitems)
84             {
85                 Group = group,
86             };
87
88             this.ListViewApi.Items.Add(item);
89         }
90
91         private void UpdateEndpointLimit(string endpoint)
92         {
93             var item = this.ListViewApi.Items.Cast<ListViewItem>().FirstOrDefault(x => x.SubItems[0].Text == endpoint);
94             if (item != null)
95             {
96                 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
97                 item.SubItems[1].Text = apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount;
98                 item.SubItems[2].Text = apiLimit.AccessLimitResetDate.ToLocalTimeString();
99             }
100         }
101
102         private async void TwitterApiStatus_AccessLimitUpdated(object sender, EventArgs e)
103         {
104             try
105             {
106                 if (this.InvokeRequired && !this.IsDisposed)
107                 {
108                     await this.InvokeAsync(() => this.TwitterApiStatus_AccessLimitUpdated(sender, e));
109                 }
110                 else
111                 {
112                     var endpoint = (e as TwitterApiStatus.AccessLimitUpdatedEventArgs).EndpointName;
113                     UpdateEndpointLimit(endpoint);
114                 }
115             }
116             catch (ObjectDisposedException)
117             {
118                 return;
119             }
120             catch (InvalidOperationException)
121             {
122                 return;
123             }
124         }
125
126         private void ApiInfoDialog_FormClosing(object sender, FormClosingEventArgs e)
127             => MyCommon.TwitterApiInfo.AccessLimitUpdated -= this.TwitterApiStatus_AccessLimitUpdated;
128
129         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
130         {
131             base.ScaleControl(factor, specified);
132             ScaleChildControl(this.ListViewApi, factor);
133         }
134     }
135
136     // ちらつき軽減用
137     public class BufferedListView : ListView
138     {
139         public BufferedListView()
140             => this.DoubleBuffered = true;
141     }
142 }