class LedgerAuthDialog(QDialog):
    def __init__(self, handler, data):
        '''Ask user for 2nd factor authentication. Support text, security card and paired mobile methods.
        Use last method from settings, but support new pairing and downgrade.
        '''
        QDialog.__init__(self, handler.top_level_window())
        self.handler = handler
        self.txdata = data
        self.idxs = self.txdata[
            'keycardData'] if self.txdata['confirmationType'] > 1 else ''
        self.setMinimumWidth(600)
        self.setWindowTitle(_("Ledger Wallet Authentication"))
        self.cfg = copy.deepcopy(self.handler.win.wallet.get_keystore().cfg)
        self.dongle = self.handler.win.wallet.get_keystore().get_client(
        ).dongle
        self.ws = None
        self.pin = ''

        self.devmode = self.getDevice2FAMode()
        if self.devmode == 0x11 or self.txdata['confirmationType'] == 1:
            self.cfg['mode'] = 0

        vbox = QVBoxLayout()
        self.setLayout(vbox)

        def on_change_mode(idx):
            if idx < 2 and self.ws:
                self.ws.stop()
                self.ws = None
            self.cfg[
                'mode'] = 0 if self.devmode == 0x11 else idx if idx > 0 else 1
            if self.cfg['mode'] > 1 and self.cfg['pair'] and not self.ws:
                self.req_validation()
            if self.cfg['mode'] > 0:
                self.handler.win.wallet.get_keystore().cfg = self.cfg
                self.handler.win.wallet.save_keystore()
            self.update_dlg()

        def add_pairing():
            self.do_pairing()

        def return_pin():
            self.pin = self.pintxt.text(
            ) if self.txdata['confirmationType'] == 1 else self.cardtxt.text()
            if self.cfg['mode'] == 1:
                self.pin = ''.join(chr(int(str(i), 16)) for i in self.pin)
            self.accept()

        self.modebox = QWidget()
        modelayout = QHBoxLayout()
        self.modebox.setLayout(modelayout)
        modelayout.addWidget(QLabel(_("Method:")))
        self.modes = QComboBox()
        modelayout.addWidget(self.modes, 2)
        self.addPair = QPushButton(_("Pair"))
        self.addPair.setMaximumWidth(60)
        modelayout.addWidget(self.addPair)
        modelayout.addStretch(1)
        self.modebox.setMaximumHeight(50)
        vbox.addWidget(self.modebox)

        self.populate_modes()
        self.modes.currentIndexChanged.connect(on_change_mode)
        self.addPair.clicked.connect(add_pairing)

        self.helpmsg = QTextEdit()
        self.helpmsg.setStyleSheet(
            "QTextEdit { background-color: lightgray; }")
        self.helpmsg.setReadOnly(True)
        vbox.addWidget(self.helpmsg)

        self.pinbox = QWidget()
        pinlayout = QHBoxLayout()
        self.pinbox.setLayout(pinlayout)
        self.pintxt = QLineEdit()
        self.pintxt.setEchoMode(2)
        self.pintxt.setMaxLength(4)
        self.pintxt.returnPressed.connect(return_pin)
        pinlayout.addWidget(QLabel(_("Enter PIN:")))
        pinlayout.addWidget(self.pintxt)
        pinlayout.addWidget(QLabel(_("NOT DEVICE PIN - see above")))
        pinlayout.addStretch(1)
        self.pinbox.setVisible(self.cfg['mode'] == 0)
        vbox.addWidget(self.pinbox)

        self.cardbox = QWidget()
        card = QVBoxLayout()
        self.cardbox.setLayout(card)
        self.addrtext = QTextEdit()
        self.addrtext.setStyleSheet(
            "QTextEdit { color:blue; background-color:lightgray; padding:15px 10px; border:none; font-size:20pt; }"
        )
        self.addrtext.setReadOnly(True)
        self.addrtext.setMaximumHeight(120)
        card.addWidget(self.addrtext)

        def pin_changed(s):
            if len(s) < len(self.idxs):
                i = self.idxs[len(s)]
                addr = self.txdata['address']
                addr = addr[:i] + '<u><b>' + addr[i:i +
                                                  1] + '</u></b>' + addr[i +
                                                                         1:]
                self.addrtext.setHtml(str(addr))
            else:
                self.addrtext.setHtml(_("Press Enter"))

        pin_changed('')
        cardpin = QHBoxLayout()
        cardpin.addWidget(QLabel(_("Enter PIN:")))
        self.cardtxt = QLineEdit()
        self.cardtxt.setEchoMode(2)
        self.cardtxt.setMaxLength(len(self.idxs))
        self.cardtxt.textChanged.connect(pin_changed)
        self.cardtxt.returnPressed.connect(return_pin)
        cardpin.addWidget(self.cardtxt)
        cardpin.addWidget(QLabel(_("NOT DEVICE PIN - see above")))
        cardpin.addStretch(1)
        card.addLayout(cardpin)
        self.cardbox.setVisible(self.cfg['mode'] == 1)
        vbox.addWidget(self.cardbox)

        self.pairbox = QWidget()
        pairlayout = QVBoxLayout()
        self.pairbox.setLayout(pairlayout)
        pairhelp = QTextEdit(helpTxt[5])
        pairhelp.setStyleSheet("QTextEdit { background-color: lightgray; }")
        pairhelp.setReadOnly(True)
        pairlayout.addWidget(pairhelp, 1)
        self.pairqr = QRCodeWidget()
        pairlayout.addWidget(self.pairqr, 4)
        self.pairbox.setVisible(False)
        vbox.addWidget(self.pairbox)
        self.update_dlg()

        if self.cfg['mode'] > 1 and not self.ws:
            self.req_validation()

    def populate_modes(self):
        self.modes.blockSignals(True)
        self.modes.clear()
        self.modes.addItem(
            _("Summary Text PIN (requires dongle replugging)"
              ) if self.txdata['confirmationType'] ==
            1 else _("Summary Text PIN is Disabled"))
        if self.txdata['confirmationType'] > 1:
            self.modes.addItem(_("Security Card Challenge"))
            if not self.cfg['pair']:
                self.modes.addItem(_("Mobile - Not paired"))
            else:
                self.modes.addItem(_("Mobile - %s") % self.cfg['pair'][1])
        self.modes.blockSignals(False)

    def update_dlg(self):
        self.modes.setCurrentIndex(self.cfg['mode'])
        self.modebox.setVisible(True)
        self.addPair.setText(
            _("Pair") if not self.cfg['pair'] else _("Re-Pair"))
        self.addPair.setVisible(self.txdata['confirmationType'] > 2)
        self.helpmsg.setText(
            helpTxt[self.cfg['mode'] if self.cfg['mode'] < 2 else 2 if self.
                    cfg['pair'] else 4])
        self.helpmsg.setMinimumHeight(180 if self.txdata['confirmationType'] ==
                                      1 else 100)
        self.pairbox.setVisible(False)
        self.helpmsg.setVisible(True)
        self.pinbox.setVisible(self.cfg['mode'] == 0)
        self.cardbox.setVisible(self.cfg['mode'] == 1)
        self.pintxt.setFocus(
            True) if self.cfg['mode'] == 0 else self.cardtxt.setFocus(True)
        self.setMaximumHeight(200)

    def do_pairing(self):
        rng = os.urandom(16)
        pairID = rng.encode('hex') + hashlib.sha256(rng).digest()[0].encode(
            'hex')
        self.pairqr.setData(pairID)
        self.modebox.setVisible(False)
        self.helpmsg.setVisible(False)
        self.pinbox.setVisible(False)
        self.cardbox.setVisible(False)
        self.pairbox.setVisible(True)
        self.pairqr.setMinimumSize(300, 300)
        if self.ws:
            self.ws.stop()
        self.ws = LedgerWebSocket(self, pairID)
        self.ws.pairing_done.connect(self.pairing_done)
        self.ws.start()

    def pairing_done(self, data):
        if data is not None:
            self.cfg['pair'] = [data['pairid'], data['name'], data['platform']]
            self.cfg['mode'] = 2
            self.handler.win.wallet.get_keystore().cfg = self.cfg
            self.handler.win.wallet.save_keystore()
        self.pin = 'paired'
        self.accept()

    def req_validation(self):
        if self.cfg['pair'] and 'secureScreenData' in self.txdata:
            if self.ws:
                self.ws.stop()
            self.ws = LedgerWebSocket(self, self.cfg['pair'][0], self.txdata)
            self.ws.req_updated.connect(self.req_updated)
            self.ws.start()

    def req_updated(self, pin):
        if pin == 'accepted':
            self.helpmsg.setText(helpTxt[3])
        else:
            self.pin = str(pin)
            self.accept()

    def getDevice2FAMode(self):
        apdu = [0xe0, 0x24, 0x01, 0x00, 0x00, 0x01]  # get 2fa mode
        try:
            mode = self.dongle.exchange(bytearray(apdu))
            return mode
        except BTChipException, e:
            debug_msg('Device getMode Failed')
        return 0x11
