00001 /** $Revision: 1.3 $ 00002 Last updated: $Date: 2002/09/20 12:21:31 $ 00003 00004 Copyright (C) 2000-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 #ifndef __GENERIC_DB_H__ 00017 #define __GENERIC_DB_H__ 00018 00019 #include <PalmOS.h> 00020 #include <FeatureMgr.h> 00021 #include "MiscFunc.h" //for section definitions 00022 00023 /// Application Info Block 00024 typedef struct { 00025 UInt16 renamedCategories; // bitfield of categories with a different name 00026 Char categoryLabels [dmRecNumCategories] [dmCategoryLength]; 00027 UInt8 categoryUniqIDs [dmRecNumCategories]; 00028 UInt8 lastUniqID; // Uniq IDs generated by the device are between 00029 // 0 - 127. Those from the PC are 128 - 255. 00030 UInt8 reserved1; // from the compiler word aligning things 00031 UInt16 reserved2; 00032 UInt16 dirtyAppInfo; 00033 UInt8 sortOrder; 00034 UInt8 reserved3; 00035 } appInfoType; 00036 00037 /** Generic Database class 00038 This contains the basic database functionality that does not depend 00039 on the record structre. 00040 */ 00041 class GenericDB 00042 { 00043 public: 00044 /// the currenct category 00045 UInt16 current_cat; 00046 00047 /** get the pointer to the db (to use for calling PalmOS db functions directly) 00048 @return pointer to the db (DmOpenRef) */ 00049 DmOpenRef getRef() DB_SECTION2; 00050 00051 /// @return true if the constructor created the db instead of just opening it 00052 Boolean isNew() DB_SECTION2; 00053 00054 /// @return the uid of the selected_record 00055 UInt32 getSelectedUid() DB_SECTION2; 00056 00057 /** Fetch the uid of a record 00058 @param id the record id 00059 00060 @return the record uid */ 00061 UInt32 getRecordUid(UInt16 id) DB_SECTION2; 00062 00063 /** Fetch the uid of the current record 00064 @return the record uid */ 00065 UInt32 getRecordUid() DB_SECTION2; 00066 00067 /** Fetch the uid of a record 00068 @param uid the record uid 00069 00070 @return the record id */ 00071 UInt16 getRecordId(UInt32 uid) DB_SECTION2; 00072 00073 /** Returns the category of a given record 00074 @param id the id of the record 00075 @return the record category */ 00076 UInt16 getRecordCategory(UInt16 id) DB_SECTION2; 00077 00078 /** Returns the category of a given record 00079 @param index unique id of the record 00080 @return the record category */ 00081 UInt16 getRecordCategory(UInt32 uid) DB_SECTION2; 00082 00083 /** sets the category of a given record 00084 @param id id of the record 00085 @param cat the record category */ 00086 void setRecordCategory(UInt16 id, UInt16 cat) DB_SECTION2; 00087 00088 /** sets the category of a given record 00089 @param uid unique id of the record 00090 @param cat the record category */ 00091 void setRecordCategory(UInt32 uid, UInt16 cat) DB_SECTION2; 00092 00093 /** finds a category in this db by its name 00094 @param cat_name the name of the category to be found 00095 @return the category id */ 00096 UInt16 findCategory(Char* cat_name) DB_SECTION2; 00097 00098 /** fetches the name of a category in an allocated buffer 00099 @param cat_id the id of the category (in the current db only) 00100 @param cat_name allocated buffer to which the name will be written */ 00101 void getCategoryName(UInt16 cat_id, Char* cat_name) DB_SECTION2; 00102 00103 /** sets the selected_record to id and sets record_selected flag to false 00104 @param id id of the record to be selected */ 00105 void selectRecord(UInt16 id) DB_SECTION2; 00106 00107 /** sets the selected_record from an unique and sets record_selected flag to false 00108 @param id id of the record to be selected */ 00109 void selectRecord(UInt32 uid) DB_SECTION2; 00110 00111 /** if a record is selected this function unselects it (by setting the record_selected flag) */ 00112 void unselectRecord() DB_SECTION2; 00113 00114 /** use this function to find out whether a record from the database is currently selected 00115 @return true if a record is selected, false otherwise */ 00116 Boolean isRecordSelected() DB_SECTION2; 00117 00118 /** use this function to retrieve the currently selected record 00119 @return the index of the selected record */ 00120 UInt16 getSelectedRecord() DB_SECTION2; 00121 00122 /** create a database using a reference to an already opened database */ 00123 GenericDB(DmOpenRef db_open_ref) DB_SECTION2; 00124 /** create a database without using categories */ 00125 GenericDB(UInt32 type, UInt32 creator, Char* db_name) DB_SECTION2; 00126 /** create a database with category support */ 00127 GenericDB(UInt32 type, UInt32 creator, Char* db_name, UInt16 categ_string) DB_SECTION2; 00128 virtual ~GenericDB() DB_SECTION2; 00129 00130 /** This function writes a given string into a structure; it is meant as a helper function for Database#packRecord. Also see 00131 Database#writeAndAdvance. 00132 00133 @note I am actually amazed that C++ can handle polymorphism in this way... 00134 00135 @param s pointer to the start of the block where the str is to be written 00136 @param offset offset within the block. note that offset is changed by the function 00137 @param str string to be written */ 00138 void writeAndAdvance(MemPtr s, UInt32& offset, Char* str) DB_SECTION2; 00139 00140 /** This is a helper function for pack record. Given a field from a structure together with a destination pointer and an offset 00141 it will write the field in the specified location. 00142 @param s pointer to the start of the block where the str is to be written 00143 @param offset offset within the block. note that offset is changed by the function 00144 @param f a \b pointer to the field to be written. if it is a char* it will (hopefully) be handled by the other writeAndAdvance funcition */ 00145 template <class Field> void writeAndAdvance(MemPtr s, UInt32& offset, Field* f) DB_SECTION2; 00146 00147 /** Deletes a record given by its id 00148 @param id the id of the record to be deleted 00149 @return true if deleted successfuly, false otherwise */ 00150 virtual bool deleteRecord(UInt16 id) DB_SECTION2; 00151 00152 /** Deletes a record given by its uid 00153 @param uid the unique id of the record to be deleted 00154 @return true if deleted successfuly, false otherwise */ 00155 virtual bool deleteRecord(UInt32 uid) DB_SECTION2; 00156 00157 protected: 00158 /// reference to the database 00159 DmOpenRef ref; 00160 /// total number of categories 00161 UInt32 num_cat; 00162 /// true if the db had to be created when it was opened 00163 Boolean created; 00164 ///true if a record is selected, false otherwise 00165 Boolean record_selected; 00166 /// the current record 00167 UInt16 selected_record; 00168 00169 /** opens or creates a database 00170 @param type database type 00171 @param creator database creator 00172 @param mode mode in which to open the database 00173 @param name db name 00174 @param created on return is set to true if the db had to be created */ 00175 Err openOrCreate(UInt32 type, UInt32 creator, UInt16 mode, UInt16 cardNo, Char* name) DB_SECTION2; 00176 00177 ///returns a pointer to the app info record 00178 MemPtr getLockedAppInfo() DB_SECTION2; 00179 00180 /** returns a pointer to the app info record 00181 @param categ_string the function will use this id to initialise the categories for the db */ 00182 MemPtr getLockedAppInfo(UInt16 categ_string) DB_SECTION2; 00183 00184 ///used by the func above to initialise the app info rec if it is empty 00185 MemPtr appInfoInit(LocalID dbID, UInt16 cardNo) DB_SECTION2; 00186 00187 ///used by the func above to initialise the app info rec if it is empty 00188 MemPtr appInfoInit(LocalID dbID, UInt16 cardNo, UInt16 categ_string) DB_SECTION2; 00189 }; 00190 00191 #endif