#!/usr/bin/python

#####################################################################
#
#       Copyright : Roshan Kumar Singh <singh.roshan08@gmail.com>
#       Licenced under GPLv2 or later.
#       Copyright: See License file that comes with this distribution
#
#####################################################################

import sys
import os

from libgscribble.Wordpress import Wordpress
from libgscribble.htmlview import HtmlView
from libgscribble.Blogger import Blogger

try:
	import pygtk	
	pygtk.require('2.0')
except:
	print "Install PyGtk >= 2.0."
	sys.exit(1)

try:
	import gtk
	import gtk.glade
	import gobject
except:
	print "Install Gtk."
	sys.exit(1)

try:
	import gtkspell
except:
	print "Install pygtkspell."
	sys.exit(1)

__project__ = 'GScribble'
__website__ = 'http://sourceforge.net/projects/gscribble'
__version__ = '0.1.2'
__authors__ = ['Roshan Kumar Singh <singh.roshan08@gmail.com>',]
__artists__ = ['Shreyank Gupta <shreyank@redhat.com>',]
__program_name__ = 'GScribble'
__copyright__ = '(c) 2009-2010 Roshan Kumar Singh'
__license__ = "GScribble is free software; you can redistribute \nit and/or modify it under the terms of the GNU \nGeneral Public License as published by the Free \nSoftware Foundation, either version 2 of the License, \nor higher. \n\n\
GScribble is distributed in the hope that it will \nbe useful, but WITHOUT ANY WARRANTY; without \neven the implied warranty of MERCHANTABILITY \nor FITNESS FOR A PARTICULAR PURPOSE. See \nthe GNU General Public License for more details."


