From f4b6dee2a6fb92188bc556e5527318d1ad5200ab Mon Sep 17 00:00:00 2001 From: Phred Date: Fri, 12 Dec 2014 13:25:42 -0600 Subject: [PATCH] DRY'd out code Removed commented out code --- src/main_linux.py | 289 +++++++++++++++++----------------------------- 1 file changed, 103 insertions(+), 186 deletions(-) diff --git a/src/main_linux.py b/src/main_linux.py index f21ec8f..025870c 100755 --- a/src/main_linux.py +++ b/src/main_linux.py @@ -34,6 +34,60 @@ """ +CONTROLS = { + 'btn1': { + 'geometry': [34, 109, 235, 26], + 'trigger': 'backup_all_without_system' + }, + 'btn2': { + 'geometry': [34, 138, 235, 26], + 'trigger': 'backup_all_with_system' + }, + 'btn3': { + 'geometry': [34, 167, 235, 26], + 'trigger': 'backup_app_data_and_device_data' + }, + 'btn4': { + 'geometry': [34, 196, 235, 26], + 'trigger': 'backup_apps' + }, + 'btn5': { + 'geometry': [34, 225, 235, 26], + 'trigger': 'backup_storage' + }, + 'restore': { + 'geometry': [34, 270, 235, 26], + 'trigger': 'get_backup_location' + }, + 'sms': { + 'geometry': [34, 315, 235, 26], + 'trigger': 'install_sms_app' + }, + 'browse': { + 'geometry': [216, 372, 69, 26], + 'trigger': 'open_browse_window' + }, + 'wirelessAdb': { + 'geometry': [34, 407, 235, 26], + 'trigger': 'connect_wireless_adb' + }, + 'btc': { + 'geometry': [34, 474, 235, 26], + 'trigger': 'donate_btc' + } +} + + +def password_required(wrapped): + def wrapper(self, *args): + if self.password_popup(): + self.progress_popup() + + wrapped(self, *args) + + return wrapper + + class Window(QtGui.QWidget): su_command = '' @@ -42,10 +96,6 @@ def __init__(self): Window.su_command = Window.get_su_gui_command() - self.package_to_backup = self.use_system_adb_binary = self.background = self.btn1 = self.btn2 = self.btn3 = \ - self.btn4 = self.btn5 = self.restore = self.sms = self.browse = self.path_label = self.wireless_adb = \ - self.btc = self.system_adb = None - self.init_ui() def center(self): @@ -80,7 +130,10 @@ def password_popup(self): popup_msg = "This program only works properly if you've set a 'Desktop backup password' in Developer Options." \ " Have you done that?" reply = QtGui.QMessageBox.question(self, 'Warning!', - popup_msg, QtGui.QMessageBox.Yes) + popup_msg, QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, + QtGui.QMessageBox.Yes) + + return reply == QtGui.QMessageBox.Yes def progress_popup(self): popup_msg = "Ready to start the operation. You'll need to monitor the rest of the process on your device. " \ @@ -101,136 +154,49 @@ def sms_popup(self): reply = QtGui.QMessageBox.question(self, 'SMS Backup / Restore', popup_msg, QtGui.QMessageBox.Ok) - def btn1_enter(self, event): - self.btn1.setPixmap(QtGui.QPixmap('img/btn1_down.png')) - - def btn1_leave(self, event): - self.btn1.setPixmap(QtGui.QPixmap('img/btn1_up.png')) - - def btn2_enter(self, event): - self.btn2.setPixmap(QtGui.QPixmap('img/btn2_down.png')) - - def btn2_leave(self, event): - self.btn2.setPixmap(QtGui.QPixmap('img/btn2_up.png')) - - def btn3_enter(self, event): - self.btn3.setPixmap(QtGui.QPixmap('img/btn3_down.png')) - - def btn3_leave(self, event): - self.btn3.setPixmap(QtGui.QPixmap('img/btn3_up.png')) - - def btn4_enter(self, event): - self.btn4.setPixmap(QtGui.QPixmap('img/btn4_down.png')) - - def btn4_leave(self, event): - self.btn4.setPixmap(QtGui.QPixmap('img/btn4_up.png')) - - def btn5_enter(self, event): - self.btn5.setPixmap(QtGui.QPixmap('img/btn5_down.png')) - - def btn5_leave(self, event): - self.btn5.setPixmap(QtGui.QPixmap('img/btn5_up.png')) - - def restore_enter(self, event): - self.restore.setPixmap(QtGui.QPixmap('img/restore_down.png')) - - def restore_leave(self, event): - self.restore.setPixmap(QtGui.QPixmap('img/restore_up.png')) - - def browse_enter(self, event): - self.browse.setPixmap(QtGui.QPixmap('img/browse_down.png')) - - def browse_leave(self, event): - self.browse.setPixmap(QtGui.QPixmap('img/browse_up.png')) - - def sms_enter(self, event): - self.sms.setPixmap(QtGui.QPixmap('img/sms_down.png')) - - def sms_leave(self, event): - self.sms.setPixmap(QtGui.QPixmap('img/sms_up.png')) - - def btc_enter(self, event): - self.btc.setPixmap(QtGui.QPixmap('img/btc_down.png')) - - def btc_leave(self, event): - self.btc.setPixmap(QtGui.QPixmap('img/btc_up.png')) - - def wireless_adb_enter(self, event): - self.wireless_adb.setPixmap(QtGui.QPixmap('img/wirelessAdb_down.png')) + def run_command(self, command): + if not self.use_system_adb_binary: + command = '{0} \'./{1}\''.format(Window.su_command, command) - def wireless_adb_leave(self, event): - self.wireless_adb.setPixmap(QtGui.QPixmap('img/wirelessAdb_up.png')) + os.system(command) + @password_required def backup_all_without_system(self, event): - self.password_popup() - self.progress_popup() - - if not self.use_system_adb_binary: - os.system(Window.su_command + ' "./adb backup -apk -shared -all -nosystem -f "' + Path + '""') - else: - os.system('adb backup -apk -shared -all -nosystem -f "' + Path + '"') + self.run_command('adb backup -apk -shared -all -nosystem -f "{0}"'.format(Path)) + @password_required def backup_all_with_system(self, event): - self.password_popup() - self.progress_popup() - - if not self.use_system_adb_binary: - os.system(Window.su_command + ' "./adb backup -apk -shared -all -system -f "' + Path + '""') - else: - os.system('adb backup -apk -shared -all -system -f "' + Path + '"') + self.run_command('adb backup -apk -shared -all -system -f "{0}"'.format(Path)) + @password_required def backup_app_data_and_device_data(self, event): - self.password_popup() - self.progress_popup() - - if not self.use_system_adb_binary: - os.system(Window.su_command + ' "./adb backup -all -f "' + Path + '""') - else: - os.system('adb backup -all -f "' + Path + '"') + self.run_command('adb backup -all -f "{0}"'.format(Path)) + @password_required def backup_apps(self, event): - self.password_popup() - self.progress_popup() - - if not self.use_system_adb_binary: - os.system(Window.su_command + ' "./adb backup -apk -all -noshared -nosystem -f "' + Path + '""') - else: - os.system('adb backup -apk -noshared -nosystem -f "' + Path + '"') + self.run_command('adb backup -apk -noshared -nosystem -f "{0}"'.format(Path)) + @password_required def backup_storage(self, event): - self.password_popup() - self.progress_popup() + self.run_command('adb backup -noapk -shared -nosystem -f "{0}"'.format(Path)) - if not self.use_system_adb_binary: - os.system(Window.su_command + ' "./adb backup -noapk -shared -nosystem -f "' + Path + '""') - else: - os.system('adb backup -noapk -shared -nosystem -f "' + Path + '"') + @password_required + def _get_backup_location(self, backup_location): + self.run_command("adb restore {0}".format(backup_location)) def get_backup_location(self, event): backup_location = QtGui.QFileDialog.getOpenFileName(self, 'Open backup file', os.getcwd()) - self.password_popup() - self.progress_popup() - if not self.use_system_adb_binary: - os.system(Window.su_command + ' "./adb restore ' + str(backup_location) + '"') - else: - os.system("adb restore " + str(backup_location)) + self._get_backup_location(backup_location) def install_sms_app(self, event): self.sms_popup() - if not self.use_system_adb_binary: - os.system(Window.su_command + ' "./adb install smsBackupPlus/sms_backup_plus.apk"') - os.system(Window.su_command + ' "./adb shell am start -n ' - 'com.zegoggles.smssync/com.zegoggles.smssync.SmsSync"') - else: - os.system("adb install smsBackupPlus/sms_backup_plus.apk") - os.system("adb shell am start -n com.zegoggles.smssync/com.zegoggles.smssync.SmsSync") + + self.run_command("adb install smsBackupPlus/sms_backup_plus.apk") + self.run_command("adb shell am start -n com.zegoggles.smssync/com.zegoggles.smssync.SmsSync") def open_browse_window(self, event): # print "Browse window placeholder" - #dialog = QtGui.QFileDialog(self, 'Select: Shared Music Directory', os.getcwd()) - #dialog.setFileMode(QtGui.QFileDialog.DirectoryOnly) - directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select backup directory') Path = directory + "/backup.ab" @@ -239,10 +205,12 @@ def open_browse_window(self, event): def adb_binary_change(self, event): if not self.use_system_adb_binary: self.use_system_adb_binary = True - self.system_adb.setPixmap(QtGui.QPixmap('img/adb_checked.png')) + direction = 'checked' else: self.use_system_adb_binary = False - self.system_adb.setPixmap(QtGui.QPixmap('img/adb_unchecked.png')) + direction = 'unchecked' + + self.system_adb.setPixmap(QtGui.QPixmap('img/adb_{0}.png'.format(direction))) @staticmethod def donate_btc(event): @@ -266,6 +234,23 @@ def get_su_gui_command(): else: return 'gksudo' + def wrap_event(self, label, direction): + def wrapped(event): + getattr(self, label).setPixmap( + QtGui.QPixmap('img/{0}_{1}.png'.format(label, direction))) + + return wrapped + + def create_button(self, label, geometry, method): + button = QtGui.QLabel(self) + button.setPixmap(QtGui.QPixmap('img/{0}_up.png'.format(label))) + button.setGeometry(*geometry) + button.enterEvent = self.wrap_event(label, 'down') + button.leaveEvent = self.wrap_event(label, 'up') + button.mouseReleaseEvent = getattr(self, method) + + setattr(self, label, button) + def init_ui(self): self.show_disclaimer() @@ -284,81 +269,10 @@ def init_ui(self): self.background.setGeometry(0, 0, 300, 500) # self.show() - self.btn1 = QtGui.QLabel(self) - self.btn1.setPixmap(QtGui.QPixmap('img/btn1_up.png')) - self.btn1.setGeometry(34, 109, 235, 26) - self.btn1.enterEvent = self.btn1_enter - self.btn1.leaveEvent = self.btn1_leave - self.btn1.mouseReleaseEvent = self.backup_all_without_system - - self.btn2 = QtGui.QLabel(self) - self.btn2.setPixmap(QtGui.QPixmap('img/btn2_up.png')) - self.btn2.setGeometry(34, 138, 235, 26) - self.btn2.enterEvent = self.btn2_enter - self.btn2.leaveEvent = self.btn2_leave - self.btn2.mouseReleaseEvent = self.backup_all_with_system - - self.btn3 = QtGui.QLabel(self) - self.btn3.setPixmap(QtGui.QPixmap('img/btn3_up.png')) - self.btn3.setGeometry(34, 167, 235, 26) - self.btn3.enterEvent = self.btn3_enter - self.btn3.leaveEvent = self.btn3_leave - self.btn3.mouseReleaseEvent = self.backup_app_data_and_device_data - - self.btn4 = QtGui.QLabel(self) - self.btn4.setPixmap(QtGui.QPixmap('img/btn4_up.png')) - self.btn4.setGeometry(34, 196, 235, 26) - self.btn4.enterEvent = self.btn4_enter - self.btn4.leaveEvent = self.btn4_leave - self.btn4.mouseReleaseEvent = self.backup_apps - - self.btn5 = QtGui.QLabel(self) - self.btn5.setPixmap(QtGui.QPixmap('img/btn5_up.png')) - self.btn5.setGeometry(34, 225, 235, 26) - self.btn5.enterEvent = self.btn5_enter - self.btn5.leaveEvent = self.btn5_leave - self.btn5.mouseReleaseEvent = self.backup_storage - - self.restore = QtGui.QLabel(self) - self.restore.setPixmap(QtGui.QPixmap('img/restore_up.png')) - self.restore.setGeometry(34, 270, 235, 26) - self.restore.enterEvent = self.restore_enter - self.restore.leaveEvent = self.restore_leave - self.restore.mouseReleaseEvent = self.get_backup_location - - self.sms = QtGui.QLabel(self) - self.sms.setPixmap(QtGui.QPixmap('img/sms_up.png')) - self.sms.setGeometry(34, 315, 235, 26) - self.sms.enterEvent = self.sms_enter - self.sms.leaveEvent = self.sms_leave - self.sms.mouseReleaseEvent = self.install_sms_app - - self.browse = QtGui.QLabel(self) - self.browse.setPixmap(QtGui.QPixmap('img/browse_up.png')) - self.browse.setGeometry(216, 372, 69, 26) - self.browse.enterEvent = self.browse_enter - self.browse.leaveEvent = self.browse_leave - self.browse.mouseReleaseEvent = self.open_browse_window - self.path_label = QtGui.QLabel(self) self.path_label.setGeometry(22, 381, 189, 14) self.path_label.setText(os.getcwd() + '/Backups/') - self.wireless_adb = QtGui.QLabel(self) - self.wireless_adb.setPixmap(QtGui.QPixmap('img/wirelessAdb_up.png')) - self.wireless_adb.setGeometry(34, 407, 235, 26) - self.wireless_adb.enterEvent = self.wireless_adb_enter - self.wireless_adb.leaveEvent = self.wireless_adb_leave - self.wireless_adb.mouseReleaseEvent = self.connect_wireless_adb - - self.btc = QtGui.QLabel(self) - self.btc.setPixmap(QtGui.QPixmap('img/btc_up.png')) - self.btc.setGeometry(34, 474, 235, 26) - self.btc.enterEvent = self.btc_enter - self.btc.leaveEvent = self.btc_leave - self.btc.mouseReleaseEvent = self.donate_btc - self.btc.show() - self.system_adb = QtGui.QLabel(self) self.system_adb.setPixmap(QtGui.QPixmap('img/adb_unchecked.png')) self.system_adb.setGeometry(10, 450, 272, 29) @@ -366,6 +280,9 @@ def init_ui(self): # self.system_adb.leaveEvent = self.browse_leave self.system_adb.mouseReleaseEvent = self.adb_binary_change + for label, settings in CONTROLS.iteritems(): + self.create_button(label, settings['geometry'], settings['trigger']) + def main(): app = QtGui.QApplication(sys.argv)