sshfs-mapper qasim: Lot of UI improvements.

* distict map menu & context menu
* reload maps on map menu display


git-svn-id: https://websvn.glenux.net/svn/Upoc/sshfs-mapper/trunk@1709 eaee96b3-f302-0410-b096-c6cfd47f7835
This commit is contained in:
glenux 2011-07-24 15:41:33 +00:00
parent 131feaf13b
commit 1b77b3da68
3 changed files with 152 additions and 118 deletions

View file

@ -23,8 +23,10 @@ module Qasim
def initialize def initialize
@config = Config.new @config = Config.new
@config.parse_cmd_line ARGV #@config.parse_cmd_line ARGV
@config.parse_file
@map_menu = nil
@context_menu = nil
end end
def dbus_notify title, body, icon def dbus_notify title, body, icon
@ -48,6 +50,98 @@ module Qasim
# "Sorry dude", 2, 5000 ) # "Sorry dude", 2, 5000 )
end end
#
# Rebuild map menu
#
def build_map_menu
# reload maps dynamically
@config.parse_maps
if @map_menu.nil? then
@map_menu = Qt::Menu.new
else
@map_menu.clear
end
previous_host = nil
@config.maps.sort do |mx,my|
mx.host <=> my.host
end.each do |map|
if map.host != previous_host and not previous_host.nil? then
@map_menu.addSeparator
end
name = (File.basename map.path).gsub(/\.map$/,'')
itemx = Qt::Action.new(name, @map_menu)
itemx.setCheckable true;
itemx.connect(SIGNAL(:triggered)) do
action_trigger_map_item map, itemx
end
@map_menu.addAction itemx;
previous_host = map.host
end
end
#
# Action when map item triggered
#
def action_trigger_map_item map, item
puts "%s => %s" % [map.path, item.checked ]
if map.connected? then
puts "disconnect !"
else
puts "connect !"
begin
success = true
map.connect do |cmd,cmd_args|
process = Qt::Process.new
process.connect(SIGNAL('finished(int, QProcess::ExitStatus)')) do |exitcode,exitstatus|
puts "exitcode = %s, exitstatus = %s" % [exitcode, exitstatus]
if exitcode != 0 then
success = false
item.setChecked success
end
end
process.start cmd, cmd_args
end
rescue Map::ConnectError => e
puts e.inspect
end
#FIXME: on error, setChecked false
end
end
#
#
#
def build_context_menu
@context_menu = Qt::Menu.new
act_pref = Qt::Action.new _('&Preferences'), @context_menu
act_pref.setIcon( Qt::Icon::fromTheme("configure") )
act_pref.setIconVisibleInMenu true
act_pref.setEnabled false
@context_menu.addAction act_pref;
act_about = Qt::Action.new '&About', @context_menu
act_about.setIcon( Qt::Icon::fromTheme("help-about") )
act_about.setIconVisibleInMenu true
act_about.setEnabled false
@context_menu.addAction act_about;
@context_menu.addSeparator
act_quit = Qt::Action.new _('Quit'), @context_menu
act_quit.setIcon( Qt::Icon::fromTheme("application-exit") )
act_quit.setIconVisibleInMenu true
act_quit.connect(SIGNAL(:triggered)) { @app.quit }
@context_menu.addAction act_quit
end
#
#
#
def build_interface def build_interface
@app = Qt::Application.new(ARGV) @app = Qt::Application.new(ARGV)
@ -62,55 +156,29 @@ module Qasim
dbus_notify "Hello", "World", 'dialog-information' dbus_notify "Hello", "World", 'dialog-information'
si.setToolTip("Qasim %s" % VERSION); si.setToolTip("Qasim %s" % APP_VERSION);
=begin
Qt::Timer.new(@app) do |timer| Qt::Timer.new(@app) do |timer|
timer.connect(SIGNAL('timeout()')) do timer.connect(SIGNAL('timeout()')) do
si.icon = (si.icon.isNull ? std_icon : alt_icon) if blinking si.icon = (si.icon.isNull ? std_icon : alt_icon) if blinking
end end
timer.start(500) timer.start(500)
end end
=end
menu = Qt::Menu.new build_map_menu
build_context_menu
@config.maps.each do |map| si.contextMenu = @context_menu
name = (File.basename map.path).gsub(/\.map$/,'')
itemx = Qt::Action.new(name, menu)
itemx.setCheckable true;
itemx.connect(SIGNAL(:triggered)) do
puts "%s => %s" % [name, itemx.checked ]
map.connect
end
menu.addAction itemx;
end
menu.addSeparator
act_pref = Qt::Action.new _('&Preferences'), menu
act_pref.setIcon( Qt::Icon::fromTheme("configure") )
act_pref.setIconVisibleInMenu true
menu.addAction act_pref;
act_about = Qt::Action.new '&About', menu
act_about.setIcon( Qt::Icon::fromTheme("help-about") )
act_about.setIconVisibleInMenu true
menu.addAction act_about;
menu.addSeparator
act_quit = Qt::Action.new _('Quit'), menu
act_quit.setIcon( Qt::Icon::fromTheme("application-exit") )
act_quit.setIconVisibleInMenu true
act_quit.connect(SIGNAL(:triggered)) { @app.quit }
menu.addAction act_quit
si.contextMenu = menu
si.connect(SIGNAL('activated(QSystemTrayIcon::ActivationReason)')) do |reason| si.connect(SIGNAL('activated(QSystemTrayIcon::ActivationReason)')) do |reason|
case reason case reason
when Qt::SystemTrayIcon::Trigger when Qt::SystemTrayIcon::Trigger
blinking = !blinking build_map_menu
si.icon = blinking ? alt_icon : std_icon @map_menu.popup(Qt::Cursor::pos())
#blinking = !blinking
#si.icon = blinking ? alt_icon : std_icon
when Qt::SystemTrayIcon::MiddleClick: puts 'Middle Click' when Qt::SystemTrayIcon::MiddleClick: puts 'Middle Click'
when Qt::SystemTrayIcon::Context: puts 'Right Click' when Qt::SystemTrayIcon::Context: puts 'Right Click'
when Qt::SystemTrayIcon::DoubleClick: puts 'Double Click' when Qt::SystemTrayIcon::DoubleClick: puts 'Double Click'
@ -118,10 +186,18 @@ module Qasim
end end
end end
#
#
#
def run def run
@app.exec @app.exec
end end
#
#
#
def self.main def self.main
qasim = QasimGui.new qasim = QasimGui.new
qasim.build_interface qasim.build_interface