class gscribble:
	'''class for the main window'''
	def __init__(self):
		self.gladefile = self.getGladeFile()
		self.wTree = gtk.glade.XML(self.gladefile, 'mainWindow')
				
		dic = { 
			'on_preferencesButton_clicked': self.preferencesBtn_clicked,
			'on_postButton_clicked': self.postBtn_clicked,
			'on_publishButton_clicked': self.publishBtn_clicked,
			'on_saveButton_clicked':self.saveBtn_clicked,
			'on_editPostButton_clicked': self.editPostBtn_clicked,
			'on_addCatButton_clicked': self.addCatBtn_clicked,
			'on_boldToolButton_clicked': self.boldBtn_clicked,
			'on_italicToolButton_clicked': self.italicBtn_clicked,
			'on_linkToolButton_clicked': self.linkBtn_clicked,
			'on_preferencesToolButton_clicked': self.preferencesBtn_clicked,
			'on_newPostToolButton_clicked': self.newpostBtn_clicked,
			'on_new_activate': self.newpostBtn_clicked,
			'on_cut_activate': self.on_cut_activate,
			'on_copy_activate': self.on_copy_activate,
			'on_paste_activate': self.on_paste_activate,
			'on_save_activate': self.saveBtn_clicked,
			'on_openToolButton_clicked': self.on_open_activate,
			'on_open_activate': self.on_open_activate,
			'on_quit_activate': gtk.main_quit,
			'on_preferences_activate': self.preferencesBtn_clicked,	
			'on_about_activate': self.on_about_activate,
			'on_pageCheckButton_toggled': self.on_pageCheckBtn_toggled,
			'on_spellToggleButton_toggled': self.on_spellBtn_toggled,
			'on_noteBook_switch_page': self.on_noteBook_switch_page}

		self.wTree.signal_autoconnect(dic)
		self.mainWindow = self.wTree.get_widget('mainWindow')
		self.mainWindow.connect('destroy', gtk.main_quit)
		self.statusBar = self.wTree.get_widget("statusBar")
		
		self.cid = 1
		self.cname = 'Published Posts'
		self.publishedTreeView = self.wTree.get_widget("publishedTreeView")
		self.draftsTreeView = self.wTree.get_widget("draftsTreeView")
		self.addColPublishedTree(self.cid, self.cname)	
		
		self.postList = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
		self.publishedTreeView.set_model(self.postList)
		
		# For the categories
		self.categoryTreeView = self.wTree.get_widget("categoryTreeView")
		self.categoryTreeView.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
		self.addCatTree(1,'Categories')
		
		self.categoryList = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
		self.categoryTreeView.set_model(self.categoryList)

		# content part
		self.titleEntry = self.wTree.get_widget("titleEntry")
		self.buf = gtk.TextBuffer()		
		self.contentTextView = self.wTree.get_widget("contentTextView")
		self.contentTextView.set_buffer(self.buf)
		
		# clipboard
		#self.display = gtk.gdk.display_manager_get().get_default_display()
		#self.clipboard = gtk.Clipboard(self.display, "CLIPBOARD")
		#self.clipboardBuf = gtk.TextBuffer()
		self.clipboard = gtk.Clipboard(gtk.gdk.display_get_default(), 'CLIPBOARD')
		self.clipPrimary = gtk.Clipboard(gtk.gdk.display_get_default(), 'PRIMARY')

		self.tagsEntry = self.wTree.get_widget("tagsEntry")
		
		# for visual UI part
		self.noteBook = self.wTree.get_widget("noteBook")
		self.htmlview = self.make_htmlview()
		scr = gtk.ScrolledWindow()
		scr.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
		scr.add(self.htmlview)
		self.noteBook.append_page(scr, gtk.Label("Visual"))
		scr.show_all()
		
		# for tray icon	
		iconFile = self.gladefile.split('.')[0]+'.png'
		trayIcon = gtk.status_icon_new_from_file(iconFile)
		trayIcon.set_visible(True)
		trayIcon.connect("activate", self.trayIcon_clicked)
		trayIcon.connect("popup-menu", self.trayIcon_rightClick)
		
		
		# now load all values
		self.loadValues()
	
	def trayIcon_clicked(self, param):
		if self.mainWindow.get_property('visible'):
			self.mainWindow.hide_all()
		else:
			self.mainWindow.show_all() 
	
	def trayIcon_rightClick(self, widget, button, activate_time):
		menu = gtk.Menu()
		show_app = gtk.MenuItem('Show/Hide')
		quit_app = gtk.MenuItem('Quit')

		#Append the menu items  
		menu.append(show_app)
		menu.append(quit_app)
		#add callbacks
		show_app.connect_object('activate', self.trayIcon_clicked, 'Show/Hide')
		quit_app.connect_object('activate', gtk.main_quit, 'Quit')

		#Show the menu items
		show_app.show()
		quit_app.show()

		#Popup the menu
		menu.popup(None, None, None, button, activate_time)

	def showError(self, error):
		''''This Function is used to show an error dialog when'''
		errorBox = gtk.MessageDialog(type=gtk.MESSAGE_ERROR
					, message_format=error
					, buttons=gtk.BUTTONS_OK)
		errorBox.run()
		errorBox.destroy()

	def saveBtn_clicked(self, widget):
		post = self.buf.get_text(self.buf.get_start_iter(),self.buf.get_end_iter(), True)
		title = self.titleEntry.get_text()
		tags = self.tagsEntry.get_text()
		filename = ''
		chooserBox = draftFileChooser(self.gladefile)
		response = chooserBox.run()
		if response == gtk.RESPONSE_OK:
			filename = chooserBox.chooser.get_filename()
		chooserBox.destroy()

		if filename != '':
			try:
				f = open(filename,'w')
				f.write('#The file is created by gscribble. Do not edit manually it may make it unreadable by gscribble.\n')
				f.write('title=%s\n' %title )
				f.write('tags=%s\n' %tags )
				f.write('post=%s\n' %post)
				f.close()
			except:
				self.showError('Draft could not be saved. File I/O error !!')
	
	def on_open_activate(self, obj):
		chooserBox = draftOpenFileChooser(self.gladefile)
		response = chooserBox.run()
		filename = ''
		if response == gtk.RESPONSE_OK:
			filename = chooserBox.chooser.get_filename()

		chooserBox.destroy()
			
		if filename != '':
			f = open(filename,'r')
			lines = f.readlines()
			line = lines[1]
			line = line.strip()
			token = line.split("=")
			title = token[1]
			
			line = lines[2]
			line = line.strip()
			token = line.split("=")
			tags = token[1]
			line = lines[3]
			
			token = line.split("=")
			post = token[1]
			
			for i in range(len(lines)-4):
				line = lines[i+4]
				post = post+line
			self.titleEntry.set_text(title)
			self.tagsEntry.set_text(tags)
			self.buf.set_text(post)
								
	def on_pageCheckBtn_toggled(self, obj):
		'''Emitted when page/post check button is toggled (1 for blog 0 for page)'''
		if self.blog.post.type == 1:
			self.blog.post.type = 0
			self.wTree.get_widget('postButton').set_sensitive(False)
		else:
			self.blog.post.type = 1
			self.wTree.get_widget('postButton').set_sensitive(True)

	# Cut-copy-paste code taken from PyGtk faqs and modified.
	def on_cut_activate(self, *extra):
		owner = self.clipPrimary.get_owner()
		
		if owner:
			# If the widget which owns Primary selection is an Editable, we can
			# use the editable's clipboard functions to do our operations.
			# Otherwise just perform a copy.
			if isinstance(owner, gtk.Editable):
				owner.cut_clipboard()
			elif isinstance(owner, gtk.TextBuffer):
				self.buf.cut_clipboard(self.clipboard, self.contentTextView.get_editable())
			else:
				selectionText = self.clipPrimary.wait_for_text()
				if selectionText:
					self.clipboard.set_text(selectionText, -1)


	#def on_copy_activate(self, obj):
	#	self.buf.copy_clipboard(self.clipboard)
	def on_copy_activate(self, *extra):
		# If the PRIMARY selection has a listed owner, then it belongs to our app
		if self.clipPrimary.get_owner():
			selectionText = self.clipPrimary.wait_for_text()
			if selectionText:
				self.clipboard.set_text(selectionText, -1)


	#def on_paste_activate(self, obj):
	#	self.buf.paste_clipboard(self.clipboard, None, self.contentTextView.get_editable())
	def on_paste_activate(self, *extra):
		# Get the widget with the focus
		entry = self.mainWindow.focus_widget
		if isinstance(entry, gtk.Editable):
			entry.paste_clipboard()
		elif isinstance(entry, gtk.TextView):
			self.buf.paste_clipboard(self.clipboard, None, self.contentTextView.get_editable())

	def on_about_activate(self, obj):
		'''Shows the about box'''
		dialog = gtk.AboutDialog()
		dialog.set_name(__project__)
		dialog.set_copyright(__copyright__)
		dialog.set_website(__website__)
		dialog.set_authors(__authors__)
		dialog.set_artists(__artists__)
		dialog.set_program_name(__program_name__)
		dialog.set_version(__version__)
		dialog.set_license(__license__)
		iconFile = self.gladefile.split('.')[0]+'.png'
		dialog.set_icon(gtk.gdk.pixbuf_new_from_file(iconFile))
		dialog.run()
		dialog.destroy()

	def addColPublishedTree(self, cid, cname):
		'''Adds the published posts treeview to the main window'''
		column = gtk.TreeViewColumn(cname, gtk.CellRendererText(), text=cid)
		column.set_resizable(True)
		#column.set_sort_column_id(cid)
		self.publishedTreeView.append_column(column)
	
	#Need to check its usability
	def addCatTree(self, cid, cname):
		'''Adds the category treeview to the window'''
		column = gtk.TreeViewColumn(cname, gtk.CellRendererText(), text=cid)
		column.set_resizable(True)
		#column.set_sort_column_id(cid)
		self.categoryTreeView.append_column(column)
	
	def getPost(self):
		'''Get the post with the postid'''
		postCat = []
		for cat in self.categoryTreeView.get_selection().get_selected_rows()[1]:
			postCat.append(self.blog.postedCategory[cat[0]])
		title = self.titleEntry.get_text()
		content = self.buf.get_text(self.buf.get_start_iter(),self.buf.get_end_iter(), True)
		if self.blog.post.postId == -1:
			content = content+'\n\nPosted from <a href="http://sourceforge.net/projects/gscribble/">GScribble</a>.' 
			self.buf.set_text(content)
			self.update_htmlview()
		tags = self.tagsEntry.get_text()
		
		blogPost = {'title': title, 'description': content, 'categories': postCat,'mt_keywords':tags}	
		return blogPost		
		
	def postBtn_clicked(self, widget):
		'''Posts the content'''
		blogPost = self.getPost()
		if blogPost['title'] == '' or blogPost['description'] == '':
			self.showError('Please enter the title and content of the post.')
		else:
			if self.blog.post.type == 1:
				if self.blog.post.postId == -1:
					res = self.blog.newPost(blogPost)
				else:
					res = self.blog.editPost(blogPost)
			else:
				res = self.blog.addPage(blogPost)
			if res:
				self.setStatus('Content posted successfully.')
				self.loadRecentPosts()
			else:
				self.showError('There was an error in posting the content. Please check your network settings.')

	def publishBtn_clicked(self, widget):
		'''Publishes the post'''
		blogPost = self.getPost()

		if blogPost['title'] == '' or blogPost['description'] == '':
			self.showError('Please enter the title and content of the post.')
		else:
			if self.blog.post.type == 1:
				if self.blog.post.postId == -1:
					res = self.blog.newPost(blogPost)
				else:
					res = self.blog.editPost(blogPost)
			else:
				res = self.blog.addPage(blogPost)
				
			if res:
				if self.blog.publishPost():
					self.setStatus('Content published successfully.')
					self.loadRecentPosts()					
			else:
				self.showError('There was an error in posting the content. Please check your network settings.')

	def editPostBtn_clicked(self, widget):
		'''Lets you edit the published post.'''
		post = self.blog.posts[self.publishedTreeView.get_selection().get_selected_rows()[1][0][0]]
		if self.blog.blogType == 'Wordpress':
			p = self.blog.getPost(post['postid'])
		else:
			p = self.blog.getPost(post['title'])

		self.buf.set_text(p.body)
		self.titleEntry.set_text(p.title)
		self.tagsEntry.set_text(p.tags)
		catIter = self.categoryList.get_iter_first()

		self.categoryTreeView.get_selection().unselect_all()

		while catIter:
			sel = self.categoryList.get_value(catIter, 0)
			if sel in p.category:
				self.categoryTreeView.get_selection().select_iter(catIter)
			catIter = self.categoryList.iter_next(catIter)
		self.update_htmlview()
	
	def preferencesBtn_clicked(self, widget):
		'''Creates the preferences dailog box for settings.'''
		preferencesBox = preferencesDialog(self.gladefile)
		try:	
			if self.blog.blogType == 'Wordpress':
				preferencesBox.blogTypeComboBox.set_active(0)
			elif self.blog.blogType == 'Blogger':
				preferencesBox.blogTypeComboBox.set_active(1)
			if self.blog.url == '':
				preferencesBox.urlEntry.set_text('http://www.example.com/xmlrpc.php')		
			else:
				preferencesBox.urlEntry.set_text(self.blog.url)
			preferencesBox.usernameEntry.set_text(self.blog.username)
			preferencesBox.passwordEntry.set_text(self.blog.password)
			preferencesBox.hostEntry.set_text(self.blog.host)
			preferencesBox.portEntry.set_text(self.blog.port)
			preferencesBox.proxyUsernameEntry.set_text(self.blog.proxyUsername)
			preferencesBox.proxyPasswordEntry.set_text(self.blog.proxyPassword)
			if self.blog.proxyEnable == '1':
				preferencesBox.proxyCheckButton.set_active(True)
			else:
				preferencesBox.proxyCheckButton.set_active(False)
				preferencesBox.hostEntry.set_sensitive(False)
				preferencesBox.portEntry.set_sensitive(False)
				preferencesBox.proxyUsernameEntry.set_sensitive(False)
				preferencesBox.proxyPasswordEntry.set_sensitive(False)
		except:
			preferencesBox.proxyCheckButton.set_active(False)
			preferencesBox.hostEntry.set_sensitive(False)
			preferencesBox.portEntry.set_sensitive(False)
			preferencesBox.proxyUsernameEntry.set_sensitive(False)
			preferencesBox.proxyPasswordEntry.set_sensitive(False)
		response = preferencesBox.run()

		if response == gtk.RESPONSE_OK:
			blogType = preferencesBox.blogTypeComboBox.get_active_text()
			url = preferencesBox.urlEntry.get_text()
			username = preferencesBox.usernameEntry.get_text()
			password = preferencesBox.passwordEntry.get_text()
			if preferencesBox.proxyCheckButton.get_active():
				proxyEnable = 1
			else:
				proxyEnable = 0
			host = preferencesBox.hostEntry.get_text()
			port = preferencesBox.portEntry.get_text()
			proxyUsername = preferencesBox.proxyUsernameEntry.get_text()
			proxyPassword = preferencesBox.proxyPasswordEntry.get_text()
			try:
				f = file(self.settingsFile,'w')
				f.write('blogType=%s\n' % blogType)
				f.write('url=%s\n' % url)	
				f.write('username=%s\n' % username)
				f.write('password=%s\n' % password)
				f.write('proxyEnable=%s\n' % proxyEnable)
				f.write('host=%s\n' % host)
				f.write('port=%s\n' % port)
				f.write('proxyUsername=%s\n' % proxyUsername)
				f.write('proxyPassword=%s\n' % proxyPassword)
				f.close()
				context_id = self.statusBar.get_context_id("settings")
				self.statusBar.push(context_id,'Settings saved ...')
			except:
				self.showError('Settings could not be saved. File creation error !!')
			self.loadValues()	
		preferencesBox.destroy()		
		
	def addCatBtn_clicked(self, widget):
		'''Adds a new category'''
		categoryEntry = self.wTree.get_widget("categoryEntry")
		newCategory = categoryEntry.get_text().strip()
		if newCategory == "":
			self.showError("Category can not be blank.")
		else:
			categoryEntry.set_text("")
			self.categoryList.append([newCategory, newCategory])
			self.blog.postedCategory.append(newCategory)
			#if self.blog.addCategory(newCategory):
			#	self.loadCategories()	
			#	self.setStatus('New category added successfully ...')
			#else:
			#self.showError('There was an error in posting the content. Please check your network settings.')
		
	
	def loadCategories(self):
		'''Loads the categories in the categories tree view'''
		if self.blog.getCategories():
			self.categoryList.clear()
			for c in self.blog.postedCategory:
				self.categoryList.append([c, c])
			return True
		else:
			self.showError('There was an error in loading the content. Please check your network settings or preferences.')
			return False

	def loadRecentPosts(self):
		'''Loads the recent post in the recent post treeview'''
		if self.blog.getRecentPosts():
			self.postList.clear()
		
			for p in self.blog.posts:
				self.postList.append([p,p['title']])
		else:
			self.showError('There was an error in loading the content. Please check your network settings or preferenes.')

	def setStatus(self, status):
		'''Sets the status message.'''	
		context_id = self.statusBar.get_context_id("settings")
		self.statusBar.push(context_id, status)

	def loadValues(self):
		'''Loads the various variables for the blog'''
		blogType = 'undefined'
		url = ''
		username = ''
		password = ''
		proxyEnable = 0
		host = ''
		port = ''
		proxyUsername = ''
		proxyPassword = ''
		
		#Clear category list and post list		
		self.categoryList.clear()
		self.postList.clear()

		homeDir = os.path.expanduser('~')
		gblogPath = os.path.join(homeDir,'.gscribble')
		if not os.path.exists(gblogPath):
			os.mkdir(gblogPath)
		self.settingsFile = os.path.join(gblogPath,'settings')
		if os.path.exists(self.settingsFile):
			f = file(self.settingsFile,'r')
			lines = f.readlines()
			f.close()
			for i in range(len(lines)):
				line = lines[i]
				
				if line == '' or line.startswith('#'):
					continue
				token = line.split('=')
				if token[0].strip() == 'blogType':
					blogType = token[1].strip()
				if token[0].strip() == 'url':
					url = token[1].strip()
				if token[0].strip() == 'username':
					username = token[1].strip()
				if token[0].strip() == 'password':
					password = token[1].strip()
				if token[0].strip() == 'proxyEnable':
					proxyEnable = token[1].strip()
				if token[0].strip() == 'host':
					host = token[1].strip()
				if token[0].strip() == 'port':
					port = token[1].strip()
				if token[0].strip() == 'proxyUsername':
					proxyUsername = token[1].strip()
				if token[0].strip() == 'proxyPassword':
					proxyPassword = token[1].strip()
			if blogType != 'undefined' and username != '' and password != '':
				if blogType == 'Wordpress':
					if url != '':
						self.blog = Wordpress(url, username, password, proxyEnable, host, port, proxyUsername, proxyPassword, blogType)
					else:
						self.showError('Incorrect settings found !')
				elif blogType == 'Blogger':
					self.blog = Blogger('', username, password, proxyEnable, host, port, proxyUsername, proxyPassword, blogType)
					# disable post as page and tags
					self.tagsEntry.set_sensitive(False)
					self.wTree.get_widget("pageCheckButton").set_sensitive(False)
					
				if self.loadCategories():
					self.loadRecentPosts()
					self.setStatus('Settings loaded ...')
			else:
				self.showError('Incorrect settings found !')
		else:
			try:
				f = file(self.settingsFile,'w')
				f.write('blogType=%s\n' % blogType)
				f.write('url=%s\n' % url)
				f.write('username=%s\n' % username)
				f.write('password=%s\n' % password)
				f.write('proxyEnable=%s\n' % proxyEnable)
				f.write('host=%s\n' % host)
				f.write('port=%s\n' % port)
				f.write('proxyUsername=%s\n' % proxyUsername)
				f.write('proxyPassword=%s\n' % proxyPassword)
				f.close()
				context_id = self.statusBar.get_context_id("settings")
				self.statusBar.push(context_id,'Settings saved ...')
			except:
				self.showError('Settings could not be saved. File creation error !!')
			self.preferencesBtn_clicked("widget")	
						
	def boldBtn_clicked(self, widget):
		'''Activated when bold button in clicked adds bold tag in textbox'''
		self.wrap_selection("<b>","</b>")		
			
	def italicBtn_clicked(self, widget):
		'''Activated when italic button in clicked adds italic tag in textbox'''
		self.wrap_selection("<i>","</i>")
		
	def linkBtn_clicked(self, widget):
		"""Called when the link button is clicked"""
		linkBox = linkDialog(self.gladefile)

		"""Get the selection not and reselect becuase somethimes
		the dialog will remove the selection"""
		start, end = self.get_selection_iters()

		#run the dialog
		if (linkBox.run()==gtk.RESPONSE_OK):
			#Reset the selection
			if ((start)and(end)):
				#Select the text
				self.buf.select_range(end,start)

			#Wrap the selection with the value of the entry fields
			self.wrap_selection("<a href=\"%s\">%s" %(linkBox.urlEntry.get_text(), linkBox.textEntry.get_text()),"</a>")
		linkBox.destroy()

	def update_htmlview(self):
	        start, end = self.buf.get_bounds()
	        text = self.buf.get_text(start, end)
	        self.htmlview.update(text, self)

	def make_htmlview(self):
	        widget = HtmlView()
		widget.show_all()
		return widget

	def on_noteBook_switch_page(self, page, pageNum, param):
		if param == 1:
			self.update_htmlview()

	def wrap_selection(self, start_tag, end_tag):
		"""This fucntion is used to wrap the currently selected
		text in the gtk.TextView with start_tag and end_tag. If
		there is no selection start_tag and end_tag will be
		inserted at the cursor position
		start_tag - The text that will go at the start of the
		selection.
		end_tag - The text that will go at the end of the
		selection."""

		start, end = self.get_selection_iters();
		if ((not start)or(not end)):
			print "Error inserting text"
			return;
		#Create a mark at the start and end
		start_mark = self.buf.create_mark(None,start, True)
		end_mark = self.buf.create_mark(None, end, False)
		#Insert the start_tag
		self.buf.insert(start, start_tag)
		#Get the end iter again
		end = self.buf.get_iter_at_mark(end_mark)
		#Insert the end tagasx
		self.buf.insert(end, end_tag)
		#Get the start and end iters
		start = self.buf.get_iter_at_mark(start_mark)
		end = self.buf.get_iter_at_mark(end_mark)
		#Select the text
		self.buf.select_range(end,start)
		#Delete the gtk.TextMark objects
		self.buf.delete_mark(start_mark)
		self.buf.delete_mark(end_mark)

	def get_selection_iters(self):
		"""This function gets the start and end selection
		iters from the text view.  If there is no selection
		the current position of the cursor will be returned.
		Returns - start,end - gtk.TextIter objects"""

		#init
		start = None
		end = None

		#First check to see that the text buffer is valid
		if (not self.buf):
			self.show_error_dlg("Text buffer not available")
			return start,end

		#Get the selection bounds
		bounds = self.buf.get_selection_bounds();
		if (bounds):
			#If there is a selection we are done
			start,end = bounds
		else:
			#There is no selection so just get the cursor mark
			cursor_mark = self.buf.get_insert()
			"""Set start and end to be gtk.TextIter objercts at the
			position of the cursor mark"""
			start = self.buf.get_iter_at_mark(cursor_mark)
			end = self.buf.get_iter_at_mark(cursor_mark)

		return start, end

	def newpostBtn_clicked(self, widget):
		'''Activated when new post button on toolbar is clicked.'''
		self.blog.post.postId = -1
		self.titleEntry.set_text('')
		self.buf.set_text('')
		self.tagsEntry.set_text('')
		self.categoryTreeView.get_selection().unselect_all()
		self.update_htmlview()
	
	def getGladeFile(self):
		'''if a local glade file exists, returns its path, else it returns the path of installed glade file'''
		if os.path.exists('glade/gscribble.glade'):
			print 'using the local glade file'
			return 'glade/gscribble.glade'
		else:
			return '/usr/share/gscribble/glade/gscribble.glade'

	def on_spellBtn_toggled(self, widget):
		'''This function checks the spelling in the textview'''
		if widget.get_active():
			self.spell = gtkspell.Spell(self.contentTextView)
			self.spell.recheck_all()
		else:
			self.spell.detach()
		

