"""Written 04 august 2004 by Fred Dubuy <effediwhy@gmail.com>
The CD template is from Yves Ceccone <yves@yeccoe.org>
This work is released under the GNU GPL, version 2 or later.

This script reads the ID of the CD in the drive
and queries CDDB to get the album and song titles
The songs are displayed in a frame text on the back side
There are no font settings, they are up to you...
This script only avoid having to type the songs titles
It is possible to choose to add or not track number and track time

08/10/2004 : added multiple CDDB answers support

10/16/2004 - Petr Vanek <petr@yarpen.cz>
Some strange artists use strange characters in names of their artworks.
You need specify the right encoding for these texts. The system encoding
 is taken as default. If you're using Scribus 1.2.1cvs or later you
can change it in the GUI dialog.
Or set e.g.:
encoding = 'utf8'
or whatever on line 42

10/17/2004 - Fred Dubuy
Put the encoding selection into the main dialog, and added tooltips, mainly for the encoding selection.
The tooltip code is from Michael Lange <klappnase at 8ung dot at>, taken from Tkinter wiki (http://tkinter.unpythonic.net/wiki/)

"""


# Make sure you have python-cddb installed
# (http://cddb-py.sourceforge.net/)

# Change the dev value below according to your configuration


from scribus import *
#following import moved at the end of the script with test
#import DiscID, CDDB,cdrom
from Tkinter import *

import locale
language, encoding = locale.getdefaultlocale()


dev = "/dev/cdrom"

class ToolTip:
    def __init__(self, master, text='Your text here', delay=1000, **opts):
        self.master = master
        self._opts = {'anchor':'center', 'bd':1, 'bg':'lightyellow', 'delay':delay, 'fg':'black',\
                      'follow_mouse':0, 'font':None, 'justify':'left', 'padx':4, 'pady':2,\
                      'relief':'solid', 'state':'normal', 'text':text, 'textvariable':None,\
                      'width':0, 'wraplength':150}
        self.configure(**opts)
        self._tipwindow = None
        self._id = None
        self._id1 = self.master.bind("<Enter>", self.enter, '+')
        self._id2 = self.master.bind("<Leave>", self.leave, '+')
        self._id3 = self.master.bind("<ButtonPress>", self.leave, '+')
        self._follow_mouse = 0
        if self._opts['follow_mouse']:
            self._id4 = self.master.bind("<Motion>", self.motion, '+')
            self._follow_mouse = 1
    
    def configure(self, **opts):
        for key in opts:
            if self._opts.has_key(key):
                self._opts[key] = opts[key]
            else:
                KeyError = 'KeyError: Unknown option: "%s"' %key
                raise KeyError
    
    ##----these methods handle the callbacks on "<Enter>", "<Leave>" and "<Motion>"---------------##
    ##----events on the parent widget; override them if you want to change the widget's behavior--##
    
    def enter(self, event=None):
        self._schedule()
        
    def leave(self, event=None):
        self._unschedule()
        self._hide()
    
    def motion(self, event=None):
        if self._tipwindow and self._follow_mouse:
            x, y = self.coords()
            self._tipwindow.wm_geometry("+%d+%d" % (x, y))
    
    ##------the methods that do the work:-----##
    
    def _schedule(self):
        self._unschedule()
        if self._opts['state'] == 'disabled':
            return
        self._id = self.master.after(self._opts['delay'], self._show)

    def _unschedule(self):
        id = self._id
        self._id = None
        if id:
            self.master.after_cancel(id)

    def _show(self):
        if self._opts['state'] == 'disabled':
            self._unschedule()
            return
        if not self._tipwindow:
            self._tipwindow = tw = Toplevel(self.master)
            # hide the window until we know the geometry
            tw.withdraw()
            tw.wm_overrideredirect(1)
            self.create_contents()
            tw.update_idletasks()
            x, y = self.coords()
            tw.wm_geometry("+%d+%d" % (x, y))
            tw.deiconify()
    
    def _hide(self):
        tw = self._tipwindow
        self._tipwindow = None
        if tw:
            tw.destroy()
                
    ##----these methods might be overridden in derived classes:----##
    
    def coords(self):
        # The tip window must be completely outside the master widget;
        # otherwise when the mouse enters the tip window we get
        # a leave event and it disappears, and then we get an enter
        # event and it reappears, and so on forever :-(
        # or we take care that the mouse pointer is always outside the tipwindow :-)
        tw = self._tipwindow
        twx, twy = tw.winfo_reqwidth(), tw.winfo_reqheight()
        w, h = tw.winfo_screenwidth(), tw.winfo_screenheight()
        # calculate the y coordinate:
        if self._follow_mouse:
            y = tw.winfo_pointery() + 20
            # make sure the tipwindow is never outside the screen:
            if y + twy > h:
                y = y - twy - 30
        else:
            y = self.master.winfo_rooty() + self.master.winfo_height() + 3
            if y + twy > h:
                y = self.master.winfo_rooty() - twy - 3
        # we can use the same x coord in both cases:
        x = tw.winfo_pointerx() - twx / 2
        if x < 0:
            x = 0
        elif x + twx > w:
            x = w - twx
        return x, y

    def create_contents(self):
        opts = self._opts.copy()
        for opt in ('delay', 'follow_mouse', 'state'):
            del opts[opt]
        label = Label(self._tipwindow, **opts)
        label.pack()

