OSDN Git Service

Merge pull request #240 from opentween/fix-playable-mark
[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 #nullable enable
23
24 using System;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.Data;
28 using System.Diagnostics;
29 using System.Drawing;
30 using System.IO;
31 using System.IO.Compression;
32 using System.Linq;
33 using System.Runtime.CompilerServices;
34 using System.Text;
35 using System.Threading.Tasks;
36 using System.Windows.Forms;
37 using OpenTween.Api.DataModel;
38
39 namespace OpenTween
40 {
41     public partial class SendErrorReportForm : OTBaseForm
42     {
43         public ErrorReport ErrorReport
44         {
45             get => this.errorReport;
46             set
47             {
48                 this.errorReport = value;
49                 this.bindingSource.DataSource = value;
50             }
51         }
52
53         private ErrorReport errorReport = null!;
54
55         public SendErrorReportForm()
56             => this.InitializeComponent();
57
58         private void SendErrorReportForm_Shown(object sender, EventArgs e)
59         {
60             this.pictureBoxIcon.Image = SystemIcons.Error.ToBitmap();
61             this.textBoxErrorReport.DeselectAll();
62         }
63
64         private void ButtonReset_Click(object sender, EventArgs e)
65             => this.ErrorReport.Reset();
66
67         private async void ButtonSendByMail_Click(object sender, EventArgs e)
68         {
69             this.DialogResult = DialogResult.OK;
70             await this.ErrorReport.SendByMailAsync();
71         }
72
73         private async void ButtonSendByDM_Click(object sender, EventArgs e)
74         {
75             this.DialogResult = DialogResult.OK;
76
77             try
78             {
79                 await this.ErrorReport.SendByDmAsync();
80
81                 MessageBox.Show(Properties.Resources.SendErrorReport_DmSendCompleted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
82             }
83             catch (WebApiException ex)
84             {
85                 var message = Properties.Resources.SendErrorReport_DmSendError + Environment.NewLine + "Err:" + ex.Message;
86                 MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
87             }
88         }
89
90         private void ButtonNotSend_Click(object sender, EventArgs e)
91             => this.DialogResult = DialogResult.Cancel;
92     }
93
94     public class ErrorReport : NotifyPropertyChangedBase
95     {
96         public string ReportText
97         {
98             get => this.reportText;
99             set
100             {
101                 this.SetProperty(ref this.reportText, value);
102                 this.UpdateEncodedReport();
103             }
104         }
105
106         private string reportText = "";
107
108         public bool AnonymousReport
109         {
110             get => this.anonymousReport;
111             set
112             {
113                 this.SetProperty(ref this.anonymousReport, value);
114                 this.UpdateEncodedReport();
115             }
116         }
117
118         private bool anonymousReport = true;
119
120         public bool CanSendByDM
121         {
122             get => this.canSendByDm;
123             private set => this.SetProperty(ref this.canSendByDm, value);
124         }
125
126         private bool canSendByDm;
127
128         public string EncodedReportForDM
129         {
130             get => this.encodedReportForDM;
131             private set => this.SetProperty(ref this.encodedReportForDM, value);
132         }
133
134         private string encodedReportForDM = "";
135
136         private readonly Twitter? tw;
137         private readonly string originalReportText;
138
139         public ErrorReport(string reportText)
140             : this(null, reportText)
141         {
142         }
143
144         public ErrorReport(Twitter? tw, string reportText)
145         {
146             this.tw = tw;
147             this.originalReportText = reportText;
148
149             this.Reset();
150         }
151
152         public void Reset()
153             => this.ReportText = this.originalReportText;
154
155         public async Task SendByMailAsync()
156         {
157             var toAddress = ApplicationSettings.FeedbackEmailAddress;
158             var subject = $"{ApplicationSettings.ApplicationName} {MyCommon.GetReadableVersion()} エラーログ";
159             var body = this.ReportText;
160
161             var mailto = $"mailto:{Uri.EscapeDataString(toAddress)}?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(body)}";
162             await Task.Run(() => Process.Start(mailto));
163         }
164
165         public async Task SendByDmAsync()
166         {
167             if (!this.CheckDmAvailable())
168                 return;
169
170             await this.tw!.SendDirectMessage(this.EncodedReportForDM);
171         }
172
173         private void UpdateEncodedReport()
174         {
175             if (!this.CheckDmAvailable())
176             {
177                 this.CanSendByDM = false;
178                 return;
179             }
180
181             var body = $"Anonymous: {this.AnonymousReport}" + Environment.NewLine + this.ReportText;
182             var originalBytes = Encoding.UTF8.GetBytes(body);
183
184             using (var outputStream = new MemoryStream())
185             {
186                 using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress, leaveOpen: true))
187                 {
188                     gzipStream.Write(originalBytes, 0, originalBytes.Length);
189                 }
190
191                 var encodedReport = Convert.ToBase64String(outputStream.ToArray());
192                 var destScreenName = ApplicationSettings.FeedbackTwitterName.Substring(1);
193                 this.EncodedReportForDM = $"D {destScreenName} ErrorReport: {encodedReport}";
194             }
195
196             this.CanSendByDM = this.tw!.GetTextLengthRemain(this.EncodedReportForDM) >= 0;
197         }
198
199         private bool CheckDmAvailable()
200         {
201             if (!ApplicationSettings.AllowSendErrorReportByDM)
202                 return false;
203
204             if (this.tw == null || !this.tw.AccessLevel.HasFlag(TwitterApiAccessLevel.DirectMessage))
205                 return false;
206
207             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
208                 return false;
209
210             return true;
211         }
212     }
213 }