[chronojump] Added ping on start and on help menu



commit 5c83b0d5bd656ae8a4f48726eaed95b29ccfcf25
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Feb 16 01:54:16 2015 +0100

    Added ping on start and on help menu

 glade/chronojump.glade    |   66 +++++++++++++++++++++++++++++++++++++++++++++
 src/chronojump.cs         |   60 ++++++++++++++++++++++++++--------------
 src/gui/chronojump.cs     |   21 ++++++++++++++
 src/json.cs               |   63 ++++++++++++++++++++++++++++++++++++++++--
 src/sqlite/preferences.cs |   14 ++++++++-
 5 files changed, 198 insertions(+), 26 deletions(-)
---
diff --git a/glade/chronojump.glade b/glade/chronojump.glade
index 7017b58..0bcebec 100644
--- a/glade/chronojump.glade
+++ b/glade/chronojump.glade
@@ -378,6 +378,15 @@
                       </widget>
                     </child>
                     <child>
+                      <widget class="GtkMenuItem" id="menuitem_ping">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">Ping</property>
+                        <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_menuitem_ping_activate" swapped="no"/>
+                      </widget>
+                    </child>
+                    <child>
                       <widget class="GtkMenuItem" id="menuitem_check_last_version">
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
@@ -6434,6 +6443,9 @@ Second Chronopic to platforms.</property>
                                                         <child>
                                                           <placeholder/>
                                                         </child>
+                                                        <child>
+                                                          <placeholder/>
+                                                        </child>
                                                       </widget>
                                                       <packing>
                                                         <property name="expand">True</property>
@@ -7242,6 +7254,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             </child>
                                                             <child>
@@ -8302,6 +8317,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="position">2</property>
@@ -9003,6 +9021,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="position">4</property>
@@ -19334,6 +19355,9 @@ by you</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -19943,6 +19967,9 @@ by you</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -21044,6 +21071,9 @@ by you</property>
               <placeholder/>
             </child>
             <child>
+              <placeholder/>
+            </child>
+            <child>
               <widget class="GtkButton" id="button_video_url">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
@@ -21202,6 +21232,9 @@ by you</property>
             <child>
               <placeholder/>
             </child>
+            <child>
+              <placeholder/>
+            </child>
           </widget>
           <packing>
             <property name="expand">True</property>
@@ -30753,6 +30786,24 @@ options</property>
                             <child>
                               <placeholder/>
                             </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
                           </widget>
                         </child>
                       </widget>
@@ -32742,6 +32793,9 @@ To differentiate between male and female, use the values 1/0, or m/f, or M/F on
                                 <child>
                                   <placeholder/>
                                 </child>
+                                <child>
+                                  <placeholder/>
+                                </child>
                               </widget>
                             </child>
                           </widget>
@@ -33389,6 +33443,9 @@ To differentiate between male and female, use the values 1/0, or m/f, or M/F on
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -36002,6 +36059,9 @@ show elevation as:</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                       <packing>
                         <property name="left_attach">2</property>
@@ -36275,6 +36335,9 @@ show elevation as:</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
@@ -36563,6 +36626,9 @@ show elevation as:</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
diff --git a/src/chronojump.cs b/src/chronojump.cs
index 9b11a2d..88c7eb0 100644
--- a/src/chronojump.cs
+++ b/src/chronojump.cs
@@ -185,8 +185,8 @@ public class ChronoJump
 
        //variables to manage the ping thread
        string versionAvailable;
-       bool pingStart;
-       bool pingEnd;
+       //bool pingStart;
+       //bool pingEnd;
        bool pulseGTKPingShouldEnd;
        bool allSQLCallsDoneOnSqliteThingsThread;
 
@@ -347,10 +347,11 @@ public class ChronoJump
 
                //connect to server to Ping
                versionAvailable = "";
+               /*
                pingStart = false;
                pingEnd = false;
                
-               /* disable server connection on start until server is not working on windows again  */
+               // disable server connection on start until server is not working on windows again 
                bool connectServerAtStart = false;
                if(connectServerAtStart) {
                        thread = new Thread(new ThreadStart(findVersion));
@@ -363,15 +364,28 @@ public class ChronoJump
                }
                else
                        on_find_version_cancelled(new object(), new EventArgs());
+               */
+               versionAvailable = Constants.ServerOffline;
+       
+
+               //doing ping using json methods
+               Json js = new Json();
+               bool success = js.Ping(UtilAll.GetOS(), progVersion);
+               if(success)
+                       LogB.Information(js.ResultMessage);
+               else
+                       LogB.Error(js.ResultMessage);
 
                
                allSQLCallsDoneOnSqliteThingsThread = false;
