kpilot/lib

pilotMemo.cc

00001 /* KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 **
00005 ** This is a C++ wrapper for the Pilot's Memo Pad 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., 51 Franklin Street, Fifth Floor, Boston,
00022 ** MA 02110-1301, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 static const char *pilotMemo_id =
00029     "$Id: pilotMemo.cc 437980 2005-07-23 19:53:57Z kainhofe $";
00030 
00031 #include "options.h"
00032 
00033 #include <qtextcodec.h>
00034 
00035 #include "pilotMemo.h"
00036 #include "pilotDatabase.h"
00037 
00038 
00039 
00040 PilotMemo::PilotMemo(const PilotRecord * rec) : PilotAppCategory(rec)
00041 {
00042     FUNCTIONSETUP;
00043     fText = codec()->toUnicode((const char *)(rec->data()),rec->size());
00044     (void) pilotMemo_id;
00045 }
00046 
00047 void PilotMemo::unpack(const void *text, int /* firstTime */)
00048 {
00049     FUNCTIONSETUP;
00050     kdWarning() << k_funcinfo << ": deprecated and broken function." << endl;
00051     fText = codec()->toUnicode((const char *)text);
00052 }
00053 
00054 PilotRecord *PilotMemo::pack()
00055 {
00056     char *buf = new char[fText.length() + 8];
00057     int len = fText.length() + 8;
00058     pack_(buf,&len);
00059     PilotRecord *r = new PilotRecord(buf, len, attributes(), category(), id());
00060     delete[] buf;
00061     return r;
00062 }
00063 
00064 void *PilotMemo::pack_(void *buf, int *len)
00065 {
00066     FUNCTIONSETUP;
00067     if (!*len) return NULL;
00068     if (*len < 0) return NULL; // buffer size being silly
00069     if (fText.length() > (unsigned) *len) return NULL; // won't fit either
00070 
00071     QCString s = codec()->fromUnicode(fText);
00072 
00073     int use_length = *len;
00074     if (MAX_MEMO_LEN < use_length) use_length = MAX_MEMO_LEN;
00075 
00076     // Zero out the buffer, up to the max memo size.
00077     memset(buf,0,use_length);
00078 
00079     // Copy the encoded string and make extra sure it's NUL terminated.
00080     // Yay, _every_ parameter needs a cast.
00081     // *NOTE* This will truncate the memo text if it was passed in as being
00082     //        too long, but this is better than allowing garbage in
00083     strlcpy(( char *)buf,(const char *)s,use_length);
00084 
00085     // Finally, we set the length of the memo to the used length
00086     // of the data buffer, which might be the length of the string.
00087     if ((int)s.length() < use_length) use_length = s.length()+1;
00088     *len = use_length;
00089     return buf;
00090 }
00091 
00092 
00093 QString PilotMemo::getTextRepresentation(bool richText)
00094 {
00095     if (richText)
00096         return i18n("<i>Title:</i> %1<br>\n<i>MemoText:</i><br>%2").
00097             arg(rtExpand(getTitle(), richText)).arg(rtExpand(text(), richText));
00098     else
00099         return i18n("Title: %1\nMemoText:\n%2").arg(getTitle()).arg(text());
00100 }
00101 
00102 
00103 QString PilotMemo::getTitle() const
00104 {
00105     if (fText.isEmpty()) return QString::null;
00106 
00107     int memoTitleLen = fText.find('\n');
00108     if (-1 == memoTitleLen) memoTitleLen=fText.length();
00109     return fText.left(memoTitleLen);
00110 }
00111 
00112 QString PilotMemo::shortTitle() const
00113 {
00114     FUNCTIONSETUP;
00115     QString t = QString(getTitle()).simplifyWhiteSpace();
00116 
00117     if (t.length() < 32)
00118         return t;
00119     t.truncate(40);
00120 
00121     int spaceIndex = t.findRev(' ');
00122 
00123     if (spaceIndex > 32)
00124     {
00125         t.truncate(spaceIndex);
00126     }
00127 
00128     t += CSL1("...");
00129 
00130     return t;
00131 }
00132 
00133 QString PilotMemo::sensibleTitle() const
00134 {
00135     FUNCTIONSETUP;
00136     QString s = getTitle();
00137 
00138     if (!s.isEmpty())
00139     {
00140         return s;
00141     }
00142     else
00143     {
00144         return i18n("[unknown]");
00145     }
00146 }
00147 
KDE Home | KDE Accessibility Home | Description of Access Keys