Associate a file with an application made in Lazarus under Linux

4

I developed an application in Lazarus on the linux platform for report printing, now I need to click on the file with any extension, ex arquivo.gsa , open my lazarus application in linux. On windows, it was not so complicated I used this code:

function RegisterLink(Ext, FType, FriendlyName, Cmd: PChar): Boolean;
//
// Tenta associar um tipo de arquivo a um aplicativo
//
// Ext: Extensão a ser registrada.
// FType: Categoria do arquivo
// FriendlyName: Tipo de arquivo
// Cmd: linha de comando para abrí-lo
// RegisterLink('.Bsd','ArqInutil','Arquivo inútil','C:\Windows\Notepad.exe "%1"')
//
var
Key: HKey;
SZEntry: Array[0..255] of Char;
SZSize: LongInt;
begin
Result := True;
if RegOpenKey(HKEY_CLASSES_ROOT,Ext,Key) = ERROR_SUCCESS then
    begin
   SZSize := SizeOf(SZEntry);
   RegQueryValue(Key,'',SZEntry,SZSize);
   StrCat(SZEntry,'\Shell\Open\Command');
   if RegOpenKey(HKEY_CLASSES_ROOT,SZEntry,Key) = ERROR_SUCCESS then
      begin
      SZSize := SizeOf(SZEntry);
      RegQueryValue(Key,'',SZEntry,SZSize);
      if (StrIComp(SZEntry,Cmd) = 0) then // and (MessageDlg('A extensão "'+StrPas(Ext)+ '" já está associada para '+copy(StrPas(SZEntry),1,22)+#13+'Você deseja substituir a associação atual por esta?', mtConfirmation, [mbYes,mbNo],0) <> IDYES)  then
         begin
         Result := False;
         Exit;
         end;
      end;
   end;
RegCreateKey(HKEY_CLASSES_ROOT,Ext,Key);
RegSetValue(Key,'',REG_SZ,FType,StrLen(FType));
RegCreateKey(HKEY_CLASSES_ROOT,FType,Key);
RegSetValue(Key,'',REG_SZ,FriendlyName,StrLen(FriendlyName));
StrCat(StrCopy(SZEntry,FType),'\Shell\Open\Command');
RegCreateKey(HKEY_CLASSES_ROOT,SZEntry,Key);
RegSetValue(Key,'',REG_SZ,Cmd,StrLen(Cmd));
end;

Then I pass this information to the function:

 RegisterLink('.gsa','ArqGestor','Archivos del Informe',pchar(application.exename+' "%1"'))

Now the problem is how to do this in linux?

    
asked by anonymous 21.04.2014 / 20:10

1 answer

2

I believe you can use xdg-utils to do this in a Linux environment.

First, you'll need to register the icon for the MIME > via the command:

xdg-icon-resource install --context mimetypes --size 48 myicon-file-type.png x-application-mytype

You also need to create a configuration file ( freedesktop Shared MIME documentation ):

<?xml version="1.0"?>
 <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
   <mime-type type="application/x-mytype">  
   <comment>Algum comentario</comment>
   <comment xml:lang="en">Some comment</comment>
   <glob pattern="*.gsa"/>
  </mime-type>
 </mime-info>

Install the configuration file:

xdg-mime install mytype-mime.xml

This makes your file recognized and associated with an icon.

For more information, see here and here .

Update

Make sure the code below works for you.

procedure TMainForm.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  Memo1.Lines.Add(ParamStr(0)); // executavel.exe
  for I := 1 to ParamCount do
    Memo1.Lines.Add(Format('Param %d : %s', [I, ParamStr(I)]));

  Memo1.Lines.Add(GetCommandLine);
end; 
    
21.04.2014 / 20:43