home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / progect-src-0.20.tar.gz / progect-src-0.20.tar / progect-0.20 / ToDoDB.c < prev    next >
C/C++ Source or Header  |  2000-10-26  |  27KB  |  1,026 lines

  1. /******************************************************************************
  2.  *
  3.  * Copyright (c) 1995-1999 Palm Computing, Inc. or its subsidiaries.
  4.  * All rights reserved.
  5.  *
  6.  * File: ToDoDB.c
  7.  *
  8.  * Description:
  9.  *        To Do Manager routines
  10.  *
  11.  * History:
  12.  *       1/20/95  rsf - Created
  13.  *        10/4/99    jmp - Added ToDoGetDatabase() routine.
  14.  *        10/4/99    rbb - Moved SetDBBackupBit from ToDo.c (used by
  15.  *                            ToDoGetDatabase) and renamed as ToDoSetDBBackupBit
  16.  *
  17.  *****************************************************************************/
  18.  
  19. // Set this to get to private database defines
  20. #define __TODOMGR_PRIVATE__
  21.  
  22. #include <PalmOS.h>
  23. #include "progect.h"
  24. #include "progectRsc.h"
  25.  
  26. #include "ToDo.h"
  27. #include "ToDoDB.h"
  28.  
  29. // Export error checking routines
  30. //void ECToDoDBValidate(DmOpenRef dbP);
  31.  
  32.  
  33. /************************************************************
  34.  * Private routines used only in this module
  35.  *************************************************************/
  36.  
  37.  
  38. /************************************************************
  39.  *
  40.  *  FUNCTION: ECToDoDBValidate
  41.  *
  42.  *  DESCRIPTION: This routine validates the integrity of the to do
  43.  *        datebase.
  44.  *
  45.  *  PARAMETERS: database pointer
  46.  *
  47.  *  RETURNS: nothing
  48.  *
  49.  *  CREATED: 6/22/95 
  50.  *
  51.  *  BY: Art Lamb
  52.  *
  53.  *************************************************************/
  54. #if ERROR_CHECK_LEVEL == ERROR_CHECK_FULL
  55.  
  56. #define maxDescLen    256
  57. #define maxNoteLen    4000
  58.  
  59. //void ECToDoDBValidate (DmOpenRef dbP)
  60. //{
  61. //    UInt16 i;
  62. //    UInt16 size;
  63. //    UInt16 cLen;
  64. //    UInt16 recSize;
  65. //    UInt16 descLen;
  66. //    UInt16 noteLen;
  67. //    UInt16 numRecord;
  68. //    UInt16 priority;
  69. //    Char * c;
  70. //    Char * note;
  71. //    MemHandle recH;
  72. //    ToDoDBRecordPtr rec;
  73. //    
  74. //    numRecord = DmNumRecords (dbP);
  75. //    for (i = 0 ; i < numRecord; i++)
  76. //        {
  77. //        recH = DmQueryRecord (dbP, i);
  78. //        if (! recH) continue;
  79. //
  80. //        rec = MemHandleLock (recH);
  81. //        
  82. //        priority = rec->priority & priorityOnly;
  83. //        ErrFatalDisplayIf (priority > toDoMaxPriority,  "DB integrity error");
  84. //        
  85. //        descLen = StrLen (&rec->description);
  86. //        ErrFatalDisplayIf (descLen > maxDescLen, "DB integrity error");
  87. //
  88. //        note = &rec->description + descLen + 1;
  89. //        noteLen = StrLen (note);
  90. //        ErrFatalDisplayIf (descLen > maxNoteLen, "DB integrity error");
  91. //        
  92. //        // Validate the record size.
  93. //        size = sizeof (DateType) + 1;
  94. //        c = &rec->description;
  95. //        cLen = StrLen(c) + 1;
  96. //        size += cLen;
  97. //        c += cLen;
  98. //        size += StrLen(c) + 1;
  99. //        recSize = MemPtrSize (rec);
  100. //        ErrFatalDisplayIf ( (recSize != size), "DB integrity error");
  101. //
  102. //        MemPtrUnlock (rec);
  103. //        }
  104. //}
  105. #endif
  106.  
  107. /************************************************************
  108.  *
  109.  *  FUNCTION: DateTypeCmp
  110.  *
  111.  *  DESCRIPTION: Compare two dates
  112.  *
  113.  *  PARAMETERS: 
  114.  *
  115.  *  RETURNS: 
  116.  *
  117.  *  CREATED: 1/20/95 
  118.  *
  119.  *  BY: Roger Flores
  120.  *
  121.  *************************************************************/
  122. static Int16 DateTypeCmp(DateType d1, DateType d2)
  123. {
  124.     Int16 result;
  125.     
  126.     result = d1.year - d2.year;
  127.     if (result != 0)
  128.         {
  129.         if (DateToInt(d1) == 0xffff)
  130.             return 1;
  131.         if (DateToInt(d2) == 0xffff)
  132.             return -1;
  133.         return result;
  134.         }
  135.     
  136.     result = d1.month - d2.month;
  137.     if (result != 0)
  138.         return result;
  139.     
  140.     result = d1.day - d2.day;
  141.  
  142.     return result;
  143.  
  144. }
  145.  
  146.  
  147. /************************************************************
  148.  *
  149.  *  FUNCTION: CategoryCompare
  150.  *
  151.  *  DESCRIPTION: Compare two categories
  152.  *
  153.  *  PARAMETERS:  attr1, attr2 - record attributes, which contain 
  154.  *                          the category. 
  155.  *                      appInfoH - MemHandle of the applications category info
  156.  *
  157.  *  RETURNS: 0 if they match, non-zero if not
  158.  *                 + if s1 > s2
  159.  *                 - if s1 < s2
  160.  *
  161.  * REVISION HISTORY:
  162.  *            Name    Date        Description
  163.  *            ----    ----        -----------
  164.  *            art    8/5/96    Initial Revision
  165.  *
  166.  *************************************************************/
  167. static Int16 CategoryCompare (UInt8 attr1, UInt8 attr2, MemHandle appInfoH)
  168. {
  169.     Int16 result;
  170.     UInt8 category1;
  171.     UInt8 category2;
  172.     ToDoAppInfoPtr appInfoP;
  173.     
  174.     
  175.     category1 = attr1 & dmRecAttrCategoryMask;
  176.     category2 = attr2 & dmRecAttrCategoryMask;
  177.  
  178.     result = category1 - category2;
  179.     if (result != 0)
  180.         {
  181.         if (category1 == dmUnfiledCategory)
  182.             return (1);
  183.         else if (category2 == dmUnfiledCategory)
  184.             return (-1);
  185.         
  186.         appInfoP = MemHandleLock (appInfoH);
  187.         result = StrCompare (appInfoP->categoryLabels[category1],
  188.                                     appInfoP->categoryLabels[category2]);
  189.  
  190.         MemPtrUnlock (appInfoP);
  191.         return result;
  192.         }
  193.     
  194.     return result;
  195. }
  196.  
  197.  
  198. /************************************************************
  199.  *
  200.  *  FUNCTION: ToDoAppInfoInit
  201.  *
  202.  *  DESCRIPTION: Create an app info chunk if missing.  Set
  203.  *        the strings to a default.
  204.  *
  205.  *  PARAMETERS: database pointer
  206.  *
  207.  *  RETURNS: 0 if successful, errorcode if not
  208.  *
  209.  *  CREATED: 1/20/95 
  210.  *
  211.  *  BY: Roger Flores
  212.  *
  213.  *************************************************************/
  214. //Err    ToDoAppInfoInit(DmOpenRef dbP)
  215. //{
  216. //    UInt16 cardNo;
  217. //    UInt16 wordValue;
  218. //    MemHandle h;
  219. //    LocalID dbID;
  220. //    LocalID appInfoID;
  221. //    ToDoAppInfoPtr    nilP = 0;
  222. //    ToDoAppInfoPtr appInfoP;
  223. //    
  224. //    if (DmOpenDatabaseInfo(dbP, &dbID, NULL, NULL, &cardNo, NULL))
  225. //        return dmErrInvalidParam;
  226. //
  227. //    if (DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, 
  228. //         NULL, &appInfoID, NULL, NULL, NULL))
  229. //        return dmErrInvalidParam;
  230. //    
  231. //    if (appInfoID == NULL)
  232. //        {
  233. //        h = DmNewHandle(dbP, sizeof (ToDoAppInfoType));
  234. //        if (! h) return dmErrMemError;
  235. //
  236. //        appInfoID = MemHandleToLocalID (h);
  237. //        DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, 
  238. //            NULL, &appInfoID, NULL, NULL, NULL);
  239. //        }
  240. //    
  241. //    appInfoP = MemLocalIDToLockedPtr(appInfoID, cardNo);
  242. //
  243. //    // Clear the app info block.
  244. //    DmSet (appInfoP, 0, sizeof(ToDoAppInfoType), 0);
  245. //
  246. //    // Initialize the categories.
  247. //    CategoryInitialize ((AppInfoPtr) appInfoP, LocalizedAppInfoStr);
  248. //
  249. //    // I don't know what this field is used for.
  250. //    wordValue = 0xFFFF;
  251. //    DmWrite (appInfoP, (UInt32)&nilP->dirtyAppInfo, &wordValue,
  252. //        sizeof(appInfoP->dirtyAppInfo));
  253. //
  254. //    // Initialize the sort order.
  255. //    DmSet (appInfoP, (UInt32)&nilP->sortOrder, sizeof(appInfoP->sortOrder), 
  256. //        soPriorityDueDate);
  257. //
  258. //    MemPtrUnlock (appInfoP);
  259. //
  260. //    return 0;
  261. //}
  262.  
  263.  
  264. /************************************************************
  265.  *
  266.  *  FUNCTION: ToDoGetAppInfo
  267.  *
  268.  *  DESCRIPTION: Get the app info chunk 
  269.  *
  270.  *  PARAMETERS: database pointer
  271.  *
  272.  *  RETURNS: MemHandle to the to do application info block (ToDoAppInfoType)
  273.  *
  274.  *  CREATED: 5/12/95 
  275.  *
  276.  *  BY: Art Lamb
  277.  *
  278.  *************************************************************/
  279. MemHandle ToDoGetAppInfo (DmOpenRef dbP)
  280. {
  281.     Err error;
  282.     UInt16 cardNo;
  283.     LocalID dbID;
  284.     LocalID appInfoID;
  285.     
  286.     error = DmOpenDatabaseInfo (dbP, &dbID, NULL, NULL, &cardNo, NULL);
  287.     ErrFatalDisplayIf (error,  "Get getting to do app info block");
  288.  
  289.     error = DmDatabaseInfo (cardNo, dbID, NULL, NULL, NULL, NULL, NULL, 
  290.             NULL, NULL, &appInfoID, NULL, NULL, NULL);
  291.     ErrFatalDisplayIf (error,  "Get getting to do app info block");
  292.  
  293.     return ((MemHandle) MemLocalIDToGlobal (appInfoID, cardNo));
  294. }
  295.  
  296.  
  297. /************************************************************
  298.  *
  299.  *  FUNCTION: ToDoGetSortOrder
  300.  *
  301.  *  DESCRIPTION: This routine gets the sort order value from the 
  302.  *               'to do' application info block.
  303.  *
  304.  *  PARAMETERS: database pointer
  305.  *
  306.  *  RETURNS:    true - if the 'to do' record are sorted by priority, 
  307.  *              false - if the records are sorted by due date.
  308.  *
  309.  * REVISION HISTORY:
  310.  *            Name    Date        Description
  311.  *            ----    ----        -----------
  312.  *            art    5/12/95    Initial Revision
  313.  *       art    3/22/96    Rename routine and added more sort orders
  314.  *
  315.  *************************************************************/
  316. UInt8 ToDoGetSortOrder (DmOpenRef dbP)
  317. {
  318.     UInt8 sortOrder;
  319.     ToDoAppInfoPtr appInfoP;
  320.             
  321.     appInfoP = MemHandleLock (ToDoGetAppInfo (dbP));
  322.     sortOrder = appInfoP->sortOrder;
  323.     MemPtrUnlock (appInfoP);    
  324.  
  325.     return (sortOrder);
  326. }
  327.  
  328.  
  329. /************************************************************
  330.  *
  331.  *  FUNCTION: ToDoCompareRecords
  332.  *
  333.  *  DESCRIPTION: Compare two records.
  334.  *
  335.  *  PARAMETERS: database record 1
  336.  *                database record 2
  337.  *
  338.  *  RETURNS: -n if record one is less (n != 0)
  339.  *              n if record two is less
  340.  *
  341.  *  CREATED: 1/23/95 
  342.  *
  343.  *  BY: Roger Flores
  344.  *
  345.  *    COMMENTS:    Compare the two records key by key until
  346.  *    there is a difference.  Return -n if r1 is less or n if r2
  347.  *    is less.  A zero is never returned because if two records
  348.  *    seem identical then their unique IDs are compared!
  349.  *
  350.  * This function accepts record data chunk pointers to avoid
  351.  * requiring that the record be within the database.  This is
  352.  * important when adding records to a database.  This prevents
  353.  * determining if a record is a deleted record (which are kept
  354.  * at the end of the database and should be considered "greater").
  355.  * The caller should test for deleted records before calling this
  356.  * function!
  357.  *
  358.  *************************************************************/
  359.  
  360. static Int16 ToDoCompareRecords(ToDoDBRecordPtr r1, 
  361.     ToDoDBRecordPtr r2, Int16 sortOrder, 
  362.     SortRecordInfoPtr info1, SortRecordInfoPtr info2,
  363.     MemHandle appInfoH)
  364. {
  365.     Int16 result = 0;
  366.  
  367.     // Sort by priority, due date, and category.
  368.     if (sortOrder == soPriorityDueDate)
  369.         {
  370.         result = (r1->priority & priorityOnly) - (r2->priority & priorityOnly);
  371.         if (result == 0)
  372.             {
  373.             result = DateTypeCmp (r1->dueDate, r2->dueDate);
  374.             if (result == 0)
  375.                 {
  376.                 result = CategoryCompare (info1->attributes, info2->attributes, appInfoH);
  377.                 }
  378.             }
  379.         }
  380.  
  381.     // Sort by due date, priority, and category.
  382.     else if (sortOrder == soDueDatePriority)
  383.         {
  384.         result = DateTypeCmp(r1->dueDate, r2->dueDate);
  385.         if (result == 0)
  386.             {
  387.             result = (r1->priority & priorityOnly) - (r2->priority & priorityOnly);
  388.             if (result == 0)
  389.                 {
  390.                 result = CategoryCompare (info1->attributes, info2->attributes, appInfoH);
  391.                 }
  392.             }
  393.         }
  394.     
  395.     // Sort by category, priority, due date
  396.     else if (sortOrder == soCategoryPriority)
  397.         {
  398.         result = CategoryCompare (info1->attributes, info2->attributes, appInfoH);
  399.         if (result == 0)
  400.             {
  401.             result = (r1->priority & priorityOnly) - (r2->priority & priorityOnly);
  402.             if (result == 0)
  403.                 {
  404.                 result = DateTypeCmp (r1->dueDate, r2->dueDate);
  405.                 }
  406.             }
  407.         }
  408.  
  409.     // Sort by category, due date, priority
  410.     else if (sortOrder == soCategoryDueDate)
  411.         {
  412.         result = CategoryCompare (info1->attributes, info2->attributes, appInfoH);
  413.         if (result == 0)
  414.             {
  415.             result = DateTypeCmp (r1->dueDate, r2->dueDate);
  416.             if (result == 0)
  417.                 {
  418.                 result = (r1->priority & priorityOnly) - (r2->priority & priorityOnly);
  419.                 }
  420.             }
  421.         }
  422.     
  423.     return result;
  424. }
  425.  
  426.  
  427. /************************************************************
  428.  *
  429.  *  FUNCTION: ToDoSize
  430.  *
  431.  *  DESCRIPTION: Return the size of a ToDoDBRecordType
  432.  *
  433.  *  PARAMETERS: database record
  434.  *
  435.  *  RETURNS: the size in bytes
  436.  *
  437.  *  CREATED: 1/10/95 
  438.  *
  439.  *  BY: Roger Flores
  440.  *
  441.  *************************************************************/
  442. //static UInt16 ToDoSize(ToDoDBRecordPtr r)
  443. //{
  444. //    UInt16 size;
  445. //    char *c;
  446. //    int cLen;
  447. //    
  448. //    c = &r->description;
  449. //    cLen = StrLen(c) + 1;
  450. //    size = sizeof (DateType) + 1 + cLen;
  451. //    c += cLen;
  452. //    
  453. //    size += StrLen(c) + 1;
  454. //
  455. //    return size;
  456. //}
  457.  
  458.  
  459. /************************************************************
  460.  *
  461.  *  FUNCTION: ToDoFindSortPosition
  462.  *
  463.  *  DESCRIPTION: Return where a record is or should be.
  464.  *        Useful to find a record or find where to insert a record.
  465.  *
  466.  *  PARAMETERS: database record (not deleted!)
  467.  *
  468.  *  RETURNS: the size in bytes
  469.  *
  470.  *  CREATED: 1/11/95 
  471.  *
  472.  *  BY: Roger Flores
  473.  *
  474.  *************************************************************/
  475. static UInt16 ToDoFindSortPosition(DmOpenRef dbP, ToDoDBRecord *newRecord, 
  476.         SortRecordInfoPtr newRecordInfo)
  477. {
  478.     int sortOrder;
  479.     
  480.     sortOrder = ToDoGetSortOrder (dbP);
  481.         
  482.     return (DmFindSortPosition (dbP, newRecord, newRecordInfo, 
  483.         (DmComparF *)ToDoCompareRecords, sortOrder));
  484. }
  485.  
  486.  
  487. /************************************************************
  488.  *
  489.  *  FUNCTION: ToDoNewRecord
  490.  *
  491.  *  DESCRIPTION: Create a new record in sorted position
  492.  *
  493.  *  PARAMETERS: database pointer
  494.  *                database record
  495.  *
  496.  *  RETURNS: ##0 if successful, errorcode if not
  497.  *
  498.  *  CREATED: 1/10/95 
  499.  *
  500.  *  BY: Roger Flores
  501.  *
  502.  *************************************************************/
  503. Err ToDoNewRecord(DmOpenRef dbP, ToDoItemPtr item, UInt16 category, UInt16 *index)
  504. {
  505.     Err                         err;
  506.     UInt16                    size;
  507.     Char                        zero=0;
  508.     UInt16                    attr;
  509.     UInt16                     newIndex;
  510.     UInt32                    offset;
  511.     ToDoDBRecordPtr        nilP=0;
  512.     ToDoDBRecordPtr        recordP;
  513.     MemHandle                 recordH;
  514.     SortRecordInfoType    sortInfo;
  515.  
  516.     // Compute the size of the new to do record.
  517.     size = sizeDBRecord;
  518.     if (item->description)
  519.         size += StrLen (item->description);
  520.     if (item->note)
  521.         size += StrLen (item->note);
  522.         
  523.  
  524.     //  Allocate a chunk in the database for the new record.
  525.     recordH = (MemHandle)DmNewHandle(dbP, size);
  526.     if (recordH == NULL)
  527.         return dmErrMemError;
  528.  
  529.     // Pack the the data into the new record.
  530.     recordP = MemHandleLock (recordH);
  531.     DmWrite(recordP, (UInt32)&nilP->dueDate, &item->dueDate, sizeof(nilP->dueDate));
  532.     DmWrite(recordP, (UInt32)&nilP->priority, &item->priority, sizeof(nilP->priority));
  533.  
  534.     offset = (UInt32)&nilP->description;
  535.     if (item->description)
  536.         {
  537.         DmStrCopy(recordP, offset, item->description);
  538.         offset += StrLen (item->description) + 1;
  539.         }
  540.     else
  541.         {
  542.         DmWrite(recordP, offset, &zero, 1);
  543.         offset++;
  544.         }
  545.     
  546.     if (item->note) 
  547.         DmStrCopy(recordP, offset, item->note);
  548.     else
  549.         DmWrite(recordP, offset, &zero, 1);
  550.     
  551.     
  552.     sortInfo.attributes = category;
  553.     sortInfo.uniqueID[0] = 0;
  554.     sortInfo.uniqueID[1] = 0;
  555.     sortInfo.uniqueID[2] = 0;
  556.     
  557.     // Determine the sort position of the new record.
  558.     newIndex = ToDoFindSortPosition (dbP, recordP, &sortInfo);
  559.  
  560.     MemPtrUnlock (recordP);
  561.  
  562.     // Insert the record.
  563.     err = DmAttachRecord(dbP, &newIndex, recordH, 0);
  564.     if (err) 
  565.         MemHandleFree(recordH);
  566.     else
  567.         {
  568.         *index = newIndex;
  569.  
  570.         // Set the category.
  571.         DmRecordInfo (dbP, newIndex, &attr, NULL, NULL);
  572.         attr &= ~dmRecAttrCategoryMask;
  573.         attr |= category;
  574.         DmSetRecordInfo (dbP, newIndex, &attr, NULL);
  575.         }
  576.  
  577.     return err;
  578. }
  579.  
  580.  
  581. /***********************************************************************
  582.  *
  583.  * FUNCTION:    ToDoInsertNewRecord
  584.  *
  585.  * DESCRIPTION: This routine creates a new record and inserts it after
  586.  *              the specified position.  The new record is assigned the 
  587.  *              same priority and due date as the record it is 
  588.  *              inserted after.
  589.  *
  590.  * PARAMETERS:  database pointer
  591.  *              database record
  592.  *
  593.  * RETURNED:    nothing
  594.  *
  595.  * REVISION HISTORY:
  596.  *            Name    Date        Description
  597.  *            ----    ----        -----------
  598.  *            art    5/1/95    Initial Revision
  599.  *
  600.  ***********************************************************************/
  601. //Err ToDoInsertNewRecord (DmOpenRef dbP, UInt16 * index)
  602. //{
  603. //    UInt8                    priority;
  604. //    ToDoDBRecordPtr     rec;
  605. //    ToDoDBRecordPtr     newRec;
  606. //    MemHandle              recH;
  607. //    MemHandle                 newRecH;
  608. //    Err                     err;
  609. //    UInt16                 size;
  610. //    ToDoDBRecordPtr    nilP=0;
  611. //    UInt16                zero=0;
  612. //    UInt16                 newIndex;
  613. //    UInt16                category;
  614. //    UInt16                attr;
  615. //
  616. //
  617. //    // Make a new chunk
  618. //    size = sizeof (ToDoDBRecord) + 1;
  619. //    newRecH = DmNewHandle (dbP, size);
  620. //    if (! newRecH) return dmErrMemError;
  621. //
  622. //
  623. //    // Get the record that the new record will be inserted after.
  624. //    recH = DmQueryRecord (dbP, *index);
  625. //    ErrFatalDisplayIf (!recH, "Error inserting new to do record");
  626. //    rec = MemHandleLock (recH);
  627. //
  628. //    // Get the category of the current record.
  629. //    DmRecordInfo (dbP, *index, &attr, NULL, NULL);
  630. //    category = (attr & dmRecAttrCategoryMask);
  631. //        
  632. //    // Set the priority and the due date of the new record to the same
  633. //    // values as the record we're inserting after.  This will insure
  634. //    // that the records are in the proper sort order.
  635. //    newRec = (ToDoDBRecord *) MemHandleLock (newRecH);
  636. //    DmWrite(newRec, (UInt32)&nilP->dueDate, &rec->dueDate, sizeof(rec->dueDate));
  637. //
  638. //    priority = rec->priority & priorityOnly;
  639. //    DmWrite(newRec, (UInt32)&nilP->priority, &priority, sizeof(rec->priority));
  640. //    
  641. //    // Description is 1 byte, plus add extra byte for note field
  642. //    DmWrite(newRec, (UInt32)&nilP->description, &zero, 2);
  643. //
  644. //    MemPtrUnlock (rec);
  645. //
  646. //    // Attach in place
  647. //    newIndex = *index + 1;
  648. //    err = DmAttachRecord(dbP, &newIndex, newRecH, 0);
  649. //    if (err) 
  650. //        {
  651. //        MemHandleFree(newRecH);
  652. //        }
  653. //    else
  654. //        {
  655. //        MemPtrUnlock (newRec);
  656. //
  657. //        // Set the category of the new record.
  658. //        DmRecordInfo (dbP, newIndex, &attr, NULL, NULL);
  659. //        attr |= category;
  660. //        DmSetRecordInfo (dbP, newIndex, &attr, NULL);
  661. //
  662. //        *index = newIndex;
  663. //        }
  664. //
  665. //#if ERROR_CHECK_LEVEL == ERROR_CHECK_FULL
  666. //    ECToDoDBValidate (dbP);
  667. //#endif
  668. //    
  669. //    return err;
  670. //}
  671.  
  672.  
  673. /************************************************************
  674.  *
  675.  *  FUNCTION: ToDoChangeSortOrder
  676.  *
  677.  *  DESCRIPTION: Change the ToDo Database's sort order
  678.  *
  679.  *  PARAMETERS: database pointer
  680.  *                TRUE if sort by company
  681.  *
  682.  *  RETURNS: nothing
  683.  *
  684.  *  CREATED: 1/17/95 
  685.  *
  686.  *  BY: Roger Flores
  687.  *
  688.  *************************************************************/
  689. //Err ToDoChangeSortOrder(DmOpenRef dbP, Boolean sortOrder)
  690. //{
  691. //    ToDoAppInfoPtr appInfoP;
  692. //    ToDoAppInfoPtr    nilP = 0;
  693. //    UInt16            dirtyAppInfo;
  694. //
  695. //
  696. //    appInfoP = MemHandleLock (ToDoGetAppInfo (dbP));
  697. //
  698. //    if (appInfoP->sortOrder != sortOrder)
  699. //        {
  700. //        dirtyAppInfo = appInfoP->dirtyAppInfo | toDoSortByPriorityDirty;
  701. //        DmWrite(appInfoP, (UInt32)&nilP->dirtyAppInfo, &dirtyAppInfo, sizeof(appInfoP->dirtyAppInfo)); 
  702. //        DmWrite(appInfoP, (UInt32)&nilP->sortOrder, &sortOrder, sizeof(appInfoP->sortOrder));
  703. //        
  704. //        DmInsertionSort(dbP, (DmComparF *) &ToDoCompareRecords, (Int16) sortOrder);
  705. //        }
  706. //        
  707. //    MemPtrUnlock (appInfoP);    
  708. //
  709. //    return 0;
  710. //}
  711.  
  712.  
  713. /************************************************************
  714.  *
  715.  *  FUNCTION: ToDoSort
  716.  *
  717.  *  DESCRIPTION: Sort the appointment database.
  718.  *
  719.  *  PARAMETERS: database record
  720.  *
  721.  *  RETURNS: nothing
  722.  *
  723.  *  CREATED: 10/17/95 
  724.  *
  725.  *  BY: Art Lamb
  726.  *
  727.  *************************************************************/
  728. //void ToDoSort (DmOpenRef dbP)
  729. //{
  730. //    int sortOrder;
  731. //    
  732. //    sortOrder = ToDoGetSortOrder (dbP);
  733. //    DmInsertionSort(dbP, (DmComparF *) &ToDoCompareRecords, (Int16) sortOrder);
  734. //}
  735.  
  736.  
  737. /************************************************************
  738.  *
  739.  *  FUNCTION: ToDoChangeRecord
  740.  *
  741.  *  DESCRIPTION: Change a record in the ToDo Database
  742.  *
  743.  *  PARAMETERS: database pointer
  744.  *                     database index
  745.  *                     database record
  746.  *                     changed fields
  747.  *
  748.  *  RETURNS: ##0 if successful, errorcode if not
  749.  *
  750.  *  CREATED: 1/14/95 
  751.  *
  752.  *  BY: Roger Flores
  753.  *
  754.  *    COMMENTS:    Records are not stored with extra padding - they
  755.  *    are always resized to their exact storage space.  This avoids
  756.  *    a database compression issue.  The code works as follows:
  757.  *    
  758.  *
  759.  *************************************************************/
  760. Err ToDoChangeRecord(DmOpenRef dbP, UInt16 *index, 
  761.     ToDoRecordFieldType changedField, void * data)
  762. {
  763.     Err                         err;
  764.     Int16                     cLen;
  765.     UInt16                    attr;
  766.     UInt16                     curSize;
  767.     UInt16                     newSize;
  768.     UInt16                     newIndex = 0;
  769.     UInt8                        priority;
  770.     UInt32                    offset;
  771.     Char *                     c;
  772.     MemHandle                 recordH=0;
  773.     ToDoDBRecord            temp;
  774.     ToDoDBRecordPtr         src;
  775.     ToDoDBRecordPtr         nilP = 0;
  776.     SortRecordInfoType    sortInfo;
  777.     
  778.     // Get the record which we are going to change
  779.     recordH = DmQueryRecord(dbP, *index);
  780.     src = MemHandleLock (recordH);
  781.     
  782.  
  783.     // If the  record is being change such that its sort position will 
  784.     // change,  move the record to its new sort position.
  785.     if ( (changedField == toDoPriority) || 
  786.           (changedField == toDoDueDate)  ||
  787.           (changedField == toDoCategory) )
  788.         {
  789.         MemSet (&sortInfo, sizeof (sortInfo), 0);
  790.         DmRecordInfo (dbP, *index, &attr, NULL, NULL);
  791.         sortInfo.attributes = attr;
  792.  
  793.         if (changedField == toDoPriority)
  794.             {
  795.             temp.priority = *(UInt16 *) data;
  796.             temp.dueDate = src->dueDate;
  797.             sortInfo.attributes = attr;
  798.             }
  799.         else if (changedField == toDoDueDate) 
  800.             {
  801.             temp.priority = src->priority;
  802.             temp.dueDate = *((DatePtr)data);
  803.             sortInfo.attributes = attr;
  804.             }
  805.         else
  806.             {
  807.             temp.priority = src->priority;
  808.             temp.dueDate = src->dueDate;
  809.             sortInfo.attributes = *(UInt16 *) data;            
  810.             }
  811.  
  812.         newIndex = ToDoFindSortPosition (dbP, &temp, &sortInfo);
  813.         DmMoveRecord (dbP, *index, newIndex);
  814.         if (newIndex > *index) newIndex--;
  815.         *index = newIndex;
  816.         }
  817.  
  818.  
  819.     if (changedField == toDoPriority)
  820.         {
  821.         priority = *(UInt16 *) data | (src->priority & completeFlag);
  822.         DmWrite (src, (UInt32)&nilP->priority, &priority, sizeof(src->priority));
  823.         goto exit;
  824.         }
  825.             
  826.     if (changedField == toDoComplete) 
  827.         {
  828.         priority = (*(UInt16 *) data << 7) |  (src->priority & priorityOnly);
  829.         DmWrite (src, (UInt32)&nilP->priority, &priority, sizeof(src->priority));
  830.         goto exit;
  831.         }
  832.  
  833.     if (changedField == toDoDueDate)
  834.         { 
  835.         DmWrite (src, (UInt32)&nilP->dueDate, data, sizeof(src->dueDate));
  836.         goto exit;
  837.         }
  838.         
  839.     if (changedField == toDoCategory)
  840.         {
  841.         attr = (attr & ~dmRecAttrCategoryMask) | *(UInt16 *) data;
  842.         DmSetRecordInfo (dbP, newIndex, &attr, NULL);
  843.         goto exit;
  844.         }
  845.  
  846.  
  847.     // Calculate the size of the changed record. First,
  848.     // find the size of the data used from the old record.
  849.     newSize = sizeof (DateType) + 1;
  850.     newSize += StrLen((Char *) data) + 1;
  851.     c = &src->description;
  852.     cLen = StrLen(c) + 1;
  853.     if (changedField != toDoDescription)
  854.         newSize += cLen;
  855.     if (changedField != toDoNote)
  856.         {
  857.         c += cLen;
  858.         newSize += StrLen(c) + 1;
  859.         }
  860.         
  861.  
  862.     // Change the description field.
  863.     if (changedField == toDoDescription)
  864.         {
  865.         // If the new description is longer, expand the record.
  866.         curSize = MemPtrSize (src);
  867.         if (newSize > curSize)
  868.             {
  869.             MemPtrUnlock (src);
  870.             err = MemHandleResize (recordH, newSize);
  871.             if (err) return (err);
  872.             src = MemHandleLock (recordH);
  873.             }
  874.         
  875.         // Move the note field.
  876.         offset = sizeof(DateType) + 1 + StrLen (data) + 1;
  877.         c = &src->description;
  878.         c += StrLen(c) + 1;
  879.         DmWrite (src, offset, c, StrLen(c) + 1);
  880.         
  881.         
  882.         // Write the new description field.
  883.         offset = sizeof(DateType) + 1;
  884.         DmStrCopy (src, offset, data);
  885.  
  886.         // If the new description is shorter, shrink the record.
  887.         if (newSize < curSize)
  888.             MemPtrResize (recordH, newSize);
  889.         goto exit;
  890.         }
  891.  
  892.  
  893.     // Change the note field
  894.     if (changedField == toDoNote)
  895.         {
  896.         offset = sizeof(DateType) + 1 + StrLen((Char *)&src->description) + 1;
  897.         MemPtrUnlock (src);
  898.         err = MemHandleResize (recordH, newSize);
  899.         if (err) return (err);
  900.  
  901.         src = MemHandleLock (recordH);
  902.         DmStrCopy (src, offset, data);
  903.         goto exit;
  904.         }
  905.  
  906. exit:
  907.     MemPtrUnlock (src);
  908.  
  909. #if ERROR_CHECK_LEVEL == ERROR_CHECK_FULL
  910.     ECToDoDBValidate (dbP);
  911. #endif
  912.  
  913.     return 0;
  914. }
  915.  
  916.  
  917. /***********************************************************************
  918.  *
  919.  * FUNCTION:     ToDoSetDBBackupBit
  920.  *
  921.  * DESCRIPTION:  This routine sets the backup bit on the given database.
  922.  *                      This is to aid syncs with non Palm software.
  923.  *                      If no DB is given, open the app's default database and set
  924.  *                      the backup bit on it.
  925.  *
  926.  * PARAMETERS:   dbP -    the database to set backup bit,
  927.  *                                can be NULL to indicate app's default database
  928.  *
  929.  * RETURNED:     nothing
  930.  *
  931.  * REVISION HISTORY:
  932.  *            Name    Date        Description
  933.  *            ----    ----        -----------
  934.  *            grant    4/1/99    Initial Revision
  935.  *
  936.  ***********************************************************************/
  937. //void ToDoSetDBBackupBit(DmOpenRef dbP)
  938. //{
  939. //    DmOpenRef localDBP;
  940. //    LocalID dbID;
  941. //    UInt16 cardNo;
  942. //    UInt16 attributes;
  943. //
  944. //    // Open database if necessary. If it doesn't exist, simply exit (don't create it).
  945. //    if (dbP == NULL)
  946. //        {
  947. //        localDBP = DmOpenDatabaseByTypeCreator (toDoDBType, sysFileCToDo, dmModeReadWrite);
  948. //        if (localDBP == NULL)  return;
  949. //        }
  950. //    else
  951. //        {
  952. //        localDBP = dbP;
  953. //        }
  954. //    
  955. //    // now set the backup bit on localDBP
  956. //    DmOpenDatabaseInfo(localDBP, &dbID, NULL, NULL, &cardNo, NULL);
  957. //    DmDatabaseInfo(cardNo, dbID, NULL, &attributes, NULL, NULL,
  958. //                NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  959. //    attributes |= dmHdrAttrBackup;
  960. //    DmSetDatabaseInfo(cardNo, dbID, NULL, &attributes, NULL, NULL,
  961. //                NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  962. //    
  963. //    // close database if necessary
  964. //   if (dbP == NULL) 
  965. //       {
  966. //       DmCloseDatabase(localDBP);
  967. //      }
  968. //}
  969.  
  970.  
  971. /***********************************************************************
  972.  *
  973.  * FUNCTION:     ToDoGetDatabase
  974.  *
  975.  * DESCRIPTION:  Get the application's database.  Open the database if it
  976.  * exists, create it if neccessary.
  977.  *
  978.  * PARAMETERS:   *dbPP - pointer to a database ref (DmOpenRef) to be set
  979.  *                      mode - how to open the database (dmModeReadWrite)
  980.  *
  981.  * RETURNED:     Err - zero if no error, else the error
  982.  *
  983.  * REVISION HISTORY:
  984.  *            Name        Date        Description
  985.  *            ----        ----        -----------
  986.  *            jmp        10/04/99    Initial Revision
  987.  *
  988.  ***********************************************************************/
  989. Err ToDoGetDatabase (DmOpenRef *dbPP, UInt16 mode)
  990. {
  991.     DmOpenRef dbP;
  992.  
  993.     *dbPP = NULL;
  994.   
  995.   // Find the application's data file.  If it doesn't exist create it.
  996.     dbP = DmOpenDatabaseByTypeCreator (toDoDBType, sysFileCToDo, mode);
  997.     if (!dbP)
  998.         {
  999.             DEBUG1("Error, can't open ToDo database");
  1000.             return 1;
  1001. //        error = DmCreateDatabase (0, toDoDBName, sysFileCToDo, toDoDBType, false);
  1002. //        if (error)
  1003. //            return error;
  1004. //        
  1005. //        dbP = DmOpenDatabaseByTypeCreator(toDoDBType, sysFileCToDo, mode);
  1006. //        if (!dbP)
  1007. //            return (1);
  1008. //
  1009. //        // Set the backup bit.  This is to aid syncs with non Palm software.
  1010. //        ToDoSetDBBackupBit(dbP);
  1011. //        
  1012. //        error = ToDoAppInfoInit (dbP);
  1013. //      if (error) 
  1014. //          {
  1015. //            DmOpenDatabaseInfo(dbP, &dbID, NULL, NULL, &cardNo, NULL);
  1016. //          DmCloseDatabase(dbP);
  1017. //          DmDeleteDatabase(cardNo, dbID);
  1018. //         return error;
  1019. //         }
  1020.         }
  1021.     
  1022.     *dbPP = dbP;
  1023.     return 0;
  1024. }
  1025.  
  1026.