Esempio n. 1
0
								
					n = pynotify.Notification(
						mail['title'], 
						'from ' + mail['author_detail']['name'] + ' <' + mail['author_detail']['email'] + '>', 
						GmailIcon
					)
					n.show()
		
			deleteIndicators = []
		
			for i in self.indicators:
				if not i.title in [mail['title'] for mail in data['entries']]:
					deleteIndicators.append(i)
				
		for i in deleteIndicators:
			i.unstress()
			i.hide()
			self.indicators.remove(i)
		
		gobject.timeout_add_seconds(self.timeout, self.checkMails)
		
if __name__ == "__main__":	
	gkr = Keyring(GmailNotifyName, 'login data')
	username, password = gkr.getLogin()
	if username == '':
		username = None
	if password == '':
		password = None
	gmn = Notifier(username,password)
	gtk.main()
class GMailNotifySettings:
	def __init__(self):
		self.gladeFile = '/usr/lib/gmailnotify/gmailnotify-settings.glade'
		self.valid = False
		self.initialisation = True
		self.timeouts = 0
		self.builder = gtk.Builder()	
		self.builder.add_from_file(self.gladeFile)
		self.builder.connect_signals({
			'on_settings_destroy': 			self.quit,  
			'on_close_clicked': 			self.quit, 
			'gtk_main_quit': 				self.quit,
			'on_apply_clicked': 			self.save,
			'on_entry_username_changed':	self.refreshTimeout,
			'on_entry_password_changed':	self.refreshTimeout,
		})
		
		# save needed object to manipulate
		self.window = self.builder.get_object('settings')
		self.aboutName = self.builder.get_object('name')
		self.aboutVersion = self.builder.get_object('version')
		self.entryUsername = self.builder.get_object('entry_username')
		self.entryPassword = self.builder.get_object('entry_password')
		self.validation = self.builder.get_object('validation')
		self.validationImage = self.builder.get_object('validation_image')
		self.applyButton = self.builder.get_object('apply')
		
		self.applyButton.set_sensitive(False)
		self.gkr = Keyring(GmailNotifyName, 'login data')
		self.loadSettings()
		#self.naming()
		self.initialisation = False
		self.window.show()	
		self.refresh()
			
	def naming(self):
		""" prevent incorrect naming (ugly way) """
		self.window.set_title(GmailNotifyName + ' Settings')
		self.aboutName.set_text(GmailNotifyName)
		self.aboutVersion.set_text('Version ' + '0.1')
			
	def save(self, widget=None):
		""" save settings to keyring """
		print 'save settings'		
		self.gkr.setLogin(self.entryUsername.get_text(), self.entryPassword.get_text())		
	
	def quit(self, widget):
		""" save settings and close window """
		if self.valid:
			self.save()
		print 'quit settings dialog'
		gtk.main_quit()
	
	def loadSettings(self):		
		""" load login information if already saved to keyring """
		username, password = self.gkr.getLogin()
		if not username == None:
			self.entryUsername.set_text(username)
		if not password == None:		
			self.entryPassword.set_text(password)
			
	def refreshTimeout(self, widget=None):
		""" initialize timeout - prevent check login information after every type in """
		if self.initialisation:
			return
		self.applyButton.set_sensitive(False)
		self.validationImage.set_from_stock('gtk-refresh', gtk.ICON_SIZE_SMALL_TOOLBAR)
		self.validation.set_text('Checking ...')
		self.timeouts += 1
		gobject.timeout_add(700, self.refresh)
	
	def refresh(self):	
		""" initiates validation check """
		if self.initialisation:
			return
			
		if self.timeouts > 1:
			self.timeouts -= 1
			return
		
		self.timeouts = 0
		
		print 'refresh validation'
		self.setValid(False)
		
		if self.entryUsername.get_text() == '' or self.entryPassword.get_text() == '':
			return
			
		self.validLoginInformation()
			
	def setValid(self, value):
		""" sets icon and text if valid or not """
		if value:
			self.applyButton.set_sensitive(True)
			self.valid = True
			self.validation.set_text('Valid login information')
			self.validationImage.set_from_stock('gtk-yes', gtk.ICON_SIZE_SMALL_TOOLBAR)
		else:
			self.applyButton.set_sensitive(False)
			self.valid = False
			self.validation.set_text('Invalid login information')
			self.validationImage.set_from_stock('gtk-dialog-error', gtk.ICON_SIZE_SMALL_TOOLBAR)
			
	def validLoginInformation(self):
		""" check if login information is valid """
		request = urllib2.Request('https://mail.google.com/mail/feed/atom/')
		request.add_header('Authorization', 'Basic %s'%(base64.encodestring('%s:%s'%(self.entryUsername.get_text(), self.entryPassword.get_text()))[:-1]))
		try:
			urllib2.urlopen(request)
			self.setValid(True)
		except urllib2.HTTPError:
			self.setValid(False)