Class: FtpBrowserWidget

Inherits:
DirectoryDiffWidget show all
Includes:
Fox
Defined in:
lib/piggy-gui/ftp_browser_widget.rb

Constant Summary

Constants inherited from DirectoryDiffWidget

DirectoryDiffWidget::MsgStyle, DirectoryDiffWidget::Placement

Instance Attribute Summary

Attributes inherited from DirectoryDiffWidget

#options, #quitButton

Instance Method Summary collapse

Methods inherited from DirectoryDiffWidget

#basic_load_icon, #create, #dir_changed, #dir_chooser_for_field, #error, #fox_path, #h_sep_into, #include_file?, #initialize_buttons, #initialize_controls, #initialize_controls_top_right, #initialize_frames, #initialize_layout, #item_filename, #on_dir_up_lef_list, #on_dir_up_right_list, #on_doubleclick_left_file_list, #on_doubleclick_right_file_list, #preferred_width, #prepare_closure, #right_path, #ruby_path, #selection_in_list, #set_right_path, #space_into, #txt_field_num_chars, #update_list, #update_local_list, #update_right_list, #update_status_icons, #update_status_icons_for, #update_status_icons_left

Constructor Details

#initialize(window, options = PiggyOptions.new) ⇒ FtpBrowserWidget

Returns a new instance of FtpBrowserWidget.



63
64
65
66
67
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 63

def initialize(window, options = PiggyOptions.new)
  @ftpAdapter = FtpAdapter.new(options)
  @connectionDataVerified = false
  super(window, options)
end

Instance Method Details

#ask_and_ftp_delete_selectionObject



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 408

def ask_and_ftp_delete_selection
  sel = selection_in_list(@leftFileList)
  dirs = sel.select { |file|
    @leftContents[file].directory?
  }
  files = sel.reject { |file|
    @leftContents[file].directory?
  }
  return false if sel.empty?
  answer = FXMessageBox.question(self, MBOX_YES_NO,
    "Really delete?",
    "Selected:\n#{dirs.size.to_s} directories\n#{files.size.to_s} files\nReally delete?")
  if answer == MBOX_CLICKED_YES
    ftp_delete_left(files)
    ftp_remove_left(dirs)
  end 
end

#ask_and_upload_selectionObject



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 392

def ask_and_upload_selection
  sel = selection_in_list(@rightFileList)
  return false if !sel || sel.empty?
  files = sel.collect { |e| FilePath.join(right_path, e) }
  upload_info = UploadInfo.new(files)
  num = upload_info.num_files
  if(num < 1)
    FXMessageBox.information(self, MBOX_OK, 'No files',
      'No files found in selected directories!')
    return false
  end
  answer = FXMessageBox.question(self, MBOX_YES_NO, 'Really upload?',
    "Upload #{num} fil#{num == 1 ? 'e' : 'es'}?")
  ftp_upload_right(upload_info.bytes_hash, upload_info.bytes) if answer == MBOX_CLICKED_YES
end

#auto_select_leftObject

actions



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 296

def auto_select_left
  list = @leftFileList
  list.killSelection
  (0..list.getNumItems-1).each do
    |index|
    file = item_filename(list, index)
    myInfo = @leftContents[file]
    otherInfo = @rightContents[file]
    select = if myInfo.directory?
      false
    elsif otherInfo
      false
    else
      true
    end
    list.selectItem(index) if select
  end
end

#auto_select_rightObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 315

def auto_select_right
  list = @rightFileList
  list.killSelection
  (0..list.getNumItems-1).each do
    |index|
    file = item_filename(list, index)
    myInfo = @rightContents[file]
    otherInfo = @leftContents[file]
    select = if myInfo.directory?
      false
    elsif otherInfo && otherInfo.directory?
      false
    elsif !otherInfo || otherInfo.mtime < myInfo.mtime
      true
    else
      false
    end
    list.selectItem(index) if select
  end
end

#connectObject



275
276
277
278
279
280
281
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 275

def connect
  @connectionDataVerified = false
  @ftpAdapter.connect(host, user, password)
  @ftpAdapter.chdir(left_path)
  @connectionDataVerified = true
  @connect_button.setState(STATE_DOWN)  
end

#connect_eventsObject



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
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 133

def connect_events
  super
  @serverField.connect(SEL_COMMAND)  {
    @options.ftpHost = fox_2_ruby(@serverField.getText)
    @connect_button.setState(STATE_UP)
  }
  @userField.connect(SEL_COMMAND)  {
    @options.ftpUser = fox_2_ruby(@userField.getText)
    @connect_button.setState(STATE_UP)
  }
  @passwordField.connect(SEL_COMMAND) {
    @connect_button.setState(STATE_UP)
  }
  [@connect_button, @updateButton].each { |b|
    b.connect(SEL_COMMAND, method(:on_cmd_update))
  }
  @autoSelectForDelButton.connect(SEL_COMMAND, method(:on_cmd_auto_select_left))
  @removeSelectedLeftButton.connect(SEL_COMMAND, method(:on_cmd_remove_selected_left))
  @autoSelectRightButton.connect(SEL_COMMAND, method(:on_cmd_auto_select_right))
  @uploadSelectedButton.connect(SEL_COMMAND, method(:on_cmd_upload))
  @details_button.connect(SEL_COMMAND) {
    if(details_shown?)
      hide_connection_details
    else
      show_connection_details
    end
  }
end

#connection_details_fieldsObject



192
193
194
195
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 192

def connection_details_fields
  [ @passwordField, @serverField, @userField, @connect_button ] +
    @labels.slice(0..2)
end

#details_shown?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 106

def details_shown?
  @passwordField.visible?
end

#ftp_delete_left(files) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 336

def ftp_delete_left(files)
  ftp_do do
    files.each { |file|
      @ftpAdapter.delete(file)
    }
  end
  update_status_icons
end

#ftp_do(&block) ⇒ Object

don’t chdir in block



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
271
272
273
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 241

def ftp_do(&block) # don't chdir in block
  getApp.beginWaitCursor
  @leftFileList.clearItems
  @leftContents = DirectoryContents.new(left_path)
  begin
    connect
    block.call # <------ the call
    puts 'ftpDo: call finished'
    begin
      connect if @ftpAdapter.closed? 
      update_left_connected
    rescue Errno::EINVAL => ex
      puts "Recover from: #{ex.message}"
      # abortion and big files
      # might cause socket errors
      # force reconnect
      connect
      update_left_connected
    end
  rescue SocketError => ex
    error("SocketError: #{ex.message}")
  rescue Net::FTPError => ex
    error("Net::FTPError: #{ex.message}")
  rescue StandardError => ex
    error("#{ex.class.to_s}: #{ex.message}")
  rescue Timeout::Error => ex
    error("Timeout - operation took longer than expected. \nCheck network connection and try again.")
  ensure
    @ftpAdapter.close
    @connect_button.setState(STATE_UP) unless @connectionDataVerified
  end
  getApp.endWaitCursor
end

#ftp_remove_left(dirs) ⇒ Object



345
346
347
348
349
350
351
352
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 345

def ftp_remove_left(dirs)
  ftp_do do
    dirs.each { |dir|
      @ftpAdapter.rmdir(dir)
    }
  end
  update_status_icons
end

#ftp_upload_right(hash, totalSize) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 354

def ftp_upload_right(hash, totalSize)
  all = hash.keys.size
  current = 0
  ftp_do do
    progressDialog = FXProgressDialog.new(self,
      'Transferring data',
      'Transferring data',
       PROGRESSDIALOG_NORMAL | PROGRESSDIALOG_CANCEL)
    begin
      progressDialog.setProgress(0)
      progressDialog.setTotal(totalSize)
      progressDialog.create
      progressDialog.show
      progressDialog.repaint
      hash.keys.sort.each do
        |file|
        current = current + 1
        progressDialog.setMessage(ruby_2_fox("Transferring #{current.to_s}/#{all.to_s}: #{File.basename(file)}"))
        progressDialog.repaint
        @ftpAdapter.upload(right_path, file) { |data|
          progressDialog.increment(data.size)
          progressDialog.repaint
          # Workaround for
          # response problem with vista
          # getApp().runModalWhileEvents()
          getApp().runWhileEvents()
          !progressDialog.cancelled?
        }
        break if progressDialog.cancelled?
      end
    ensure
      puts "Upload stopped at #{current.to_s}/#{all.to_s}"
      progressDialog.hide
    end
  end
  update_status_icons
end

#hide_connection_detailsObject

update



199
200
201
202
203
204
205
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 199

