home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / happydays-src-1.37.tar.gz / happydays-src-1.37.tar / happydays-1.37 / todo.c < prev    next >
C/C++ Source or Header  |  2000-07-27  |  12KB  |  426 lines

  1. /*
  2. HappyDays - A Birthdate displayer for the PalmPilot
  3. Copyright (C) 1999-2000 JaeMok Jeong
  4.  
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. */
  19.  
  20. /*
  21.  * Just so you know some of these routines are pulled from the Todo
  22.  * source code as provided by Palm.  At least that is what I started from,
  23.  * I may have touched a few lines to get it to compile with gcc without
  24.  * warnings.
  25.  */
  26.  
  27. #include <PalmOS.h>
  28. #include "todo.h"
  29. #include "util.h"
  30.  
  31. #include "happydays.h"
  32. #include "happydaysRsc.h"
  33.  
  34. /***********************************************************************
  35.  *
  36.  * FUNCTION:    GetToDoNotePtr
  37.  *
  38.  * DESCRIPTION: This routine returns a pointer to the note field in a to 
  39.  *              do record.
  40.  *
  41.  * PARAMETERS:  recordP - pointer to a ToDo record
  42.  *
  43.  * RETURNED:    pointer to a null-terminated note
  44.  *
  45.  * REVISION HISTORY:
  46.  *          Name    Date        Description
  47.  *          ----    ----        -----------
  48.  *          art 4/15/95 Initial Revision
  49.  *
  50.  ***********************************************************************/
  51. Char* GetToDoNotePtr (ToDoDBRecordPtr recordP)
  52. {
  53.     return (&recordP->description + StrLen (&recordP->description) + 1);
  54. }
  55.  
  56. /************************************************************
  57.  *
  58.  *  FUNCTION: DateTypeCmp
  59.  *
  60.  *  DESCRIPTION: Compare two dates
  61.  *
  62.  *  PARAMETERS: 
  63.  *
  64.  *  RETURNS: 
  65.  *
  66.  *  CREATED: 1/20/95 
  67.  *
  68.  *  BY: Roger Flores
  69.  *
  70.  *************************************************************/
  71. static Int16 DateTypeCmp(DateType d1, DateType d2)
  72. {
  73.     Int16 result;
  74.     
  75.     result = d1.year - d2.year;
  76.     if (result != 0)
  77.     {
  78.         if (DateToInt(d1) == 0xffff)
  79.             return 1;
  80.         if (DateToInt(d2) == 0xffff)
  81.             return -1;
  82.         return result;
  83.     }
  84.     
  85.     result = d1.month - d2.month;
  86.     if (result != 0)
  87.         return result;
  88.     
  89.     result = d1.day - d2.day;
  90.  
  91.     return result;
  92.  
  93. }
  94.  
  95. /************************************************************
  96.  *
  97.  *  FUNCTION: ToDoGetSortOrder
  98.  *
  99.  *  DESCRIPTION: This routine gets the sort order value from the 
  100.  *               'to do' application info block.
  101.  *
  102.  *  PARAMETERS: database pointer
  103.  *
  104.  *  RETURNS:    true - if the 'to do' record are sorted by priority, 
  105.  *              false - if the records are sorted by due date.
  106.  *
  107.  * REVISION HISTORY:
  108.  *          Name    Date        Description
  109.  *          ----    ----        -----------
  110.  *          art 5/12/95 Initial Revision
  111.  *       art    3/22/96 Rename routine and added more sort orders
  112.  *
  113.  *************************************************************/
  114. UInt8 ToDoGetSortOrder (DmOpenRef dbP)
  115. {
  116.     UInt8 sortOrder;
  117.     ToDoAppInfoPtr appInfoP;
  118.             
  119.     appInfoP = MemHandleLock (ToDoGetAppInfo (dbP));
  120.     sortOrder = appInfoP->sortOrder;
  121.     MemPtrUnlock (appInfoP);    
  122.  
  123.     return (sortOrder);
  124. }
  125.  
  126. /************************************************************
  127.  *
  128.  *  FUNCTION: ToDoGetAppInfo
  129.  *
  130.  *  DESCRIPTION: Get the app info chunk 
  131.  *
  132.  *  PARAMETERS: database pointer
  133.  *
  134.  *  RETURNS: MemHandle to the to do application info block (ToDoAppInfoType)
  135.  *
  136.  *  CREATED: 5/12/95 
  137.  *
  138.  *  BY: Art Lamb
  139.  *
  140.  *************************************************************/
  141. MemHandle ToDoGetAppInfo (DmOpenRef dbP)
  142. {
  143.     Err error;
  144.     UInt16 cardNo;
  145.     LocalID dbID;
  146.     LocalID appInfoID;
  147.     
  148.     error = DmOpenDatabaseInfo (dbP, &dbID, NULL, NULL, &cardNo, NULL);
  149.     ErrFatalDisplayIf (error,  "Get getting to do app info block");
  150.  
  151.     error = DmDatabaseInfo (cardNo, dbID, NULL, NULL, NULL, NULL, NULL, 
  152.                             NULL, NULL, &appInfoID, NULL, NULL, NULL);
  153.     ErrFatalDisplayIf (error,  "Get getting to do app info block");
  154.  
  155.     return ((MemHandle) MemLocalIDToGlobal (appInfoID, cardNo));
  156. }
  157.  
  158.  
  159. /************************************************************
  160.  *
  161.  *  FUNCTION: CategoryCompare
  162.  *
  163.  *  DESCRIPTION: Compare two categories
  164.  *
  165.  *  PARAMETERS:  attr1, attr2 - record attributes, which contain 
  166.  *                        the category. 
  167.  *                    appInfoH - MemHandle of the applications category info
  168.  *
  169.  *  RETURNS: 0 if they match, non-zero if not
  170.  *               + if s1 > s2
  171.  *               - if s1 < s2
  172.  *
  173.  * REVISION HISTORY:
  174.  *          Name    Date        Description
  175.  *          ----    ----        -----------
  176.  *          art 8/5/96  Initial Revision
  177.  *
  178.  *************************************************************/
  179. static Int16 CategoryCompare (UInt8 attr1, UInt8 attr2, MemHandle appInfoH)
  180. {
  181.     Int16 result;
  182.     UInt8 category1;
  183.     UInt8 category2;
  184.     ToDoAppInfoPtr appInfoP;
  185.     
  186.     
  187.     category1 = attr1 & dmRecAttrCategoryMask;
  188.     category2 = attr2 & dmRecAttrCategoryMask;
  189.  
  190.     result = category1 - category2;
  191.     if (result != 0)
  192.     {
  193.         if (category1 == dmUnfiledCategory)
  194.             return (1);
  195.         else if (category2 == dmUnfiledCategory)
  196.             return (-1);
  197.         
  198.         appInfoP = MemHandleLock (appInfoH);
  199.         result = StrCompare (appInfoP->categoryLabels[category1],
  200.                              appInfoP->categoryLabels[category2]);
  201.  
  202.         MemPtrUnlock (appInfoP);
  203.         return result;
  204.     }
  205.     
  206.     return result;
  207. }
  208.  
  209. /************************************************************
  210.  *
  211.  *  FUNCTION: ToDoCompareRecords
  212.  *
  213.  *  DESCRIPTION: Compare two records.
  214.  *
  215.  *  PARAMETERS: database record 1
  216.  *              database record 2
  217.  *
  218.  *  RETURNS: -n if record one is less (n != 0)
  219.  *            n if record two is less
  220.  *
  221.  *  CREATED: 1/23/95 
  222.  *
  223.  *  BY: Roger Flores
  224.  *
  225.  *  COMMENTS:   Compare the two records key by key until
  226.  *  there is a difference.  Return -n if r1 is less or n if r2
  227.  *  is less.  A zero is never returned because if two records
  228.  *  seem identical then their unique IDs are compared!
  229.  *
  230.  * This function accepts record data chunk pointers to avoid
  231.  * requiring that the record be within the database.  This is
  232.  * important when adding records to a database.  This prevents
  233.  * determining if a record is a deleted record (which are kept
  234.  * at the end of the database and should be considered "greater").
  235.  * The caller should test for deleted records before calling this
  236.  * function!
  237.  *
  238.  *************************************************************/
  239.  
  240. static Int16 ToDoCompareRecords(ToDoDBRecordPtr r1, 
  241.                                 ToDoDBRecordPtr r2, Int16 sortOrder, 
  242.                                 SortRecordInfoPtr info1, SortRecordInfoPtr info2,
  243.                                 MemHandle appInfoH)
  244. {
  245.     Int16 result = 0;
  246.  
  247.     // Sort by priority, due date, and category.
  248.     if (sortOrder == soPriorityDueDate)
  249.     {
  250.         result = (r1->priority & priorityOnly) - (r2->priority & priorityOnly);
  251.         if (result == 0)
  252.         {
  253.             result = DateTypeCmp (r1->dueDate, r2->dueDate);
  254.             if (result == 0)
  255.             {
  256.                 result = CategoryCompare (info1->attributes, info2->attributes, appInfoH);
  257.             }
  258.         }
  259.     }
  260.  
  261.     // Sort by due date, priority, and category.
  262.     else if (sortOrder == soDueDatePriority)
  263.     {
  264.         result = DateTypeCmp(r1->dueDate, r2->dueDate);
  265.         if (result == 0)
  266.         {
  267.             result = (r1->priority & priorityOnly) - (r2->priority & priorityOnly);
  268.             if (result == 0)
  269.             {
  270.                 result = CategoryCompare (info1->attributes, info2->attributes, appInfoH);
  271.             }
  272.         }
  273.     }
  274.     
  275.     // Sort by category, priority, due date
  276.     else if (sortOrder == soCategoryPriority)
  277.     {
  278.         result = CategoryCompare (info1->attributes, info2->attributes, appInfoH);
  279.         if (result == 0)
  280.         {
  281.             result = (r1->priority & priorityOnly) - (r2->priority & priorityOnly);
  282.             if (result == 0)
  283.             {
  284.                 result = DateTypeCmp (r1->dueDate, r2->dueDate);
  285.             }
  286.         }
  287.     }
  288.  
  289.     // Sort by category, due date, priority
  290.     else if (sortOrder == soCategoryDueDate)
  291.     {
  292.         result = CategoryCompare (info1->attributes, info2->attributes, appInfoH);
  293.         if (result == 0)
  294.         {
  295.             result = DateTypeCmp (r1->dueDate, r2->dueDate);
  296.             if (result == 0)
  297.             {
  298.                 result = (r1->priority & priorityOnly) - (r2->priority & priorityOnly);
  299.             }
  300.         }
  301.     }
  302.     
  303.     return result;
  304. }
  305.  
  306. /************************************************************
  307.  *
  308.  *  FUNCTION: ToDoFindSortPosition
  309.  *
  310.  *  DESCRIPTION: Return where a record is or should be.
  311.  *      Useful to find a record or find where to insert a record.
  312.  *
  313.  *  PARAMETERS: database record (not deleted!)
  314.  *
  315.  *  RETURNS: the size in bytes
  316.  *
  317.  *  CREATED: 1/11/95 
  318.  *
  319.  *  BY: Roger Flores
  320.  *
  321.  *************************************************************/
  322. static UInt16 ToDoFindSortPosition(DmOpenRef dbP, ToDoDBRecord *newRecord, 
  323.                                    SortRecordInfoPtr newRecordInfo)
  324. {
  325.     int sortOrder;
  326.     
  327.     sortOrder = ToDoGetSortOrder (dbP);
  328.         
  329.     return (DmFindSortPosition (dbP, newRecord, newRecordInfo, 
  330.                                 (DmComparF *)ToDoCompareRecords, sortOrder));
  331. }
  332.  
  333. /************************************************************
  334.  *
  335.  *  FUNCTION: ToDoNewRecord
  336.  *
  337.  *  DESCRIPTION: Create a new record in sorted position
  338.  *
  339.  *  PARAMETERS: database pointer
  340.  *              database record
  341.  *
  342.  *  RETURNS: ##0 if successful, errorcode if not
  343.  *
  344.  *  CREATED: 1/10/95 
  345.  *
  346.  *  BY: Roger Flores
  347.  *
  348.  *************************************************************/
  349. Err ToDoNewRecord(DmOpenRef dbP, ToDoItemPtr item, UInt16 category, UInt16 *index)
  350. {
  351.     Err                         err;
  352.     UInt16                  size;
  353.     Char                        zero=0;
  354.     UInt16                  attr;
  355.     UInt16                  newIndex;
  356.     UInt32                  offset;
  357.     ToDoDBRecordPtr     nilP=0;
  358.     ToDoDBRecordPtr     recordP;
  359.     MemHandle               recordH;
  360.     SortRecordInfoType  sortInfo;
  361.  
  362.     // Compute the size of the new to do record.
  363.     size = sizeDBRecord;
  364.     if (item->description)
  365.         size += StrLen (item->description);
  366.     if (item->note)
  367.         size += StrLen (item->note);
  368.         
  369.  
  370.     //  Allocate a chunk in the database for the new record.
  371.     recordH = (MemHandle)DmNewHandle(dbP, size);
  372.     if (recordH == NULL)
  373.         return dmErrMemError;
  374.  
  375.     // Pack the the data into the new record.
  376.     recordP = MemHandleLock (recordH);
  377.     DmWrite(recordP, (UInt32)&nilP->dueDate, &item->dueDate, sizeof(nilP->dueDate));
  378.     DmWrite(recordP, (UInt32)&nilP->priority, &item->priority, sizeof(nilP->priority));
  379.  
  380.     offset = (UInt32)&nilP->description;
  381.     if (item->description)
  382.     {
  383.         DmStrCopy(recordP, offset, item->description);
  384.         offset += StrLen (item->description) + 1;
  385.     }
  386.     else
  387.     {
  388.         DmWrite(recordP, offset, &zero, 1);
  389.         offset++;
  390.     }
  391.     
  392.     if (item->note) 
  393.         DmStrCopy(recordP, offset, item->note);
  394.     else
  395.         DmWrite(recordP, offset, &zero, 1);
  396.     
  397.     
  398.     sortInfo.attributes = category;
  399.     sortInfo.uniqueID[0] = 0;
  400.     sortInfo.uniqueID[1] = 0;
  401.     sortInfo.uniqueID[2] = 0;
  402.     
  403.     // Determine the sort position of the new record.
  404.     newIndex = ToDoFindSortPosition (dbP, recordP, &sortInfo);
  405.  
  406.     MemPtrUnlock (recordP);
  407.  
  408.     // Insert the record.
  409.     err = DmAttachRecord(dbP, &newIndex, recordH, 0);
  410.     if (err) 
  411.         MemHandleFree(recordH);
  412.     else
  413.     {
  414.         *index = newIndex;
  415.  
  416.         // Set the category.
  417.         DmRecordInfo (dbP, newIndex, &attr, NULL, NULL);
  418.         attr &= ~dmRecAttrCategoryMask;
  419.         attr |= category;
  420.         DmSetRecordInfo (dbP, newIndex, &attr, NULL);
  421.     }
  422.  
  423.     return err;
  424. }
  425.  
  426.