[chronojump] After a crash. a log can be uploaded. thanks to Carles.



commit eabe3480d994c661633d5a0e8e96ba89063f5fac
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sat Jan 17 02:45:28 2015 +0100

    After a crash. a log can be uploaded. thanks to Carles.

 build/build.environment.mk |    4 +-
 glade/chronojump.glade     |  202 +++++++++++++++++++++++++++++++++++++++++++-
 po/POTFILES.in             |    1 +
 src/Makefile.am            |    1 +
 src/chronojump.cs          |    1 +
 src/constants.cs           |    3 +
 src/gui/error.cs           |   26 ++++++-
 src/json_post.cs           |  113 ++++++++++++++++++++++++
 src/utilAll.cs             |    6 +-
 testing-stuff/json_post.cs |   29 ++++++-
 10 files changed, 377 insertions(+), 9 deletions(-)
---
diff --git a/build/build.environment.mk b/build/build.environment.mk
index 9b4a519..3cf85d3 100644
--- a/build/build.environment.mk
+++ b/build/build.environment.mk
@@ -25,6 +25,7 @@ LINK_GLADE = $(GLADE_SHARP_20_LIBS)
 LINK_CESARPLAYER = -r:$(DIR_BIN)/CesarPlayer.dll
 LINK_RDOTNET = -r:$(DIR_BIN)/RDotNet.dll
 LINK_RDOTNET_NATIVE = -r:$(DIR_BIN)/RDotNet.NativeLibrary.dll
+LINK_JSON = -r:System.Json.dll
 
 REF_DEP_CHRONOJUMP = \
        $(LINK_SYSTEM) \
@@ -39,7 +40,8 @@ REF_DEP_CHRONOJUMP = \
        $(LINK_GTK) \
        $(LINK_CESARPLAYER) \
        $(LINK_RDOTNET) \
-       $(LINK_RDOTNET_NATIVE)
+       $(LINK_RDOTNET_NATIVE) \
+       $(LINK_JSON)
 
 REF_DEP_CESARPLAYER = \
        $(LINK_SYSTEM) \
diff --git a/glade/chronojump.glade b/glade/chronojump.glade
index 5a452a0..7389929 100644
--- a/glade/chronojump.glade
+++ b/glade/chronojump.glade
@@ -6377,6 +6377,12 @@ Second Chronopic to platforms.</property>
                                                         <child>
                                                           <placeholder/>
                                                         </child>
+                                                        <child>
+                                                          <placeholder/>
+                                                        </child>
+                                                        <child>
+                                                          <placeholder/>
+                                                        </child>
                                                       </widget>
                                                       <packing>
                                                         <property name="expand">True</property>
@@ -7143,6 +7149,12 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             </child>
                                                             <child>
@@ -8161,6 +8173,12 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="position">2</property>
@@ -8820,6 +8838,12 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="position">4</property>
@@ -19009,6 +19033,12 @@ by you</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -19576,6 +19606,12 @@ by you</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -20635,6 +20671,12 @@ by you</property>
               <placeholder/>
             </child>
             <child>
+              <placeholder/>
+            </child>
+            <child>
+              <placeholder/>
+            </child>
+            <child>
               <widget class="GtkButton" id="button_video_url">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
@@ -20751,6 +20793,12 @@ by you</property>
             <child>
               <placeholder/>
             </child>
+            <child>
+              <placeholder/>
+            </child>
+            <child>
+              <placeholder/>
+            </child>
           </widget>
           <packing>
             <property name="expand">True</property>
@@ -24533,7 +24581,7 @@ comments</property>
       <widget class="GtkVBox" id="vbox22">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
-        <property name="spacing">10</property>
+        <property name="spacing">12</property>
         <child>
           <widget class="GtkLabel" id="label1">
             <property name="visible">True</property>
@@ -24546,6 +24594,90 @@ comments</property>
           </packing>
         </child>
         <child>
