OSDN Git Service

ApplicationSettings.AllowSendErrorReportByDM をconstからstatic readonlyに変更
[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 using System;
23 using System.Collections.Generic;
24 using System.ComponentModel;
25 using System.Data;
26 using System.Diagnostics;
27 using System.Drawing;
28 using System.IO;
29 using System.IO.Compression;
30 using System.Linq;
31 using System.Runtime.CompilerServices;
32 using System.Text;
33 using System.Threading.Tasks;
34 using System.Windows.Forms;
35 using OpenTween.Api;
36
37 namespace OpenTween
38 {
39     public partial class SendErrorReportForm : Form
40     {
41         public ErrorReport ErrorReport
42         {
43             get { return this._errorReport; }
44             set
45             {
46                 this._errorReport = value;
47                 this.bindingSource.DataSource = value;
48             }
49         }
50         private ErrorReport _errorReport;
51
52         public SendErrorReportForm()
53         {
54             this.InitializeComponent();
55         }
56
57         private void SendErrorReportForm_Shown(object sender, EventArgs e)
58         {
59             this.textBoxErrorReport.DeselectAll();
60         }
61
62         private void buttonReset_Click(object sender, EventArgs e)
63         {
64             this.ErrorReport.Reset();
65         }
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 + 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         {
92             this.DialogResult = DialogResult.Cancel;
93         }
94     }
95
96     public class ErrorReport : INotifyPropertyChanged
97     {
98         public string ReportText
99         {
100             get { return this._reportText; }
101             set
102             {
103                 this.SetProperty(ref this._reportText, value);
104                 this.UpdateEncodedReport();
105             }
106         }
107         private string _reportText;
108
109         public bool CanSendByDM
110         {
111             get { return this._canSendByDm; }
112             private set { this.SetProperty(ref this._canSendByDm, value); }
113         }
114         private bool _canSendByDm;
115
116         public string EncodedReportForDM
117         {
118             get { return this._encodedReportForDM; }
119             private set { this.SetProperty(ref this._encodedReportForDM, value); }
120         }
121         private string _encodedReportForDM;
122
123         private readonly Twitter tw;
124         private readonly string originalReportText;
125
126         public event PropertyChangedEventHandler PropertyChanged;
127
128         public ErrorReport(string reportText)
129             : this(null, reportText)
130         {
131         }
132
133         public ErrorReport(Twitter tw, string reportText)
134         {
135             this.tw = tw;
136             this.originalReportText = reportText;
137
138             this.Reset();
139         }
140
141         public void Reset()
142         {
143             this.ReportText = this.originalReportText;
144         }
145
146         public async Task SendByMailAsync()
147         {
148             var toAddress = ApplicationSettings.FeedbackEmailAddress;
149             var subject = $"{Application.ProductName} {MyCommon.GetReadableVersion()} エラーログ";
150             var body = this.ReportText;
151
152             var mailto = $"mailto:{toAddress}?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(body)}";
153             await Task.Run(() => Process.Start(mailto));
154         }
155
156         public async Task SendByDmAsync()
157         {
158             await Task.Run(() => this.tw.SendDirectMessage(this.EncodedReportForDM));
159         }
160
161         private void UpdateEncodedReport()
162         {
163             if (!this.CheckDmAvailable())
164             {
165                 this.CanSendByDM = false;
166                 return;
167             }
168
169             var originalBytes = Encoding.UTF8.GetBytes(this.ReportText);
170
171             using (var outputStream = new MemoryStream())
172             {
173                 using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress, leaveOpen: true))
174                 {
175                     gzipStream.Write(originalBytes, 0, originalBytes.Length);
176                 }
177
178                 var encodedReport = Convert.ToBase64String(outputStream.ToArray());
179                 var destScreenName = ApplicationSettings.FeedbackTwitterName.Substring(1);
180                 this.EncodedReportForDM = $"D {destScreenName} ErrorLog: {encodedReport}";
181             }
182
183             this.CanSendByDM = this.tw.GetTextLengthRemain(this.EncodedReportForDM) >= 0;
184         }
185
186         private bool CheckDmAvailable()
187         {
188             if (!ApplicationSettings.AllowSendErrorReportByDM)
189                 return false;
190
191             if (this.tw == null || !this.tw.AccessLevel.HasFlag(TwitterApiAccessLevel.DirectMessage))
192                 return false;
193
194             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
195                 return false;
196
197             return true;
198         }
199
200         protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
201         {
202             if (EqualityComparer<T>.Default.Equals(field, value))
203                 return;
204
205             field = value;
206             this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
207         }
208
209         protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
210         {
211             this.PropertyChanged?.Invoke(this, e);
212         }
213     }
214 }