kpilot

pilot.h

Go to the documentation of this file.
00001 #ifndef _KPILOT_PILOT_H
00002 #define _KPILOT_PILOT_H
00003 /* KPilot
00004 **
00005 ** Copyright (C) 1998-2001 by Dan Pilone
00006 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00007 ** Copyright (C) 2003-2006 Adriaan de Groot <groot@kde.org>
00008 **
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 <sys/types.h>
00033 
00034 #include <pi-appinfo.h>
00035 #include <pi-buffer.h>
00036 #include <pi-dlp.h>
00037 
00038 #include <qstring.h>
00039 #include <qstringlist.h>
00040 #include <qvaluelist.h>
00041 
00042 #include "pilotLinkVersion.h"
00043 
00044 
00050 class PilotDatabase;     // A database
00051 class PilotRecord;       // ... has records
00052 class PilotCategoryInfo; // ... and category information
00053 
00054 #define MIN(X, Y)  ((X) < (Y) ? (X) : (Y))
00055 
00063 namespace Pilot
00064 {
00066     static const int MAX_APPINFO_SIZE=8192;
00067 
00069     static const unsigned int CATEGORY_COUNT=16;
00070 
00072     static const unsigned int CATEGORY_SIZE=16;
00073 
00075     static const int Unfiled = 0;
00076 
00078     static const int MAX_RECORD_SIZE = 65535;
00079 
00080     typedef QValueList<recordid_t> RecordIDList;
00081 
00087     QString fromPilot( const char *c, int len );
00088 
00095     QString fromPilot( const char *c );
00096 
00102     int toPilot( const QString &s, char *buf, int len);
00103     int toPilot( const QString &s, unsigned char *buf, int len);
00104 
00111     QCString toPilot( const QString &s );
00112 
00120     bool setupPilotCodec(const QString &name);
00121 
00123     QString codecName();
00124 
00128     void dumpCategories(const struct CategoryAppInfo *info);
00129 
00134     inline bool validCategory(int c)
00135     {
00136         if (c<0)
00137         {
00138             return false;
00139         }
00140         return ((unsigned int)c<CATEGORY_COUNT);
00141     }
00142 
00148     inline QString categoryName(const struct CategoryAppInfo *info, unsigned int i)
00149     {
00150         if ( ( i < CATEGORY_COUNT ) && ( info->name[i][0] ) )
00151         {
00152             /*
00153              * Seems to be important that we try to pass the real length here
00154              * to the codec.
00155              */
00156             return fromPilot( info->name[i], MIN(strlen(info->name[i]),CATEGORY_SIZE) );
00157         }
00158         else
00159         {
00160             return QString::null;
00161         }
00162     }
00163 
00169     inline QStringList categoryNames(const struct CategoryAppInfo *info)
00170     {
00171         QStringList l;
00172         if (!info)
00173         {
00174             return l;
00175         }
00176         for (unsigned int i=0; i<CATEGORY_COUNT; ++i)
00177         {
00178             QString s = categoryName(info,i);
00179             if (!s.isEmpty())
00180             {
00181                 l.append(s);
00182             }
00183         }
00184         return l;
00185     }
00186 
00201     int findCategory(const struct CategoryAppInfo *info, const QString &name, bool unknownIsUnfiled);
00202 
00221     int insertCategory(struct CategoryAppInfo *info, const QString &label, bool unknownIsUnfiled);
00222 
00227     static inline bool isResource(struct DBInfo *info)
00228     {
00229         return (info->flags & dlpDBFlagResource);
00230     }
00231 
00232 
00267 template<typename t> struct dlp { } ;
00268 
00269 template<> struct dlp<char>
00270 {
00271     enum { size = 1 };
00272 
00273     static void append(pi_buffer_t *b, char v)
00274     {
00275         pi_buffer_append(b,&v,size);
00276     }
00277 
00282     static char read(const pi_buffer_t *b, unsigned int &offset)
00283     {
00284         if (offset+size > b->used)
00285         {
00286             return 0;
00287         }
00288         char c = b->data[offset];
00289         offset+=size;
00290         return c;
00291     }
00292 } ;
00293 
00294 template<> struct dlp<short>
00295 {
00296     enum { size = 2 };
00297 
00298     static void append(pi_buffer_t *b, short v)
00299     {
00300         char buf[size];
00301         set_short(buf,v);
00302         pi_buffer_append(b,buf,size);
00303     }
00304 
00309     static int read(const pi_buffer_t *b, unsigned int &offset)
00310     {
00311         if (offset+size > b->used)
00312         {
00313             return -1;
00314         }
00315         else
00316         {
00317             int r = get_short(b->data + offset);
00318             offset+=size;
00319             return r;
00320         }
00321     }
00322 
00327     static int read(const unsigned char *b, unsigned int &offset)
00328     {
00329         int r = get_short(b+offset);
00330         offset+=size;
00331         return r;
00332     }
00333 } ;
00334 
00335 template<> struct dlp<long>
00336 {
00337     enum { size = 4 };
00338 
00339     static void append(pi_buffer_t *b, int v)
00340     {
00341         char buf[size];
00342         set_long(buf,v);
00343         pi_buffer_append(b,buf,size);
00344     }
00345 
00350     static int read(const pi_buffer_t *b, unsigned int &offset)
00351     {
00352         if (offset+size > b->used)
00353         {
00354             return -1;
00355         }
00356         else
00357         {
00358             int r = get_long(b->data + offset);
00359             offset+=size;
00360             return r;
00361         }
00362     }
00363 
00368     static int read(const unsigned char *b, unsigned int &offset)
00369     {
00370         int r = get_long(b+offset);
00371         offset+=size;
00372         return r;
00373     }
00374 } ;
00375 
00376 template<> struct dlp<char *>
00377 {
00378     // No size enum, doesn't make sense
00379     // No append, use pi_buffer_append
00386     static int read(const pi_buffer_t *b,
00387         unsigned int &offset,
00388         unsigned char *v,
00389         size_t s)
00390     {
00391         if ( s+offset > b->used )
00392         {
00393             s = b->allocated - offset;
00394         }
00395         memcpy(v, b->data + offset, s);
00396         offset+=s;
00397         return s;
00398     }
00399 
00401     inline static int read(const pi_buffer_t *b, unsigned int &offset, char *v, size_t s)
00402     {
00403         return read(b,offset,(unsigned char *)v,s);
00404     }
00405 } ;
00406 
00407 }
00408 
00409 #endif
00410