Module: Device::Ibooks

Defined in:
lib/device/ibooks.rb

Overview

Copyright 2013 whiteleaf. All rights reserved.

Constant Summary collapse

PHYSICAL_SUPPORT =
false
VOLUME_NAME =
nil
DOCUMENTS_PATH_LIST =
nil
EBOOK_FILE_EXT =
".epub"
NAME =
"iBooks"
DISPLAY_NAME =
"iBooks"
IBOOKS_CONTAINER_DIR =
"~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books"
{
  "default.enable_half_indent_bracket" => false,
}

Instance Method Summary collapse

Instance Method Details

#extract_epub(ebook_file_path, epubdir_path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/device/ibooks.rb', line 71

def extract_epub(ebook_file_path, epubdir_path)
  require "zip"
  Zip.on_exists_proc = true
  Zip::File.open(ebook_file_path) do |zip_file|
    zip_file.each do |entry|
      extract_path = File.join(epubdir_path, entry.name)
      FileUtils.mkdir_p(File.dirname(extract_path))
      entry.extract(extract_path)
    end
  end
end

#get_epubdir_path_in_ibooks_containerObject



62
63
64
65
66
67
68
69
# File 'lib/device/ibooks.rb', line 62

def get_epubdir_path_in_ibooks_container
  list = Inventory.load("ibooks_epubdir_path_list")
  if list[@toc_url]
    list[@toc_url]
  else
    nil
  end
end

#get_ibooks_containing_epub_listObject



104
105
106
# File 'lib/device/ibooks.rb', line 104

def get_ibooks_containing_epub_list
  Dir.glob("#{@@__ibooks_container_dir}/*.epub/")
end

#hook_change_settings(&original_func) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/device/ibooks.rb', line 20

def hook_change_settings(&original_func)
  @@__already_exec_change_settings ||= false
  return if @@__already_exec_change_settings
  @@__ibooks_container_dir = File.expand_path(IBOOKS_CONTAINER_DIR)
  unless File.exist?(@@__ibooks_container_dir)
    error "iBooksの管理フォルダが見つかりませんでした。" \
          "OSX Mavericks以降のiBooksのみ管理に対応しています"
    @@__ibooks_container_dir = nil
  end
  @@__already_exec_change_settings = true
end

#hook_convert_txt_to_ebook_file(&original_func) ⇒ Object

EPUBへ変換したあとiBooksが管理しているディレクトリに展開する



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/device/ibooks.rb', line 35

def hook_convert_txt_to_ebook_file(&original_func)
  ebook_file_path = original_func.call
  return ebook_file_path unless ebook_file_path
  return ebook_file_path unless @@__ibooks_container_dir
  epubdir_path = nil
  if @argument_target_type == :file
    @toc_url = nil
  else
    @toc_url = @novel_data["toc_url"]
    epubdir_path = get_epubdir_path_in_ibooks_container
  end
  if epubdir_path && File.exist?(epubdir_path)
    extract_epub(ebook_file_path, epubdir_path)
    puts "iBooksに登録してあるEPUBを更新しました"
  else
    epubdir_path = watch_ibooks_container(ebook_file_path)
    if epubdir_path
      regist_epubdir_path_to_setting(epubdir_path) if @toc_url
      puts "iBooksへの登録を確認しました"
    else
      error "EPUBの展開後のフォルダが見つかりませんでした。" \
            "iBooksがインストールされているか確認して下さい"
    end
  end
  ebook_file_path
end

#regist_epubdir_path_to_setting(path) ⇒ Object



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

def regist_epubdir_path_to_setting(path)
  list = Inventory.load("ibooks_epubdir_path_list")
  list[@toc_url] = path
  list.save
end

#watch_ibooks_container(ebook_file_path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/device/ibooks.rb', line 83

def watch_ibooks_container(ebook_file_path)
  just_before_list = get_ibooks_containing_epub_list
  unless system(%!open -a iBooks "#{ebook_file_path}"!)
    error "iBooksが開けませんでした"
    return nil
  end
  limit = 15
  found_path = nil
  while limit > 0
    sleep(1)
    just_after_list = get_ibooks_containing_epub_list
    differ = just_after_list - just_before_list
    if differ.length == 1
      found_path = differ[0]
      break
    end
    limit -= 1
  end
  found_path
end