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"
{
  "force.enable_half_indent_bracket" => false,
  "force.enable_add_date_to_title" => false,    # タイトルを変えてもiBooksに反映されないため
}

Instance Method Summary collapse

Instance Method Details

#extract_epub(ebook_file_path, epubdir_path) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/device/ibooks.rb', line 74

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



65
66
67
68
69
70
71
72
# File 'lib/device/ibooks.rb', line 65

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

#get_ibooks_containing_epub_listObject



107
108
109
# File 'lib/device/ibooks.rb', line 107

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

#hook_change_settings(&original_func) ⇒ Object



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

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

#hook_convert_txt_to_ebook_file(&original_func) ⇒ Object

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



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

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
  if @argument_target_type == :file
    warn "テキストファイル変換時はiBooksへの登録は行われません"
    return ebook_file_path
  end
  @toc_url = @novel_data["toc_url"]
  epubdir_path = get_epubdir_path_in_ibooks_container
  if epubdir_path && File.exists?(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)
      puts "iBooksへの登録を確認しました"
    else
      error "EPUBの展開後のフォルダが見つかりませんでした。" \
            "iBooksがインストールされているか確認して下さい"
    end
  end
  ebook_file_path
end

#regist_epubdir_path_to_setting(path) ⇒ Object



111
112
113
114
115
# File 'lib/device/ibooks.rb', line 111

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

#watch_ibooks_container(ebook_file_path) ⇒ Object



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

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