This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
indicator-applet-skype.py
executable file
·218 lines (171 loc) · 6.53 KB
/
indicator-applet-skype.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
#
#Copyright 2010 Andreas Happe
#
#Authors:
# Andreas Happe <andreashappe@snikt.net>
#
#This program is free software: you can redistribute it and/or modify it
#under the terms of either or both of the following licenses:
#
#1) the GNU Lesser General Public License version 3, as published by the
#Free Software Foundation; and/or
#2) the GNU Lesser General Public License version 2.1, as published by
#the Free Software Foundation.
#
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranties of
#MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
#PURPOSE. See the applicable version of the GNU Lesser General Public
#License for more details.
#
#You should have received a copy of both the GNU Lesser General Public
#License version 3 and version 2.1 along with this program. If not, see
#<http://www.gnu.org/licenses/>
#
# Documentation:
# just start it
import indicate
import gobject
import gtk
import Skype4Py
import os
import commands
import time
def do_nothing(indicator):
True
# this is the high-level notification functionality
class NotificationServer:
def __init__(self):
self.server = indicate.indicate_server_ref_default()
self.server.set_type("message.im")
# this is kinda ugly, or?
self.server.set_desktop_file("/usr/share/applications/skype.desktop")
self.server.show()
pass
def connect(self, skype):
self.skype = skype
self.server.connect("server-display", self.on_click)
def on_click(self, server,data=None):
self.skype.skype.Client.Focus()
def activate_timeout_check(self):
gobject.timeout_add_seconds(5, self.skype.check_timeout, self.server)
def show_conversation(self, indicator, timestamp):
display_name = indicator.get_property("name")
self.skype.remove_conversation(display_name)
# this might blow up.. don't know why, seems like an error within skype
self.skype.show_chat_windows(display_name);
# this is needed, cause otherwise the skype menu is only showed
# when there are new unread messages.. another workaround
def workaround_show_skype():
indicator = indicate.Indicator()
indicator.set_property("name", "workaround..")
indicator.connect("user-display", self.show_conversation)
indicator.show()
indicator.hide()
def show_indicator(self, conversation):
print "adding " + conversation.display_name
try:
# Ubuntu 9.10 and above
indicator = indicate.Indicator()
except:
# Ubuntu 9.04
indicator = indicate.IndicatorMessage()
indicator.set_property("name", conversation.display_name)
indicator.set_property("subtype", "instant")
indicator.set_property('draw-attention', 'true');
# we can only display timestamp OR count
if conversation.count == 1:
indicator.set_property_time('time', conversation.timestamp)
else:
indicator.set_property('count', str(conversation.count));
indicator.connect("user-display", self.show_conversation)
indicator.show()
# TODO: why?
gobject.timeout_add_seconds(5, do_nothing, indicator)
class UnreadConversation:
def __init__(self, display_name, timestamp, skype_id):
self.display_name = display_name
self.count = 0
self.timestamps = [timestamp]
self.timestamp=timestamp
def add_timestamp(self, timestamp):
if not timestamp in self.timestamps:
self.timestamps.append(timestamp)
self.count += 1
class SkypeBehaviour:
# initialize skype
def __init__(self):
self.skype = Skype4Py.Skype();
self.skype.OnAttachmentStatus = self.OnAttach;
self.skype.OnMessageStatus = self.OnMessageStatus;
self.skype.Attach();
self.name_mappings = {}
self.unread_conversations = {}
self.cb_show_conversation = None
self.cb_show_indicator = None
def SetShowConversationCallback(self, func):
self.cb_show_conversation = func
def SetShowIndicatorCallback(self, func):
self.cb_show_indicator = func
def OnAttach(self, status):
if status == Skype4Py.apiAttachAvailable:
self.skype.Attach();
if status == Skype4Py.apiAttachSuccess:
print 'connected to skype!'
def OnMessageStatus(self, mesg, Status):
print 'message status\n'
if Status == 'RECEIVED':
print(mesg.FromDisplayName + "sent a message")
display_name = mesg.FromDisplayName
if not display_name in self.unread_conversations:
conversation = UnreadConversation(display_name, mesg.Timestamp, mesg.Sender.Handle)
self.name_mappings[display_name] = mesg.Sender.Handle
# TODO: should we do some sort of update for this?
self.unread_conversations[display_name] = conversation
else:
self.unread_conversations[display_name].add_timestamp(mesg.Timestamp)
self.cb_show_indicator(conversation)
def remove_conversation(self, display_name):
skype_name = self.name_mappings[display_name]
del self.unread_conversations[display_name]
return skype_name
# update unread message collection
# TODO: also remove messages that have been read through skype, otherwise
# they would stay until somebody clicks them in the indicator-applet
def check_timeout(self, server):
print "timeout.\n"
for mesg in self.skype.MissedMessages:
display_name = mesg.FromDisplayName
if not display_name in self.unread_conversations:
conversation = UnreadConversation(display_name, mesg.Timestamp, mesg.Sender.Handle)
self.name_mappings[display_name] = mesg.Sender.Handle
# TODO: should we do some sort of update for this?
self.unread_conversations[display_name] = conversation
else:
self.unread_conversations[display_name].add_timestamp(mesg.Timestamp)
conversation = self.unread_conversations[display_name]
self.cb_show_indicator(conversation)
return True
def show_chat_windows(self, skype_name):
self.skype.Client.OpenMessageDialog(self.name_mappings[skype_name]);
if __name__ == "__main__":
output = commands.getoutput('ps -A | grep skype' )
if 'skype' in output:
print "Skype already running"
else:
print "Starting up Skype"
os.system("skype &")
print "Waiting 5 seconds..."
time.sleep(5)
skype = SkypeBehaviour();
server = NotificationServer()
skype.SetShowConversationCallback(server.show_conversation)
skype.SetShowIndicatorCallback(server.show_indicator)
server.connect(skype)
#workaround_show_skype()
# why is this needed?
server.activate_timeout_check()
# check for newly unread messages..
skype.check_timeout(server)
gtk.main()