|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -"""Generate a QR code image for an entry.""" |
3 | 2 |
|
4 | | -## PyAuth - Google Authenticator desktop application |
5 | | -## Copyright (C) 2016 Todd T Knarr <tknarr@silverglass.org> |
| 3 | +# Copyright (C) 2017 Silverglass Technical |
| 4 | +# Author: Todd Knarr (tknarr) |
6 | 5 |
|
7 | | -## This program is free software: you can redistribute it and/or modify |
8 | | -## it under the terms of the GNU General Public License as published by |
9 | | -## the Free Software Foundation, either version 3 of the License, or |
10 | | -## (at your option) any later version. |
11 | | - |
12 | | -## This program is distributed in the hope that it will be useful, |
13 | | -## but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | -## GNU General Public License for more details. |
16 | | - |
17 | | -## You should have received a copy of the GNU General Public License |
18 | | -## along with this program. If not, see http://www.gnu.org/licenses/ |
19 | | - |
20 | | -import qrtools |
21 | | -import tempfile |
22 | 6 | import wx |
23 | | -from io import BytesIO |
24 | 7 |
|
25 | 8 | import Configuration |
26 | | -import qrcode |
27 | | - |
28 | | - |
29 | | -class QrCodeUri: |
30 | | - """Represents a provisioning URI decoded from a QR code image.""" |
31 | | - |
32 | | - def __init__(self): |
33 | | - self.uri = None |
34 | | - self.temp_dir = Configuration.GetConfigDirectory() |
35 | | - self.qr = qrtools.QR() |
36 | | - |
37 | | - def GetUri(self): |
38 | | - return self.uri |
39 | | - |
40 | | - def decode_file(self, filename): |
41 | | - # TODO decode file |
42 | | - pass |
43 | | - |
44 | | - def decode_url(self, url): |
45 | | - # TODO fetch image from url and decode |
46 | | - pass |
47 | | - |
48 | | - def decode_image(self, image): |
49 | | - temp_file = tempfile.NamedTemporaryFile(dir = self.temp_dir, suffix = ".png") |
50 | | - # TODO write image into temp file |
51 | | - self.uri = self.decode_file(temp_file.name) |
52 | | - temp_file.close() |
53 | | - return self.uri |
54 | | - |
55 | | - |
56 | | -class QrCodeImage: |
57 | | - """Represents a QR code image.""" |
58 | | - |
59 | | - def __init__(self, uri, box_size): |
60 | | - self.uri = uri |
61 | | - self.box_size = box_size |
62 | | - |
63 | | - def GetImage(self): |
64 | | - qr = qrcode.QRCode(version = None, box_size = self.box_size, |
65 | | - error_correction = qrcode.constants.ERROR_CORRECT_M) |
66 | | - qr.add_data(self.uri) |
67 | | - qr.make(fit = True) |
68 | | - img = qr.make_image() |
69 | | - strm = BytesIO() |
70 | | - img.save(strm, "PNG") |
71 | | - strm.seek(0) |
72 | | - image = wx.ImageFromStream(strm, wx.BITMAP_TYPE_PNG) |
73 | | - return image |
| 9 | +from QrCodeImage import QrCodeImage |
74 | 10 |
|
75 | 11 |
|
76 | 12 | class QrCodePanel(wx.Panel): |
@@ -192,11 +128,3 @@ def UpdateBitmap(self): |
192 | 128 | frame_size = self.ClientSizeToFrameSize(client_size) |
193 | 129 | p = self.GetParent() |
194 | 130 | p.SetSize(frame_size) |
195 | | - |
196 | | - |
197 | | -class QrCodeFrame(wx.Frame): |
198 | | - def __init__(self, parent, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, |
199 | | - style = wx.DEFAULT_FRAME_STYLE, name = wx.FrameNameStr, uri = None, border = 0): |
200 | | - wx.Frame.__init__(self, parent, id, title, pos, size, style, name) |
201 | | - self.panel = QrCodePanel(self, uri = uri, border = border) |
202 | | - self.panel.SetFocus() |
0 commit comments