File: ExceptionNotifyFormController.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-6
  • links: PTS
  • area: main
  • in suites: buster
  • size: 35,916 kB
  • sloc: cs: 160,836; java: 42,449; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 236; php: 142; xml: 117; sh: 21
file content (186 lines) | stat: -rw-r--r-- 6,973 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * ExceptionNotifyFormController.cs
 * Copyright © 2011 kbinani
 *
 * This file is part of org.kbinani.cadencii.
 *
 * org.kbinani.cadencii is free software; you can redistribute it and/or
 * modify it under the terms of the GPLv3 License.
 *
 * org.kbinani.cadencii is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
#if JAVA

package org.kbinani.cadencii;

import java.io.*;
import java.net.*;
import org.kbinani.apputil.*;
import org.kbinani.*;

#else

namespace org
{
    namespace kbinani
    {
        namespace cadencii
        {
#if CSHARP
            using System;
            using org.kbinani.apputil;
#endif

#endif

#if JAVA
            public class ExceptionNotifyFormController extends ControllerBase implements ExceptionNotifyFormUiListener
#else
            public class ExceptionNotifyFormController : ControllerBase, ExceptionNotifyFormUiListener
#endif
            {
                protected ExceptionNotifyFormUi ui;
                protected string exceptionMessage = "";

                public ExceptionNotifyFormController()
                {
                    ui = (ExceptionNotifyFormUi)new ExceptionNotifyFormUiImpl( this );
                    this.applyLanguage();
                }

                #region パブリックメソッド

                public void setReportTarget( Exception ex )
                {
                    int count = 0;
                    string message = "";
                    message += "[version]\r\n" + str.replace( Utility.getVersion(), "\n\n", "\n" ) + "\r\n";
                    message += "[system]\r\n" + this.getSystemInfo() + "\r\n";
                    message += this.extractMessageString( ex, count );
                    this.exceptionMessage = message;
                    this.ui.setExceptionMessage( this.exceptionMessage );
                }

                public ExceptionNotifyFormUi getUi()
                {
                    return this.ui;
                }

                #endregion


                #region ExceptionNotifyFormUiListenerの実装

                public void sendButtonClick()
                {
#if DEBUG
					sout.println( "ExceptionNotifyFormController::sendButtonClick" );
#endif
                    string url = "http://www.kbinani.info/cadenciiProblemReport.php";
#if CSHARP
                    try {
                        System.Text.Encoding enc = System.Text.Encoding.GetEncoding( "UTF-8" );
                        string postData = "message=" + System.Web.HttpUtility.UrlEncode( this.exceptionMessage, enc );
                        System.Net.WebClient client = new System.Net.WebClient();
                        client.Encoding = enc;
                        client.Headers.Add( "Content-Type", "application/x-www-form-urlencoded" );
                        client.UploadString( url, postData );
                    } catch ( Exception ex ) {
                    }
#elif JAVA
                    try{
                        URL urlObj = new URL( url );
                        URLConnection connection = urlObj.openConnection();
                        connection.setDoOutput( true );
                        OutputStream stream = connection.getOutputStream();
                        String postData = "message=" + URLEncoder.encode( this.exceptionMessage, "UTF-8" );
                        PrintStream printStream = new PrintStream( stream );
                        printStream.print( postData );
                        printStream.close();
                        
                        InputStream inputStream = connection.getInputStream();
                        BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
                        String str = "";
                        String s;
                        while( (s = reader.readLine()) != null ){
                        	str += s;
                        }
                        reader.close();
#if DEBUG
                        sout.println( "ExceptionNotifyFormController::sendButtonClick; str=" + str );
#endif
                    }catch( Exception ex ){
                    	ex.printStackTrace();
                    }
#endif
                    ui.close();
                }

                public void cancelButtonClick()
                {
                    ui.close();
                }

                #endregion


                /// <summary>
                /// 例外からその情報を再帰的に取り出す
                /// </summary>
                /// <param name="ex"></param>
                /// <returns></returns>
                protected string extractMessageString( Exception ex, int count )
                {
#if JAVA
                    String str = "[exception-" + count + "]\r\n" + ex.getMessage() + "\r\n";
                    StringWriter stream = new StringWriter();
                    ex.printStackTrace( new PrintWriter( stream ) );
                    str += stream.toString() + "\r\n";
                    Throwable t = ex.getCause();
                    if ( t != null && t instanceof Exception ) {
                        str += extractMessageString( (Exception)t, ++count );
                    }
#else
                    string str = "[exception-" + count + "]\r\n" + ex.Message + "\r\n";
                    str += ex.StackTrace + "\r\n";
                    if ( ex.InnerException != null ) {
                        str += extractMessageString( ex.InnerException, ++count );
                    }
#endif
                    return str;
                }

                /// <summary>
                /// システムの情報を取得する
                /// </summary>
                /// <returns></returns>
                protected string getSystemInfo()
                {
#if JAVA
					return "OSVersion=" + System.getProperty("os.name") + "\njavaVersion=" + System.getProperty("java.version");
#else
                    return "OSVersion=" + Environment.OSVersion.ToString() + "\ndotNetVersion=" + System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion();
#endif
                }

                protected void applyLanguage()
                {
                    this.ui.setTitle( _( "Problem Report for Cadencii" ) );
                    this.ui.setDescription( _( "Problem Details" ) );
                    this.ui.setCancelButtonText( _( "Cancel" ) );
                    this.ui.setSendButtonText( _( "Send to Developper" ) );
                }

                protected string _( string id )
                {
                    return Messaging.getMessage( id );
                }
            }

#if !JAVA
        }
    }
}
#endif