Single instance lock is now functional.

This commit is contained in:
Glenn Y. Rolland 2011-09-04 05:06:23 +02:00
parent d3d12be208
commit 4df09954fa

View file

@ -144,13 +144,13 @@ module Qasim
@context_menu = Qt::Menu.new @context_menu = Qt::Menu.new
act_pref = Qt::Action.new _('&Preferences'), @context_menu act_pref = Qt::Action.new _('&Preferences'), @context_menu
act_pref.setIcon( Qt::Icon::fromTheme("configure") ) act_pref.setIcon( Qt::Icon::fromTheme("configure") ) rescue nil
act_pref.setIconVisibleInMenu true act_pref.setIconVisibleInMenu true
act_pref.setEnabled false act_pref.setEnabled false
@context_menu.addAction act_pref; @context_menu.addAction act_pref;
act_about = Qt::Action.new '&About', @context_menu act_about = Qt::Action.new '&About', @context_menu
act_about.setIcon( Qt::Icon::fromTheme("help-about") ) act_about.setIcon( Qt::Icon::fromTheme("help-about") ) rescue nil
act_about.setIconVisibleInMenu true act_about.setIconVisibleInMenu true
act_about.setEnabled false act_about.setEnabled false
@context_menu.addAction act_about; @context_menu.addAction act_about;
@ -158,7 +158,7 @@ module Qasim
@context_menu.addSeparator @context_menu.addSeparator
act_quit = Qt::Action.new _('Quit'), @context_menu act_quit = Qt::Action.new _('Quit'), @context_menu
act_quit.setIcon( Qt::Icon::fromTheme("application-exit") ) act_quit.setIcon( Qt::Icon::fromTheme("application-exit") ) rescue nil
act_quit.setIconVisibleInMenu true act_quit.setIconVisibleInMenu true
act_quit.connect(SIGNAL(:triggered)) { @app.quit } act_quit.connect(SIGNAL(:triggered)) { @app.quit }
@context_menu.addAction act_quit @context_menu.addAction act_quit
@ -213,24 +213,22 @@ module Qasim
# create lock # create lock
have_lock = true have_lock = true
lockfname = APP_CONFIG_DIR, "lock" FileUtils.mkdir_p APP_CONFIG_DIR unless File.exist? APP_CONFIG_DIR
File.open lockfname, "w" do lockfname = File.join APP_CONFIG_DIR, "lock"
unless f.flock File::LOCK_EX | File::LOCK_NB fd = IO::sysopen( lockfname,
warn "Another instance of %s is already running." % APP_NAME
exit 1
end
f.flock File::LOCK_EX
end
fd = IO::sysopen('/tmp/tempfile',
Fcntl::O_WRONLY | Fcntl::O_EXCL | Fcntl::O_CREAT) Fcntl::O_WRONLY | Fcntl::O_EXCL | Fcntl::O_CREAT)
f = IO.open(fd) f = IO.open(fd)
f.syswrite("TEMP DATA") f.syswrite( "Process.pid\n" )
f.close f.close
masterpid = fh.gets
@app.exec @app.exec
rescue Errno::EEXIST => e
warn "error: Another instance of %s is already running."
exit 1
ensure ensure
fh.close masterpid = File.read(lockfname).strip
if masterpid == Process.pid then
FileUtils.rm lockfname
end
end end