Skip to content

Commit 77d37f7

Browse files
committed
CalendarView: add contractor menu
1 parent 2889b00 commit 77d37f7

4 files changed

Lines changed: 90 additions & 118 deletions

File tree

po/POTFILES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ src/TodayEventMonitor.vala
4343
src/Widgets/AgendaEventRow.vala
4444
src/Widgets/CalendarChooser.vala
4545
src/Widgets/ConnectivityInfoBar.vala
46-
src/Widgets/ContractorButtonWithMenu.vala
4746
src/Widgets/DateSwitcher.vala
4847
src/Widgets/DateTimePicker.vala
4948
src/Widgets/DynamicSpinner.vala

src/Grid/CalendarView.vala

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class Maya.View.CalendarView : Gtk.Box {
2929
public signal void on_event_add (DateTime date);
3030
public signal void selection_changed (DateTime new_date);
3131

32+
private const string ACTION_GROUP_PREFIX = "calendar";
33+
private const string ACTION_PREFIX = ACTION_GROUP_PREFIX + ".";
34+
3235
public Gtk.SearchEntry search_bar;
3336
private Calendar.Widgets.DateSwitcher month_switcher;
3437
private Calendar.Widgets.DateSwitcher year_switcher;
@@ -57,6 +60,53 @@ public class Maya.View.CalendarView : Gtk.Box {
5760
}
5861

5962
construct {
63+
var export_action = new SimpleAction ("export", null);
64+
export_action.activate.connect (action_export);
65+
66+
var action_group = new SimpleActionGroup ();
67+
action_group.add_action (export_action);
68+
69+
var contractor_menu = new GLib.Menu ();
70+
71+
try {
72+
var contracts = Granite.Services.ContractorProxy.get_contracts_by_mime ("text/calender");
73+
74+
int i = 0;
75+
foreach (var contract in contracts) {
76+
var contract_action = new SimpleAction ("contract-%i".printf (i), null);
77+
contract_action.activate.connect (() => {
78+
/* creates a .ics file */
79+
Util.save_temp_selected_calendars ();
80+
81+
var file_path = GLib.Environment.get_tmp_dir () + "/calendar.ics";
82+
var cal_file = File.new_for_path (file_path);
83+
84+
try {
85+
contract.execute_with_file (cal_file);
86+
} catch (Error err) {
87+
warning (err.message);
88+
}
89+
});
90+
91+
action_group.add_action (contract_action);
92+
93+
contractor_menu.append (
94+
contract.get_display_name (),
95+
ACTION_PREFIX + "contract-%i".printf (i)
96+
);
97+
i++;
98+
}
99+
} catch (GLib.Error error) {
100+
critical (error.message);
101+
}
102+
103+
contractor_menu.append (
104+
_("Export Calendar…"),
105+
"calendar.export"
106+
);
107+
108+
insert_action_group (ACTION_GROUP_PREFIX, action_group);
109+
60110
selected_date = Maya.Application.get_selected_datetime ();
61111

62112
var error_label = new Gtk.Label (null);
@@ -93,7 +143,11 @@ public class Maya.View.CalendarView : Gtk.Box {
93143

94144
var spinner = new Maya.View.Widgets.DynamicSpinner ();
95145

96-
var contractor = new Maya.View.Widgets.ContractorButtonWithMenu (_("Export or Share the default Calendar"));
146+
var contractor = new Gtk.MenuButton () {
147+
image = new Gtk.Image.from_icon_name ("document-export", LARGE_TOOLBAR),
148+
popup = new Gtk.Menu.from_model (contractor_menu),
149+
tooltip_text = _("Export or Share the default Calendar")
150+
};
97151

98152
var source_popover = new Calendar.Widgets.SourcePopover ();
99153

@@ -171,6 +225,41 @@ public class Maya.View.CalendarView : Gtk.Box {
171225
});
172226
}
173227

228+
private void action_export () {
229+
/* creates a .ics file */
230+
Util.save_temp_selected_calendars ();
231+
232+
var filter = new Gtk.FileFilter ();
233+
filter.add_mime_type ("text/calendar");
234+
235+
var filechooser = new Gtk.FileChooserNative (
236+
_("Export Calendar…"),
237+
null,
238+
Gtk.FileChooserAction.SAVE,
239+
_("Save"),
240+
_("Cancel")
241+
);
242+
filechooser.do_overwrite_confirmation = true;
243+
filechooser.filter = filter;
244+
filechooser.set_current_name (_("calendar.ics"));
245+
246+
if (filechooser.run () == Gtk.ResponseType.ACCEPT) {
247+
var destination = filechooser.get_filename ();
248+
if (destination == null) {
249+
destination = filechooser.get_current_folder ();
250+
} else if (!destination.has_suffix (".ics")) {
251+
destination += ".ics";
252+
}
253+
try {
254+
GLib.Process.spawn_command_line_async ("mv " + GLib.Environment.get_tmp_dir () + "/calendar.ics " + destination);
255+
} catch (SpawnError e) {
256+
warning (e.message);
257+
}
258+
}
259+
260+
filechooser.destroy ();
261+
}
262+
174263
private void set_switcher_date (DateTime date) {
175264
month_switcher.text = date.format ("%OB");
176265
year_switcher.text = date.format ("%Y");

src/Widgets/ContractorButtonWithMenu.vala

Lines changed: 0 additions & 115 deletions
This file was deleted.

src/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ calendar_files = files(
2929
'Widgets/AgendaEventRow.vala',
3030
'Widgets/CalendarChooser.vala',
3131
'Widgets/CalendarRow.vala',
32-
'Widgets/ContractorButtonWithMenu.vala',
3332
'Widgets/DateSwitcher.vala',
3433
'Widgets/DateTimePicker.vala',
3534
'Widgets/DeleteDialog.vala',

0 commit comments

Comments
 (0)