[SOLVED] Drag.Paste("text/uri-list") problem.

Post

Posted
Rating:
#1 (In Topic #1113)
Trainee
I dragged some files from "Nautilus File Manager" and Dropped to "Listbox1".
Normally, "Debug a" Should be Processed, but some files Will be Processed with "Debug B".

Public Sub ListBox1_Drop()

Code (gambas)

  1. For Each f As String In Drag.Paste("text/uri-list")
  2.     If Not IsDir(f) Then
  3.       If Exist(f) Then
  4.         Debug "A"
  5.       Else
  6.         Debug "B"
  7.       Endif
  8.     Endif
  9.   Next
  10.  
  11.  

The actual file name is 'ABC+123.txt' but the variable F is 'ABC 123.txt'.
Apparently, the '+' character is incorrectly converted into a space.

I want to solve the error conversion, but I don't know what to do.

Thank you all.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
 Have you tried using FromURL() as it should be an array of URLs

f = Replace(FromURL(f), "file://", "")

If Exist(f) ……
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
 Thank you BruceSteers.

I don't think it is necessary to "Replace()" because the "file://" has already been removed for the variable F in this program.

And I think there is a problem with the "FromURL()" function.

ex) ? FromURL("A+B")
    A B
    
This function is also converted to "+" symbols to space.

Thanks
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
I have found a possible solution to this, it is a little convoluted, but it works for me.

<IMG src="https://www.cogier.com/gambas/ListBoxDrop.png"> </IMG>

Code (gambas)

  1. Public Sub Listbox1_Drop()
  2.  
  3.   Dim sHold As String
  4.   Dim iStart, iEnd As Integer
  5.  
  6.   sHold = Drag.Paste()
  7.   iStart = InStr(sHold, "///") + 2
  8.   iEnd = InStr(sHold, gb.Cr) - iStart
  9.  
  10.   Print Mid(sHold, iStart, iEnd)
  11.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
It does seem to be a bug in FromURL

my solution was to convert the + to an unusual ascii char then convert it back after FromURL…

Code (gambas)

  1.  
  2.   If Drag.Formats.Exist("text/uri-list") Then
  3.   Dim aFiles As String[] = Split(Drag.Data, "\r\n", Null, True)
  4.   For Each f As String In aFiles
  5.     f = Replace(f, "+", "¤")
  6.     f = FromUrl$(f)
  7.     f = Replace(f, "¤", "+")
  8.     Print f
  9.   Next
  10.   Endif
  11.  
  12.  
  13.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
Or maybe a hand made function that only converts any %xx codes to chars
I'll call it FromURI()

Code (gambas)

  1. Public Sub GridView1_Drop()
  2.  
  3.  
  4.   If Drag.Formats.Exist("text/uri-list") Then
  5.   Dim aFiles As String[] = Split(Drag.Data, "\r\n", Null, True)
  6.   For Each f As String In aFiles
  7.     Print FromURI(f)
  8.   Next
  9.   Endif
  10.  
  11.  
  12. Public Sub FromURI(URI As String) As String ' rmove file:// and convert any %xx hex values to chars
  13.  
  14.   Dim sURI As String = URI, sCode As String
  15.   Dim iPos As Integer
  16.   If sURI Begins "file://" Then sURI = Mid(sURI, 8)
  17.   Do
  18.     iPos = InStr(sURI, "%", iPos + 1)
  19.     If Not iPos Then Break
  20.     sCode = Mid(sURI, iPos, 3)
  21.     sURI = Mid(sURI, 1, iPos - 1) & Chr(Val(Replace(sCode, "%", "&"))) & Mid(sURI, iPos + 3)
  22.   Loop
  23.  
  24.  Return sURI
  25.  
  26.  
  27.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
Or here is the same code but as an alternative to the Drag.Paste("text/uri-list") function you used that returns an array…

Code (gambas)

  1. Public Sub GridView1_Drop()
  2.  
  3.   If Drag.Formats.Exist("text/uri-list") Then
  4.     For Each f As String In GetDragFiles()
  5.       Print f
  6.     Next
  7.  
  8.  
  9. Public Sub GetDragFiles() As String[]
  10.  
  11.   Dim aFiles As New String[]
  12.  
  13.   For Each sURI As String In Split(Drag.Data, "\r\n", Null, True)
  14.  
  15.     Dim sCode As String
  16.     Dim iPos As Integer
  17.     If sURI Begins "file://" Then sURI = Mid(sURI, 8)
  18.     Do
  19.       iPos = InStr(sURI, "%", iPos + 1)
  20.       If Not iPos Then Break
  21.       sCode = Mid(sURI, iPos, 3)
  22.       sURI = Mid(sURI, 1, iPos - 1) & Chr(Val(Replace(sCode, "%", "&"))) & Mid(sURI, iPos + 3)
  23.     Loop
  24.  
  25.     aFiles.Add(sURI)
  26.   Next
  27.  
  28.   Return aFiles
  29.  
  30.  
  31.  
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
Just a little ore testing and i found gtk drag data is different to QT

