-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.vala
143 lines (112 loc) · 3.13 KB
/
document.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
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
using GLib;
using Gtk;
public class Document : SourceBuffer
{
public string uri { get; set construct; }
private string file_name;
private string mime_type;
public MetadataManager metadata { get; set; }
public Document ( string uri ) {
this.uri = uri;
this.highlight_syntax = true;
}
construct {
SourceLanguage lang;
bool result_uncertain = false;
this.file_name = uri_to_filename (uri);
this.metadata = new MetadataManager (Uri.escape_string (this.file_name, "", false));
this.mime_type = g_content_type_guess (uri, new uchar[0], out result_uncertain);
if (result_uncertain)
this.mime_type = null;
var slm = new SourceLanguageManager ();
if (result_uncertain)
lang = slm.guess_language (this.file_name, null);
else
lang = slm.guess_language (null, this.mime_type);
if ( lang != null ) {
set_language (lang);
}
}
public bool open () {
// TODO: error dialogs, use gio
string contents;
try {
if ( ! FileUtils.get_contents (file_name, out contents, null) )
return false;
} catch (FileError e) {
stdout.printf ("Error while loading document: %s\n", e.message);
return false;
}
if ( ! contents.validate () ) {
/* this is lame .. */
try {
contents = convert (contents, -1, "UTF-8", "ISO-8859-2", null, null);
} catch (ConvertError e) {
stdout.printf ("Error while converting document: %s\n", e.message);
return false;
}
if ( ! contents.validate () ) {
return false;
}
}
begin_not_undoable_action ();
set_text (contents, -1);
end_not_undoable_action ();
set_modified (false);
return true;
}
public void save () {
return_if_fail (this != null);
if ( !get_modified () ) return;
string buffer;
buffer = Application.get_config_dir () + "/save_buffer";
TextIter start, end;
get_bounds (out start, out end);
string contents = get_text (start, end, false);
try {
FileUtils.set_contents (buffer, contents, -1);
} catch (FileError err) {
stdout.printf ("Error: Could not save document");
return;
}
if ( FileUtils.rename (buffer, file_name) == -1 ) {
stdout.printf ("Error: Could not save document");
return;
}
set_modified (false);
}
public string get_file_name () {
return file_name;
}
public string get_mimetype () {
return mime_type;
}
public string get_folder () {
var parts = file_name.reverse ().split ("/", 2);
return parts[1].reverse ();
}
public string get_basename () {
return Filename.display_basename (file_name);
}
public string get_title_string () {
string folder = get_folder ();
string home = Environment.get_home_dir ();
if ( folder == home ) {
folder = "";
} else if ( folder.has_prefix (home) ) {
folder = " (" + folder.offset (home.len ()+1) + ")";
} else {
folder = " (" + folder + ")";
}
return get_basename () + folder;
}
public static string uri_to_filename (string uri) {
string f = uri;
// only local URIs are supported for now
if ( uri.has_prefix ("file:") )
f = uri.split ("://")[1];
else if ( ! Path.is_absolute (uri) )
f = Path.build_filename (Environment.get_current_dir (), uri);
return f;
}
}