class Warning2(Frame):
	"class to display a message for import problems"
	def __init__(self,parent,message):
		Frame.__init__(self)
		frame = Frame(parent)
		self.parent=parent
		lab2=Label(frame,text=message)
		lab2.pack()
		button=Button(frame,text='OK',command=self.parent.destroy)
		button.pack()
		frame.pack()

class Warning(Toplevel):
	"generic class to display a message in a new dialog"
	def __init__(self,parent,message):
		Toplevel.__init__(self)
		lab2=Label(self,text=message)
		lab2.pack()
		button=Button(self,text='OK',command=self.quit)
		button.pack()
		self.geometry("+%d+%d" % ( (w-150)/2, (h-50)/2 ) )
	def quit(self):
		app.only_once=0
		self.destroy()


class Multiple(Toplevel):
	"opens a dialog to choose between multiples CDDB answers"
	def __init__(self,parent,query_info):
		Toplevel.__init__(self)
		self.parent=parent
		self.title=("Multiple choice")
		mlabel=Label(self,text="Multiple entries found, select one :")
		mlabel.pack()
		length=0
		count=0
		for i in query_info:
			count+=1
			if len(i['title'])>length:
				length=len(i['title'])
		self.list = Listbox(self, height=count, width=length)
		for i in query_info:
			self.list.insert(END, i['title'])
		self.list.bind("<Double-Button-1>", self.choice2)
		self.list.pack()
		b1=Button(self,text='Select',command=self.choice)
		b1.pack(side=LEFT)
		b2=Button(self,text='Cancel',command=self.cancel_multiple)
		b2.pack(side=RIGHT)
		#wi=self.winfo_width()
		#hg=self.winfo_height()
		self.geometry("+%d+%d" % ( (w-300)/2, (h-150)/2 ) )
	def choice(self):
        #no current selection
		if len(map(int,self.list.curselection()))==0:
			return
		app.sel_index=map(int,self.list.curselection())[0]
		self.destroy()
	def choice2(self,event):
		self.choice()
	def cancel_multiple(self):
		app.only_once=0
		self.destroy()

