Beispiel #1
0
OP_STATUS AccountManager::Init(OpString8& status)
{
    if (!m_prefs_file)
        return OpStatus::ERR;

	const char *accounts_str = "Accounts";
	int account_count, accounts_version;

	RETURN_IF_ERROR(PrefsUtils::ReadPrefsInt(m_prefs_file, accounts_str, "Count", 0, account_count));
	RETURN_IF_ERROR(PrefsUtils::ReadPrefsInt(m_prefs_file, accounts_str, "Version", -1, accounts_version));

	// if we have no information, we simply must assume it's the current version
	if (accounts_version == -1)
	{
		m_old_version = CURRENT_M2_ACCOUNT_VERSION;
		accounts_version = CURRENT_M2_ACCOUNT_VERSION;
		RETURN_IF_ERROR(PrefsUtils::WritePrefsInt(m_prefs_file, accounts_str, "Version", CURRENT_M2_ACCOUNT_VERSION));
	}
	if (accounts_version > CURRENT_M2_ACCOUNT_VERSION && account_count)
	{
		status.Append("Not possible to run old Opera version with new Opera mail files.\r\n");
		return OpStatus::ERR; // refuse to start
	}

	if (accounts_version < CURRENT_M2_ACCOUNT_VERSION)
	{
		m_old_version = accounts_version;
		RETURN_IF_ERROR(PrefsUtils::WritePrefsInt(m_prefs_file, accounts_str, "Version", CURRENT_M2_ACCOUNT_VERSION));
	}
	
	INT32 default_mail_account;
	RETURN_IF_ERROR(PrefsUtils::ReadPrefsInt(m_prefs_file, accounts_str, "Next Available Id"			, 1, m_next_available_account_id));
	RETURN_IF_ERROR(PrefsUtils::ReadPrefsInt(m_prefs_file, accounts_str, "Default Mail Account"			, 0, m_last_used_mail_account));
	RETURN_IF_ERROR(PrefsUtils::ReadPrefsInt(m_prefs_file, accounts_str, "Default Outgoing Mail Account", 0, default_mail_account));
	RETURN_IF_ERROR(PrefsUtils::ReadPrefsInt(m_prefs_file, accounts_str, "Default News Account"			, 0, m_default_news_account));
	RETURN_IF_ERROR(PrefsUtils::ReadPrefsInt(m_prefs_file, accounts_str, "Default Chat Account"			, 0, m_default_chat_account));
	m_default_mail_account.Set(default_mail_account);

	m_default_mail_account.Subscribe(MAKE_DELEGATE(*this, &AccountManager::CommitDefaultMailAccount));

	if (m_active_account!=-1) //Category is only available for active account -1 ('custom category')
		m_active_category.Empty();

    UINT16 account_id = 0;
	OpINTSortedVector valid_account_ids;
	char account_key[13];

	OP_STATUS ret;
    for (UINT16 i=1; i<=account_count; i++)
    {
		op_sprintf(account_key, "Account%u", i);

		// first get the account id
		RETURN_IF_ERROR(PrefsUtils::ReadPrefsInt(m_prefs_file, accounts_str, account_key, 0, account_id));

		OpString8 account_section;
		RETURN_IF_ERROR(account_section.AppendFormat("Account%d", account_id));; 
		OpString8 protocol;
		RETURN_IF_ERROR(PrefsUtils::ReadPrefsString(m_prefs_file, account_section, "Incoming Protocol", protocol));
		// check if it's a known type or an old unsupported type
		if (Account::GetProtocolFromName(protocol) == AccountTypes::UNDEFINED)
			continue;

        if (account_id != 0)
        {
            Account* account = OP_NEW(Account, (m_database));
            if (!account)
                return OpStatus::ERR_NO_MEMORY;

            Account* does_it_exist = NULL;
			if (OpStatus::IsError(ret=GetAccountById(account_id, does_it_exist)))
            {
                OP_DELETE(account);
                return ret;
            }
            if (does_it_exist)
            {
                OP_DELETE(account);
                return OpStatus::ERR; //Already exists in list!
            }

            account->Into(&m_accountlist);

            if (OpStatus::IsError(ret=account->Init(account_id, status)))
            {
                status.Append("Initializing ");
                status.Append(account_key);
                status.Append(" failed\n");
                return ret;
            }

            if (account->GetIsTemporary())
            {
				UINT16* temporary_id = OP_NEW(UINT16, ());
				if(!temporary_id)
					return OpStatus::ERR_NO_MEMORY;
				*temporary_id = account->GetAccountId();
				m_temporary_accounts.Add(temporary_id);
            }
			else
			{