Package fr.valhalla.mailcheck

Source Code of fr.valhalla.mailcheck.Account

/*   Valhalla MailChecker -- Simple IMAP Mail Checker
*    Copyright (C) 2010-2011  Guillaume Florimond (gflorimond at gmail dot com)
*
*    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
*    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/>.
*/

package fr.valhalla.mailcheck;

import fr.valhalla.mailcheck.utils.EncryptedProperties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

/**
*
* @author guillaume
*/
public class Account {

    private EncryptedProperties props;
    private String masterpassword;

    public int id;
    public String accountName;
    public String login;
    public String password;
    public String protocol;
    public String host;
    public String folder;
    public int port;
    public int sleep;
    public boolean ssl;
    public String sound;
    public boolean notifications;

    public Account(String masterpassword) {
        this.masterpassword = masterpassword;
        init();
    }

    public Account(String accountName, String masterpassword) {
        this.accountName = accountName;
        this.masterpassword = masterpassword;
        init();
        login = props.getProperty("login-" + accountName);
        password = props.getProperty("password-" + accountName);
        protocol = props.getProperty("protocol-" + accountName);
        host = props.getProperty("host-" + accountName);
        folder = props.getProperty("folder-" + accountName);
        port = new Integer(props.getProperty("port-" + accountName));
        sleep = new Integer(props.getProperty("sleep-" + accountName));
        ssl = new Boolean(props.getProperty("ssl-" + accountName));
        sound = props.getProperty("sound-" + accountName);
        notifications = new Boolean(props.getProperty("notifications-" + accountName));
    }

    /**
     * Méthode utilisée par tous les constructeurs
     */
    public void init() {
        try {
            props = new EncryptedProperties(masterpassword);
        } catch(Exception e) { e.printStackTrace(); }
        // On essaie de charger les properties
        try {
            FileInputStream in = new FileInputStream("fr.valhalla.mailcheck.accounts.properties");
            props.load(in);
            in.close();
        }
        catch(FileNotFoundException e) {
            // Le fichier n'existe pas : on le crée
            try {
                props.store(new FileOutputStream(new File("fr.valhalla.mailcheck.accounts.properties")), null);
            }
            catch(Exception e2) { e2.printStackTrace(); }
        }
        catch(IOException e) { e.printStackTrace(); }
    }

    /**
     * Créer un nouveau compte
     * @param accountName
     */
    public void addAccount(String accountName, String login, String password, String protocol, String host, String folder, int port, int sleep, boolean ssl, String sound, boolean notifications) {
        if(props.getProperty("count") == null) {
            id = 1;
        }
        else {
            id = new Integer( props.getProperty("count") ) + 1;
        }
        props.setProperty("count", new Integer(id).toString());
        props.setProperty("id-" + accountName, new Integer(id).toString());
        props.setProperty("account-" + accountName, accountName);
        props.setProperty("login-" + accountName, login);
        props.setProperty("password-" + accountName, password);
        props.setProperty("protocol-" + accountName, protocol);
        props.setProperty("host-" + accountName, host);
        props.setProperty("folder-" + accountName, folder);
        props.setProperty("port-" + accountName, new Integer(port).toString());
        props.setProperty("sleep-" + accountName, new Integer(sleep).toString());
        props.setProperty("ssl-" + accountName, new Boolean(ssl).toString());
        props.setProperty("sound-" + accountName, sound);
        props.setProperty("notifications-" + accountName, new Boolean(notifications).toString());
        saveAccount();
    }

    public void updateAccount(String accountName, String login, String password, String protocol, String host, String folder, int port, int sleep, boolean ssl, String sound, boolean notifications) {
        props.setProperty("id-" + accountName, new Integer(id).toString());
        props.setProperty("account-" + accountName, accountName);
        props.setProperty("login-" + accountName, login);
        props.setProperty("password-" + accountName, password);
        props.setProperty("protocol-" + accountName, protocol);
        props.setProperty("host-" + accountName, host);
        props.setProperty("folder-" + accountName, folder);
        props.setProperty("port-" + accountName, new Integer(port).toString());
        props.setProperty("sleep-" + accountName, new Integer(sleep).toString());
        props.setProperty("ssl-" + accountName, new Boolean(ssl).toString());
        props.setProperty("sound-" + accountName, sound);
        props.setProperty("notifications-" + accountName, new Boolean(notifications).toString());
        saveAccount();
    }

    public void deleteAccount() {
        id = new Integer( props.getProperty("count") ) - 1;
        props.setProperty("count", new Integer(id).toString());
        props.remove("id-" + accountName);
        props.remove("account-" + accountName);
        props.remove("login-" + accountName);
        props.remove("password-" + accountName);
        props.remove("protocol-" + accountName);
        props.remove("host-" + accountName);
        props.remove("folder-" + accountName);
        props.remove("port-" + accountName);
        props.remove("sleep-" + accountName);
        props.remove("ssl-" + accountName);
        props.remove("sound-" + accountName);
        props.remove("notifications-" + accountName);
        saveAccount();
    }

    public void saveAccount() {
        try {
            props.store(new FileOutputStream("fr.valhalla.mailcheck.accounts.properties"),"Accounts configuration");
        } catch(Exception e) { e.printStackTrace(); }
    }

    public int reCountAccounts() {
        Iterator i = props.keySet().iterator();
        int index = 0;
        while(i.hasNext()) {
            String key = (String) i.next();
            //String value = props.getProperty(key);
            if(key.substring(0, 7) == null ? "account" == null : key.substring(0, 7).equals("account"))
                index++;
        }
        props.setProperty("count", new Integer(index).toString());
        System.out.println("Recount: " + index);
        return index;
    }

    public String getAccountName() {
        return accountName;
    }

    public String getHost() {
        return host;
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }

    public String getFolder() {
        return folder;
    }

    public int getPort() {
        return port;
    }

    public String getProtocol() {
        return protocol;
    }

    public int getSleep() {
        return sleep;
    }

    public boolean isSSL() {
        return ssl;
    }

    public String getSound() {
        return sound;
    }

    public boolean hasNotifications() {
        return notifications;
    }


    public void setHost(String host) {
        this.host = host;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setFolder(String folder) {
        this.folder = folder;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public void setSleep(int sleep) {
        this.sleep = sleep;
    }

    public void setSsl(boolean ssl) {
        this.ssl = ssl;
    }

    public void setSound(String sound) {
        this.sound = sound;
    }

    public void setNotifications(boolean notifications) {
        this.notifications = notifications;
    }


   

}
TOP

Related Classes of fr.valhalla.mailcheck.Account

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.