#!/usr/bin/env ruby # # Author: Rune Hammersland # Lisence: GNU/GPL. # # Drag and drop program to get files with wget and put files through FTP. # If a local file is dropped, it's transferred to $remote_destination on # $remote_server. If a URL from a browser is dropped, it's downloaded to # $local_destination via wget. # # To use this program, you need to set the global variables. # # FIXME: Not nice to use zenity to get user and pass, especially since the # password is echoed while it's being typed. # TODO: Find out if threads are being terminated and the array emptied, or # if the array is collecting dead threads and consuming memory. require 'gtk2' require 'net/ftp' # GLOBAL VARS - set these. $local_destination = '' # Where to store downloaded files. $remote_server = '' # Hostname to remote server to store files on. $remote_destination = '' # Where to store files on remote server. class DnD < Gtk::Window attr_reader :button # Button to drop things on attr_reader :threads # Array of downloading and uploading threads def initialize super @threads = [] targets = [ ["text/plain", nil, 102] ] # Create button and set it as a drag destination. @button = Gtk::Button.new("Drop it like it's hot!") Gtk::Drag.dest_set(@button, Gtk::Drag::DEST_DEFAULT_MOTION | Gtk::Drag::DEST_DEFAULT_HIGHLIGHT | Gtk::Drag::DEST_DEFAULT_DROP, targets, Gdk::DragContext::ACTION_COPY) # Connect to signals. @button.signal_connect("drag_data_received") { |w, dc, x, y, data, info, time| dc.targets.each do |target| if target.name == "text/plain" file_put(data.data) if data.data[0..6] == 'file://' file_get(data.data) if data.data[0..6] == 'http://' end end Gtk::Drag.finish(dc, true, false, time) } @button.signal_connect("clicked") { self.visible = false puts "Completing gets and puts ..." if @threads.length > 0 @threads.each { |th| th.join } Gtk.main_quit } # Set up window (self). self.title = "DND Test" self.set_default_size(50, 50) self.decorated = false self.add(@button) self.show_all end # Method to get URL from a website. def file_get(url) puts "Getting #{url}" @threads << Thread.new(url) { system("wget -P #{$local_destination} #{url}") } end # Method to put URL to server. def file_put(url) user, pass = get_credentials() file = url[7..-1].chomp! puts "Putting #{file}" @threads << Thread.new(file) { begin Net::FTP.open($remote_server, user, pass) { |ftp| ftp.chdir($remote_destination) ftp.putbinaryfile(file, File.basename(file)) } rescue `zenity --error --title "Error with connection" --text "Error connecting to server"` end } end # Method to get username and password for FTP connection. Nasty stuff # right now. def get_credentials() user = `zenity --entry --text "Username: " --title "Username for connection"` pass = `zenity --entry --text "Password: " --title "Password for connection"` [user.chomp, pass.chomp] end end if $0 == __FILE__ unless ($remote_server.length > 0 && $remote_destination.length > 0 && $local_destination.length > 0) puts "Forgot to set the global vars, did we?" exit 1 end Gtk.init drop = DnD.new Gtk.main end