Class: Alexandria::UI::SidePaneManager

Inherits:
Object
  • Object
show all
Includes:
Logging, GetText
Defined in:
lib/alexandria/ui/sidepane.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log

Constructor Details

#initialize(library_listview, parent) ⇒ SidePaneManager

Returns a new instance of SidePaneManager.



28
29
30
31
32
33
34
# File 'lib/alexandria/ui/sidepane.rb', line 28

def initialize(library_listview, parent)
  @library_listview = library_listview
  @parent = parent
  @libraries = Libraries.instance
  @main_app = @parent.main_app
  setup_sidepane
end

Instance Attribute Details

#library_listviewObject

Returns the value of attribute library_listview.



27
28
29
# File 'lib/alexandria/ui/sidepane.rb', line 27

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



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/alexandria/ui/sidepane.rb', line 46

def contains_illegal_character(new_text)
  new_text.unpack('U*') # attempt to unpack as UTF-8 characters
  match = /(^\.|\/)/.match(new_text)
  # 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.
  match
rescue => ex
  log.warn { "New library name not valid UTF-8: #{ex.message}" }
  true
  # /([^\w\s'"()&?!:;.\-])/.match(new_text) # anglocentric!
end

#library_already_exists(new_text) ⇒ Object



36
37
38
39
40
41
# File 'lib/alexandria/ui/sidepane.rb', line 36

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



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/alexandria/ui/sidepane.rb', line 60

def on_edited_library(cell, path_string, new_text)
  log.debug { "edited library name #{new_text}" }
  ## new_text = new_text.reverse # for testing;
  # a great way to generate broken UTF-8
  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)
      else
        ErrorDialog.new(@main_app, _('Invalid library name'),
                        _('The name provided contains ' \
                          'invalid characters.'))
      end

    elsif new_text.strip.empty?
      log.debug { 'Empty text' }
      ErrorDialog.new(@main_app, _('The library name ' \
                                   'can not be empty'))
    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)
    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
end

#setup_sidepaneObject



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
# File 'lib/alexandria/ui/sidepane.rb', line 101

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', &method(:on_edited_library))
  @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 |_widget, 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 |widget, drag_context, _x, _y, time, _data|
    log.debug { 'drag-drop' }

    widget.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 => ex
      log.error { "drag_context.finish failed: #{ex}" }
      raise
    end
  end
end