/*
*
* Panbox - encryption for cloud storage
* Copyright (C) 2014-2015 by Fraunhofer SIT and Sirrix AG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additonally, third party code may be provided with notices and open source
* licenses from communities and third parties that govern the use of those
* portions, and any licenses granted hereunder do not alter any rights and
* obligations you may have under such open source licenses, however, the
* disclaimer of warranty and limitation of liability provisions of the GPLv3
* will apply to all the product.
*
*/
package org.panbox.desktop.common.gui;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileView;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.panbox.OS;
import org.panbox.Settings;
import org.panbox.core.csp.ICSPAPIIntegration;
import org.panbox.core.csp.Revision;
import org.panbox.desktop.common.PanboxClient;
/**
* Created by Timo Nolle on 12.09.14.
*/
public class RestoreRevisionDialog extends javax.swing.JDialog {
/**
*
*/
private static final long serialVersionUID = 8511137183519416650L;
private static final File PANBOX_DIR = new File(Settings.getInstance()
.getMountDir());
private final ICSPAPIIntegration apiInt;
private final String shareName;
private String filePath;
private final PanboxClient client;
private final static Logger logger = Logger
.getLogger(RestoreRevisionDialog.class);
private final static java.util.ResourceBundle bundle = java.util.ResourceBundle
.getBundle("org/panbox/desktop/common/gui/Messages", Settings
.getInstance().getLocale()); // NOI18N
/**
* Creates new form RestoreRevisionGui
*/
public RestoreRevisionDialog(PanboxClient client,
ICSPAPIIntegration apiInt, String shareName) {
initComponents();
this.client = client;
this.apiInt = apiInt;
this.shareName = shareName;
}
/**
* Creates the gui for an already chosen file
*
* @param pathInsideShare
*/
public RestoreRevisionDialog(PanboxClient client,
ICSPAPIIntegration apiInt, String shareName, String pathInsideShare) {
initComponents();
this.client = client;
this.apiInt = apiInt;
this.shareName = shareName;
// Preset the fields
fileTextField.setText(File.separator + shareName + pathInsideShare);
fillRevisionsForFileName(pathInsideShare);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed"
// desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
fileLabel = new javax.swing.JLabel();
revisionLabel = new javax.swing.JLabel();
fileTextField = new javax.swing.JTextField();
browseButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
restoreButton = new javax.swing.JButton();
revisionComboBox = new javax.swing.JComboBox<Revision>();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle(bundle.getString("client.restorerevisiondialog.title")); // NOI18N
setAlwaysOnTop(true);
setModal(true);
setResizable(false);
fileLabel
.setText(bundle.getString("client.restorerevisiondialog.file")); // NOI18N
revisionLabel.setText(bundle
.getString("client.restorerevisiondialog.revisions")); // NOI18N
fileTextField.setMinimumSize(new java.awt.Dimension(4, 24));
fileTextField.setPreferredSize(new java.awt.Dimension(70, 24));
fileTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileTextFieldActionPerformed(evt);
}
});
browseButton.setText(bundle
.getString("client.restorerevisiondialog.browsebutton")); // NOI18N
browseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseButtonActionPerformed(evt);
}
});
cancelButton.setText(bundle
.getString("client.restorerevisiondialog.cancelbutton")); // NOI18N
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
restoreButton.setText(bundle
.getString("client.restorerevisiondialog.restorebutton")); // NOI18N
restoreButton.setEnabled(false);
restoreButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
restoreButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
fileLabel,
javax.swing.GroupLayout.PREFERRED_SIZE,
59,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
revisionLabel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(
fileTextField,
javax.swing.GroupLayout.DEFAULT_SIZE,
223,
Short.MAX_VALUE)
.addGap(18,
18,
18)
.addComponent(
browseButton))
.addComponent(
revisionComboBox,
0,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)))
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(0,
0,
Short.MAX_VALUE)
.addComponent(
restoreButton)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
cancelButton)))
.addContainerGap()));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fileLabel)
.addComponent(browseButton)
.addComponent(
fileTextField,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(revisionLabel)
.addComponent(
revisionComboBox,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(restoreButton))
.addContainerGap()));
pack();
}// </editor-fold>//GEN-END:initComponents
private void fileTextFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_fileTextFieldActionPerformed
String path = fileTextField.getText();
if (!path.equals("")) {
fillRevisionsForFileName(path);
}
}// GEN-LAST:event_fileTextFieldActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelButtonActionPerformed
this.dispose();
}// GEN-LAST:event_cancelButtonActionPerformed
private void restoreButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_restoreButtonActionPerformed
String selectedRev = ((Revision) revisionComboBox.getSelectedItem())
.getRevision();
boolean restoreSuccessful;
try {
restoreSuccessful = apiInt.restoreToRevision(filePath, selectedRev);
} catch (Exception e) {
logger.error("Error trying to restore old file version!", e);
restoreSuccessful = false;
}
if (!restoreSuccessful) {
JOptionPane
.showMessageDialog(
this,
"Restoring file failed. Please make sure the file has been fully synchronized with the cloud storage provider.",
"Restore failed", JOptionPane.ERROR_MESSAGE);
} else {
this.dispose();
}
}// GEN-LAST:event_restoreButtonActionPerformed
private String getCSPSupportedPath(String path) {
return path.replace(PANBOX_DIR.getAbsolutePath(), "");
}
private void fillRevisionsForFileName(String path) {
if(OS.getOperatingSystem().isWindows()) {
// Windows
path = path.replaceFirst(File.separator + File.separator + shareName, "");
} else {
// Linux
path = path.replaceFirst(File.separator + shareName, "");
}
String obfFilePath = client.getOnlineFilename(shareName, path);
filePath = obfFilePath;
// check if file is actually available at the given location
try {
if (apiInt.exists(obfFilePath)) {
List<Revision> revs = apiInt.getRevisions(obfFilePath);
revisionComboBox.setModel(new DefaultComboBoxModel<Revision>());
if (revs != null) {
for (Revision r : revs) {
revisionComboBox.addItem(r);
}
restoreButton.setEnabled(true);
}
} else {
JOptionPane
.showMessageDialog(
this,
bundle.getString("PublishIdentitiesDialog.fileNotFoundInCloudStorage"),
bundle.getString("PublishIdentitiesDialog.fileNotFound"),
JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception e) {
logger.error(
"Unable to determine if file exists in cloud storage!", e);
}
}
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_browseButtonActionPerformed
JFileChooser fileChooser = new JFileChooser(PANBOX_DIR + "/"
+ shareName);
fileChooser.setFileView(new FileView() {
@Override
public Boolean isTraversable(File f) {
try {
return f.isDirectory()
&& FilenameUtils.directoryContains(
PANBOX_DIR.getAbsolutePath(),
f.getAbsolutePath());
} catch (IOException e) {
logger.error("Error determining folder parent status!");
return true;
}
}
});
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int ret = fileChooser.showOpenDialog(this);
if (ret == JFileChooser.APPROVE_OPTION
&& fileChooser.getSelectedFile().getParentFile()
.getAbsolutePath()
.startsWith(PANBOX_DIR.getAbsolutePath())) {
String path = getCSPSupportedPath(fileChooser.getSelectedFile()
.getPath());
fileTextField.setText(path);
fillRevisionsForFileName(path);
}
}// GEN-LAST:event_browseButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton browseButton;
private javax.swing.JButton cancelButton;
private javax.swing.JLabel fileLabel;
private javax.swing.JTextField fileTextField;
private javax.swing.JButton restoreButton;
private javax.swing.JComboBox<Revision> revisionComboBox;
private javax.swing.JLabel revisionLabel;
// End of variables declaration//GEN-END:variables
}