prompter.cpp
来自「linux subdivision ying gai ke yi le ba」· C++ 代码 · 共 831 行 · 第 1/2 页
CPP
831 行
/**
* @copyright
* ====================================================================
* Copyright (c) 2003 CollabNet. All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://subversion.tigris.org/license-1.html.
* If newer versions of this license are posted there, you may use a
* newer version instead, at your option.
*
* This software consists of voluntary contributions made by many
* individuals. For exact contribution history, see the revision
* history and logs, available at http://subversion.tigris.org/.
* ====================================================================
* @endcopyright
*
* @file Prompter.cpp
* @brief Implementation of the class Prompter
*/
#include "Prompter.h"
#include "Pool.h"
#include "JNIUtil.h"
#include "JNIStringHolder.h"
#include "../include/org_tigris_subversion_javahl_PromptUserPassword2.h"
#include <svn_client.h>
#include "svn_private_config.h"
/**
* Constructor
* @param jprompter a global reference to the java callback object
* @param v2 the callback objects implements PromptUserPassword2
* @param v3 the callback objects implements PromptUserPassword3
*/
Prompter::Prompter(jobject jprompter, bool v2, bool v3)
{
m_prompter = jprompter;
m_version2 = v2;
m_version3 = v3;
}
/**
* Destructor
*/
Prompter::~Prompter()
{
if(m_prompter!= NULL)
{
// since the reference to the java object is a global one, it has to
// be deleted
JNIEnv *env = JNIUtil::getEnv();
env->DeleteGlobalRef(m_prompter);
}
}
/**
* Create a C++ peer object for the java callback object
*
* @param jprompter java callback object
* @return C++ peer object
*/
Prompter *Prompter::makeCPrompter(jobject jprompter)
{
if(jprompter == NULL) // if we have no java object -> we need no C++ object
{
return NULL;
}
JNIEnv *env = JNIUtil::getEnv();
// sanity check that the java object implements PromptUserPassword
jclass clazz = env->FindClass(JAVA_PACKAGE"/PromptUserPassword");
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
if(!env->IsInstanceOf(jprompter, clazz))
{
env->DeleteLocalRef(clazz);
return NULL;
}
env->DeleteLocalRef(clazz);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
// check if PromptUserPassword2 is implemented by the java object
jclass clazz2 = env->FindClass(JAVA_PACKAGE"/PromptUserPassword2");
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
bool v2 = env->IsInstanceOf(jprompter, clazz2) ? true: false;
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
env->DeleteLocalRef(clazz2);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
bool v3 = false;
if(v2)
{
// check if PromptUserPassword3 is implemented by the java object
jclass clazz3 = env->FindClass(JAVA_PACKAGE"/PromptUserPassword3");
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
v3 = env->IsInstanceOf(jprompter, clazz3) ? true: false;
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
env->DeleteLocalRef(clazz3);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
}
// create a new global ref for the java object, because it is longer used
// that this call
jobject myPrompt = env->NewGlobalRef(jprompter);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
// create the C++ peer
return new Prompter(myPrompt, v2, v3);
}
/**
* Retrieve the username from the java object
* @return java string for the username or NULL
*/
jstring Prompter::username()
{
JNIEnv *env = JNIUtil::getEnv();
// the method id will not change during
// the time this library is loaded, so
// it can be cached.
static jmethodID mid = 0;
if(mid == 0)
{
jclass clazz = env->FindClass(JAVA_PACKAGE"/PromptUserPassword");
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
mid = env->GetMethodID(clazz, "getUsername", "()Ljava/lang/String;");
if(JNIUtil::isJavaExceptionThrown() || mid == 0)
{
return NULL;
}
env->DeleteLocalRef(clazz);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
}
jstring ret = static_cast<jstring>(env->CallObjectMethod(m_prompter, mid));
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
return ret;
}
/**
* Retrieve the username from the java object
* @return java string for the username or NULL
*/
jstring Prompter::password()
{
JNIEnv *env = JNIUtil::getEnv();
// the method id will not change during
// the time this library is loaded, so
// it can be cached.
static jmethodID mid = 0;
if(mid == 0)
{
jclass clazz = env->FindClass(JAVA_PACKAGE"/PromptUserPassword");
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
mid = env->GetMethodID(clazz, "getPassword", "()Ljava/lang/String;");
if(JNIUtil::isJavaExceptionThrown() || mid == 0)
{
return NULL;
}
env->DeleteLocalRef(clazz);
if(JNIUtil::isJavaExceptionThrown())
{
return false;
}
}
jstring ret = static_cast<jstring>(env->CallObjectMethod(m_prompter, mid));
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
return ret;
}
/**
* Ask the user a question, which can be answered by yes/no.
* @param realm the server realm, for which this question is asked
* @param question the question to ask the user
* @param yesIsDefault flag if the yes-button should be the default button
* @return flag who the user answered the question
*/
bool Prompter::askYesNo(const char *realm, const char *question,
bool yesIsDefault)
{
JNIEnv *env = JNIUtil::getEnv();
// the method id will not change during
// the time this library is loaded, so
// it can be cached.
static jmethodID mid = 0;
if(mid == 0)
{
jclass clazz = env->FindClass(JAVA_PACKAGE"/PromptUserPassword");
if(JNIUtil::isJavaExceptionThrown())
{
return false;
}
mid = env->GetMethodID(clazz, "askYesNo",
"(Ljava/lang/String;Ljava/lang/String;Z)Z");
if(JNIUtil::isJavaExceptionThrown() || mid == 0)
{
return false;
}
env->DeleteLocalRef(clazz);
if(JNIUtil::isJavaExceptionThrown())
{
return false;
}
}
// convert the texts to java strings
jstring jrealm = JNIUtil::makeJString(realm);
if(JNIUtil::isJavaExceptionThrown())
{
return false;
}
jstring jquestion = JNIUtil::makeJString(question);
if(JNIUtil::isJavaExceptionThrown())
{
return false;
}
// execute the callback
jboolean ret = env->CallBooleanMethod(m_prompter, mid, jrealm, jquestion,
yesIsDefault ? JNI_TRUE : JNI_FALSE);
if(JNIUtil::isJavaExceptionThrown())
{
return false;
}
// delete the java strings
env->DeleteLocalRef(jquestion);
if(JNIUtil::isJavaExceptionThrown())
{
return false;
}
env->DeleteLocalRef(jrealm);
if(JNIUtil::isJavaExceptionThrown())
{
return false;
}
return ret ? true:false;
}
/**
*
*/
const char *Prompter::askQuestion(const char *realm, const char *question,
bool showAnswer, bool maySave)
{
JNIEnv *env = JNIUtil::getEnv();
if(m_version3)
{
static jmethodID mid = 0;
static jmethodID mid2 = 0;
if(mid == 0)
{
jclass clazz = env->FindClass(JAVA_PACKAGE"/PromptUserPassword3");
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
mid = env->GetMethodID(clazz, "askQuestion",
"(Ljava/lang/String;Ljava/lang/String;ZZ)Ljava/lang/String;");
if(JNIUtil::isJavaExceptionThrown() || mid == 0)
{
return NULL;
}
mid2 = env->GetMethodID(clazz, "userAllowedSave", "()Z");
if(JNIUtil::isJavaExceptionThrown() || mid == 0)
{
return NULL;
}
env->DeleteLocalRef(clazz);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
}
jstring jrealm = JNIUtil::makeJString(realm);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
jstring jquestion = JNIUtil::makeJString(question);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
jstring janswer = static_cast<jstring>(
env->CallObjectMethod(m_prompter, mid, jrealm, jquestion,
showAnswer ? JNI_TRUE : JNI_FALSE,
maySave ? JNI_TRUE : JNI_FALSE));
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
env->DeleteLocalRef(jquestion);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
env->DeleteLocalRef(jrealm);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
JNIStringHolder answer(janswer);
if(answer != NULL)
{
m_answer = answer;
m_maySave = env->CallBooleanMethod(m_prompter, mid2) ? true: false;
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
}
else
{
m_answer = "";
m_maySave = false;
}
return m_answer.c_str();
}
else
{
static jmethodID mid = 0;
if(mid == 0)
{
jclass clazz = env->FindClass(JAVA_PACKAGE"/PromptUserPassword");
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
mid = env->GetMethodID(clazz, "askQuestion",
"(Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;");
if(JNIUtil::isJavaExceptionThrown() || mid == 0)
{
return NULL;
}
env->DeleteLocalRef(clazz);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
}
jstring jrealm = JNIUtil::makeJString(realm);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
jstring jquestion = JNIUtil::makeJString(question);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
jstring janswer = static_cast<jstring>(
env->CallObjectMethod(m_prompter, mid, jrealm, jquestion,
showAnswer ? JNI_TRUE : JNI_FALSE));
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
env->DeleteLocalRef(jquestion);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
env->DeleteLocalRef(jrealm);
if(JNIUtil::isJavaExceptionThrown())
{
return NULL;
}
JNIStringHolder answer(janswer);
if(answer != NULL)
{
m_answer = answer;
if(maySave)
m_maySave = askYesNo(realm, _("May save the answer ?"), true);
else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?