Search in sources :

Example 1 with DownloadPlugins

use of net.pms.configuration.DownloadPlugins in project UniversalMediaServer by UniversalMediaServer.

the class RootFolder method addAdminFolder.

private void addAdminFolder() {
    DLNAResource res = new VirtualFolder(Messages.getString("PMS.131"), null);
    DLNAResource vsf = getVideoSettingsFolder();
    if (vsf != null) {
        res.addChild(vsf);
    }
    res.addChild(new VirtualFolder(Messages.getString("NetworkTab.39"), null) {

        @Override
        public void discoverChildren() {
            final ArrayList<DownloadPlugins> plugins = DownloadPlugins.downloadList();
            for (final DownloadPlugins plugin : plugins) {
                addChild(new VirtualVideoAction(plugin.getName(), true) {

                    @Override
                    public boolean enable() {
                        try {
                            plugin.install(null);
                        } catch (Exception e) {
                        }
                        return true;
                    }
                });
            }
        }
    });
    if (configuration.getScriptDir() != null) {
        final File scriptDir = new File(configuration.getScriptDir());
        if (scriptDir.exists()) {
            res.addChild(new VirtualFolder(Messages.getString("PMS.132"), null) {

                @Override
                public void discoverChildren() {
                    File[] files = scriptDir.listFiles();
                    if (files != null) {
                        for (File file : files) {
                            String name = file.getName().replaceAll("_", " ");
                            int pos = name.lastIndexOf('.');
                            if (pos != -1) {
                                name = name.substring(0, pos);
                            }
                            final File f = file;
                            addChild(new VirtualVideoAction(name, true) {

                                @Override
                                public boolean enable() {
                                    try {
                                        ProcessBuilder pb = new ProcessBuilder(f.getAbsolutePath());
                                        pb.redirectErrorStream(true);
                                        Process pid = pb.start();
                                        // consume the error and output process streams
                                        StreamGobbler.consume(pid.getInputStream());
                                        pid.waitFor();
                                    } catch (IOException | InterruptedException e) {
                                    }
                                    return true;
                                }
                            });
                        }
                    }
                }
            });
        }
    }
    // Resume file management
    if (configuration.isResumeEnabled()) {
        res.addChild(new VirtualFolder(Messages.getString("PMS.135"), null) {

            @Override
            public void discoverChildren() {
                final File[] files = ResumeObj.resumeFiles();
                addChild(new VirtualVideoAction(Messages.getString("PMS.136"), true) {

                    @Override
                    public boolean enable() {
                        for (File f : files) {
                            f.delete();
                        }
                        getParent().getChildren().remove(this);
                        return false;
                    }
                });
                for (final File f : files) {
                    String name = FileUtil.getFileNameWithoutExtension(f.getName());
                    name = name.replaceAll(ResumeObj.CLEAN_REG, "");
                    addChild(new VirtualVideoAction(name, false) {

                        @Override
                        public boolean enable() {
                            f.delete();
                            getParent().getChildren().remove(this);
                            return false;
                        }
                    });
                }
            }
        });
    }
    // Reboot UMS
    res.addChild(new VirtualVideoAction(Messages.getString("PMS.149"), true) {

        @Override
        public boolean enable() {
            ProcessUtil.reboot();
            // Reboot failed if we get here
            return false;
        }
    });
    addChild(res);
}
Also used : VirtualFolder(net.pms.dlna.virtual.VirtualFolder) URISyntaxException(java.net.URISyntaxException) XmlParseException(xmlwise.XmlParseException) MalformedURLException(java.net.MalformedURLException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) VirtualVideoAction(net.pms.dlna.virtual.VirtualVideoAction) DownloadPlugins(net.pms.configuration.DownloadPlugins)

Example 2 with DownloadPlugins

use of net.pms.configuration.DownloadPlugins in project UniversalMediaServer by UniversalMediaServer.

the class PluginTab method build.

