Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions core/Model/CalendarModel.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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<E.CalComponent> events);
public signal void events_updated (E.Source source, Gee.Collection<E.CalComponent> events);
Expand Down Expand Up @@ -265,6 +262,42 @@ public class Maya.Model.CalendarModel : Object {
return events;
}

private static int search_rid (E.CalComponent a, string? rid) {

@jeremypw jeremypw Jun 29, 2019

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would normally be named compare_id or similar. DId you mean to name the following "get_event ()" this? It would make more sense.

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) {
Comment thread
jeremypw marked this conversation as resolved.
Outdated
List<weak E.CalClient> clients = source_client.get_values ();
foreach (unowned E.CalClient client in clients) {
GLib.SList<E.CalComponent> ecalcomps;
try {
client.get_objects_for_uid_sync (uid, out ecalcomps, null);
if (ecalcomps.length () > 0) {
unowned GLib.SList<E.CalComponent> results = ecalcomps.search<string> (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 () {
Expand Down
41 changes: 27 additions & 14 deletions src/Grid/EventButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
46 changes: 30 additions & 16 deletions src/Grid/GridDay.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 (() => {
Expand All @@ -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) {
Expand Down