View file

@ -14,11 +14,7 @@ module Qasim
attr_reader :maps_active attr_reader :maps_active
attr_reader :maps attr_reader :maps
attr_reader :action attr_reader :mnt_dir
ACTION_UMOUNT = :umount
ACTION_MOUNT = :mount
ACTION_INIT = :init
def initialize def initialize
@ -40,7 +36,8 @@ module Qasim
home_dir + '/.config' home_dir + '/.config'
end end
@action = ACTION_MOUNT mnt_dir = File.join home_dir, "mnt"
@config_dir = xdg_dir + '/sshfs-mapper' @config_dir = xdg_dir + '/sshfs-mapper'
@config_file = nil @config_file = nil
@maps = [] @maps = []
@ -51,7 +48,7 @@ module Qasim
@debug = false @debug = false
end end
def parse_file &blk def parse_maps &blk
rdebug "Config: #{@config_dir}/config" rdebug "Config: #{@config_dir}/config"
@maps = [] @maps = []
@ -71,53 +68,5 @@ module Qasim
end end
end end
def parse_cmd_line args
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [action] [options]"
opts.separator ""
opts.separator "Action (mount by default):"
opts.on('-u', '--umount', 'Umount') do |umount|
@action = ACTION_UMOUNT
end
opts.on('-i', '--initialize', 'Populate with default configuration' ) do |init|
@action = ACTION_INIT
end
opts.separator "Specific options:"
opts.on('-a', '--all', 'Targets all enabled maps (disables -s)') do |all|
@targets_all = all
end
#FIXME: use target list there
opts.on('-s', '--select TARGET', 'Target selected map (even disabled)') do |target|
@targets << target
end
opts.on('-v', '--[no-]verbose', 'Run verbosely' ) do |verbose|
@verbose_enable = verbose
end
end
begin
opts.parse! args
rescue OptionParser::ParseError => e
puts opts.to_s
puts ""
puts e.message
exit 1
end
end
def to_s
s = []
s << "config_file = #{@config_file}"
s << "verbose_enable = #{@verbose_enable}"
s.join "\n"
end
end end
end end

