Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package femmod;
- import haven.Button;
- import haven.Coord;
- import haven.Label;
- import haven.LoginScreen;
- import haven.TextEntry;
- import haven.Widget;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.util.ArrayList;
- public class AccountsPanel extends Widget {
- public ArrayList<AccountData> accounts = new ArrayList<AccountData>();
- ArrayList<Button> buttons = new ArrayList<Button>();
- ArrayList<Button> buttonsDelete = new ArrayList<Button>();
- String fileName = "accounts.conf";
- int accountsMax = 22;
- public static boolean once = true;
- public AccountsPanel(Coord pos, Coord size, Widget parent) {
- super(pos, size, parent);
- LoadAccounts();
- // create buttons
- int count = 0;
- for (AccountData account : accounts) {
- buttons.add(new Button(new Coord(0, (count * 22)), 200, this, account.Name));
- buttonsDelete.add(new Button(new Coord(210, (count * 22)), 35, this, "x"));
- count++;
- }
- }
- public void LoadAccounts() {
- if (once) {
- LoginScreen.Instance.error("Passwords are not stored securely!");
- once = false;
- }
- try {
- FileInputStream fstream = new FileInputStream(fileName);
- InputStreamReader in = new InputStreamReader(fstream);
- BufferedReader br = new BufferedReader(in);
- String strLine;
- while ((strLine = br.readLine()) != null) {
- String[] accountRawData = strLine.split("\t");
- String name = Base64Coder.decodeString(accountRawData[0].trim());
- String pass = Base64Coder.decodeString(accountRawData[1].trim());
- AddAccount(name, pass);
- }
- br.close();
- in.close();
- fstream.close();
- } catch (Exception e) {
- LoginScreen.Instance.error(e.getMessage());
- }
- if (accounts.size() <= 0)
- LoginScreen.Instance.error("Account List is empty!");
- }
- private void SaveAccounts() {
- try {
- FileOutputStream ofstream = new FileOutputStream(fileName);
- OutputStreamWriter out = new OutputStreamWriter(ofstream);
- BufferedWriter bw = new BufferedWriter(out);
- for (AccountData account : accounts) {
- String LoginEnc = Base64Coder.encodeString(account.Name);
- String PassEnc = Base64Coder.encodeString(account.Pass);
- bw.write(LoginEnc + "\t" + PassEnc + "\n");
- }
- bw.close();
- out.close();
- ofstream.close();
- } catch (Exception e) {
- LoginScreen.Instance.error(e.getMessage());
- }
- }
- private void RemoveAccount(int id) {
- if (id < 0 || id >= accounts.size()) {
- LoginScreen.Instance.error("Incorrect Account id!");
- return;
- }
- accounts.remove(id);
- SaveAccounts();
- LoginScreen.Instance.error("");
- LoginScreen.Instance.clear();
- if (accounts.size() <= 0)
- LoginScreen.Instance.error("Account List is empty!");
- }
- private boolean AddAccount(String name, String password) {
- if (accounts.size() > accountsMax) {
- LoginScreen.Instance.error("Account List is full!");
- return false;
- }
- for (AccountData account : accounts) {
- if (account.Name.equals(name)) {
- LoginScreen.Instance.error("Account " + name + " already exits!");
- return false;
- }
- }
- if (name == null || password == null)
- return false;
- accounts.add(new AccountData(name, password));
- return true;
- }
- public void wdgmsg(Widget sender, String msg, Object... args) {
- if (msg == "accountsAdd") {
- AddAccount((String) args[0], (String) args[1]);
- }
- if (msg == "accountsAddAndSave") {
- if (AddAccount((String) args[0], (String) args[1])) {
- SaveAccounts();
- LoginScreen.Instance.error("");
- LoginScreen.Instance.clear();
- }
- } else if (buttons.contains(sender))
- super.wdgmsg("accountLogin", new Object[] { accounts.get(buttons.indexOf(sender)) });
- else if (buttonsDelete.contains(sender))
- RemoveAccount(buttonsDelete.indexOf(sender));
- else
- super.wdgmsg(sender, msg, args);
- }
- // internal classess
- public class AccountData {
- public AccountData(String name, String password) {
- Name = name;
- Pass = password;
- }
- public String Name;
- public String Pass;
- };
- public class AccountAddPanel extends Widget {
- private TextEntry accAddName = null;
- private TextEntry accAddPass = null;
- private Button accAddButton = null;
- public AccountAddPanel(Coord pos, Coord size, Widget parent) {
- super(pos, size, parent);
- new Label(new Coord(10, 10), this, "Login:");
- new Label(new Coord(10, 30), this, "Password:");
- accAddName = new TextEntry(new Coord(68, 10), new Coord(100, 18), this, "");
- accAddPass = new TextEntry(new Coord(68, 30), new Coord(100, 18), this, "");
- accAddPass.uimsg("pw", 1);
- accAddButton = new Button(new Coord(180, 20), 50, this, "Add");
- }
- public void wdgmsg(Widget sender, String msg, Object... args) {
- if (sender == accAddButton) {
- if (accAddName.text.equals("")) {
- setfocus(accAddName);
- LoginScreen.Instance.error("Enter Login!");
- } else if (accAddPass.text.equals("")) {
- setfocus(accAddPass);
- LoginScreen.Instance.error("Enter Password!");
- } else
- super.wdgmsg("accountsAddAndSave", new Object[] { accAddName.text, accAddPass.text });
- } else
- super.wdgmsg(sender, msg, args);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment