Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ContainerObjectDetails.java @ 08922f29

History | View | Annotate | Download (33.9 kB)

1 7dbfc514 koutsoub
package com.rackspace.cloud.android;
2 3d6041e8 Phillip Toohill
3 51938302 Adam Menz
import java.io.BufferedOutputStream;
4 51938302 Adam Menz
import java.io.File;
5 51938302 Adam Menz
import java.io.FileOutputStream;
6 3d6041e8 Phillip Toohill
import java.io.IOException;
7 9c4430bd koutsoub
import java.io.InputStream;
8 9c4430bd koutsoub
import java.io.OutputStream;
9 d37efce5 koutsoub
import java.util.ArrayList;
10 bd384c08 koutsoub
import java.util.HashMap;
11 c99967ba koutsoub
import java.util.Iterator;
12 d37efce5 koutsoub
import java.util.List;
13 bd384c08 koutsoub
import java.util.Map;
14 c99967ba koutsoub
import java.util.Map.Entry;
15 9c4430bd koutsoub
import java.util.StringTokenizer;
16 3d6041e8 Phillip Toohill
17 51938302 Adam Menz
import org.apache.http.HttpEntity;
18 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
19 3d6041e8 Phillip Toohill
20 3d6041e8 Phillip Toohill
import android.app.Activity;
21 3d6041e8 Phillip Toohill
import android.app.AlertDialog;
22 3d6041e8 Phillip Toohill
import android.app.Dialog;
23 3d6041e8 Phillip Toohill
import android.content.DialogInterface;
24 82cd98ea unknown
import android.content.Intent;
25 53026239 koutsoub
import android.graphics.Color;
26 82cd98ea unknown
import android.net.Uri;
27 3d6041e8 Phillip Toohill
import android.os.AsyncTask;
28 3d6041e8 Phillip Toohill
import android.os.Bundle;
29 51938302 Adam Menz
import android.os.Environment;
30 c99967ba koutsoub
import android.util.Log;
31 c99967ba koutsoub
import android.view.LayoutInflater;
32 3d6041e8 Phillip Toohill
import android.view.Menu;
33 3d6041e8 Phillip Toohill
import android.view.MenuInflater;
34 3d6041e8 Phillip Toohill
import android.view.MenuItem;
35 82cd98ea unknown
import android.view.View;
36 732d9c2c koutsoub
import android.view.View.OnClickListener;
37 82cd98ea unknown
import android.widget.Button;
38 732d9c2c koutsoub
import android.widget.CheckBox;
39 732d9c2c koutsoub
import android.widget.CompoundButton;
40 732d9c2c koutsoub
import android.widget.CompoundButton.OnCheckedChangeListener;
41 bd384c08 koutsoub
import android.widget.EditText;
42 b2436f98 koutsoub
import android.widget.ImageButton;
43 c99967ba koutsoub
import android.widget.LinearLayout;
44 732d9c2c koutsoub
import android.widget.TabHost;
45 3d6041e8 Phillip Toohill
import android.widget.TextView;
46 697895bc Adam Menz
import android.widget.Toast;
47 3d6041e8 Phillip Toohill
48 2579ba07 koutsoub
import com.rackspace.cloud.files.api.client.Container;
49 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
50 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjects;
51 53026239 koutsoub
import com.rackspace.cloud.files.api.client.GroupResource;
52 d37efce5 koutsoub
import com.rackspace.cloud.files.api.client.ObjectVersion;
53 732d9c2c koutsoub
import com.rackspace.cloud.files.api.client.Permission;
54 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
55 32731215 Adam Menz
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
56 3d6041e8 Phillip Toohill
57 9c4430bd koutsoub
/**
58 3d6041e8 Phillip Toohill
 * 
59 3d6041e8 Phillip Toohill
 * @author Phillip Toohill
60 9c4430bd koutsoub
 * 
61 3d6041e8 Phillip Toohill
 */
