From 94dcc8a678d2203b07b0c1b36e85d72f8a6176a1 Mon Sep 17 00:00:00 2001 From: Cristiano Colangelo Date: Sat, 24 Oct 2020 15:16:18 +0200 Subject: Add double-click download/open of files --- gui.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/gui.py b/gui.py index eb5fe86..cab60aa 100644 --- a/gui.py +++ b/gui.py @@ -2,12 +2,15 @@ # This document *and this document only* uses the Qt naming convention instead # of PEP because the PyQt5 bindings do not have pythonic names # - +import os +import platform +import subprocess import sys import json import enum import html import logging +import tempfile from PyQt5.QtGui import QFont from PyQt5.Qt import QStyle @@ -151,8 +154,19 @@ class MoodleTreeView(QTreeWidget): def onItemDoubleClicked(self, item, col): log.debug(f"double clicked on item with type {str(item.metadata.type)}") if item.metadata.type == MoodleItem.Type.FILE: - # TODO: download in a temp folder and open - pass + log.debug(f"started download from {item.metadata.url}") + filepath = tempfile.gettempdir()+"/"+item.metadata.title + self.worker.apihelper.get_file(item.metadata.url, filepath) + + # TODO: Maybe extract in util function + if platform.system() == 'Darwin': # macOS + subprocess.call(('open', )) + elif platform.system() == 'Windows': # Windows + os.startfile(filepath) + else: # linux variants + subprocess.call(('xdg-open', filepath)) + + @pyqtSlot(MoodleItem.Type, object) def onWorkerLoadedItem(self, type, item): -- cgit v1.2.1