kpilot/lib
pilot.cc00001
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 #include "options.h"
00033
00034 #include <qtextcodec.h>
00035 #include <kcharsets.h>
00036 #include <kglobal.h>
00037
00038 #include "pilot.h"
00039 #include "pilotDatabase.h"
00040 #include "pilotAppInfo.h"
00041 #include "pilotRecord.h"
00042
00043
00044 namespace Pilot
00045 {
00046 static QTextCodec *codec = 0L;
00047
00048
00049 QString fromPilot( const char *c, int len )
00050 {
00051 return codec->toUnicode(c,len);
00052 }
00053
00054 QString fromPilot( const char *c )
00055 {
00056 return codec->toUnicode(c);
00057 }
00058
00059 QCString toPilot( const QString &s )
00060 {
00061 return codec->fromUnicode(s);
00062 }
00063
00064 int toPilot( const QString &s, char *buf, int len)
00065 {
00066
00067 memset( buf, 0, len );
00068 int used = len;
00069 QCString cbuf = codec->fromUnicode(s,used);
00070 if (used > len)
00071 {
00072 used=len;
00073 }
00074 memcpy( buf, cbuf.data(), used );
00075 return used;
00076 }
00077
00078 int toPilot( const QString &s, unsigned char *buf, int len)
00079 {
00080
00081 memset( buf, 0, len );
00082
00083
00084 int used = len;
00085 QCString cbuf = codec->fromUnicode(s,used);
00086
00087
00088 if (used > len)
00089 {
00090
00091
00092
00093 used=len;
00094 }
00095
00096
00097 memcpy( buf, cbuf.data(), used );
00098 return used;
00099 }
00100
00101 bool setupPilotCodec(const QString &s)
00102 {
00103 FUNCTIONSETUP;
00104 QString encoding(KGlobal::charsets()->encodingForName(s));
00105
00106 DEBUGLIBRARY << fname << ": Using codec name " << s << endl;
00107 DEBUGLIBRARY << fname << ": Creating codec " << encoding << endl;
00108
00109
00110 codec = KGlobal::charsets()->codecForName(encoding);
00111
00112 if (codec)
00113 {
00114 DEBUGLIBRARY << fname << ": Got codec " << codec->name() << endl;
00115 }
00116
00117 return codec;
00118 }
00119
00120 QString codecName()
00121 {
00122 return QString::fromLatin1(codec->name());
00123 }
00124
00125 QString category(const struct CategoryAppInfo *info, unsigned int i)
00126 {
00127 if (!info || (i>=CATEGORY_COUNT))
00128 {
00129 return QString::null;
00130 }
00131
00132 return codec->toUnicode(info->name[i],CATEGORY_SIZE-1);
00133 }
00134
00135
00136 int findCategory(const struct CategoryAppInfo *info,
00137 const QString &selectedCategory,
00138 bool unknownIsUnfiled)
00139 {
00140 FUNCTIONSETUP;
00141
00142 if (!info)
00143 {
00144 kdError() << k_funcinfo << "! Bad CategoryAppInfo pointer" << endl;
00145 return -1;
00146 }
00147
00148 int currentCatID = -1;
00149 for (unsigned int i=0; i<CATEGORY_COUNT; i++)
00150 {
00151 if (!info->name[i][0]) continue;
00152 if (selectedCategory == category(info, i))
00153 {
00154 currentCatID = i;
00155 break;
00156 }
00157 }
00158
00159 if (-1 == currentCatID)
00160 {
00161 DEBUGLIBRARY << fname << ": Category name "
00162 << selectedCategory << " not found." << endl;
00163 }
00164 else
00165 {
00166 DEBUGLIBRARY << fname << ": Matched category " << currentCatID << endl;
00167 }
00168
00169 if ((currentCatID == -1) && unknownIsUnfiled)
00170 currentCatID = 0;
00171 return currentCatID;
00172 }
00173
00174 int insertCategory(struct CategoryAppInfo *info,
00175 const QString &label,
00176 bool unknownIsUnfiled)
00177 {
00178 FUNCTIONSETUP;
00179
00180 if (!info)
00181 {
00182 kdError() << k_funcinfo << "! Bad CategoryAppInfo pointer" << endl;
00183 return -1;
00184 }
00185
00186
00187 int c = findCategory(info,label,unknownIsUnfiled);
00188 if (c<0)
00189 {
00190
00191
00192 for (unsigned int i=0; i<CATEGORY_COUNT; i++)
00193 {
00194 if (!info->name[i][0])
00195 {
00196 c = i;
00197 break;
00198 }
00199 }
00200
00201 if ((c>0) && (c < (int)CATEGORY_COUNT))
00202 {
00203
00204 toPilot(label,info->name[c],CATEGORY_SIZE);
00205 }
00206 else
00207 {
00208 kdWarning() << k_funcinfo << "! Category name "
00209 << label
00210 << " could not be added." << endl;
00211 c = -1;
00212 }
00213 }
00214
00215 return c;
00216 }
00217
00218 void dumpCategories(const struct CategoryAppInfo *info)
00219 {
00220 FUNCTIONSETUP;
00221
00222 if (!info)
00223 {
00224 kdWarning() << "! Dumping bad pointer." << endl;
00225 return;
00226 }
00227
00228 DEBUGLIBRARY << fname << " lastUniqueId: "
00229 << (int) info->lastUniqueID << endl;
00230 for (unsigned int i = 0; i < CATEGORY_COUNT; i++)
00231 {
00232 if (!info->name[i][0]) continue;
00233 DEBUGLIBRARY << fname << ": " << i << " = "
00234 << (int)(info->ID[i]) << " <"
00235 << info->name[i] << ">" << endl;
00236 }
00237 }
00238
00239
00240 }
00241
00242
|