[Gambas-user] File extensions

Willy Raets willy at ...2734...
Sun May 19 16:35:00 CEST 2013


On Sat, 2013-05-18 at 01:38 +0200, Willy Raets wrote:
> On Fri, 2013-05-17 at 19:52 +0200, Willy Raets wrote:
> > On Fri, 2013-05-17 at 01:18 +0200, Benoît Minisini wrote:
> > > Le 16/05/2013 17:39, Willy Raets a écrit :
> > > > Hi all,
> > > >
> > > > I'm trying to achieve something but have no idea even where to start
> > > > looking.
> > > >
> > > > Let's say I have a gambas3 application named MyFileMaker
> > > >
> > > > The application MyFileMaker saves files with extension .myfile
> > > >
> > > > What I want to achieve is when double clicking a .myfile for application
> > > > MyFileMaker to open with the double clicked .myfile
> > > >
> > > > 1. How does my application know it is opened because a .myfile was
> > > > double clicked instead of opened from menu as usual as it has to act
> > > > differently?
> > > > 2. How does the system know that a .myfile needs to be opened with
> > > > MyFileMaker?
> > > > 3. How do I give the .myfile their own icon, meaning how does the system
> > > > know that when there is a .myfile it has to use an icon I provided?
> > > >
> > > > This all needs to work when a end user installs MyFileMaker, so:
> > > >
> > > > 4. What do I need to do when making a package. Are there any options
> > > > that need to be set in the packaging wizard and how do they need to be
> > > > set?
> > > >
> > > > Any hint or link to more information would be welcome.
> > > >
> > > 
> > > The 6th step of the wizard packager allows you to define which mimetypes 
> > > are associated with your program.
> > > 
> > > Alas, there is nothing to define a new mimetype, which is needed to make 
> > > the system aware of the *.myfile extension.
> > > 
> > > All the information is there:
> > > 
> > > http://freedesktop.org/wiki/Specifications/shared-mime-info-spec
> > > 
> > > A new mime type can be defined by running the 'xdg-mime install xxx.xml' 
> > > command where 'xxx.xml' is an XML file that defines how to identify the 
> > > mime type.
> > > 
> > > The syntax of that xml file is defined there:
> > > 
> > > http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html#id2604543
> > > 
> > > Regards,
> > 
> > This has been really helpful information, thanks.
> > 
> > 1. Implemented gb.args to get the path and file name that needs opening 
> > 2. Added mimetype information to packager
> > 3. Have a myapp.xml file set in place in packager (I chose
> > for /usr/local/share/mime/packages/)
> > 
> > All I need to do know is code that on first run the myapp.xml file gets
> > added as mimetype
> > 
> > All that needs to be done is:
> > sudo xdg-mime install /usr/local/share/mime/packages/myapp.xml
> > --novendor
> > OR
> > sudo update-mime-database /usr/local/share/mime/
> > 
> > This last option I found in the documentation and needs the xml file to
> > be located in /usr/local/share/mime/packages/
> > 
> > I'm almost there, one more thing:
> > 
> > Is there a likewise solution to handle the icon being associated with
> > the new mimetype.
> > Where do these icons have to be placed and how to tell the system to use
> > them?
> 
> I found this:
> 
> sudo xdg-icon-resource install --context mimetypes --size 48 appicon.png
> application/fdap
> 
> 
> Where application/fdap is the mimetype to connect the icon to and
> appicon.png is a 48x48 icon
> 
> I was wondering where does Gambas store the Project Icon after
> installation (or does it reside inside the gambas executable).
> 
> If so what would be the proper location to copy the icon.png to before
> running the 'xdg-icon-recourse install' mentioned a above.
> 
> Any suggestions what is appropriate location for icons according to
> linux standards, as I can't find any on Google. Maybe I'm using the
> wrong search string, tried all kinds. 
> 
> > 
> > 
> > P.S.
> > If I look at all this is could be implemented in the packager.
> > If mime type defined in Gambas package wizard 6th step give option to
> > make or browse for the xml file in packager wizard and add it
> > automatically to Extra files in step 8 of package wizerd to be copied to
> > to /usr/local/share/mime/packages/ upon installing application.
> > Next have the packager (if mimetype conditions are met) make a script to
> > be run after install doing a
> > update-mime-database /usr/local/share/mime/ 
> > 
> > All xml files residing in /usr/local/share/mime/packages/ will have
> > their mimetypes registered, so you don't even need to know the name of
> > the .xml with above command.
> > 
> > Could something like that be done as it would kind of automate the
> > mimetype configuration during packaging?
> 

Some small mistakes in my last post but for a complete picture and
future reference, here is how I did it.
In Gambas IDE add a myapp.xml file to the hidden folder and a myapp.png
(icon):
Content of xml file:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info
xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/x-myapp">
         <comment xml:lang="de">MyApp Projekt</comment>
         <comment xml:lang="en">MyApp Project</comment>
         <comment xml:lang="es">Proyecto MyApp</comment>
         <comment xml:lang="fr">MyApp projet</comment>
         <comment xml:lang="nl">MyApp Project</comment>
         <icon name="application-x-myapp"/>
         <glob pattern="*.myapp"/>
  </mime-type>
