| 1 |
import urllib |
|---|
| 2 |
import ConfigParser, os |
|---|
| 3 |
|
|---|
| 4 |
from ffadomixer_config import REGISTER_URL, INI_FILE_PATH, FFADO_CONFIG_DIR |
|---|
| 5 |
|
|---|
| 6 |
from qt import * |
|---|
| 7 |
from ffado_regdialog import * |
|---|
| 8 |
|
|---|
| 9 |
class ffado_registration: |
|---|
| 10 |
def __init__(self, ffado_version, |
|---|
| 11 |
guid, |
|---|
| 12 |
vendor_id, |
|---|
| 13 |
model_id, |
|---|
| 14 |
vendor_string, |
|---|
| 15 |
model_string): |
|---|
| 16 |
# only use the section before the SVN mark |
|---|
| 17 |
# we don't need to keep track of all SVN version changes |
|---|
| 18 |
self.ffado_version = ffado_version.split('-')[0] |
|---|
| 19 |
self.guid = guid |
|---|
| 20 |
self.vendor_id = vendor_id |
|---|
| 21 |
self.model_id = model_id |
|---|
| 22 |
self.vendor_string = vendor_string |
|---|
| 23 |
self.model_string = model_string |
|---|
| 24 |
|
|---|
| 25 |
#check if config file path exists, if not, create it |
|---|
| 26 |
config_path = os.path.expanduser(FFADO_CONFIG_DIR) |
|---|
| 27 |
if not os.path.exists(config_path): |
|---|
| 28 |
os.makedirs(config_path) |
|---|
| 29 |
|
|---|
| 30 |
# parse the ini file |
|---|
| 31 |
self.config_filename = os.path.expanduser(INI_FILE_PATH) |
|---|
| 32 |
self.parser = ConfigParser.SafeConfigParser() |
|---|
| 33 |
self.parser.read(self.config_filename) |
|---|
| 34 |
self.section_name = "%s:%X" % (self.ffado_version, self.guid) |
|---|
| 35 |
self.email = "(optional)" |
|---|
| 36 |
if self.parser.has_section("history") \ |
|---|
| 37 |
and self.parser.has_option("history", "email"): |
|---|
| 38 |
self.email = self.parser.get("history", "email") |
|---|
| 39 |
|
|---|
| 40 |
def register_ffado_usage(self): |
|---|
| 41 |
post_vals = {} |
|---|
| 42 |
post_vals['guid'] = self.guid |
|---|
| 43 |
post_vals['vendor_id'] = self.vendor_id |
|---|
| 44 |
post_vals['model_id'] = self.model_id |
|---|
| 45 |
post_vals['vendor_string'] = self.vendor_string |
|---|
| 46 |
post_vals['model_string'] = self.model_string |
|---|
| 47 |
post_vals['ffado_version'] = self.ffado_version |
|---|
| 48 |
post_vals['email'] = self.email |
|---|
| 49 |
|
|---|
| 50 |
try: |
|---|
| 51 |
response = urllib.urlopen(REGISTER_URL, |
|---|
| 52 |
urllib.urlencode(post_vals)) |
|---|
| 53 |
except: |
|---|
| 54 |
print "failed, network error" |
|---|
| 55 |
return (-1, "Network Error") |
|---|
| 56 |
|
|---|
| 57 |
lines = response.readlines() |
|---|
| 58 |
|
|---|
| 59 |
ok = False |
|---|
| 60 |
errline = "Bad response from server" |
|---|
| 61 |
for i in range(len(lines)): |
|---|
| 62 |
if lines[i][0:10] == "RESULT: OK": |
|---|
| 63 |
ok = True |
|---|
| 64 |
elif lines[i][0:12] == "RESULT: FAIL": |
|---|
| 65 |
ok = False |
|---|
| 66 |
if len(lines)>i+1: |
|---|
| 67 |
errline = lines[i+1] |
|---|
| 68 |
if not ok: |
|---|
| 69 |
print "registration failed" |
|---|
| 70 |
print " " + errline |
|---|
| 71 |
return (-2, errline) |
|---|
| 72 |
else: |
|---|
| 73 |
return (0, "") |
|---|
| 74 |
|
|---|
| 75 |
def check_for(self, what): |
|---|
| 76 |
if not self.parser.has_section(self.section_name): |
|---|
| 77 |
return False |
|---|
| 78 |
if not self.parser.has_option(self.section_name, what): |
|---|
| 79 |
return False |
|---|
| 80 |
return self.parser.getboolean(self.section_name, what) |
|---|
| 81 |
|
|---|
| 82 |
def check_if_already_registered(self): |
|---|
| 83 |
return self.check_for("registered") |
|---|
| 84 |
|
|---|
| 85 |
def check_for_ignore(self): |
|---|
| 86 |
return self.check_for("ignore") |
|---|
| 87 |
|
|---|
| 88 |
def mark(self, what, value): |
|---|
| 89 |
if not self.parser.has_section(self.section_name): |
|---|
| 90 |
self.parser.add_section(self.section_name) |
|---|
| 91 |
self.parser.set(self.section_name, what, str(value)) |
|---|
| 92 |
|
|---|
| 93 |
def mark_version_registered(self): |
|---|
| 94 |
self.mark("registered", True) |
|---|
| 95 |
|
|---|
| 96 |
def mark_ignore_version(self): |
|---|
| 97 |
self.mark("ignore", True) |
|---|
| 98 |
|
|---|
| 99 |
def remember_email(self, email): |
|---|
| 100 |
if not self.parser.has_section("history"): |
|---|
| 101 |
self.parser.add_section("history") |
|---|
| 102 |
self.parser.set("history", "email", str(email)) |
|---|
| 103 |
|
|---|
| 104 |
def check_for_registration(self): |
|---|
| 105 |
|
|---|
| 106 |
if self.check_for_ignore(): |
|---|
| 107 |
print "user requested to ignore registration" |
|---|
| 108 |
else: |
|---|
| 109 |
if self.check_if_already_registered(): |
|---|
| 110 |
print "version/GUID combo already registered" |
|---|
| 111 |
else: |
|---|
| 112 |
print "show dialog..." |
|---|
| 113 |
|
|---|
| 114 |
dlg = ffadoRegDialog(self.vendor_string, "0x%X" % self.vendor_id, |
|---|
| 115 |
self.model_string, "0x%X" % self.model_id, |
|---|
| 116 |
"0x%016X" % self.guid, self.ffado_version, |
|---|
| 117 |
self.email) |
|---|
| 118 |
dlg.exec_loop() |
|---|
| 119 |
|
|---|
| 120 |
#import pdb;pdb.set_trace() |
|---|
| 121 |
if dlg.choice == "neversend": |
|---|
| 122 |
self.mark_ignore_version() |
|---|
| 123 |
elif dlg.choice == "send": |
|---|
| 124 |
self.email = dlg.getEmail().latin1() |
|---|
| 125 |
self.remember_email(self.email) |
|---|
| 126 |
|
|---|
| 127 |
retval = self.register_ffado_usage() |
|---|
| 128 |
msg = QMessageBox() |
|---|
| 129 |
if retval[0] == 0: |
|---|
| 130 |
print "registration successful" |
|---|
| 131 |
devinfomsg = "<p>Device: %s %s<br> Vendor/Model Id: %X/%X<br>Device GUID: %016X<br>FFADO Version: %s<br>E-Mail: %s</p>" % \ |
|---|
| 132 |
(self.vendor_string, self.model_string, self.vendor_id, self.model_id, self.guid, self.ffado_version, self.email) |
|---|
| 133 |
tmp = msg.question( msg, "Registration Successful", |
|---|
| 134 |
"<qt><b>Thank you.</b>" + |
|---|
| 135 |
"<p>The registration of the following information was successful:</p>" + |
|---|
| 136 |
devinfomsg + |
|---|
| 137 |
"</p>For this device you won't be asked to register again until you upgrade to a new version of FFADO.</p>", |
|---|
| 138 |
QMessageBox.Ok ) |
|---|
| 139 |
self.mark_version_registered() |
|---|
| 140 |
else: |
|---|
| 141 |
print "error: " + retval[1] |
|---|
| 142 |
tmp = msg.question( msg, "Registration Failed", |
|---|
| 143 |
"<qt><b>The registration at ffado.org failed.</b>" + |
|---|
| 144 |
"<p>Error message:</p><p>" + retval[1] + |
|---|
| 145 |
"</p><p>Try again next time?</p></qt>", |
|---|
| 146 |
QMessageBox.Yes, QMessageBox.No ) |
|---|
| 147 |
if tmp == 4: |
|---|
| 148 |
self.mark_ignore_version() |
|---|
| 149 |
elif dlg.choice == "nosend": |
|---|
| 150 |
pass |
|---|
| 151 |
# write the updated config |
|---|
| 152 |
f = open(self.config_filename, "w+") |
|---|
| 153 |
self.parser.write(f) |
|---|
| 154 |
f.close() |
|---|