Exemple #2
0
class CheckLibraryDialog(QDialog):

    def __init__(self, parent, db):
        QDialog.__init__(self, parent)
        self.db = db

        self.setWindowTitle(_('Check Library -- Problems Found'))
        self.setWindowIcon(QIcon(I('debug.png')))

        self._tl = QHBoxLayout()
        self.setLayout(self._tl)
        self.splitter = QSplitter(self)
        self.left = QWidget(self)
        self.splitter.addWidget(self.left)
        self.helpw = QTextEdit(self)
        self.splitter.addWidget(self.helpw)
        self._tl.addWidget(self.splitter)
        self._layout = QVBoxLayout()
        self.left.setLayout(self._layout)
        self.helpw.setReadOnly(True)
        self.helpw.setText(_('''\
        <h1>Help</h1>

        <p>calibre stores the list of your books and their metadata in a
        database. The actual book files and covers are stored as normal
        files in the calibre library folder. The database contains a list of the files
        and covers belonging to each book entry. This tool checks that the
        actual files in the library folder on your computer match the
        information in the database.</p>

        <p>The result of each type of check is shown to the left. The various
        checks are:
        </p>
        <ul>
        <li><b>Invalid titles</b>: These are files and folders appearing
        in the library where books titles should, but that do not have the
        correct form to be a book title.</li>
        <li><b>Extra titles</b>: These are extra files in your calibre
        library that appear to be correctly-formed titles, but have no corresponding
        entries in the database</li>
        <li><b>Invalid authors</b>: These are files appearing
        in the library where only author folders should be.</li>
        <li><b>Extra authors</b>: These are folders in the
        calibre library that appear to be authors but that do not have entries
        in the database</li>
        <li><b>Missing book formats</b>: These are book formats that are in
        the database but have no corresponding format file in the book's folder.
        <li><b>Extra book formats</b>: These are book format files found in
        the book's folder but not in the database.
        <li><b>Unknown files in books</b>: These are extra files in the
        folder of each book that do not correspond to a known format or cover
        file.</li>
        <li><b>Missing cover files</b>: These represent books that are marked
        in the database as having covers but the actual cover files are
        missing.</li>
        <li><b>Cover files not in database</b>: These are books that have
        cover files but are marked as not having covers in the database.</li>
        <li><b>Folder raising exception</b>: These represent folders in the
        calibre library that could not be processed/understood by this
        tool.</li>
        </ul>

        <p>There are two kinds of automatic fixes possible: <i>Delete
        marked</i> and <i>Fix marked</i>.</p>
        <p><i>Delete marked</i> is used to remove extra files/folders/covers that
        have no entries in the database. Check the box next to the item you want
        to delete. Use with caution.</p>

        <p><i>Fix marked</i> is applicable only to covers and missing formats
        (the three lines marked 'fixable'). In the case of missing cover files,
        checking the fixable box and pushing this button will tell calibre that
        there is no cover for all of the books listed. Use this option if you
        are not going to restore the covers from a backup. In the case of extra
        cover files, checking the fixable box and pushing this button will tell
        calibre that the cover files it found are correct for all the books
        listed. Use this when you are not going to delete the file(s). In the
        case of missing formats, checking the fixable box and pushing this
        button will tell calibre that the formats are really gone. Use this if
        you are not going to restore the formats from a backup.</p>

        '''))

        self.log = QTreeWidget(self)
        self.log.itemChanged.connect(self.item_changed)
        self.log.itemExpanded.connect(self.item_expanded_or_collapsed)
        self.log.itemCollapsed.connect(self.item_expanded_or_collapsed)
        self._layout.addWidget(self.log)

        self.check_button = QPushButton(_('&Run the check again'))
        self.check_button.setDefault(False)
        self.check_button.clicked.connect(self.run_the_check)
        self.copy_button = QPushButton(_('Copy &to clipboard'))
        self.copy_button.setDefault(False)
        self.copy_button.clicked.connect(self.copy_to_clipboard)
        self.ok_button = QPushButton(_('&Done'))
        self.ok_button.setDefault(True)
        self.ok_button.clicked.connect(self.accept)
        self.delete_button = QPushButton(_('Delete &marked'))
        self.delete_button.setToolTip(_('Delete marked files (checked subitems)'))
        self.delete_button.setDefault(False)
        self.delete_button.clicked.connect(self.delete_marked)
        self.fix_button = QPushButton(_('&Fix marked'))
        self.fix_button.setDefault(False)
        self.fix_button.setEnabled(False)
        self.fix_button.setToolTip(_('Fix marked sections (checked fixable items)'))
        self.fix_button.clicked.connect(self.fix_items)
        self.bbox = QDialogButtonBox(self)
        self.bbox.addButton(self.check_button, QDialogButtonBox.ActionRole)
        self.bbox.addButton(self.delete_button, QDialogButtonBox.ActionRole)
        self.bbox.addButton(self.fix_button, QDialogButtonBox.ActionRole)
        self.bbox.addButton(self.copy_button, QDialogButtonBox.ActionRole)
        self.bbox.addButton(self.ok_button, QDialogButtonBox.AcceptRole)

        h = QHBoxLayout()
        ln = QLabel(_('Names to ignore:'))
        h.addWidget(ln)
        self.name_ignores = QLineEdit()
        self.name_ignores.setText(db.prefs.get('check_library_ignore_names', ''))
        self.name_ignores.setToolTip(
            _('Enter comma-separated standard file name wildcards, such as synctoy*.dat'))
        ln.setBuddy(self.name_ignores)
        h.addWidget(self.name_ignores)
        le = QLabel(_('Extensions to ignore'))
        h.addWidget(le)
        self.ext_ignores = QLineEdit()
        self.ext_ignores.setText(db.prefs.get('check_library_ignore_extensions', ''))
        self.ext_ignores.setToolTip(
            _('Enter comma-separated extensions without a leading dot. Used only in book folders'))
        le.setBuddy(self.ext_ignores)
        h.addWidget(self.ext_ignores)
        self._layout.addLayout(h)

        self._layout.addWidget(self.bbox)
        self.resize(950, 500)
        self.bbox.setEnabled(True)

    def do_exec(self):
        self.run_the_check()

        probs = 0
        for c in self.problem_count:
            probs += self.problem_count[c]
        if probs == 0:
            return False
        self.exec_()
        return True

    def accept(self):
        self.db.prefs['check_library_ignore_extensions'] = \
                                            unicode(self.ext_ignores.text())
        self.db.prefs['check_library_ignore_names'] = \
                                            unicode(self.name_ignores.text())
        QDialog.accept(self)

    def box_to_list(self, txt):
        return [f.strip() for f in txt.split(',') if f.strip()]

    def run_the_check(self):
        checker = CheckLibrary(self.db.library_path, self.db)
        checker.scan_library(self.box_to_list(unicode(self.name_ignores.text())),
                             self.box_to_list(unicode(self.ext_ignores.text())))

        plaintext = []

        def builder(tree, checker, check):
            attr, h, checkable, fixable = check
            list = getattr(checker, attr, None)
            if list is None:
                self.problem_count[attr] = 0
                return
            else:
                self.problem_count[attr] = len(list)

            tl = Item()
            tl.setText(0, h)
            if fixable and list:
                tl.setText(1, _('(fixable)'))
                tl.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
                tl.setCheckState(1, False)
            self.top_level_items[attr] = tl

            for problem in list:
                it = Item()
                if checkable:
                    it.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
                    it.setCheckState(1, False)
                else:
                    it.setFlags(Qt.ItemIsEnabled)
                it.setText(0, problem[0])
                it.setData(0, Qt.UserRole, problem[2])
                it.setText(1, problem[1])
                tl.addChild(it)
                self.all_items.append(it)
                plaintext.append(','.join([h, problem[0], problem[1]]))
            tree.addTopLevelItem(tl)

        t = self.log
        t.clear()
        t.setColumnCount(2)
        t.setHeaderLabels([_('Name'), _('Path from library')])
        self.all_items = []
        self.top_level_items = {}
        self.problem_count = {}
        for check in CHECKS:
            builder(t, checker, check)

        t.resizeColumnToContents(0)
        t.resizeColumnToContents(1)
        self.delete_button.setEnabled(False)
        self.text_results = '\n'.join(plaintext)

    def item_expanded_or_collapsed(self, item):
        self.log.resizeColumnToContents(0)
        self.log.resizeColumnToContents(1)

    def item_changed(self, item, column):
        self.fix_button.setEnabled(False)
        for it in self.top_level_items.values():
            if it.checkState(1):
                self.fix_button.setEnabled(True)

        self.delete_button.setEnabled(False)
        for it in self.all_items:
            if it.checkState(1):
                self.delete_button.setEnabled(True)
                return

    def delete_marked(self):
        if not confirm('<p>'+_('The marked files and folders will be '
               '<b>permanently deleted</b>. Are you sure?')
               +'</p>', 'check_library_editor_delete', self):
            return

        # Sort the paths in reverse length order so that we can be sure that
        # if an item is in another item, the sub-item will be deleted first.
        items = sorted(self.all_items,
                       key=lambda x: len(x.text(1)),
                       reverse=True)
        for it in items:
            if it.checkState(1):
                try:
                    p = os.path.join(self.db.library_path ,unicode(it.text(1)))
                    if os.path.isdir(p):
                        delete_tree(p)
                    else:
                        delete_file(p)
                except:
                    prints('failed to delete',
                            os.path.join(self.db.library_path,
                                unicode(it.text(1))))
        self.run_the_check()

    def fix_missing_formats(self):
        tl = self.top_level_items['missing_formats']
        child_count = tl.childCount()
        for i in range(0, child_count):
            item = tl.child(i)
            id = item.data(0, Qt.UserRole).toInt()[0]
            all = self.db.formats(id, index_is_id=True, verify_formats=False)
            all = set([f.strip() for f in all.split(',')]) if all else set()
            valid = self.db.formats(id, index_is_id=True, verify_formats=True)
            valid = set([f.strip() for f in valid.split(',')]) if valid else set()
            for fmt in all-valid:
                self.db.remove_format(id, fmt, index_is_id=True, db_only=True)

    def fix_missing_covers(self):
        tl = self.top_level_items['missing_covers']
        child_count = tl.childCount()
        for i in range(0, child_count):
            item = tl.child(i)
            id = item.data(0, Qt.UserRole).toInt()[0]
            self.db.set_has_cover(id, False)

    def fix_extra_covers(self):
        tl = self.top_level_items['extra_covers']
        child_count = tl.childCount()
        for i in range(0, child_count):
            item = tl.child(i)
            id = item.data(0, Qt.UserRole).toInt()[0]
            self.db.set_has_cover(id, True)

    def fix_items(self):
        for check in CHECKS:
            attr = check[0]
            fixable = check[3]
            tl = self.top_level_items[attr]
            if fixable and tl.checkState(1):
                func = getattr(self, 'fix_' + attr, None)
                if func is not None and callable(func):
                    func()
        self.run_the_check()

    def copy_to_clipboard(self):
        QApplication.clipboard().setText(self.text_results)
