mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 12:56:30 +03:00
make accounts sortable
This commit is contained in:
@@ -3119,6 +3119,18 @@ dc_context_t* dc_accounts_get_selected_account (dc_accounts_t* accounts);
|
|||||||
int dc_accounts_select_account (dc_accounts_t* accounts, uint32_t account_id);
|
int dc_accounts_select_account (dc_accounts_t* accounts, uint32_t account_id);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move an account and change the order returned by dc_accounts_get_all().
|
||||||
|
*
|
||||||
|
* @param accounts
|
||||||
|
* @param to_move_id The account ID to move.
|
||||||
|
* @param predecessor_id `to_move_id` will be sorted below `predecessor_id`.
|
||||||
|
* Set to 0 to move `to_move_id` to the top of the list returned by dc_accounts_get_all().
|
||||||
|
* @return 1=success, 0=error
|
||||||
|
*/
|
||||||
|
int dc_accounts_move_below (dc_accounts_t* accounts, uint32_t to_move_id, uint32_t predecessor_id);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start job and IMAP/SMTP tasks for all accounts managed by the account manager.
|
* Start job and IMAP/SMTP tasks for all accounts managed by the account manager.
|
||||||
* If IO is already running, nothing happens.
|
* If IO is already running, nothing happens.
|
||||||
|
|||||||
@@ -4703,6 +4703,35 @@ pub unsafe extern "C" fn dc_accounts_select_account(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn dc_accounts_move_below(
|
||||||
|
accounts: *mut dc_accounts_t,
|
||||||
|
to_move_id: u32,
|
||||||
|
predecessor_id: u32,
|
||||||
|
) -> libc::c_int {
|
||||||
|
if accounts.is_null() {
|
||||||
|
eprintln!("ignoring careless call to dc_accounts_move_below()");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let accounts = &*accounts;
|
||||||
|
let predecessor_id = if predecessor_id == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(predecessor_id)
|
||||||
|
};
|
||||||
|
block_on(async move {
|
||||||
|
let mut accounts = accounts.write().await;
|
||||||
|
match accounts.move_below(to_move_id, predecessor_id).await {
|
||||||
|
Ok(()) => 1,
|
||||||
|
Err(err) => {
|
||||||
|
accounts.emit_event(EventType::Error(format!("Failed to move account: {err:#}")));
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn dc_accounts_add_account(accounts: *mut dc_accounts_t) -> u32 {
|
pub unsafe extern "C" fn dc_accounts_add_account(accounts: *mut dc_accounts_t) -> u32 {
|
||||||
if accounts.is_null() {
|
if accounts.is_null() {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
//! # Account manager module.
|
//! # Account manager module.
|
||||||
|
|
||||||
|
use std::cmp::Ordering;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
@@ -119,6 +120,12 @@ impl Accounts {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Move an account and change the ordering returned by get_all().
|
||||||
|
pub async fn move_below(&mut self, _to_move_id: u32, _predecessor_id: Option<u32>) -> Result<()> {
|
||||||
|
// TODO: change priority of affected accounts
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Adds a new account and opens it.
|
/// Adds a new account and opens it.
|
||||||
///
|
///
|
||||||
/// Returns account ID.
|
/// Returns account ID.
|
||||||
@@ -261,9 +268,19 @@ impl Accounts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sort_accounts(a: &(&u32, &Context), b: &(&u32, &Context)) -> std::cmp::Ordering {
|
||||||
|
match a.1.sort_number.cmp(&b.1.sort_number) {
|
||||||
|
Ordering::Less => Ordering::Less,
|
||||||
|
Ordering::Equal => a.1.id.cmp(&b.1.id),
|
||||||
|
Ordering::Greater => Ordering::Greater,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a list of all account ids.
|
/// Get a list of all account ids.
|
||||||
pub fn get_all(&self) -> Vec<u32> {
|
pub fn get_all(&self) -> Vec<u32> {
|
||||||
self.accounts.keys().copied().collect()
|
let mut accounts_vec: Vec<_> = self.accounts.iter().collect();
|
||||||
|
accounts_vec.sort_by(Accounts::sort_accounts);
|
||||||
|
return accounts_vec.into_iter().map(|(&k, _)| k).collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Starts background tasks such as IMAP and SMTP loops for all accounts.
|
/// Starts background tasks such as IMAP and SMTP loops for all accounts.
|
||||||
|
|||||||
@@ -272,6 +272,9 @@ pub struct InnerContext {
|
|||||||
|
|
||||||
creation_time: tools::Time,
|
creation_time: tools::Time,
|
||||||
|
|
||||||
|
/// Accounts with higher numbers are returned at beginning of get_all().
|
||||||
|
pub(crate) sort_number: u32,
|
||||||
|
|
||||||
/// The text of the last error logged and emitted as an event.
|
/// The text of the last error logged and emitted as an event.
|
||||||
/// If the ui wants to display an error after a failure,
|
/// If the ui wants to display an error after a failure,
|
||||||
/// `last_error` should be used to avoid races with the event thread.
|
/// `last_error` should be used to avoid races with the event thread.
|
||||||
@@ -427,6 +430,7 @@ impl Context {
|
|||||||
|
|
||||||
let inner = InnerContext {
|
let inner = InnerContext {
|
||||||
id,
|
id,
|
||||||
|
sort_number: 0,
|
||||||
blobdir,
|
blobdir,
|
||||||
running_state: RwLock::new(Default::default()),
|
running_state: RwLock::new(Default::default()),
|
||||||
sql: Sql::new(dbfile),
|
sql: Sql::new(dbfile),
|
||||||
|
|||||||
Reference in New Issue
Block a user