Open A Document From BlitzMax

Archives Forums/Linux Discussion/Open A Document From BlitzMax

SebHoll(Posted 2006) [#1]
Hi,

I need to get my program to open a PDF document when Help is called, but I can't find a command that I can pass the path of the file, and it will open in its default viewer.

In Windows, I am using the ShellExecute command. The PDF file opes up in Adobe Reader as expected. The PDF file is incbin'd into the executable and is extracted into a temporary directory when the program loads, using SaveBank().

In addition, the program needs to continue, i.e. *not halt*, when the file is "executed". (I read somewhere for executing other EXEs, you could do system_ path$ + " &" to acheive this in Linux, but this doesn't work with PDF documents.)

Thanks


Seb


P.S. I am also looking for a Mac compatible command so any help on that is welcome too...


Brucey(Posted 2006) [#2]
I posted a tweak a while back for Mac where OpenURL could do that - not sure what the current implementation of openURL on Mac does atm.

As for Linux, perhaps you need to add a preference to allow them to select their preferred pdf reader? I installed acrobat reader 7 on linux the other day and it changed pdf icons to its own, but I doubt the system would know to use it (acroread) to open them without me telling it to do so...


Robert(Posted 2006) [#3]
Hello,

If the user is using the KDE desktop environment you can do this by running the command:

"kfmclient exec <path>"

eg. "kfmclient exec /path/to/pdf"

If they are using the Gnome desktop environment you can do this by running the command

"gnome-open <path>"

I'm not sure what the easiest way is to detect whether KDE or Gnome is running, but the simplest solution is probably to try one and if that fails, use the other one.


Robert(Posted 2006) [#4]
It is worth noting that the user might not be running KDE or Gnome, although the number of users that have neither utility installed is probably quite small.


skidracer(Posted 2006) [#5]
Thanks Robert! I'm going to do some testing now, from a quick google:


The standard that's developing is that you don't invoke a KDE program
unless KDE_FULL_DESKTOP is set and you don't invoke a Gnome session
unless GNOME_DESKTOP_SESSION_ID is set.



Robert(Posted 2006) [#6]
Here is some C code which will do the job. You should be able to make use of the openURL function from within a .bmx file (I cannot remember how that works - I don't write BMax code that often these days):

The "main" function is just there for a demo, you will need to remove it for use in a BlitzMAX project.

You can try it on your system by saving it somewhere and executing the following from a terminal:

gcc your_file_name.c -o output_name.o
./output_name.o

#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

#include <stdio.h>
#include <errno.h>

int openURL(char* url);

int main(int argc, char** argv)
{
        openURL(argv[1]);
}

void kdeOpenURL(char* url)
{
   const char* launcher = {"kfmclient"};
   execlp(launcher,launcher,"exec",url,0);
}

void gnomeOpenURL(char* url)
{
    const char* launcher = {"gnome-open"};
    execlp(launcher,launcher,url,0);
}

int openURL(char* url)
{
    int pid = fork();

    if ( pid == 0 )
    {
        if (strcmp(getenv("DESKTOP_SESSION"),"kde") == 0)
        {
            kdeOpenURL(url);
            gnomeOpenURL(url);
        }
        else
        {
            gnomeOpenURL(url);
            kdeOpenURL(url);
        }

        exit(1);
    }
    else
    {
        int status = 0;
        waitpid( pid , &status , 0 );

        return ( status == 0 );
    }
}


The code itself will probably compile on a mac, but since neither "gnome-open" nor "kfmclient" are available, a suitable alternative will have to be substituted.

EDIT: Quick explanation of the above code: It creates a new process using the "fork()" call. In the new process, it sees whether KDE or GNOME is running and tries to use the preferred method for that desktop to open the URL. The parent process waits for the child process to return and returns 0 if it was successful ( signified by the child exiting with a status of 0 ) or non-zero otherwise.


skidracer(Posted 2006) [#7]
ok, here's the new implementation of OpenURL which seems to be working with a website url under gnome

	Method OpenURL( url$ )
		If getenv_("KDE_FULL_DESKTOP")
			system_ "kfmclient exec ~q"+url+"~q"
		ElseIf getenv_("GNOME_DESKTOP_SESSION_ID")
			system_ "gnome-open ~q"+url+"~q"
		EndIf
	End Method


syncmods to test


SebHoll(Posted 2006) [#8]
Syncmod'd and I get an "Unhandled Exception: ooops" error when I try to open a PDF document using both absolute and relative paths... In the file browser, PDFs automatically open up in "Document Viewer" when double-clicked.

I've debugged to find which line it stammers on and it's...

system_ "gnome-open ~q"+url+"~q"



skidracer(Posted 2006) [#9]
Seb, can you try a "gnome-open mypdf.pdf" from a command line terminal.


SebHoll(Posted 2006) [#10]
Hi Skid,

I've tried the above: works.
I've tried using system_ "gnome-open mypdf.pdf": works.

However, calling OpenURL$() doesn't...


skidracer(Posted 2006) [#11]
Oops, old version of fltksystem is possibly still overriding that method, will check syncmods... Thanks.


skidracer(Posted 2006) [#12]
OK, Syncmods and try again.


SebHoll(Posted 2006) [#13]
Thanks Skid, FLTK seems now fixed...

Will have to speak to Brucey about updating the GTK MaxGUI module to work with the latest OpenURL()...