2025-08-08 10:36:23 +05:00
|
|
|
import 'package:dartssh2/dartssh2.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2025-08-10 07:10:30 +05:00
|
|
|
import 'package:fluxcloud/main.dart';
|
2025-08-14 07:07:50 +05:00
|
|
|
import 'package:fluxcloud/sftp_provider.dart';
|
2025-08-13 09:58:47 +05:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2025-08-14 07:07:50 +05:00
|
|
|
import 'package:provider/provider.dart';
|
2025-08-08 10:36:23 +05:00
|
|
|
|
|
|
|
class OperationButtons extends StatelessWidget {
|
|
|
|
const OperationButtons({
|
2025-08-14 07:07:50 +05:00
|
|
|
super.key, required this.dirEntries,
|
2025-08-08 10:36:23 +05:00
|
|
|
});
|
|
|
|
|
2025-08-10 07:08:14 +05:00
|
|
|
final List<SftpName> dirEntries;
|
2025-08-08 10:36:23 +05:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2025-08-14 07:07:50 +05:00
|
|
|
final sftpProvider = context.read<SftpProvider>();
|
2025-08-08 10:36:23 +05:00
|
|
|
return Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
|
|
|
|
},
|
|
|
|
icon: Icon(Icons.drive_file_move)
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
|
|
|
|
},
|
|
|
|
icon: Icon(Icons.copy)
|
|
|
|
),
|
2025-08-10 07:08:14 +05:00
|
|
|
if (dirEntries.length == 1)
|
2025-08-08 10:36:23 +05:00
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
2025-08-10 07:08:14 +05:00
|
|
|
final dirEntry = dirEntries[0];
|
2025-08-08 10:36:23 +05:00
|
|
|
final newNameController = TextEditingController(text: dirEntry.filename);
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
title: Text('Rename'),
|
|
|
|
content: TextField(
|
|
|
|
controller: newNameController,
|
|
|
|
autofocus: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'Enter new name'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
2025-08-10 07:16:13 +05:00
|
|
|
try {
|
2025-08-14 07:07:50 +05:00
|
|
|
await sftpProvider.sftpWorker.rename('${sftpProvider.path}${dirEntry.filename}', '${sftpProvider.path}${newNameController.text}');
|
|
|
|
sftpProvider.listDir();
|
2025-08-10 07:16:13 +05:00
|
|
|
}
|
|
|
|
on SftpStatusError catch (e) {
|
|
|
|
if (context.mounted) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (context.mounted) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
|
2025-08-08 10:36:23 +05:00
|
|
|
},
|
|
|
|
child: Text('Rename')
|
|
|
|
),
|
|
|
|
|
|
|
|
],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
icon: Icon(Icons.drive_file_rename_outline)
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
2025-08-10 07:08:14 +05:00
|
|
|
builder: (context) {
|
|
|
|
String warningText = 'This action cannot be undone';
|
|
|
|
if (dirEntries.length == 1 && dirEntries[0].attr.isDirectory) {
|
|
|
|
warningText = 'The contents of this folder will be deleted as well\n$warningText';
|
|
|
|
}
|
|
|
|
else if (dirEntries.length > 1) {
|
|
|
|
warningText = 'All selected files will be deleted\n$warningText';
|
|
|
|
}
|
|
|
|
return AlertDialog(
|
|
|
|
title: Text('Delete Permanently?'),
|
|
|
|
content: Text(warningText),
|
|
|
|
actions: [
|
|
|
|
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
|
|
|
for (final dirEntry in dirEntries) {
|
|
|
|
try {
|
2025-08-14 07:07:50 +05:00
|
|
|
await sftpProvider.sftpWorker.remove(dirEntry, sftpProvider.path);
|
2025-08-10 07:08:14 +05:00
|
|
|
}
|
|
|
|
catch (e) {
|
2025-08-10 07:10:30 +05:00
|
|
|
if (context.mounted) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
|
|
|
|
}
|
2025-08-10 07:08:14 +05:00
|
|
|
}
|
2025-08-14 07:07:50 +05:00
|
|
|
sftpProvider.listDir();
|
2025-08-10 07:08:14 +05:00
|
|
|
if (context.mounted) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: Text('Yes')
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2025-08-08 10:36:23 +05:00
|
|
|
);
|
|
|
|
},
|
|
|
|
icon: Icon(Icons.delete)
|
|
|
|
),
|
2025-08-13 09:58:47 +05:00
|
|
|
IconButton(
|
|
|
|
onPressed: () async {
|
|
|
|
final downloadsDir = await getDownloadsDirectory();
|
|
|
|
if (downloadsDir == null) return;
|
2025-08-14 06:22:05 +05:00
|
|
|
for (final dirEntry in dirEntries) {
|
|
|
|
try {
|
2025-08-14 07:07:50 +05:00
|
|
|
await for (final progress in sftpProvider.sftpWorker.downloadFile(dirEntry, sftpProvider.path, downloadsDir.path)) {
|
|
|
|
sftpProvider.setDownloadProgress(progress);
|
2025-08-14 06:22:05 +05:00
|
|
|
}
|
2025-08-13 09:58:47 +05:00
|
|
|
}
|
2025-08-14 06:22:05 +05:00
|
|
|
catch (e) {
|
|
|
|
if (context.mounted) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
|
|
|
|
}
|
2025-08-13 09:58:47 +05:00
|
|
|
}
|
|
|
|
}
|
2025-08-14 07:07:50 +05:00
|
|
|
sftpProvider.setDownloadProgress(null);
|
2025-08-13 09:58:47 +05:00
|
|
|
},
|
|
|
|
icon: Icon(Icons.download)
|
|
|
|
)
|
2025-08-08 10:36:23 +05:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|