aboutsummaryrefslogtreecommitdiff
path: root/desktop/onionshare/widgets.py
blob: 64a077034054e73eccc07e4186efa48c65201601 (plain)
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
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/

Copyright (C) 2014-2022 Micah Lee, et al. <micah@micahflee.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
(at your option) 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/>.
"""

from PySide2 import QtCore, QtWidgets, QtGui
import qrcode

from . import strings
from .gui_common import GuiCommon


class Alert(QtWidgets.QMessageBox):
    """
    An alert box dialog.
    """

    def __init__(
        self,
        common,
        message,
        icon=QtWidgets.QMessageBox.NoIcon,
        buttons=QtWidgets.QMessageBox.Ok,
        autostart=True,
        title="OnionShare",
    ):
        super(Alert, self).__init__(None)

        self.common = common

        self.common.log("Alert", "__init__")

        self.setWindowTitle(title)
        self.setWindowIcon(QtGui.QIcon(GuiCommon.get_resource_path("images/logo.png")))
        self.setText(message)
        self.setIcon(icon)
        self.setStandardButtons(buttons)

        if autostart:
            self.exec_()


class AddFileDialog(QtWidgets.QFileDialog):
    """
    Overridden version of QFileDialog which allows us to select folders as well
    as, or instead of, files. For adding files/folders to share.

    Note that this dialog can't be used in macOS, only in Windows, Linux, and BSD.
    This is because the macOS sandbox requires native dialogs, and this is a Qt5
    dialog.
    """

    def __init__(self, common, *args, **kwargs):
        QtWidgets.QFileDialog.__init__(self, *args, **kwargs)

        self.common = common
        self.common.log("AddFileDialog", "__init__")

        self.setOption(self.DontUseNativeDialog, True)
        self.setOption(self.ReadOnly, True)
        self.setOption(self.ShowDirsOnly, False)
        self.setFileMode(self.ExistingFiles)
        tree_view = self.findChild(QtWidgets.QTreeView)
        tree_view.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        list_view = self.findChild(QtWidgets.QListView, "listView")
        list_view.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)

    def accept(self):
        self.common.log("AddFileDialog", "accept")
        QtWidgets.QDialog.accept(self)


class MinimumSizeWidget(QtWidgets.QWidget):
    """
    An empty widget with a minimum width and height, just to force layouts to behave
    """

    def __init__(self, width, height):
        super(MinimumSizeWidget, self).__init__()
        self.setMinimumWidth(width)
        self.setMinimumHeight(height)


class Image(qrcode.image.base.BaseImage):
    """
    A custom Image class, for use with the QR Code pixmap.
    """

    def __init__(self, border, width, box_size):
        self.border = border
        self.width = width
        self.box_size = box_size
        size = (width + border * 2) * box_size
        self._image = QtGui.QImage(size, size, QtGui.QImage.Format_RGB16)
        self._image.fill(QtCore.Qt.white)

    def pixmap(self):
        return QtGui.QPixmap.fromImage(self._image)

    def drawrect(self, row, col):
        painter = QtGui.QPainter(self._image)
        painter.fillRect(
            (col + self.border) * self.box_size,
            (row + self.border) * self.box_size,
            self.box_size,
            self.box_size,
            QtCore.Qt.black,
        )

    def save(self, stream, kind=None):
        pass


class QRCodeDialog(QtWidgets.QDialog):
    """
    A dialog showing a QR code.
    """

    def __init__(self, common, title, text):
        super(QRCodeDialog, self).__init__()

        self.common = common

        self.common.log("QrCode", "__init__")

        self.qr_label_title = QtWidgets.QLabel(self)
        self.qr_label_title.setText(title)
        self.qr_label_title.setAlignment(QtCore.Qt.AlignCenter)

        self.qr_label = QtWidgets.QLabel(self)
        self.qr_label.setPixmap(qrcode.make(text, image_factory=Image).pixmap())
        self.qr_label.setScaledContents(True)
        self.qr_label.setFixedSize(350, 350)

        self.setWindowTitle(strings._("gui_qr_code_dialog_title"))
        self.setWindowIcon(QtGui.QIcon(GuiCommon.get_resource_path("images/logo.png")))
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.qr_label_title)
        layout.addWidget(self.qr_label)

        self.exec_()