~tristan957/harvest-almanac

db4b099737fddce3c496345f02c1066832316b8c — Tristan Partin 1 year, 9 months ago 04ab2ef
harvest-almanac: added support for getting current company
M harvest-glib/harvest-company.c => harvest-glib/harvest-company.c +122 -22
@@ 6,12 6,16 @@

#include <glib-object.h>
#include <glib/gi18n.h>
#include <json-glib/json-glib.h>

#include "harvest-company.h"
#include "harvest-enum-types.h"
#include "harvest-get-company-request.h"

struct _HarvestCompany
{
	GObject parent_instance;

	char *base_uri;
	char *full_domain;
	char *name;


@@ 31,7 35,10 @@ struct _HarvestCompany
	gboolean approval_feature : 1;
};

G_DEFINE_TYPE(HarvestCompany, harvest_company, G_TYPE_OBJECT)
static void harvest_company_json_serializable_init(JsonSerializableIface *iface);

G_DEFINE_TYPE_WITH_CODE(HarvestCompany, harvest_company, G_TYPE_OBJECT,
	G_IMPLEMENT_INTERFACE(JSON_TYPE_SERIALIZABLE, harvest_company_json_serializable_init))

enum HarvestCompanyProps
{


@@ 58,6 65,80 @@ enum HarvestCompanyProps

static GParamSpec *obj_properties[N_PROPS];

static gboolean
harvest_company_json_deserialize_property(JsonSerializable *serializable, const gchar *prop_name,
	GValue *val, GParamSpec *pspec, JsonNode *prop_node)
{
	if (g_strcmp0(prop_name, "week-start-day") == 0) {
		const char *week_start_day = json_node_get_string(prop_node);
		if (g_strcmp0(week_start_day, "Saturday") == 0) {
			g_value_set_enum(val, (gint) HARVEST_WEEK_START_DAY_SATURDAY);
		} else if (g_strcmp0(week_start_day, "Sunday") == 0) {
			g_value_set_enum(val, (gint) HARVEST_WEEK_START_DAY_SUNDAY);
		} else if (g_strcmp0(week_start_day, "Monday") == 0) {
			g_value_set_enum(val, (gint) HARVEST_WEEK_START_DAY_MONDAY);
		} else {
			return FALSE;
		}

		return TRUE;
	} else if (g_strcmp0(prop_name, "time-format") == 0) {
		const char *time_format = json_node_get_string(prop_node);
		if (g_strcmp0(time_format, "decimal") == 0) {
			g_value_set_enum(val, (gint) HARVEST_TIME_FORMAT_DECIMAL);
		} else if (g_strcmp0(time_format, "hours_minutes") == 0) {
			g_value_set_enum(val, (gint) HARVEST_TIME_FORMAT_HOURS_MINUTES);
		} else {
			return FALSE;
		}

		return TRUE;
	} else if (g_strcmp0(prop_name, "plan-type") == 0) {
		const char *plan_type = json_node_get_string(prop_node);
		if (g_strcmp0(plan_type, "trial") == 0) {
			g_value_set_enum(val, (gint) HARVEST_PLAN_TYPE_TRIAL);
		} else if (g_strcmp0(plan_type, "free") == 0) {
			g_value_set_enum(val, (gint) HARVEST_PLAN_TYPE_FREE);
		} else if (g_strcmp0(plan_type, "business") == 0) {
			g_value_set_enum(val, (gint) HARVEST_PLAN_TYPE_BUSINESS);
		} else if (g_strcmp0(plan_type, "simple-v4") == 0) {
			g_value_set_enum(val, (gint) HARVEST_PLAN_TYPE_SIMPLE_V4);
		} else {
			return FALSE;
		}

		return TRUE;
	} else if (g_strcmp0(prop_name, "clock") == 0) {
		const char *clock = json_node_get_string(prop_node);
		if (g_strcmp0(clock, "12h") == 0) {
			g_value_set_enum(val, (gint) HARVEST_CLOCK_12H);
		} else if (g_strcmp0(clock, "24h") == 0) {
			g_value_set_enum(val, (gint) HARVEST_CLOCK_24H);
		} else {
			return FALSE;
		}

		return TRUE;
	}

	return json_serializable_default_deserialize_property(
		serializable, prop_name, val, pspec, prop_node);
}

static JsonNode *
harvest_company_json_serialize_property(
	JsonSerializable *serializable, const gchar *prop_name, const GValue *val, GParamSpec *pspec)
{
	return json_serializable_default_serialize_property(serializable, prop_name, val, pspec);
}

static void
harvest_company_json_serializable_init(JsonSerializableIface *iface)
{
	iface->deserialize_property = harvest_company_json_deserialize_property;
	iface->serialize_property	= harvest_company_json_serialize_property;
}

static void
harvest_company_finalize(GObject *obj)
{


@@ 146,12 227,12 @@ harvest_company_set_property(GObject *obj, guint prop_id, const GValue *val, GPa
		self->base_uri = g_value_dup_string(val);
		break;
	case PROP_FULL_DOMAIN:
		g_free(self->base_uri);
		self->base_uri = g_value_dup_string(val);
		g_free(self->full_domain);
		self->full_domain = g_value_dup_string(val);
		break;
	case PROP_NAME:
		g_free(self->base_uri);
		self->base_uri = g_value_dup_string(val);
		g_free(self->name);
		self->name = g_value_dup_string(val);
		break;
	case PROP_IS_ACTIVE:
		self->is_active = g_value_get_boolean(val);


@@ 214,62 295,64 @@ harvest_company_class_init(HarvestCompanyClass *klass)

	obj_properties[PROP_BASE_URI]
		= g_param_spec_string("base-uri", _("Base URI"), _("The Harvest URL for the company."),
			NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
			NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_FULL_DOMAIN] = g_param_spec_string("full-domain", _("Full Domain"),
		_("The Harvest domain for the company."), NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_NAME]
		= g_param_spec_string("name", _("Name"), _("The name of the company."), NULL,
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_IS_ACTIVE]		= g_param_spec_boolean("is-active", _("Is Active"),
		 _("Whether the company is active or archived."), FALSE,
		 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_WEEK_START_DAY] = g_param_spec_enum("week-start-day", _("Week Start Day"),
		_("The week day used as the start of the week. Returns one of: Saturday, Sunday, or "
		  "Monday."),
		HARVEST_TYPE_WEEK_START_DAY, HARVEST_WEEK_START_DAY_SUNDAY,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_WANTS_TIMESTAMP_TIMERS]
		= g_param_spec_boolean("wants-timestamp-timers", _("Wants Timestamp Timers"),
			_("Whether time is tracked via duration or start and end times."), FALSE,
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_TIME_FORMAT]	= g_param_spec_enum("time-format", _("Time Format"),
		   _("The format used to display time in Harvest. Returns either decimal or hours_minutes."),
		   HARVEST_TYPE_TIME_FORMAT, HARVEST_TIME_FORMAT_HOURS_MINUTES,
		   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		   G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_PLAN_TYPE]		= g_param_spec_enum("plan-type", _("Plan Type"),
		 _("The type of plan the company is on. Examples: trial, free, or simple-v4"),
		 HARVEST_TYPE_PLAN_TYPE, HARVEST_PLAN_TYPE_FREE,
		 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_CLOCK]			= g_param_spec_enum("clock", _("Clock"),
		 _("Used to represent whether the company is using a 12-hour or 24-hour clock. Returns "
		   "either 12h or 24h."),
		 HARVEST_TYPE_CLOCK, HARVEST_CLOCK_12H,
		 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_DECIMAL_SYMBOL] = g_param_spec_string("decimal-symbol", _("Decimal Symbol"),
		_("Symbol used when formatting decimals."), NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_THOUSANDS_OPERATOR] = g_param_spec_string("thousands-operator",
		_("Thousands Operator"), _("Separator used when formatting numbers."), NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_COLOR_SCHEME]		= g_param_spec_string("color-scheme", _("Color Scheme"),
		  _("The color scheme being used in the Harvest web client."), NULL,
		  G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		  G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_WEEKLY_CAPACITY]	= g_param_spec_uint("weekly-capacity",
		   _("Weekly Capacity"), _("The weekly capacity in seconds."), 0, UINT_MAX, 0,
		   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		   G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_EXPENSE_FEATURE]	= g_param_spec_boolean("expense-feature", _(" Feature"),
		   _("Whether the expense module is enabled."), FALSE,
		   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		   G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_INVOICE_FEATURE]	= g_param_spec_boolean("invoice-feature", _(" Feature"),
		   _("Whether the invoice module is enabled."), FALSE,
		   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		   G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_ESTIMATE_FEATURE] = g_param_spec_boolean("estimate-feature", _(" Feature"),
		_("Whether the estimate module is enabled."), FALSE,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_APPROVAL_FEATURE] = g_param_spec_boolean("approval-feature", _(" Feature"),
		_("Whether the approval module is enabled."), FALSE,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPS, obj_properties);
}

static void


@@ 287,3 370,20 @@ harvest_company_get_name(HarvestCompany *self)
{
	return self->name;
}

HarvestResponse *
harvest_company_get_company()
{
	g_autoptr(HarvestGetCompanyRequest) request = harvest_get_company_request_new();

	return harvest_api_client_execute_request_sync(HARVEST_REQUEST(request));
}

void
harvest_company_get_company_async(HarvestCompletedCallback *callback, gpointer user_data)
{
	HarvestGetCompanyRequest *request = harvest_get_company_request_new();
	g_signal_connect(HARVEST_REQUEST(request), "completed", G_CALLBACK(callback), user_data);

	harvest_api_client_execute_request_async(HARVEST_REQUEST(request));
}

M harvest-glib/harvest-company.h => harvest-glib/harvest-company.h +7 -0
@@ 6,6 6,9 @@

#include <glib-object.h>

#include "harvest-api-client.h"
#include "harvest-response.h"

G_BEGIN_DECLS

#define HARVEST_TYPE_COMPANY (harvest_company_get_type())


@@ 29,6 32,7 @@ typedef enum
	HARVEST_PLAN_TYPE_TRIAL,
	HARVEST_PLAN_TYPE_FREE,
	HARVEST_PLAN_TYPE_SIMPLE_V4,
	HARVEST_PLAN_TYPE_BUSINESS,
} HarvestPlanType;

typedef enum


@@ 40,4 44,7 @@ typedef enum
const char *harvest_company_get_full_domain(HarvestCompany *self);
const char *harvest_company_get_name(HarvestCompany *self);

HarvestResponse *harvest_company_get_company(void) G_GNUC_WARN_UNUSED_RESULT;
void harvest_company_get_company_async(HarvestCompletedCallback *callback, gpointer user_data);

G_END_DECLS

A harvest-glib/harvest-get-company-request.c => harvest-glib/harvest-get-company-request.c +37 -0
@@ 0,0 1,37 @@
#include "config.h"

#define G_LOG_DOMAIN "HarvestGetComapnyRequest"

#include <glib-object.h>

#include "harvest-company.h"
#include "harvest-get-company-request.h"
#include "harvest-http.h"
#include "harvest-request.h"
#include "harvest-response-metadata.h"

struct _HarvestGetCompanyRequest
{
	HarvestRequest parent_instance;
};

G_DEFINE_TYPE(HarvestGetCompanyRequest, harvest_get_company_request, HARVEST_TYPE_REQUEST)

static void
harvest_get_company_request_class_init(G_GNUC_UNUSED HarvestGetCompanyRequestClass *klass)
{}

static void
harvest_get_company_request_init(G_GNUC_UNUSED HarvestGetCompanyRequest *self)
{}

HarvestGetCompanyRequest *
harvest_get_company_request_new()
{
	g_autofree char *endpoint = g_strdup("/company");
	g_autoptr(HarvestResponseMetadata) response_metadata
		= harvest_response_metadata_new(HARVEST_TYPE_COMPANY, HTTP_STATUS_OK);

	return g_object_new(HARVEST_TYPE_GET_COMPANY_REQUEST, "http-method", HTTP_METHOD_GET,
		"endpoint", endpoint, "response-metadata", response_metadata, NULL);
}

A harvest-glib/harvest-get-company-request.h => harvest-glib/harvest-get-company-request.h +19 -0
@@ 0,0 1,19 @@
#pragma once

#if !defined(__HARVEST_HEADER_INTERNAL__) && !defined(__HARVEST_COMPILATION__)
#	error "Only <harvest-glib/harvest.h> can be included directly."
#endif

#include <glib-object.h>

#include "harvest-request.h"

G_BEGIN_DECLS

#define HARVEST_TYPE_GET_COMPANY_REQUEST (harvest_get_company_request_get_type())
G_DECLARE_FINAL_TYPE(HarvestGetCompanyRequest, harvest_get_company_request, HARVEST,
	GET_COMPANY_REQUEST, HarvestRequest)

HarvestGetCompanyRequest *harvest_get_company_request_new(void) G_GNUC_WARN_UNUSED_RESULT;

G_END_DECLS

M harvest-glib/harvest.h => harvest-glib/harvest.h +3 -0
@@ 5,10 5,13 @@
#include "harvest-api-client.h"
#include "harvest-client.h"
#include "harvest-common.h"
#include "harvest-company.h"
#include "harvest-creator.h"
#include "harvest-enum-types.h"
#include "harvest-estimate-item-category.h"
#include "harvest-estimate-line-item.h"
#include "harvest-estimate.h"
#include "harvest-get-company-request.h"
#include "harvest-http.h"
#include "harvest-invoice-item-category.h"
#include "harvest-invoice-line-item.h"

M harvest-glib/meson.build => harvest-glib/meson.build +14 -11
@@ 19,6 19,7 @@ harvest_glib_sources = [
    'harvest-estimate.c',
    'harvest-estimate-item-category.c',
    'harvest-estimate-line-item.c',
    'harvest-get-company-request.c',
    'harvest-invoice.c',
    'harvest-invoice-item-category.c',
    'harvest-invoice-line-item.c',


@@ 38,6 39,17 @@ harvest_glib_sources = [
    'harvest-users-me-request.c'
]

enums = gnome.mkenums_simple(
    'harvest-enum-types',
    sources: [
        'harvest-company.h',
        'harvest-estimate.h',
        'harvest-http.h',
        'harvest-invoice.h',
    ],
    install_header: true,
)

harvest_glib_public_headers = [
    'harvest.h',
    'harvest-api-client.h',


@@ 48,6 60,7 @@ harvest_glib_public_headers = [
    'harvest-estimate.h',
    'harvest-estimate-item-category.h',
    'harvest-estimate-line-item.h',
    'harvest-get-company-request.h',
    'harvest-http.h',
    'harvest-invoice.h',
    'harvest-invoice-item-category.h',


@@ 68,16 81,6 @@ harvest_glib_public_headers = [
    'harvest-users-me-request.h',
]

enums = gnome.mkenums_simple(
    'harvest-enum-types',
    sources: [
        'harvest-company.h',
        'harvest-estimate.h',
        'harvest-http.h',
        'harvest-invoice.h',
    ]
)

install_headers(harvest_glib_public_headers, subdir: 'harvest-glib')

harvest_glib_c_args = [


@@ 118,7 121,7 @@ harvest_glib_dep = declare_dependency(
    version: harvest_glib_version
)

if get_option('tests')
if get_option('with-tests')
    subdir('tests')
endif