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.
25 using System.Collections.Generic;
26 using System.ComponentModel;
28 using System.Diagnostics;
31 using System.IO.Compression;
33 using System.Runtime.CompilerServices;
35 using System.Threading.Tasks;
36 using System.Windows.Forms;
37 using OpenTween.Api.DataModel;
41 public partial class SendErrorReportForm : OTBaseForm
43 public ErrorReport ErrorReport
45 get => this.errorReport;
48 this.errorReport = value;
49 this.bindingSource.DataSource = value;
53 private ErrorReport errorReport = null!;
55 public SendErrorReportForm()
56 => this.InitializeComponent();
58 private void SendErrorReportForm_Shown(object sender, EventArgs e)
60 this.pictureBoxIcon.Image = SystemIcons.Error.ToBitmap();
61 this.textBoxErrorReport.DeselectAll();
64 private void ButtonReset_Click(object sender, EventArgs e)
65 => this.ErrorReport.Reset();
67 private async void ButtonSendByMail_Click(object sender, EventArgs e)
69 this.DialogResult = DialogResult.OK;
70 await this.ErrorReport.SendByMailAsync();
73 private async void ButtonSendByDM_Click(object sender, EventArgs e)
75 this.DialogResult = DialogResult.OK;
79 await this.ErrorReport.SendByDmAsync();
81 MessageBox.Show(Properties.Resources.SendErrorReport_DmSendCompleted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
83 catch (WebApiException ex)
85 var message = Properties.Resources.SendErrorReport_DmSendError + Environment.NewLine + "Err:" + ex.Message;
86 MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
90 private void ButtonNotSend_Click(object sender, EventArgs e)
91 => this.DialogResult = DialogResult.Cancel;
94 public class ErrorReport : NotifyPropertyChangedBase
96 public string ReportText
98 get => this.reportText;
101 this.SetProperty(ref this.reportText, value);
102 this.UpdateEncodedReport();
106 private string reportText = "";
108 public bool AnonymousReport
110 get => this.anonymousReport;
113 this.SetProperty(ref this.anonymousReport, value);
114 this.UpdateEncodedReport();
118 private bool anonymousReport = true;
120 public bool CanSendByDM
122 get => this.canSendByDm;
123 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);
134 private string encodedReportForDM = "";
136 private readonly Twitter? tw;
137 private readonly string originalReportText;
139 public ErrorReport(string reportText)
140 : this(null, reportText)
144 public ErrorReport(Twitter? tw, string reportText)
147 this.originalReportText = reportText;
153 => this.ReportText = this.originalReportText;
155 public async Task SendByMailAsync()
157 var toAddress = ApplicationSettings.FeedbackEmailAddress;
158 var subject = $"{ApplicationSettings.ApplicationName} {MyCommon.GetReadableVersion()} エラーログ";
159 var body = this.ReportText;
161 var mailto = $"mailto:{Uri.EscapeDataString(toAddress)}?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(body)}";
162 await Task.Run(() => Process.Start(mailto));
165 public async Task SendByDmAsync()
167 if (!this.CheckDmAvailable())
170 await this.tw!.SendDirectMessage(this.EncodedReportForDM);
173 private void UpdateEncodedReport()
175 if (!this.CheckDmAvailable())
177 this.CanSendByDM = false;
181 var body = $"Anonymous: {this.AnonymousReport}" + Environment.NewLine + this.ReportText;
182 var originalBytes = Encoding.UTF8.GetBytes(body);
184 using (var outputStream = new MemoryStream())
186 using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress, leaveOpen: true))
188 gzipStream.Write(originalBytes, 0, originalBytes.Length);
191 var encodedReport = Convert.ToBase64String(outputStream.ToArray());
192 var destScreenName = ApplicationSettings.FeedbackTwitterName.Substring(1);
193 this.EncodedReportForDM = $"D {destScreenName} ErrorReport: {encodedReport}";
196 this.CanSendByDM = this.tw!.GetTextLengthRemain(this.EncodedReportForDM) >= 0;
199 private bool CheckDmAvailable()
201 if (!ApplicationSettings.AllowSendErrorReportByDM)
204 if (this.tw == null || !this.tw.AccessLevel.HasFlag(TwitterApiAccessLevel.DirectMessage))
207 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)