OSDN Git Service

usingの順序を揃える (SA1208, SA1210)
[opentween/open-tween.git] / OpenTween / MyLists.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 #nullable enable
28
29 using System;
30 using System.Collections.Generic;
31 using System.ComponentModel;
32 using System.Data;
33 using System.Drawing;
34 using System.Linq;
35 using System.Text;
36 using System.Threading.Tasks;
37 using System.Windows.Forms;
38 using OpenTween.Api;
39 using OpenTween.Api.DataModel;
40 using OpenTween.Connection;
41
42 namespace OpenTween
43 {
44     public partial class MyLists : OTBaseForm
45     {
46         private readonly TwitterApi twitterApi = null!;
47         private readonly string contextScreenName = null!;
48
49         /// <summary>自分が所有しているリスト</summary>
50         private ListElement[] ownedLists = Array.Empty<ListElement>();
51
52         /// <summary>操作対象のユーザーが追加されているリストのID</summary>
53         private long[] addedListIds = Array.Empty<long>();
54
55         public MyLists()
56             => this.InitializeComponent();
57
58         public MyLists(string screenName, TwitterApi twitterApi)
59         {
60             this.InitializeComponent();
61
62             this.twitterApi = twitterApi;
63             this.contextScreenName = screenName;
64
65             this.Text = screenName + Properties.Resources.MyLists1;
66         }
67
68         private async Task RefreshListBox()
69         {
70             using (var dialog = new WaitingDialog(Properties.Resources.ListsGetting))
71             {
72                 var cancellationToken = dialog.EnableCancellation();
73
74                 var task = Task.Run(() => this.FetchMembershipListIds());
75                 await dialog.WaitForAsync(this, task);
76
77                 cancellationToken.ThrowIfCancellationRequested();
78             }
79
80             using (ControlTransaction.Update(this.ListsCheckedListBox))
81             {
82                 this.ListsCheckedListBox.Items.Clear();
83
84                 foreach (var list in this.ownedLists)
85                 {
86                     var added = this.addedListIds.Contains(list.Id);
87                     this.ListsCheckedListBox.Items.Add(list, isChecked: added);
88                 }
89             }
90         }
91
92         private async Task FetchMembershipListIds()
93         {
94             var ownedListData = await TwitterLists.GetAllItemsAsync(x =>
95                 this.twitterApi.ListsOwnerships(this.twitterApi.CurrentScreenName, cursor: x, count: 1000))
96                     .ConfigureAwait(false);
97
98             this.ownedLists = ownedListData.Select(x => new ListElement(x, null!)).ToArray();
99
100             var listsUserAddedTo = await TwitterLists.GetAllItemsAsync(x =>
101                 this.twitterApi.ListsMemberships(this.contextScreenName, cursor: x, count: 1000, filterToOwnedLists: true))
102                     .ConfigureAwait(false);
103
104             this.addedListIds = listsUserAddedTo.Select(x => x.Id).ToArray();
105         }
106
107         private async Task AddToList(ListElement list)
108         {
109             try
110             {
111                 await this.twitterApi.ListsMembersCreate(list.Id, this.contextScreenName)
112                     .IgnoreResponse();
113
114                 var index = this.ListsCheckedListBox.Items.IndexOf(list);
115                 this.ListsCheckedListBox.SetItemCheckState(index, CheckState.Checked);
116             }
117             catch (WebApiException ex)
118             {
119                 MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
120             }
121         }
122
123         private async Task RemoveFromList(ListElement list)
124         {
125             try
126             {
127                 await this.twitterApi.ListsMembersDestroy(list.Id, this.contextScreenName)
128                     .IgnoreResponse();
129
130                 var index = this.ListsCheckedListBox.Items.IndexOf(list);
131                 this.ListsCheckedListBox.SetItemCheckState(index, CheckState.Unchecked);
132             }
133             catch (WebApiException ex)
134             {
135                 MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
136             }
137         }
138
139         private async void MyLists_Load(object sender, EventArgs e)
140         {
141             using (ControlTransaction.Disabled(this))
142             {
143                 try
144                 {
145                     await this.RefreshListBox();
146                 }
147                 catch (OperationCanceledException)
148                 {
149                     this.DialogResult = DialogResult.Cancel;
150                 }
151                 catch (WebApiException ex)
152                 {
153                     MessageBox.Show($"Failed to get lists. ({ex.Message})");
154                     this.DialogResult = DialogResult.Abort;
155                 }
156             }
157         }
158
159         private async void ListsCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
160         {
161             // 他のイベント等で操作中の場合は無視する
162             if (!this.Enabled)
163                 return;
164
165             using (ControlTransaction.Disabled(this))
166             {
167                 var list = (ListElement)this.ListsCheckedListBox.Items[e.Index];
168
169                 if (e.CurrentValue == CheckState.Unchecked)
170                     await this.AddToList(list);
171                 else
172                     await this.RemoveFromList(list);
173             }
174         }
175
176         private void ListsCheckedListBox_MouseDown(object sender, MouseEventArgs e)
177         {
178             switch (e.Button)
179             {
180                 case MouseButtons.Left:
181                     // 項目が無い部分をクリックしても、選択されている項目のチェック状態が変更されてしまうので、その対策
182                     for (var index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
183                     {
184                         if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
185                             return;
186                     }
187                     this.ListsCheckedListBox.SelectedItem = null;
188                     break;
189                 case MouseButtons.Right:
190                     // コンテキストメニューの項目実行時にSelectedItemプロパティを利用出来るように
191                     for (var index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
192                     {
193                         if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
194                         {
195                             this.ListsCheckedListBox.SetSelected(index, true);
196                             return;
197                         }
198                     }
199                     this.ListsCheckedListBox.SelectedItem = null;
200                     break;
201             }
202         }
203
204         private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
205             => e.Cancel = this.ListsCheckedListBox.SelectedItem == null;
206
207         private async void MenuItemAdd_Click(object sender, EventArgs e)
208         {
209             using (ControlTransaction.Disabled(this))
210             {
211                 await this.AddToList((ListElement)this.ListsCheckedListBox.SelectedItem);
212             }
213         }
214
215         private async void MenuItemDelete_Click(object sender, EventArgs e)
216         {
217             using (ControlTransaction.Disabled(this))
218             {
219                 await this.RemoveFromList((ListElement)this.ListsCheckedListBox.SelectedItem);
220             }
221         }
222
223         private async void MenuItemReload_Click(object sender, EventArgs e)
224         {
225             using (ControlTransaction.Disabled(this))
226             {
227                 try
228                 {
229                     await this.RefreshListBox();
230                 }
231                 catch (OperationCanceledException) { }
232                 catch (WebApiException ex)
233                 {
234                     MessageBox.Show($"Failed to get lists. ({ex.Message})");
235                 }
236             }
237         }
238
239         private async void ListRefreshButton_Click(object sender, EventArgs e)
240         {
241             using (ControlTransaction.Disabled(this))
242             {
243                 try
244                 {
245                     await this.RefreshListBox();
246                 }
247                 catch (OperationCanceledException) { }
248                 catch (WebApiException ex)
249                 {
250                     MessageBox.Show($"Failed to get lists. ({ex.Message})");
251                 }
252             }
253         }
254
255         private void CloseButton_Click(object sender, EventArgs e)
256             => this.Close();
257     }
258 }