kpilot/lib

pilotRecord.cc

00001 /* KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 **
00006 ** This is a wrapper for pilot-link's general
00007 ** Pilot database structures. These records are
00008 *** just collections of bits. See PilotAppCategory
00009 ** for interpreting the bits in a meaningful way.
00010 **
00011 ** As a crufty hack, the non-inline parts of
00012 ** PilotAppCategory live in this file as well.
00013 */
00014 
00015 /*
00016 ** This program is free software; you can redistribute it and/or modify
00017 ** it under the terms of the GNU Lesser General Public License as published by
00018 ** the Free Software Foundation; either version 2.1 of the License, or
00019 ** (at your option) any later version.
00020 **
00021 ** This program is distributed in the hope that it will be useful,
00022 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00024 ** GNU Lesser General Public License for more details.
00025 **
00026 ** You should have received a copy of the GNU Lesser General Public License
00027 ** along with this program in a file called COPYING; if not, write to
00028 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00029 ** MA 02110-1301, USA.
00030 */
00031 
00032 /*
00033 ** Bug reports and questions can be sent to kde-pim@kde.org
00034 */
00035 #include "options.h"
00036 
00037 #include <string.h>
00038 
00039 #include <qtextcodec.h>
00040 #include <qregexp.h>
00041 
00042 #include <kglobal.h>
00043 #include <kcharsets.h>
00044 
00045 // PilotAppCategory includes pilotRecord and we
00046 // provide its implementation here as well.
00047 //
00048 #include "pilotAppCategory.h"
00049 
00050 
00051 
00052 static const char *pilotRecord_id =
00053     "$Id: pilotRecord.cc 437980 2005-07-23 19:53:57Z kainhofe $";
00054 
00055 /* static */ int PilotRecord::fAllocated = 0;
00056 /* static */ int PilotRecord::fDeleted = 0;
00057 
00058 /* static */ void PilotRecord::allocationInfo()
00059 {
00060 #ifdef DEBUG
00061     FUNCTIONSETUP;
00062     DEBUGKPILOT << fname
00063         << ": Allocated " << fAllocated
00064         << "  Deleted " << fDeleted << endl;
00065 #endif
00066 }
00067 
00068 PilotRecord::PilotRecord(void *data, int len, int attrib, int cat, recordid_t uid) :
00069     PilotRecordBase(attrib,cat,uid),
00070     fData(0L),
00071     fLen(len)
00072 #if PILOT_LINK_NUMBER >= PILOT_LINK_0_12_0
00073     ,
00074     fBuffer(0L)
00075 #endif
00076 {
00077     FUNCTIONSETUPL(4);
00078     fData = new char[len];
00079 
00080     memcpy(fData, data, len);
00081 
00082     fAllocated++;
00083     (void) pilotRecord_id;
00084 }
00085 
00086 PilotRecord::PilotRecord(PilotRecord * orig) :
00087     PilotRecordBase( orig->attributes(), orig->category(), orig->id() )
00088 #if PILOT_LINK_NUMBER >= PILOT_LINK_0_12_0
00089     ,
00090     fBuffer(0L)
00091 #endif
00092 {
00093     FUNCTIONSETUPL(4);
00094     fData = new char[orig->size()];
00095 
00096     memcpy(fData, orig->data(), orig->size());
00097     fLen = orig->size();
00098     fAllocated++;
00099 }
00100 
00101 PilotRecord & PilotRecord::operator = (PilotRecord & orig)
00102 {
00103     FUNCTIONSETUP;
00104 #if PILOT_LINK_NUMBER >= PILOT_LINK_0_12_0
00105     if (fBuffer)
00106     {
00107         pi_buffer_free(fBuffer);
00108         fBuffer=0L;
00109         fData=0L;
00110     }
00111 #endif
00112 
00113     if (fData)
00114         delete[]fData;
00115     fData = new char[orig.size()];
00116 
00117     memcpy(fData, orig.data(), orig.size());
00118     fLen = orig.size();
00119     setAttributes( orig.attributes() );
00120     setCategory( orig.category() );
00121     setID( orig.id() );
00122     return *this;
00123 }
00124 
00125 void PilotRecord::setData(const char *data, int len)
00126 {
00127     FUNCTIONSETUP;
00128     if (fData)
00129         delete[]fData;
00130     fData = new char[len];
00131 
00132     memcpy(fData, data, len);
00133     fLen = len;
00134 }
00135 
00136 
00137 /* static */ QTextCodec *PilotAppCategory::pilotCodec = 0L;
00138 
00139 /* static */ QTextCodec *PilotAppCategory::setupPilotCodec(const QString &s)
00140 {
00141     FUNCTIONSETUP;
00142     QString encoding(KGlobal::charsets()->encodingForName(s));
00143 
00144 #ifdef DEBUG
00145     DEBUGKPILOT << fname << ": Creating codec " << encoding << endl;
00146 #endif
00147 
00148     // if the desired codec can't be found, latin1 will be returned anyway, no need to do this manually
00149     pilotCodec = KGlobal::charsets()->codecForName(encoding);
00150 
00151 #ifdef DEBUG
00152     DEBUGKPILOT << fname
00153         << ": Got codec " << codecName() << " for setting "
00154         << s << endl;
00155 #endif
00156     return codec();
00157 }
00158 
00159 /* static */ QString PilotAppCategory::codecName()
00160 {
00161     return QString::fromLatin1(codec()->name());
00162 }
00163 
00164 bool PilotAppCategory::setCategory(struct CategoryAppInfo &info,const QString &label)
00165 {
00166     int emptyAvailable = -1;
00167     if (label.isEmpty()) { setCategory(0); return true; }
00168     for (int catId = 1; catId < PILOT_CATEGORY_MAX; catId++)
00169     {
00170         QString aCat;
00171         if (!info.name[catId][0])
00172         {
00173             emptyAvailable=catId; continue;
00174         }
00175         aCat = codec()->toUnicode(info.name[catId]);
00176         if (label == aCat) { setCategory(catId); return true; }
00177     }
00178     if (emptyAvailable<0) return false;
00179     strlcpy(info.name[emptyAvailable], codec()->fromUnicode(label), sizeof(info.name[emptyAvailable]) );
00180     setCategory(emptyAvailable);
00181     return true;
00182 }
00183 
00184 PilotRecord *PilotAppCategory::pack()
00185 {
00186     int len = PilotRecord::APP_BUFFER_SIZE;
00187     void* buff = new unsigned char[len];
00188     pack_(buff, &len);
00189     PilotRecord* rec =  new PilotRecord(buff, len, attributes(), category(), id());
00190     delete [] (unsigned char*)buff;
00191     return rec;
00192 }
00193 
00194 QString PilotAppCategory::fromPilot( const char *c, int len )
00195 {
00196     return codec()->toUnicode(c,len);
00197 }
00198 
00199 int PilotAppCategory::toPilot( const QString &s, char *buf, int len)
00200 {
00201     int used = len;
00202     QCString cbuf = codec()->fromUnicode(s,used);
00203     memset( buf, 0, len );
00204     if (used > len) used=len;
00205     memcpy( buf, cbuf.data(), used );
00206     return used;
00207 }
KDE Home | KDE Accessibility Home | Description of Access Keys