OSDN Git Service

5cda03ac5c1568cc148cc6b0f63fb531c777526e
[opentween/open-tween.git] / OpenTween / SendErrorReportForm.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.ComponentModel;
25 using System.Data;
26 using System.Diagnostics;
27 using System.Drawing;
28 using System.IO;
29 using System.IO.Compression;
30 using System.Linq;
31 using System.Runtime.CompilerServices;
32 using System.Text;
33 using System.Threading.Tasks;
34 using System.Windows.Forms;
35 using OpenTween.Api;
36
37 namespace OpenTween
38 {
39     public partial class SendErrorReportForm : Form
40     {
41         public ErrorReport ErrorReport
42         {
43             get { return this._errorReport; }
44             set
45             {
46                 this._errorReport = value;
47                 this.bindingSource.DataSource = value;
48             }
49         }
50         private ErrorReport _errorReport;
51
52         public SendErrorReportForm()
53         {
54             this.InitializeComponent();
55         }
56
57         private void SendErrorReportForm_Shown(object sender, EventArgs e)
58         {
59             this.pictureBoxIcon.Image = SystemIcons.Error.ToBitmap();
60             this.textBoxErrorReport.DeselectAll();
61         }
62
63         private void buttonReset_Click(object sender, EventArgs e)
64         {
65             this.ErrorReport.Reset();
66         }
67
68         private async void buttonSendByMail_Click(object sender, EventArgs e)
69         {
70             this.DialogResult = DialogResult.OK;
71             await this.ErrorReport.SendByMailAsync();
72         }
73
74         private async void buttonSendByDM_Click(object sender, EventArgs e)
75         {
76             this.DialogResult = DialogResult.OK;
77
78             try
79             {
80                 await this.ErrorReport.SendByDmAsync();
81
82                 MessageBox.Show(Properties.Resources.SendErrorReport_DmSendCompleted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
83             }
84             catch (WebApiException ex)
85             {
86                 var message = Properties.Resources.SendErrorReport_DmSendError + Environment.NewLine + ex.Message;
87                 MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
88             }
89         }
90
91         private void buttonNotSend_Click(object sender, EventArgs e)
92         {
93             this.DialogResult = DialogResult.Cancel;
94         }
95     }
96
97     public class ErrorReport : INotifyPropertyChanged
98     {
99         public string ReportText
100         {
101             get { return this._reportText; }
102             set
103             {
104                 this.SetProperty(ref this._reportText, value);
105                 this.UpdateEncodedReport();
106             }
107         }
108         private string _reportText;
109
110         public bool CanSendByDM
111         {
112             get { return this._canSendByDm; }
113             private set { this.SetProperty(ref this._canSendByDm, value); }
114         }
115         private bool _canSendByDm;
116
117         public string EncodedReportForDM
118         {
119             get { return this._encodedReportForDM; }
120             private set { this.SetProperty(ref this._encodedReportForDM, value); }
121         }
122         private string _encodedReportForDM;
123
124         private readonly Twitter tw;
125         private readonly string originalReportText;
126
127         public event PropertyChangedEventHandler PropertyChanged;
128
129         public ErrorReport(string reportText)
130             : this(null, reportText)
131         {
132         }
133
134         public ErrorReport(Twitter tw, string reportText)
135         {
136             this.tw = tw;
137             this.originalReportText = reportText;
138
139             this.Reset();
140         }
141
142         public void Reset()
143         {
144             this.ReportText = this.originalReportText;
145         }
146
147         public async Task SendByMailAsync()
148         {
149             var toAddress = ApplicationSettings.FeedbackEmailAddress;
150             var subject = $"{Application.ProductName} {MyCommon.GetReadableVersion()} エラーログ";
151             var body = this.ReportText;
152
153             var mailto = $"mailto:{toAddress}?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(body)}";
154             await Task.Run(() => Process.Start(mailto));
155         }
156
157         public async Task SendByDmAsync()
158         {
159             await Task.Run(() => this.tw.SendDirectMessage(this.EncodedReportForDM));
160         }
161
162         private void UpdateEncodedReport()
163         {
164             if (!this.CheckDmAvailable())
165             {
166                 this.CanSendByDM = false;
167                 return;
168             }
169
170             var originalBytes = Encoding.UTF8.GetBytes(this.ReportText);
171
172             using (var outputStream = new MemoryStream())
173             {
174                 using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress, leaveOpen: true))
175                 {
176                     gzipStream.Write(originalBytes, 0, originalBytes.Length);
177                 }
178
179                 var encodedReport = Convert.ToBase64String(outputStream.ToArray());
180                 var destScreenName = ApplicationSettings.FeedbackTwitterName.Substring(1);
181                 this.EncodedReportForDM = $"D {destScreenName} ErrorReport: {encodedReport}";
182             }
183
184             this.CanSendByDM = this.tw.GetTextLengthRemain(this.EncodedReportForDM) >= 0;
185         }
186
187         private bool CheckDmAvailable()
188         {
189             if (!ApplicationSettings.AllowSendErrorReportByDM)
190                 return false;
191
192             if (this.tw == null || !this.tw.AccessLevel.HasFlag(TwitterApiAccessLevel.DirectMessage))
193                 return false;
194
195             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
196                 return false;
197
198             return true;
199         }
200
201         protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
202         {
203             if (EqualityComparer<T>.Default.Equals(field, value))
204                 return;
205
206             field = value;
207             this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
208         }
209
210         protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
211         {
212             this.PropertyChanged?.Invoke(this, e);
213         }
214     }
215 }