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-2012 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;
31 using System.Diagnostics.CodeAnalysis;
35 using System.Windows.Forms;
36 using System.Text.RegularExpressions;
41 public partial class EventViewerDialog : OTBaseForm
43 public List<Twitter.FormattedEvent> EventSource { get; set; }
45 private Twitter.FormattedEvent[] _filterdEventSource;
47 private ListViewItem[] _ItemCache = null;
48 private int _itemCacheIndex;
50 private TabPage _curTab = null;
52 public EventViewerDialog()
54 InitializeComponent();
56 // メイリオフォント指定時にタブの最小幅が広くなる問題の対策
57 this.TabEventType.HandleCreated += (s, e) => NativeMethods.SetMinTabWidth((TabControl)s, 40);
60 private void OK_Button_Click(object sender, EventArgs e)
62 this.DialogResult = DialogResult.OK;
66 private void Cancel_Button_Click(object sender, EventArgs e)
68 this.DialogResult = DialogResult.Cancel;
72 private ListViewItem CreateListViewItem(Twitter.FormattedEvent source)
74 string[] s = { source.CreatedAt.ToString(), source.Event.ToUpper(), source.Username, source.Target };
75 return new ListViewItem(s);
78 private void EventViewerDialog_Shown(object sender, EventArgs e)
81 foreach (var tabPage in CreateTabsFromUserStreamsEvent())
83 TabEventType.TabPages.Add(tabPage);
86 EventList.BeginUpdate();
87 _curTab = TabEventType.SelectedTab;
88 CreateFilterdEventSource();
89 EventList.EndUpdate();
90 this.TopMost = AppendSettingDialog.Instance.AlwaysTop;
93 private void EventList_DoubleClick(object sender, EventArgs e)
95 if (EventList.SelectedIndices.Count != 0 && _filterdEventSource[EventList.SelectedIndices[0]] != null)
97 ((TweenMain)this.Owner).OpenUriAsync("https://twitter.com/" + _filterdEventSource[EventList.SelectedIndices[0]].Username);
101 private MyCommon.EVENTTYPE ParseEventTypeFromTag()
103 return (MyCommon.EVENTTYPE)Enum.Parse(typeof(MyCommon.EVENTTYPE), _curTab.Tag.ToString());
106 private bool IsFilterMatch(Twitter.FormattedEvent x)
108 if (!CheckBoxFilter.Checked || string.IsNullOrEmpty(TextBoxKeyword.Text))
114 if (CheckRegex.Checked)
118 Regex rx = new Regex(TextBoxKeyword.Text);
119 return rx.Match(x.Username).Success || rx.Match(x.Target).Success;
123 MessageBox.Show(Properties.Resources.ButtonOK_ClickText3 + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
129 return x.Username.Contains(TextBoxKeyword.Text) || x.Target.Contains(TextBoxKeyword.Text);
134 private void CreateFilterdEventSource()
136 if (EventSource != null && EventSource.Count > 0)
138 _filterdEventSource = EventSource.FindAll((x) => (CheckExcludeMyEvent.Checked ? !x.IsMe : true) &&
139 (x.Eventtype & ParseEventTypeFromTag()) != 0 &&
140 IsFilterMatch(x)).ToArray();
142 EventList.VirtualListSize = _filterdEventSource.Count();
143 StatusLabelCount.Text = string.Format("{0} / {1}", _filterdEventSource.Count(), EventSource.Count());
147 StatusLabelCount.Text = "0 / 0";
151 private void CheckExcludeMyEvent_CheckedChanged(object sender, EventArgs e)
153 CreateFilterdEventSource();
156 private void ButtonRefresh_Click(object sender, EventArgs e)
158 CreateFilterdEventSource();
161 private void TabEventType_SelectedIndexChanged(object sender, EventArgs e)
163 CreateFilterdEventSource();
166 private void TabEventType_Selecting(object sender, TabControlCancelEventArgs e)
169 if (!e.TabPage.Controls.Contains(EventList))
171 e.TabPage.Controls.Add(EventList);
175 private void TextBoxKeyword_KeyPress(object sender, KeyPressEventArgs e)
177 if (e.KeyChar == (char)Keys.Enter)
179 CreateFilterdEventSource();
184 private void EventList_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
186 if (_ItemCache != null && e.ItemIndex >= _itemCacheIndex && e.ItemIndex < _itemCacheIndex + _ItemCache.Length)
189 e.Item = _ItemCache[e.ItemIndex - _itemCacheIndex];
194 e.Item = CreateListViewItem(_filterdEventSource[e.ItemIndex]);
198 private void EventList_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
200 CreateCache(e.StartIndex, e.EndIndex);
203 private void CreateCache(int StartIndex, int EndIndex)
205 //キャッシュ要求(要求範囲±30を作成)
207 if (StartIndex < 0) StartIndex = 0;
209 if (EndIndex > _filterdEventSource.Count() - 1)
211 EndIndex = _filterdEventSource.Count() - 1;
213 _ItemCache = new ListViewItem[] { };
214 Array.Resize(ref _ItemCache, EndIndex - StartIndex + 1);
215 _itemCacheIndex = StartIndex;
216 for (int i = 0; i < _ItemCache.Length; i++)
218 _ItemCache[i] = CreateListViewItem(_filterdEventSource[StartIndex + i]);
222 private void SaveLogButton_Click(object sender, EventArgs e)
224 DialogResult rslt = MessageBox.Show(string.Format(Properties.Resources.SaveLogMenuItem_ClickText5, Environment.NewLine),
225 Properties.Resources.SaveLogMenuItem_ClickText2,
226 MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
229 case DialogResult.Yes:
230 SaveFileDialog1.FileName = MyCommon.GetAssemblyName() + "Events" + _curTab.Tag.ToString() + DateTime.Now.ToString("yyMMdd-HHmmss") + ".tsv";
232 case DialogResult.No:
233 SaveFileDialog1.FileName = MyCommon.GetAssemblyName() + "Events" + DateTime.Now.ToString("yyMMdd-HHmmss") + ".tsv";
239 SaveFileDialog1.InitialDirectory = Application.StartupPath;
240 SaveFileDialog1.Filter = Properties.Resources.SaveLogMenuItem_ClickText3;
241 SaveFileDialog1.FilterIndex = 0;
242 SaveFileDialog1.Title = Properties.Resources.SaveLogMenuItem_ClickText4;
243 SaveFileDialog1.RestoreDirectory = true;
245 if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
247 if (!SaveFileDialog1.ValidateNames) return;
248 using (StreamWriter sw = new StreamWriter(SaveFileDialog1.FileName, false, Encoding.UTF8))
252 case DialogResult.Yes:
253 SaveEventLog(_filterdEventSource.ToList(), sw);
255 case DialogResult.No:
256 SaveEventLog(EventSource, sw);
264 this.TopMost = AppendSettingDialog.Instance.AlwaysTop;
267 private void SaveEventLog(List<Twitter.FormattedEvent> source, StreamWriter sw)
269 foreach (Twitter.FormattedEvent _event in source)
271 sw.WriteLine(_event.Eventtype.ToString() + "\t" +
272 "\"" + _event.CreatedAt.ToString() + "\"\t" +
273 _event.Event + "\t" +
274 _event.Username + "\t" +
275 _event.Target + "\t" +
276 _event.Id.ToString());
280 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
281 private static IEnumerable<TabPage> CreateTabsFromUserStreamsEvent()
283 return Enum.GetNames(typeof(MyCommon.EVENTTYPE))
284 .Where(e => e != "None" && e != "All")
285 .Select(e => new TabPage(e)
287 // Name = "TabPage" + e,
289 UseVisualStyleBackColor = true