-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcls_Frm_AccActions.py
More file actions
289 lines (225 loc) · 7.82 KB
/
cls_Frm_AccActions.py
File metadata and controls
289 lines (225 loc) · 7.82 KB
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
#!/usr/bin/env python3
############################################
#### cls_Frm_AccActions.py ####
#### Version 20250506 grid ####
############################################
import tkinter as tk
from tkinter import messagebox
import myTheme as skin
import nvpnPort as nvpnT
from cls_Dlg_URLWndw import URLDialog as urlMsgBox
import sys
import os
import time
import json
import subprocess
import psutil
import asyncio
"""
--------------------
AccountActionsFrame
--------------------
Contains 3 buttons.
LogIn
LogOut
StartServices
In nvpnPort.getNvpnItem['login'] :
# Either the user is already logged in. The response will be "You are already logged in."
# or ...
# A html link is returned to be opened in a browser window
# e.g.: "Continue in the browser: https://api.nordvpn.com/v1/users/oauth/login-redirect?attempt=2e7e88d5-1ee5-4ac4-9a47-3ded0f0307a2"
# embedded html because of authentication occurs outside this app.
nvpnTnnlResponse = rawLginResponse
# "https://api.nordvpn.com/v1/users/oauth/login-redirect?"#
# handleResponseStrings( rawLginResponse )
From 'nvpnPort' we get the result of 'nordvpn login'.
-> When already logged in the result is "You are already logged in."
-> If not logged in, the result contains an url that we launch off the cmmand line.
"""
global extAuthStarted
extAuthStarted = False
global curLogInStts
curLogInStts = False
async def startService():
# As the service is started with systemctl, the user should be prompted for admin rights automatically
stProcessResp = subprocess.run( ["systemctl","start", "nordvpnd"], capture_output = True )
print(f"AccountActionsFrame.async-startService() start the service response: { stProcessResp }" )
return stProcessResp
class AccountActionsFrame( tk.Frame ):
def __init__(
self,
master = None,
dimensions = [ 400, 400 ]
):
super().__init__( master )
global extAuthStarted
global curLogInStts
self.master = master
self.configure(background=skin.myBlack)
self.grid()
print(f"AccountActionsFrame called with dimensions: { dimensions }")
self.dimensions = dimensions
self.frmAccActnsLIArr = []
self.frmAccActnsLIStr = ""
self.frmAccActnsLIUrl = ""
self.aaLblBGClr = skin.myLRed
self.frmAccActnsGrid = tk.Frame(
self,
bg = skin.myBlack,
pady = 10,
padx = 10
)
self.frmAccActnsStatTitlelLbl = tk.Label(
self.frmAccActnsGrid,
bg = skin.myWhite,
fg = skin.myBlack,
font = skin.provideFont("H"),
text = "Log In Status"
)
self.frmAccActnsLogInStatusLbl = tk.Label(
self.frmAccActnsGrid,
text = self.frmAccActnsLIStr,
bg = self.aaLblBGClr,
fg = skin.myBlack,
padx = 4,
pady = 4,
wraplength = 280
)
self.frmAccActnsAwaitAuthLbl = tk.Label(
self.frmAccActnsGrid,
text = "Waiting for external log in",
bg = skin.myLGreen,
fg = skin.myDRed,
padx = 4,
pady = 4
)
self.frmAccActnsLogInBttn = tk.Button(
self.frmAccActnsGrid,
command = self.launchURL,
text = f"LOG IN\n(opens a webbrowser)",
bg = skin.myBlack,
fg = skin.myNYellow
)
self.frmAccActnsLogOutBttn = tk.Button(
self.frmAccActnsGrid,
command = self.startLogOut,
text = "LOG OUT",
bg = skin.myBlack,
fg = skin.myNYellow
)
self.frmAccActnsWidgets = [
self.frmAccActnsStatTitlelLbl, # 0
self.frmAccActnsLogInStatusLbl, # 1
self.frmAccActnsAwaitAuthLbl, # 2
self.frmAccActnsLogInBttn, # 3
self.frmAccActnsLogOutBttn # 4
]
self.refreshData()
self.initGrid()
def showParams( self ):
global extAuthStarted
global curLogInStts
paramObj = {
'extAuthStarted': extAuthStarted,
'curLogInStts': curLogInStts,
'LIString': self.frmAccActnsLIStr,
'URL': self.frmAccActnsLIUrl,
'lblBG': self.aaLblBGClr
}
# print(f"showParams: {json.dumps(paramObj, indent=4)}")
def launchStartService( self ):
#return asyncio.run( startService() );
self.showParams()
def launchURL( self ):
global extAuthStarted
extAuthStarted = True
urlPopUp = urlMsgBox(self, url = self.frmAccActnsLIUrl ) # "https://example.com"
self.wait_window(urlPopUp.dlgRoot)
print(f"launchURL| --urlPopUp.result: {urlPopUp.result}")
self.showParams()
def startLogOut( self ):
global extAuthStarted
global curLogInStts
confMsg = messagebox.askyesno(f"Confirm Log Out", f"Do you really want to log out?")
print(f"startLogOut| --confMsg: { confMsg }" )
if confMsg:
nvpnT.getNvpnItem("logout")
extAuthStarted = False
curLogInStts = False
def refreshData( self ):
# Get the login status from nvpnPort, then launch 'doLayOut'
global extAuthStarted
global curLogInStts
self.showParams()
if not extAuthStarted:
self.frmAccActnsLIArr = nvpnT.getNvpnItem( 'login' )
self.frmAccActnsLIStr = str( self.frmAccActnsLIArr[0] )
self.showParams()
if( "You are already logged in." == self.frmAccActnsLIStr ):
extAuthStarted = True
curLogInStts = True
self.frmAccActnsLIStr = "You are already logged in."
self.aaLblBGClr = skin.myTrue
# print(f"finished changing params for logged IN ")
# skip the rest if logged in, services and extAuth are not needed
# self.doLayout()
else:
curLogInStts = False
self.aaLblBGClr = skin.myFalse
if extAuthStarted:
self.frmAccActnsLIStr = "Awaiting external login (webbrowser)"
else:
if( "https://" in self.frmAccActnsLIStr ):
# ['Continue in the browser: https://api.nordvpn.com/v1/users/oauth/login-redirect?attempt=3d1b4c1f-1326-4d02-8bce-b0f2047df609']
self.frmAccActnsLIUrl = self.frmAccActnsLIStr[25:]
self.frmAccActnsLIStr = "You are logged OUT"
self.showParams()
self.doLayOut()
def hideWidget( self, widget ):
widget.grid_forget()
def showWidget( self, widget, rw, clmn):
widget.grid( row = rw, column = clmn )
def initGrid( self ):
self.frmAccActnsStatTitlelLbl.grid( row = 0, column = 0, sticky = 'WE' )
self.frmAccActnsLogInStatusLbl.grid(row = 1, column = 0, sticky = 'WE' )
self.frmAccActnsAwaitAuthLbl.grid( row = 2, column = 0, sticky = 'WE' )
self.frmAccActnsLogInBttn.grid( row = 3, column = 0, sticky = 'WE' )
self.frmAccActnsLogOutBttn.grid( row = 4, column = 0, sticky = 'WE' )
# And finally the grid container Frame
self.frmAccActnsGrid.grid( row = 0, column = 0)
# , sticky = 'WENS'
self.doLayOut()
def doLayOut( self ):
global extAuthStarted
global curLogInStts
# self.frmAccActnsStatTitlelLbl, # 0
# self.frmAccActnsLogInStatusLbl, # 1
# self.frmAccActnsAwaitAuthLbl, # 2
# self.frmAccActnsLogInBttn, # 3
# self.frmAccActnsLogOutBttn # 4
# Always show 0,1
self.frmAccActnsWidgets[1].configure( text = self.frmAccActnsLIStr )
if curLogInStts:
# show 4
# hide 2,3. Can't log in when already logged in, and no extAuth needed
# print(f"AccountActionsFrame.doLayout --curLogInStts: {curLogInStts} ")
self.hideWidget( self.frmAccActnsWidgets[2] )
self.hideWidget( self.frmAccActnsWidgets[3] )
self.frmAccActnsWidgets[1].configure( bg = skin.myTrue )
else:
# print(f"AccountActionsFrame.doLayout --curLogInStts==False: --extAuthStarted: { extAuthStarted }")
# hide 4. Can't log out when not logged in
self.hideWidget( self.frmAccActnsWidgets[4] )
self.frmAccActnsWidgets[1].configure( bg = skin.myFalse )
if extAuthStarted:
# show 2 should show url
# hide 3 the log in process has started. maybe keep if we dont catch fresh log in status
# print(f"AccountActionsFrame.doLayout --curLogInStts==False: --extAuthStarted==True")
self.hideWidget( self.frmAccActnsWidgets[3] )
self.frmAccActnsWidgets[1].configure( bg = skin.myLYellow )
else:
# show 3. Not logged in,
# hide 2. extAuth not started
# print(f"AccountActionsFrame.doLayout --curLogInStts==False: --extAuthStarted==False")
self.hideWidget( self.frmAccActnsWidgets[2] )
self.frmAccActnsWidgets[1].configure( bg = skin.myFalse )