kpilot

pilotAppInfo.h

Go to the documentation of this file.
00001 #ifndef _KPILOT_PILOTAPPINFO_H
00002 #define _KPILOT_PILOTAPPINFO_H
00003 /* pilotAppInfo.h           KPilot
00004 **
00005 ** Copyright (C) 2005-2006 Adriaan de Groot <groot@kde.org>
00006 **
00007 */
00008 
00009 /*
00010 ** This program is free software; you can redistribute it and/or modify
00011 ** it under the terms of the GNU Lesser General Public License as published by
00012 ** the Free Software Foundation; either version 2.1 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018 ** GNU Lesser General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU Lesser General Public License
00021 ** along with this program in a file called COPYING; if not, write to
00022 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 ** MA 02110-1301, USA.
00024 */
00025 
00026 /*
00027 ** Bug reports and questions can be sent to kde-pim@kde.org
00028 */
00029 
00030 #include "pilotLinkVersion.h"
00031 
00032 #include "pilot.h"
00033 #include "pilotDatabase.h"
00034 
00050 class KDE_EXPORT PilotAppInfoBase
00051 {
00052 protected:
00056     void init(struct CategoryAppInfo *c, int len)
00057     {
00058         fC = c;
00059         fLen = len ;
00060     } ;
00061 
00062 public:
00067     PilotAppInfoBase() : fC(0L), fLen(0), fOwn(false) { } ;
00068 
00073     PilotAppInfoBase(PilotDatabase *d);
00074 
00076     virtual ~PilotAppInfoBase();
00077 
00082     struct CategoryAppInfo *categoryInfo()
00083     {
00084         return fC;
00085     } ;
00086 
00088     inline const struct CategoryAppInfo *categoryInfo() const
00089     {
00090         return fC;
00091     } ;
00092 
00094     inline PI_SIZE_T length() const
00095     {
00096         return fLen;
00097     } ;
00098 
00100     inline int findCategory(const QString &name, bool unknownIsUnfiled = false) const
00101     {
00102         return Pilot::findCategory(fC,name,unknownIsUnfiled);
00103     } ;
00104 
00107     inline QString categoryName(unsigned int i) const
00108     {
00109         return Pilot::categoryName(fC,i);
00110     }
00111 
00116     bool setCategoryName(unsigned int i, const QString &s);
00117 
00119     inline void dump() const
00120     {
00121         Pilot::dumpCategories(fC);
00122     };
00123 
00124 protected:
00125     struct CategoryAppInfo *fC;
00126     PI_SIZE_T fLen;
00127 
00128     bool fOwn;
00129 } ;
00130 
00138 template <typename appinfo,
00139 #if PILOT_LINK_IS(0,12,2)
00140     /* There are additional consts introduced in 0.12.2 */
00141     int(*unpack)(appinfo *, const unsigned char *, PI_SIZE_T),
00142     int(*pack)(const appinfo *, unsigned char *, PI_SIZE_T)
00143 #else
00144     int(*unpack)(appinfo *, unsigned char *, PI_SIZE_T),
00145     int(*pack)(appinfo *, unsigned char *, PI_SIZE_T)
00146 #endif
00147     >
00148 class PilotAppInfo : public PilotAppInfoBase
00149 {
00150 public:
00154     PilotAppInfo(PilotDatabase *d) : PilotAppInfoBase()
00155     {
00156         int appLen = Pilot::MAX_APPINFO_SIZE;
00157         unsigned char buffer[Pilot::MAX_APPINFO_SIZE];
00158 
00159         memset(&fInfo,0,sizeof(fInfo));
00160         if (d && d->isOpen())
00161         {
00162             appLen = d->readAppBlock(buffer,appLen);
00163             (*unpack)(&fInfo, buffer, appLen);
00164             // fInfo is just a struct, so we can point to it anyway.
00165             init(&fInfo.category,appLen);
00166         }
00167         else
00168         {
00169             delete fC;
00170             fC = 0L;
00171             fLen = 0;
00172             init(&fInfo.category,sizeof(fInfo));
00173         }
00174     } ;
00175 
00176     PilotAppInfo()
00177     {
00178         memset(&fInfo,0,sizeof(fInfo));
00179         init(&fInfo.category,sizeof(fInfo));
00180     }
00181 
00182 
00187     int writeTo(PilotDatabase *d)
00188     {
00189         unsigned char buffer[Pilot::MAX_APPINFO_SIZE];
00190         if (!d || !d->isOpen())
00191         {
00192             return -1;
00193         }
00194         int appLen = (*pack)(&fInfo, buffer, length());
00195         if (appLen > 0)
00196         {
00197             d->writeAppBlock(buffer,appLen);
00198         }
00199         return appLen;
00200     } ;
00201 
00205     appinfo *info() { return &fInfo; } ;
00209     const appinfo *info() const { return &fInfo; } ;
00210 
00211 protected:
00212     appinfo fInfo;
00213 } ;
00214 
00215 
00216 #endif