-
+                       
                //wait until pinging ends (or it's cancelled)
-               while(! pingEnd) {
-               }
-
-               string versionAvailableKnown = SqlitePreferences.Select("versionAvailable");
+               //while(! pingEnd) {
+               //}
+
+               Sqlite.Open();
+
+               string versionAvailableKnown = SqlitePreferences.Select("versionAvailable", true);
                if( versionAvailable != Constants.ServerOffline && new Version(versionAvailable) > new 
Version(progVersion) ) {
                        //check if available version is higher than known available version
                        Version versionAvailableAsV = new Version(versionAvailable);
@@ -389,14 +403,14 @@ public class ChronoJump
                        if(updateKnownVersion) {
                                //is the first time we know about this new version
                                //just write on db and show message to user
-                               SqlitePreferences.Update(Constants.PrefVersionAvailable, versionAvailable, 
false);
+                               SqlitePreferences.Update(Constants.PrefVersionAvailable, versionAvailable, 
true);
                                versionAvailableKnown = versionAvailable;
                                messageToShowOnBoot += string.Format(Catalog.GetString(
                                                        "\nNew Chronojump version available on website.\nYour 
Chronojump version is: {1}"), 
                                                versionAvailable, progVersion) + "\n\n" + 
                                        Catalog.GetString("Please, update to new version.") + "\n";
                        }
-               }
+               }
 
 
                //if chronojump chrashed before
@@ -406,7 +420,7 @@ public class ChronoJump
                                       Catalog.GetString("Please, update to new version: ") + 
versionAvailableKnown + "\n";
                        else {
                                messageToShowOnBoot += messageChrashedBefore;
-                               //SqlitePreferences.Update("videoOn", "False", false);
+                               //SqlitePreferences.Update("videoOn", "False", true);
                        }
                }
                
@@ -415,7 +429,9 @@ public class ChronoJump
                
 
                //start as "simulated"
-               SqlitePreferences.Update("simulated", "True", false); //false (dbcon not opened)
+               SqlitePreferences.Update("simulated", "True", true); //dbcon opened
+
+               Sqlite.Close();
                
                allSQLCallsDoneOnSqliteThingsThread = true;
                LogB.SQL("all SQL calls done on sqliteThings thread");
@@ -430,7 +446,7 @@ public class ChronoJump
 
        private void findVersion() {
                LogB.Debug("--1--");
-               pingStart = true;
+               //pingStart = true;
                pulseGTKPingShouldEnd = false;
                splashShowButton = true;
                
@@ -461,7 +477,7 @@ public class ChronoJump
                splashShowButton = false;
                LogB.Information(" version:  ", versionAvailable);
                LogB.Debug("\n--5--");
-               pingEnd = true;
+               //pingEnd = true;
                LogB.Debug("--6--");
        }
                
@@ -469,7 +485,7 @@ public class ChronoJump
                splashShowButton = false;
                pulseGTKPingShouldEnd = true;
                versionAvailable = Constants.ServerOffline;
-               pingEnd = true;
+               //pingEnd = true;
        }
 
        protected void readMessageToStart() {
@@ -560,8 +576,9 @@ public class ChronoJump
                if(quitNow) 
                        return false;
 
-               if( ( needEndSplashWin && pingEnd ) 
-                               || ! thread.IsAlive) {
+               //if( ( needEndSplashWin && pingEnd ) 
+               //              || ! thread.IsAlive) {
+               if( needEndSplashWin || ! thread.IsAlive ) {
                        LogB.ThreadEnding();
                        fakeSplashButton.Click();
 
@@ -607,7 +624,8 @@ public class ChronoJump
                LogB.Information("splash screen ENDED!");
                readMessageToStart();
        }
-
+
+       /*
        protected bool PulseGTKPing ()
        {
                if(pulseGTKPingShouldEnd) {
@@ -626,9 +644,9 @@ public class ChronoJump
                if(thread.ThreadState == System.Threading.ThreadState.Stopped)
                        pulseGTKPingShouldEnd = true;
                return true;
-       }
-       
-               
+       }
+       */
+       
        /* ---------------------
         * other support methods 
         * ---------------------*/
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 3b1597f..7e3c02b 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -5938,6 +5938,27 @@ LogB.Debug("X");
                                        js.ResultMessage);
                }
        }