62 3d6041e8 Phillip Toohill
63 b2a2d2f1 Adam Menz
public class ContainerObjectDetails extends CloudActivity {
64 9c4430bd koutsoub
        private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
65 3d6041e8 Phillip Toohill
        private static final int deleteObject = 0;
66 08922f29 koutsoub
        private static final int restoreVersion = 1;
67 9c4430bd koutsoub
        private final String DOWNLOAD_DIRECTORY = "PithosPlus";
68 9c4430bd koutsoub
69 3d6041e8 Phillip Toohill
        private ContainerObjects objects;
70 3d6041e8 Phillip Toohill
        private String containerNames;
71 3d6041e8 Phillip Toohill
        private String cdnURL;
72 8b432514 Phillip Toohill
        private String cdnEnabled;
73 3d6041e8 Phillip Toohill
        public String LOG = "ViewObject";
74 6864568a Phillip Toohill
        private int bConver = 1048576;
75 6864568a Phillip Toohill
        private int kbConver = 1024;
76 6864568a Phillip Toohill
        private double megaBytes;
77 6864568a Phillip Toohill
        private double kiloBytes;
78 82cd98ea unknown
        public Button previewButton;
79 51938302 Adam Menz
        public Button downloadButton;
80 51938302 Adam Menz
        private Boolean isDownloaded;
81 3e180b04 Adam Menz
        private AndroidCloudApplication app;
82 3e180b04 Adam Menz
        private DeleteObjectListenerTask deleteObjTask;
83 3e180b04 Adam Menz
        private DownloadObjectListenerTask downloadObjTask;
84 9c4430bd koutsoub
        private List<ObjectVersion> versions = new ArrayList<ObjectVersion>();
85 2579ba07 koutsoub
        private boolean isReadOnly = false;
86 7b27f0dc koutsoub
        private final List<String> metadataRemoved = new ArrayList<String>();
87 7b27f0dc koutsoub
        private final Map<String,String> metadataAdded = new HashMap<String, String>();
88 7b27f0dc koutsoub
        
89 3e180b04 Adam Menz
        /** Called when the activity is first created. */
90 3e180b04 Adam Menz
        @Override
91 3e180b04 Adam Menz
        public void onCreate(Bundle savedInstanceState) {
92 3e180b04 Adam Menz
                super.onCreate(savedInstanceState);
93 5018a7f8 Adam Menz
                trackPageView(GoogleAnalytics.PAGE_STORAGE_OBJECT);
94 3e180b04 Adam Menz
95 9c4430bd koutsoub
                objects = (ContainerObjects) this.getIntent().getExtras()
96 9c4430bd koutsoub
                                .get("container");
97 9c4430bd koutsoub
                containerNames = (String) this.getIntent().getExtras()
98 9c4430bd koutsoub
                                .get("containerNames");
99 3e180b04 Adam Menz
                cdnURL = (String) this.getIntent().getExtras().get("cdnUrl");
100 3e180b04 Adam Menz
                cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled");
101 7b27f0dc koutsoub
                
102 9c4430bd koutsoub
                setContentView(R.layout.viewobject);
103 9c4430bd koutsoub
                TabHost tabs = (TabHost) findViewById(R.id.tabhost2);
104 9c4430bd koutsoub
105 9c4430bd koutsoub
                tabs.setup();
106 9c4430bd koutsoub
107 9c4430bd koutsoub
                TabHost.TabSpec spec = tabs.newTabSpec("tag1");
108 9c4430bd koutsoub
109 9c4430bd koutsoub
                spec.setContent(R.id.details);
110 9c4430bd koutsoub
                spec.setIndicator("Details");
111 9c4430bd koutsoub
                tabs.addTab(spec);
112 9c4430bd koutsoub
113 9c4430bd koutsoub
                spec = tabs.newTabSpec("tag2");
114 9c4430bd koutsoub
                spec.setContent(R.id.metadata);
115 9c4430bd koutsoub
                spec.setIndicator("Metadata");
116 9c4430bd koutsoub
                tabs.addTab(spec);
117 9c4430bd koutsoub
118 9c4430bd koutsoub
                spec = tabs.newTabSpec("tag3");
119 9c4430bd koutsoub
                spec.setContent(R.id.sharing);
120 9c4430bd koutsoub
                spec.setIndicator("Sharing");
121 9c4430bd koutsoub
                tabs.addTab(spec);
122 9c4430bd koutsoub
123 9c4430bd koutsoub
                spec = tabs.newTabSpec("tag4");
124 9c4430bd koutsoub
                spec.setContent(R.id.versions);
125 9c4430bd koutsoub
                spec.setIndicator("Versions");
126 9c4430bd koutsoub
                tabs.addTab(spec);
127 9c4430bd koutsoub
128 3e180b04 Adam Menz
                restoreState(savedInstanceState);
129 3e180b04 Adam Menz
        }
130 3e180b04 Adam Menz
131 da02192c koutsoub
        public boolean isFolder(){
132 ecbad159 koutsoub
                return objects.getContentType().startsWith("application/folder")||objects.getContentType().startsWith("application/directory");
133 da02192c koutsoub
        }
134 3d6041e8 Phillip Toohill
        @Override
135 3d6041e8 Phillip Toohill
        protected void onSaveInstanceState(Bundle outState) {
136 3d6041e8 Phillip Toohill
                super.onSaveInstanceState(outState);
137 3d6041e8 Phillip Toohill
                outState.putSerializable("container", objects);
138 51938302 Adam Menz
                outState.putBoolean("isDownloaded", isDownloaded);
139 3d6041e8 Phillip Toohill
        }
140 3d6041e8 Phillip Toohill
141 b2a2d2f1 Adam Menz
        protected void restoreState(Bundle state) {
142 b2a2d2f1 Adam Menz
                super.restoreState(state);
143 3e180b04 Adam Menz
                /*
144 9c4430bd koutsoub
                 * need reference to the app so you can access curDirFiles as well as
145 9c4430bd koutsoub
                 * processing status
146 3e180b04 Adam Menz
                 */
147 9c4430bd koutsoub
                app = (AndroidCloudApplication) this.getApplication();
148 51938302 Adam Menz
149 3e180b04 Adam Menz
                if (state != null && state.containsKey("container")) {
150 3e180b04 Adam Menz
                        objects = (ContainerObjects) state.getSerializable("container");
151 3e180b04 Adam Menz
                }
152 3e180b04 Adam Menz
                loadObjectData();
153 51938302 Adam Menz
154 9c4430bd koutsoub
                if (cdnEnabled.equals("true")) {
155 3e180b04 Adam Menz
                        this.previewButton = (Button) findViewById(R.id.preview_button);
156 3e180b04 Adam Menz
                        previewButton.setOnClickListener(new MyOnClickListener());
157 3e180b04 Adam Menz
                } else {
158 3e180b04 Adam Menz
                        this.previewButton = (Button) findViewById(R.id.preview_button);
159 3e180b04 Adam Menz
                        previewButton.setVisibility(View.GONE);
160 3e180b04 Adam Menz
                }
161 3e180b04 Adam Menz
162 9c4430bd koutsoub
                if (state != null && state.containsKey("isDownloaded")) {
163 3e180b04 Adam Menz
                        isDownloaded = state.getBoolean("isDownloaded");
164 9c4430bd koutsoub
                } else {
165 3e180b04 Adam Menz
                        isDownloaded = fileIsDownloaded();
166 3e180b04 Adam Menz
                }
167 3e180b04 Adam Menz
                this.downloadButton = (Button) findViewById(R.id.download_button);
168 9c4430bd koutsoub
                if (isDownloaded) {
169 3e180b04 Adam Menz
                        downloadButton.setText("Open File");
170 3e180b04 Adam Menz
                } else {
171 3e180b04 Adam Menz
                        downloadButton.setText("Download File");
172 9c4430bd koutsoub
                }
173 3e180b04 Adam Menz
                downloadButton.setOnClickListener(new MyOnClickListener());
174 9c4430bd koutsoub
175 9c4430bd koutsoub
                if (app.isDeletingObject()) {
176 3e180b04 Adam Menz
                        deleteObjTask = new DeleteObjectListenerTask();
177 3e180b04 Adam Menz
                        deleteObjTask.execute();
178 3e180b04 Adam Menz
                }
179 9c4430bd koutsoub
180 9c4430bd koutsoub
                if (app.isDownloadingObject()) {
181 3e180b04 Adam Menz
                        downloadObjTask = new DownloadObjectListenerTask();
182 3e180b04 Adam Menz
                        downloadObjTask.execute();
183 3e180b04 Adam Menz
                }
184 9c4430bd koutsoub
185 3e180b04 Adam Menz
        }
186 9c4430bd koutsoub
187 3e180b04 Adam Menz
        @Override
188 9c4430bd koutsoub
        protected void onStop() {
189 3e180b04 Adam Menz
                super.onStop();
190 51938302 Adam Menz
191 3e180b04 Adam Menz
                /*
192 9c4430bd koutsoub
                 * Need to stop running listener task if we exit
193 3e180b04 Adam Menz
                 */
194 9c4430bd koutsoub
                if (deleteObjTask != null) {
195 3e180b04 Adam Menz
                        deleteObjTask.cancel(true);
196 3e180b04 Adam Menz
                }
197 9c4430bd koutsoub
198 9c4430bd koutsoub
                if (downloadObjTask != null) {
199 3e180b04 Adam Menz
                        downloadObjTask.cancel(true);
200 3e180b04 Adam Menz
                }
201 3e180b04 Adam Menz
        }
202 3e180b04 Adam Menz
203 3e180b04 Adam Menz
        private void loadObjectData() {
204 7b27f0dc koutsoub
                setTitle("Details: " +objects.getCName());
205 7b27f0dc koutsoub
                metadataRemoved.clear();
206 7b27f0dc koutsoub
                metadataAdded.clear();
207 ecbad159 koutsoub
                if(Container.MYSHARED.equals(objects.getContainerName())||Container.MYSHARED.equals(containerNames)){
208 2579ba07 koutsoub
                        isReadOnly = true;
209 2579ba07 koutsoub
                        
210 2579ba07 koutsoub
                }
211 9c4430bd koutsoub
                // Object Name
212 3e180b04 Adam Menz
                TextView name = (TextView) findViewById(R.id.view_container_name);
213 3e180b04 Adam Menz
                name.setText(objects.getCName().toString());
214 3e180b04 Adam Menz
215 9c4430bd koutsoub
                // File size
216 3e180b04 Adam Menz
                if (objects.getBytes() >= bConver) {
217 9c4430bd koutsoub
                        megaBytes = Math.abs(objects.getBytes() / bConver + 0.2);
218 3e180b04 Adam Menz
                        TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
219 3e180b04 Adam Menz
                        sublabel.setText(megaBytes + " MB");
220 9c4430bd koutsoub
                } else if (objects.getBytes() >= kbConver) {
221 9c4430bd koutsoub
                        kiloBytes = Math.abs(objects.getBytes() / kbConver + 0.2);
222 3e180b04 Adam Menz
                        TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
223 3e180b04 Adam Menz
                        sublabel.setText(kiloBytes + " KB");
224 6864568a Phillip Toohill
                } else {
225 3e180b04 Adam Menz
                        TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
226 3e180b04 Adam Menz
                        sublabel.setText(objects.getBytes() + " B");
227 9c4430bd koutsoub
                }
228 3e180b04 Adam Menz
229 9c4430bd koutsoub
                // Content Type
230 3e180b04 Adam Menz
                TextView cType = (TextView) findViewById(R.id.view_content_type);
231 3e180b04 Adam Menz
                cType.setText(objects.getContentType().toString());
232 3e180b04 Adam Menz
233 9c4430bd koutsoub
                // Last Modification date
234 3e180b04 Adam Menz
                String strDate = objects.getLastMod();
235 877f6f58 Adam Menz
                strDate = strDate.substring(0, strDate.indexOf('T'));
236 877f6f58 Adam Menz
237 3e180b04 Adam Menz
                TextView lastmod = (TextView) findViewById(R.id.view_file_modification);
238 9c4430bd koutsoub
                lastmod.setText(strDate);
239 c99967ba koutsoub
                rebuildMetadataList();
240 da02192c koutsoub
                if(isFolder()){
241 da02192c koutsoub
                        findViewById(R.id.download_text).setVisibility(View.GONE);
242 da02192c koutsoub
                        findViewById(R.id.linearLayout1).setVisibility(View.GONE);
243 08922f29 koutsoub
                        findViewById(R.id.layoutPublic).setVisibility(View.GONE);
244 da02192c koutsoub
                }
245 732d9c2c koutsoub
                rebuildPermissionList();
246 d37efce5 koutsoub
                try {
247 9c4430bd koutsoub
                        versions = new ContainerObjectManager(getApplicationContext())
248 9c4430bd koutsoub
                                        .getObjectVersions(objects.getContainerName(),
249 9c4430bd koutsoub
                                                        objects.getCName());
250 d37efce5 koutsoub
                        rebuildVersionList();
251 d37efce5 koutsoub
                } catch (CloudServersException e) {
252 d37efce5 koutsoub
                        // TODO Auto-generated catch block
253 d37efce5 koutsoub
                        e.printStackTrace();
254 d37efce5 koutsoub
                }
255 2579ba07 koutsoub
                
256 c99967ba koutsoub
        }
257 bd384c08 koutsoub
        
258 9c4430bd koutsoub
        private void rebuildMetadataList() {
259 bd384c08 koutsoub
                Button newmetadata = (Button)findViewById(R.id.newMetadata);
260 2579ba07 koutsoub
                if(isReadOnly)
261 2579ba07 koutsoub
                        newmetadata.setVisibility(View.GONE);
262 bd384c08 koutsoub
                newmetadata.setOnClickListener(new OnClickListener() {
263 bd384c08 koutsoub
                        
264 bd384c08 koutsoub
                        @Override
265 bd384c08 koutsoub
                        public void onClick(View arg0) {
266 bd384c08 koutsoub
                                final AlertDialog.Builder alert = new AlertDialog.Builder(ContainerObjectDetails.this);
267 bd384c08 koutsoub
                                alert.setTitle("Add Metadata");
268 bd384c08 koutsoub
                                LinearLayout ll = new LinearLayout(ContainerObjectDetails.this);
269 bd384c08 koutsoub
                                ll.setOrientation(LinearLayout.VERTICAL);
270 bd384c08 koutsoub
271 bd384c08 koutsoub
                                final EditText input = new EditText(ContainerObjectDetails.this);
272 bd384c08 koutsoub
                                final EditText input2 = new EditText(ContainerObjectDetails.this);
273 bd384c08 koutsoub
                                ll.addView(input);
274 bd384c08 koutsoub
                                ll.addView(input2);
275 1634500c koutsoub
                                alert.setView(ll);
276 bd384c08 koutsoub
                                alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {
277 bd384c08 koutsoub
                                        public void onClick(DialogInterface dialog, int whichButton) {
278 bd384c08 koutsoub
                                                String key = input.getText().toString().trim();
279 bd384c08 koutsoub
                                                String value = input2.getText().toString().trim();
280 bd384c08 koutsoub
                                                addMetatadata(key, value);
281 bd384c08 koutsoub
                                        }
282 bd384c08 koutsoub
                                });
283 bd384c08 koutsoub
                                
284 bd384c08 koutsoub
                                alert.setNegativeButton("Cancel",
285 bd384c08 koutsoub
                                                new DialogInterface.OnClickListener() {
286 bd384c08 koutsoub
                                        public void onClick(DialogInterface dialog, int whichButton) {
287 bd384c08 koutsoub
                                                dialog.cancel();
288 bd384c08 koutsoub
                                        }
289 bd384c08 koutsoub
                                });
290 bd384c08 koutsoub
                                alert.show();
291 bd384c08 koutsoub
                        }
292 bd384c08 koutsoub
                });
293 9c4430bd koutsoub
                LayoutInflater layoutInflater = LayoutInflater
294 9c4430bd koutsoub
                                .from(ContainerObjectDetails.this);
295 c99967ba koutsoub
                final LinearLayout metadata = (LinearLayout) findViewById(R.id.metadataList);
296 9c4430bd koutsoub
                if (metadata.getChildCount() > 0)
297 9c4430bd koutsoub
                        metadata.removeViews(0, metadata.getChildCount());
298 c99967ba koutsoub
                metadata.removeAllViews();
299 9c4430bd koutsoub
300 9c4430bd koutsoub
                if (objects.getMetadata() != null) {
301 9c4430bd koutsoub
                        int i = 0;
302 9c4430bd koutsoub
                        Iterator<Entry<String, String>> it = objects.getMetadata()
303 9c4430bd koutsoub
                                        .entrySet().iterator();
304 c99967ba koutsoub
                        while (it.hasNext()) {
305 9c4430bd koutsoub
                                final Entry<String, String> perm = it.next();
306 9c4430bd koutsoub
                                final View v = layoutInflater.inflate(R.layout.metadatarow,
307 9c4430bd koutsoub
                                                null);
308 c99967ba koutsoub
                                populateMetadataList(perm, v, metadata, i);
309 c99967ba koutsoub
                                i++;
310 c99967ba koutsoub
                        }
311 c99967ba koutsoub
                }
312 c99967ba koutsoub
        }
