class Skype_lock_logout

A class that connects to the Gnome-Screensaver and listens for locks. When locking the app will log out of Skype. When unlocking the app will lock back into Skype.

Public Class Methods

new() click to toggle source
# File lib/skype_lock_logout.rb, line 27
def initialize
  #Spawn the session-DBus.
  @bus = DBus::SessionBus.instance
  
  #Spawn Skype-DBus objects.
  skype_service = @bus.service("com.Skype.API")
  @skype_obj = skype_service.object("/com/Skype")
  @skype_obj.introspect
  @skype_obj.default_iface = "com.Skype.API"
  
  #Register the application with Skype.
  res = @skype_obj.Invoke("NAME SkypeLockLogout").first
  raise "The application wasnt allowed use Skype: '#{res}'." if res != "OK"
  
  #Set the protocol to 8 - could only find documentation on this protocol. Are there any better ones???
  res = @skype_obj.Invoke("PROTOCOL 8").first
  raise "Couldnt set the protocol for Skype: '#{res}'." if res != "PROTOCOL 8"
  
  #Listen for lock-events on the screensaver.
  mr = DBus::MatchRule.new.from_s("interface='org.gnome.ScreenSaver',member='ActiveChanged'")
  @bus.add_match(mr, &self.method(:gnome_screensaver_status))
  
  #Listen for when Skype stops running to quit listening.
  mr = DBus::MatchRule.new.from_s("path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'")
  @bus.add_match(mr, &self.method(:name_owner_changed))
end
start() click to toggle source

Spawns the object, starts to listen and blocks the thread.

# File lib/skype_lock_logout.rb, line 6
def self.start
  loop do
    print "Starting Skype-lock-logout.\n"
    
    begin
      Skype_lock_logout.new.listen
    rescue => e
      if e.is_a?(DBus::Error) and e.message == "The name com.Skype.API was not provided by any .service files"
        puts "Skype isnt running - trying again in 10 secs."
      elsif e.is_a?(RuntimeError) and e.message == "Skype has stopped running."
        puts "Skype stopped running - trying to reconnect in 10 secs."
      else
        puts e.inspect
        puts e.backtrace
      end
    end
    
    sleep 10
  end
end

Public Instance Methods

gnome_screensaver_status(msg) click to toggle source

This method is called, when the Gnome-Screensaver locks or unlocks.

# File lib/skype_lock_logout.rb, line 60
def gnome_screensaver_status(msg)
  val = msg.params.first
  
  if val == true
    puts "Detected screenlock by Gnome Screensaver - logging out of Skype."
    self.skype_status_offline
  else
    puts "Detected screenlock unlock by Gnome Screensaver - going online on Skype."
    self.skype_status_online
  end
end
listen() click to toggle source

This listens for the connected events and blocks the thread calling it.

# File lib/skype_lock_logout.rb, line 73
def listen
  main = DBus::Main.new
  main << @bus
  main.run
  
  nil
end
name_owner_changed(msg) click to toggle source

This method is being called on 'NameOwnerChanged', which helps us stop the app when Skype stops (a reconnect is needed after Skype restarts).

# File lib/skype_lock_logout.rb, line 55
def name_owner_changed(msg)
  raise "Skype has stopped running." if msg.params.first == "com.Skype.API" and msg.params[1].to_s.match(/^:\d\.\d+$/)
end
skype_status_offline() click to toggle source

Tells Skype to go offline.

# File lib/skype_lock_logout.rb, line 82
def skype_status_offline
  ret = @skype_obj.Invoke("SET USERSTATUS OFFLINE").first
  raise "Couldnt go offline: '#{ret}'." if ret != "USERSTATUS OFFLINE"
  nil
end
skype_status_online() click to toggle source

Tells skype to go online.

# File lib/skype_lock_logout.rb, line 89
def skype_status_online
  ret = @skype_obj.Invoke("SET USERSTATUS ONLINE").first
  raise "Couldnt go online: '#{ret}'." if ret != "USERSTATUS ONLINE"
  nil
end