-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.py
136 lines (106 loc) · 4.97 KB
/
buffer.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Andy Stewart
#
# Author: Andy Stewart <lazycat.manatee@gmail.com>
# Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import http.server as BaseHTTPServer
import os
import shutil
import socket
import threading
from urllib.parse import quote
from core.buffer import Buffer
from core.utils import *
from PyQt6 import QtCore, QtGui
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QLabel, QVBoxLayout, QWidget
class AppBuffer(Buffer):
def __init__(self, buffer_id, url, arguments):
Buffer.__init__(self, buffer_id, url, arguments, False)
self.add_widget(FileTransferWidget(url, self.theme_foreground_color))
@interactive
def update_theme(self):
super().update_theme()
self.buffer_widget.change_color(self.theme_background_color, self.theme_foreground_color)
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
global local_file_path
try:
with open(local_file_path, 'rb') as f:
self.send_response(200)
self.send_header("Content-Type", 'application/octet-stream')
self.send_header("Content-Disposition", 'attachment; filename="{}"'.format(quote(os.path.basename(local_file_path))))
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs.st_size))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
except socket.error:
# Don't need handle socket error.
pass
class FileTransferWidget(QWidget):
def __init__(self, url, foreground_color):
QWidget.__init__(self)
self.setStyleSheet("background-color: transparent;")
file_path = os.path.expanduser(url)
self.file_name_font = QFont()
self.file_name_font.setPointSize(48)
self.file_name_label = QLabel(self)
self.file_name_label.setText(file_path)
self.file_name_label.setFont(self.file_name_font)
self.file_name_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.file_name_label.setStyleSheet("color: {}".format(foreground_color))
self.qrcode_label = QLabel(self)
self.notify_font = QFont()
self.notify_font.setPointSize(24)
self.notify_label = QLabel(self)
self.notify_label.setText("Scan QR code above to download this file on your smartphone.\nMake sure the smartphone is connected to the same WiFi network as this computer.")
self.notify_label.setFont(self.notify_font)
self.notify_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.notify_label.setStyleSheet("color: {}".format(foreground_color))
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addStretch()
layout.addWidget(self.qrcode_label, 0, Qt.AlignmentFlag.AlignCenter)
layout.addSpacing(20)
layout.addWidget(self.file_name_label, 0, Qt.AlignmentFlag.AlignCenter)
layout.addSpacing(40)
layout.addWidget(self.notify_label, 0, Qt.AlignmentFlag.AlignCenter)
layout.addStretch()
self.start_server(file_path)
def set_address(self, address):
self.qrcode_label.setPixmap(get_qrcode_pixmap(address))
def start_server(self, filename):
global local_file_path
local_file_path = filename
self.port = get_free_port()
self.local_ip = get_local_ip()
self.set_address("http://{0}:{1}/{2}".format(self.local_ip, self.port, filename))
self.sender_thread = threading.Thread(target=self.run_http_server, name='LoopThread')
self.sender_thread.start()
def run_http_server(self):
httpd = BaseHTTPServer.HTTPServer(('', self.port), SimpleHTTPRequestHandler)
httpd.serve_forever()
def destroy_buffer(self):
global local_file_path
message_to_emacs("Stop file sender server: http://{0}:{1}/{2}".format(self.local_ip, self.port, local_file_path))
self.sender_thread.stop()
super().destroy_buffer()
def change_color(self, background_color, foreground_color):
self.setStyleSheet("background-color: {};".format(background_color))
self.file_name_label.setStyleSheet("color: {}".format(foreground_color))
self.notify_label.setStyleSheet("color: {}".format(foreground_color))