class CheckLibraryDialog(QDialog):
    def __init__(self, parent, db):
        QDialog.__init__(self, parent)
        self.db = db

        self.setWindowTitle(_('Check Library -- Problems Found'))
        self.setWindowIcon(QIcon(I('debug.png')))

        self._tl = QHBoxLayout()
        self.setLayout(self._tl)
        self.splitter = QSplitter(self)
        self.left = QWidget(self)
        self.splitter.addWidget(self.left)
        self.helpw = QTextEdit(self)
        self.splitter.addWidget(self.helpw)
        self._tl.addWidget(self.splitter)
        self._layout = QVBoxLayout()
        self.left.setLayout(self._layout)
        self.helpw.setReadOnly(True)
        self.helpw.setText(
            _('''\
        <h1>Help</h1>

        <p>calibre stores the list of your books and their metadata in a
        database. The actual book files and covers are stored as normal
        files in the calibre library folder. The database contains a list of the files
        and covers belonging to each book entry. This tool checks that the
        actual files in the library folder on your computer match the
        information in the database.</p>

        <p>The result of each type of check is shown to the left. The various
        checks are:
        </p>
        <ul>
        <li><b>Invalid titles</b>: These are files and folders appearing
        in the library where books titles should, but that do not have the
        correct form to be a book title.</li>
        <li><b>Extra titles</b>: These are extra files in your calibre
        library that appear to be correctly-formed titles, but have no corresponding
        entries in the database</li>
        <li><b>Invalid authors</b>: These are files appearing
        in the library where only author folders should be.</li>
        <li><b>Extra authors</b>: These are folders in the
        calibre library that appear to be authors but that do not have entries
        in the database</li>
        <li><b>Missing book formats</b>: These are book formats that are in
        the database but have no corresponding format file in the book's folder.
        <li><b>Extra book formats</b>: These are book format files found in
        the book's folder but not in the database.
        <li><b>Unknown files in books</b>: These are extra files in the
        folder of each book that do not correspond to a known format or cover
        file.</li>
        <li><b>Missing cover files</b>: These represent books that are marked
        in the database as having covers but the actual cover files are
        missing.</li>
        <li><b>Cover files not in database</b>: These are books that have
        cover files but are marked as not having covers in the database.</li>
        <li><b>Folder raising exception</b>: These represent folders in the
        calibre library that could not be processed/understood by this
        tool.</li>
        </ul>

        <p>There are two kinds of automatic fixes possible: <i>Delete
        marked</i> and <i>Fix marked</i>.</p>
        <p><i>Delete marked</i> is used to remove extra files/folders/covers that
        have no entries in the database. Check the box next to the item you want
        to delete. Use with caution.</p>

        <p><i>Fix marked</i> is applicable only to covers and missing formats
        (the three lines marked 'fixable'). In the case of missing cover files,
        checking the fixable box and pushing this button will tell calibre that
        there is no cover for all of the books listed. Use this option if you
        are not going to restore the covers from a backup. In the case of extra
        cover files, checking the fixable box and pushing this button will tell
        calibre that the cover files it found are correct for all the books
        listed. Use this when you are not going to delete the file(s). In the
        case of missing formats, checking the fixable box and pushing this
        button will tell calibre that the formats are really gone. Use this if
        you are not going to restore the formats from a backup.</p>

        '''))

        self.log = QTreeWidget(self)
        self.log.itemChanged.connect(self.item_changed)
        self.log.itemExpanded.connect(self.item_expanded_or_collapsed)
        self.log.itemCollapsed.connect(self.item_expanded_or_collapsed)
        self._layout.addWidget(self.log)

        self.check_button = QPushButton(_('&Run the check again'))
        self.check_button.setDefault(False)
        self.check_button.clicked.connect(self.run_the_check)
        self.copy_button = QPushButton(_('Copy &to clipboard'))
        self.copy_button.setDefault(False)
        self.copy_button.clicked.connect(self.copy_to_clipboard)
        self.ok_button = QPushButton(_('&Done'))
        self.ok_button.setDefault(True)
        self.ok_button.clicked.connect(self.accept)
        self.delete_button = QPushButton(_('Delete &marked'))
        self.delete_button.setToolTip(
            _('Delete marked files (checked subitems)'))
        self.delete_button.setDefault(False)
        self.delete_button.clicked.connect(self.delete_marked)
        self.fix_button = QPushButton(_('&Fix marked'))
        self.fix_button.setDefault(False)
        self.fix_button.setEnabled(False)
        self.fix_button.setToolTip(
            _('Fix marked sections (checked fixable items)'))
        self.fix_button.clicked.connect(self.fix_items)
        self.bbox = QDialogButtonBox(self)
        self.bbox.addButton(self.check_button, QDialogButtonBox.ActionRole)
        self.bbox.addButton(self.delete_button, QDialogButtonBox.ActionRole)
        self.bbox.addButton(self.fix_button, QDialogButtonBox.ActionRole)
        self.bbox.addButton(self.copy_button, QDialogButtonBox.ActionRole)
        self.bbox.addButton(self.ok_button, QDialogButtonBox.AcceptRole)

        h = QHBoxLayout()
        ln = QLabel(_('Names to ignore:'))
        h.addWidget(ln)
        self.name_ignores = QLineEdit()
        self.name_ignores.setText(
            db.prefs.get('check_library_ignore_names', ''))
        self.name_ignores.setToolTip(
            _('Enter comma-separated standard file name wildcards, such as synctoy*.dat'
              ))
        ln.setBuddy(self.name_ignores)
        h.addWidget(self.name_ignores)
        le = QLabel(_('Extensions to ignore'))
        h.addWidget(le)
        self.ext_ignores = QLineEdit()
        self.ext_ignores.setText(
            db.prefs.get('check_library_ignore_extensions', ''))
        self.ext_ignores.setToolTip(
            _('Enter comma-separated extensions without a leading dot. Used only in book folders'
              ))
        le.setBuddy(self.ext_ignores)
        h.addWidget(self.ext_ignores)
        self._layout.addLayout(h)

        self._layout.addWidget(self.bbox)
        self.resize(950, 500)
        self.bbox.setEnabled(True)

    def do_exec(self):
        self.run_the_check()

        probs = 0
        for c in self.problem_count:
            probs += self.problem_count[c]
        if probs == 0:
            return False
        self.exec_()
        return True

    def accept(self):
        self.db.prefs['check_library_ignore_extensions'] = \
                                            unicode(self.ext_ignores.text())
        self.db.prefs['check_library_ignore_names'] = \
                                            unicode(self.name_ignores.text())
        QDialog.accept(self)

    def box_to_list(self, txt):
        return [f.strip() for f in txt.split(',') if f.strip()]

    def run_the_check(self):
        checker = CheckLibrary(self.db.library_path, self.db)
        checker.scan_library(
            self.box_to_list(unicode(self.name_ignores.text())),
            self.box_to_list(unicode(self.ext_ignores.text())))

        plaintext = []

        def builder(tree, checker, check):
            attr, h, checkable, fixable = check
            list = getattr(checker, attr, None)
            if list is None:
                self.problem_count[attr] = 0
                return
            else:
                self.problem_count[attr] = len(list)

            tl = Item()
            tl.setText(0, h)
            if fixable and list:
                tl.setText(1, _('(fixable)'))
                tl.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
                tl.setCheckState(1, False)
            self.top_level_items[attr] = tl

            for problem in list:
                it = Item()
                if checkable:
                    it.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
                    it.setCheckState(1, False)
                else:
                    it.setFlags(Qt.ItemIsEnabled)
                it.setText(0, problem[0])
                it.setData(0, Qt.UserRole, problem[2])
                it.setText(1, problem[1])
                tl.addChild(it)
                self.all_items.append(it)
                plaintext.append(','.join([h, problem[0], problem[1]]))
            tree.addTopLevelItem(tl)

        t = self.log
        t.clear()
        t.setColumnCount(2)
        t.setHeaderLabels([_('Name'), _('Path from library')])
        self.all_items = []
        self.top_level_items = {}
        self.problem_count = {}
        for check in CHECKS:
            builder(t, checker, check)

        t.resizeColumnToContents(0)
        t.resizeColumnToContents(1)
        self.delete_button.setEnabled(False)
        self.text_results = '\n'.join(plaintext)

    def item_expanded_or_collapsed(self, item):
        self.log.resizeColumnToContents(0)
        self.log.resizeColumnToContents(1)

    def item_changed(self, item, column):
        self.fix_button.setEnabled(False)
        for it in self.top_level_items.values():
            if it.checkState(1):
                self.fix_button.setEnabled(True)

        self.delete_button.setEnabled(False)
        for it in self.all_items:
            if it.checkState(1):
                self.delete_button.setEnabled(True)
                return

    def delete_marked(self):
        if not confirm(
                '<p>' + _('The marked files and folders will be '
                          '<b>permanently deleted</b>. Are you sure?') +
                '</p>', 'check_library_editor_delete', self):
            return

        # Sort the paths in reverse length order so that we can be sure that
        # if an item is in another item, the sub-item will be deleted first.
        items = sorted(self.all_items,
                       key=lambda x: len(x.text(1)),
                       reverse=True)
        for it in items:
            if it.checkState(1):
                try:
                    p = os.path.join(self.db.library_path, unicode(it.text(1)))
                    if os.path.isdir(p):
                        delete_tree(p)
                    else:
                        delete_file(p)
                except:
                    prints(
                        'failed to delete',
                        os.path.join(self.db.library_path,
                                     unicode(it.text(1))))
        self.run_the_check()

    def fix_missing_formats(self):
        tl = self.top_level_items['missing_formats']
        child_count = tl.childCount()
        for i in range(0, child_count):
            item = tl.child(i)
            id = item.data(0, Qt.UserRole).toInt()[0]
            all = self.db.formats(id, index_is_id=True, verify_formats=False)
            all = set([f.strip() for f in all.split(',')]) if all else set()
            valid = self.db.formats(id, index_is_id=True, verify_formats=True)
            valid = set([f.strip()
                         for f in valid.split(',')]) if valid else set()
            for fmt in all - valid:
                self.db.remove_format(id, fmt, index_is_id=True, db_only=True)

    def fix_missing_covers(self):
        tl = self.top_level_items['missing_covers']
        child_count = tl.childCount()
        for i in range(0, child_count):
            item = tl.child(i)
            id = item.data(0, Qt.UserRole).toInt()[0]
            self.db.set_has_cover(id, False)

    def fix_extra_covers(self):
        tl = self.top_level_items['extra_covers']
        child_count = tl.childCount()
        for i in range(0, child_count):
            item = tl.child(i)
            id = item.data(0, Qt.UserRole).toInt()[0]
            self.db.set_has_cover(id, True)

    def fix_items(self):
        for check in CHECKS:
            attr = check[0]
            fixable = check[3]
            tl = self.top_level_items[attr]
            if fixable and tl.checkState(1):
                func = getattr(self, 'fix_' + attr, None)
                if func is not None and callable(func):
                    func()
        self.run_the_check()

    def copy_to_clipboard(self):
        QApplication.clipboard().setText(self.text_results)
