-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.vala
161 lines (135 loc) · 4.15 KB
/
application.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using GLib;
using Gtk;
public class Application : GLib.Object
{
private static string config_dir;
private SList<BaseWindow> window_list;
private Unique.App instance;
private int _win_count = 0;
public int win_count {
get { return _win_count; }
set {
_win_count = value;
if ( _win_count <= 0 ) {
Gtk.main_quit ();
}
}
}
public static string get_config_dir () {
return config_dir;
}
[CCode (array_length = false)]
public void run (string[] args) {
Gtk.init (ref args);
// set this envvar to anything before starting glasscat if you don't want it
// to attach to your current instance during development.
string suffix = Environment.get_variable ("GLASSCAT_INSTANCE");
this.instance = new Unique.App ("org.gnome.glasscat" + suffix, null);
// working around array length limitations in Vala
// TODO: check if still necessary
int num_args = -1;
foreach (string s in args)
num_args++;
string[] urilist = new string[num_args];
if ( this.instance.is_running ) {
for (int i = 0; i < num_args; i++)
urilist[i] = args[i + 1];
if ( urilist.length == 0 ) {
this.instance.send_message (Unique.Command.NEW, null);
} else {
var md = new Unique.MessageData ();
md.set_uris (urilist);
this.instance.send_message (Unique.Command.OPEN, md);
}
Gdk.notify_startup_complete ();
} else {
this.instance.message_received += on_message_received;
bool first = true;
int i = 0;
foreach (string doc in args) {
if (first) { first = false; continue; }
open_document (doc);
i++;
}
if ( i == 0 )
open_project ();
}
if ( win_count > 0 ) {
Gtk.AboutDialog.set_url_hook (url_hook);
Gtk.AboutDialog.set_email_hook (email_hook);
Gtk.main ();
}
}
private void url_hook (AboutDialog dialog, string link) {
try {
Process.spawn_command_line_async ("gnome-open " + link);
} catch (SpawnError e) {
stdout.printf ("Error: %s\n", e.message);
}
}
private void email_hook (AboutDialog dialog, string email) {
try {
Process.spawn_command_line_async ("gnome-open mailto:" + email);
} catch (SpawnError e) {
stdout.printf ("Error: %s\n", e.message);
}
}
private void open_project () {
var dialog = new Gtk.FileChooserDialog ("Create Project - Glasscat",
null,
FileChooserAction.SAVE,
STOCK_CANCEL, ResponseType.CANCEL,
"Create _Project", ResponseType.ACCEPT);
var response = dialog.run ();
if (response == ResponseType.ACCEPT) {
dialog.hide ();
this.win_count = this.win_count + 1;
var w = new ProjectWindow (dialog.get_uri ());
w.show_all ();
window_list.append (w);
w.destroy += w => { window_list.remove (w); win_count = win_count - 1; };
} else {
dialog.hide ();
}
}
private void open_document (string uri) {
foreach ( BaseWindow window in window_list ) {
if ( window.is_document (uri) ) {
window.present_with_time (time ());
return;
}
}
win_count = win_count + 1;
BaseWindow w;
if (g_content_type_guess (uri, new uchar[0], null) == "application/x-glasscat-project") {
w = new ProjectWindow (uri);
} else {
w = new DocumentWindow (uri);
}
w.show_all ();
w.present_with_time (time ());
window_list.append (w);
w.destroy += w => { window_list.remove (w); win_count--; };
}
static int main (string[] args) {
config_dir = Environment.get_home_dir () + "/.gnome2/glasscat";
DirUtils.create_with_parents ( Application.get_config_dir () + "/metadata/", 0750 );
var app = new Application ();
app.run (args);
return 0;
}
private Unique.Response on_message_received (Unique.App instance,
int command,
Unique.MessageData data,
uint time_)
{
if ( command == Unique.Command.OPEN ) {
foreach (string uri in data.get_uris ()) {
open_document (uri);
}
} else if ( command == Unique.Command.NEW ) {
open_project ();
}
return Unique.Response.OK;
}
}