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.Windows.Forms;
38 public partial class MyLists : OTBaseForm
40 private string contextUserName;
45 InitializeComponent();
48 public MyLists(string userName, Twitter tw)
50 this.InitializeComponent();
52 this.contextUserName = userName;
55 this.Text = this.contextUserName + Properties.Resources.MyLists1;
58 private void MyLists_Load(object sender, EventArgs e)
60 this.ListsCheckedListBox.ItemCheck -= this.ListsCheckedListBox_ItemCheck;
62 this.ListsCheckedListBox.Items.AddRange(TabInformations.GetInstance().SubscribableLists.FindAll((item) => item.Username == this._tw.Username).ToArray());
64 for (int i = 0; i < this.ListsCheckedListBox.Items.Count; i++)
66 ListElement listItem = (ListElement)this.ListsCheckedListBox.Items[i];
68 List<PostClass> listPost = new List<PostClass>();
69 List<PostClass> otherPost = new List<PostClass>();
71 foreach (TabClass tab in TabInformations.GetInstance().Tabs.Values)
73 if (tab.TabType == MyCommon.TabUsageType.Lists)
75 if (listItem.Id == tab.ListInfo.Id)
76 listPost.AddRange(tab.Posts.Values);
78 otherPost.AddRange(tab.Posts.Values);
83 if (listPost.Count == 0)
85 this.ListsCheckedListBox.SetItemCheckState(i, CheckState.Indeterminate);
89 //リストに該当ユーザーのポストが含まれていれば、リストにユーザーが含まれているとする。
90 if (listPost.Exists((item) => item.ScreenName == contextUserName))
92 this.ListsCheckedListBox.SetItemChecked(i, true);
96 List<long> listPostUserIDs = new List<long>();
97 List<string> listPostUserNames = new List<string>();
98 DateTime listOlderPostCreatedAt = DateTime.MaxValue;
99 DateTime listNewistPostCreatedAt = DateTime.MinValue;
101 foreach (PostClass post in listPost)
103 if (post.UserId > 0 && !listPostUserIDs.Contains(post.UserId))
105 listPostUserIDs.Add(post.UserId);
107 if (post.ScreenName != null && !listPostUserNames.Contains(post.ScreenName))
109 listPostUserNames.Add(post.ScreenName);
111 if (post.CreatedAt < listOlderPostCreatedAt)
113 listOlderPostCreatedAt = post.CreatedAt;
115 if (post.CreatedAt > listNewistPostCreatedAt)
117 listNewistPostCreatedAt = post.CreatedAt;
121 //リスト中のユーザーの人数がlistItem.MemberCount以上で、かつ該当のユーザーが含まれていなければ、リストにユーザーは含まれていないとする。
122 if (listItem.MemberCount > 0 && listItem.MemberCount <= listPostUserIDs.Count && (!listPostUserNames.Contains(contextUserName)))
124 this.ListsCheckedListBox.SetItemChecked(i, false);
128 otherPost.AddRange(TabInformations.GetInstance().Posts.Values);
130 //リストに該当ユーザーのポストが含まれていないのにリスト以外で取得したポストの中にリストに含まれるべきポストがある場合は、リストにユーザーは含まれていないとする。
131 if (otherPost.Exists((item) => (item.ScreenName == this.contextUserName) && (item.CreatedAt > listOlderPostCreatedAt) && (item.CreatedAt < listNewistPostCreatedAt) && ((!item.IsReply) || listPostUserNames.Contains(item.InReplyToUser))))
133 this.ListsCheckedListBox.SetItemChecked(i, false);
137 this.ListsCheckedListBox.SetItemCheckState(i, CheckState.Indeterminate);
140 this.ListsCheckedListBox.ItemCheck += this.ListsCheckedListBox_ItemCheck;
143 private void ListRefreshButton_Click(object sender, EventArgs e)
145 string rslt = this._tw.GetListsApi();
146 if (!string.IsNullOrEmpty(rslt))
148 MessageBox.Show(String.Format(Properties.Resources.ListsDeleteFailed, rslt));
152 this.ListsCheckedListBox.Items.Clear();
153 this.MyLists_Load(this, EventArgs.Empty);
157 private void ListsCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
159 ListElement list = (ListElement)this.ListsCheckedListBox.Items[e.Index];
162 switch (e.CurrentValue)
164 case CheckState.Indeterminate:
166 rslt = this._tw.ContainsUserAtList(list.Id.ToString(), contextUserName, ref ret);
167 if (!string.IsNullOrEmpty(rslt))
169 MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, rslt));
170 e.NewValue = CheckState.Indeterminate;
175 e.NewValue = CheckState.Checked;
177 e.NewValue = CheckState.Unchecked;
180 case CheckState.Unchecked:
181 rslt = this._tw.AddUserToList(list.Id.ToString(), this.contextUserName.ToString());
182 if (!string.IsNullOrEmpty(rslt))
184 MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, rslt));
185 e.NewValue = CheckState.Indeterminate;
188 case CheckState.Checked:
189 rslt = this._tw.RemoveUserToList(list.Id.ToString(), this.contextUserName.ToString());
190 if (!string.IsNullOrEmpty(rslt))
192 MessageBox.Show(String.Format(Properties.Resources.ListManageOKButton2, rslt));
193 e.NewValue = CheckState.Indeterminate;
199 private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
201 e.Cancel = this.ListsCheckedListBox.SelectedItem == null;
204 private void 追加AToolStripMenuItem_Click(object sender, EventArgs e)
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);
212 private void 削除DToolStripMenuItem_Click(object sender, EventArgs e)
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);
220 private void 更新RToolStripMenuItem_Click(object sender, EventArgs e)
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);
228 private void ListsCheckedListBox_MouseDown(object sender, MouseEventArgs e)
232 case MouseButtons.Left:
233 //項目が無い部分をクリックしても、選択されている項目のチェック状態が変更されてしまうので、その対策
234 for (int index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
236 if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
239 this.ListsCheckedListBox.SelectedItem = null;
241 case MouseButtons.Right:
242 //コンテキストメニューの項目実行時にSelectedItemプロパティを利用出来るように
243 for (int index = 0; index < this.ListsCheckedListBox.Items.Count; index++)
245 if (this.ListsCheckedListBox.GetItemRectangle(index).Contains(e.Location))
247 this.ListsCheckedListBox.SetSelected(index, true);
251 this.ListsCheckedListBox.SelectedItem = null;
256 private void CloseButton_Click(object sender, EventArgs e)