Skip to content

Commit ef8e208

Browse files
committed
Manage file deletion.
1 parent df0bfae commit ef8e208

6 files changed

Lines changed: 170 additions & 3 deletions

File tree

app/src/main/java/com/paulds/simpleftp/data/repository/FileRepository.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ public void createFolder(String path, String name) {
5454
}
5555
}
5656

57+
/**
58+
* Delete a list of files and directories.
59+
* @param elements The specified list.
60+
*/
61+
public void deleteFiles(List<FileEntity> elements) {
62+
for (FileEntity element: elements) {
63+
File file = new File(element.getPath());
64+
65+
if(file.exists()) {
66+
file.delete();
67+
}
68+
}
69+
}
70+
5771
/**
5872
* Indicates whether the storage is writable.
5973
* @return A value indicating whether the storage is writable.

app/src/main/java/com/paulds/simpleftp/data/repository/FtpRepository.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.io.File;
99
import java.io.IOException;
1010
import java.util.ArrayList;
11+
import java.util.Dictionary;
12+
import java.util.Enumeration;
13+
import java.util.Hashtable;
1114
import java.util.List;
1215

1316
import it.sauronsoftware.ftp4j.FTPAbortedException;
@@ -24,16 +27,18 @@
2427
* @author Paul-DS
2528
*/
2629
public class FtpRepository {
30+
/**
31+
* File containing open FTP connections.
32+
*/
33+
private Hashtable<Integer, FTPClient> openConnections = new Hashtable<Integer, FTPClient>();
2734

2835
/**
2936
* Lists the files present at the specified path.
3037
* @param path The specified path.
3138
* @return The list of files.
3239
*/
3340
public List<FileEntity> listFiles(FtpServer server, String path) throws FTPException, IOException, FTPIllegalReplyException, FTPAbortedException, FTPDataTransferException, FTPListParseException {
34-
FTPClient client = new FTPClient();
35-
client.connect(server.getHost(), server.getPort());
36-
client.login(server.isAnonymous() ? "anonymous" : server.getLogin(), server.getPassword());
41+
FTPClient client = this.getConnection(server);
3742

3843
if(path != null && !path.equals("/")) {
3944
client.changeDirectory(path);
@@ -68,6 +73,49 @@ public void createFolder(String path, String name) {
6873
}
6974
}
7075

76+
/**
77+
* Delete a list of files and directories.
78+
* @param server The FTP server.
79+
* @param elements The specified list.
80+
*/
81+
public void deleteFiles(FtpServer server, List<FileEntity> elements) throws FTPException, IOException, FTPIllegalReplyException {
82+
FTPClient client = this.getConnection(server);
83+
84+
for (FileEntity element: elements) {
85+
if(element.isDirectory()) {
86+
client.deleteDirectory(element.getPath());
87+
}
88+
else {
89+
client.deleteFile(element.getPath());
90+
}
91+
}
92+
}
93+
94+
/**
95+
* Gets a FTP connection for a given server.
96+
* @param server The server to connect.
97+
* @return The FTP connection.
98+
*/
99+
private FTPClient getConnection(FtpServer server) throws FTPException, IOException, FTPIllegalReplyException {
100+
FTPClient client = openConnections.get(server.getId());
101+
102+
if(client == null)
103+
{
104+
client = new FTPClient();
105+
openConnections.put(server.getId(), client);
106+
}
107+
108+
if(!client.isConnected()) {
109+
client.connect(server.getHost(), server.getPort());
110+
}
111+
112+
if(!client.isAuthenticated()) {
113+
client.login(server.isAnonymous() ? "anonymous" : server.getLogin(), server.getPassword());
114+
}
115+
116+
return client;
117+
}
118+
71119
/**
72120
* Convert a FTP file to a file entity.
73121
* @param file The FTP file to convert.

app/src/main/java/com/paulds/simpleftp/presentation/model/ExplorerViewModel.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,75 @@ public void clearSelection(View view) {
276276

277277
this.isSelectionMode.set(false);
278278
}
279+
280+
/**
281+
* Delete the selection.
282+
* @param view The current view.
283+
*/
284+
public void deleteSelection(View view) {
285+
final List<FileEntity> filesToRemove = new ArrayList<FileEntity>();
286+
287+
for (FileViewModel file: files) {
288+
if(file.isSelected.get()) {
289+
filesToRemove.add(file.toEntity());
290+
}
291+
}
292+
293+
this.clearSelection(view);
294+
295+
final Handler handler = new Handler();
296+
final ExplorerViewModel instance = this;
297+
298+
AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
299+
300+
builder.setMessage(String.format(this.context.getString(R.string.dialog_delete_files_message), filesToRemove.size()));
301+
302+
builder.setPositiveButton(R.string.dialog_delete_files_positive_button, new DialogInterface.OnClickListener() {
303+
@Override
304+
public void onClick(DialogInterface dialog, int which) {
305+
isLoading.set(true);
306+
307+
Thread loadingThread = new Thread() {
308+
@Override
309+
public void run() {
310+
try {
311+
if(server != null) {
312+
AndroidApplication.getRepository().getFtpRepository().deleteFiles(server, filesToRemove);
313+
}
314+
else {
315+
AndroidApplication.getRepository().getFileRepository().deleteFiles(filesToRemove);
316+
}
317+
} catch (FTPException e) {
318+
e.printStackTrace();
319+
} catch (IOException e) {
320+
e.printStackTrace();
321+
} catch (FTPIllegalReplyException e) {
322+
e.printStackTrace();
323+
}
324+
325+
final List<FileViewModel> newList = new ArrayList<FileViewModel>();
326+
327+
handler.post(new Runnable() {
328+
@Override
329+
public void run() {
330+
changeDirectory(path);
331+
}
332+
});
333+
}
334+
};
335+
336+
loadingThread.start();
337+
}
338+
});
339+
340+
builder.setNegativeButton(R.string.dialog_delete_files_negative_button, new DialogInterface.OnClickListener() {
341+
@Override
342+
public void onClick(DialogInterface dialog, int which) {
343+
dialog.cancel();
344+
}
345+
});
346+
347+
AlertDialog dialog = builder.create();
348+
dialog.show();
349+
}
279350
}

app/src/main/java/com/paulds/simpleftp/presentation/model/FileViewModel.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,24 @@ public ExplorerViewModel getMainModel() {
189189
return this.mainViewModel;
190190
}
191191

192+
/**
193+
* Convert the file view model into a file entity.
194+
* @return The converted file entity.
195+
*/
196+
public FileEntity toEntity() {
197+
FileEntity result = new FileEntity();
198+
199+
result.setName(this.name);
200+
result.setPath(this.path);
201+
result.setIsDirectory(this.isDirectory);
202+
203+
if(!this.isDirectory) {
204+
result.setSize(this.size);
205+
}
206+
207+
return result;
208+
}
209+
192210
/**
193211
* Called when the file item is clicked.
194212
* @param view The current view.

app/src/main/res/layout/fragment_explorer.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@
9494
android:textColor="@color/selection_toolbar_text"
9595
android:textSize="@dimen/toolbar_title_text_size" />
9696

97+
<ImageView
98+
android:layout_width="wrap_content"
99+
android:layout_height="match_parent"
100+
android:layout_gravity="right"
101+
android:src="@drawable/ic_delete_24dp"
102+
android:onClick="@{model.deleteSelection}"
103+
android:clickable="true"
104+
android:focusable="true"
105+
android:focusableInTouchMode="true"
106+
android:paddingLeft="15dp"
107+
android:paddingRight="15dp"
108+
android:visibility="@{model.numberSelectedItems > 0 ? View.VISIBLE : View.GONE}"/>
109+
97110
</android.support.v7.widget.Toolbar>
98111

99112
<android.support.design.widget.FloatingActionButton

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
<string name="dialog_delete_server_message">The server will be deleted.</string>
1818
<string name="dialog_delete_server_positive_button">Delete</string>
1919
<string name="dialog_delete_server_negative_button">Cancel</string>
20+
<string name="dialog_delete_files_message">The %d files will be deleted.</string>
21+
<string name="dialog_delete_files_positive_button">Delete</string>
22+
<string name="dialog_delete_files_negative_button">Cancel</string>
2023
</resources>

0 commit comments

Comments
 (0)