OSDN Git Service

プロフィール画面へのD&Dによるアイコン変更時に確認ダイアログを表示するように変更
[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.Windows.Forms;
35
36 namespace OpenTween
37 {
38     public partial class MyLists : OTBaseForm
39     {
40         private string contextUserName;
41         private Twitter _tw;
42
43         public MyLists()
44         {
45             InitializeComponent();
46         }
47
48         public MyLists(string userName, Twitter tw)
49         {
50             this.InitializeComponent();
51
52             this.contextUserName = userName;
53             this._tw = tw;
54
55             this.Text = this.contextUserName + Properties.Resources.MyLists1;
56         }
57
58         private void MyLists_Load(object sender, EventArgs e)
59         {
60             this.ListsCheckedListBox.ItemCheck -= this.ListsCheckedListBox_ItemCheck;
61
62             this.ListsCheckedListBox.Items.AddRange(TabInformations.GetInstance().SubscribableLists.FindAll((item) => item.Username == this._tw.Username).ToArray());
63
64             for (int i = 0; i < this.ListsCheckedListBox.Items.Count; i++)
65             {
66                 ListElement listItem = (ListElement)this.ListsCheckedListBox.Items[i];
67
68                 List<PostClass> listPost = new List<PostClass>();
69                 List<PostClass> otherPost = new List<PostClass>();
70
71                 foreach (TabClass tab in TabInformations.GetInstance().Tabs.Values)
72                 {
73                     if (tab.TabType == MyCommon.TabUsageType.Lists)
74                     {
75                         if (listItem.Id == tab.ListInfo.Id)
76                             listPost.AddRange(tab.Posts.Values);
77                         else
78                             otherPost.AddRange(tab.Posts.Values);
79                     }
80                 }
81
82                 //リストが空の場合は推定不能
83                 if (listPost.Count == 0)
84                 {
85                     this.ListsCheckedListBox.SetItemCheckState(i, CheckState.Indeterminate);
86                     continue;
87                 }
88
89                 //リストに該当ユーザーのポストが含まれていれば、リストにユーザーが含まれているとする。
90                 if (listPost.Exists((item) => item.ScreenName == contextUserName))
91                 {
92                     this.ListsCheckedListBox.SetItemChecked(i, true);
93                     continue;
94                 }
95
96                 List<long> listPostUserIDs = new List<long>();
97                 List<string> listPostUserNames = new List<string>();
98                 DateTime listOlderPostCreatedAt = DateTime.MaxValue;
99                 DateTime listNewistPostCreatedAt = DateTime.MinValue;
100
101                 foreach (PostClass post in listPost)
102                 {
103                     if (post.UserId > 0 && !listPostUserIDs.Contains(post.UserId))
104                     {
105                         listPostUserIDs.Add(post.UserId);
106                     }
107                     if (post.ScreenName != null && !listPostUserNames.Contains(post.ScreenName))
108                     {
109                         listPostUserNames.Add(post.ScreenName);
110                     }
111                     if (post.CreatedAt < listOlderPostCreatedAt)
112                     {
113                         listOlderPostCreatedAt = post.CreatedAt;
114                     }
115                     if (post.CreatedAt > listNewistPostCreatedAt)
116                     {
117                         listNewistPostCreatedAt = post.CreatedAt;
118                     }
119                 }
120
121                 //リスト中のユーザーの人数がlistItem.MemberCount以上で、かつ該当のユーザーが含まれていなければ、リストにユーザーは含まれていないとする。
122                 if (listItem.MemberCount > 0 && listItem.MemberCount <= listPostUserIDs.Count && (!listPostUserNames.Contains(contextUserName)))
123                 {
124                     this.ListsCheckedListBox.SetItemChecked(i, false);
125                     continue;
126                 }
127
128                 otherPost.AddRange(TabInformations.GetInstance().Posts.Values);
129
130                 //リストに該当ユーザーのポストが含まれていないのにリスト以外で取得したポストの中にリストに含まれるべきポストがある場合は、リストにユーザーは含まれていないとする。
131                 if (otherPost.Exists((item) => (item.ScreenName == this.contextUserName) && (item.CreatedAt > listOlderPostCreatedAt) && (item.CreatedAt < listNewistPostCreatedAt) && ((!item.IsReply) || listPostUserNames.Contains(item.InReplyToUser))))
132                 {
133                     this.ListsCheckedListBox.SetItemChecked(i, false);
134                     continue;
135                 }
136
137                 this.ListsCheckedListBox.SetItemCheckState(i, CheckState.Indeterminate);
138             }
139
140             this.ListsCheckedListBox.ItemCheck += this.ListsCheckedListBox_ItemCheck;
141         }
142
143         private void ListRefreshButton_Click(object sender, EventArgs e)
144         {
145             string rslt = this._tw.GetListsApi();
146             if (!string.IsNullOrEmpty(rslt))
147             {
148                 MessageBox.Show(String.Format(Properties.Resources.ListsDeleteFailed, rslt));
149             }
150             else
151             {
152                 this.ListsCheckedListBox.Items.Clear();
153                 this.MyLists_Load(this, EventArgs.Empty);
154             }
155         }
156
157         private void ListsCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
158         {
159             ListElement list = (ListElement)this.ListsCheckedListBox.Items[e.Index];
160             string rslt;
161
162             switch (e.CurrentValue)
163             {
164                 case CheckState.Indeterminate:
165                     bool ret = false;
166                     rslt = this._tw.ContainsUserAtList(list.Id.ToString(), contextUserName, ref ret);
167                     if (!string.IsNullOrEmpty(rslt))
168                     {
169                         MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, rslt));
170                         e.NewValue = CheckState.Indeterminate;
171                     }
172                     else
173                     {
174                         if (ret)
175                             e.NewValue = CheckState.Checked;
176                         else
177                             e.NewValue = CheckState.Unchecked;
178                     }
179                     break;
180                 case CheckState.Unchecked:
181                     rslt = this._tw.AddUserToList(list.Id.ToString(), this.contextUserName.ToString());
182                     if (!string.IsNullOrEmpty(rslt))
183                     {
184                         MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, rslt));
185                         e.NewValue = CheckState.Indeterminate;
186                     }
187                     break;
188                 case CheckState.Checked:
189                     rslt = this._tw.RemoveUserToList(list.Id.ToString(), this.contextUserName.ToString());
190                     if (!string.IsNullOrEmpty(rslt))
191                     {
192                         MessageBox.Show(String.Format(Properties.Resources.ListManageOKButton2, rslt));
193                         e.NewValue = CheckState.Indeterminate;
194                     }
195                     break;
196             }
197         }
198
199         private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
200         {
201             e.Cancel = this.ListsCheckedListBox.SelectedItem == null;
202         }
203
204         private void 追加AToolStripMenuItem_Click(object sender, EventArgs e)
205         {
206             this.ListsCheckedListBox.ItemCheck -= this.ListsCheckedListBox_ItemCheck;
207             this.ListsCheckedListBox.SetItemCheckState(this.ListsCheckedListBox.SelectedIndex, CheckState.Unchecked);
208             this.ListsCheckedListBox.ItemCheck += this.ListsCheckedListBox_ItemCheck;
209             this.ListsCheckedListBox.SetItemCheckState(this.ListsCheckedListBox.SelectedIndex, CheckState.Checked);
210         }
211
212         private void 削除DToolStripMenuItem_Click(object sender, EventArgs e)
213         {
214             this.ListsCheckedListBox.ItemCheck -= this.ListsCheckedListBox_ItemCheck;
215             this.ListsCheckedListBox.SetItemCheckState(this.ListsCheckedListBox.SelectedIndex, CheckState.Checked);
216             this.ListsCheckedListBox.ItemCheck += this.ListsCheckedListBox_ItemCheck;
217             this.ListsCheckedListBox.SetItemCheckState(this.ListsCheckedListBox.SelectedIndex, CheckState.Unchecked);
218         }
219
220         private void 更新RToolStripMenuItem_Click(object sender, EventArgs e)
221         {
222             this.ListsCheckedListBox.ItemCheck -= this.ListsCheckedListBox_ItemCheck;
223             this.ListsCheckedListBox.SetItemCheckState(this.ListsCheckedListBox.SelectedIndex, CheckState.Indeterminate);
224             this.ListsCheckedListBox.ItemCheck += this.ListsCheckedListBox_ItemCheck;
225             this.ListsCheckedListBox.SetItemCheckState(this.ListsCheckedListBox.SelectedIndex, CheckState.Checked);
226         }
227
228         private void ListsCheckedListBox_MouseDown(object sender, MouseEventArgs e)
229         {
230             switch (e.Button)
231             {
232                 case MouseButtons.Left:
233                     //項目が無い部分をクリックしても、選択されている項目のチェック状態が変更されてしまうので、その対策
234                     for (int index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
235                     {
236                         if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
237                             return;
238                     }
239                     this.ListsCheckedListBox.SelectedItem = null;
240                     break;
241                 case MouseButtons.Right:
242                     //コンテキストメニューの項目実行時にSelectedItemプロパティを利用出来るように
243                     for (int index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
244                     {
245                         if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
246                         {
247                             this.ListsCheckedListBox.SetSelected(index, true);
248                             return;
249                         }
250                     }
251                     this.ListsCheckedListBox.SelectedItem = null;
252                     break;
253             }
254         }
255
256         private void CloseButton_Click(object sender, EventArgs e)
257         {
258             this.Close();
259         }
260     }
261 }