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