OSDN Git Service

d8e6c5812d9031e1a4033b3f6f3ea94d0c1de154
[opentween/open-tween.git] / OpenTween / EventViewerDialog.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-2012 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.Diagnostics.CodeAnalysis;
32 using System.Drawing;
33 using System.Linq;
34 using System.Text;
35 using System.Windows.Forms;
36 using System.Text.RegularExpressions;
37 using System.IO;
38
39 namespace OpenTween
40 {
41     public partial class EventViewerDialog : OTBaseForm
42     {
43         public List<Twitter.FormattedEvent> EventSource { get; set; }
44
45         private Twitter.FormattedEvent[] _filterdEventSource;
46
47         private ListViewItem[] _ItemCache = null;
48         private int _itemCacheIndex;
49
50         private TabPage _curTab = null;
51
52         public EventViewerDialog()
53         {
54             InitializeComponent();
55
56             // メイリオフォント指定時にタブの最小幅が広くなる問題の対策
57             this.TabEventType.HandleCreated += (s, e) => NativeMethods.SetMinTabWidth((TabControl)s, 40);
58         }
59
60         private void OK_Button_Click(object sender, EventArgs e)
61         {
62             this.DialogResult = DialogResult.OK;
63             this.Close();
64         }
65
66         private void Cancel_Button_Click(object sender, EventArgs e)
67         {
68             this.DialogResult = DialogResult.Cancel;
69             this.Close();
70         }
71
72         private ListViewItem CreateListViewItem(Twitter.FormattedEvent source)
73         {
74             string[] s = { source.CreatedAt.ToString(), source.Event.ToUpper(), source.Username, source.Target };
75             return new ListViewItem(s);
76         }
77
78         private void EventViewerDialog_Shown(object sender, EventArgs e)
79         {
80             // タブ初期化
81             foreach (var tabPage in CreateTabsFromUserStreamsEvent())
82             {
83                 TabEventType.TabPages.Add(tabPage);
84             }
85
86             EventList.BeginUpdate();
87             _curTab = TabEventType.SelectedTab;
88             CreateFilterdEventSource();
89             EventList.EndUpdate();
90             this.TopMost = AppendSettingDialog.Instance.AlwaysTop;
91         }
92
93         private void EventList_DoubleClick(object sender, EventArgs e)
94         {
95             if (EventList.SelectedIndices.Count != 0 && _filterdEventSource[EventList.SelectedIndices[0]] != null)
96             {
97                 ((TweenMain)this.Owner).OpenUriAsync("https://twitter.com/" + _filterdEventSource[EventList.SelectedIndices[0]].Username);
98             }
99         }
100
101         private MyCommon.EVENTTYPE ParseEventTypeFromTag()
102         {
103             return (MyCommon.EVENTTYPE)Enum.Parse(typeof(MyCommon.EVENTTYPE), _curTab.Tag.ToString());
104         }
105
106         private bool IsFilterMatch(Twitter.FormattedEvent x)
107         {
108             if (!CheckBoxFilter.Checked || string.IsNullOrEmpty(TextBoxKeyword.Text))
109             {
110                 return true;
111             }
112             else
113             {
114                 if (CheckRegex.Checked)
115                 {
116                     try
117                     {
118                         Regex rx = new Regex(TextBoxKeyword.Text);
119                         return rx.Match(x.Username).Success || rx.Match(x.Target).Success;
120                     }
121                     catch (Exception ex)
122                     {
123                         MessageBox.Show(Properties.Resources.ButtonOK_ClickText3 + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
124                         return false;
125                     }
126                 }
127                 else
128                 {
129                     return x.Username.Contains(TextBoxKeyword.Text) || x.Target.Contains(TextBoxKeyword.Text);
130                 }
131             }
132         }
133
134         private void CreateFilterdEventSource()
135         {
136             if (EventSource != null && EventSource.Count > 0)
137             {
138                 _filterdEventSource = EventSource.FindAll((x) => (CheckExcludeMyEvent.Checked ? !x.IsMe : true) &&
139                                                                  (x.Eventtype & ParseEventTypeFromTag()) != 0 &&
140                                                                  IsFilterMatch(x)).ToArray();
141                 _ItemCache = null;
142                 EventList.VirtualListSize = _filterdEventSource.Count();
143                 StatusLabelCount.Text = string.Format("{0} / {1}", _filterdEventSource.Count(), EventSource.Count());
144             }
145             else
146             {
147                 StatusLabelCount.Text = "0 / 0";
148             }
149         }
150
151         private void CheckExcludeMyEvent_CheckedChanged(object sender, EventArgs e)
152         {
153             CreateFilterdEventSource();
154         }
155
156         private void ButtonRefresh_Click(object sender, EventArgs e)
157         {
158             CreateFilterdEventSource();
159         }
160
161         private void TabEventType_SelectedIndexChanged(object sender, EventArgs e)
162         {
163             CreateFilterdEventSource();
164         }
165
166         private void TabEventType_Selecting(object sender, TabControlCancelEventArgs e)
167         {
168             _curTab = e.TabPage;
169             if (!e.TabPage.Controls.Contains(EventList))
170             {
171                 e.TabPage.Controls.Add(EventList);
172             }
173         }
174
175         private void TextBoxKeyword_KeyPress(object sender, KeyPressEventArgs e)
176         {
177             if (e.KeyChar == (char)Keys.Enter)
178             {
179                 CreateFilterdEventSource();
180                 e.Handled = true;
181             }
182         }
183
184         private void EventList_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
185         {
186             if (_ItemCache != null && e.ItemIndex >= _itemCacheIndex && e.ItemIndex < _itemCacheIndex + _ItemCache.Length)
187             {
188                 //キャッシュヒット
189                 e.Item = _ItemCache[e.ItemIndex - _itemCacheIndex];
190             }
191             else
192             {
193                 //キャッシュミス
194                 e.Item = CreateListViewItem(_filterdEventSource[e.ItemIndex]);
195             }
196         }
197
198         private void EventList_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
199         {
200             CreateCache(e.StartIndex, e.EndIndex);
201         }
202
203         private void CreateCache(int StartIndex, int EndIndex)
204         {
205             //キャッシュ要求(要求範囲±30を作成)
206             StartIndex -= 30;
207             if (StartIndex < 0) StartIndex = 0;
208             EndIndex += 30;
209             if (EndIndex > _filterdEventSource.Count() - 1)
210             {
211                 EndIndex = _filterdEventSource.Count() - 1;
212             }
213             _ItemCache = new ListViewItem[] { };
214             Array.Resize(ref _ItemCache, EndIndex - StartIndex + 1);
215             _itemCacheIndex = StartIndex;
216             for (int i = 0; i < _ItemCache.Length; i++)
217             {
218                 _ItemCache[i] = CreateListViewItem(_filterdEventSource[StartIndex + i]);
219             }
220         }
221
222         private void SaveLogButton_Click(object sender, EventArgs e)
223         {
224             DialogResult rslt = MessageBox.Show(string.Format(Properties.Resources.SaveLogMenuItem_ClickText5, Environment.NewLine),
225                     Properties.Resources.SaveLogMenuItem_ClickText2,
226                     MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
227             switch (rslt)
228             {
229                 case DialogResult.Yes:
230                     SaveFileDialog1.FileName = MyCommon.GetAssemblyName() + "Events" + _curTab.Tag.ToString() + DateTime.Now.ToString("yyMMdd-HHmmss") + ".tsv";
231                     break;
232                 case DialogResult.No:
233                     SaveFileDialog1.FileName = MyCommon.GetAssemblyName() + "Events" + DateTime.Now.ToString("yyMMdd-HHmmss") + ".tsv";
234                     break;
235                 default:
236                     return;
237             }
238
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;
244
245             if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
246             {
247                 if (!SaveFileDialog1.ValidateNames) return;
248                 using (StreamWriter sw = new StreamWriter(SaveFileDialog1.FileName, false, Encoding.UTF8))
249                 {
250                     switch (rslt)
251                     {
252                         case DialogResult.Yes:
253                             SaveEventLog(_filterdEventSource.ToList(), sw);
254                             break;
255                         case DialogResult.No:
256                             SaveEventLog(EventSource, sw);
257                             break;
258                         default:
259                             //
260                             break;
261                     }
262                 }
263             }
264             this.TopMost = AppendSettingDialog.Instance.AlwaysTop;
265         }
266
267         private void SaveEventLog(List<Twitter.FormattedEvent> source, StreamWriter sw)
268         {
269             foreach (Twitter.FormattedEvent _event in source)
270             {
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());
277             }
278         }
279
280         [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
281         private static IEnumerable<TabPage> CreateTabsFromUserStreamsEvent()
282         {
283             return Enum.GetNames(typeof(MyCommon.EVENTTYPE))
284                        .Where(e => e != "None" && e != "All")
285                        .Select(e => new TabPage(e)
286                        {
287                            // Name = "TabPage" + e,
288                            Tag = e,
289                            UseVisualStyleBackColor = true
290                        });
291         }
292     }
293 }