home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / wapuniverse-src-0.3.5.build9.tar.gz / wapuniverse-src-0.3.5.build9.tar / wapuniverse-0.3.5.build9 / dbConn.c < prev    next >
C/C++ Source or Header  |  2000-11-12  |  16KB  |  464 lines

  1. //---------------------------------------------------------------------------
  2. // dbConn
  3. // Contains all PalmOS specific dbConnConnection Database code
  4. //
  5. // Project: WAPUniverse for PalmOS
  6. // Copyright ⌐ 1999-2000 Filip Onkelinx
  7. //
  8. // http://www.wapuniverse.com/
  9. // filip@onkelinx.com
  10. //
  11. // This program is free software; you can redistribute it and/or
  12. // modify it under the terms of the GNU General Public License
  13. // as published by the Free Software Foundation; either version 2
  14. // of the License, or (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program; if not, write to the Free Software 
  23. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
  24. //
  25. //
  26. // $Workfile: dbConn.c $  
  27. // $Author: wapuniverse $  
  28. // $Date: 2000/11/11 20:51:09 $     
  29. // $Revision: 1.8 $
  30. //
  31. // $Header: /cvsroot/wapuniverse/wapuniverse/src/dbConn.c,v 1.8 2000/11/11 20:51:09 wapuniverse Exp $
  32. //
  33. //---------------------------------------------------------------------------
  34. #include    <PalmOS.h>
  35. #include    <SysEvtMgr.h>
  36. #include    "WAPUniverse.h"
  37. #include    "../res/WAPUniverse_res.h"
  38. #include    "dbUrl.h"
  39. #include    "dbConn.h"
  40.  
  41. Boolean dbConnCreateRecord (GlobalsType *g)
  42. {
  43.   MemPtr            p;
  44.   MemHandle            connRec;
  45.   UInt16            index = 0;
  46.   Err                error;
  47.   UInt16            attr;         // record attributes (category)
  48.  
  49.   // Create a new first record in the database
  50.   connRec = DmNewRecord (g->DbConnDb, &index, dbConnNewConnSize);
  51.   // Lock down the block containing the new record.
  52.   p = MemHandleLock (connRec);
  53.   // Write a zero to the first byte of the record to null terminate the new
  54.   // memo string.
  55.   error = DmSet (p, 0, dbConnNewConnSize, (UInt8) 0);
  56.  
  57.   // Check for fatal error.
  58.   ErrFatalDisplayIf (error, "Could not write to new record.");
  59.  
  60.   // Unlock the block of the new record.
  61.   MemPtrUnlock (p);
  62.  
  63.  
  64.   // P11. Set the category of the new record to the current category.
  65.   DmRecordInfo (g->DbConnDb, index, &attr, NULL, NULL);
  66.   attr &= ~dmRecAttrCategoryMask; // Remove all category bits
  67.   // no category support for connection settings (yet?), place
  68.   // the new record in the unfiled category.
  69.   attr |= dmUnfiledCategory;
  70.   DmSetRecordInfo (g->DbConnDb, index, &attr, NULL);
  71.  
  72.  
  73.   // Release the record to the database manager.  The true value indicates
  74.   // that
  75.   // the record contains "dirty" data.  Release Record will set the record's 
  76.   // dirty flag and update the database modification count.
  77.   DmReleaseRecord (g->DbConnDb, index, true);
  78.  
  79.   // Remember the index of the current record.
  80.   g->DbConnCurrentRecord = 0;
  81.   return (true);
  82. }
  83.  
  84. /************************************************************
  85.  *  FUNCTION: PackConnection
  86.  *  DESCRIPTION: Packs a dbConnConnection to consume less space in the DB
  87.  *  PARAMETERS: the unpacked dbConnConnection (conn), and the destination DB handle
  88.  *  RETURNS: no nothing, but should probably 
  89.  *************************************************************/
  90. void dbConnPackConnection (dbConnConnection * conn, MemHandle connDBEntry)
  91. {
  92.   // figure out necessary size
  93.   UInt16            length = 0;
  94.   Char *         s;
  95.   UInt16            offset = 0;
  96.  
  97.   length = StrLen (conn->name) +
  98.     StrLen (conn->ipaddress) +
  99.     3 +                         // 1 for bearer, 1 for security, 1 for connection type
  100.     2;                          // 3 for 3 string terminators
  101.  
  102.   // resize the MemHandle
  103.   if (MemHandleResize (connDBEntry, length) == 0)
  104.     {
  105.       // copy the fields
  106.       s = MemHandleLock (connDBEntry);
  107.       offset = 0;
  108.       DmStrCopy (s, offset, (Char *) conn->name);
  109.       offset += StrLen (conn->name) + 1;
  110.       DmStrCopy (s, offset, (Char *) conn->ipaddress);
  111.       offset += StrLen (conn->ipaddress) + 1;
  112.       DmWrite (s, offset, (Char *) &conn->security,3);
  113.       MemHandleUnlock (connDBEntry);
  114.     }
  115.     else
  116.     {
  117.         ErrDisplay ("***MemHandleResize failed");    
  118.     }
  119. }
  120.  
  121. /************************************************************
  122.  *  FUNCTION: 
  123.  *  DESCRIPTION: 
  124.  *  PARAMETERS: 
  125.  *  RETURNS: 
  126.  *************************************************************/
  127. void dbConnUnPackConnection (dbConnConnection * conn, dbConnPackedConnection * packedConnection)
  128. {
  129.   char*           s = packedConnection->name;
  130.  
  131.   StrCopy (conn->name, s);
  132.   s += StrLen (s) + 1;
  133.   StrCopy (conn->ipaddress, s);
  134.   s += StrLen (s) + 1;
  135.   conn->security = *s;
  136.   s++;
  137.   conn->bearer = *s;
  138.   s++;
  139.   conn->connectionType = *s;
  140.  
  141. }
  142.  
  143. /************************************************************
  144.  *  FUNCTION: 
  145.  *  DESCRIPTION: 
  146.  *  PARAMETERS: 
  147.  *  RETURNS: 
  148.  *************************************************************/
  149. void dbConnDisposeConnectionsList(GlobalsType *g)
  150. {
  151.     if (g->ConnectionsListArrayH) {
  152.         MemHandleFree(g->ConnectionsListArrayH);
  153.         g->ConnectionsListArrayH=NULL;
  154.     }
  155.     if (g->ConnectionsHandle) {
  156.       MemHandleUnlock(g->ConnectionsHandle);
  157.       MemHandleFree(g->ConnectionsHandle);
  158.       g->ConnectionsHandle=NULL;
  159.     }
  160. }
  161.  
  162. /************************************************************
  163.  *  FUNCTION: 
  164.  *  DESCRIPTION: 
  165.  *  PARAMETERS: 
  166.  *  RETURNS: 
  167.  *************************************************************/
  168. void dbConnGetConnection(char *connstr, dbConnConnection *conn, GlobalsType *g)
  169. {
  170.   UInt16            nrRecs, i, pos;
  171.   MemHandle        connRec;
  172.   dbConnPackedConnection* pconn;
  173.  
  174.   nrRecs = DmNumRecordsInCategory (g->DbConnDb, dmAllCategories);
  175.   pos = 0;
  176.   for (i = 0; i < nrRecs; i++)
  177.     {
  178.       connRec = DmQueryNextInCategory (g->DbConnDb, &pos, dmAllCategories);
  179.       pconn = MemHandleLock (connRec);
  180.       if (StrCompare((Char *)pconn,connstr) == 0)
  181.         { 
  182.             dbConnUnPackConnection (conn, pconn);
  183.         }
  184.       MemHandleUnlock (connRec);
  185.       pos++;
  186.     }
  187. }
  188.  
  189. /************************************************************
  190.  *  FUNCTION: 
  191.  *  DESCRIPTION: 
  192.  *  PARAMETERS: 
  193.  *  RETURNS: 
  194.  *************************************************************/
  195. void dbConnGetConnectionsList (GlobalsType *g)
  196. {
  197.   ListPtr         connectionslstPtr;
  198.   // dbConnConnectionPtr connRecPtr;
  199.   ControlPtr      ctlPtr;
  200.   MemHandle        connRec;
  201.   UInt16            nrRecs, nrConnections, i, pos, titlelen,
  202.   connectionsOffset = 0;
  203.   UInt16            j = 0;
  204.   FormPtr         frm;
  205.   Char*           cptr;
  206.   Char *         connections;
  207.   Err             error;
  208.  
  209.   frm = FrmGetFormPtr (frmEditURL);
  210.   nrRecs = DmNumRecordsInCategory (g->DbConnDb, dmAllCategories);
  211.   pos = 0;
  212.   nrConnections = 0;
  213.   g->ConnectionsHandle = MemHandleNew (sizeof (char));
  214.   connections = MemHandleLock (g->ConnectionsHandle);
  215.   *connections = 0;
  216.   for (i = 0; i < nrRecs; i++)
  217.     {
  218.       connRec = DmQueryNextInCategory (g->DbConnDb, &pos, dmAllCategories);
  219.       cptr = MemHandleLock (connRec);
  220.       titlelen = StrLen (cptr);
  221.       MemHandleUnlock (g->ConnectionsHandle);
  222.       error = MemHandleResize (g->ConnectionsHandle, titlelen +
  223.                                connectionsOffset + sizeof ('\0'));
  224.       connections = MemHandleLock (g->ConnectionsHandle);
  225.       StrCopy(connections+connectionsOffset,cptr);
  226.       connectionsOffset += (titlelen+1);
  227.       nrConnections++;
  228.       MemHandleUnlock (connRec);
  229.       pos++;
  230.     }
  231.   if (nrConnections == 0)
  232.     {
  233.       MemHandleUnlock (g->ConnectionsHandle);
  234.       return;
  235.     }
  236.   g->ConnectionsListArrayH = SysFormPointerArrayToStrings (connections, nrConnections);
  237.   connectionslstPtr = (ListPtr) (FrmGetObjectPtr (frm, (FrmGetObjectIndex
  238.                                                         (frm, listURLConnection))));
  239.   LstSetListChoices (connectionslstPtr, MemHandleLock
  240.                      (g->ConnectionsListArrayH), nrConnections);
  241.   LstSetHeight (connectionslstPtr, nrConnections);
  242.   // find current connection index
  243.   for (i = 0; i < nrConnections; i++)
  244.   {
  245.       if (StrCompare(LstGetSelectionText(connectionslstPtr,i),g->DbUrlCurrentURL.connection )==0)
  246.          j= i;
  247.   }
  248.   LstSetSelection(connectionslstPtr,j); // current connection 
  249.   ctlPtr =(ControlPtr)(FrmGetObjectPtr(frm,(FrmGetObjectIndex(frm,triggerURLConnection))));
  250.   g->EditURLConnection = LstGetSelectionText(connectionslstPtr,
  251.                           LstGetSelection(connectionslstPtr));
  252.   CtlSetLabel(ctlPtr,g->EditURLConnection);
  253.    
  254. }
  255.  
  256.  
  257. /************************************************************
  258.  *  FUNCTION: 
  259.  *  DESCRIPTION: 
  260.  *  PARAMETERS: 
  261.  *  RETURNS: 
  262.  *************************************************************/
  263. void dbConnOpenOrCreateDB(UInt16 mode, GlobalsType *g)
  264. {
  265.   Int16             error;        // error code
  266.   UInt16            cardNo;       // card containing the application database
  267.   LocalID             dbID;         // handle for application database
  268.   UInt16            dbAttrs;      // database attributes
  269.   MemHandle            currec;
  270.  
  271.  
  272.   g->DbConnDb = DmOpenDatabaseByTypeCreator (dbConnDBType, wuAppType, mode);
  273.   if (!g->DbConnDb)
  274.     {
  275.       // The database doesn't exist, create it now.
  276.       error = DmCreateDatabase (0, dbConnDBName, wuAppType,
  277.                                 dbConnDBType, false);
  278.  
  279.       // Check for fatal error.
  280.       ErrFatalDisplayIf (error,
  281.                          "Create new ConnDB failed");
  282.  
  283.       // Find the application's database.
  284.       g->DbConnDb = DmOpenDatabaseByTypeCreator (dbConnDBType,
  285.                                                 wuAppType, mode);
  286.  
  287.       // Get info about the database
  288.       error = DmOpenDatabaseInfo (g->DbConnDb, &dbID, NULL, NULL, &cardNo, NULL);
  289.  
  290.       // Check for fatal error.
  291.       ErrFatalDisplayIf (error,
  292.                          "Get ConnDB info failed");
  293.  
  294.       // Get attributes for the database
  295.       error = DmDatabaseInfo (0, dbID, NULL, &dbAttrs, NULL, NULL, NULL,
  296.                               NULL, NULL, NULL, NULL, NULL, NULL);
  297.  
  298.       // Check for fatal error.
  299.       ErrFatalDisplayIf (error,
  300.                          "Get ConnDB info failed");
  301.  
  302.       // Set the new attributes in the database
  303.       error = DmSetDatabaseInfo (0, dbID, NULL, &dbAttrs, NULL, NULL,
  304.                                  NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  305.  
  306.       // Check for fatal error.
  307.       ErrFatalDisplayIf (error,
  308.                          "Set ConnDB info failed");
  309.  
  310.       // Store category info in the application's information block.
  311.       dbUrlInitAppInfo (g->DbConnDb,g);
  312.  
  313.       // Create a default record for "Sonera"
  314.       dbConnCreateRecord (g);
  315.       StrCopy (g->CurrentConnection.name, "Sonera");
  316.       StrCopy (g->CurrentConnection.ipaddress, "193.64.89.161");
  317.       g->CurrentConnection.security = 'N';     // 'N'o
  318.       g->CurrentConnection.bearer = 'C'; // 'C'SD
  319.       g->CurrentConnection.connectionType = 'L'; // C'L'
  320.       currec = DmGetRecord (g->DbConnDb, 0);
  321.       dbConnPackConnection (&g->CurrentConnection, currec);
  322.       DmReleaseRecord (g->DbConnDb, 0, true);
  323.       // Create a default record for "Ericsson"
  324.       dbConnCreateRecord (g);
  325.       StrCopy (g->CurrentConnection.name, "Ericsson");
  326.       StrCopy (g->CurrentConnection.ipaddress, "195.58.110.201");
  327.       g->CurrentConnection.security = 'N';     // 'N'o
  328.       g->CurrentConnection.bearer = 'C'; // 'C'SD
  329.       g->CurrentConnection.connectionType = 'L'; // C'L'
  330.       currec = DmGetRecord (g->DbConnDb, 0);
  331.       dbConnPackConnection (&g->CurrentConnection, currec);
  332.       DmReleaseRecord (g->DbConnDb, 0, true);
  333.     }
  334.  
  335.   // Set current database record to no current record.
  336.   g->DbConnCurrentRecord = noRecordSelected;
  337. }
  338.  
  339.  
  340. /************************************************************
  341.  *  FUNCTION: 
  342.  *  DESCRIPTION: 
  343.  *  PARAMETERS: 
  344.  *  RETURNS: 
  345.  *************************************************************/
  346. static Int16 dbConnCompareRecords (dbConnConnectionPtr c1, dbConnConnectionPtr c2)
  347. {
  348.   return (StrCompare ((Char *) & c1->name, (Char *) & c2->name));
  349. }
  350.  
  351. void dbConnReadCurrentConnection (GlobalsType *g)
  352. {
  353.   dbConnPackedConnection* pconn;
  354.   MemHandle        rechand;
  355.   Int16             seekAmount = g->DbConnCurrentRecord;
  356.   UInt16            index = 0;
  357.  
  358.   // must do seek to skip over secret records
  359.   DmSeekRecordInCategory (g->DbConnDb, &index, seekAmount, dmSeekForward, dmAllCategories);
  360.   rechand = DmQueryRecord (g->DbConnDb, index);
  361.   if (rechand)
  362.     {
  363.       pconn = MemHandleLock (rechand);
  364.       dbConnUnPackConnection (&(g->CurrentConnection), pconn);
  365.       MemHandleUnlock (rechand);
  366.     }
  367.  
  368. }
  369.  
  370. /************************************************************
  371.  *  FUNCTION: 
  372.  *  DESCRIPTION: 
  373.  *  PARAMETERS: 
  374.  *  RETURNS: 
  375.  *************************************************************/
  376. Err dbConnSortRecord (DmOpenRef dbP, UInt16 *indexP)
  377. {
  378.   Err                     err;
  379.   UInt16                index;
  380.   UInt16                attributes;
  381.   UInt32                   uniqueID;
  382.   MemHandle              recordH;
  383.   MemHandle                h;
  384.   Boolean                 dontMove;
  385.   dbConnConnectionPtr    cmp;
  386.   dbConnConnectionPtr   recordP;
  387.  
  388.   // Check if the record is already in the correct position.
  389.   recordP = MemHandleLock (DmQueryRecord (dbP, *indexP));
  390.   if (*indexP > 0)
  391.     {
  392.       // This record wasn't deleted and deleted records are at the end of the
  393.       // database so the prior record may not be deleted!
  394.       h = DmQueryRecord (dbP, *indexP - 1);
  395.       if (!h)
  396.         dontMove = false;
  397.       else
  398.         {
  399.           cmp = MemHandleLock (h);
  400.           dontMove = (dbConnCompareRecords (cmp, recordP) == -1);
  401.           MemPtrUnlock (cmp);
  402.         }
  403.     }
  404.   else
  405.     dontMove = true;
  406.  
  407.   if (dontMove && (*indexP + 1 < DmNumRecords (dbP)))
  408.     {
  409.       DmRecordInfo (dbP, *indexP + 1, &attributes, NULL, NULL);
  410.       if (!(attributes & dmRecAttrDelete))
  411.         {
  412.           cmp = MemHandleLock (DmQueryRecord (dbP, *indexP + 1));
  413.           dontMove &= (dbConnCompareRecords (recordP, cmp) == -1);
  414.           MemPtrUnlock (cmp);
  415.         }
  416.     }
  417.   MemPtrUnlock (recordP);
  418.  
  419.   if (dontMove)
  420.     return (0);
  421.  
  422.  
  423.   // Since the routine that determines the records sort position uses a 
  424.   // binary search algorythm we need to remove the record from the database 
  425.   // before we can determine its new position.  We will also save and restore 
  426.   // the 
  427.   // record's attributes and unique ID.
  428.   DmRecordInfo (dbP, *indexP, &attributes, &uniqueID, NULL);
  429.  
  430.   err = DmDetachRecord (dbP, *indexP, &recordH);
  431.   if (err)
  432.     return (err);
  433.  
  434.   recordP = MemHandleLock ((MemHandle) recordH);
  435.   index = DmFindSortPosition (dbP, recordP, NULL, (DmComparF *)
  436.                               dbConnCompareRecords, 0);
  437.   MemPtrUnlock (recordP);
  438.  
  439.   err = DmAttachRecord (dbP, &index, recordH, 0);
  440.   if (err)
  441.     return (err);
  442.  
  443.   DmSetRecordInfo (dbP, index, &attributes, &uniqueID);
  444.  
  445.   *indexP = index;
  446.  
  447.   return (err);
  448. }
  449.  
  450. Boolean dbConnDeleteCurrentRecord (GlobalsType *g)
  451. {
  452.   Int16             seekAmount = g->DbConnCurrentRecord;
  453.   UInt16            index = 0;
  454.  
  455.   DmSeekRecordInCategory (g->DbConnDb, &index, seekAmount, dmSeekForward, dmAllCategories);
  456.   DmRemoveRecord (g->DbConnDb, index);
  457.   return (true);
  458. }
  459.  
  460. UInt16 dbConnNumRecords(GlobalsType *g)
  461. {
  462.   return(DmNumRecordsInCategory (g->DbConnDb, dmAllCategories));
  463. }
  464.