]> git.saurik.com Git - cydia.git/blob - Cydia.app/storage.js
Apple's Reachability code is ludicrously complex (and APSL).
[cydia.git] / Cydia.app / storage.js
1 var colors = ["#9090e0", "#4d4d70", "#7d7da0", "#7da0e0", "#d0d0f0", "#7070e0"];
2
3 var list = function (legend, color, name, value) {
4 legend.append('<div class="key">' +
5 '<div class="color" style="background-color: ' + color + '"><div></div></div>' +
6 '<div class="name">' + name + ' (' + Math.round(value * 1000) / 10 + '%)</div>' +
7 '</div>');
8 };
9
10 console.log(cydia.statfs("/"));
11
12 var cut = function (parent, color, fraction, z) {
13 var deg = Math.round(360 * fraction);
14 if (deg < 2)
15 deg = 2;
16 parent.append('<div class="xslice" style="' +
17 'background-color: ' + color + ';' +
18 '-webkit-transform: rotate(' + deg + 'deg);' +
19 'z-index: ' + z + ';' +
20 '"></div>');
21 };
22
23 var chart = function (right, left, slices) {
24 var total = 0;
25 for (var i = 0; i != slices.length; ++i) {
26 var slice = slices[i];
27 var z = slices.length - i;
28 if (slice[1] > 0.5)
29 cut(right, slice[0], total + 0.5, z);
30 total += slice[1];
31 cut(total > 0.5 ? left : right, slice[0], total, z);
32 }
33 };
34
35 var setup = function (name, root, folders) {
36 var size = $("#" + name + "-size");
37 var statfs = cydia.statfs(root);
38 var kb = statfs[0] * statfs[1] / 1024;
39 var total = kb / 1024;
40
41 var unit;
42 if (total < 1000)
43 unit = 'M';
44 else {
45 total = total / 1024;
46 unit = 'G'
47 }
48
49 size.html(Math.round(total * 10) / 10 + " " + unit);
50
51 var legend = $("#" + name + "-legend");
52 var used = 0;
53
54 var slices = [];
55
56 if (folders != null)
57 for (var i = 0; i != folders.length; ++i) {
58 var folder = folders[i];
59 var usage = cydia.du(folder[1]);
60 if (usage == null)
61 usage = 0;
62 var color = colors[i + 2];
63 var percent = usage / kb;
64 list(legend, color, folder[0], percent);
65 slices.push([color, percent]);
66 used += usage;
67 }
68
69 var free = statfs[0] * statfs[2] / 1024;
70 var other = (kb - free - used) / kb;
71
72 slices.push([colors[0], other]);
73 chart($("#" + name + "-right"), $("#" + name + "-left"), slices);
74
75 list(legend, colors[0], folders == null ? "Used" : "Other", other);
76 list(legend, colors[1], "Free", statfs[2] / statfs[1]);
77 };
78
79 $(function () {
80 setup("system", "/", null);
81
82 setup("private", "/private/var", [
83 ["Themes", "/Library/Themes/"],
84 ["iTunes", "/var/mobile/Media/iTunes_Control/"],
85 ["App Store", "/var/mobile/Applications/"],
86 ["Camera", "/var/mobile/Media/DCIM/"]
87 ]);
88 });