forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsDragService.cpp
1795 lines (1477 loc) · 54.8 KB
/
nsDragService.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#define INCL_DOSMISC
#define INCL_DOSERRORS
#include "nsDragService.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsString.h"
#include "nsXPIDLString.h"
#include "nsReadableUtils.h"
#include "nsIWebBrowserPersist.h"
#include "nsIFile.h"
#include "nsIURI.h"
#include "nsIURL.h"
#include "nsNetUtil.h"
#include "nsOS2Uni.h"
#include "wdgtos2rc.h"
#include "nsILocalFileOS2.h"
#include "nsIDocument.h"
#include "nsGUIEvent.h"
#include "nsISelection.h"
#include <algorithm>
// --------------------------------------------------------------------------
// Local defines
// undocumented(?)
#ifndef DC_PREPAREITEM
#define DC_PREPAREITEM 0x0040
#endif
// limit URL object titles to a reasonable length
#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>"
// not defined in the OS/2 toolkit headers
extern "C" {
APIRET APIENTRY DosQueryModFromEIP(HMODULE *phMod, ULONG *pObjNum,
ULONG BuffLen, PCHAR pBuff,
ULONG *pOffset, ULONG Address);
}
// --------------------------------------------------------------------------
// Helper functions
nsresult RenderToOS2File( PDRAGITEM pditem, HWND hwnd);
nsresult RenderToOS2FileComplete(PDRAGTRANSFER pdxfer, USHORT usResult,
bool 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(nsIFile *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);
// --------------------------------------------------------------------------
// Global data
static HPOINTER gPtrArray[IDC_DNDCOUNT];
static char * gTempFile = 0;
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
nsDragService::nsDragService()
{
// member initializers and constructor code
mDragWnd = WinCreateWindow( HWND_DESKTOP, WC_STATIC, 0, 0, 0, 0, 0, 0,
HWND_DESKTOP, HWND_BOTTOM, 0, 0, 0);
WinSubclassWindow( mDragWnd, nsDragWindowProc);
HMODULE hModResources = NULLHANDLE;
DosQueryModFromEIP(&hModResources, NULL, 0, NULL, NULL, (ULONG) &gPtrArray);
for (int i = 0; i < IDC_DNDCOUNT; i++)
gPtrArray[i] = ::WinLoadPointer(HWND_DESKTOP, hModResources, i+IDC_DNDBASE);
}
// --------------------------------------------------------------------------
nsDragService::~nsDragService()
{
// destructor code
WinDestroyWindow(mDragWnd);
for (int i = 0; i < IDC_DNDCOUNT; i++) {
WinDestroyPointer(gPtrArray[i]);
gPtrArray[i] = 0;
}
}
NS_IMPL_ISUPPORTS_INHERITED1(nsDragService, nsBaseDragService, nsIDragSessionOS2)
// --------------------------------------------------------------------------
NS_IMETHODIMP nsDragService::InvokeDragSession(nsIDOMNode *aDOMNode,
nsISupportsArray *aTransferables,
nsIScriptableRegion *aRegion,
uint32_t aActionType)
{
if (mDoingDrag)
return NS_ERROR_UNEXPECTED;
nsresult rv = nsBaseDragService::InvokeDragSession(aDOMNode, aTransferables,
aRegion, aActionType);
NS_ENSURE_SUCCESS(rv, rv);
mSourceDataItems = aTransferables;
WinSetCapture(HWND_DESKTOP, NULLHANDLE);
// Assume we are only dragging one thing for now
PDRAGINFO pDragInfo = DrgAllocDraginfo(1);
if (!pDragInfo)
return NS_ERROR_UNEXPECTED;
pDragInfo->usOperation = DO_DEFAULT;
DRAGITEM dragitem;
dragitem.hwndItem = mDragWnd;
dragitem.ulItemID = (ULONG)this;
dragitem.fsControl = DC_OPEN;
dragitem.cxOffset = 2;
dragitem.cyOffset = 2;
dragitem.fsSupportedOps = DO_COPYABLE|DO_MOVEABLE|DO_LINKABLE;
// since there is no source file, leave these "blank"
dragitem.hstrContainerName = NULLHANDLE;
dragitem.hstrSourceName = NULLHANDLE;
rv = NS_ERROR_FAILURE;
ULONG idIcon = 0;
// bracket this to reduce our footprint before the drag begins
{
nsCOMPtr<nsISupports> genericItem;
mSourceDataItems->GetElementAt(0, getter_AddRefs(genericItem));
nsCOMPtr<nsITransferable> transItem (do_QueryInterface(genericItem));
nsCOMPtr<nsISupports> genericData;
uint32_t len = 0;
// see if we have a URL or text; if so, the title method
// will save the data and mimetype for use with a native drop
if (NS_SUCCEEDED(transItem->GetTransferData(kURLMime,
getter_AddRefs(genericData), &len))) {
nsXPIDLCString targetName;
rv = GetUrlAndTitle( genericData, getter_Copies(targetName));
if (NS_SUCCEEDED(rv)) {
// advise PM that we need a DM_RENDERPREPARE msg
// *before* it composes a render-to filename
dragitem.fsControl |= DC_PREPAREITEM;
dragitem.hstrType = DrgAddStrHandle("UniformResourceLocator");
dragitem.hstrRMF = DrgAddStrHandle("<DRM_OS2FILE,DRF_TEXT>");
dragitem.hstrTargetName = DrgAddStrHandle(targetName.get());
idIcon = IDC_DNDURL;
}
}
else
if (NS_SUCCEEDED(transItem->GetTransferData(kUnicodeMime,
getter_AddRefs(genericData), &len))) {
nsXPIDLCString targetName;
rv = GetUniTextTitle( genericData, getter_Copies(targetName));
if (NS_SUCCEEDED(rv)) {
dragitem.hstrType = DrgAddStrHandle("Plain Text");
dragitem.hstrRMF = DrgAddStrHandle("<DRM_OS2FILE,DRF_TEXT>");
dragitem.hstrTargetName = DrgAddStrHandle(targetName.get());
idIcon = IDC_DNDTEXT;
}
}
}
// if neither URL nor text are available, make this a Moz-only drag
// by making it unidentifiable to native apps
if (NS_FAILED(rv)) {
mMimeType = 0;
dragitem.hstrType = DrgAddStrHandle("Unknown");
dragitem.hstrRMF = DrgAddStrHandle("<DRM_UNKNOWN,DRF_UNKNOWN>");
dragitem.hstrTargetName = NULLHANDLE;
}
DrgSetDragitem(pDragInfo, &dragitem, sizeof(DRAGITEM), 0);
DRAGIMAGE dragimage;
memset(&dragimage, 0, sizeof(DRAGIMAGE));
dragimage.cb = sizeof(DRAGIMAGE);
dragimage.fl = DRG_ICON;
if (idIcon)
dragimage.hImage = gPtrArray[idIcon-IDC_DNDBASE];
if (dragimage.hImage) {
dragimage.cyOffset = 8;
dragimage.cxOffset = 2;
}
else
dragimage.hImage = WinQuerySysPointer(HWND_DESKTOP, SPTR_FILE, FALSE);
mDoingDrag = true;
LONG escState = WinGetKeyState(HWND_DESKTOP, VK_ESC) & 0x01;
HWND hwndDest = DrgDrag(mDragWnd, pDragInfo, &dragimage, 1, VK_BUTTON2,
(void*)0x80000000L); // Don't lock the desktop PS
// determine whether the drag ended because Escape was pressed
if (hwndDest == 0 && (WinGetKeyState(HWND_DESKTOP, VK_ESC) & 0x01) != escState)
mUserCancelled = true;
FireDragEventAtSource(NS_DRAGDROP_END);
mDoingDrag = false;
// do clean up; if the drop completed,
// the target will delete the string handles
if (hwndDest == 0)
DrgDeleteDraginfoStrHandles(pDragInfo);
DrgFreeDraginfo(pDragInfo);
// reset nsDragService's members
mSourceDataItems = 0;
mSourceData = 0;
mMimeType = 0;
// reset nsBaseDragService's members
mSourceDocument = nullptr;
mSourceNode = nullptr;
mSelection = nullptr;
mDataTransfer = nullptr;
mUserCancelled = false;
mHasImage = false;
mImage = nullptr;
mImageX = 0;
mImageY = 0;
mScreenX = -1;
mScreenY = -1;
return NS_OK;
}
// --------------------------------------------------------------------------
MRESULT EXPENTRY nsDragWindowProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
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) {
nsAutoCString 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);
}
//-------------------------------------------------------------------------
// if the versions of Start & EndDragSession in nsBaseDragService
// were called (and they shouldn't be), they'd break nsIDragSessionOS2;
// they're overridden here and turned into no-ops to prevent this
NS_IMETHODIMP nsDragService::StartDragSession()
{
NS_ERROR("OS/2 version of StartDragSession() should never be called!");
return NS_OK;
}
NS_IMETHODIMP nsDragService::EndDragSession(bool aDragDone)
{
NS_ERROR("OS/2 version of EndDragSession() should never be called!");
return NS_OK;
}
// --------------------------------------------------------------------------
NS_IMETHODIMP nsDragService::GetNumDropItems(uint32_t *aNumDropItems)
{
if (mSourceDataItems)
mSourceDataItems->Count(aNumDropItems);
else
*aNumDropItems = 0;
return NS_OK;
}
// --------------------------------------------------------------------------
NS_IMETHODIMP nsDragService::GetData(nsITransferable *aTransferable,
uint32_t aItemIndex)
{
// make sure that we have a transferable
if (!aTransferable)
return NS_ERROR_INVALID_ARG;
// get flavor list that includes all acceptable flavors (including
// ones obtained through conversion). Flavors are nsISupportsCStrings
// so that they can be seen from JS.
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsISupportsArray> flavorList;
rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
if (NS_FAILED(rv))
return rv;
// count the number of flavors
uint32_t cnt;
flavorList->Count (&cnt);
for (unsigned int i= 0; i < cnt; ++i ) {
nsCOMPtr<nsISupports> genericWrapper;
flavorList->GetElementAt(i, getter_AddRefs(genericWrapper));
nsCOMPtr<nsISupportsCString> currentFlavor;
currentFlavor = do_QueryInterface(genericWrapper);
if (currentFlavor) {
nsXPIDLCString flavorStr;
currentFlavor->ToString(getter_Copies(flavorStr));
nsCOMPtr<nsISupports> genericItem;
mSourceDataItems->GetElementAt(aItemIndex, getter_AddRefs(genericItem));
nsCOMPtr<nsITransferable> item (do_QueryInterface(genericItem));
if (item) {
nsCOMPtr<nsISupports> data;
uint32_t tmpDataLen = 0;
rv = item->GetTransferData(flavorStr, getter_AddRefs(data),
&tmpDataLen);
if (NS_SUCCEEDED(rv)) {
rv = aTransferable->SetTransferData(flavorStr, data, tmpDataLen);
break;
}
}
}
}
return rv;
}
// --------------------------------------------------------------------------
// This returns true if any of the dragged items support a specified data
// flavor. This doesn't make a lot of sense when dragging multiple items:
// all of them ought to match. OTOH, Moz doesn't support multiple drag
// items so no problems arise. If they do, use the commented-out code to
// switch from "any" to "all".
NS_IMETHODIMP nsDragService::IsDataFlavorSupported(const char *aDataFlavor,
bool *_retval)
{
if (!_retval)
return NS_ERROR_INVALID_ARG;
*_retval = false;
uint32_t numDragItems = 0;
if (mSourceDataItems)
mSourceDataItems->Count(&numDragItems);
if (!numDragItems)
return NS_OK;
// return true if all items support this flavor
// for (uint32_t itemIndex = 0, *_retval = true;
// itemIndex < numDragItems && *_retval; ++itemIndex) {
// *_retval = false;
// return true if any item supports this flavor
for (uint32_t itemIndex = 0;
itemIndex < numDragItems && !(*_retval); ++itemIndex) {
nsCOMPtr<nsISupports> genericItem;
mSourceDataItems->GetElementAt(itemIndex, getter_AddRefs(genericItem));
nsCOMPtr<nsITransferable> currItem (do_QueryInterface(genericItem));
if (currItem) {
nsCOMPtr <nsISupportsArray> flavorList;
currItem->FlavorsTransferableCanExport(getter_AddRefs(flavorList));
if (flavorList) {
uint32_t numFlavors;
flavorList->Count( &numFlavors );
for (uint32_t flavorIndex=0; flavorIndex < numFlavors; ++flavorIndex) {
nsCOMPtr<nsISupports> genericWrapper;
flavorList->GetElementAt(flavorIndex, getter_AddRefs(genericWrapper));
nsCOMPtr<nsISupportsCString> currentFlavor;
currentFlavor = do_QueryInterface(genericWrapper);
if (currentFlavor) {
nsXPIDLCString flavorStr;
currentFlavor->ToString ( getter_Copies(flavorStr) );
if (strcmp(flavorStr, aDataFlavor) == 0) {
*_retval = true;
break;
}
}
} // for each flavor
}
}
}
return NS_OK;
}
// --------------------------------------------------------------------------
// use nsIWebBrowserPersist to fetch the contents of a URL
nsresult nsDragService::SaveAsContents(PCSZ pszDest, nsIURL* aURL)
{
nsCOMPtr<nsIURI> linkURI(do_QueryInterface(aURL));
if (!linkURI)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIWebBrowserPersist> webPersist(
do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1"));
if (!webPersist)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIFile> file;
NS_NewNativeLocalFile(nsDependentCString(pszDest), true,
getter_AddRefs(file));
if (!file)
return NS_ERROR_FAILURE;
FILE* fp;
if (NS_FAILED(file->OpenANSIFileDesc("wb+", &fp)))
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDOMDocument> domDoc;
GetSourceDocument(getter_AddRefs(domDoc));
nsCOMPtr<nsIDocument> document = do_QueryInterface(domDoc);
fwrite("", 0, 1, fp);
fclose(fp);
webPersist->SaveURI(linkURI, nullptr, nullptr, nullptr, nullptr, file,
document->GetLoadContext());
return NS_OK;
}
// --------------------------------------------------------------------------
// save this URL in a file that the WPS will identify as a WPUrl object
nsresult nsDragService::SaveAsURL(PCSZ pszDest, nsIURI* aURI)
{
nsAutoCString strUri;
aURI->GetSpec(strUri);
if (strUri.IsEmpty())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIFile> file;
NS_NewNativeLocalFile(nsDependentCString(pszDest), true,
getter_AddRefs(file));
if (!file)
return NS_ERROR_FAILURE;
FILE* fp;
if (NS_FAILED(file->OpenANSIFileDesc("wb+", &fp)))
return NS_ERROR_FAILURE;
fwrite(strUri.get(), strUri.Length(), 1, fp);
fclose(fp);
nsCOMPtr<nsIDOMDocument> domDoc;
GetSourceDocument(getter_AddRefs(domDoc));
SaveTypeAndSource(file, domDoc, "UniformResourceLocator");
return NS_OK;
}
// --------------------------------------------------------------------------
// save this text to file after conversion to the current codepage
nsresult nsDragService::SaveAsText(PCSZ pszDest, nsISupportsString* aString)
{
nsAutoString strData;
aString->GetData(strData);
if (strData.IsEmpty())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIFile> file;
NS_NewNativeLocalFile(nsDependentCString(pszDest), true,
getter_AddRefs(file));
if (!file)
return NS_ERROR_FAILURE;
nsXPIDLCString textStr;
int cnt = UnicodeToCodepage(strData, getter_Copies(textStr));
if (!cnt)
return NS_ERROR_FAILURE;
FILE* fp;
if (NS_FAILED(file->OpenANSIFileDesc("wb+", &fp)))
return NS_ERROR_FAILURE;
fwrite(textStr.get(), cnt, 1, fp);
fclose(fp);
nsCOMPtr<nsIDOMDocument> domDoc;
GetSourceDocument(getter_AddRefs(domDoc));
SaveTypeAndSource(file, domDoc, "Plain Text");
return NS_OK;
}
// --------------------------------------------------------------------------
// Split a Moz Url/Title into its components, save the Url for use by
// a native drop, then compose a title.
nsresult nsDragService::GetUrlAndTitle(nsISupports *aGenericData,
char **aTargetName)
{
// get the URL/title string
nsCOMPtr<nsISupportsString> strObject ( do_QueryInterface(aGenericData));
if (!strObject)
return NS_ERROR_FAILURE;
nsAutoString strData;
strObject->GetData(strData);
// split string into URL and Title -
// if there's a title but no URL, there's no reason to continue
int32_t lineIndex = strData.FindChar ('\n');
if (lineIndex == 0)
return NS_ERROR_FAILURE;
// get the URL portion of the text
nsAutoString strUrl;
if (lineIndex == -1)
strUrl = strData;
else
strData.Left(strUrl, lineIndex);
// save the URL for later use
nsCOMPtr<nsIURI> saveURI;
NS_NewURI(getter_AddRefs(saveURI), strUrl);
if (!saveURI)
return NS_ERROR_FAILURE;
// if there's a bona-fide title & it isn't just a copy of the URL,
// limit it to a reasonable length, perform NLS conversion, then return
if (++lineIndex && lineIndex != (int)strData.Length() &&
!strUrl.Equals(Substring(strData, lineIndex, strData.Length()))) {
uint32_t strLth = std::min((int)strData.Length()-lineIndex, MAXTITLELTH);
nsAutoString strTitle;
strData.Mid(strTitle, lineIndex, strLth);
if (!UnicodeToCodepage(strTitle, aTargetName))
return NS_ERROR_FAILURE;
mSourceData = do_QueryInterface(saveURI);
mMimeType = kURLMime;
return NS_OK;
}
// if the URI can be handled as a URL, construct a title from
// the hostname & filename; if not, use the first MAXTITLELTH
// characters that appear after the scheme name
nsAutoCString strTitle;
nsCOMPtr<nsIURL> urlObj( do_QueryInterface(saveURI));
if (urlObj) {
nsAutoCString strFile;
urlObj->GetHost(strTitle);
urlObj->GetFileName(strFile);
if (!strFile.IsEmpty()) {
strTitle.AppendLiteral("/");
strTitle.Append(strFile);
}
else {
urlObj->GetDirectory(strFile);
if (strFile.Length() > 1) {
nsAutoCString::const_iterator start, end, curr;
strFile.BeginReading(start);
strFile.EndReading(end);
strFile.EndReading(curr);
for (curr.advance(-2); curr != start; --curr)
if (*curr == '/')
break;
strTitle.Append(Substring(curr, end));
}
}
}
else {
saveURI->GetSpec(strTitle);
int32_t index = strTitle.FindChar (':');
if (index != -1) {
if ((strTitle.get())[++index] == '/')
if ((strTitle.get())[++index] == '/')
++index;
strTitle.Cut(0, index);
}
if (strTitle.Length() > MAXTITLELTH)
strTitle.Truncate(MAXTITLELTH);
}
*aTargetName = ToNewCString(strTitle);
mSourceData = do_QueryInterface(saveURI);
mMimeType = kURLMime;
return NS_OK;
}
// --------------------------------------------------------------------------
// Construct a title for text drops from the leading words of the text.
// Alphanumeric characters are copied to the title; sequences of
// non-alphanums are replaced by a single space
nsresult nsDragService::GetUniTextTitle(nsISupports *aGenericData,
char **aTargetName)
{
// get the string
nsCOMPtr<nsISupportsString> strObject ( do_QueryInterface(aGenericData));
if (!strObject)
return NS_ERROR_FAILURE;
// alloc a buffer to hold the unicode title text
int bufsize = (MAXTITLELTH+1)*2;
PRUnichar * buffer = (PRUnichar*)nsMemory::Alloc(bufsize);
if (!buffer)
return NS_ERROR_FAILURE;
nsAutoString strData;
strObject->GetData(strData);
nsAutoString::const_iterator start, end;
strData.BeginReading(start);
strData.EndReading(end);
// skip over leading non-alphanumerics
for( ; start != end; ++start)
if (UniQueryChar( *start, CT_ALNUM))
break;
// move alphanumerics into the buffer & replace contiguous
// non-alnums with a single separator character
int ctr, sep;
for (ctr=0, sep=0; start != end && ctr < MAXTITLELTH; ++start) {
if (UniQueryChar( *start, CT_ALNUM)) {
buffer[ctr] = *start;
ctr++;
sep = 0;
}
else
if (!sep) {
buffer[ctr] = TITLESEPARATOR;
ctr++;
sep = 1;
}
}
// eliminate trailing separators & lone characters
// orphaned when the title is truncated
if (sep)
ctr--;
if (ctr >= MAXTITLELTH - sep && buffer[ctr-2] == TITLESEPARATOR)
ctr -= 2;
buffer[ctr] = 0;
// if we ended up with no alnums, call the result "text";
// otherwise, do NLS conversion
if (!ctr) {
*aTargetName = ToNewCString(NS_LITERAL_CSTRING("text"));
ctr = 1;
}
else
ctr = UnicodeToCodepage( nsDependentString(buffer), aTargetName);
// free our buffer, then exit
nsMemory::Free(buffer);
if (!ctr)
return NS_ERROR_FAILURE;
mSourceData = aGenericData;
mMimeType = kUnicodeMime;
return NS_OK;
}
// --------------------------------------------------------------------------
// nsIDragSessionOS2
// --------------------------------------------------------------------------
// DragOverMsg() provides minimal handling if a drag session is already
// in progress. If not, it assumes this is a native drag that has just
// entered the window and calls NativeDragEnter() to start a session.
NS_IMETHODIMP nsDragService::DragOverMsg(PDRAGINFO pdinfo, MRESULT &mr,
uint32_t* dragFlags)
{
nsresult rv = NS_ERROR_FAILURE;
if (!&mr || !dragFlags || !pdinfo || !DrgAccessDraginfo(pdinfo))
return rv;
*dragFlags = 0;
mr = MRFROM2SHORT(DOR_NEVERDROP, 0);
// examine the dragged item & "start" a drag session if OK;
// also, signal the need for a dragenter event
if (!mDoingDrag)
if (NS_SUCCEEDED(NativeDragEnter(pdinfo)))
*dragFlags |= DND_DISPATCHENTEREVENT;
// if we're in a drag, set it up to be dispatched
if (mDoingDrag) {
SetCanDrop(false);
switch (pdinfo->usOperation) {
case DO_COPY:
SetDragAction(DRAGDROP_ACTION_COPY);
break;
case DO_LINK:
SetDragAction(DRAGDROP_ACTION_LINK);
break;
default:
SetDragAction(DRAGDROP_ACTION_MOVE);
break;
}
if (mSourceNode)
*dragFlags |= DND_DISPATCHEVENT | DND_GETDRAGOVERRESULT | DND_MOZDRAG;
else
*dragFlags |= DND_DISPATCHEVENT | DND_GETDRAGOVERRESULT | DND_NATIVEDRAG;
rv = NS_OK;
}
DrgFreeDraginfo(pdinfo);
return rv;
}
// --------------------------------------------------------------------------
// Evaluates native drag data, and if acceptable, creates & stores
// a transferable with the available flavors (but not the data);
// if successful, it "starts" the session.
NS_IMETHODIMP nsDragService::NativeDragEnter(PDRAGINFO pdinfo)
{
nsresult rv = NS_ERROR_FAILURE;
bool isFQFile = FALSE;
bool isAtom = FALSE;
PDRAGITEM pditem = 0;
if (pdinfo->cditem != 1)
return rv;
pditem = DrgQueryDragitemPtr(pdinfo, 0);
if (pditem) {
if (DrgVerifyRMF(pditem, "DRM_ATOM", 0)) {
isAtom = TRUE;
rv = NS_OK;
}
else
if (DrgVerifyRMF(pditem, "DRM_DTSHARE", 0))
rv = NS_OK;
else
if (DrgVerifyRMF(pditem, "DRM_OS2FILE", 0)) {
rv = NS_OK;
if (pditem->hstrContainerName && pditem->hstrSourceName)
isFQFile = TRUE;
}
}
if (NS_SUCCEEDED(rv)) {
rv = NS_ERROR_FAILURE;
nsCOMPtr<nsITransferable> trans(
do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
if (trans) {
trans->Init(nullptr);
bool isUrl = DrgVerifyType(pditem, "UniformResourceLocator");
bool isAlt = (WinGetKeyState(HWND_DESKTOP, VK_ALT) & 0x8000);
// if this is a fully-qualified file or the item claims to be
// a Url, identify it as a Url & also offer it as html
if ((isFQFile && !isAlt) || isUrl) {
trans->AddDataFlavor(kURLMime);
trans->AddDataFlavor(kHTMLMime);
}
// everything is always "text"
trans->AddDataFlavor(kUnicodeMime);
// if we can create the array, initialize the session
nsCOMPtr<nsISupportsArray> transArray(
do_CreateInstance("@mozilla.org/supports-array;1", &rv));
if (transArray) {
transArray->InsertElementAt(trans, 0);
mSourceDataItems = transArray;
// add the dragged data to the transferable if we have easy access
// to it (i.e. no need to read a file or request rendering); for
// URLs, we'll skip creating a title until the drop occurs
nsXPIDLCString someText;
if (isAtom) {
if (NS_SUCCEEDED(GetAtom(pditem->ulItemID, getter_Copies(someText))))
NativeDataToTransferable( someText.get(), 0, isUrl);
}
else
if (isFQFile && !isAlt &&
NS_SUCCEEDED(GetFileName(pditem, getter_Copies(someText)))) {
nsCOMPtr<nsIFile> file;
if (NS_SUCCEEDED(NS_NewNativeLocalFile(someText, true,
getter_AddRefs(file)))) {
nsAutoCString textStr;
NS_GetURLSpecFromFile(file, textStr);
if (!textStr.IsEmpty()) {
someText.Assign(ToNewCString(textStr));
NativeDataToTransferable( someText.get(), 0, TRUE);
}
}
}
mSourceNode = 0;
mSourceDocument = 0;
mDoingDrag = TRUE;
rv = NS_OK;
}
}
}
return rv;
}
// --------------------------------------------------------------------------
// Invoked after a dragover event has been dispatched, this constructs
// a reply to DM_DRAGOVER based on the canDrop & dragAction attributes.
NS_IMETHODIMP nsDragService::GetDragoverResult(MRESULT& mr)
{
nsresult rv = NS_ERROR_FAILURE;
if (!&mr)
return rv;
if (mDoingDrag) {
bool canDrop = false;
USHORT usDrop;
GetCanDrop(&canDrop);
if (canDrop)
usDrop = DOR_DROP;
else
usDrop = DOR_NODROP;
uint32_t action;
USHORT usOp;
GetDragAction(&action);
if (action & DRAGDROP_ACTION_COPY)
usOp = DO_COPY;
else
if (action & DRAGDROP_ACTION_LINK)
usOp = DO_LINK;
else {
if (mSourceNode)
usOp = DO_MOVE;
else
usOp = DO_UNKNOWN+1;
if (action == DRAGDROP_ACTION_NONE)
usDrop = DOR_NODROP;
}
mr = MRFROM2SHORT(usDrop, usOp);
rv = NS_OK;
}
else
mr = MRFROM2SHORT(DOR_NEVERDROP, 0);
return rv;
}
// --------------------------------------------------------------------------
// have the client dispatch the event, then call ExitSession()
NS_IMETHODIMP nsDragService::DragLeaveMsg(PDRAGINFO pdinfo, uint32_t* dragFlags)
{
if (!mDoingDrag || !dragFlags)
return NS_ERROR_FAILURE;
if (mSourceNode)
*dragFlags = DND_DISPATCHEVENT | DND_EXITSESSION | DND_MOZDRAG;
else
*dragFlags = DND_DISPATCHEVENT | DND_EXITSESSION | DND_NATIVEDRAG;
return NS_OK;
}
// --------------------------------------------------------------------------
// DropHelp occurs when you press F1 during a drag; apparently,
// it's like a regular drop in that the target has to do clean up
NS_IMETHODIMP nsDragService::DropHelpMsg(PDRAGINFO pdinfo, uint32_t* dragFlags)
{
if (!mDoingDrag)
return NS_ERROR_FAILURE;
if (pdinfo && DrgAccessDraginfo(pdinfo)) {
DrgDeleteDraginfoStrHandles(pdinfo);
DrgFreeDraginfo(pdinfo);
}
if (!dragFlags)
return NS_ERROR_FAILURE;
if (mSourceNode)
*dragFlags = DND_DISPATCHEVENT | DND_EXITSESSION | DND_MOZDRAG;
else
*dragFlags = DND_DISPATCHEVENT | DND_EXITSESSION | DND_NATIVEDRAG;
return NS_OK;
}
// --------------------------------------------------------------------------