Class: Launchy::Spawnable::Browser

Inherits:
Application show all
Defined in:
lib/launchy/spawnable/browser.rb

Constant Summary collapse

DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS =
{
    :kde     => "kfmclient",
    :gnome   => "gnome-open",
    :xfce    => "exo-open",
    :generic => "htmlview"
}
FALLBACK_BROWSERS =
%w[ firefox seamonkey opera mozilla netscape galeon ]
APP_LIST =
{ 
:windows => %w[ start ],
:darwin  => %w[ open ],
:nix     => nix_app_list,
:unknown => [],
}

Constants inherited from Application

Application::KNOWN_OS_FAMILIES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Application

application_classes, find_application_class_for, #find_executable, find_executable, inherited, #my_os, my_os, my_os_family, #my_os_family, nix_desktop_environment, #run

Constructor Details

#initializeBrowser

Returns a new instance of Browser.



57
58
59
# File 'lib/launchy/spawnable/browser.rb', line 57

def initialize
    raise "Unable to find browser to launch for os family '#{my_os_family}'." unless browser
end

Class Method Details

.handle?(*args) ⇒ Boolean

return true if this class can handle the given parameter(s)

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/launchy/spawnable/browser.rb', line 23

def handle?(*args)
    begin 
        uri = URI.parse(args[0])
        return [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
    rescue Exception 
        return false
    end
end

.nix_app_listObject

Find a list of potential browser applications to run on *nix machines.

The order is:

1) What is in ENV['LAUNCHY_BROWSER'] or ENV['BROWSER']
2) xdg-open
3) desktop environment launcher program
4) a list of fallback browsers


38
39
40
41
42
43
44
45
46
# File 'lib/launchy/spawnable/browser.rb', line 38

def nix_app_list
    browser_cmds = ['xdg-open']
    browser_cmds << DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS[nix_desktop_environment]
    browser_cmds << FALLBACK_BROWSERS
    browser_cmds.flatten!
    browser_cmds.delete_if { |b| b.nil? || (b.strip.size == 0) }
    Launchy.log "*Nix Browser List: #{browser_cmds.join(', ')}"
    browser_cmds
end

.run(*args) ⇒ Object



18
19
20
# File 'lib/launchy/spawnable/browser.rb', line 18

def run(*args)
    Browser.new.visit(args[0]) 
end

Instance Method Details

#app_listObject

returns the list of command line application names for the current os



62
63
64
# File 'lib/launchy/spawnable/browser.rb', line 62

def app_list
    APP_LIST[my_os_family]
end

#browserObject

return the full command line path to the browser or nil



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/launchy/spawnable/browser.rb', line 67

def browser
    if not @browser then
        if ENV['LAUNCHY_BROWSER'] and File.exists?(ENV['LAUNCHY_BROWSER']) then
            Launchy.log "Using LAUNCHY_BROWSER environment variable : #{ENV['LAUNCHY_BROWSER']}"
            @browser = ENV['LAUNCHY_BROWSER']
        elsif ENV['BROWSER'] and File.exists?(ENV['BROWSER']) then
            Launchy.log "Using BROWSER environment variable : #{ENV['BROWSER']}"
            @browser = ENV['BROWSER']
        else
            @browser = app_list.collect { |bin| find_executable(bin) }.find { |x| not x.nil? }
            Launchy.log "Using application list : #{@browser}"
        end
    end
    return @browser
end

#visit(url) ⇒ Object

launch the browser at the appointed url



84
85
86
# File 'lib/launchy/spawnable/browser.rb', line 84

def visit(url)
    run(browser,url)
end