OSDN Git Service

48b27153b0f0428e0d6d34f8e8a9b5eff3dd9c62
[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
36 namespace OpenTween
37 {
38     public partial class ListAvailable : OTBaseForm
39     {
40         private ListElement _selectedList = null;
41         public ListElement SelectedList
42         {
43             get
44             {
45                 return _selectedList;
46             }
47         }
48
49         public ListAvailable()
50         {
51             InitializeComponent();
52         }
53
54         private void OK_Button_Click(object sender, EventArgs e)
55         {
56             if (this.ListsList.SelectedIndex > -1) {
57                 _selectedList = (ListElement)this.ListsList.SelectedItem;
58                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
59                 this.Close();
60             }
61         }
62
63         private void Cancel_Button_Click(object sender, EventArgs e)
64         {
65             _selectedList = null;
66             this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
67             this.Close();
68         }
69
70         private async void ListAvailable_Shown(object sender, EventArgs e)
71         {
72             using (ControlTransaction.Disabled(this))
73             {
74                 if (TabInformations.GetInstance().SubscribableLists.Count == 0)
75                     await this.RefreshLists();
76
77                 this.ListsList.Items.AddRange(TabInformations.GetInstance().SubscribableLists.ToArray());
78                 if (this.ListsList.Items.Count > 0)
79                 {
80                     this.ListsList.SelectedIndex = 0;
81                 }
82                 else
83                 {
84                     this.UsernameLabel.Text = "";
85                     this.NameLabel.Text = "";
86                     this.StatusLabel.Text = "";
87                     this.MemberCountLabel.Text = "0";
88                     this.SubscriberCountLabel.Text = "0";
89                     this.DescriptionText.Text = "";
90                 }
91             }
92         }
93
94         private void ListsList_SelectedIndexChanged(object sender, EventArgs e)
95         {
96             ListElement lst;
97             if (this.ListsList.SelectedIndex > -1)
98             {
99                 lst = (ListElement)this.ListsList.SelectedItem;
100             }
101             else
102             {
103                 lst = null;
104             }
105             if (lst == null)
106             {
107                 this.UsernameLabel.Text = "";
108                 this.NameLabel.Text = "";
109                 this.StatusLabel.Text = "";
110                 this.MemberCountLabel.Text = "0";
111                 this.SubscriberCountLabel.Text = "0";
112                 this.DescriptionText.Text = "";
113             }
114             else
115             {
116                 this.UsernameLabel.Text = lst.Username + " / " + lst.Nickname;
117                 this.NameLabel.Text = lst.Name;
118                 if (lst.IsPublic)
119                 {
120                     this.StatusLabel.Text = "Public";
121                 }
122                 else
123                 {
124                     this.StatusLabel.Text = "Private";
125                 }
126                 this.MemberCountLabel.Text = lst.MemberCount.ToString("#,##0");
127                 this.SubscriberCountLabel.Text = lst.SubscriberCount.ToString("#,##0");
128                 this.DescriptionText.Text = lst.Description;
129             }
130         }
131
132         private async void RefreshButton_Click(object sender, EventArgs e)
133         {
134             using (ControlTransaction.Disabled(this))
135             {
136                 await this.RefreshLists();
137
138                 this.ListsList.Items.Clear();
139                 this.ListsList.Items.AddRange(TabInformations.GetInstance().SubscribableLists.ToArray());
140                 if (this.ListsList.Items.Count > 0)
141                 {
142                     this.ListsList.SelectedIndex = 0;
143                 }
144             }
145         }
146
147         private async Task RefreshLists()
148         {
149             using (var dialog = new WaitingDialog("Getting Lists..."))
150             {
151                 var tw = ((TweenMain)this.Owner).TwitterInstance;
152                 var task = Task.Run(() => tw.GetListsApi());
153
154                 var err = await dialog.WaitForAsync(this, task);
155                 if (!string.IsNullOrEmpty(err))
156                 {
157                     MessageBox.Show("Failed to get lists. (" + err + ")");
158                     return;
159                 }
160             }
161         }
162     }
163 }