All pastes #811125 Raw Edit

Mine

public text v1 · immutable
·published 2007-12-10 17:05 UTC
rendered paste body
# -*- coding: latin-1 -*-
import wx
import time
import locale
import os

LANDLINE = 1
MOBILE = 2

numbers_mobile = []
numbers_landline = []
numbers_both = []
current_list = []
current_numbers = []

app_name = "GetWired Phone Dialer"

class MainWindow(wx.Frame):
	def __init__(self,parent,id,title):
		self.call_type = 1

		wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(800,600), style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))
		menubar = wx.MenuBar()
		menu_file = wx.Menu()
		menu_help = wx.Menu()

		menu_file.Append(101, "E&xit", "Exit GetWired phone dialer.")
		menu_help.Append(201, "&About", "Display information about this program.")

		menubar.Append(menu_file, '&File')
		menubar.Append(menu_help, '&Help')

		self.SetMenuBar(menubar)
		self.CreateStatusBar()

		self.panel = wx.Panel(self, -1)
		  
		self.number_entry = wx.TextCtrl(self.panel, -1, '', (150,450), (550, 50), style=wx.TE_RIGHT)
		self.number_entry.SetFont( wx.Font(26, wx.MODERN, wx.NORMAL, wx.BOLD) )
		
		self.dial_button = wx.Button(self.panel, 11, 'Dial', (710,450), (70,50),5)
		self.dial_button.SetDefault()	
		self.dial_button.SetFont (wx.Font(14, wx.NORMAL, wx.NORMAL, wx.NORMAL))

		keypad_label = wx.StaticText(self.panel, -1, "Dial desired phone number:", (10,195))	
		keypad_label.SetFont( wx.Font(14, wx.NORMAL, wx.NORMAL, wx.NORMAL) )

		dial_buttons_font = wx.Font(20, wx.MODERN, wx.NORMAL, wx.BOLD)
		self.dial_buttons = []

		x = 0
		y = 1
		button_size = 70
		y_offset = 170
		x_offset = 10

		self.dial_buttons.append(wx.Button(self.panel, 10, "0", (x_offset + button_size, y_offset + button_size*4), (button_size,button_size)))
		self.dial_buttons[0].SetFont( dial_buttons_font )

		for i in range(1,10):
			if i in (4,7):
				x = 0
				y += 1
			self.dial_buttons.append(wx.Button(self.panel, i, str(i), (x + x_offset, y*button_size + y_offset), (button_size,button_size)))
			self.dial_buttons[i].SetFont( dial_buttons_font )
			x += button_size


		dest_type_label = wx.StaticText(self.panel, -1, "Choose mobile or landline:", (10,10) )
		dest_type_label.SetFont( wx.Font(14, wx.NORMAL, wx.NORMAL, wx.NORMAL) )
		
		radio_landline = wx.RadioButton(self.panel, 1, "Landline", (20,40), (100,20), style=wx.RB_GROUP)
		radio_mobile = wx.RadioButton(self.panel, 2, "Mobile", (20,70), (100,20))
		radio_mobile.SetFont( wx.Font(14, wx.NORMAL, wx.NORMAL, wx.NORMAL) )
		radio_landline.SetFont( wx.Font(14, wx.NORMAL, wx.NORMAL, wx.NORMAL) )

		self.landline_img = wx.Bitmap('landline.bmp')
		self.mobile_img = wx.Bitmap('mobile.bmp')

		countries_label = wx.StaticText(self.panel, -1, "Choose which country you wish to call:", (10,110))	
		countries_label.SetFont( wx.Font(14, wx.NORMAL, wx.NORMAL, wx.NORMAL) )
		self.countries_list = wx.Choice(self.panel, -1, (10,150), (300,200))
		self.countries_list.SetFont( wx.Font(12, wx.NORMAL, wx.NORMAL, wx.BOLD) )
		self.countries_list.SetForegroundColour('#4444CC')

		
		self.Bind(wx.EVT_BUTTON, self.OnClick)
		self.Bind(wx.EVT_MENU, self.OnClick)
		self.Bind(wx.EVT_RADIOBUTTON, self.OnRadio)
		self.Bind(wx.EVT_PAINT, self.OnPaint)

		self.Centre()
	
	def OnPaint (self, event):
		dc = wx.PaintDC(self.panel)
		if self.call_type == 1:
			img = self.landline_img
		elif self.call_type == 2:
			img = self.mobile_img

		dc.DrawBitmap(img, 150, 32)
		wx.PaintDC(self)
				
	def OnClick (self, evt):
		if evt.GetId():
			id = evt.GetId()
			if id in range(1,11):
				if id == 10: id = 0
				self.number_entry.AppendText(str(id))
				return
			if id == 11:
				dial_number()
			if id == 101:
				self.Close()
			if id == 201:
				self.ShowHelp()

	def OnRadio (self, evt):
		self.call_type = evt.GetId()
		self.Refresh()
		populate_list(self.call_type)

	def ShowHelp (self):
		message_dialog('About', 'GetWired telephone dialer.\r\nBy Bob Farrell, copyright (c) 2007.')


