~tristan957/harvest-almanac

ref: b879963b39b5a4bbc0cca532857541f453a8f4c3 harvest-almanac/harvest-almanac/hal-application.c -rw-r--r-- 8.2 KiB
b879963bTristan Partin harvest-almanac: add back in about/prefs popover 1 year, 5 months ago
                                                                                
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
#include "config.h"

#include <string.h>

#include <glib-object.h>
#include <gtk/gtk.h>
#include <harvest-glib/harvest.h>
#include <libsecret/secret.h>
#include <libsoup/soup.h>

#include "hal-application.h"
#include "hal-context.h"
#include "hal-preferences-window.h"
#include "hal-resources.h"
#include "hal-time-entry.h"
#include "hal-window.h"

#define HAL_MAX_CONNS_PER_HOST 4

extern HalContext *CONTEXT;
extern HarvestApiClient *API_CLIENT;
extern HalTimeEntry *CURRENTLY_RUNNING_TIME_ENTRY;

static HarvestApiClient *CLIENT;

struct _HalApplication
{
	GtkApplication parent_instance;

	GSettings *settings;
	HarvestApiClient *client;
};

typedef struct HalApplicationPrivate
{
	HalWindow *main_window;
} HalApplicationPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(HalApplication, hal_application, GTK_TYPE_APPLICATION)

static void
set_company(
	G_GNUC_UNUSED HarvestRequest *req, HarvestResponse *res, G_GNUC_UNUSED gpointer user_data)
{
	if (res->err == NULL) {
		HarvestCompany *company = HARVEST_COMPANY(g_value_get_object(res->body));
		hal_context_set_company(company);
	} else {
		g_critical("set_company: %s", res->err->message);
	}
}

static void
validate_user(G_GNUC_UNUSED HarvestRequest *req, HarvestResponse *res, gpointer user_data)
{
	HalApplication *self		= HAL_APPLICATION(user_data);
	HalApplicationPrivate *priv = hal_application_get_instance_private(self);

	if (res->err == NULL) {
		harvest_company_get_company_async(CLIENT, set_company, self);
		hal_context_set_user(g_value_get_object(res->body));
		hal_window_show_content(priv->main_window);
	} else {
		g_critical("validate_user: %s", res->err->message);
		hal_window_hide_content(priv->main_window);
	}
}

static void
construct_client(HalApplication *self, const char *access_token, const char *account_id,
	const char *contact_email)
{
	g_autoptr(GVariant) max_connections_v
		= g_settings_get_value(self->settings, "soup-max-connections");
	guint32 max_connections = HAL_DEFAULT_SOUP_MAX_CONNECTIONS;
	g_variant_get(max_connections_v, "u", &max_connections);
	g_autoptr(GVariant) logger_level_v = g_settings_get_value(self->settings, "soup-logger-level");
	unsigned char logger_level		   = HAL_DEFAULT_SOUP_LOGGER_LEVEL;
	g_variant_get(logger_level_v, "y", &logger_level);

	g_autoptr(SoupLogger) logger = soup_logger_new(logger_level, -1);

	g_autofree char *user_agent = g_strdup_printf("Harvest Almanac (%s)", contact_email);

	g_autoptr(SoupSession) session = soup_session_new_with_options(SOUP_SESSION_MAX_CONNS,
		max_connections, SOUP_SESSION_USER_AGENT, user_agent, SOUP_SESSION_ADD_FEATURE_BY_TYPE,
		SOUP_TYPE_CONTENT_SNIFFER, SOUP_SESSION_ADD_FEATURE, SOUP_SESSION_FEATURE(logger), NULL);

	if (CLIENT != NULL)
		g_object_unref(CLIENT);

	CLIENT = harvest_api_client_new(session, access_token, account_id);

	harvest_user_get_me_async(CLIENT, validate_user, self);
}

static void
hal_application_activate(GApplication *app)
{
	HalApplication *self		= HAL_APPLICATION(app);
	HalApplicationPrivate *priv = hal_application_get_instance_private(HAL_APPLICATION(self));

	if (priv->main_window == NULL) {
		priv->main_window = hal_window_new(app);

		g_autofree char *account_id = g_settings_get_string(self->settings, "harvest-account-id");
		g_autofree char *contact_email
			= g_settings_get_string(self->settings, "harvest-api-contact-email");

		g_autoptr(GError) err = NULL;

		// Synchronous since window hasn't been presented yet
		gchar *access_token = secret_password_lookup_sync(HAL_SECRET_SCHEMA, NULL, &err,
			"account-id", account_id, "contact-email", contact_email, NULL);
		if (err != NULL) {
			g_error("Failed to look up Harvest API access token: %s", err->message);
		} else if (access_token == NULL) {
			g_debug("Harvest API access token is <null>");
		} else {
			g_debug("Constructing Harvest API access token");
			construct_client(self, access_token, account_id, contact_email);
			secret_password_free(access_token);
		}

		g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme",
			g_settings_get_boolean(self->settings, "prefer-dark-theme"), NULL);

		g_autoptr(GtkCssProvider) css_provider = gtk_css_provider_new();
		gtk_css_provider_load_from_resource(
			css_provider, "/io/partin/tristan/HarvestAlmanac/ui/style.css");
		gtk_style_context_add_provider_for_screen(
			gtk_window_get_screen(GTK_WINDOW(priv->main_window)), GTK_STYLE_PROVIDER(css_provider),
			GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
	}

	gtk_window_present(GTK_WINDOW(priv->main_window));
}

