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

Revision 1934, 7.2 kB (checked in by adi, 13 years ago)

Shebang-line cleanup. Closes: #292

Patch provided by Orcan Ogetbil (Fedora).

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