/*
*
* 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 org.apache.log4j.Logger;
import org.panbox.Settings;
import org.panbox.core.csp.CSPAdapterFactory;
import org.panbox.core.csp.StorageBackendType;
import org.panbox.desktop.common.vfs.backend.dropbox.DropboxAPIIntegration;
import org.panbox.desktop.common.vfs.backend.dropbox.DropboxAdapterFactory;
import org.panbox.desktop.common.vfs.backend.dropbox.DropboxClientIntegration;
import javax.swing.*;
import javax.swing.event.DocumentListener;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Flushable;
import java.io.IOException;
import java.util.ResourceBundle;
/**
*
* @author palige
*/
public class DropboxSettingsPanel extends javax.swing.JPanel implements
Flushable {
/**
*
*/
private static final long serialVersionUID = 3432563998047501515L;
private final static Logger logger = Logger
.getLogger(DropboxSettingsPanel.class);
private DropboxAdapterFactory dropboxAdapterFactory;
private Settings settings;
private static final ResourceBundle bundle = ResourceBundle.getBundle(
"org.panbox.desktop.common.gui.Messages", Settings.getInstance()
.getLocale());
/**
* Creates new form CSPSettingsPanel
*/
public DropboxSettingsPanel(ActionListener a, DocumentListener d) {
this.settings = Settings.getInstance();
this.dropboxAdapterFactory = (DropboxAdapterFactory) CSPAdapterFactory
.getInstance(StorageBackendType.DROPBOX);
initComponents();
syncDirTextField.getDocument().addDocumentListener(d);
accessTokenRefreshButton.addActionListener(a);
refresh();
}
public void refresh() {
String syncdir = settings.getDropboxSynchronizationDir(), acs = settings
.getDropboxAccessToken();
if ((syncdir != null) && (syncdir.length() > 0)) {
syncDirTextField.setText(syncdir);
}
if ((acs != null) && (acs.length() > 0)) {
accessTokenTextField.setText(acs);
}
}
/**
* 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() {
snycDirLabel = new javax.swing.JLabel();
syncDirTextField = new javax.swing.JTextField();
syncDirButton = new javax.swing.JButton();
accessTokenLabel = new javax.swing.JLabel();
accessTokenTextField = new javax.swing.JTextField();
accessTokenRefreshButton = new javax.swing.JButton();
setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
setPreferredSize(new java.awt.Dimension(588, 141));
snycDirLabel.setText(bundle
.getString("DropboxSettingsPanel.syncDirLabel")); // NOI18N
syncDirTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
syncDirTextFieldActionPerformed(evt);
}
});
syncDirButton.setText(bundle
.getString("DropboxSettingsPanel.syncDirButton")); // NOI18N
syncDirButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
syncDirButtonActionPerformed(evt);
}
});
accessTokenLabel.setText(bundle
.getString("DropboxSettingsPanel.accessTokenLabel.text")); // NOI18N
accessTokenTextField.setEditable(false);
accessTokenTextField
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
accessTokenTextFieldActionPerformed(evt);
}
});
accessTokenRefreshButton.setText(bundle
.getString("DropboxSettingsPanel.accessTokenRefreshButton")); // NOI18N
accessTokenRefreshButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
accessTokenRefreshButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(snycDirLabel)
.addComponent(accessTokenLabel))
.addGap(24, 24, 24)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
accessTokenTextField,
javax.swing.GroupLayout.DEFAULT_SIZE,
312, Short.MAX_VALUE)
.addComponent(syncDirTextField))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(
accessTokenRefreshButton,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
syncDirButton,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(6, 6, 6)));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(snycDirLabel)
.addComponent(
syncDirTextField,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(syncDirButton))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(accessTokenLabel)
.addComponent(
accessTokenTextField,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
accessTokenRefreshButton))
.addContainerGap(
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));
}// </editor-fold>//GEN-END:initComponents
private void syncDirTextFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_syncDirTextFieldActionPerformed
// currently no implementation is needed here.
}// GEN-LAST:event_syncDirTextFieldActionPerformed
private void syncDirButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_syncDirButtonActionPerformed
DropboxClientIntegration dcIntegration = (DropboxClientIntegration) dropboxAdapterFactory
.getClientAdapter();
File syncDir = null;
try {
if (dcIntegration.isClientInstalled()) {
syncDir = dcIntegration.readSyncDirFromMetadata();
} else {
logger.error("No dropbox installation found!");
JOptionPane
.showMessageDialog(
this,
bundle.getString("DropboxSettingsPanel.noDropboxInstallationMsg"),
bundle.getString("DropboxSettingsPanel.noDropboxInstallationTitle"),
JOptionPane.ERROR_MESSAGE);
return;
}
} catch (IOException e) {
logger.error(
"Error while trying to resolve dropbox synchronization directory!",
e);
}
// if syncDir *may* be null at this point
JFileChooser fileChooser = null;
if (syncDir == null) {
fileChooser = new JFileChooser();
} else {
fileChooser = new JFileChooser(syncDir.getParent());
}
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setSelectedFile(syncDir);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File sel = fileChooser.getSelectedFile();
if ((sel != null) && (sel.isDirectory()) && (sel.canRead())) {
syncDirTextField.setText(sel.getAbsolutePath());
}
}
}// GEN-LAST:event_syncDirButtonActionPerformed
private void accessTokenTextFieldActionPerformed(
java.awt.event.ActionEvent evt) {// GEN-FIRST:event_accessTokenTextFieldActionPerformed
}// GEN-LAST:event_accessTokenTextFieldActionPerformed
private void accessTokenRefreshButtonActionPerformed(
java.awt.event.ActionEvent evt) {// GEN-FIRST:event_accessTokenRefreshButtonActionPerformed
try {
String res = DropboxAPIIntegration.authenticate();
if ((res != null) && (res.length() > 0)) {
accessTokenTextField.setText(res);
}
} catch (Exception e) {
logger.error(
"Unable to obtain dropbox access token due to exception.",
e);
}
}// GEN-LAST:event_accessTokenRefreshButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel accessTokenLabel;
private javax.swing.JButton accessTokenRefreshButton;
private javax.swing.JTextField accessTokenTextField;
private javax.swing.JLabel snycDirLabel;
private javax.swing.JButton syncDirButton;
private javax.swing.JTextField syncDirTextField;
// End of variables declaration//GEN-END:variables
@Override
public void flush() throws IOException {
Settings settings = Settings.getInstance();
settings.setDropboxAccessToken(accessTokenTextField.getText());
settings.setDropboxSynchronizationDir(syncDirTextField.getText());
settings.flush();
}
}