Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
nsDragService.cpp File Reference
#include "nsDragService.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsString.h"
#include "nsXPIDLString.h"
#include "nsReadableUtils.h"
#include "nsIWebBrowserPersist.h"
#include "nsILocalFile.h"
#include "nsIURI.h"
#include "nsIURL.h"
#include "nsNetUtil.h"
#include "nsOS2Uni.h"
#include "nsdefs.h"
#include "wdgtos2rc.h"
#include "nsILocalFileOS2.h"
#include "nsIDocument.h"

Go to the source code of this file.

Defines

#define INCL_DOSMISC
#define INCL_DOSERRORS
#define DC_PREPAREITEM   0x0040
#define MAXTITLELTH   31
#define TITLESEPARATOR   (L' ')
#define DTSHARE_NAME   "\\SHAREMEM\\MOZ_DND"
#define DTSHARE_RMF   "<DRM_DTSHARE, DRF_TEXT>"
#define OS2FILE_NAME   "MOZ_TGT.TMP"
#define OS2FILE_TXTRMF   "<DRM_OS2FILE, DRF_TEXT>"
#define OS2FILE_UNKRMF   "<DRM_OS2FILE, DRF_UNKNOWN>"

Functions

 NS_IMPL_QUERY_INTERFACE4 (nsDragService, nsIDragService, nsIDragService_1_8_BRANCH, nsIDragSession, nsIDragSessionOS2) nsresult RenderToOS2File(PDRAGITEM pditem
nsresult RenderToOS2FileComplete (PDRAGTRANSFER pdxfer, USHORT usResult, PRBool content, char **outText)
nsresult RenderToDTShare (PDRAGITEM pditem, HWND hwnd)
nsresult RenderToDTShareComplete (PDRAGTRANSFER pdxfer, USHORT usResult, char **outText)
nsresult RequestRendering (PDRAGITEM pditem, HWND hwnd, PCSZ pRMF, PCSZ pName)
nsresult GetAtom (ATOM aAtom, char **outText)
nsresult GetFileName (PDRAGITEM pditem, char **outText)
nsresult GetFileContents (PCSZ pszPath, char **outText)
nsresult GetTempFileName (char **outText)
void SaveTypeAndSource (nsILocalFile *file, nsIDOMDocument *domDoc, PCSZ pszType)
int UnicodeToCodepage (const nsAString &inString, char **outText)
int CodepageToUnicode (const nsACString &inString, PRUnichar **outText)
void RemoveCarriageReturns (char *pszText)
MRESULT EXPENTRY nsDragWindowProc (HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
nsresult RenderToOS2File (PDRAGITEM pditem, HWND hwnd)

Variables

HWND hwnd
static HPOINTER gPtrArray [IDC_DNDCOUNT]
static char * gTempFile = 0

Define Documentation

#define DC_PREPAREITEM   0x0040
#define DTSHARE_NAME   "\\SHAREMEM\\MOZ_DND"
#define DTSHARE_RMF   "<DRM_DTSHARE, DRF_TEXT>"

Definition at line 39 of file nsDragService.cpp.

Definition at line 38 of file nsDragService.cpp.

#define MAXTITLELTH   31
#define OS2FILE_NAME   "MOZ_TGT.TMP"
#define OS2FILE_TXTRMF   "<DRM_OS2FILE, DRF_TEXT>"
#define OS2FILE_UNKRMF   "<DRM_OS2FILE, DRF_UNKNOWN>"
#define TITLESEPARATOR   (L' ')

Function Documentation

int CodepageToUnicode ( const nsACString &  inString,
PRUnichar **  outText 
)

Definition at line 1743 of file nsDragService.cpp.

{
  nsAutoChar16Buffer buffer;
  PRInt32 bufLength;
  MultiByteToWideChar(0, PromiseFlatCString(aString).get(),
                      aString.Length(), buffer, bufLength);
  *aResult = ToNewUnicode(nsDependentString(buffer.get()));
  return bufLength;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult GetAtom ( ATOM  aAtom,
char **  outText 
)

Definition at line 1566 of file nsDragService.cpp.

{
  nsresult rv = NS_ERROR_FAILURE;

  ULONG ulInLength = DrgQueryStrNameLen(aAtom);
  if (ulInLength) {
    char* pszText = (char*)nsMemory::Alloc(++ulInLength);
    if (pszText) {
      DrgQueryStrName(aAtom, ulInLength, pszText);
      RemoveCarriageReturns(pszText);
      *outText = pszText;
      rv = NS_OK;
    }
  }
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult GetFileContents ( PCSZ  pszPath,
char **  outText 
)

Definition at line 1610 of file nsDragService.cpp.

{
  nsresult rv = NS_ERROR_FAILURE;
  char* pszText = 0;

  if (pszPath) {
    FILE *fp = fopen(pszPath, "r");
    if (fp) {
      fseek(fp, 0, SEEK_END);
      ULONG filesize = ftell(fp);
      fseek(fp, 0, SEEK_SET);
      if (filesize > 0) {
        size_t readsize = (size_t)filesize;
        pszText = (char*)nsMemory::Alloc(readsize+1);
        if (pszText) {
          readsize = fread((void *)pszText, 1, readsize, fp);
          if (readsize) {
            pszText[readsize] = '\0';
            RemoveCarriageReturns(pszText);
            *outText = pszText;
            rv = NS_OK;
          }
          else {
            nsMemory::Free(pszText);
            pszText = 0;
          }
        }
      }
      fclose(fp);
    }
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult GetFileName ( PDRAGITEM  pditem,
char **  outText 
)

Definition at line 1588 of file nsDragService.cpp.

{
  nsresult rv = NS_ERROR_FAILURE;
  ULONG cntCnr = DrgQueryStrNameLen(pditem->hstrContainerName);
  ULONG cntSrc = DrgQueryStrNameLen(pditem->hstrSourceName);

  char* pszText = (char*)nsMemory::Alloc(cntCnr+cntSrc+1);
  if (pszText) {
    DrgQueryStrName(pditem->hstrContainerName, cntCnr+1, pszText);
    DrgQueryStrName(pditem->hstrSourceName, cntSrc+1, &pszText[cntCnr]);
    pszText[cntCnr+cntSrc] = 0;
    *outText = pszText;
    rv = NS_OK;
  }
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult GetTempFileName ( char **  outText)

Definition at line 1649 of file nsDragService.cpp.

{
  char * pszText = (char*)nsMemory::Alloc(CCHMAXPATH);
  if (!pszText)
    return NS_ERROR_FAILURE;

  char * pszPath;
  if (!DosScanEnv("TEMP", &pszPath) || !DosScanEnv("TMP", &pszPath))
    strcpy(pszText, pszPath);
  else
    if (DosQueryPathInfo(".\\.", FIL_QUERYFULLNAME, pszText, CCHMAXPATH))
      pszText[0] = 0;

  strcat(pszText, "\\");
  strcat(pszText, OS2FILE_NAME);
  *outText = pszText;

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

MRESULT EXPENTRY nsDragWindowProc ( HWND  hWnd,
ULONG  msg,
MPARAM  mp1,
MPARAM  mp2 
)

Definition at line 264 of file nsDragService.cpp.

{
  switch (msg) {

      // if the user requests the contents of a URL be rendered (vs the URL
      // itself), change the suggested target name from the URL's title to
      // the name of the file that will be retrieved
    case DM_RENDERPREPARE: {
      PDRAGTRANSFER  pdxfer = (PDRAGTRANSFER)mp1;
      nsDragService* dragservice = (nsDragService*)pdxfer->pditem->ulItemID;
  
      if (pdxfer->usOperation == DO_COPY &&
          (WinGetKeyState(HWND_DESKTOP, VK_CTRL) & 0x8000) &&
          !strcmp(dragservice->mMimeType, kURLMime)) {
          // QI'ing nsIURL will fail for mailto: and the like
        nsCOMPtr<nsIURL> urlObject(do_QueryInterface(dragservice->mSourceData));
        if (urlObject) {
          nsCAutoString filename;
          urlObject->GetFileName(filename);
          if (filename.IsEmpty()) {
            urlObject->GetHost(filename);
            filename.Append("/file");
          }
          DrgDeleteStrHandle(pdxfer->pditem->hstrTargetName);
          pdxfer->pditem->hstrTargetName = DrgAddStrHandle(filename.get());
        }
      }
      return (MRESULT)TRUE;
    }
  
    case DM_RENDER: {
      nsresult       rv = NS_ERROR_FAILURE;
      PDRAGTRANSFER  pdxfer = (PDRAGTRANSFER)mp1;
      nsDragService* dragservice = (nsDragService*)pdxfer->pditem->ulItemID;
      char           chPath[CCHMAXPATH];

      DrgQueryStrName(pdxfer->hstrRenderToName, CCHMAXPATH, chPath);

        // if the user Ctrl-dropped a URL, use the nsIURL interface
        // to determine if it points to content (i.e. a file);  if so,
        // fetch its contents; if not (e.g. a 'mailto:' url), drop into
        // the code that uses nsIURI to render a URL object

      if (!strcmp(dragservice->mMimeType, kURLMime)) {
        if (pdxfer->usOperation == DO_COPY &&
            (WinGetKeyState(HWND_DESKTOP, VK_CTRL) & 0x8000)) {
          nsCOMPtr<nsIURL> urlObject(do_QueryInterface(dragservice->mSourceData));
          if (urlObject)
            rv = dragservice->SaveAsContents(chPath, urlObject);
        }
        if (!NS_SUCCEEDED(rv)) {
          nsCOMPtr<nsIURI> uriObject(do_QueryInterface(dragservice->mSourceData));
          if (uriObject)
            rv = dragservice->SaveAsURL(chPath, uriObject);
        }
      }
      else
          // if we're dragging text, do NLS conversion then write it to file
        if (!strcmp(dragservice->mMimeType, kUnicodeMime)) {
          nsCOMPtr<nsISupportsString> strObject(
                                 do_QueryInterface(dragservice->mSourceData));
          if (strObject)
            rv = dragservice->SaveAsText(chPath, strObject);
        }
  
      DrgPostTransferMsg(pdxfer->hwndClient, DM_RENDERCOMPLETE, pdxfer,
                         (NS_SUCCEEDED(rv) ? DMFL_RENDEROK : DMFL_RENDERFAIL),
                         0, TRUE);
      DrgFreeDragtransfer(pdxfer);
      return (MRESULT)TRUE;
    }

      // we don't use these msgs but neither does WinDefWindowProc()
    case DM_DRAGOVERNOTIFY:
    case DM_ENDCONVERSATION:
      return 0;
  
    default:
      break;
  }

  return ::WinDefWindowProc(hWnd, msg, mp1, mp2);
}

Here is the call graph for this function:

void RemoveCarriageReturns ( char *  pszText)

Definition at line 1758 of file nsDragService.cpp.

{
  ULONG  cnt;
  char * next;
  char * source;
  char * target;

  target = strchr(pszText, 0x0d);
  if (!target)
    return;

  source = target + 1;

  while ((next = strchr(source, 0x0d)) != 0) {

    cnt = next - source;
    memcpy(target, source, cnt);
    target += cnt;
    source = next + 1;

  }

  strcpy(target, source);
  return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult RenderToDTShare ( PDRAGITEM  pditem,
HWND  hwnd 
)

Definition at line 1475 of file nsDragService.cpp.

{
  nsresult rv;
  void *   pMem;

  APIRET rc = DosAllocSharedMem( &pMem, DTSHARE_NAME, 0x100000,
                                 PAG_WRITE | PAG_READ);
  if (rc == ERROR_ALREADY_EXISTS)
    rc = DosGetNamedSharedMem( &pMem, DTSHARE_NAME,
                               PAG_WRITE | PAG_READ);
  if (rc)
    rv = NS_ERROR_FAILURE;
  else
    rv = RequestRendering( pditem, hwnd, DTSHARE_RMF, DTSHARE_NAME);

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult RenderToDTShareComplete ( PDRAGTRANSFER  pdxfer,
USHORT  usResult,
char **  outText 
)

Definition at line 1497 of file nsDragService.cpp.

{
  nsresult rv = NS_ERROR_FAILURE;
  void * pMem;
  char * pszText = 0;

  APIRET rc = DosGetNamedSharedMem( &pMem, DTSHARE_NAME, PAG_WRITE | PAG_READ);

  if (!rc) {
    if (usResult & DMFL_RENDEROK) {
      pszText = (char*)nsMemory::Alloc( ((ULONG*)pMem)[0] + 1);
      if (pszText) {
        strcpy(pszText, &((char*)pMem)[sizeof(ULONG)] );
        RemoveCarriageReturns(pszText);
        *outText = pszText;
        rv = NS_OK;
      }
    }
      // using DosGetNamedSharedMem() on memory we allocated appears
      // to increment its usage ctr, so we have to free it 2x
    DosFreeMem(pMem);
    DosFreeMem(pMem);
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult RenderToOS2File ( PDRAGITEM  pditem,
HWND  hwnd 
)

Definition at line 1417 of file nsDragService.cpp.

{
  nsresult rv = NS_ERROR_FAILURE;
  nsXPIDLCString fileName;

  if (NS_SUCCEEDED(GetTempFileName(getter_Copies(fileName)))) {
    char * pszRMF;
    if (DrgVerifyRMF(pditem, "DRM_OS2FILE", "DRF_TEXT"))
      pszRMF = OS2FILE_TXTRMF;
    else
      pszRMF = OS2FILE_UNKRMF;

    rv = RequestRendering( pditem, hwnd, pszRMF, fileName.get());
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult RenderToOS2FileComplete ( PDRAGTRANSFER  pdxfer,
USHORT  usResult,
PRBool  content,
char **  outText 
)

Definition at line 1439 of file nsDragService.cpp.

{
  nsresult rv = NS_ERROR_FAILURE;

    // for now, override content flag & always return content
  content = PR_TRUE;

  if (usResult & DMFL_RENDEROK) {
    if (NS_SUCCEEDED(GetAtom( pdxfer->hstrRenderToName, &gTempFile))) {
      if (content)
        rv = GetFileContents(gTempFile, outText);
      else {
        nsCOMPtr<nsILocalFile> file;
        if (NS_SUCCEEDED(NS_NewNativeLocalFile(nsDependentCString(gTempFile),
                                         PR_TRUE, getter_AddRefs(file)))) {
          nsCAutoString textStr;
          NS_GetURLSpecFromFile(file, textStr);
          if (!textStr.IsEmpty()) {
            *outText = ToNewCString(textStr);
            rv = NS_OK;
          }
        }
      }
    }
  }
    // gTempFile will be deleted when ExitSession() is called

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult RequestRendering ( PDRAGITEM  pditem,
HWND  hwnd,
PCSZ  pRMF,
PCSZ  pName 
)

Definition at line 1529 of file nsDragService.cpp.

{
  PDRAGTRANSFER pdxfer = DrgAllocDragtransfer( 1);
  if (!pdxfer)
    return NS_ERROR_FAILURE;
 
  pdxfer->cb = sizeof(DRAGTRANSFER);
  pdxfer->hwndClient = hwnd;
  pdxfer->pditem = pditem;
  pdxfer->hstrSelectedRMF = DrgAddStrHandle( pRMF);
  pdxfer->hstrRenderToName = 0;
  pdxfer->ulTargetInfo = pditem->ulItemID;
  pdxfer->usOperation = (USHORT)DO_COPY;
  pdxfer->fsReply = 0;
 
    // send the msg before setting a render-to name
  if (pditem->fsControl & DC_PREPAREITEM)
    DrgSendTransferMsg( pditem->hwndItem, DM_RENDERPREPARE, (MPARAM)pdxfer, 0);
 
  pdxfer->hstrRenderToName = DrgAddStrHandle( pName);
 
    // send the msg after setting a render-to name
  if ((pditem->fsControl & (DC_PREPARE | DC_PREPAREITEM)) == DC_PREPARE)
    DrgSendTransferMsg( pditem->hwndItem, DM_RENDERPREPARE, (MPARAM)pdxfer, 0);
 
    // ask the source to render the selected item
  if (!DrgSendTransferMsg( pditem->hwndItem, DM_RENDER, (MPARAM)pdxfer, 0))
    return NS_ERROR_FAILURE;
 
  return NS_OK;
}

Here is the caller graph for this function:

void SaveTypeAndSource ( nsILocalFile file,
nsIDOMDocument domDoc,
PCSZ  pszType 
)

Definition at line 1675 of file nsDragService.cpp.

{
  if (!file)
    return;

  nsCOMPtr<nsILocalFileOS2> os2file (do_QueryInterface(file));
  if (!os2file ||
      NS_FAILED(os2file->SetFileTypes(nsDependentCString(pszType))))
    return;

  // since the filetype has to be saved, this function
  // may be called even if there isn't any document
  if (!domDoc)
    return;

  nsCOMPtr<nsIDocument> doc (do_QueryInterface(domDoc));
  if (!doc)
    return;

  // this gets the top-level content URL when frames are used;
  // when nextDoc is zero, currDoc is the browser window, and
  // prevDoc points to the content;
  // note:  neither GetParentDocument() nor GetDocumentURI()
  // refcount the pointers they return, so nsCOMPtr isn't needed
  nsIDocument * prevDoc;
  nsIDocument * currDoc = doc;
  nsIDocument * nextDoc = doc;
  do {
    prevDoc = currDoc;
    currDoc = nextDoc;
    nextDoc = currDoc->GetParentDocument();
  } while (nextDoc);

  nsIURI* srcUri = prevDoc->GetDocumentURI();
  if (!srcUri)
    return;

  // identifying content as coming from chrome is none too useful...
  PRBool ignore = PR_FALSE;
  srcUri->SchemeIs("chrome", &ignore);
  if (ignore)
    return;

  nsCAutoString url;
  srcUri->GetSpec(url);
  os2file->SetFileSource(url);

  return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int UnicodeToCodepage ( const nsAString &  inString,
char **  outText 
)

Definition at line 1731 of file nsDragService.cpp.

{
  nsAutoCharBuffer buffer;
  PRInt32 bufLength;
  WideCharToMultiByte(0, PromiseFlatString(aString).get(), aString.Length(),
                      buffer, bufLength);
  *aResult = ToNewCString(nsDependentCString(buffer.get()));
  return bufLength;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

HPOINTER gPtrArray[IDC_DNDCOUNT] [static]

Definition at line 108 of file nsDragService.cpp.

char* gTempFile = 0 [static]

Definition at line 109 of file nsDragService.cpp.

Definition at line 88 of file nsDragService.cpp.