+          <widget class="GtkHBox" id="hbox_send_log">
+            <property name="can_focus">False</property>
+            <property name="border_width">4</property>
+            <child>
+              <widget class="GtkVBox" id="vbox1">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="spacing">8</property>
+                <child>
+                  <widget class="GtkButton" id="button_send_log">
+                    <property name="label">Send error log</property>
+                    <property name="width_request">180</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="can_default">True</property>
+                    <property name="receives_default">False</property>
+                    <signal name="clicked" handler="on_button_send_log_clicked" swapped="no"/>
+                  </widget>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">False</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <widget class="GtkHBox" id="hbox1">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="spacing">8</property>
+                    <child>
+                      <widget class="GtkImage" id="image_send_log_no">
+                        <property name="can_focus">False</property>
+                        <property name="stock">gtk-no</property>
+                      </widget>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">False</property>
+                        <property name="position">0</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <widget class="GtkImage" id="image_send_log_yes">
+                        <property name="can_focus">False</property>
+                        <property name="stock">gtk-yes</property>
+                      </widget>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">False</property>
+                        <property name="position">1</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <widget class="GtkLabel" id="label_send_log_message">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                      </widget>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">False</property>
+                        <property name="position">2</property>
+                      </packing>
+                    </child>
+                  </widget>
+                  <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
+              </widget>
+              <packing>
+                <property name="expand">True</property>
+                <property name="fill">False</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">False</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child>
           <widget class="GtkHButtonBox" id="hbuttonbox13">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
@@ -24599,7 +24731,7 @@ comments</property>
           <packing>
             <property name="expand">False</property>
             <property name="fill">False</property>
-            <property name="position">1</property>
+            <property name="position">2</property>
           </packing>
         </child>
       </widget>
@@ -29841,6 +29973,42 @@ options</property>
                             <child>
                               <placeholder/>
                             </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
                           </widget>
                         </child>
                       </widget>
@@ -31788,6 +31956,12 @@ To differentiate between male and female, use the values 1/0, or m/f, or M/F on
                                 <child>
                                   <placeholder/>
                                 </child>
+                                <child>
+                                  <placeholder/>
+                                </child>
+                                <child>
+                                  <placeholder/>
+                                </child>
                               </widget>
                             </child>
                           </widget>
@@ -32393,6 +32567,12 @@ To differentiate between male and female, use the values 1/0, or m/f, or M/F on
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -34964,6 +35144,12 @@ show elevation as:</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                       <packing>
                         <property name="left_attach">2</property>
@@ -35195,6 +35381,12 @@ show elevation as:</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
@@ -35441,6 +35633,12 @@ show elevation as:</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index a33b8db..5e3c562 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -20,6 +20,7 @@ src/execute/pulse.cs
 src/execute/reactionTime.cs
 src/execute/run.cs
 src/exportSession.cs
+src/json_post.cs
 src/gui/chronojump.cs
 src/gui/chronopic.cs
 src/gui/convertWeight.cs
diff --git a/src/Makefile.am b/src/Makefile.am
index 3f81c8d..0db32cd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -121,6 +121,7 @@ SOURCES = \
        event.cs\
        eventType.cs\
        genericObjects.cs\
+       json_post.cs\
        jump.cs\
        jumpType.cs\
        person.cs\
diff --git a/src/chronojump.cs b/src/chronojump.cs
index c2213f2..fe9d48c 100644
--- a/src/chronojump.cs
+++ b/src/chronojump.cs
@@ -485,6 +485,7 @@ public class ChronoJump
                        } else { 
                                errorWin = ErrorWindow.Show(messageToShowOnBoot);
                                errorWin.Show_button_open_docs_folder();
+                               errorWin.Show_send_log();
                                errorWin.Button_accept_label(Catalog.GetString("Open Chronojump"));
                                errorWin.Button_accept.Clicked += new 
EventHandler(on_message_boot_accepted_continue);
                        }
diff --git a/src/constants.cs b/src/constants.cs
index 8407ccf..0479c3b 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -429,6 +429,9 @@ public class Constants
        public static string FileNameSelectorJumps = "chronojump-jumps-small.png";
        public static string FileNameSelectorRuns = "chronojump-runs-small.png";
        public static string FileNameSelectorEncoder = "chronojump-encoder-small.jpg";
+
+       public static string FileNameLog = "log_chronojump.txt";
+       public static string FileNameLogOld = "log_chronojump_old.txt";
        
        //30 colors defined
        //see als UtilGtk that's not used by the server
