kpilot

pilot.cc

Go to the documentation of this file.
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 <qmutex.h>
00036 #include <kcharsets.h>
00037 #include <kglobal.h>
00038 
00039 #include "pilot.h"
00040 #include "pilotDatabase.h"
00041 #include "pilotAppInfo.h"
00042 #include "pilotRecord.h"
00043 
00044 
00045 namespace Pilot
00046 {
00047 static QTextCodec *codec = 0L;
00048 static QMutex* mutex = 0L;
00049 
00050 
00051 QString fromPilot( const char *c, int len )
00052 {
00053     mutex->lock();
00054     QString str = codec->toUnicode(c,len);
00055     mutex->unlock();
00056     return str;
00057 }
00058 
00059 QString fromPilot( const char *c )
00060 {
00061     mutex->lock();
00062     QString str = codec->toUnicode(c);
00063     mutex->unlock();
00064     return str;
00065 }
00066 
00067 QCString toPilot( const QString &s )
00068 {
00069     mutex->lock();
00070     QCString str = codec->fromUnicode(s);
00071     mutex->unlock();
00072     return str;
00073 }
00074 
00075 int toPilot( const QString &s, char *buf, int len)
00076 {
00077     mutex->lock();
00078     // See toPilot() below.
00079     memset( buf, 0, len );
00080     int used = len;
00081     QCString cbuf = codec->fromUnicode(s,used);
00082     if (used > len)
00083     {
00084         used=len;
00085     }
00086     memcpy( buf, cbuf.data(), used );
00087     mutex->unlock();
00088     return used;
00089 }
00090 
00091 int toPilot( const QString &s, unsigned char *buf, int len)
00092 {
00093     mutex->lock();
00094     // Clear the buffer
00095     memset( buf, 0, len );
00096 
00097     // Convert to 8-bit encoding
00098     int used = len;
00099     QCString cbuf = codec->fromUnicode(s,used);
00100 
00101     // Will it fit in the buffer?
00102     if (used > len)
00103     {
00104         // Ought to be impossible, anyway, since 8-bit encodings
00105         // are shorter than the UTF-8 encodings (1 byte per character
00106         // vs. 1-or-more byte per character).
00107         used=len;
00108     }
00109 
00110     // Fill the buffer with encoded data.
00111     memcpy( buf, cbuf.data(), used );
00112     mutex->unlock();
00113     return used;
00114 }
00115 
00116 bool setupPilotCodec(const QString &s)
00117 {
00118     FUNCTIONSETUP;
00119     mutex = new QMutex();
00120     mutex->lock();
00121     QString encoding(KGlobal::charsets()->encodingForName(s));
00122 
00123     DEBUGKPILOT << fname << ": Using codec name " << s << endl;
00124     DEBUGKPILOT << fname << ": Creating codec " << encoding << endl;
00125 
00126     // if the desired codec can't be found, latin1 will be returned anyway, no need to do this manually
00127     codec = KGlobal::charsets()->codecForName(encoding);
00128 
00129     if (codec)
00130     {
00131         DEBUGKPILOT << fname << ": Got codec " << codec->name() << endl;
00132     }
00133 
00134     mutex->unlock();
00135     return codec;
00136 }
00137 
00138 QString codecName()
00139 {
00140     return QString::fromLatin1(codec->name());
00141 }
00142 
00143 QString category(const struct CategoryAppInfo *info, unsigned int i)
00144 {
00145     if (!info || (i>=CATEGORY_COUNT))
00146     {
00147         return QString::null;
00148     }
00149 
00150     mutex->lock();
00151     QString str = codec->toUnicode(info->name[i],
00152                                   MIN(strlen(info->name[i]), CATEGORY_SIZE-1));
00153     mutex->unlock();
00154     return str;
00155 }
00156 
00157 
00158 int findCategory(const struct CategoryAppInfo *info,
00159     const QString &selectedCategory,
00160     bool unknownIsUnfiled)
00161 {
00162     FUNCTIONSETUP;
00163 
00164     if (!info)
00165     {
00166         WARNINGKPILOT << "Bad CategoryAppInfo pointer" << endl;
00167         return -1;
00168     }
00169 
00170     int currentCatID = -1;
00171     for (unsigned int i=0; i<CATEGORY_COUNT; i++)
00172     {
00173         if (!info->name[i][0]) continue;
00174         if (selectedCategory == category(info, i))
00175         {
00176             currentCatID = i;
00177             break;
00178         }
00179     }
00180 
00181     if (-1 == currentCatID)
00182     {
00183         DEBUGKPILOT << fname << ": Category name "
00184             << selectedCategory << " not found." << endl;
00185     }
00186     else
00187     {
00188         DEBUGKPILOT << fname << ": Matched category " << currentCatID << endl;
00189     }
00190 
00191     if ((currentCatID == -1) && unknownIsUnfiled)
00192         currentCatID = 0;
00193     return currentCatID;
00194 }
00195 
00196 int insertCategory(struct CategoryAppInfo *info,
00197     const QString &label,
00198     bool unknownIsUnfiled)
00199 {
00200     FUNCTIONSETUP;
00201 
00202     if (!info)
00203     {
00204         WARNINGKPILOT << "Bad CategoryAppInfo pointer" << endl;
00205         return -1;
00206     }
00207 
00208 
00209     int c = findCategory(info,label,unknownIsUnfiled);
00210     if (c<0)
00211     {
00212         // This is the case when the category is not known
00213         // and unknownIsUnfiled is false.
00214         for (unsigned int i=0; i<CATEGORY_COUNT; i++)
00215         {
00216             if (!info->name[i][0])
00217             {
00218                 c = i;
00219                 break;
00220             }
00221         }
00222 
00223         if ((c>0) && (c < (int)CATEGORY_COUNT))
00224         {
00225             // 0 is always unfiled, can't change that.
00226             toPilot(label,info->name[c],CATEGORY_SIZE);
00227         }
00228         else
00229         {
00230             WARNINGKPILOT << "Category name "
00231                 << label
00232                 << " could not be added." << endl;
00233             c = -1;
00234         }
00235     }
00236 
00237     return c;
00238 }
00239 
00240 void dumpCategories(const struct CategoryAppInfo *info)
00241 {
00242     FUNCTIONSETUP;
00243 
00244     if (!info)
00245     {
00246         WARNINGKPILOT << "Dumping bad pointer." << endl;
00247         return;
00248     }
00249 
00250     DEBUGKPILOT << fname << " lastUniqueId: "
00251         << (int) info->lastUniqueID << endl;
00252     for (unsigned int i = 0; i < CATEGORY_COUNT; i++)
00253     {
00254         if (!info->name[i][0]) continue;
00255         DEBUGKPILOT << fname << ": " << i << " = "
00256             << (int)(info->ID[i]) << " <"
00257             << info->name[i] << ">" << endl;
00258     }
00259 }
00260 
00261 
00262 }
00263 
00264