Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidFileSystemView.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Erik Duisters <e.duisters1@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.Plugins.SftpPlugin; | ||||
22 | | ||||
23 | import android.content.Context; | ||||
24 | | ||||
25 | import org.apache.sshd.common.file.FileSystemView; | ||||
26 | import org.apache.sshd.common.file.SshFile; | ||||
27 | import org.apache.sshd.common.file.nativefs.NativeFileSystemView; | ||||
28 | import org.apache.sshd.common.file.nativefs.NativeSshFile; | ||||
29 | | ||||
30 | import java.io.File; | ||||
31 | import java.util.ArrayList; | ||||
32 | import java.util.List; | ||||
33 | import java.util.Map; | ||||
34 | | ||||
35 | class AndroidFileSystemView extends NativeFileSystemView { | ||||
36 | final private String userName; | ||||
37 | final private Context context; | ||||
38 | private final Map<String, String> roots; | ||||
39 | private final RootFile rootFile; | ||||
40 | | ||||
41 | AndroidFileSystemView(Map<String, String> roots, String currentRoot, final String userName, Context context) { | ||||
42 | super(userName, roots, currentRoot, File.separatorChar, true); | ||||
43 | this.roots = roots; | ||||
44 | this.userName = userName; | ||||
45 | this.context = context; | ||||
46 | this.rootFile = new RootFile( createFileList(), userName, true); | ||||
47 | } | ||||
48 | | ||||
49 | private List<SshFile> createFileList() { | ||||
50 | List<SshFile> list = new ArrayList<>(); | ||||
51 | for (Map.Entry<String, String> entry : roots.entrySet()) { | ||||
52 | String displayName = entry.getKey(); | ||||
53 | String path = entry.getValue(); | ||||
54 | | ||||
55 | list.add(createNativeSshFile(displayName, new File(path), userName)); | ||||
56 | } | ||||
57 | | ||||
58 | return list; | ||||
59 | } | ||||
60 | | ||||
61 | @Override | ||||
62 | public SshFile getFile(String file) { | ||||
63 | return getFile("/", file); | ||||
64 | } | ||||
65 | | ||||
66 | @Override | ||||
67 | public SshFile getFile(SshFile baseDir, String file) { | ||||
68 | return getFile(baseDir.getAbsolutePath(), file); | ||||
69 | } | ||||
70 | | ||||
71 | @Override | ||||
72 | protected SshFile getFile(String dir, String file) { | ||||
73 | if (!dir.endsWith("/")) { | ||||
74 | dir = dir + "/"; | ||||
75 | } | ||||
76 | | ||||
77 | if (!file.startsWith("/")) { | ||||
78 | file = dir + file; | ||||
79 | } | ||||
80 | | ||||
81 | String filename = NativeSshFile.getPhysicalName("/", "/", file, false); | ||||
82 | | ||||
83 | if (filename.equals("/")) { | ||||
84 | return rootFile; | ||||
85 | } | ||||
86 | | ||||
87 | for (String root : roots.keySet()) { | ||||
88 | if (filename.indexOf(root) == 1) { | ||||
89 | String nameWithoutRoot = filename.substring(root.length() + 1); | ||||
90 | String path = roots.get(root); | ||||
91 | | ||||
92 | if (nameWithoutRoot.isEmpty()) { | ||||
93 | return createNativeSshFile(filename, new File(path), userName); | ||||
94 | } else { | ||||
95 | return createNativeSshFile(filename, new File(path, nameWithoutRoot), userName); | ||||
96 | } | ||||
97 | } | ||||
98 | } | ||||
99 | | ||||
100 | //It's a file under / but not one covered by any Tree | ||||
101 | return new RootFile(new ArrayList<>(0), userName, false); | ||||
102 | } | ||||
103 | | ||||
104 | // NativeFileSystemView.getFile(), NativeSshFile.getParentFile() and NativeSshFile.listSshFiles() call | ||||
105 | // createNativeSshFile to create new NativeSshFiles so override that instead of getFile() to always create an AndroidSshFile | ||||
106 | @Override | ||||
107 | public AndroidSshFile createNativeSshFile(String name, File file, String username) { | ||||
108 | return new AndroidSshFile(this, name, file, username, context); | ||||
109 | } | ||||
110 | | ||||
111 | @Override | ||||
112 | public FileSystemView getNormalizedView() { | ||||
113 | return this; | ||||
114 | } | ||||
115 | } |