summaryrefslogtreecommitdiffstats
path: root/android/source/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java
blob: db2b69825c6af340a58bb3e68f7f8b72a41d7415 (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
package org.libreoffice.storage.owncloud;

import java.io.File;
import java.net.URI;

import org.libreoffice.R;
import org.libreoffice.storage.IDocumentProvider;
import org.libreoffice.storage.IFile;

import android.content.Context;
import android.net.Uri;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
import com.owncloud.android.lib.resources.files.RemoteFile;

/**
 * Implementation of IDocumentProvider for ownCloud servers.
 */
public class OwnCloudProvider implements IDocumentProvider {

    private OwnCloudClient client;
    private File cacheDir;

    // TODO: these must be configurable
    final private String serverUrl = "http://10.0.2.2/owncloud"; //emulator host machine
    final private String userName = "admin";
    final private String password = "admin";

    public OwnCloudProvider(Context context) {
        Uri serverUri = Uri.parse(serverUrl);
        client = OwnCloudClientFactory.createOwnCloudClient(serverUri,
                context, true);
        client.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials(
                userName, password));

        // make sure cache directory exists, and clear it
        // TODO: probably we should do smarter cache management
        cacheDir = new File(context.getCacheDir(), "ownCloud");
        if (cacheDir.exists()) {
            deleteRecursive(cacheDir);
        }
        cacheDir.mkdirs();
    }

    @Override
    public IFile getRootDirectory() {
        return createFromUri(URI.create(FileUtils.PATH_SEPARATOR));
    }

    @Override
    public IFile createFromUri(URI uri) {
        ReadRemoteFileOperation refreshOperation = new ReadRemoteFileOperation(
                uri.getPath());
        RemoteOperationResult result = refreshOperation.execute(client);
        if (!result.isSuccess()) {
            throw new RuntimeException(result.getLogMessage(),
                    result.getException());
        }
        if (result.getData().size() > 0) {
            return new OwnCloudFile(this, (RemoteFile) result.getData().get(0));
        }
        return null;
    }

    @Override
    public int getNameResource() {
        return R.string.owncloud;
    }

    /**
     * Used by OwnCloudFiles to get a configured client to run their own
     * operations.
     *
     * @return configured OwnCloudClient.
     */
    protected OwnCloudClient getClient() {
        return client;
    }

    /**
     * Used by OwnCloudFiles to get the cache directory they should download
     * files to.
     *
     * @return cache directory.
     */
    protected File getCacheDir() {
        return cacheDir;
    }

    /**
     * Deletes files and recursively deletes directories.
     *
     * @param file
     *            File or directory to be deleted.
     */
    private void deleteRecursive(File file) {
        if (file.isDirectory()) {
            for (File child : file.listFiles())
                deleteRecursive(child);
        }
        file.delete();
    }
}