Starting to implement Connection object and RequestConnection, etc.
[twitterpathy.git] / twitterpathy.rb
blobfc1c83aafb41db2873919f45e98a4970298940f6
1 #!/usr/bin/env ruby
3 require 'rubygems'
4 gem('twitter4r', '>=0.2.1')
5 require 'twitter'
6 require 'dbus'
8 CONN_MGR_PARAM_FLAG_REQUIRED = 1
9 CONN_MGR_PARAM_FLAG_REGISTER = 2
10 CONN_MGR_PARAM_FLAG_HAS_DEFAULT = 4
11 CONN_MGR_PARAM_FLAG_SECRET = 8
13 class Twitterpathy < DBus::Object
14         # Create an interface.
15         dbus_interface "org.ruby.SampleInterface" do
16                 # Create a hello method in that interface.
17                 dbus_method :hello, "in name:s, in name2:s" do |name, name2|
18                         puts "hello(#{name}, #{name2})"
19                 end
20         end
21         
22         dbus_interface "org.freedesktop.Telepathy.ConnectionManager" do
23                 dbus_method :GetParameters, "in proto:s, out parameters:a(susv)" do |proto|
24                         puts "GetParameters(#{proto.inspect})"
25                         $stderr.puts "Attempt to use unsupported protocol #{proto}" if proto != 'twitter'
26                         
27                         parameters = [['account', CONN_MGR_PARAM_FLAG_REQUIRED | CONN_MGR_PARAM_FLAG_REGISTER, 's', ['s', '']], ['password', CONN_MGR_PARAM_FLAG_REQUIRED | CONN_MGR_PARAM_FLAG_REGISTER | CONN_MGR_PARAM_FLAG_SECRET, 's', ['s', '']]]
28                         [parameters]
29                 end
30                 
31                 dbus_method :ListProtocols, "out protocols:as" do ||
32                         puts "ListProtocols"
33                         protocols = ['twitter']
34                         [protocols]
35                 end
36                 
37                 dbus_method :RequestConnection, "in proto:s, in parameters:a{sv}, out service:s, out object:o" do |proto, parameters|
38                         puts "RequestConnection(#{proto.inspect}, #{parameters.inspect})"
39                         $stderr.puts "Attempt to use unsupported protocol #{proto}" if proto != 'twitter'
40                         
41                         object = "/org/freedesktop/Telepathy/Connection/Twitterpathy/twitter/#{parameters['account']}"
42                         service = "org.freedesktop.Telepathy.Connection.Twitterpathy.twitter.#{parameters['account']}"
43                         
44                         #Register a new D-BUS service for this connection
45                         connection_service = $bus.request_service(service)
46                         
47                         #Create the connection object, and export it on the new service
48                         connection = TwitterConnection.new(object, parameters['account'], parameters['password'])
49                         connection_service.export(connection)
50                         
51                         puts "#{service}, #{object}"
52                         [service, object]
53                 end
54         end
55 end
57 class TwitterConnection < DBus::Object
58         def initialize(path, account, password)
59                 puts "initialize TwitterConnection(#{path}, #{account}, ***)"
60                 @account = account
61                 @password = password
62                 super(path)
63         end
64         
65         dbus_interface 'org.freedesktop.Telepathy.Connection' do
66                 dbus_method :Connect, '' do ||
67                         puts "Connect, account=#{@account}"
68                         @twitter_client = Twitter::Client.new(:login => $account, :password => $password)
69                 end
70                 
71                 dbus_method :Disconnect, '' do ||
72                 end
73                 
74                 dbus_method :GetInterfaces, 'out interfaces:as' do ||
75                         interfaces = []
76                         [interfaces]
77                 end
78                 
79                 dbus_method :GetProtocol, 'out protocol:s' do ||
80                         protocol = 'twitter'
81                         [protocol]
82                 end
83         end
84 end
86 $bus = DBus::SessionBus.instance
88 service = $bus.request_service("org.freedesktop.Telepathy.ConnectionManager.Twitterpathy")
90 exported_obj = Twitterpathy.new("/org/freedesktop/Telepathy/ConnectionManager/Twitterpathy")
91 service.export(exported_obj)
93 puts "listening"
94 main = DBus::Main.new
95 main << $bus
96 main.run