Class: Alexandria::UI::AcquireDialog

Inherits:
BuilderBase show all
Extended by:
GetText
Includes:
ImageFetcher, Logging, GetText
Defined in:
lib/alexandria/ui/acquire_dialog.rb

Instance Method Summary collapse

Methods included from Logging

included, #log

Methods included from ImageFetcher

#fetch_image

Constructor Details

#initialize(parent, selected_library = nil, &block) ⇒ AcquireDialog

Returns a new instance of AcquireDialog.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/alexandria/ui/acquire_dialog.rb', line 51

def initialize(parent, selected_library = nil, &block)
  super("acquire_dialog__builder.glade", widget_names)
  @acquire_dialog.transient_for = @parent = parent
  @block = block

  libraries = LibraryCollection.instance.all_regular_libraries
  selected_library = libraries.first if selected_library.is_a?(SmartLibrary)
  @combo_libraries.populate_with_libraries(libraries,
                                           selected_library)

  @add_button.sensitive = false
  @prefs = Alexandria::Preferences.instance
  setup_scanner_area
  init_treeview
  @book_results = {}

  @search_thread_counter = SearchThreadCounter.new
  @search_threads_running = @search_thread_counter.new_cond
end

Instance Method Details

#book_in_library(isbn10, library) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/alexandria/ui/acquire_dialog.rb', line 80

def book_in_library(isbn10, library)
  isbn13 = Library.canonicalise_ean(isbn10)
  match = library.find do |book|
    (book.isbn == isbn10 || book.isbn == isbn13)
  end
  !match.nil?
rescue StandardError
  log.warn { "Failed to check for book #{isbn10} in library #{library}" }
  true
end

#notify_end_add_by_isbnObject



277
278
279
280
281
282
283
# File 'lib/alexandria/ui/acquire_dialog.rb', line 277

def notify_end_add_by_isbn
  GLib::Idle.add do
    MainApp.instance.appbar.children.first.visible = false
    GLib::Source.remove(@progress_pulsing) if @progress_pulsing
    false
  end
end

#notify_start_add_by_isbnObject

begin copy-n-paste from new_book_dialog



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/alexandria/ui/acquire_dialog.rb', line 260

def notify_start_add_by_isbn
  GLib::Idle.add do
    main_progress_bar = MainApp.instance.appbar.children.first
    main_progress_bar.visible = true
    @progress_pulsing = GLib::Timeout.add(100) do
      if @destroyed
        @progress_pulsing = nil
        false
      else
        main_progress_bar.pulse
        true
      end
    end
    false
  end
end

#on_addObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/alexandria/ui/acquire_dialog.rb', line 91