static void
hal_application_about(
	G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *param, gpointer data)
{
	static const char *authors[] = {"Tristan Partin"};

	HalApplication *self		= HAL_APPLICATION(data);
	HalApplicationPrivate *priv = hal_application_get_instance_private(self);

	gtk_show_about_dialog(GTK_WINDOW(priv->main_window), "program-name", PACKAGE_NAME, "version",
		PACKAGE_VERSION, "license-type", PACKAGE_LICENSE, "website", PACKAGE_WEBSITE,
		"website-label", PACKAGE_WEBSITE_LABEL, "authors", authors, "logo-icon-name", "trophy-gold",
		NULL);
}

static void
hal_application_preferences(
	G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *param, gpointer data)
{
	HalApplication *self = HAL_APPLICATION(data);

	HalPreferencesWindow *preferences_window = hal_preferences_window_new(self->settings);
	gtk_window_present(GTK_WINDOW(preferences_window));
}

static void
hal_application_time_entry_start(
	G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *param, gpointer data)
{
	g_autoptr(GNotification) notification = g_notification_new("Harvest Almanac");
	g_notification_set_body(notification, "Client -- Project timer started");
	g_notification_add_button(notification, "Stop Timer", "app.time-entry-stop");
	g_application_send_notification(G_APPLICATION(data), "time-entry", notification);
}

static void
hal_application_time_entry_stop(
	G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *param, gpointer data)
{
	hal_time_entry_stop(CURRENTLY_RUNNING_TIME_ENTRY);
	g_application_withdraw_notification(G_APPLICATION(data), "time-entry");
}

static void
hal_application_reconstruct_client(
	G_GNUC_UNUSED GSimpleAction *action, GVariant *param, gpointer data)
{
	HalApplication *self = HAL_APPLICATION(data);

	const char *harvest_api_access_token = g_variant_get_string(param, NULL);
	g_autofree char *harvest_api_contact_email
		= g_settings_get_string(self->settings, "harvest-api-contact-email");
	g_autofree char *harvest_account_id
		= g_settings_get_string(self->settings, "harvest-account-id");

	construct_client(self, harvest_api_access_token, harvest_account_id, harvest_api_contact_email);
}

static void
hal_application_startup(GApplication *self)
{
	g_resources_register(hal_get_resource());
	g_application_set_resource_base_path(self, "/io/partin/tristan/HarvestAlmanac");

	G_APPLICATION_CLASS(hal_application_parent_class)->startup(self);
}

static void
hal_application_finalize(GObject *obj)
{
	HalApplication *self = HAL_APPLICATION(obj);

	g_clear_object(&CLIENT);
	g_clear_object(&self->settings);
	g_clear_object(&CONTEXT);

	G_OBJECT_CLASS(hal_application_parent_class)->finalize(obj);
}

static void
hal_application_class_init(HalApplicationClass *klass)
{
	GObjectClass *obj_class		 = G_OBJECT_CLASS(klass);
	GApplicationClass *app_class = G_APPLICATION_CLASS(klass);

	obj_class->finalize = hal_application_finalize;
	app_class->activate = hal_application_activate;
	app_class->startup	= hal_application_startup;
}

// clang-format off
static const GActionEntry app_entries[] = {
	{
		.name	  = "about",
		.activate = hal_application_about
	},
	{
		.name	  = "preferences",
		.activate = hal_application_preferences
	},
	{
		.name			= "time-entry-start",
		.activate		= hal_application_time_entry_start
	},
	{
		.name			= "time-entry-stop",
		.activate		= hal_application_time_entry_stop
	},
	{
		.name	  		= "reconstruct-client",
		.activate 		= hal_application_reconstruct_client,
		.parameter_type = "s"
	}
};
// clang-format on

static void
hal_application_init(HalApplication *self)
{
	self->settings = g_settings_new("io.partin.tristan.HarvestAlmanac");

	g_action_map_add_action_entries(
		G_ACTION_MAP(self), app_entries, G_N_ELEMENTS(app_entries), self);

	hal_context_initialize();
}

HalApplication *
hal_application_new(const char *id)
{
	return g_object_new(HAL_TYPE_APPLICATION, "application-id", id, NULL);
}