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.
30 using System.Collections.Generic;
31 using System.ComponentModel;
36 using System.Threading.Tasks;
37 using System.Windows.Forms;
39 using OpenTween.Api.DataModel;
40 using OpenTween.Connection;
44 public partial class MyLists : OTBaseForm
46 private readonly TwitterApi twitterApi = null!;
47 private readonly string contextScreenName = null!;
49 /// <summary>自分が所有しているリスト</summary>
50 private ListElement[] ownedLists = Array.Empty<ListElement>();
52 /// <summary>操作対象のユーザーが追加されているリストのID</summary>
53 private long[] addedListIds = Array.Empty<long>();
56 => this.InitializeComponent();
58 public MyLists(string screenName, TwitterApi twitterApi)
60 this.InitializeComponent();
62 this.twitterApi = twitterApi;
63 this.contextScreenName = screenName;
65 this.Text = screenName + Properties.Resources.MyLists1;
68 private async Task RefreshListBox()
70 using (var dialog = new WaitingDialog(Properties.Resources.ListsGetting))
72 var cancellationToken = dialog.EnableCancellation();
74 var task = Task.Run(() => this.FetchMembershipListIds());
75 await dialog.WaitForAsync(this, task);
77 cancellationToken.ThrowIfCancellationRequested();
80 using (ControlTransaction.Update(this.ListsCheckedListBox))
82 this.ListsCheckedListBox.Items.Clear();
84 foreach (var list in this.ownedLists)
86 var added = this.addedListIds.Contains(list.Id);
87 this.ListsCheckedListBox.Items.Add(list, isChecked: added);
92 private async Task FetchMembershipListIds()
94 var ownedListData = await TwitterLists.GetAllItemsAsync(x =>
95 this.twitterApi.ListsOwnerships(this.twitterApi.CurrentScreenName, cursor: x, count: 1000))
96 .ConfigureAwait(false);
98 this.ownedLists = ownedListData.Select(x => new ListElement(x, null!)).ToArray();
100 var listsUserAddedTo = await TwitterLists.GetAllItemsAsync(x =>
101 this.twitterApi.ListsMemberships(this.contextScreenName, cursor: x, count: 1000, filterToOwnedLists: true))
102 .ConfigureAwait(false);
104 this.addedListIds = listsUserAddedTo.Select(x => x.Id).ToArray();
107 private async Task AddToList(ListElement list)
111 await this.twitterApi.ListsMembersCreate(list.Id, this.contextScreenName)
114 var index = this.ListsCheckedListBox.Items.IndexOf(list);
115 this.ListsCheckedListBox.SetItemCheckState(index, CheckState.Checked);
117 catch (WebApiException ex)
119 MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
123 private async Task RemoveFromList(ListElement list)
127 await this.twitterApi.ListsMembersDestroy(list.Id, this.contextScreenName)
130 var index = this.ListsCheckedListBox.Items.IndexOf(list);
131 this.ListsCheckedListBox.SetItemCheckState(index, CheckState.Unchecked);
133 catch (WebApiException ex)
135 MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
139 private async void MyLists_Load(object sender, EventArgs e)
141 using (ControlTransaction.Disabled(this))
145 await this.RefreshListBox();
147 catch (OperationCanceledException)
149 this.DialogResult = DialogResult.Cancel;
151 catch (WebApiException ex)
153 MessageBox.Show($"Failed to get lists. ({ex.Message})");
154 this.DialogResult = DialogResult.Abort;
159 private async void ListsCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
161 // 他のイベント等で操作中の場合は無視する
165 using (ControlTransaction.Disabled(this))
167 var list = (ListElement)this.ListsCheckedListBox.Items[e.Index];
169 if (e.CurrentValue == CheckState.Unchecked)
170 await this.AddToList(list);
172 await this.RemoveFromList(list);
176 private void ListsCheckedListBox_MouseDown(object sender, MouseEventArgs e)
180 case MouseButtons.Left:
181 // 項目が無い部分をクリックしても、選択されている項目のチェック状態が変更されてしまうので、その対策
182 for (var index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
184 if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
187 this.ListsCheckedListBox.SelectedItem = null;
189 case MouseButtons.Right:
190 // コンテキストメニューの項目実行時にSelectedItemプロパティを利用出来るように
191 for (var index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
193 if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
195 this.ListsCheckedListBox.SetSelected(index, true);
199 this.ListsCheckedListBox.SelectedItem = null;
204 private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
205 => e.Cancel = this.ListsCheckedListBox.SelectedItem == null;
207 private async void MenuItemAdd_Click(object sender, EventArgs e)
209 using (ControlTransaction.Disabled(this))
211 await this.AddToList((ListElement)this.ListsCheckedListBox.SelectedItem);
215 private async void MenuItemDelete_Click(object sender, EventArgs e)
217 using (ControlTransaction.Disabled(this))
219 await this.RemoveFromList((ListElement)this.ListsCheckedListBox.SelectedItem);
223 private async void MenuItemReload_Click(object sender, EventArgs e)
225 using (ControlTransaction.Disabled(this))
229 await this.RefreshListBox();
231 catch (OperationCanceledException)
234 catch (WebApiException ex)
236 MessageBox.Show($"Failed to get lists. ({ex.Message})");
241 private async void ListRefreshButton_Click(object sender, EventArgs e)
243 using (ControlTransaction.Disabled(this))
247 await this.RefreshListBox();
249 catch (OperationCanceledException)
252 catch (WebApiException ex)
254 MessageBox.Show($"Failed to get lists. ({ex.Message})");
259 private void CloseButton_Click(object sender, EventArgs e)