OSDN Git Service

自動プロパティを使用する (IDE0032)
[opentween/open-tween.git] / OpenTween / ListAvailable.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 // All rights reserved.
8 // 
9 // This file is part of OpenTween.
10 // 
11 // This program is free software; you can redistribute it and/or modify it
12 // under the terms of the GNU General Public License as published by the Free
13 // Software Foundation; either version 3 of the License, or (at your option)
14 // any later version.
15 // 
16 // This program is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 // for more details. 
20 // 
21 // You should have received a copy of the GNU General Public License along
22 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
23 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 // Boston, MA 02110-1301, USA.
25
26 using System;
27 using System.Collections.Generic;
28 using System.ComponentModel;
29 using System.Data;
30 using System.Drawing;
31 using System.Linq;
32 using System.Text;
33 using System.Threading.Tasks;
34 using System.Windows.Forms;
35 using OpenTween.Models;
36
37 namespace OpenTween
38 {
39     public partial class ListAvailable : OTBaseForm
40     {
41         public ListElement SelectedList { get; private set; }
42
43         public ListAvailable()
44         {
45             InitializeComponent();
46         }
47
48         private void OK_Button_Click(object sender, EventArgs e)
49         {
50             if (this.ListsList.SelectedIndex > -1) {
51                 this.SelectedList = (ListElement)this.ListsList.SelectedItem;
52                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
53                 this.Close();
54             }
55         }
56
57         private void Cancel_Button_Click(object sender, EventArgs e)
58         {
59             this.SelectedList = null;
60             this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
61             this.Close();
62         }
63
64         private async void ListAvailable_Shown(object sender, EventArgs e)
65         {
66             using (ControlTransaction.Disabled(this))
67             {
68                 try
69                 {
70                     var lists = (IReadOnlyList<ListElement>)TabInformations.GetInstance().SubscribableLists;
71                     if (lists.Count == 0)
72                         lists = await this.FetchListsAsync();
73
74                     this.UpdateListsListBox(lists);
75                 }
76                 catch (OperationCanceledException)
77                 {
78                     this.DialogResult = DialogResult.Cancel;
79                     return;
80                 }
81                 catch (WebApiException)
82                 {
83                     this.DialogResult = DialogResult.Abort;
84                     return;
85                 }
86             }
87         }
88
89         private void ListsList_SelectedIndexChanged(object sender, EventArgs e)
90         {
91             ListElement lst;
92             if (this.ListsList.SelectedIndex > -1)
93             {
94                 lst = (ListElement)this.ListsList.SelectedItem;
95             }
96             else
97             {
98                 lst = null;
99             }
100             if (lst == null)
101             {
102                 this.UsernameLabel.Text = "";
103                 this.NameLabel.Text = "";
104                 this.StatusLabel.Text = "";
105                 this.MemberCountLabel.Text = "0";
106                 this.SubscriberCountLabel.Text = "0";
107                 this.DescriptionText.Text = "";
108             }
109             else
110             {
111                 this.UsernameLabel.Text = lst.Username + " / " + lst.Nickname;
112                 this.NameLabel.Text = lst.Name;
113                 if (lst.IsPublic)
114                 {
115                     this.StatusLabel.Text = "Public";
116                 }
117                 else
118                 {
119                     this.StatusLabel.Text = "Private";
120                 }
121                 this.MemberCountLabel.Text = lst.MemberCount.ToString("#,##0");
122                 this.SubscriberCountLabel.Text = lst.SubscriberCount.ToString("#,##0");
123                 this.DescriptionText.Text = lst.Description;
124             }
125         }
126
127         private async void RefreshButton_Click(object sender, EventArgs e)
128         {
129             using (ControlTransaction.Disabled(this))
130             {
131                 try
132                 {
133                     var lists = await this.FetchListsAsync();
134                     this.UpdateListsListBox(lists);
135                 }
136                 catch (OperationCanceledException) { }
137                 catch (WebApiException ex)
138                 {
139                     MessageBox.Show("Failed to get lists. (" + ex.Message + ")");
140                 }
141             }
142         }
143
144         private async Task<IReadOnlyList<ListElement>> FetchListsAsync()
145         {
146             using (var dialog = new WaitingDialog("Getting Lists..."))
147             {
148                 var cancellationToken = dialog.EnableCancellation();
149
150                 var tw = ((TweenMain)this.Owner).TwitterInstance;
151                 var task = tw.GetListsApi();
152                 await dialog.WaitForAsync(this, task);
153
154                 cancellationToken.ThrowIfCancellationRequested();
155             }
156
157             return TabInformations.GetInstance().SubscribableLists;
158         }
159
160         private void UpdateListsListBox(IEnumerable<ListElement> lists)
161         {
162             using (ControlTransaction.Update(this.ListsList))
163             {
164                 this.ListsList.Items.Clear();
165                 this.ListsList.Items.AddRange(lists.ToArray());
166                 if (this.ListsList.Items.Count > 0)
167                 {
168                     this.ListsList.SelectedIndex = 0;
169                 }
170                 else
171                 {
172                     this.UsernameLabel.Text = "";
173                     this.NameLabel.Text = "";
174                     this.StatusLabel.Text = "";
175                     this.MemberCountLabel.Text = "0";
176                     this.SubscriberCountLabel.Text = "0";
177                     this.DescriptionText.Text = "";
178                 }
179             }
180         }
181     }
182 }