Module: Gtk2AppLib::UserSpace

Defined in:
lib/gtk2applib/user_space.rb

Overview

UserSpace primarily sets up the user's hidden application directory, ~/.application_name-majorversion. It copies into the hidden directory the application's appconfig.rb, README.txt, and png images.

Constant Summary

APP_CONF_FILE =
'appconfig'
README =
'README'
RB =
'.rb'
TXT =
'.txt'
MN =
'-'

Class Method Summary (collapse)

Class Method Details

+ (Object) _cp(fr, wfile)



39
40
41
# File 'lib/gtk2applib/user_space.rb', line 39

def self._cp(fr, wfile)
  File.open(wfile, 'w', 0600) { |fw| fw.write fr.read }
end

+ (Object) appconfig



23
24
25
# File 'lib/gtk2applib/user_space.rb', line 23

def self.appconfig
  File.join(Constants::USERDIR, UserSpace.appconfig_base)
end

+ (Object) appconfig_base



15
16
17
# File 'lib/gtk2applib/user_space.rb', line 15

def self.appconfig_base
  APP_CONF_FILE+MN + Constants::APPMINOR + RB
end

+ (Object) appconfig_lib



19
20
21
# File 'lib/gtk2applib/user_space.rb', line 19

def self.appconfig_lib
  File.join('lib', Constants::APPNAME, APP_CONF_FILE+RB)
end

+ (Boolean) copy(rfile, wfile = rfile, directory = Constants::USERDIR)

Copies APPDIR/rfile into the application's hidden directory (by default).



86
87
88
89
90
# File 'lib/gtk2applib/user_space.rb', line 86

def self.copy(rfile, wfile=rfile, directory=Constants::USERDIR)
  UserSpace.cp(
    File.join(Constants::APPDIR, rfile),
    File.join(directory, wfile))
end

+ (Boolean) cp(rfile, wfile)

Copies rfile to wfile if wfile does not exist and makes it rw to user only.



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

def self.cp(rfile, wfile)
  if !File.exist?(wfile) && File.exist?(rfile)
    return UserSpace.cp!(rfile, wfile)
  end
  return false
end

+ (Object) cp!(rfile, wfile)



43
44
45
46
47
# File 'lib/gtk2applib/user_space.rb', line 43

def self.cp!(rfile, wfile)
  File.open(rfile, 'r') { |fr| UserSpace._cp(fr, wfile) }
  $stderr.puts "Wrote #{wfile}" if $trace
  return true
end

+ (Object) cpdir(subdir, pattern)

Copies the contents of subdir (which matches pattern) into the users application hidden directory



105
106
107
108
109
# File 'lib/gtk2applib/user_space.rb', line 105

def self.cpdir(subdir, pattern)
  UserSpace.mkdir(File.join(Constants::USERDIR, subdir))
  return if !File.exist?(dir = File.join(Constants::APPDIR, subdir))
  UserSpace.cpdir_find(dir, pattern)
end

+ (Object) cpdir_find(dir, pattern)



92
93
94
95
96
97
98
# File 'lib/gtk2applib/user_space.rb', line 92

def self.cpdir_find(dir, pattern)
  Find.find(dir) do |fn|
    # Note that needs to be flat directory, no subdirectories
    Find.prune if (fn != dir) && File.directory?(fn)
    UserSpace.copy(fn[Constants::APPDIR.length..fn.length]) if fn=~pattern
  end
end

+ (Object) init

Initiates the user's application hidden space, as needed.

0. Create hidden directory.
1. Copy over the README.txt file.
2. Copy over the pngs (images).
3. Copy over configuration files.


117
118
119
120
121
122
# File 'lib/gtk2applib/user_space.rb', line 117

def self.init
  UserSpace.mkdir(Constants::USERDIR)
  UserSpace.copy(UserSpace.readme_base, UserSpace.readme_lib)
  UserSpace.cpdir('pngs', /\.png$/)
  UserSpace.copy(UserSpace.appconfig_lib, UserSpace.appconfig_base)
end

+ (Boolean) mkdir(directory)

Creates directory if it does not exist and makes it rwx to user only.



75
76
77
78
79
80
# File 'lib/gtk2applib/user_space.rb', line 75

def self.mkdir(directory)
  if !File.exist?(directory)
    return UserSpace.mkdir!(directory)
  end
  return false
end

+ (Object) mkdir!(directory)



62
63
64
65
66
67
68
# File 'lib/gtk2applib/user_space.rb', line 62

def self.mkdir!(directory)
  Dir.mkdir(directory)
  # if available for the OS...
  File.chmod(0700, directory)
  $stderr.puts "Created #{directory}" if $trace
  return true
end

+ (Object) readme



35
36
37
# File 'lib/gtk2applib/user_space.rb', line 35

def self.readme
  File.join(Constants::USERDIR, UserSpace.readme_lib)
end

+ (Object) readme_base



27
28
29
# File 'lib/gtk2applib/user_space.rb', line 27

def self.readme_base
  README+TXT
end

+ (Object) readme_lib



31
32
33
# File 'lib/gtk2applib/user_space.rb', line 31

def self.readme_lib
  README+MN + Constants::APPMINOR + TXT
end