50 lines
1.6 KiB
Dart
50 lines
1.6 KiB
Dart
import 'package:worldhopper/models/opds_server.dart';
|
|
import 'package:worldhopper/services/opds_service.dart';
|
|
import 'package:worldhopper/services/server_handlers/kavita_handler.dart';
|
|
import 'package:worldhopper/services/server_handlers/opds_server_handler.dart';
|
|
|
|
/// Detects the software running behind an OPDS server.
|
|
///
|
|
/// Fetches the root feed once and iterates all registered handlers,
|
|
/// returning the identifier of the first match (or null).
|
|
class ServerSoftwareDetector {
|
|
static final List<OPDSServerHandler> handlers = [
|
|
KavitaHandler(),
|
|
];
|
|
|
|
/// Attempt to detect the server software for [server].
|
|
///
|
|
/// Returns the handler identifier (e.g. "kavita") on match, or null.
|
|
Future<String?> detect(OPDSServer server) async {
|
|
try {
|
|
final parsedFeed = await OPDSService().fetchRootFeed(server);
|
|
final feed = parsedFeed.feed;
|
|
|
|
for (final handler in handlers) {
|
|
if (await handler.detect(server, feed)) {
|
|
return handler.identifier;
|
|
}
|
|
}
|
|
} catch (_) {
|
|
// Detection is best-effort; return null on any failure.
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Get the human-readable display name for a handler identifier.
|
|
static String? displayName(String identifier) {
|
|
for (final handler in handlers) {
|
|
if (handler.identifier == identifier) {
|
|
return handler.displayName;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// All known server software types for use in UI dropdowns.
|
|
static List<({String identifier, String displayName})> get knownTypes {
|
|
return handlers
|
|
.map((h) => (identifier: h.identifier, displayName: h.displayName))
|
|
.toList();
|
|
}
|
|
}
|