-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathInfoPanel.vala
More file actions
493 lines (396 loc) · 16.8 KB
/
Copy pathInfoPanel.vala
File metadata and controls
493 lines (396 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/*-
* Copyright (c) 2013-2020 elementary, Inc. (http://launchpad.net/maya)
*
* This program is free software: you can redistribute it and/or modify
* 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 <http://www.gnu.org/licenses/>.
*
* Authored by: Corentin Noël <corentin@elementaryos.org>
*/
public class Maya.View.EventEdition.InfoPanel : Gtk.Grid {
private Gtk.Entry title_entry;
private Granite.HyperTextView comment_textview;
private Granite.Widgets.DatePicker from_date_picker;
private Granite.Widgets.DatePicker to_date_picker;
private Gtk.Switch allday_switch;
private Granite.Widgets.TimePicker from_time_picker;
private Granite.Widgets.TimePicker to_time_picker;
private Gtk.Label timezone_label;
private Widgets.CalendarChooser calchooser;
private EventDialog parent_dialog;
public string title {
get { return title_entry.get_text (); }
set { title_entry.set_text (value); }
}
public DateTime from_date {
get { return from_date_picker.date; }
set { from_date_picker.date = value; }
}
public DateTime to_date {
get { return to_date_picker.date; }
set { to_date_picker.date = value; }
}
// TODO Also use all_day
public DateTime from_time {
get { return from_time_picker.time; }
set { from_time_picker.time = value; }
}
public DateTime to_time {
get { return to_time_picker.time; }
set { to_time_picker.time = value; }
}
public bool all_day {
get { return allday_switch.get_active (); }
set { allday_switch.set_active (value); }
}
public ICal.Timezone timezone { get; private set; }
public bool nl_parsing_enabled = false;
public signal void parse_event (string event_str);
public signal void valid_event (bool is_valid);
public InfoPanel (EventDialog parent_dialog) {
this.parent_dialog = parent_dialog;
margin_start = 12;
margin_end = 12;
column_spacing = 12;
var from_label = new Granite.HeaderLabel (_("From:"));
from_date_picker = make_date_picker ();
from_date_picker.notify["date"].connect (() => {on_date_modified (0);} );
from_time_picker = make_time_picker ();
from_time_picker.time_changed.connect (() => {on_time_modified (0);} );
allday_switch = new Gtk.Switch () {
valign = CENTER
};
var allday_label = new Gtk.Label (_("All day:")) {
mnemonic_widget = allday_switch,
xalign = 1
};
allday_switch.notify["active"].connect (() => {
on_date_modified (1);
from_time_picker.sensitive = !allday_switch.get_active ();
to_time_picker.sensitive = !allday_switch.get_active ();
});
var to_label = new Granite.HeaderLabel (_("To:"));
to_date_picker = make_date_picker ();
to_date_picker.notify["date"].connect (() => {on_date_modified (1);} );
to_time_picker = make_time_picker ();
to_time_picker.time_changed.connect (() => {on_time_modified (1);} );
var timezone_header = new Granite.HeaderLabel (_("Time zone:"));
timezone_label = new Gtk.Label (null) {
halign = START
};
title_entry = new Gtk.Entry () {
placeholder_text = _("Name of Event")
};
var title_label = new Granite.HeaderLabel (_("Title:")) {
mnemonic_widget = title_entry
};
title_entry.changed.connect (on_title_entry_modified);
title_entry.activate.connect (() => {
parse_event (title_entry.get_text ());
title_entry.secondary_icon_name = null;
title_entry.secondary_icon_tooltip_text = null;
});
title_entry.changed.connect (() => {
if (title_entry.get_text ().length > 0 && nl_parsing_enabled) {
title_entry.secondary_icon_name = "go-jump-symbolic";
title_entry.secondary_icon_tooltip_text = _("Press enter to parse event");
}
else {
title_entry.secondary_icon_name = null;
title_entry.secondary_icon_tooltip_text = null;
}
});
realize.connect (() => {
Idle.add (() => {
title_entry.grab_focus ();
return false;
});
});
calchooser = new Widgets.CalendarChooser () {
margin_bottom = 6
};
var popover = new Gtk.Popover (null) {
child = calchooser,
width_request = 310
};
var current_calendar_grid = new CalendarRow (calchooser.current_source) {
halign = START,
hexpand = true
};
var button_box = new Gtk.Box (HORIZONTAL, 6);
button_box.add (current_calendar_grid);
button_box.add (new Gtk.Image.from_icon_name ("pan-down-symbolic", Gtk.IconSize.MENU));
var calendar_button = new Gtk.MenuButton () {
child = button_box,
popover = popover
};
var calendar_label = new Granite.HeaderLabel (_("Calendar:")) {
mnemonic_widget = calendar_button
};
// Select the first calendar we can find, if none is default
if (parent_dialog.source == null) {
parent_dialog.source = calchooser.current_source;
}
comment_textview = new Granite.HyperTextView () {
accepts_tab = false,
wrap_mode = WORD_CHAR,
top_margin = 3,
right_margin = 3,
bottom_margin = 3,
left_margin = 3,
};
var comment_label = new Granite.HeaderLabel (_("Comments:")) {
mnemonic_widget = comment_textview
};
var scrolled = new Gtk.ScrolledWindow (null, null) {
child = comment_textview,
height_request = 100,
hexpand = true,
vexpand = true,
hscrollbar_policy = NEVER
};
var frame = new Gtk.Frame (null) {
child = scrolled
};
// Row: title & calendar
attach (title_label, 0, 0, 1, 1);
attach (title_entry, 0, 1, 1, 1);
if (calchooser.sources.length () > 1) {
attach (calendar_label, 1, 0, 4, 1);
attach (calendar_button, 1, 1, 4, 1);
}
// Row: start date/time
attach (from_label, 0, 2, 4, 1);
attach (from_date_picker, 0, 3, 1, 1);
attach (from_time_picker, 1, 3, 1, 1);
attach (allday_label, 2, 3, 1, 1);
attach (allday_switch, 3, 3, 1, 1);
// Row: end date/time
attach (to_label, 0, 4, 2, 1);
attach (to_date_picker, 0, 5, 1, 1);
attach (to_time_picker, 1, 5, 1, 1);
// Row: timezone
attach (timezone_header, 0, 6, 1, 1);
attach (timezone_label, 0, 7, 1, 1);
// Row: comment
attach (comment_label, 0, 8, 4, 1);
attach (frame, 0, 9, 5, 1);
load ();
calchooser.bind_property ("current-source", current_calendar_grid, "source", SYNC_CREATE);
calchooser.notify["current-source"].connect ((s, p) => {
calendar_button.tooltip_text = "%s—%s".printf (current_calendar_grid.label, current_calendar_grid.location);
popover.popdown ();
});
popover.unmap.connect (() => {
calchooser.clear_search_entry ();
});
}
/**
* Save the values in the dialog into the component.
*/
public void save () {
unowned ICal.Component comp = parent_dialog.ecal.get_icalcomponent ();
// Save the title
comp.set_summary (title_entry.text);
// Save the time
if (allday_switch.get_active () == true) {
var dt_start = Calendar.Util.datetimes_to_icaltime (from_date_picker.date, null);
var dt_end = Calendar.Util.datetimes_to_icaltime (to_date_picker.date.add_days (1), null);
comp.set_dtstart (dt_start);
comp.set_dtend (dt_end);
} else {
var dt_start = Calendar.Util.datetimes_to_icaltime (from_date_picker.date, from_time_picker.time, timezone);
var dt_end = Calendar.Util.datetimes_to_icaltime (to_date_picker.date, to_time_picker.time, timezone);
comp.set_dtstart (dt_start);
comp.set_dtend (dt_end);
}
// First, clear the comments
int count = comp.count_properties (ICal.PropertyKind.DESCRIPTION_PROPERTY);
for (int i = 0; i < count; i++) {
ICal.Property remove_prop;
remove_prop = comp.get_first_property (ICal.PropertyKind.COMMENT_PROPERTY);
comp.remove_property (remove_prop);
}
// Add the comment
var property = new ICal.Property (ICal.PropertyKind.DESCRIPTION_PROPERTY);
property.set_comment (comment_textview.get_buffer ().text);
comp.add_property (property);
// Save the selected source
parent_dialog.source = calchooser.current_source;
}
//--- Helpers ---//
/**
* Populate the dialog's widgets with the component's values.
*/
void load () {
if (parent_dialog.ecal != null) {
unowned ICal.Component comp = parent_dialog.ecal.get_icalcomponent ();
// Load the title
string summary = comp.get_summary ();
if (summary != null) {
title_entry.text = summary;
}
DateTime from_date, to_date;
Calendar.Util.icalcomponent_get_datetimes_for_display (comp, out from_date, out to_date);
// Load the timezone
timezone = comp.get_dtstart ().get_timezone ();
// If end time zone is different from start, convert to same as start.
// This is permanent once the event is saved, but the actual time is
// unaffected (only its display).
if (to_date.get_utc_offset () != from_date.get_utc_offset ()) {
to_date = to_date.to_timezone (from_date.get_timezone ());
}
from_date_picker.date = from_date;
from_time_picker.time = from_date;
parent_dialog.date_time = from_date;
// Is this all day
bool allday = Calendar.Util.datetime_is_all_day (from_date, to_date);
to_date_picker.date = to_date;
to_time_picker.time = to_date;
// Load the allday_switch
if (allday) {
allday_switch.set_active (true);
from_time_picker.sensitive = false;
to_time_picker.sensitive = false;
}
ICal.Property property;
property = comp.get_first_property (ICal.PropertyKind.DESCRIPTION_PROPERTY);
if (property != null) {
Gtk.TextBuffer buffer = new Gtk.TextBuffer (null);
buffer.text = property.get_comment ();
comment_textview.set_buffer (buffer);
}
// Load the source
calchooser.current_source = parent_dialog.original_source;
} else {
parent_dialog.ecal = new ECal.Component ();
parent_dialog.ecal.set_new_vtype (ECal.ComponentVType.EVENT);
var time = new DateTime.now_local ();
var minutes = time.get_minute ();
var now_before_2300 = (time.get_hour () < 23);
/* Set convenient start time but do not change day */
from_date_picker.date = parent_dialog.date_time;
if (now_before_2300) { /* Default start time to next whole hour*/
time = time.add_minutes (60 - minutes);
} else { /* Default start time 23.00 (11 PM)*/
time = time.add_minutes (-minutes);
}
from_time_picker.time = time;
/* Default event duration of one hour, changing day if required */
to_time_picker.time = time.add_hours (1);
if (now_before_2300) {
to_date_picker.date = parent_dialog.date_time;
} else { /* Default end time is 00.00 (12.00 AM) next morning*/
to_date_picker.date = parent_dialog.date_time.add_days (1);
}
// Use local time zone
timezone = Calendar.TimeManager.get_default ().system_timezone;
// Load the source
calchooser.current_source = parent_dialog.source;
}
// Populate timezone label with the timezone that was decided
timezone_label.label = timezone.get_display_name ();
}
Granite.Widgets.DatePicker make_date_picker () {
var format = Granite.DateTime.get_default_date_format (false, true, true);
var date_picker = new Granite.Widgets.DatePicker.with_format (format);
date_picker.width_request = 200;
return date_picker;
}
Granite.Widgets.TimePicker make_time_picker () {
var time_picker = new Granite.Widgets.TimePicker ();
time_picker.width_request = 120;
return time_picker;
}
void on_title_entry_modified () {
update_create_sensitivity ();
}
void on_date_modified (int index) {
parent_dialog.date_time = from_date_picker.date;
var start_date = from_date_picker.date;
var end_date = to_date_picker.date;
switch (index) {
case 0:
if (start_date.get_year () == end_date.get_year ()) {
if (start_date.get_day_of_year () >= end_date.get_day_of_year ()) {
to_date_picker.date = from_date_picker.date;
}
}
break;
case 1:
if (start_date.get_year () == end_date.get_year ()) {
if (end_date.get_day_of_year () < start_date.get_day_of_year ())
from_date_picker.date = to_date_picker.date;
}
break;
}
update_create_sensitivity ();
}
void on_time_modified (int index) {
var start_date = from_date_picker.date;
var end_date = to_date_picker.date;
var start_time = from_time_picker.time;
var end_time = to_time_picker.time;
switch (index) {
case 0:
if (start_date.get_year () == end_date.get_year ()) {
if (start_date.get_day_of_year () == end_date.get_day_of_year ()) {
if (start_time.get_hour () > end_time.get_hour ()) {
to_time_picker.time = from_time_picker.time.add_hours (1);
}
if ((start_time.get_hour () == end_time.get_hour ()) && (start_time.get_minute () >= end_time.get_minute ())) {
to_time_picker.time = from_time_picker.time.add_hours (1);
}
if (start_time.get_hour () >= 23) {
to_date_picker.date = from_date_picker.date.add_days (1);
}
}
}
break;
case 1:
break;
}
update_create_sensitivity ();
}
void update_create_sensitivity () {
valid_event (is_valid_event ());
}
bool is_valid_event () {
return title_entry.text != "" && is_valid_dates ();
}
bool is_valid_dates () {
var start_date = from_date_picker.date;
var end_date = to_date_picker.date;
var start_time = from_time_picker.time;
var end_time = to_time_picker.time;
if (start_date.get_year () == end_date.get_year ()) {
// Same year, compare dates.
if (start_date.get_day_of_year () == end_date.get_day_of_year ()) {
// Same date, compare times.
// If it's all day, just return ok
if (allday_switch.get_active ())
return true;
if (start_time.get_hour () == end_time.get_hour ()) {
// Same hour, compare minutes
return start_time.get_minute () <= end_time.get_minute ();
}
// Different hour, start should be smaller
return start_time.get_hour () < end_time.get_hour ();
}
// Same year but different day, start should be smaller.
return start_date.get_day_of_year () < end_date.get_day_of_year ();
}
// Different years, start should be smaller.
return start_date.get_year () < end_date.get_year ();
}
}