gs-price.c 4.51 KB
Newer Older
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2016 Canonical Ltd.
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <glib/gi18n.h>

#include "gs-price.h"

struct _GsPrice
{
	GObject			 parent_instance;

	gdouble			 amount;
	gchar			*currency;
};

G_DEFINE_TYPE (GsPrice, gs_price, G_TYPE_OBJECT)

/**
 * gs_price_get_amount:
 * @price: a #GsPrice
 *
 * Get the amount of money in this price.
 *
 * Returns: The amount of money in this price, e.g. 0.99
 */
gdouble
gs_price_get_amount (GsPrice *price)
{
	g_return_val_if_fail (GS_IS_PRICE (price), 0);
	return price->amount;
}

/**
 * gs_price_set_amount:
 * @price: a #GsPrice
 * @amount: The amount of this price, e.g. 0.99
 *
 * Set the amount of money in this price.
 */
void
gs_price_set_amount (GsPrice *price, gdouble amount)
{
	g_return_if_fail (GS_IS_PRICE (price));
	price->amount = amount;
}

/**
 * gs_price_get_currency:
 * @price: a #GsPrice
 *
 * Get the currency a price is using.
 *
 * Returns: an ISO 4217 currency code for this price, e.g. "USD"
 */
const gchar *
gs_price_get_currency (GsPrice *price)
{
	g_return_val_if_fail (GS_IS_PRICE (price), NULL);
	return price->currency;
}

/**
 * gs_price_set_currency:
 * @price: a #GsPrice
 * @currency: An ISO 4217 currency code, e.g. "USD"
 *
 * Set the currency this price is using.
 */
void
gs_price_set_currency (GsPrice *price, const gchar *currency)
{
	g_return_if_fail (GS_IS_PRICE (price));
	g_free (price->currency);
	price->currency = g_strdup (currency);
}

/**
 * gs_price_to_string:
 * @price: a #GsPrice
 *
 * Convert a price object to a human readable string.
 *
 * Returns: A human readable string for this price, e.g. "US$0.99"
 */
gchar *
gs_price_to_string (GsPrice *price)
{
	g_return_val_if_fail (GS_IS_PRICE (price), NULL);

	if (g_strcmp0 (price->currency, "AUD") == 0) {
		return g_strdup_printf (_("A$%.2f"), price->amount);
	} else if (g_strcmp0 (price->currency, "CAD") == 0) {
		return g_strdup_printf (_("C$%.2f"), price->amount);
	} else if (g_strcmp0 (price->currency, "CNY") == 0) {
		return g_strdup_printf (_("CN¥%.2f"), price->amount);
	} else if (g_strcmp0 (price->currency, "EUR") == 0) {
		return g_strdup_printf (_("€%.2f"), price->amount);
	} else if (g_strcmp0 (price->currency, "GBP") == 0) {
		return g_strdup_printf (_("£%.2f"), price->amount);
	} else if (g_strcmp0 (price->currency, "JPY") == 0) {
		return g_strdup_printf (_("¥%.2f"), price->amount);
	} else if (g_strcmp0 (price->currency, "NZD") == 0) {
		return g_strdup_printf (_("NZ$%.2f"), price->amount);
	} else if (g_strcmp0 (price->currency, "RUB") == 0) {
		return g_strdup_printf (_("₽%.2f"), price->amount);
	} else if (g_strcmp0 (price->currency, "USD") == 0) {
		return g_strdup_printf (_("US$%.2f"), price->amount);
	} else {
129 130
		/* Translators: %s is the currency, and %f is the amount.
		 * You can switch the order by using “%2$f %1$s” instead. */
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
		return g_strdup_printf (_("%s %f"), price->currency, price->amount);
	}
}

static void
gs_price_finalize (GObject *object)
{
	GsPrice *price = GS_PRICE (object);

	g_free (price->currency);

	G_OBJECT_CLASS (gs_price_parent_class)->finalize (object);
}

static void
gs_price_class_init (GsPriceClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = gs_price_finalize;
}

static void
gs_price_init (GsPrice *price)
{
}

/**
 * gs_price_new:
 * @amount: The amount of this price, e.g. 0.99
 * @currency: An ISO 4217 currency code, e.g. "USD"
 *
 * Creates a new price object.
 *
 * Return value: a new #GsPrice object.
 **/
GsPrice *
gs_price_new (gdouble amount, const gchar *currency)
{
	GsPrice *price;
	price = g_object_new (GS_TYPE_PRICE, NULL);
	price->amount = amount;
	price->currency = g_strdup (currency);
	return GS_PRICE (price);
}

/* vim: set noexpandtab: */