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:
Glenn Y. Rolland 2011-07-24 15:41:33 +00:00
parent bc7293ecea
commit cf584cc633
3 changed files with 152 additions and 118 deletions

View file

@ -23,8 +23,10 @@ module Qasim
def initialize
@config = Config.new
@config.parse_cmd_line ARGV
@config.parse_file
#@config.parse_cmd_line ARGV
@map_menu = nil
@context_menu = nil
end
def dbus_notify title, body, icon
@ -48,6 +50,98 @@ module Qasim
# "Sorry dude", 2, 5000 )
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
@app = Qt::Application.new(ARGV)
@ -62,55 +156,29 @@ module Qasim
dbus_notify "Hello", "World", 'dialog-information'
si.setToolTip("Qasim %s" % VERSION);
si.setToolTip("Qasim %s" % APP_VERSION);
=begin
Qt::Timer.new(@app) do |timer|
timer.connect(SIGNAL('timeout()')) do
si.icon = (si.icon.isNull ? std_icon : alt_icon) if blinking
end
timer.start(500)
end
=end
menu = Qt::Menu.new
build_map_menu
build_context_menu
@config.maps.each do |map|
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.contextMenu = @context_menu
si.connect(SIGNAL('activated(QSystemTrayIcon::ActivationReason)')) do |reason|
case reason
when Qt::SystemTrayIcon::Trigger
blinking = !blinking
si.icon = blinking ? alt_icon : std_icon
build_map_menu
@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::Context: puts 'Right Click'
when Qt::SystemTrayIcon::DoubleClick: puts 'Double Click'
@ -118,10 +186,18 @@ module Qasim
end
end
#
#
#
def run
@app.exec
end
#
#
#
def self.main
qasim = QasimGui.new
qasim.build_interface

View file

@ -14,11 +14,7 @@ module Qasim
attr_reader :maps_active
attr_reader :maps
attr_reader :action
ACTION_UMOUNT = :umount
ACTION_MOUNT = :mount
ACTION_INIT = :init
attr_reader :mnt_dir
def initialize
@ -40,7 +36,8 @@ module Qasim
home_dir + '/.config'
end
@action = ACTION_MOUNT
mnt_dir = File.join home_dir, "mnt"
@config_dir = xdg_dir + '/sshfs-mapper'
@config_file = nil
@maps = []
@ -51,7 +48,7 @@ module Qasim
@debug = false
end
def parse_file &blk
def parse_maps &blk
rdebug "Config: #{@config_dir}/config"
@maps = []
@ -71,53 +68,5 @@ module Qasim
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

View file

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