aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gui.py94
-rw-r--r--muddle.ini.example3
-rw-r--r--muddle.py8
-rw-r--r--muddle.ui43
4 files changed, 122 insertions, 26 deletions
diff --git a/gui.py b/gui.py
index 2439f72..c4bb8c7 100644
--- a/gui.py
+++ b/gui.py
@@ -48,6 +48,7 @@ from PyQt5.QtWidgets import (
QPlainTextEdit,
QFileSystemModel,
QFileDialog,
+ QCheckBox,
)
import moodle
@@ -114,10 +115,10 @@ class MoodleItem(QStandardItem):
class MoodleFetcher(QThread):
loadedItem = pyqtSignal(MoodleItem.Type, object)
- def __init__(self, parent, instance_url, token):
+ def __init__(self, parent, instanceUrl, token):
super().__init__()
- self.api = moodle.RestApi(instance_url, token)
+ self.api = moodle.RestApi(instanceUrl, token)
self.apihelper = moodle.ApiHelper(self.api)
def run(self):
@@ -177,11 +178,11 @@ class MoodleTreeModel(QStandardItemModel):
self.worker = None
@pyqtSlot(str, str)
- def refresh(self, instance_url, token):
+ def refresh(self, instanceUrl, token):
if not self.worker or self.worker.isFinished():
self.setRowCount(0) # instead of clear(), because clear() removes the headers
- self.worker = MoodleFetcher(self, instance_url, token)
+ self.worker = MoodleFetcher(self, instanceUrl, token)
self.worker.loadedItem.connect(self.onWorkerLoadedItem)
self.worker.finished.connect(self.onWorkerDone)
self.worker.start()
@@ -271,18 +272,51 @@ class QLogHandler(QObject, logging.Handler):
class MuddleWindow(QMainWindow):
- def __init__(self, instance_url, token):
+ def __init__(self, config):
super(MuddleWindow, self).__init__()
uic.loadUi("muddle.ui", self)
self.setCentralWidget(self.findChild(QTabWidget, "Muddle"))
- # setup logging
+ self.instanceUrl = config["server"]["url"] if config.has_option("server", "url") else None
+ self.token = config["server"]["token"] if config.has_option("server", "token") else None
+
+ # config tab
+ ## TODO: when any of the settings change, update the values (but not in the config, yet)
+
+ instanceUrlEdit = self.findChild(QLineEdit, "instanceUrlEdit")
+ if self.instanceUrl:
+ instanceUrlEdit.setText(self.instanceUrl)
+
+ tokenEdit = self.findChild(QLineEdit, "tokenEdit")
+ if self.token:
+ tokenEdit.setText(self.token)
+
+ requestTokenBtn = self.findChild(QPushButton, "requestTokenBtn")
+ requestTokenBtn.clicked.connect(self.onRequestTokenBtnClicked)
+
+ tokenEdit.textEdited.connect(lambda text: requestTokenBtn.setEnabled(not bool(text)))
+
+ alwaysStartGuiCheckBox = self.findChild(QCheckBox, "alwaysStartGuiCheckBox")
+ if config.has_option("muddle", "always_run_gui"):
+ alwaysStartGuiCheckBox.setChecked(config.getboolean("muddle", "always_run_gui"))
+
+ configEdit = self.findChild(QLineEdit, "configEdit")
+ configEdit.setText(config["runtime_data"]["config_path"])
+
+ defaultDownloadPathEdit = self.findChild(QLineEdit, "defaultDownloadPathEdit")
+ if config.has_option("muddle", "default_download_dir"):
+ defaultDownloadPathEdit.setText(config["muddle"]["default_download_dir"])
+
+
+ # log tab
+ ## setup logging
self.loghandler = QLogHandler(self)
self.loghandler.setFormatter(logging.Formatter("%(name)s - %(levelname)s - %(message)s"))
self.loghandler.newLogMessage.connect(self.onNewLogMessage)
logging.getLogger("muddle").addHandler(self.loghandler)
- # set up proxymodel for moodle treeview
+ # moodle tab
+ ## set up proxymodel for moodle treeview
self.moodleTreeModel = MoodleTreeModel()
self.moodleTreeModel.dataChanged.connect(self.onMoodleTreeModelDataChanged)
@@ -295,20 +329,33 @@ class MuddleWindow(QMainWindow):
moodleTreeView.setModel(self.filterModel)
moodleTreeView.setSortingEnabled(True)
moodleTreeView.sortByColumn(0, Qt.AscendingOrder)
- # TODO: change with minimumSize (?)
+ ## TODO: change with minimumSize (?)
moodleTreeView.header().setSectionResizeMode(0, QHeaderView.ResizeToContents)
moodleTreeView.doubleClicked.connect(self.onMoodleTreeViewDoubleClicked)
- # refresh moodle treeview
+ ## refresh moodle treeview
refreshBtn = self.findChild(QToolButton, "refreshBtn")
- refreshBtn.clicked.connect(lambda b: self.moodleTreeModel.refresh(instance_url, token))
+ refreshBtn.clicked.connect(self.onRefreshBtnClicked)
+
+ if not self.instanceUrl:
+ refreshBtn.setEnabled(False)
+ log.warning("no server url configured!")
+
+ if not self.token:
+ refreshBtn.setEnabled(False)
+ log.warning("no server token configured!")
+
- # searchbar
+ ## searchbar
searchBar = self.findChild(QLineEdit, "searchBar")
searchBar.textChanged.connect(self.onSearchBarTextChanged)
searchBar.textEdited.connect(self.onSearchBarTextChanged)
- # local filesystem view
+ ## select path
+ selectPathBtn = self.findChild(QToolButton, "selectPathBtn")
+ selectPathBtn.clicked.connect(self.onSelectPathBtnClicked)
+
+ # local filesystem tab
self.downloadPath = QDir.homePath()
self.fileSystemModel = QFileSystemModel()
@@ -323,12 +370,15 @@ class MuddleWindow(QMainWindow):
downloadPathEdit.setText(self.downloadPath)
downloadPathEdit.editingFinished.connect(self.onDownloadPathEditEditingFinished)
- # select path
- selectPathBtn = self.findChild(QToolButton, "selectPathBtn")
- selectPathBtn.clicked.connect(self.onSelectPathBtnClicked)
-
self.show()
+ @pyqtSlot()
+ def onRequestTokenBtnClicked(self):
+ # TODO: open login dialog
+ # TODO: test and maybe check if there is already a token
+ # req = moodle.request_token(self.instance_url, user, password)
+ pass
+
@pyqtSlot(str)
def onSearchBarTextChanged(self, text):
moodleTreeView = self.findChild(QTreeView, "moodleTree")
@@ -367,6 +417,14 @@ class MuddleWindow(QMainWindow):
self.updateDownloadPath(path)
@pyqtSlot()
+ def onRefreshBtnClicked(self):
+ if self.instanceUrl and self.token:
+ self.moodleTreeModel.refresh(self.instanceUrl, self.token)
+ else:
+ # TODO: implement error dialog
+ pass
+
+ @pyqtSlot()
def updateDownloadPath(self, newpath):
if not self.fileSystemModel.index(newpath).isValid():
return False
@@ -411,7 +469,7 @@ class MuddleWindow(QMainWindow):
item.child(i).setCheckState(item.checkState())
-def start(instance_url, token):
+def start(config):
app = QApplication(sys.argv)
- ex = MuddleWindow(instance_url, token)
+ ex = MuddleWindow(config)
sys.exit(app.exec_())
diff --git a/muddle.ini.example b/muddle.ini.example
index 6a1c2f0..cabcd05 100644
--- a/muddle.ini.example
+++ b/muddle.ini.example
@@ -1,3 +1,6 @@
[server]
url = https://moodle.rj.ost.ch
token = <your token here>
+
+[muddle]
+always_run_gui = false
diff --git a/muddle.py b/muddle.py
index b1dc7f1..c7adbab 100644
--- a/muddle.py
+++ b/muddle.py
@@ -117,6 +117,10 @@ log.debug(f"reading config file {config_file}")
config = configparser.ConfigParser()
config.read(config_file)
+# runtime data that should NOT be written
+config.add_section("runtime_data")
+config["runtime_data"]["config_path"] = str(config_file)
+
# S T A R T
@@ -129,5 +133,5 @@ are welcome to redistribute it under certain conditions; see LICENSE.txt for
details. Project repository: https://github.com/NaoPross/Muddle
""")
-if args.gui:
- gui.start(config["server"]["url"], config["server"]["token"])
+if args.gui or config.getboolean("muddle", "always_run_gui"):
+ gui.start(config)
diff --git a/muddle.ui b/muddle.ui
index 02dbae5..ccd98bc 100644
--- a/muddle.ui
+++ b/muddle.ui
@@ -92,6 +92,12 @@
<property name="enabled">
<bool>true</bool>
</property>
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
<property name="text">
<string>Select</string>
</property>
@@ -153,7 +159,11 @@
</widget>
</item>
<item row="0" column="1">
- <widget class="QLineEdit" name="instanceUrlEdit"/>
+ <widget class="QLineEdit" name="instanceUrlEdit">
+ <property name="clearButtonEnabled">
+ <bool>true</bool>
+ </property>
+ </widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="tokenLabel">
@@ -163,7 +173,21 @@
</widget>
</item>
<item row="1" column="1">
- <widget class="QLineEdit" name="tokenEdit"/>
+ <widget class="QLineEdit" name="tokenEdit">
+ <property name="clearButtonEnabled">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item row="2" column="1">
+ <widget class="QPushButton" name="requestTokenBtn">
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="text">
+ <string>Request Token</string>
+ </property>
+ </widget>
</item>
</layout>
</widget>
@@ -195,16 +219,23 @@
</property>
</widget>
</item>
+ <item row="1" column="1">
+ <widget class="QLineEdit" name="defaltDownloadPathEdit">
+ <property name="placeholderText">
+ <string>Not set</string>
+ </property>
+ <property name="clearButtonEnabled">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
<item row="2" column="1">
- <widget class="QCheckBox" name="checkBox">
+ <widget class="QCheckBox" name="alwaysStartGuiCheckBox">
<property name="text">
<string>Always start GUI</string>
</property>
</widget>
</item>
- <item row="1" column="1">
- <widget class="QLineEdit" name="defaltDownloadPathEdit"/>
- </item>
</layout>
</widget>
</item>