8dfebd1dc922887f706b6531ac5f65a857fdad5c
[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
240             Spacer{}
241
242         }
243     }
244
245     FileDialog {
246         id: fileSelector
247         z: 200
248         hidden: true
249         folder: Utils.lastFolder()=="" ? Const.DEFAULT_FOLDER : Utils.lastFolder()
250         folderOnly: true
251         onFolderSelected: {
252             fileSelector.close();
253             U1.getFileContent(secrets,root,properties.content_path,folder,properties.size,Utils);
254             Utils.setLastFolder(folder);
255         }
256     }
257
258
259     DialogYesNo {
260         id: dialogDelete
261         z: 200
262         text: qsTr("Delete file?")
263         onOpened: mask.state = "dialog"
264         onClosed: {
265             mask.state = "idle";
266             if(ok) {
267                 mask.state = "busy";
268                 U1.deleteFile(secrets,properties.resource_path,root,Utils);
269             }
270         }
271         onCanceled: mask.state = "idle"
272     }
273
274     DialogYesNo {
275         id: dialogStopPublish
276         z: 200
277         text: qsTr("Stop publishing?")
278         onOpened: mask.state = "dialog"
279         onClosed: {
280             mask.state = "idle";
281             if(ok) {
282                 mask.state = "busy";
283                 var currentPath = root.properties.resource_path;
284                 U1.stopPublishing(root.secrets,currentPath,root);
285             }
286         }
287         onCanceled: mask.state = "idle"
288     }
289
290     DialogYesNo {
291         id: dialogStartPublish
292         z: 200
293         text: qsTr("Start publishing?")
294         onOpened: mask.state = "dialog"
295         onClosed: {
296             mask.state = "idle";
297             if(ok) {
298                 mask.state = "busy";
299                 var currentPath = root.properties.resource_path;
300                 U1.startPublishing(root.secrets,currentPath,root);
301             }
302         }
303         onCanceled: mask.state = "idle"
304     }
305
306
307     function getParentPath(path) {
308         var ppath;
309         var ind = path.lastIndexOf("/");
310         if(ind>=0) {
311             ppath = path.substr(0,ind);
312         }
313         if(path=="") ppath = "/";
314         return ppath;
315     }
316
317     function trim(s) {
318         var l=0; var r=s.length -1;
319         while(l < s.length && s[l] == ' ')
320         {       l++; }
321         while(r > l && s[r] == ' ')
322         {       r-=1;   }
323         return s.substring(l, r+1);
324     }
325
326     DialogInput {
327         id: dialogRename
328         z: 200
329         textWidth: root.width - 4*Const.DEFAULT_MARGIN
330         label: qsTr("Enter new file name:")
331         placeholderText: filename.text
332         onOpened: {
333             reset();
334             Utils.setOrientation("auto");
335             mask.state = "dialog";
336         }
337         onClosed: {
338             mask.state = "idle";
339             Utils.setOrientation(root.orientation);
340             var r = trim(resp);
341             if(r!="") {
342                 mask.state = "busy";
343                 var currentPath = root.properties.resource_path;
344                 var targetPath = getParentPath(root.properties.path)+"/"+resp;
345                 U1.renameFile(secrets,currentPath,targetPath,root);;
346             } else {
347                 tip.show(qsTr("Invalid file name!"))
348             }
349         }
350         onCanceled: {
351             Utils.setOrientation(root.orientation);
352             mask.state = "idle";
353         }
354     }
355
356     TaskMenu {
357         z: 200
358         id: taskMenu
359
360         contexMenu: true
361         menuDynamic: _menuDyn
362         menuHeight: menuDynamic.height+menuFixed.height+7*Const.DEFAULT_MARGIN
363
364         Flow {
365             id: _menuDyn
366
367             y: root.height-taskMenu.menuHeight-Const.SYSTEM_BAR_HEIGHT+2*Const.DEFAULT_MARGIN
368             x: Const.DEFAULT_MARGIN
369
370             width: parent.width-2*Const.DEFAULT_MARGIN
371             spacing: Const.DEFAULT_MARGIN
372
373             Button {
374                 label: qsTr("Download");
375                 onButtonClicked: {
376                     taskMenu.close();
377                     fileSelector.open();
378                 }
379             }
380
381             Button {
382                 label: qsTr("Publish");
383                 onButtonClicked: {
384                     taskMenu.close();
385                     if(isPublic) {
386                         dialogStopPublish.open();
387                     } else {
388                         dialogStartPublish.open();
389                     }
390                 }
391             }
392
393             Button {
394                 label: qsTr("Rename");
395                 onButtonClicked: {
396                     taskMenu.close();
397                     dialogRename.open();
398                 }
399             }
400
401             Button {
402                 label: qsTr("Delete");
403                 onButtonClicked: {
404                     taskMenu.close();
405                     dialogDelete.open();
406                 }
407             }
408         }
409     }
410
411 }