2024-09-23 00:26:11 +02:00
|
|
|
use jni_toolbox::jni;
|
|
|
|
use crate::{api::Config, client::Client, errors::{ConnectionError, RemoteError}, Workspace};
|
2024-09-22 02:22:51 +02:00
|
|
|
|
2024-09-23 00:26:11 +02:00
|
|
|
/// Connect using the given credentials to the default server, and return a [Client] to interact with it.
|
2024-09-22 02:22:51 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client", ptr)]
|
|
|
|
fn connect(config: Config) -> Result<Client, ConnectionError> {
|
2024-09-23 00:26:11 +02:00
|
|
|
super::tokio().block_on(Client::connect(config))
|
2024-09-22 02:22:51 +02:00
|
|
|
}
|
|
|
|
|
2024-09-15 02:00:04 +02:00
|
|
|
/// Gets the current [crate::api::User].
|
2024-09-22 02:22:51 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client", ptr)]
|
2024-09-23 18:12:13 +02:00
|
|
|
fn get_user(client: Client) -> crate::api::User {
|
2024-09-15 02:00:04 +02:00
|
|
|
client.user().clone()
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Join a [Workspace] and return a pointer to it.
|
2024-09-23 18:12:13 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
|
|
|
fn join_workspace(client: Client, workspace: String) -> Result<Workspace, ConnectionError> {
|
2024-09-23 00:26:11 +02:00
|
|
|
super::tokio().block_on(client.join_workspace(workspace))
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 00:26:11 +02:00
|
|
|
/// Create a workspace on server, if allowed to.
|
2024-09-22 02:22:51 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
2024-09-23 18:12:13 +02:00
|
|
|
fn create_workspace(client: Client, workspace: String) -> Result<(), RemoteError> {
|
2024-09-23 00:26:11 +02:00
|
|
|
super::tokio().block_on(client.create_workspace(workspace))
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Delete a workspace on server, if allowed to.
|
2024-09-22 02:22:51 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
2024-09-23 18:12:13 +02:00
|
|
|
fn delete_workspace(client: Client, workspace: String) -> Result<(), RemoteError> {
|
2024-09-23 00:26:11 +02:00
|
|
|
super::tokio().block_on(client.delete_workspace(workspace))
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Invite another user to an owned workspace.
|
2024-09-22 02:22:51 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
2024-09-23 18:12:13 +02:00
|
|
|
fn invite_to_workspace(client: Client, workspace: String, user: String) -> Result<(), RemoteError> {
|
2024-09-23 00:26:11 +02:00
|
|
|
super::tokio().block_on(client.invite_to_workspace(workspace, user))
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// List available workspaces.
|
2024-09-23 18:12:13 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
|
|
|
fn list_workspaces(client: Client, owned: bool, invited: bool) -> Result<Vec<String>, RemoteError> {
|
2024-09-23 00:26:11 +02:00
|
|
|
super::tokio().block_on(client.list_workspaces(owned, invited))
|
2024-09-15 02:00:04 +02:00
|
|
|
}
|
2024-08-27 23:04:56 +02:00
|
|
|
|
2024-09-15 02:00:04 +02:00
|
|
|
/// List available workspaces.
|
2024-09-23 18:12:13 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
|
|
|
fn active_workspaces(client: Client) -> Vec<String> {
|
2024-09-22 02:22:51 +02:00
|
|
|
client.active_workspaces()
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
2024-08-07 10:22:01 +02:00
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Leave a [Workspace] and return whether or not the client was in such workspace.
|
2024-09-22 02:22:51 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
2024-09-23 18:12:13 +02:00
|
|
|
fn leave_workspace(client: Client, workspace: String) -> bool {
|
2024-09-22 02:22:51 +02:00
|
|
|
client.leave_workspace(&workspace)
|
2024-08-08 02:45:52 +02:00
|
|
|
}
|
2024-08-27 23:04:56 +02:00
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Get a [Workspace] by name and returns a pointer to it.
|
2024-09-23 18:12:13 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
|
|
|
fn get_workspace(client: Client, workspace: String) -> Option<Workspace> {
|
2024-09-22 02:22:51 +02:00
|
|
|
client.get_workspace(&workspace)
|
2024-08-07 10:22:01 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Refresh the client's session token.
|
2024-09-22 02:22:51 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
2024-09-23 18:12:13 +02:00
|
|
|
fn refresh(client: Client) -> Result<(), RemoteError> {
|
2024-09-23 00:26:11 +02:00
|
|
|
super::tokio().block_on(client.refresh())
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Called by the Java GC to drop a [Client].
|
2024-09-23 00:26:11 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client")]
|
|
|
|
fn free(input: jni::sys::jlong) {
|
2024-08-08 00:29:54 +02:00
|
|
|
let _ = unsafe { Box::from_raw(input as *mut Client) };
|
|
|
|
}
|