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.
10 // This file is part of OpenTween.
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)
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
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.
28 using System.Collections.Generic;
29 using System.ComponentModel;
34 using System.Windows.Forms;
38 public partial class ListManage : OTBaseForm
42 public ListManage(Twitter tw)
44 InitializeComponent();
49 private void ListManage_KeyDown(object sender, KeyEventArgs e)
51 if (e.KeyCode == Keys.Enter && this.EditCheckBox.Checked)
52 this.OKEditButton.PerformClick();
55 private void ListManage_Load(object sender, EventArgs e)
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))
61 this.ListsList.Items.Add(listItem);
63 if (this.ListsList.Items.Count > 0)
64 this.ListsList.SelectedIndex = 0;
65 this.ListsList.Focus();
68 private void ListsList_SelectedIndexChanged(object sender, EventArgs e)
70 if (this.ListsList.SelectedItem == null) return;
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;
81 this.UserList.Items.Clear();
82 foreach (UserInfo user in list.Members)
83 this.UserList.Items.Add(user);
85 this.GetMoreUsersButton.Text = (this.UserList.Items.Count > 0 ? Properties.Resources.ListManageGetMoreUsers2 : Properties.Resources.ListManageGetMoreUsers1).ToString();
88 private void EditCheckBox_CheckedChanged(object sender, EventArgs e)
90 this.AddListButton.Enabled = !this.EditCheckBox.Checked;
91 this.EditCheckBox.Enabled = !this.EditCheckBox.Checked;
92 this.DeleteListButton.Enabled = !this.EditCheckBox.Checked;
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;
100 this.OKEditButton.Enabled = this.EditCheckBox.Checked;
101 this.CancelEditButton.Enabled = this.EditCheckBox.Checked;
102 this.EditCheckBox.AutoCheck = !this.EditCheckBox.Checked;
104 this.MemberGroup.Enabled = !this.EditCheckBox.Checked;
105 this.UserGroup.Enabled = !this.EditCheckBox.Checked;
106 this.CloseButton.Enabled = !this.EditCheckBox.Checked;
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();
114 private void OKEditButton_Click(object sender, EventArgs e)
116 if (this.ListsList.SelectedItem == null) return;
117 ListElement listItem = (ListElement) this.ListsList.SelectedItem;
119 if (string.IsNullOrEmpty(this.NameTextBox.Text))
121 MessageBox.Show(Properties.Resources.ListManageOKButton1);
125 listItem.Name = this.NameTextBox.Text;
126 listItem.IsPublic = this.PublicRadioButton.Checked;
127 listItem.Description = this.DescriptionText.Text;
129 string rslt = listItem.Refresh();
131 if (!string.IsNullOrEmpty(rslt))
133 MessageBox.Show(String.Format(Properties.Resources.ListManageOKButton2, rslt));
137 this.ListsList.Items.Clear();
138 this.ListManage_Load(null, EventArgs.Empty);
140 this.EditCheckBox.AutoCheck = true;
141 this.EditCheckBox.Checked = false;
144 private void CancelEditButton_Click(object sender, EventArgs e)
146 this.EditCheckBox.AutoCheck = true;
147 this.EditCheckBox.Checked = false;
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);
153 this.ListsList_SelectedIndexChanged(this.ListsList, EventArgs.Empty);
156 private void RefreshUsersButton_Click(object sender, EventArgs e)
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);
165 private void GetMoreUsersButton_Click(object sender, EventArgs e)
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);
174 private void GetListMembersCallback(string result)
176 if (result == this.ListsList.SelectedItem.ToString())
178 this.ListsList_SelectedIndexChanged(this.ListsList, EventArgs.Empty);
179 this.GetMoreUsersButton.Text = Properties.Resources.ListManageGetMoreUsers1;
183 MessageBox.Show(String.Format(Properties.Resources.ListManageGetListMembersCallback1, result));
187 private void DeleteUserButton_Click(object sender, EventArgs e)
189 if (this.ListsList.SelectedItem == null || this.UserList.SelectedItem == null)
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)
196 string rslt = this.tw.RemoveUserToList(list.Id.ToString(), user.Id.ToString());
198 if (!string.IsNullOrEmpty(rslt))
200 MessageBox.Show(String.Format(Properties.Resources.ListManageDeleteUser2, rslt));
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;
210 private void DeleteListButton_Click(object sender, EventArgs e)
212 if (this.ListsList.SelectedItem == null) return;
213 ListElement list = (ListElement) this.ListsList.SelectedItem;
215 if (MessageBox.Show(Properties.Resources.ListManageDeleteLists1, Application.ProductName, MessageBoxButtons.OKCancel) == DialogResult.OK)
219 rslt = this.tw.DeleteList(list.Id.ToString());
221 if (!string.IsNullOrEmpty(rslt))
223 MessageBox.Show(Properties.Resources.ListManageOKButton2, rslt);
227 rslt = this.tw.GetListsApi();
229 if (!string.IsNullOrEmpty(rslt))
231 MessageBox.Show(Properties.Resources.ListsDeleteFailed, rslt);
235 this.ListsList.Items.Clear();
236 this.ListManage_Load(this, EventArgs.Empty);
240 private void AddListButton_Click(object sender, EventArgs e)
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);
249 private void UserList_SelectedIndexChanged(object sender, EventArgs e)
251 if (UserList.SelectedItem == null)
253 if (this.UserIcon.Image != null)
255 this.UserIcon.Image.Dispose();
256 this.UserIcon.Image = null;
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;
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))
279 this.UserTweetDateTime.Text = user.PostCreatedAt.ToString("yy/MM/dd HH:mm");
280 this.UserTweet.Text = user.RecentPost;
284 this.UserTweetDateTime.Text = "";
285 this.UserTweet.Text = "";
287 this.DeleteUserButton.Enabled = true;
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);
295 private void DisplayIcon(Image img)
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;
302 private void RefreshListsButton_Click(object sender, EventArgs e)
305 this.ListsList.Items.Clear();
306 this.ListManage_Load(null, EventArgs.Empty);
309 private void RefreshLists()
311 using (FormInfo dlg = new FormInfo(this, Properties.Resources.ListsGetting, RefreshLists_Dowork))
314 if (!String.IsNullOrEmpty((string)dlg.Result))
316 MessageBox.Show(String.Format(Properties.Resources.ListsDeleteFailed, (string)dlg.Result));
322 private void RefreshLists_Dowork(object sender, DoWorkEventArgs e)
324 e.Result = tw.GetListsApi();
327 private void UserWeb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
329 if (this.Owner != null)
330 ((TweenMain)this.Owner).OpenUriAsync(UserWeb.Text);
333 private class NewListElement : ListElement
335 private bool _isCreated = false;
337 public NewListElement(Twitter tw)
342 public override string Refresh()
346 return base.Refresh();
350 string rslt = this._tw.CreateListApi(this.Name, !this.IsPublic, this.Description);
351 this._isCreated = string.IsNullOrEmpty(rslt);
356 public bool IsCreated
358 get { return this._isCreated; }
361 public override string ToString()
364 return base.ToString();
370 private void ListManage_Validating(object sender, CancelEventArgs e)
372 if (this.EditCheckBox.Checked)
375 this.CancelButton.PerformClick();