Class: Alexandria::UI::AcquireDialog

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

Instance Method Summary collapse

Methods included from Logging

included, #log

Constructor Details

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

Returns a new instance of AcquireDialog.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 62

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

  libraries = Libraries.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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 89

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

#notify_end_add_by_isbnObject



291
292
293
294
295
296
297
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 291

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



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 274

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



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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 105

def on_add
  model = @barcodes_treeview.model

  libraries = Libraries.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(@parent, title, message)
  end

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

#on_cancelObject



232
233
234
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 232

def on_cancel
  @acquire_dialog.destroy
end

#on_helpObject



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

def on_help; end

#read_barcode_scanObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 238

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 => err
    log.error { "Bad scan:  #{@scanner_buffer} #{err}" }
  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

#update(status, provider) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 299

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



82
83
84
85
86
87
# File 'lib/alexandria/ui/dialogs/acquire_dialog.rb', line 82

def widget_names
  [:acquire_dialog, :dialog_vbox1, :dialog_action_area1,
   :help_button, :cancel_button, :add_button, :vbox1,
   :barcode_label, :scan_area, :scan_frame, :scrolledwindow1,
   :barcodes_treeview, :hbox1, :label1, :combo_libraries]
end