313 bd384c08 koutsoub
        
314 bd384c08 koutsoub
        private void addMetatadata(String key, String value){
315 bd384c08 koutsoub
                if(objects.getMetadata()==null)
316 bd384c08 koutsoub
                        objects.setMetadata(new HashMap<String, String>());
317 7b27f0dc koutsoub
                metadataAdded.put(key,value);
318 bd384c08 koutsoub
                objects.getMetadata().put(key, value);
319 bd384c08 koutsoub
                rebuildMetadataList();
320 bd384c08 koutsoub
        }
321 9c4430bd koutsoub
322 9c4430bd koutsoub
        private void populateMetadataList(final Entry<String, String> metadata,
323 9c4430bd koutsoub
                        final View v, final LinearLayout properties, int i) {
324 c99967ba koutsoub
                properties.addView(v, i);
325 9c4430bd koutsoub
                Log.d(LOG, i + " " + metadata.getKey() + " " + metadata.getValue());
326 c99967ba koutsoub
                ((TextView) v.findViewById(R.id.mkey)).setText(metadata.getKey());
327 c99967ba koutsoub
                ((TextView) v.findViewById(R.id.mvalue)).setText(metadata.getValue());
328 2579ba07 koutsoub
                if(isReadOnly)
329 2579ba07 koutsoub
                        v.findViewById(R.id.remove).setVisibility(View.GONE);
330 b2436f98 koutsoub
                ((ImageButton) v.findViewById(R.id.remove))
331 53026239 koutsoub
                .setOnClickListener(new OnClickListener() {
332 53026239 koutsoub
333 53026239 koutsoub
                        @Override
334 53026239 koutsoub
                        public void onClick(View v1) {
335 53026239 koutsoub
                                properties.removeView(v);
336 7b27f0dc koutsoub
                                metadataRemoved.add(metadata.getKey());
337 53026239 koutsoub
                                objects.getMetadata().remove(metadata.getKey());
338 7b27f0dc koutsoub
                                
339 53026239 koutsoub
340 53026239 koutsoub
                        }
341 53026239 koutsoub
                });
342 3e180b04 Adam Menz
        }
343 3e180b04 Adam Menz
344 9c4430bd koutsoub
        private void rebuildPermissionList() {
345 53026239 koutsoub
                Button newmetadata = (Button)findViewById(R.id.newPermission);
346 08922f29 koutsoub
                CheckBox readForAll = (CheckBox)findViewById(R.id.folderPublic);
347 08922f29 koutsoub
                if(isReadOnly){
348 2579ba07 koutsoub
                        newmetadata.setVisibility(View.GONE);
349 08922f29 koutsoub
                        readForAll.setEnabled(false);
350 08922f29 koutsoub
                }
351 08922f29 koutsoub
                objects.setPublicf(objects.getIsPublic()!=null);
352 08922f29 koutsoub
                readForAll.setChecked(objects.isPublicf());
353 08922f29 koutsoub
354 08922f29 koutsoub
                readForAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
355 08922f29 koutsoub
356 08922f29 koutsoub
                                        @Override
357 08922f29 koutsoub
                                        public void onCheckedChanged(CompoundButton buttonView,
358 08922f29 koutsoub
                                                        boolean isChecked) {
359 08922f29 koutsoub
                                                objects.setPublicf(isChecked);
360 08922f29 koutsoub
361 08922f29 koutsoub
                                        }
362 08922f29 koutsoub
                                });
363 53026239 koutsoub
                newmetadata.setOnClickListener(new OnClickListener() {
364 53026239 koutsoub
365 53026239 koutsoub
                        @Override
366 53026239 koutsoub
                        public void onClick(View v) {
367 53026239 koutsoub
                                showAddDialog();                                
368 53026239 koutsoub
                        }
369 53026239 koutsoub
                        
370 53026239 koutsoub
                });
371 9c4430bd koutsoub
                LayoutInflater layoutInflater = LayoutInflater
372 9c4430bd koutsoub
                                .from(ContainerObjectDetails.this);
373 732d9c2c koutsoub
                final LinearLayout properties = (LinearLayout) findViewById(R.id.permissionsList);
374 9c4430bd koutsoub
                if (properties.getChildCount() > 0)
375 9c4430bd koutsoub
                        properties.removeViews(0, properties.getChildCount());
376 732d9c2c koutsoub
                properties.removeAllViews();
377 9c4430bd koutsoub
                Iterator<Permission> it = null;
378 9c4430bd koutsoub
                if (objects.getPermissions() != null) {
379 9c4430bd koutsoub
                        it = objects.getPermissions().iterator();
380 732d9c2c koutsoub
                        int i = 0;
381 732d9c2c koutsoub
                        while (it.hasNext()) {
382 732d9c2c koutsoub
                                final Permission perm = it.next();
383 9c4430bd koutsoub
                                final View v = layoutInflater.inflate(R.layout.propertiesrow,
384 9c4430bd koutsoub
                                                null);
385 732d9c2c koutsoub
                                populatePermissionList(perm, v, properties, i);
386 732d9c2c koutsoub
                                i++;
387 732d9c2c koutsoub
                        }
388 732d9c2c koutsoub
                }
389 732d9c2c koutsoub
        }
390 9c4430bd koutsoub
391 9c4430bd koutsoub
        private void populatePermissionList(final Permission perm, final View v,
392 9c4430bd koutsoub
                        final LinearLayout properties, int i) {
393 9c4430bd koutsoub
394 732d9c2c koutsoub
                properties.addView(v, i);
395 2579ba07 koutsoub
                if(isReadOnly){
396 2579ba07 koutsoub
                        v.findViewById(R.id.remove).setVisibility(View.GONE);
397 2579ba07 koutsoub
                        ((CheckBox) v.findViewById(R.id.read)).setEnabled(false);
398 2579ba07 koutsoub
                        ((CheckBox) v.findViewById(R.id.write)).setEnabled(false);
399 2579ba07 koutsoub
                }
400 7b27f0dc koutsoub
                
401 7b27f0dc koutsoub
                ((TextView) v.findViewById(R.id.ownerName)).setText(perm.getUser()==null?perm.getGroup()+":":perm.getUser());
402 732d9c2c koutsoub
                ((CheckBox) v.findViewById(R.id.read)).setChecked(perm.isRead());
403 732d9c2c koutsoub
                ((CheckBox) v.findViewById(R.id.write)).setChecked(perm.isWrite());
404 732d9c2c koutsoub
405 9c4430bd koutsoub
                ((CheckBox) v.findViewById(R.id.read))
406 9c4430bd koutsoub
                                .setOnCheckedChangeListener(new OnCheckedChangeListener() {
407 732d9c2c koutsoub
408 9c4430bd koutsoub
                                        @Override
409 9c4430bd koutsoub
                                        public void onCheckedChanged(CompoundButton buttonView,
410 9c4430bd koutsoub
                                                        boolean isChecked) {
411 9c4430bd koutsoub
                                                perm.setRead(isChecked);
412 732d9c2c koutsoub
413 9c4430bd koutsoub
                                        }
414 9c4430bd koutsoub
                                });
415 9c4430bd koutsoub
                ((CheckBox) v.findViewById(R.id.write))
416 9c4430bd koutsoub
                                .setOnCheckedChangeListener(new OnCheckedChangeListener() {
417 732d9c2c koutsoub
418 9c4430bd koutsoub
                                        @Override
419 9c4430bd koutsoub
                                        public void onCheckedChanged(CompoundButton buttonView,
420 9c4430bd koutsoub
                                                        boolean isChecked) {
421 9c4430bd koutsoub
                                                perm.setWrite(isChecked);
422 732d9c2c koutsoub
423 9c4430bd koutsoub
                                        }
424 9c4430bd koutsoub
                                });
425 9c4430bd koutsoub
                ((Button) v.findViewById(R.id.remove))
426 9c4430bd koutsoub
                                .setOnClickListener(new OnClickListener() {
427 732d9c2c koutsoub
428 9c4430bd koutsoub
                                        @Override
429 9c4430bd koutsoub
                                        public void onClick(View v1) {
430 9c4430bd koutsoub
                                                properties.removeView(v);
431 9c4430bd koutsoub
                                                objects.getPermissions().remove(perm);
432 9c4430bd koutsoub
433 9c4430bd koutsoub
                                        }
434 9c4430bd koutsoub
                                });
435 732d9c2c koutsoub
        }
