Initial commit
[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
15     menu: [
16         [qsTr("Download"),false],
17         [qsTr("Rename"),false],
18         [qsTr("Delete"),false]
19     ]
20
21     function menuFun(id) {
22         if(id==qsTr("Download")) {
23             fileSelector.state = "visible";
24         }
25         if(id==qsTr("Rename")) {
26             dialogRename.open();
27         }
28         if(id==qsTr("Delete")) {
29             dialogDelete.open();
30         }
31     }
32
33     Connections {
34         target: Utils
35         onFileDeleted: {
36             mask.state = "idle";
37             tip.show(qsTr("File deleted!"));
38             pageStack.pop();
39             pageStack.currentPage.init();
40         }
41         onOperationError: {
42             mask.state = "idle";
43             if(status==401) {
44                 tip.show(qsTr("Authorization failed!"));
45             } else {
46                 tip.show(qsTr("Error: ")+status);
47             }
48         }
49     }
50
51     function init(prop)
52     {
53         secrets = {
54             token: Utils.token(),
55             secret: Utils.tokenSecret(),
56             consumer_key : Utils.customerKey(),
57             consumer_secret: Utils.customerSecret()
58         };
59
60         if(prop) {
61             var name = U1.fixFilename(prop.path);
62             //console.log(name);
63             filename.text = name;
64             var crd = new Date(); crd.setISO8601(prop.when_created);
65             var chd = new Date(); chd.setISO8601(prop.when_changed);
66             created.text = Qt.formatDateTime(crd, "d/M/yyyy h:mm");
67             changed.text = Qt.formatDateTime(chd, "d/M/yyyy h:mm");
68             size.text = Conv.bytesToSize(prop.size);
69         } else {
70             tip.show(qsTr("Internal error!"));
71         }
72         root.properties = prop;
73     }
74
75     function setContentType(type)
76     {
77         //ctype.text = type;
78         //ctype.font.italic = false;
79     }
80
81     function onRespRename(prop)
82     {
83         //console.log("onRespRename");
84         mask.state = "idle";
85         init(prop); pageStack.prevPage().init();
86         tip.show(qsTr("File renamed!"));
87     }
88
89     function onErrRename(status)
90     {
91         //console.log("onErrRename");
92         mask.state = "idle";
93         if(status==401) {
94             tip.show(qsTr("Authorization failed!"));
95         } else if(status==0) {
96             tip.show(qsTr("Unable to connect!"));
97         } else {
98             tip.show(qsTr("Error: ")+status);
99         }
100     }
101
102     Flickable {
103         width: root.width
104         height: root.height
105         contentHeight: content.height+Const.SYSTEM_BAR_HEIGHT+Const.TEXT_MARGIN
106         y: Const.SYSTEM_BAR_HEIGHT+Const.TEXT_MARGIN
107
108         Column {
109             id: content
110             spacing: Const.DEFAULT_MARGIN
111             x: Const.TEXT_MARGIN
112
113             Text {
114                 font.pixelSize: 30
115                 color: "white"
116                 text: qsTr("File name:")
117             }
118             Text {
119                 id: filename
120                 font.pixelSize: 30
121                 color: "black"
122                 wrapMode: Text.Wrap
123                 width: root.width - 6*Const.DEFAULT_MARGIN
124             }
125             Line {
126                 width: root.width-2*Const.TEXT_MARGIN
127             }
128             Text {
129                 font.pixelSize: 30
130                 color: "white"
131                 text: qsTr("Size:")
132             }
133             Text {
134                 id: size
135                 font.pixelSize: 30
136                 color: "black"
137                 wrapMode: Text.Wrap
138             }
139             Line {
140                 width: root.width-2*Const.TEXT_MARGIN
141             }
142             Text {
143                 font.pixelSize: 30
144                 color: "white"
145                 text: qsTr("Created:")
146             }
147             Text {
148                 id: created
149                 font.pixelSize: 30
150                 color: "black"
151             }
152             Line {
153                 width: root.width-2*Const.TEXT_MARGIN
154             }
155             Text {
156                 font.pixelSize: 30
157                 color: "white"
158                 text: qsTr("Changed:")
159             }
160             Text {
161                 id: changed
162                 font.pixelSize: 30
163                 color: "black"
164             }
165             /*Text {
166                 font.pixelSize: 30
167                 color: "white"
168                 text: qsTr("Preview:")
169             }
170             Rectangle {
171                 color: Const.TRANSPARENT
172                 height: 200; width: 200
173                 border.color: Const.DEFAULT_FOREGROUND_COLOR
174                 border.width: 1
175                 radius: 5
176             }*/
177
178             Spacer{}
179
180         }
181     }
182
183     FileSelector {
184         id: fileSelector
185         z: 200
186         y: 200
187         folder: Utils.lastFolder()=="" ? Const.DEFAULT_FOLDER : Utils.lastFolder()
188         onFolderSelected: {
189             fileSelector.state = "invisible";
190             U1.getFileContent(secrets,root,properties.content_path,folder,properties.size,Utils);
191             Utils.setLastFolder(folder);
192         }
193     }
194
195     DialogYesNo {
196         id: dialogDelete
197         z: 200
198         text: qsTr("Delete file?")
199         onOpened: mask.state = "dialog"
200         onClosed: {
201             mask.state = "idle";
202             if(ok) {
203                 mask.state = "busy";
204                 U1.deleteFile(secrets,properties.resource_path,root,Utils);
205             }
206         }
207         onCanceled: mask.state = "idle"
208     }
209
210
211     function getParentPath(path) {
212         //console.log(path);
213         var ppath;
214         var ind = path.lastIndexOf("/");
215         if(ind>=0) {
216             ppath = path.substr(0,ind);
217         }
218         if(path=="") ppath = "/";
219
220         //console.log(ppath);
221         return ppath;
222     }
223
224     function trim(s) {
225         var l=0; var r=s.length -1;
226         while(l < s.length && s[l] == ' ')
227         {       l++; }
228         while(r > l && s[r] == ' ')
229         {       r-=1;   }
230         return s.substring(l, r+1);
231     }
232
233     DialogInput {
234         id: dialogRename
235         z: 200
236         textWidth: root.width - 4*Const.DEFAULT_MARGIN
237         label: qsTr("Enter new file name:")
238         placeholderText: filename.text
239         onOpened: {
240             reset();
241             Utils.setOrientation("auto");
242             mask.state = "dialog";
243         }
244         onClosed: {
245             mask.state = "idle";
246             Utils.setOrientation(root.orientation);
247             var r = trim(resp);
248             if(r!="") {
249                 mask.state = "busy";
250                 var currentPath = root.properties.resource_path;
251                 var targetPath = getParentPath(root.properties.path)+"/"+resp;
252                 //console.log("targetPath: "+targetPath);
253                 U1.renameFile(secrets,currentPath,targetPath,root);;
254             } else {
255                 tip.show(qsTr("Invalid file name!"))
256             }
257         }
258         onCanceled: {
259             Utils.setOrientation(root.orientation);
260             mask.state = "idle";
261         }
262     }
263
264 }