class DownloadDialog(QDialog):

    def __init__(self, gui, icon, do_user_config):
        QDialog.__init__(self, gui)
        self.gui = gui
        self.do_user_config = do_user_config

        # The current database shown in the GUI
        self.db = gui.current_db

        self.prefs = PrefsFacade(self.db)

        self.version = Downloader.version

        # The GUI, created and layouted by hand...
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        self.setWindowTitle('Beam EBooks Downloader')
        self.setWindowIcon(icon)

        self.log_area = QTextEdit('Log output', self)
        self.log_area.setReadOnly(True)
        self.log_area.setLineWrapMode(QTextEdit.NoWrap);
        self.log_area.setText("")
        self.layout.addWidget(self.log_area)

        self.download_button = QPushButton('Download books', self)
        self.download_button.clicked.connect(self.download)
        self.layout.addWidget(self.download_button)

        self.conf_button = QPushButton('Configure this plugin', self)
        self.conf_button.clicked.connect(self.config)
        self.layout.addWidget(self.conf_button)

        self.resize(self.sizeHint())


    def config(self):
        self.do_user_config(parent=self)
        # Apply the changes
        # Not necessary, the downloader will obtain fresh config anyway...
        # self.label.setText(prefs['hello_world_msg'])


    def notify(self, message = None):
        if message is not None:
            # insertPlainText inserts at the beginning of the log area...
            self.log_area.append(message)
            sb = self.log_area.verticalScrollBar()
            sb.setValue(sb.maximum())


    def download(self):
        prefs = self.prefs

        self.download_button.setEnabled(False)
        self.conf_button.setEnabled(False)

        downloader = BeamEbooksDownloader(self.prefs, self.version, caller = self)
        self.notify("Downloader is: %s" % (downloader))

        # Loop over all accounts until we have support for selection
        for account_id in prefs[prefs.ACCOUNTS]:
            account = prefs[prefs.ACCOUNTS][account_id]
            account[prefs.ACCOUNT_ID] = account_id

            if account[prefs.ENABLED]:
                self.enqueue(account, downloader)

        self.hide()


    def enqueue(self, account, downloader):
        prefs = self.prefs

        self.notify("Account: '%s'" % account[prefs.USERNAME])
        # downloader.login(account)

        func = 'arbitrary_n'
        # func = 'arbitrary'
        cpus = self.gui.job_manager.server.pool_size
        print "CPUs: %s" % (cpus)
        args = ['calibre_plugins.beam_ebooks_downloader.jobs', 'do_obtain_new_books', (cpus, account)]
        desc = 'Beam EBooks Downloader'
        job = self.gui.job_manager.run_job(Dispatcher(self._done), func, args=args, description=desc)
        print "Job: %s" % (job)

        self.notify("  Start parsing OPDS catalog")

        # if downloader.successful_login == False:
        #     self.notify("Failed to log in...")
        # else:
        #     self.notify("Scanning (beam) private library now...")
        #     downloader.recursive_descent(norms(prefs[prefs.URLBASE]))

    def _done(self, job):
        print "Done Downloading"
        print "Self: %s" % (self)
        print "Job: %s" % (job)
        # print "  Result: %s" % (job.result)
        # print "  Result: %s" % (len(job.result))
        self.notify("  Finished download catalog...")

        if job.result is not None:
            for entry in job.result:
                beamebooks_id = entry['id']
                message = "<br/>Ebook # %s" % (beamebooks_id)
                self.notify(message)

        self.download_button.setEnabled(True)
        self.conf_button.setEnabled(True)

        payload = job.result
        msg = "Parsing OPDS Catalog complete, found %s ebooks." % (len(job.result))
        msg = msg + "<br>Do you want to import the books into the library?"

        # question_dialog
        self.gui.proceed_question(self._add_ebooks, payload, job.details,
                    'OPDS Download Log', 'OPDS parse complete', msg, show_copy_button = False)


    def _done_2(self, job):
        print "Done Downloading"
        print "Self: %s" % (self)
        print "Job: %s" % (job)
        # print "  Result: %s" % (job.result)
        # print "  Result: %s" % (len(job.result))
        self.notify("  Finished download book...")


    def _add_ebooks(self, payload):
        print "Done Downloading, Step 2"
        print "Self: %s" % (self)
        # print "Payload: %s" % (payload)
        # Printing the complete payload at once gives [IOError 12] - Out of space (memory, that is...)
        self.notify("  Finished adding books...")
        for entry in payload:
            print "Entry: %s" % entry

            # TODO allow for checkbox list of entries to download
            func = 'arbitrary_n'
            # func = 'arbitrary'
            cpus = self.gui.job_manager.server.pool_size
            print "CPUs: %s" % (cpus)
            args = ['calibre_plugins.beam_ebooks_downloader.jobs', 'do_download_book', (cpus, entry)]
            desc = 'Beam EBooks Downloader'
            job = self.gui.job_manager.run_job(Dispatcher(self._done_2), func, args=args, description=desc)
            print "Job: %s" % (job)
