forked from ohwgiles/focal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
216 lines (178 loc) · 6.5 KB
/
main.c
File metadata and controls
216 lines (178 loc) · 6.5 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
/*
* main.c
* This file is part of focal, a calendar application for Linux
* Copyright 2018 Oliver Giles and focal contributors.
*
* Focal is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Focal is distributed without any explicit or implied warranty.
* You should have received a copy of the GNU General Public License
* version 3 with focal. If not, see <http://www.gnu.org/licenses/>.
*/
#include "event-panel.h"
#include "remote-calendar.h"
#include "rpc.h"
#include "week-view.h"
#include <curl/curl.h>
#include <gtk/gtk.h>
#include <libical/ical.h>
#include <strings.h>
typedef struct {
GtkWidget* mainWindow;
GSList* calendars;
GtkWidget* weekView;
} FocalMain;
static char* icc_read_stream(char* s, size_t sz, void* ud)
{
return fgets(s, sz, (FILE*) ud);
}
static icalcomponent* icalcomponent_from_file(const char* path)
{
FILE* stream = fopen(path, "r");
if (!stream)
return NULL;
icalcomponent* c;
char* line;
icalparser* parser = icalparser_new();
icalparser_set_gen_data(parser, stream);
do {
line = icalparser_get_line(parser, &icc_read_stream);
c = icalparser_add_line(parser, line);
if (c)
return c;
} while (line);
return NULL;
}
static void focal_add_event(FocalMain* focal, icalcomponent* vev)
{
Calendar* cal = NULL;
icalparameter* partstat = NULL;
for (icalproperty* attendees = icalcomponent_get_first_property(vev, ICAL_ATTENDEE_PROPERTY); attendees; attendees = icalcomponent_get_next_property(vev, ICAL_ATTENDEE_PROPERTY)) {
const char* cal_addr = icalproperty_get_attendee(attendees);
if (strncasecmp(cal_addr, "mailto:", 7) != 0)
continue;
cal_addr = &cal_addr[7];
// check each known Calendar for matching address
for (GSList* c = focal->calendars; c; c = c->next) {
const char* email = calendar_get_email(FOCAL_CALENDAR(c->data));
if (email && strcasecmp(email, cal_addr) == 0) {
cal = FOCAL_CALENDAR(c->data);
partstat = icalproperty_get_first_parameter(attendees, ICAL_PARTSTAT_PARAMETER);
// TODO what is the effect of this on common caldav servers?
icalproperty_remove_parameter_by_kind(attendees, ICAL_RSVP_PARAMETER);
}
}
}
week_view_add_event(FOCAL_WEEK_VIEW(focal->weekView), vev);
// TODO allow selecting a calendar in the dialog
if (!cal)
cal = FOCAL_CALENDAR(focal->calendars->data);
GtkWidget* dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(focal->mainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_YES_NO,
"Add event \"%s\" to calendar?",
icalcomponent_get_summary(vev));
int resp = gtk_dialog_run(GTK_DIALOG(dialog));
if (resp == GTK_RESPONSE_YES) {
// TODO allow selecting a different response
if (partstat) {
icalparameter_set_partstat(partstat, ICAL_PARTSTAT_ACCEPTED);
}
calendar_add_event(cal, vev);
} else {
week_view_remove_event(FOCAL_WEEK_VIEW(focal->weekView), vev);
}
gtk_widget_destroy(dialog);
}
static void rpc_handle_cmd(const char* cmd, void* data)
{
icalcomponent* c = icalcomponent_from_file(cmd);
if (c) {
icalcomponent* vev = icalcomponent_get_first_real_component(c);
if (vev)
focal_add_event(data, vev);
}
}
static void cal_add_vevent(WeekView* widget, icalcomponent* ev, Calendar* cal)
{
calendar_add_event(cal, ev);
}
static void cal_event_selected(WeekView* widget, icalcomponent* e, EventPanel* ew)
{
event_panel_set_event(ew, e);
}
static void event_delete(EventPanel* event_panel, icalcomponent* ev, FocalMain* focal)
{
// TODO "are you sure?" popup
// TODO pass calendar association with callback
calendar_delete_event(FOCAL_CALENDAR(focal->calendars->data), ev);
week_view_remove_event(FOCAL_WEEK_VIEW(focal->weekView), ev);
}
int main(int argc, char** argv)
{
rpc_status_t rpc = rpc_init();
if (rpc == RPC_BIND_ERROR) {
return 1;
} else if (rpc == RPC_BIND_INUSE) {
rpc_connect();
for (int i = 1; i < argc; ++i) {
// todo realpath
rpc_send_command(argv[i]);
}
return 0;
}
GKeyFile* config = g_key_file_new();
const char *config_dir, *home;
char* config_file;
if ((config_dir = g_getenv("XDG_CONFIG_HOME")))
asprintf(&config_file, "%s/focal.conf", config_dir);
else if ((home = g_getenv("HOME")))
asprintf(&config_file, "%s/.config/focal.conf", home);
else
return fprintf(stderr, "Could not find .config path\n"), -1;
g_key_file_load_from_file(config, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL);
gchar* url = g_key_file_get_string(config, "main", "url", NULL);
gchar* user = g_key_file_get_string(config, "main", "user", NULL);
gchar* pass = g_key_file_get_string(config, "main", "pass", NULL);
gchar* email = g_key_file_get_string(config, "main", "email", NULL);
if (!url || !user || !pass)
return fprintf(stderr, "Set main/{url,user,pass} in focal.conf\n"), -1;
gtk_init(&argc, &argv);
Calendar* rc = remote_calendar_new(url, user, pass);
calendar_set_email(rc, email);
FocalMain fm = {0};
fm.calendars = g_slist_append(fm.calendars, rc);
fm.mainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
fm.weekView = week_view_new();
GtkWidget* event_panel = event_panel_new();
rpc_server(&rpc_handle_cmd, &fm);
remote_calendar_sync(FOCAL_REMOTE_CALENDAR(rc));
gtk_window_set_type_hint((GtkWindow*) fm.mainWindow, GDK_WINDOW_TYPE_HINT_DIALOG);
g_signal_connect(fm.weekView, "add-vevent", (GCallback) &cal_add_vevent, rc);
g_signal_connect(fm.weekView, "event-selected", (GCallback) &cal_event_selected, event_panel);
g_signal_connect(event_panel, "cal-event-delete", (GCallback) &event_delete, &fm);
week_view_add_calendar(FOCAL_WEEK_VIEW(fm.weekView), rc);
GtkWidget* header = gtk_header_bar_new();
gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(header), TRUE);
GtkWidget* overlay = gtk_overlay_new();
gtk_container_add(GTK_CONTAINER(overlay), fm.weekView);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), event_panel);
gtk_window_set_titlebar(GTK_WINDOW(fm.mainWindow), header);
gtk_container_add(GTK_CONTAINER(fm.mainWindow), overlay);
g_signal_connect(fm.mainWindow, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_default_size(GTK_WINDOW(fm.mainWindow), 780, 630);
// create window title
int week_num = week_view_get_current_week(FOCAL_WEEK_VIEW(fm.weekView));
char week_title[8];
snprintf(week_title, 8, "Week %d", week_num);
gtk_window_set_title(GTK_WINDOW(fm.mainWindow), week_title);
gtk_widget_show_all(fm.mainWindow);
// handle invitations on command line
for (int i = 1; i < argc; ++i)
rpc_handle_cmd(argv[i], &fm);
gtk_main();
}