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 / flat.c < prev    next >
C/C++ Source or Header  |  2000-10-26  |  13KB  |  568 lines

  1. #include "flat.h"
  2. #include "task.h"
  3. #include "progect.h"
  4. #include "progectdb.h"
  5. #include "progectRsc.h"
  6.  
  7. /****************************************************************************
  8.  * Name : FlatFilter
  9.  * Desc : tell if a task can go to the flat list
  10.  * Parm : 
  11.  *             -> db
  12.  *             -> index
  13.  *             <- true if can go
  14.  * Out  : pgErr
  15.  * Auth : lb, 07.09.2000
  16.  * Mod  : lb, 25.09.2000
  17.  *             added overdue and by date filtering
  18.  ***************************************************************************/
  19. Boolean FlatFilter(DmOpenRef dbP, UInt16 index)
  20. {
  21.     UInt8 priority, completed;
  22.     Boolean dateOK = false;
  23.     Boolean priorityOK = false;
  24.     DateType date;
  25.  
  26.     // only terminal tasks
  27.     if (TaskGetHasChild(dbP, index))
  28.         return false;
  29.  
  30.     completed = TaskGetCompleted(dbP, index);
  31.     if (completed == 10 || completed == ACTION_OK)
  32.     {
  33.         if (gProjectPrefs.flatHideDone)
  34.             return false;
  35.     }
  36.  
  37.     date = TaskGetDueDate(dbP, index);
  38.  
  39.     if (date.month != 0)
  40.     {
  41.         // with date
  42.         dateOK = (gProjectPrefs.flatDated == 0);
  43.         if (gProjectPrefs.flatDateLimit)
  44.         {
  45.             if (!DateInLimit(date, gProjectPrefs.flatDateLimit))
  46.             {
  47.                 dateOK = false;
  48.             }
  49.         }
  50.     }
  51.     else
  52.     {
  53.         // without date
  54.         dateOK = gProjectPrefs.flatDated == 1;
  55.     }
  56.  
  57.  
  58.     priority = TaskGetPriority(dbP, index);
  59.  
  60.     if (gProjectPrefs.flatMin)
  61.     {
  62.         priorityOK = (priority <= gProjectPrefs.flatMinPriority);
  63.         if (priority == 0 && gProjectPrefs.flatMinPriority != 6)
  64.             priorityOK = false;
  65.     }
  66.     else
  67.     {
  68.         priorityOK = (priority == gProjectPrefs.flatMinPriority);
  69.         // backward compatible
  70.         if (priority == 0 && gProjectPrefs.flatMinPriority == 6)
  71.             priorityOK = true;
  72.     }
  73.  
  74.     if (gProjectPrefs.flatOr)
  75.         return gProjectPrefs.flatDated == 2 ? priorityOK : dateOK || priorityOK;
  76.     else
  77.         return gProjectPrefs.flatDated == 2 ? priorityOK : dateOK && priorityOK;
  78.  
  79. } // Boolean FlatFilter(DmOpenRef dbP, UInt16 index)
  80.  
  81.  
  82.  
  83. /****************************************************************************
  84.  * Name : FlatCreateList
  85.  * Desc : Create a flat list (remove the old one if it exists)
  86.  * Parm : 
  87.  *             -> db to add a list
  88.  * Out  : pgErr
  89.  * Auth : lb, 07.09.2000
  90.  ***************************************************************************/
  91. pgErr FlatCreateList(DmOpenRef dbP)
  92. {
  93.     UInt16 cardNo;
  94.     LocalID dbID, sortInfoID, oldSortInfoID = 0;
  95.     MemHandle h;
  96.     MemPtr p;
  97.     UInt32 size = 0, offset = 0, uniqueID;
  98.     UInt16 numRec = DmNumRecords(dbP), i;
  99.  
  100.     // get the sort info ID
  101.     DmOpenDatabaseInfo(dbP, &dbID, NULL, NULL, &cardNo, NULL);
  102.     DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
  103.         NULL, &sortInfoID, NULL, NULL);
  104.  
  105.     if (sortInfoID)
  106.         oldSortInfoID = sortInfoID;
  107.  
  108.     // count the terminal tasks
  109.     for (i = 1; i < numRec; i++)
  110.     {
  111.         if (FlatFilter(dbP, i))
  112.             size++;
  113.     }
  114.     size *= sizeof(UInt32); // UniqueID stored here as UInt32
  115.  
  116.     if (size == 0) // no tasks pass filtering
  117.     {
  118.         UInt32 zero = 0;
  119.         h = DmNewHandle(dbP, sizeof(UInt32));
  120.         sortInfoID = MemHandleToLocalID(h);
  121.  
  122.         // set this handle in the db
  123.         DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, 
  124.             NULL, NULL, &sortInfoID, NULL, NULL);
  125.         
  126.         p = MemHandleLock(h);
  127.         DmWrite(p, 0, &zero, sizeof(UInt32));
  128.     }
  129.     else
  130.     {
  131.         // get a new handle for the list
  132.         h = DmNewHandle(dbP, size);
  133.         if (!h)
  134.         {
  135.             DEBUG1("Cannot allocate memory for the flat list. Free some memory before retrying.");
  136.             return pgError;
  137.         }
  138.  
  139.         sortInfoID = MemHandleToLocalID(h);
  140.  
  141.         // set this handle in the db
  142.         DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, 
  143.             NULL, NULL, &sortInfoID, NULL, NULL);
  144.         
  145.         p = MemHandleLock(h);
  146.  
  147.         // store the terminal task's UniqueID
  148.         for (i = 1; i < numRec; i++)
  149.         {
  150.             if (FlatFilter(dbP, i))
  151.             {
  152.                 // get the uniqueID of the task
  153.                 DmRecordInfo(dbP, i, NULL, &uniqueID, NULL);
  154.                 // store it
  155.                 DmWrite(p, offset, &uniqueID, sizeof(UInt32));
  156.                 offset += sizeof(UInt32);
  157.             }
  158.         }
  159.     }
  160.  
  161.     // release the handle
  162.     MemHandleUnlock(h);
  163.  
  164.     if (oldSortInfoID)
  165.     {
  166.         h = MemLocalIDToGlobal(oldSortInfoID, cardNo);
  167.         if (h)
  168.         {
  169.             MemHandleFree(h);
  170.         }
  171.     }
  172.  
  173.     if (gProjectPrefs.flatSorted)
  174.         FlatSort(dbP);
  175.  
  176.     return pgOK;
  177. } // pgErr FlatCreateList(DmOpenRef dbP)
  178.  
  179.  
  180.  
  181. /****************************************************************************
  182.  * Name : FlatCreateGlobalList
  183.  * Desc : Create a flat list from all projects
  184.  * Parm : 
  185.  * Out  : pgErr
  186.  * Auth : lb, 21.10.2000
  187.  * Rem  : MUST be called from FrmProjectList, because it needs an initialized
  188.  *           namesData array
  189.  ***************************************************************************/
  190. #if 0
  191. pgErr FlatCreateGlobalList(void)
  192. {
  193.     UInt16 cardNo;
  194.     LocalID dbID, sortInfoID, oldSortInfoID = 0;
  195.     MemHandle h;
  196.     MemPtr p;
  197.     UInt32 size = 0, offset = 0, uniqueID;
  198.     UInt16 numRec, i;
  199.     UInt16 numDB = 0;
  200.  
  201.     // get the sort info ID
  202.     DmOpenDatabaseInfo(dbP, &dbID, NULL, NULL, &cardNo, NULL);
  203.     DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
  204.         NULL, &sortInfoID, NULL, NULL);
  205.  
  206.     if (sortInfoID)
  207.         oldSortInfoID = sortInfoID;
  208.  
  209.     // count the terminal tasks that pass the filter
  210.     for (i = 1; i < numRec; i++)
  211.     {
  212.         if (FlatFilter(dbP, i))
  213.             size++;
  214.     }
  215.     size *= sizeof(UInt32) + sizeof(UInt16); // UniqueID stored here as UInt32
  216.                                              // DB stored as index in the list
  217.  
  218.     if (size == 0) // no tasks pass filtering
  219.     {
  220.         UInt32 zero = 0;
  221.         h = DmNewHandle(dbP, sizeof(UInt32));
  222.         sortInfoID = MemHandleToLocalID(h);
  223.  
  224.         // set this handle in the db
  225.         DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, 
  226.             NULL, NULL, &sortInfoID, NULL, NULL);
  227.         
  228.         p = MemHandleLock(h);
  229.         DmWrite(p, 0, &zero, sizeof(UInt32));
  230.     }
  231.     else
  232.     {
  233.         // get a new handle for the list
  234.         h = DmNewHandle(dbP, size);
  235.         if (!h)
  236.         {
  237.             DEBUG1("Cannot allocate memory for the flat list. Free some memory before retrying.");
  238.             return pgError;
  239.         }
  240.  
  241.         sortInfoID = MemHandleToLocalID(h);
  242.  
  243.         // set this handle in the db
  244.         DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, 
  245.             NULL, NULL, &sortInfoID, NULL, NULL);
  246.         
  247.         p = MemHandleLock(h);
  248.  
  249.         // store the terminal task's UniqueID
  250.         for (i = 1; i < numRec; i++)
  251.         {
  252.             if (FlatFilter(dbP, i))
  253.             {
  254.                 // get the uniqueID of the task
  255.                 DmRecordInfo(dbP, i, NULL, &uniqueID, NULL);
  256.                 // store it
  257.                 DmWrite(p, offset, &uniqueID, sizeof(UInt32));
  258.                 offset += sizeof(UInt32);
  259.             }
  260.         }
  261.     }
  262.  
  263.     // release the handle
  264.     MemHandleUnlock(h);
  265.  
  266.     if (oldSortInfoID)
  267.     {
  268.         h = MemLocalIDToGlobal(oldSortInfoID, cardNo);
  269.         if (h)
  270.         {
  271.             MemHandleFree(h);
  272.         }
  273.     }
  274.  
  275.     if (gProjectPrefs.flatSorted)
  276.         FlatSort(dbP);
  277.  
  278.     return pgOK;
  279. } // pgErr FlatCreateGlobalList(void)
  280. #endif
  281.  
  282.  
  283.  
  284. /****************************************************************************
  285.  * Name : FlatSort
  286.  * Desc : sort the flat view array
  287.  * Parm : 
  288.  *             -> db
  289.  * Out  : pgErr
  290.  * Auth : lb, 21.09.2000
  291.  ***************************************************************************/
  292. pgErr FlatSort(DmOpenRef dbP)
  293. {
  294.     UInt16 cardNo = 0;
  295.     LocalID sortInfoID;
  296.     UInt32 *p;
  297.  
  298.     // get the sort info block
  299.     DmDatabaseInfo(cardNo, gdbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
  300.         NULL, &sortInfoID, NULL, NULL);
  301.  
  302.     // nothing to sort
  303.     if (!sortInfoID)
  304.         return pgOK;
  305.  
  306.     p = MemLocalIDToLockedPtr(sortInfoID, cardNo);
  307.  
  308.     Sort(dbP, p);
  309.  
  310.     MemPtrUnlock(p);
  311.     return pgOK;
  312. } // pgErr FlatSort(DmOpenRef dbP)
  313.  
  314.  
  315.  
  316.  
  317. /****************************************************************************
  318.  * Name : sort
  319.  * Desc : sort an array of pointers to tasks
  320.  * Parm : 
  321.  *             -> db
  322.  *             -> pointer to the array
  323.  * Out  : pgErr
  324.  * Auth : lb, 21.09.2000
  325.  * Rem  : bubble to test
  326.  * TODO : insertion sort, or quick sort
  327.  ***************************************************************************/
  328. pgErr Sort(DmOpenRef dbP, UInt32 *p)
  329. {
  330.     UInt16 nbItems = MemPtrSize(p) / sizeof(UInt32);
  331.     UInt16 i = 0, j;
  332.     Boolean finished = false;
  333.     MemPtr temp;
  334.     TaskRecordType **sortArray;
  335.     UInt32 a, b;
  336.     UInt16 index;
  337.     UInt16 debug = 0;
  338.  
  339.     // empty or one-item list, just return
  340.     if (nbItems < 2)
  341.         return pgOK;
  342.  
  343.     sortArray = MemPtrNew(nbItems * sizeof(MemPtr));
  344.     if (!sortArray)
  345.     {
  346.         DEBUG1("Not enough memory to sort");
  347.         return pgError;
  348.     }
  349.  
  350.     // fill the array
  351.     for (i = 0; i < nbItems; i++)
  352.     {
  353.         DmFindRecordByID(dbP, p[i], &index);
  354.         sortArray[i] = MemHandleLock(DmQueryRecord(dbP, index));
  355.     }
  356.  
  357.     i = 0;
  358.     while (i < nbItems - 1 && !finished)
  359.     {
  360.         finished = true;
  361.         for (j = 0; j < nbItems - 1 - i; j++)
  362.         {
  363.             if (CmpTasks(sortArray[j], sortArray[j+1]) == -1)
  364.             {
  365.                 // swap
  366.                 temp = sortArray[j];
  367.                 sortArray[j] = sortArray[j+1];
  368.                 sortArray[j+1] = temp;
  369.                 a = p[j];
  370.                 b = p[j+1];
  371.                 DmWrite(p, j * sizeof(UInt32), &b, sizeof(UInt32));
  372.                 DmWrite(p, (j+1) * sizeof(UInt32), &a, sizeof(UInt32));
  373.                 finished = false;
  374.                 debug++;
  375.             }
  376.         }
  377.         i++;
  378.     }
  379.  
  380.     // unlock all
  381.     for (i = 0; i < nbItems; i++)
  382.     {
  383.         MemPtrUnlock(sortArray[i]);
  384.     }
  385.     MemPtrFree(sortArray);
  386.  
  387.     return pgOK;
  388.  
  389. } // pgErr Sort(DmOpenRef dbP, UInt32 *p)
  390.  
  391.  
  392.  
  393. /****************************************************************************
  394.  * Name : CmpTasks
  395.  * Desc : compares two tasks
  396.  * Parm : 
  397.  *             -> pointers to the array
  398.  * Out  : -1 : a comes after b
  399.  *            1 : a comes before b
  400.  *            0 : a equal b
  401.  * Auth : lb, 21.09.2000
  402.  ***************************************************************************/
  403. Int16 CmpTasks(TaskRecordType *a, TaskRecordType *b)
  404. {
  405.     Int16 result;
  406.  
  407.     if (gProjectPrefs.flatSorted == sortDateFirst)
  408.     {
  409.         result = CmpDate(a, b);
  410.         if (result == 0)
  411.             result = CmpPriority(a, b);
  412.     }
  413.     else
  414.     {
  415.         result = CmpPriority(a, b);
  416.         if (result == 0)
  417.             result = CmpDate(a, b);
  418.     }
  419.  
  420.     return result;
  421. } // Int16 CmpTasks(TaskRecordType *a, TaskRecordType *b)
  422.  
  423.  
  424.  
  425. /****************************************************************************
  426.  * Name : CmpDate
  427.  * Desc : compares the dates of two pointed tasks
  428.  * Parm : 
  429.  *             -> pointers to the tasks
  430.  * Out  : -1 : a comes after b
  431.  *            1 : a comes before b
  432.  *            0 : a equal b
  433.  * Auth : lb, 21.09.2000
  434.  ***************************************************************************/
  435. Int16 CmpDate(TaskRecordType *a, TaskRecordType *b)
  436. {
  437.     // a and/or b have no date
  438.     if (a->dueDate.month == 0 || b->dueDate.month == 0)
  439.     {
  440.         if (a->dueDate.month == 0 && b->dueDate.month == 0)
  441.         {
  442.             return 0;
  443.         }
  444.         else if (a->dueDate.month == 0)
  445.         {
  446.             return -1;
  447.         }
  448.         else
  449.         {
  450.             return 1;
  451.         }
  452.     }
  453.  
  454.     // both have a date
  455.     if (a->dueDate.year == b->dueDate.year)
  456.     {
  457.         if (a->dueDate.month == b->dueDate.month)
  458.         {
  459.             if (a->dueDate.day == b->dueDate.day)
  460.             {
  461.                 return 0;
  462.             }
  463.             else
  464.             {
  465.                 return a->dueDate.day < b->dueDate.day ? 1 : -1;
  466.             }
  467.         }
  468.         else
  469.         {
  470.             return a->dueDate.month < b->dueDate.month ? 1 : -1;
  471.         }
  472.     }
  473.     else
  474.     {
  475.         return a->dueDate.year < b->dueDate.year ? 1 : -1;
  476.     }
  477. } // Int16 CmpDate(TaskRecordType *a, TaskRecordType *b)
  478.  
  479.  
  480.  
  481. /****************************************************************************
  482.  * Name : CmpPriority
  483.  * Desc : compares the priorities of two pointed tasks
  484.  * Parm : 
  485.  *             -> pointers to the tasks
  486.  * Out  : -1 : a comes after b
  487.  *            1 : a comes before b
  488.  *            0 : a equal b
  489.  * Auth : lb, 21.09.2000
  490.  ***************************************************************************/
  491. Int16 CmpPriority(TaskRecordType *a, TaskRecordType *b)
  492. {
  493.     if (a->priority == b->priority)
  494.     {
  495.         return 0;
  496.     }
  497.     else
  498.     {
  499.         if (a->priority == 6 || a->priority == 0)
  500.             return -1;
  501.         if (b->priority == 6 || b->priority == 0)
  502.             return 1;
  503.         return a->priority < b->priority ? 1 : -1;
  504.     }
  505. } // Int16 CmpPriority(TaskRecordType *a, TaskRecordType *b)
  506.  
  507.  
  508.  
  509. /****************************************************************************
  510.  * Name : FlatLinkAll
  511.  * Desc : ToDo (un)link all tasks in the flatview
  512.  * Parm : 
  513.  *             -> db
  514.  *             -> link (true = link, false = unlink)
  515.  * Out  :
  516.  * Auth : lb, 22.10.2000
  517.  ***************************************************************************/
  518. void FlatLinkAll(DmOpenRef dbP, Boolean link)
  519. {
  520.     UInt16 i, index;
  521.     UInt16 cardNo = 0;
  522.     LocalID sortInfoID;
  523.     UInt32 *p;
  524.     UInt32 size = 0;
  525.  
  526.     // get the sort info block
  527.     DmDatabaseInfo(cardNo, gdbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
  528.         NULL, &sortInfoID, NULL, NULL);
  529.  
  530.     p = MemLocalIDToLockedPtr(sortInfoID, cardNo);
  531.     size = MemPtrSize(p) / sizeof(UInt32);
  532.  
  533.     // if gdbP contains at least one task, there is at least one terminal task
  534.     // special case, db empty (just task 0)
  535.     if ((DmNumRecords(gdbP) == 1) || (*p == 0))
  536.     {
  537.         MemPtrUnlock(p);
  538.         return;
  539.     }
  540.  
  541.     for (i = 0; i < size; i++)
  542.     {
  543.         DmFindRecordByID(gdbP, p[i], &index);
  544.         if (index)
  545.         {
  546.             // if hasToDo != link
  547.             if (TaskGetFormat(dbP, index).hasToDo != link)
  548.             {
  549.                 if (link)
  550.                 {
  551.                     UInt8 comp = TaskGetCompleted(dbP, index);
  552.                     if (comp < ACTION)
  553.                     {
  554.                         TaskSetCompleted(dbP, index,
  555.                             comp == 10 ? ACTION_OK : ACTION_NO);
  556.                     }
  557.                     TaskPublishToDo(dbP, index);
  558.                 }
  559.                 else
  560.                 {
  561.                     TaskRemoveHasToDo(dbP, index);
  562.                 }
  563.             }
  564.         }
  565.     }
  566.     MemPtrUnlock(p);
  567. }
  568.