home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / rdf / src / glue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.4 KB  |  484 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * This file contains the glue code that links the RDF module with
  21.  * ugly navigator stuff including Netlib for getURL, preferences for
  22.  * finding out where bookmarks.htm and lclstore.mcf are located.  By
  23.  * changing this glue file, one should be able to use the rest of the
  24.  * RDF library with something else (like the server).  
  25.  
  26.  * For more information on this file, contact rjc or guha 
  27.  * For more information on RDF, look at the RDF section of www.mozilla.org
  28. */
  29.  
  30. #include "rdf-int.h"
  31. #include "glue.h"
  32. #include "remstore.h"
  33. #include "rdfparse.h"
  34. #include "xmlglue.h"
  35. #include "es2mcf.h"
  36. #include "mcff2mcf.h"
  37. #include "nlcstore.h"
  38.  
  39.  
  40.  
  41. /* external routines */
  42. extern    MWContext    *FE_GetRDFContext(void);
  43.  
  44.  
  45. /* globals */
  46. char            *profileDirURL = NULL;
  47. char            *gLocalStoreURL = NULL;
  48. char            *gBookmarkURL = NULL;
  49. char            *gGlobalHistoryURL = NULL;
  50. void            *timerID = NULL;
  51. char                    *gRLForbiddenDomains = NULL;
  52.  
  53.  
  54.  
  55. unsigned int
  56. rdf_write_ready(NET_StreamClass *stream)
  57. {
  58.     return MAX_WRITE_READY;
  59. }
  60.  
  61.  
  62.  
  63. void
  64. rdf_complete(NET_StreamClass *stream)
  65. {
  66.   RDFFile f = (RDFFile)stream->data_object;
  67.   if (f) {
  68.     freeMem(f->line);
  69.     freeMem(f->currentSlot);
  70.     freeMem(f->holdOver);
  71.     freeNamespaces(f) ;
  72.   }
  73. }
  74.  
  75.  
  76.  
  77. void
  78. rdf_abort(NET_StreamClass *stream, int status)
  79. {
  80.   RDFFile f = (RDFFile)stream->data_object;
  81.   if (f) {
  82.      f->locked = false;
  83.      gcRDFFile (f);
  84.     freeMem(f->line);
  85.     freeMem(f->currentSlot);
  86.     freeMem(f->holdOver);
  87.     freeNamespaces(f) ;
  88.   }
  89. }
  90.  
  91.  
  92.  
  93. #ifdef    XP_MAC
  94. PR_PUBLIC_API(NET_StreamClass *)
  95. #else
  96. PUBLIC NET_StreamClass *
  97. #endif
  98.  
  99. rdf_Converter(FO_Present_Types format_out, void *client_data,
  100.         URL_Struct *urls, MWContext *cx)
  101. {
  102.     RDFFile            rdfFile;
  103.     MKStreamWriteFunc    writeFunc = NULL;
  104.  
  105.     if ((urls == NULL) || (urls->fe_data == NULL))
  106.     {
  107.         return(NULL);
  108.     }
  109.  
  110.     /* determine appropriate write function to use */
  111.     rdfFile = urls->fe_data;
  112.     switch(rdfFile->fileType)
  113.     {
  114.         case    ES_RT:
  115.         case    FTP_RT:
  116.         writeFunc = (MKStreamWriteFunc)parseNextESFTPBlob;
  117.         break;
  118.  
  119.         case    RDF_MCF:
  120.         writeFunc = (MKStreamWriteFunc)parseNextMCFBlob;
  121.         break;
  122.  
  123.         default:
  124.         writeFunc = (MKStreamWriteFunc)parseNextRDFXMLBlob;
  125.         break;
  126.     }
  127.  
  128.     /* allocate stream data block */
  129.     return NET_NewStream("RDF", writeFunc, 
  130.             (MKStreamCompleteFunc)rdf_complete,
  131.             (MKStreamAbortFunc)rdf_abort, 
  132.             (MKStreamWriteReadyFunc)rdf_write_ready,
  133.             urls->fe_data, cx);
  134. }
  135.  
  136.  
  137.  
  138. void
  139. rdf_GetUrlExitFunc (URL_Struct *urls, int status, MWContext *cx)
  140. {
  141.     char        *navCenterURL = NULL;
  142.  
  143.     if ((status < 0) && (urls != NULL))
  144.     {
  145.         if ((cx != NULL) && (urls->error_msg != NULL))
  146.         {
  147.             FE_Alert(cx, urls->error_msg);
  148.         }
  149.  
  150.         /* if unable to read in navcntr.rdf file, create some default views */
  151.         
  152.         PREF_CopyCharPref("browser.NavCenter", &navCenterURL);
  153.         if (navCenterURL != NULL)
  154.         {
  155.             if (urls->address != NULL)
  156.             {
  157.                 if (!strcmp(urls->address, navCenterURL))
  158.                 {
  159.                     remoteStoreAdd(gRemoteStore, gNavCenter->RDF_BookmarkFolderCategory,
  160.                         gCoreVocab->RDF_parent, gNavCenter->RDF_Top, RDF_RESOURCE_TYPE, 1);
  161.                     remoteStoreAdd(gRemoteStore, gNavCenter->RDF_History, gCoreVocab->RDF_parent,
  162.                         gNavCenter->RDF_Top, RDF_RESOURCE_TYPE, 1);
  163.                     remoteStoreAdd(gRemoteStore, gNavCenter->RDF_LocalFiles, gCoreVocab->RDF_parent,
  164.                         gNavCenter->RDF_Top, RDF_RESOURCE_TYPE, 1);
  165.                 }
  166.             }
  167.             freeMem(navCenterURL);
  168.         }
  169.     }
  170.     NET_FreeURLStruct (urls);
  171. }
  172.  
  173.  
  174.  
  175. int
  176. rdfRetrievalType (RDFFile f)
  177. {
  178.     URL_Struct        *urls;
  179.     char            *url;
  180.     int            type;
  181.  
  182.     url = f->url;
  183.     if (f->localp)
  184.     {
  185.         urls = NET_CreateURLStruct(url,  NET_CACHE_ONLY_RELOAD);
  186.         if ((urls != NULL) && (NET_IsURLInDiskCache(urls) || NET_IsURLInMemCache(urls)))
  187.         {
  188.             type = NET_DONT_RELOAD;
  189.         }
  190.         else
  191.         {
  192.             type = NET_DONT_RELOAD;
  193.         }
  194.         if (urls != NULL)    NET_FreeURLStruct(urls);
  195.     }
  196.     else
  197.     {
  198.         type = NET_DONT_RELOAD;
  199.     }
  200.     return(type);
  201. }
  202.  
  203.  
  204.  
  205. int
  206. rdf_GetURL (MWContext *cx,  int method, Net_GetUrlExitFunc *exit_routine, RDFFile rdfFile)
  207. {
  208.     URL_Struct                      *urls;
  209.  
  210.     if (cx == NULL)  return 0;
  211.     urls = NET_CreateURLStruct(rdfFile->url, NET_DONT_RELOAD);
  212.     if (urls == NULL) return 0;
  213.         /*    urls->use_local_copy = rdfFile->localp;*/
  214.     urls->fe_data = rdfFile;
  215.     if (method) urls->method = method;  
  216.     NET_GetURL(urls, FO_CACHE_AND_RDF, cx, rdf_GetUrlExitFunc);
  217.     return 1;
  218. }
  219.  
  220.  
  221.  
  222.  
  223. void
  224. possiblyRereadRDFFiles (void* data)
  225. {
  226.     possiblyRefreshRDFFiles();
  227. /*    timerID = FE_SetTimeout(possiblyRereadRDFFiles, NULL, 1000 * 60 * 10); 
  228.     once every 10 minutes
  229.     diabled for legal reasons.*/
  230. }
  231.  
  232.  
  233.  
  234. void
  235. RDFglueInitialize()
  236. {
  237.     timerID = FE_SetTimeout(possiblyRereadRDFFiles, NULL, 1000 * 60 * 10); /* once every 10 minutes */
  238.     if (gRLForbiddenDomains != NULL)
  239.     {
  240.         freeMem(gRLForbiddenDomains);
  241.         gRLForbiddenDomains = NULL;
  242.     }
  243.     if (PREF_CopyCharPref("browser.relatedLinksDisabledForDomains", &gRLForbiddenDomains) != PREF_OK)
  244.     {
  245.         gRLForbiddenDomains = NULL;
  246.     }
  247. }
  248.  
  249.  
  250.  
  251. void
  252. RDFglueExit (void)
  253. {
  254.     if (timerID != NULL)
  255.     {
  256.         /* commented out as the timer's window has already been destroyed */
  257.  
  258.         /* FE_ClearTimeout(timerID); */
  259.         timerID = NULL;
  260.     }
  261. }
  262.  
  263.  
  264.  
  265. void *
  266. gRDFMWContext()
  267. {
  268.     void        *cx;
  269.  
  270.     cx = (void *)FE_GetRDFContext();
  271.     return(cx);
  272. }
  273.  
  274.  
  275.  
  276. /* 
  277.  * beginReadingRDFFile is called whenever we need to read something of
  278.  * the net (or local drive). The url of the file to be read is at
  279.  * file->url.  As the bits are read in (and it can take the bits in
  280.  * any sized chunks) it should call parseNextRDFBlob(file, nextBlock,
  281.  * blobSize) when its done, it should call void finishRDFParse
  282.  * (RDFFile f) to abort, it should call void abortRDFParse (RDFFile f)
  283.  * [which will undo all that has been read from that file] 
  284.  */
  285.  
  286. void
  287. beginReadingRDFFile (RDFFile file)
  288. {
  289.     char        *url;
  290.     int        method = 0;
  291.  
  292.     url = file->url;
  293.     if (file->fileType == ES_RT)    method = URL_INDEX_METHOD;
  294.     rdf_GetURL (gRDFMWContext(), method, NULL, file);
  295. }
  296.  
  297.  
  298.  
  299. /* Returns a new string with inURL unescaped. */
  300. /* We return a new string because NET_UnEscape unescapes */
  301. /* string in place */
  302. char *
  303. unescapeURL(char *inURL)
  304. {
  305.     char *escapedPath = copyString(inURL);
  306.  
  307. #ifdef XP_WIN
  308.     replacePipeWithColon(escapedPath);
  309. #endif
  310.  
  311.     NET_UnEscape(escapedPath);
  312.     return (escapedPath);
  313. }
  314.  
  315.  
  316.  
  317. /* Given a file URL of form "file:///", return substring */
  318. /* that can be used as a path for PR_Open. */
  319. /* NOTE: This routine DOESN'T allocate a new string */
  320. char *
  321. convertFileURLToNSPRCopaceticPath(char* inURL)
  322. {
  323. #ifdef    XP_WIN
  324.     if (startsWith("file://", inURL))    return (inURL + 8);
  325.     else if (startsWith("mailbox:/", inURL))    return (inURL + 9);
  326.     else if (startsWith("IMAP:/", inURL))    return (inURL + 6);
  327.     else return (NULL);
  328. #else
  329.     /* For Mac & Unix, need preceeding '/' so that NSPR */
  330.     /* interprets path as full path */
  331.     if (startsWith("file://", inURL))    return (inURL + 7);
  332.     else if (startsWith("mailbox:/", inURL))    return (inURL + 8);
  333.     else if (startsWith("IMAP:/", inURL))    return (inURL + 5);
  334.     else return (NULL);
  335. #endif
  336. }
  337.  
  338.  
  339.  
  340. PRFileDesc *
  341. CallPROpenUsingFileURL(char *fileURL, PRIntn flags, PRIntn mode)
  342. {
  343.     PRFileDesc* result = NULL;
  344.     const char *path;
  345.  
  346.     char *escapedPath = unescapeURL(fileURL);
  347.     path = convertFileURLToNSPRCopaceticPath(escapedPath);
  348.  
  349.     if (path != NULL)    {
  350.         result = PR_Open(path, flags, mode);
  351.     }
  352.  
  353.     if (escapedPath != NULL)    freeMem(escapedPath);
  354.  
  355.     return result;
  356. }
  357.  
  358.  
  359.  
  360. PRDir *
  361. CallPROpenDirUsingFileURL(char *fileURL)
  362. {
  363.     PRDir* result = NULL;
  364.     const char *path;
  365.     char *escapedPath = unescapeURL(fileURL);
  366.     path = convertFileURLToNSPRCopaceticPath(escapedPath);
  367.  
  368.     if (path != NULL)    {
  369.         result = PR_OpenDir(path);
  370.     }
  371.  
  372.     if (escapedPath != NULL)    freeMem(escapedPath);
  373.  
  374.     return result;
  375. }
  376.  
  377.  
  378.  
  379. int32
  380. CallPRWriteAccessFileUsingFileURL(char *fileURL)
  381. {
  382.     int32 result = -1;
  383.     const char *path;
  384.     char *escapedPath = unescapeURL(fileURL);
  385.     path = convertFileURLToNSPRCopaceticPath(escapedPath);
  386.  
  387.     if (path != NULL)    {
  388.         result = PR_Access(path, PR_ACCESS_WRITE_OK);
  389.     }
  390.  
  391.     if (escapedPath != NULL)    freeMem(escapedPath);
  392.  
  393.     return result;
  394. }
  395.  
  396.  
  397.  
  398. int32
  399. CallPRDeleteFileUsingFileURL(char *fileURL)
  400. {
  401.     int32 result = -1;
  402.     const char *path;
  403.     char *escapedPath = unescapeURL(fileURL);
  404.     path = convertFileURLToNSPRCopaceticPath(escapedPath);
  405.  
  406.     if (path != NULL)    {
  407.         result = PR_Delete(path);
  408.     }
  409.  
  410.     if (escapedPath != NULL)    freeMem(escapedPath);
  411.  
  412.     return result;
  413. }
  414.  
  415.  
  416.  
  417. int
  418. CallPR_RmDirUsingFileURL(char *dirURL)
  419. {
  420.     int32 result=-1;
  421.     const char *path;
  422.  
  423.     char *escapedPath = unescapeURL(dirURL);
  424.     path = convertFileURLToNSPRCopaceticPath(escapedPath);
  425.  
  426.     if (path != NULL)    {
  427.         result = PR_RmDir(path);
  428.     }
  429.  
  430.     if (escapedPath != NULL)    freeMem(escapedPath);
  431.  
  432.     return result;
  433. }
  434.  
  435.  
  436.  
  437. int32
  438. CallPRMkDirUsingFileURL(char *dirURL, int mode)
  439. {
  440.     int32 result=-1;
  441.     const char *path;
  442.  
  443.     char *escapedPath = unescapeURL(dirURL);
  444.     path = convertFileURLToNSPRCopaceticPath(escapedPath);
  445.  
  446.     if (path != NULL)    {
  447.         result = PR_MkDir(path,mode);
  448.     }
  449.  
  450.     if (escapedPath != NULL)    freeMem(escapedPath);
  451.  
  452.     return result;
  453. }
  454.  
  455.  
  456.  
  457. DB *
  458. CallDBOpenUsingFileURL(char *fileURL, int flags,int mode, DBTYPE type, const void *openinfo)
  459. {
  460.     DB *result;
  461.     char *path;
  462.     char *escapedPath;
  463.  
  464.         if (fileURL == NULL) return NULL;
  465.  
  466.     escapedPath = unescapeURL(fileURL);
  467.  
  468. #ifdef XP_MAC
  469.     path = WH_FilePlatformName(convertFileURLToNSPRCopaceticPath(fileURL));
  470.     XP_ASSERT(path != NULL);
  471. #else
  472.     
  473.     path = convertFileURLToNSPRCopaceticPath(escapedPath);
  474. #endif
  475.     result = dbopen(path, flags, mode, type, openinfo);
  476. #ifdef XP_MAC
  477.     XP_FREE(path);
  478. #endif
  479.  
  480.     if (escapedPath != NULL)    freeMem(escapedPath);
  481.  
  482.     return result;
  483. }
  484.