Exemple #5
0
class IPETLogFileView(QWidget):
    StyleSheet = """
        QComboBox { color: darkblue; }
        QTextEdit { font-family : monospace;
                    font-size : 12px; }
        """
    """
    a view of a log file, with selection mechanisms for the desired test run and problem that should be shown
    """
    def __init__(self, parent=None):
        super(IPETLogFileView, self).__init__(parent)
        vlayout = QVBoxLayout(self)
        self.textbrowser = QTextEdit(self)
        self.textbrowser.setReadOnly(True)
        self.testrunselection = OptionsComboBox(self)
        self.problemselection = OptionsComboBox(self)
        vlayout.addWidget(self.textbrowser)
        self.setStyleSheet(self.StyleSheet)
        hlayout = QHBoxLayout()
        testrunselectionlabel = QLabel("Select a test run", self)
        problemselectionlabel = QLabel("Select an instance", self)
        testrunselectionlabel.setBuddy(self.testrunselection)
        problemselectionlabel.setBuddy(self.problemselection)
        for l, s in [(testrunselectionlabel, self.testrunselection),
                     (problemselectionlabel, self.problemselection)]:
            v = QVBoxLayout(self)
            v.addWidget(l)
            v.addWidget(s)
            hlayout.addLayout(v)
        vlayout.addLayout(hlayout)
        self.setLayout(vlayout)

        self.initConnections()

    def getProblem(self):
        problem = self.problemselection.currentText()
        return problem

    def getSelectedText(self):
        return self.textbrowser.textCursor().selection()

    def getLineSelectionIndex(self):
        c = self.textbrowser.textCursor()
        if c.anchor() < c.position():
            # normal left to right selection
            return (c.positionInBlock() -
                    (c.selectionEnd() - c.selectionStart()),
                    c.positionInBlock())
        else:
            return (c.positionInBlock(), c.positionInBlock() +
                    (c.selectionEnd() - c.selectionStart()))

    def getSelectedLine(self):
        cursor = self.textbrowser.textCursor()
        cursor.select(QTextCursor.LineUnderCursor)
        return str(cursor.selectedText())

    def getTestRun(self):
        testruntext = self.testrunselection.currentText()
        for t in ExperimentManagement.getExperiment().getTestRuns():
            if t.getName() == testruntext:
                return t
        return None

    def initConnections(self):
        for s in (self.testrunselection, self.problemselection):
            self.connect(s, SIGNAL("currentIndexChanged(int)"),
                         self.updateView)

    def updateExperimentData(self):
        self.testrunselection.clear()
        self.problemselection.clear()

        testruns = ExperimentManagement.getExperiment().getTestRuns()
        problems = ExperimentManagement.getExperiment().getProblemNames()

        self.testrunselection.addItems([t.getName() for t in testruns])
        self.problemselection.addItems([p for p in problems])

    def updateView(self):
        selectedtestrun = self.getTestRun()
        selectedproblem = self.getProblem()
        if selectedtestrun is None:
            return
        outfile = selectedtestrun.getLogFile()

        with open(outfile, 'r') as in_file:
            linesstart = selectedtestrun.getProblemDataById(
                str(selectedproblem), "LineNumbers_BeginLogFile")
            linesend = selectedtestrun.getProblemDataById(
                str(selectedproblem), "LineNumbers_EndLogFile")
            self.text = []
            for idx, line in enumerate(in_file):
                if idx > linesend:
                    break
                elif idx >= linesstart:
                    self.text.append(line)
            self.text = "".join(self.text)
            self.textbrowser.setText(self.text)
            self.textbrowser.update()
Exemple #6
0
class ServiceControl(QDialog):
    SSHcnt =0
    HTTPcnt =0
    FTPcnt =0
    ip =''     

    
    def __init__(self, parent=None): 
        
        
        self.ip=''
        
        super(ServiceControl, self).__init__(parent)
        try:
            self.ip = ni.ifaddresses('eth0')[2][0]['addr']
            
            
        except:
            self.ip = 'No Network '
            
        

# servis etiketleri...........................        
        SSHLabel = QLabel('<center><font color="#2980B9" size="4"> SSH </font></center>')
        HTTPLabel = QLabel('<center><font color="#2980B9" size="4"> HTTP </font></center>')
        FTPLabel = QLabel('<center><font color="#2980B9" size="4"> FTP </font></center>')
        
# servis calisiyor etiketleri ..............................     
     
        self.SSHRLabel = QLabel()
        self.HTTPRLabel =QLabel()
        self.FTPRLabel =QLabel()
        
# servis Buttonlari.....................................        
        self.SSHBtn=QPushButton()
        self.HTTPBtn=QPushButton()
        self.FTPBtn=QPushButton()
        
# servis Buttonlarina tiklandigi zaman gidecekleri fonsiyonlar...................       
        
        self.SSHBtn.clicked.connect(self.SSH_Change)
        self.HTTPBtn.clicked.connect(self.HTTP_Change)
        self.FTPBtn.clicked.connect(self.FTP_Change)        
        
        self.HTTPRestart = QPushButton('Restart')       
        self.FTPRestart = QPushButton('Restart')        
        self.SSHRestart = QPushButton('Restart')
        
        self.SSHRestart.clicked.connect(self.SSH_Restart)
        self.HTTPRestart.clicked.connect(self.Http_Restart)
        self.FTPRestart.clicked.connect(self.Ftp_Restart) 
        
        
        
# logo...................................
        self.label = QLabel()
        self.pixmap = QPixmap(os.getcwd() + '/p2.png')
        self.label.setPixmap(self.pixmap)
    
        
       
        
# ssh kapaliysa kapali yaz... 

        Stringssh = commands.getoutput('/etc/init.d/ssh status')        
        Stringhttp = commands.getoutput('/etc/init.d/lighttpd status')
        Stringftp = os.system('/etc/init.d/vsftpd status')
        
        StringCheck = 'Active: inactive'

        Cssh = Stringssh.find('Active: active')
        Chttp = Stringhttp.find('Active: active')
        Cftp = Stringssh.find('Active: active')

    
        
        if Cssh != -1:
            self.SSHBtn.setText('DURDUR')
            self.SSHRLabel.setText('<center><font color="#2980B9" size="2"> Runing... </font></center>')
            print('Aktif')
    
        else:
            self.SSHBtn.setText('BASLAT')
                       
            
        if Chttp !=-1:
            self.HTTPBtn.setText('DURDUR')
            self.HTTPRLabel.setText('<center><font color="#2980B9" size="2"> Runing... </font></center>')
            print('Aktif')
        else:
            self.HTTPBtn.setText('BASLAT')
            
            
        if Cftp !=-1:
            self.FTPBtn.setText('DURDUR')
            self.FTPRLabel.setText('<center><font color="#2980B9" size="2"> Runing... </font></center>')
            print('Aktif')
        else:
            self.FTPBtn.setText('BASLAT')


    
# alt kisim. ip ve kernel surumleri.............

        self.Text = QTextEdit()
        self.Text.setText('Pardus ARM Servis Kontrol'+'\n'+
                          'Host Name  : ' +
                          str(commands.getoutput("hostname")) + '\n'+
                          'Kernel  : '+
                          str(commands.getoutput("uname -r")) +'\n' 
                          +'IP Adress  : '+ self.ip)
        self.Text.setReadOnly(True)
        
        
        
