/
start.py
395 lines (312 loc) · 12.1 KB
/
start.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
395
import os
import tkinter as tk
import tkinter.filedialog as tkfd
#import Tkinter as tk
import subprocess
#import tkFileDialog as tkfd
import database
import sys
import mainGui
import operator
import sync
from functools import partial
from PySide.QtCore import *
from PySide.QtGui import *
from PySide import QtGui
sourceDir = ''
destinationDir = ''
data = []
editing = False
ipAddress = ''
user = ''
class MainDialog(QDialog, mainGui.Ui_MainWindow):
def __init__(self, parent=None):
global data, ipAddress, user
super(MainDialog, self).__init__(parent)
self.setupUi(self)
self.connect(self.sourceButton, SIGNAL("clicked()"), partial(self.openFolderDialog, 'source'))
self.connect(self.destinationButton, SIGNAL("clicked()"), partial(self.openFolderDialog, 'destination'))
self.connect(self.addButton, SIGNAL("clicked()"), self.saveMap)
self.connect(self.tableView, SIGNAL("clicked(QModelIndex)"), self.rowSelected)
self.connect(self.syncButton, SIGNAL("clicked()"), self.sync)
self.connect(self.deleteButton, SIGNAL("clicked()"), self.deleteEntry)
self.connect(self.sourceEdit, SIGNAL("textChanged(QString)"), partial(self.editTextChanged, 'source'))
self.connect(self.destinationEdit, SIGNAL("textChanged(QString)"), partial(self.editTextChanged, 'destination'))
self.connect(self.syncAllButton, SIGNAL("clicked()"), self.syncAll)
self.connect(self.editButton, SIGNAL("clicked()"), self.edit)
self.setWindowIcon(QIcon('ibisCloud.png'))
# Create tray icon
self.createActions()
self.createTrayIcon()
self.trayIcon.activated.connect(self.iconActivated)
self.trayIcon.setToolTip('Ibis Cloud')
self.trayIcon.show()
self.addButton.setEnabled(False)
model = QFileSystemModel();
model.setRootPath(QDir.currentPath())
model.setFilter(QDir.Dirs | QDir.NoDotAndDotDot)
self.treeView.setModel(model)
self.treeView.setRootIndex(model.index(QDir.currentPath()))
self.treeView.hideColumn(1)
self.treeView.hideColumn(2)
self.treeView.hideColumn(3)
self.treeView.expandsOnDoubleClick()
self.treeView.show()
self.treeView.expandAll()
#Get server data
data = database.GetServerData()
ipAddress = data[0]
user = data[1]
#Get map data
data = database.GetMap()
i=0
for row in data:
row = list(row)
row.append('Not Synced')
data[i] = row
i+=1
self.updateTable()
self.tableView.resizeColumnsToContents()
def iconActivated(self, reason):
if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
self.showNormal()
def showMessage(self, duration, icon, message):
#duration = 3
#icon = QtGui.QSystemTrayIcon.Information
# self.trayIcon.showMessage('Ibis Cloud',
# 'Don\'t worry, your files will remain synchronized.', icon,
# duration * 1000)
self.trayIcon.showMessage('Ibis Cloud',
message, icon,
duration * 1000)
# Create tray actions
def createActions(self):
# self.minimizeAction = QtGui.QAction("Mi&nimize", self,
# triggered=self.hide)
# self.maximizeAction = QtGui.QAction("Ma&ximize", self,
# triggered=self.showMaximized)
self.restoreAction = QtGui.QAction("&Restore", self,
triggered=self.showNormal)
self.quitAction = QtGui.QAction("&Quit", self,
triggered=QtGui.qApp.quit)
# Create tray icon and its options
def createTrayIcon(self):
self.trayIconMenu = QtGui.QMenu(self)
# self.trayIconMenu.addAction(self.minimizeAction)
# self.trayIconMenu.addAction(self.maximizeAction)
self.trayIconMenu.addAction(self.restoreAction)
self.trayIconMenu.addSeparator()
self.trayIconMenu.addAction(self.quitAction)
self.trayIcon = QtGui.QSystemTrayIcon(self)
self.trayIcon.setIcon(QIcon('ibisCloud.png'))
self.trayIcon.setContextMenu(self.trayIconMenu)
# Software closed
def closeEvent(self, event):
if self.trayIcon.isVisible():
self.showMessage(3, QtGui.QSystemTrayIcon.Information, 'Don\'t worry, your files will remain synchronized.')
self.hide()
event.ignore()
# Textbox text changed
def editTextChanged(self, buttonName, path):
global sourceDir, destinationDir
if (buttonName == 'source'):
sourceDir = path
elif (buttonName == 'destination'):
destinationDir = path
self.enableAddButton()
# Map deleted
def deleteEntry(self):
global data
#TODO Stop synchronization if synced
row = self.getSelectedRow()
id = data[row][0]
database.deleteMapEntry(id)
data = list(data)
data.remove(data[row])
self.updateTable()
# Execute when a table entry is selected
def rowSelected(self):
global data
#Enable Buttons
self.enableButtons()
row = self.getSelectedRow()
if data[row][3] == 'Synced':
self.syncButton.setText(QtGui.QApplication.translate("MainWindow", "Stop Sync", None, QtGui.QApplication.UnicodeUTF8))
else:
self.syncButton.setText(QtGui.QApplication.translate("MainWindow", "Start Sync", None, QtGui.QApplication.UnicodeUTF8))
#Enable Buttons
def enableButtons(self):
self.syncButton.setEnabled(True)
self.editButton.setEnabled(True)
self.deleteButton.setEnabled(True)
#Disable Buttons
def disableButtons(self):
self.syncButton.setEnabled(False)
self.editButton.setEnabled(False)
self.deleteButton.setEnabled(False)
# Return the selected
def getSelectedRow(self):
rows=[]
for idx in self.tableView.selectedIndexes():
rows.append(idx.row())
return rows[0]
# Enable Add button if possible
def enableAddButton(self):
if (self.sourceEdit.text() != '' and self.destinationEdit.text() != ''):
self.addButton.setEnabled(True)
else:
self.addButton.setEnabled(False)
# Button Edit clicked
def edit(self):
global editing, sourceDir, destinationDir, data
editing = True
self.disableButtons()
row = self.getSelectedRow()
sourceDir = data[row][1]
destinationDir = data[row][2]
self.setText()
# Button Start Sync clicked
def sync(self):
global data
row = self.getSelectedRow()
if data[row][3] == 'Synced':
#TODO Implement Stop Sync
data[row][3] = 'Not Synced'
else:
#TODO Implement Sync
data[row][3] = 'Synced'
self.rowSelected()
self.updateTable()
# Button Sync All clicked
def syncAll(self):
global data
for row in data:
if row[3] == 'Not Synced':
#TODO Implement Sync
row[3] = 'Synced'
print(row)
#self.rowSelected()
self.updateTable()
# Update the data table
def updateTable(self):
global data
#Disable Buttons
self.disableButtons()
header=["Source", "Destination", "Status"]
table_model = MyTableModel(self, data, header)
self.tableView.setModel(table_model)
# Save map in the database
def saveMap(self):
global sourceDir, destinationDir, data, editing, user, ipAddress
# check if path exists and if folder is created
print(os.path.isdir(sourceDir))
print(os.path.exists(sourceDir))
destinationDir = destinationDir.replace('Server/', '[user]@[ip]' + ':Data/')
if (editing == True):
row = self.getSelectedRow()
id = data[row][0]
database.UpdateMap([sourceDir, destinationDir, str(id)])
editing = False
else:
database.AddMap([sourceDir, destinationDir])
sourceDir = ''
destinationDir = ''
data = database.GetMap()
i=0
for row in data:
row = list(row)
row.append('Not Synced')
data[i] = row
i+=1
self.setText()
self.updateTable()
self.enableAddButton()
# Open folder dialog to chose Source/Destination
def openFolderDialog(self, buttonName):
global sourceDir, destinationDir, ipAddress, user
root = tk.Tk()
root.withdraw()
rootFolder = repr(os.getcwd())
rootFolder = rootFolder.replace("\\\\", "\\").replace("'", '')
rootFolder += '\Folder_Structure'
if (buttonName == 'source'):
temp = sourceDir
sourceDir = tkfd.askdirectory(parent=root,initialdir="/",title='Please select the source directory')
if (sourceDir == ''):
sourceDir = temp
elif (buttonName == 'destination'):
temp = destinationDir
destinationDir = 'a'
# Download the folder structure from the server
#try:
cmd = 'rsync -a -v -z -ssh --delete --delete-excluded ' + '-f"+ */" -f"- *" ' + user + '@' + ipAddress + ':Data/ ' + sync.convertPathToCygdrive(rootFolder)
print (cmd)
#TODO insert
proc = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
remainder = str(proc.communicate()[0])
ibis = remainder.replace('\\n', '\n')
#print(remainder)
print(ibis)
# output = ''
# while not 'password' in output:
# output = proc.stdout.readline()
# print(output)
# proc.stdin.write('ibiscp')
#except:
# self.showMessage(3, QtGui.QSystemTrayIcon.Warning, 'Folder structure could not update!')
while (not rootFolder in destinationDir) and (destinationDir != ''):
destinationDir = tkfd.askdirectory(parent=root,initialdir=rootFolder,title='Please select the destination directory inside the folder: ' + rootFolder)
destinationDir = destinationDir.replace("/", "\\")
if (destinationDir == ''):
destinationDir = temp
# Change the folder path to server path
destinationDir = 'Server/' + destinationDir[(len(rootFolder)+1):]
self.setText()
self.enableAddButton()
# Update Source/Destination
def setText(self):
global sourceDir, destinationDir
self.sourceEdit.setText(sourceDir)
self.destinationEdit.setText(destinationDir)
class MyTableModel(QAbstractTableModel):
def __init__(self, parent, mylist, header, *args):
global ipAddress
QAbstractTableModel.__init__(self, parent, *args)
data = []
if not len(mylist) == 0:
for i in mylist:
i[2] = i[2].replace('[user]@[ip]:Data/', 'Server/')
data.append(i[1:])
self.mylist = data
self.header = header
def rowCount(self, parent):
return len(self.mylist)
def columnCount(self, parent):
return len(self.mylist[0])
def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None
def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))
app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()