-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathItem.vala
More file actions
133 lines (125 loc) · 4.75 KB
/
Item.vala
File metadata and controls
133 lines (125 loc) · 4.75 KB
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
/*
* Copyright (c) 2020 - Today Goncalo Margalho (https://github.com/devalien)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Goncalo Margalho <g@margalho.info>
*/
public class Workspaces.Models.Item : Object {
public string id { get; set; }
public string name { get; set; }
public string icon { get; set; }
public string item_type { get; set; }
public string command { get; set; }
public string url { get; set; }
public bool auto_start { get; set; }
public bool run_in_terminal { get; set; }
public Workspaces.Models.AppInfo app_info {get; set;}
public string directory {get; set;}
public Item (string name) {
Object ();
this.id = Uuid.string_random ();
this.name = name;
this.command = "";
this.item_type = "Custom";
this.run_in_terminal = false;
this.auto_start = false;
}
public void execute_command () {
var to_run_command = prepare_command ();
if (is_flatpak () == true) {
to_run_command = "flatpak-spawn --host " + to_run_command;
}
try {
string[] ? argvp = null;
Shell.parse_argv (to_run_command, out argvp);
info ("Command to launch: %s".printf (to_run_command));
string[] env = Environ.get ();
string cdir = GLib.Environment.get_home_dir ();
Process.spawn_async (cdir,
argvp,
env,
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD | SpawnFlags.STDOUT_TO_DEV_NULL | SpawnFlags.STDERR_TO_DEV_NULL,
null,
null
);
} catch (SpawnError e) {
warning ("Error: %s\n", e.message);
} catch (ShellError e) {
warning ("Error: %s\n", e.message);
}
}
private string prepare_command () {
var c = "";
if (command != null) {
c = command;
}
switch (item_type) {
case "URL" :
if (url != null && url.length > 0) {
if (url.contains("http://") || url.contains("https://")) {
c = "xdg-open " + url;
} else {
info(_("Not valid link"));
var dialog_error = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, _("Not a valid link, must have http or https"));
dialog_error.run();
dialog_error.destroy();
}
}
break;
case "Directory" :
if (directory != null && directory.length > 0) {
if (GLib.FileUtils.test(directory, GLib.FileTest.IS_DIR)) {
c = "xdg-open " + directory;
} else {
info(_ ("Folder not found"));
var dialog_error = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, _("Folder not found"));
dialog_error.run();
dialog_error.destroy();
}
}
break;
case "Application" :
if (app_info != null && app_info.executable.length > 0) {
c = app_info.executable + " ";
break;
} else {
return "";
}
case "ApplicationDirectory" :
if (app_info != null && app_info.executable != null && app_info.executable.length > 0) {
var d = "";
if (directory != null && directory.length > 0) {
d = " " + directory;
}
c = app_info.executable + d;
break;
} else {
return "";
}
default :
c = command;
break;
}
if (run_in_terminal) {
c = "x-terminal-emulator -x " + c;
}
return c;
}
public string to_string () {
return @"[$(this.id)] $(this.name)";
}
}