def on_add
  model = @barcodes_treeview.model

  libraries = LibraryCollection.instance.all_libraries
  library, is_new_library =
    @combo_libraries.selection_from_libraries(libraries)

  # NOTE: at this stage, the ISBN is 10-digit...
  #

  selection = @barcodes_treeview.selection
  isbns = []
  isbn_duplicates = []

  adding_a_selection = false

  if selection.count_selected_rows > 0

    adding_a_selection = true

    model.freeze_notify do
      # capture isbns
      selection.selected_each do |_mod, _path, iter|
        isbn = iter[0]
        if book_in_library(isbn, library)
          isbn_duplicates << isbn
        elsif isbns.include? isbn
          log.info { "detected duplicate in scanned list #{isbn}" }
          isbn_duplicates << isbn
        else
          isbns << isbn
        end
      end

      # try it this way... works because of persistent iters
      row_iters = []
      selection.selected_rows.each do |path|
        iter = model.get_iter(path)
        isbn = iter[0]
        if book_in_library(isbn, library)
          log.debug { "#{isbn} is a duplicate" }
        elsif !@book_results.key?(isbn) # HAX
          log.debug { "no book found for #{isbn}, not adding" }

          # good enough for now
        else
          log.debug { "scheduling #{isbn} for removal from list" }
          row_iters << iter
        end
      end
      row_iters.each do |iter|
        log.debug { "removing iter #{iter[0]}" }
        model.remove(iter)
      end
    end
  else
    model.freeze_notify do
      # capture isbns
      row_iters = []
      model.each do |_mod, _path, iter|
        isbn = iter[0]
        if !@book_results.key?(isbn)
          log.debug { "no book found for #{isbn}, not adding" }
          adding_a_selection = true
        elsif book_in_library(isbn, library)
          log.info { "#{isbn} is a duplicate" }
          isbn_duplicates << isbn
        elsif isbns.include? isbn
          log.info { "detected duplicate in scanned list #{isbn}" }
          isbn_duplicates << isbn
        else
          log.info { "scheduling #{isbn} for removal from list" }
          isbns << isbn
          row_iters << iter
        end
      end
      # remove list items
      if isbn_duplicates.empty? && !adding_a_selection
        model.clear # TODO: unless!!!
        row_iters.clear
      else
        row_iters.each do |iter|
          log.debug { "removing iter #{iter[0]}" }
          model.remove(iter)
        end
      end
    end
  end

  books = []

  isbns.each do |isbn|
    log.debug { "Adding #{isbn}" }
    result = @book_results[isbn]

    if result.nil?
      # used to crash if book was not found in online lookup!
      adding_a_selection = true
      # ISBN not found, so keep it in the list
      # TODO (for 0.6.5) should offer to add this book manually
    else

      book = result[0]
      cover_uri = result[1]

      library.save_cover(book, cover_uri) unless cover_uri.nil?
      books << book
      library << book
      library.save(book)
    end
  end

  if isbn_duplicates.empty?
    @acquire_dialog.destroy unless adding_a_selection
  else
    message = n_("There was %d duplicate",
                 "There were %d duplicates",
                 isbn_duplicates.size) % isbn_duplicates.size
    title = n_("Couldn't add this book",
               "Couldn't add these books",
               isbn_duplicates.size)
    ErrorDialog.new(@acquire_dialog, title, message).display
  end

  @block.call(books, library, is_new_library)
end

#on_cancelObject



218
219
220
# File 'lib/alexandria/ui/acquire_dialog.rb', line 218

def on_cancel
  @acquire_dialog.destroy
end

#on_helpObject



222
# File 'lib/alexandria/ui/acquire_dialog.rb', line 222

def on_help; end

#read_barcode_scanObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/alexandria/ui/acquire_dialog.rb', line 224

def read_barcode_scan
  @animation.start
  play_sound("scanning") if @test_scan
  log.debug { "reading scanner data #{@scanner_buffer}" }
  barcode_text = nil
  isbn = nil
  begin
    barcode_text = @scanner.decode(@scanner_buffer)
    log.debug { "got barcode text #{barcode_text}" }
    isbn = Library.canonicalise_isbn(barcode_text)
    # TODO: : use an AppFacade
    # isbn =  LookupBook.get_isbn(barcode_text)
  rescue StandardError => ex
    log.error { "Bad scan:  #{@scanner_buffer} #{ex}" }
  ensure
    @scanner_buffer = ""
  end
  if isbn
    log.debug { "Got ISBN #{isbn}" }
    play_sound("good_scan")

    @barcodes_treeview.model.freeze_notify do
      iter = @barcodes_treeview.model.append
      iter[0] = isbn
      iter[1] = Icons::BOOK
      iter[2] = ""
    end
    lookup_book(isbn)
  else
    log.debug { "was not an ISBN barcode" }
    play_sound("bad_scan")
  end
end

#showObject



71
72
73
# File 'lib/alexandria/ui/acquire_dialog.rb', line 71

def show
  @acquire_dialog.show
end

#update(status, provider) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/alexandria/ui/acquire_dialog.rb', line 285

def update(status, provider)
  GLib::Idle.add do
    messages = {
      searching: _("Searching Provider '%s'..."),
      error: _("Error while Searching Provider '%s'"),
      not_found: _("Not Found at Provider '%s'"),
      found: _("Found at Provider '%s'")
    }
    message = messages[status] % provider
    log.debug { "update message : #{message}" }
    MainApp.instance.ui_manager.set_status_label(message)
    false
  end
end

#widget_namesObject



75
76
77
78
# File 'lib/alexandria/ui/acquire_dialog.rb', line 75

def widget_names
  [:acquire_dialog, :add_button, :barcodes_treeview, :barcode_label,
   :scan_area, :scan_frame, :combo_libraries]
end