436 53026239 koutsoub
        
437 53026239 koutsoub
        private void addPermission(boolean group, String userOrGroup){
438 53026239 koutsoub
                if(objects.getPermissions()==null)
439 53026239 koutsoub
                        objects.setPermissions(new ArrayList<Permission>());
440 53026239 koutsoub
                Permission np = new Permission();
441 53026239 koutsoub
                if(group)
442 53026239 koutsoub
                        np.setGroup(userOrGroup);
443 53026239 koutsoub
                else
444 53026239 koutsoub
                        np.setUser(userOrGroup);
445 53026239 koutsoub
                objects.getPermissions().add(np);
446 53026239 koutsoub
                rebuildPermissionList();
447 53026239 koutsoub
        }
448 53026239 koutsoub
        private void showAddDialog(){
449 53026239 koutsoub
                final CharSequence[] items = {"Add User", "Add Group"};
450 53026239 koutsoub
451 53026239 koutsoub
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
452 53026239 koutsoub
                builder.setTitle("User Or Group");
453 53026239 koutsoub
                builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
454 53026239 koutsoub
                    public void onClick(DialogInterface dialog, int item) {
455 53026239 koutsoub
                            boolean user;
456 53026239 koutsoub
                       if(item ==0){
457 53026239 koutsoub
                               user=true;
458 53026239 koutsoub
                       }
459 53026239 koutsoub
                       else
460 53026239 koutsoub
                               user=false;
461 53026239 koutsoub
                       populateAddDialog(user);
462 53026239 koutsoub
                       dialog.dismiss();
463 53026239 koutsoub
                       
464 53026239 koutsoub
                    }
465 53026239 koutsoub
                });
466 53026239 koutsoub
                AlertDialog alert2 = builder.create();
467 53026239 koutsoub
                alert2.show();
468 53026239 koutsoub
                
469 53026239 koutsoub
        }
470 53026239 koutsoub
        
471 53026239 koutsoub
        private void populateAddDialog(final boolean user){
472 53026239 koutsoub
                final AlertDialog.Builder alert = new AlertDialog.Builder(ContainerObjectDetails.this);
473 53026239 koutsoub
               if(user){
474 53026239 koutsoub
                       alert.setTitle("Add User");
475 53026239 koutsoub
                       final EditText input = new EditText(ContainerObjectDetails.this);
476 53026239 koutsoub
                input.setTextColor(Color.BLACK);
477 53026239 koutsoub
                    
478 53026239 koutsoub
                    alert.setView(input);
479 53026239 koutsoub
                    
480 53026239 koutsoub
                    alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
481 53026239 koutsoub
                            public void onClick(DialogInterface dialog, int whichButton) {
482 53026239 koutsoub
                                    String value = input.getText().toString().trim();
483 53026239 koutsoub
                                    addPermission(false, value);
484 53026239 koutsoub
                                    
485 53026239 koutsoub
                            }
486 53026239 koutsoub
                    });
487 53026239 koutsoub
488 53026239 koutsoub
                    alert.setNegativeButton("Cancel",
489 53026239 koutsoub
                                    new DialogInterface.OnClickListener() {
490 53026239 koutsoub
                                            public void onClick(DialogInterface dialog, int whichButton) {
491 53026239 koutsoub
                                                    dialog.cancel();
492 53026239 koutsoub
                                            
493 53026239 koutsoub
                                            }
494 53026239 koutsoub
                                    });
495 53026239 koutsoub
                    alert.show();
496 53026239 koutsoub
               }
497 53026239 koutsoub
               else{
498 53026239 koutsoub
                       AlertDialog.Builder builder = new AlertDialog.Builder(this);
499 53026239 koutsoub
                    builder.setTitle("Add Group");
500 53026239 koutsoub
                    builder.setSingleChoiceItems(getGroupNames().toArray(new String[0]), -1, new DialogInterface.OnClickListener() {
501 53026239 koutsoub
                        public void onClick(DialogInterface dialog, int item) {
502 53026239 koutsoub
                                addPermission(true, getGroupNames().get(item));
503 53026239 koutsoub
                           dialog.dismiss();
504 53026239 koutsoub
                           
505 53026239 koutsoub
                        }
506 53026239 koutsoub
                    });
507 53026239 koutsoub
                    AlertDialog alert2 = builder.create();
508 53026239 koutsoub
                    alert2.show();
509 53026239 koutsoub
               }
510 53026239 koutsoub
            
511 53026239 koutsoub
        }
512 53026239 koutsoub
        
513 53026239 koutsoub
        public List<String> getGroupNames(){
514 53026239 koutsoub
                List<String> result = new ArrayList<String>();
515 53026239 koutsoub
                List<GroupResource> groups = ((AndroidCloudApplication)getApplication()).getGroups();
516 53026239 koutsoub
                for(GroupResource g : groups){
517 53026239 koutsoub
                        result.add(g.getName());
518 53026239 koutsoub
                }
519 53026239 koutsoub
                return result;
520 53026239 koutsoub
        }
521 53026239 koutsoub
        
522 d37efce5 koutsoub
        private void rebuildVersionList() {
523 9c4430bd koutsoub
                LayoutInflater layoutInflater = LayoutInflater
524 9c4430bd koutsoub
                                .from(ContainerObjectDetails.this);
525 d37efce5 koutsoub
                final LinearLayout properties = (LinearLayout) findViewById(R.id.versionsList);
526 d37efce5 koutsoub
                if (properties.getChildCount() > 0)
527 d37efce5 koutsoub
                        properties.removeViews(0, properties.getChildCount());
528 d37efce5 koutsoub
                properties.removeAllViews();
529 d37efce5 koutsoub
                Iterator<ObjectVersion> it;
530 9c4430bd koutsoub
                // Collections.reverse(versions);
531 d37efce5 koutsoub
                it = versions.iterator();
532 d37efce5 koutsoub
                int i = 0;
533 d37efce5 koutsoub
                while (it.hasNext()) {
534 d37efce5 koutsoub
                        final ObjectVersion perm = it.next();
535 d37efce5 koutsoub
                        final View v = layoutInflater.inflate(R.layout.versionsrow, null);
536 d37efce5 koutsoub
                        populateVersionList(perm, v, properties, i);
537 d37efce5 koutsoub
                        i++;
538 d37efce5 koutsoub
                }
539 d37efce5 koutsoub
        }
540 08922f29 koutsoub
        String selectedVersion=null;
541 d37efce5 koutsoub
        private void populateVersionList(final ObjectVersion perm, final View v,
542 d37efce5 koutsoub
                        final LinearLayout properties, int i) {
543 d37efce5 koutsoub
544 d37efce5 koutsoub
                properties.addView(v, i);
545 d37efce5 koutsoub
546 d37efce5 koutsoub
                ((TextView) v.findViewById(R.id.versionName)).setText("Version: "
547 d37efce5 koutsoub
                                + perm.getVersion());
548 08922f29 koutsoub
                
549 d37efce5 koutsoub
                ((TextView) v.findViewById(R.id.versionModified)).setText("Modified: "
550 d37efce5 koutsoub
                                + perm.getDateString());
551 2579ba07 koutsoub
                if (versions.size() == 1 || isReadOnly) {
552 9c4430bd koutsoub
                        // ((Button)
553 9c4430bd koutsoub
                        // v.findViewById(R.id.vremove)).setVisibility(View.INVISIBLE);
554 9c4430bd koutsoub
                        ((Button) v.findViewById(R.id.vrestore))
555 9c4430bd koutsoub
                                        .setVisibility(View.INVISIBLE);
556 d37efce5 koutsoub
                }
557 d37efce5 koutsoub
                /*
558 9c4430bd koutsoub
                 * ((Button) v.findViewById(R.id.vremove)) .setOnClickListener(new
559 9c4430bd koutsoub
                 * OnClickListener() {
560 9c4430bd koutsoub
                 * 
561 9c4430bd koutsoub
                 * @Override public void onClick(View v1) { Log.d("PERMS",
562 9c4430bd koutsoub
                 * perm.getUri()); try { new GssHttpCommands(getDroidApplication()
563 9c4430bd koutsoub
                 * .getUserDetails
564 9c4430bd koutsoub
                 * ()).deleteFolder(perm.getUri()+"?version="+perm.getVersion()); }
565 9c4430bd koutsoub
                 * catch (SystemErrorException e) { // TODO Auto-generated catch block
566 9c4430bd koutsoub
                 * e.printStackTrace(); } catch (GssHttpException e) { // TODO
567 9c4430bd koutsoub
                 * Auto-generated catch block e.printStackTrace(); }
568 9c4430bd koutsoub
                 * getFileTask().execute(res.getUri()); //properties.removeView(v);
569 9c4430bd koutsoub
                 * 
570 9c4430bd koutsoub
                 * } });
571 9c4430bd koutsoub
                 */
572 9c4430bd koutsoub
                ((Button) v.findViewById(R.id.vrestore))
573 d37efce5 koutsoub
                                .setOnClickListener(new OnClickListener() {
574 d37efce5 koutsoub
575 d37efce5 koutsoub
                                        @Override
576 d37efce5 koutsoub
                                        public void onClick(View v1) {
577 08922f29 koutsoub
                                                selectedVersion = String.valueOf(perm.getVersion());
578 08922f29 koutsoub
                                                showDialog(restoreVersion);
579 d37efce5 koutsoub
580 d37efce5 koutsoub
                                        }
581 9c4430bd koutsoub
                                });
582 d37efce5 koutsoub
                ((Button) v.findViewById(R.id.vdownload))
583 9c4430bd koutsoub
                                .setOnClickListener(new OnClickListener() {
584 d37efce5 koutsoub
585 9c4430bd koutsoub
                                        @Override
586 9c4430bd koutsoub
                                        public void onClick(View v1) {
587 d37efce5 koutsoub
588 9c4430bd koutsoub
                                                // getDownloadTask().execute(perm.getUri()+"?version="+perm.getVersion());
589 9c4430bd koutsoub
                                                // properties.removeView(v);
590 9c4430bd koutsoub
591 9c4430bd koutsoub
                                        }
592 9c4430bd koutsoub
                                });
593 d37efce5 koutsoub
        }