View file

@ -43,10 +43,15 @@ module Qasim
linect += 1 linect += 1
while line =~ /\$(.*)/ do while line =~ /\$(.*)/ do
pattern = $1
puts "FOUND PATTERN %s => %s" % [$1, local_env[$1]] puts "FOUND PATTERN %s => %s" % [$1, local_env[$1]]
line.gsub!(/\$#{pattern}/,local_env[$1]) case line
line.gsub!(/\$\{#{pattern}\}/,local_env[$1]) when /\$\{(.+?)\}/ then
pattern = $1
line.gsub!(/\$\{#{attern}\}/,local_env[pattern])
when /\$(\w+)/ then
pattern = $1
line.gsub!(/\$#{pattern}/,local_env[pattern])
end
end end
case line case line
@ -95,7 +100,7 @@ module Qasim
#FIXME test if connected / mounted #FIXME test if connected / mounted
end end
def connect def connect &block
puts "[#{File.basename @path}] Connecting..." puts "[#{File.basename @path}] Connecting..."
puts " #{@user}@#{@host}:#{@port}" puts " #{@user}@#{@host}:#{@port}"
#puts " maps = %s" % @maps.map{ |k,v| "%s => %s" % [ k, v ] }.join(', ') #puts " maps = %s" % @maps.map{ |k,v| "%s => %s" % [ k, v ] }.join(', ')
@ -106,31 +111,35 @@ module Qasim
# FIXME: test connexion with Net::SSH + timeout or ask password # FIXME: test connexion with Net::SSH + timeout or ask password
@maps.each do |name, remotepath| @maps.each do |name, remotepath|
pp map pp map
cmd = [ "sshfs" , localpath = File.join ENV['HOME'], "mnt", name
"-o allow_root" , cmd = "sshfs"
"-o idmap=user" , cmd_args = [
"-o uid=%s" % Process.uid, "-o","allow_root" ,
"-o gid=%s" % Process.gid, "-o","idmap=user" ,
"-o reconnect", "-o","uid=%s" % Process.uid,
"-o workaround=all", "-o","gid=%s" % Process.gid,
"-o cache_timeout=240", "-o","reconnect",
"-o ServerAliveInterval 15", "-o","workaround=all",
"-o no_readahead", "-o","cache_timeout=240",
"-o Ciphers=arcfour", "-o","ServerAliveInterval=15",
"-o Port=%s" % @port, "-o","no_readahead",
"%s@%s:%s:%s" % [@user,@host,@port,remotepath], "-o","Ciphers=arcfour",
"$localdir" ].join(' ') "-o","Port=%s" % @port,
rdebug "command: %s" % cmd "%s@%s:%s" % [@user,@host,remotepath],
=begin localpath ]
system cmd rdebug "command: %s" % [ cmd, cmd_args ].flatten.join(' ')
if block_given? then
yield cmd, cmd_args
else
system cmd, cmd_args
if $?.exitstatus != 0 then if $?.exitstatus != 0 then
raise ConnectError, self raise ConnectError, self
end end
=end end
end end
end end
def disconnect def disconnect &block
puts "Disconnecting map #{@path}" puts "Disconnecting map #{@path}"
# umount # umount
end end