Search in sources :

Example 1 with GetAddressbooksException

use of de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException in project janrufmonitor by tbrandt77.

the class FritzOSFirmware method getAddressbooks.

public Map getAddressbooks() throws GetAddressbooksException, IOException {
    if (!this.isInitialized())
        throw new GetAddressbooksException("Could not get address book list from FritzBox: FritzBox firmware not initialized.");
    StringBuffer data = new StringBuffer();
    String urlstr = getProtocol() + this.m_address + ":" + this.m_port + "/fon_num/fonbook_select.lua?sid=" + this.m_sid;
    try {
        data.append(this.executeURL(urlstr, null, true).trim());
    } catch (UnsupportedEncodingException e) {
        this.m_logger.log(Level.WARNING, e.getMessage(), e);
    } catch (IOException e) {
        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
        throw new GetAddressbooksException("Could not get address book list from FritzBox: " + e.getMessage());
    }
    Map abs = new HashMap();
    String[] s = data.toString().split("uiBookid");
    Pattern p = Pattern.compile(PATTERN_DETECT_AB, Pattern.UNICODE_CASE);
    for (int i = 0, j = s.length; i < j; i++) {
        Matcher m = p.matcher(s[i]);
        while (m.find() && m.groupCount() > 1) {
            abs.put(Integer.parseInt(m.group(1)), m.group(2).trim());
        }
    }
    return abs;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) GetAddressbooksException(de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with GetAddressbooksException

use of de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException in project janrufmonitor by tbrandt77.

the class FritzBoxPhonebookManager method createFieldEditors.

protected void createFieldEditors() {
    FirmwareManager fwm = FirmwareManager.getInstance();
    String label = this.m_i18n.getString(this.getNamespace(), "enabled", "label", this.m_language);
    if (label.length() < 150)
        for (int i = 150; i > label.length(); i--) {
            label += " ";
        }
    BooleanFieldEditor bfe = new BooleanFieldEditor(this.getConfigNamespace() + SEPARATOR + "enabled", label, this.getFieldEditorParent());
    bfe.setEnabled((fwm.isInstance(UnitymediaFirmware.class) || fwm.isInstance(SessionIDFritzBoxFirmware.class) || fwm.isInstance(FritzOSFirmware.class) || fwm.isInstance(FritzOS559Firmware.class) || fwm.isInstance(TR064FritzBoxFirmware.class)), this.getFieldEditorParent());
    addField(bfe);
    if (fwm.isInstance(FritzOSFirmware.class) || fwm.isInstance(FritzOS559Firmware.class) || fwm.isInstance(TR064FritzBoxFirmware.class)) {
        try {
            if (!fwm.isLoggedIn())
                fwm.login();
            Map adb = fwm.getAddressbooks();
            String[][] list = new String[adb.size()][2];
            Iterator i = adb.keySet().iterator();
            int c = 0;
            while (i.hasNext()) {
                list[c][1] = ((Integer) i.next()).toString();
                list[c][0] = (String) adb.get(Integer.parseInt(list[c][1]));
                c++;
            }
            ComboFieldEditor cfe = new ComboFieldEditor(getConfigNamespace() + SEPARATOR + "ab", this.m_i18n.getString(this.getNamespace(), "ab", "label", this.m_language), list, this.getFieldEditorParent());
            addField(cfe);
        } catch (GetAddressbooksException e) {
            this.m_logger.severe(e.getMessage());
        } catch (IOException e) {
            this.m_logger.severe(e.getMessage());
        } catch (FritzBoxLoginException e) {
            this.m_logger.severe(e.getMessage());
        }
    }
}
Also used : FritzBoxLoginException(de.janrufmonitor.fritzbox.firmware.exception.FritzBoxLoginException) TR064FritzBoxFirmware(de.janrufmonitor.fritzbox.firmware.TR064FritzBoxFirmware) BooleanFieldEditor(org.eclipse.jface.preference.BooleanFieldEditor) IOException(java.io.IOException) ComboFieldEditor(org.eclipse.jface.preference.ComboFieldEditor) FirmwareManager(de.janrufmonitor.fritzbox.firmware.FirmwareManager) FritzOSFirmware(de.janrufmonitor.fritzbox.firmware.FritzOSFirmware) Iterator(java.util.Iterator) GetAddressbooksException(de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException) Map(java.util.Map)

Example 3 with GetAddressbooksException

use of de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException in project janrufmonitor by tbrandt77.

the class FritzBoxPhonebookManager method createCallerListFromFritzBoxPhonebook.

private void createCallerListFromFritzBoxPhonebook() {
    Thread t = new Thread(new Runnable() {

        Logger m_logger;

        public void run() {
            isSyncing = true;
            this.m_logger = LogManager.getLogManager().getLogger(IJAMConst.DEFAULT_LOGGER);
            if (this.m_logger.isLoggable(Level.FINE))
                this.m_logger.fine("Starting JAM-FritzBoxPhonebookSync-Thread");
            File mso_cache = new File(FBP_CACHE_PATH);
            if (!mso_cache.exists())
                mso_cache.mkdirs();
            ICallerList cl = getRuntime().getCallerFactory().createCallerList();
            FirmwareManager fwm = FirmwareManager.getInstance();
            fwm.startup();
            try {
                if (!fwm.isLoggedIn())
                    fwm.login();
                if (this.m_logger.isLoggable(Level.INFO))
                    this.m_logger.info("FritzBox Firmware created.");
                if (this.m_logger.isLoggable(Level.INFO))
                    this.m_logger.info("Login to FritzBox successfull.");
                // check if phonebook is configured
                String abId = getConfiguration().getProperty(CFG_ADDRESSBOOK, "0");
                if (this.m_logger.isLoggable(Level.INFO))
                    this.m_logger.info("Getting FritzBox phonebook ID: #" + abId);
                int id = Integer.parseInt(abId);
                String name = null;
                try {
                    Map abs = fwm.getAddressbooks();
                    if (abs.containsKey(Integer.parseInt(abId))) {
                        name = (String) abs.get(Integer.parseInt(abId));
                        if (this.m_logger.isLoggable(Level.INFO))
                            this.m_logger.info("Getting FritzBox phonebook name: " + name);
                    }
                } catch (GetAddressbooksException e) {
                    this.m_logger.log(Level.WARNING, e.getMessage(), e);
                }
                List callers = null;
                if (name != null) {
                    callers = fwm.getCallerList(id, name);
                    if (this.m_logger.isLoggable(Level.INFO))
                        this.m_logger.info("Getting FritzBox phonebook callers: " + callers.size());
                } else {
                    callers = fwm.getCallerList();
                    if (this.m_logger.isLoggable(Level.INFO))
                        this.m_logger.info("Getting FritzBox default phonebook callers: " + callers.size());
                }
                if (callers.size() == 0) {
                    try {
                        getDatabaseHandler().deleteCallerList(getRuntime().getCallerFactory().createCallerList());
                        getDatabaseHandler().commit();
                    } catch (SQLException e) {
                        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                        try {
                            getDatabaseHandler().rollback();
                        } catch (SQLException e1) {
                            this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
                        }
                    }
                    isSyncing = false;
                    return;
                }
                cl.add(FritzBoxMappingManager.getInstance().toCallerList(callers));
            } catch (FritzBoxLoginException e2) {
                this.m_logger.log(Level.SEVERE, e2.getMessage(), e2);
            } catch (GetCallerListException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            } catch (IOException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            } catch (Throwable e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            }
            try {
                getDatabaseHandler().deleteCallerList(getRuntime().getCallerFactory().createCallerList());
                getDatabaseHandler().insertOrUpdateCallerList(cl);
                getDatabaseHandler().commit();
            } catch (SQLException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                try {
                    getDatabaseHandler().rollback();
                } catch (SQLException e1) {
                    this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
                }
            }
            isSyncing = false;
            if (this.m_logger.isLoggable(Level.FINE))
                this.m_logger.fine("Stopping JAM-FritzBoxPhonebookSync-Thread");
        }
    });
    t.setName("JAM-FritzBoxPhonebookSync-Thread-(non-deamon)");
    t.start();
}
Also used : FritzBoxLoginException(de.janrufmonitor.fritzbox.firmware.exception.FritzBoxLoginException) GetCallerListException(de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Logger(java.util.logging.Logger) ICallerList(de.janrufmonitor.framework.ICallerList) FirmwareManager(de.janrufmonitor.fritzbox.firmware.FirmwareManager) GetAddressbooksException(de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException) ICallerList(de.janrufmonitor.framework.ICallerList) List(java.util.List) File(java.io.File) Map(java.util.Map)

Example 4 with GetAddressbooksException

use of de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException in project janrufmonitor by tbrandt77.

the class FritzOS559Firmware method getAddressbooks.

public Map getAddressbooks() throws GetAddressbooksException, IOException {
    if (!this.isInitialized())
        throw new GetAddressbooksException("Could not get address book list from FritzBox: FritzBox firmware not initialized.");
    StringBuffer data = new StringBuffer();
    String urlstr = getProtocol() + this.m_address + ":" + this.m_port + "/fon_num/fonbook_select.lua?sid=" + this.m_sid;
    try {
        data.append(this.executeURL(urlstr, null, true).trim());
    } catch (UnsupportedEncodingException e) {
        this.m_logger.log(Level.WARNING, e.getMessage(), e);
    } catch (IOException e) {
        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
        throw new GetAddressbooksException("Could not get address book list from FritzBox: " + e.getMessage());
    }
    Map abs = new HashMap();
    String[] s = data.toString().split("uiBookid");
    Pattern p = Pattern.compile(PATTERN_DETECT_AB, Pattern.UNICODE_CASE);
    for (int i = 0, j = s.length; i < j; i++) {
        Matcher m = p.matcher(s[i]);
        while (m.find() && m.groupCount() > 1) {
            abs.put(Integer.parseInt(m.group(1)), m.group(2).trim());
        }
    }
    return abs;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) GetAddressbooksException(de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with GetAddressbooksException

use of de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException in project janrufmonitor by tbrandt77.

the class UnitymediaFirmware method getAddressbooks.

public Map getAddressbooks() throws GetAddressbooksException, IOException {
    if (!this.isInitialized())
        throw new GetAddressbooksException("Could not get address book list from FritzBox: FritzBox firmware not initialized.");
    StringBuffer data = new StringBuffer();
    String urlstr = getProtocol() + this.m_address + ":" + this.m_port + "/fon_num/fonbook_select.lua?sid=" + this.m_sid;
    try {
        data.append(this.executeURL(urlstr, null, true).trim());
    } catch (UnsupportedEncodingException e) {
        this.m_logger.log(Level.WARNING, e.getMessage(), e);
    } catch (IOException e) {
        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
        throw new GetAddressbooksException("Could not get address book list from FritzBox: " + e.getMessage());
    }
    Map abs = new HashMap();
    String[] s = data.toString().split("uiBookid");
    Pattern p = Pattern.compile(PATTERN_DETECT_AB, Pattern.UNICODE_CASE);
    for (int i = 0, j = s.length; i < j; i++) {
        Matcher m = p.matcher(s[i]);
        while (m.find() && m.groupCount() > 1) {
            abs.put(Integer.parseInt(m.group(1)), m.group(2).trim());
        }
    }
    return abs;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) GetAddressbooksException(de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

GetAddressbooksException (de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException)5 IOException (java.io.IOException)5 Map (java.util.Map)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 FirmwareManager (de.janrufmonitor.fritzbox.firmware.FirmwareManager)2 FritzBoxLoginException (de.janrufmonitor.fritzbox.firmware.exception.FritzBoxLoginException)2 ICallerList (de.janrufmonitor.framework.ICallerList)1 FritzOSFirmware (de.janrufmonitor.fritzbox.firmware.FritzOSFirmware)1 TR064FritzBoxFirmware (de.janrufmonitor.fritzbox.firmware.TR064FritzBoxFirmware)1 GetCallerListException (de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException)1 File (java.io.File)1 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Logger (java.util.logging.Logger)1 BooleanFieldEditor (org.eclipse.jface.preference.BooleanFieldEditor)1 ComboFieldEditor (org.eclipse.jface.preference.ComboFieldEditor)1