</mime-info>

You can add as many languages as you wish.
The <icon name="application-x-myapp"/> is important to connect the later
installed icon to the .myapp files.

In step 6 of package wizard add application/x-myapp to the mime types
In step 8 of package wizard have myapp.xml copied to:
/usr/local/share/mime/packages
In step 8 of package wizard have myapp.png copied to:
/usr/share/myapp/icons

When application gets installed all will be set in place but no
mimetypes will be installed.
I implemented a setting App/FirstRun to check if application is run for
first time. If so a form Register mimetype opens.

Form Register mimetype has 
a TextBox named tbxPass    -> to enter password
a Button named btnRegister -> to register the mimetype and icon
a TextArea named tarOutput -> to output progress
a Button named btnClose    -> to close the form

The code for Form Register mimetype:
-----
Private $hProcess As Process
Private $bFound As Boolean
Private MyIndex As Integer

Public Sub Form_Open()

  Me.Center
  Me.Caption = Application.Title & " - Register application mime type."
  tbxPass.Clear
  tbxPass.SetFocus
  Me.TopOnly = True
  If IsNull(Settings["App/FirstRun"]) Then
    Me.Border = False
    Me.Background = Color.White
  Endif
  
End

Public Sub btnRegister_Click()

  Dim sCommand As String
  Dim iX As Integer
  btnClose.Enabled = False
  tarOutput.Clear
  Me.Refresh
  Wait 0.15
  If tbxPass.Text = Null Then
    Message.Warning("First enter a password")
    btnClose.Enabled = True
    tbxPass.SetFocus
  Else
    tarOutput.Clear
    For iX = 0 To 1
      If iX = 0 Then
        MyIndex = iX
        sCommand = "update-mime-database /usr/local/share/mime/"
        tarOutput.Text = "Starting registration of MyApp project mime
type...\n "
        Wait 0.05
        tarOutput.Text &= "Running
update-mime-database /usr/local/share/mime/...\n "
        Wait 0.05
        $hProcess = Exec [System.Shell, "-c", "sudo -s " & sCommand] For
Input Output As "Process"
        Wait 0.25
        $hProcess.Close
        Wait 0.05
      Else
        MyIndex = iX
        sCommand = "xdg-icon-resource install --context mimetypes --size
48 /usr/share/myapp/icons/myapp.png application-x-myapp"
        tarOutput.Text &= "Starting registration of MyApp project
icon...\n "
        Wait 0.05
        tarOutput.Text &= "Running xdg-icon-resource install --context
mimetypes --size 48 myapp.png application-x-myapp...\n "
        Wait 0.05
        $hProcess = Exec [System.Shell, "-c", "sudo -s " & sCommand] For
Input Output As "Process"
        Wait 0.25
        $hProcess.Close
        Wait 0.05
      Endif
    Next
    
    
  Endif
  
  Finally
    btnClose.Enabled = True
    btnRegister.Enabled = False
    btnClose.SetFocus
  
  Catch
    Error.Clear

End

Public Sub Process_Read()

  Dim sLine As String
  Dim iB As Integer
  sLine = Read #Last, -256
  If Not $bFound Then
    If Len(sLine) <> 0 Then
      iB = InStr(sLine, "password for", 1)
      If iB > 1 Then
        tarOutput.Text &= "Passing password...\n"
        tarOutput.Insert(sLine & "\n")
        Print #$hProcess, tbxPass.Text
        $bFound = True
      Else
        tarOutput.Insert(sLine)
      Endif
    Endif
  Else
    tarOutput.Insert(sLine)
  Endif
  
End

Public Sub Process_Error(MyError As String)
   
  If Not IsNull($hProcess) Then
    $hProcess.Kill
    tarOutput.Text &= "Terminated registration with reason:\n" & MyError
& "\n"
  Else
    tarOutput.Text &= MyError & "\n"
  Endif
  
End

Public Sub Process_Kill()
   
  If MyIndex = 0 Then
    tarOutput.Text &= "Ended registration of MyApp project mime
type...\n\n"
  Else
    tarOutput.Text &= "\nEnded registration of MyApp project icon..."
  Endif
  
End

Public Sub btnClose_Click()

  Me.Close

End
-----

This should result in all files with extension .myapp to have the
myapp.png as icon in File Manager. Also the files will be marked as
MyApp Project. Double clicking them will start application MyApp.

Always first install the mimetype
sudo update-mime-database /usr/local/share/mime/
Before installing the icon:
sudo xdg-icon-resource install --context mimetypes --size
48 /usr/share/myapp/icons/myapp.png application-x-myapp
as done in above code.

Hope this can be useful for others as well.
Enjoy...

-- 
Kind regards,

Willy (aka gbWilly)

http://gambasshowcase.org/
http://howtogambas.org
http://gambos.org








More information about the User mailing list