-
Notifications
You must be signed in to change notification settings - Fork 2
/
clientGUI.py
355 lines (253 loc) · 11.8 KB
/
clientGUI.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
from PyQt5 import QtCore, QtGui, QtWidgets
from clientInterface import Ui_MainWindow
from ftpClient import FTPclient
import sys
import os
import socket
class cleintInterface(Ui_MainWindow):
def __init__(self, ftpClientUI, ftpLogic):
Ui_MainWindow.__init__(self)
self.setupUi(ftpClientUI)
self.ftpLogic = ftpLogic
self.loginButton.clicked.connect(self.loginButtonClicked)
self.numFiles = 0
self.finerList = []
self.r = 0
self.passiveMode = True
self.selectedDir = ' '
self.a = False
self.b = False
self.nameDirec = ''
# ------------- Set up tree model for the local host window--------------------
self.clientDirectory = QtWidgets.QFileSystemModel()
self.clientDirectory.setRootPath(QtCore.QDir.rootPath())
self.localdir.setModel(self.clientDirectory)
self.localdir.setRootIndex(self.clientDirectory.setRootPath(QtCore.QDir.rootPath()))
self.pathSelectedItem = QtCore.QDir.rootPath()
self.localdir.header().resizeSection(0, 150)
# ----------------- End Tree View -----------------------------
def loginButtonClicked(self):
self.pasv.setStyleSheet("background-color: rgb(138, 226, 52);")
#self.ftpLogic.initConnection("127.0.1.1", int("21"))
#self.ftpLogic.login("Elias", "aswedeal")
self.ftpLogic.initConnection(self.hostname.text(), int(self.port.text()))
self.ftpLogic.login(self.username.text(), self.password.text())
self.generateLogTable()
self.statusMSG()
self.ftpLogic.setMode('I')
self.localdir.setEnabled(True)
if self.passiveMode:
self.ftpLogic.startPassiveDTPconnection()
else:
self.ftpLogic.startActiveConnection()
self.ftpLogic.getList()
self.getRemoteDirList()
# Set the number of rows according to the number of files
self.numFiles = len(self.finerList)
#--------------------Set the window for the remote directory----------------
self.remoteWindow()
self.logWindow()
self.generateRemoteTable()
# Check which file is selected on the remoted directory window
self.selectedFile()
# Check which file is selected on the local directory window
self.selectedLocalFile()
# Events handlers when buttons are pressed
self.noopButtonClicked()
self.homeDirButtonClicked()
self.logoutButtonClicked()
self.activeButtonClicked()
self.passiveButtonClicked()
self.makeDirButtonClicked()
self.oneClickselectedFile()
self.removeDirButtonClicked()
def remoteWindow(self):
self.remotedir.setRowCount(self.numFiles)
self.remotedir.setColumnCount(6)
self.remotedir.setColumnWidth(0, 230)
self.remotedir.setHorizontalHeaderItem(0, QtWidgets.QTableWidgetItem("Filename"))
self.remotedir.setHorizontalHeaderItem(1, QtWidgets.QTableWidgetItem("Last Modified"))
self.remotedir.setHorizontalHeaderItem(2, QtWidgets.QTableWidgetItem("Size"))
self.remotedir.setHorizontalHeaderItem(3, QtWidgets.QTableWidgetItem("Group"))
self.remotedir.setHorizontalHeaderItem(4, QtWidgets.QTableWidgetItem("User"))
self.remotedir.setHorizontalHeaderItem(5, QtWidgets.QTableWidgetItem("Permissions"))
def logWindow(self):
self.statusWindow.setColumnCount(1)
self.statusWindow.setColumnWidth(0, 820)
self.statusWindow.setHorizontalHeaderItem(0, QtWidgets.QTableWidgetItem("Latest Session Log"))
# ========================Buttons Event Handlers======================================
def passiveButtonClicked(self):
self.pasv.clicked.connect(self.pasvMode)
def pasvMode(self):
self.passiveMode = True
self.pasv.setStyleSheet("background-color: rgb(138, 226, 52);")
self.active.setStyleSheet("")
def activeButtonClicked(self):
self.active.clicked.connect(self.actMode)
def actMode(self):
self.passiveMode = False
self.active.setStyleSheet("background-color: rgb(138, 226, 52);")
self.pasv.setStyleSheet("")
def oneClickselectedFile(self):
self.remotedir.cellClicked.connect(self.cellClickedOnce_)
def cellClickedOnce_(self, row, column):
self.nameDirec = self.remotedir.item(row,column).text().strip('\r')
self.a = True
def removeDirButtonClicked(self):
self.remove.clicked.connect(self.removeDirectory)
self.b = True
def removeDirectory(self):
#if both the directory and removeDiretory button are clicked then delete the dir
if self.a == True and self.b == True:
self.ftpLogic.remDir(self.nameDirec)
self.statusMSG()
self.generateLogTable()
self.b = False
def makeDirButtonClicked(self):
self.makeDir.clicked.connect(self.mkDir)
def mkDir(self):
self.ftpLogic.makeDir(self.dirName.text())
self.statusMSG()
self.generateLogTable()
#A file or folder is double clicked
def selectedFile(self):
self.remotedir.cellDoubleClicked.connect(self.cellDoubleClicked_)
self.statusMSG()
self.generateLogTable()
def cellDoubleClicked_(self, row, column):
item = self.remotedir.item(row,column).text()
if column ==0:
# Identify element with a . as a file else it is a folder
if item.find('.') != -1:
b = str(item).strip('\r')
self.downloadFile(b)
else:
b = str(item).strip('\r')
self.openDir(b)
def selectedLocalFile(self):
self.localdir.doubleClicked.connect(self.test)
def test(self, signal):
file_path= self.clientDirectory.filePath(signal)
if file_path.find('.') > 0:
self.uploadFile(file_path)
def logoutButtonClicked(self):
self.logoutButton.clicked.connect(self.Logout)
def Logout(self):
self.getRemoteDirList()
self.ftpLogic.logout()
self.ftpLogic.logout()
self.statusMSG()
def noopButtonClicked(self):
self.noop.clicked.connect(self.nooP)
def nooP(self):
self.ftpLogic.checkConnection()
self.statusMSG()
self.generateLogTable()
#=======================================End of Button Event Handlers================================
def statusMSG(self):
self.status.setText(self.ftpLogic.getStatus())
def generateLogTable(self):
self.statusWindow.setRowCount(len(self.ftpLogic.getComm()))
for row in range(len(self.ftpLogic.getComm())):
# Place files on the GUI
self.statusWindow.setItem(row,0, QtWidgets.QTableWidgetItem(self.ftpLogic.getComm()[len(self.ftpLogic.getComm())-row -1]))
self.ftpLogic.clearComm()
def treeViewClientDirectoryClicked(self, signal):
self.pathSelectedItem = self.localdir.model().filePath(signal)
print(self.pathSelectedItem)
self.statusMSG()
self.generateLogTable()
# Display the file and folders and its characteristic in the remote directory window
def generateRemoteTable(self):
self.numFiles = len(self.finerList)
templist = []
for row in range(self.numFiles):
items = self.finerList[row]
print(items)
templist.append(items[0])
templist.append(items[1] + ' ' + items[2])
templist.append(items[3])
templist.append( items[4])
templist.append(items[5] + ' ' +items[6] + ' ' + items[7] )
templist.append(items[8])
for col in range(6):
# Place files on the GUI
self.remotedir.setItem(row,col, QtWidgets.QTableWidgetItem(templist[5-col]))
templist.clear()
""" Gets the files and folders on server side and decomposes the result
so that it can be presented in a table format on the GUI """
def getRemoteDirList(self):
for row in range(len(self.finerList)):
for col in range(6):
self.remotedir.setItem(row,col, QtWidgets.QTableWidgetItem(''))
# Clear the List that stores files that are on the server everytime a directory is changed
self.finerList.clear()
self.dirList = self.ftpLogic.returnDirList()
print('Dir..............\n') #, self.dirList)
for element in self.dirList:
temp = element.split()
#print(len(temp), temp)
if(len(temp) <= 9 and temp != []):
# Avoid storing the null element at postion 1 or the arrray
self.finerList.append(temp)
elif(len(temp)> 9 ):
s = int(len(temp)/9)
print(s)
for i in range(s):
# Store the new files that are in the new directory
self.finerList.append(temp[9*i:9 + 9*i])
print(self.finerList)
def uploadFile(self, filePath):
if self.passiveMode:
self.ftpLogic.startPassiveDTPconnection()
else:
self.ftpLogic.startActiveConnection()
self.ftpLogic.uploadFile(filePath)
self.statusMSG()
self.generateLogTable()
def downloadFile(self, fileName):
if self.passiveMode:
self.ftpLogic.startPassiveDTPconnection()
else:
self.ftpLogic.startActiveConnection()
self.ftpLogic.downloadFile(fileName)
self.statusMSG()
self.generateLogTable()
# open a folder and show its contents on the GUI
def openDir(self,folderName):
self.ftpLogic.changeWD(folderName)
if self.passiveMode:
self.ftpLogic.startPassiveDTPconnection()
else:
self.ftpLogic.startActiveConnection()
self.ftpLogic.getList()
self.getRemoteDirList()
self.generateRemoteTable()
self.statusMSG()
self.generateLogTable()
def homeDirButtonClicked(self):
self.homedir.clicked.connect(self.toHomeDir)
self.statusMSG()
self.generateLogTable()
# Return to the home directory on remote host
def toHomeDir(self):
self.ftpLogic.changeWD('/')
if self.passiveMode:
self.ftpLogic.startPassiveDTPconnection()
else:
self.ftpLogic.startActiveConnection()
self.ftpLogic.getList()
self.getRemoteDirList()
self.generateRemoteTable()
self.statusMSG()
self.generateLogTable()
def Main():
clientName = socket.gethostbyname(socket.gethostname())
# Testing ftp servers
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
client = FTPclient(clientName)
application = cleintInterface(MainWindow, client)
MainWindow.show()
sys.exit(app.exec_())
Main()