WWW update
[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         mask.state = "idle";
116         init(prop); pageStack.prevPage().init();
117         tip.show(qsTr("File renamed!"));
118     }
119
120     function onErrRename(status)
121     {
122         onErr(status);
123     }
124
125     function onRespStopPublishing(prop)
126     {
127         mask.state = "idle";
128         init(prop); pageStack.prevPage().init();
129         tip.show(qsTr("Publishing stopped!"));
130     }
131
132     function onErrStopPublishing(status)
133     {
134         onErr(status);
135     }
136
137     function onRespStartPublishing(prop)
138     {
139         mask.state = "idle";
140         init(prop); pageStack.prevPage().init();
141         tip.show(qsTr("Publishing started!"));
142     }
143
144     function onErrStartPublishing(status)
145     {
146         onErr(status);
147     }
148
149     Flickable {
150         width: root.width
151         height: root.height
152         contentHeight: content.height+Const.TOP_BAR_HEIGHT+Const.SYSTEM_BAR_HEIGHT+Const.TEXT_MARGIN
153         y: Const.TOP_BAR_HEIGHT
154
155         Column {
156             id: content
157             spacing: Const.DEFAULT_MARGIN
158             x: Const.TEXT_MARGIN
159
160             Text {
161                 font.pixelSize: 30
162                 color: "white"
163                 text: qsTr("File name:")
164             }
165             Text {
166                 id: filename
167                 font.pixelSize: 30
168                 color: "black"
169                 wrapMode: Text.Wrap
170                 width: root.width - 6*Const.DEFAULT_MARGIN
171             }
172             Line {
173                 width: root.width-2*Const.TEXT_MARGIN
174             }
175             Text {
176                 font.pixelSize: 30
177                 color: "white"
178                 text: qsTr("Size:")
179             }
180             Text {
181                 id: size
182                 font.pixelSize: 30
183                 color: "black"
184                 wrapMode: Text.Wrap
185             }
186             Line {
187                 width: root.width-2*Const.TEXT_MARGIN
188             }
189             Text {
190                 font.pixelSize: 30
191                 color: "white"
192                 text: qsTr("Created:")
193             }
194             Text {
195                 id: created
196                 font.pixelSize: 30
197                 color: "black"
198             }
199             Line {
200                 width: root.width-2*Const.TEXT_MARGIN
201             }
202             Text {
203                 font.pixelSize: 30
204                 color: "white"
205                 text: qsTr("Changed:")
206             }
207             Text {
208                 id: changed
209                 font.pixelSize: 30
210                 color: "black"
211             }
212             Line {
213                 width: root.width-2*Const.TEXT_MARGIN
214                 visible: root.isPublic
215             }
216             Text {
217                 font.pixelSize: 30
218                 color: "white"
219                 text: qsTr("Public URL:")
220                 visible: root.isPublic
221             }
222             Text {
223                 id: url
224                 font.pixelSize: 30
225                 color: "black"
226                 wrapMode: Text.Wrap
227                 width: root.width - 6*Const.DEFAULT_MARGIN
228                 visible: root.isPublic
229             }
230             Button {
231                 label: qsTr("Copy")
232                 fontSize: 25
233                 visible: root.isPublic
234                 onButtonClicked: {
235                     Utils.setClipboardText(url.text);
236                     tip.show(qsTr("Public URL copied to clipboard!"));
237                 }
238             }
239             Spacer{}
240         }
241     }
242
243     FileDialog {
244         id: fileSelector
245         z: 200
246         hidden: true
247         folder: Utils.lastFolder()=="" ? Const.DEFAULT_FOLDER : Utils.lastFolder()
248         folderOnly: true
249         onFolderSelected: {
250             fileSelector.close();
251             U1.getFileContent(secrets,root,properties.content_path,folder,properties.size,Utils);
252             Utils.setLastFolder(folder);
253         }
254     }
255
256
257     DialogYesNo {
258         id: dialogDelete
259         z: 200
260         text: qsTr("Delete file?")
261         onOpened: mask.state = "dialog"
262         onClosed: {
263             mask.state = "idle";
264             if(ok) {
265                 mask.state = "busy";
266                 U1.deleteFile(secrets,properties.resource_path,root,Utils);
267             }
268         }
269         onCanceled: mask.state = "idle"
270     }
271
272     DialogYesNo {
273         id: dialogStopPublish
274         z: 200
275         text: qsTr("Stop publishing?")
276         onOpened: mask.state = "dialog"
277         onClosed: {
278             mask.state = "idle";
279             if(ok) {
280                 mask.state = "busy";
281                 var currentPath = root.properties.resource_path;
282                 U1.stopPublishing(root.secrets,currentPath,root);
283             }
284         }
285         onCanceled: mask.state = "idle"
286     }
287
288     DialogYesNo {
289         id: dialogStartPublish
290         z: 200
291         text: qsTr("Start publishing?")
292         onOpened: mask.state = "dialog"
293         onClosed: {
294             mask.state = "idle";
295             if(ok) {
296                 mask.state = "busy";
297                 var currentPath = root.properties.resource_path;
298                 U1.startPublishing(root.secrets,currentPath,root);
299             }
300         }
301         onCanceled: mask.state = "idle"
302     }
303
304
305     function getParentPath(path) {
306         var ppath;
307         var ind = path.lastIndexOf("/");
308         if(ind>=0) {
309             ppath = path.substr(0,ind);
310         }
311         if(path=="") ppath = "/";
312         return ppath;
313     }
314
315     function trim(s) {
316         var l=0; var r=s.length -1;
317         while(l < s.length && s[l] == ' ')
318         {       l++; }
319         while(r > l && s[r] == ' ')
320         {       r-=1;   }
321         return s.substring(l, r+1);
322     }
323
324     DialogInput {
325         id: dialogRename
326         z: 200
327         textWidth: root.width - 4*Const.DEFAULT_MARGIN
328         label: qsTr("Enter new file name:")
329         placeholderText: filename.text
330         onOpened: {
331             reset();
332             mask.state = "dialog";
333         }
334         onClosed: {
335             mask.state = "idle";
336             var r = trim(resp);
337             if(r!="") {
338                 mask.state = "busy";
339                 var currentPath = root.properties.resource_path;
340                 var targetPath = getParentPath(root.properties.path)+"/"+resp;
341                 U1.renameFile(secrets,currentPath,targetPath,root);;
342             } else {
343                 tip.show(qsTr("Invalid file name!"))
344             }
345         }
346         onCanceled: {
347             mask.state = "idle";
348         }
349     }
350
351     TaskMenu {
352         z: 200
353         id: taskMenu
354
355         contexMenu: true
356         menuDynamic: _menuDyn
357         menuHeight: menuDynamic.height+menuFixed.height+7*Const.DEFAULT_MARGIN
358
359         Flow {
360             id: _menuDyn
361
362             y: root.height-taskMenu.menuHeight-Const.SYSTEM_BAR_HEIGHT+2*Const.DEFAULT_MARGIN
363             x: Const.DEFAULT_MARGIN
364
365             width: parent.width-2*Const.DEFAULT_MARGIN
366             spacing: Const.DEFAULT_MARGIN
367
368             Button {
369                 label: qsTr("Download");
370                 onButtonClicked: {
371                     taskMenu.close();
372                     fileSelector.open();
373                 }
374             }
375
376             Button {
377                 label: qsTr("Publish");
378                 onButtonClicked: {
379                     taskMenu.close();
380                     if(isPublic) {
381                         dialogStopPublish.open();
382                     } else {
383                         dialogStartPublish.open();
384                     }
385                 }
386             }
387
388             Button {
389                 label: qsTr("Rename");
390                 onButtonClicked: {
391                     taskMenu.close();
392                     dialogRename.open();
393                 }
394             }
395
396             Button {
397                 label: qsTr("Delete");
398                 onButtonClicked: {
399                     taskMenu.close();
400                     dialogDelete.open();
401                 }
402             }
403         }
404     }
405
406 }