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;
38 using System.Globalization;
39 using OpenTween.Setting;
43 public partial class EventViewerDialog : OTBaseForm
45 public List<Twitter.FormattedEvent> EventSource { get; set; }
47 private Twitter.FormattedEvent[] _filterdEventSource;
49 private ListViewItem[] _ItemCache = null;
50 private int _itemCacheIndex;
52 private TabPage _curTab = null;
54 public EventViewerDialog()
56 InitializeComponent();
58 // メイリオフォント指定時にタブの最小幅が広くなる問題の対策
59 this.TabEventType.HandleCreated += (s, e) => NativeMethods.SetMinTabWidth((TabControl)s, 40);
62 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
64 base.ScaleControl(factor, specified);
65 ScaleChildControl(this.EventList, factor);
68 private void OK_Button_Click(object sender, EventArgs e)
70 this.DialogResult = DialogResult.OK;
74 private void Cancel_Button_Click(object sender, EventArgs e)
76 this.DialogResult = DialogResult.Cancel;
80 private ListViewItem CreateListViewItem(Twitter.FormattedEvent source)
82 string[] s = { source.CreatedAt.ToString(), source.Event.ToUpper(CultureInfo.CurrentCulture), source.Username, source.Target };
83 return new ListViewItem(s);
86 private void EventViewerDialog_Shown(object sender, EventArgs e)
89 foreach (var tabPage in CreateTabsFromUserStreamsEvent())
91 TabEventType.TabPages.Add(tabPage);
94 EventList.BeginUpdate();
95 _curTab = TabEventType.SelectedTab;
96 CreateFilterdEventSource();
97 EventList.EndUpdate();
98 this.TopMost = SettingManager.Common.AlwaysTop;
101 private async void EventList_DoubleClick(object sender, EventArgs e)
103 if (EventList.SelectedIndices.Count != 0 && _filterdEventSource[EventList.SelectedIndices[0]] != null)
105 await ((TweenMain)this.Owner).OpenUriInBrowserAsync("https://twitter.com/" + _filterdEventSource[EventList.SelectedIndices[0]].Username);
109 private MyCommon.EVENTTYPE ParseEventTypeFromTag()
111 return (MyCommon.EVENTTYPE)Enum.Parse(typeof(MyCommon.EVENTTYPE), _curTab.Tag.ToString());
114 private bool IsFilterMatch(Twitter.FormattedEvent x)
116 if (!CheckBoxFilter.Checked || string.IsNullOrEmpty(TextBoxKeyword.Text))
122 if (CheckRegex.Checked)
126 Regex rx = new Regex(TextBoxKeyword.Text);
127 return rx.Match(x.Username).Success || rx.Match(x.Target).Success;
131 MessageBox.Show(Properties.Resources.ButtonOK_ClickText3 + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
137 return x.Username.Contains(TextBoxKeyword.Text) || x.Target.Contains(TextBoxKeyword.Text);
142 private void CreateFilterdEventSource()
144 if (EventSource != null && EventSource.Count > 0)
146 _filterdEventSource = EventSource.FindAll((x) => !(CheckExcludeMyEvent.Checked && x.IsMe) &&
147 (x.Eventtype & ParseEventTypeFromTag()) != 0 &&
148 IsFilterMatch(x)).ToArray();
150 EventList.VirtualListSize = _filterdEventSource.Count();
151 StatusLabelCount.Text = string.Format("{0} / {1}", _filterdEventSource.Count(), EventSource.Count());
155 StatusLabelCount.Text = "0 / 0";
159 private void CheckExcludeMyEvent_CheckedChanged(object sender, EventArgs e)
161 CreateFilterdEventSource();
164 private void ButtonRefresh_Click(object sender, EventArgs e)
166 CreateFilterdEventSource();
169 private void TabEventType_SelectedIndexChanged(object sender, EventArgs e)
171 CreateFilterdEventSource();
174 private void TabEventType_Selecting(object sender, TabControlCancelEventArgs e)
177 if (!e.TabPage.Controls.Contains(EventList))
179 e.TabPage.Controls.Add(EventList);
183 private void TextBoxKeyword_KeyPress(object sender, KeyPressEventArgs e)
185 if (e.KeyChar == (char)Keys.Enter)
187 CreateFilterdEventSource();
192 private void EventList_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
194 if (_ItemCache != null && e.ItemIndex >= _itemCacheIndex && e.ItemIndex < _itemCacheIndex + _ItemCache.Length)
197 e.Item = _ItemCache[e.ItemIndex - _itemCacheIndex];
202 e.Item = CreateListViewItem(_filterdEventSource[e.ItemIndex]);
206 private void EventList_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
208 CreateCache(e.StartIndex, e.EndIndex);
211 private void CreateCache(int StartIndex, int EndIndex)
213 //キャッシュ要求(要求範囲±30を作成)
215 if (StartIndex < 0) StartIndex = 0;
217 if (EndIndex > _filterdEventSource.Count() - 1)
219 EndIndex = _filterdEventSource.Count() - 1;
221 _ItemCache = new ListViewItem[] { };
222 Array.Resize(ref _ItemCache, EndIndex - StartIndex + 1);
223 _itemCacheIndex = StartIndex;
224 for (int i = 0; i < _ItemCache.Length; i++)
226 _ItemCache[i] = CreateListViewItem(_filterdEventSource[StartIndex + i]);
230 private void SaveLogButton_Click(object sender, EventArgs e)
232 DialogResult rslt = MessageBox.Show(string.Format(Properties.Resources.SaveLogMenuItem_ClickText5, Environment.NewLine),
233 Properties.Resources.SaveLogMenuItem_ClickText2,
234 MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
237 case DialogResult.Yes:
238 SaveFileDialog1.FileName = MyCommon.GetAssemblyName() + "Events" + _curTab.Tag + DateTime.Now.ToString("yyMMdd-HHmmss") + ".tsv";
240 case DialogResult.No:
241 SaveFileDialog1.FileName = MyCommon.GetAssemblyName() + "Events" + DateTime.Now.ToString("yyMMdd-HHmmss") + ".tsv";
247 SaveFileDialog1.InitialDirectory = Application.StartupPath;
248 SaveFileDialog1.Filter = Properties.Resources.SaveLogMenuItem_ClickText3;
249 SaveFileDialog1.FilterIndex = 0;
250 SaveFileDialog1.Title = Properties.Resources.SaveLogMenuItem_ClickText4;
251 SaveFileDialog1.RestoreDirectory = true;
253 if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
255 if (!SaveFileDialog1.ValidateNames) return;
256 using (StreamWriter sw = new StreamWriter(SaveFileDialog1.FileName, false, Encoding.UTF8))
260 case DialogResult.Yes:
261 SaveEventLog(_filterdEventSource.ToList(), sw);
263 case DialogResult.No:
264 SaveEventLog(EventSource, sw);
272 this.TopMost = SettingManager.Common.AlwaysTop;
275 private void SaveEventLog(List<Twitter.FormattedEvent> source, StreamWriter sw)
277 foreach (Twitter.FormattedEvent _event in source)
279 sw.WriteLine(_event.Eventtype + "\t" +
280 "\"" + _event.CreatedAt + "\"\t" +
281 _event.Event + "\t" +
282 _event.Username + "\t" +
283 _event.Target + "\t" +
288 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
289 private static IEnumerable<TabPage> CreateTabsFromUserStreamsEvent()
291 return Enum.GetNames(typeof(MyCommon.EVENTTYPE))
292 .Where(e => e != "None" && e != "All")
293 .Select(e => new TabPage(e)
295 // Name = "TabPage" + e,
297 UseVisualStyleBackColor = true,
298 AccessibleRole = AccessibleRole.PageTab,