class preferencesDialog:
	'''This is the class for the preferences window'''
	def __init__(self, gladefile):
		self.gladefile = gladefile
		self.wTree = gtk.glade.XML(self.gladefile,'preferencesDialog')
		self.dialogBox = self.wTree.get_widget('preferencesDialog')
		self.blogTypeComboBox = self.wTree.get_widget("blogTypeComboBox")
		self.urlEntry = self.wTree.get_widget("urlEntry")
		self.usernameEntry = self.wTree.get_widget("usernameEntry")
		self.passwordEntry = self.wTree.get_widget("passwordEntry")
		self.proxyCheckButton = self.wTree.get_widget("proxyCheckButton")
		self.hostEntry = self.wTree.get_widget("hostEntry")
		self.portEntry = self.wTree.get_widget("portEntry")
		self.proxyUsernameEntry = self.wTree.get_widget("proxyUsernameEntry")
		self.proxyPasswordEntry = self.wTree.get_widget("proxyPasswordEntry")
		self.blogTypeComboBox.set_active(0)

		dic = {'on_proxyCheckButton_toggled': self.on_proxyCheckBtn_toggled,
		       'on_blogTypeComboBox_changed': self.on_blogTypeComboBox_changed}
		self.wTree.signal_autoconnect(dic)
	
	def run(self):
		'''To run the window.'''
		return self.dialogBox.run()
	
	def destroy(self):
		'''To destroy the window.'''
		self.dialogBox.destroy()	

	def on_proxyCheckBtn_toggled(self, obj):
		'''Emitted when proxy check button in preferences is toggled'''
		if self.proxyCheckButton.get_active():
			self.hostEntry.set_sensitive(True)
			self.portEntry.set_sensitive(True)
			self.proxyUsernameEntry.set_sensitive(True)
			self.proxyPasswordEntry.set_sensitive(True)
		else:
			self.hostEntry.set_sensitive(False)
			self.portEntry.set_sensitive(False)
			self.proxyUsernameEntry.set_sensitive(False)
			self.proxyUsernameEntry.set_sensitive(False)
			self.proxyPasswordEntry.set_sensitive(False)
	
	def on_blogTypeComboBox_changed(self, widget):
		if widget.get_active() != 0:
			self.urlEntry.set_sensitive(False)
		else:
			self.urlEntry.set_sensitive(True)

