kpilot Library API Documentation

pilotSerialDatabase.cc

00001 /* pilotSerialDatabase.cc KPilot 00002 ** 00003 ** Copyright (C) 1998-2001 by Dan Pilone 00004 ** 00005 ** Databases approached through DLP / Pilot-link look different, 00006 ** so this file defines an API for them. 00007 */ 00008 00009 /* 00010 ** This program is free software; you can redistribute it and/or modify 00011 ** it under the terms of the GNU Lesser General Public License as published by 00012 ** the Free Software Foundation; either version 2.1 of the License, or 00013 ** (at your option) any later version. 00014 ** 00015 ** This program is distributed in the hope that it will be useful, 00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 ** GNU Lesser General Public License for more details. 00019 ** 00020 ** You should have received a copy of the GNU Lesser General Public License 00021 ** along with this program in a file called COPYING; if not, write to 00022 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00023 ** MA 02111-1307, USA. 00024 */ 00025 00026 /* 00027 ** Bug reports and questions can be sent to kde-pim@kde.org 00028 */ 00029 #include "options.h" 00030 00031 #include <time.h> 00032 00033 #include <pi-dlp.h> 00034 00035 #include <qfile.h> 00036 00037 #include <klocale.h> 00038 #include <kdebug.h> 00039 00040 #include "pilotSerialDatabase.h" 00041 00042 static const char *pilotSerialDatabase_id = 00043 "$Id: pilotSerialDatabase.cc,v 1.18 2003/08/12 18:11:52 mueller Exp $"; 00044 00045 PilotSerialDatabase::PilotSerialDatabase(int linksocket, 00046 const QString &dbName, 00047 QObject *p,const char *n) : 00048 PilotDatabase(p,n), 00049 fDBName(QString::null), 00050 fDBHandle(-1), 00051 fDBSocket(linksocket) 00052 { 00053 FUNCTIONSETUP; 00054 fDBName = dbName; 00055 00056 openDatabase(); 00057 00058 /* NOTREACHED */ 00059 (void) pilotSerialDatabase_id; 00060 } 00061 00062 PilotSerialDatabase::~PilotSerialDatabase() 00063 { 00064 FUNCTIONSETUP; 00065 closeDatabase(); 00066 } 00067 00068 QString PilotSerialDatabase::dbPathName() const 00069 { 00070 QString s = CSL1("Pilot:"); 00071 s.append(fDBName); 00072 return s; 00073 } 00074 00075 // Reads the application block info 00076 int PilotSerialDatabase::readAppBlock(unsigned char *buffer, int maxLen) 00077 { 00078 FUNCTIONSETUP; 00079 if (isDBOpen() == false) 00080 { 00081 kdError() << k_funcinfo << ": DB not open" << endl; 00082 return -1; 00083 } 00084 return dlp_ReadAppBlock(fDBSocket, getDBHandle(), 0, (void *) buffer, 00085 maxLen); 00086 } 00087 00088 // Writes the application block info. 00089 int PilotSerialDatabase::writeAppBlock(unsigned char *buffer, int len) 00090 { 00091 FUNCTIONSETUP; 00092 if (isDBOpen() == false) 00093 { 00094 kdError() << k_funcinfo << ": DB not open" << endl; 00095 return -1; 00096 } 00097 return dlp_WriteAppBlock(fDBSocket, getDBHandle(), buffer, len); 00098 } 00099 00100 // returns the number of records in the database 00101 int PilotSerialDatabase::recordCount() 00102 { 00103 int idlen; 00104 // dlp_ReadOpenDBInfo returns the number of bytes read and sets idlen to the # of recs 00105 if (isDBOpen() && dlp_ReadOpenDBInfo(fDBSocket, getDBHandle(), &idlen)>0) 00106 { 00107 return idlen; 00108 } 00109 else return -1; 00110 } 00111 00112 00113 // Returns a QValueList of all record ids in the database. 00114 QValueList<recordid_t> PilotSerialDatabase::idList() 00115 { 00116 QValueList<recordid_t> idlist; 00117 int idlen=recordCount(); 00118 if (idlen<=0) return idlist; 00119 00120 recordid_t *idarr=new recordid_t[idlen]; 00121 int idlenread; 00122 dlp_ReadRecordIDList (fDBSocket, getDBHandle(), 0, 0, idlen, idarr, &idlenread); 00123 00124 // now create the QValue list from the idarr: 00125 for (idlen=0; idlen<idlenread; idlen++) 00126 { 00127 idlist.append(idarr[idlen]); 00128 } 00129 delete[] idarr; 00130 return idlist; 00131 } 00132 00133 00134 // Reads a record from database by id, returns record length 00135 PilotRecord *PilotSerialDatabase::readRecordById(recordid_t id) 00136 { 00137 FUNCTIONSETUP; 00138 char *buffer[0xffff]; 00139 int index, size, attr, category; 00140 00141 if (isDBOpen() == false) 00142 { 00143 kdError() << k_funcinfo << ": DB not open" << endl; 00144 return 0L; 00145 } 00146 if (id>0xFFFFFF) 00147 { 00148 kdError() << k_funcinfo << " Encountered an invalid record id " 00149 <<id<<endl;; 00150 return 0L; 00151 } 00152 if (dlp_ReadRecordById(fDBSocket, getDBHandle(), id, buffer, &index, 00153 &size, &attr, &category) >= 0) 00154 return new PilotRecord(buffer, size, attr, category, id); 00155 return 0L; 00156 } 00157 00158 // Reads a record from database, returns the record length 00159 PilotRecord *PilotSerialDatabase::readRecordByIndex(int index) 00160 { 00161 FUNCTIONSETUP; 00162 char *buffer[0xffff]; 00163 int size, attr, category; 00164 recordid_t id; 00165 00166 if (isDBOpen() == false) 00167 { 00168 kdError() << k_funcinfo << ": DB not open" << endl; 00169 return 0L; 00170 } 00171 if (dlp_ReadRecordByIndex(fDBSocket, getDBHandle(), index, 00172 (void *) buffer, &id, &size, &attr, &category) >= 0) 00173 return new PilotRecord(buffer, size, attr, category, id); 00174 return 0L; 00175 } 00176 00177 // Reads the next record from database in category 'category' 00178 PilotRecord *PilotSerialDatabase::readNextRecInCategory(int category) 00179 { 00180 FUNCTIONSETUP; 00181 char *buffer[0xffff]; 00182 int index, size, attr; 00183 recordid_t id; 00184 00185 if (isDBOpen() == false) 00186 { 00187 kdError() << k_funcinfo << ": DB not open" << endl; 00188 return 0L; 00189 } 00190 if (dlp_ReadNextRecInCategory(fDBSocket, getDBHandle(), 00191 category, buffer, &id, &index, &size, &attr) >= 0) 00192 return new PilotRecord(buffer, size, attr, category, id); 00193 return 0L; 00194 } 00195 00196 // Reads the next record from database that has the dirty flag set. 00197 PilotRecord *PilotSerialDatabase::readNextModifiedRec(int *ind) 00198 { 00199 FUNCTIONSETUP; 00200 char *buffer[0xffff]; 00201 int index, size, attr, category; 00202 recordid_t id; 00203 00204 if (isDBOpen() == false) 00205 { 00206 kdError() << k_funcinfo << ": DB not open" << endl; 00207 return 0L; 00208 } 00209 if (dlp_ReadNextModifiedRec(fDBSocket, getDBHandle(), (void *) buffer, 00210 &id, &index, &size, &attr, &category) >= 0) 00211 { 00212 if (ind) *ind=index; 00213 return new PilotRecord(buffer, size, attr, category, id); 00214 } 00215 return 0L; 00216 } 00217 00218 // Writes a new record to database (if 'id' == 0 or id>0xFFFFFF, one will be assigned and returned in 'newid') 00219 recordid_t PilotSerialDatabase::writeRecord(PilotRecord * newRecord) 00220 { 00221 FUNCTIONSETUP; 00222 recordid_t newid; 00223 int success; 00224 00225 if (isDBOpen() == false) 00226 { 00227 kdError() << k_funcinfo << ": DB not open" << endl; 00228 return 0; 00229 } 00230 // Do some sanity checking to prevent invalid UniqueIDs from being written 00231 // to the handheld (RecordIDs are only 3 bytes!!!). Under normal conditions 00232 // this check should never yield true, so write out an error to indicate 00233 // someone messed up full time... 00234 if (newRecord->getID()>0xFFFFFF) 00235 { 00236 kdError() << k_funcinfo << "Encountered an invalid record id " 00237 <<newRecord->getID()<<", resetting it to zero."; 00238 newRecord->setID(0); 00239 } 00240 success = 00241 dlp_WriteRecord(fDBSocket, getDBHandle(), 00242 newRecord->getAttrib(), newRecord->getID(), 00243 newRecord->getCat(), newRecord->getData(), 00244 newRecord->getLen(), &newid); 00245 if ( (newRecord->getID() != newid) && (newid!=0) ) 00246 newRecord->setID(newid); 00247 return newid; 00248 } 00249 00250 // Deletes a record with the given recordid_t from the database, or all records, if all is set to true. The recordid_t will be ignored in this case 00251 int PilotSerialDatabase::deleteRecord(recordid_t id, bool all) 00252 { 00253 FUNCTIONSETUP; 00254 if (isDBOpen() == false) 00255 { 00256 kdError() << k_funcinfo <<": DB not open"<<endl; 00257 return -1; 00258 } 00259 return dlp_DeleteRecord(fDBSocket, getDBHandle(), all?1:0, id); 00260 } 00261 00262 00263 // Resets all records in the database to not dirty. 00264 int PilotSerialDatabase::resetSyncFlags() 00265 { 00266 FUNCTIONSETUP; 00267 if (isDBOpen() == false) 00268 { 00269 kdError() << k_funcinfo << ": DB not open" << endl; 00270 return -1; 00271 } 00272 return dlp_ResetSyncFlags(fDBSocket, getDBHandle()); 00273 } 00274 00275 // Resets next record index to beginning 00276 int PilotSerialDatabase::resetDBIndex() 00277 { 00278 FUNCTIONSETUP; 00279 if (isDBOpen() == false) 00280 { 00281 kdError() << k_funcinfo << ": DB not open" << endl; 00282 return -1; 00283 } 00284 return dlp_ResetDBIndex(fDBSocket, getDBHandle()); 00285 } 00286 00287 // Purges all Archived/Deleted records from Palm Pilot database 00288 int PilotSerialDatabase::cleanup() 00289 { 00290 FUNCTIONSETUP; 00291 if (isDBOpen() == false) 00292 { 00293 kdError() << k_funcinfo << ": DB not open" << endl; 00294 return -1; 00295 } 00296 return dlp_CleanUpDatabase(fDBSocket, getDBHandle()); 00297 } 00298 00299 void PilotSerialDatabase::openDatabase() 00300 { 00301 FUNCTIONSETUP; 00302 int db; 00303 00304 if (dlp_OpenDB(fDBSocket, 0, dlpOpenReadWrite, 00305 QFile::encodeName(getDBName()).data(), &db) < 0) 00306 { 00307 kdError() << k_funcinfo 00308 << i18n("Cannot open database") 00309 << i18n("Pilot database error") << endl; 00310 return; 00311 } 00312 setDBHandle(db); 00313 setDBOpen(true); 00314 } 00315 00316 bool PilotSerialDatabase::createDatabase(long creator, long type, int cardno, int flags, int version) 00317 { 00318 FUNCTIONSETUP; 00319 int db; 00320 00321 // if the database is already open, we cannot create it again. How about completely resetting it? (i.e. deleting it and the createing it again) 00322 if (isDBOpen()) return true; 00323 // The latin1 seems ok, database names are latin1. 00324 int res=dlp_CreateDB(fDBSocket, 00325 creator, type, cardno, flags, version, 00326 getDBName().latin1(), &db); 00327 if (res<0) { 00328 kdError() <<k_funcinfo 00329 << i18n("Cannot create database %1 on the handheld").arg(getDBName())<<endl; 00330 return false; 00331 } 00332 // TODO: Do I have to open it explicitly??? 00333 setDBHandle(db); 00334 setDBOpen(true); 00335 return true; 00336 } 00337 00338 void PilotSerialDatabase::closeDatabase() 00339 { 00340 FUNCTIONSETUP; 00341 if (!isDBOpen() ) return; 00342 00343 dlp_CloseDB(fDBSocket, getDBHandle()); 00344 setDBOpen(false); 00345 } 00346 00347 int PilotSerialDatabase::deleteDatabase() 00348 { 00349 FUNCTIONSETUP; 00350 00351 if (isDBOpen()) closeDatabase(); 00352 00353 return dlp_DeleteDB(fDBSocket, 0, fDBName.latin1()); 00354 } 00355 00356
KDE Logo
This file is part of the documentation for kpilot Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 28 23:57:49 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003