-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRecently.xcsm
188 lines (153 loc) · 4.39 KB
/
Recently.xcsm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//xlang Source, Name:Recently.xcsm
//Date: Wed Sep 21:41:38 2018
class Recently{
public static Recently recent = new Recently();
public static class RecentlyObject {
public String name;
public String path;
public String date;
};
public Recently(){
loadRecently();
}
JsonObject recently;
public @NotNilptr String getRecentlyPath(){
String file = XPlatform.getAppDirectory();
return String.formatPath(file.appendPath("config").appendPath("recently.prop"), false);
}
public void loadRecently(){
String file = getRecentlyPath();
FileInputStream fis = nilptr;
try{
fis = new FileInputStream(file);
}catch(Exception e){
_system_.consoleWrite("canot read file " + file);
}
if (fis != nilptr){
byte []data = fis.read();
String content = new String(data);
try{
recently = new JsonObject(content);
}catch(Exception e){
}
}
if (recently == nilptr){
recently = new JsonObject();
}
}
public int getCount(){
try{
JsonArray node = (JsonArray)recently.get("projects");
if (node != nilptr){
return node.length();
}
}catch(Exception e){
}
return 0;
}
public RecentlyObject get(int i){
try{
JsonNode node = recently.get("projects");
if (node != nilptr){
JsonArray rootnode = (JsonArray)node;
JsonObject project = (JsonObject)rootnode.get(i);
if (project != nilptr){
RecentlyObject ro = new RecentlyObject();
ro.name = project.getString("name");
ro.path = project.getString("path");
ro.date = project.getString("date");
return ro;
}
}
}catch(Exception e){
}
return nilptr;
}
public void checktoArray(@NotNilptr JsonArray node, String name , String path){
try{
for (int i = 0, c = node.length(); i < c; i++){
JsonObject obj = (JsonObject )node.get(i);
if (obj != nilptr){
String ename = obj.getString("name");
String epath = obj.getString("path");
if (ename != nilptr && epath != nilptr){
if (ename.equals(name ) && epath.equals(path)){
node.remove(i);
break;
}
}
}
}
}catch(Exception e){
}
}
public void put(String name , String path){
String date = String.formatDate("%Y年 %m月 %d日 %H时 %M分"/*"%c"*/, _system_.currentTimeMillis());
JsonObject pnode = new JsonObject();
pnode.put("name", name);
pnode.put("path", path);
pnode.put("date", date);
JsonArray node = nilptr;
try{
node = (JsonArray)recently.get("projects");
if (node == nilptr){
node = new JsonArray();
recently.put("projects", node);
}
}catch(Exception e){
node = new JsonArray();
recently.put("projects", node);
}finally{
checktoArray(node, name, path);
node.put(pnode);
}
while (node.length() > 10){
node.remove(0);
}
save();
}
public void save(){
String file = getRecentlyPath();
FileOutputStream fos = nilptr;
try{
fos = new FileOutputStream(file);
}catch(Exception e){
_system_.consoleWrite("canot write file " + file);
}
if (fos != nilptr){
String content = recently.toString(true);
byte []data = content.getBytes();
try{
fos.write(data);
}catch(Exception e){
}
fos.close();
}
}
public static void putRecent(String name , String path){
recent.put(name, path);
}
public static int count(){
return recent.getCount();
}
public static String getLastPath(){
int c = count();
if (c > 0){
RecentlyObject ro = getItem(c - 1);
if (ro != nilptr){
return ro.path;
}
}
return nilptr;
}
public static RecentlyObject getItem(int i){
return recent.get(i);
}
public static bool isFirstRun(){
return false == XPlatform.existsSystemFile(recent.getRecentlyPath());
}
public static void mkFirstFile(){
long fp = XPlatform.openSystemFile(recent.getRecentlyPath(), "w");
_system_.closeFile(fp);
}
};