From e364d6fac3495064a6868a28ed7aab2a51ee0053 Mon Sep 17 00:00:00 2001
From: gyan000 <gyan000@ijaz.fr>
Date: Fri, 23 Nov 2018 21:43:25 +0100
Subject: [PATCH] GH-2 - delete contract files at startup.

And some cleanup.
---
 data/dbus-send.sh                             | 19 -------
 src/EOSConnect/Contractor.vala                | 49 +++++++++++++------
 src/EOSConnect/EOSConnect.vala                |  1 +
 .../Plugin/Popover/ShareSettings.vala         |  2 -
 src/MConnect/DeviceManager.vala               | 44 ++++++++---------
 src/MConnect/DownloadTransfer.vala            |  3 +-
 src/MConnect/NotificationMonitor.vala         |  2 +-
 src/MConnect/Packet.vala                      |  5 +-
 src/MConnect/ShareHandlerProxy.vala           | 25 +++++-----
 src/MConnect/TelephonyHandler.vala            | 32 ++++++------
 src/MConnect/UploadTransfer.vala              |  3 +-
 11 files changed, 94 insertions(+), 91 deletions(-)
 delete mode 100644 data/dbus-send.sh

diff --git a/data/dbus-send.sh b/data/dbus-send.sh
deleted file mode 100644
index 564ae29..0000000
--- a/data/dbus-send.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-dbus-send --print-reply --dest=com.github.gyan000.eos-connect               \
-          /com/github/gyan000/eosconnect/share                              \
-          com.github.gyan000.eosconnect.Share.Files                         \
-          string:'fffefc6422d1dda0'                                         \
-          string:'/home/gyan000e/send-to.jpeg /home/gyan000e/k-yuko-contacts.csv'
-
-
-
-
-
-
-dbus-send --dest=org.freedesktop.ExampleName               \
-                /org/freedesktop/sample/object/name              \
-                org.freedesktop.ExampleInterface.ExampleMethod   \
-                int32:47 string:'hello world' double:65.32       \
-                array:string:"1st item","next item","last item"  \
-                dict:string:int32:"one",1,"two",2,"three",3      \
-                variant:int32:-8                                 \
-                objpath:/org/freedesktop/sample/object/name
diff --git a/src/EOSConnect/Contractor.vala b/src/EOSConnect/Contractor.vala
index cc0d340..f572851 100644
--- a/src/EOSConnect/Contractor.vala
+++ b/src/EOSConnect/Contractor.vala
@@ -33,16 +33,33 @@ namespace EOSConnect {
             return Contractor.get_contract_dir ();
         }
 
+        public async static void clean_contractor_directory () {
+            try {
+                File directory = File.new_for_path (get_contract_dir ());
+                FileEnumerator enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
+                FileInfo file_info, iterated_file_info;
+                File iterated_file;
+                while ((file_info = enumerator.next_file ()) != null) {
+                    enumerator.iterate (out iterated_file_info, out iterated_file);
+                    debug ("Deleting %s", iterated_file_info.get_name ());
+                    iterated_file.delete ();
+                }
+            } catch (Error e) {
+        		warning ("Error: %s\n", e.message);
+        	}
+        }
+
         public static void create_contract(Device device) {
-            var contract_file = Path.build_filename (Contractor.create_if_not_exists_contract_dir (), device.id + ".contract");
+            try {
+                var contract_file = Path.build_filename (Contractor.create_if_not_exists_contract_dir (), device.id + ".contract");
+
+                debug ("Creating contract : %s", contract_file);
+                File file = File.new_for_path (contract_file);
+                if (file.query_exists () == true) {
+                    file.delete ();
+                }
 
-            debug ("Creating contract : %s", contract_file);
-            File file = File.new_for_path (contract_file);
-            if (file.query_exists () == true) {
-                file.delete ();
-            }
 
-        	try {
         		FileOutputStream os = file.create (FileCreateFlags.PRIVATE);
                 string str_name="Name=Send to " + device.custom_name + "\n";
                 string str_desc="Description=Send this file to  " + device.custom_name + "\n";
@@ -57,17 +74,21 @@ namespace EOSConnect {
                 os.write ("MimeType=!inode;\n".data);
                 os.write (str_command.data);
         	} catch (Error e) {
-        		warning ("Unable to create contract file: %s\n", e.message);
+        		warning ("Error: %s\n", e.message);
         	}
         }
 
         public static void destroy_contract(Device device) {
-            var contract_file = Path.build_filename (Contractor.create_if_not_exists_contract_dir (), device.id + ".contract");
-            File file = File.new_for_path (contract_file);
-            if (file.query_exists ()) {
-                debug ("Deleting contract : %s", contract_file);
-                file.delete ();
-            }
+            try {
+                var contract_file = Path.build_filename (Contractor.create_if_not_exists_contract_dir (), device.id + ".contract");
+                File file = File.new_for_path (contract_file);
+                if (file.query_exists ()) {
+                    debug ("Deleting contract : %s", contract_file);
+                    file.delete ();
+                }
+            } catch (Error e) {
+        		warning ("Error: %s\n", e.message);
+        	}
         }
     }
 }