# grid layout etiketleri ve buttonlari ekliyoruz.....................

        kutu = QHBoxLayout()
        kutu.addStretch(1)
        kutu.addWidget(self.label)
        
        
        grid = QGridLayout()
      #  grid.addWidget(kutu,3,0,0,0)
        grid.addWidget(self.label,3,0,4,4)
        
        grid.addWidget(SSHLabel,3,1)
        grid.addWidget(HTTPLabel,4,1)
        grid.addWidget(FTPLabel,5,1)
        
        grid.addWidget(self.SSHRLabel,3,2)
        grid.addWidget(self.HTTPRLabel,4,2)
        grid.addWidget(self.FTPRLabel,5,2)
        
        grid.addWidget(self.SSHBtn,3,3)
        grid.addWidget(self.HTTPBtn,4,3)
        grid.addWidget(self.FTPBtn,5,3)
        
        grid.addWidget(self.SSHRestart,3,4)
        grid.addWidget(self.HTTPRestart,4,4)
        grid.addWidget(self.FTPRestart,5,4)
        
        
        grid.addWidget(self.Text, 8, 0, 7, 4)      
        
        self.setLayout(grid)        
        self.setWindowTitle('Pardus Servis Control')
        self.setFixedSize(650, 350)
        
        
    def SSH_Change(self):
   
        if(self.SSHBtn.text()=='BASLAT'):
            os.system('/etc/init.d/ssh start')
            self.SSHBtn.setText('DURDUR')
            self.SSHRLabel.setText('<center><font color="#2980B9" size="2"> Runing... </font></center>') 
            
                       
            
        else:
            os.system('/etc/init.d/ssh stop')
            self.SSHRLabel.setText('')            
            self.SSHBtn.setText('BASLAT')
            print('kapandi') 
            
    
    def HTTP_Change(self):
          
        if(self.HTTPBtn.text()=='BASLAT'):
            os.system('/etc/init.d/lighttpd start')
            self.HTTPBtn.setText('DURDUR')
            self.HTTPRLabel.setText('<center><font color="#2980B9" size="2"> Runing... </font></center>') 
                        
            
        else:
            os.system('/etc/init.d/lighttpd stop') 
            self.HTTPBtn.setText('BASLAT')
            self.HTTPRLabel.setText('')
            print('kapandi') 
            
    def FTP_Change(self):
          
        if(self.FTPBtn.text()=='BASLAT'):
            os.system('/etc/init.d/vsftpd start')
            self.FTPBtn.setText('DURDUR')
            self.FTPRLabel.setText('<center><font color="#2980B9" size="2"> Runing... </font></center>') 
            print('acildi')                 
            
        else:
            os.system('/etc/init.d/vsftpd stop') 
            self.FTPBtn.setText('BASLAT')
            self.FTPRLabel.setText('')
            print('kapandi')         

            
            
    def SSH_Restart(self):
        os.system('/etc/init.d/ssh restart') 
        
    def Http_Restart(self):
        os.system('/etc/init.d/lighttpd restart')
        
    def Ftp_Restart(self):
        os.system('/etc/init.d/vsftpd restart')  

    def Close(self):
        
        self.close()
Exemple #7
0
class LedgerAuthDialog(QDialog):
    def __init__(self, handler, data):
        '''Ask user for 2nd factor authentication. Support text, security card and paired mobile methods.
        Use last method from settings, but support new pairing and downgrade.
        '''
        QDialog.__init__(self, handler.top_level_window())
        self.handler = handler
        self.txdata = data
        self.idxs = self.txdata['keycardData'] if self.txdata['confirmationType'] > 1 else ''
        self.setMinimumWidth(600)
        self.setWindowTitle(_("Ledger Wallet Authentication"))
        self.cfg = copy.deepcopy(self.handler.win.wallet.get_keystore().cfg)
        self.dongle = self.handler.win.wallet.get_keystore().get_client().dongle
        self.ws = None
        self.pin = ''
        
        self.devmode = self.getDevice2FAMode()
        if self.devmode == 0x11 or self.txdata['confirmationType'] == 1:
            self.cfg['mode'] = 0
        
        vbox = QVBoxLayout()
        self.setLayout(vbox)
        
        def on_change_mode(idx):
            if idx < 2 and self.ws:
                self.ws.stop()
                self.ws = None
            self.cfg['mode'] = 0 if self.devmode == 0x11 else idx if idx > 0 else 1
            if self.cfg['mode'] > 1 and self.cfg['pair'] and not self.ws:
                self.req_validation()
            if self.cfg['mode'] > 0:
                self.handler.win.wallet.get_keystore().cfg = self.cfg
                self.handler.win.wallet.save_keystore()
            self.update_dlg()
        def add_pairing():
            self.do_pairing()
        def return_pin():
            self.pin = self.pintxt.text() if self.txdata['confirmationType'] == 1 else self.cardtxt.text() 
            if self.cfg['mode'] == 1:
                self.pin = ''.join(chr(int(str(i),16)) for i in self.pin)
            self.accept()
        
        self.modebox = QWidget()
        modelayout = QHBoxLayout()
        self.modebox.setLayout(modelayout)
        modelayout.addWidget(QLabel(_("Method:")))
        self.modes = QComboBox()
        modelayout.addWidget(self.modes, 2)
        self.addPair = QPushButton(_("Pair"))
        self.addPair.setMaximumWidth(60)
        modelayout.addWidget(self.addPair)
        modelayout.addStretch(1)
        self.modebox.setMaximumHeight(50)
        vbox.addWidget(self.modebox)
        
        self.populate_modes()
        self.modes.currentIndexChanged.connect(on_change_mode)
        self.addPair.clicked.connect(add_pairing)
        
        self.helpmsg = QTextEdit()
        self.helpmsg.setStyleSheet("QTextEdit { background-color: lightgray; }")
        self.helpmsg.setReadOnly(True)
        vbox.addWidget(self.helpmsg)
        
        self.pinbox = QWidget()
        pinlayout = QHBoxLayout()
        self.pinbox.setLayout(pinlayout)
        self.pintxt = QLineEdit()
        self.pintxt.setEchoMode(2)
        self.pintxt.setMaxLength(4)
        self.pintxt.returnPressed.connect(return_pin)
        pinlayout.addWidget(QLabel(_("Enter PIN:")))
        pinlayout.addWidget(self.pintxt)
        pinlayout.addWidget(QLabel(_("NOT DEVICE PIN - see above")))
        pinlayout.addStretch(1)
        self.pinbox.setVisible(self.cfg['mode'] == 0)
        vbox.addWidget(self.pinbox)
                    
        self.cardbox = QWidget()
        card = QVBoxLayout()
        self.cardbox.setLayout(card)
        self.addrtext = QTextEdit()
        self.addrtext.setStyleSheet("QTextEdit { color:blue; background-color:lightgray; padding:15px 10px; border:none; font-size:20pt; }")
        self.addrtext.setReadOnly(True)
        self.addrtext.setMaximumHeight(120)
        card.addWidget(self.addrtext)
        
        def pin_changed(s):
            if len(s) < len(self.idxs):
                i = self.idxs[len(s)]
                addr = self.txdata['address']
                addr = addr[:i] + '<u><b>' + addr[i:i+1] + '</u></b>' + addr[i+1:]
                self.addrtext.setHtml(str(addr))
            else:
                self.addrtext.setHtml(_("Press Enter"))
                
        pin_changed('')    
        cardpin = QHBoxLayout()
        cardpin.addWidget(QLabel(_("Enter PIN:")))
        self.cardtxt = QLineEdit()
        self.cardtxt.setEchoMode(2)
        self.cardtxt.setMaxLength(len(self.idxs))
        self.cardtxt.textChanged.connect(pin_changed)
        self.cardtxt.returnPressed.connect(return_pin)
        cardpin.addWidget(self.cardtxt)
        cardpin.addWidget(QLabel(_("NOT DEVICE PIN - see above")))
        cardpin.addStretch(1)
        card.addLayout(cardpin)
        self.cardbox.setVisible(self.cfg['mode'] == 1)
        vbox.addWidget(self.cardbox)
        
        self.pairbox = QWidget()
        pairlayout = QVBoxLayout()
        self.pairbox.setLayout(pairlayout)
        pairhelp = QTextEdit(helpTxt[5])
        pairhelp.setStyleSheet("QTextEdit { background-color: lightgray; }")
        pairhelp.setReadOnly(True)
        pairlayout.addWidget(pairhelp, 1)
        self.pairqr = QRCodeWidget()
        pairlayout.addWidget(self.pairqr, 4)
        self.pairbox.setVisible(False)
        vbox.addWidget(self.pairbox)
        self.update_dlg()
        
        if self.cfg['mode'] > 1 and not self.ws:
            self.req_validation()
        
    def populate_modes(self):
        self.modes.blockSignals(True)
        self.modes.clear()
        self.modes.addItem(_("Summary Text PIN (requires dongle replugging)") if self.txdata['confirmationType'] == 1 else _("Summary Text PIN is Disabled"))
        if self.txdata['confirmationType'] > 1:
            self.modes.addItem(_("Security Card Challenge"))
            if not self.cfg['pair']:
                self.modes.addItem(_("Mobile - Not paired")) 
            else:
                self.modes.addItem(_("Mobile - %s") % self.cfg['pair'][1]) 
        self.modes.blockSignals(False)
        
    def update_dlg(self):
        self.modes.setCurrentIndex(self.cfg['mode'])
        self.modebox.setVisible(True)
        self.addPair.setText(_("Pair") if not self.cfg['pair'] else _("Re-Pair"))
        self.addPair.setVisible(self.txdata['confirmationType'] > 2)
        self.helpmsg.setText(helpTxt[self.cfg['mode'] if self.cfg['mode'] < 2 else 2 if self.cfg['pair'] else 4])
        self.helpmsg.setMinimumHeight(180 if self.txdata['confirmationType'] == 1 else 100)
        self.pairbox.setVisible(False)
        self.helpmsg.setVisible(True)
        self.pinbox.setVisible(self.cfg['mode'] == 0)
        self.cardbox.setVisible(self.cfg['mode'] == 1)
        self.pintxt.setFocus(True) if self.cfg['mode'] == 0 else self.cardtxt.setFocus(True)
        self.setMaximumHeight(200)
        
    def do_pairing(self):
        rng = os.urandom(16)
        pairID = rng.encode('hex') + hashlib.sha256(rng).digest()[0].encode('hex')
        self.pairqr.setData(pairID)
        self.modebox.setVisible(False)
        self.helpmsg.setVisible(False)
        self.pinbox.setVisible(False)
        self.cardbox.setVisible(False)
        self.pairbox.setVisible(True)
        self.pairqr.setMinimumSize(300,300)
        if self.ws:
            self.ws.stop()
        self.ws = LedgerWebSocket(self, pairID)
        self.ws.pairing_done.connect(self.pairing_done)
        self.ws.start() 
               
    def pairing_done(self, data):
        if data is not None:
            self.cfg['pair'] = [ data['pairid'], data['name'], data['platform'] ]
            self.cfg['mode'] = 2
            self.handler.win.wallet.get_keystore().cfg = self.cfg
            self.handler.win.wallet.save_keystore()
        self.pin = 'paired'
        self.accept()
    
    def req_validation(self):
        if self.cfg['pair'] and 'secureScreenData' in self.txdata:
            if self.ws:
                self.ws.stop()
            self.ws = LedgerWebSocket(self, self.cfg['pair'][0], self.txdata)
            self.ws.req_updated.connect(self.req_updated)
            self.ws.start()
              
    def req_updated(self, pin):
        if pin == 'accepted':
            self.helpmsg.setText(helpTxt[3])
        else:
            self.pin = str(pin)
            self.accept()
        
    def getDevice2FAMode(self):
        apdu = [0xe0, 0x24, 0x01, 0x00, 0x00, 0x01] # get 2fa mode
        try:
            mode = self.dongle.exchange( bytearray(apdu) )
            return mode
        except BTChipException, e:
            debug_msg('Device getMode Failed')
        return 0x11