594 9c4430bd koutsoub
595 3e180b04 Adam Menz
        private class MyOnClickListener implements View.OnClickListener {
596 3e180b04 Adam Menz
                @Override
597 3e180b04 Adam Menz
                public void onClick(View v) {
598 9c4430bd koutsoub
                        if (v.equals(findViewById(R.id.preview_button))) {
599 9c4430bd koutsoub
                                Intent viewIntent = new Intent("android.intent.action.VIEW",
600 9c4430bd koutsoub
                                                Uri.parse(cdnURL + "/" + objects.getCName()));
601 9c4430bd koutsoub
                                startActivity(viewIntent);
602 78af992b unknown
                        }
603 3e180b04 Adam Menz
                        /*
604 9c4430bd koutsoub
                         * need to perform different functions based on if the file is in
605 9c4430bd koutsoub
                         * the devices filesystem
606 3e180b04 Adam Menz
                         */
607 9c4430bd koutsoub
                        if (v.equals(findViewById(R.id.download_button))) {
608 9c4430bd koutsoub
                                if (!isDownloaded) {
609 9c4430bd koutsoub
                                        if (storageIsReady()) {
610 3e180b04 Adam Menz
                                                new ContainerObjectDownloadTask().execute();
611 9c4430bd koutsoub
                                        } else {
612 3e180b04 Adam Menz
                                                showAlert("Error", "Storage not found.");
613 3e180b04 Adam Menz
                                        }
614 9c4430bd koutsoub
                                } else {
615 3e180b04 Adam Menz
                                        openFile();
616 3e180b04 Adam Menz
                                }
617 3e180b04 Adam Menz
                        }
618 3e180b04 Adam Menz
                }
619 3e180b04 Adam Menz
        }
620 3e180b04 Adam Menz
621 9c4430bd koutsoub
        // Create the Menu options
622 9c4430bd koutsoub
        @Override
623 3e180b04 Adam Menz
        public boolean onCreateOptionsMenu(Menu menu) {
624 3d6041e8 Phillip Toohill
                super.onCreateOptionsMenu(menu);
625 3d6041e8 Phillip Toohill
                MenuInflater inflater = getMenuInflater();
626 3d6041e8 Phillip Toohill
                inflater.inflate(R.menu.container_object_list_menu, menu);
627 7b27f0dc koutsoub
                menu.findItem(R.id.delete_object).setVisible(!isReadOnly);
628 7b27f0dc koutsoub
                menu.findItem(R.id.save).setVisible(!isReadOnly);
629 3d6041e8 Phillip Toohill
                return true;
630 9c4430bd koutsoub
        }
631 3e180b04 Adam Menz
632 9c4430bd koutsoub
        @Override
633 3e180b04 Adam Menz
        public boolean onOptionsItemSelected(MenuItem item) {
634 3d6041e8 Phillip Toohill
                switch (item.getItemId()) {
635 3d6041e8 Phillip Toohill
                case R.id.delete_object:
636 9c4430bd koutsoub
                        showDialog(deleteObject);
637 3d6041e8 Phillip Toohill
                        return true;
638 3d6041e8 Phillip Toohill
                case R.id.refresh:
639 3d6041e8 Phillip Toohill
                        loadObjectData();
640 3e180b04 Adam Menz
                        return true;
641 05a718db koutsoub
                case R.id.save:
642 08922f29 koutsoub
                        new ContainerObjectUpdateTask().execute();
643 05a718db koutsoub
                        return true;
644 3d6041e8 Phillip Toohill
                }
645 3d6041e8 Phillip Toohill
                return false;
646 9c4430bd koutsoub
        }
647 3e180b04 Adam Menz
648 3e180b04 Adam Menz
        @Override
649 9c4430bd koutsoub
        protected Dialog onCreateDialog(int id) {
650 3e180b04 Adam Menz
                switch (id) {
651 3e180b04 Adam Menz
                case deleteObject:
652 3e180b04 Adam Menz
                        return new AlertDialog.Builder(ContainerObjectDetails.this)
653 9c4430bd koutsoub
                                        .setIcon(R.drawable.alert_dialog_icon)
654 9c4430bd koutsoub
                                        .setTitle("Delete File")
655 9c4430bd koutsoub
                                        .setMessage("Are you sure you want to delete this file?")
656 9c4430bd koutsoub
                                        .setPositiveButton("Delete File",
657 9c4430bd koutsoub
                                                        new DialogInterface.OnClickListener() {
658 9c4430bd koutsoub
                                                                public void onClick(DialogInterface dialog,
659 9c4430bd koutsoub
                                                                                int whichButton) {
660 9c4430bd koutsoub
                                                                        // User clicked OK so do some stuff
661 9c4430bd koutsoub
                                                                        trackEvent(GoogleAnalytics.CATEGORY_FILE,
662 9c4430bd koutsoub
                                                                                        GoogleAnalytics.EVENT_DELETE, "",
663 9c4430bd koutsoub
                                                                                        -1);
664 9c4430bd koutsoub
                                                                        new ContainerObjectDeleteTask()
665 9c4430bd koutsoub
                                                                                        .execute((Void[]) null);
666 9c4430bd koutsoub
                                                                }
667 9c4430bd koutsoub
                                                        })
668 9c4430bd koutsoub
                                        .setNegativeButton("Cancel",
669 9c4430bd koutsoub
                                                        new DialogInterface.OnClickListener() {
670 9c4430bd koutsoub
                                                                public void onClick(DialogInterface dialog,
671 9c4430bd koutsoub
                                                                                int whichButton) {
672 9c4430bd koutsoub
                                                                        // User clicked Cancel so do some stuff
673 9c4430bd koutsoub
                                                                }
674 9c4430bd koutsoub
                                                        }).create();
675 08922f29 koutsoub
                case restoreVersion:
676 08922f29 koutsoub
                        return new AlertDialog.Builder(ContainerObjectDetails.this)
677 08922f29 koutsoub
                                        .setIcon(R.drawable.alert_dialog_icon)
678 08922f29 koutsoub
                                        .setTitle("Restore Version")
679 08922f29 koutsoub
                                        .setMessage("Are you sure you want to restore this version?")
680 08922f29 koutsoub
                                        .setPositiveButton("Restore Version",
681 08922f29 koutsoub
                                                        new DialogInterface.OnClickListener() {
682 08922f29 koutsoub
                                                                public void onClick(DialogInterface dialog,
683 08922f29 koutsoub
                                                                                int whichButton) {
684 08922f29 koutsoub
                                                                        // User clicked OK so do some stuff
685 08922f29 koutsoub
                                                                        /*trackEvent(GoogleAnalytics.CATEGORY_FILE,
686 08922f29 koutsoub
                                                                                        GoogleAnalytics.EVENT_DELETE, "",
687 08922f29 koutsoub
                                                                                        -1);*/
688 08922f29 koutsoub
                                                                        new VersionRestoreTask()
689 08922f29 koutsoub
                                                                                        .execute(selectedVersion);
690 08922f29 koutsoub
                                                                }
691 08922f29 koutsoub
                                                        })
692 08922f29 koutsoub
                                        .setNegativeButton("Cancel",
693 08922f29 koutsoub
                                                        new DialogInterface.OnClickListener() {
694 08922f29 koutsoub
                                                                public void onClick(DialogInterface dialog,
695 08922f29 koutsoub
                                                                                int whichButton) {
696 08922f29 koutsoub
                                                                        // User clicked Cancel so do some stuff
697 08922f29 koutsoub
                                                                }
698 08922f29 koutsoub
                                                        }).create();
699 08922f29 koutsoub
                
700 3e180b04 Adam Menz
                }
701 08922f29 koutsoub
                
702 3e180b04 Adam Menz
                return null;
703 3e180b04 Adam Menz
        }
704 9c4430bd koutsoub
705 3e180b04 Adam Menz
        /**
706 3d6041e8 Phillip Toohill
         * @return the file
707 3d6041e8 Phillip Toohill
         */
708 3d6041e8 Phillip Toohill
        public ContainerObjects getViewFile() {
709 3d6041e8 Phillip Toohill
                return objects;
710 3d6041e8 Phillip Toohill
        }
711 3d6041e8 Phillip Toohill
712 3d6041e8 Phillip Toohill
        /**
713 9c4430bd koutsoub
         * @param File
714 9c4430bd koutsoub
         *            the file to set
715 3d6041e8 Phillip Toohill
         */
716 3d6041e8 Phillip Toohill
        public void setViewFile(ContainerObjects object) {
717 3d6041e8 Phillip Toohill
                this.objects = object;
718 3d6041e8 Phillip Toohill
        }
719 3e180b04 Adam Menz
720 51938302 Adam Menz
        /*
721 9c4430bd koutsoub
         * returns false if external storage is not avaliable (if its mounted,
722 9c4430bd koutsoub
         * missing, read-only, etc) from:
723 9c4430bd koutsoub
         * http://developer.android.com/guide/topics/data
724 9c4430bd koutsoub
         * /data-storage.html#filesExternal
725 51938302 Adam Menz
         */
