3b00ae7d66e0f31f4c848e7dba824057abdb8c14
[ubi] / qml / ubi / PropertiesPage.qml
1 import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
2 import "components"
3 import "UIConstants.js" as Const
4 import "ISOdate.js" as ISOdate
5 import "bytesconv.js" as Conv
6 import "u1.js" as U1
7
8 Page {
9     id: root
10     title: qsTr("File")
11
12     property variant secrets
13     property variant properties
14     property bool isPublic
15
16     property alias taskMenu: taskMenu
17
18     menu: [
19         [qsTr("Download"),false],
20         [qsTr("Publish"),false],
21         [qsTr("Rename"),false],
22         [qsTr("Delete"),false]
23     ]
24
25     function menuFun(id) {
26         if(id==qsTr("Download")) {
27             fileSelector.state = "visible";
28         }
29         if(id==qsTr("Publish")) {
30             if(isPublic) {
31                 dialogStopPublish.open();
32             } else {
33                 dialogStartPublish.open();
34             }
35         }
36         if(id==qsTr("Rename")) {
37             dialogRename.open();
38         }
39         if(id==qsTr("Delete")) {
40             dialogDelete.open();
41         }
42     }
43
44     Connections {
45         target: Utils
46         onFileDeleted: {
47             mask.state = "idle";
48             tip.show(qsTr("File deleted!"));
49             pageStack.pop();
50             pageStack.currentPage.init();
51         }
52         onOperationError: {
53             mask.state = "idle";
54             if(status==401) {
55                 tip.show(qsTr("Ubuntu One authorization has failed. Try once again or check login settings."));
56             } else {
57                 tip.show(qsTr("Unknown error: ")+status);
58             }
59         }
60     }
61
62     function init(prop)
63     {
64         secrets = {
65             token: Utils.token(),
66             secret: Utils.tokenSecret(),
67             consumer_key : Utils.customerKey(),
68             consumer_secret: Utils.customerSecret()
69         };
70
71         if(prop) {
72             var name = U1.fixFilename(prop.path);
73             //console.log(name);
74             filename.text = name;
75             var crd = new Date(); crd.setISO8601(prop.when_created);
76             var chd = new Date(); chd.setISO8601(prop.when_changed);
77             created.text = Qt.formatDateTime(crd, "d/M/yyyy h:mm");
78             changed.text = Qt.formatDateTime(chd, "d/M/yyyy h:mm");
79             size.text = Conv.bytesToSize(prop.size);
80             if(prop && prop.is_public) {
81                 url.text = prop.public_url;
82             }
83
84         } else {
85             tip.show(qsTr("Internal error!"));
86         }
87         root.properties = prop;
88         if(root.properties && root.properties.is_public) {
89             root.isPublic = true;
90         } else {
91             root.isPublic = false;
92         }
93     }
94
95     function setContentType(type)
96     {
97         //ctype.text = type;
98         //ctype.font.italic = false;
99     }
100
101     function onErr(status)
102     {
103         mask.state = "idle";
104         if(status==401) {
105             tip.show(qsTr("Ubuntu One authorization has failed. Try once again or check login settings."));
106         } else if(status==0) {
107             tip.show(qsTr("Unable to connect. Check internet connection."));
108         } else {
109             tip.show(qsTr("Unknown error: ")+status);
110         }
111     }
112
113     function onRespRename(prop)
114     {
115         //console.log("onRespRename");
116         mask.state = "idle";
117         init(prop); pageStack.prevPage().init();
118         tip.show(qsTr("File renamed!"));
119     }
120
121     function onErrRename(status)
122     {
123         onErr(status);
124     }
125
126     function onRespStopPublishing(prop)
127     {
128         //console.log("onRespStopPublishing");
129         mask.state = "idle";
130         init(prop); pageStack.prevPage().init();
131         tip.show(qsTr("Publishing stopped!"));
132     }
133
134     function onErrStopPublishing(status)
135     {
136         onErr(status);
137     }
138
139     function onRespStartPublishing(prop)
140     {
141         //console.log("onRespStartPublishing");
142         mask.state = "idle";
143         init(prop); pageStack.prevPage().init();
144         tip.show(qsTr("Publishing started!"));
145     }
146
147     function onErrStartPublishing(status)
148     {
149         onErr(status);
150     }
151
152     Flickable {
153         width: root.width
154         height: root.height
155         contentHeight: content.height+Const.TOP_BAR_HEIGHT+Const.SYSTEM_BAR_HEIGHT+Const.TEXT_MARGIN
156         y: Const.TOP_BAR_HEIGHT
157
158         Column {
159             id: content
160             spacing: Const.DEFAULT_MARGIN
161             x: Const.TEXT_MARGIN
162
163             Text {
164                 font.pixelSize: 30
165                 color: "white"
166                 text: qsTr("File name:")
167             }
168             Text {
169                 id: filename
170                 font.pixelSize: 30
171                 color: "black"
172                 wrapMode: Text.Wrap
173                 width: root.width - 6*Const.DEFAULT_MARGIN
174             }
175             Line {
176                 width: root.width-2*Const.TEXT_MARGIN
177             }
178             Text {
179                 font.pixelSize: 30
180                 color: "white"
181                 text: qsTr("Size:")
182             }
183             Text {
184                 id: size
185                 font.pixelSize: 30
186                 color: "black"
187                 wrapMode: Text.Wrap
188             }
189             Line {
190                 width: root.width-2*Const.TEXT_MARGIN
191             }
192             Text {
193                 font.pixelSize: 30
194                 color: "white"
195                 text: qsTr("Created:")
196             }
197             Text {
198                 id: created
199                 font.pixelSize: 30
200                 color: "black"
201             }
202             Line {
203                 width: root.width-2*Const.TEXT_MARGIN
204             }
205             Text {
206                 font.pixelSize: 30
207                 color: "white"
208                 text: qsTr("Changed:")
209             }
210             Text {
211                 id: changed
212                 font.pixelSize: 30
213                 color: "black"
214             }
215             Line {
216                 width: root.width-2*Const.TEXT_MARGIN
217                 visible: root.isPublic
218             }
219             Text {
220                 font.pixelSize: 30
221                 color: "white"
222                 text: qsTr("Public URL:")
223                 visible: root.isPublic
224             }
225             Text {
226                 id: url
227                 font.pixelSize: 30
228                 color: "black"
229                 wrapMode: Text.Wrap
230                 width: root.width - 6*Const.DEFAULT_MARGIN
231                 visible: root.isPublic
232             }
233             Button {
234                 label: qsTr("Copy")
235                 fontSize: 25
236                 visible: root.isPublic
237                 onButtonClicked: {
238                     Utils.setClipboardText(url.text);
239                     tip.show(qsTr("Public URL copied to clipboard!"));
240                 }
241             }
242
243             /*Text {
244                 font.pixelSize: 30
245                 color: "white"
246                 text: qsTr("Preview:")
247             }
248             Rectangle {
249                 color: Const.TRANSPARENT
250                 height: 200; width: 200
251                 border.color: Const.DEFAULT_FOREGROUND_COLOR
252                 border.width: 1
253                 radius: 5
254             }*/
255
256             Spacer{}
257
258         }
259     }
260
261     /*FileSelector {
262         id: fileSelector
263         z: 200
264         y: 200
265         folder: Utils.lastFolder()=="" ? Const.DEFAULT_FOLDER : Utils.lastFolder()
266         onFolderSelected: {
267             fileSelector.state = "invisible";
268             U1.getFileContent(secrets,root,properties.content_path,folder,properties.size,Utils);
269             Utils.setLastFolder(folder);
270         }
271     }*/
272
273     FileDialog {
274         id: fileSelector
275         z: 200
276         hidden: true
277         folder: Utils.lastFolder()=="" ? Const.DEFAULT_FOLDER : Utils.lastFolder()
278         folderOnly: true
279         onFolderSelected: {
280             fileSelector.close();
281             U1.getFileContent(secrets,root,properties.content_path,folder,properties.size,Utils);
282             Utils.setLastFolder(folder);
283         }
284     }
285
286
287     DialogYesNo {
288         id: dialogDelete
289         z: 200
290         text: qsTr("Delete file?")
291         onOpened: mask.state = "dialog"
292         onClosed: {
293             mask.state = "idle";
294             if(ok) {
295                 mask.state = "busy";
296                 U1.deleteFile(secrets,properties.resource_path,root,Utils);
297             }
298         }
299         onCanceled: mask.state = "idle"
300     }
301
302     DialogYesNo {
303         id: dialogStopPublish
304         z: 200
305         text: qsTr("Stop publishing?")
306         onOpened: mask.state = "dialog"
307         onClosed: {
308             mask.state = "idle";
309             if(ok) {
310                 mask.state = "busy";
311                 var currentPath = root.properties.resource_path;
312                 U1.stopPublishing(root.secrets,currentPath,root);
313             }
314         }
315         onCanceled: mask.state = "idle"
316     }
317
318     DialogYesNo {
319         id: dialogStartPublish
320         z: 200
321         text: qsTr("Start publishing?")
322         onOpened: mask.state = "dialog"
323         onClosed: {
324             mask.state = "idle";
325             if(ok) {
326                 mask.state = "busy";
327                 var currentPath = root.properties.resource_path;
328                 U1.startPublishing(root.secrets,currentPath,root);
329             }
330         }
331         onCanceled: mask.state = "idle"
332     }
333
334
335     function getParentPath(path) {
336         //console.log(path);
337         var ppath;
338         var ind = path.lastIndexOf("/");
339         if(ind>=0) {
340             ppath = path.substr(0,ind);
341         }
342         if(path=="") ppath = "/";
343
344         //console.log(ppath);
345         return ppath;
346     }
347
348     function trim(s) {
349         var l=0; var r=s.length -1;
350         while(l < s.length && s[l] == ' ')
351         {       l++; }
352         while(r > l && s[r] == ' ')
353         {       r-=1;   }
354         return s.substring(l, r+1);
355     }
356
357     DialogInput {
358         id: dialogRename
359         z: 200
360         textWidth: root.width - 4*Const.DEFAULT_MARGIN
361         label: qsTr("Enter new file name:")
362         placeholderText: filename.text
363         onOpened: {
364             reset();
365             Utils.setOrientation("auto");
366             mask.state = "dialog";
367         }
368         onClosed: {
369             mask.state = "idle";
370             Utils.setOrientation(root.orientation);
371             var r = trim(resp);
372             if(r!="") {
373                 mask.state = "busy";
374                 var currentPath = root.properties.resource_path;
375                 var targetPath = getParentPath(root.properties.path)+"/"+resp;
376                 //console.log("targetPath: "+targetPath);
377                 U1.renameFile(secrets,currentPath,targetPath,root);;
378             } else {
379                 tip.show(qsTr("Invalid file name!"))
380             }
381         }
382         onCanceled: {
383             Utils.setOrientation(root.orientation);
384             mask.state = "idle";
385         }
386     }
387
388     /*
389     menu: [
390         [qsTr("Download"),false],
391         [qsTr("Publish"),false],
392         [qsTr("Rename"),false],
393         [qsTr("Delete"),false]
394     ]
395
396     function menuFun(id) {
397         if(id==qsTr("Download")) {
398             fileSelector.state = "visible";
399         }
400         if(id==qsTr("Publish")) {
401             if(isPublic) {
402                 dialogStopPublish.open();
403             } else {
404                 dialogStartPublish.open();
405             }
406         }
407         if(id==qsTr("Rename")) {
408             dialogRename.open();
409         }
410         if(id==qsTr("Delete")) {
411             dialogDelete.open();
412         }
413     }*/
414
415     TaskMenu {
416         z: 200
417         id: taskMenu
418
419         contexMenu: true
420         menuDynamic: _menuDyn
421         menuHeight: menuDynamic.height+menuFixed.height+7*Const.DEFAULT_MARGIN
422
423         Flow {
424             id: _menuDyn
425
426             y: root.height-taskMenu.menuHeight-Const.SYSTEM_BAR_HEIGHT+2*Const.DEFAULT_MARGIN
427             x: Const.DEFAULT_MARGIN
428
429             width: parent.width-2*Const.DEFAULT_MARGIN
430             spacing: Const.DEFAULT_MARGIN
431
432             Button {
433                 label: qsTr("Download");
434                 onButtonClicked: {
435                     taskMenu.close();
436                     fileSelector.open();
437                 }
438             }
439
440             Button {
441                 label: qsTr("Publish");
442                 onButtonClicked: {
443                     taskMenu.close();
444                     if(isPublic) {
445                         dialogStopPublish.open();
446                     } else {
447                         dialogStartPublish.open();
448                     }
449                 }
450             }
451
452             Button {
453                 label: qsTr("Rename");
454                 onButtonClicked: {
455                     taskMenu.close();
456                     dialogRename.open();
457                 }
458             }
459
460             Button {
461                 label: qsTr("Delete");
462                 onButtonClicked: {
463                     taskMenu.close();
464                     dialogDelete.open();
465                 }
466             }
467         }
468     }
469
470 }