-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCalendarView.vala
More file actions
388 lines (316 loc) · 13.2 KB
/
Copy pathCalendarView.vala
File metadata and controls
388 lines (316 loc) · 13.2 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
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2011-2026 elementary, Inc. (https://elementary.io)
*
* Authored by: Maxwell Barvian
* Corentin Noël <corentin@elementaryos.org>
*/
/**
* Represents the entire calendar, including the headers, the week labels and the grid.
*/
public class Maya.View.CalendarView : Gtk.Box {
/*
* Event emitted when the day is double clicked or the ENTER key is pressed.
*/
public signal void on_event_add (DateTime date);
public signal void selection_changed (DateTime new_date);
private const string ACTION_GROUP_PREFIX = "calendar";
private const string ACTION_PREFIX = ACTION_GROUP_PREFIX + ".";
public DateTime? selected_date { get; private set; }
public Gtk.SearchEntry search_bar { get; private set; }
public Hdy.HeaderBar header_bar { get; private set; }
private Calendar.Widgets.DateSwitcher month_switcher;
private Calendar.Widgets.DateSwitcher year_switcher;
private Grid days_grid;
private Gtk.EventControllerScroll scroll_controller;
private Gtk.Stack stack;
private WeekLabels weeks;
private static GLib.Settings settings;
static construct {
if (Application.wingpanel_settings != null) {
settings = Application.wingpanel_settings;
} else {
settings = Application.saved_state;
}
}
construct {
var export_action = new SimpleAction ("export", null);
export_action.activate.connect (action_export);
var action_group = new SimpleActionGroup ();
action_group.add_action (export_action);
insert_action_group (ACTION_GROUP_PREFIX, action_group);
selected_date = Maya.Application.get_selected_datetime ();
var error_label = new Gtk.Label (null);
error_label.show ();
var error_bar = new Gtk.InfoBar () {
message_type = Gtk.MessageType.ERROR,
revealed = false,
show_close_button = true
};
error_bar.get_content_area ().add (error_label);
var info_label = new Gtk.Label ("<b>%s</b> %s".printf (
_("Network Not Available."),
_("Connect to the Internet to see additional details and new events from online calendars.")
)) {
use_markup = true,
wrap = true
};
var info_bar = new Gtk.InfoBar () {
message_type = WARNING,
revealed = false
};
info_bar.get_content_area ().add (info_label);
info_bar.add_button (_("Network Settings…"), Gtk.ResponseType.ACCEPT);
var application_instance = ((Gtk.Application) GLib.Application.get_default ());
var button_today = new Gtk.Button.from_icon_name ("calendar-go-today", Gtk.IconSize.LARGE_TOOLBAR) {
action_name = Maya.MainWindow.ACTION_PREFIX + Maya.MainWindow.ACTION_SHOW_TODAY
};
button_today.tooltip_markup = Granite.markup_accel_tooltip (
application_instance.get_accels_for_action (button_today.action_name),
_("Go to today's date")
);
month_switcher = new Calendar.Widgets.DateSwitcher (10) {
valign = CENTER
};
year_switcher = new Calendar.Widgets.DateSwitcher (-1) {
valign = CENTER
};
var calmodel = Calendar.EventStore.get_default ();
var spinner = new Maya.View.Widgets.DynamicSpinner ();
var source_popover = new Calendar.Widgets.SourcePopover ();
var menu_button = new Gtk.MenuButton () {
image = new Gtk.Image.from_icon_name ("open-menu", Gtk.IconSize.LARGE_TOOLBAR),
popover = source_popover,
tooltip_text = _("Manage Calendars")
};
header_bar = new Hdy.HeaderBar () {
show_close_button = true
};
header_bar.pack_start (month_switcher);
header_bar.pack_start (year_switcher);
header_bar.pack_start (button_today);
header_bar.pack_end (menu_button);
header_bar.pack_end (spinner);
header_bar.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
stack = new Gtk.Stack () {
hexpand = true,
vexpand = true
};
sync_with_model (); // Populate stack with a grid
var model = Calendar.EventStore.get_default ();
model.parameters_changed.connect (on_model_parameters_changed);
model.events_added.connect (on_events_added);
model.events_updated.connect (on_events_updated);
model.events_removed.connect (on_events_removed);
stack.notify["transition-running"].connect (() => {
if (stack.transition_running == false) {
stack.get_children ().foreach ((child) => {
if (child != stack.visible_child) {
child.destroy ();
}
});
}
});
settings.changed["show-weeks"].connect (on_show_weeks_changed);
settings.get_value ("show-weeks");
events |= Gdk.EventMask.BUTTON_PRESS_MASK;
events |= Gdk.EventMask.KEY_PRESS_MASK;
events |= Gdk.EventMask.SCROLL_MASK;
events |= Gdk.EventMask.SMOOTH_SCROLL_MASK;
orientation = VERTICAL;
get_style_context ().add_class (Gtk.STYLE_CLASS_VIEW);
add (header_bar);
add (error_bar);
add (info_bar);
add (stack);
show_all ();
var network_monitor = GLib.NetworkMonitor.get_default ();
network_monitor.network_changed.connect (() => {
info_bar.revealed = !(
network_monitor.get_network_available () &&
network_monitor.get_connectivity () == FULL
);
});
info_bar.response.connect (() => {
try {
AppInfo.launch_default_for_uri ("settings://network", null);
} catch (GLib.Error e) {
critical (e.message);
}
});
error_bar.response.connect ((id) => error_bar.set_revealed (false));
Calendar.EventStore.get_default ().error_received.connect ((message) => {
Idle.add (() => {
error_label.label = message;
error_bar.set_revealed (true);
return false;
});
});
month_switcher.left_clicked.connect (() => Calendar.EventStore.get_default ().change_month (-1));
month_switcher.right_clicked.connect (() => Calendar.EventStore.get_default ().change_month (1));
year_switcher.left_clicked.connect (() => Calendar.EventStore.get_default ().change_year (-1));
year_switcher.right_clicked.connect (() => Calendar.EventStore.get_default ().change_year (1));
set_switcher_date (calmodel.month_start);
calmodel.parameters_changed.connect (() => {
set_switcher_date (calmodel.month_start);
});
scroll_controller = new Gtk.EventControllerScroll (this, Gtk.EventControllerScrollFlags.BOTH_AXES);
scroll_controller.scroll.connect (GesturesUtils.on_scroll);
}
private void action_export () {
var filter = new Gtk.FileFilter ();
filter.add_mime_type ("text/calendar");
var filechooser = new Gtk.FileChooserNative (
_("Export Calendar…"),
null,
Gtk.FileChooserAction.SAVE,
_("Save"),
_("Cancel")
);
filechooser.do_overwrite_confirmation = true;
filechooser.filter = filter;
filechooser.set_current_name (_("calendar.ics"));
if (filechooser.run () == Gtk.ResponseType.ACCEPT) {
var events = Calendar.EventStore.get_default ().get_events ();
var builder = new StringBuilder ();
builder.append ("BEGIN:VCALENDAR\n");
foreach (ECal.Component event in events) {
builder.append (event.get_as_string ());
}
builder.append ("END:VCALENDAR");
var file = filechooser.get_file ();
try {
file.replace_contents (builder.data, null, false, FileCreateFlags.REPLACE_DESTINATION, null);
} catch (Error e) {
warning ("%s\n", e.message);
}
}
filechooser.destroy ();
}
private void set_switcher_date (DateTime date) {
month_switcher.text = date.format ("%OB");
year_switcher.text = date.format ("%Y");
}
public void today () {
var today = Calendar.Util.datetime_strip_time (new DateTime.now_local ());
var calmodel = Calendar.EventStore.get_default ();
var start = Calendar.Util.datetime_get_start_of_month (today);
if (!start.equal (calmodel.month_start)) {
calmodel.month_start = start;
}
sync_with_model ();
days_grid.focus_date (today);
}
private void on_show_weeks_changed () {
var model = Calendar.EventStore.get_default ();
weeks.update (model.data_range.first_dt, model.num_weeks);
}
private void on_events_added (E.Source source, Gee.Collection<ECal.Component> events) {
Idle.add ( () => {
foreach (var event in events) {
add_event (source, event);
}
return false;
});
}
private void on_events_updated (E.Source source, Gee.Collection<ECal.Component> events) {
Idle.add ( () => {
foreach (var event in events) {
update_event (source, event);
}
return false;
});
}
private void on_events_removed (E.Source source, Gee.Collection<ECal.Component> events) {
Idle.add ( () => {
foreach (var event in events)
remove_event (source, event);
return false;
});
}
/* Indicates the month has changed */
private void on_model_parameters_changed () {
var model = Calendar.EventStore.get_default ();
if (days_grid.grid_range != null && model.data_range.equals (days_grid.grid_range))
return; // nothing to do
Idle.add ( () => {
remove_all_events ();
sync_with_model ();
return false;
});
}
/* Sets the calendar widgets to the date range of the model */
private void sync_with_model () {
var model = Calendar.EventStore.get_default ();
DateTime previous_first = null;
if (days_grid != null) {
if (days_grid.grid_range != null && (model.data_range.equals (days_grid.grid_range) || days_grid.grid_range.first_dt.compare (model.data_range.first_dt) == 0)) {
return; // nothing to do
}
if (days_grid.grid_range != null) {
previous_first = days_grid.grid_range.first_dt;
}
}
var spacer = new Gtk.Label ("");
spacer.get_style_context ().add_class ("weeks");
var spacer_revealer = new Gtk.Revealer () {
child = spacer,
transition_type = CROSSFADE
};
weeks = new WeekLabels ();
var header = new Header ();
days_grid = new Grid ();
days_grid.focus_date (selected_date);
days_grid.on_event_add.connect ((date) => on_event_add (date));
days_grid.selection_changed.connect ((date) => {
selected_date = date;
selection_changed (date);
});
var big_grid = new Gtk.Grid () {
hexpand = true,
vexpand = true
};
big_grid.attach (spacer_revealer, 0, 0);
big_grid.attach (header, 1, 0);
big_grid.attach (days_grid, 1, 1);
big_grid.attach (weeks, 0, 1);
big_grid.show_all ();
settings.bind ("show-weeks", spacer_revealer, "reveal-child", SettingsBindFlags.GET);
stack.add (big_grid);
header.update_columns (model.week_starts_on);
weeks.update (model.data_range.first_dt, model.num_weeks);
days_grid.set_range (model.data_range, model.month_start);
// keep focus date on the same day of the month
if (selected_date != null) {
var bumpdate = model.month_start.add_days (selected_date.get_day_of_month () - 1);
days_grid.focus_date (bumpdate);
}
if (previous_first != null) {
if (previous_first.compare (days_grid.grid_range.first_dt) == -1) {
stack.transition_type = SLIDE_LEFT;
} else {
stack.transition_type = SLIDE_RIGHT;
}
}
stack.set_visible_child (big_grid);
}
/* Render new event on the grid */
private void add_event (E.Source source, ECal.Component event) {
/* The "source" data is added to events by the Calendar.EventStore. The grid must only show events that have
been added to the model first */
assert (event.get_data<E.Source> ("source") != null);
days_grid.add_event (event);
}
/* Update the event on the grid */
private void update_event (E.Source source, ECal.Component event) {
days_grid.update_event (event);
}
/* Remove event from the grid */
private void remove_event (E.Source source, ECal.Component event) {
days_grid.remove_event (event);
}
/* Remove all events from the grid */
private void remove_all_events () {
days_grid.remove_all_events ();
}
}