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

  1. /* -*- Mode: C; tab-width: 4; 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 implements mail support for the rdf data model.
  21.    For more information on this file, contact rjc or guha 
  22.    For more information on RDF, look at the RDF section of www.mozilla.org
  23. */
  24.  
  25. #include "pm2rdf.h"
  26. #include "glue.h"
  27.  
  28.  
  29.  
  30. char *
  31. popmailboxesurl(void)
  32. {
  33.     char        *ans = NULL, *mboxFileURL;
  34.  
  35.     if ((mboxFileURL = makeDBURL("Mail")) != NULL)
  36.     {
  37.         if ((ans = getMem(strlen(mboxFileURL) + 8)) != NULL)
  38.         {
  39.             sprintf(ans, "mailbox:%s", &mboxFileURL[7]);
  40. #ifdef    XP_WIN
  41.             if (ans[10] == ':') ans[10] = '|';
  42. #endif
  43.         }
  44.     }
  45.     return(ans);
  46. }
  47.  
  48.  
  49.  
  50. char *
  51. imapmailboxesurl(void)
  52. {
  53.     char        *ans = NULL, *mboxFileURL;
  54.  
  55.     if ((mboxFileURL = makeDBURL("IMAPMail")) != NULL)
  56.     {
  57.         if ((ans = getMem(strlen(mboxFileURL) + 5)) != NULL)
  58.         {
  59.             sprintf(ans, "imap:%s", &mboxFileURL[7]);
  60. #ifdef    XP_WIN
  61.             if (ans[7] == ':') ans[7] = '|';
  62. #endif
  63.         }
  64.     }
  65.     return(ans);
  66. }
  67.  
  68.  
  69.  
  70. void
  71. buildMailList(RDF_Resource ms)
  72. {
  73.     RDF_Resource        imapmail, popmail;
  74.  
  75.     if ((imapmail = RDF_GetResource(NULL, imapmailboxesurl(), 1)) != NULL)
  76.     {
  77.         setContainerp(imapmail, 1);
  78.         setResourceType(imapmail, IM_RT);
  79.         remoteStoreAdd(gRemoteStore, imapmail, gCoreVocab->RDF_name,
  80.                 "Remote Mail", RDF_STRING_TYPE, 1);
  81.         remoteStoreAdd(gRemoteStore, imapmail, gCoreVocab->RDF_parent,
  82.                 ms, RDF_RESOURCE_TYPE, 1);
  83.     }
  84.     if ((popmail = RDF_GetResource(NULL, popmailboxesurl(), 1)) != NULL)
  85.     {
  86.         setContainerp(popmail, 1);
  87.         setResourceType(popmail, PM_RT);
  88.         remoteStoreAdd(gRemoteStore, popmail, gCoreVocab->RDF_name,
  89.                 "Local Mail", RDF_STRING_TYPE, 1);
  90.         remoteStoreAdd(gRemoteStore, popmail, gCoreVocab->RDF_parent,
  91.                 ms, RDF_RESOURCE_TYPE, 1);
  92.     }
  93. }
  94.  
  95.  
  96.  
  97. PRDir *
  98. OpenMailDir(char *name)
  99. {
  100.     PRBool        nameHacked = false;
  101.     PRDir        *dir = NULL;
  102.     int            pathnameStart=0;
  103.  
  104.     if (startsWith("mailbox:",name))    pathnameStart=POPMAIL_URL_OFFSET;
  105.     else if (startsWith("imap:",name))    pathnameStart=IMAPMAIL_URL_OFFSET;
  106.     if (pathnameStart > 0)
  107.     {
  108. #ifdef    XP_WIN
  109.         if (name[pathnameStart+1] == '|')
  110.         {
  111.             nameHacked = true;
  112.             name[pathnameStart+1] = ':';
  113.         }
  114. #endif
  115.     dir = CallPROpenDirUsingFileURL(name);
  116.  
  117. #ifdef    XP_WIN
  118.         if (nameHacked == true)
  119.         {
  120.             name[pathnameStart+1] = '|';
  121.         }
  122. #endif
  123.     }
  124. /*
  125.     else
  126.     {
  127.         dir = CallPROpenDirUsingFileURL(name);
  128.     }
  129. */
  130.     return(dir);
  131. }
  132.  
  133.  
  134.  
  135. RDFT
  136. MakeMailStore (char* url)
  137. {
  138.   RDFT ntr = (RDFT)getMem(sizeof(RDFT));
  139.   ntr->assert = NULL;
  140.   ntr->unassert = NULL;
  141.   ntr->getSlotValue = pmGetSlotValue;
  142.   ntr->getSlotValues = pmGetSlotValues;
  143.   ntr->hasAssertion = pmHasAssertion;
  144.   ntr->nextValue = pmNextValue;
  145.   ntr->disposeCursor = pmDisposeCursor;
  146.   buildMailList(gNavCenter->RDF_Mail);
  147.   ntr->url = copyString(url);
  148.   return ntr;
  149. }
  150.  
  151.  
  152.  
  153.  
  154. PRBool
  155. pmHasAssertion (RDFT rdf, RDF_Resource u, RDF_Resource s, void* v, 
  156.                RDF_ValueType type, PRBool tv)
  157. {
  158.   if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE) &&
  159.       (resourceType(u) == PM_RT) && (containerp(u)) && 
  160.       (resourceType((RDF_Resource)v) == PM_RT) &&
  161.       (startsWith( resourceID(u), resourceID((RDF_Resource)v)))) {
  162.     PRDir* d = OpenMailDir( resourceID(u));
  163.     char* filePathname =  resourceID((RDF_Resource)v);
  164.     char* name = &filePathname[revCharSearch('/', filePathname)];
  165.     PRDirEntry* de;
  166.     int n = 0;
  167.     PRBool ans = 0;
  168.  
  169.     while (de = PR_ReadDir(d, n++)) {
  170.       if (strcmp(name, de->name) == 0) {
  171.     ans = 1;
  172.     break;
  173.       }
  174.     }
  175.     PR_CloseDir(d);
  176.     return ans;
  177.   } else {
  178.     return 0;
  179.   }
  180. }
  181.  
  182.  
  183.  
  184. void *
  185. pmGetSlotValue (RDFT rdf, RDF_Resource u, RDF_Resource s, RDF_ValueType type, PRBool inversep,  PRBool tv)
  186. {
  187.   if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE) && (pmUnitp(u)) && tv) {
  188.     if (inversep) {
  189.       char* filePathname =  resourceID(u);
  190.       size_t n = revCharSearch('/', filePathname);
  191.       char nname[512];
  192.       PRDir *d;
  193.       PRBool ans = 0;
  194.       memcpy((char*)nname, filePathname, n);
  195.       d = OpenMailDir(nname);
  196.       ans = (d != NULL);
  197.       PR_CloseDir(d);
  198.       if (ans) {
  199.     RDF_Resource r = RDF_GetResource(NULL, nname, 1);
  200.     setResourceType(r, PM_RT);
  201.     setContainerp(r, 1);
  202.     return r;
  203.       } else return NULL;
  204.     } else {
  205.       PRDir* d = OpenMailDir( resourceID(u));
  206.       PRDirEntry* de = PR_ReadDir(d, PR_SKIP_BOTH);
  207.       if (de != NULL) {
  208.     char nname[100];
  209.     sprintf(nname, "%s/%s",  resourceID(u), de->name);
  210.     PR_CloseDir(d);
  211.     return CreatePMUnit(nname, resourceType(u), false);
  212.       } else {
  213.     PR_CloseDir(d);
  214.     return NULL;
  215.       }
  216.     }
  217.   } else if ((s == gCoreVocab->RDF_name) && (type == RDF_STRING_TYPE) && (tv) && (pmUnitp(u))) {
  218.       char *pathname, *name = NULL;
  219.       int len,n;
  220.       
  221.       if (pathname = copyString( resourceID(u))) {
  222.         len = strlen(pathname);
  223.         if (pathname[len-1] == '/')  pathname[--len] = '\0';
  224.         if (endsWith(".sbd", pathname))    pathname[len-4] = '\0';
  225.         n = revCharSearch('/', pathname);
  226.         name = unescapeURL(&pathname[n+1]);
  227.         freeMem(pathname);
  228.       }
  229.     return(name);
  230.   }
  231.   else if ((s == gWebData->RDF_URL) && (type == RDF_STRING_TYPE) && (tv) && (pmUnitp(u))) {
  232.     int        len;
  233.     char        *pos, *name;
  234.  
  235.         if (resourceType(u) == PM_RT) return( copyString(resourceID(u)));
  236.     if (resourceType(u) == IM_RT)
  237.     {
  238.         len = strlen( resourceID(u));
  239.         if (resourceID(u)[len-1] == '/')    return(copyString(resourceID(u)));
  240.         if ((pos=strstr(resourceID(u),"IMAPMail/")) != NULL)
  241.         {
  242.             if ((name = PR_smprintf("imap://%s",pos+9)) != NULL)
  243.             {
  244.  
  245.                 /* IMAP RFC specifies INBOX (all uppercase) ??? */
  246.  
  247.                 if (endsWith ("/Inbox", name))
  248.                 {
  249.                     strcpy(&name[strlen(name)-5],"INBOX");
  250.                 }
  251.             }
  252.             return(name);
  253.         }
  254.     }
  255.   }
  256.  
  257.   return NULL;
  258. }
  259.  
  260.  
  261.  
  262. RDF_Cursor
  263. pmGetSlotValues (RDFT rdf, RDF_Resource u, RDF_Resource s, 
  264.              RDF_ValueType type,  PRBool inversep, PRBool tv)
  265. {
  266.   if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE) && (pmUnitp(u))
  267.       && (inversep) && (tv)) {
  268.     PRDir *d = OpenMailDir(resourceID(u));
  269.     RDF_Cursor c;
  270.     if (d == NULL) return NULL;
  271.     c = (RDF_Cursor) getMem(sizeof(struct RDF_CursorStruct));
  272.     c->u = u;
  273.     c->count = PR_SKIP_BOTH;
  274.     c->pdata = d;
  275.     c->type = type;
  276.     return c;
  277.   } else return NULL;
  278. }
  279.  
  280.  
  281.  
  282. void *
  283. pmNextValue (RDFT rdf, RDF_Cursor c)
  284. {
  285.     PRFileInfo fn;
  286.     int len, pathnameStart=0;
  287.     char *encoded = NULL, *pos;
  288.  
  289.   if (c == NULL) {
  290.     return NULL;
  291.   } else {
  292.  
  293.     PRDirEntry* de = PR_ReadDir((PRDir*)c->pdata, c->count++);
  294.     if (de == NULL) {
  295.  
  296.       PR_CloseDir((PRDir*)c->pdata);
  297.       c->pdata = NULL;
  298.       return NULL;
  299.     } else if ((resourceType((RDF_Resource)(c->u)) == IM_RT) || (endsWith(".snm", (char *)de->name)) || (endsWith(".sbd", (char *)de->name)))    {
  300.  
  301. /*
  302.     } else if (endsWith(".snm", (char*)de->name) || (endsWith(".dat", (char*)de->name))){
  303. */
  304.       char nname[512], *base;
  305.       PRBool isDir = false, sep = (( resourceID(c->u))[strlen( resourceID(c->u))-1] == '/');
  306.     
  307.  
  308.       base = NET_Escape(de->name, URL_PATH);
  309.       if (base != NULL)    {
  310.         if (sep) {
  311.         sprintf(nname, "%s%s",  resourceID(c->u), base);
  312.         } else
  313.         sprintf(nname, "%s/%s",  resourceID(c->u), base);
  314.         XP_FREE(base);
  315.       }
  316.  
  317.        if (resourceType(c->u) == PM_RT)  {
  318.            pathnameStart = POPMAIL_URL_OFFSET;
  319.            encoded = unescapeURL(&nname[pathnameStart]);
  320.          }
  321.        else if (resourceType(c->u) == IM_RT)  {
  322.            pathnameStart = IMAPMAIL_URL_OFFSET;
  323.          }
  324.  
  325.         encoded = unescapeURL(&nname[pathnameStart]);
  326.     if (encoded != NULL)    {
  327.  
  328. #ifdef  XP_WIN
  329.             if (encoded[1] == '|') encoded[1] = ':';
  330. #endif
  331.  
  332.         PR_GetFileInfo(encoded, &fn);
  333.         if (fn.type == PR_FILE_DIRECTORY)    {
  334.             isDir = true;
  335.              len=strlen(nname);
  336.             nname[len] = '/';
  337.             nname[len+1] = '\0';
  338.             }
  339.         else if (resourceType(c->u) == IM_RT)    {
  340.             if ((pos=strstr(encoded,"IMAPMail/")) != NULL)    {
  341.                 sprintf(nname,"imap://%s",pos+9);
  342.                 }
  343.             }
  344.         freeMem(encoded);
  345.         }
  346.  
  347.       return CreatePMUnit(nname, resourceType(c->u), isDir);
  348.     } else {
  349.     return pmNextValue(rdf, c);
  350. }
  351.   }
  352. }
  353.  
  354.  
  355.  
  356. RDF_Error
  357. pmDisposeCursor (RDFT rdf, RDF_Cursor c)
  358. {
  359.   if (c != NULL) {
  360.  
  361.     if (c->pdata) PR_CloseDir((PRDir*)c->pdata);
  362.     freeMem(c);
  363.   }
  364.   return 0;
  365. }
  366.  
  367.  
  368.  
  369. RDF_Resource
  370. CreatePMUnit (char* nname, RDF_BT rdfType, PRBool isDirectoryFlag)
  371. {
  372.   char *name;
  373.   PRBool newName = 0;
  374.   RDF_Resource existing;
  375.  
  376.   if (startsWith("mailbox:/", nname) || startsWith("imap:/", nname)) {
  377.     if (endsWith(".snm", nname)) {
  378.       name = (char*) getMem(strlen(nname)+1);
  379.       memcpy(name, nname, strlen(nname));
  380.       name[strlen(name)-4] = '\0';
  381.       newName = 1;
  382.  
  383.       /* IMAP RFC specifies INBOX (all uppercase) ??? */
  384.       if ((rdfType == IM_RT) && (endsWith ("/Inbox", name))) {
  385.         strcpy(&name[strlen(name)-5],"INBOX");
  386.         }
  387.  
  388.     } else  name = nname;
  389.   } 
  390.   else {
  391.     if (rdfType == PM_RT)    {
  392.       name = (char*) getMem(strlen(nname) + 9);
  393.       memcpy(name,  "mailbox:/", 9);
  394.       memcpy(&name[9], nname, strlen(nname));
  395.       }
  396.     else if (rdfType == IM_RT)    {
  397.       name = (char*) getMem(strlen(nname) + 6);
  398.       memcpy(name,  "imap:/", 6);
  399.       memcpy(&name[6], nname, strlen(nname));
  400.       }
  401.     newName = 1;
  402.     if (endsWith(".snm", nname)) {
  403.       name[strlen(name)-3] = '\0';
  404.     }
  405.   }
  406.  
  407.   existing = RDF_GetResource(NULL, name, 0);
  408.   if (existing != NULL) {
  409.     if (newName) freeMem(name);
  410.     if (isDirectoryFlag) setContainerp(existing, 1);
  411.     return existing;
  412.   } else {
  413.     existing = RDF_GetResource(NULL, name, 1);
  414.     if (isDirectoryFlag) setContainerp(existing, 1);
  415.     setResourceType(existing, rdfType);
  416.     if (newName) freeMem(name);
  417.     return existing;
  418.   }
  419. }
  420.