726 9c4430bd koutsoub
        private boolean storageIsReady() {
727 51938302 Adam Menz
                boolean mExternalStorageAvailable = false;
728 51938302 Adam Menz
                boolean mExternalStorageWriteable = false;
729 51938302 Adam Menz
                String state = Environment.getExternalStorageState();
730 51938302 Adam Menz
731 51938302 Adam Menz
                if (Environment.MEDIA_MOUNTED.equals(state)) {
732 3e180b04 Adam Menz
                        // We can read and write the media
733 3e180b04 Adam Menz
                        mExternalStorageAvailable = mExternalStorageWriteable = true;
734 51938302 Adam Menz
                } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
735 3e180b04 Adam Menz
                        // We can only read the media
736 3e180b04 Adam Menz
                        mExternalStorageAvailable = true;
737 3e180b04 Adam Menz
                        mExternalStorageWriteable = false;
738 51938302 Adam Menz
                } else {
739 9c4430bd koutsoub
                        // Something else is wrong. It may be one of many other states, but
740 9c4430bd koutsoub
                        // all we need
741 9c4430bd koutsoub
                        // to know is we can neither read nor write
742 3e180b04 Adam Menz
                        mExternalStorageAvailable = mExternalStorageWriteable = false;
743 51938302 Adam Menz
                }
744 51938302 Adam Menz
                return mExternalStorageAvailable && mExternalStorageWriteable;
745 51938302 Adam Menz
        }
746 3e180b04 Adam Menz
747 9c4430bd koutsoub
        private boolean fileIsDownloaded() {
748 9c4430bd koutsoub
                if (storageIsReady()) {
749 9c4430bd koutsoub
                        String fileName = Environment.getExternalStorageDirectory()
750 9c4430bd koutsoub
                                        .getPath() + "/PithosPlus/" + objects.getCName();
751 51938302 Adam Menz
                        File f = new File(fileName);
752 51938302 Adam Menz
                        return f.isFile();
753 51938302 Adam Menz
                }
754 51938302 Adam Menz
                return false;
755 51938302 Adam Menz
        }
756 3e180b04 Adam Menz
757 9c4430bd koutsoub
        private void openFile() {
758 9c4430bd koutsoub
                File object = new File(Environment.getExternalStorageDirectory()
759 9c4430bd koutsoub
                                .getPath() + "/PithosPlus/" + objects.getCName());
760 51938302 Adam Menz
                Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
761 9c4430bd koutsoub
                File file = new File(object.getAbsolutePath());
762 9c4430bd koutsoub
                String extension = android.webkit.MimeTypeMap
763 9c4430bd koutsoub
                                .getFileExtensionFromUrl(Uri.fromFile(file).toString());
764 9c4430bd koutsoub
                String mimetype = android.webkit.MimeTypeMap.getSingleton()
765 9c4430bd koutsoub
                                .getMimeTypeFromExtension(extension);
766 9c4430bd koutsoub
                myIntent.setDataAndType(Uri.fromFile(file), mimetype);
767 9c4430bd koutsoub
                // myIntent.setData(Uri.fromFile(file));
768 9c4430bd koutsoub
                try {
769 3e180b04 Adam Menz
                        startActivity(myIntent);
770 9c4430bd koutsoub
                } catch (Exception e) {
771 9c4430bd koutsoub
                        Toast.makeText(this, "Could not open file.", Toast.LENGTH_SHORT)
772 9c4430bd koutsoub
                                        .show();
773 3e180b04 Adam Menz
                }
774 51938302 Adam Menz
        }
775 3e180b04 Adam Menz
776 9c4430bd koutsoub
        // Task's
777 3e180b04 Adam Menz
778 9c4430bd koutsoub
        private class ContainerObjectDeleteTask extends
779 9c4430bd koutsoub
                        AsyncTask<Void, Void, HttpBundle> {
780 3e180b04 Adam Menz
781 3e180b04 Adam Menz
                private CloudServersException exception;
782 3e180b04 Adam Menz
783 9c4430bd koutsoub
                protected void onPreExecute() {
784 3e180b04 Adam Menz
                        showDialog();
785 3e180b04 Adam Menz
                        app.setDeleteingObject(true);
786 3e180b04 Adam Menz
                        deleteObjTask = new DeleteObjectListenerTask();
787 3e180b04 Adam Menz
                        deleteObjTask.execute();
788 3e180b04 Adam Menz
                }
789 3e180b04 Adam Menz
790 3e180b04 Adam Menz
                @Override
791 3e180b04 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
792 9c4430bd koutsoub
                        HttpBundle bundle = null;
793 3e180b04 Adam Menz
                        try {
794 9c4430bd koutsoub
                                bundle = (new ContainerObjectManager(getContext()))
795 9c4430bd koutsoub
                                                .deleteObject(containerNames, objects.getCName());
796 3e180b04 Adam Menz
                        } catch (CloudServersException e) {
797 3e180b04 Adam Menz
                                exception = e;
798 3e180b04 Adam Menz
                        }
799 9c4430bd koutsoub
800 3e180b04 Adam Menz
                        return bundle;
801 3e180b04 Adam Menz
                }
802 3e180b04 Adam Menz
803 3e180b04 Adam Menz
                @Override
804 3e180b04 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
805 3e180b04 Adam Menz
                        app.setDeleteingObject(false);
806 3e180b04 Adam Menz
                        hideDialog();
807 3e180b04 Adam Menz
                        HttpResponse response = bundle.getResponse();
808 3e180b04 Adam Menz
                        if (response != null) {
809 3e180b04 Adam Menz
                                int statusCode = response.getStatusLine().getStatusCode();
810 3e180b04 Adam Menz
                                if (statusCode == 204) {
811 9c4430bd koutsoub
                                        // handled by listener
812 3e180b04 Adam Menz
                                } else {
813 3e180b04 Adam Menz
                                        CloudServersException cse = parseCloudServersException(response);
814 3e180b04 Adam Menz
                                        if ("".equals(cse.getMessage())) {
815 9c4430bd koutsoub
                                                showError("There was a problem deleting your File.",
816 9c4430bd koutsoub
                                                                bundle);
817 3e180b04 Adam Menz
                                        } else {
818 9c4430bd koutsoub
                                                showError("There was a problem deleting your file: "
819 9c4430bd koutsoub
                                                                + cse.getMessage(), bundle);
820 3e180b04 Adam Menz
                                        }
821 3e180b04 Adam Menz
                                }
822 3e180b04 Adam Menz
                        } else if (exception != null) {
823 9c4430bd koutsoub
                                showError("There was a problem deleting your file: "
824 9c4430bd koutsoub
                                                + exception.getMessage(), bundle);
825 9c4430bd koutsoub
                        }
826 3e180b04 Adam Menz
                }
827 3e180b04 Adam Menz
        }
828 3e180b04 Adam Menz
829 9c4430bd koutsoub
        private class ContainerObjectDownloadTask extends
830 9c4430bd koutsoub
                        AsyncTask<Void, Void, HttpBundle> {
831 3e180b04 Adam Menz
832 3e180b04 Adam Menz
                private CloudServersException exception;
833 3e180b04 Adam Menz
834 3e180b04 Adam Menz
                @Override
835 9c4430bd koutsoub
                protected void onPreExecute() {
836 3e180b04 Adam Menz
                        showDialog();
837 3e180b04 Adam Menz
                        app.setDownloadingObject(true);
838 3e180b04 Adam Menz
                        downloadObjTask = new DownloadObjectListenerTask();
839 3e180b04 Adam Menz
                        downloadObjTask.execute();
840 3e180b04 Adam Menz
                }
841 3e180b04 Adam Menz
842 3e180b04 Adam Menz
                @Override
843 3e180b04 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
844 9c4430bd koutsoub
                        HttpBundle bundle = null;
845 3e180b04 Adam Menz
                        try {
846 9c4430bd koutsoub
                                bundle = (new ContainerObjectManager(getContext())).getObject(
847 9c4430bd koutsoub
                                                containerNames, objects.getCName());
848 3e180b04 Adam Menz
                        } catch (CloudServersException e) {
849 3e180b04 Adam Menz
                                exception = e;
850 3e180b04 Adam Menz
                        }
851 3e180b04 Adam Menz
                        return bundle;
852 3e180b04 Adam Menz
                }
853 3e180b04 Adam Menz
854 3e180b04 Adam Menz
                @Override
855 3e180b04 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
856 9c4430bd koutsoub
                        
857 9c4430bd koutsoub
                        
858 3e180b04 Adam Menz
                        HttpResponse response = bundle.getResponse();
859 3e180b04 Adam Menz
                        if (response != null) {
860 3e180b04 Adam Menz
                                int statusCode = response.getStatusLine().getStatusCode();
861 3e180b04 Adam Menz
                                if (statusCode == 200) {
862 3e180b04 Adam Menz
                                        setResult(Activity.RESULT_OK);
863 3e180b04 Adam Menz
                                        HttpEntity entity = response.getEntity();
864 3e180b04 Adam Menz
                                        app.setDownloadedEntity(entity);
865 3e180b04 Adam Menz
                                } else {
866 3e180b04 Adam Menz
                                        CloudServersException cse = parseCloudServersException(response);
867 3e180b04 Adam Menz
                                        if ("".equals(cse.getMessage())) {
868 9c4430bd koutsoub
                                                showError("There was a problem downloading your File.",
869 9c4430bd koutsoub
                                                                bundle);
870 3e180b04 Adam Menz
                                        } else {
871 9c4430bd koutsoub
                                                showError("There was a problem downloading your file: "
872 9c4430bd koutsoub
                                                                + cse.getMessage(), bundle);
873 3e180b04 Adam Menz
                                        }
874 3e180b04 Adam Menz
                                }
875 3e180b04 Adam Menz
                        } else if (exception != null) {
876 9c4430bd koutsoub
                                showError("There was a problem downloading your file: "
877 9c4430bd koutsoub
                                                + exception.getMessage(), bundle);
878 9c4430bd koutsoub
                        }
879 9c4430bd koutsoub
                        app.setDownloadingObject(false);
880 3e180b04 Adam Menz
                }
881 3e180b04 Adam Menz
        }
