summaryrefslogtreecommitdiff
path: root/android/source/src/java/org/libreoffice/storage/owncloud/OwnCloudFile.java
blob: ce10ab64a1e611f28f3fe300085cacd9da40513d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.libreoffice.storage.owncloud;

import java.io.File;
import java.io.FileFilter;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.libreoffice.storage.IFile;

import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
import com.owncloud.android.lib.resources.files.RemoteFile;

/**
 * Implementation of IFile for ownCloud servers.
 */
public class OwnCloudFile implements IFile {

    private OwnCloudProvider provider;
    private RemoteFile file;

    private String name;
    private String parentPath;

    protected OwnCloudFile(OwnCloudProvider provider, RemoteFile file) {
        this.provider = provider;
        this.file = file;

        // get name and parent from path
        File localFile = new File(file.getRemotePath());
        this.name = localFile.getName();
        this.parentPath = localFile.getParent();
    }

    @Override
    public URI getUri() {
        return URI.create(file.getRemotePath());
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public boolean isDirectory() {
        return file.getMimeType().equals("DIR");
    }

    @Override
    public long getSize() {
        return file.getLength();
    }

    @Override
    public Date getLastModified() {
        return new Date(file.getModifiedTimestamp());
    }

    @Override
    public List<IFile> listFiles() {
        List<IFile> children = new ArrayList<IFile>();
        if (isDirectory()) {
            ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(
                    file.getRemotePath());
            RemoteOperationResult result = refreshOperation.execute(provider
                    .getClient());
            if (!result.isSuccess()) {
                throw provider.buildRuntimeExceptionForResultCode(result.getCode());
            }
            for (Object obj : result.getData()) {
                RemoteFile child = (RemoteFile) obj;
                if (!child.getRemotePath().equals(file.getRemotePath()))
                    children.add(new OwnCloudFile(provider, child));
            }
        }
        return children;
    }

    @Override
    public List<IFile> listFiles(FileFilter filter) {
        // TODO no filtering yet
        return listFiles();
    }

    @Override
    public IFile getParent() {
        if (parentPath == null)
            // this is the root node
            return null;

        return provider.createFromUri(URI.create(parentPath));
    }

    @Override
    public File getDocument() {
        if (isDirectory()) {
            return null;
        }
        File downFolder = provider.getCacheDir();
        DownloadRemoteFileOperation operation = new DownloadRemoteFileOperation(
                file.getRemotePath(), downFolder.getAbsolutePath());
        RemoteOperationResult result = operation.execute(provider.getClient());
        if (!result.isSuccess()) {
            throw provider.buildRuntimeExceptionForResultCode(result.getCode());
        }
        return new File(downFolder.getAbsolutePath() + file.getRemotePath());
    }

    @Override
    public boolean equals(Object object) {
        if (this == object)
            return true;
        if (!(object instanceof OwnCloudFile))
            return false;
        OwnCloudFile file = (OwnCloudFile) object;
        return file.getUri().equals(getUri());
    }
}