class CallDialog(wx.Dialog):
	def __init__ (self, parent, id, title, prefix_num, call_rate, base_rate, country):
		self.phone_number = title
		self.country = country
		self.call_rate = call_rate
		self.base_rate = base_rate
		wx.Dialog.__init__(self, parent, id, title, size=(400,220), style=0)
		main_font = wx.Font(14, wx.NORMAL, wx.NORMAL, wx.NORMAL)
		now_calling = wx.StaticText(self, -1, "", (0, 20), (400,70), style=wx.ALIGN_CENTER | wx.ST_NO_AUTORESIZE)
		now_calling.SetLabel("Now calling (" + str(call_rate) + "p/min):")
		now_calling.SetFont(main_font)

		number_dialed = wx.StaticText(self, -1, "", (0,45), (400,70), style=wx.ALIGN_CENTER | wx.ST_NO_AUTORESIZE)
		number_dialed.SetFont(main_font)
		number_dialed.SetLabel(self.phone_number)
		number_dialed.SetForegroundColour(wx.BLUE)	

		self.call_length = wx.StaticText(self, -1, "Call Duration: 00:00:00", (0,80), (400,20), style=wx.ALIGN_CENTER | wx.ST_NO_AUTORESIZE)
		self.call_length.SetFont(main_font)
		self.call_length.SetForegroundColour('sea green')

		self.cost = wx.StaticText(self, -1, "Call Cost: £0.00", (0,105), (400,20), style=wx.ALIGN_CENTER | wx.ST_NO_AUTORESIZE)
		self.cost.SetFont( main_font )
		end_button = wx.Button(self, -1, "End Call", (150, 150), (100,50), style=wx.CENTER)
		end_button.SetFont( wx.Font(14, wx.NORMAL, wx.NORMAL, wx.BOLD) )
		self.Center()
		self.Bind(wx.EVT_BUTTON, self.OnClick)
		self.StartTimer()
	
	def OnClick (self, evt):
		self.timer.Stop()
		
		try:
			if not os.path.exists('call_log.html'):
				f = open('call_log.html', 'w')
				f.write('<html><body> <table border=1 cellpaddiing=5><tr height=30><td><font size=4><b>Time of Call:</td><td><font size=4><b>Country Called:</td><td><font size=4><b>Number Called:</td><td><font size=4><b>Call Duration:</td><td><font size=4><b>Call Cost:</b></td></tr>\r\n')
				f.close()

			f = open('call_log.html', 'a')
			ps = u'£'	
			f.write( '<tr><td>' + time.strftime('%c') + '</td><td>' + self.country + '</td><td>' + self.phone_number + '</td><td>' + self.call_length.GetLabel().split('Call Duration:')[1] + '</td><td>£'.encode('latin-1') + ps.encode('latin-1') + str(self.pounds) + '</td></tr>\r\n' )
			f.close()
		except:
			raise

		app.frame.number_entry.SetValue('')
		
		self.Destroy()

	
	def TimerEvent (self, evt):
		self.call_length_seconds = time.time() - self.call_start
		label = str( time.strftime( "Call Duration: %H:%M:%S", time.strptime( time.ctime(self.call_length_seconds) ) ) )
		self.call_length.SetLabel( label )

		label = (( int(self.call_length_seconds / 60) * self.call_rate) + self.base_rate) / 100.0
		self.pounds = str("%.02f" % float(label))
		self.cost.SetLabel("Call Cost: £" + self.pounds) 

	def StartTimer (self):
		self.call_start = time.time()
		self.timer = wx.Timer(self)
		self.Bind(wx.EVT_TIMER, self.TimerEvent, self.timer)
		self.timer.Start(1000)

		
class App(wx.App):
	def OnInit(self):
		self.frame = MainWindow(None, -1, app_name)
		self.frame.SetToolTip(wx.ToolTip(app_name))
		self.frame.SetPosition(wx.Point(0,0))
		self.frame.SetSize(wx.Size(800,600))
		self.frame.SetTitle(app_name)
		self.frame.Show(True)
		return True
def message_dialog (title, msg):
		dlg = wx.MessageDialog(app.frame, msg, title, wx.OK)
		dlg.ShowModal()
		dlg.Destroy()

def dial_number ():
	base_rate = 100

	if app.frame.countries_list.GetSelection() == wx.NOT_FOUND:
		message_dialog('Error', 'Please select a country from the list.')
		return

	if app.frame.number_entry.GetValue() == "":
		message_dialog('Error', 'Please enter the number you with to call.')
		return

	sel = app.frame.countries_list.GetSelection()

	prefix_num = current_numbers[ sel ][0]
	call_rate = float( current_numbers[ sel ][1] )
	dia = CallDialog(app.frame, -1, app.frame.number_entry.GetValue(), prefix_num, call_rate, base_rate, current_list[sel].split('::')[0] )
	val = dia.ShowModal()

def populate_list (which_list=None):
	global numbers_mobile
	global numbers_landline
	global numbers_both
	global current_list
	global current_numbers

	if which_list:
		current_list = []
		current_numbers = []

		if which_list == MOBILE:
			list = numbers_mobile
		elif which_list == LANDLINE:
			list = numbers_landline


		for item in list:
			current_list.append(item)

		list = numbers_both
		for item in list:
			current_list.append(item)

		current_list.sort()

		app.frame.countries_list.Clear()
		for item in current_list:
			app.frame.countries_list.Append(item.split('::')[0])
			current_numbers.append( [ item.split('::')[2], item.split('::')[3] ] )
		return


	try:
		f = open('rates.dat', 'r')
		for line in f:
			number_type = line.split('::')[1]

			if number_type == 'landline':
				numbers_landline.append(line)
			if number_type == 'mobile':
				numbers_mobile.append(line)
			if number_type == 'landline/mobile':
				numbers_both.append(line)

	except:
		pass

app = App(0)
populate_list()
populate_list(LANDLINE)
app.MainLoop()