Class: WinShell

Inherits:
ShellInterface show all
Includes:
Singleton
Defined in:
lib/piggy-core/winshell.rb,
lib/piggy-core/winshell.rb

Overview

Default access to file system abstraction. A platform dependent singleton will be instantiated.

Instance Method Summary collapse

Methods inherited from ShellInterface

#desktop_directory, #has_extension?, #init_home, #os_path, #picture_directory, #remove_path, #separator, #tmp_path

Constructor Details

#initializeWinShell

Returns a new instance of WinShell.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/piggy-core/winshell.rb', line 137

def initialize
  @shell = WIN32OLE.new('Shell.application')
  begin
    require 'win32/dir'
    @os_paths = {
      "ALTSTARTUP" => Dir::ALTSTARTUP,
      "APPDATA" => Dir::APPDATA,
      "COMMONALTSTARTUP" => Dir::COMMON_ALTSTARTUP,
      "COMMONAPPDATA" => Dir::COMMON_APPDATA,
      "COMMONDESKTOPDIR" => Dir::COMMON_DESKTOPDIRECTORY,
      "COMMONFAVORITES" => Dir::COMMON_FAVORITES,
      "COMMONPROGRAMS" => Dir::COMMON_PROGRAMS,
      "COMMONSTARTMENU" => Dir::COMMON_STARTMENU,
      "COMMONSTARTUP" =>Dir::COMMON_STARTUP,
      "COOKIES" => Dir::COOKIES,
      "DESKTOP" => Dir::DESKTOP,
      "DESKTOPDIRECTORY" => Dir::DESKTOPDIRECTORY,
      "FAVORITES" => Dir::FAVORITES,
      "FONTS" => Dir::FONTS,
      "HISTORY" => Dir::HISTORY,
      "INTERNETCACHE" => Dir::INTERNET_CACHE,
      "LOCALAPPDATA" => Dir::LOCAL_APPDATA,
      "MYPICTURES" => Dir::MYPICTURES,
      "NETHOOD" => Dir::NETHOOD,
      "PERSONAL" => Dir::PERSONAL,
      "PRINTHOOD" => Dir::PRINTHOOD,
      "PROFILE" => Dir::PROFILE,
      "PROGRAMFILES" => Dir::PROGRAM_FILES,
      "PROGRAMS" => Dir::PROGRAMS,
      "RECENT" => Dir::RECENT,
      "SENDTO" => Dir::SENDTO,
      "STARTMENU" => Dir::STARTMENU,
      "STARTUP" => Dir::STARTUP,
      "SYSTEM" => Dir::SYSTEM,
      "TEMPLATES" => Dir::TEMPLATES,
      "WINDOWS" => Dir::WINDOWS
    }
  rescue LoadError => load_error
    @os_paths = {}
  end
  super
end

Instance Method Details

#escape(pathString) ⇒ Object

for use in commandline execution



225
226
227
# File 'lib/piggy-core/winshell.rb', line 225

def escape(pathString)
  return "\"#{pathString}\""
end

#get_folder_items(directoryPathString) ⇒ Object

Get Directory entries as ole-objects, not as strings!



288
289
290
291
292
293
# File 'lib/piggy-core/winshell.rb', line 288

def get_folder_items(directoryPathString)
  folder = shell.NameSpace(win_path(directoryPathString))
  raise "Folderermittlung fehlgeschlagen" if !folder
  items = folder.items
  return (0..items.Count-1).collect { |i| items.item(i) }
end

private; public for better testing only

Raises:

  • (ArgumentError)


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/piggy-core/winshell.rb', line 245

def get_link_item(filename, ole_folder)
  begin
    return ole_folder.ParseName(filename)
  rescue Error => ex
    puts ex.message
  end
  # Fallback to old solution:
  # Compare filename with all item names in folder.
  # This didn't work with Vista's shortcut "Sample Pictures"
  # (The item name in german Vista is "Beispielbilder"
  # which is not equal to the filename.)
  ext = '.' + filename.split('.')[-1]
  linkdef = File.basename(filename,  ext)
  raise(ArgumentError, "Link name empty") if linkdef.empty?
  puts "Location: #{path_string}"
  puts "Shortcut name: #{linkdef}"
  ole_items = ole_folder.items
  ole_item_col = (0..ole_items.Count-1).collect { |i| ole_items.item(i) }
  return ole_item_col.find { |fi| fi.isLink && linkdef == (fi.Name.to_s) }