So it seems gtk3 drag data looks something like this

FileName1\r\n
FileName2\r\n

but QT looks something more like this..

FileName1\r\n
-00:00:00\r
FileName2\r\n
-00:00:00\r

so here a URI-list decoder that should work with gtk and qt

Code (gambas)

  1. Public Sub GetDragFiles() As String[]
  2.  
  3.   Dim aFiles As New String[]
  4.   Dim iCount As Integer
  5.   Dim bIsQT As Boolean = Component.IsLoaded("gb.qt4") Or Component.IsLoaded("gb.qt5")
  6.   For Each sURI As String In Split(Drag.Data, "\r\n", Null, True)
  7.     Inc iCount
  8.     If bIsQT And If Even(iCount) Then Continue
  9.     Dim sCode As String
  10.     Dim iPos As Integer
  11.     If sURI Begins "file://" Then sURI = Mid(sURI, 8)
  12.     Do
  13.       iPos = InStr(sURI, "%", iPos + 1)
  14.       If Not iPos Then Break
  15.       sCode = Mid(sURI, iPos, 3)
  16.       sURI = Mid(sURI, 1, iPos - 1) & Chr(Val(Replace(sCode, "%", "&"))) & Mid(sURI, iPos + 3)
  17.     Loop
  18.  
  19.     aFiles.Add(sURI)
  20.   Next
  21.  
  22.   Return aFiles
  23.  
  24.  
  25.  
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
vuott is in the usergroup ‘Regular’

cogier said

Code (gambas)

  1.   sHold = Drag.Paste()
Uhmmm. very interesting !

So, I propose:

Code (gambas)

  1. Public Sub Listbox1_Drop()
  2.  
  3.   Print Replace(Scan(Drag.Paste(), "*//*\r*")[1], "%20", " ")
  4.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
Benoit has accepted this is a bug and has a plan to fix it by making FromURL() and Drag.Paste() only translate + to a space after a '?' Char in a URL.  8-)
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’
It is now fixed in gambas master…
FromURL() does not decode '+' into a space before having encountered the '?'... (b610a6a2) · Commits · Gambas / gambas · GitLab

Well for the FromURL() function.
I'm not at home to test if it also fixed it for Drag.Paste() but if it uses FromURL() then it will be.

Thank you Benoit  8-)
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘Guru’
After examining Drag.class and gb.gui.base source files I can say it is not currently fixed for Drag.paste()

I've suggested a fix to Benoit.
I'm sure it will soon be squashed <EMOJI seq="1f60e" tseq="1f60e">😎</EMOJI>
Online now: No Back to the top

Post

Posted
Rating:
#13
Regular
vuott is in the usergroup ‘Regular’

BruceSteers said

…but if it uses FromURL() then…
Currently, by using "FromUrl()" function:

Code (gambas)

  1. ListBox1.Add(Scan(FromUrl(Drag.Paste()), "*//*\r*")[1])

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#14
Trainee
 > Benoit has accepted this is a bug and has a plan to fix it by making FromURL() and Drag.Paste() only translate + to a space after a '?' Char in a URL.

I compiled the latest Gambas.
And the problem was solved.

Thank you everyone.
Online now: No Back to the top

Post

Posted
Rating:
#15
Guru
BruceSteers is in the usergroup ‘Guru’
You're welcome  8-)

after fixing the interpreters FromURL() function Benoit then modified the Drag.Paste() uri-list function to use FromURL() and removed the code from gb.gui.base it used to use that was essentially the same as FromURL() anyway.

So both FromURL() and Drag.Paste() are fixed and gb.gui.base got a little lighter <EMOJI seq="1f60e" tseq="1f60e">😎</EMOJI>

Just a note….
When something like this happens and a bug gets fixed remember it's only fixed in the latest gambas master.

If you need backward compatibility (if your software is public for others to use they may not have latest gambas) you'll still need a workaround like some of the ideas we provided.

If you know your software is only going to be used on machines that will have latest gambas then it's fine and that bug will never occur again.
Online now: No Back to the top
1 guest and 0 members have just viewed this.