From 19b180b50b550d434f7879398017cd56c0d18fba Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Wed, 31 May 2017 21:50:01 +0200 Subject: [PATCH] share: POC file sharing --- Makefile.am | 1 + src/mconnect/packet.vala | 17 ++++++++- src/mconnect/packethandlers.vala | 2 ++ src/mconnect/share.vala | 62 ++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/mconnect/share.vala diff --git a/Makefile.am b/Makefile.am index bed9981..5e8cd7c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,6 +65,7 @@ mconnect_SOURCES = \ src/mconnect/battery.vala \ src/mconnect/telephony.vala \ src/mconnect/mousepad.vala \ + src/mconnect/share.vala \ src/mconnect/config.vala mconnect_LDADD = \ diff --git a/src/mconnect/packet.vala b/src/mconnect/packet.vala index 0eff6f4..d39de76 100644 --- a/src/mconnect/packet.vala +++ b/src/mconnect/packet.vala @@ -35,6 +35,8 @@ class Packet : GLib.Object { public string pkt_type { get; private set; default = ""; } public int64 id { get; private set; default = 0; } public Json.Object body { get; private set; default = null; } + public int64 payload_size {get; private set; default = 0; } + public Json.Object payload_transfer {get; private set; default = null;} public Packet(string type, Json.Object body, int64 id = 0) { this.pkt_type = type; @@ -69,7 +71,20 @@ class Packet : GLib.Object { debug("packet type: %s", type); - return new Packet(type, body, id); + // may have payload information + int64 payload_size = 0; + Json.Object payload = null; + if (root_obj.has_member("payloadSize")) { + payload_size = root_obj.get_int_member("payloadSize"); + debug("packet has paylod of size %lld", payload_size); + payload = root_obj.get_object_member("payloadTranferInfo"); + } + + var pkt = new Packet(type, body, id); + pkt.payload_size = payload_size; + pkt.payload_transfer = payload; + + return pkt; } catch (Error e) { message("failed to parse message: \'%s\', error: %s", data, e.message); diff --git a/src/mconnect/packethandlers.vala b/src/mconnect/packethandlers.vala index 3d81f0c..c3dd45b 100644 --- a/src/mconnect/packethandlers.vala +++ b/src/mconnect/packethandlers.vala @@ -46,11 +46,13 @@ class PacketHandlers : Object { var battery = BatteryHandler.instance(); var telephony = TelephonyHandler.instance(); var mousepad = MousepadHandler.instance(); + var fileshare = ShareHandler.instance(); hnd.append(notification); hnd.append(battery); hnd.append(telephony); hnd.append(mousepad); + hnd.append(fileshare); return hnd; } diff --git a/src/mconnect/share.vala b/src/mconnect/share.vala new file mode 100644 index 0000000..830c774 --- /dev/null +++ b/src/mconnect/share.vala @@ -0,0 +1,62 @@ +/* ex:ts=4:sw=4:sts=4:et */ +/* -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/** + * 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 + * Maciek Borzecki + */ + +class ShareHandler : Object, PacketHandlerInterface { + + private const string SHARE = "kdeconnect.share"; + + public string get_pkt_type() { + return SHARE; + } + + private ShareHandler() { + + } + + public static ShareHandler instance() { + return new ShareHandler(); + } + + public void use_device(Device dev) { + dev.message.connect((d, pkt) => { + debug("message signal"); + if (pkt.pkt_type == SHARE) { + debug("share packet"); + this.message(pkt); + } + }); + } + + public void message(Packet pkt) { + debug("got share packet"); + + if (pkt.payload_size == 0 || pkt.payload_transfer == null) { + warning("payload size %lld nor no transfer info?", pkt.payload_size); + return; + } + + string filename; + if (pkt.body.has_member("filename")) { + filename = pkt.body.get_string_member("filename"); + } else { + // TODO generate filename + } + } +} \ No newline at end of file