-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
285 lines (227 loc) · 9.69 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtWebKit, QtGui, QtCore, QtGui, uic
import ctypes
import sys
import time
import threading
import os
lib_path = os.path.abspath('src/')
sys.path.append(lib_path)
import Pyro4
import threading
import adblocker
from player import Player, PlayerState
from client import Client
from utils import *
from handler import CallbackHandler
class WebPage(QtWebKit.QWebPage):
def javaScriptConsoleMessage(self, msg, line, source):
# print '%s line %d: %s' % (source, line, msg)
# print 'js console, line %d: "%s"' % (line, msg)
pass
class Main(QtGui.QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.ui = uic.loadUi('ui/main.ui', self)
### webView setup ###
self.ui.webView.setPage(WebPage())
adManager = adblocker.AdblockerNetworkManager()
self.ui.webView.page().setNetworkAccessManager(adManager)
self.ui.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
self.ui.webView.setUrl(QtCore.QUrl('res/docs/index.html'))
### queueList setup ###
self.ui.queueList.doubleClicked.connect(self.play_video_at_index)
self.ui.queueList.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.ui.queueList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.ui.queueList.customContextMenuRequested.connect(self.queue_context_menu)
self.ui.queueList.dropEvent = self.queue_drop_event
### videoInput setup ###
self.ui.videoInput.returnPressed.connect(self.add_video)
### voteButton setup ###
self.ui.voteButton.clicked.connect(self.player_state_changed)
### userList setup ###
### chatText setup ###
### chatInput setup ###
self.ui.chatInput.returnPressed.connect(self.send_message)
### Playback Handlers ###
self.player = Player(self.ui.webView)
self.player.set_on_state_change_listener(self.player_state_changed)
### initial setup ###
# this will eventually pull from an ini with previous settings
self.user_name = "Lizzy"
self.user_group = "curator"
self.dialog = None
self.show_connect_dialog()
self.local_users = []
self.local_queue = []
self.local_chat = []
self.local_player_state = {}
self.daemon = Pyro4.core.Daemon()
self.callback = CallbackHandler()
self.callback.user_connected_signal.connect(self.user_connected)
self.callback.message_received_signal.connect(self.message_received)
self.daemon.register(self.callback)
def run(self):
def daemon_loop():
self.daemon.requestLoop()
threading.Thread(target=daemon_loop).start()
### PLAYER ###
def play_video_at_index(self, item):
queue = self.proxy.get_queue()[:]
index = item.row()
qi = queue[index]
self.player.cue_video(qi['vid_id'])
self.player.play()
def player_state_changed(self, vid_id, time, state):
if self.user_group == "curator":
self.proxy.set_player_state(vid_id, time, state, 4)
print("id = {0}, time = {1}, state = {2}".format(vid_id, time, state))
def player_state_changed(self):
data = self.proxy.get_player_state()
state = data['state']
print data
if state == PlayerState.PLAYING:
self.player.cue_video(data['vid_id'])
self.player.seek(data['time'])
self.player.play()
elif state == PlayerState.PAUSED:
self.player.seek(data['time'])
self.player.pause()
elif state == PlayerState.CUED:
self.player.cue_video(data['vid_id'])
self.player.seek(data['time'])
####################
### SERVER CALLS ###
####################
def connect(self):
## RO Proxy ##
name = str(self.dialog.ui.nameInput.text().toAscii())
ip = str(self.dialog.ui.addressInput.text().toAscii())
port = str(self.dialog.ui.portInput.text().toAscii())
uri = "PYRO:cutie@{0}:{1}".format(ip, port)
self.proxy = Pyro4.Proxy(uri)
users = self.proxy.get_users()[:]
queue = self.proxy.get_queue()[:]
# populate queue and user list
for user in users:
self.ui.userList.addItem(user['name'])
for qi in queue:
self.video_added(qi)
# ensure a connecting user has a unique name
unique = 0
for user in users:
if self.user_name == user['name']:
unique += 1
self.user_name = name+" ({0})".format(unique)
if unique == 0:
self.user_name = name
for user in users:
if user['group'] == "curator" and len(users) >= 1:
self.user_group = "regular"
self.proxy._pyroOneway.add("connect_user")
self.proxy.connect_user(self.user_name, self.user_group, self.callback)
def disconnect(self):
self.proxy.disconnect_user(self.user_name)
def add_video(self):
url = str(self.ui.videoInput.text())
self.proxy.add_video(url, self.user_name)
self.ui.videoInput.clear()
def send_message(self):
text = str(self.ui.chatInput.text().toAscii())
text = text.replace('"', """)
m = {}
m['name'] = self.user_name
m['message'] = text
self.proxy._pyroOneway.add("broadcast_message")
self.proxy.broadcast_message(m, self.callback)
self.ui.chatInput.setText("")
################
### HANDLERS ###
################
def user_connected(self, user):
self.ui.userList.addItem(user['name'])
def user_disconnected(self, index):
self.ui.userList.takeItem(index)
def video_added(self, qi):
# future point release: create a custom listwidgetitem that contains
# more info about the video (and possibly a thumbnail?)
title = qi['title']
added_by = qi['added_by']
rating = str(qi['rating'])[0:4]
views = intWithCommas(int(qi['views']))
duration = clock_from_seconds(qi['duration'])
item = QtGui.QListWidgetItem()
item.setText(title + " - " + duration)
item.setToolTip("Added by:\t{0}\nRating:\t\t{1}\nTotal views:\t{2}".format(added_by, rating, views))
self.ui.queueList.addItem(item)
def video_removed(self, index):
self.ui.queueList.takeItem(index)
def queue_sorted(self, initial_row, dropped_row):
if initial_row != dropped_row:
item = self.ui.queueList.takeItem(initial_row)
self.ui.queueList.insertItem(dropped_row, item)
def message_received(self, m):
self.ui.chatText.append("<b>"+m['name']+"</b>: "+m['message'])
self.ui.chatText.verticalScrollBar().setValue(self.ui.chatText.verticalScrollBar().maximum())
### UI ###
def queue_drop_event(self, event):
# get inital row of dragged item before continuing
source = event.source()
item = source.currentItem()
initial_index = source.row(item)
# do the drop action
QtGui.QListWidget.dropEvent(self.ui.queueList, event)
if event.isAccepted():
dropped_index = source.row(item)
self.proxy.sort_queue(initial_index, dropped_index)
print "initial: {0}, dropped: {1}".format(str(initial_index), str(dropped_index))
def queue_context_menu(self, position):
if len(self.ui.queueList) == 0:
return
cbicon = QtGui.QIcon(r"res/img/clipboard.png")
ericon = QtGui.QIcon(r"res/img/eraser.png")
menu = QtGui.QMenu(self.ui.queueList)
removeAction = menu.addAction(ericon, "Remove from queue")
copyAction = menu.addAction(cbicon, "Copy URL to clipboard")
action = menu.exec_(self.ui.queueList.mapToGlobal(position))
queue = self.proxy.get_queue()[:]
item = self.ui.queueList.currentItem()
item_index = self.ui.queueList.row(item)
if action == removeAction:
self.proxy.remove_video(item_index)
del item
elif action == copyAction:
qi = queue[item_index]
clipboard = QtGui.QApplication.clipboard()
clipboard.clear(mode = clipboard.Clipboard)
clipboard.setText("http://youtu.be/"+qi['vid_id'], mode = clipboard.Clipboard)
def show_connect_dialog(self):
self.dialog = QtGui.QDialog(self, QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint)
self.dialog.ui = uic.loadUi('ui/connectdialog.ui', self.dialog)
self.dialog.ui.nameInput.setText("Lizzy") # debug
self.dialog.ui.addressInput.setText("192.168.1.109") # debug 10.0.1.12
self.dialog.ui.portInput.setText("8080")
self.dialog.ui.okCancle.accepted.connect(self.connect)
self.dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.dialog.show()
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self,
"Cutie",
"Quit Cutie?",
QtGui.QMessageBox.Yes,
QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
self.disconnect()
event.accept()
else:
event.ignore()
if __name__ == '__main__':
# allow taskbar to display icon
myappid = 'elizabwth.cutie'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
app = QtGui.QApplication(sys.argv)
myapp = Main()
myapp.show()
myapp.run()
sys.exit(app.exec_())