diff --git a/src/gui/error.cs b/src/gui/error.cs
index 1dec79c..a006dd4 100644
--- a/src/gui/error.cs
+++ b/src/gui/error.cs
@@ -15,7 +15,7 @@
  *  along with this program; if not, write to the Free Software
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- * Copyright (C) 2004-2014   Xavier de Blas <xaviblas gmail com> 
+ * Copyright (C) 2004-2015   Xavier de Blas <xaviblas gmail com> 
  */
 
 using System;
@@ -33,8 +33,12 @@ public class ErrorWindow
        [Widget] Gtk.Window error_window;
        [Widget] Gtk.Label label1;
        [Widget] Gtk.Button button_accept;
+       [Widget] Gtk.Box hbox_send_log;
        [Widget] Gtk.Button button_open_database_folder;
        [Widget] Gtk.Button button_open_docs_folder;
+       [Widget] Gtk.Image image_send_log_no;
+       [Widget] Gtk.Image image_send_log_yes;
+       [Widget] Gtk.Label label_send_log_message;
 
        string table;
        static ErrorWindow ErrorWindowBox;
@@ -61,6 +65,7 @@ public class ErrorWindow
                }
                
                //hidden always excepted when called to be shown (see below)
+               ErrorWindowBox.hbox_send_log.Hide();
                ErrorWindowBox.button_open_database_folder.Hide();
 
                ErrorWindowBox.error_window.Show();
@@ -79,6 +84,25 @@ public class ErrorWindow
                button_accept.Click();
        }
        
+       public void Show_send_log() {
+               hbox_send_log.Show();
+       }
+       private void on_button_send_log_clicked (object o, EventArgs args)
+       {
+               JsonPost jp = new JsonPost();
+               bool success = jp.PostCrashLog();
+               
+               if(success) {
+                       image_send_log_yes.Show();
+                       LogB.Information(jp.ResultMessage);
+               } else {
+                       image_send_log_no.Show();
+                       LogB.Error(jp.ResultMessage);
+               }
+
+               label_send_log_message.Text = jp.ResultMessage;
+       }
+
        public void Show_button_open_database_folder () {
                button_open_database_folder.Show();
        }
