diff --git a/lib/Cli.rb b/lib/Cli.rb index f848457..0d426a9 100644 --- a/lib/Cli.rb +++ b/lib/Cli.rb @@ -29,32 +29,35 @@ class Cli end # Sync the data with the server + # @rtnr: true if the synchro is finish def sync() if !defined?(@sync) @sync = Sync.new() - if @config.sync_host.nil? || @config.sync_port.nil? - @sync.disable() - elsif !@sync.connect(@config.sync_host, @config.sync_port, @config.key, @config.sync_pwd, @config.sync_suffix) - puts "#{I18n.t('sync.not_connect')}:\n#{@sync.error_msg}" + if !@config.sync_host.nil? && !@config.sync_port.nil? + if !@sync.connect(@config.sync_host, @config.sync_port, @config.key, @config.sync_pwd, @config.sync_suffix) + puts "#{I18n.t('display.error')}: #{@sync.error_msg}" + end end end begin - if !@mpw.sync(@sync.get(@passwd), @config.last_update) - puts "#{I18n.t('display.error')}: #{@mpw.error_msg}" - end - - if !@sync.update(File.open(@config.file_gpg).read) - puts "#{I18n.t('display.error')}: #{@sync.error_msg}" - end - - if !@config.setLastUpdate() - puts "#{I18n.t('display.error')}: #{@config.error_msg}" + if @sync.enable + if !@mpw.sync(@sync.get(@passwd), @config.last_update) + puts "#{I18n.t('display.error')}: #{@mpw.error_msg}" + elsif !@sync.update(File.open(@config.file_gpg).read) + puts "#{I18n.t('display.error')}: #{@sync.error_msg}" + elsif !@config.setLastUpdate() + puts "#{I18n.t('display.error')}: #{@config.error_msg}" + else + return true + end end rescue Exception => e - puts "#{I18n.t('sync.error')}:\n#{e}" + puts "#{I18n.t('display.error')}: #{I18n.t('sync.error')}\n#{e}" end + + return false end # Create a new config file diff --git a/lib/Sync.rb b/lib/Sync.rb index 2fa8ec7..9969ea5 100644 --- a/lib/Sync.rb +++ b/lib/Sync.rb @@ -13,15 +13,12 @@ require "#{APP_ROOT}/lib/MPW.rb" class Sync attr_accessor :error_msg + attr_accessor :enable # Constructor def initialize() @error_msg = nil - end - - # Disable the sync - def disable() - @sync = false + @enable = false end # Connect to server @@ -37,21 +34,21 @@ class Sync @suffix = suffix begin - @socket= TCPSocket.new(host, port) - @sync = true + @socket = TCPSocket.new(host, port) + @enable = true rescue Exception => e @error_msg = "#{I18n.t('error.sync.connection')}\n#{e}" - @sync = false + @enable = false end - return @sync + return @enable end # Get data on server # @args: gpg_password -> the gpg password # @rtrn: nil if nothing data or error def get(gpg_password) - if !@sync + if !@enable return nil end @@ -91,7 +88,7 @@ class Sync # @args: data -> the data to send on server # @rtrn: false if there is a problem def update(data) - if !@sync + if !@enable return true end @@ -121,7 +118,7 @@ class Sync # Close the connection def close() - if !@sync + if !@enable return end