Class: Alexandria::UI::SidepaneManager
- Inherits:
-
Object
- Object
- Alexandria::UI::SidepaneManager
- Includes:
- Logging, GetText
- Defined in:
- lib/alexandria/ui/sidepane_manager.rb
Instance Attribute Summary collapse
-
#library_listview ⇒ Object
Returns the value of attribute library_listview.
Instance Method Summary collapse
-
#contains_illegal_character(new_text) ⇒ Object
if new_text is invalid utf-8, returns true if new_text contains disallowed char (/ or initial .), returns a MatchData object otherwise returns nil.
-
#initialize(library_listview, parent) ⇒ SidepaneManager
constructor
A new instance of SidepaneManager.
- #library_already_exists(new_text) ⇒ Object
- #on_edited_library(cell, path_string, new_text) ⇒ Object
- #setup_sidepane ⇒ Object
Methods included from Logging
Constructor Details
#initialize(library_listview, parent) ⇒ SidepaneManager
Returns a new instance of SidepaneManager.
16 17 18 19 20 21 22 |
# File 'lib/alexandria/ui/sidepane_manager.rb', line 16 def initialize(library_listview, parent) @library_listview = library_listview @parent = parent @libraries = LibraryCollection.instance @main_app = @parent.main_app setup_sidepane end |
Instance Attribute Details
#library_listview ⇒ Object
Returns the value of attribute library_listview.
14 15 16 |
# File 'lib/alexandria/ui/sidepane_manager.rb', line 14 def library_listview @library_listview end |
Instance Method Details
#contains_illegal_character(new_text) ⇒ Object
if new_text is invalid utf-8, returns true if new_text contains disallowed char (/ or initial .), returns a MatchData object otherwise returns nil
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/alexandria/ui/sidepane_manager.rb', line 34 def contains_illegal_character(new_text) new_text.unpack("U*") # attempt to unpack as UTF-8 characters # forbid / character (since Library names become dir names) # also no initial . since that hides the Library (hidden file) # forbidding an initial dot also disallows "." and ".." # which are of course pre-existing directories. %r{(^\.|/)}.match(new_text) rescue StandardError => ex log.warn { "New library name not valid UTF-8: #{ex.}" } true # /([^\w\s'"()&?!:;.\-])/.match(new_text) # anglocentric! end |
#library_already_exists(new_text) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/alexandria/ui/sidepane_manager.rb', line 24 def library_already_exists(new_text) x = (@libraries.all_libraries + Library.deleted_libraries).find do |library| library.name == new_text.strip end x && (x.name != @parent.selected_library.name) end |
#on_edited_library(cell, path_string, new_text) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/alexandria/ui/sidepane_manager.rb', line 47 def on_edited_library(cell, path_string, new_text) log.debug { "edited library name #{new_text}" } return if cell.text == new_text if (match = contains_illegal_character(new_text)) if match.instance_of? MatchData chars = match[1].gsub(/&/, "&") ErrorDialog.new(@main_app, _("Invalid library name '%s'") % new_text, _("The name provided contains the " \ "disallowed character <b>%s</b>") % chars).display else ErrorDialog.new(@main_app, _("Invalid library name"), _("The name provided contains " \ "invalid characters.")).display end elsif new_text.strip.empty? log.debug { "Empty text" } ErrorDialog.new(@main_app, _("The library name " \ "can not be empty")).display elsif library_already_exists new_text log.debug { "Already exists" } ErrorDialog.new(@main_app, _("The library can not be renamed"), _("There is already a library named " \ "'%s'. Please choose a different " \ "name.") % new_text.strip).display else log.debug { "Attempting to apply #{path_string}, #{new_text}" } path = Gtk::TreePath.new(path_string) iter = @library_listview.model.get_iter(path) library_name = new_text.strip log.info { "library name is #{library_name}" } iter[1] = @parent.selected_library.name = library_name @parent.setup_move_actions @parent.refresh_libraries end end |
#setup_sidepane ⇒ Object
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 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 |
# File 'lib/alexandria/ui/sidepane_manager.rb', line 86 def setup_sidepane @library_listview.model = Gtk::ListStore.new(GdkPixbuf::Pixbuf, String, TrueClass, TrueClass) @library_separator_iter = nil @libraries.all_regular_libraries.each { |x| @parent.append_library(x) } @libraries.all_smart_libraries.each { |x| @parent.append_library(x) } renderer = Gtk::CellRendererPixbuf.new column = Gtk::TreeViewColumn.new(_("Library")) column.pack_start(renderer, false) column.set_cell_data_func(renderer) do |_col, cell, _model, iter| # log.debug { "sidepane: cell_data_func #{col}, #{cell}, #{iter}" } cell.pixbuf = iter[0] end renderer = Gtk::CellRendererText.new renderer.ellipsize = :end column.pack_start(renderer, true) column.set_cell_data_func(renderer) do |_col, cell, _model, iter| # log.debug { "sidepane: editable #{cell}, #{iter} #{iter[1]}: #{iter[2]}" } cell.text = iter[1] cell.editable = iter[2] # log.debug { "exit sidepane: editable #{cell}, #{iter}" } end renderer.signal_connect("edited") do |cell, path_string, new_text| on_edited_library(cell, path_string, new_text) end @library_listview.append_column(column) @library_listview.set_row_separator_func do |model, iter| # TODO: Replace with iter[3] if possible model.get_value(iter, 3) end @library_listview.selection.signal_connect("changed") do log.debug { "changed" } @parent.refresh_libraries @parent.refresh_books end @library_listview.enable_model_drag_dest(BOOKS_TARGET_TABLE, :move) @library_listview .signal_connect("drag-motion") do |, drag_context, x, y, time, _data| log.debug { "drag-motion" } path, column, = @library_listview.get_path_at_pos(x, y) if path # Refuse drags from/to smart libraries. if @parent.selected_library.is_a?(SmartLibrary) path = nil else iter = @library_listview.model.get_iter(path) if iter[3] # separator? path = nil else library = @libraries.all_libraries.find do |lib| lib.name == iter[1] end path = nil if library.is_a?(SmartLibrary) end end end @library_listview.set_drag_dest_row(path, :into_or_after) Gdk.drag_status(drag_context, path ? drag_context.suggested_action : 0, time) end @library_listview .signal_connect("drag-drop") do |, drag_context, _x, _y, time, _data| log.debug { "drag-drop" } .drag_get_data(drag_context, drag_context.targets.first, time) true end @library_listview .signal_connect("drag-data-received") do |_, drag_context, x, y, data, _, _| log.debug { "drag-data-received" } success = false # FIXME: Ruby-GNOME2 should make comparison work without needing to # call #name. if data.data_type.name == Gdk::Selection::TYPE_STRING.name success, path = @library_listview.get_dest_row_at_pos(x, y) if success iter = @library_listview.model.get_iter(path) library = @libraries.all_libraries.find do |lib| lib.name == iter[1] end @parent.move_selected_books_to_library(library) success = true end end begin drag_context.finish(success: success, delete: false) rescue StandardError => ex log.error { "drag_context.finish failed: #{ex}" } raise end end end |