-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
394 lines (339 loc) · 13.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
__version__ = "0.4.2"
# Set environment variables for firebase
import libs.firebase_config as firebase_config
import math
import threading
from time import time
from colorsys import rgb_to_hls, hls_to_rgb
import os.path
from libs.screens.root import Root
from libs.firebase import Firebase
from libs.utils import *
from kivy.core.clipboard import Clipboard
from kivy import platform
from kivy.animation import Animation
from kivy.core.window import Window
from kivy.properties import (
BooleanProperty,
ColorProperty,
get_color_from_hex,
StringProperty,
)
from kivy.clock import Clock
from kivymd.toast import toast
from kivymd.app import MDApp
# Current TODOs
# TODO: Add a way to export all the passwords in a csv file or json file.
# TODO: Merge Passlock Android and Passlock Desktop to 1 repo.
# Old TODOs
# TODO: Add date and time while backing up password to prevent restore all the time.
# FIXME: fix delete not working properly
# FIXME: on switching app in android device scroll view shifts up and nothing is visible..
def emulate_android_device(
pixels_horizontal=1080,
pixels_vertical=2240,
android_dpi=None,
monitor_dpi=157,
display_size_mobile=6.5,
):
if android_dpi is None:
android_dpi = int(
math.sqrt(pixels_horizontal**2 + pixels_vertical**2)
/ display_size_mobile
)
scale_factor = monitor_dpi / android_dpi
# Setting windows size messes up design in the app
Window.size = (scale_factor * pixels_horizontal,
scale_factor * pixels_vertical)
if platform == "android":
from libs.modules.AndroidAPI import statusbar, android_dark_mode
font_file = "kivymd/fonts/Poppins-Regular.ttf"
class MainApp(MDApp):
dark_mode = BooleanProperty(False)
extra_security = BooleanProperty(False)
entered_app = BooleanProperty(False)
text_color = ColorProperty()
primary_accent = ColorProperty()
primary_palette = StringProperty()
bg_color = ColorProperty()
email = StringProperty("DemoMail")
password_changed = False
system_dark_mode = False
backup_failure = False
encryption_class = None
update_dialog = None
exit_dialog = None
sync_widget = None
anim_sync = None
passwords = {}
encrypted_keys = {}
screen_history = []
path_to_live_ui = "backup_design.kv"
def __init__(self):
super().__init__()
from libs.save_config import SaveConfig
self.save_config = SaveConfig(
"auto_sync",
"dark_mode",
"system_dark_mode",
"backup_failure",
"extra_security",
"primary_palette",
)
self.theme_cls.font_styles.update(
{
"H1": [font_file, 96, False, -1.5],
"H2": [font_file, 60, False, -0.5],
"H3": [font_file, 48, False, 0],
"H4": [font_file, 34, False, 0.25],
"H5": [font_file, 24, False, 0],
"H6": [font_file, 20, False, 0.15],
"Button": [font_file, 14, True, 1.25],
"Subtitle1": [font_file, 16, False, 0.15],
"Body1": [font_file, 16, False, 0.5],
"Body2": [font_file, 14, False, 0.25],
}
)
self.primary_palette = get_primary_palette()
self.signup = False if os.path.exists("data/user_id.txt") else True
self.auto_sync = check_auto_sync()
self.extra_security = is_extra_security()
Window.on_minimize = lambda: self.backup_on_pause()
self.firebase = Firebase()
threading.Thread(target=self.set_dark_mode, daemon=True).start()
threading.Thread(target=self.set_user_mail, daemon=True).start()
def build(self):
self.root = Root()
self.root.load_screen("SignupScreen" if self.signup else "LoginScreen")
self.root.load_screen("HomeScreen", set_current=False)
if not self.signup:
self.root.LoginScreen.ids.password.focus = True
def on_primary_palette(self, instance, value):
self.theme_cls.primary_palette = value
self.set_theme_colors()
def set_theme_colors(self):
self.text_color = (
self.generate_color(lightness=0.25)
if not self.dark_mode
else self.generate_color(lightness=0.91)
)
self.light_color = self.generate_color()
self.bg_color_light = self.generate_color(lightness=0.98)
self.bg_color_light_hex = self.generate_color(
lightness=0.98, return_hex=True)
self.bg_color_dark = self.generate_color(darkness=0.1)
self.bg_color_dark_hex = self.generate_color(
darkness=0.1, return_hex=True)
self.bg_color = self.bg_color_dark if self.dark_mode else self.bg_color_light
self.dark_color = self.generate_color(darkness=0.18) # 262626
self.login_circle_light = self.generate_color(lightness=0.85)
self.primary_accent = self.dark_color if self.dark_mode else self.light_color
self.light_hex = self.generate_color(return_hex=True)
self.dark_hex = self.generate_color(darkness=0.18, return_hex=True)
def set_user_mail(self, *args):
self.email = get_email()
def backup(self, sync_widget):
def backup_success():
toast("Backup Successful")
self.backup_failure = False
sync_widget.stop()
self.password_changed = False
def backup_failure(*args):
print(*args)
self.backup_failure = True
toast("Couldn't backup :(, Check your internet connection")
sync_widget.stop()
sync_widget.icon = "cloud-upload"
sync_widget.text = "Backing up.."
sync_widget.start()
self.firebase.backup_success = lambda *args: backup_success()
self.firebase.backup_failure = lambda *args: backup_failure(*args)
self.firebase.backup()
def restore(self, sync_widget, user_id=None, decrypt=True):
def restore_success(req, result):
sync_widget.stop()
if result is not None:
from libs.utils import write_passwords
write_passwords(result)
if decrypt:
self.passwords = self.encryption_class.load_decrypted()
toast("Restored successfully")
else:
toast("No passwords to restore.")
def restore_failure(req, result):
sync_widget.stop()
toast("Restore Failed")
sync_widget.icon = "cloud-download"
sync_widget.text = "Restoring.."
sync_widget.start()
self.firebase.restore_success = lambda req, result: restore_success(
req, result)
self.firebase.restore_failure = lambda req, result: restore_failure(
req, result)
if user_id:
self.firebase.restore(user_id)
else:
self.firebase.restore()
def set_dark_mode(self):
self.system_dark_mode = is_dark_mode(system=True)
if platform == "android":
self.dark_mode = (
android_dark_mode() if self.system_dark_mode else is_dark_mode()
)
else:
self.dark_mode = is_dark_mode()
Clock.schedule_once(
lambda x: exec("self.entered_app = True", {"self": self}), 1
)
def show_toast_copied(self, item):
toast("Item copied")
Clipboard.copy(item)
def animate_signup(self, instance):
"""Animation to be shown when user enters the signup screen"""
if instance:
Animation(pos_hint={"top": 0.95}, opacity=1, d=0.5, t="out_back").start(
instance
)
def generate_color(
self,
hex_color=False,
color=None,
return_hex=False,
lightness=0.92,
darkness=0,
saturation=None,
):
"""
:param hex_color: Instead of passing color as list hexadecimal value can be passed.
:param color: Takes color like [.5,.5,.5, 1] as Parameter
:param return_hex: Boolean value if set true the function will return hexadecimal value.
:param lightness: Value from 0-1. If set to 1 it will return white and 0 will return original color.
:return:
"""
if hex_color:
color = get_color_from_hex(hex_color)
elif not color:
color = self.theme_cls.primary_color[:-1]
h, l, s = rgb_to_hls(*color)
l = lightness if not darkness else darkness
if saturation is None:
s = 0.7 if not darkness else 0.15
else:
s = saturation
color = list(hls_to_rgb(h, l, s))
if not return_hex:
return color + [1]
else:
r, g, b = color
_hex = (
hex(round(r * 255))[2:]
+ hex(round(g * 255))[2:]
+ hex(round(b * 255))[2:]
)
return _hex
def back_button(self, home_screen=False, *args):
if not home_screen:
self.screen_history.pop()
else:
self.screen_history = ["HomeScreen"]
self.root.transition.mode = "pop"
self.root.transition.direction = "right"
self.root.current = self.screen_history[-1]
def on_dark_mode(self, instance, mode):
if self.entered_app:
current_screen = self.root.current
if current_screen == "HomeScreen":
tab_manager = self.root.current_screen.ids.tab_manager
primary_color = Animation(
primary_accent=self.dark_color
if self.dark_mode
else self.light_color,
duration=0.3,
)
primary_color.start(self)
if tab_manager.current == "CreateScreen":
self.anim = Animation(
md_bg_color=self.bg_color_dark if mode else self.bg_color_light,
duration=0.3,
)
self.anim.start(self.root.HomeScreen)
primary_color.on_complete = self.set_theme_style
else:
self.set_theme_style()
def set_theme_style(self, *args):
print("theme_style set")
self.text_color = (
self.generate_color(lightness=0.25) # get_color_from_hex("611c05")
if not self.dark_mode
# get_color_from_hex("fde9e2")
else self.generate_color(lightness=0.91)
)
self.bg_color = self.bg_color_dark if self.dark_mode else self.bg_color_light
self.primary_accent = self.dark_color if self.dark_mode else self.light_color
if self.entered_app:
self.root.HomeScreen.ids.create.ids.dark_animation.rad = 0.1
if self.dark_mode:
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_hue = "300"
if platform == "android":
statusbar(
status_color=self.dark_hex,
nav_color=self.bg_color_dark_hex,
white_text=False,
)
else:
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_hue = "500"
if platform == "android":
statusbar(
status_color=self.light_hex,
nav_color=self.bg_color_light_hex,
white_text=True,
)
def toggle_mode(self, *args):
self.dark_mode = not self.dark_mode
def on_start(self):
"""Sets status bar color in android."""
if platform == "android":
statusbar(
status_color=self.dark_hex if self.dark_mode else self.light_hex,
nav_color=self.bg_color_dark_hex
if self.dark_mode
else self.bg_color_light_hex,
white_text=not self.dark_mode,
)
def backup_on_pause(self):
def success():
toast("Backed up!")
self.backup_failure = False
def failure(*args):
toast("Some Error occured couldn't backup!")
self.backup_failure = True
if self.auto_sync and self.password_changed:
self.firebase.backup()
self.firebase.backup_success = lambda *args: success()
self.firebase.backup_failure = lambda *args: failure(*args)
self.password_changed = False
def on_pause(self):
"""Saves data on pause."""
self.pause_start = time()
self.save_config.save_settings()
self.backup_on_pause()
return True
def on_resume(self):
"""Asks user to login after pausing app for specific time period"""
if (
self.extra_security
and not self.signup
and (time() - self.pause_start) > 300
):
self.root.load_screen("LoginScreen")
self.root.LoginScreen.ids.password.focus = True
self.root.LoginScreen.ids.password.text = ""
return True
def on_stop(self):
self.save_config.save_settings()
if __name__ == "__main__":
if platform != "android":
emulate_android_device()
MainApp().run()