home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / ReadAttachDrag.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.7 KB  |  216 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.    ReadAttachDrag.cpp -- class definitions for message attachment drag source
  20.    Created: Alastair Gourlay(SGI) c/o Dora Hsu<dora@netscape.com>, 26 Nov 1996
  21.  */
  22.  
  23. // classes:
  24. //      XFE_ReadAttachDrag
  25. //
  26.  
  27. #include <stdio.h>
  28. #include <Xm/Xm.h>
  29. #include "ReadAttachDrag.h"
  30. #include "DesktopTypes.h"
  31.  
  32. extern void fe_SaveSynchronousURL(MWContext*,URL_Struct*,const char*);
  33.  
  34. // constructor
  35.  
  36. XFE_ReadAttachDrag::XFE_ReadAttachDrag(Widget w,XFE_ReadAttachPanel *attachPanel) : XFE_DragNetscape(w)
  37. {
  38.     _attachPanel=attachPanel;
  39.     _dragDataURL=NULL;
  40.     _dragDataName=NULL;
  41.     _tmpDirectory=NULL;
  42.     _tmpFileName=NULL;
  43. }
  44.  
  45. // destructor
  46.  
  47. XFE_ReadAttachDrag::~XFE_ReadAttachDrag()
  48. {
  49. }
  50.  
  51.  
  52. // decide if this drag is interesting
  53.  
  54. int XFE_ReadAttachDrag::dragStart(int,int)
  55. {
  56.     if (!_dragWidget || !XmIsPushButton(_dragWidget))
  57.         return FALSE;
  58.  
  59.     // If drag widget is an AttachPanelItem, extract the URL
  60.     XtPointer userData;
  61.     XtVaGetValues(_dragWidget,XmNuserData,&userData,NULL);
  62.     XFE_AttachPanelItem *item=(XFE_AttachPanelItem*)userData;
  63.  
  64.     if (item && item->data()) {
  65.         _attachPanel->selectItem(item);
  66.         _dragDataURL=XP_STRDUP(item->data());
  67.         if (item->dataLabel())
  68.             _dragDataName=XP_STRDUP(item->dataLabel());
  69.         else
  70.             _dragDataName=XP_STRDUP("noname");
  71.             
  72.         setDragIconForType(item->dataType());
  73.         return TRUE;
  74.     }
  75.  
  76.     return FALSE;
  77. }
  78.  
  79. // specify types for this particular drag
  80.  
  81. void XFE_ReadAttachDrag::targets()
  82. {
  83.     _numTargets=2;
  84.     _targets=new Atom[_numTargets];
  85.  
  86.     _targets[0]=_XA_NETSCAPE_URL;
  87.     _targets[1]=XA_STRING;
  88.  
  89.     setFileTarget(_XA_FILE_NAME);
  90. }
  91.  
  92. // specify operations for this particular drag
  93.  
  94. void XFE_ReadAttachDrag::operations()
  95. {
  96.     _operations=XmDROP_COPY;
  97. }
  98.  
  99. // provide data for requested target from targets() list
  100.  
  101. char *XFE_ReadAttachDrag::getTargetData(Atom target)
  102. {
  103.     // WARNING - data *must* be allocated with Xt malloc API, or Xt
  104.     // will spring a leak!
  105.  
  106.     if (!_dragDataURL || !_dragDataName)
  107.         return NULL;
  108.     
  109.     if (target==_XA_NETSCAPE_URL) {
  110.         // translate drag data to NetscapeURL format
  111.         XFE_URLDesktopType urlData;
  112.  
  113.         urlData.createItemList(1);
  114.         urlData.url(0,_dragDataURL);
  115.         return (char*) XtNewString(urlData.getString());
  116.     }
  117.  
  118.     if (target==_XA_FILE_NAME) {
  119.         // save url as appropriately named file in a tmp
  120.         // directory.
  121.  
  122.         if ((_tmpDirectory=XFE_DesktopType::createTmpDirectory())==NULL)
  123.             return NULL;
  124.  
  125.         _tmpFileName=new char[strlen(_tmpDirectory)+1+strlen(_dragDataName)+1];
  126.         sprintf(_tmpFileName,"%s/%s",_tmpDirectory,_dragDataName);
  127.         URL_Struct *urlStruct=NET_CreateURLStruct(_dragDataURL,NET_DONT_RELOAD);
  128.         if (urlStruct) {
  129.             lockFrame();
  130.             fe_SaveSynchronousURL(_attachPanel->context(),urlStruct,_tmpFileName);
  131.             unlockFrame();
  132.         }
  133.  
  134.         // return file name        
  135.         return (char*) XtNewString(_tmpFileName);
  136.     }
  137.  
  138.     if (target==XA_STRING) {
  139.         // return the URL
  140.         return (char*) XtNewString(_dragDataURL);
  141.     }
  142.  
  143.     return NULL;
  144. }
  145.  
  146.     
  147. void XFE_ReadAttachDrag::dragComplete()
  148. {
  149.     if (_dragDataURL) {
  150.         XP_FREE(_dragDataURL);
  151.         _dragDataURL=NULL;
  152.     }
  153.  
  154.     if (_dragDataName) {
  155.         XP_FREE(_dragDataName);
  156.         _dragDataName=NULL;
  157.     }
  158.  
  159.     // if we created tmp files, delete them.
  160.     cleanupDataFiles();    
  161. }
  162.  
  163.  
  164. // provide safe locking when saving to tmp file. Need to
  165. // prevent clicking in thread window or message from interrupting
  166. // save. Ideally fe_SaveSynchronousURL() would use its own
  167. // context and not be interruptable.
  168.  
  169. void XFE_ReadAttachDrag::lockFrame()
  170. {
  171.     Widget s=_widget;
  172.     while (s && !XtIsShell(s)) {
  173.         s=XtParent(s);
  174.     }
  175.     XtSetSensitive(s,FALSE);
  176.  
  177.     CONTEXT_DATA (_attachPanel->context())->clicking_blocked = True;
  178.     fe_SetCursor (_attachPanel->context(), False);
  179. }
  180.  
  181. void XFE_ReadAttachDrag::unlockFrame()
  182. {
  183.     Widget s=_widget;
  184.     while (s && !XtIsShell(s)) {
  185.         s=XtParent(s);
  186.     }
  187.     XtSetSensitive(s,TRUE);
  188.     
  189.     CONTEXT_DATA (_attachPanel->context())->clicking_blocked = False;
  190.     fe_SetCursor (_attachPanel->context(), False);
  191. }
  192.  
  193. // remove tmp files and directory
  194. // make sure that we only remove files that we created in /tmp/nsdndXXXXXX/
  195.  
  196. void XFE_ReadAttachDrag::cleanupDataFiles()
  197. {
  198.  
  199.     if (_tmpFileName) {
  200.         // delete tmp file
  201.         if (_tmpDirectory &&
  202.             strncmp(_tmpFileName,_tmpDirectory,strlen(_tmpDirectory))==0) {
  203.             unlink(_tmpFileName);
  204.         }
  205.         delete _tmpFileName;
  206.         _tmpFileName=NULL;
  207.     }
  208.  
  209.     if (_tmpDirectory) {
  210.         // delete tmp directory
  211.         rmdir(_tmpDirectory);
  212.         free((void*)_tmpDirectory);
  213.         _tmpDirectory=NULL;
  214.     }
  215. }
  216.