1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/*-
3 * Copyright (c) 2011-2015 elementary LLC
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: Mario Guerriero <marioguerriero33@gmail.com>
19 *              pantor
20 */
21
22namespace Maya.Services {
23
24public class ParsedEvent : GLib.Object {
25
26    public string title;
27    public string location;
28    public string participants;
29    public DateTime from;
30    public DateTime to;
31    public bool? all_day;
32    public bool date_parsed;
33    public bool time_parsed;
34
35    public ParsedEvent (string _title = "", DateTime? _from = null, DateTime? _to = null, string _location = "", bool? _all_day = null, string _participants = "", bool _date_parsed = false, bool _time_parsed = false) {
36        this.title = _title;
37        this.location = _location;
38        this.participants = _participants;
39        this.from = _from;
40        this.to = _to;
41        this.all_day = _all_day;
42        this.date_parsed = _date_parsed;
43        this.time_parsed = _time_parsed;
44    }
45
46    public void set_length_to_minutes (int minutes) {
47        this.to = this.from.add_minutes (minutes);
48    }
49
50    public void set_length_to_hours (int hours) {
51        this.to = this.from.add_hours (hours);
52    }
53
54    public void set_length_to_days (int days) {
55        this.to = this.from.add_days (days);
56    }
57
58    public void set_length_to_weeks (int weeks) {
59        this.to = this.from.add_days (7 * weeks);
60    }
61
62    public void from_set_second (int second) {
63        this.from = this.from.add_seconds (second - this.from.get_second ());
64    }
65
66    public void to_set_second (int second) {
67        this.to = this.to.add_seconds (second - this.to.get_second ());
68    }
69
70    public void from_set_minute (int minute) {
71        this.from = this.from.add_minutes (minute - this.from.get_minute ());
72    }
73
74    public void to_set_minute (int minute) {
75        this.to = this.to.add_minutes (minute - this.to.get_minute ());
76    }
77
78    private int hour_from_half (int hour, string half) {
79        if (hour > 12) {
80            return hour;
81        }
82
83        if (half == "pm") {
84            return hour + 12;
85        }
86
87        if (half == "p") {
88            return hour + 12;
89        }
90
91        if (half == "") {
92            if (hour < 8) {
93                hour += 12;
94            }
95        }
96
97        return hour;
98    }
99
100    public void from_set_hour (int hour, string half = "") {
101        hour = hour_from_half (hour, half);
102        this.from = this.from.add_hours (hour - this.from.get_hour ());
103    }
104
105    public void to_set_hour (int hour, string half = "") {
106        hour = hour_from_half (hour, half);
107        this.to = this.to.add_hours (hour - this.to.get_hour ());
108    }
109
110    public void from_set_day (int day) {
111        if (day > 0) {
112            this.from = this.from.add_days (day - this.from.get_day_of_month ());
113        }
114    }
115
116    public void to_set_day (int day) {
117        if (day > 0) {
118            this.to = this.to.add_days (day - this.to.get_day_of_month ());
119        }
120    }
121
122    public void from_set_month (int month) {
123        if (month > 0) {
124            this.from = this.from.add_months (month - this.from.get_month ());
125        }
126    }
127
128    public void to_set_month (int month) {
129        if (month > 0) {
130            this.to = this.to.add_months (month - this.to.get_month ());
131        }
132    }
133
134    public void from_set_year (int year) {
135        if (year > 0) {
136            this.from = this.from.add_years (year - this.from.get_year ());
137        }
138    }
139
140    public void to_set_year (int year) {
141        if (year > 0) {
142            this.to = this.to.add_years (year - this.to.get_year ());
143        }
144    }
145
146    public void set_all_day () {
147        this.from = this.from.add_hours (-this.from.get_hour () ).add_minutes (-this.from.get_minute ());
148        this.to = this.to.add_hours (-this.to.get_hour ()).add_minutes (-this.to.get_minute ());
149        this.all_day = true;
150    }
151
152    public void unset_all_day () {
153        this.set_length_to_hours (1);
154        this.all_day = false;
155    }
156
157    public void set_one_entire_day () {
158        this.from = this.from.add_hours (-this.from.get_hour ()).add_minutes (-this.from.get_minute ());
159        this.to = this.from;
160        this.all_day = true;
161    }
162
163    public void if_elapsed_delay_to_next_day (DateTime simulated_dt) {
164        if (this.from.compare (simulated_dt) < 0) {
165            this.from = this.from.add_days (1);
166            this.to = this.to.add_days (1);
167        }
168    }
169
170    public void if_elapsed_delay_to_next_week (DateTime simulated_dt) {
171        if (this.from.compare (simulated_dt) < 0) {
172            this.from = this. from.add_days (7);
173            this.to = this.to.add_days (7);
174        }
175    }
176
177    public void if_elapsed_delay_to_next_month (DateTime simulated_dt) {
178        if (this.from.compare (simulated_dt) < 0) {
179            this.from = this.from.add_months (1);
180            this.to = this.to.add_months (1);
181        }
182    }
183
184    public void if_elapsed_delay_to_next_year (DateTime simulated_dt) {
185        if (this.from.compare (simulated_dt) < 0) {
186            this.from = this.from.add_years (1);
187            this.to = this.to.add_years (1);
188        }
189    }
190}
191
192}
193