2021-12-05 13:02:22 +01:00
|
|
|
import 'dart:math';
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
2021-11-12 22:13:02 +01:00
|
|
|
import 'package:nc_photos/ci_string.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/string_extension.dart';
|
2021-08-06 19:11:00 +02:00
|
|
|
import 'package:nc_photos/type.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
|
|
|
|
/// Details of a remote Nextcloud server account
|
|
|
|
class Account with EquatableMixin {
|
|
|
|
Account(
|
2021-12-05 13:02:22 +01:00
|
|
|
this.id,
|
2021-04-10 06:28:12 +02:00
|
|
|
this.scheme,
|
|
|
|
String address,
|
|
|
|
this.username,
|
|
|
|
this.password,
|
|
|
|
List<String> roots,
|
2021-09-15 08:58:06 +02:00
|
|
|
) : address = address.trimRightAny("/"),
|
2021-04-10 06:28:12 +02:00
|
|
|
_roots = roots.map((e) => e.trimRightAny("/")).toList() {
|
|
|
|
if (scheme != "http" && scheme != "https") {
|
2021-09-15 08:58:06 +02:00
|
|
|
throw const FormatException("scheme is neither http or https");
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Account copyWith({
|
2021-12-05 13:02:22 +01:00
|
|
|
String? id,
|
2021-07-23 22:05:57 +02:00
|
|
|
String? scheme,
|
|
|
|
String? address,
|
2021-11-12 22:13:02 +01:00
|
|
|
CiString? username,
|
2021-07-23 22:05:57 +02:00
|
|
|
String? password,
|
|
|
|
List<String>? roots,
|
2021-04-10 06:28:12 +02:00
|
|
|
}) {
|
|
|
|
return Account(
|
2021-12-05 13:02:22 +01:00
|
|
|
id ?? this.id,
|
2021-04-10 06:28:12 +02:00
|
|
|
scheme ?? this.scheme,
|
|
|
|
address ?? this.address,
|
|
|
|
username ?? this.username,
|
|
|
|
password ?? this.password,
|
2021-12-19 19:27:40 +01:00
|
|
|
roots ?? List.of(_roots),
|
2021-04-10 06:28:12 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-05 13:02:22 +01:00
|
|
|
static String newId() {
|
|
|
|
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
final random = Random().nextInt(0xFFFFFF);
|
|
|
|
return "${timestamp.toRadixString(16)}-${random.toRadixString(16).padLeft(6, '0')}";
|
|
|
|
}
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
2021-12-05 13:02:22 +01:00
|
|
|
"id: '$id', "
|
2021-04-10 06:28:12 +02:00
|
|
|
"scheme: '$scheme', "
|
2021-09-19 13:02:26 +02:00
|
|
|
"address: '${kDebugMode ? address : "***"}', "
|
|
|
|
"username: '${kDebugMode ? username : "***"}', "
|
2021-07-23 22:05:57 +02:00
|
|
|
"password: '${password.isNotEmpty == true ? (kDebugMode ? password : '***') : null}', "
|
2021-04-10 06:28:12 +02:00
|
|
|
"roots: List {'${roots.join('\', \'')}'}, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
2021-08-06 19:11:00 +02:00
|
|
|
Account.fromJson(JsonObj json)
|
2021-12-05 13:02:22 +01:00
|
|
|
: id = json["id"],
|
|
|
|
scheme = json["scheme"],
|
2021-04-10 06:28:12 +02:00
|
|
|
address = json["address"],
|
2021-11-12 22:13:02 +01:00
|
|
|
username = CiString(json["username"]),
|
2021-04-10 06:28:12 +02:00
|
|
|
password = json["password"],
|
|
|
|
_roots = json["roots"].cast<String>();
|
|
|
|
|
2021-08-06 19:11:00 +02:00
|
|
|
JsonObj toJson() => {
|
2021-12-05 13:02:22 +01:00
|
|
|
"id": id,
|
2021-04-10 06:28:12 +02:00
|
|
|
"scheme": scheme,
|
|
|
|
"address": address,
|
2021-11-12 22:13:02 +01:00
|
|
|
"username": username.toString(),
|
2021-04-10 06:28:12 +02:00
|
|
|
"password": password,
|
|
|
|
"roots": _roots,
|
|
|
|
};
|
|
|
|
|
|
|
|
@override
|
2021-12-05 13:02:22 +01:00
|
|
|
List<Object> get props => [id, scheme, address, username, password, _roots];
|
2021-04-10 06:28:12 +02:00
|
|
|
|
|
|
|
List<String> get roots => _roots;
|
|
|
|
|
2021-12-05 13:02:22 +01:00
|
|
|
final String id;
|
2021-04-10 06:28:12 +02:00
|
|
|
final String scheme;
|
|
|
|
final String address;
|
2021-11-12 22:13:02 +01:00
|
|
|
final CiString username;
|
2021-04-10 06:28:12 +02:00
|
|
|
final String password;
|
|
|
|
final List<String> _roots;
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AccountExtension on Account {
|
|
|
|
String get url => "$scheme://$address";
|
2021-11-21 14:48:30 +01:00
|
|
|
|
|
|
|
/// Compare the server identity of two Accounts
|
|
|
|
///
|
|
|
|
/// Return true if two Accounts point to the same user on server. Be careful
|
|
|
|
/// that this does NOT mean that the two Accounts are identical (e.g., they
|
|
|
|
/// can have difference password)
|
|
|
|
bool compareServerIdentity(Account other) {
|
|
|
|
return scheme == other.scheme &&
|
|
|
|
address == other.address &&
|
|
|
|
username == other.username;
|
|
|
|
}
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|