KDE-Support: KDE-JobViewer-DBUS-Service. Registration and usable-checks for progressbar-handler.

This commit is contained in:
Denis Knauf 2018-12-28 12:28:41 +01:00
parent 13106d5fdf
commit 5178dac9ac

View file

@ -1,13 +1,20 @@
require 'dbus'
module ProgressBar
def self.new *a
if STDERR.tty?
Console.new *a
elsif 'KDE' == ENV['XDG_CURRENT_DESKTOP']
KDialog.new *a
else
KDialog.new *a
@klasses ||= []
class <<self
def register klass
if klass.possible?
@klasses.push klass
true
else
false
end
end
def new *a, **o
klass = @klasses.find &:possible?
klass.new *a, **o
end
end
end
@ -15,7 +22,7 @@ end
class ProgressBar::Base
attr_reader :max, :i, :text, :error
attr_accessor :start
def initialize max = nil, text = nil
def initialize max = nil, text = nil, **_options
@start, @max, @i, @text, @error = Time.now, max || 100, 0, text || '', nil
at_exit {finish}
end
@ -54,9 +61,14 @@ class ProgressBar::Base
def change_text() end
def change_error() end
def finish() end
def self.possible?() false end
end
class ProgressBar::Console < ProgressBar::Base
def self.possible?
STDERR.tty?
end
def initialize *a
super *a
change_text
@ -89,15 +101,61 @@ class ProgressBar::Console < ProgressBar::Base
end
end
class ProgressBar::KDialog < ProgressBar::Base
attr_reader :dialog_service_path, :dialog_object_path, :errors, :dialog_object
def initialize max, text = nil, title = nil
class ProgressBar::KDE < ProgressBar::Base
def self.possible?
'KDE' == ENV['XDG_CURRENT_DESKTOP']
end
attr_reader :bus, :service, :view
def initialize max, text = nil, title = nil, app_name: nil, app_icon_name: nil, unit: nil, is_cancable: nil, is_suspendable: nil, unit: nil, **_options
super max, text
text = @text
text = nil if text.nil? or text.empty?
text = nil if text.nil? or text.empty?
title = nil if title.nil? or title.empty?
@unit = '' if unit.nil? or unit.empty?
@errors = []
@bus = DBus::SessionBus.instance
@service = @bus['org.kde.JobViewServer']
jvs = @service['/JobViewServer']['org.kde.JobViewServer']
flags = (is_cancable ? 0x1 : 0) | (is_suspendable ? 0x2 : 0)
view_path = jvs.requestView app_name || $0, app_icon_name || '', flags
@view = @service[view_path]['org.kde.JobViewV2']
@view.setTotalAmount max, unit
end
def change_text
@view.setInfoMessage text
end
def change_progress
@view.setProcessedAmount i, unit
@view.setPercentage done_rel
end
def change_error
@view.setError error
end
def finish
@view.terminate
end
end
class ProgressBar::KDialog < ProgressBar::Base
def self.possible?
path = Pathname.new `which kdialog`.chomp
$?.exitstatus and path.executable?
end
attr_reader :dialog_service_path, :dialog_object_path, :errors, :dialog_object
def initialize max, text = nil, title = nil, **_options
super max, text
text = @text
text = nil if text.nil? or text.empty?
title = nil if title.nil? or title.empty?
@errors = []
args = ['kdialog', *(title ? ['--title', title] : []), '--progressbar', text || '.', max.to_s]
args = ['kdialog', title ? ['--title', title] : [], '--progressbar', text || '.', max].flatten.compact.map &:to_s
@dialog_service_path, @dialog_object_path = IO.popen( args, 'r', &:readlines).join("\n").split ' '
@dialog_bus = DBus.session_bus
@dialog_service = @dialog_bus[@dialog_service_path]
@ -148,3 +206,7 @@ class ProgressBar::KDialog < ProgressBar::Base
@dialog_object.Get '', 'maximum'
end
end
ProgressBar.register ProgressBar::Console
ProgressBar.register ProgressBar::KDE
ProgressBar.register ProgressBar::KDialog