root/trunk/libffado/support/mixer-qt4/ffado/registration.py

Revision 2689, 7.9 kB (checked in by jwoithe, 7 years ago)

ffado-mixer: the registration code utilises urlopen() and urlencode() which have moved from urllib to urllib.request and urllib.parse respectively. urlencode() also requires additional treatment in python3. Allow for all this in a python2 compatible way. The original suggestion for the python3 code was from Xavier Forestier.

  • Property svn:mergeinfo set to
Line 
1 #
2 # Copyright (C) 2008-2009 by Pieter Palmers
3 #               2009      by Arnold Krille
4 #
5 # This file is part of FFADO
6 # FFADO = Free Firewire (pro-)audio drivers for linux
7 #
8 # FFADO is based upon FreeBoB.
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23
24 import urllib
25 import os
26
27 # Python3 renamed ConfigParser to configparser.  Deal with this in a way
28 # which maintains compatibility with python2.
29 try:
30     from configparser import SafeConfigParser
31 except:
32     from ConfigParser import SafeConfigParser
33
34 # The urlopen()/urlencode() functions from urllib in python2 are in
35 # urllib.request and urllib.parse respectively under python2.
36 try:
37     import urllib.request, urllib.parse
38     url_newapi = 1
39 except ImportError:
40     import urllib
41     url_newapi = 0
42
43 from ffado.config import REGISTER_URL, INI_FILE_PATH, FFADO_CONFIG_DIR
44 from PyQt4.QtGui import QMessageBox
45 from PyQt4.QtCore import QByteArray
46
47 from ffado.regdialog import *
48
49 import logging
50 log = logging.getLogger('registration')
51
52 class ffado_registration:
53     def __init__(self, ffado_version,
54                        guid,
55                        vendor_id,
56                        model_id,
57                        vendor_string,
58                        model_string):
59         # only use the section before the SVN mark
60         # we don't need to keep track of all SVN version changes
61         self.ffado_version = ffado_version.split('-')[0]
62         self.guid = guid
63         self.vendor_id = vendor_id
64         self.model_id = model_id
65         self.vendor_string = vendor_string
66         self.model_string = model_string
67
68         #check if config file path exists, if not, create it
69         config_path = os.path.expanduser(FFADO_CONFIG_DIR)
70         if not os.path.exists(config_path):
71             os.makedirs(config_path)
72
73         # parse the ini file
74         self.config_filename = os.path.expanduser(INI_FILE_PATH)
75         self.parser = SafeConfigParser()
76         self.parser.read(self.config_filename)
77         self.section_name = "%s:%X" % (self.ffado_version, self.guid)
78         self.email = "(optional)"
79         if self.parser.has_section("history") \
80            and self.parser.has_option("history", "email"):
81             self.email = self.parser.get("history", "email")
82
83     def register_ffado_usage(self):
84         post_vals = {}
85         post_vals['guid'] = self.guid
86         post_vals['vendor_id'] = self.vendor_id
87         post_vals['model_id'] = self.model_id
88         post_vals['vendor_string'] = self.vendor_string
89         post_vals['model_string'] = self.model_string
90         post_vals['ffado_version'] = self.ffado_version
91         post_vals['email'] = self.email
92
93         try:
94             if url_newapi == 1:
95                 response = urllib.request.urlopen(REGISTER_URL,
96                                                   urllib.parse.urlencode(post_vals).encode('ascii'))
97             else:
98                 response = urllib.urlopen(REGISTER_URL,
99                                           urllib.urlencode(post_vals))
100         except:
101             log.error("failed, network error")
102             return (-1, "Network Error")
103    
104         lines = response.readlines()
105        
106         ok = False
107         errline = "Bad response from server"
108         for i in range(len(lines)):
109             if lines[i][0:10] == b"RESULT: OK":
110                 ok = True
111             elif lines[i][0:12] == b"RESULT: FAIL":
112                 ok = False
113                 if len(lines)>i+1:
114                     errline = lines[i+1]
115         if not ok:
116             log.info("registration failed %s" % errline)
117             return (-2, errline)
118         else:
119             return (0, "")
120    
121     def check_for(self, what):
122         if not self.parser.has_section(self.section_name):
123             return False
124         if not self.parser.has_option(self.section_name, what):
125             return False
126         return self.parser.getboolean(self.section_name, what)
127    
128     def check_if_already_registered(self):
129         return self.check_for("registered")
130    
131     def check_for_ignore(self):
132         return self.check_for("ignore")
133    
134     def mark(self, what, value):
135         if not self.parser.has_section(self.section_name):
136             self.parser.add_section(self.section_name)
137         self.parser.set(self.section_name, what, str(value))
138    
139     def mark_version_registered(self):
140         self.mark("registered", True)
141    
142     def mark_ignore_version(self):
143         self.mark("ignore", True)
144
145     def remember_email(self, email):
146         if not self.parser.has_section("history"):
147             self.parser.add_section("history")
148         self.parser.set("history", "email", str(email))
149
150     def check_for_registration(self):
151
152         if self.check_for_ignore():
153             log.debug("user requested to ignore registration")
154         else:
155             if self.check_if_already_registered():
156                 log.debug("version/GUID combo already registered")
157             else:
158                 log.debug("show dialog...")
159
160                 dlg = ffadoRegDialog(self.vendor_string, "0x%X" % self.vendor_id,
161                                      self.model_string, "0x%X" % self.model_id,
162                                      "0x%016X" % self.guid, self.ffado_version,
163                                      self.email)
164                 dlg.exec_()
165
166                 if dlg.choice == "neversend":
167                     self.mark_ignore_version()
168                 elif dlg.choice == "send":
169                     asciiData = dlg.getEmail().toAscii()
170                     self.email = asciiData.data()
171                     self.remember_email(self.email)
172
173                     retval = self.register_ffado_usage()
174                     msg = QMessageBox()
175                     if retval[0] == 0:
176                         log.debug("registration successful")
177                         devinfomsg = "<p>Device: %s %s<br> Vendor/Model Id: %X/%X<br>Device GUID: %016X<br>FFADO Version: %s<br>E-Mail: %s</p>" % \
178                             (self.vendor_string, self.model_string, self.vendor_id, self.model_id, self.guid, self.ffado_version, self.email)
179                         tmp = msg.question( msg, "Registration Successful",
180                                             "<qt><b>Thank you.</b>" +
181                                             "<p>The registration of the following information was successful:</p>" +
182                                             devinfomsg +
183                                             "</p>For this device you won't be asked to register again until you upgrade to a new version of FFADO.</p>",
184                                             QMessageBox.Ok )
185                         self.mark_version_registered()
186                     else:
187                         log.error("error: " + retval[1])
188                         tmp = msg.question( msg, "Registration Failed",
189                                             "<qt><b>The registration at ffado.org failed.</b>" +
190                                             "<p>Error message:</p><p>" + retval[1] +
191                                             "</p><p>Try again next time?</p></qt>",
192                                             QMessageBox.Yes, QMessageBox.No )
193                         if tmp == 4:
194                             self.mark_ignore_version()
195                 elif dlg.choice == "nosend":
196                     pass
197         # write the updated config
198         f = open(self.config_filename, "w+")
199         self.parser.write(f)
200         f.close()
201
202 # vim: et
Note: See TracBrowser for help on using the browser.