1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/*-
3 * Copyright (c) 2011-2015 Maya Developers (http://launchpad.net/maya)
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Authored by: Jaap Broekhuizen
19 */
20
21namespace Maya.View.Widgets {
22
23    public class ContractorMenuItem : Gtk.MenuItem {
24        private Granite.Services.Contract contract;
25
26        public ContractorMenuItem (Granite.Services.Contract cont) {
27            this.contract = cont;
28
29            label = cont.get_display_name ();
30        }
31
32        public override void activate () {
33            /* creates a .ics file */
34            Util.save_temp_selected_calendars ();
35
36            string file_path = GLib.Environment.get_tmp_dir () + "/calendar.ics";
37            File cal_file = File.new_for_path (file_path);
38
39            try {
40                contract.execute_with_file (cal_file);
41            } catch (Error err) {
42                warning (err.message);
43            }
44        }
45    }
46
47    public class ContractorButtonWithMenu : Gtk.MenuButton {
48
49        private Gtk.FileChooserNative filechooser;
50
51        public ContractorButtonWithMenu (string tooltiptext) {
52            Object (
53                image: new Gtk.Image.from_icon_name ("document-export", Gtk.IconSize.LARGE_TOOLBAR),
54                tooltip_text: tooltiptext
55            );
56
57            var menu = new Gtk.Menu ();
58
59            try {
60                var contracts = Granite.Services.ContractorProxy.get_contracts_by_mime ("text/calender");
61
62                for (int i = 0; i < contracts.size; i++) {
63                    var contract = contracts.get (i);
64                    Gtk.MenuItem menu_item;
65
66                    menu_item = new ContractorMenuItem (contract);
67                    menu.append (menu_item);
68                }
69            } catch (GLib.Error error) {
70                critical (error.message);
71            }
72            Gtk.MenuItem item = new Gtk.MenuItem.with_label (_("Export Calendar…"));
73            item.activate.connect (savecal);
74            menu.append (item);
75            menu.show_all ();
76            popup = menu;
77        }
78
79        private void savecal () {
80            /* creates a .ics file */
81            Util.save_temp_selected_calendars ();
82
83            var filter = new Gtk.FileFilter ();
84            filter.add_mime_type ("text/calendar");
85
86            filechooser = new Gtk.FileChooserNative (
87                _("Export Calendar…"),
88                null,
89                Gtk.FileChooserAction.SAVE,
90                _("Save"),
91                _("Cancel")
92            );
93            filechooser.do_overwrite_confirmation = true;
94            filechooser.filter = filter;
95            filechooser.set_current_name (_("calendar.ics"));
96
97            if (filechooser.run () == Gtk.ResponseType.ACCEPT) {
98                var destination = filechooser.get_filename ();
99                if (destination == null) {
100                    destination = filechooser.get_current_folder ();
101                } else if (!destination.has_suffix (".ics")) {
102                    destination += ".ics";
103                }
104                try {
105                    GLib.Process.spawn_command_line_async ("mv " + GLib.Environment.get_tmp_dir () + "/calendar.ics " + destination);
106                } catch (SpawnError e) {
107                    warning (e.message);
108                }
109            }
110
111            filechooser.destroy ();
112        }
113    }
114
115}
116