-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.vala
94 lines (77 loc) · 2.13 KB
/
metadata.vala
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
using GLib;
public class MetadataManager : Object
{
// temporary hack until we can iterate through hashtables
private const string[] keynames = { "x", "y", "width", "height",
"hscroll", "vscroll",
"cur_offset", "sel_offset",
"word-wrap" };
private string file_name;
private HashTable<string,string> keys = new HashTable<string,string> (str_hash, str_equal);
private bool modified = false;
public string hash {get; set construct;}
public MetadataManager (string hash) {
this.hash = hash;
}
construct {
string contents;
file_name = Application.get_config_dir() + "/metadata/" + hash;
if ( FileUtils.test (file_name, FileTest.EXISTS) ) {
try {
FileUtils.get_contents (file_name, out contents, null); // FIXME: use async gio?
var lines = contents.split ("\n");
foreach ( string line in lines ) {
var keyval = line.split (":");
if ( keyval[0] == null || keyval[1] == null )
continue;
keys.insert (keyval[0], keyval[1]);
}
} catch (FileError e) {
stdout.printf ("Error while loading metadata: %s\n", e.message);
}
}
}
public new string @get (string key) {
string val = keys.lookup (key);
if (val != null) {
return val;
} else {
return "";
}
}
public int get_int (string key) {
string r = keys.lookup (key);
if ( keys.lookup (key) != null )
return r.to_int ();
else
return -1;
}
public long get_long (string key) {
string r = keys.lookup (key);
if ( keys.lookup (key) != null )
return (long)r.to_int64 ();
else
return -1;
}
public new void @set (string key, string value) {
keys.replace (key, value);
modified = true;
}
public void write () {
if ( !modified ) return;
string output = "";
foreach (string key in keynames) {
string val = keys.lookup (key);
if ( val != null ) {
output = output + key + ":" + val + "\n";
}
}
try {
FileUtils.set_contents (file_name, output, -1);
}
catch (FileError e) {
stdout.printf ("Warning: Could not save metadata\n");
}
modified = false;
}
}