Class: Firefox::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/firefox/profile.rb

Overview

A Firefox profile. Allows basic creation and management of a profile. Supports preference setting and addon installation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Profile

Returns a new instance of Profile.



12
13
14
15
# File 'lib/firefox/profile.rb', line 12

def initialize path
  @prefs = Prefs.new(File.join(path,'prefs.js'))
  @addons = Addons.new(File.join(path,'extensions'))
end

Instance Attribute Details

#addonsObject

Returns the value of attribute addons.



10
11
12
# File 'lib/firefox/profile.rb', line 10

def addons
  @addons
end

#prefsObject

Returns the value of attribute prefs.



10
11
12
# File 'lib/firefox/profile.rb', line 10

def prefs
  @prefs
end

Class Method Details

.create(path) ⇒ Object

Register a new profile with firefox. This is the right way to initialize a new profile. It checks if the given directory exists, creates it if not



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/firefox/profile.rb', line 25

def create path
  ini = inifile.dup
  FileUtils.mkdir_p(path) unless File.directory?(path)
  FileUtils.touch(%W[prefs user].map { |f| File.join(path,"#{f}.js") })
  
  last_entry_num = ini.sections.last.scan(/Profile([0-9]*)/).first.first.to_i
  ini["Profile#{last_entry_num+1}"] = {
    'Name' => File.split(path).last,
    'IsRelative' => 0,
    'Path' => path
  }
  ini.write
  new(path)
end

.destroy(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/firefox/profile.rb', line 40

def destroy name
  deleted = false
  # delete profile form registry if name matches
  inifile.sections.each do |section|
    if inifile[section]['Name'] == name.to_s
      FileUtils.rm_rf inifile[section]['Path']
      inifile.delete_section(section)
      deleted = true
    end
  end
  
  # if we just deleted a profile, we need to renumber all other profiles
  # so that firefox doesn't get confused and cries
  if deleted
    new_content = {}
    i = 0
    inifile.sections.each do |s| 
      if s =~ /Profile[0-9]*/
        new_content["Profile#{i}"] = inifile[s]
        inifile.delete_section(s)
        i+=1
      end
    end
    inifile.merge!(new_content)
  end
  
  inifile.write
  deleted
end

.inifile(reload = false) ⇒ Object



70
71
72
73
# File 'lib/firefox/profile.rb', line 70

def inifile reload = false
  @inifile = IniFile.load(File.join(inifile_path(Base.platform(RUBY_PLATFORM)),'profiles.ini')) if @inifile.nil? or reload
  @inifile
end

.inifile_path(os) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/firefox/profile.rb', line 75

def inifile_path os
  case os
  when :osx
    File.join('/','Users',ENV['USER'],'Library','Application Support','Firefox')
  when :linux
    File.join('/','home',ENV['USER'],'.mozilla','firefox')
  when :win
    File.join('C:','Documents and Settings',ENV['USER'],'Application Data','Mozilla','Firefox')
  else
    raise UnsupportedOSError, "I don't really know what OS you're on, sorry"
  end
end

Instance Method Details

#save!Object



17
18
19
# File 'lib/firefox/profile.rb', line 17

def save!
  self.prefs.write!
end