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;
52 private ErrorReport _errorReport = null!;
54 public SendErrorReportForm()
55 => 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)
64 => this.ErrorReport.Reset();
66 private async void buttonSendByMail_Click(object sender, EventArgs e)
68 this.DialogResult = DialogResult.OK;
69 await this.ErrorReport.SendByMailAsync();
72 private async void buttonSendByDM_Click(object sender, EventArgs e)
74 this.DialogResult = DialogResult.OK;
78 await this.ErrorReport.SendByDmAsync();
80 MessageBox.Show(Properties.Resources.SendErrorReport_DmSendCompleted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
82 catch (WebApiException ex)
84 var message = Properties.Resources.SendErrorReport_DmSendError + Environment.NewLine + "Err:" + ex.Message;
85 MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
89 private void buttonNotSend_Click(object sender, EventArgs e)
90 => this.DialogResult = DialogResult.Cancel;
93 public class ErrorReport : NotifyPropertyChangedBase
95 public string ReportText
97 get => this._reportText;
100 this.SetProperty(ref this._reportText, value);
101 this.UpdateEncodedReport();
104 private string _reportText = "";
106 public bool AnonymousReport
108 get => this._anonymousReport;
111 this.SetProperty(ref this._anonymousReport, value);
112 this.UpdateEncodedReport();
115 private bool _anonymousReport = true;
117 public bool CanSendByDM
119 get => this._canSendByDm;
120 private set => this.SetProperty(ref this._canSendByDm, value);
122 private bool _canSendByDm;
124 public string EncodedReportForDM
126 get => this._encodedReportForDM;
127 private set => this.SetProperty(ref this._encodedReportForDM, value);
129 private string _encodedReportForDM = "";
131 private readonly Twitter? tw;
132 private readonly string originalReportText;
134 public ErrorReport(string reportText)
135 : this(null, reportText)
139 public ErrorReport(Twitter? tw, string reportText)
142 this.originalReportText = reportText;
148 => this.ReportText = this.originalReportText;
150 public async Task SendByMailAsync()
152 var toAddress = ApplicationSettings.FeedbackEmailAddress;
153 var subject = $"{ApplicationSettings.ApplicationName} {MyCommon.GetReadableVersion()} エラーログ";
154 var body = this.ReportText;
156 var mailto = $"mailto:{Uri.EscapeDataString(toAddress)}?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(body)}";
157 await Task.Run(() => Process.Start(mailto));
160 public async Task SendByDmAsync()
162 if (!this.CheckDmAvailable())
165 await this.tw!.SendDirectMessage(this.EncodedReportForDM);
168 private void UpdateEncodedReport()
170 if (!this.CheckDmAvailable())
172 this.CanSendByDM = false;
176 var body = $"Anonymous: {this.AnonymousReport}" + Environment.NewLine + this.ReportText;
177 var originalBytes = Encoding.UTF8.GetBytes(body);
179 using (var outputStream = new MemoryStream())
181 using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress, leaveOpen: true))
183 gzipStream.Write(originalBytes, 0, originalBytes.Length);
186 var encodedReport = Convert.ToBase64String(outputStream.ToArray());
187 var destScreenName = ApplicationSettings.FeedbackTwitterName.Substring(1);
188 this.EncodedReportForDM = $"D {destScreenName} ErrorReport: {encodedReport}";
191 this.CanSendByDM = this.tw!.GetTextLengthRemain(this.EncodedReportForDM) >= 0;
194 private bool CheckDmAvailable()
196 if (!ApplicationSettings.AllowSendErrorReportByDM)
199 if (this.tw == null || !this.tw.AccessLevel.HasFlag(TwitterApiAccessLevel.DirectMessage))
202 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)