kpilot Library API Documentation

pilotAddress.cc

00001 /* pilotAddress.cc                      KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 **
00005 ** This is a C++ wrapper for the pilot's address database structures.
00006 */
00007 
00008 /*
00009 ** This program is free software; you can redistribute it and/or modify
00010 ** it under the terms of the GNU Lesser General Public License as published by
00011 ** the Free Software Foundation; either version 2.1 of the License, or
00012 ** (at your option) any later version.
00013 **
00014 ** This program is distributed in the hope that it will be useful,
00015 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017 ** GNU Lesser General Public License for more details.
00018 **
00019 ** You should have received a copy of the GNU Lesser General Public License
00020 ** along with this program in a file called COPYING; if not, write to
00021 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
00022 ** MA 02111-1307, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 
00029 
00030 static const char *pilotadress_id =
00031         "$Id: pilotAddress.cc,v 1.4.4.3 2003/03/12 23:31:15 adridg Exp $";
00032 
00033 #ifndef _KPILOT_OPTIONS_H
00034 #include "options.h"
00035 #endif
00036 
00037 #include <stdlib.h>
00038 #include <assert.h>
00039 
00040 #include <qtextcodec.h>
00041 
00042 #ifndef _KPILOT_PILOTADDRESS_H
00043 #include "pilotAddress.h"
00044 #endif
00045 
00046 
00047 const int PilotAddress::APP_BUFFER_SIZE = 0xffff;
00048 #define MAXFIELDS 19
00049 
00050 PilotAddress::PilotAddress(struct AddressAppInfo &appInfo,
00051         PilotRecord * rec) :
00052         PilotAppCategory(rec), 
00053         fAppInfo(appInfo),
00054         fAddressInfo()
00055 {
00056         FUNCTIONSETUPL(4);
00057         if (rec) unpack_Address(&fAddressInfo, (unsigned char *) rec->getData(), rec->getLen());
00058         (void) pilotadress_id;
00059 }
00060 
00061 PilotAddress::PilotAddress(struct AddressAppInfo &appInfo) :
00062         PilotAppCategory(),
00063         fAppInfo(appInfo)
00064 {
00065         FUNCTIONSETUPL(4);
00066         reset();
00067 
00068         // assign the phoneLabel so it doesn't appear in the pilot as
00069         // work for all fields, but at least shows other fields
00070         fAddressInfo.phoneLabel[0] = (int) eWork;
00071         fAddressInfo.phoneLabel[1] = (int) eHome;
00072         fAddressInfo.phoneLabel[2] = (int) eOther;
00073         fAddressInfo.phoneLabel[3] = (int) eMobile;
00074         fAddressInfo.phoneLabel[4] = (int) eEmail;
00075 }
00076 
00077 PilotAddress::PilotAddress(const PilotAddress & copyFrom) :
00078         PilotAppCategory(copyFrom),
00079         fAppInfo(copyFrom.fAppInfo), 
00080         fAddressInfo()
00081 {
00082         FUNCTIONSETUPL(4);
00083         _copyAddressInfo(copyFrom.fAddressInfo);
00084 }
00085 
00086 PilotAddress & PilotAddress::operator = (const PilotAddress & copyFrom)
00087 {
00088         FUNCTIONSETUPL(4);
00089         PilotAppCategory::operator = (copyFrom);
00090         _copyAddressInfo(copyFrom.fAddressInfo);
00091         return *this;
00092 }
00093 
00094 bool PilotAddress::operator==(const PilotAddress &compareTo) 
00095 {
00096         FUNCTIONSETUPL(4);
00097         // TODO: call == of PilotAppCategory. I don't think this is necessary, but I'm not so sure...
00098 //      if (!(PilotAppCategory)(this)->operator==(compareTo) ) return false;
00099         
00100         // now compare all the fields stored in the fAddressInfo.entry array of char*[19]
00101         for (int i=0; i<MAXFIELDS; i++) {
00102                 // if one is NULL, and the other non-empty, they are not equal for sure
00103                 if ( !getFieldP(i) && compareTo.getFieldP(i)) return false;
00104                 if ( getFieldP(i) && !compareTo.getFieldP(i)) return false;
00105                 // test for getField(i)!=... to prevent strcmp or NULL strings!  None or both can be zero, but not a single one.
00106                 if ( (getFieldP(i) != compareTo.getFieldP(i)) && ( strcmp(getFieldP(i), compareTo.getFieldP(i)) ) )  return false;
00107         }
00108         return true;
00109 }
00110 
00111 
00112 void PilotAddress::_copyAddressInfo(const struct Address &copyFrom)
00113 {
00114         FUNCTIONSETUPL(4);
00115         fAddressInfo.showPhone = copyFrom.showPhone;
00116 
00117         for (int labelLp = 0; labelLp < 5; labelLp++)
00118         {
00119                 fAddressInfo.phoneLabel[labelLp] =
00120                         copyFrom.phoneLabel[labelLp];
00121         }
00122 
00123         for (int entryLp = 0; entryLp < 19; entryLp++)
00124         {
00125                 if (copyFrom.entry[entryLp])
00126                         fAddressInfo.entry[entryLp] =
00127                                 qstrdup(copyFrom.entry[entryLp]);
00128                 else
00129                         fAddressInfo.entry[entryLp] = 0L;
00130         }
00131 }
00132 
00133 
00134 PilotAddress::~PilotAddress()
00135 {
00136         FUNCTIONSETUPL(4);
00137         free_Address(&fAddressInfo);
00138 }
00139 
00140 bool PilotAddress::setCategory(const QString &label)
00141 {
00142         FUNCTIONSETUPL(4);
00143         for (int catId = 0; catId < 16; catId++)
00144         {
00145                 QString aCat = codec()->toUnicode(fAppInfo.category.name[catId]);
00146 
00147                 if (label == aCat)
00148                 {
00149                         setCat(catId);
00150                         return true;
00151                 }
00152                 else
00153                         // if empty, then no more labels; add it 
00154                 if (aCat.isEmpty())
00155                 {
00156                         qstrncpy(fAppInfo.category.name[catId], 
00157                                 codec()->fromUnicode(label), 16);
00158                         setCat(catId);
00159                         return true;
00160                 }
00161         }
00162         // if got here, the category slots were full
00163         return false;
00164 }
00165 
00166 QString PilotAddress::getCategoryLabel() const
00167 {
00168         return codec()->toUnicode(fAppInfo.category.name[getCat()]);
00169 }
00170 
00171 QString PilotAddress::getField(int field) const
00172 {
00173         return codec()->toUnicode(fAddressInfo.entry[field]);
00174 }
00175 
00176 int PilotAddress::_getNextEmptyPhoneSlot() const
00177 {
00178         FUNCTIONSETUPL(4);
00179         for (int phoneSlot = entryPhone1; phoneSlot <= entryPhone5;
00180                 phoneSlot++)
00181         {
00182                 QString phoneField = getField(phoneSlot);
00183 
00184                 if (phoneField.isEmpty())
00185                         return phoneSlot;
00186         }
00187         return entryCustom4;
00188 }
00189 
00190 void PilotAddress::setPhoneField(EPhoneType type, const QString &field,
00191         bool overflowCustom)
00192 {
00193         FUNCTIONSETUPL(4);
00194         // first look to see if the type is already assigned to a fieldSlot
00195         //QString typeStr(_typeToStr(type));
00196         //int appPhoneLabelNum = _getAppPhoneLabelNum(typeStr);
00197         int appPhoneLabelNum = (int) type;
00198         QString fieldStr(field);
00199         int fieldSlot = _findPhoneFieldSlot(appPhoneLabelNum);
00200 
00201         if (fieldSlot == -1)
00202                 fieldSlot = _getNextEmptyPhoneSlot();
00203 
00204         // store the overflow phone
00205         if (fieldSlot == entryCustom4)
00206         {
00207                 if (!fieldStr.isEmpty() && overflowCustom)
00208                 {
00209                         QString custom4Field = getField(entryCustom4);
00210                         QString typeStr(
00211                                 codec()->toUnicode(fAppInfo.phoneLabels[appPhoneLabelNum]));
00212 
00213                         custom4Field += typeStr + CSL1(" ") + fieldStr;
00214                         setField(entryCustom4, custom4Field);
00215                 }
00216         }
00217         else                    // phone field 1 - 5; straight forward storage
00218         {
00219                 setField(fieldSlot, field);
00220                 int labelIndex = fieldSlot - entryPhone1;
00221 
00222                 fAddressInfo.phoneLabel[labelIndex] = appPhoneLabelNum;
00223         }
00224 }
00225 
00226 int PilotAddress::_findPhoneFieldSlot(int appTypeNum) const
00227 {
00228         FUNCTIONSETUPL(4);
00229         for (int index = 0; index < 5; index++)
00230         {
00231                 if (fAddressInfo.phoneLabel[index] == appTypeNum)
00232                         return index + entryPhone1;
00233         }
00234 
00235         return -1;
00236 }
00237 
00238 QString PilotAddress::getPhoneField(EPhoneType type, bool checkCustom4) const
00239 {
00240         FUNCTIONSETUPL(4);
00241         // given the type, need to find which slot is associated with it
00242         //QString typeToStr(_typeToStr(type));
00243         //int appTypeNum = _getAppPhoneLabelNum(typeToStr);
00244         int appTypeNum = (int) type;
00245 
00246         int fieldSlot = _findPhoneFieldSlot(appTypeNum);
00247 
00248         if (fieldSlot != -1)
00249                 return getField(fieldSlot);
00250 
00251         // look through custom 4 for the field
00252         if (!checkCustom4)
00253                 return QString::null;
00254 
00255         // look for the phone type str
00256         QString typeToStr(codec()->toUnicode(fAppInfo.phoneLabels[appTypeNum]));
00257         QString customField(getField(entryCustom4));
00258         int foundField = customField.find(typeToStr);
00259 
00260         if (foundField == -1)
00261                 return QString::null;
00262 
00263         // parse out the next token
00264         int startPos = foundField + typeToStr.length() + 1;
00265         int endPos = customField.find(' ', startPos);
00266 
00267         if (endPos == -1)
00268                 endPos = customField.length();
00269         QString field = customField.mid(startPos, endPos);
00270 
00271         field = field.simplifyWhiteSpace();
00272 
00273         // return the token
00274         return field;
00275 }
00276 
00277 
00278 int PilotAddress::_getAppPhoneLabelNum(const QString & phoneType) const
00279 {
00280         FUNCTIONSETUPL(4);
00281         for (int index = 0; index < 8; index++)
00282         {
00283                 if (phoneType == codec()->toUnicode(fAppInfo.phoneLabels[index]))
00284                         return index;
00285         }
00286 
00287         return -1;
00288 }
00289 
00290 void PilotAddress::setShownPhone(EPhoneType type)
00291 {
00292         FUNCTIONSETUPL(4);
00293         int appPhoneLabelNum = (int) type;
00294         int fieldSlot = _findPhoneFieldSlot(appPhoneLabelNum);
00295 
00296         if (fieldSlot == -1)
00297         {
00298                 if (type != eHome)
00299                 {
00300                         setShownPhone(eHome);
00301                         return;
00302                 }
00303                 fieldSlot = entryPhone1;
00304         }
00305         fAddressInfo.showPhone = fieldSlot - entryPhone1;
00306 }
00307 
00308 void PilotAddress::setField(int field, const QString &text)
00309 {
00310         FUNCTIONSETUPL(4);
00311         // This will have either been created with unpack_Address, and/or will
00312         // be released with free_Address, so use malloc/free here:
00313         if (fAddressInfo.entry[field])
00314         {
00315                 free(fAddressInfo.entry[field]);
00316         }
00317         if (!text.isEmpty())
00318         {
00319                 fAddressInfo.entry[field] = (char *) malloc(text.length() + 1);
00320                 strcpy(fAddressInfo.entry[field], codec()->fromUnicode(text));
00321         }
00322         else
00323         {
00324                 fAddressInfo.entry[field] = 0L;
00325         }
00326 }
00327 
00328 void *PilotAddress::pack(void *buf, int *len)
00329 {
00330         FUNCTIONSETUPL(4);
00331         int i;
00332 
00333         i = pack_Address(&fAddressInfo, (unsigned char *) buf, *len);
00334         *len = i;
00335         return buf;
00336 }
00337 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:44 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001