def hide_connection_details
  fields = connection_details_fields
  fields.each { |f| f.hide }
  @details_button.enable
  @details_button.setIcon(@showDetailsIcon)
  @left_frame.recalc
end

#hostObject



184
185
186
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 184

def host
  fox_2_ruby(@serverField.getText)
end

#in_createObject

open/close



488
489
490
491
492
493
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 488

def in_create
  update_right_list
  update_status_icons
  puts preferred_width.to_s
  @horizontal_splitter.setSplit(0, preferred_width/2)
end

#initialize_controls_bottom_leftObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 110

def initialize_controls_bottom_left
  @updateButton = FXButton.new(@leftButtonsFrame,
    "\tUpdate list",
    @reloadIcon)
  super
  @autoSelectForDelButton = FXButton.new(@leftButtonsFrame,
    "\tChoose files automatically",
    @autoSelectForDelIco)
  @removeSelectedLeftButton = FXButton.new(@leftButtonsFrame,
    "\tRemove selected files",
    @removeSelectedIco)
end

#initialize_controls_bottom_rightObject



123
124
125
126
127
128
129
130
131
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 123

def initialize_controls_bottom_right
  super
  @autoSelectRightButton = FXButton.new(@rightButtonsFrame,
    "\tChoose files automatically",
    @autoSelectRightIcon)
  @uploadSelectedButton = FXButton.new(@rightButtonsFrame,
    "\tUpload selected",
    @uploadIcon)
end

#initialize_controls_top_leftObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 83

def initialize_controls_top_left
  @labels.push(FXLabel.new(@leftListPropFrame, "FTP server:"))
  @serverField = FXTextField.new(@leftListPropFrame, txt_field_num_chars)
  @serverField.setLayoutHints(LAYOUT_FILL_X|LAYOUT_FILL_COLUMN) 
  @labels.push(FXLabel.new(@leftListPropFrame, "User:"))
  @userField = FXTextField.new(@leftListPropFrame, txt_field_num_chars)
  @userField.setLayoutHints(LAYOUT_FILL_X|LAYOUT_FILL_COLUMN)
  @labels.push(FXLabel.new(@leftListPropFrame, "Password:"))
  pw_frame = new_hframe_without_padding(@leftListPropFrame)
  @passwordField = FXTextField.new(pw_frame, txt_field_num_chars)
  @passwordField.setLayoutHints(LAYOUT_FILL_X|LAYOUT_FILL_COLUMN)    
  @passwordField.setTextStyle(TEXTFIELD_PASSWD)
  @connect_button = FXButton.new(pw_frame, "\tConnect", @connectIcon)
  @labels.push(FXLabel.new(@leftListPropFrame, "Destination path:"))
  left_path_frame = new_hframe_without_padding(@leftListPropFrame)
  @leftPathField = FXTextField.new(left_path_frame, txt_field_num_chars)
  @leftPathField.setLayoutHints(LAYOUT_FILL_X|LAYOUT_FILL_COLUMN)
  @details_button = FXButton.new(left_path_frame,
    "\tShow connection details", @hideDetailsIcon)
  @details_button.setFrameStyle(FRAME_NONE)
  @details_button.disable
end

#left_pathObject

accessing



171
172
173
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 171

def left_path
  @options.remoteDestinationPath
end

#load_iconsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 69

def load_icons
  super
  @reloadIcon = basic_load_icon('reload.ico')
  @autoSelectForDelIco =  basic_load_icon('auto-select-for-del.ico')
  @removeSelectedIco = basic_load_icon('remove-selected.ico')
  @autoSelectRightIcon = basic_load_icon('auto-select-right.ico')
  @uploadIcon = basic_load_icon('auto-upload-right.ico')
  @moveRightDirIcon = basic_load_icon('move-right-dir.ico')
  @moveRightFileIcon = basic_load_icon('move-right-file.ico')
  @connectIcon = basic_load_icon('connect.ico')
  @showDetailsIcon = basic_load_icon('show_details.ico')
  @hideDetailsIcon = basic_load_icon('hide_details.ico')
end

#on_cmd_auto_select_left(sender, sel, ptr) ⇒ Object



434
435
436
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 434

def on_cmd_auto_select_left(sender, sel, ptr)
  auto_select_left
end

#on_cmd_auto_select_right(sender, sel, ptr) ⇒ Object



442
443
444
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 442

