kpilot/lib

pilot.cc

00001 /* pilot.cc         KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 ** Copyright (C) 2003-2006 Adriaan de Groot <groot@kde.org>
00006 **
00007 ** These are the base class structures that reside on the
00008 ** handheld device -- databases and their parts.
00009 */
00010 
00011 /*
00012 ** This program is free software; you can redistribute it and/or modify
00013 ** it under the terms of the GNU Lesser General Public License as published by
00014 ** the Free Software Foundation; either version 2.1 of the License, or
00015 ** (at your option) any later version.
00016 **
00017 ** This program is distributed in the hope that it will be useful,
00018 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00020 ** GNU Lesser General Public License for more details.
00021 **
00022 ** You should have received a copy of the GNU Lesser General Public License
00023 ** along with this program in a file called COPYING; if not, write to
00024 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 ** MA 02110-1301, USA.
00026 */
00027 
00028 /*
00029 ** Bug reports and questions can be sent to kde-pim@kde.org
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     // See toPilot() below.
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     // Clear the buffer
00081     memset( buf, 0, len );
00082 
00083     // Convert to 8-bit encoding
00084     int used = len;
00085     QCString cbuf = codec->fromUnicode(s,used);
00086 
00087     // Will it fit in the buffer?
00088     if (used > len)
00089     {
00090         // Ought to be impossible, anyway, since 8-bit encodings
00091         // are shorter than the UTF-8 encodings (1 byte per character
00092         // vs. 1-or-more byte per character).
00093         used=len;
00094     }
00095 
00096     // Fill the buffer with encoded data.
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     // if the desired codec can't be found, latin1 will be returned anyway, no need to do this manually
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         // This is the case when the category is not known
00191         // and unknownIsUnfiled is false.
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             // 0 is always unfiled, can't change that.
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 
KDE Home | KDE Accessibility Home | Description of Access Keys