alexandria-book-collection-manager 0.6.9.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (293) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rubocop.yml +68 -0
  4. data/.rubocop_todo.yml +200 -0
  5. data/CHANGELOG.md +23 -0
  6. data/COPYING +339 -0
  7. data/ChangeLog.0 +3598 -0
  8. data/Gemfile +9 -0
  9. data/INSTALL.rdoc +156 -0
  10. data/PACKAGING +36 -0
  11. data/README.md +88 -0
  12. data/RELEASE_CHECKLIST +18 -0
  13. data/Rakefile +264 -0
  14. data/TODO +24 -0
  15. data/alexandria-book-collection-manager.gemspec +35 -0
  16. data/alexandria.desktop.in +10 -0
  17. data/bin/alexandria +82 -0
  18. data/doc/AUTHORS +60 -0
  19. data/doc/BUGS +31 -0
  20. data/doc/FAQ +369 -0
  21. data/doc/HACKING +19 -0
  22. data/doc/NEWS +341 -0
  23. data/doc/alexandria.1 +120 -0
  24. data/doc/cuecat_support.rdoc +67 -0
  25. data/lib/alexandria.rb +85 -0
  26. data/lib/alexandria/about.rb +82 -0
  27. data/lib/alexandria/book_providers.rb +411 -0
  28. data/lib/alexandria/book_providers/adlibris.rb +235 -0
  29. data/lib/alexandria/book_providers/amazon_aws.rb +261 -0
  30. data/lib/alexandria/book_providers/amazon_ecs_util.rb +405 -0
  31. data/lib/alexandria/book_providers/barnes_and_noble.rb +229 -0
  32. data/lib/alexandria/book_providers/bol_it.rb +162 -0
  33. data/lib/alexandria/book_providers/deastore.rb +277 -0
  34. data/lib/alexandria/book_providers/douban.rb +135 -0
  35. data/lib/alexandria/book_providers/ibs_it.rb +149 -0
  36. data/lib/alexandria/book_providers/mcu.rb +177 -0
  37. data/lib/alexandria/book_providers/proxis.rb +205 -0
  38. data/lib/alexandria/book_providers/pseudomarc.rb +185 -0
  39. data/lib/alexandria/book_providers/renaud.rb +142 -0
  40. data/lib/alexandria/book_providers/siciliano.rb +271 -0
  41. data/lib/alexandria/book_providers/thalia.rb +197 -0
  42. data/lib/alexandria/book_providers/web.rb +59 -0
  43. data/lib/alexandria/book_providers/webster_it.rb +173 -0
  44. data/lib/alexandria/book_providers/worldcat.rb +251 -0
  45. data/lib/alexandria/book_providers/z3950.rb +422 -0
  46. data/lib/alexandria/config.rb +8 -0
  47. data/lib/alexandria/console.rb +31 -0
  48. data/lib/alexandria/execution_queue.rb +96 -0
  49. data/lib/alexandria/export_library.rb +536 -0
  50. data/lib/alexandria/import_library.rb +316 -0
  51. data/lib/alexandria/import_library_csv.rb +270 -0
  52. data/lib/alexandria/logging.rb +159 -0
  53. data/lib/alexandria/models/book.rb +72 -0
  54. data/lib/alexandria/models/library.rb +714 -0
  55. data/lib/alexandria/net.rb +53 -0
  56. data/lib/alexandria/preferences.rb +324 -0
  57. data/lib/alexandria/scanners.rb +42 -0
  58. data/lib/alexandria/scanners/cuecat.rb +118 -0
  59. data/lib/alexandria/scanners/keyboard.rb +57 -0
  60. data/lib/alexandria/smart_library.rb +525 -0
  61. data/lib/alexandria/ui.rb +53 -0
  62. data/lib/alexandria/ui/builder_base.rb +43 -0
  63. data/lib/alexandria/ui/callbacks.rb +389 -0
  64. data/lib/alexandria/ui/completion_models.rb +228 -0
  65. data/lib/alexandria/ui/dialogs/about_dialog.rb +59 -0
  66. data/lib/alexandria/ui/dialogs/acquire_dialog.rb +640 -0
  67. data/lib/alexandria/ui/dialogs/alert_dialog.rb +68 -0
  68. data/lib/alexandria/ui/dialogs/bad_isbns_dialog.rb +43 -0
  69. data/lib/alexandria/ui/dialogs/barcode_animation.rb +159 -0
  70. data/lib/alexandria/ui/dialogs/book_properties_dialog.rb +210 -0
  71. data/lib/alexandria/ui/dialogs/book_properties_dialog_base.rb +432 -0
  72. data/lib/alexandria/ui/dialogs/export_dialog.rb +172 -0
  73. data/lib/alexandria/ui/dialogs/import_dialog.rb +205 -0
  74. data/lib/alexandria/ui/dialogs/misc_dialogs.rb +85 -0
  75. data/lib/alexandria/ui/dialogs/new_book_dialog.rb +639 -0
  76. data/lib/alexandria/ui/dialogs/new_book_dialog_manual.rb +153 -0
  77. data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +67 -0
  78. data/lib/alexandria/ui/dialogs/preferences_dialog.rb +587 -0
  79. data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +56 -0
  80. data/lib/alexandria/ui/dialogs/smart_library_properties_dialog_base.rb +432 -0
  81. data/lib/alexandria/ui/dndable.rb +81 -0
  82. data/lib/alexandria/ui/gtk_thread_help.rb +88 -0
  83. data/lib/alexandria/ui/icons.rb +100 -0
  84. data/lib/alexandria/ui/iconview.rb +91 -0
  85. data/lib/alexandria/ui/iconview_tooltips.rb +162 -0
  86. data/lib/alexandria/ui/init.rb +93 -0
  87. data/lib/alexandria/ui/libraries_combo.rb +75 -0
  88. data/lib/alexandria/ui/listview.rb +310 -0
  89. data/lib/alexandria/ui/main_app.rb +67 -0
  90. data/lib/alexandria/ui/multi_drag_treeview.rb +150 -0
  91. data/lib/alexandria/ui/sidepane.rb +194 -0
  92. data/lib/alexandria/ui/sound.rb +107 -0
  93. data/lib/alexandria/ui/ui_manager.rb +1308 -0
  94. data/lib/alexandria/undo_manager.rb +78 -0
  95. data/lib/alexandria/utils.rb +30 -0
  96. data/lib/alexandria/version.rb +24 -0
  97. data/lib/alexandria/web_themes.rb +75 -0
  98. data/misc/sounds/README +15 -0
  99. data/misc/sounds/bad_scan.csd +62 -0
  100. data/misc/sounds/good_scan.csd +61 -0
  101. data/misc/sounds/scanning.csd +46 -0
  102. data/po/ChangeLog +488 -0
  103. data/po/Makefile +44 -0
  104. data/po/README +29 -0
  105. data/po/commit-po +72 -0
  106. data/po/cs.po +1437 -0
  107. data/po/cy.po +1521 -0
  108. data/po/de.po +1400 -0
  109. data/po/el.po +1379 -0
  110. data/po/es.po +1376 -0
  111. data/po/fr.po +1420 -0
  112. data/po/ga.po +1359 -0
  113. data/po/gl.po +1397 -0
  114. data/po/it.po +1406 -0
  115. data/po/ja.po +1355 -0
  116. data/po/mk.po +1373 -0
  117. data/po/nb.po +1386 -0
  118. data/po/nl.po +1405 -0
  119. data/po/pl.po +1373 -0
  120. data/po/pt.po +1398 -0
  121. data/po/pt_BR.po +1409 -0
  122. data/po/ru.po +1372 -0
  123. data/po/sk.po +1380 -0
  124. data/po/sv.po +1402 -0
  125. data/po/uk.po +1423 -0
  126. data/po/zh_TW.po +1394 -0
  127. data/schemas/alexandria.schemas +300 -0
  128. data/share/alexandria/glade/acquire_dialog__builder.glade +201 -0
  129. data/share/alexandria/glade/book_properties_dialog__builder.glade +910 -0
  130. data/share/alexandria/glade/main_app__builder.glade +229 -0
  131. data/share/alexandria/glade/new_book_dialog__builder.glade +379 -0
  132. data/share/alexandria/glade/preferences_dialog__builder.glade +733 -0
  133. data/share/alexandria/icons/alexandria.png +0 -0
  134. data/share/alexandria/icons/alexandria_small.png +0 -0
  135. data/share/alexandria/icons/book.png +0 -0
  136. data/share/alexandria/icons/book_icon.png +0 -0
  137. data/share/alexandria/icons/book_small.png +0 -0
  138. data/share/alexandria/icons/cuecat.png +0 -0
  139. data/share/alexandria/icons/cuecat_inactive.png +0 -0
  140. data/share/alexandria/icons/favorite_tag.png +0 -0
  141. data/share/alexandria/icons/less.png +0 -0
  142. data/share/alexandria/icons/library.png +0 -0
  143. data/share/alexandria/icons/library_small.png +0 -0
  144. data/share/alexandria/icons/lookup.png +0 -0
  145. data/share/alexandria/icons/more.png +0 -0
  146. data/share/alexandria/icons/no_cover.png +0 -0
  147. data/share/alexandria/icons/smart_library.png +0 -0
  148. data/share/alexandria/icons/smart_library_small.png +0 -0
  149. data/share/alexandria/icons/star_set.png +0 -0
  150. data/share/alexandria/icons/star_unset.png +0 -0
  151. data/share/alexandria/icons/view_as_icons.png +0 -0
  152. data/share/alexandria/icons/view_as_list.png +0 -0
  153. data/share/alexandria/ui/menus.xml +91 -0
  154. data/share/alexandria/ui/popups.xml +91 -0
  155. data/share/alexandria/web-themes/clean/clean.css +85 -0
  156. data/share/alexandria/web-themes/clean/preview.jpg +0 -0
  157. data/share/alexandria/web-themes/list/list.css +105 -0
  158. data/share/alexandria/web-themes/list/preview.jpg +0 -0
  159. data/share/app-icon/16x16/alexandria.png +0 -0
  160. data/share/app-icon/16x16/alexandria.svg +263 -0
  161. data/share/app-icon/22x22/alexandria.png +0 -0
  162. data/share/app-icon/22x22/alexandria.svg +465 -0
  163. data/share/app-icon/24x24/alexandria.png +0 -0
  164. data/share/app-icon/32x32/alexandria.png +0 -0
  165. data/share/app-icon/32x32/alexandria.svg +813 -0
  166. data/share/app-icon/32x32/alexandria.xpm +241 -0
  167. data/share/app-icon/48x48/alexandria.png +0 -0
  168. data/share/app-icon/scalable/alexandria.svg +700 -0
  169. data/share/gnome/help/alexandria/C/about.xml +44 -0
  170. data/share/gnome/help/alexandria/C/adding-books.xml +339 -0
  171. data/share/gnome/help/alexandria/C/alexandria.xml +185 -0
  172. data/share/gnome/help/alexandria/C/bugs.xml +18 -0
  173. data/share/gnome/help/alexandria/C/editing-book-properties.xml +124 -0
  174. data/share/gnome/help/alexandria/C/exporting.xml +81 -0
  175. data/share/gnome/help/alexandria/C/figures/adding_books_acquire_from_scanner_process.png +0 -0
  176. data/share/gnome/help/alexandria/C/figures/adding_books_add_by_isbn.png +0 -0
  177. data/share/gnome/help/alexandria/C/figures/adding_books_isbn_import.png +0 -0
  178. data/share/gnome/help/alexandria/C/figures/adding_books_manual_details.png +0 -0
  179. data/share/gnome/help/alexandria/C/figures/adding_books_rename_library_after_import.png +0 -0
  180. data/share/gnome/help/alexandria/C/figures/adding_books_search_results.png +0 -0
  181. data/share/gnome/help/alexandria/C/figures/editing_book_properties_info.png +0 -0
  182. data/share/gnome/help/alexandria/C/figures/editing_book_properties_loaning.png +0 -0
  183. data/share/gnome/help/alexandria/C/figures/exporting_information_html.png +0 -0
  184. data/share/gnome/help/alexandria/C/figures/getting_started_first_launched.png +0 -0
  185. data/share/gnome/help/alexandria/C/figures/searching_filtering_views_list_view.png +0 -0
  186. data/share/gnome/help/alexandria/C/figures/searching_filtering_views_list_view_search.png +0 -0
  187. data/share/gnome/help/alexandria/C/figures/settings_providers_new_z3950.png +0 -0
  188. data/share/gnome/help/alexandria/C/figures/smart_libraries_new_smart_library.png +0 -0
  189. data/share/gnome/help/alexandria/C/figures/working_with_libraries_library_pane.png +0 -0
  190. data/share/gnome/help/alexandria/C/getting-started.xml +154 -0
  191. data/share/gnome/help/alexandria/C/gnu-fdl-1.2.xml +543 -0
  192. data/share/gnome/help/alexandria/C/introduction.xml +142 -0
  193. data/share/gnome/help/alexandria/C/searching.xml +90 -0
  194. data/share/gnome/help/alexandria/C/settings.xml +140 -0
  195. data/share/gnome/help/alexandria/C/smart-libraries.xml +160 -0
  196. data/share/gnome/help/alexandria/C/working-with-libraries.xml +76 -0
  197. data/share/gnome/help/alexandria/ChangeLog +99 -0
  198. data/share/gnome/help/alexandria/fr/alexandria.xml +2292 -0
  199. data/share/gnome/help/alexandria/fr/figures/alexandria_add_button.png +0 -0
  200. data/share/gnome/help/alexandria/fr/figures/alexandria_add_by_isbn_1.png +0 -0
  201. data/share/gnome/help/alexandria/fr/figures/alexandria_add_by_search_1.png +0 -0
  202. data/share/gnome/help/alexandria/fr/figures/alexandria_add_manually.png +0 -0
  203. data/share/gnome/help/alexandria/fr/figures/alexandria_add_z3950.png +0 -0
  204. data/share/gnome/help/alexandria/fr/figures/alexandria_close_button.png +0 -0
  205. data/share/gnome/help/alexandria/fr/figures/alexandria_edit_info.png +0 -0
  206. data/share/gnome/help/alexandria/fr/figures/alexandria_export_web_page.png +0 -0
  207. data/share/gnome/help/alexandria/fr/figures/alexandria_importing.png +0 -0
  208. data/share/gnome/help/alexandria/fr/figures/alexandria_library_pane.png +0 -0
  209. data/share/gnome/help/alexandria/fr/figures/alexandria_list_view.png +0 -0
  210. data/share/gnome/help/alexandria/fr/figures/alexandria_list_view_search.png +0 -0
  211. data/share/gnome/help/alexandria/fr/figures/alexandria_loaning.png +0 -0
  212. data/share/gnome/help/alexandria/fr/figures/alexandria_main_window.png +0 -0
  213. data/share/gnome/help/alexandria/fr/figures/alexandria_remove_button.png +0 -0
  214. data/share/gnome/help/alexandria/ja/about.xml +33 -0
  215. data/share/gnome/help/alexandria/ja/adding-books.xml +314 -0
  216. data/share/gnome/help/alexandria/ja/alexandria.xml +172 -0
  217. data/share/gnome/help/alexandria/ja/bugs.xml +11 -0
  218. data/share/gnome/help/alexandria/ja/editing-book-properties.xml +100 -0
  219. data/share/gnome/help/alexandria/ja/exporting.xml +98 -0
  220. data/share/gnome/help/alexandria/ja/figures/adding_books_acquire_from_scanner_process.png +0 -0
  221. data/share/gnome/help/alexandria/ja/figures/adding_books_add_by_isbn.png +0 -0
  222. data/share/gnome/help/alexandria/ja/figures/adding_books_isbn_import.png +0 -0
  223. data/share/gnome/help/alexandria/ja/figures/adding_books_manual_details.png +0 -0
  224. data/share/gnome/help/alexandria/ja/figures/adding_books_rename_library_after_import.png +0 -0
  225. data/share/gnome/help/alexandria/ja/figures/adding_books_search_results.png +0 -0
  226. data/share/gnome/help/alexandria/ja/figures/editing_book_properties_info.png +0 -0
  227. data/share/gnome/help/alexandria/ja/figures/editing_book_properties_loaning.png +0 -0
  228. data/share/gnome/help/alexandria/ja/figures/exporting_information_html.png +0 -0
  229. data/share/gnome/help/alexandria/ja/figures/getting_started_first_launched.png +0 -0
  230. data/share/gnome/help/alexandria/ja/figures/searching_filtering_views_list_view.png +0 -0
  231. data/share/gnome/help/alexandria/ja/figures/searching_filtering_views_list_view_search.png +0 -0
  232. data/share/gnome/help/alexandria/ja/figures/settings_providers_new_z3950.png +0 -0
  233. data/share/gnome/help/alexandria/ja/figures/smart_libraries_new_smart_library.png +0 -0
  234. data/share/gnome/help/alexandria/ja/figures/working_with_libraries_library_pane.png +0 -0
  235. data/share/gnome/help/alexandria/ja/getting-started.xml +144 -0
  236. data/share/gnome/help/alexandria/ja/gnu-fdl-1.2.xml +541 -0
  237. data/share/gnome/help/alexandria/ja/introduction.xml +134 -0
  238. data/share/gnome/help/alexandria/ja/searching.xml +104 -0
  239. data/share/gnome/help/alexandria/ja/settings.xml +129 -0
  240. data/share/gnome/help/alexandria/ja/smart-libraries.xml +140 -0
  241. data/share/gnome/help/alexandria/ja/working-with-libraries.xml +88 -0
  242. data/share/menu/alexandria +7 -0
  243. data/share/omf/alexandria/alexandria-C.omf.in +27 -0
  244. data/share/omf/alexandria/alexandria-fr.omf.in +30 -0
  245. data/share/sounds/alexandria/bad_scan.ogg +0 -0
  246. data/share/sounds/alexandria/bad_scan.wav +0 -0
  247. data/share/sounds/alexandria/good_scan.ogg +0 -0
  248. data/share/sounds/alexandria/good_scan.wav +0 -0
  249. data/share/sounds/alexandria/scanning.ogg +0 -0
  250. data/share/sounds/alexandria/scanning.wav +0 -0
  251. data/spec/alexandria/library_spec.rb +205 -0
  252. data/spec/alexandria/preferences_spec.rb +22 -0
  253. data/spec/alexandria/scanners/cuecat_spec.rb +68 -0
  254. data/spec/alexandria/smart_library_spec.rb +22 -0
  255. data/spec/alexandria/ui/dialogs_spec.rb +90 -0
  256. data/spec/alexandria/ui/iconview_spec.rb +27 -0
  257. data/spec/alexandria/ui/listview_spec.rb +28 -0
  258. data/spec/alexandria/ui/main_app_spec.rb +48 -0
  259. data/spec/alexandria/ui/sidepane_spec.rb +27 -0
  260. data/spec/alexandria/ui/ui_manager_spec.rb +26 -0
  261. data/spec/alexandria/ui/ui_utilities_spec.rb +60 -0
  262. data/spec/alexandria/utilities_spec.rb +50 -0
  263. data/spec/data/libraries/0.6.1-noisbn/My Library/0201398257.yaml +10 -0
  264. data/spec/data/libraries/0.6.1-noisbn/My Library/1565920007.yaml +10 -0
  265. data/spec/data/libraries/0.6.1/My Library/0192812173.yaml +9 -0
  266. data/spec/data/libraries/0.6.1/My Library/0201398257.cover +0 -0
  267. data/spec/data/libraries/0.6.1/My Library/0201398257.yaml +10 -0
  268. data/spec/data/libraries/0.6.1/My Library/1565920007.yaml +10 -0
  269. data/spec/data/libraries/0.6.2/My Library/9780140266146.cover +0 -0
  270. data/spec/data/libraries/0.6.2/My Library/9780140266146.yaml +16 -0
  271. data/spec/data/libraries/0.6.2/My Library/9780140278781.cover +0 -0
  272. data/spec/data/libraries/0.6.2/My Library/9780140278781.yaml +21 -0
  273. data/spec/data/libraries/0.6.2/My Library/9780571147168.cover +0 -0
  274. data/spec/data/libraries/0.6.2/My Library/9780571147168.yaml +20 -0
  275. data/spec/data/libraries/0.6.2/My Library/9780575079038.cover +0 -0
  276. data/spec/data/libraries/0.6.2/My Library/9780575079038.yaml +20 -0
  277. data/spec/data/libraries/0.6.2/My Library/9780755322800.cover +0 -0
  278. data/spec/data/libraries/0.6.2/My Library/9780755322800.yaml +20 -0
  279. data/spec/spec_helper.rb +46 -0
  280. data/tasks/rdoc.rake +6 -0
  281. data/tasks/setup.rb +30 -0
  282. data/tasks/spec.rake +29 -0
  283. data/tasks/test.rake +38 -0
  284. data/test/application_test.rb +39 -0
  285. data/test/book_test.rb +34 -0
  286. data/test/data/isbns.txt +3 -0
  287. data/test/isbn_test.rb +68 -0
  288. data/test/providers_test.rb +254 -0
  289. data/test/test_helper.rb +42 -0
  290. data/util/rake/fileinstall.rb +313 -0
  291. data/util/rake/gettextgenerate.rb +158 -0
  292. data/util/rake/omfgenerate.rb +79 -0
  293. metadata +452 -0
