Commit 96d58401 authored by Kevin Whitaker's avatar Kevin Whitaker

Add some possible future fields and implement json config saving/loading.

parent acc5d258
from PySide2 import QtCore
import keyring
import json
import pyotp
SERVICE = "otpclient"
class OTPModel(QtCore.QObject):
_name=""
_OTPType=""
_secret=""
_icon=""
def __init__(self,name="",OTPType="",secret="",icon=""):
_timeout=30
_algo="SHA1"
def __init__(self,name="",OTPType="",secret="",icon="",timeout=30,algo="SHA1"):
QtCore.QObject.__init__(self)
self._name=name
self._OTPType=OTPType
self._secret=secret
self._icon=icon
self._timeout=timeout
self._algo=algo
nameChanged = QtCore.Signal()
@QtCore.Property(str,notify=nameChanged)
......@@ -48,6 +57,32 @@ class OTPModel(QtCore.QObject):
def setIcon(self,icon):
self._icon = icon
timeoutChanged = QtCore.Signal()
@QtCore.Property(int,notify=timeoutChanged)
def timeout(self):
return self._timeout
@timeout.setter
def setTimeout(self,timeout):
self._timeout = timeout
algoChanged = QtCore.Signal()
@QtCore.Property(str,notify=algoChanged)
def algo(self):
return self._algo
@algo.setter
def setAlgo(self,algo):
self._algo = algo
def getOTPCode(self):
if self._OTPType == "TOTP":
return pyotp.TOTP(self._secret).now()
elif self._OTPType == "HOTP":
return pyotp.HOTP(self._secret).now()
else:
return None
class OPTListModel(QtCore.QAbstractListModel):
_items=[]
......@@ -57,6 +92,28 @@ class OPTListModel(QtCore.QAbstractListModel):
def setup(self, list):
self._items = list
def load(self):
jsonDict = json.loads(keyring.get_password(SERVICE,"__config"))
size = len(self._items)
self._items = []
for item in jsonDict:
self._items.append(OTPModel(item["name"],item["OTPType"],keyring.get_password(SERVICE,item["name"]),item["icon"],item["timeout"],item["algo"]))
self.dataChanged.emit(self.index(0,0),self.index(size-1,0))
def save(self):
dict=[]
for item in self._items:
dItem = {}
dItem["name"] = item.name
dItem["OTPType"] = item.OTPType
dItem["icon"] = item.icon
dItem["timeout"] = item.timeout
dItem["algo"] = item.algo
keyring.set_password(SERVICE,item.name,item.secret)
dict.append(dItem)
keyring.set_password(SERVICE,"__config",json.dumps(dict))
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self._items)
......
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtWidgets import QApplication
import OTPListModel
import keyring
import json
qapp = QApplication()
app = QQmlApplicationEngine()
model = OTPListModel.OPTListModel()
#keyring.set_password("test","boo","yeah")
app.rootContext().setContextProperty("otpListModel", model)
app.load("qml/main.qml")
qapp.exec_()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment