Class: WinProfile

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

Overview

WinProfile

Constant Summary collapse

FOLDERS_BASE =

Base regkey for folder redirection

'Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
PROFILE_BASE =

Default base for profile folders (its a Windows default)

'%USERPROFILE%'
FOLDER_DEFAULTS =

List of user folder redirection regkeys

[
{:name => "AppData", :dir => 'Datos de programa' },
{:name => "Desktop", :dir => 'Escritorio' },
{:name => "Favorites", :dir => 'Favoritos' },
{:name => "NetHood", :dir => 'Entorno de red' },
{:name => "Personal", :dir => 'Mis documentos' },
{:name => "PrintHood", :dir => 'Impresoras' },
{:name => "Programs", :dir => 'Menú Inicio\Programas' },
{:name => "SendTo", :dir => 'SendTo' },
{:name => "Start Menu", :dir => 'Menú Inicio' },
{:name => "Startup", :dir => 'Menú Inicio\Programas\Inicio' },
{:name => "Templates", :dir => 'Plantillas' },
{:name => "Local Settings", :dir => 'Configuración Local' },
{:name => "Local AppData", :dir => 'Configuración Local\Datos de programa' },
{:name => "Cache", :dir => 'Configuración Local\Archivos temporales de Internet' },
{:name => "Cookies", :dir => 'Cookies' },
{:name => "Recent", :dir => 'Reciente' },
{:name => "History", :dir => 'Configuración Local\Historial' },
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, profiles = "/home/samba/profiles", homes = "/home") ⇒ WinProfile

Returns a new instance of WinProfile.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/winprofile.rb', line 40

def initialize(user=nil,profiles="/home/samba/profiles",homes="/home")
  # Defaults
  @profiles = profiles
  @homes = homes
  @folders = Array.new()

  # If user=nil don't check existence, late initialization
  if user != nil
    # HKLU hive file
    @file = "#{@profiles}/#{user}/NTUSER.DAT"
    raise "Invalid profile: #{@file}" unless File.exists? @file
  end
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



9
10
11
# File 'lib/winprofile.rb', line 9

def debug
  @debug
end

#fileObject

Returns the value of attribute file.



9
10
11
# File 'lib/winprofile.rb', line 9

def file
  @file
end

#foldersObject (readonly)

Returns the value of attribute folders.



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

def folders
  @folders
end

#homesObject

Returns the value of attribute homes.



9
10
11
# File 'lib/winprofile.rb', line 9

def homes
  @homes
end

#profilesObject

Returns the value of attribute profiles.



9
10
11
# File 'lib/winprofile.rb', line 9

def profiles
  @profiles
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/winprofile.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#change_folder(folder = nil, base = nil, dir = nil) ⇒ Object

Stage a folder to be changed to the given base. The changes will be written on commit method



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/winprofile.rb', line 63

def change_folder(folder=nil, base=nil, dir=nil)
 
  # Set defaults if parameters not given
  base=PROFILE_BASE unless base

  FOLDER_DEFAULTS.each do |key|
    if key[:name] == folder
      puts "Found key: #{key[:name]}" if @debug
      # Ok key found
      dir=key[:dir] unless dir
      # Add it to stage
      @folders.push({ :name => folder, :value => base+'\\'+dir })
    end
  end
  @folders
end

#commitObject

Commit (write) changes to hive file. NOTE: You should ALWAYS commit, or you will lose the changes



186
187
188
189
190
191
192
193
194
# File 'lib/winprofile.rb', line 186

def commit
  # Compose changes array
  @changes=Array.new
  @folders.each do |key|
    @changes.push({ :name => FOLDERS_BASE+'\\'+key[:name], :value => key[:value] })
  end
  w=WinReg.new(@file,@debug)
  w.write_keys(@changes)
end

#init_folders(dir) ⇒ Object

Initialize a roving profile directory structure in the given directory



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/winprofile.rb', line 147

