Author: jflesch
Date: 2007-04-30 21:28:42 +0000 (Mon, 30 Apr 2007)
New Revision: 13066

Modified:
   trunk/apps/Thaw/src/thaw/gui/FileChooser.java
   trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
   trunk/apps/Thaw/src/thaw/plugins/signatures/SigConfigTab.java
Log:
Add functions (and buttons) to import/export identities

Modified: trunk/apps/Thaw/src/thaw/gui/FileChooser.java
===================================================================
--- trunk/apps/Thaw/src/thaw/gui/FileChooser.java       2007-04-30 19:34:39 UTC 
(rev 13065)
+++ trunk/apps/Thaw/src/thaw/gui/FileChooser.java       2007-04-30 21:28:42 UTC 
(rev 13066)
@@ -16,6 +16,10 @@

        private String finalDir = null;

+       public final static int OPEN_DIALOG = JFileChooser.OPEN_DIALOG;
+       public final static int SAVE_DIALOG = JFileChooser.SAVE_DIALOG;
+
+
        public FileChooser() {
                fileChooser = new JFileChooser();
        }
@@ -34,7 +38,8 @@
                if(v)
                        
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                else
-                       
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); /* 
Directories -> Recursivity */
+                        /* Directories -> Recursivity */
+                       
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        }

        /**

Modified: trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java   2007-04-30 
19:34:39 UTC (rev 13065)
+++ trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java   2007-04-30 
21:28:42 UTC (rev 13066)
@@ -17,12 +17,18 @@
 import freenet.crypt.Global;
 import freenet.crypt.RandomSource;

+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
 import thaw.core.Core;
 import thaw.core.Logger;
 import thaw.core.I18n;
 import thaw.plugins.Hsqldb;
 import thaw.core.Config;

+
+
 public class Identity {

        public final static int[] trustLevelInt = {
@@ -489,5 +495,76 @@
        public static Vector getOtherIdentities(Hsqldb db) {
                return getIdentities(db, "x IS NULL");
        }
+
+
+       public boolean exportIdentity(File file) {
+
+               try {
+                       FileOutputStream out = new FileOutputStream(file);
+
+                       out.write((nick+"\n").getBytes("UTF-8"));
+
+                       if (getX() != null)
+                               out.write( 
(Base64.encode(getX())+"\n").getBytes("UTF-8") );
+                       else
+                               out.write( "\n".getBytes("UTF-8"));
+
+                       if (getY() != null)
+                               out.write( 
(Base64.encode(getY())+"\n").getBytes("UTF-8") );
+                       else
+                               out.write( "\n".getBytes("UTF-8"));
+
+                       out.close();
+               } catch(java.io.FileNotFoundException e) {
+                       Logger.error(this, "(1) Can't export identity because: 
"+e.toString());
+                       return false;
+               } catch(java.io.UnsupportedEncodingException e) {
+                       Logger.error(this, "(2) Can't export identity because: 
"+e.toString());
+                       return false;
+               } catch (java.io.IOException e) {
+                       Logger.error(this, "(2) Can't export identity because: 
"+e.toString());
+                       return false;
+               }
+
+               return true;
+       }
+
+       public static Identity importIdentity(Hsqldb db, File file) {
+               try {
+                       byte[] lapin = new byte[5120];
+
+                       FileInputStream in = new FileInputStream(file);
+
+                       for (int i = 0 ; i < 5120 ; i++)
+                               lapin[i] = 0;
+
+                       in.read(lapin);
+                       in.close();
+
+                       String[] elements = new String(lapin).split("\n");
+
+
+                       if (elements.length < 3) {
+                               Logger.error(new Identity(), "not enought 
inforation in the file");
+                               return null;
+                       }
+
+                       Identity i = new Identity(db, -1, elements[0],
+                                                 Base64.decode(elements[2]),
+                                                 Base64.decode(elements[1]),
+                                                 false, 10);
+                       i.insert();
+
+                       return i;
+               } catch(java.io.FileNotFoundException e) {
+                       Logger.error(new Identity(), "(1) Unable to import 
identity because : "+e.toString());
+               } catch(java.io.IOException e) {
+                       Logger.error(new Identity(), "(2) Unable to import 
identity because : "+e.toString());
+               } catch(freenet.support.IllegalBase64Exception e) {
+                       Logger.error(new Identity(), "(2) Unable to import 
identity because : "+e.toString());
+               }
+
+               return null;
+       }
 }


Modified: trunk/apps/Thaw/src/thaw/plugins/signatures/SigConfigTab.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/signatures/SigConfigTab.java       
2007-04-30 19:34:39 UTC (rev 13065)
+++ trunk/apps/Thaw/src/thaw/plugins/signatures/SigConfigTab.java       
2007-04-30 21:28:42 UTC (rev 13066)
@@ -27,14 +27,17 @@

 import java.util.Vector;

+import java.io.File;
+
+
 import thaw.core.I18n;
 import thaw.core.ConfigWindow;
 import thaw.core.Config;
 import thaw.core.Logger;

 import thaw.gui.IconBox;
+import thaw.gui.FileChooser;

-
 import thaw.plugins.Hsqldb;


@@ -178,11 +181,11 @@

                        JPanel buttonPanel = new JPanel(new GridLayout(1, 3));

-                       addIdentity = new JButton(IconBox.minAdd);
+                       addIdentity    = new JButton(IconBox.minAdd);
                        removeIdentity = new JButton(IconBox.minRemove);
                        importIdentity = new JButton(IconBox.minImportAction);
                        exportIdentity = new JButton(IconBox.minExportAction);
-                       closeWindow = new JButton(IconBox.minClose);
+                       closeWindow    = new JButton(IconBox.minClose);

                        
addIdentity.setToolTipText(I18n.getMessage("thaw.plugin.signature.addIdentity"));
                        
removeIdentity.setToolTipText(I18n.getMessage("thaw.plugin.signature.removeIdentity"));
@@ -241,16 +244,40 @@
                        public IdentityImporter() { }

                        public void run() {
+                               FileChooser fc = new FileChooser();
+                               
fc.setTitle(I18n.getMessage("thaw.plugin.signature.import"));
+                               fc.setDirectoryOnly(false);
+                               fc.setDialogType(FileChooser.OPEN_DIALOG);

+                               File file = fc.askOneFile();
+
+                               if (file != null) {
+                                       Identity.importIdentity(db, file);
+                                       updateList();
+                               }
                        }
                }


                private class IdentityExporter implements Runnable {
-                       public IdentityExporter() { }
+                       private Identity i;

+                       public IdentityExporter(Identity i) {
+                               this.i = i;
+                       }
+
                        public void run() {
+                               FileChooser fc = new FileChooser();
+                               
fc.setTitle(I18n.getMessage("thaw.plugin.signature.export"));
+                               fc.setDirectoryOnly(false);
+                               fc.setDialogType(FileChooser.SAVE_DIALOG);

+                               File file = fc.askOneFile();
+
+                               if (file != null) {
+                                       i.exportIdentity(file);
+                                       updateList();
+                               }
                        }
                }

@@ -275,8 +302,12 @@
                        }

                        if (e.getSource() == exportIdentity) {
-                               Thread th = new Thread(new IdentityExporter());
-                               th.start();
+                               Identity i = (Identity)list.getSelectedValue();
+
+                               if (i != null) {
+                                       Thread th = new Thread(new 
IdentityExporter(i));
+                                       th.start();
+                               }
                        }

                        if (e.getSource() == closeWindow) {


Reply via email to