def on_cmd_auto_select_right(sender, sel, ptr)
  auto_select_right
end

#on_cmd_remove_selected_left(sender, sel, ptr) ⇒ Object



438
439
440
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 438

def on_cmd_remove_selected_left(sender, sel, ptr)
  ask_and_ftp_delete_selection
end

#on_cmd_update(sender, sel, ptr) ⇒ Object

events



428
429
430
431
432
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 428

def on_cmd_update(sender, sel, ptr)
  update_ftp_list
  update_status_icons
  hide_connection_details if @connectionDataVerified
end

#on_cmd_upload(sender, sel, event) ⇒ Object



446
447
448
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 446

def on_cmd_upload(sender, sel, event)
  ask_and_upload_selection
end

#on_menu_left_list(sender, sel, event) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 450

def on_menu_left_list(sender, sel, event)
  selection = selection_in_list(@leftFileList)
  pane = FXMenuPane.new(self)
  # FXMenuSeparator.new(pane)
  item = FXMenuCommand.new(pane, "&Remove selected")
  if(selection.empty?)
    item.disable
  else
    item.connect(SEL_COMMAND){ |sender, sel, event|
      ask_and_ftp_delete_selection
    }
  end
  pane.create
  pane.popup(nil, event.root_x, event.root_y)
  getApp.runModalWhileShown(pane)
  return 1
end

#on_menu_right_list(sender, sel, event) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 468

def on_menu_right_list(sender, sel, event)
  selection = selection_in_list(@rightFileList)
  pane = FXMenuPane.new(self)
  item = FXMenuCommand.new(pane, "&Upload selected")
  if(selection.empty?)
    item.disable
  else
    item.connect(SEL_COMMAND){ |sender, sel, event|
      ask_and_upload_selection
    }
  end
  # FXMenuSeparator.new(pane)
  pane.create
  pane.popup(nil, event.root_x, event.root_y)
  getApp.runModalWhileShown(pane)
  return 1
end

#passwordObject



180
181
182
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 180

def password
  fox_2_ruby(@passwordField.getText)
end

#prepare_closuresObject



495
496
497
498
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 495

def prepare_closures
  @ftpAdapter.close
  super
end

#set_defaultsObject



162
163
164
165
166
167
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 162

def set_defaults
  @serverField.setText(ruby_2_fox(@options.ftpHost))
  @userField.setText(ruby_2_fox(@options.ftpUser))
  @rightPathField.setText(fox_path(@options.localDestinationPath))
  @leftPathField.setText(ruby_2_fox(@options.remoteDestinationPath))
end

#set_left_path(p) ⇒ Object



175
176
177
178
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 175

def set_left_path(p)
  @options.remoteDestinationPath = FilePath.intern_path(p)
  @leftPathField.setText(left_path)
end

#show_connection_detailsObject



207
208
209
210
211
212
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 207

def show_connection_details
  fields = connection_details_fields
  fields.each { |f| f.show }
  @details_button.setIcon(@hideDetailsIcon)
  @left_frame.recalc
end

#update_ftp_listObject



290
291
292
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 290

def update_ftp_list
  ftp_do {} # every ftp action includes an update :-)
end

#update_left_connectedObject



283
284
285
286
287
288
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 283

def update_left_connected
  update_list(@ftpAdapter.nlst, @leftFileList, @leftContents) do
    |file, path|
    @ftpAdapter.info(file, path)
  end
end

#update_left_listObject



233
234
235
236
237
238
239
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 233

def update_left_list
  if @connectionDataVerified
    update_ftp_list
  else
    show_connection_details 
  end
end

#update_status_icons_rightObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 214

def update_status_icons_right
  (0..@rightFileList.getNumItems - 1).each do
    |index|
    file = item_filename(@rightFileList, index)
    myInfo = @rightContents[file]
    otherInfo = @leftContents[file]
    icon = if myInfo.directory?
      otherInfo ? @dirIcon : @moveRightDirIcon
    else
      if otherInfo and otherInfo.mtime
        otherInfo.mtime < myInfo.mtime ? @newerFileIcon : @fileIcon
      else
        @moveRightFileIcon
      end
    end
    @rightFileList.setItemMiniIcon(index, icon)
  end
end

#userObject



188
189
190
# File 'lib/piggy-gui/ftp_browser_widget.rb', line 188

def user
  fox_2_ruby(@userField.getText)
end