~iltony/antenna-dtv/trunk

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
#!/usr/bin/python
# coding: utf-8

import gtk
import gtk.glade
import thread
# ext modules
import os
import subprocess
from asyncproc import Process
import time
import gobject
import shared
import string
import vte
import collections
import thread
import datetime

TERM = "\n"
PREPEND = "clear"  + TERM
POSTPEND = "nc localhost 9997"  + TERM
SRC_PATH = shared.determine_path ()
SCAN_GLADE_PATH = os.path.join( SRC_PATH, "data", "scan_wizard.glade" )
LAUNCH_SCAN1 = "/usr/bin/w_scan -X -P -O 0 -E 0 " # -t 2
LAUNCH_SCAN2 = " >> ~/.tzap/new-channels.conf"
AFTER_SCAN = "mv ~/.tzap/new-channels.conf ~/.tzap/channels.conf"
KILL_TUNE = "killall /usr/bin/tzap"
KILL_SCAN = "killall w_scan"
CHANNEL_COUNT = "wc -l ~/.tzap/new-channels.conf | cut -d \" \" -f 1"
OLD_CHANNEL_COUNT = "wc -l ~/.tzap/channels.conf | cut -d \" \" -f 1"
SCAN_COMPLETED1 = "Scan took "
SCAN_COMPLETED2 = ". Found " 
SCAN_COMPLETED3 = " channels in the sky. Previously known channels were "
SCAN_COMPLETED4 = ". By hitting okay, Antenna will overwrite current channel list with recently acquired fresh ones."

def prepare (assistantobj, page):
	#print str(page) + "*"
	global comboview, speedcomboview, v, assistant
	# run only if we need scanning!
	# (i.e., after all options have been marked
	# and progress are going on)
	if (page == assistant.get_nth_page( 2 )):
		# get rid of everything
		shared.killOldClient()
		os.system(KILL_TUNE)
		os.system(KILL_SCAN)
		# our country
		sel = comboview.get_active_text()
		country = sel
		speed = speedcomboview.get_active_text().split(",")[0]
		# just in case... who knows?
		os.system("cp ~/.tzap/channels.conf ~/.tzap/channels\ \(backup\).conf")
		# scan command
		scans = LAUNCH_SCAN1 + "-c " + country + " -t " + speed + " " + LAUNCH_SCAN2 + TERM
		print scans
		# feed commands
		os.chdir( SRC_PATH )
		# process = subprocess.Popen('femon -H | cut -d " " -f 17', shell=True, stdout=subprocess.PIPE)
		# do scan (in a separate thread)
		worker_thread_id = thread.start_new_thread(scanthread,(scans,))
		gtk.gdk.threads_init ()
	 
# we're quite busy now!
def scanthread(scans):
	# needs some global
	global wTree, assistant
	myProc = Process(scans, shell=True)
	# retrieve useful objects
	obj = wTree.get_widget("textview1")
	objP = wTree.get_widget("progressbar1")
	objS = wTree.get_widget("table2")
	# informs we're going on
	print "Thread started"
	# start chronometer
	start = time.time()
	while True:
		# pooooolling
		poll = myProc.wait(os.WNOHANG) 
		if poll != None:
			break
		# what's up, w_scan?
		out = myProc.readerr()
		if out != "":
			shared.safeUpdateTextbox(obj,out)
		objP.pulse()
		time.sleep(.5)
		# break; just for testing next steps
	# stop chronometering
	diff = time.time() - start
	# format the difference
	difftext = datetime.datetime.utcfromtimestamp(diff).strftime("%H hours %M minutes and %S seconds")
	print "finished scanning"		
	
	# when finished
	# set progress bar to complete
	objP.set_fraction(1.0)
	# unlocks next steps
	assistant.set_page_complete( assistant.get_nth_page( 2 ), True)
	assistant.set_page_complete( assistant.get_nth_page( 3 ), True)

	# counts number of channels found
	fin, fout = os.popen4(CHANNEL_COUNT)
	number_of_channels = fout.read().split("\n")[0]

	# counts old number of channels found
	fin, fout = os.popen4(OLD_CHANNEL_COUNT)
	old_number_of_channels = fout.read().split("\n")[0]
		
	# update the report
	# looks like options which were set under gladexml file 
	# are totally ignored,
	# and it's not all.
	# This made me mad.
	# set_wrap on an already instantiated label did not work
	# and why?
	# WTF!!!
	# Have to create a NEW label, and only THEN
	# it will get the line_wrap option.
	# ****!
	label = gtk.Label(SCAN_COMPLETED1 + difftext + SCAN_COMPLETED2 + number_of_channels + SCAN_COMPLETED3 + old_number_of_channels + SCAN_COMPLETED4)
	label.set_line_wrap(True)
	objS.add(label)
	assistant.show_all()

# user accepted it all
def everythingsok(self,*args):
	# moves files gained trough scanning to main channel list
	os.system(AFTER_SCAN + TERM + POSTPEND)
	gtk.main_quit()

# someone might still want to exit at any time from the wizard
def get_out(widget, data=None): 
	# simply stop all operations pending...
	os.system(KILL_SCAN)
	# and get out of here!
	gtk.main_quit()  

def main(*args):

	global wTree, comboview, speedcomboview, v, assistant # damn! we need those globals! (read only though...)
	# gets the window designed with glade
	wTree = gtk.glade.XML( SCAN_GLADE_PATH )
	# the window itseld
	assistant = wTree.get_widget( "assistant1" )
	# wow! we might also click on the x!
	assistant.connect("delete_event", get_out)
	assistant.connect("cancel", get_out)
	assistant.connect("prepare", prepare)
	assistant.connect("apply",everythingsok)

	# initialize country list
	comboview =  wTree.get_widget( "combobox1" )
	# our land, our beloved land!
        fin,fout = os.popen4('locale | head -n1 | cut -d "=" -f 2 | cut -d "_" -f 2 | cut -c1-2')
	# and still it doesn't need no cleanup! shell power!
	mycountry = fout.read().split("\n")[0]
	# which countries does w_scan support? ask him!
	fin,fout = os.popen4("/usr/bin/w_scan -c?")
	countrylist = fout.read()
	countries = countrylist.split("\n")
	# huhu, we got a long list!
        i = 0
        myi = 0
	for country in countries:
		cids = country.split("\t") # we want only ISO3166...
		if (len(cids) > 2):                        
			cid = cids[1]
                        if (cid == mycountry): # lucky there, it's our contry! Patriotic hymn!
                                myi = i 
                        i = i +1
			cname = cids[3]
			comboview.append_text(cid) # we're there!

        comboview.set_active(myi) # sets default's country

	# speed comboview
	speedcomboview =  wTree.get_widget( "combobox2" ) 
        speedcomboview.set_active(0) # sets default's country

	# enables unbounded pages (third and fourth will come next)
	# note that by marking first and second page (0 and 1 in base0)
	# completed, third page is still accessibile
	assistant.set_page_complete( assistant.get_nth_page( 0 ), True)
	assistant.set_page_complete( assistant.get_nth_page( 1 ), True)

	# on air
	assistant.show_all() # is this all needed? who knows? but... who cares?
	gtk.main()

# let it run!
main()