Update WWW
[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     menu: [
17         [qsTr("Download"),false],
18         [qsTr("Publish"),false],
19         [qsTr("Rename"),false],
20         [qsTr("Delete"),false]
21     ]
22
23     function menuFun(id) {
24         if(id==qsTr("Download")) {
25             fileSelector.state = "visible";
26         }
27         if(id==qsTr("Publish")) {
28             if(isPublic) {
29                 dialogStopPublish.open();
30             } else {
31                 dialogStartPublish.open();
32             }
33         }
34         if(id==qsTr("Rename")) {
35             dialogRename.open();
36         }
37         if(id==qsTr("Delete")) {
38             dialogDelete.open();
39         }
40     }
41
42     Connections {
43         target: Utils
44         onFileDeleted: {
45             mask.state = "idle";
46             tip.show(qsTr("File deleted!"));
47             pageStack.pop();
48             pageStack.currentPage.init();
49         }
50         onOperationError: {
51             mask.state = "idle";
52             if(status==401) {
53                 tip.show(qsTr("Authorization failed!"));
54             } else {
55                 tip.show(qsTr("Error: ")+status);
56             }
57         }
58     }
59
60     function init(prop)
61     {
62         secrets = {
63             token: Utils.token(),
64             secret: Utils.tokenSecret(),
65             consumer_key : Utils.customerKey(),
66             consumer_secret: Utils.customerSecret()
67         };
68
69         if(prop) {
70             var name = U1.fixFilename(prop.path);
71             //console.log(name);
72             filename.text = name;
73             var crd = new Date(); crd.setISO8601(prop.when_created);
74             var chd = new Date(); chd.setISO8601(prop.when_changed);
75             created.text = Qt.formatDateTime(crd, "d/M/yyyy h:mm");
76             changed.text = Qt.formatDateTime(chd, "d/M/yyyy h:mm");
77             size.text = Conv.bytesToSize(prop.size);
78             if(prop && prop.is_public) {
79                 url.text = prop.public_url;
80             }
81
82         } else {
83             tip.show(qsTr("Internal error!"));
84         }
85         root.properties = prop;
86         if(root.properties && root.properties.is_public) {
87             root.isPublic = true;
88         } else {
89             root.isPublic = false;
90         }
91     }
92
93     function setContentType(type)
94     {
95         //ctype.text = type;
96         //ctype.font.italic = false;
97     }
98
99     function onErr(status)
100     {
101         mask.state = "idle";
102         if(status==401) {
103             tip.show(qsTr("Authorization failed!"));
104         } else if(status==0) {
105             tip.show(qsTr("Unable to connect!"));
106         } else {
107             tip.show(qsTr("Error: ")+status);
108         }
109     }
110
111     function onRespRename(prop)
112     {
113         //console.log("onRespRename");
114         mask.state = "idle";
115         init(prop); pageStack.prevPage().init();
116         tip.show(qsTr("File renamed!"));
117     }
118
119     function onErrRename(status)
120     {
121         onErr(status);
122     }
123
124     function onRespStopPublishing(prop)
125     {
126         //console.log("onRespStopPublishing");
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         //console.log("onRespStartPublishing");
140         mask.state = "idle";
141         init(prop); pageStack.prevPage().init();
142         tip.show(qsTr("Publishing started!"));
143     }
144
145     function onErrStartPublishing(status)
146     {
147         onErr(status);
148     }
149
150     Flickable {
151         width: root.width
152         height: root.height
153         contentHeight: content.height+Const.SYSTEM_BAR_HEIGHT+Const.TEXT_MARGIN
154         y: Const.SYSTEM_BAR_HEIGHT+Const.TEXT_MARGIN
155
156         Column {
157             id: content
158             spacing: Const.DEFAULT_MARGIN
159             x: Const.TEXT_MARGIN
160
161             Text {
162                 font.pixelSize: 30
163                 color: "white"
164                 text: qsTr("File name:")
165             }
166             Text {
167                 id: filename
168                 font.pixelSize: 30
169                 color: "black"
170                 wrapMode: Text.Wrap
171                 width: root.width - 6*Const.DEFAULT_MARGIN
172             }
173             Line {
174                 width: root.width-2*Const.TEXT_MARGIN
175             }
176             Text {
177                 font.pixelSize: 30
178                 color: "white"
179                 text: qsTr("Size:")
180             }
181             Text {
182                 id: size
183                 font.pixelSize: 30
184                 color: "black"
185                 wrapMode: Text.Wrap
186             }
187             Line {
188                 width: root.width-2*Const.TEXT_MARGIN
189             }
190             Text {
191                 font.pixelSize: 30
192                 color: "white"
193                 text: qsTr("Created:")
194             }
195             Text {
196                 id: created
197                 font.pixelSize: 30
198                 color: "black"
199             }
200             Line {
201                 width: root.width-2*Const.TEXT_MARGIN
202             }
203             Text {
204                 font.pixelSize: 30
205                 color: "white"
206                 text: qsTr("Changed:")
207             }
208             Text {
209                 id: changed
210                 font.pixelSize: 30
211                 color: "black"
212             }
213             Line {
214                 width: root.width-2*Const.TEXT_MARGIN
215                 visible: root.isPublic
216             }
217             Text {
218                 font.pixelSize: 30
219                 color: "white"
220                 text: qsTr("Public URL:")
221                 visible: root.isPublic
222             }
223             Text {
224                 id: url
225                 font.pixelSize: 30
226                 color: "black"
227                 wrapMode: Text.Wrap
228                 width: root.width - 6*Const.DEFAULT_MARGIN
229                 visible: root.isPublic
230             }
231             Button {
232                 label: qsTr("Copy")
233                 fontSize: 25
234                 visible: root.isPublic
235                 onButtonClicked: {
236                     Utils.setClipboardText(url.text);
237                     tip.show(qsTr("Public URL copied to clipboard!"));
238                 }
239             }
240
241             /*Text {
242                 font.pixelSize: 30
243                 color: "white"
244                 text: qsTr("Preview:")
245             }
246             Rectangle {
247                 color: Const.TRANSPARENT
248                 height: 200; width: 200
249                 border.color: Const.DEFAULT_FOREGROUND_COLOR
250                 border.width: 1
251                 radius: 5
252             }*/
253
254             Spacer{}
255
256         }
257     }
258
259     FileSelector {
260         id: fileSelector
261         z: 200
262         y: 200
263         folder: Utils.lastFolder()=="" ? Const.DEFAULT_FOLDER : Utils.lastFolder()
264         onFolderSelected: {
265             fileSelector.state = "invisible";
266             U1.getFileContent(secrets,root,properties.content_path,folder,properties.size,Utils);
267             Utils.setLastFolder(folder);
268         }
269     }
270
271     DialogYesNo {
272         id: dialogDelete
273         z: 200
274         text: qsTr("Delete file?")
275         onOpened: mask.state = "dialog"
276         onClosed: {
277             mask.state = "idle";
278             if(ok) {
279                 mask.state = "busy";
280                 U1.deleteFile(secrets,properties.resource_path,root,Utils);
281             }
282         }
283         onCanceled: mask.state = "idle"
284     }
285
286     DialogYesNo {
287         id: dialogStopPublish
288         z: 200
289         text: qsTr("Stop publishing?")
290         onOpened: mask.state = "dialog"
291         onClosed: {
292             mask.state = "idle";
293             if(ok) {
294                 mask.state = "busy";
295                 var currentPath = root.properties.resource_path;
296                 U1.stopPublishing(root.secrets,currentPath,root);
297             }
298         }
299         onCanceled: mask.state = "idle"
300     }
301
302     DialogYesNo {
303         id: dialogStartPublish
304         z: 200
305         text: qsTr("Start 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.startPublishing(root.secrets,currentPath,root);
313             }
314         }
315         onCanceled: mask.state = "idle"
316     }
317
318
319     function getParentPath(path) {
320         //console.log(path);
321         var ppath;
322         var ind = path.lastIndexOf("/");
323         if(ind>=0) {
324             ppath = path.substr(0,ind);
325         }
326         if(path=="") ppath = "/";
327
328         //console.log(ppath);
329         return ppath;
330     }
331
332     function trim(s) {
333         var l=0; var r=s.length -1;
334         while(l < s.length && s[l] == ' ')
335         {       l++; }
336         while(r > l && s[r] == ' ')
337         {       r-=1;   }
338         return s.substring(l, r+1);
339     }
340
341     DialogInput {
342         id: dialogRename
343         z: 200
344         textWidth: root.width - 4*Const.DEFAULT_MARGIN
345         label: qsTr("Enter new file name:")
346         placeholderText: filename.text
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 currentPath = root.properties.resource_path;
359                 var targetPath = getParentPath(root.properties.path)+"/"+resp;
360                 //console.log("targetPath: "+targetPath);
361                 U1.renameFile(secrets,currentPath,targetPath,root);;
362             } else {
363                 tip.show(qsTr("Invalid file name!"))
364             }
365         }
366         onCanceled: {
367             Utils.setOrientation(root.orientation);
368             mask.state = "idle";
369         }
370     }
371
372 }