kpilot

pilotTodoEntry.cc

Go to the documentation of this file.
00001 /* KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 **
00006 ** This is a C++ wrapper for the todo-list entry structures.
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 #include "options.h"
00030 
00031 
00032 #include <stdlib.h>
00033 
00034 #include <qdatetime.h>
00035 #include <qnamespace.h>
00036 
00037 #include <kglobal.h>
00038 #include <kdebug.h>
00039 
00040 
00041 #include "pilotTodoEntry.h"
00042 
00043 
00044 PilotTodoEntry::PilotTodoEntry() :
00045     fDescriptionSize(0),
00046     fNoteSize(0)
00047 {
00048     FUNCTIONSETUP;
00049     ::memset(&fTodoInfo, 0, sizeof(struct ToDo));
00050 }
00051 
00052 PilotTodoEntry::PilotTodoEntry(PilotRecord * rec) :
00053     PilotRecordBase(rec),
00054     fDescriptionSize(0),
00055     fNoteSize(0)
00056 {
00057     ::memset(&fTodoInfo, 0, sizeof(struct ToDo));
00058     if (rec)
00059     {
00060         pi_buffer_t b;
00061         b.data = (unsigned char *) rec->data();
00062         b.allocated = b.used = rec->size();
00063         unpack_ToDo(&fTodoInfo, &b, todo_v1);
00064         if (fTodoInfo.description)
00065         {
00066             // Assume size of buffer allocated is just large enough;
00067             // count trailing NUL as well.
00068             fDescriptionSize = strlen(fTodoInfo.description)+1;
00069         }
00070         if (fTodoInfo.note)
00071         {
00072             // Same
00073             fNoteSize = strlen(fTodoInfo.note)+1;
00074         }
00075     }
00076 
00077 }
00078 
00079 
00080 PilotTodoEntry::PilotTodoEntry(const PilotTodoEntry & e) :
00081     PilotRecordBase( &e ),
00082     fDescriptionSize(0),
00083     fNoteSize(0)
00084 {
00085     FUNCTIONSETUP;
00086     ::memcpy(&fTodoInfo, &e.fTodoInfo, sizeof(fTodoInfo));
00087     // See PilotDateEntry::operator = for details
00088     fTodoInfo.description = 0L;
00089     fTodoInfo.note = 0L;
00090 
00091     setDescriptionP(e.getDescriptionP());
00092     setNoteP(e.getNoteP());
00093 }
00094 
00095 
00096 PilotTodoEntry & PilotTodoEntry::operator = (const PilotTodoEntry & e)
00097 {
00098     if (this != &e)
00099     {
00100         KPILOT_FREE(fTodoInfo.description);
00101         KPILOT_FREE(fTodoInfo.note);
00102 
00103         ::memcpy(&fTodoInfo, &e.fTodoInfo, sizeof(fTodoInfo));
00104         // See PilotDateEntry::operator = for details
00105         fTodoInfo.description = 0L;
00106         fTodoInfo.note = 0L;
00107         fDescriptionSize = 0;
00108         fNoteSize = 0;
00109 
00110         setDescriptionP(e.getDescriptionP());
00111         setNoteP(e.getNoteP());
00112 
00113     }
00114 
00115     return *this;
00116 }
00117 
00118 QString PilotTodoEntry::getTextRepresentation(Qt::TextFormat richText)
00119 {
00120     QString text, tmp;
00121     QString par = (richText==Qt::RichText) ?CSL1("<p>"): QString();
00122     QString ps = (richText==Qt::RichText) ?CSL1("</p>"):CSL1("\n");
00123     QString br = (richText==Qt::RichText) ?CSL1("<br/>"):CSL1("\n");
00124 
00125     // title + name
00126     text += par;
00127     tmp= (richText==Qt::RichText) ?CSL1("<b><big>%1</big></b>"):CSL1("%1");
00128     text += tmp.arg(rtExpand(getDescription(), richText));
00129     text += ps;
00130 
00131     text += par;
00132     if (getComplete())
00133         text += i18n("Completed");
00134     else
00135         text += i18n("Not completed");
00136     text += ps;
00137 
00138     if (!getIndefinite())
00139     {
00140         QDate dt(readTm(getDueDate()).date());
00141         QString dueDate(dt.toString(Qt::LocalDate));
00142         text+=par;
00143         text+=i18n("Due date: %1").arg(dueDate);
00144         text+=ps;
00145     }
00146 
00147     text+=par;
00148     text+=ps;
00149 
00150     text+=par;
00151     text+=i18n("Priority: %1").arg(getPriority());
00152     text+=ps;
00153 
00154     if (!getNote().isEmpty())
00155     {
00156         text += (richText==Qt::RichText) ?CSL1("<hr/>"):CSL1("-------------------------\n");
00157         text+=par;
00158         text+= (richText==Qt::RichText) ?i18n("<b><em>Note:</em></b><br>"):i18n("Note:\n");
00159         text+=rtExpand(getNote(), richText);
00160         text+=ps;
00161     }
00162 
00163     return text;
00164 }
00165 
00166 PilotRecord *PilotTodoEntry::pack() const
00167 {
00168     int i;
00169 
00170     pi_buffer_t *b = pi_buffer_new( sizeof(fTodoInfo) );
00171     i = pack_ToDo(const_cast<ToDo_t *>(&fTodoInfo), b, todo_v1);
00172     if (i<0)
00173     {
00174         return 0;
00175     }
00176     // pack_ToDo sets b->used
00177     return new PilotRecord( b, this );
00178 }
00179 
00180 void PilotTodoEntry::setDescription(const QString &desc)
00181 {
00182     if (desc.length() < fDescriptionSize)
00183     {
00184         Pilot::toPilot(desc, fTodoInfo.description, fDescriptionSize);
00185     }
00186     else
00187     {
00188         setDescriptionP(Pilot::toPilot(desc),desc.length());
00189     }
00190 }
00191 
00192 void PilotTodoEntry::setDescriptionP(const char *desc, int len)
00193 {
00194     KPILOT_FREE(fTodoInfo.description);
00195     if (desc && *desc)
00196     {
00197         if (-1 == len)
00198         {
00199             len=::strlen(desc);
00200         }
00201 
00202         fDescriptionSize = len+1;
00203         fTodoInfo.description = (char *)::malloc(len + 1);
00204         if (fTodoInfo.description)
00205         {
00206             strncpy(fTodoInfo.description, desc, len);
00207             fTodoInfo.description[len] = 0;
00208         }
00209         else
00210         {
00211             WARNINGKPILOT << "malloc() failed, description not set"
00212                 << endl;
00213         }
00214     }
00215     else
00216     {
00217         fTodoInfo.description = 0L;
00218     }
00219 }
00220 
00221 QString PilotTodoEntry::getDescription() const
00222 {
00223     return Pilot::fromPilot(getDescriptionP());
00224 }
00225 
00226 void PilotTodoEntry::setNote(const QString &note)
00227 {
00228     if (note.length() < fNoteSize)
00229     {
00230         Pilot::toPilot(note, fTodoInfo.note, fNoteSize);
00231     }
00232     else
00233     {
00234         setNoteP(Pilot::toPilot(note),note.length());
00235     }
00236 }
00237 
00238 void PilotTodoEntry::setNoteP(const char *note, int len)
00239 {
00240     KPILOT_FREE(fTodoInfo.note);
00241     if (note && *note)
00242     {
00243         if (-1 == len)
00244         {
00245             len=::strlen(note);
00246         }
00247 
00248         fNoteSize = len+1;
00249         fTodoInfo.note = (char *)::malloc(len + 1);
00250         if (fTodoInfo.note)
00251         {
00252             strncpy(fTodoInfo.note, note, len);
00253             fTodoInfo.note[len] = 0;
00254         }
00255         else
00256         {
00257             WARNINGKPILOT << "malloc() failed, note not set" << endl;
00258         }
00259     }
00260     else
00261     {
00262         fTodoInfo.note = 0L;
00263     }
00264 }
00265 
00266 QString PilotTodoEntry::getNote() const
00267 {
00268     return Pilot::fromPilot(getNoteP());
00269 }
00270