public JComponent build() {
    ComponentOrientation orientation = ComponentOrientation.getOrientation(PMS.getLocale());
    String colSpec = FormLayoutUtil.getColSpec(COL_SPEC, orientation);
    FormLayout layout = new FormLayout(colSpec, ROW_SPEC);
    PanelBuilder builder = new PanelBuilder(layout);
    builder.border(Borders.DLU4);
    builder.opaque(true);
    CellConstraints cc = new CellConstraints();
    // Available Plugins section
    JComponent availablePluginsHeading = builder.addSeparator(Messages.getString("PluginTab.1"), FormLayoutUtil.flip(cc.xyw(1, 1, 9), colSpec, orientation));
    availablePluginsHeading = (JComponent) availablePluginsHeading.getComponent(0);
    availablePluginsHeading.setFont(availablePluginsHeading.getFont().deriveFont(Font.BOLD));
    final String[] cols = { Messages.getString("NetworkTab.41"), Messages.getString("PluginTab.3"), Messages.getString("NetworkTab.42"), Messages.getString("NetworkTab.43"), Messages.getString("NetworkTab.53") };
    final JTable table = new JTable(1, cols.length) {

        private static final long serialVersionUID = -5032210766949508624L;

        @Override
        public boolean isCellEditable(int rowIndex, int vColIndex) {
            return false;
        }

        @Override
        public String getToolTipText(MouseEvent e) {
            java.awt.Point p = e.getPoint();
            int rowIndex = rowAtPoint(p);
            DownloadPlugins plugin = plugins.get(rowIndex);
            return plugin.htmlString();
        }
    };
    refresh(table, cols);
    /* An attempt to set the correct row height adjusted for font scaling.
		 * It sets all rows based on the font size of cell (0, 0). The + 4 is
		 * to allow 2 pixels above and below the text. */
    DefaultTableCellRenderer cellRenderer = (DefaultTableCellRenderer) table.getCellRenderer(0, 0);
    FontMetrics metrics = cellRenderer.getFontMetrics(cellRenderer.getFont());
    table.setRowHeight(metrics.getLeading() + metrics.getMaxAscent() + metrics.getMaxDescent() + 4);
    table.setIntercellSpacing(new Dimension(8, 2));
    // Define column widths
    TableColumn nameColumn = table.getColumnModel().getColumn(0);
    nameColumn.setMinWidth(70);
    TableColumn versionColumn = table.getColumnModel().getColumn(2);
    versionColumn.setPreferredWidth(45);
    TableColumn ratingColumn = table.getColumnModel().getColumn(2);
    ratingColumn.setPreferredWidth(45);
    TableColumn authorColumn = table.getColumnModel().getColumn(3);
    authorColumn.setMinWidth(150);
    TableColumn descriptionColumn = table.getColumnModel().getColumn(4);
    descriptionColumn.setMinWidth(300);
    JScrollPane pane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane.setBorder(BorderFactory.createEmptyBorder());
    pane.setPreferredSize(new Dimension(200, 139));
    builder.add(pane, FormLayoutUtil.flip(cc.xyw(1, 3, 9), colSpec, orientation));
    CustomJButton install = new CustomJButton(Messages.getString("NetworkTab.39"));
    builder.add(install, FormLayoutUtil.flip(cc.xy(1, 5), colSpec, orientation));
    install.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!ExternalFactory.localPluginsInstalled()) {
                JOptionPane.showMessageDialog(looksFrame, Messages.getString("NetworkTab.40"));
                return;
            }
            // need admin rights here.
            try {
                if (!FileUtil.getFilePermissions(configuration.getPluginDirectory()).isWritable()) {
                    JOptionPane.showMessageDialog(looksFrame, Messages.getString("PluginTab.16") + (Platform.isWindows() ? "\n" + Messages.getString("AutoUpdate.13") : ""), Messages.getString("Dialog.PermissionsError"), JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (FileNotFoundException e1) {
                JOptionPane.showMessageDialog(looksFrame, String.format(Messages.getString("PluginTab.17"), configuration.getPluginDirectory()), Messages.getString("Dialog.Error"), JOptionPane.ERROR_MESSAGE);
                return;
            }
            final int[] rows = table.getSelectedRows();
            JPanel panel = new JPanel();
            GridLayout layout = new GridLayout(3, 1);
            panel.setLayout(layout);
            final JFrame frame = new JFrame(Messages.getString("NetworkTab.46"));
            frame.setSize(250, 110);
            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true);
            panel.add(progressBar);
            final JLabel label = new JLabel("");
            final JLabel inst = new JLabel("");
            panel.add(inst);
            panel.add(label);
            frame.add(panel);
            // Center the installation progress window
            frame.setLocationRelativeTo(null);
            Runnable r = new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < rows.length; i++) {
                        DownloadPlugins plugin = plugins.get(rows[i]);
                        if (plugin.isOld()) {
                            // This plugin requires newer UMS
                            // display error and skip it.
                            JOptionPane.showMessageDialog(looksFrame, "Plugin " + plugin.getName() + " requires a newer version of UMS. Please upgrade.", "Version Error", JOptionPane.ERROR_MESSAGE);
                            frame.setVisible(false);
                            continue;
                        }
                        frame.setVisible(true);
                        inst.setText(Messages.getString("NetworkTab.50") + ": " + plugin.getName());
                        try {
                            plugin.install(label);
                        } catch (Exception e) {
                            LOGGER.debug("An error occurred when trying to install the plugin: " + plugin.getName());
                            LOGGER.debug("Full error: " + e);
                        }
                    }
                    frame.setVisible(false);
                }
            };
            new Thread(r).start();
        }
    });
    CustomJButton refresh = new CustomJButton(Messages.getString("PluginTab.2") + " " + Messages.getString("PluginTab.1").toLowerCase());
    builder.add(refresh, FormLayoutUtil.flip(cc.xy(3, 5), colSpec, orientation));
    refresh.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            refresh(table, cols);
        }
    });
    // Installed Plugins section
    JComponent component;
    installedPluginsSeparator = (JPanel) builder.addSeparator(Messages.getString("PluginTab.0"), FormLayoutUtil.flip(cc.xyw(1, 7, 9), colSpec, orientation));
    installedPluginsSeparator.setVisible(false);
    component = (JComponent) installedPluginsSeparator.getComponent(0);
    component.setFont(component.getFont().deriveFont(Font.BOLD));
    pPlugins = new JPanel(new GridLayout());
    pPlugins.setVisible(false);
    builder.add(pPlugins, FormLayoutUtil.flip(cc.xyw(1, 9, 9), colSpec, orientation));
    // Credentials section
    component = builder.addSeparator(Messages.getString("PluginTab.8"), FormLayoutUtil.flip(cc.xyw(1, 11, 9), colSpec, orientation));
    component = (JComponent) component.getComponent(0);
    component.setFont(component.getFont().deriveFont(Font.BOLD));
    /* An attempt to set the correct row height adjusted for font scaling.
		 * It sets all rows based on the font size of cell (0, 0). The + 4 is
		 * to allow 2 pixels above and below the text. */
    cellRenderer = (DefaultTableCellRenderer) credTable.getCellRenderer(0, 0);
    metrics = cellRenderer.getFontMetrics(cellRenderer.getFont());
    credTable.setRowHeight(metrics.getLeading() + metrics.getMaxAscent() + metrics.getMaxDescent() + 4);
    credTable.setFillsViewportHeight(true);
    credTable.setIntercellSpacing(new Dimension(8, 2));
    // Define column widths
    TableColumn ownerColumn = credTable.getColumnModel().getColumn(0);
    ownerColumn.setMinWidth(70);
    TableColumn tagColumn = credTable.getColumnModel().getColumn(2);
    tagColumn.setPreferredWidth(45);
    TableColumn usrColumn = credTable.getColumnModel().getColumn(2);
    usrColumn.setPreferredWidth(45);
    TableColumn pwdColumn = credTable.getColumnModel().getColumn(3);
    pwdColumn.setPreferredWidth(45);
    pane = new JScrollPane(credTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane.setBorder(BorderFactory.createEmptyBorder());
    pane.setPreferredSize(new Dimension(200, 95));
    builder.add(pane, FormLayoutUtil.flip(cc.xyw(1, 13, 9), colSpec, orientation));
    // Add button
    CustomJButton add = new CustomJButton(Messages.getString("PluginTab.9"));
    builder.add(add, FormLayoutUtil.flip(cc.xy(1, 15), colSpec, orientation));
    add.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            addEditDialog(credTable, -1);
        }
    });
    // Edit button
    CustomJButton edit = new CustomJButton(Messages.getString("PluginTab.11"));
    builder.add(edit, FormLayoutUtil.flip(cc.xy(3, 15), colSpec, orientation));
    edit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            addEditDialog(credTable, credTable.getSelectedRow());
        }
    });
    // Delete button
    CustomJButton del = new CustomJButton(Messages.getString("PluginTab.12"));
    builder.add(del, FormLayoutUtil.flip(cc.xy(5, 15), colSpec, orientation));
    del.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int[] rows = credTable.getSelectedRows();
            if (rows.length > 0) {
                int n = JOptionPane.showConfirmDialog(looksFrame, Messages.getString("PluginTab.13"), "", JOptionPane.YES_NO_OPTION);
                if (n == JOptionPane.YES_OPTION) {
                    for (int i = 0; i < rows.length; i++) {
                        String key = (String) credTable.getValueAt(rows[i], 0);
                        if (StringUtils.isNotEmpty((String) credTable.getValueAt(rows[i], 1))) {
                            key = key + "." + (String) credTable.getValueAt(rows[i], 1);
                        }
                        cred.clearProperty(key);
                    }
                }
                try {
                    cred.save();
                } catch (ConfigurationException e1) {
                    LOGGER.warn("Couldn't save credentials file {}", e1.getMessage());
                    LOGGER.trace("", e1);
                }
                refreshCred(credTable);
            }
        }
    });
    // Edit Plugin Credential File button
    CustomJButton credEdit = new CustomJButton(Messages.getString("NetworkTab.54"));
    credEdit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JPanel tPanel = new JPanel(new BorderLayout());
            final JTextArea textArea = new JTextArea();
            textArea.setFont(new Font("Courier", Font.PLAIN, 12));
            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setPreferredSize(new Dimension(900, 450));
            try {
                configuration.initCred();
            } catch (IOException e2) {
                LOGGER.error("Could not create credentials file: {}", e2.getMessage());
                LOGGER.trace("", e2);
                return;
            }
            try {
                cred.save();
            } catch (ConfigurationException e3) {
                LOGGER.error("Could not save credentials file: {}", e3.getMessage());
                LOGGER.trace("", e3);
            }
            File f = configuration.getCredFile();
            try {
                try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8))) {
                    String line;
                    StringBuilder sb = new StringBuilder();
                    while ((line = in.readLine()) != null) {
                        sb.append(line);
                        sb.append("\n");
                    }
                    textArea.setText(sb.toString());
                }
            } catch (IOException e1) {
                LOGGER.error("Could not read credentials file: {}", e1.getMessage());
                LOGGER.trace("", e1);
                return;
            }
            tPanel.add(scrollPane, BorderLayout.NORTH);
            Object[] options = { Messages.getString("LooksFrame.9"), Messages.getString("NetworkTab.45") };
            if (JOptionPane.showOptionDialog(looksFrame, tPanel, Messages.getString("NetworkTab.54"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, null) == JOptionPane.OK_OPTION) {
                String text = textArea.getText();
                try {
                    try (FileOutputStream fos = new FileOutputStream(f)) {
                        fos.write(text.getBytes(StandardCharsets.UTF_8));
                        fos.flush();
                    }
                    PMS.getConfiguration().reload();
                    try {
                        cred.refresh();
                    } catch (ConfigurationException e2) {
                        LOGGER.error("An error occurred while updating credentials: {}", e2);
                        LOGGER.trace("", e2);
                    }
                    refreshCred(credTable);
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(looksFrame, Messages.getString("NetworkTab.55") + ": " + e1.getMessage());
                }
            }
        }
    });
    builder.add(credEdit, FormLayoutUtil.flip(cc.xy(7, 15), colSpec, orientation));
    JPanel panel = builder.getPanel();
    JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setBorder(BorderFactory.createEmptyBorder());
    return scrollPane;
}
Also used : JPanel(javax.swing.JPanel) JTextArea(javax.swing.JTextArea) ActionEvent(java.awt.event.ActionEvent) FileNotFoundException(java.io.FileNotFoundException) JProgressBar(javax.swing.JProgressBar) Font(java.awt.Font) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) GridLayout(java.awt.GridLayout) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) ConfigurationException(org.apache.commons.configuration.ConfigurationException) FontMetrics(java.awt.FontMetrics) DownloadPlugins(net.pms.configuration.DownloadPlugins) CellConstraints(com.jgoodies.forms.layout.CellConstraints) FormLayout(com.jgoodies.forms.layout.FormLayout) JScrollPane(javax.swing.JScrollPane) PanelBuilder(com.jgoodies.forms.builder.PanelBuilder) MouseEvent(java.awt.event.MouseEvent) InputStreamReader(java.io.InputStreamReader) JComponent(javax.swing.JComponent) JLabel(javax.swing.JLabel) ComponentOrientation(java.awt.ComponentOrientation) Dimension(java.awt.Dimension) IOException(java.io.IOException) TableColumn(javax.swing.table.TableColumn) FileNotFoundException(java.io.FileNotFoundException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CustomJButton(net.pms.newgui.components.CustomJButton) ActionListener(java.awt.event.ActionListener) JTable(javax.swing.JTable) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 3 with DownloadPlugins

use of net.pms.configuration.DownloadPlugins in project UniversalMediaServer by UniversalMediaServer.

the class PluginTab method refresh.

private void refresh(JTable table, String[] cols) {
    plugins = DownloadPlugins.downloadList();
    prepareTable(table, cols);
    DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    tableModel.setRowCount(0);
    for (int i = 0; i < plugins.size(); i++) {
        tableModel.insertRow(i, (Object[]) null);
        DownloadPlugins p = plugins.get(i);
        table.setValueAt(p.getName(), i, 0);
        table.setValueAt(p.getVersion(), i, 1);
        table.setValueAt(p.getRating(), i, 2);
        table.setValueAt(p.getAuthor(), i, 3);
        table.setValueAt(p.getDescription(), i, 4);
    }
    tableModel.fireTableDataChanged();
}
Also used : DefaultTableModel(javax.swing.table.DefaultTableModel) DownloadPlugins(net.pms.configuration.DownloadPlugins)

Aggregations

DownloadPlugins (net.pms.configuration.DownloadPlugins)3 ConfigurationException (org.apache.commons.configuration.ConfigurationException)2 PanelBuilder (com.jgoodies.forms.builder.PanelBuilder)1 CellConstraints (com.jgoodies.forms.layout.CellConstraints)1 FormLayout (com.jgoodies.forms.layout.FormLayout)1 BorderLayout (java.awt.BorderLayout)1 ComponentOrientation (java.awt.ComponentOrientation)1 Dimension (java.awt.Dimension)1 Font (java.awt.Font)1 FontMetrics (java.awt.FontMetrics)1 GridLayout (java.awt.GridLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 MouseEvent (java.awt.event.MouseEvent)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1