Skip to content

Commit

Permalink
high level API for creating and deleting mailboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-virkus committed Jun 10, 2021
1 parent 6d79fee commit d04076e
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/mail/mail_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,33 @@ class MailClient {
// }
return _outgoingMailClient.supports8BitEncoding();
}

/// Checks if this mail client supports different mailboxes
bool get supportsMailboxes => _incomingMailClient.supportsMailboxes;

/// Creates a new mailbox with the given [mailboxName].
///
/// Specify a [parentMailbox] in case the mailbox should not be created in the root.
Future<Mailbox> createMailbox(String mailboxName,
{Mailbox? parentMailbox}) async {
if (!supportsMailboxes) {
throw MailException(
this, 'Mailboxes are not supported, check "supportsMailboxes" first');
}
final box = await _incomingMailClient.createMailbox(mailboxName,
parentMailbox: parentMailbox);
_mailboxes?.add(box);
return box;
}

Future<void> deleteMailbox(Mailbox mailbox) async {
if (!supportsMailboxes) {
throw MailException(
this, 'Mailboxes are not supported, check "supportsMailboxes" first');
}
await _incomingMailClient.deleteMailbox(mailbox);
_mailboxes?.remove(mailbox);
}
}

/// Defines the thread fetching preference
Expand Down Expand Up @@ -971,6 +998,8 @@ abstract class _IncomingMailClient {

bool get supportsThreading;

bool get supportsMailboxes;

Future<void> connect();

Future disconnect();
Expand Down Expand Up @@ -1053,6 +1082,10 @@ abstract class _IncomingMailClient {

Future<ThreadDataResult> fetchThreadData(
Mailbox mailbox, DateTime since, bool setThreadSequences);

Future<Mailbox> createMailbox(String mailboxName, {Mailbox? parentMailbox});

Future<void> deleteMailbox(Mailbox mailbox);
}

class _IncomingImapClient extends _IncomingMailClient {
Expand All @@ -1070,6 +1103,8 @@ class _IncomingImapClient extends _IncomingMailClient {
int _reconnectCounter = 0;
bool _isIdlePaused = false;
ThreadDataResult? _threadData;
@override
bool get supportsMailboxes => true;

_IncomingImapClient(
int? downloadSizeLimit,
Expand Down Expand Up @@ -2005,6 +2040,34 @@ class _IncomingImapClient extends _IncomingMailClient {
await _resumeIdle();
}
}

@override
Future<Mailbox> createMailbox(String mailboxName,
{Mailbox? parentMailbox}) async {
final path = (parentMailbox != null)
? parentMailbox.encodedPath + parentMailbox.pathSeparator + mailboxName
: mailboxName;
try {
await _pauseIdle();
return await _imapClient.createMailbox(path);
} on ImapException catch (e, s) {
throw MailException.fromImap(mailClient, e, s);
} finally {
await _resumeIdle();
}
}

@override
Future<void> deleteMailbox(Mailbox mailbox) async {
try {
await _pauseIdle();
await _imapClient.deleteMailbox(mailbox);
} on ImapException catch (e, s) {
throw MailException.fromImap(mailClient, e, s);
} finally {
await _resumeIdle();
}
}
}

class _IncomingPopClient extends _IncomingMailClient {
Expand Down Expand Up @@ -2244,6 +2307,21 @@ class _IncomingPopClient extends _IncomingMailClient {
// TODO: implement fetchThreadData
throw UnimplementedError();
}

@override
Future<Mailbox> createMailbox(String mailboxName, {Mailbox? parentMailbox}) {
// TODO: implement createMailbox
throw UnimplementedError();
}

@override
bool get supportsMailboxes => false;

@override
Future<void> deleteMailbox(Mailbox mailbox) {
// TODO: implement deleteMailbox
throw UnimplementedError();
}
}

abstract class _OutgoingMailClient {
Expand Down

0 comments on commit d04076e

Please sign in to comment.