~tristan957/harvest-almanac

3e4722b8fc00df0fbbb320a4ac7d9155d485a1ea — Tristan Partin 1 year, 9 months ago a753c2c
harvest-almanac: added support for updating company
M data/ui/hal-window.ui => data/ui/hal-window.ui +2 -2
@@ 288,12 288,12 @@
                      </packing>
                    </child>
                    <child>
                      <object class="GtkLabel">
                      <object class="GtkLabel" id="company">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="valign">start</property>
                        <property name="vexpand">True</property>
                        <property name="label" translatable="yes">Expero, Inc.</property>
                        <property name="label" translatable="yes"></property>
                        <property name="xalign">0</property>
                      </object>
                      <packing>

M harvest-almanac/hal-application.c => harvest-almanac/hal-application.c +14 -2
@@ 39,13 39,25 @@ typedef struct HalApplicationPrivate
G_DEFINE_TYPE_WITH_PRIVATE(HalApplication, hal_application, GTK_TYPE_APPLICATION)

static void
validate_user(
	G_GNUC_UNUSED HarvestRequest *req, G_GNUC_UNUSED HarvestResponse *res, gpointer user_data)
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(set_company, self);
		hal_context_set_user(g_value_get_object(res->body));
		hal_window_show_content(priv->main_window);
	} else {

M harvest-almanac/hal-context.c => harvest-almanac/hal-context.c +33 -2
@@ 17,6 17,8 @@ struct _HalContext

	// The currently logged in user
	HarvestUser *user;
	// The company of the currently logged in user
	HarvestCompany *company;
};

G_DEFINE_TYPE(HalContext, hal_context, G_TYPE_OBJECT)


@@ 25,6 27,7 @@ enum HalContextProps
{
	PROP_0,
	PROP_USER,
	PROP_COMPANY,
	N_PROPS,
};



@@ 40,6 43,7 @@ hal_context_finalize(GObject *obj)
	 * garbage collected before the application itself.
	 */
	g_clear_object(&self->user);
	g_clear_object(&self->company);

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


@@ 53,6 57,9 @@ hal_context_get_property(GObject *obj, guint prop_id, GValue *val, GParamSpec *p
	case PROP_USER:
		g_value_set_object(val, self->user);
		break;
	case PROP_COMPANY:
		g_value_set_object(val, self->company);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}


@@ 69,6 76,11 @@ hal_context_set_property(GObject *obj, guint prop_id, const GValue *val, GParamS
			g_object_unref(self->user);
		self->user = g_value_dup_object(val);
		break;
	case PROP_COMPANY:
		if (self->company != NULL)
			g_object_unref(self->company);
		self->company = g_value_dup_object(val);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}


@@ 83,8 95,11 @@ hal_context_class_init(HalContextClass *klass)
	obj_class->get_property = hal_context_get_property;
	obj_class->set_property = hal_context_set_property;

	obj_properties[PROP_USER] = g_param_spec_object("user", _("User"),
		_("Currently logged in user of the application."), HARVEST_TYPE_USER,
	obj_properties[PROP_USER]	 = g_param_spec_object("user", _("User"),
		   _("Currently logged in user of the application."), HARVEST_TYPE_USER,
		   G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_COMPANY] = g_param_spec_object("company", _("Company"),
		_("Company of the currently logged in user."), HARVEST_TYPE_COMPANY,
		G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPS, obj_properties);


@@ 115,3 130,19 @@ hal_context_get_user()
{
	return CONTEXT->user;
}

void
hal_context_set_company(HarvestCompany *company)
{
	g_return_if_fail(CONTEXT != NULL && HARVEST_IS_COMPANY(company));

	CONTEXT->company = company;

	g_object_notify_by_pspec(G_OBJECT(CONTEXT), obj_properties[PROP_COMPANY]);
}

HarvestCompany *
hal_context_get_company()
{
	return CONTEXT->company;
}

M harvest-almanac/hal-context.h => harvest-almanac/hal-context.h +4 -2
@@ 8,8 8,10 @@ G_BEGIN_DECLS
#define HAL_TYPE_CONTEXT (hal_context_get_type())
G_DECLARE_FINAL_TYPE(HalContext, hal_context, HAL, CONTEXT, GObject)

void hal_context_initialize();
void hal_context_initialize(void);
void hal_context_set_user(HarvestUser *user);
HarvestUser *hal_context_get_user() G_GNUC_CONST;
HarvestUser *hal_context_get_user(void) G_GNUC_CONST;
void hal_context_set_company(HarvestCompany *company);
HarvestCompany *hal_context_get_company(void) G_GNUC_CONST;

G_END_DECLS

M harvest-almanac/hal-window.c => harvest-almanac/hal-window.c +17 -1
@@ 29,6 29,7 @@ typedef struct HalWindowPrivate
	GtkStack *stack;
	GtkButton *back_button;
	GtkLabel *username;
	GtkLabel *company;
	HalTimeTracker *time_tracker;
	HalProfile *profile;
} HalWindowPrivate;


@@ 41,7 42,7 @@ on_context_notify_user(
{
	HalWindow *self		   = HAL_WINDOW(user_data);
	HalWindowPrivate *priv = hal_window_get_instance_private(self);
	HarvestUser *user	   = hal_context_get_user(CONTEXT);
	HarvestUser *user	   = hal_context_get_user();

	if (user != NULL) {
		gtk_label_set_text(priv->username, g_strconcat(harvest_user_get_first_name(user), " ",


@@ 50,6 51,19 @@ on_context_notify_user(
}

static void
on_contect_notify_computer(
	G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GParamSpec *pspec, gpointer user_data)
{
	HalWindow *self			= HAL_WINDOW(user_data);
	HalWindowPrivate *priv	= hal_window_get_instance_private(self);
	HarvestCompany *company = hal_context_get_company();

	if (company != NULL) {
		gtk_label_set_text(priv->company, harvest_company_get_name(company));
	}
}

static void
update_header_bar_title(HalWindow *self)
{
	HalWindowPrivate *priv = hal_window_get_instance_private(self);


@@ 153,6 167,7 @@ hal_window_class_init(HalWindowClass *klass)
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, stack);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, back_button);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, username);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, company);
	gtk_widget_class_bind_template_callback(wid_class, header_leaflet_notify_fold_cb);
	gtk_widget_class_bind_template_callback(wid_class, header_leaflet_notify_visible_child_cb);
	gtk_widget_class_bind_template_callback(wid_class, stack_notify_visible_child_cb);


@@ 177,6 192,7 @@ hal_window_init(HalWindow *self)
	hdy_leaflet_set_visible_child_name(priv->content_leaflet, "content");

	g_signal_connect(CONTEXT, "notify::user", G_CALLBACK(on_context_notify_user), self);
	g_signal_connect(CONTEXT, "notify::company", G_CALLBACK(on_contect_notify_computer), self);
}

HalWindow *