1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
24 using System.ComponentModel;
26 using System.Diagnostics;
29 using System.IO.Compression;
31 using System.Runtime.CompilerServices;
33 using System.Threading.Tasks;
34 using System.Windows.Forms;
35 using OpenTween.Api.DataModel;
39 public partial class SendErrorReportForm : OTBaseForm
41 public ErrorReport ErrorReport
43 get { return this._errorReport; }
46 this._errorReport = value;
47 this.bindingSource.DataSource = value;
50 private ErrorReport _errorReport;
52 public SendErrorReportForm()
54 this.InitializeComponent();
57 private void SendErrorReportForm_Shown(object sender, EventArgs e)
59 this.pictureBoxIcon.Image = SystemIcons.Error.ToBitmap();
60 this.textBoxErrorReport.DeselectAll();
63 private void buttonReset_Click(object sender, EventArgs e)
65 this.ErrorReport.Reset();
68 private async void buttonSendByMail_Click(object sender, EventArgs e)
70 this.DialogResult = DialogResult.OK;
71 await this.ErrorReport.SendByMailAsync();
74 private async void buttonSendByDM_Click(object sender, EventArgs e)
76 this.DialogResult = DialogResult.OK;
80 await this.ErrorReport.SendByDmAsync();
82 MessageBox.Show(Properties.Resources.SendErrorReport_DmSendCompleted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
84 catch (WebApiException ex)
86 var message = Properties.Resources.SendErrorReport_DmSendError + Environment.NewLine + "Err:" + ex.Message;
87 MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
91 private void buttonNotSend_Click(object sender, EventArgs e)
93 this.DialogResult = DialogResult.Cancel;
97 public class ErrorReport : NotifyPropertyChangedBase
99 public string ReportText
101 get { return this._reportText; }
104 this.SetProperty(ref this._reportText, value);
105 this.UpdateEncodedReport();
108 private string _reportText;
110 public bool AnonymousReport
112 get { return this._anonymousReport; }
115 this.SetProperty(ref this._anonymousReport, value);
116 this.UpdateEncodedReport();
119 private bool _anonymousReport = true;
121 public bool CanSendByDM
123 get => this._canSendByDm;
124 private set => this.SetProperty(ref this._canSendByDm, value);
126 private bool _canSendByDm;
128 public string EncodedReportForDM
130 get => this._encodedReportForDM;
131 private set => this.SetProperty(ref this._encodedReportForDM, value);
133 private string _encodedReportForDM;
135 private readonly Twitter tw;
136 private readonly string originalReportText;
138 public ErrorReport(string reportText)
139 : this(null, reportText)
143 public ErrorReport(Twitter tw, string reportText)
146 this.originalReportText = reportText;
153 this.ReportText = this.originalReportText;
156 public async Task SendByMailAsync()
158 var toAddress = ApplicationSettings.FeedbackEmailAddress;
159 var subject = $"{Application.ProductName} {MyCommon.GetReadableVersion()} エラーログ";
160 var body = this.ReportText;
162 var mailto = $"mailto:{Uri.EscapeDataString(toAddress)}?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(body)}";
163 await Task.Run(() => Process.Start(mailto));
166 public async Task SendByDmAsync()
168 await this.tw.SendDirectMessage(this.EncodedReportForDM);
171 private void UpdateEncodedReport()
173 if (!this.CheckDmAvailable())
175 this.CanSendByDM = false;
179 var body = $"Anonymous: {this.AnonymousReport}" + Environment.NewLine + this.ReportText;
180 var originalBytes = Encoding.UTF8.GetBytes(body);
182 using (var outputStream = new MemoryStream())
184 using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress, leaveOpen: true))
186 gzipStream.Write(originalBytes, 0, originalBytes.Length);
189 var encodedReport = Convert.ToBase64String(outputStream.ToArray());
190 var destScreenName = ApplicationSettings.FeedbackTwitterName.Substring(1);
191 this.EncodedReportForDM = $"D {destScreenName} ErrorReport: {encodedReport}";
194 this.CanSendByDM = this.tw.GetTextLengthRemain(this.EncodedReportForDM) >= 0;
197 private bool CheckDmAvailable()
199 if (!ApplicationSettings.AllowSendErrorReportByDM)
202 if (this.tw == null || !this.tw.AccessLevel.HasFlag(TwitterApiAccessLevel.DirectMessage))
205 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)