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.Threading.Tasks;
35 using System.Windows.Forms;
37 using OpenTween.Api.DataModel;
41 public partial class MyLists : OTBaseForm
43 private readonly TwitterApi twitterApi;
44 private readonly string contextScreenName;
46 /// <summary>自分が所有しているリスト</summary>
47 private ListElement[] ownedLists = new ListElement[0];
49 /// <summary>操作対象のユーザーが追加されているリストのID</summary>
50 private long[] addedListIds = new long[0];
54 InitializeComponent();
57 public MyLists(string screenName, TwitterApi twitterApi)
59 this.InitializeComponent();
61 this.twitterApi = twitterApi;
62 this.contextScreenName = screenName;
64 this.Text = screenName + Properties.Resources.MyLists1;
67 private async Task RefreshListBox()
69 using (var dialog = new WaitingDialog(Properties.Resources.ListsGetting))
71 var cancellationToken = dialog.EnableCancellation();
73 var task = Task.Run(() => this.FetchMembershipListIds());
74 await dialog.WaitForAsync(this, task);
76 cancellationToken.ThrowIfCancellationRequested();
79 using (ControlTransaction.Update(this.ListsCheckedListBox))
81 this.ListsCheckedListBox.Items.Clear();
83 foreach (var list in this.ownedLists)
85 var added = this.addedListIds.Contains(list.Id);
86 this.ListsCheckedListBox.Items.Add(list, isChecked: added);
91 private async Task FetchMembershipListIds()
93 var ownedListData = await TwitterLists.GetAllItemsAsync(x =>
94 this.twitterApi.ListsOwnerships(this.twitterApi.CurrentScreenName, cursor: x, count: 1000))
95 .ConfigureAwait(false);
97 this.ownedLists = ownedListData.Select(x => new ListElement(x, null)).ToArray();
99 var listsUserAddedTo = await TwitterLists.GetAllItemsAsync(x =>
100 this.twitterApi.ListsMemberships(this.contextScreenName, cursor: x, count: 1000, filterToOwnedLists: true))
101 .ConfigureAwait(false);
103 this.addedListIds = listsUserAddedTo.Select(x => x.Id).ToArray();
106 private async Task AddToList(ListElement list)
110 await this.twitterApi.ListsMembersCreate(list.Id, this.contextScreenName);
112 var index = this.ListsCheckedListBox.Items.IndexOf(list);
113 this.ListsCheckedListBox.SetItemCheckState(index, CheckState.Checked);
115 catch (WebApiException ex)
117 MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
121 private async Task RemoveFromList(ListElement list)
125 await this.twitterApi.ListsMembersDestroy(list.Id, this.contextScreenName);
127 var index = this.ListsCheckedListBox.Items.IndexOf(list);
128 this.ListsCheckedListBox.SetItemCheckState(index, CheckState.Unchecked);
130 catch (WebApiException ex)
132 MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
136 private async void MyLists_Load(object sender, EventArgs e)
138 using (ControlTransaction.Disabled(this))
142 await this.RefreshListBox();
144 catch (OperationCanceledException)
146 this.DialogResult = DialogResult.Cancel;
148 catch (WebApiException ex)
150 MessageBox.Show($"Failed to get lists. ({ex.Message})");
151 this.DialogResult = DialogResult.Abort;
156 private async void ListsCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
158 // 他のイベント等で操作中の場合は無視する
162 using (ControlTransaction.Disabled(this))
164 var list = (ListElement)this.ListsCheckedListBox.Items[e.Index];
166 if (e.CurrentValue == CheckState.Unchecked)
167 await this.AddToList(list);
169 await this.RemoveFromList(list);
173 private void ListsCheckedListBox_MouseDown(object sender, MouseEventArgs e)
177 case MouseButtons.Left:
178 //項目が無い部分をクリックしても、選択されている項目のチェック状態が変更されてしまうので、その対策
179 for (int index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
181 if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
184 this.ListsCheckedListBox.SelectedItem = null;
186 case MouseButtons.Right:
187 //コンテキストメニューの項目実行時にSelectedItemプロパティを利用出来るように
188 for (int index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
190 if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
192 this.ListsCheckedListBox.SetSelected(index, true);
196 this.ListsCheckedListBox.SelectedItem = null;
201 private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
203 e.Cancel = this.ListsCheckedListBox.SelectedItem == null;
206 private async void MenuItemAdd_Click(object sender, EventArgs e)
208 using (ControlTransaction.Disabled(this))
210 await this.AddToList((ListElement)this.ListsCheckedListBox.SelectedItem);
214 private async void MenuItemDelete_Click(object sender, EventArgs e)
216 using (ControlTransaction.Disabled(this))
218 await this.RemoveFromList((ListElement)this.ListsCheckedListBox.SelectedItem);
222 private async void MenuItemReload_Click(object sender, EventArgs e)
224 using (ControlTransaction.Disabled(this))
228 await this.RefreshListBox();
230 catch (OperationCanceledException) { }
231 catch (WebApiException ex)
233 MessageBox.Show($"Failed to get lists. ({ex.Message})");
238 private async void ListRefreshButton_Click(object sender, EventArgs e)
240 using (ControlTransaction.Disabled(this))
244 await this.RefreshListBox();
246 catch (OperationCanceledException) { }
247 catch (WebApiException ex)
249 MessageBox.Show($"Failed to get lists. ({ex.Message})");
254 private void CloseButton_Click(object sender, EventArgs e)