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;
40 using System.Threading.Tasks;
44 public partial class EventViewerDialog : OTBaseForm
46 public List<Twitter.FormattedEvent> EventSource { get; set; }
48 private Twitter.FormattedEvent[] _filterdEventSource;
50 private ListViewItem[] _ItemCache = null;
51 private int _itemCacheIndex;
53 private TabPage _curTab = null;
55 public EventViewerDialog()
57 InitializeComponent();
59 // メイリオフォント指定時にタブの最小幅が広くなる問題の対策
60 this.TabEventType.HandleCreated += (s, e) => NativeMethods.SetMinTabWidth((TabControl)s, 40);
63 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
65 base.ScaleControl(factor, specified);
66 ScaleChildControl(this.EventList, factor);
69 private void OK_Button_Click(object sender, EventArgs e)
71 this.DialogResult = DialogResult.OK;
75 private void Cancel_Button_Click(object sender, EventArgs e)
77 this.DialogResult = DialogResult.Cancel;
81 private ListViewItem CreateListViewItem(Twitter.FormattedEvent source)
83 string[] s = { source.CreatedAt.ToLocalTimeString(), source.Event.ToUpper(CultureInfo.CurrentCulture), source.Username, source.Target };
84 return new ListViewItem(s);
87 private void EventViewerDialog_Shown(object sender, EventArgs e)
90 foreach (var tabPage in CreateTabsFromUserStreamsEvent())
92 TabEventType.TabPages.Add(tabPage);
95 EventList.BeginUpdate();
96 _curTab = TabEventType.SelectedTab;
97 CreateFilterdEventSource();
98 EventList.EndUpdate();
99 this.TopMost = SettingManager.Common.AlwaysTop;
102 private async void EventList_DoubleClick(object sender, EventArgs e)
103 => await this.OpenEventStatusOrUser();
105 private async void EventList_KeyDown(object sender, KeyEventArgs e)
107 if (e.KeyData == Keys.Enter)
108 await this.OpenEventStatusOrUser();
111 private async Task OpenEventStatusOrUser()
113 if (this.EventList.SelectedIndices.Count == 0)
116 var tweenMain = (TweenMain)this.Owner;
117 var selectedEvent = this._filterdEventSource[this.EventList.SelectedIndices[0]];
118 if (selectedEvent != null)
120 if (selectedEvent.Id != 0)
121 await tweenMain.OpenRelatedTab(selectedEvent.Id);
123 await tweenMain.OpenUriAsync(new Uri("https://twitter.com/" + selectedEvent.Username));
127 private MyCommon.EVENTTYPE ParseEventTypeFromTag()
129 return (MyCommon.EVENTTYPE)Enum.Parse(typeof(MyCommon.EVENTTYPE), _curTab.Tag.ToString());
132 private bool IsFilterMatch(Twitter.FormattedEvent x)
134 if (!CheckBoxFilter.Checked || string.IsNullOrEmpty(TextBoxKeyword.Text))
140 if (CheckRegex.Checked)
144 Regex rx = new Regex(TextBoxKeyword.Text);
145 return rx.Match(x.Username).Success || rx.Match(x.Target).Success;
149 MessageBox.Show(Properties.Resources.ButtonOK_ClickText3 + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
155 return x.Username.Contains(TextBoxKeyword.Text) || x.Target.Contains(TextBoxKeyword.Text);
160 private void CreateFilterdEventSource()
162 if (EventSource != null && EventSource.Count > 0)
164 _filterdEventSource = EventSource.FindAll((x) => !(CheckExcludeMyEvent.Checked && x.IsMe) &&
165 (x.Eventtype & ParseEventTypeFromTag()) != 0 &&
166 IsFilterMatch(x)).ToArray();
168 EventList.VirtualListSize = _filterdEventSource.Count();
169 StatusLabelCount.Text = string.Format("{0} / {1}", _filterdEventSource.Count(), EventSource.Count());
173 StatusLabelCount.Text = "0 / 0";
177 private void CheckExcludeMyEvent_CheckedChanged(object sender, EventArgs e)
179 CreateFilterdEventSource();
182 private void ButtonRefresh_Click(object sender, EventArgs e)
184 CreateFilterdEventSource();
187 private void TabEventType_SelectedIndexChanged(object sender, EventArgs e)
189 CreateFilterdEventSource();
192 private void TabEventType_Selecting(object sender, TabControlCancelEventArgs e)
195 if (!e.TabPage.Controls.Contains(EventList))
197 e.TabPage.Controls.Add(EventList);
201 private void TextBoxKeyword_KeyPress(object sender, KeyPressEventArgs e)
203 if (e.KeyChar == (char)Keys.Enter)
205 CreateFilterdEventSource();
210 private void EventList_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
212 if (_ItemCache != null && e.ItemIndex >= _itemCacheIndex && e.ItemIndex < _itemCacheIndex + _ItemCache.Length)
215 e.Item = _ItemCache[e.ItemIndex - _itemCacheIndex];
220 e.Item = CreateListViewItem(_filterdEventSource[e.ItemIndex]);
224 private void EventList_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
226 CreateCache(e.StartIndex, e.EndIndex);
229 private void CreateCache(int StartIndex, int EndIndex)
231 //キャッシュ要求(要求範囲±30を作成)
233 if (StartIndex < 0) StartIndex = 0;
235 if (EndIndex > _filterdEventSource.Count() - 1)
237 EndIndex = _filterdEventSource.Count() - 1;
239 _ItemCache = new ListViewItem[] { };
240 Array.Resize(ref _ItemCache, EndIndex - StartIndex + 1);
241 _itemCacheIndex = StartIndex;
242 for (int i = 0; i < _ItemCache.Length; i++)
244 _ItemCache[i] = CreateListViewItem(_filterdEventSource[StartIndex + i]);
248 private void SaveLogButton_Click(object sender, EventArgs e)
250 DialogResult rslt = MessageBox.Show(string.Format(Properties.Resources.SaveLogMenuItem_ClickText5, Environment.NewLine),
251 Properties.Resources.SaveLogMenuItem_ClickText2,
252 MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
255 case DialogResult.Yes:
256 SaveFileDialog1.FileName = $"{MyCommon.GetAssemblyName()}Events{_curTab.Tag}{DateTimeUtc.Now.ToLocalTime():yyMMdd-HHmmss}.tsv";
258 case DialogResult.No:
259 SaveFileDialog1.FileName = $"{MyCommon.GetAssemblyName()}Events{DateTimeUtc.Now.ToLocalTime():yyMMdd-HHmmss}.tsv";
265 SaveFileDialog1.InitialDirectory = Application.StartupPath;
266 SaveFileDialog1.Filter = Properties.Resources.SaveLogMenuItem_ClickText3;
267 SaveFileDialog1.FilterIndex = 0;
268 SaveFileDialog1.Title = Properties.Resources.SaveLogMenuItem_ClickText4;
269 SaveFileDialog1.RestoreDirectory = true;
271 if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
273 if (!SaveFileDialog1.ValidateNames) return;
274 using (StreamWriter sw = new StreamWriter(SaveFileDialog1.FileName, false, Encoding.UTF8))
278 case DialogResult.Yes:
279 SaveEventLog(_filterdEventSource.ToList(), sw);
281 case DialogResult.No:
282 SaveEventLog(EventSource, sw);
290 this.TopMost = SettingManager.Common.AlwaysTop;
293 private void SaveEventLog(List<Twitter.FormattedEvent> source, StreamWriter sw)
295 foreach (Twitter.FormattedEvent _event in source)
297 sw.WriteLine(_event.Eventtype + "\t" +
298 "\"" + _event.CreatedAt.ToLocalTimeString() + "\"\t" +
299 _event.Event + "\t" +
300 _event.Username + "\t" +
301 _event.Target + "\t" +
306 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
307 private static IEnumerable<TabPage> CreateTabsFromUserStreamsEvent()
309 return Enum.GetNames(typeof(MyCommon.EVENTTYPE))
310 .Where(e => e != "None" && e != "All")
311 .Select(e => new TabPage(e)
313 // Name = "TabPage" + e,
315 UseVisualStyleBackColor = true,
316 AccessibleRole = AccessibleRole.PageTab,