# Path: lib/socketiogame.py
# class SocketApp(object):
#     def __init__(self):
#         self.buffer = []
#         self.request = {}
# 
#     def __call__(self, environ, start_response):
#         path = environ['PATH_INFO'].strip('/')
# 
#         if not path:
#             headers = [('Location', '/client/index.html'), ]
#             start_response('301 Redirect', headers)
#             return []
# 
#         if path.startswith('client/'):
#             mode = 'rb'
#             if path.endswith(".png"):
#                 content_type = "image/png"
#             elif path.endswith(".js"):
#                 mode = 'r'
#                 content_type = "text/javascript"
#             elif path.endswith(".css"):
#                 mode = 'r'
#                 content_type = "text/css"
#             elif path.endswith(".swf"):
#                 content_type = "application/x-shockwave-flash"
#             else:
#                 mode = 'r'
#                 content_type = "text/html"
# 
#             try:
#                 data = open(path.replace('/', os.sep), mode).read()
#             except Exception:
#                 return self.not_found(start_response)
# 
#             start_response('200 OK', [('Content-Type', content_type)])
#             return [data]
# 
#         if path.startswith("socket.io"):
#             socketio_manage(environ, {'': GameNamespace}, self.request)
#         else:
#             return self.not_found(start_response)
# 
#     def not_found(self, start_response):
#         start_response('404 Not Found', [])
#         return ['<h1>Not Found</h1>']


from gevent import monkey
from PyQt4 import QtGui, QtCore, QtWebKit
from socketio.server import SocketIOServer
from lib.socketiogame import SocketApp
import gipc
import sys
    def showSelectFirstDeviceDialog(self):
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/dev')
        print fname

    def showSelectSecondDeviceDialog(self):
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/dev')
        print fname

    def initUI(self):
        self.setWindowTitle("Meditation Deathmatch")

        icon = QtGui.QIcon.fromTheme("open")
        openFirst = QtGui.QAction(icon, 'Select First Device', self)
        openFirst.setShortcut('Ctrl+1')
        openFirst.setStatusTip('Select First Device')
        openFirst.triggered.connect(self.showSelectFirstDeviceDialog)

        icon = QtGui.QIcon.fromTheme("open")
        openSecond = QtGui.QAction(icon, 'Select Second Device', self)
        openSecond.setShortcut('Ctrl+2')
        openSecond.setStatusTip('Select First Device')
        openSecond.triggered.connect(self.showSelectFirstDeviceDialog)

        self.menubar = QtGui.QMenuBar()
        fileMenu = self.menubar.addMenu('&File')
        fileMenu.addAction(openFirst)
        fileMenu.addAction(openSecond)


def qt_app():

# Path: lib/socketiogame.py
# class SocketApp(object):
#     def __init__(self):
#         self.buffer = []
#         self.request = {}
# 
#     def __call__(self, environ, start_response):
#         path = environ['PATH_INFO'].strip('/')
# 
#         if not path:
#             headers = [('Location', '/client/index.html'), ]
#             start_response('301 Redirect', headers)
#             return []
# 
#         if path.startswith('client/'):
#             mode = 'rb'
#             if path.endswith(".png"):
#                 content_type = "image/png"
#             elif path.endswith(".js"):
#                 mode = 'r'
#                 content_type = "text/javascript"
#             elif path.endswith(".css"):
#                 mode = 'r'
#                 content_type = "text/css"
#             elif path.endswith(".swf"):
#                 content_type = "application/x-shockwave-flash"
#             else:
#                 mode = 'r'
#                 content_type = "text/html"
# 
#             try:
#                 data = open(path.replace('/', os.sep), mode).read()
#             except Exception:
#                 return self.not_found(start_response)
# 
#             start_response('200 OK', [('Content-Type', content_type)])
#             return [data]
# 
#         if path.startswith("socket.io"):
#             socketio_manage(environ, {'': GameNamespace}, self.request)
#         else:
#             return self.not_found(start_response)
# 
#     def not_found(self, start_response):
#         start_response('404 Not Found', [])
#         return ['<h1>Not Found</h1>']


# Path: qt4-webview.py
from gevent import monkey
from PyQt4 import QtGui, QtCore, QtWebKit
from socketio.server import SocketIOServer
from lib.socketiogame import SocketApp
import gipc
import sys

    def showSelectFirstDeviceDialog(self):
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/dev')
        print fname

    def showSelectSecondDeviceDialog(self):
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/dev')
        print fname

    def initUI(self):
        self.setWindowTitle("Meditation Deathmatch")

        icon = QtGui.QIcon.fromTheme("open")
        openFirst = QtGui.QAction(icon, 'Select First Device', self)
        openFirst.setShortcut('Ctrl+1')
        openFirst.setStatusTip('Select First Device')
        openFirst.triggered.connect(self.showSelectFirstDeviceDialog)

        icon = QtGui.QIcon.fromTheme("open")
        openSecond = QtGui.QAction(icon, 'Select Second Device', self)
        openSecond.setShortcut('Ctrl+2')
        openSecond.setStatusTip('Select First Device')
        openSecond.triggered.connect(self.showSelectFirstDeviceDialog)

        self.menubar = QtGui.QMenuBar()
        fileMenu = self.menubar.addMenu('&File')
        fileMenu.addAction(openFirst)
        fileMenu.addAction(openSecond)


def qt_app():

    app = QtGui.QApplication(sys.argv)