using AIStudio.Dialogs; using AIStudio.Settings; using AIStudio.Settings.DataModel; using AIStudio.Tools.PluginSystem; using AIStudio.Tools.Services; using DialogOptions = AIStudio.Dialogs.DialogOptions; namespace AIStudio.Tools; /// /// Opens the settings of a mailbox, and stores what the user changed in them. /// /// /// Kept here rather than in the places which offer it -- the data source table and the background /// embeddings page, which leads to it from a refused sign-in -- so that saving a mailbox works the /// same way everywhere: the settings are stored, and the mailbox is synced right after. /// public static class MailboxEditing { private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(MailboxEditing).Namespace, nameof(MailboxEditing)); /// /// Lets the user edit a mailbox, and stores and syncs it when they save. /// /// /// The caller tells the rest of the app that the configuration changed, as the sender of that message. /// /// The dialog service to show the settings with. /// The settings, which hold the mailbox. /// The service which syncs the mailbox. /// The id of the mailbox. /// True when the user saved the mailbox. False when they cancelled, or the mailbox is gone or managed by the organization. public static async Task EditAsync(IDialogService dialogService, SettingsManager settingsManager, DataSourceEmbeddingService embeddingService, string mailboxId) { var mailboxes = settingsManager.ConfigurationData.Mailboxes; var position = mailboxes.FindIndex(mailbox => mailbox.Id == mailboxId); if (position < 0 || mailboxes[position].IsEnterpriseConfiguration) return false; var storedMailbox = mailboxes[position]; var availableEmbeddings = settingsManager.GetAllEmbeddingProviders() .Select(provider => new ConfigurationSelectData(provider.Name, provider.Id)) .ToList(); var dialogParameters = new DialogParameters { { x => x.IsEditing, true }, { x => x.DataSource, storedMailbox }, { x => x.LockSource, await embeddingService.ShouldLockDataSourceOriginAsync(storedMailbox.Id) }, { x => x.AvailableEmbeddings, availableEmbeddings } }; var dialogReference = await dialogService.ShowAsync(TB("Edit Mailbox"), dialogParameters, DialogOptions.FULLSCREEN); var dialogResult = await dialogReference.Result; if (dialogResult is null || dialogResult.Canceled) return false; // Found anew, since the list may have changed while the dialog was open: position = mailboxes.IndexOf(storedMailbox); if (position < 0) return false; var editedMailbox = (DataSourceMailbox)dialogResult.Data!; mailboxes[position] = editedMailbox; await settingsManager.StoreSettings(); await embeddingService.QueueDataSourceAsync(editedMailbox); return true; } }