~muelnet/heybuddy/pulltimes

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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
'''
this is a part of the heybuddy project
copyright 2010 jezra lickter http://www.jezra.net
'''
import gtk
import gobject
from DentScroller import DentScroller
from Dent import Dent
try:
	import gtkspell
	has_gtkspell=True
except:
	has_gtkspell=False

from PlatformSpecific import has_hildon
#make a fake enumeration
DENT,MENTION,DIRECT,CONVERSATION,USER,GROUP,ACCOUNT,ABOUT = range(8)
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,)
			),
		'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)
			),
		'add-tag-filter': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 (gobject.TYPE_STRING,)
			),
		'remove-tag-filter' : (
			gobject.SIGNAL_RUN_FIRST,gobject.TYPE_NONE,
				(gobject.TYPE_STRING,)
			),
		'add-user-filter': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 (gobject.TYPE_STRING,)
			),
		'remove-user-filter' : (
			gobject.SIGNAL_RUN_FIRST,gobject.TYPE_NONE,
				(gobject.TYPE_STRING,)
			),
		'add-group-filter': (
			gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 (gobject.TYPE_STRING,)
			),
		'remove-group-filter' : (
			gobject.SIGNAL_RUN_FIRST,gobject.TYPE_NONE,
				(gobject.TYPE_STRING,)
			),
		'clear-respond-to-id' : (
			gobject.SIGNAL_RUN_FIRST,gobject.TYPE_NONE,
				()
			),
		'quit' : (
			gobject.SIGNAL_RUN_FIRST,gobject.TYPE_NONE,
				()
			),
		'window-to-tray' : (
			gobject.SIGNAL_RUN_FIRST,gobject.TYPE_NONE,
				()
			)		 
	}
	def __init__(self,app_name,version,branch):
		gtk.Window.__init__(self,gtk.WINDOW_TOPLEVEL)	
		gobject.GObject.__init__(self)
		self.textlimit = 140 #this is default
		self.last_clicked_label=None
		self.do_submit=False
		self.is_fullscreen=False
		self.set_title(app_name)
		self.set_default_size(500,500)
		self.notebook_current_page=DENT
		self.string_update_textview=_("Update Status")
		self.string_direct_message=_("Direct Message to")
		self.string_status_frame=self.string_update_textview
		self.conversation_page=None
		self.pre_conversation_page=None
		self.user_page=None
		self.pre_user_page=None
		self.pre_group_page=None
		self.is_direct_message_mode=False
		#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 )
		#add ctrl+w to set tray app and close to tray
		accel.connect_group(gtk.keysyms.w, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.window_to_tray )
		#add the ctrl+c to copy
		accel.connect_group(gtk.keysyms.c, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.copy )
		#add the ctrl+Page_Up and ctrl Page_Down to notebook focus
		accel.connect_group(gtk.keysyms.Page_Up, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.focus_notebook )
		accel.connect_group(gtk.keysyms.Page_Down, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.focus_notebook )
		#set focus for ctrl+u		
		accel.connect_group(gtk.keysyms.u, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.focus_update_textview )
		#set focus for ctrl+l		
		accel.connect_group(gtk.keysyms.l, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.focus_update_textview)
		#lock the group
		accel.lock()
		#add the group to the window
		self.add_accel_group(accel)
		
		''' make the GUI stuff '''
		#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 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_CHAR)
		#try and add spell checking to the textview
		if has_gtkspell:
			#what if there is no aspell library?
			try:
				self.spell = gtkspell.Spell(self.update_textview)
				self.has_spell_library=True
			except Exception:
				#bummer
				self.has_spell_library=False
				print Exception.message
				
		#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_status_frame)
		update_frame.add(tvframe)
		update_frame.set_label_widget(self.update_status_label)
		#make an hbox for the update frame and throbber
		update_hbox = gtk.HBox(False)
		self.throbber = gtk.Image()
		update_hbox.pack_start(update_frame,True,True)
		update_hbox.pack_start(self.throbber,False,False)
		self.main_vbox.pack_start(update_hbox,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)
		#check for a keypress
		self.connect('key-press-event',self.keypressed)
		
	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 show_some(self):
		self.show_all()
		#hide *some* stuff
		self.message_label.hide()
	
	def add_notebook_page(self,widget,title):
		self.notebook.append_page(widget,title)
	
	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,conv_backwards=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('text-label-clicked',self.dent_text_clicked)
		d.connect('redent-clicked',self.redent_clicked)
		#add this to the dent it's targeting
		target = self.get_target_scroller(target_index)
		target.add_dent(d,is_conv,conv_backwards)	
		#return the dent
		return d
	#filters
	def add_tag_filter(self,filterframe,tag):
		self.emit('add-tag-filter',tag)
		
	def remove_tag_filter(self,filterframe,tag):
		self.emit('remove-tag-filter',tag)
	
	def set_tag_filters(self, filters ):
		self.filterpage.set_tag_filters(filters)
	
	def add_user_filter(self,filterframe,user):
		self.emit('add-user-filter',user)
		
	def remove_user_filter(self,filterframe,user):
		self.emit('remove-user-filter',user)
	
	def set_user_filters(self, filters ):
		self.filterpage.set_user_filters(filters)
	
	def add_group_filter(self,filterframe,group):
		self.emit('add-group-filter',group)
		
	def remove_group_filter(self,filterframe,group):
		self.emit('remove-group-filter',group)
	
	def set_group_filters(self, filters ):
		self.filterpage.set_group_filters(filters)
	
	def update_textbuffer_insert(self, textbuffer, iter, text, length):
		if text=="\n" :
			#TODO: delete the fucking \n. PS fuck you PYGTK for making this
			#so fucking hard
			#textbuffer.backspace(iter,True,True) doesn't work
			self.do_submit=True

	def set_textlimit(self, limit):
		self.textlimit = int(limit)
		
	def emit_update_textview(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!=""):
			#disable the textview
			self.update_textview.set_sensitive(False)
			self.emit('update-status',text)
			
	'''function process_update_response
	@param self self
	@param response the string response returned by the communicator
	'''
	def emit_update_textview_responsed(self, error = False):
		if not error:
			#detach the spellchecker
			self.detach_gtkspell()
			#clear the text area
			self.update_textbuffer.set_text('')
			#reattach the spellchecker
			self.connect_gtkspell()
		#enable the textview
		self.update_textview.set_sensitive(True)
			
	'''function update_textbuffer_changed
	@param self self
	@param textbuffer - the textbuffer that called the function
	'''
	def update_textbuffer_changed(self,textbuffer):
		if self.do_submit:
			self.emit_update_textview()
			self.do_submit=False
			return 
		#we aren't submitting, check the amount of characters
		char_count = textbuffer.get_char_count()
		self.string_update_textview_append_count(self.textlimit-char_count)
		if (char_count > self.textlimit ):
			#prune the text to self.textlimit chars
			start_iter = textbuffer.get_iter_at_offset(self.textlimit)
			end_iter = textbuffer.get_end_iter()
			textbuffer.delete(start_iter,end_iter)
		elif(char_count==0 and not self.is_direct_message_mode):
			#the text has possibly been deleted, clear associated stuff
			self.emit('clear-respond-to-id')

	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 string_update_textview_append_count(self,count):
		if count<self.textlimit:
			string = "%s %d" % (self.string_status_frame,count)
		else:
			string = self.string_status_frame
		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 close_conversation(self,widget):
		self.conversation_vbox.hide_all()
		self.notebook.set_current_page(self.pre_conversation_page)
	
	def quit(self,accel_group, acceleratable, keyval, modifier):
		self.emit("quit")
	
	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)
		
	def copy(self,accel_group,acceleratable,keyval,modifier):
		'''the user want to copy some text'''
		#can we get the selected text?
		try:
			#get the bounds of the last clicked label
			bounds = self.last_clicked_label.get_selection_bounds()
			#is the bounds empty or not?
			if bounds != ():
				#get the text from the last clicked label
				text = self.last_clicked_label.get_text()
				#get the text within the bounds
				selection = text[bounds[0]:bounds[1]]
				# get the clipboard
				clipboard = gtk.Clipboard()
				#set clipboard text
				clipboard.set_text(selection)
				#store that shit!
				clipboard.store()
		except:
			pass
		
	'''
	function window_to_tray
	emit 'window-to-tray' signal
	'''
	def window_to_tray(self,accel_group,acceleratable,keyval,modifier):
		self.emit('window-to-tray')
		
	def connect_gtkspell(self):
		if has_gtkspell and self.has_spell_library:
			self.spell = gtkspell.Spell(self.update_textview)
			
	def detach_gtkspell(self):
		if has_gtkspell and self.has_spell_library:
			self.spell.detach()

	def set_throbber(self,image_path):
		self.throbber.set_from_file(image_path)
		
	def dent_text_clicked(self,dent,text_label):
		self.last_clicked_label = text_label
	
	def set_is_direct_message_mode(self,mode,user=None):
		self.is_direct_message_mode=mode
		if mode:
			self.string_status_frame="%s %s" % (self.string_direct_message, user)
		else:
			self.string_status_frame=self.string_update_textview
			#clear the text
			self.update_textbuffer.set_text("")
		#update the label text
		self.update_status_label.set_text(self.string_status_frame)

	#we may need to set the focus on the notebook
	def focus_notebook(self,accel_group, acceleratable, keyval, modifier):
		#set the focus on the notebook
		self.notebook.grab_focus()
	
	#we may need to set the focus on the status update textview
	def focus_update_textview(self,accel_group, acceleratable, keyval, modifier):
		#set the focus on the notebook
		self.update_textview.grab_focus()
		if keyval==gtk.keysyms.l:
			self.update_textbuffer.set_text('')