(self, addressAtCurrentRow)
| 3705 | currentRow, 0).setIcon(avatarize(addressAtCurrentRow)) |
| 3706 | |
| 3707 | def setAvatar(self, addressAtCurrentRow): |
| 3708 | if not os.path.exists(state.appdata + 'avatars/'): |
| 3709 | os.makedirs(state.appdata + 'avatars/') |
| 3710 | hash = hashlib.md5(addBMIfNotPresent(addressAtCurrentRow)).hexdigest() |
| 3711 | extensions = ['PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', 'PGM', 'PPM', 'TIFF', 'XBM', 'XPM', 'TGA'] |
| 3712 | # http://pyqt.sourceforge.net/Docs/PyQt4/qimagereader.html#supportedImageFormats |
| 3713 | names = {'BMP':'Windows Bitmap', 'GIF':'Graphic Interchange Format', 'JPG':'Joint Photographic Experts Group', 'JPEG':'Joint Photographic Experts Group', 'MNG':'Multiple-image Network Graphics', 'PNG':'Portable Network Graphics', 'PBM':'Portable Bitmap', 'PGM':'Portable Graymap', 'PPM':'Portable Pixmap', 'TIFF':'Tagged Image File Format', 'XBM':'X11 Bitmap', 'XPM':'X11 Pixmap', 'SVG':'Scalable Vector Graphics', 'TGA':'Targa Image Format'} |
| 3714 | filters = [] |
| 3715 | all_images_filter = [] |
| 3716 | current_files = [] |
| 3717 | for ext in extensions: |
| 3718 | filters += [ names[ext] + ' (*.' + ext.lower() + ')' ] |
| 3719 | all_images_filter += [ '*.' + ext.lower() ] |
| 3720 | upper = state.appdata + 'avatars/' + hash + '.' + ext.upper() |
| 3721 | lower = state.appdata + 'avatars/' + hash + '.' + ext.lower() |
| 3722 | if os.path.isfile(lower): |
| 3723 | current_files += [lower] |
| 3724 | elif os.path.isfile(upper): |
| 3725 | current_files += [upper] |
| 3726 | filters[0:0] = ['Image files (' + ' '.join(all_images_filter) + ')'] |
| 3727 | filters[1:1] = ['All files (*.*)'] |
| 3728 | sourcefile = QtGui.QFileDialog.getOpenFileName( |
| 3729 | self, _translate("MainWindow", "Set avatar..."), |
| 3730 | filter = ';;'.join(filters) |
| 3731 | ) |
| 3732 | # determine the correct filename (note that avatars don't use the suffix) |
| 3733 | destination = state.appdata + 'avatars/' + hash + '.' + sourcefile.split('.')[-1] |
| 3734 | exists = QtCore.QFile.exists(destination) |
| 3735 | if sourcefile == '': |
| 3736 | # ask for removal of avatar |
| 3737 | if exists | (len(current_files)>0): |
| 3738 | displayMsg = _translate("MainWindow", "Do you really want to remove this avatar?") |
| 3739 | overwrite = QtGui.QMessageBox.question( |
| 3740 | self, 'Message', displayMsg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) |
| 3741 | else: |
| 3742 | overwrite = QtGui.QMessageBox.No |
| 3743 | else: |
| 3744 | # ask whether to overwrite old avatar |
| 3745 | if exists | (len(current_files)>0): |
| 3746 | displayMsg = _translate("MainWindow", "You have already set an avatar for this address. Do you really want to overwrite it?") |
| 3747 | overwrite = QtGui.QMessageBox.question( |
| 3748 | self, 'Message', displayMsg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) |
| 3749 | else: |
| 3750 | overwrite = QtGui.QMessageBox.No |
| 3751 | |
| 3752 | # copy the image file to the appdata folder |
| 3753 | if (not exists) | (overwrite == QtGui.QMessageBox.Yes): |
| 3754 | if overwrite == QtGui.QMessageBox.Yes: |
| 3755 | for file in current_files: |
| 3756 | QtCore.QFile.remove(file) |
| 3757 | QtCore.QFile.remove(destination) |
| 3758 | # copy it |
| 3759 | if sourcefile != '': |
| 3760 | copied = QtCore.QFile.copy(sourcefile, destination) |
| 3761 | if not copied: |
| 3762 | logger.error('couldn\'t copy :(') |
| 3763 | # set the icon |
| 3764 | self.rerenderTabTreeMessages() |
no test coverage detected