def onFileModelDirectoryLoaded(self, path): lastDir = str(self._state.get_video_path()) qDirLastDir = QDir(lastDir) qDirLastDir.cdUp() if qDirLastDir.path() == path: index = self.fileModel.index(lastDir) proxyIndex = self.proxyFileModel.mapFromSource(index) self.ui.folderView.scrollTo(proxyIndex) self.ui.folderView.setCurrentIndex(proxyIndex)
def onFileModelDirectoryLoaded(self, path): settings = QSettings() lastDir = settings.value('mainwindow/workingDirectory', QDir.homePath()) qDirLastDir = QDir(lastDir) qDirLastDir.cdUp() if qDirLastDir.path() == path: index = self.fileModel.index(lastDir) proxyIndex = self.proxyFileModel.mapFromSource(index) self.ui.folderView.scrollTo(proxyIndex) self.ui.folderView.setCurrentIndex(proxyIndex)
def _appBundlePluginsPath(cls, appDirPath): ''' path to plugin directory of OSX app's bundle (especially when sandboxed, i.e. in a cls-contained bundle w/o shared libraries) If not (platform is OSX and app has PlugIns dir in the bundle), returns None. On other platforms (or when OSX app is not sandboxed) plugins are not usually bundled, but in the shared install directory of the Qt package. Implementation: use Qt since it understands colons (e.g. ':/') for resource paths. (Instead of Python os.path, which is problematic.) Convert string to QDir, move it around, convert back to string of abs path without colons. ''' # appDirPath typically "/Users/<user>/Library/<appName>.app/Contents/MacOS" on OSX. appDir = QDir(appDirPath) if not appDir.cdUp(): logAlert("Could not cdUp from appDir") # assert like ..../Contents if appDir.cd("PlugIns"): # !!! Capital I result = appDir.absolutePath() # assert like ..../Contents/PlugIns else: logAlert("Could not cd to PlugIns") result = None assert result is None or isinstance(result, str) return result
class LayoutBox(QVBoxLayout): def __init__(self): super().__init__() self.profileGroup = QGroupBox("Profile") self.profileLabel = QLabel("none") self.profileSelect = QPushButton("Open...") self.profileSelect.clicked.connect(self.openProfileClick) self.profileSave = QPushButton("Save") self.profileSave.clicked.connect(self.saveProfileClick) self.profileSave.setEnabled(False) self.profileBox = QHBoxLayout() self.profileBox.addWidget(self.profileLabel) self.profileBox.addStretch() self.profileBox.addWidget(self.profileSelect) self.profileBox.addWidget(self.profileSave) self.profileGroup.setLayout(self.profileBox) self.filesBox = FilesBox() self.filesBox.buttonBox.setEnable(False) # self.filesBox.availableTweaks.setEnabled(False) # self.filesBox.selectedTweaks.setEnabled(False) # self.filesBox.availableTweaks.itemClicked.connect(self.availClick) self.filesBox.selectedTweaks.itemClicked.connect(self.selectClick) self.descriptionGroup = QGroupBox("Tweak description") self.descriptionGroup.setMinimumHeight(50) self.description = QLabel("") self.description.setTextInteractionFlags(Qt.TextInteractionFlags(5)) self.descriptionBox = QHBoxLayout() self.descriptionBox.addWidget(self.description) self.descriptionGroup.setLayout(self.descriptionBox) self.addWidget(self.profileGroup) self.addItem(self.filesBox) self.addWidget(self.descriptionGroup) self.profileDir = QDir() self.profilePathDialog = QFileDialog() self.profilePath = "" self.appDir = QDir().current() def openProfileClick(self): #Button Open... click self.profilePath = self.profilePathDialog.getExistingDirectory( self.profileSelect, "Select Firefox profile") # self.profilePath = "~/Library/Application Support/Firefox/Profiles/rpk2uobe.default" logging.debug("Selected directory: \"%s\"", self.profilePath) if self.profilePath != "": self.selectedProfile = QDir(self.profilePath) self.firefoxDir = QDir(self.profilePath) logging.debug("Selected profile qdir: %s", self.selectedProfile.path()) self.firefoxDir.cdUp() self.profilesFile = QFile(self.firefoxDir.path() + "/profiles.ini") logging.debug("Profiles file: %s", self.profilesFile.fileName()) logging.debug("Profiles file exists: %s", self.profilesFile.exists()) logging.debug("Firefox folder: %s", self.firefoxDir.dirName()) # Basic check if parent directory is named 'firefox' and contains a file named 'profiles.ini' #if self.firefoxDir.dirName() == "firefox" and self.profilesFile.exists(): if True: self.profilePath = self.profilePath self.profileLabel.setText(self.profilePath) self.filesBox.buttonBox.setEnable(True) self.filesBox.availableTweaks.setEnabled(True) self.filesBox.selectedTweaks.setEnabled(True) self.profileSave.setEnabled(True) self.profileDir.setPath(self.profilePath) logging.debug("Profile dirs: %s", self.profileDir.entryList()) self.userChrome = QFile( self.profilePath + "/chrome/userChrome.css") self.userFFSettings = QFile( self.profilePath + "/chrome/userFirefox.json") logging.debug("userChrome exists: %s", self.userChrome.exists()) if self.userChrome.exists( ) and not self.userFFSettings.exists(): self.backupChrome = QMessageBox() self.backupChrome.setIcon(QMessageBox.Question) self.backupChrome.setText( "userChrome.css file already exists in this profile. This may be overwritten. Do you want to make a backup?" ) self.backupChrome.setStandardButtons( QMessageBox.StandardButtons(81920)) #yes, no self.backupChrome.exec() logging.debug("Dialog result: %s", self.backupChrome.result()) if self.backupChrome.result() == 16384: #yes logging.debug("Backing up userChrome") self.backupDone = QMessageBox() self.backupFile = QFile( self.profilePath + "/chrome/userChrome.css~") logging.debug("Backup file: %s", self.backupFile.fileName()) logging.debug("Backup exists: %s", self.backupFile.exists()) if self.backupFile.exists(): logging.debug("Backup already exists") self.backupDone.setIcon(QMessageBox.Warning) self.backupDone.setText( "Backup already exists. The file was NOT overwritten and new backup not made." ) else: if self.userChrome.copy(self.profilePath + "/chrome/userChrome.css~"): self.backupDone.setIcon( QMessageBox.Information) self.backupDone.setText( "Backed up to 'userChrome.css~'") else: self.backupDone.setIcon(QMessageBox.Critical) self.backupDone.setText("Backing up failed.") self.backupDone.exec() elif self.backupChrome.result() == 65536: #no logging.debug("Not backing up userChome") # Load existing settings try: with open(self.profilePath + "/chrome/userFirefox.json" ) as uFP: savedTweaks = json.load(uFP) logging.debug("Loaded json settings: %s", savedTweaks) for loadedTweak in savedTweaks: logging.debug( "Loaded tweak. check: %i, category: %s, name: %s", loadedTweak["Enabled"], loadedTweak["Category"], loadedTweak["Name"]) tweakCat = loadedTweak["Category"] tweakName = loadedTweak["Name"] self.filesBox.tweaksAdded[ tweakCat + tweakName] = QTreeWidgetItem( self.filesBox.selectedTweaks) self.filesBox.tweaksAdded[tweakCat + tweakName].setCheckState( 0, loadedTweak["Enabled"]) self.filesBox.tweaksAdded[tweakCat + tweakName].setText( 1, tweakCat) self.filesBox.tweaksAdded[tweakCat + tweakName].setText( 2, tweakName) except FileNotFoundError: pass self.filesBox.resizeColumns() else: self.noProfile = QMessageBox() self.noProfile.setIcon(QMessageBox.Warning) self.noProfile.setText( "The selected directory does not appear to be a valid Firefox profile. Profiles are usually located at '~/.mozilla/firefox/xxxx' and is most likely a hidden directory." ) self.noProfile.exec() def availClick(self): if self.filesBox.availableTweaks.currentItem().parent() is not None: tweakName = self.filesBox.availableTweaks.currentItem().text(0) tweakCat = self.filesBox.availableTweaks.currentItem().parent( ).text(0) self.showDesc(tweakCat, tweakName) def selectClick(self): try: tweakName = self.filesBox.selectedTweaks.currentItem().text(2) tweakCat = self.filesBox.selectedTweaks.currentItem().text(1) self.showDesc(tweakCat, tweakName) except: pass def showDesc(self, category, name): logging.debug("Showing description for %s/%s: ", category, name) currentDir = QDir().current().path() tweakPath = currentDir + "/" + category + "/" + name logging.debug("Loading %s", tweakPath) tweakDesc = "" descLines = 0 with open(tweakPath) as tweakContent: for line in tweakContent: if (re.search("^(\/\* ?)|( \* ?)|( \*\/)", line) is not None): if len(line) > 3: trimmedLine = line[3:] tweakDesc += trimmedLine descLines += 1 else: break logging.debug("Description: %s", tweakDesc) self.description.setText(tweakDesc) logging.debug("Desc lines: %i", descLines) def saveProfileClick(self): logging.debug("Saving profile") self.saveQuestion = QMessageBox() self.saveQuestion.setIcon(QMessageBox.Question) self.saveQuestion.setStandardButtons( QMessageBox.StandardButtons(81920)) #yes, no self.saveQuestion.setText( "Do you want to apply selected tweaks and overwrite userChrome?") self.saveQuestion.exec() self.saveCSS = "" if self.saveQuestion.result() == 16384: #yes logging.debug("Saving userChrome") selTweaksNumber = self.filesBox.selectedTweaks.topLevelItemCount() savingTweaks = [] for i in range(selTweaksNumber): currentSavingTweak = self.filesBox.selectedTweaks.topLevelItem( i) currentSavingData = {} currentSavingData["Enabled"] = currentSavingTweak.checkState(0) currentSavingData["Category"] = currentSavingTweak.text(1) currentSavingData["Name"] = currentSavingTweak.text(2) logging.debug("Tweak cat %s, name %s, selected %s", currentSavingData["Category"], currentSavingData["Name"], currentSavingData["Enabled"]) savingTweaks.append(currentSavingData) if currentSavingData["Enabled"] == 2: with open(self.appDir.path() + "/" + currentSavingData["Category"] + "/" + currentSavingData["Name"]) as twkFile: self.saveCSS += twkFile.read() + "\n" logging.debug("Selected tweaks: %s", savingTweaks) with open(self.profilePath + "/chrome/userFirefox.json", "w") as fp: json.dump(savingTweaks, fp) logging.debug("userChrome.css: %s", self.saveCSS) with open(self.profilePath + "/chrome/userChrome.css", "w") as fp: fp.write(self.saveCSS) elif self.saveQuestion.result() == 65536: #no logging.debug("Not saving userChrome.")