end

Get the path stored in the Windows shortcut filename. PRE: link?(filename)

Raises:

  • (ArgumentError)


231
232
233
234
235
236
237
238
239
240
241
# File 'lib/piggy-core/winshell.rb', line 231

def get_link_target(filename, path_string = Dir.getwd)
  raise(ArgumentError, "No link") unless link?(filename)
  puts "Compute shortcut: #{filename}"
  filename_only = remove_path(filename) # avoid errors ignore performance
  location = win_path(path_string)
  ole_folder = shell.NameSpace(location)
  raise(ArgumentError, "#{path_string} is no folder") unless ole_folder
  ole_item = get_link_item(filename_only, ole_folder)
  return '' unless ole_item
  return ruby_path(ole_item.GetLink.Path.to_s)
end

#link?(filename) ⇒ Boolean

Is filename a Windows shortcut?

Returns:

  • (Boolean)


210
211
212
# File 'lib/piggy-core/winshell.rb', line 210

def link?(filename)
  return has_extension?(filename, '.lnk')
end

#path_for_folder(folder) ⇒ Object



276
277
278
279
280
281
282
283
284
285
# File 'lib/piggy-core/winshell.rb', line 276

def path_for_folder(folder)
  current = folder.ParentFolder
  path = folder.Title
  while current do
    title = current.Title
    path = title + separator + path
    current = current.ParentFolder
  end
  return path
end

#path_for_special_folder(name) ⇒ Object



180
181
182
183
# File 'lib/piggy-core/winshell.rb', line 180

def path_for_special_folder(name)
  win32_utils_path = @os_paths[name]
  return ruby_path(win32_utils_path ? win32_utils_path : path_from_env(name))
end

#path_from_env(name) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/piggy-core/winshell.rb', line 185

def path_from_env(name)
  path_in_env = ENV[name]
  return path_in_env if(path_in_env)
  if(name=="MYPICTURES")
    win7 = "#{@home}\\Pictures"
    return win7 if(File.exists?(ruby_path(win7)))
  end
  return @home
end

#ruby_path(pathString) ⇒ Object

Get a path string usable by common ruby classes such as Dir and File.



220
221
222
# File 'lib/piggy-core/winshell.rb', line 220

def ruby_path(pathString)
  return pathString.gsub(/\\/, separator)
end

#shellObject

My shell is the ole object “Shell” as described in the Windows SDK.



267
268
269
# File 'lib/piggy-core/winshell.rb', line 267

def shell
  return @shell
end

#special_folder(name) ⇒ Object



271
272
273
274
# File 'lib/piggy-core/winshell.rb', line 271

def special_folder(name)
  id = @special_folder_ids[name]
  return shell.NameSpace(id)
end

#special_folder_listingObject

Include virtual paths on vindows



196
197
198
199
200
201
202
# File 'lib/piggy-core/winshell.rb', line 196

def special_folder_listing
  @special_folder_ids.keys.sort.each do |aFolder|
    puts "#{aFolder}:"
    puts "\tPath: #{path_for_special_folder aFolder}"
    puts "\tVirtual path: #{virtual_path_for aFolder}"
  end
end

#virtual_path_for(name) ⇒ Object



204
205
206
207
# File 'lib/piggy-core/winshell.rb', line 204

def virtual_path_for(name)
  ole_folder = special_folder(name)
  return path_for_folder(ole_folder)
end

#win_path(pathString) ⇒ Object

Get a path string usable by Windows systems.



215
216
217
# File 'lib/piggy-core/winshell.rb', line 215

def win_path(pathString)
  return pathString.gsub(separator, "\\")
end