more polishing
[rekonq:elproxys-rekonq.git] / src / data / downloads.js
1 function generateDownloadTabMockup()
2 {
3     var mockup = new Object;
4     mockup.title = "Download";
5     mockup.icon = "http://openiconlibrary.sourceforge.net/gallery2/open_icon_library-full/icons/png/22x22/actions/download.png";
6
7     var clearPrivateData = new Object;
8     clearPrivateData.text = "Clear Private Data";
9     clearPrivateData.toolTip = "Tool to clear the private data store by the browser.";
10     clearPrivateData.image = "http://openiconlibrary.sourceforge.net/gallery2/open_icon_library-full/icons/png/22x22/actions/edit-clear-2.png";
11     clearPrivateData.trigger = function() { console.log("Clear Private Data triggered"); return false; }
12
13     var clearHistory = new Object;
14     clearHistory.text = "Clear Download History";
15     clearHistory.toolTip = "Clear the references to past downloads.";
16     clearHistory.image = "http://openiconlibrary.sourceforge.net/gallery2/open_icon_library-full/icons/png/22x22/actions/edit-clear-2.png";
17     clearHistory.trigger = function() { console.log("Clear Download History"); return false; }
18
19     mockup.actions = [clearPrivateData, clearHistory];
20
21     function generateFakeItem()
22     {
23         var item = new Object;
24         var fileName = randomString(randomNumber(100) + 1);
25         item.fileName = fileName;
26         item.date = randomDate();
27         item.originUrl = "http://rekonq.org/" + fileName;
28         item.directoryUrl = "file:///home/ikipou/Download";
29         item.fileUrl = item.directoryUrl + "/" + fileName;
30         return item;
31     }
32
33     var downloadItemArray = [];
34     for (var i = 0; i < 5; ++i)
35         downloadItemArray.push(generateFakeItem());
36     mockup.downloadItems = downloadItemArray;
37
38     return mockup;
39 }
40
41 function updateProgressBar(progress)
42 {
43     this.setAttribute("value", progress);
44 }
45
46 function hideProgressBar(success)
47 {
48     var list = this.getElementsByTagName("progress");
49     var progress = null;
50     if (list.length > 0)
51         progress = list[0];
52     progress.style.opacity = 0;
53
54     if (!success) {
55         var errorSpan = document.createElement("span");
56         errorSpan.appendChild(document.createTextNode("an error occured"));
57         errorSpan.style.color = "red";
58         this.appendChild(errorSpan);
59     } else {
60         var childrenDiv=this.getElementsByTagName("div");
61         var openLinks;
62         for (i = 0; i < childrenDiv.length; i++)
63         {
64             if (childrenDiv[i].hasClass("openLinks"))
65                 openLinks = childrenDiv[i];
66         }
67         openLinks.style.display = "block";
68         openLinks.style.position = "relative";
69         openLinks.style.marginTop = (- parseInt(progress.computedStyle("height"))
70                                      - parseInt(progress.computedStyle("margin-top"))
71                                      - parseInt(progress.computedStyle("margin-bottom"))) + "px";
72         openLinks.style.opacity = 1;
73     }
74 }
75
76 function prependNewDownload(downloadItem)
77 {
78     var downloadItemElement = generateDownloadItemElement(downloadItem, false);
79     var contentElement = document.getElementById(contentsIdForTab(window.downloadTab));
80     if (contentElement == null)
81         return;
82     if (!contentElement.firstChild)
83         contentElement.appendChild(downloadItemElement);
84     else
85         contentElement.insertBefore(downloadItemElement, contentElement.firstChild);
86 }
87
88 function generateDownloadItemElement(downloadItem, finished)
89 {
90     var downloadItemElement = document.createElement("div");
91     downloadItemElement.addClass("download");
92
93     var icon = document.createElement("img")
94     icon.setAttribute("src", downloadItem.iconFile);
95     downloadItemElement.appendChild(icon)
96
97     var titleSpan = document.createElement("strong");
98     titleSpan.appendChild(document.createTextNode(downloadItem.fileName));
99     downloadItemElement.appendChild(titleSpan);
100
101     downloadItemElement.appendChild(document.createTextNode(" - "));
102
103     var dateSpan = document.createElement("em");
104     // TODO get localized date string
105     dateSpan.appendChild(document.createTextNode(utils.prettyDateTime(downloadItem.date)));
106     downloadItemElement.appendChild(dateSpan);
107
108     downloadItemElement.appendChild(document.createElement("br"));
109
110     var originUrlLink = document.createElement("a");
111     originUrlLink.setAttribute("href", downloadItem.originUrl);
112     originUrlLink.appendChild(document.createTextNode(downloadItem.originUrl));
113     downloadItemElement.appendChild(originUrlLink);
114
115     downloadItemElement.appendChild(document.createElement("br"));
116
117     // TODO : progress bar first for downloads in progress, and then only open actions
118     var openLinks = document.createElement("div");
119     openLinks.addClass("openLinks")
120     var openFolderLink = document.createElement("a");
121     openFolderLink.setAttribute("href", downloadItem.fileDirectory);
122     openFolderLink.appendChild(document.createTextNode(downloadItem.i18nOpenDir()));
123     openLinks.appendChild(openFolderLink);
124
125     openLinks.appendChild(document.createTextNode(" - "));
126
127     var openFileLink = document.createElement("a");
128     openFileLink.setAttribute("href", downloadItem.destinationUrl);
129     openFileLink.appendChild(document.createTextNode(downloadItem.i18nOpenFile()));
130     openLinks.appendChild(openFileLink);
131     if (!finished) {
132         openLinks.style.opacity = 0;
133         openLinks.style.display = "none";
134         var progress = document.createElement("progress");
135         progress.setAttribute("max", 100);
136         downloadItem.downloadProgress.connect(progress, updateProgressBar);
137         downloadItem.downloadFinished.connect(downloadItemElement, hideProgressBar);
138         downloadItemElement.appendChild(progress);
139     }
140     downloadItemElement.appendChild(openLinks);
141
142     return downloadItemElement;
143 }
144
145 function initDownloadsTabContent(contentElement)
146 {
147     // Insert the content
148     for (var i in downloadTab.downloadItems) {
149         var downloadItem = window.downloadTab.downloadItems[i];
150         contentElement.appendChild(generateDownloadItemElement(downloadItem, true));
151     }
152
153     downloadTab.newDownloadAdded.connect(prependNewDownload);
154 }
155
156 if (!window.downloadTab)
157     window.downloadTab = generateDownloadTabMockup();
158
159 registerTab(downloadTab, "downloads", initDownloadsTabContent);