Class: Device

Inherits:
Object
  • Object
show all
Defined in:
lib/device/library/windows/eject.rb,
lib/device.rb,
lib/device/library/linux.rb,
lib/device/library/windows.rb

Overview

Copyright 2013 whiteleaf. All rights reserved.

rubocop:disable Style/MethodName rubocop:disable Style/VariableName

Defined Under Namespace

Modules: BackupBookmarkUtility, Epub, Ibooks, Ibunko, Kindle, Kobo, Library, Reader Classes: CantEject, DontConneting, SendFailure, UnknownDevice

Constant Summary collapse

DEVICES =
{}.tap do |h|
  [Narou.get_root_dir, File.dirname(__FILE__)].each do |dir|
    next unless dir   # narou init 前だと get_root_dir は nil
    Dir.glob(File.join(dir, "device", "*.rb")).each do |path|
      name = File.basename(path, ".rb")
      unless h[name]
        if Helper.os_windows?
          # パスにマルチバイト文字が使われる可能性があるのでrequireは使えない
          eval(File.read(path, encoding: Encoding::UTF_8), binding, path)
        else
          require path
        end
        h[name] = Device.const_get(name.capitalize)
      end
    end
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_name) ⇒ Device

Returns a new instance of Device.



77
78
79
80
81
82
83
84
85
86
# File 'lib/device.rb', line 77

def initialize(device_name)
  unless Device.exists?(device_name)
    raise UnknownDevice, "#{device_name} という端末は存在しません"
  end
  @device_module = DEVICES[device_name.downcase]
  @name = @device_module::NAME
  @display_name = @device_module::DISPLAY_NAME
  @ebook_file_ext = @device_module::EBOOK_FILE_EXT
  create_device_check_methods
end

Instance Attribute Details

#display_nameObject (readonly)

Returns the value of attribute display_name.



40
41
42
# File 'lib/device.rb', line 40

def display_name
  @display_name
end

#ebook_file_extObject (readonly)

Returns the value of attribute ebook_file_ext.



40
41
42
# File 'lib/device.rb', line 40

def ebook_file_ext
  @ebook_file_ext
end

#nameObject (readonly)

Returns the value of attribute name.



40
41
42
# File 'lib/device.rb', line 40

def name
  @name
end

Class Method Details

.create(device_name) ⇒ Object



69
70
71
72
73
# File 'lib/device.rb', line 69

def self.create(device_name)
  @@device_cache ||= {}
  name = device_name.downcase
  return @@device_cache[name] ||= new(name)
end

.exists?(device) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/device.rb', line 65

def self.exists?(device)
  DEVICES.include?(device.downcase)
end

.support_eject?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/device.rb', line 102

def self.support_eject?
  respond_to?(:eject)
end

Instance Method Details

#connecting?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/device.rb', line 88

def connecting?
  physical_support? && !!get_documents_path
end

#copy_to_documents(src_file) ⇒ Object



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
# File 'lib/device.rb', line 143

def copy_to_documents(src_file)
  documents_path = get_documents_path
  if documents_path
    dst_path = File.join(documents_path, File.basename(src_file))
    # Rubyでコピーするのは遅いのでOSのコマンドを叩く
    case Helper.determine_os
    when :windows
      cmd = "copy /B " + %!"#{src_file}" "#{dst_path}"!.gsub("/", "\\").encode(Encoding::Windows_31J)
      res = Helper::AsyncCommand.exec(cmd)
      unless res[2].success?
        raise SendFailure, res[1].force_encoding(Encoding::Windows_31J).encode(Encoding::UTF_8).rstrip
      end
    else
      res = Helper::AsyncCommand.exec(%!cp "#{src_file}" "#{dst_path}"!)
      unless res[2].success?
        # cp コマンドで送信失敗するとファイルがぶっ壊れたり0バイトのファイルが作られる
        # ので一旦削除しておく
        File.delete(dst_path) if File.exist?(dst_path)
        raise SendFailure, res[1].rstrip
      end
    end
    if Narou.economy?("send_delete")
      FileUtils.rm_f(src_file)
    end
    dst_path
  else
    nil
  end
rescue SendFailure => e
  puts
  error $@.shift + ": #{e.message} (#{e.class})"
  exit Narou::EXIT_ERROR_CODE
end

#ebook_file_old?(src_file) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
# File 'lib/device.rb', line 132

def ebook_file_old?(src_file)
  documents_path = get_documents_path
  if documents_path
    dst_path = File.join(documents_path, File.basename(src_file))
    if File.exist?(dst_path)
      return File.mtime(src_file) > File.mtime(dst_path)
    end
  end
  true
end

#ejectObject



92
93
94
95
96
97
98
99
100
# File 'lib/device.rb', line 92

def eject
  if ejectable?
    begin
      Device.eject(@device_module::VOLUME_NAME)
    rescue CantEject => e
      error e.message
    end
  end
end

#ejectable?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/device.rb', line 106

def ejectable?
  if Device.respond_to?(:ejectable?)
    Device.ejectable?(@device_module::VOLUME_NAME)
  else
    Device.support_eject? && connecting?
  end
end

#find_documents_directory(device_root_dir) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/device.rb', line 114

def find_documents_directory(device_root_dir)
  @device_module::DOCUMENTS_PATH_LIST.each do |documents_path|
    documents_directory = File.join(device_root_dir, documents_path)
    return documents_directory if File.directory?(documents_directory)
  end
  nil
end

#get_documents_pathObject



122
123
124
125
126
127
128
129
130
# File 'lib/device.rb', line 122

def get_documents_path
  if Device.respond_to?(:get_device_root_dir)
    dir = Device.get_device_root_dir(@device_module::VOLUME_NAME)
    if dir
      return find_documents_directory(dir)
    end
  end
  nil
end

#get_hook_moduleObject



181
182
183
# File 'lib/device.rb', line 181

def get_hook_module
  @device_module
end

#get_relative_variablesObject



185
186
187
188
189
190
191
# File 'lib/device.rb', line 185

def get_relative_variables
  if @device_module.const_defined?(:RELATED_VARIABLES)
    @device_module::RELATED_VARIABLES
  else
    {}
  end
end

#physical_support?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/device.rb', line 177

def physical_support?
  @device_module::PHYSICAL_SUPPORT
end