Class: Alexandria::UI::NewBookDialog
- Inherits:
-
BuilderBase
- Object
- BuilderBase
- Alexandria::UI::NewBookDialog
- Extended by:
- GetText
- Includes:
- Logging, GetText
- Defined in:
- lib/alexandria/ui/dialogs/new_book_dialog.rb
Constant Summary collapse
- @@last_criterion_was_not_isbn =
false
Instance Method Summary collapse
- #add_book_to_library(library, book, cover_uri) ⇒ Object
- #add_selected_books(library, _is_new) ⇒ Object
- #add_single_book_by_isbn(library, is_new) ⇒ Object
-
#decode_cuecat?(entry) ⇒ Boolean
srsly?.
- #get_images_async ⇒ Object
- #image_error_dialog(error) ⇒ Object
-
#initialize(parent, selected_library = nil, &block) ⇒ NewBookDialog
constructor
A new instance of NewBookDialog.
- #notify_end_add_by_isbn ⇒ Object
- #notify_start_add_by_isbn ⇒ Object
- #on_add ⇒ Object
- #on_cancel ⇒ Object
- #on_changed(entry) ⇒ Object
- #on_clicked(widget, event) ⇒ Object
- #on_criterion_toggled(item) ⇒ Object
- #on_find ⇒ Object
- #on_focus ⇒ Object
- #on_help ⇒ Object
- #on_results_button_press_event(_widget, event) ⇒ Object
- #post_addition(books, library, is_new_library) ⇒ Object
- #setup_dialog_gui ⇒ Object
- #update(status, provider) ⇒ Object
- #widget_names ⇒ Object
Methods included from Logging
Constructor Details
#initialize(parent, selected_library = nil, &block) ⇒ NewBookDialog
Returns a new instance of NewBookDialog.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 57 def initialize(parent, selected_library = nil, &block) super('new_book_dialog__builder.glade', ) log.info { 'New Book Dialog' } @new_book_dialog.transient_for = @parent = parent @block = block @destroyed = false @selected_library = selected_library setup_dialog_gui @progressbar.hide end |
Instance Method Details
#add_book_to_library(library, book, cover_uri) ⇒ Object
452 453 454 455 456 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 452 def add_book_to_library(library, book, cover_uri) library.save_cover(book, cover_uri) unless cover_uri.nil? library << book library.save(book) end |
#add_selected_books(library, _is_new) ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 425 def add_selected_books(library, _is_new) books_to_add = [] @treeview_results.selection.selected_each do |_model, _path, iter| @results.each do |book, cover| next unless book.ident == iter[1] begin next unless assert_not_exist(library, book.isbn) rescue Alexandria::Library::NoISBNError puts 'noisbn' book.isbn = book.saved_ident = nil books_to_add << [book, cover] next rescue Alexandria::Library::InvalidISBNError puts "invalidisbn #{book.isbn}" next unless KeepBadISBNDialog.new(@parent, book).keep? book.isbn = book.saved_ident = nil end books_to_add << [book, cover] end end books_to_add.each do |book, cover_uri| add_book_to_library(library, book, cover_uri) end books_to_add.map(&:first) # array of Books only end |
#add_single_book_by_isbn(library, is_new) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 376 def add_single_book_by_isbn(library, is_new) # Perform the ISBN search via the providers. isbn = begin Library.canonicalise_isbn(@entry_isbn.text) rescue raise _("Couldn't validate the EAN/ISBN you " \ 'provided. Make sure it is written ' \ 'correctly, and try again.') end assert_not_exist(library, @entry_isbn.text) @button_add.sensitive = false notify_start_add_by_isbn GLib::Idle.add do @find_thread = Thread.new do log.info { "New @find_thread #{Thread.current}" } begin # MAJOR HACK, add this again... Alexandria::BookProviders.instance.add_observer(self) book, cover_url = Alexandria::BookProviders.isbn_search(isbn) # prov = FakeBookProviders.new() # prov.add_observer(self) # book, cover_url = prov.isbn_search(isbn) notify_end_add_by_isbn if book puts "adding book #{book} to library" add_book_to_library(library, book, cover_url) @entry_isbn.text = '' post_addition([book], library, is_new) else post_addition([], library, is_new) end rescue Alexandria::BookProviders::NoResultsError => e @find_error = e. @button_add.sensitive = true notify_end_add_by_isbn ensure Alexandria::BookProviders.instance.delete_observer(self) notify_end_add_by_isbn end end false end end |
#decode_cuecat?(entry) ⇒ Boolean
srsly?
361 362 363 364 365 366 367 368 369 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 361 def decode_cuecat?(entry) # srsly? if entry.text =~ /^\..*?\..*?\.(.*?)\.$/ tmp = Regexp.last_match[1].tr('a-zA-Z0-9+-', ' -_') tmp = ((32 + tmp.length * 3 / 4).to_i.chr << tmp).unpack('u')[0] tmp.chomp!("\000") entry.text = tmp.gsub!(/./) { |c| (c[0] ^ 67).chr } entry.text = 'Bad scan result' if entry.text.count('^ -~') > 0 end end |
#get_images_async ⇒ Object
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 189 def get_images_async log.info { 'get_images_async' } @images = {} @image_error = nil @image_thread = Thread.new do log.info { "New @image_thread #{Thread.current}" } begin @results.each_with_index do |result, i| uri = result[1] if uri if URI.parse(uri).scheme.nil? File.open(uri, 'r') do |io| @images[i] = io.read end else @images[i] = URI.parse(uri).read end end end rescue => e @image_error = e. end end GLib::Timeout.add(100) do if @image_error image_error_dialog(@image_error) else @images.each_pair do |key, value| begin loader = GdkPixbuf::PixbufLoader.new loader.last_write(value) pixbuf = loader.pixbuf if pixbuf.width > 1 iter = @treeview_results.model.get_iter(key.to_s) unless @treeview_results.model.iter_is_valid?(iter) raise format('Iter is invalid! %s', iter) end iter[2] = pixbuf # I bet you this is it! end @images.delete(key) rescue => e image_error_dialog(e.) end end end # Stop if the image download thread has stopped. if @image_thread.alive? log.info { "@image_thread (#{@image_thread}) still alive." } true else log.info { "@image_thread (#{@image_thread}) asleep now." } false end end end |
#image_error_dialog(error) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 182 def image_error_dialog(error) ErrorDialog.new( @parent, _('A problem occurred while downloading images'), error) end |
#notify_end_add_by_isbn ⇒ Object
530 531 532 533 534 535 536 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 530 def notify_end_add_by_isbn MainApp.instance..children.first.visible = false if @progress_pulsing GLib::Source.remove(@progress_pulsing) @progress_pulsing = nil end end |
#notify_start_add_by_isbn ⇒ Object
517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 517 def notify_start_add_by_isbn = MainApp.instance..children.first .visible = true @progress_pulsing = GLib::Timeout.add(100) do if @destroyed false else .pulse true end end end |
#on_add ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 484 def on_add return unless @button_add.sensitive? @find_thread&.kill @image_thread&.kill begin libraries = Libraries.instance.all_libraries library, is_new_library = @combo_libraries.selection_from_libraries(libraries) # book_was_added = false if @isbn_radiobutton.active? add_single_book_by_isbn(library, is_new_library) else books = add_selected_books(library, is_new_library) post_addition(books, library, is_new_library) end # Do not destroy if there is no addition. # return unless book_was_added rescue => e ErrorDialog.new(@parent, _("Couldn't add the book"), e.) end # books_to_add end |
#on_cancel ⇒ Object
511 512 513 514 515 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 511 def on_cancel @find_thread&.kill @image_thread&.kill @new_book_dialog.destroy end |
#on_changed(entry) ⇒ Object
176 177 178 179 180 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 176 def on_changed(entry) ok = !entry.text.strip.empty? decode_cuecat?(@entry_isbn) if entry == @entry_isbn (entry == @entry_isbn ? @button_add : @button_find).sensitive = ok end |
#on_clicked(widget, event) ⇒ Object
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 573 def on_clicked(, event) if (event.event_type == :button_press) && (event. == 1) radio, , box2, box3 = case when @eventbox_entry_search [@title_radiobutton, @entry_search, @eventbox_combo_search, @eventbox_entry_isbn] when @eventbox_combo_search [@title_radiobutton, @combo_search, @eventbox_entry_search, @eventbox_entry_isbn] when @eventbox_entry_isbn [@isbn_radiobutton, @entry_isbn, @eventbox_entry_search, @eventbox_combo_search] end radio.active = true .grab_focus .above_child = false box2.above_child = box3.above_child = true end end |
#on_criterion_toggled(item) ⇒ Object
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 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 132 def on_criterion_toggled(item) log.debug { 'on_criterion_toggled' } return unless item.active? # There used to be a strange effect here (pre SVN r1022). # When item is first toggled to "Search" the entry_search # field was unselectable. One used to have to click the dialog # title bar to be able to focus it again. Putting the GUI # modifications in an GLib::Idle.add block fixed the problem. is_isbn = item == @isbn_radiobutton if is_isbn GLib::Idle.add do @latest_size = @new_book_dialog.size @new_book_dialog.resizable = false @entry_isbn.grab_focus false end else GLib::Idle.add do @new_book_dialog.resizable = true @new_book_dialog.resize(*@latest_size) unless @latest_size.nil? @entry_search.grab_focus false end end @entry_isbn.sensitive = is_isbn @combo_search.sensitive = !is_isbn @entry_search.sensitive = !is_isbn @button_find.sensitive = !is_isbn @scrolledwindow.visible = !is_isbn on_changed(is_isbn ? @entry_isbn : @entry_search) unless is_isbn @button_add.sensitive = @treeview_results.selection.count_selected_rows > 0 end # Remember the last criterion selected (so that we can re-select # it when the dialog opens again). @@last_criterion_was_not_isbn = !is_isbn # @new_book_dialog.present # attempted fix, bring dialog to foreground end |
#on_find ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 249 def on_find log.info { 'on_find' } mode = case @combo_search.active when 0 BookProviders::SEARCH_BY_TITLE when 1 BookProviders::SEARCH_BY_AUTHORS when 2 BookProviders::SEARCH_BY_KEYWORD end # @progressbar.show # progress_pulsing = GLib::Timeout.add(100) do # if @destroyed # false # else # @progressbar.pulse # true # end # end criterion = @entry_search.text.strip @treeview_results.model.clear log.info { format('TreeStore Model: %s columns; ref_counts: %s', @treeview_results.model.n_columns, @treeview_results.model.ref_count) } @find_error = nil @results = nil @find_thread&.kill @image_thread&.kill notify_start_add_by_isbn GLib::Idle.add do @find_thread = Thread.new do log.info { "New @find_thread #{Thread.current}" } begin Alexandria::BookProviders.instance.add_observer(self) @results = Alexandria::BookProviders.search(criterion, mode) log.info { "got #{@results.length} results" } rescue => e @find_error = e. ensure Alexandria::BookProviders.instance.delete_observer(self) # notify_end_add_by_isbn end end false end GLib::Timeout.add(100) do # This block copies results into the tree view, or shows an # error if the search failed. # Err... continue == false if @find_error continue = if @find_error ErrorDialog.new(@parent, _('Unable to find matches for your search'), @find_error) false elsif @results log.info { "Got results: #{@results[0]}..." } @results.each_key do |book| s = format(_('%s, by %s'), book.title, book..join(', ')) similar_books = @results.find { |book2, _cover2| (book.title == book2.title) && (book. == book2.) } s += " (#{book.edition}, #{book.publisher})" if similar_books.length > 1 log.info { format('Copying %s into tree view.', book.title) } iter = @treeview_results.model.append iter[0] = s iter[1] = book.ident iter[2] = Icons::BOOK end # Kick off the image download thread. if @find_thread.alive? log.info { "@find_thread (#{@find_thread}) still alive." } true else log.info { "@find_thread (#{@find_thread}) asleep now." } # Not really async now. get_images_async false # continue == false if you get to here. Stop timeout. end else # Stop if the book find thread has stopped. @find_thread.alive? end # continue == false if @find_error OR if results are returned # timeout ends if continue is false! unless continue unless @find_thread.alive? # This happens after find_thread is done unless @destroyed # GLib::Source.remove(progress_pulsing) # @progressbar.hide notify_end_add_by_isbn @button_add.sensitive = false end end end continue # timeout loop condition end end |
#on_focus ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 550 def on_focus if @isbn_radiobutton.active? && @entry_isbn.text.strip.empty? clipboard = Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD) if (text = clipboard.wait_for_text) if Library.valid_isbn?(text) || Library.valid_ean?(text) || Library.valid_upc?(text) GLib::Idle.add do @entry_isbn.text = text @entry_isbn.grab_focus @entry_isbn.select_region(0, -1) # select all... # @button_add.grab_focus false end log.debug { "Setting ISBN field to #{text}" } puts text # required, strangely, to prevent GUI strangeness # above last checked with ruby-gnome2 0.17.1 2009-12-09 # if this puts is commented out, the cursor disappears # from the @entry_isbn box... weird, ne? - CathalMagus end end end end |
#on_help ⇒ Object
597 598 599 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 597 def on_help Alexandria::UI.display_help(@preferences_dialog, 'add-book-by-isbn') end |
#on_results_button_press_event(_widget, event) ⇒ Object
371 372 373 374 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 371 def (, event) # double left click on_add if (event.event_type == :'2button_press') && (event. == 1) end |
#post_addition(books, library, is_new_library) ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 458 def post_addition(books, library, is_new_library) puts "post_addition #{books.size}" return if books.empty? # books, a 1d array of Alexandria::Book @block.call(books, library, is_new_library) if @keep_open.active? # TODO: reset and clear fields if @@last_criterion_was_not_isbn @entry_search.select_region(0, -1) # select all, ready to delete @treeview_results.model.clear @entry_search.grab_focus else @button_add.sensitive = true # @entry_isbn.text = '' # blank ISBN field @entry_isbn.grab_focus end else # Now we can destroy the dialog and go back to the main # application. @new_book_dialog.destroy end end |
#setup_dialog_gui ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 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 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 79 def setup_dialog_gui libraries = Libraries.instance.all_regular_libraries @selected_library = libraries.first if @selected_library.is_a?(SmartLibrary) @combo_libraries.populate_with_libraries(libraries, @selected_library) @treeview_results.model = Gtk::ListStore.new(String, String, GdkPixbuf::Pixbuf) @treeview_results.selection.mode = Gtk::SELECTION_MULTIPLE @treeview_results.selection.signal_connect('changed') do @button_add.sensitive = true end renderer = Gtk::CellRendererPixbuf.new col = Gtk::TreeViewColumn.new('', renderer) col.set_cell_data_func(renderer) do |_column, cell, _model, iter| pixbuf = iter[2] max_height = 25 if pixbuf.height > max_height new_width = pixbuf.width * (max_height.to_f / pixbuf.height) pixbuf = pixbuf.scale(new_width, max_height) end cell.pixbuf = pixbuf end @treeview_results.append_column(col) col = Gtk::TreeViewColumn.new('', Gtk::CellRendererText.new, text: 0) @treeview_results.append_column(col) @combo_search.active = 0 # Re-select the last selected criterion. # TODO let's do this from a Gconf setting instead, maybe? @title_radiobutton.active = @@last_criterion_was_not_isbn if @@last_criterion_was_not_isbn @entry_search.grab_focus else @entry_isbn.grab_focus end @find_thread = nil @image_thread = nil @new_book_dialog.signal_connect('destroy') { @new_book_dialog.destroy @destroyed = true } end |
#update(status, provider) ⇒ Object
538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 538 def update(status, provider) = { searching: _("Searching Provider '%s'..."), error: _("Error while Searching Provider '%s'"), not_found: _("Not Found at Provider '%s'"), found: _("Found at Provider '%s'") } = [status] % provider log.debug { "update message : #{}" } MainApp.instance.ui_manager.set_status_label() end |
#widget_names ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/alexandria/ui/dialogs/new_book_dialog.rb', line 68 def [:liststore1, :liststore2, :new_book_dialog, :dialog_vbox1, :dialog_action_area1, :button_help, :button_cancel, :button_add, :table1, :keep_open, :combo_libraries, :cellrenderertext1, :eventbox_entry_isbn, :entry_isbn, :label3, :hbox2, :eventbox_combo_search, :combo_search, :cellrenderertext2, :eventbox_entry_search, :entry_search, :button_find, :scrolledwindow, :treeview_results, :title_radiobutton, :isbn_radiobutton, :progressbar] end |