class TwitterGui(QWidget):    
    URL_REGEX = re.compile(r'''((?:mailto:|ftp://|http://|https://)[^ <>'"{}|\\^`[\]]*)''')
    
    def __init__(self, parent, logger, db_conn, update_func, safe_conn):
        super(TwitterGui, self).__init__(parent)
        self._db_conn = db_conn
        self.logger = logger
        self._reply_to_id = 0
        self._update_func = update_func
        
        self._list = None
        
        if get_settings().get_proxy():
            u = urlparse.urlsplit(get_settings().get_proxy())
            proxy = QNetworkProxy()
            
            proxy.setType(QNetworkProxy.HttpProxy)
            proxy.setHostName(u.hostname);
            proxy.setPort(u.port)
            QNetworkProxy.setApplicationProxy(proxy);
        
        self.msgview = QWebView(self)
        self.msgview.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self.msgview.linkClicked.connect(self.link_clicked)
        
        self.userCombo = QComboBox(self)
        self.userCombo.setEditable(True)
        self.userCombo.activated.connect(self.toggle_user_in_list)
        
        self.showButton = QPushButton(chr(94), self)  
        self.showButton.setMaximumHeight(13)
        self.showButton.clicked.connect(self.show_hide_animation)
        
        self.post_field = QTextEdit(self)
        self.post_field.setMaximumHeight(50)
        self.post_field.textChanged.connect(self.text_changed)
        self.send_button = QPushButton("Post", self)
        self.send_button.clicked.connect(self.post_status_clicked)
        self.refresh_button = QPushButton("Refresh", self)
        self.refresh_button.clicked.connect(self._update_func)
        self.attach_button = QPushButton("Attach", self)
        self.attach_button.clicked.connect(lambda _ : self.set_status("Attach something"))
        self.lists_box = QComboBox(self)
        self.lists_box.currentIndexChanged.connect(self.list_changed)
        self.lists_box.setEditable(False)
        self.lists_box.addItems([u"Home"] + self._db_conn.get_lists())
        self.statusLabel = QLabel("Status", self)
        self.charCounter = QLabel("0", self)
        
        self.gridw = QWidget(self)
        self.gridw.setContentsMargins(0, 0, 0, 0)
        gridlay = QGridLayout(self.gridw)
        gridlay.setContentsMargins(0, 0, 0, 0)
        gridlay.addWidget(self.post_field, 0, 0, 2, 1)
        gridlay.addWidget(self.attach_button, 0, 1, 1, 1)
        gridlay.addWidget(self.send_button, 1, 1, 1, 1)
        gridlay.addWidget(self.lists_box, 0, 2, 1, 1)
        gridlay.addWidget(self.refresh_button, 1, 2, 1, 1)
        gridlay.addWidget(self.statusLabel, 2, 0, 1, 1)
        gridlay.addWidget(self.charCounter, 2, 1, 1, 2)
        
        hlay = QVBoxLayout(self)
        hlay.addWidget(self.msgview)
        hlay.addWidget(self.userCombo)
        hlay.addWidget(self.showButton)
        hlay.addWidget(self.gridw)
        
        safe_conn.connect_home_timeline_updated(self.update_view)
        safe_conn.connect_twitter_loop_started(self.start_refresh_animation)
        safe_conn.connect_twitter_loop_stopped(self.stop_refresh_animation)
        safe_conn.connect_update_posted(self.enable_posting)
        safe_conn.connect_range_limit_exceeded(lambda _ : self.set_status("Range limit exceeded"))
        safe_conn.connect_not_authenticated(lambda _ : self.set_status("Authentication failed"))
        
        self.gridw.hide()
        self.update_view()
        self.set_status("Twitter plugin initialized")
        
    def enable_posting(self, q_id, m_id):
        if m_id>1:
            self.post_field.setText("")
            self.set_status("Tweet posted")
        else:
            self.set_status("Failed to post tweet, Error: " + str(abs(m_id)))
        self.post_field.setEnabled(True)
        
    def link_clicked(self, url):
        if not url.host():
            if url.hasQueryItem("reply-to") and url.hasQueryItem("screen-name"):
                self._reply_to_id = long(convert_string(url.queryItemValue("reply-to")))
                self.post_field.setPlainText("@"+convert_string(url.queryItemValue("screen-name"))+" ")
                self.set_status("Reply to @"+convert_string(url.queryItemValue("screen-name")))
            else:
                self.logger.error("Unknown command from link: "+str(url.toString()))
        else:
            webbrowser.open(str(url.toString()))
            
    def list_changed(self, list_idx):
        if list_idx:
            self._list = convert_string(self.lists_box.currentText())
            self.userCombo.clear()
            self.userCombo.addItems(self._db_conn.get_known_users())            
            self.userCombo.completer().setCompletionMode(QCompleter.PopupCompletion)
            self.userCombo.show()
            self.set_status(self._list)
        else:
            self.userCombo.hide()
            self._list = None
        self.update_view()
        
    def post_status_clicked(self):
        msg = unicode(self.post_field.toPlainText().toUtf8(), encoding="UTF-8")
        if msg:
            self._db_conn.insert_post_queue(msg, self._reply_to_id)
            self._reply_to_id = 0
            self._update_func()
            self.post_field.setDisabled(True)
    
    def start_refresh_animation(self):
        self.refresh_button.setDisabled(True)
    
    def stop_refresh_animation(self):
        self.refresh_button.setEnabled(True)
        
    def show_hide_animation(self):
        if self.gridw.isHidden():
            self.gridw.show()            
            self.showButton.setText("v")
        else:
            self.gridw.hide()
            self.showButton.setText(chr(94))
            
    def text_changed(self):
        count = len(self.post_field.toPlainText())
        if count==0:
            self._reply_to_id = 0
        if self._reply_to_id:
            self.charCounter.setText(str(count) + " - reply to ")
        else:
            self.charCounter.setText(str(count))
            
    def toggle_user_in_list(self, _):
        user = convert_string(self.userCombo.currentText())
        if user in self._db_conn.get_users_from_list(self._list):
            self._db_conn.delete_user_from_list(user, self._list)
            self.set_status("Removed user %s from list %s"%(user, self._list))
        else:
            self._db_conn.add_user_to_list(user, self._list)
            self.set_status("Added user %s to list %s"%(user, self._list))
        self.update_view()
        
        
    @pyqtSlot(object)
    def update_view(self, _ = None):
        template_file_path = os.path.join(get_settings().get_main_config_dir(),"tweet.thtml")
        
        tweets = self._db_conn.get_last_tweets(user_list = self._list)
        if len(tweets)==0:
            return 0
        
        templ_text = '<div>\
                    <a href="http://twitter.com/$NAME$/status/$ID$">\
                            <img src="$IMAGE$" style="float: left; margin-right: 2px" alt="$NAME$" title="$NAME$"/>\
                    </a>\
                    <p>$TEXT$</p>\
                    <span><a href="http://twitter.com/$RT_USER$">$RT_USER$</a></span>\
                    <span style="float: right">$CREATE_TIME$ <a href="?retweet=$ID$">retweet</a> <a href="?reply-to=$ID$&screen-name=$NAME$">reply</a></span>\
                    </div>\
                    <hr style="clear: both" />\
                    '
        if os.path.exists(template_file_path):
            t_file = open(template_file_path, "r")
            templ_text = t_file.read()
            t_file.close()
        
        txt = ""
        for t in tweets:
            """m_id, screen_name, user_image, create_time, message_text, retweeted_by"""
            text = self.URL_REGEX.sub(r'<a href="\1">\1</a>', t[4])
            t_txt = templ_text.replace("$IMAGE$", t[2]).replace("$NAME$", t[1])
            t_txt = t_txt.replace("$ID$", str(t[0])).replace("$TEXT$", text)
            t_txt = t_txt.replace("$CREATE_TIME$", self.humanReadableTime(t[3]))
            t_txt = t_txt.replace("$RT_USER$", t[5] if t[5] else "")
            txt += t_txt
        txt += "<p style=\"float:right\">Updated: %s</p>"%time.strftime("%H:%M")

        self.msgview.setHtml(txt)
        
            
    """helper:"""
    def set_status(self, status_text):
        self.statusLabel.setText(status_text)
        self.showButton.setToolTip(status_text)
    
    def humanReadableTime(self, post_time):
        fudge = 1.25
        delta = long(time.time()) - long(post_time)
    
        if delta < (1 * fudge):
            return 'about a second ago' 
        elif delta < (60 * (1 / fudge)):
            return 'about %d seconds ago' % (delta)
        elif delta < (60 * fudge):
            return 'about a minute ago'
        elif delta < (60 * 60 * (1 / fudge)):
            return 'about %d minutes ago' % (delta / 60)
        elif delta < (60 * 60 * fudge) or delta / (60 * 60) == 1:
            return 'about an hour ago'
        elif delta < (60 * 60 * 24 * (1 / fudge)):
            return 'about %d hours ago' % (delta / (60 * 60))
        elif delta < (60 * 60 * 24 * fudge) or delta / (60 * 60 * 24) == 1:
            return 'about a day ago'
        else:
            return 'about %d days ago' % (delta / (60 * 60 * 24))
