From 290fbc185540b09c5837111725cbf3e21655e6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20No=C3=ABl?= Date: Wed, 19 Jun 2019 18:59:32 +0200 Subject: [PATCH 1/3] Make Drag-n-Drop events less hacky --- core/Model/CalendarModel.vala | 39 ++++++++++++++++++++++++++--- src/Grid/EventButton.vala | 41 ++++++++++++++++++++----------- src/Grid/GridDay.vala | 46 +++++++++++++++++++++++------------ 3 files changed, 93 insertions(+), 33 deletions(-) diff --git a/core/Model/CalendarModel.vala b/core/Model/CalendarModel.vala index 3e3b8cbd3..a7b2bb914 100644 --- a/core/Model/CalendarModel.vala +++ b/core/Model/CalendarModel.vala @@ -37,9 +37,6 @@ public class Maya.Model.CalendarModel : Object { /* The start of week, ie. Monday=1 or Sunday=7 */ public Settings.Weekday week_starts_on { get; set; default = Settings.Weekday.MONDAY; } - /* The event that is currently dragged */ - public E.CalComponent drag_component {get; set;} - /* Notifies when events are added, updated, or removed */ public signal void events_added (E.Source source, Gee.Collection events); public signal void events_updated (E.Source source, Gee.Collection events); @@ -265,6 +262,42 @@ public class Maya.Model.CalendarModel : Object { return events; } + private static int search_rid (E.CalComponent a, string? rid) { + string? a_rid = a.get_recurid_as_string (); + if (a_rid == null && rid == null) { + return 0; + } + + return GLib.strcmp (a_rid, rid); + } + + public E.CalComponent? get_event (string uid, string? rid) { + List clients = source_client.get_values (); + foreach (unowned E.CalClient client in clients) { + GLib.SList ecalcomps; + try { + client.get_objects_for_uid_sync (uid, out ecalcomps, null); + if (ecalcomps.length () > 0) { + unowned GLib.SList results = ecalcomps.search (rid, search_rid); + + if (results.length () > 0) { + unowned E.CalComponent comp = results.data; + comp.set_data ("source", client.source); + return comp; + } else { + unowned E.CalComponent comp = ecalcomps.data; + comp.set_data ("source", client.source); + return comp; + } + } + } catch (Error e) { + debug (e.message); + } + } + + return null; + } + //--- Helper Methods ---// private void compute_ranges () { diff --git a/src/Grid/EventButton.vala b/src/Grid/EventButton.vala index ccab90054..2cd0b38d5 100644 --- a/src/Grid/EventButton.vala +++ b/src/Grid/EventButton.vala @@ -79,22 +79,35 @@ public class Maya.View.EventButton : Gtk.Revealer { return true; }); - Gtk.TargetEntry dnd = {"binary/calendar", 0, 0}; - Gtk.TargetEntry dnd2 = {"text/uri-list", 0, 0}; - Gtk.drag_source_set (event_box, Gdk.ModifierType.BUTTON1_MASK, {dnd, dnd2}, Gdk.DragAction.MOVE); + const Gtk.TargetEntry dnd = {"text/x-io-elementary-calendar", 0, 0}; + const Gtk.TargetEntry dnd2 = {"text/uri-list", 0, 1}; + const Gtk.TargetEntry dnd3 = {"text/calendar", 0, 2}; + Gtk.drag_source_set (event_box, Gdk.ModifierType.BUTTON1_MASK, {dnd, dnd2, dnd3}, Gdk.DragAction.MOVE); event_box.drag_data_get.connect ( (ctx, sel, info, time) => { - Model.CalendarModel.get_default ().drag_component = comp; - unowned iCal.Component icalcomp = comp.get_icalcomponent (); - unowned string ical_str = icalcomp.as_ical_string (); - sel.set_text (ical_str, ical_str.length); - try { - var path = GLib.Path.build_filename (GLib.Environment.get_tmp_dir (), icalcomp.get_summary () + ".ics"); - var file = File.new_for_path (path); - if (file.replace_contents (ical_str.data, null, false, FileCreateFlags.PRIVATE, null)) - sel.set_uris ({file.get_uri ()}); - } catch (Error e) { - critical (e.message); + if (info == 0) { + var comp_id = comp.get_id (); + if (comp_id.rid != null) { + var data = "%s\n%s".printf (comp_id.uid, comp_id.rid); + sel.set (Gdk.Atom.intern_static_string ("text/x-io-elementary-calendar"), (int)sizeof(char), data.data); + } else { + sel.set (Gdk.Atom.intern_static_string ("text/x-io-elementary-calendar"), (int)sizeof(char), comp_id.uid.data); + } + } else if (info == 1) { + comp.commit_sequence (); + var ical_str = comp.get_as_string (); + try { + var path = GLib.Path.build_filename (GLib.Environment.get_tmp_dir (), comp.get_summary ().value + ".ics"); + var file = File.new_for_path (path); + if (file.replace_contents (ical_str.data, null, false, FileCreateFlags.PRIVATE, null)) + sel.set_uris ({file.get_uri ()}); + } catch (Error e) { + critical (e.message); + } + } else if (info == 2) { + comp.commit_sequence (); + var ical_str = comp.get_as_string (); + sel.set_text (ical_str, ical_str.length); } }); diff --git a/src/Grid/GridDay.vala b/src/Grid/GridDay.vala index 4144cff9f..e87316b05 100644 --- a/src/Grid/GridDay.vala +++ b/src/Grid/GridDay.vala @@ -89,7 +89,7 @@ public class Maya.View.GridDay : Gtk.EventBox { key_press_event.connect (on_key_press); scroll_event.connect ((event) => {return GesturesUtils.on_scroll_event (event);}); - Gtk.TargetEntry dnd = {"binary/calendar", 0, 0}; + const Gtk.TargetEntry dnd = {"text/x-io-elementary-calendar", 0, 0}; Gtk.drag_dest_set (this, Gtk.DestDefaults.MOTION, {dnd}, Gdk.DragAction.MOVE); this.notify["date"].connect (() => { @@ -105,22 +105,36 @@ public class Maya.View.GridDay : Gtk.EventBox { } public override void drag_data_received (Gdk.DragContext context, int x, int y, Gtk.SelectionData selection_data, uint info, uint time_) { - var calmodel = Model.CalendarModel.get_default (); - var comp = calmodel.drag_component; - unowned iCal.Component icalcomp = comp.get_icalcomponent (); - E.Source src = comp.get_data ("source"); - var start = icalcomp.get_dtstart (); - var end = icalcomp.get_dtend (); - var gap = date.get_day_of_month () - start.day; - start.day += gap; - - if (end.is_null_time () == 0) { - end.day += gap; - icalcomp.set_dtend (end); - } + string? compid = (string)selection_data.get_data (); + if (compid != null) { + string uid; + string? rid = null; + if ("\n" in compid) { + var parts = compid.split ("\n", 2); + uid = parts[0]; + rid = parts[1]; + } else { + uid = compid; + } + + var calmodel = Model.CalendarModel.get_default (); + E.CalComponent? comp = calmodel.get_event (uid, rid); - icalcomp.set_dtstart (start); - calmodel.update_event (src, comp, E.CalObjModType.ALL); + unowned iCal.Component icalcomp = comp.get_icalcomponent (); + E.Source src = comp.get_data ("source"); + var start = icalcomp.get_dtstart (); + var end = icalcomp.get_dtend (); + var gap = date.get_day_of_month () - start.day; + start.day += gap; + + if (end.is_null_time () == 0) { + end.day += gap; + icalcomp.set_dtend (end); + } + + icalcomp.set_dtstart (start); + calmodel.update_event (src, comp, E.CalObjModType.ALL); + } } public void add_event_button (EventButton button) { From 7e545564ed895881f15a9aab04e68d5bf4a48f3e Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 15 May 2020 18:27:47 +0100 Subject: [PATCH 2/3] Fix compilation --- core/Model/CalendarModel.vala | 16 ++++++++-------- src/Grid/GridDay.vala | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core/Model/CalendarModel.vala b/core/Model/CalendarModel.vala index 1d1341692..a5e72e131 100644 --- a/core/Model/CalendarModel.vala +++ b/core/Model/CalendarModel.vala @@ -289,7 +289,7 @@ public class Maya.Model.CalendarModel : Object { return events; } - private static int search_rid (E.CalComponent a, string? rid) { + private static int search_rid (ECal.Component a, string? rid) { string? a_rid = a.get_recurid_as_string (); if (a_rid == null && rid == null) { return 0; @@ -298,21 +298,21 @@ public class Maya.Model.CalendarModel : Object { return GLib.strcmp (a_rid, rid); } - public E.CalComponent? get_event (string uid, string? rid) { - List clients = source_client.get_values (); - foreach (unowned E.CalClient client in clients) { - GLib.SList ecalcomps; + public ECal.Component? get_event (string uid, string? rid) { + List clients = source_client.get_values (); + foreach (unowned ECal.Client client in clients) { + GLib.SList ecalcomps; try { client.get_objects_for_uid_sync (uid, out ecalcomps, null); if (ecalcomps.length () > 0) { - unowned GLib.SList results = ecalcomps.search (rid, search_rid); + unowned GLib.SList results = ecalcomps.search (rid, search_rid); if (results.length () > 0) { - unowned E.CalComponent comp = results.data; + unowned ECal.Component comp = results.data; comp.set_data ("source", client.source); return comp; } else { - unowned E.CalComponent comp = ecalcomps.data; + unowned ECal.Component comp = ecalcomps.data; comp.set_data ("source", client.source); return comp; } diff --git a/src/Grid/GridDay.vala b/src/Grid/GridDay.vala index e60955cd0..a946f5c8e 100644 --- a/src/Grid/GridDay.vala +++ b/src/Grid/GridDay.vala @@ -6,12 +6,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * 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, see . * @@ -125,22 +125,22 @@ public class Maya.View.GridDay : Gtk.EventBox { } var calmodel = Model.CalendarModel.get_default (); - E.CalComponent? comp = calmodel.get_event (uid, rid); + ECal.Component? comp = calmodel.get_event (uid, rid); - unowned iCal.Component icalcomp = comp.get_icalcomponent (); + unowned ICal.Component icalcomp = comp.get_icalcomponent (); E.Source src = comp.get_data ("source"); var start = icalcomp.get_dtstart (); var end = icalcomp.get_dtend (); var gap = date.get_day_of_month () - start.day; start.day += gap; - if (end.is_null_time () == 0) { + if (!end.is_null_time ()) { end.day += gap; icalcomp.set_dtend (end); } icalcomp.set_dtstart (start); - calmodel.update_event (src, comp, E.CalObjModType.ALL); + calmodel.update_event (src, comp, ECal.ObjModType.ALL); } } From 7e9d645bc313dd2e81a8f79c120dde9cb9c99efe Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 15 May 2020 18:34:36 +0100 Subject: [PATCH 3/3] Fix lint errors --- src/Grid/EventButton.vala | 18 ++++++++++-------- src/Grid/GridDay.vala | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Grid/EventButton.vala b/src/Grid/EventButton.vala index 323370e76..2cf25f3f7 100644 --- a/src/Grid/EventButton.vala +++ b/src/Grid/EventButton.vala @@ -5,12 +5,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * 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, see . * @@ -81,19 +81,21 @@ public class Maya.View.EventButton : Gtk.Revealer { return true; }); - const Gtk.TargetEntry dnd = {"text/x-io-elementary-calendar", 0, 0}; - const Gtk.TargetEntry dnd2 = {"text/uri-list", 0, 1}; - const Gtk.TargetEntry dnd3 = {"text/calendar", 0, 2}; - Gtk.drag_source_set (event_box, Gdk.ModifierType.BUTTON1_MASK, {dnd, dnd2, dnd3}, Gdk.DragAction.MOVE); + const Gtk.TargetEntry DND = {"text/x-io-elementary-calendar", 0, 0}; + const Gtk.TargetEntry DND2 = {"text/uri-list", 0, 1}; + const Gtk.TargetEntry DND3 = {"text/calendar", 0, 2}; + Gtk.drag_source_set (event_box, Gdk.ModifierType.BUTTON1_MASK, {DND, DND2, DND3}, Gdk.DragAction.MOVE); event_box.drag_data_get.connect ( (ctx, sel, info, time) => { if (info == 0) { var comp_id = comp.get_id (); if (comp_id.rid != null) { var data = "%s\n%s".printf (comp_id.uid, comp_id.rid); - sel.set (Gdk.Atom.intern_static_string ("text/x-io-elementary-calendar"), (int)sizeof(char), data.data); + sel.set (Gdk.Atom.intern_static_string ("text/x-io-elementary-calendar"), + (int)sizeof (char), data.data); } else { - sel.set (Gdk.Atom.intern_static_string ("text/x-io-elementary-calendar"), (int)sizeof(char), comp_id.uid.data); + sel.set (Gdk.Atom.intern_static_string ("text/x-io-elementary-calendar"), + (int)sizeof (char), comp_id.uid.data); } } else if (info == 1) { comp.commit_sequence (); diff --git a/src/Grid/GridDay.vala b/src/Grid/GridDay.vala index a946f5c8e..a1f31625d 100644 --- a/src/Grid/GridDay.vala +++ b/src/Grid/GridDay.vala @@ -96,8 +96,8 @@ public class Maya.View.GridDay : Gtk.EventBox { key_press_event.connect (on_key_press); scroll_event.connect ((event) => {return GesturesUtils.on_scroll_event (event);}); - const Gtk.TargetEntry dnd = {"text/x-io-elementary-calendar", 0, 0}; - Gtk.drag_dest_set (this, Gtk.DestDefaults.MOTION, {dnd}, Gdk.DragAction.MOVE); + const Gtk.TargetEntry DND = {"text/x-io-elementary-calendar", 0, 0}; + Gtk.drag_dest_set (this, Gtk.DestDefaults.MOTION, {DND}, Gdk.DragAction.MOVE); this.notify["date"].connect (() => { label.label = date.get_day_of_month ().to_string ();