+       
+       private void on_menuitem_ping_activate (object o, EventArgs args) {
+               Json js = new Json();
+               bool success = js.Ping(UtilAll.GetOS(), UtilAll.ReadVersion());
+
+               if(success) {
+                       LogB.Information(js.ResultMessage);
+                       new DialogMessage(
+                                       "Chronojump",
+                                       Constants.MessageTypes.INFO, 
+                                       js.ResultMessage);
+               }
+               else {
+                       LogB.Error(js.ResultMessage);
+                       new DialogMessage(
+                                       "Chronojump",
+                                       Constants.MessageTypes.WARNING, 
+                                       js.ResultMessage);
+               }
+       }
+
 
        private void on_about1_activate (object o, EventArgs args) {
                string translator_credits = Catalog.GetString ("translator-credits");
diff --git a/src/json.cs b/src/json.cs
index 7a35613..2d7e51b 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -23,12 +23,16 @@ using System.Net;
 using System.Web;
 using System.IO;
 using System.Json;
+using System.Text;
 using System.Collections.Generic; //Dictionary
 using Mono.Unix;
 
+
 public class Json
 {
        public string ResultMessage;
+       string serverUrl = "http://api.chronojump.org:8080";;
+
 
        public Json()
        {
@@ -37,7 +41,6 @@ public class Json
 
        public bool PostCrashLog(string email, string comments) 
        {
-               string serverUrl = "http://api.chronojump.org:8080";;
                string filePath = UtilAll.GetLogFileOld();
 
                if(! File.Exists(filePath)) {
@@ -121,8 +124,6 @@ public class Json
 
        public bool GetLastVersion() 
        {
-               string serverUrl = "http://api.chronojump.org:8080";;
-
                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create (serverUrl + "/version");
                
@@ -153,5 +154,61 @@ public class Json
                return true;
        }
 
+       public bool Ping(string osVersion, string cjVersion) 
+       {
+               // Create a request using a URL that can receive a post. 
+               WebRequest request = WebRequest.Create (serverUrl + "/ping");
+
+               // Set the Method property of the request to POST.
+               request.Method = "POST";
+
+               // Set the ContentType property of the WebRequest.
+               request.ContentType = "application/json";
+
+               // Creates the json object
+               JsonObject json = new JsonObject();
+               json.Add("os_version", osVersion);
+               json.Add("cj_version", cjVersion);
+
+               // Converts it to a String
+               String js = json.ToString();
+
+               // Writes the json object into the request dataStream
+               Stream dataStream;
+               try {
+                       dataStream = request.GetRequestStream ();
+               } catch {
+                       this.ResultMessage = 
+                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."), 
+                               serverUrl);
+                       return false;
+               }
+               dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
+
+               dataStream.Close ();
+
+               // Get the response.
+               WebResponse response;
+               try {
+                       response = request.GetResponse ();
+               } catch {
+                       this.ResultMessage = 
+                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."), 
+                               serverUrl);
+                       return false;
+               }
+
+               // Display the status (will be 201, CREATED)
+               Console.WriteLine (((HttpWebResponse)response).StatusDescription);
+
+               // Clean up the streams.
+               dataStream.Close ();
+               response.Close ();
+               
+               this.ResultMessage = "Ping sent.";
+               return true;
+       }
+
+
        ~Json() {}
 }
diff --git a/src/sqlite/preferences.cs b/src/sqlite/preferences.cs
index 331b0df..e00bafe 100644
--- a/src/sqlite/preferences.cs
+++ b/src/sqlite/preferences.cs
@@ -126,9 +126,17 @@ class SqlitePreferences : Sqlite
                        Sqlite.Close();
        }
 
+       //Called from most of all old Chronojump methods
        public static string Select (string myName) 
        {
-               Sqlite.Open();
+               return Select(myName, false);
+       }
+       //Called from new methods were dbcon is opened
+       public static string Select (string myName, bool dbconOpened) 
+       {
+               if(! dbconOpened)
+                       Sqlite.Open();
+
                dbcmd.CommandText = "SELECT value FROM " + Constants.PreferencesTable + 
                        " WHERE name == '" + myName + "'" ;
                LogB.SQL(dbcmd.CommandText.ToString());
@@ -144,7 +152,9 @@ class SqlitePreferences : Sqlite
                        myReturn = reader[0].ToString();
                }
                reader.Close();
-               Sqlite.Close();
+               
+               if(! dbconOpened)
+                       Sqlite.Close();
 
                return myReturn;
        }


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