class linkDialog:
	'''This is the class for the preferences window'''
	def __init__(self, gladefile):
		self.gladefile = gladefile
		self.wTree = gtk.glade.XML(self.gladefile,'linkDialog')
		self.dialogBox = self.wTree.get_widget('linkDialog')
		self.urlEntry = self.wTree.get_widget("urlEntry1")
		self.textEntry = self.wTree.get_widget("textEntry")
	
	def run(self):
		'''To run the window.'''
		return self.dialogBox.run()
	
	def destroy(self):
		'''To destroy the window.'''
		self.dialogBox.destroy()

class aboutDialog:
	'''This is the class for the preferences window'''
	def __init__(self, gladefile):
		self.gladefile = gladefile
		self.wTree = gtk.glade.XML(self.gladefile,'aboutDialog')
		self.dialogBox = self.wTree.get_widget('aboutDialog')
	
	def run(self):
		'''To run the window.'''
		return self.dialogBox.run()
	
	def destroy(self):
		'''To destroy the window.'''
		self.dialogBox.destroy()		

 
class draftFileChooser:
	'''This is the class for saving drafts.'''
	def __init__(self, gladefile):
		self.chooser = gtk.FileChooserDialog(title='Save Draft',action=gtk.FILE_CHOOSER_ACTION_SAVE,
	            buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
	
	def run(self):
		'''To run the window.'''
		return self.chooser.run()
	
	def destroy(self):
		'''To destroy the window'''
		return self.chooser.destroy()

class draftOpenFileChooser:
	'''This is the class for opening saved drafts.'''
	def __init__(self, gladefile):
		self.chooser = gtk.FileChooserDialog(title='Open Draft',action=gtk.FILE_CHOOSER_ACTION_OPEN,
	            buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
		
	def run(self):
		'''To run the program.'''
		return self.chooser.run()
	
	def destroy(self):
		'''To destroy the window.'''
		return self.chooser.destroy()
	
# main begins here
if __name__ == "__main__":
	window = gscribble()
	gtk.main()

