OSDN Git Service

WebApiExceptionをシリアライズに対応できるように修正 (CA2240)
[opentween/open-tween.git] / OpenTween / ListManage.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) 2011      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 using System;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Data;
31 using System.Drawing;
32 using System.Linq;
33 using System.Text;
34 using System.Windows.Forms;
35
36 namespace OpenTween
37 {
38     public partial class ListManage : OTBaseForm
39     {
40         private Twitter tw;
41
42         public ListManage(Twitter tw)
43         {
44             InitializeComponent();
45
46             this.tw = tw;
47         }
48
49         private void ListManage_KeyDown(object sender, KeyEventArgs e)
50         {
51             if (e.KeyCode == Keys.Enter && this.EditCheckBox.Checked)
52                 this.OKEditButton.PerformClick();
53         }
54
55         private void ListManage_Load(object sender, EventArgs e)
56         {
57             this.UserList_SelectedIndexChanged(null, EventArgs.Empty);
58             if (TabInformations.GetInstance().SubscribableLists.Count == 0) this.RefreshLists();
59             foreach (ListElement listItem in TabInformations.GetInstance().SubscribableLists.FindAll((i) => i.Username == this.tw.Username))
60             {
61                 this.ListsList.Items.Add(listItem);
62             }
63             if (this.ListsList.Items.Count > 0)
64                 this.ListsList.SelectedIndex = 0;
65             this.ListsList.Focus();
66         }
67
68         private void ListsList_SelectedIndexChanged(object sender, EventArgs e)
69         {
70             if (this.ListsList.SelectedItem == null) return;
71
72             ListElement list = (ListElement) this.ListsList.SelectedItem;
73             this.UsernameTextBox.Text = list.Username;
74             this.NameTextBox.Text = list.Name;
75             this.PublicRadioButton.Checked = list.IsPublic;
76             this.PrivateRadioButton.Checked = !list.IsPublic;
77             this.MemberCountTextBox.Text = list.MemberCount.ToString();
78             this.SubscriberCountTextBox.Text = list.SubscriberCount.ToString();
79             this.DescriptionText.Text = list.Description;
80
81             this.UserList.Items.Clear();
82             foreach (UserInfo user in list.Members)
83                 this.UserList.Items.Add(user);
84
85             this.GetMoreUsersButton.Text = (this.UserList.Items.Count > 0 ? Properties.Resources.ListManageGetMoreUsers2 : Properties.Resources.ListManageGetMoreUsers1).ToString();
86         }
87
88         private void EditCheckBox_CheckedChanged(object sender, EventArgs e)
89         {
90             this.AddListButton.Enabled = !this.EditCheckBox.Checked;
91             this.EditCheckBox.Enabled = !this.EditCheckBox.Checked;
92             this.DeleteListButton.Enabled = !this.EditCheckBox.Checked;
93
94             this.NameTextBox.ReadOnly = !this.EditCheckBox.Checked;
95             this.PublicRadioButton.Enabled = this.EditCheckBox.Checked;
96             this.PrivateRadioButton.Enabled = this.EditCheckBox.Checked;
97             this.DescriptionText.ReadOnly = !this.EditCheckBox.Checked;
98             this.ListsList.Enabled = !this.EditCheckBox.Checked;
99
100             this.OKEditButton.Enabled = this.EditCheckBox.Checked;
101             this.CancelEditButton.Enabled = this.EditCheckBox.Checked;
102             this.EditCheckBox.AutoCheck = !this.EditCheckBox.Checked;
103
104             this.MemberGroup.Enabled = !this.EditCheckBox.Checked;
105             this.UserGroup.Enabled = !this.EditCheckBox.Checked;
106             this.CloseButton.Enabled = !this.EditCheckBox.Checked;
107
108             this.UsernameTextBox.TabStop = !this.EditCheckBox.Checked;
109             this.MemberCountTextBox.TabStop = !this.EditCheckBox.Checked;
110             this.SubscriberCountTextBox.TabStop = !this.EditCheckBox.Checked;
111             if (this.EditCheckBox.Checked == true) this.NameTextBox.Focus();
112         }
113
114         private void OKEditButton_Click(object sender, EventArgs e)
115         {
116             if (this.ListsList.SelectedItem == null) return;
117             ListElement listItem = (ListElement) this.ListsList.SelectedItem;
118
119             if (string.IsNullOrEmpty(this.NameTextBox.Text))
120             {
121                 MessageBox.Show(Properties.Resources.ListManageOKButton1);
122                 return;
123             }
124
125             listItem.Name = this.NameTextBox.Text;
126             listItem.IsPublic = this.PublicRadioButton.Checked;
127             listItem.Description = this.DescriptionText.Text;
128
129             string rslt = listItem.Refresh();
130
131             if (!string.IsNullOrEmpty(rslt))
132             {
133                 MessageBox.Show(String.Format(Properties.Resources.ListManageOKButton2, rslt));
134                 return;
135             }
136
137             this.ListsList.Items.Clear();
138             this.ListManage_Load(null, EventArgs.Empty);
139
140             this.EditCheckBox.AutoCheck = true;
141             this.EditCheckBox.Checked = false;
142         }
143
144         private void CancelEditButton_Click(object sender, EventArgs e)
145         {
146             this.EditCheckBox.AutoCheck = true;
147             this.EditCheckBox.Checked = false;
148
149             for (int i = this.ListsList.Items.Count - 1; i >= 0; i--)
150                 if (this.ListsList.Items[i] is NewListElement)
151                     this.ListsList.Items.RemoveAt(i);
152
153             this.ListsList_SelectedIndexChanged(this.ListsList, EventArgs.Empty);
154         }
155
156         private void RefreshUsersButton_Click(object sender, EventArgs e)
157         {
158             if (this.ListsList.SelectedItem == null) return;
159             this.UserList.Items.Clear();
160             Action<ListElement> dlgt = new Action<ListElement>((lElement) =>
161                                                    this.Invoke(new Action<string>(GetListMembersCallback), lElement.RefreshMembers()));
162             dlgt.BeginInvoke((ListElement) this.ListsList.SelectedItem, null, null);
163         }
164
165         private void GetMoreUsersButton_Click(object sender, EventArgs e)
166         {
167             if (this.ListsList.SelectedItem == null) return;
168             this.UserList.Items.Clear();
169             Action<ListElement> dlgt = new Action<ListElement>((lElement) =>
170                                                    this.Invoke(new Action<string>(GetListMembersCallback), lElement.GetMoreMembers()));
171             dlgt.BeginInvoke((ListElement)this.ListsList.SelectedItem, null, null);
172         }
173
174         private void GetListMembersCallback(string result)
175         {
176             if (result == this.ListsList.SelectedItem.ToString())
177             {
178                 this.ListsList_SelectedIndexChanged(this.ListsList, EventArgs.Empty);
179                 this.GetMoreUsersButton.Text = Properties.Resources.ListManageGetMoreUsers1;
180             }
181             else
182             {
183                 MessageBox.Show(String.Format(Properties.Resources.ListManageGetListMembersCallback1, result));
184             }
185         }
186
187         private void DeleteUserButton_Click(object sender, EventArgs e)
188         {
189             if (this.ListsList.SelectedItem == null || this.UserList.SelectedItem == null)
190                 return;
191
192             ListElement list = (ListElement) this.ListsList.SelectedItem;
193             UserInfo user = (UserInfo) this.UserList.SelectedItem;
194             if (MessageBox.Show(Properties.Resources.ListManageDeleteUser1, Application.ProductName, MessageBoxButtons.OKCancel) == DialogResult.OK)
195             {
196                 string rslt = this.tw.RemoveUserToList(list.Id.ToString(), user.Id.ToString());
197
198                 if (!string.IsNullOrEmpty(rslt))
199                 {
200                     MessageBox.Show(String.Format(Properties.Resources.ListManageDeleteUser2, rslt));
201                     return;
202                 }
203                 int idx = ListsList.SelectedIndex;
204                 list.Members.Remove(user);
205                 this.ListsList_SelectedIndexChanged(this.ListsList, EventArgs.Empty);
206                 if (idx < ListsList.Items.Count) ListsList.SelectedIndex = idx;
207             }
208         }
209
210         private void DeleteListButton_Click(object sender, EventArgs e)
211         {
212             if (this.ListsList.SelectedItem == null) return;
213             ListElement list = (ListElement) this.ListsList.SelectedItem;
214
215             if (MessageBox.Show(Properties.Resources.ListManageDeleteLists1, Application.ProductName, MessageBoxButtons.OKCancel) == DialogResult.OK)
216             {
217                 string rslt = "";
218
219                 rslt = this.tw.DeleteList(list.Id.ToString());
220
221                 if (!string.IsNullOrEmpty(rslt))
222                 {
223                     MessageBox.Show(Properties.Resources.ListManageOKButton2, rslt);
224                     return;
225                 }
226
227                 rslt = this.tw.GetListsApi();
228
229                 if (!string.IsNullOrEmpty(rslt))
230                 {
231                     MessageBox.Show(Properties.Resources.ListsDeleteFailed, rslt);
232                     return;
233                 }
234
235                 this.ListsList.Items.Clear();
236                 this.ListManage_Load(this, EventArgs.Empty);
237             }
238         }
239
240         private void AddListButton_Click(object sender, EventArgs e)
241         {
242             NewListElement newList = new NewListElement(this.tw);
243             this.ListsList.Items.Add(newList);
244             this.ListsList.SelectedItem = newList;
245             this.EditCheckBox.Checked = true;
246             this.EditCheckBox_CheckedChanged(this.EditCheckBox, EventArgs.Empty);
247         }
248
249         private void UserList_SelectedIndexChanged(object sender, EventArgs e)
250         {
251             if (UserList.SelectedItem == null)
252             {
253                 if (this.UserIcon.Image != null)
254                 {
255                     this.UserIcon.Image.Dispose();
256                     this.UserIcon.Image = null;
257                 }
258                 this.UserLocation.Text = "";
259                 this.UserWeb.Text = "";
260                 this.UserFollowNum.Text = "0";
261                 this.UserFollowerNum.Text = "0";
262                 this.UserPostsNum.Text = "0";
263                 this.UserProfile.Text = "";
264                 this.UserTweetDateTime.Text = "";
265                 this.UserTweet.Text = "";
266                 this.DeleteUserButton.Enabled = false;
267             }
268             else
269             {
270                 UserInfo user = (UserInfo)this.UserList.SelectedItem;
271                 this.UserLocation.Text = user.Location;
272                 this.UserWeb.Text = user.Url;
273                 this.UserFollowNum.Text = user.FriendsCount.ToString("#,###,##0");
274                 this.UserFollowerNum.Text = user.FollowersCount.ToString("#,###,##0");
275                 this.UserPostsNum.Text = user.StatusesCount.ToString("#,###,##0");
276                 this.UserProfile.Text = user.Description;
277                 if (!String.IsNullOrEmpty(user.RecentPost))
278                 {
279                     this.UserTweetDateTime.Text = user.PostCreatedAt.ToString("yy/MM/dd HH:mm");
280                     this.UserTweet.Text = user.RecentPost;
281                 }
282                 else
283                 {
284                     this.UserTweetDateTime.Text = "";
285                     this.UserTweet.Text = "";
286                 }
287                 this.DeleteUserButton.Enabled = true;
288
289                 Action<Uri> a = new Action<Uri>((url) =>
290                                             this.Invoke(new Action<Image>(DisplayIcon), (new HttpVarious()).GetImage(url)));
291                 a.BeginInvoke(user.ImageUrl, null, null);
292             }
293         }
294
295         private void DisplayIcon(Image img)
296         {
297             if (img == null || this.UserList.SelectedItem == null) return;
298             if (((UserInfo)this.UserList.SelectedItem).ImageUrl.ToString() == (string)img.Tag)
299                 this.UserIcon.Image = img;
300         }
301
302         private void RefreshListsButton_Click(object sender, EventArgs e)
303         {
304             this.RefreshLists();
305             this.ListsList.Items.Clear();
306             this.ListManage_Load(null, EventArgs.Empty);
307         }
308
309         private void RefreshLists()
310         {
311             using (FormInfo dlg = new FormInfo(this, Properties.Resources.ListsGetting, RefreshLists_Dowork))
312             {
313                 dlg.ShowDialog();
314                 if (!String.IsNullOrEmpty((string)dlg.Result))
315                 {
316                     MessageBox.Show(String.Format(Properties.Resources.ListsDeleteFailed, (string)dlg.Result));
317                     return;
318                 }
319             }
320         }
321
322         private void RefreshLists_Dowork(object sender, DoWorkEventArgs e)
323         {
324             e.Result = tw.GetListsApi();
325         }
326
327         private void UserWeb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
328         {
329             if (this.Owner != null)
330                 ((TweenMain)this.Owner).OpenUriAsync(UserWeb.Text);
331         }
332
333         private class NewListElement : ListElement
334         {
335             private bool _isCreated = false;
336
337             public NewListElement(Twitter tw)
338             {
339                 this._tw = tw;
340             }
341
342             public override string Refresh()
343             {
344                 if (this.IsCreated)
345                 {
346                     return base.Refresh();
347                 }
348                 else
349                 {
350                     string rslt = this._tw.CreateListApi(this.Name, !this.IsPublic, this.Description);
351                     this._isCreated = string.IsNullOrEmpty(rslt);
352                     return rslt;
353                 }
354             }
355
356             public bool IsCreated
357             {
358                 get { return this._isCreated; }
359             }
360
361             public override string ToString()
362             {
363                 if (IsCreated)
364                     return base.ToString();
365                 else
366                     return "NewList";
367             }
368         }
369
370         private void ListManage_Validating(object sender, CancelEventArgs e)
371         {
372             if (this.EditCheckBox.Checked)
373             {
374                 e.Cancel = true;
375                 this.CancelButton.PerformClick();
376             }
377         }
378     }
379 }