@@ -0,0 +1,57 @@
1
+ # Copyright (C) 2005-2006 Christopher Cyll
2
+ # Copyright (C) 2008 Cathal Mc Ginley
3
+ #
4
+ # Alexandria is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU General Public License as
6
+ # published by the Free Software Foundation; either version 2 of the
7
+ # License, or (at your option) any later version.
8
+ #
9
+ # Alexandria is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public
15
+ # License along with Alexandria; see the file COPYING. If not,
16
+ # write to the Free Software Foundation, Inc., 51 Franklin Street,
17
+ # Fifth Floor, Boston, MA 02110-1301 USA.
18
+
19
+ require 'alexandria/scanners'
20
+
21
+ module Alexandria
22
+ module Scanners
23
+ # A simple keyboard-wedge style barcode scanner which presents
24
+ # scan data as if typed from a keyboard. (Modified CueCats act
25
+ # like this.)
26
+ class KeyboardWedge
27
+ def name
28
+ 'KeyboardWedge'
29
+ end
30
+
31
+ def display_name
32
+ 'Keyboard Wedge'
33
+ end
34
+
35
+ # Checks if data looks like a completed scan
36
+ def match?(data)
37
+ data.gsub!(/\s/, '')
38
+ (data =~ /[0-9]{12,18}/) || (data =~ /[0-9]{9}[0-9Xx]/)
39
+ end
40
+
41
+ # Gets the essential 13-digits from an ISBN barcode (EAN-13)
42
+ def decode(data)
43
+ data.gsub!(/\s/, '')
44
+ if data.length == 10
45
+ return data
46
+ elsif data.length >= 13
47
+ return data[0, 13]
48
+ else
49
+ raise "Unknown scan data #{data}"
50
+ end
51
+ end
52
+ end
53
+
54
+ # Register the wedge scanner with the Scanner Registry
55
+ register KeyboardWedge.new
56
+ end
57
+ end
@@ -0,0 +1,525 @@
1
+ # Copyright (C) 2004-2006 Laurent Sansonetti
2
+ # Copyright (C) 2011, 2014 Matijs van Zuijlen
3
+ #
4
+ # Alexandria is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU General Public License as
6
+ # published by the Free Software Foundation; either version 2 of the
7
+ # License, or (at your option) any later version.
8
+ #
9
+ # Alexandria is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public
15
+ # License along with Alexandria; see the file COPYING. If not,
16
+ # write to the Free Software Foundation, Inc., 51 Franklin Street,
17
+ # Fifth Floor, Boston, MA 02110-1301 USA.
18
+
19
+ # require 'date'
20
+ require 'time'
21
+
22
+ module Alexandria
23
+ class SmartLibrary < Array
24
+ include Logging
25
+ include GetText
26
+ extend GetText
27
+ bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
28
+
29
+ ALL_RULES, ANY_RULE = 1, 2
30
+ attr_reader :name
31
+ attr_accessor :rules, :predicate_operator_rule, :deleted_books
32
+
33
+ DIR = File.join(ENV['HOME'], '.alexandria', '.smart_libraries')
34
+ EXT = '.yaml'
35
+
36
+ def initialize(name, rules, predicate_operator_rule)
37
+ super()
38
+ raise if name.nil? or rules.nil? or predicate_operator_rule.nil?
39
+ @name = name
40
+ @rules = rules
41
+ @predicate_operator_rule = predicate_operator_rule
42
+ save
43
+ libraries = Libraries.instance
44
+ libraries.add_observer(self)
45
+ self.libraries = libraries.all_regular_libraries
46
+ # carry deleted books over from libraries that are part of the smart library
47
+ self.deleted_books = libraries.deleted_books
48
+ @cache = {}
49
+ end
50
+
51
+ def self.loadall
52
+ a = []
53
+ begin
54
+ # Deserialize smart libraries.
55
+ Dir.chdir(DIR) do
56
+ Dir['*' + EXT].each do |filename|
57
+ # Skip non-regular files.
58
+ next unless File.stat(filename).file?
59
+
60
+ text = IO.read(filename)
61
+ hash = YAML.load(text)
62
+ begin
63
+ smart_library = from_hash(hash)
64
+ a << smart_library
65
+ rescue => e
66
+ puts "Cannot load serialized smart library: #{e}"
67
+ puts e.backtrace
68
+ end
69
+ end
70
+ end
71
+
72
+ rescue Errno::ENOENT
73
+ # First run and no smart libraries yet? Provide some default
74
+ # ones.
75
+ sample_smart_libraries.each do |smart_library|
76
+ smart_library.save
77
+ a << smart_library
78
+ end
79
+ end
80
+ a.each(&:refilter)
81
+ a
82
+ end
83
+
84
+ def self.sample_smart_libraries
85
+ a = []
86
+
87
+ operands = Rule::Operands::LEFT
88
+
89
+ # Favorite books.
90
+ rule = Rule.new(operands.find { |x| x.book_selector == :rating },
91
+ Rule::Operators::IS,
92
+ UI::MainApp::MAX_RATING_STARS.to_s)
93
+ a << new(_('Favorite'), [rule], ALL_RULES)
94
+
95
+ # Loaned books.
96
+ rule = Rule.new(operands.find { |x| x.book_selector == :loaned },
97
+ Rule::Operators::IS_TRUE,
98
+ nil)
99
+ a << new(_('Loaned'), [rule], ALL_RULES)
100
+
101
+ # Redd books.
102
+ rule = Rule.new(operands.find { |x| x.book_selector == :redd },
103
+ Rule::Operators::IS_TRUE,
104
+ nil)
105
+ a << new(_('Read'), [rule], ALL_RULES)
106
+
107
+ # Own books.
108
+ rule = Rule.new(operands.find { |x| x.book_selector == :own },
109
+ Rule::Operators::IS_TRUE,
110
+ nil)
111
+ a << new(_('Owned'), [rule], ALL_RULES)
112
+
113
+ # Want books.
114
+ rule = Rule.new(operands.find { |x| x.book_selector == :want },
115
+ Rule::Operators::IS_TRUE,
116
+ nil)
117
+ rule2 = Rule.new(operands.find { |x| x.book_selector == :own },
118
+ Rule::Operators::IS_NOT_TRUE,
119
+ nil)
120
+ a << new(_('Wishlist'), [rule, rule2], ALL_RULES)
121
+
122
+ a
123
+ end
124
+
125
+ def self.from_hash(hash)
126
+ SmartLibrary.new(hash[:name],
127
+ hash[:rules].map { |x| Rule.from_hash(x) },
128
+ hash[:predicate_operator_rule] == :all ? ALL_RULES : ANY_RULE)
129
+ end
130
+
131
+ def to_hash
132
+ {
133
+ name: @name,
134
+ predicate_operator_rule: @predicate_operator_rule == ALL_RULES ? :all : :any,
135
+ rules: @rules.map(&:to_hash)
136
+ }
137
+ end
138
+
139
+ def name=(new_name)
140
+ if @name != new_name
141
+ old_yaml = yaml
142
+ @name = new_name
143
+ FileUtils.mv(old_yaml, yaml)
144
+ save
145
+ end
146
+ end
147
+
148
+ def update(*params)
149
+ if params.first.is_a?(Libraries)
150
+ libraries, _, library = params
151
+ unless library.is_a?(self.class)
152
+ self.libraries = libraries.all_libraries
153
+ refilter
154
+ end
155
+ elsif params.first.is_a?(Library)
156
+ refilter
157
+ end
158
+ end
159
+
160
+ def refilter
161
+ raise 'need libraries' if @libraries.nil?
162
+ raise 'no libraries' if @libraries.empty?
163
+ raise 'need predicate operator' if @predicate_operator_rule.nil?
164
+ raise 'need rule' if @rules.nil? or @rules.empty?
165
+
166
+ filters = @rules.map(&:filter_proc)
167
+ selector = @predicate_operator_rule == ALL_RULES ? :all? : :any?
168
+
169
+ clear
170
+ @cache.clear
171
+
172
+ @libraries.each do |library|
173
+ filtered_library = library.select do |book|
174
+ filters.send(selector) { |filter| filter.call(book) } # Problem here.
175
+ end
176
+ filtered_library.each { |x| @cache[x] = library }
177
+ concat(filtered_library)
178
+ end
179
+ @n_rated = select { |x| !x.rating.nil? and x.rating > 0 }.length
180
+ end
181
+
182
+ def cover(book)
183
+ @cache[book].cover(book)
184
+ end
185
+
186
+ def yaml(book = nil)
187
+ if book
188
+ @cache[book].yaml(book)
189
+ else
190
+ File.join(DIR, @name + EXT)
191
+ end
192
+ end
193
+
194
+ def save(book = nil)
195
+ if book
196
+ @cache[book].save(book)
197
+ else
198
+ FileUtils.mkdir_p(DIR)
199
+ File.open(yaml, 'w') { |io| io.puts to_hash.to_yaml }
200
+ end
201
+ end
202
+
203
+ def save_cover(book, _cover_uri)
204
+ @cache[book].save_cover(book)
205
+ end
206
+
207
+ def final_cover(book)
208
+ @cache[book].final_cover(book)
209
+ end
210
+
211
+ def copy_covers(somewhere)
212
+ FileUtils.rm_rf(somewhere) if File.exist?(somewhere)
213
+ FileUtils.mkdir(somewhere)
214
+ each do |book|
215
+ library = @cache[book]
216
+ next unless File.exist?(library.cover(book))
217
+ FileUtils.cp(File.join(library.path, book.ident + Library::EXT[:cover]),
218
+ File.join(somewhere, library.final_cover(book)))
219
+ end
220
+ end
221
+
222
+ attr_reader :n_rated
223
+
224
+ def n_unrated
225
+ length - n_rated
226
+ end
227
+
228
+ def ==(object)
229
+ object.is_a?(self.class) && object.name == name
230
+ end
231
+
232
+ @@deleted_libraries = []
233
+
234
+ def self.deleted_libraries
235
+ @@deleted_libraries
236
+ end
237
+
238
+ def self.really_delete_deleted_libraries
239
+ @@deleted_libraries.each do |library|
240
+ puts "Deleting smart library file (#{yaml})" if $DEBUG
241
+ FileUtils.rm_rf(library.yaml)
242
+ end
243
+ end
244
+
245
+ def delete
246
+ if @@deleted_libraries.include?(self)
247
+ puts 'Already deleted a SmartLibrary with this name'
248
+ puts '(this might mess up undeletes...)'
249
+ FileUtils.rm_rf(yaml)
250
+ # so we just delete the old smart library, and
251
+ # 'pending' delete the new one of the same name...
252
+ # urrr... yeah, that'll work!
253
+ end
254
+ @@deleted_libraries << self
255
+ end
256
+
257
+ def deleted?
258
+ @@deleted_libraries.include?(self)
259
+ end
260
+
261
+ def undelete
262
+ raise unless @@deleted_libraries.include?(self)
263
+ @@deleted_libraries.delete(self)
264
+ end
265
+
266
+ private
267
+
268
+ def libraries=(ary)
269
+ @libraries ||= []
270
+ @libraries.each { |x| x.delete_observer(self) }
271
+ @libraries = ary.select { |x| x.is_a?(Library) }
272
+ @libraries.each { |x| x.add_observer(self) }
273
+ end
274
+
275
+ class Rule
276
+ include GetText
277
+ extend GetText
278
+ bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
279
+
280
+ attr_accessor :operand, :operation, :value
281
+
282
+ def initialize(operand, operation, value)
283
+ raise if operand.nil? or operation.nil? # value can be nil
284
+ @operand = operand
285
+ @operation = operation
286
+ @value = value
287
+ end
288
+
289
+ def self.from_hash(hash)
290
+ operand = Operands::LEFT.find do |x|
291
+ x.book_selector == hash[:operand]
292
+ end
293
+ operator = Operators::ALL.find do |x|
294
+ x.sym == hash[:operation]
295
+ end
296
+ Rule.new(operand, operator, hash[:value])
297
+ end
298
+
299
+ def to_hash
300
+ {
301
+ operand: @operand.book_selector,
302
+ operation: @operation.sym,
303
+ value: @value
304
+ }
305
+ end
306
+
307
+ class Operand < Struct.new(:name, :klass)
308
+ def <=>(x)
309
+ name <=> x.name
310
+ end
311
+ end
312
+
313
+ class LeftOperand < Operand
314
+ attr_accessor :book_selector
315
+
316
+ def initialize(book_selector, *args)
317
+ super(*args)
318
+ @book_selector = book_selector
319
+ end
320
+ end
321
+
322
+ class Operator < Struct.new(:sym, :name, :proc)
323
+ def <=>(x)
324
+ name <=> x.name
325
+ end
326
+ end
327
+
328
+ module Operands
329
+ include GetText
330
+ extend GetText
331
+ bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
332
+
333
+ LEFT = [
334
+ LeftOperand.new(:title, _('Title'), String),
335
+ LeftOperand.new(:isbn, _('ISBN'), String),
336
+ LeftOperand.new(:authors, _('Authors'), String),
337
+ LeftOperand.new(:publisher, _('Publisher'), String),
338
+ LeftOperand.new(:publishing_year, _('Publish Year'), Integer),
339
+ LeftOperand.new(:edition, _('Binding'), String),
340
+ LeftOperand.new(:rating, _('Rating'), Integer),
341
+ LeftOperand.new(:notes, _('Notes'), String),
342
+ LeftOperand.new(:tags, _('Tags'), Array),
343
+ LeftOperand.new(:loaned, _('Loaning State'), TrueClass),
344
+ LeftOperand.new(:loaned_since, _('Loaning Date'), Time),
345
+ LeftOperand.new(:loaned_to, _('Loaning Person'), String),
346
+ LeftOperand.new(:redd, _('Read'), TrueClass),
347
+ LeftOperand.new(:redd_when, _('Date Read'), Time),
348
+ LeftOperand.new(:own, _('Own'), TrueClass),
349
+ LeftOperand.new(:want, _('Want'), TrueClass),
350
+ ].sort
351
+
352
+ STRING = Operand.new(nil, String)
353
+ STRING_ARRAY = Operand.new(nil, String)
354
+ INTEGER = Operand.new(nil, Integer)
355
+ TIME = Operand.new(nil, Time)
356
+ DAYS = Operand.new(_('days'), Integer)
357
+ end
358
+
359
+ module Operators
360
+ include Logging
361
+ include GetText
362
+ extend GetText
363
+ bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
364
+
365
+ IS_TRUE = Operator.new(:is_true,
366
+ _('is set'),
367
+ proc { |x| x })
368
+ IS_NOT_TRUE = Operator.new(:is_not_true,
369
+ _('is not set'),
370
+ proc { |x| !x })
371
+ IS = Operator.new(:is,
372
+ _('is'),
373
+ proc { |x, y| x == y })
374
+ IS_NOT = Operator.new(:is_not,
375
+ _('is not'),
376
+ proc { |x, y| x != y })
377
+ CONTAINS = Operator.new(:contains,
378
+ _('contains'),
379
+ proc { |x, y| x.include?(y) })
380
+ DOES_NOT_CONTAIN = Operator.new(:does_not_contain,
381
+ _('does not contain'),
382
+ proc { |x, y| !x.include?(y) })
383
+ STARTS_WITH = Operator.new(:starts_with,
384
+ _('starts with'),
385
+ proc { |x, y| /^#{y}/.match(x) })
386
+ ENDS_WITH = Operator.new(:ends_with,
387
+ _('ends with'),
388
+ proc { |x, y| /#{y}$/.match(x) })
389
+ IS_GREATER_THAN = Operator.new(:is_greater_than,
390
+ _('is greater than'),
391
+ proc { |x, y| x > y })
392
+ IS_LESS_THAN = Operator.new(:is_less_than,
393
+ _('is less than'),
394
+ proc { |x, y| x < y })
395
+ IS_AFTER = Operator.new(:is_after,
396
+ _('is after'),
397
+ proc { |x, y| x.to_i > y.to_i and !x.nil? })
398
+ IS_BEFORE = Operator.new(:is_before,
399
+ _('is before'),
400
+ proc { |x, y| x.to_i < y.to_i and !x.nil? })
401
+ IS_IN_LAST = Operator.new(:is_in_last_days,
402
+ _('is in last'),
403
+ proc { |x, y|
404
+ begin
405
+ if x.nil? or x.empty?
406
+ false
407
+ else
408
+ log.debug { "Given Date: #{x.inspect} #{x.class}" }
409
+ given_date = Time.parse(x)
410
+ days = y.to_i * (24 * 60 * 60)
411
+
412
+ Time.now - given_date <= days
413
+ end
414
+ rescue => ex
415
+ trace = ex.backtrace.join("\n >")
416
+ log.warn { "Date matching failed #{ex} #{trace}" }
417
+ false
418
+ end
419
+ })
420
+ IS_NOT_IN_LAST = Operator.new(:is_not_in_last_days,
421
+ _('is not in last'),
422
+ proc { |x, y|
423
+ begin
424
+ if x.nil? or x.empty?
425
+ false
426
+ else
427
+ log.debug { "Given Date: #{x.inspect} #{x.class}" }
428
+ given_date = Time.parse(x)
429
+ days = y.to_i * (24 * 60 * 60)
430
+
431
+ Time.now - given_date > days
432
+ end
433
+ rescue => ex
434
+ trace = ex.backtrace.join("\n >")
435
+ log.warn { "Date matching failed #{ex} #{trace}" }
436
+ false
437
+ end
438
+ # Time.now - x > 3600*24*y
439
+ })
440
+
441
+ ALL = constants.map \
442
+ { |x| module_eval(x.to_s) }.select \
443
+ { |x| x.is_a?(Operator) }
444
+ end
445
+
446
+ BOOLEAN_OPERATORS = [
447
+ Operators::IS_TRUE,
448
+ Operators::IS_NOT_TRUE
449
+ ].sort
450
+
451
+ STRING_OPERATORS = [
452
+ Operators::IS,
453
+ Operators::IS_NOT,
454
+ Operators::CONTAINS,
455
+ Operators::DOES_NOT_CONTAIN,
456
+ Operators::STARTS_WITH,
457
+ Operators::ENDS_WITH
458
+ ].sort
459
+
460
+ STRING_ARRAY_OPERATORS = [
461
+ Operators::CONTAINS,
462
+ Operators::DOES_NOT_CONTAIN
463
+ ].sort
464
+
465
+ INTEGER_OPERATORS = [
466
+ Operators::IS,
467
+ Operators::IS_NOT,
468
+ Operators::IS_GREATER_THAN,
469
+ Operators::IS_LESS_THAN
470
+ ].sort
471
+
472
+ TIME_OPERATORS = [
473
+ Operators::IS,
474
+ Operators::IS_NOT,
475
+ Operators::IS_AFTER,
476
+ Operators::IS_BEFORE,
477
+ Operators::IS_IN_LAST,
478
+ Operators::IS_NOT_IN_LAST
479
+ ].sort
480
+
481
+ def self.operations_for_operand(operand)
482
+ case operand.klass.name
483
+ when 'String'
484
+ STRING_OPERATORS.map { |x| [x, Operands::STRING] }
485
+ when 'Array'
486
+ STRING_ARRAY_OPERATORS.map { |x| [x, Operands::STRING] }
487
+ when 'Integer'
488
+ INTEGER_OPERATORS.map { |x| [x, Operands::INTEGER] }
489
+ when 'TrueClass'
490
+ BOOLEAN_OPERATORS.map { |x| [x, nil] }
491
+ when 'Time'
492
+ TIME_OPERATORS.map do |x|
493
+ if x == Operators::IS_IN_LAST or
494
+ x == Operators::IS_NOT_IN_LAST
495
+
496
+ [x, Operands::DAYS]
497
+ else
498
+ [x, Operands::TIME]
499
+ end
500
+ end
501
+ else
502
+ raise "invalid operand klass #{operand.klass}"
503
+ end
504
+ end
505
+
506
+ def filter_proc
507
+ proc do |book|
508
+ begin
509
+ left_value = book.send(@operand.book_selector)
510
+ rescue => e
511
+ puts e.message
512
+ end
513
+ right_value = @value
514
+ if right_value.is_a?(String)
515
+ left_value = left_value.to_s.downcase
516
+ right_value = right_value.downcase
517
+ end
518
+ params = [left_value]
519
+ params << right_value unless right_value.nil?
520
+ @operation.proc.call(*params)
521
+ end
522
+ end
523
+ end
524
+ end
525
+ end