00001 /** $Revision: 1.7 $ 00002 Last updated: $Date: 2002/12/17 22:51:59 $ 00003 00004 Copyright (C) 2002 Vlad Mereuta <dizzy@users.sourceforge.net> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00019 00020 #ifndef __CURRENCY_DB__ 00021 #define __CURRENCY_DB__ 00022 00023 #include "Database.h" 00024 00025 /// type of the Currency database 00026 #define CURRENCYDB_TYPE 'DATA' 00027 /// db name 00028 #define CURRENCYDB_NAME "FreeCoins-Currency" 00029 00030 ///the packed structure for a currency entry 00031 typedef struct { 00032 /// exchange rate, relative to the default currency (one unit of this currency = 1 unit of default currency * exchange_rate) 00033 double exchange_rate; 00034 /// text containing the code and description of the currency (will be resized to fit) 00035 Char text[1]; 00036 } packedCurrencyStructure; 00037 00038 ///unpacked structure for a currency entry 00039 typedef struct { 00040 /** exchange rate is value of 1 unit of default currency in this currency (if you get £2 for $1 and £ is default, 00041 £ exchange rate is 1 and $ exchange rate is 0.5) */ 00042 double exchange_rate; 00043 ///user defined currency code (i.e. USD, GBP, etc) 00044 Char* code; 00045 ///long textual description for this currency entry 00046 Char* description; 00047 } currencyStructure; 00048 00049 ///class to manipulate the currency database 00050 class CurrencyDB : public Database <currencyStructure, packedCurrencyStructure> 00051 { 00052 public: 00053 /// unpacks a currency record 00054 void unpackRecord (currencyStructure* currency, packedCurrencyStructure* packed_currency) DB_SECTION1; 00055 /// packs a currency record 00056 void packRecord (currencyStructure* currency, MemHandle db_entry) DB_SECTION1; 00057 /// initialize the database 00058 CurrencyDB() DB_SECTION1; 00059 /** creates a new record; if this is the only currency the default currency for the application is 00060 automatically set to the uid for this new record; the exchange rate is automatically set to 1. Otherwise 00061 the record is simply created without any further manipulation 00062 The code names have to be unique, so giving an existing code name will result in a warning a refusal to create 00063 @param c pointer to the structure containing the currency data 00064 @return id of the newly created record */ 00065 UInt16 newRecord(currencyStructure* c) DB_SECTION1; 00066 /** deletes a currency, given its id. The process fails if the given currency is set as default, or if there are 00067 any accounts which use the currency 00068 @param id the id of the record to be deleted */ 00069 bool deleteRecord(UInt16 id) DB_SECTION1; 00070 00071 /** check whether there is any kind of dependency on the given currency 00072 @return true if there is such a dependency, false otherwise*/ 00073 bool checkCurrencyDependency (UInt16 id) DB_SECTION1; 00074 /** checks if a given code already exists in the database 00075 @return true if a currency with the given code exists in the database, false othewise 00076 @param code currency code to look for 00077 @param ignore_uid if not 0 (default value) the given uid is going to be ignored in the search. This is useful when 00078 updating an existing record */ 00079 bool checkForExistingCode (Char* code, UInt32 ignore_uid = 0) DB_SECTION1; 00080 /** This method can be used to obtain the exchange rate between any two currencies. The calculation 00081 will make use of the exchange rate w.r.t the default currency 00082 @param currency1 UID of the first currency (source currency) 00083 @param currency2 UID of the destination currency 00084 @return a fraction x such that: 00085 one unit of currency1 * x = value of 1 unit currency1 in currency2 */ 00086 double getExchangeRate (UInt32 currency1, UInt32 currency2) DB_SECTION1; 00087 /** modifies the exchange rate in between the given currency to match some given rate. 00088 @note the operation fails if none of the given currencies is the default currency. This is because 00089 we would have to set the exchange rate of both give currencies w.r.t. the default currency. We cannot do this 00090 without more information 00091 @param src_currency UID for the source currency 00092 @param dest_currency UID for the destination currency 00093 @param rate new exchange rate between src_currency and dest_currency */ 00094 void adjustExchangeRate (UInt32 src_currency, UInt32 dest_currency, double rate) DB_SECTION1; 00095 /** Update all exchange rates so that they correspond to a new given default currency 00096 @param currency the UID of the new default currency 00097 @note this method does not change the actual currency, as it is stored in the application preferences */ 00098 void newDefaultCurrency (UInt32 currency) DB_SECTION1; 00099 00100 /** creates a description for a given currency and puts it in a (preallocated) buffer 00101 @param uid the uid of the currency to be described 00102 @param width maximum width of generated text (if generated text is longer will be truncated) 00103 @param buffer buffer where the new text is going to be generated */ 00104 void getDescriptionTruncLeftToFit(UInt32 uid, Int16 width, Char* buffer) DB_SECTION1; 00105 00106 /** Compares two packed currency structures. The currency database is sorted by code 00107 For parameters see the PalmOS reference */ 00108 static Int16 compareRecords(packedCurrencyStructure* rec1,packedCurrencyStructure* rec2,Int16 other,SortRecordInfoPtr sr1,SortRecordInfoPtr sr2,MemHandle app_info) DB_SECTION1; 00109 }; 00110 00111 extern CurrencyDB* currency_db; 00112 00113 #endif