882 9c4430bd koutsoub
883 9c4430bd koutsoub
        private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> {
884 9c4430bd koutsoub
885 3e180b04 Adam Menz
                @Override
886 3e180b04 Adam Menz
                protected Void doInBackground(Void... arg1) {
887 3e180b04 Adam Menz
888 9c4430bd koutsoub
                        while (app.isDeletingObject()) {
889 3e180b04 Adam Menz
                                // wait for process to finish
890 3e180b04 Adam Menz
                                // or have it be canceled
891 9c4430bd koutsoub
                                if (deleteObjTask.isCancelled()) {
892 3e180b04 Adam Menz
                                        return null;
893 3e180b04 Adam Menz
                                }
894 3e180b04 Adam Menz
                        }
895 3e180b04 Adam Menz
                        return null;
896 3e180b04 Adam Menz
                }
897 3e180b04 Adam Menz
898 3e180b04 Adam Menz
                /*
899 9c4430bd koutsoub
                 * when no longer processing, time to load the new files
900 3e180b04 Adam Menz
                 */
901 3e180b04 Adam Menz
                @Override
902 3e180b04 Adam Menz
                protected void onPostExecute(Void arg1) {
903 3e180b04 Adam Menz
                        hideDialog();
904 3e180b04 Adam Menz
                        setResult(99);
905 3e180b04 Adam Menz
                        finish();
906 3e180b04 Adam Menz
                }
907 3e180b04 Adam Menz
        }
908 9c4430bd koutsoub
909 3e180b04 Adam Menz
        private class DownloadObjectListenerTask extends
910 9c4430bd koutsoub
                        AsyncTask<Void, Void, Void> {
911 9c4430bd koutsoub
912 3e180b04 Adam Menz
                @Override
913 3e180b04 Adam Menz
                protected Void doInBackground(Void... arg1) {
914 3e180b04 Adam Menz
915 9c4430bd koutsoub
                        while (app.isDownloadingObject()) {
916 3e180b04 Adam Menz
                                // wait for process to finish
917 3e180b04 Adam Menz
                                // or have it be canceled
918 9c4430bd koutsoub
                                if (downloadObjTask.isCancelled()) {
919 3e180b04 Adam Menz
                                        return null;
920 3e180b04 Adam Menz
                                }
921 3e180b04 Adam Menz
                        }
922 3e180b04 Adam Menz
                        return null;
923 3e180b04 Adam Menz
                }
924 3e180b04 Adam Menz
925 3e180b04 Adam Menz
                /*
926 9c4430bd koutsoub
                 * when no longer processing, time to load the new files
927 3e180b04 Adam Menz
                 */
928 3e180b04 Adam Menz
                @Override
929 3e180b04 Adam Menz
                protected void onPostExecute(Void arg1) {
930 9c4430bd koutsoub
                        
931 3e180b04 Adam Menz
                        try {
932 9c4430bd koutsoub
                                //TODO: run in background
933 9c4430bd koutsoub
                                if (writeFile(app.getDownloadedEntity().getContent())) {
934 3e180b04 Adam Menz
                                        downloadButton.setText("Open File");
935 3e180b04 Adam Menz
                                        isDownloaded = true;
936 9c4430bd koutsoub
                                } else {
937 9c4430bd koutsoub
                                        showAlert("Error",
938 9c4430bd koutsoub
                                                        "There was a problem downloading your file.");
939 3e180b04 Adam Menz
                                }
940 3e180b04 Adam Menz
941 3e180b04 Adam Menz
                        } catch (IOException e) {
942 3e180b04 Adam Menz
                                showAlert("Error", "There was a problem downloading your file.");
943 3e180b04 Adam Menz
                                e.printStackTrace();
944 969195cf Adam Menz
                        } catch (Exception e) {
945 969195cf Adam Menz
                                showAlert("Error", "There was a problem downloading your file.");
946 969195cf Adam Menz
                                e.printStackTrace();
947 3e180b04 Adam Menz
                        }
948 9c4430bd koutsoub
                        hideDialog();
949 3e180b04 Adam Menz
                }
950 3e180b04 Adam Menz
        }
951 3e180b04 Adam Menz
952 9c4430bd koutsoub
        private boolean writeFile(InputStream in) {
953 9c4430bd koutsoub
                String directoryName = Environment.getExternalStorageDirectory()
954 9c4430bd koutsoub
                                .getPath();
955 9c4430bd koutsoub
                File f = new File(directoryName, DOWNLOAD_DIRECTORY);
956 9c4430bd koutsoub
                Log.i(LOG,directoryName);
957 9c4430bd koutsoub
                if (!f.isDirectory()) {
958 9c4430bd koutsoub
                        if (!f.mkdir()) {
959 9c4430bd koutsoub
                                return false;
960 9c4430bd koutsoub
                        }
961 9c4430bd koutsoub
                }
962 9c4430bd koutsoub
                Log.i(LOG,objects.toString());
963 9c4430bd koutsoub
                //String filename = directoryName + "/" + objects.getName();
964 9c4430bd koutsoub
                StringTokenizer str = new StringTokenizer(objects.getCName(),"/");
965 9c4430bd koutsoub
                String path="";
966 9c4430bd koutsoub
                String fname="";
967 9c4430bd koutsoub
                int count = str.countTokens();
968 9c4430bd koutsoub
                Log.i(LOG,"object is: "+objects.getCName()+" "+count);
969 9c4430bd koutsoub
                for(int i=0;i<count;i++){
970 9c4430bd koutsoub
                        if(i<(count-1)){
971 9c4430bd koutsoub
                                path = path+str.nextToken()+"/";
972 9c4430bd koutsoub
                        }
973 9c4430bd koutsoub
                        else
974 9c4430bd koutsoub
                                fname=str.nextToken();
975 9c4430bd koutsoub
                }
976 9c4430bd koutsoub
                Log.i(LOG,"Path is:"+path);
977 9c4430bd koutsoub
                Log.i(LOG,"Fname is:"+fname);
978 9c4430bd koutsoub
                File object;
979 9c4430bd koutsoub
                if("".equals(path)){
980 9c4430bd koutsoub
                        object = new File(f,fname);
981 9c4430bd koutsoub
                }
982 9c4430bd koutsoub
                else{
983 9c4430bd koutsoub
                        File t = new File(f,path);
984 9c4430bd koutsoub
                        t.mkdirs();
985 9c4430bd koutsoub
                        object = new File(t,fname);
986 9c4430bd koutsoub
                }
987 9c4430bd koutsoub
                
988 9c4430bd koutsoub
                
989 9c4430bd koutsoub
                BufferedOutputStream bos = null;
990 9c4430bd koutsoub
991 9c4430bd koutsoub
                try {
992 9c4430bd koutsoub
                        FileOutputStream fos = new FileOutputStream(object);
993 9c4430bd koutsoub
                        copy(in, fos);
994 9c4430bd koutsoub
                } catch (IOException e) {
995 9c4430bd koutsoub
                        e.printStackTrace();
996 9c4430bd koutsoub
                } finally {
997 9c4430bd koutsoub
                        if (bos != null) {
998 9c4430bd koutsoub
                                try {
999 9c4430bd koutsoub
                                        bos.flush();
1000 9c4430bd koutsoub
                                        bos.close();
1001 9c4430bd koutsoub
                                } catch (IOException e) {
1002 9c4430bd koutsoub
                                        // TODO Auto-generated catch block
1003 9c4430bd koutsoub
                                        e.printStackTrace();
1004 9c4430bd koutsoub
                                }
1005 9c4430bd koutsoub
                        }
1006 9c4430bd koutsoub
                }
1007 9c4430bd koutsoub
                return true;
1008 9c4430bd koutsoub
        }
1009 9c4430bd koutsoub
1010 9c4430bd koutsoub
        public static long copy(InputStream input, OutputStream output) throws IOException {
1011 9c4430bd koutsoub
                
1012 9c4430bd koutsoub
                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
1013 9c4430bd koutsoub
                long count = 0;
1014 9c4430bd koutsoub
                int n = 0;
1015 9c4430bd koutsoub
                try{
1016 9c4430bd koutsoub
                        while (-1 != (n = input.read(buffer))) {
1017 9c4430bd koutsoub
                                output.write(buffer, 0, n);
1018 9c4430bd koutsoub
                                count += n;
1019 9c4430bd koutsoub
                                //monitor.setCurrent(count);
1020 9c4430bd koutsoub
                        }
1021 9c4430bd koutsoub
                }
1022 9c4430bd koutsoub
                finally{
1023 9c4430bd koutsoub
                        input.close();
1024 9c4430bd koutsoub
                        output.close();
1025 9c4430bd koutsoub
                }
1026 9c4430bd koutsoub
                return count;
1027 9c4430bd koutsoub
        }
1028 53026239 koutsoub
        
1029 53026239 koutsoub
        @Override
1030 53026239 koutsoub
        public void onBackPressed() {
1031 05a718db koutsoub
                
1032 53026239 koutsoub
                //TODO: perform update
1033 53026239 koutsoub
                super.onBackPressed();
1034 53026239 koutsoub
        }
1035 9c4430bd koutsoub
        
1036 08922f29 koutsoub
        
1037 08922f29 koutsoub
        
1038 08922f29 koutsoub
        
1039 08922f29 koutsoub
        private class ContainerObjectUpdateTask extends
