OSDN Git Service

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