~doxxx/qbzr/qconflicts-cmdline-splitting

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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# QBzr - Qt frontend to Bazaar commands
# Copyright (C) 2008 Gary van der Merwe <garyvdm@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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from PyQt4 import QtCore, QtGui
import time

from bzrlib import ui
from bzrlib.plugins.qbzr.lib.i18n import gettext

def ui_current_widget(f):
    def decorate(*args, **kargs):
        if isinstance(ui.ui_factory, QUIFactory):
            ui.ui_factory.current_widget_stack.append(args[0])
            try:
                r = f(*args, **kargs)
            finally:
                del ui.ui_factory.current_widget_stack[-1]
            return r
        else:
            return f(*args, **kargs)
    return decorate

def quifactory():
    if isinstance(ui.ui_factory, QUIFactory):
        return ui.ui_factory
    return None

def current_throbber():
    ui = quifactory()
    if ui:
        return ui.throbber()
    return None

class QUIFactory(ui.UIFactory):

    def __init__(self):
        super(QUIFactory, self).__init__()
        self.current_widget_stack = []
        
        self._transport_update_time = 0
        self._total_byte_count = 0
        self._bytes_since_update = 0
        self._transport_rate = None
    
    def current_widget(self):
        if self.current_widget_stack:
            return self.current_widget_stack[-1]
        return None
    
    def throbber(self):
        current_widget = self.current_widget()
        if current_widget and getattr(current_widget, 'throbber', None) is not None:
            return current_widget.throbber
        return None
    
    def report_transport_activity(self, transport, byte_count, direction):
        """Called by transports as they do IO.
        
        This may update a progress bar, spinner, or similar display.
        By default it does nothing.
        """
        
        self._total_byte_count += byte_count
        self._bytes_since_update += byte_count

        throbber = self.throbber()
        if throbber:
            now = time.time()
            if self._transport_update_time is None:
                self._transport_update_time = now
            elif now >= (self._transport_update_time + 0.2):
                # guard against clock stepping backwards, and don't update too
                # often
                self._transport_rate  = self._bytes_since_update / (now - self._transport_update_time)
                self._transport_update_time = now
                self._bytes_since_update = 0
            
            if self._transport_rate:
                msg = ("%6dkB @ %4dkB/s" %
                    (self._total_byte_count>>10, int(self._transport_rate)>>10,))
            else:
                msg = ("%6dkB @         " %
                    (self._total_byte_count>>10,))
            
            throbber.transport.setText(msg)
        
        QtCore.QCoreApplication.processEvents()

    def get_password(self, prompt='', **kwargs):
        password, ok = QtGui.QInputDialog.getText(self.current_widget(),
                                                  gettext("Enter Password"),
                                                  (prompt % kwargs),
                                                  QtGui.QLineEdit.Password)
        
        if ok:
            return str(password)
        else:
            raise KeyboardInterrupt()

    def get_username(self, prompt='', **kwargs):
        username, ok = QtGui.QInputDialog.getText(self.current_widget(),
                                                  gettext("Enter Username"),
                                                  (prompt % kwargs))
        
        if ok:
            return str(username)
        else:
            raise KeyboardInterrupt()
    
    def get_boolean(self, prompt):
        button = QtGui.QMessageBox.question(
            self.current_widget(), "Bazaar", prompt,
            QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
        
        return button == QtGui.QMessageBox.Yes

    def clear_term(self):
        """Prepare the terminal for output.

        This will, for example, clear text progress bars, and leave the
        cursor at the leftmost position."""
        pass
    

# You can run this file to test the ui factory. This is not in the test suit
# because it actualy open the ui, and so user interaction is required to run
# the test.
if __name__ == "__main__":
    application = QtGui.QApplication([])
    ui_factory = QUIFactory()
    print ui_factory.get_username("Enter password 123")
    #print ui_factory.get_boolean("Question?")