diff --git a/src/EOSConnect/EOSConnect.vala b/src/EOSConnect/EOSConnect.vala
index 8500ac6..6b1c04b 100644
--- a/src/EOSConnect/EOSConnect.vala
+++ b/src/EOSConnect/EOSConnect.vala
@@ -50,6 +50,7 @@ namespace EOSConnect {
 
         public override void activate ()
         {
+            Contractor.clean_contractor_directory.begin ();
             MConnectThread mconnect_thread = new MConnectThread (this, devices_map, main_settings);
 
             launcher_entry = LauncherEntry.get_for_desktop_id (GSETTINGS_SCHEMA_ID + ".desktop");
diff --git a/src/EOSConnect/Plugin/Popover/ShareSettings.vala b/src/EOSConnect/Plugin/Popover/ShareSettings.vala
index 26fbf9c..d9615ff 100644
--- a/src/EOSConnect/Plugin/Popover/ShareSettings.vala
+++ b/src/EOSConnect/Plugin/Popover/ShareSettings.vala
@@ -43,8 +43,6 @@ namespace EOSConnect.Plugin.Popover {
 
             var file_chooser = new Gtk.FileChooserButton (_("Select default download directory"),
                                                           Gtk.FileChooserAction.SELECT_FOLDER);
-            file_chooser.margin_right = 10;
-
             var directory = device.settings.get_string ("kdeconnect-share-directory");
             if (directory.length > 5) {
                 file_chooser.set_uri (directory);
diff --git a/src/MConnect/DeviceManager.vala b/src/MConnect/DeviceManager.vala
index 4fe537f..7de7246 100644
--- a/src/MConnect/DeviceManager.vala
+++ b/src/MConnect/DeviceManager.vala
@@ -176,28 +176,28 @@ namespace MConnect {
         }
 
         private void on_local_notification_closed (Notification notification) {
-            try {
-
-                print ("\n>>%S\n", notification.id);
-
-                // var notification = new Notification.from_message (message, id);
-                // Core.instance ().application.withdraw_notification (notification_id);
-
-                // if (!notification.get_is_valid () || notification.app_name in Notification.EXCEPTIONS) {
-                //     return;
-                // }
-                //
-                // foreach (Device device in Core.instance ().devices_map.values) {
-                //     if (device.has_capability_handler (NotificationHandler.NOTIFICATION)) {
-                //         NotificationHandler notification_handler = (NotificationHandler)device.get_path_capability_handler(
-                //             NotificationHandler.NOTIFICATION);
-                //
-                //         notification_handler.send_notification (device, message, id);
-                //     }
-                // }
-            } catch (Error e) {
-                warning ("Error: %s", e.message);
-            }
+            //try {
+
+            //    print ("\n>>%S\n", notification.id);
+
+            //    // var notification = new Notification.from_message (message, id);
+            //    // Core.instance ().application.withdraw_notification (notification_id);
+
+            //    // if (!notification.get_is_valid () || notification.app_name in Notification.EXCEPTIONS) {
+            //    //     return;
+            //    // }
+            //    //
+            //    // foreach (Device device in Core.instance ().devices_map.values) {
+            //    //     if (device.has_capability_handler (NotificationHandler.NOTIFICATION)) {
+            //    //         NotificationHandler notification_handler = (NotificationHandler)device.get_path_capability_handler(
+            //    //             NotificationHandler.NOTIFICATION);
+            //    //
+            //    //         notification_handler.send_notification (device, message, id);
+            //    //     }
+            //    // }
+            //} catch (Error e) {
+            //    warning ("Error: %s", e.message);
+            //}
         }
 
         /**
diff --git a/src/MConnect/DownloadTransfer.vala b/src/MConnect/DownloadTransfer.vala
index dce7ff2..30bd5ac 100644
--- a/src/MConnect/DownloadTransfer.vala
+++ b/src/MConnect/DownloadTransfer.vala
@@ -77,7 +77,8 @@ namespace MConnect {
                                                             Utils.TlsConnectionMode.CLIENT);
 
                 debug ("Attempt TLS handshake.");
-                var tls_res = yield this.tls_connection.handshake_async ();
+                //var tls_res = yield this.tls_connection.handshake_async ();
+                yield this.tls_connection.handshake_async ();
 
                 debug ("TLS handshake complete.");
             } catch (Error e) {
diff --git a/src/MConnect/NotificationMonitor.vala b/src/MConnect/NotificationMonitor.vala
index f764880..0462a4e 100644
--- a/src/MConnect/NotificationMonitor.vala
+++ b/src/MConnect/NotificationMonitor.vala
@@ -121,7 +121,7 @@ namespace MConnect {
                         return message;
                     }
 
-                    uint32 id = child.get_uint32 ();
+                    //uint32 id = child.get_uint32 ();
                     Idle.add (() => {
                         return false;
                     });
diff --git a/src/MConnect/Packet.vala b/src/MConnect/Packet.vala
index 8a04f3d..d17e3d8 100644
--- a/src/MConnect/Packet.vala
+++ b/src/MConnect/Packet.vala
@@ -40,6 +40,7 @@ namespace MConnect {
             public uint64 size;
             public uint port;
         }
+
         public string pkt_type {
             get; private set; default = "";
         }
@@ -49,9 +50,7 @@ namespace MConnect {
         public Json.Object body {
             get; private set; default = null;
         }
-        public Packet.Payload ? payload {
-            get; set; default = null;
-        }
+        public Packet.Payload ? payload;
 
         public Packet (string type, Json.Object body, int64 id = 0) {
             this.pkt_type = type;
diff --git a/src/MConnect/ShareHandlerProxy.vala b/src/MConnect/ShareHandlerProxy.vala
index 09f3aac..f64f171 100644
--- a/src/MConnect/ShareHandlerProxy.vala
+++ b/src/MConnect/ShareHandlerProxy.vala
@@ -30,22 +30,23 @@ class ShareHandlerProxy : Object {
     // }
 
     public void files (string device_id, string pathnames) throws IOError {
+        try {
+            foreach (var entry in Core.instance ().devices_map.entries) {
 
-        bool device_available = false;
+                if (entry.value.id == device_id &&
+                    entry.value.is_active == true) {
+                    SList<string> files_to_send = new SList<string> ();
 
-        foreach (var entry in Core.instance ().devices_map.entries) {
+                    debug ("Device ID: %s (%s)", device_id, entry.value.custom_name);
+                	foreach (unowned string pathname in pathnames.split (" ")) {
+                        files_to_send.append (File.new_for_path (pathname).get_uri ());
+                	}
 
-            if (entry.value.id == device_id &&
-                entry.value.is_active == true) {
-                SList<string> files_to_send = new SList<string> ();
-
-                debug ("Device ID: %s (%s)", device_id, entry.value.custom_name);
-            	foreach (unowned string pathname in pathnames.split (" ")) {
-                    files_to_send.append (File.new_for_path (pathname).get_uri ());
-            	}
-
-                ((Share)entry.value.get_plugin (ShareHandler.SHARE_PKT)).send_files (entry.value, files_to_send);
+                    ((Share)entry.value.get_plugin (ShareHandler.SHARE_PKT)).send_files (entry.value, files_to_send);
+                }
             }
+        } catch (Error e) {
+            warning ("Error: %s", e.message);
         }
     }
 }
diff --git a/src/MConnect/TelephonyHandler.vala b/src/MConnect/TelephonyHandler.vala
index f073ee0..5b248df 100644
--- a/src/MConnect/TelephonyHandler.vala
+++ b/src/MConnect/TelephonyHandler.vala
@@ -158,22 +158,22 @@ namespace MConnect {
             // }
         }
 
-        public void handle_sms_notification (Device device, Packet pkt) {
-            debug ("Handling missed SMS from notifiation.");
-            // TOCHECK There's' a "time" field, but, does not seems to be a unix timestamp. And no mention in kdeconnect.
-            //DateTime glop = new DateTime.from_unix_local (pkt.body.get_string_member ("time").to_int());
-            //print (">> %s -> %s", pkt.body.get_string_member ("time").to_string (), glop.to_string ());
-            //return;
-            SMSStore.instance ().handle_missed_sms (
-                device,
-                new SMS (
-                    pkt.body.get_string_member ("text"),
-                    SMS.FROM_CONTACT,
-                    SMS.FROM_TYPE_NOTIFICATION,
-                    new DateTime.now_utc ()
-                ),
-                pkt.body.get_string_member ("title"));
-        }
+        //public void handle_sms_notification (Device device, Packet pkt) {
+        //    debug ("Handling missed SMS from notifiation.");
+        //    // TOCHECK There's' a "time" field, but, does not seems to be a unix timestamp. And no mention in kdeconnect.
+        //    //DateTime glop = new DateTime.from_unix_local (pkt.body.get_string_member ("time").to_int());
+        //    //print (">> %s -> %s", pkt.body.get_string_member ("time").to_string (), glop.to_string ());
+        //    //return;
+        //    SMSStore.instance ().handle_missed_sms (
+        //        device,
+        //        new SMS (
+        //            pkt.body.get_string_member ("text"),
+        //            SMS.FROM_CONTACT,
+        //            SMS.FROM_TYPE_NOTIFICATION,
+        //            new DateTime.now_utc ()
+        //        ),
+        //        pkt.body.get_string_member ("title"));
+        //}
 
         /**
          * make_sms_packet:
diff --git a/src/MConnect/UploadTransfer.vala b/src/MConnect/UploadTransfer.vala
index ff4ac30..a5d51f4 100644
--- a/src/MConnect/UploadTransfer.vala
+++ b/src/MConnect/UploadTransfer.vala
@@ -102,7 +102,8 @@ namespace MConnect {
                                                             Utils.TlsConnectionMode.SERVER);
 
                 debug ("Attempt TLS handshake.");
-                var tls_res = yield tls_connection.handshake_async ();
+                //var tls_res = yield tls_connection.handshake_async ();
+                yield tls_connection.handshake_async ();
                 debug ("TLS handshake complete.");
             } catch (Error e) {
                 var error = "TLS handshake failed: %s".printf (e.message);
-- 
GitLab