예제 #1
0
NS_IMETHODIMP KDEWallet::ModifyLogin(nsILoginInfo *oldLogin,
                                     nsISupports *modLogin) {
    PR_LOG( gKDEWalletLog, PR_LOG_DEBUG, ( "KDEWallet::ModifyLogin() Called") );
    nsresult rv;
    nsCOMPtr<nsILoginInfo> newLogin( do_QueryInterface(modLogin, &rv) );
    if(rv != NS_OK) {
        /* Otherwise, it has to be an nsIPropertyBag.
          * Let's get the attributes from the old login, then append the ones
          * fetched from the property bag. Gracefully, if an attribute appears
          * twice in an attribut list, the last value is stored. */
        nsCOMPtr<nsILoginInfo> login;
        rv = oldLogin->Clone( getter_AddRefs(login) );
        NS_ENSURE_SUCCESS(rv, rv);

        newLogin = do_QueryInterface(login, &rv);
        NS_ENSURE_SUCCESS(rv, rv);

        nsCOMPtr<nsIPropertyBag> propBag( do_QueryInterface(modLogin, &rv) );
        NS_ENSURE_SUCCESS(rv, rv);

        nsCOMPtr<nsISimpleEnumerator> enumerator;
        rv = propBag->GetEnumerator(getter_AddRefs(enumerator));
        NS_ENSURE_SUCCESS(rv, rv);

        nsCOMPtr<nsISupports> sup;
        nsCOMPtr<nsIProperty> prop;
        nsAutoString propName;
        bool hasMoreElements;

        rv = enumerator->HasMoreElements(&hasMoreElements);
        NS_ENSURE_SUCCESS(rv, rv);

        while( hasMoreElements ) {
            rv = enumerator->GetNext(getter_AddRefs(sup));
            NS_ENSURE_SUCCESS(rv, rv);

            rv = enumerator->HasMoreElements(&hasMoreElements);
            NS_ENSURE_SUCCESS(rv, rv);

            prop = do_QueryInterface(sup, &rv);
            NS_ENSURE_SUCCESS(rv, rv);

            prop->GetName(propName);
            PR_LOG( gKDEWalletLog, PR_LOG_DEBUG, ( "KDEWallet::ModifyLogin() Modify property: %s", NS_ConvertUTF16toUTF8(propName).get() ) );

            nsCOMPtr<nsIVariant> val;
            prop->GetValue(getter_AddRefs(val));
            if (!val)
                return NS_ERROR_FAILURE;
            nsString valueString;

            rv = val->GetAsDOMString(valueString);
            NS_ENSURE_SUCCESS(rv, rv);

            if( propName.EqualsLiteral( kHostnameAttr ) )
                newLogin->SetHostname( valueString );
            if( propName.EqualsLiteral( kUsernameAttr ) )
                newLogin->SetUsername( valueString );
            if( propName.EqualsLiteral( kUsernameFieldAttr ) )
                newLogin->SetUsernameField( valueString );
            if( propName.EqualsLiteral( kPasswordAttr ) )
                newLogin->SetPassword( valueString );
            if( propName.EqualsLiteral( kPasswordFieldAttr ) )
                newLogin->SetPasswordField( valueString );
            if( propName.EqualsLiteral( kFormSubmitURLAttr ) )
                newLogin->SetFormSubmitURL( valueString );
            if( propName.EqualsLiteral( kHttpRealmAttr ) )
                newLogin->SetHttpRealm( valueString );

            if( propName.EqualsLiteral( kGuidAttr ) ) {
                nsCOMPtr<nsILoginMetaInfo> loginmeta( do_QueryInterface(newLogin, &rv) );
                NS_ENSURE_SUCCESS(rv, rv);
                nsAutoString aGUID ;
                rv = loginmeta->SetGuid( valueString );
                NS_ENSURE_SUCCESS(rv, rv);
            }
        }
    }
    rv = RemoveLogin(oldLogin);
    NS_ENSURE_SUCCESS(rv, rv);
    return AddLogin(newLogin);
}
NS_IMETHODIMP GnomeKeyring::ModifyLogin(nsILoginInfo *oldLogin,
                                        nsISupports *modLogin)
{
  nsresult interfaceok;

  /* If the second argument is an nsILoginInfo,
   * just remove the old login and add the new one */
  nsCOMPtr<nsILoginInfo> newLogin( do_QueryInterface(modLogin, &interfaceok) );
  if (interfaceok == NS_OK) {
    nsresult rvremovelogin = RemoveLogin(oldLogin);
    nsresult rvaddlogin = AddLogin(newLogin);
    if(NS_FAILED(rvremovelogin)) {
        return rvremovelogin;
    } else {
        return rvaddlogin;
    }
  }

  /* Otherwise, it has to be an nsIPropertyBag.
   * Let's get the attributes from the old login, then append the ones
   * fetched from the property bag. Gracefully, if an attribute appears
   * twice in an attribut list, the last value is stored. */
  nsCOMPtr<nsIPropertyBag> matchData( do_QueryInterface(modLogin, &interfaceok) );
  if (interfaceok != NS_OK) {
    return interfaceok;
  }

  GnomeKeyringResult result;

  AutoAttributeList attributes;
  newLoginInfoAttributes(&attributes);
  appendAttributesFromLogin(oldLogin, attributes);

  // We need the id of the keyring item to set its attributes.
  AutoFoundList foundList;
  result = findLoginItems(attributes, &foundList);
  MGK_GK_CHECK_NS(result);
  PRUint32 i = 0, id;
  for (GList* l = foundList; l != NULL; l = l->next, i++)
  {
    GnomeKeyringFound* found = static_cast<GnomeKeyringFound*>(l->data);
    id = found->item_id;
    if (i >= 1) {
      NS_ERROR("found more than one item to edit");
      return NS_ERROR_FAILURE;
    }
  }

  // set new attributes
  appendAttributesFromBag(matchData.get(), attributes);
  result = gnome_keyring_item_set_attributes_sync(keyringName.get(),
                                                  id,
                                                  attributes);
  MGK_GK_CHECK_NS(result);

  // set new iteminfo, e.g. password
  AutoItemInfo itemInfo;
  result = gnome_keyring_item_get_info_sync(keyringName.get(),
                                            id,
                                            &itemInfo);
  MGK_GK_CHECK_NS(result);
  appendItemInfoFromBag(matchData.get(), itemInfo);
  result = gnome_keyring_item_set_info_sync(keyringName.get(),
                                            id,
                                            itemInfo);
  MGK_GK_CHECK_NS(result);

  return NS_OK;
}