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 int used = len;
00067 QCString cbuf = codec->fromUnicode(s,used);
00068 memset( buf, 0, len );
00069 if (used > len) used=len;
00070 memcpy( buf, cbuf.data(), used );
00071 return used;
00072 }
00073
00074 int toPilot( const QString &s, unsigned char *buf, int len)
00075 {
00076 int used = len;
00077 QCString cbuf = codec->fromUnicode(s,used);
00078 memset( buf, 0, len );
00079 if (used > len) used=len;
00080 memcpy( buf, cbuf.data(), used );
00081 return used;
00082 }
00083
00084 bool setupPilotCodec(const QString &s)
00085 {
00086 FUNCTIONSETUP;
00087 QString encoding(KGlobal::charsets()->encodingForName(s));
00088
00089 DEBUGLIBRARY << fname << ": Using codec name " << s << endl;
00090 DEBUGLIBRARY << fname << ": Creating codec " << encoding << endl;
00091
00092
00093 codec = KGlobal::charsets()->codecForName(encoding);
00094
00095 if (codec)
00096 {
00097 DEBUGLIBRARY << fname << ": Got codec " << codec->name() << endl;
00098 }
00099
00100 return codec;
00101 }
00102
00103 QString codecName()
00104 {
00105 return QString::fromLatin1(codec->name());
00106 }
00107
00108 QString category(const struct CategoryAppInfo *info, unsigned int i)
00109 {
00110 if (!info || (i>=CATEGORY_COUNT))
00111 {
00112 return QString::null;
00113 }
00114
00115 return codec->toUnicode(info->name[i],CATEGORY_SIZE-1);
00116 }
00117
00118
00119 int findCategory(const struct CategoryAppInfo *info,
00120 const QString &selectedCategory,
00121 bool unknownIsUnfiled)
00122 {
00123 FUNCTIONSETUP;
00124
00125 if (!info)
00126 {
00127 kdError() << k_funcinfo << "! Bad CategoryAppInfo pointer" << endl;
00128 return -1;
00129 }
00130
00131 int currentCatID = -1;
00132 for (unsigned int i=0; i<CATEGORY_COUNT; i++)
00133 {
00134 if (!info->name[i][0]) continue;
00135 if (selectedCategory == category(info, i))
00136 {
00137 currentCatID = i;
00138 break;
00139 }
00140 }
00141
00142 if (-1 == currentCatID)
00143 {
00144 DEBUGLIBRARY << fname << ": Category name "
00145 << selectedCategory << " not found." << endl;
00146 }
00147 else
00148 {
00149 DEBUGLIBRARY << fname << ": Matched category " << currentCatID << endl;
00150 }
00151
00152 if ((currentCatID == -1) && unknownIsUnfiled)
00153 currentCatID = 0;
00154 return currentCatID;
00155 }
00156
00157 int insertCategory(struct CategoryAppInfo *info,
00158 const QString &label,
00159 bool unknownIsUnfiled)
00160 {
00161 int c = findCategory(info,label,unknownIsUnfiled);
00162 if (c<0)
00163 {
00164
00165
00166 for (unsigned int i=0; i<CATEGORY_COUNT; i++)
00167 {
00168 if (!info->name[i][0])
00169 {
00170 c = i;
00171 break;
00172 }
00173 }
00174
00175 if ((c>0) && (c < (int)CATEGORY_COUNT))
00176 {
00177
00178 toPilot(label,info->name[c],CATEGORY_SIZE);
00179 }
00180 else
00181 {
00182 c = -1;
00183 }
00184 }
00185
00186 return c;
00187 }
00188
00189 void dumpCategories(const struct CategoryAppInfo *info)
00190 {
00191 FUNCTIONSETUP;
00192
00193 if (!info)
00194 {
00195 kdWarning() << "! Dumping bad pointer." << endl;
00196 return;
00197 }
00198
00199 DEBUGLIBRARY << fname << " lastUniqueId: "
00200 << (int) info->lastUniqueID << endl;
00201 for (unsigned int i = 0; i < CATEGORY_COUNT; i++)
00202 {
00203 if (!info->name[i][0]) continue;
00204 DEBUGLIBRARY << fname << ": " << i << " = "
00205 << info->ID[i] << " <"
00206 << info->name[i] << ">" << endl;
00207 }
00208 }
00209
00210
00211 }
00212
00213
|