~jezra/heybuddy/stable

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import gtk
import gobject
from DentScroller import DentScroller,has_hildon
from Dent import Dent
from UserPage import UserPage
from Brand import Brand
from GroupPage import GroupPage
try:
	import gtkspell
	has_gtkspell=True
except:
	has_gtkspell=False
#make a fake enumeration
DENT,MENTION,DIRECT,CONVERSATION,USER,GROUP,ACCOUNT = range(7)
STATUS_MESSAGE,ERROR_MESSAGE = range(2)
class MainWindow(gtk.Window,gobject.GObject):
	__gsignals__ = {
		'update-status': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						   (gobject.TYPE_STRING,gobject.TYPE_INT)
			),
		'update-account': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						   (gobject.TYPE_STRING,gobject.TYPE_STRING)
			),
		'get-conversation': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						   (gobject.TYPE_INT,)
			),
		'show-user': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						   (gobject.TYPE_STRING,)
			),
		'show-group': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						   (gobject.TYPE_STRING,)
			),
		'notebook-page-change': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						   (gobject.TYPE_INT,gobject.TYPE_INT)
			),
		'follow-user' : (
			gobject.SIGNAL_RUN_FIRST,gobject.TYPE_NONE,
				(gobject.TYPE_STRING,gobject.TYPE_BOOLEAN)
			),
		'join-group' : (
			gobject.SIGNAL_RUN_FIRST,gobject.TYPE_NONE,
				(gobject.TYPE_STRING,gobject.TYPE_BOOLEAN)
			)
	}
	def __init__(self,app_name):
		gtk.Window.__init__(self,gtk.WINDOW_TOPLEVEL)	
		gobject.GObject.__init__(self)
		self.do_submit=False
		self.respond_to_id=0
		self.is_fullscreen=False
		self.set_title(app_name)
		self.set_default_size(500,500)
		self.notebook_current_page=DENT
		self.string_update_status="Update Status"
		self.conversation_page=None
		self.pre_conversation_page=None
		self.user_page=None
		self.pre_user_page=None
		self.pre_group_page=None
		#create an accellerator group for this window
		accel = gtk.AccelGroup()
		#add the ctrl+q to quit
		accel.connect_group(gtk.keysyms.q, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.quit )
		#lock the group
		accel.lock()
		#add the group to the window
		self.add_accel_group(accel)
		#start with vbox
		self.main_vbox = gtk.VBox()
		#add the vbox to the self
		self.add(self.main_vbox)
		#make a notebook
		self.notebook = gtk.Notebook()
		#pack the note book in the vbox
		self.main_vbox.pack_start(self.notebook)
		#make a scroller for statuses
		self.dent_scroll = DentScroller()
		#add the dent scroller to the notebook
		self.notebook.append_page(self.dent_scroll,gtk.Label("Dents"))
		#make the update status area
		self.update_textbuffer = gtk.TextBuffer()
		self.update_textview = gtk.TextView(self.update_textbuffer)
		#the text view needs to be multiline
		self.update_textview.set_wrap_mode(gtk.WRAP_WORD)
		#try and add spell checking to the textview
		if has_gtkspell:
			self.spell = gtkspell.Spell(self.update_textview)
		#add a shadowed frame around the textview
		tvframe = gtk.Frame();
		tvframe.set_shadow_type(gtk.SHADOW_IN)
		tvframe.add(self.update_textview)
		#what happens when text is inserted in the buffer?
		self.update_textbuffer.connect("insert-text", self.update_textbuffer_insert)
		#what happens when the buffer is changed?
		self.update_textbuffer.connect("changed", self.update_textbuffer_changed)
		update_frame = gtk.Frame()
		self.update_status_label = gtk.Label(self.string_update_status)
		update_frame.add(tvframe)
		update_frame.set_label_widget(self.update_status_label)
		self.main_vbox.pack_start(update_frame,False,False,0)
		#make a message label that we might need to display to the user
		self.message_label = gtk.Label()
		self.main_vbox.pack_start(self.message_label,False,False,0)
		
		#do the mentions
		self.mention_scroll = DentScroller()
		self.notebook.append_page(self.mention_scroll,gtk.Label("Mentions"))
		
		#do direct messaging
		self.direct_scroll = DentScroller()
		self.notebook.append_page(self.direct_scroll,gtk.Label("Direct"))
		
		#create the conversation stuff
		self.conversation_vbox = gtk.VBox(False,0)
		self.conversation_scroll = DentScroller()
		self.conversation_vbox.pack_start(self.conversation_scroll,True,True,0)
		#make a close button for the conversation thing
		conversation_close_button=gtk.Button("Close")
		hbox = gtk.HBox(False)
		hbox.pack_start(conversation_close_button,False,False,0)
		conversation_close_button.connect("clicked",self.close_conversation)
		self.conversation_vbox.pack_start(hbox,False,False,0)
		#add the page
		self.notebook.append_page(self.conversation_vbox,gtk.Label("Context") )
		
		#create a user page
		self.userpage=UserPage()
		self.userpage.connect('user-page-hide',self.userpage_hide)
		self.userpage.connect('follow-user',self.follow_user)
		#add the userpage
		self.notebook.append_page(self.userpage,gtk.Label("User") )
		#create a user dent scroller and add it to the user page
		self.user_scroll = DentScroller()
		self.userpage.pack_start(self.user_scroll)
		
		#create a Group page
		self.grouppage=GroupPage()
		self.grouppage.connect('group-page-hide',self.grouppage_hide)
		self.grouppage.connect('join-group',self.join_group)
		#add the Grouppage
		self.notebook.append_page(self.grouppage,gtk.Label("Group") )
		#create a group dent scroller and add it to the group page
		self.group_scroll = DentScroller()
		self.grouppage.pack_start(self.group_scroll)
		
		#we need an account page in the notebook
		#start with a table
		table = gtk.Table(4,2)
		table.attach(gtk.Label("Name:"),0,1,0,1,gtk.SHRINK,gtk.SHRINK)
		table.attach(gtk.Label("Password:"),0,1,1,2,gtk.SHRINK,gtk.SHRINK)
		self.account_name_entry = gtk.Entry()
		self.account_password_entry = gtk.Entry()
		self.account_password_entry.set_invisible_char("*")
		self.account_password_entry.set_visibility(False)
		#add the entries to the table
		table.attach(self.account_name_entry,1,2,0,1,gtk.SHRINK,gtk.SHRINK)
		table.attach(self.account_password_entry,1,2,1,2,gtk.SHRINK,gtk.SHRINK)
		#make a button to register "submit clicks
		enter_button = gtk.Button("Submit")
		enter_button.connect("clicked", self.submit_clicked)
		#attach the button to the table
		table.attach(enter_button,1,2,2,3,gtk.SHRINK,gtk.SHRINK)
		#make the brand
		brand = Brand()
		table.attach(brand,0,2,3,4,gtk.SHRINK,gtk.SHRINK,15,15)
		self.notebook.append_page(table,gtk.Label("Account"))
		#check for a keypress
		self.connect('key-press-event',self.keypressed)
		self.show_all()
		#hide *some* stuff
		self.message_label.hide()
		self.conversation_vbox.hide_all()
		self.userpage.hide_all()
		self.grouppage.hide_all()
		
	def keypressed(self,widget,event):
		if event.keyval == 65475:
			self.toggle()
			
	def toggle(self):
		if self.is_fullscreen :
			self.unfullscreen()
			self.is_fullscreen=False
		else:
			self.fullscreen()
			self.is_fullscreen=True
	
	def run(self):
		#we don't want any signals until the app is running
		self.notebook.connect("switch-page", self.notebook_page_switch )
		
	def get_target_scroller(self,index):
		if index == DENT:
			target = self.dent_scroll
		elif index == MENTION:
			target = self.mention_scroll
		elif index == DIRECT:
			target = self.direct_scroll
		elif index == CONVERSATION:
			target = self.conversation_scroll
		elif index == GROUP:
			target = self.group_scroll
		elif index==USER:
			target = self.user_scroll
		return target
	
	def set_notebook_page(self,index):
		self.notebook.set_current_page(index)
		
	def get_notebook_index(self):
		return self.notebook.get_current_page()
		
	def add_dent(self,data,target_index,is_conv=False):
		d = Dent(data)
		d.connect("reply-clicked", self.reply_clicked)
		d.connect("view-conversation-clicked", self.view_conversation)
		d.connect("user-clicked", self.view_user)
		d.connect("group-clicked", self.view_group)
		#add this to the dent it's targeting
		target = self.get_target_scroller(target_index)
		target.add_dent(d,is_conv)	
		#return the dent
		return d
	
	def show_user_page(self):
		self.pre_user_page=self.notebook_current_page
		self.userpage.show_all()
		self.notebook.set_current_page(USER)
		#disable the user page
		self.userpage.set_sensitive(False)
		
	def show_group_page(self):
		self.pre_group_page=self.notebook_current_page
		self.grouppage.show_all()
		self.notebook.set_current_page(GROUP)
		#disable the user page
		self.grouppage.set_sensitive(False)
		
	def display_user(self,data):
		self.userpage.set_user_data(data)
		
		self.user_data_retrieved = True
		if (self.user_is_friend_retrieved):
			self.userpage.set_sensitive(True)
			
	def display_user_is_friend(self,bool):
		self.user_is_friend_retrieved = True
		self.userpage.set_is_friend(bool)
		if self.user_data_retrieved:
			self.userpage.set_sensitive(True)
			
	def display_group(self,data):
		self.grouppage.set_group_data(data)
			
	def display_user_is_member(self,bool):
		self.grouppage.set_is_member(bool)
		self.grouppage.set_sensitive(True)
			
	def update_textbuffer_insert(self,textbuffer, iter, text, length):
		if text=="\n" :
			self.do_submit=True
	
	def emit_update_status(self):
		#get the text from the buffer
		start_iter = self.update_textbuffer.get_start_iter()
		end_iter = self.update_textbuffer.get_end_iter()
		text=self.update_textbuffer.get_text(start_iter,end_iter).strip()
		if(text!=""):
			#is this in response to a dent?
			#for some reason self.respond_to_id is not longer an int
			id = int(self.respond_to_id)
			self.emit("update-status",text,id)
			#detach the spellchecker
			if has_gtkspell:
				self.spell.detach()
			#clear the text area
			self.update_textbuffer.set_text("")
			#reattach the spellchecker
			if has_gtkspell:
				self.spell = gtkspell.Spell(self.update_textview)
			
	def update_textbuffer_changed(self,textbuffer):
		#get the last char of the text buffer
		if self.do_submit:
			self.emit_update_status()
			self.do_submit=False

		char_count = textbuffer.get_char_count()
		self.string_update_status_append_count(140-char_count)
		if (char_count > 140 ):
			start_iter = textbuffer.get_iter_at_offset(140)
			end_iter = textbuffer.get_end_iter()
			textbuffer.delete(start_iter,end_iter)
		elif(char_count==0):
			#reset the respond_to_id
			self.respond_to_id=0

	def notebook_page_switch(self,nb,page,page_num):
		old_page = self.notebook_current_page
		self.notebook_current_page=page_num
		self.emit('notebook-page-change',old_page,page_num)

	def submit_clicked(self,button):
		#get the name and password
		n=self.account_name_entry.get_text()
		p=self.account_password_entry.get_text()
		if n!="" and p!="" :
			self.emit("update-account",n,p)
			#hide the message_label
			self.message_label.hide()

	def reply_clicked(self,DentScroller,id,name):
		self.respond_to_id=id
		#set the text buffer
		self.update_textbuffer.set_text("@%s " % (name) )
		self.update_textview.grab_focus()
		#set the respond to id
	
	def string_update_status_append_count(self,count):
		if count<140:
			string = "%s %d" % (self.string_update_status,count)
		else:
			string = self.string_update_status
		self.update_status_label.set_text(string)
	
	def set_message(self,message,type=STATUS_MESSAGE):
		if type==ERROR_MESSAGE:
			markup = "<span color='#dd0000'>%s</span>" % message
		else:
			markup = message
		self.message_label.set_markup(markup)
		#show the self.message label
		self.message_label.show()
	
	def view_conversation(self,dent,id):
		self.pre_conversation_page=self.notebook_current_page
		self.conversation_scroll.clear()
		self.conversation_vbox.show_all()
		#get conversation starting with id
		self.emit('get-conversation',id)
		self.notebook.set_current_page(CONVERSATION)
		
	def close_conversation(self,widget):
		self.conversation_vbox.hide_all()
		self.notebook.set_current_page(self.pre_conversation_page)
	
	def view_user(self,widget,screen_name):
		#make some flags
		self.user_data_retrieved = False
		self.user_is_friend_retrieved = False
		self.user_scroll.clear()
		self.show_user_page()
		self.emit('show-user',screen_name)
	
	def view_group(self,widget,group_name):
		#make some flags
		self.group_data_retrieved = False
		self.group_is_friend_retrieved = False
		self.group_scroll.clear()
		self.show_group_page()
		self.emit('show-group',group_name)
	
	def quit(self,accel_group, acceleratable, keyval, modifier):
		self.emit("destroy")
	
	def userpage_hide(self,widget):
		self.userpage.hide_all()
		self.set_notebook_page(self.pre_user_page)
	
	def grouppage_hide(self,widget):
		self.grouppage.hide_all()
		self.set_notebook_page(self.pre_group_page)
	
	def follow_user(self,widget,name,bool):
		self.emit('follow-user',name,bool)
	
	def join_group(self,widget,name,bool):
		self.emit('join-group',name,bool)