From 29b3212e6ea36b1d47a7954fe55a7dd51e23c8f8 Mon Sep 17 00:00:00 2001
From: gyan000 <gyan000@ijaz.fr>
Date: Sun, 18 Nov 2018 20:59:37 +0100
Subject: [PATCH] * create user contract file by device. * delete cotnract
 files when application close.

---
 data/send-to.contract                      |  5 ++
 src/EOSConnect/Contractor.vala             | 64 ++++++++++++++++++++++
 src/EOSConnect/EOSConnect.vala             |  5 ++
 src/EOSConnect/MainWindow.vala             |  1 -
 src/EOSConnect/Widgets/DeviceMenuitem.vala |  2 +
 src/meson.build                            |  1 +
 6 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 data/send-to.contract
 create mode 100644 src/EOSConnect/Contractor.vala

diff --git a/data/send-to.contract b/data/send-to.contract
new file mode 100644
index 0000000..eb70e9a
--- /dev/null
+++ b/data/send-to.contract
@@ -0,0 +1,5 @@
+[Contractor Entry]
+Name=Send to {DEVICE_NAME}
+Description=Send this file to {DEVICE_NAME}
+MimeType=!inode;
+Exec=dbus-send
diff --git a/src/EOSConnect/Contractor.vala b/src/EOSConnect/Contractor.vala
new file mode 100644
index 0000000..4dbc0c8
--- /dev/null
+++ b/src/EOSConnect/Contractor.vala
@@ -0,0 +1,64 @@
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * AUTHORS
+ * gyan000 <gyan000 (at] ijaz.fr>
+ */
+using MConnect;
+
+namespace EOSConnect {
+
+    public class Contractor {
+
+        public static string get_contract_dir () {
+            return Path.build_filename (Environment.get_home_dir (), ".local", "share", "contractor");
+        }
+
+        public static string create_if_not_exists_contract_dir () {
+            if (File.new_for_path(Contractor.get_contract_dir ()).query_exists () == false) {
+                DirUtils.create (Contractor.get_contract_dir (), 0700);
+            }
+
+            return Contractor.get_contract_dir ();
+        }
+
+        public static void create_contract(Device device) {
+            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);
+        	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";
+        		os.write ("[Contractor Entry]\n".data);
+                os.write (str_name.data);
+                os.write (str_desc.data);
+                os.write ("MimeType=!inode;\n".data);
+                os.write ("Exec=dbus-send\n".data);
+        	} catch (Error e) {
+        		warning ("Unable to create contract file: %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 ();
+            }
+        }
+    }
+}
diff --git a/src/EOSConnect/EOSConnect.vala b/src/EOSConnect/EOSConnect.vala
index d19a55b..92e783d 100644
--- a/src/EOSConnect/EOSConnect.vala
+++ b/src/EOSConnect/EOSConnect.vala
@@ -65,6 +65,9 @@ namespace EOSConnect {
             main_window.delete_event.connect ((event) => {
                 // When from launcher menu is clicked.
                 if (event.send_event == (int8)0) {
+                    foreach (var device_entry in devices_map.entries) {
+                        Contractor.destroy_contract (device_entry.value);
+                    }
                     main_window.destroy ();
                 } else { // When close button from the main window is clicked.
                     main_window.iconify ();
@@ -107,6 +110,8 @@ namespace EOSConnect {
                 error ("Cannot run without thread support.\n");
             }
 
+             message ("Report any issues/bugs you might find to https://github.com/gyan000/EOSConnect/issues");
+
             var application = new App ();
             return application.run (args);
         }
diff --git a/src/EOSConnect/MainWindow.vala b/src/EOSConnect/MainWindow.vala
index 2a04780..2f9723a 100644
--- a/src/EOSConnect/MainWindow.vala
+++ b/src/EOSConnect/MainWindow.vala
@@ -46,7 +46,6 @@ namespace EOSConnect {
             set_default_size (900, 600);
             set_size_request (750, 500);
         }
-
         construct {
 
             var theme_button = new Gtk.Button.from_icon_name ("object-inverse");
diff --git a/src/EOSConnect/Widgets/DeviceMenuitem.vala b/src/EOSConnect/Widgets/DeviceMenuitem.vala
index a75b21d..bca57a8 100644
--- a/src/EOSConnect/Widgets/DeviceMenuitem.vala
+++ b/src/EOSConnect/Widgets/DeviceMenuitem.vala
@@ -105,6 +105,7 @@ namespace EOSConnect.Widgets {
                         ((Share)device.get_plugin (ShareHandler.SHARE_PKT)).send_files (device);
                     });
                     child_append (sub_menu_item_share);
+                    EOSConnect.Contractor.create_contract (device);
                 }
 
                 sub_menu_item_share.property_set (Dbusmenu.MENUITEM_PROP_ICON_NAME, "send-to");
@@ -113,6 +114,7 @@ namespace EOSConnect.Widgets {
                 if (sub_menu_item_share != null) {
                     child_delete (sub_menu_item_share);
                     sub_menu_item_share = null;
+                    EOSConnect.Contractor.destroy_contract (device);
                 }
             }
         }
diff --git a/src/meson.build b/src/meson.build
index 72a28ac..2b85038 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -2,6 +2,7 @@ all_files = files(
     'EOSConnect/Config.vala',
     'EOSConnect/Contact.vala',
     'EOSConnect/ContactsInterface.vala',
+    'EOSConnect/Contractor.vala',
     'EOSConnect/EContacts.vala',
     'EOSConnect/EOSConnect.vala',
     'EOSConnect/DebugToolbar.vala',
-- 
GitLab