class DownloadDialog(QDialog):
    def __init__(self, gui, icon, do_user_config):
        QDialog.__init__(self, gui)
        self.gui = gui
        self.do_user_config = do_user_config

        # The current database shown in the GUI
        self.db = gui.current_db

        self.prefs = PrefsFacade(self.db)

        self.version = Downloader.version

        # The GUI, created and layouted by hand...
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        self.setWindowTitle('Beam EBooks Downloader')
        self.setWindowIcon(icon)

        self.log_area = QTextEdit('Log output', self)
        self.log_area.setReadOnly(True)
        self.log_area.setLineWrapMode(QTextEdit.NoWrap)
        self.log_area.setText("")
        self.layout.addWidget(self.log_area)

        self.download_button = QPushButton('Download books', self)
        self.download_button.clicked.connect(self.download)
        self.layout.addWidget(self.download_button)

        self.conf_button = QPushButton('Configure this plugin', self)
        self.conf_button.clicked.connect(self.config)
        self.layout.addWidget(self.conf_button)

        self.resize(self.sizeHint())

    def config(self):
        self.do_user_config(parent=self)
        # Apply the changes
        # Not necessary, the downloader will obtain fresh config anyway...
        # self.label.setText(prefs['hello_world_msg'])

    def notify(self, message=None):
        if message is not None:
            # insertPlainText inserts at the beginning of the log area...
            self.log_area.append(message)
            sb = self.log_area.verticalScrollBar()
            sb.setValue(sb.maximum())

    def download(self):
        prefs = self.prefs

        self.download_button.setEnabled(False)
        self.conf_button.setEnabled(False)

        downloader = BeamEbooksDownloader(self.prefs,
                                          self.version,
                                          caller=self)
        self.notify("Downloader is: %s" % (downloader))

        # Loop over all accounts until we have support for selection
        for account_id in prefs[prefs.ACCOUNTS]:
            account = prefs[prefs.ACCOUNTS][account_id]
            account[prefs.ACCOUNT_ID] = account_id

            if account[prefs.ENABLED]:
                self.enqueue(account, downloader)

        self.hide()

    def enqueue(self, account, downloader):
        prefs = self.prefs

        self.notify("Account: '%s'" % account[prefs.USERNAME])
        # downloader.login(account)

        func = 'arbitrary_n'
        # func = 'arbitrary'
        cpus = self.gui.job_manager.server.pool_size
        print "CPUs: %s" % (cpus)
        args = [
            'calibre_plugins.beam_ebooks_downloader.jobs',
            'do_obtain_new_books', (cpus, account)
        ]
        desc = 'Beam EBooks Downloader'
        job = self.gui.job_manager.run_job(Dispatcher(self._done),
                                           func,
                                           args=args,
                                           description=desc)
        print "Job: %s" % (job)

        self.notify("  Start parsing OPDS catalog")

        # if downloader.successful_login == False:
        #     self.notify("Failed to log in...")
        # else:
        #     self.notify("Scanning (beam) private library now...")
        #     downloader.recursive_descent(norms(prefs[prefs.URLBASE]))

    def _done(self, job):
        print "Done Downloading"
        print "Self: %s" % (self)
        print "Job: %s" % (job)
        # print "  Result: %s" % (job.result)
        # print "  Result: %s" % (len(job.result))
        self.notify("  Finished download catalog...")

        if job.result is not None:
            for entry in job.result:
                beamebooks_id = entry['id']
                message = "<br/>Ebook # %s" % (beamebooks_id)
                self.notify(message)

        self.download_button.setEnabled(True)
        self.conf_button.setEnabled(True)

        payload = job.result
        msg = "Parsing OPDS Catalog complete, found %s ebooks." % (len(
            job.result))
        msg = msg + "<br>Do you want to import the books into the library?"

        # question_dialog
        self.gui.proceed_question(self._add_ebooks,
                                  payload,
                                  job.details,
                                  'OPDS Download Log',
                                  'OPDS parse complete',
                                  msg,
                                  show_copy_button=False)

    def _done_2(self, job):
        print "Done Downloading"
        print "Self: %s" % (self)
        print "Job: %s" % (job)
        # print "  Result: %s" % (job.result)
        # print "  Result: %s" % (len(job.result))
        self.notify("  Finished download book...")

    def _add_ebooks(self, payload):
        print "Done Downloading, Step 2"
        print "Self: %s" % (self)
        # print "Payload: %s" % (payload)
        # Printing the complete payload at once gives [IOError 12] - Out of space (memory, that is...)
        self.notify("  Finished adding books...")
        for entry in payload:
            print "Entry: %s" % entry

            # TODO allow for checkbox list of entries to download
            func = 'arbitrary_n'
            # func = 'arbitrary'
            cpus = self.gui.job_manager.server.pool_size
            print "CPUs: %s" % (cpus)
            args = [
                'calibre_plugins.beam_ebooks_downloader.jobs',
                'do_download_book', (cpus, entry)
            ]
            desc = 'Beam EBooks Downloader'
            job = self.gui.job_manager.run_job(Dispatcher(self._done_2),
                                               func,
                                               args=args,
                                               description=desc)
            print "Job: %s" % (job)