OSDN Git Service

usingの順序を揃える (SA1208, SA1210)
[opentween/open-tween.git] / OpenTween / Setting / Panel / ShortUrlPanel.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) 2014      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
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.Collections.Generic;
31 using System.ComponentModel;
32 using System.Data;
33 using System.Drawing;
34 using System.Linq;
35 using System.Text;
36 using System.Threading.Tasks;
37 using System.Windows.Forms;
38 using OpenTween.Api;
39
40 namespace OpenTween.Setting.Panel
41 {
42     public partial class ShortUrlPanel : SettingPanelBase
43     {
44         public ShortUrlPanel()
45             => this.InitializeComponent();
46
47         public void LoadConfig(SettingCommon settingCommon)
48         {
49             this.CheckTinyURL.Checked = settingCommon.TinyUrlResolve;
50
51             // 使われていない設定項目 (Tween v1.0.5.0)
52             this.CheckAutoConvertUrl.Checked = false;
53             this.ShortenTcoCheck.Enabled = this.CheckAutoConvertUrl.Checked;
54
55             this.ComboBoxAutoShortUrlFirst.SelectedIndex = (int)settingCommon.AutoShortUrlFirst;
56             this.TextBitlyAccessToken.Text = settingCommon.BitlyAccessToken;
57             this.TextBitlyAccessToken.Modified = false;
58         }
59
60         public void SaveConfig(SettingCommon settingCommon)
61         {
62             settingCommon.TinyUrlResolve = this.CheckTinyURL.Checked;
63             settingCommon.UrlConvertAuto = this.CheckAutoConvertUrl.Checked;
64             settingCommon.AutoShortUrlFirst = (MyCommon.UrlConverter)this.ComboBoxAutoShortUrlFirst.SelectedIndex;
65             settingCommon.BitlyAccessToken = this.TextBitlyAccessToken.Text;
66         }
67
68         private void ComboBoxAutoShortUrlFirst_SelectedIndexChanged(object sender, EventArgs e)
69         {
70             if (this.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Bitly ||
71                this.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Jmp)
72             {
73                 this.Label77.Enabled = true;
74                 this.TextBitlyAccessToken.Enabled = true;
75                 this.ButtonBitlyAuthorize.Enabled = true;
76             }
77             else
78             {
79                 this.Label77.Enabled = false;
80                 this.TextBitlyAccessToken.Enabled = false;
81                 this.ButtonBitlyAuthorize.Enabled = false;
82             }
83         }
84
85         private void CheckAutoConvertUrl_CheckedChanged(object sender, EventArgs e)
86             => this.ShortenTcoCheck.Enabled = this.CheckAutoConvertUrl.Checked;
87
88         private void ButtonBitlyAuthorize_Click(object sender, EventArgs e)
89         {
90             using var dialog = new LoginDialog();
91
92             const string DialogText = "Bitly Login";
93             dialog.Text = DialogText;
94
95             string? accessToken = null;
96             dialog.LoginCallback = async () =>
97             {
98                 try
99                 {
100                     var bitly = new BitlyApi();
101                     accessToken = await bitly.GetAccessTokenAsync(dialog.LoginName, dialog.Password);
102                     return true;
103                 }
104                 catch (WebApiException ex)
105                 {
106                     var text = string.Format(Properties.Resources.BitlyAuthorize_ErrorText, ex.Message);
107                     MessageBox.Show(dialog, text, DialogText, MessageBoxButtons.OK, MessageBoxIcon.Error);
108                     return false;
109                 }
110             };
111
112             if (dialog.ShowDialog(this.ParentForm) == DialogResult.OK)
113             {
114                 this.TextBitlyAccessToken.Text = accessToken;
115             }
116         }
117     }
118 }