Update WWW
[ubi] / qml / ubi / FilesPage.qml
1 import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
2 import "components"
3 import Qt 4.7
4 import "u1.js" as U1
5 import "bytesconv.js" as Conv
6 import "UIConstants.js" as Const
7
8 Page {
9     id: root
10     title: qsTr("Files")
11
12     property variant secrets
13     property string resource_path: "/~/Ubuntu One";
14     property string content_path: "/content/~/Ubuntu One";
15     property variant properties
16     property string path
17     property string name
18
19     menu: [
20         [qsTr("Upload file"),false],
21         [qsTr("Rename"),false],
22         [qsTr("Delete"),false],
23         [qsTr("New folder"),false],
24         [qsTr("Refresh"),false]
25     ]
26
27     function menuFun(id) {
28         if(id==qsTr("Upload file")) {
29             mask.state = "dialog";
30             fileSelector.open();
31         }
32         if(id==qsTr("Refresh")) {
33             init(root.properties);
34         }
35         if(id==qsTr("Rename")) {
36             if(root.path=="/") {
37                 tip.show(qsTr("Root folder can't be renamed!"));
38             } else {
39                 dialogRename.open();
40             }
41         }
42         if(id==qsTr("Delete")) {
43             if(root.path=="/") {
44                 tip.show(qsTr("Root folder can't be deleted!"));
45             } else {
46                 dialogDelete.open();
47             }
48         }
49         if(id==qsTr("New folder")) {
50             dialogNew.open();
51         }
52     }
53
54     function init(prop)
55     {
56         if(mask.state!="defocused") {
57             mask.state = "busy";
58         }
59         if(root.properties && !prop) {
60             prop = root.properties;
61         }
62
63         secrets = {
64             token: Utils.token(),
65             secret: Utils.tokenSecret(),
66             consumer_key : Utils.customerKey(),
67             consumer_secret: Utils.customerSecret()
68         };
69         if(prop) {
70             root.path = prop.path;
71             root.name = U1.fixFilename(prop.path);
72             U1.getFiles(secrets,prop.resource_path,root);
73             resource_path = prop.resource_path;
74             content_path = prop.content_path;
75         } else {
76             root.path = "/";
77             root.name = "";
78             U1.getFileTree(secrets,root);
79         }
80         root.properties = prop;
81     }
82
83     function onResp(nodes)
84     {
85         createFilesView(nodes);
86     }
87
88     function onErr(status)
89     {
90         //console.log("onErr");
91         mask.state = "idle";
92         if(status==401) {
93             tip.show(qsTr("Authorization failed!"));
94         } else if(status==0) {
95             tip.show(qsTr("Unable to connect!"));
96         } else {
97             tip.show(qsTr("Error: ")+status);
98         }
99         pageStack.pop();
100     }
101
102
103     function onRespRename()
104     {
105         //console.log("onRespRename");
106         pageStack.prevPage().init();
107         mask.state = "idle";
108         tip.show(qsTr("Folder renamed!"));
109     }
110
111     function onErrRename(status)
112     {
113         //console.log("onErrRenamed");
114         mask.state = "idle";
115         if(status==401) {
116             tip.show(qsTr("Authorization failed!"));
117         } else {
118             tip.show(qsTr("Error: ")+status);
119         }
120     }
121
122     function onRespNew()
123     {
124         //console.log("onRespNew");
125         pageStack.currentPage.init();
126         mask.state = "idle";
127         tip.show(qsTr("New folder created!"));
128     }
129
130     function onErrNew(status)
131     {
132         onErrRename(status);
133     }
134
135     function createFilesView(nodes)
136     {
137         var i,l;
138         if(files.children.length>0) {
139             l = files.children.length;
140             for(i=0;i<l;++i) {
141                 files.children[i].destroy();
142             }
143         }
144         var component = Qt.createComponent("components/File.qml");
145         l = nodes.length;
146         //console.log("l="+l);
147         for (i=0; i<l; i++) {
148             var object = component.createObject(files);
149             var ind = nodes[i].path.lastIndexOf("/");
150             //object.textMax = root.width/17;
151             //console.log("ind="+ind);
152             if(ind>=0) {
153                 object.name = nodes[i].path.substr(ind+1);
154             }  else {
155                 object.name = nodes[i].path;
156             }
157             object.isDirectory = nodes[i].kind == "directory";
158             object.properties = nodes[i];
159             object.width = root.width;
160             //object.height = 50;
161             if(object.isDirectory) {
162                 /*if(!nodes[i].has_children) {
163                     object.description = "Empty";
164                 }*/
165                 object.clicked.connect(function(prop) {
166                             pageStack.push("FilesPage.qml");
167                             pageStack.currentPage.init(prop);
168                         });
169             } else {
170                 var txt = "" + Conv.bytesToSize(nodes[i].size);
171                 object.description = txt;
172                 object.isPublic = nodes[i].is_public;
173                 object.clicked.connect(function(prop) {
174                             pageStack.push("PropertiesPage.qml");
175                             pageStack.currentPage.init(prop);
176                         });
177
178                 ind = object.name.lastIndexOf(".");
179                 var ext = "";
180                 if(ind>=0) ext = object.name.substr(ind+1);
181                 if(ext=="jpg" || ext=="JPG" || ext=="Jpg" ||
182                    ext=="jpeg" || ext=="JPEG" || ext=="Jpeg" ||
183                    ext=="gif" || ext=="GIF" || ext=="Gif" ||
184                    ext=="png" || ext=="PNG" || ext=="Png") {
185                     object.isPhoto = true;
186                 }
187             }
188         }
189         if(mask.state!="defocused") {
190             mask.state = "idle";
191         }
192
193         if(files.children.length==0) {
194             empty.visible = true;
195         } else {
196             empty.visible = false;
197         }
198     }
199
200     Connections {
201         target: Utils
202         onFileUploaded: init(root.properties);
203     }
204
205     Flickable {
206         id: flickable
207         width: parent.width
208         height: parent.height
209         contentHeight: files.height+Const.SYSTEM_BAR_HEIGHT+2*Const.TEXT_MARGIN
210         //y: Const.SYSTEM_BAR_HEIGHT+Const.TEXT_MARGIN
211         y: Const.SYSTEM_BAR_HEIGHT+Const.DEFAULT_MARGIN
212         contentWidth: parent.width
213
214         Column {
215             id: files
216             //spacing: Const.DEFAULT_MARGIN
217             //x: Const.TEXT_MARGIN
218             add: Transition {
219                 NumberAnimation { properties: "opacity"; easing.type: Easing.InOutQuad }
220             }
221         }
222     }
223
224     Text {
225         id: empty
226         font.pixelSize: Const.DEFAULT_FONT_PIXEL_SIZE
227         color: Const.DEFAULT_FOREGROUND_COLOR
228         text: qsTr("Empty")
229         anchors.centerIn: parent
230         font.italic: true
231         visible: false
232     }
233
234     FileSelector {
235         id: fileSelector
236         z: 200
237         folder: Utils.lastFolder()=="" ? Const.DEFAULT_FOLDER : Utils.lastFolder()
238         folderOnly: false
239         onFileSelected: {
240             mask.state = "idle";
241             //console.log("selected: "+file+" "+U1.fixFolder(folder));
242             fileSelector.close();
243             Utils.setLastFolder(folder);
244             var path = content_path+"/"+file;
245             //console.log(path);
246             U1.uploadFile(secrets,root,path,file,folder,Utils);
247         }
248
249     }
250
251     function getParentPath(path) {
252         //console.log(path);
253         var ppath;
254         var ind = path.lastIndexOf("/");
255         if(ind>=0) {
256             ppath = path.substr(0,ind);
257         }
258         if(path=="") ppath = "/";
259
260         //console.log(ppath);
261         return ppath;
262     }
263
264     function trim(s) {
265         var l=0; var r=s.length -1;
266         while(l < s.length && s[l] == ' ')
267         {       l++; }
268         while(r > l && s[r] == ' ')
269         {       r-=1;   }
270         return s.substring(l, r+1);
271     }
272
273     Connections {
274         target: Utils
275         onFileDeleted: {
276             if(pageStack.currentPage==root) {
277                 mask.state = "idle";
278                 tip.show(qsTr("Folder was deleted!"));
279                 pageStack.pop();
280                 pageStack.currentPage.init();
281             }
282         }
283         onOperationError: {
284             if(pageStack.currentPage==root) {
285                 mask.state = "idle";
286                 if(status==401) {
287                     tip.show(qsTr("Authorization failed!"));
288                 } else {
289                     tip.show(qsTr("Error: ")+status);
290                 }
291             }
292         }
293     }
294
295     DialogYesNo {
296         id: dialogDelete
297         z: 200
298         text: qsTr("Delete folder?")
299         onOpened: mask.state = "dialog"
300         onClosed: {
301             mask.state = "idle";
302             if(ok) {
303                 mask.state = "busy";
304                 U1.deleteFile(secrets,properties.resource_path,root,Utils);
305             }
306         }
307         onCanceled: mask.state = "idle"
308     }
309
310     DialogInput {
311         id: dialogRename
312         z: 200
313         textWidth: root.width - 4*Const.DEFAULT_MARGIN
314         label: qsTr("Enter new folder name:")
315         placeholderText: root.name
316         onOpened: {
317             reset();
318             Utils.setOrientation("auto");
319             mask.state = "dialog";
320         }
321         onClosed: {
322             mask.state = "idle";
323             Utils.setOrientation(root.orientation);
324             var r = trim(resp);
325             if(r!="") {
326                 mask.state = "busy";
327                 var currentPath = root.properties.resource_path;
328                 var targetPath = getParentPath(root.properties.path)+"/"+resp;
329                 //console.log("targetPath: "+targetPath);
330                 U1.renameFile(secrets,currentPath,targetPath,root);
331             } else {
332                 tip.show(qsTr("Invalid folder name!"))
333             }
334         }
335         onCanceled: {
336             Utils.setOrientation(root.orientation);
337             mask.state = "idle";
338         }
339     }
340
341     DialogInput {
342         id: dialogNew
343         z: 200
344         textWidth: root.width - 4*Const.DEFAULT_MARGIN
345         label: qsTr("Enter new folder name:")
346         placeholderText: ""
347         onOpened: {
348             reset();
349             Utils.setOrientation("auto");
350             mask.state = "dialog";
351         }
352         onClosed: {
353             mask.state = "idle";
354             Utils.setOrientation(root.orientation);
355             var r = trim(resp);
356             if(r!="") {
357                 mask.state = "busy";
358                 var rpath;
359                 if(root.properties)
360                     rpath = root.properties.resource_path;
361                 else
362                     rpath = root.resource_path;
363                 var newPath = rpath+"/"+resp;
364                 //console.log("newPath: "+newPath);
365                 U1.newFolder(secrets,newPath,root);
366             } else {
367                 tip.show(qsTr("Invalid folder name!"))
368             }
369         }
370         onCanceled: {
371             Utils.setOrientation(root.orientation);
372             mask.state = "idle";
373         }
374     }
375 }