diff options
Diffstat (limited to 'dozentenmodul/src/main/java')
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferHelper.java | 101 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/QFileChooser.java (renamed from dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/FileChooser.java) | 13 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java | 74 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java | 4 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/permissions/ImagePerms.java | 14 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftError.java | 16 |
6 files changed, 171 insertions, 51 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferHelper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferHelper.java new file mode 100644 index 00000000..172354a9 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferHelper.java @@ -0,0 +1,101 @@ +package org.openslx.dozmod.filetransfer; + +import java.awt.Frame; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import javax.swing.JFileChooser; + +import org.apache.log4j.Logger; +import org.apache.thrift.TException; +import org.openslx.bwlp.thrift.iface.TransferInformation; +import org.openslx.dozmod.Config; +import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.gui.MainWindow; +import org.openslx.dozmod.gui.helper.MessageType; +import org.openslx.dozmod.gui.helper.QFileChooser; +import org.openslx.dozmod.thrift.Session; +import org.openslx.dozmod.thrift.ThriftError; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.util.QuickTimer; +import org.openslx.util.QuickTimer.Task; + +public class TransferHelper { + + private static final Logger LOGGER = Logger.getLogger(TransferHelper.class); + + public static void initDownload(final Frame frame, final String imageVersionId, final String imageName, + final String virtualizerId, final long imageSize) { + QFileChooser fc = new QFileChooser(Config.getDownloadPath(), true); + fc.setDialogTitle("Bitte wählen Sie einen Speicherort"); + int action = fc.showSaveDialog(frame); + final File file = fc.getSelectedFile(); + if (action != JFileChooser.APPROVE_OPTION || file == null) + return; + + QuickTimer.scheduleOnce(new Task() { + @Override + public void fire() { + final TransferInformation transInf; + try { + transInf = ThriftManager.getSatClient() + .requestDownload(Session.getSatelliteToken(), imageVersionId); + } catch (TException e) { + ThriftError.showMessage(frame, LOGGER, e, "Die Download-Anfrage ist gescheitert"); + return; + } + + File df = null; + try { + df = new File(file.getAbsolutePath(), generateFilename(imageName, virtualizerId)); + df.createNewFile(); + file.getAbsoluteFile().mkdirs(); + } catch (IOException e) { + LOGGER.warn("Cannot prepare download destination", e); + } + final File destFile = df; + // TODO: Check if file already exists + final DownloadTask dlTask; + try { + dlTask = new DownloadTask(Session.getSatelliteAddress(), transInf.getPlainPort(), transInf + .getToken(), destFile, imageSize, null); + } catch (final FileNotFoundException e) { + Gui.asyncExec(new Runnable() { + @Override + public void run() { + Gui.showMessageBox(frame, + "Konnte Download nicht vorbereiten: Der gewählte Zielort ist nicht beschreibbar", + MessageType.ERROR, LOGGER, e); + } + }); + return; + } + new Thread(dlTask).start(); + + Gui.asyncExec(new Runnable() { + @Override + public void run() { + Config.setDownloadPath(file.getAbsolutePath()); + MainWindow.addDownload(imageName, destFile.getName(), dlTask); + } + }); + } + }); + } + private static String generateFilename(String imageName, String virtualizerId) { + String fileName = imageName.replaceAll("[^a-zA-Z0-9_\\.\\-]+", "_"); + if (fileName.length() > 50) { + fileName = fileName.substring(0, 50); + } + if ("vmware".equals(virtualizerId)) { + fileName += ".vmdk"; + } else if ("virtualbox".equals(virtualizerId)) { + fileName += ".vdi"; + } else { + fileName += ".img"; + } + return fileName; + } + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/FileChooser.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/QFileChooser.java index a7698900..f3113dd0 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/FileChooser.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/QFileChooser.java @@ -7,18 +7,17 @@ import javax.swing.JFileChooser; import javax.swing.LookAndFeel; import javax.swing.UIManager; -import org.openslx.dozmod.Config; - -public class FileChooser extends JFileChooser { +@SuppressWarnings("serial") +public class QFileChooser extends JFileChooser { /** * @param isFileChooser wether filechooser is for selecting image to upload or selecting directory for download */ - public FileChooser(boolean isFileChooser){ - super(isFileChooser ? Config.getUploadPath() : Config.getDownloadPath()); + public QFileChooser(String path, boolean dirMode) { + super(path); // Ugly hack to get a prettier file chooser with GTK - should be moved to helper/util class LookAndFeel old = UIManager.getLookAndFeel(); - if (!old.getName().toLowerCase().contains("gtk")) { + if (old == null || !old.getName().toLowerCase().contains("gtk")) { old = null; } else { try { @@ -36,7 +35,7 @@ public class FileChooser extends JFileChooser { } refreshUI(this, false); } - setFileSelectionMode(isFileChooser ? JFileChooser.FILES_ONLY : JFileChooser.DIRECTORIES_ONLY); + setFileSelectionMode(dirMode ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY); } private static void refreshUI(JComponent c, boolean includeParent) { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java index d11160dc..412b2e59 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java @@ -5,36 +5,34 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.io.File; -import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; -import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenuItem; +import javax.swing.JOptionPane; import javax.swing.RowFilter; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import org.apache.log4j.Logger; import org.apache.thrift.TException; import org.openslx.bwlp.thrift.iface.ImageSummaryRead; import org.openslx.bwlp.thrift.iface.TAuthorizationException; import org.openslx.bwlp.thrift.iface.TNotFoundException; -import org.openslx.bwlp.thrift.iface.TransferInformation; -import org.openslx.dozmod.Config; -import org.openslx.dozmod.filetransfer.DownloadTask; +import org.openslx.dozmod.filetransfer.TransferHelper; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.MainWindow; -import org.openslx.dozmod.gui.helper.FileChooser; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.helper.PopupMenu; import org.openslx.dozmod.gui.window.layout.ImageListWindowLayout; import org.openslx.dozmod.gui.wizard.ImageWizard; import org.openslx.dozmod.gui.wizard.LectureWizard; +import org.openslx.dozmod.permissions.ImagePerms; import org.openslx.dozmod.thrift.ImageCache; import org.openslx.dozmod.thrift.Session; import org.openslx.dozmod.thrift.UserCache; @@ -50,9 +48,9 @@ public class ImageListWindow extends ImageListWindowLayout { public final ImageListWindow me = this; - private JMenuItem popupItemNewLecture = new JMenuItem("Neue Veranstaltung"); - private JMenuItem popupItemDelete = new JMenuItem("Löschen"); - private JMenuItem popupItemDownload = new JMenuItem("Download"); + private final JMenuItem popupItemNewLecture = new JMenuItem("Neue Veranstaltung"); + private final JMenuItem popupItemDelete = new JMenuItem("Löschen"); + private final JMenuItem popupItemDownload = new JMenuItem("Download"); public ImageListWindow() { super(); @@ -108,6 +106,14 @@ public class ImageListWindow extends ImageListWindowLayout { applyFilterOnTable(); } }); + + imageTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + ImageSummaryRead item = imageTable.getSelectedItem(); + updateAvailableOptions(item); + } + }); imageTable.addMouseListener(new MouseAdapter() { @Override @@ -206,7 +212,8 @@ public class ImageListWindow extends ImageListWindowLayout { MainWindow.showPage(MainMenuWindow.class); } }); - + + updateAvailableOptions(null); } private void startLectureWizard() { @@ -260,35 +267,9 @@ public class ImageListWindow extends ImageListWindowLayout { null, null); return; } - FileChooser fc = new FileChooser(false); - int action = fc.showOpenDialog(me); - File file = fc.getSelectedFile(); - if (action != JFileChooser.APPROVE_OPTION || file == null) - return; - TransferInformation transInf = null; - try { - transInf = ThriftManager.getSatClient().requestDownload(Session.getSatelliteToken(), - image.latestVersionId); - } catch (TException e) { - LOGGER.error("Failed to get transfer information: ", e); - return; - } - if (transInf == null) - return; - - try { - file.getAbsoluteFile().mkdirs(); - File destFile = new File(file.getAbsolutePath(), "Hans.vmdk"); // TODO: Name sanitized from display name - // TODO: Check if file exists - DownloadTask dlTask = new DownloadTask(Session.getSatelliteAddress(), transInf.getPlainPort(), - transInf.getToken(), destFile, image.fileSize, null); - new Thread(dlTask).start(); - Config.setDownloadPath(file.getAbsolutePath()); - MainWindow.addDownload(image.imageName, destFile.getName(), dlTask); - } catch (FileNotFoundException e) { - LOGGER.error("Got transfer information but failed to download: ", e); - return; - } + downloadButton.setEnabled(false); + TransferHelper.initDownload(JOptionPane.getFrameForComponent(this), image.latestVersionId, + image.imageName, image.virtId, image.fileSize); } private void applyFilterOnTable() { @@ -331,4 +312,17 @@ public class ImageListWindow extends ImageListWindowLayout { searchTextField.setForeground(Color.RED); } } + + private void updateAvailableOptions(ImageSummaryRead item) { + boolean download = ImagePerms.canDownload(item); + boolean link = ImagePerms.canLink(item); + boolean admin = ImagePerms.canAdmin(item); + downloadButton.setEnabled(download); + newLectureButton.setEnabled(link); + deleteButton.setEnabled(admin); + popupItemDownload.setEnabled(download); + popupItemNewLecture.setEnabled(link); + popupItemDelete.setEnabled(admin); + } + } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java index 4b84d8be..62290a18 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java @@ -19,7 +19,7 @@ import org.openslx.dozmod.Config; import org.openslx.dozmod.filetransfer.UploadTask; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.MainWindow; -import org.openslx.dozmod.gui.helper.FileChooser; +import org.openslx.dozmod.gui.helper.QFileChooser; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.wizard.Wizard; import org.openslx.dozmod.gui.wizard.layout.ImageUploadPageLayout; @@ -65,7 +65,7 @@ public class ImageUploadPage extends ImageUploadPageLayout { } private void browseForVm() { - FileChooser fc = new FileChooser(true); + QFileChooser fc = new QFileChooser(Config.getUploadPath(), false); FileNameExtensionFilter filter = new FileNameExtensionFilter("VMware Virtual Machine", "vmx"); fc.setFileFilter(filter); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/permissions/ImagePerms.java b/dozentenmodul/src/main/java/org/openslx/dozmod/permissions/ImagePerms.java index 198de7cd..4c6bf164 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/permissions/ImagePerms.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/permissions/ImagePerms.java @@ -2,6 +2,7 @@ package org.openslx.dozmod.permissions; import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.ImageDetailsRead; +import org.openslx.bwlp.thrift.iface.ImageSummaryRead; /** * Class for checking, whether user can edit given image @@ -18,4 +19,17 @@ public class ImagePerms { public static boolean canAdmin(ImageDetailsRead image) { return image != null && image.userPermissions != null && image.userPermissions.admin; } + + public static boolean canAdmin(ImageSummaryRead image) { + return image != null && image.userPermissions != null && image.userPermissions.admin; + } + + public static boolean canDownload(ImageSummaryRead image) { + return image != null && image.userPermissions != null && image.userPermissions.download; + } + + public static boolean canLink(ImageSummaryRead image) { + return image != null && image.userPermissions != null && image.userPermissions.link; + } + } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftError.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftError.java index ad2511ce..d6855f5a 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftError.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftError.java @@ -2,6 +2,8 @@ package org.openslx.dozmod.thrift; import java.awt.Component; +import javax.swing.SwingUtilities; + import org.apache.log4j.Logger; import org.apache.thrift.TException; import org.openslx.bwlp.thrift.iface.AuthorizationError; @@ -14,7 +16,7 @@ import org.openslx.dozmod.gui.helper.MessageType; public class ThriftError { - public static void showMessage(Component parent, Logger logger, TException ex, String messageText) { + public static void showMessage(final Component parent, Logger logger, TException ex, String messageText) { if (ex instanceof TNotFoundException) { messageText += "\n\nNicht gefunden"; } else if (ex instanceof TAuthorizationException) { @@ -28,7 +30,17 @@ public class ThriftError { messageText += "\n\nUnerwartete Ausnahme " + ex.getClass().getSimpleName() + " ist aufgetreten."; } logger.warn("A thrift call raised an exception", ex); - Gui.showMessageBox(parent, messageText, MessageType.ERROR, null, null); + if (SwingUtilities.isEventDispatchThread()) { + Gui.showMessageBox(parent, messageText, MessageType.ERROR, null, null); + return; + } + final String msg = messageText; + Gui.asyncExec(new Runnable() { + @Override + public void run() { + Gui.showMessageBox(parent, msg, MessageType.ERROR, null, null); + } + }); } public static String getString(AuthorizationError error) { |
