Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions deltachat-jsonrpc/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ impl CommandApi {
self.accounts.read().await.get_selected_account_id()
}

/// Set the order of accounts.
/// The provided list should contain all account IDs in the desired order.
/// If an account ID is missing from the list, it will be appended at the end.
/// If the list contains non-existent account IDs, they will be ignored.
async fn set_accounts_order(&self, order: Vec<u32>) -> Result<()> {
self.accounts.write().await.set_accounts_order(order).await
}

/// Get a list of all configured accounts.
async fn get_all_accounts(&self) -> Result<Vec<Account>> {
let mut accounts = Vec::new();
Expand Down
61 changes: 58 additions & 3 deletions src/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! # Account manager module.

use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use std::future::Future;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -270,9 +270,51 @@ impl Accounts {
}
}

/// Get a list of all account ids.
/// Gets a list of all account ids in the user-configured order.
pub fn get_all(&self) -> Vec<u32> {
self.accounts.keys().copied().collect()
let mut ordered_ids = Vec::new();
let mut all_ids: BTreeSet<u32> = self.accounts.keys().copied().collect();

// First, add accounts in the configured order
for &id in &self.config.inner.accounts_order {
if all_ids.remove(&id) {
ordered_ids.push(id);
}
}

// Then add any accounts not in the order list (newly added accounts)
for id in all_ids {
ordered_ids.push(id);
}

ordered_ids
}

/// Sets the order of accounts.
///
/// The provided list should contain all account IDs in the desired order.
/// If an account ID is missing from the list, it will be appended at the end.
/// If the list contains non-existent account IDs, they will be ignored.
pub async fn set_accounts_order(&mut self, order: Vec<u32>) -> Result<()> {
let existing_ids: BTreeSet<u32> = self.accounts.keys().copied().collect();

// Filter out non-existent account IDs
let mut filtered_order: Vec<u32> = order
.into_iter()
.filter(|id| existing_ids.contains(id))
.collect();

// Add any missing account IDs at the end
for &id in &existing_ids {
if !filtered_order.contains(&id) {
filtered_order.push(id);
}
}

self.config.inner.accounts_order = filtered_order;
self.config.sync().await?;
self.emit_event(EventType::AccountsChanged);
Ok(())
}

/// Starts background tasks such as IMAP and SMTP loops for all accounts.
Expand Down Expand Up @@ -415,6 +457,10 @@ struct InnerConfig {
pub selected_account: u32,
pub next_id: u32,
pub accounts: Vec<AccountConfig>,
/// Ordered list of account IDs, representing the user's preferred order.
/// If an account ID is not in this list, it will be appended at the end.
#[serde(default)]
pub accounts_order: Vec<u32>,
}

impl Drop for Config {
Expand Down Expand Up @@ -481,6 +527,7 @@ impl Config {
accounts: Vec::new(),
selected_account: 0,
next_id: 1,
accounts_order: Vec::new(),
};
if !lock {
let cfg = Self {
Expand Down Expand Up @@ -613,6 +660,10 @@ impl Config {
uuid,
});
self.inner.next_id += 1;

// Add new account to the end of the order list
self.inner.accounts_order.push(id);

id
};

Expand All @@ -634,6 +685,10 @@ impl Config {
// remove account from the configs
self.inner.accounts.remove(idx);
}

// Remove from order list as well
self.inner.accounts_order.retain(|&x| x != id);

if self.inner.selected_account == id {
// reset selected account
self.inner.selected_account = self
Expand Down
Loading