1040 08922f29 koutsoub
                        AsyncTask<Void, Void, HttpBundle> {
1041 08922f29 koutsoub
                
1042 08922f29 koutsoub
                private CloudServersException exception;
1043 08922f29 koutsoub
                
1044 08922f29 koutsoub
                protected void onPreExecute() {
1045 08922f29 koutsoub
                        showDialog();
1046 08922f29 koutsoub
                }
1047 08922f29 koutsoub
                
1048 08922f29 koutsoub
                @Override
1049 08922f29 koutsoub
                protected HttpBundle doInBackground(Void... arg0) {
1050 08922f29 koutsoub
                        HttpBundle bundle = null;
1051 08922f29 koutsoub
                        try {
1052 08922f29 koutsoub
                                bundle = saveObject();
1053 08922f29 koutsoub
                        } catch (CloudServersException e) {
1054 08922f29 koutsoub
                                exception = e;
1055 08922f29 koutsoub
                        }
1056 08922f29 koutsoub
                
1057 08922f29 koutsoub
                        return bundle;
1058 08922f29 koutsoub
                }
1059 08922f29 koutsoub
                
1060 08922f29 koutsoub
                @Override
1061 08922f29 koutsoub
                protected void onPostExecute(HttpBundle bundle) {
1062 08922f29 koutsoub
                        hideDialog();
1063 08922f29 koutsoub
                        HttpResponse response = bundle.getResponse();
1064 08922f29 koutsoub
                        if (response != null) {
1065 08922f29 koutsoub
                                int statusCode = response.getStatusLine().getStatusCode();
1066 08922f29 koutsoub
                                if (statusCode == 202) {
1067 08922f29 koutsoub
                                        hideDialog();
1068 08922f29 koutsoub
                                        setResult(99);
1069 08922f29 koutsoub
                                        finish();
1070 08922f29 koutsoub
                                } else {
1071 08922f29 koutsoub
                                        CloudServersException cse = parseCloudServersException(response);
1072 08922f29 koutsoub
                                        if ("".equals(cse.getMessage())) {
1073 08922f29 koutsoub
                                                showError("There was a problem deleting your File.",
1074 08922f29 koutsoub
                                                                bundle);
1075 08922f29 koutsoub
                                        } else {
1076 08922f29 koutsoub
                                                showError("There was a problem deleting your file: "
1077 08922f29 koutsoub
                                                                + cse.getMessage(), bundle);
1078 08922f29 koutsoub
                                        }
1079 08922f29 koutsoub
                                }
1080 08922f29 koutsoub
                        } else if (exception != null) {
1081 08922f29 koutsoub
                                showError("There was a problem deleting your file: "
1082 08922f29 koutsoub
                                                + exception.getMessage(), bundle);
1083 08922f29 koutsoub
                        }
1084 08922f29 koutsoub
                }
1085 08922f29 koutsoub
        }
1086 08922f29 koutsoub
        
1087 08922f29 koutsoub
        private class VersionRestoreTask extends
1088 08922f29 koutsoub
                                AsyncTask<String, Void, HttpBundle> {
1089 08922f29 koutsoub
                        
1090 08922f29 koutsoub
                        private CloudServersException exception;
1091 08922f29 koutsoub
                        
1092 08922f29 koutsoub
                        protected void onPreExecute() {
1093 08922f29 koutsoub
                                showDialog();
1094 08922f29 koutsoub
                        }
1095 08922f29 koutsoub
                        
1096 08922f29 koutsoub
                        @Override
1097 08922f29 koutsoub
                        protected HttpBundle doInBackground(String... arg0) {
1098 08922f29 koutsoub
                                HttpBundle bundle = null;
1099 08922f29 koutsoub
                                try {
1100 08922f29 koutsoub
                                        bundle = restoreVersion(arg0[0]);
1101 08922f29 koutsoub
                                } catch (CloudServersException e) {
1102 08922f29 koutsoub
                                        e.printStackTrace();
1103 08922f29 koutsoub
                                        exception = e;
1104 08922f29 koutsoub
                                }
1105 08922f29 koutsoub
                        
1106 08922f29 koutsoub
                                return bundle;
1107 08922f29 koutsoub
                        }
1108 08922f29 koutsoub
                        
1109 08922f29 koutsoub
                        @Override
1110 08922f29 koutsoub
                        protected void onPostExecute(HttpBundle bundle) {
1111 08922f29 koutsoub
                                hideDialog();
1112 08922f29 koutsoub
                                HttpResponse response = null;
1113 08922f29 koutsoub
                                if(bundle!=null)
1114 08922f29 koutsoub
                                        response= bundle.getResponse();
1115 08922f29 koutsoub
                                if (response != null) {
1116 08922f29 koutsoub
                                        int statusCode = response.getStatusLine().getStatusCode();
1117 08922f29 koutsoub
                                        if (statusCode == 204||statusCode==202) {
1118 08922f29 koutsoub
                                                hideDialog();
1119 08922f29 koutsoub
                                                setResult(99);
1120 08922f29 koutsoub
                                                finish();
1121 08922f29 koutsoub
                                        } else {
1122 08922f29 koutsoub
                                                CloudServersException cse = parseCloudServersException(response);
1123 08922f29 koutsoub
                                                if ("".equals(cse.getMessage())) {
1124 08922f29 koutsoub
                                                        showError("There was a problem deleting your File.",
1125 08922f29 koutsoub
                                                                        bundle);
1126 08922f29 koutsoub
                                                } else {
1127 08922f29 koutsoub
                                                        showError("There was a problem deleting your file: "
1128 08922f29 koutsoub
                                                                        + cse.getMessage(), bundle);
1129 08922f29 koutsoub
                                                }
1130 08922f29 koutsoub
                                        }
1131 08922f29 koutsoub
                                } else if (exception != null) {
1132 08922f29 koutsoub
                                        showError("There was a problem deleting your file: "
1133 08922f29 koutsoub
                                                        + exception.getMessage(), bundle);
1134 08922f29 koutsoub
                                }
1135 08922f29 koutsoub
                        }
1136 08922f29 koutsoub
                }
1137 08922f29 koutsoub
        
1138 08922f29 koutsoub
        public HttpBundle saveObject() throws CloudServersException{
1139 05a718db koutsoub
                Map<String,String> headers = new HashMap<String,String>();
1140 7b27f0dc koutsoub
                if(objects.getMetadata()!=null)
1141 7b27f0dc koutsoub
                        for(Entry<String,String> entry : objects.getMetadata().entrySet()){
1142 7b27f0dc koutsoub
                                headers.put("X-Object-Meta-"+entry.getKey(), entry.getValue());
1143 7b27f0dc koutsoub
                        }
1144 7b27f0dc koutsoub
                for(String s : metadataRemoved){
1145 7b27f0dc koutsoub
                        headers.put("X-Object-Meta-"+s, "~");
1146 7b27f0dc koutsoub
                }
1147 08922f29 koutsoub
                if(!isFolder())
1148 08922f29 koutsoub
                        headers.put("X-Object-Public", String.valueOf(objects.isPublicf()));
1149 7b27f0dc koutsoub
                String read ="";
1150 7b27f0dc koutsoub
                String write="";
1151 7b27f0dc koutsoub
                for(Permission p : objects.getPermissions()){
1152 7b27f0dc koutsoub
                        if(p.isWrite()){
1153 7b27f0dc koutsoub
                                if(!write.equals("")){
1154 7b27f0dc koutsoub
                                        write = write+",";
1155 7b27f0dc koutsoub
                                }
1156 7b27f0dc koutsoub
                                write = write + (p.getGroup()==null?p.getUser():p.getGroup());
1157 7b27f0dc koutsoub
                        }
1158 7b27f0dc koutsoub
                        else if(p.isRead()){
1159 7b27f0dc koutsoub
                                if(!read.equals("")){
1160 7b27f0dc koutsoub
                                        read = read+",";
1161 7b27f0dc koutsoub
                                }
1162 7b27f0dc koutsoub
                                read = read + (p.getGroup()==null?p.getUser():p.getGroup());
1163 7b27f0dc koutsoub
                        }
1164 7b27f0dc koutsoub
                        
1165 7b27f0dc koutsoub
                        
1166 05a718db koutsoub
                }
1167 7b27f0dc koutsoub
                Log.d(LOG,"read:"+read);
1168 7b27f0dc koutsoub
                Log.d(LOG,"write:"+write);
1169 08922f29 koutsoub
                if(!"".equals(read)&&!"".equals(write)){
1170 08922f29 koutsoub
                        read = "read="+read;
1171 08922f29 koutsoub
                        write = "write="+write;
1172 08922f29 koutsoub
                        headers.put("X-Object-Sharing", read+";"+write);
1173 05a718db koutsoub
                }
1174 08922f29 koutsoub
                HttpBundle b = new ContainerObjectManager(getApplicationContext()).updateObject(objects.getContainerName(), objects.getCName(), "", null, headers);
1175 08922f29 koutsoub
                Log.i(LOG,"response:"+b.getResponse().getStatusLine().getStatusCode());
1176 08922f29 koutsoub
                return b;
1177 08922f29 koutsoub
        }
1178 08922f29 koutsoub
        
1179 08922f29 koutsoub
        public HttpBundle restoreVersion(String version) throws CloudServersException{
1180 08922f29 koutsoub
                Log.i(LOG,"Update version:"+version);
1181 08922f29 koutsoub
                Map<String,String> headers=new HashMap<String, String>();
1182 08922f29 koutsoub
                
1183 08922f29 koutsoub
                headers.put("X-Source-Object","/"+objects.getContainerName()+"/"+objects.getCName());
1184 08922f29 koutsoub
                headers.put("X-Source-Version",version);
1185 08922f29 koutsoub
                headers.put("Content-Range","bytes 0-/*");
1186 08922f29 koutsoub
                HttpBundle b = new ContainerObjectManager(getApplicationContext()).updateObject(objects.getContainerName(), objects.getCName(), "", "text/plain; charset=UTF-8", headers);
1187 08922f29 koutsoub
                Log.i(LOG,"response:"+b.getResponse().getStatusLine().getStatusCode());
1188 08922f29 koutsoub
                return b;
1189 05a718db koutsoub
        }
1190 05a718db koutsoub
        
1191 9c4430bd koutsoub
1192 3e180b04 Adam Menz
}