class Application(Frame):
	def __init__(self, parent):
		Frame.__init__(self)
		self.parent=parent
		frame = Frame(parent)
		parent.title("CD Cover creation")
		lab_ent=Label(frame,text="Enter your CD device :")
		lab_ent.grid(row=0, columnspan=2)
		tt1 = ToolTip(lab_ent, follow_mouse=1, text="Specify your cdrom device file. The default is /dev/cdrom and can be changed by editing the script")
		self.ent=Entry(frame)
		self.ent.insert(0,dev)
		self.ent.icursor(len(dev))
		self.ent.grid(row=1,columnspan=2)
		lab_enc=Label(frame,text="Characters encoding :")
		lab_enc.grid(row=2, columnspan=2)
		tt2 = ToolTip(lab_enc, follow_mouse=1, text="If you are creating a cover for non-english record/artist, you may specify the right text encoding for the local characters. You can get some errors using wrong encoding.")
		self.enc=Entry(frame)
		self.enc.insert(0,encoding)
		self.enc.icursor(len(encoding))
		self.enc.grid(row=3,columnspan=2)
		self.numbering = IntVar()
		self.ttime = IntVar()
		self.sel_index = -1
		self.only_once = 0
		c1 = Checkbutton(frame, text="Add track number", variable=self.numbering)
		c1.grid(row=4,column=0)
		c1.select()
		t1 = ToolTip(c1, follow_mouse=1, text="If checked the track number will be put in front of the song name")
		c2 = Checkbutton(frame, text="Add track time", variable=self.ttime)
		c2.grid(row=4,column=1)
		t2 = ToolTip(c2, follow_mouse=1, text="If checked the track duration will be appended after the song name")
		b_valid=Button(frame,text='Create Cover',command=self.get_device)
		b_valid.grid(row=5,column=0)
		b_cancel=Button(frame,text='Cancel',command=self.quit)
		b_cancel.grid(row=5,column=1)
		frame.pack()
	def get_device(self):
		#protection against multiple choice-window opening
		if self.only_once==1:
			return
		self.only_once=1
		self.build_cover(self.ent.get(),self.enc.get(),self.numbering.get(),self.ttime.get())

	def quit(self):
		self.parent.destroy()

	def build_cover(self,dev,char_enc,putnumber,puttime):
		#Get info from CD as in python-cddb examples
		cdrom = None
		try:
			if dev:
				cdrom = DiscID.open(dev)
			else:
				cdrom = DiscID.open()
		except: 
			w=Warning(self,"Cannot access the device "+dev)
			return
		try:
			disc_id = DiscID.disc_id(cdrom)
		except:
			w=Warning(self,"Cannot read the device" +dev)
			return
		try:
			(query_stat, self.query_info) = CDDB.query(disc_id)
		except:
			w=Warning(self,"Cannot access to CDDB. Check your internet connection")
			return
	
		if query_stat == 200:
			(read_stat, read_info) = CDDB.read(self.query_info['category'], 
					       	self.query_info['disc_id'])
			if read_stat != 210:
				w=Warning(self,"Disc ID not found in CDDB")
				return
			else:
				album=self.query_info['title']
		elif query_stat == 210 or query_stat == 211:
			m=Multiple(self,self.query_info)
			self.wait_window(m)
			#window close but no choice made
			if self.sel_index == -1:
				return
			(read_stat, read_info) = CDDB.read(self.query_info[self.sel_index]['category'], self.query_info[self.sel_index]['disc_id'])
			album=self.query_info[self.sel_index]['title']
		else:
			w=Warning(self,"Disc ID not found in CDDB")
			return
		#here starts the template building
		SetUnit(1)
		Margins = (0, 0, 0, 0)
		Papier_A4 = (210, 297)
		if NewDoc(Papier_A4, Margins, Landscape, 1, Millimeters, NoFacingPages, FirstPageLeft):
			NewPage(-1)
			GotoPage(1)
			CreateLayer("crop")
			SetActiveLayer("crop")
			t1 = CreateLine(28.5, 38, 28.5, 43) 
			SetLineWidth(0.1, t1)
			t2 = CreateLine(148.5, 38, 148.5, 43)
			SetLineWidth(0.1, t2)
			t3 = CreateLine(268.5, 38, 268.5, 43) 
			SetLineWidth(0.1, t3)
			t4 = CreateLine(28.5, 172, 28.5, 167)
			SetLineWidth(0.1, t4)
			t5 = CreateLine(148.5, 172, 148.5, 167)
			SetLineWidth(0.1, t5) 
			t6 = CreateLine(268.5, 172, 268.5, 167)
			SetLineWidth(0.1, t6)
			t7 = CreateLine(21.5, 45, 26.5, 45)
			SetLineWidth(0.1, t7)
			t8 = CreateLine(21.5, 165, 26.5, 165)
			SetLineWidth(0.1, t8)
			t9 = CreateLine(270.5, 45, 275.5, 45)
			SetLineWidth(0.1, t9)
			t10 = CreateLine(270.5, 165, 275.5, 165)
			SetLineWidth(0.1, t10)
			CreateLayer("bleed")
			SetActiveLayer("bleed")
			img1 = CreateImage(24.35, 41.25 , 124.20, 127.95,)
			img2 = CreateImage(148.55, 41.25 , 124.20, 127.95,)
			CreateLayer("normal")
			SetActiveLayer("normal")
			a = CreateText(98.5, 20, 100, 10)
			SetText("CD cover template - Front side", a)
			SetFontSize(11, a)
			SetTextAlignment(1, a)	
			b = CreateText(28.5, 45, 120, 120)
			SetFillColor("None", b)
			c = CreateText(148.5, 45, 120, 120)
			SetFillColor("None", c)
			GotoPage(2)
			SetActiveLayer("normal")
			a2 = CreateText(98.5, 20, 100, 10)
			SetText('CD cover template - Back side', a2)
			SetFontSize(11, a2)
			SetTextAlignment(1, a2)
			a2t = CreateText(204, 44, 78, 9)
			SetText("Instructions :", a2t)
			SetFontSize(13, a2t)
			SetTextAlignment(1, a2t)	
			a21 = CreateText(204, 54, 78, 87)
			SetText('Modify either the text on the Normal layer, or the pictures on the "bleed" layers, or both. You can change the frame types (image to text frame or text to image,...). The bleeds are wider than the CD size, which avoid any blank margin when cutting the printed document. The external crop marks will allow you to cut the cover and the internal crop marks are here for the folding.\nYou can get the front picture of your CD using your favorite search engine\'s image search feature', a21)
			SetFontSize(11, a21)
			SetTextAlignment(0, a21)
			b2 = CreateText(28.5, 162.10, 117, 6)
			SetText(unicode(album,char_enc), b2)
			SetFontSize(9, b2)
			SetTextAlignment(1, b2)
			RotateObjectAbs(90, b2)
			SetFillColor("None", b2)
			c2 = CreateText(34.5, 45, 137.5, 117)
			offset=0
			InsertText(unicode(album+"\n\n",char_enc),offset,c2)
			offset=offset+len(album)+2
			for i in range(0, disc_id[1]):
				if putnumber == 1:
					song='%d - %s' % (i+1, read_info['TTITLE' + `i`]) 
				else:
					song='%s' % read_info['TTITLE' + `i`]
				if puttime == 1:
					if i < (disc_id[1]-1):
						ttime=(disc_id[i+3]-disc_id[i+2])/75
					else:
						ttime=disc_id[i+3]-disc_id[i+2]/75
					song+=' - %d:%.2d' % ((ttime/60),(ttime%60))
				InsertText(unicode(song+"\n",char_enc),offset,c2)
				offset=offset+len(song)+1
			SetFillColor("None", c2)
			d2 = CreateText(28.5, 162.10, 117, 6)
			SetText(unicode(album,char_enc), d2)
			SetFontSize(9, d2)
			SetTextAlignment(1, d2)
			RotateObjectAbs(90, d2)
			SetFillColor("None", d2)
			MoveObject(143.5, 0, d2)
			SetActiveLayer("bleed")
			img3 = CreateImage(24.35, 41.25 , 157.50, 126.50,)
			SetActiveLayer("crop")
			t21 = CreateLine(28.5, 38, 28.5, 43) 
			SetLineWidth(0.1, t21)
			t22 = CreateLine(34.5, 38, 34.5, 43)
			SetLineWidth(0.1, t22)
			t23 = CreateLine(172, 38, 172, 43)
			SetLineWidth(0.1, t23)
			t24 = CreateLine(178, 38, 178, 43)
			SetLineWidth(0.1, t24)
			t25 = CreateLine(28.5, 164.5, 28.5, 169.5) 
			SetLineWidth(0.1, t25)
			t26 = CreateLine(34.5, 164, 34.5, 169.5)
			SetLineWidth(0.1, t26)
			t27 = CreateLine(172, 164, 172, 169.5)
			SetLineWidth(0.1, t27)
			t28 = CreateLine(178, 164, 178, 169.5)
			SetLineWidth(0.1, t28)
			t29 = CreateLine(22.5, 45, 27.5, 45)
			SetLineWidth(0.1, t29)
			t30 = CreateLine(22.5, 162, 27.5, 162)
			SetLineWidth(0.1, t30)
			t31 = CreateLine(179.5, 45, 184.5, 45)
			SetLineWidth(0.1, t31)
			t32 = CreateLine(179.5, 162, 184.5, 162)
			SetLineWidth(0.1, t32)
			self.quit()

root=Tk()
w=root.winfo_screenwidth()
h=root.winfo_screenheight()
root.geometry("+%d+%d" % ( (w-300)/2, (h-100)/2 ) )
try:
	import DiscID, CDDB, cdrom
except:
	app=Warning2(root,"You need to install python-cddb!\n"+
					"see http://cddb-py.sourceforge.net/\n"
					+"Gentoo : emerge cddb-py\n"
					+"Debian : apt-get install python-cddb\n"
					+"Mandrake : urpmi python-CDDB")
else:
	app=Application(root)
	
root.mainloop()
