[chronojump] server UpdateTask implemented



commit 723fdf2c941db052f3e85e9978364e32d62b3b5c
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon May 29 00:36:54 2017 +0200

    server UpdateTask implemented

 glade/dialog_person_popup.glade |   45 ++++++++++++++++++++++++-------
 src/gui/dialogPersonPopup.cs    |   43 +++++++++++++++++++++++++++++
 src/json.cs                     |   57 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+), 10 deletions(-)
---
diff --git a/glade/dialog_person_popup.glade b/glade/dialog_person_popup.glade
index 53d1c3f..9c26160 100644
--- a/glade/dialog_person_popup.glade
+++ b/glade/dialog_person_popup.glade
@@ -107,19 +107,44 @@
                     <property name="left_padding">10</property>
                     <property name="right_padding">10</property>
                     <child>
-                      <widget class="GtkScrolledWindow" id="scrolledwindow5">
+                      <widget class="GtkVBox" id="vbox1">
                         <property name="visible">True</property>
-                        <property name="can_focus">True</property>
-                        <property name="hscrollbar_policy">automatic</property>
-                        <property name="vscrollbar_policy">never</property>
+                        <property name="can_focus">False</property>
                         <child>
-                          <widget class="GtkTextView" id="textview_task">
-                            <property name="visible">True</property>
+                          <widget class="GtkScrolledWindow" id="scrolledwindow5">
                             <property name="can_focus">True</property>
-                            <property name="border_width">2</property>
-                            <property name="editable">False</property>
-                            <property name="cursor_visible">False</property>
+                            <property name="hscrollbar_policy">automatic</property>
+                            <property name="vscrollbar_policy">never</property>
+                            <child>
+                              <widget class="GtkTextView" id="textview_task">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="border_width">2</property>
+                                <property name="editable">False</property>
+                                <property name="cursor_visible">False</property>
+                              </widget>
+                            </child>
+                          </widget>
+                          <packing>
+                            <property name="expand">True</property>
+                            <property name="fill">True</property>
+                            <property name="position">0</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <widget class="GtkVBox" id="vbox_tasks">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <property name="spacing">10</property>
+                            <child>
+                              <placeholder/>
+                            </child>
                           </widget>
+                          <packing>
+                            <property name="expand">True</property>
+                            <property name="fill">True</property>
+                            <property name="position">1</property>
+                          </packing>
                         </child>
                       </widget>
                     </child>
@@ -129,7 +154,7 @@
                   <widget class="GtkLabel" id="label23">
                     <property name="visible">True</property>
                     <property name="can_focus">False</property>
-                    <property name="label" translatable="yes">Task</property>
+                    <property name="label">TODO Tasks</property>
                     <property name="use_markup">True</property>
                   </widget>
                   <packing>
diff --git a/src/gui/dialogPersonPopup.cs b/src/gui/dialogPersonPopup.cs
index f2f234a..8685de3 100644
--- a/src/gui/dialogPersonPopup.cs
+++ b/src/gui/dialogPersonPopup.cs
@@ -32,6 +32,10 @@ public class DialogPersonPopup
        [Widget] Gtk.Image image;
        [Widget] Gtk.Label label_rfid;
        [Widget] Gtk.TextView textview_task;
+       [Widget] Gtk.VBox vbox_tasks;
+
+       private List<Gtk.CheckButton> list_checks;
+       private List<int> list_tasks_id;
 
        public DialogPersonPopup (int personID, string name, string rfid, List<Task> tasks)
        {
@@ -67,6 +71,45 @@ public class DialogPersonPopup
                        tb.Text += t.ToString() + "\n";
 
                textview_task.Buffer = tb;
+
+               list_checks = new List<Gtk.CheckButton>();
+               list_tasks_id = new List<int>();
+               foreach(Task t in tasks)
+               {
+                       CheckButton check = new Gtk.CheckButton(t.ToString());
+                       check.Toggled += on_task_toggled;
+                       check.Active = true;
+                       vbox_tasks.Add(check);
+
+                       list_checks.Add(check);
+                       list_tasks_id.Add(t.Id);
+               }
+               vbox_tasks.ShowAll();
+       }
+
+       private void on_task_toggled(object o, EventArgs args)
+       {
+               CheckButton checkToggled = o as CheckButton;
+               if (o == null)
+                       return;
+
+               int count = 0;
+               foreach(Gtk.CheckButton check in list_checks)
+               {
+                       if(check == checkToggled)
+                       {
+                               LogB.Information("check toggled row: " + count.ToString());
+                               LogB.Information(check.Active.ToString());
+
+                               Json json = new Json();
+                               /*
+                                * pass negative check because on this dialog "active" means to do task
+                                * on server the boolean is called "done" and means the opposite
+                                */
+                               json.UpdateTask(list_tasks_id[count], Util.BoolToInt(! check.Active));
+                       }
+                       count ++;
+               }
        }
 
        public void on_button_close_clicked (object obj, EventArgs args)
diff --git a/src/json.cs b/src/json.cs
index f111d29..b0387a8 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -499,6 +499,63 @@ public class Json
                return list;
        }
 
+       public bool UpdateTask(int taskId, int done)
+       {
+               // Create a request using a URL that can receive a post.
+               WebRequest request = WebRequest.Create (serverUrl + "/updateTask");
+
+               // Set the Method property of the request to POST.
+               request.Method = "POST";
+
+               // Set the ContentType property of the WebRequest.
+               request.ContentType = "application/json; Charset=UTF-8"; //but this is not enough, see this 
line:
+
+               // Creates the json object
+               JsonObject json = new JsonObject();
+
+               json.Add("taskId", taskId);
+               json.Add("done", done);
+
+               // 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 202, CREATED)
+               Console.WriteLine (((HttpWebResponse)response).StatusDescription);
+
+               // Clean up the streams.
+               dataStream.Close ();
+               response.Close ();
+
+               this.ResultMessage = "Update task sent.";
+               return true;
+       }
+
        /*
        public bool UploadEncoderData()
        {


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