diff --git a/src/json_post.cs b/src/json_post.cs
new file mode 100644
index 0000000..5291bd2
--- /dev/null
+++ b/src/json_post.cs
@@ -0,0 +1,113 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or   
+ *    (at your option) any later version.
+ *    
+ * ChronoJump 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.  See the 
+ *    GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2015 Carles Pina & Xavier de Blas
+ */
+
+using System;
+using System.Net;
+using System.Web;
+using System.IO;
+using System.Json;
+using Mono.Unix;
+
+public class JsonPost
+{
+       public string ResultMessage;
+
+       public JsonPost()
+       {
+               ResultMessage = "";
+       }
+
+       public bool PostCrashLog() 
+       {
+               string filePath = UtilAll.GetLogFileOld();
+               if(! File.Exists(filePath)) {
+                       this.ResultMessage = Catalog.GetString("Could not send file.\nIt does not exist.");
+                       return false;
+               }
+
+               // Create a request using a URL that can receive a post. 
+               WebRequest request = WebRequest.Create ("http://chronojump.org:8080/api/backtrace";);
+
+               // Set the Method property of the request to POST.
+               request.Method = "POST";
+
+               // Create POST data and convert it to a byte array.
+               byte[] byteArray = readFile(filePath);
+
+               // Set the ContentType property of the WebRequest.
+               request.ContentType = "application/x-www-form-urlencoded";
+
+               // Set the ContentLength property of the WebRequest.
+               request.ContentLength = byteArray.Length;
+
+               // Get the request stream.
+               Stream dataStream;
+               try {
+                       dataStream = request.GetRequestStream ();
+               } catch {
+                       this.ResultMessage = Catalog.GetString("Could not send file.\nYou are not connected 
to the Internet\nor server is down.");
+                       return false;
+               }
+
+               // Write the data to the request stream.
+               dataStream.Write (byteArray, 0, byteArray.Length);
+
+               // Close the Stream object.
+               dataStream.Close ();
+
+               // Get the response.
+               WebResponse response = request.GetResponse ();
+
+               // Display the status.
+               LogB.Information(((HttpWebResponse)response).StatusDescription);
+
+               // Get the stream containing content returned by the server.
+               dataStream = response.GetResponseStream ();
+
+               // Open the stream using a StreamReader for easy access.
+               StreamReader reader = new StreamReader (dataStream);
+
+               // Read the content.
+               string responseFromServer = reader.ReadToEnd ();
+
+               // Display the content.
+               LogB.Information(responseFromServer);
+
+               // Clean up the streams.
+               reader.Close ();
+               dataStream.Close ();
+               response.Close ();    
+
+
+               JsonValue result = JsonValue.Parse(responseFromServer);
+               string crash_id = result["crash_id"];
+               LogB.Information("crash_id: ", crash_id);
+
+               this.ResultMessage = Catalog.GetString("Log sent. Thank you.");
+               return true;
+       }
+       
+       private byte[] readFile(string filePath)
+       {
+               return System.IO.File.ReadAllBytes(filePath); 
+       }
+
+       ~JsonPost() {}
+}
diff --git a/src/utilAll.cs b/src/utilAll.cs
index 99d146f..ee0ba3f 100644
--- a/src/utilAll.cs
+++ b/src/utilAll.cs
@@ -15,7 +15,7 @@
  *  along with this program; if not, write to the Free Software
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- *  Copyright (C) 2004-2014   Xavier de Blas <xaviblas gmail com> 
+ *  Copyright (C) 2004-2015   Xavier de Blas <xaviblas gmail com> 
  */
 
 using System;
@@ -114,10 +114,10 @@ public class UtilAll
                                "Chronojump" + Path.DirectorySeparatorChar + "logs");
        }
        public static string GetLogFileCurrent() {
-               return Path.Combine(GetLogsDir() +  Path.DirectorySeparatorChar + "log_chronojump.txt");
+               return Path.Combine(GetLogsDir() +  Path.DirectorySeparatorChar + Constants.FileNameLog);
        }
        public static string GetLogFileOld() {
-               return Path.Combine(GetLogsDir() +  Path.DirectorySeparatorChar + "log_chronojump_old.txt");
+               return Path.Combine(GetLogsDir() +  Path.DirectorySeparatorChar + Constants.FileNameLogOld);
        }
 
 }
diff --git a/testing-stuff/json_post.cs b/testing-stuff/json_post.cs
index efaa19b..497edf6 100644
--- a/testing-stuff/json_post.cs
+++ b/testing-stuff/json_post.cs
@@ -7,7 +7,7 @@ using System.Web;
 using System.IO;
 using System.Json;
 
-public class HelloWorld
+public class JsonTest
 {
        static private string getLogsDir() 
        {
@@ -25,34 +25,59 @@ public class HelloWorld
        {
                string filePath = getLogsDir() + Path.DirectorySeparatorChar + "log_chronojump.txt";
 
+               if(! File.Exists(filePath)) {
+                       Console.WriteLine("Could not send file. It does not exist.");
+                       return;
+               }
+
                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create ("http://chronojump.org:8080/api/backtrace";);
+
                // Set the Method property of the request to POST.
                request.Method = "POST";
+               
                // Create POST data and convert it to a byte array.
                byte[] byteArray = readFile(filePath);
+               
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
+               
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
+               
                // Get the request stream.
-               Stream dataStream = request.GetRequestStream ();
+               Stream dataStream;
+               try {
+                        dataStream = request.GetRequestStream ();
+               } catch {
+                       Console.WriteLine("Could not send file. You are not connected to the Internet or 
server is down.");
+                       return;
+               }
+               
                // Write the data to the request stream.
                dataStream.Write (byteArray, 0, byteArray.Length);
+               
                // Close the Stream object.
                dataStream.Close ();
+               
                // Get the response.
                WebResponse response = request.GetResponse ();
+               
                // Display the status.
                Console.WriteLine (((HttpWebResponse)response).StatusDescription);
+               
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream ();
+               
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader (dataStream);
+               
                // Read the content.
                string responseFromServer = reader.ReadToEnd ();
+               
                // Display the content.
                Console.WriteLine (responseFromServer);
+               
                // Clean up the streams.
                reader.Close ();
                dataStream.Close ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]