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

Post your Gambas programming questions here.
keithflundole
Posts: 3
Joined: Saturday 14th October 2023 5:53am

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

Post by keithflundole »

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()

For Each f As String In Drag.Paste("text/uri-list")
    If Not IsDir(f) Then 
      If Exist(f) Then
        Debug "A"
      Else 
        Debug "B"
      Endif 
    Endif
  Next
  
End


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.
Last edited by keithflundole on Thursday 19th October 2023 4:42am, edited 1 time in total.
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Drag.Paste("text/uri-list") problem.

Post by BruceSteers »

Have you tried using FromURL() as it should be an array of URLs

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

If Exist(f) ......
If at first you don't succeed , try doing something differently.
BruceS
keithflundole
Posts: 3
Joined: Saturday 14th October 2023 5:53am

Re: Drag.Paste("text/uri-list") problem.

Post by keithflundole »

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
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Drag.Paste("text/uri-list") problem.

Post by cogier »

I have found a possible solution to this, it is a little convoluted, but it works for me.

Image

Public Sub Listbox1_Drop()
  
  Dim sHold As String
  Dim iStart, iEnd As Integer
  
  sHold = Drag.Paste()
  iStart = InStr(sHold, "///") + 2
  iEnd = InStr(sHold, gb.Cr) - iStart
  
  Print Mid(sHold, iStart, iEnd)
  
End
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Drag.Paste("text/uri-list") problem.

Post by BruceSteers »

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...


  If Drag.Formats.Exist("text/uri-list") Then
  Dim aFiles As String[] = Split(Drag.Data, "\r\n", Null, True)
  For Each f As String In aFiles
    f = Replace(f, "+", "¤")
    f = FromUrl$(f)
    f = Replace(f, "¤", "+")
    Print f
  Next
  Endif 


If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Drag.Paste("text/uri-list") problem.

Post by BruceSteers »

Or maybe a hand made function that only converts any %xx codes to chars
I'll call it FromURI()

Public Sub GridView1_Drop()


  If Drag.Formats.Exist("text/uri-list") Then
  Dim aFiles As String[] = Split(Drag.Data, "\r\n", Null, True)
  For Each f As String In aFiles
    Print FromURI(f)
  Next
  Endif 

End

Public Sub FromURI(URI As String) As String ' rmove file:// and convert any %xx hex values to chars
  
  Dim sURI As String = URI, sCode As String
  Dim iPos As Integer
  If sURI Begins "file://" Then sURI = Mid(sURI, 8)
  Do
    iPos = InStr(sURI, "%", iPos + 1)
    If Not iPos Then Break
    sCode = Mid(sURI, iPos, 3)
    sURI = Mid(sURI, 1, iPos - 1) & Chr(Val(Replace(sCode, "%", "&"))) & Mid(sURI, iPos + 3)
  Loop

 Return sURI

End

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Drag.Paste("text/uri-list") problem.

Post by BruceSteers »

Or here is the same code but as an alternative to the Drag.Paste("text/uri-list") function you used that returns an array...

Public Sub GridView1_Drop()

  If Drag.Formats.Exist("text/uri-list") Then
    For Each f As String In GetDragFiles()
      Print f
    Next
  Endif

End

Public Sub GetDragFiles() As String[]

  Dim aFiles As New String[]

  For Each sURI As String In Split(Drag.Data, "\r\n", Null, True)

    Dim sCode As String
    Dim iPos As Integer
    If sURI Begins "file://" Then sURI = Mid(sURI, 8)
    Do
      iPos = InStr(sURI, "%", iPos + 1)
      If Not iPos Then Break
      sCode = Mid(sURI, iPos, 3)
      sURI = Mid(sURI, 1, iPos - 1) & Chr(Val(Replace(sCode, "%", "&"))) & Mid(sURI, iPos + 3)
    Loop

    aFiles.Add(sURI)
  Next

  Return aFiles

End

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Drag.Paste("text/uri-list") problem.

Post by BruceSteers »

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

Public Sub GetDragFiles() As String[]

  Dim aFiles As New String[]
  Dim iCount As Integer
  Dim bIsQT As Boolean = Component.IsLoaded("gb.qt4") Or Component.IsLoaded("gb.qt5")
  For Each sURI As String In Split(Drag.Data, "\r\n", Null, True)
    Inc iCount
    If bIsQT And If Even(iCount) Then Continue 
    Dim sCode As String
    Dim iPos As Integer
    If sURI Begins "file://" Then sURI = Mid(sURI, 8)
    Do
      iPos = InStr(sURI, "%", iPos + 1)
      If Not iPos Then Break
      sCode = Mid(sURI, iPos, 3)
      sURI = Mid(sURI, 1, iPos - 1) & Chr(Val(Replace(sCode, "%", "&"))) & Mid(sURI, iPos + 3)
    Loop

    aFiles.Add(sURI)
  Next

  Return aFiles

End

If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Drag.Paste("text/uri-list") problem.

Post by vuott »

cogier wrote: Wednesday 18th October 2023 3:20pm
  sHold = Drag.Paste()
Uhmmm. very interesting !

So, I propose:
Public Sub Listbox1_Drop()

  Print Replace(Scan(Drag.Paste(), "*//*\r*")[1], "%20", " ")

End
Last edited by vuott on Wednesday 18th October 2023 9:40pm, edited 1 time in total.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Drag.Paste("text/uri-list") problem.

Post by BruceSteers »

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-)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply