-
Notifications
You must be signed in to change notification settings - Fork 4
/
gui.py
262 lines (210 loc) · 8.35 KB
/
gui.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
import sys,os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from main import Sync
class AppForm(QMainWindow):
def __init__(self, parent=None):
self.isset_credentials = False
self.sync = Sync()
QMainWindow.__init__(self, parent)
self.setWindowTitle('Moodle Sync')
self.setWindowIcon(QIcon('moodle.png'))
self.create_menu()
self.create_status_bar()
self._init_frames()
self.show_login_frame()
self.logged_in = False
def _init_frames(self):
self.login_frame = QWidget()
self.main_frame = QWidget()
###################### Login Frame ###########################
vbox = QVBoxLayout()
viewer = QLabel()
viewer.setPixmap(QPixmap(os.getcwd() + "/moodle_large.png"))
# Login Credentials :: Group Box
qg = QGroupBox("Enter your login credentials :")
qvbox = QGridLayout()
# Fields
ulabel = QLabel("Username:")
plabel = QLabel("Password:")
self.username_tb = QLineEdit()
self.username_tb.setMinimumWidth(200)
self.password_tb = QLineEdit()
self.password_tb.setEchoMode(QLineEdit.Password)
self.password_tb.setMinimumWidth(200)
self.save_credentials = QCheckBox("Remember Me")
self.login_button = QPushButton("&Login")
qvbox.addWidget(ulabel,0,0)
qvbox.addWidget(self.username_tb,0,1)
qvbox.addWidget(plabel,1,0)
qvbox.addWidget(self.password_tb,1,1)
qvbox.addWidget(self.save_credentials,2,1)
qvbox.addWidget(self.login_button,3,1)
qg.setLayout(qvbox)
vbox.addWidget(viewer)
vbox.addWidget(qg)
self.login_frame.setLayout(vbox)
self.connect( self.login_button, SIGNAL("clicked()"), self.initiate_login)
####################### Main Frame ###############################
self.main_frame_box = QVBoxLayout()
view = QListView()
self.model = QStandardItemModel()
view.setModel(self.model)
self.start_sync_button = QPushButton("&Start Sync")
self.pdfGet = QCheckBox("Get PDFs as well")
self.pdfGet.setCheckState(Qt.Checked)
self.pdfGet.setCheckable(True)
self.main_frame_box.addWidget(view)
self.main_frame_box.addWidget(self.start_sync_button)
self.main_frame_box.addWidget(self.pdfGet)
self.main_frame.setLayout(self.main_frame_box)
self.connect( self.start_sync_button, SIGNAL("clicked()"), self.start_sync )
####################### THREAD CONNECTORS ########################
self.connect(self.sync, SIGNAL("finished()"), self.updateUi)
self.connect(self.sync, SIGNAL("terminated()"), self.updateUi)
self.connect(self.sync, SIGNAL("login_status(QString)"), self.login_done)
self.connect(self.sync, SIGNAL("courses(PyQt_PyObject)"), self.load_courses)
self.connect(self.sync, SIGNAL("sync_courses(QString)"), self.sync_done)
def on_about(self):
msg = """
MBox v0.1
* Mbox is used to sync files from moodle
* Set your username and password
* Next select the folders to be synced
* Voila :)
"""
QMessageBox.about(self, "MBox v0.1", msg.strip())
def updateUi(self):
self.start_sync_button.setEnabled(True)
try:
self.login_button.setEnabled(True)
except RuntimeError:
print "No Login button"
def show_login_frame(self):
self.main_frame.hide()
self.setCentralWidget(self.login_frame)
self.login_frame.show()
try:
if not self.isset_credentials:
print "Trying to load credentials from past"
f = open("./.msync","r")
contents = f.read()
self.u,self.p = contents.split('|')
self.username_tb.setText(self.u)
self.password_tb.setText(self.p)
self.save_credentials.setCheckState(Qt.Checked) #If the user has ticked "remember me" befo
except:
print "Failed to load credentials :: Loading login frame"
def login_done(self, msg):
self.status_text.setText(msg)
if msg == "Logged In":
self.logged_in = True
self.show_main_frame()
def load_courses(self,courses_list):
print courses_list
try:
f = open("./.selected_list","r")
selected_list = f.read().split("|")
except:
selected_list = []
print "Selected List"
print selected_list
self.checkboxes = []
for item in courses_list:
cb = QStandardItem(item)
check = Qt.Checked if item in selected_list else Qt.Unchecked
cb.setCheckState(check)
cb.setCheckable(True)
self.checkboxes.append(cb)
self.model.appendRow(cb)
def show_main_frame(self):
print "Loading Main Frame"
if not self.logged_in:
self.show_login_frame()
return
self.login_frame.hide()
self.sync.listCourses()
self.setCentralWidget(self.main_frame)
self.main_frame_box.addWidget(self.pdfGet)
self.main_frame.show()
def initiate_login(self):
self.login_button.setEnabled(False)
print "Adding Credentials"
u = self.username_tb.text()
p = self.password_tb.text()
#print u,p
if self.save_credentials.isChecked():
print "Saving Credentials"
f = open("./.msync","w")
f.write("%s|%s" % (u,p))
f.close()
#else:
# os.remove("./.msync") # Remove credentials if the user doesn't want to keep them remembered.
self.isset_credentials = True
self.u = u
self.p = p
print "Now firing thread request"
self.sync.loginCredentials(u,p)
def start_sync(self):
checklist = []
try:
for box in self.checkboxes:
if box.checkState() == Qt.Checked:
checklist.append(str(box.text()))
print checklist
except AttributeError:
print "No list... Reloading..."
self.sync.listCourses()
f = open("./.selected_list","w")
f.write("|".join(checklist))
f.close()
folderNames={}
for i in checklist:
folderNames[i]=i
self.start_sync_button.setEnabled(False)
self.status_text.setText("Syncing ... ")
self.sync.syncCourses(checklist, folderNames, (self.pdfGet.checkState()==Qt.Checked))
def sync_done(self, msg):
self.status_text.setText("Finished sync")
self.start_sync_button.setEnabled(True)
def create_status_bar(self):
self.status_text = QLabel("Idle")
self.statusBar().addWidget(self.status_text, 1)
def create_menu(self):
self.file_menu = self.menuBar().addMenu("&File")
#settings_action = self.create_action("&Settings", slot=self.get_login,
# shortcut="Ctrl+S", tip="Configure MBox")
quit_action = self.create_action("&Quit", slot=self.close,
shortcut="Ctrl+Q", tip="Close the application")
self.add_actions(self.file_menu, (quit_action,))
self.help_menu = self.menuBar().addMenu("&Help")
about_action = self.create_action("&About",
shortcut='F1', slot=self.on_about,
tip='About Moodle Sync')
self.add_actions(self.help_menu, (about_action,))
def add_actions(self, target, actions):
for action in actions:
if action is None:
target.addSeparator()
else:
target.addAction(action)
def create_action( self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"):
action = QAction(text, self)
if icon is not None:
action.setIcon(QIcon(":/%s.png" % icon))
if shortcut is not None:
action.setShortcut(shortcut)
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if slot is not None:
self.connect(action, SIGNAL(signal), slot)
if checkable:
action.setCheckable(True)
return action
if __name__ == "__main__":
app = QApplication(sys.argv)
form = AppForm()
form.show()
app.exec_()
del form