00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00046
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 int PilotRecord::fAllocated = 0;
00056 int PilotRecord::fDeleted = 0;
00057
00058 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 QTextCodec *PilotAppCategory::pilotCodec = 0L;
00138
00139 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
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 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 }