def init_folders(dir)

  FOLDER_DEFAULTS.each do |key|
    @folder=dir+"/"+key[:dir]
    @folder.gsub!('\\','/')
    if not File.directory? @folder
      File.makedirs @folder
    end
  end

end

#move_folder(folder, orig, dest) ⇒ Object

Move a given profile folder to a new destination



167
168
169
170
171
172
# File 'lib/winprofile.rb', line 167

def move_folder(folder,orig,dest)
  
  puts "Moving #{orig}/#{key[:dir]} ->  #{dest}/#{key[:dir]}" if @verbose
  FileUtils.mv "#{orig}/#{key[:dir]}", "#{dest}/#{key[:dir]}"

end

#move_folders(orig, dest) ⇒ Object

Move ALL profile folders to a new destination



160
161
162
163
164
# File 'lib/winprofile.rb', line 160

def move_folders(orig,dest)
  
  puts "Moving #{orig} ->  #{dest}" if @verbose
  FileUtils.mv orig,dest
end

#redirect_folder(folder, dir) ⇒ Object

Redirects a given user folders to a dir



127
128
129
130
131
132
133
134
# File 'lib/winprofile.rb', line 127

def redirect_folder(folder,dir)

  w=WinReg.new(@file)
  w.debug=@debug

  w.write_key(FOLDERS_BASE+'\\'+folder,dir)

end

#redirect_folders(dir) ⇒ Object

Redirects all user folders to given dir



137
138
139
140
141
142
143
144
# File 'lib/winprofile.rb', line 137

def redirect_folders(dir)

  w=WinReg.new(@file)
  w.debug=@debug
  FOLDER_DEFAULTS.each do |key|
    w.write_key(FOLDERS_BASE+'\\'+key[:name],dir+'\\'+key[:dir])
  end
end

#reset_defaultObject

Reset profile to default folders



175
176
177
178
179
180
181
182
# File 'lib/winprofile.rb', line 175

def reset_default
  
  w=WinReg.new(@file)
  w.debug=@debug
  FOLDER_DEFAULTS.each do |key|
    w.write_key(FOLDERS_BASE+'\\'+key[:name],PROFILE_BASE+'\\'+key[:dir])
  end
end

#show_changed_folder(folder) ⇒ Object

Show if a folder is staged for change. Returns the staged value



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/winprofile.rb', line 81

def show_changed_folder(folder)
  @location=nil

  @folders.each do |key|
    if key[:name] == folder
      @location = key[:value]
      puts "#{key[:name]} -> #{@location}" if @verbose
    end
  end
  @location
end

#show_changed_foldersObject

Show all folders staged for change. Returns @folders



94
95
96
# File 'lib/winprofile.rb', line 94

def show_changed_folders
  @folders
end

#show_folder(folder) ⇒ Object

Show folder redirection status for a given folder



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/winprofile.rb', line 99

def show_folder(folder)
  @location=nil

  w=WinReg.new(@file)
  w.debug=@debug

  FOLDER_DEFAULTS.each do |key|
    if key[:name] == folder
      @location = w.read_key(FOLDERS_BASE+'\\'+key[:name])
      puts "#{key[:name]} -> #{@location}" if @verbose
    end
  end
  @location
end

#show_foldersObject

Show folder redirection status for all folders



115
116
117
118
119
120
121
122
123
124
# File 'lib/winprofile.rb', line 115

def show_folders

  w=WinReg.new(@file)
  w.debug=@debug

  FOLDER_DEFAULTS.each do |key|
    @location = w.read_key(FOLDERS_BASE+'\\'+key[:name])
    puts "#{key[:name]} -> #{@location}" if @verbose
  end
end

#user=(user) ⇒ Object

Set the user whose profile we want to change



55
56
57
58
59
60
# File 'lib/winprofile.rb', line 55

def user=(user)
  # HKLU hive file
  @file = "#{@profiles}/#{user}/NTUSER.DAT"

  raise "Invalid profile: #{@file}" unless File.exists? @file
end