Class: Skype_lock_logout

Inherits:
Object
  • Object
show all
Defined in:
lib/skype_lock_logout.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSkype_lock_logout

Returns a new instance of Skype_lock_logout.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Class Method Details

.startObject

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#gnome_screensaver_status(msg) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
# 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

#listenObject

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



73
74
75
76
77
78
79
# File 'lib/skype_lock_logout.rb', line 73

def listen
  main = DBus::Main.new
  main << @bus
  main.run
  
  nil
end

#name_owner_changed(msg) ⇒ Object

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



55
56
57
# 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_offlineObject

Tells Skype to go offline.



82
83
84
85
86
# 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_onlineObject

Tells skype to go online.



89
90
91
92
93
# 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