Guest User

Untitled

a guest
May 22nd, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.30 KB | None | 0 0
  1. package femmod;
  2.  
  3. import haven.Button;
  4. import haven.Coord;
  5. import haven.Label;
  6. import haven.LoginScreen;
  7. import haven.TextEntry;
  8. import haven.Widget;
  9.  
  10. import java.io.BufferedReader;
  11. import java.io.BufferedWriter;
  12. import java.io.FileInputStream;
  13. import java.io.FileOutputStream;
  14. import java.io.InputStreamReader;
  15. import java.io.OutputStreamWriter;
  16. import java.util.ArrayList;
  17.  
  18. public class AccountsPanel extends Widget {
  19.     public ArrayList<AccountData> accounts = new ArrayList<AccountData>();
  20.     ArrayList<Button> buttons = new ArrayList<Button>();
  21.     ArrayList<Button> buttonsDelete = new ArrayList<Button>();
  22.     String fileName = "accounts.conf";
  23.     int accountsMax = 22;
  24.     public static boolean once = true;
  25.  
  26.     public AccountsPanel(Coord pos, Coord size, Widget parent) {
  27.         super(pos, size, parent);
  28.  
  29.         LoadAccounts();
  30.  
  31.         // create buttons
  32.         int count = 0;
  33.         for (AccountData account : accounts) {
  34.             buttons.add(new Button(new Coord(0, (count * 22)), 200, this, account.Name));
  35.             buttonsDelete.add(new Button(new Coord(210, (count * 22)), 35, this, "x"));
  36.             count++;
  37.         }
  38.     }
  39.  
  40.     public void LoadAccounts() {
  41.         if (once) {
  42.             LoginScreen.Instance.error("Passwords are not stored securely!");
  43.             once = false;
  44.         }
  45.  
  46.         try {
  47.             FileInputStream fstream = new FileInputStream(fileName);
  48.             InputStreamReader in = new InputStreamReader(fstream);
  49.             BufferedReader br = new BufferedReader(in);
  50.             String strLine;
  51.  
  52.             while ((strLine = br.readLine()) != null) {
  53.                 String[] accountRawData = strLine.split("\t");
  54.  
  55.                 String name = Base64Coder.decodeString(accountRawData[0].trim());
  56.                 String pass = Base64Coder.decodeString(accountRawData[1].trim());
  57.  
  58.                 AddAccount(name, pass);
  59.             }
  60.  
  61.             br.close();
  62.             in.close();
  63.             fstream.close();
  64.         } catch (Exception e) {
  65.             LoginScreen.Instance.error(e.getMessage());
  66.         }
  67.  
  68.         if (accounts.size() <= 0)
  69.             LoginScreen.Instance.error("Account List is empty!");
  70.     }
  71.  
  72.     private void SaveAccounts() {
  73.         try {
  74.             FileOutputStream ofstream = new FileOutputStream(fileName);
  75.             OutputStreamWriter out = new OutputStreamWriter(ofstream);
  76.             BufferedWriter bw = new BufferedWriter(out);
  77.  
  78.             for (AccountData account : accounts) {
  79.                 String LoginEnc = Base64Coder.encodeString(account.Name);
  80.                 String PassEnc = Base64Coder.encodeString(account.Pass);
  81.                 bw.write(LoginEnc + "\t" + PassEnc + "\n");
  82.             }
  83.  
  84.             bw.close();
  85.             out.close();
  86.             ofstream.close();
  87.         } catch (Exception e) {
  88.             LoginScreen.Instance.error(e.getMessage());
  89.         }
  90.     }
  91.  
  92.     private void RemoveAccount(int id) {
  93.         if (id < 0 || id >= accounts.size()) {
  94.             LoginScreen.Instance.error("Incorrect Account id!");
  95.             return;
  96.         }
  97.  
  98.         accounts.remove(id);
  99.         SaveAccounts();
  100.  
  101.         LoginScreen.Instance.error("");
  102.         LoginScreen.Instance.clear();
  103.  
  104.         if (accounts.size() <= 0)
  105.             LoginScreen.Instance.error("Account List is empty!");
  106.  
  107.     }
  108.  
  109.     private boolean AddAccount(String name, String password) {
  110.         if (accounts.size() > accountsMax) {
  111.             LoginScreen.Instance.error("Account List is full!");
  112.             return false;
  113.         }
  114.  
  115.         for (AccountData account : accounts) {
  116.             if (account.Name.equals(name)) {
  117.                 LoginScreen.Instance.error("Account " + name + " already exits!");
  118.                 return false;
  119.             }
  120.         }
  121.  
  122.         if (name == null || password == null)
  123.             return false;
  124.  
  125.         accounts.add(new AccountData(name, password));
  126.         return true;
  127.     }
  128.  
  129.     public void wdgmsg(Widget sender, String msg, Object... args) {
  130.         if (msg == "accountsAdd") {
  131.             AddAccount((String) args[0], (String) args[1]);
  132.         }
  133.         if (msg == "accountsAddAndSave") {
  134.             if (AddAccount((String) args[0], (String) args[1])) {
  135.                 SaveAccounts();
  136.                 LoginScreen.Instance.error("");
  137.                 LoginScreen.Instance.clear();
  138.             }
  139.         } else if (buttons.contains(sender))
  140.             super.wdgmsg("accountLogin", new Object[] { accounts.get(buttons.indexOf(sender)) });
  141.         else if (buttonsDelete.contains(sender))
  142.             RemoveAccount(buttonsDelete.indexOf(sender));
  143.         else
  144.             super.wdgmsg(sender, msg, args);
  145.     }
  146.  
  147.     // internal classess
  148.     public class AccountData {
  149.         public AccountData(String name, String password) {
  150.             Name = name;
  151.             Pass = password;
  152.         }
  153.  
  154.         public String Name;
  155.         public String Pass;
  156.     };
  157.  
  158.     public class AccountAddPanel extends Widget {
  159.         private TextEntry accAddName = null;
  160.         private TextEntry accAddPass = null;
  161.         private Button accAddButton = null;
  162.  
  163.         public AccountAddPanel(Coord pos, Coord size, Widget parent) {
  164.             super(pos, size, parent);
  165.  
  166.             new Label(new Coord(10, 10), this, "Login:");
  167.             new Label(new Coord(10, 30), this, "Password:");
  168.             accAddName = new TextEntry(new Coord(68, 10), new Coord(100, 18), this, "");
  169.             accAddPass = new TextEntry(new Coord(68, 30), new Coord(100, 18), this, "");
  170.             accAddPass.uimsg("pw", 1);
  171.             accAddButton = new Button(new Coord(180, 20), 50, this, "Add");
  172.         }
  173.  
  174.         public void wdgmsg(Widget sender, String msg, Object... args) {
  175.             if (sender == accAddButton) {
  176.                 if (accAddName.text.equals("")) {
  177.                     setfocus(accAddName);
  178.                     LoginScreen.Instance.error("Enter Login!");
  179.                 } else if (accAddPass.text.equals("")) {
  180.                     setfocus(accAddPass);
  181.                     LoginScreen.Instance.error("Enter Password!");
  182.                 } else
  183.                     super.wdgmsg("accountsAddAndSave", new Object[] { accAddName.text, accAddPass.text });
  184.             } else
  185.                 super.wdgmsg(sender, msg, args);
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment