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

Revision 2803, 8.1 kB (checked in by jwoithe, 3 years ago)

Cosmetic: capitalise "L" in "Linux".

"Linux" is a proper noun so it should start with a capital letter. These
changes are almost all within comments.

This patch was originally proposed by pander on the ffado-devel mailing
list. It has been expanded to cover all similar cases to maintain
consistency throughout the source tree.

  • 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 sys import version_info
44
45 from ffado.config import REGISTER_URL, INI_FILE_PATH, FFADO_CONFIG_DIR
46
47 # from PyQt4.QtGui import QMessageBox
48 # from PyQt4.QtCore import QByteArray
49 from ffado.import_pyqt import *
50
51 from ffado.regdialog import *
52
53 import logging
54 log = logging.getLogger('registration')
55
56 class ffado_registration:
57     def __init__(self, ffado_version,
58                        guid,
59                        vendor_id,
60                        model_id,
61                        vendor_string,
62                        model_string):
63         # only use the section before the SVN mark
64         # we don't need to keep track of all SVN version changes
65         self.ffado_version = ffado_version.split('-')[0]
66         self.guid = guid
67         self.vendor_id = vendor_id
68         self.model_id = model_id
69         self.vendor_string = vendor_string
70         self.model_string = model_string
71
72         #check if config file path exists, if not, create it
73         config_path = os.path.expanduser(FFADO_CONFIG_DIR)
74         if not os.path.exists(config_path):
75             os.makedirs(config_path)
76
77         # parse the ini file
78         self.config_filename = os.path.expanduser(INI_FILE_PATH)
79         self.parser = SafeConfigParser()
80         self.parser.read(self.config_filename)
81         self.section_name = "%s:%X" % (self.ffado_version, self.guid)
82         self.email = "(optional)"
83         if self.parser.has_section("history") \
84            and self.parser.has_option("history", "email"):
85             self.email = self.parser.get("history", "email")
86
87     def register_ffado_usage(self):
88         post_vals = {}
89         post_vals['guid'] = self.guid
90         post_vals['vendor_id'] = self.vendor_id
91         post_vals['model_id'] = self.model_id
92         post_vals['vendor_string'] = self.vendor_string
93         post_vals['model_string'] = self.model_string
94         post_vals['ffado_version'] = self.ffado_version
95         post_vals['email'] = self.email
96
97         try:
98             if url_newapi == 1:
99                 response = urllib.request.urlopen(REGISTER_URL,
100                                                   urllib.parse.urlencode(post_vals).encode('ascii'))
101             else:
102                 response = urllib.urlopen(REGISTER_URL,
103                                           urllib.urlencode(post_vals))
104         except:
105             log.error("failed, network error")
106             return (-1, "Network Error")
107    
108         lines = response.readlines()
109        
110         ok = False
111         errline = "Bad response from server"
112         for i in range(len(lines)):
113             if lines[i][0:10] == b"RESULT: OK":
114                 ok = True
115             elif lines[i][0:12] == b"RESULT: FAIL":
116                 ok = False
117                 if len(lines)>i+1:
118                     errline = lines[i+1]
119         if not ok:
120             log.info("registration failed %s" % errline)
121             return (-2, errline)
122         else:
123             return (0, "")
124    
125     def check_for(self, what):
126         if not self.parser.has_section(self.section_name):
127             return False
128         if not self.parser.has_option(self.section_name, what):
129             return False
130         return self.parser.getboolean(self.section_name, what)
131    
132     def check_if_already_registered(self):
133         return self.check_for("registered")
134    
135     def check_for_ignore(self):
136         return self.check_for("ignore")
137    
138     def mark(self, what, value):
139         if not self.parser.has_section(self.section_name):
140             self.parser.add_section(self.section_name)
141         self.parser.set(self.section_name, what, str(value))
142    
143     def mark_version_registered(self):
144         self.mark("registered", True)
145    
146     def mark_ignore_version(self):
147         self.mark("ignore", True)
148
149     def remember_email(self, email):
150         if not self.parser.has_section("history"):
151             self.parser.add_section("history")
152         self.parser.set("history", "email", str(email))
153
154     def check_for_registration(self):
155
156         if self.check_for_ignore():
157             log.debug("user requested to ignore registration")
158         else:
159             if self.check_if_already_registered():
160                 log.debug("version/GUID combo already registered")
161             else:
162                 log.debug("show dialog...")
163
164                 dlg = ffadoRegDialog(self.vendor_string, "0x%X" % self.vendor_id,
165                                      self.model_string, "0x%X" % self.model_id,
166                                      "0x%016X" % self.guid, self.ffado_version,
167                                      self.email)
168                 dlg.exec_()
169
170                 if dlg.choice == "neversend":
171                     self.mark_ignore_version()
172                 elif dlg.choice == "send":
173                     if version_info[0] < 3:
174                         # Python 2.x
175                         asciiData = dlg.getEmail().toAscii()
176                         self.email = asciiData.data()
177                     else:
178                         # Python 3 and above
179                         self.email = ascii(dlg.getEmail())
180                     self.remember_email(self.email)
181
182                     retval = self.register_ffado_usage()
183                     msg = QMessageBox()
184                     if retval[0] == 0:
185                         log.debug("registration successful")
186                         devinfomsg = "<p>Device: %s %s<br> Vendor/Model Id: %X/%X<br>Device GUID: %016X<br>FFADO Version: %s<br>E-Mail: %s</p>" % \
187                             (self.vendor_string, self.model_string, self.vendor_id, self.model_id, self.guid, self.ffado_version, self.email)
188                         tmp = msg.question( msg, "Registration Successful",
189                                             "<qt><b>Thank you.</b>" +
190                                             "<p>The registration of the following information was successful:</p>" +
191                                             devinfomsg +
192                                             "</p>For this device you won't be asked to register again until you upgrade to a new version of FFADO.</p>",
193                                             QMessageBox.Ok )
194                         self.mark_version_registered()
195                     else:
196                         log.error("error: " + retval[1])
197                         tmp = msg.question( msg, "Registration Failed",
198                                             "<qt><b>The registration at ffado.org failed.</b>" +
199                                             "<p>Error message:</p><p>" + retval[1] +
200                                             "</p><p>Try again next time?</p></qt>",
201                                             QMessageBox.Yes, QMessageBox.No )
202                         if tmp == 4:
203                             self.mark_ignore_version()
204                 elif dlg.choice == "nosend":
205                     pass
206         # write the updated config
207         f = open(self.config_filename, "w+")
208         self.parser.write(f)
209         f.close()
210
211 # vim: et
Note: See TracBrowser for help on using the browser.