OSDN Git Service

OpenTween v2.4.2 リリース
[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         private ErrorReport _errorReport = null!;
53
54         public SendErrorReportForm()
55             => this.InitializeComponent();
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             => this.ErrorReport.Reset();
65
66         private async void buttonSendByMail_Click(object sender, EventArgs e)
67         {
68             this.DialogResult = DialogResult.OK;
69             await this.ErrorReport.SendByMailAsync();
70         }
71
72         private async void buttonSendByDM_Click(object sender, EventArgs e)
73         {
74             this.DialogResult = DialogResult.OK;
75
76             try
77             {
78                 await this.ErrorReport.SendByDmAsync();
79
80                 MessageBox.Show(Properties.Resources.SendErrorReport_DmSendCompleted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
81             }
82             catch (WebApiException ex)
83             {
84                 var message = Properties.Resources.SendErrorReport_DmSendError + Environment.NewLine + "Err:" + ex.Message;
85                 MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
86             }
87         }
88
89         private void buttonNotSend_Click(object sender, EventArgs e)
90             => this.DialogResult = DialogResult.Cancel;
91     }
92
93     public class ErrorReport : NotifyPropertyChangedBase
94     {
95         public string ReportText
96         {
97             get => this._reportText;
98             set
99             {
100                 this.SetProperty(ref this._reportText, value);
101                 this.UpdateEncodedReport();
102             }
103         }
104         private string _reportText = "";
105
106         public bool AnonymousReport
107         {
108             get => this._anonymousReport;
109             set
110             {
111                 this.SetProperty(ref this._anonymousReport, value);
112                 this.UpdateEncodedReport();
113             }
114         }
115         private bool _anonymousReport = true;
116
117         public bool CanSendByDM
118         {
119             get => this._canSendByDm;
120             private set => this.SetProperty(ref this._canSendByDm, value);
121         }
122         private bool _canSendByDm;
123
124         public string EncodedReportForDM
125         {
126             get => this._encodedReportForDM;
127             private set => this.SetProperty(ref this._encodedReportForDM, value);
128         }
129         private string _encodedReportForDM = "";
130
131         private readonly Twitter? tw;
132         private readonly string originalReportText;
133
134         public ErrorReport(string reportText)
135             : this(null, reportText)
136         {
137         }
138
139         public ErrorReport(Twitter? tw, string reportText)
140         {
141             this.tw = tw;
142             this.originalReportText = reportText;
143
144             this.Reset();
145         }
146
147         public void Reset()
148             => this.ReportText = this.originalReportText;
149
150         public async Task SendByMailAsync()
151         {
152             var toAddress = ApplicationSettings.FeedbackEmailAddress;
153             var subject = $"{ApplicationSettings.ApplicationName} {MyCommon.GetReadableVersion()} エラーログ";
154             var body = this.ReportText;
155
156             var mailto = $"mailto:{Uri.EscapeDataString(toAddress)}?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(body)}";
157             await Task.Run(() => Process.Start(mailto));
158         }
159
160         public async Task SendByDmAsync()
161         {
162             if (!this.CheckDmAvailable())
163                 return;
164
165             await this.tw!.SendDirectMessage(this.EncodedReportForDM);
166         }
167
168         private void UpdateEncodedReport()
169         {
170             if (!this.CheckDmAvailable())
171             {
172                 this.CanSendByDM = false;
173                 return;
174             }
175
176             var body = $"Anonymous: {this.AnonymousReport}" + Environment.NewLine + this.ReportText;
177             var originalBytes = Encoding.UTF8.GetBytes(body);
178
179             using (var outputStream = new MemoryStream())
180             {
181                 using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress, leaveOpen: true))
182                 {
183                     gzipStream.Write(originalBytes, 0, originalBytes.Length);
184                 }
185
186                 var encodedReport = Convert.ToBase64String(outputStream.ToArray());
187                 var destScreenName = ApplicationSettings.FeedbackTwitterName.Substring(1);
188                 this.EncodedReportForDM = $"D {destScreenName} ErrorReport: {encodedReport}";
189             }
190
191             this.CanSendByDM = this.tw!.GetTextLengthRemain(this.EncodedReportForDM) >= 0;
192         }
193
194         private bool CheckDmAvailable()
195         {
196             if (!ApplicationSettings.AllowSendErrorReportByDM)
197                 return false;
198
199             if (this.tw == null || !this.tw.AccessLevel.HasFlag(TwitterApiAccessLevel.DirectMessage))
200                 return false;
201
202             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
203                 return false;
204
205             return true;
206         }
207     }
208 }