~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/LinuxDistro.vala

  • Committer: Tony George
  • Date: 2016-08-13 04:16:47 UTC
  • Revision ID: tony.george.kol@gmail.com-20160813041647-ivf2g6rszt00xco5
Updated project structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * ProcStats.vala
 
4
 *
 
5
 * Copyright 2016 Tony George <teejee2008@gmail.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
20
 * MA 02110-1301, USA.
 
21
 *
 
22
 *
 
23
 */
 
24
 
 
25
using TeeJee.Logging;
 
26
using TeeJee.FileSystem;
 
27
using TeeJee.ProcessHelper;
 
28
 
 
29
public class LinuxDistro : GLib.Object{
 
30
 
 
31
        /* Class for storing information about Linux distribution */
 
32
 
 
33
        public string dist_id = "";
 
34
        public string description = "";
 
35
        public string release = "";
 
36
        public string codename = "";
 
37
 
 
38
        public LinuxDistro(){
 
39
                dist_id = "";
 
40
                description = "";
 
41
                release = "";
 
42
                codename = "";
 
43
        }
 
44
 
 
45
        public string full_name(){
 
46
                if (dist_id == ""){
 
47
                        return "";
 
48
                }
 
49
                else{
 
50
                        string val = "";
 
51
                        val += dist_id;
 
52
                        val += (release.length > 0) ? " " + release : "";
 
53
                        val += (codename.length > 0) ? " (" + codename + ")" : "";
 
54
                        return val;
 
55
                }
 
56
        }
 
57
 
 
58
        public static LinuxDistro get_dist_info(string root_path){
 
59
 
 
60
                /* Returns information about the Linux distribution
 
61
                 * installed at the given root path */
 
62
 
 
63
                LinuxDistro info = new LinuxDistro();
 
64
 
 
65
                string dist_file = root_path + "/etc/lsb-release";
 
66
                var f = File.new_for_path(dist_file);
 
67
                if (f.query_exists()){
 
68
 
 
69
                        /*
 
70
                                DISTRIB_ID=Ubuntu
 
71
                                DISTRIB_RELEASE=13.04
 
72
                                DISTRIB_CODENAME=raring
 
73
                                DISTRIB_DESCRIPTION="Ubuntu 13.04"
 
74
                        */
 
75
 
 
76
                        foreach(string line in file_read(dist_file).split("\n")){
 
77
 
 
78
                                if (line.split("=").length != 2){ continue; }
 
79
 
 
80
                                string key = line.split("=")[0].strip();
 
81
                                string val = line.split("=")[1].strip();
 
82
 
 
83
                                if (val.has_prefix("\"")){
 
84
                                        val = val[1:val.length];
 
85
                                }
 
86
 
 
87
                                if (val.has_suffix("\"")){
 
88
                                        val = val[0:val.length-1];
 
89
                                }
 
90
 
 
91
                                switch (key){
 
92
                                        case "DISTRIB_ID":
 
93
                                                info.dist_id = val;
 
94
                                                break;
 
95
                                        case "DISTRIB_RELEASE":
 
96
                                                info.release = val;
 
97
                                                break;
 
98
                                        case "DISTRIB_CODENAME":
 
99
                                                info.codename = val;
 
100
                                                break;
 
101
                                        case "DISTRIB_DESCRIPTION":
 
102
                                                info.description = val;
 
103
                                                break;
 
104
                                }
 
105
                        }
 
106
                }
 
107
                else{
 
108
 
 
109
                        dist_file = root_path + "/etc/os-release";
 
110
                        f = File.new_for_path(dist_file);
 
111
                        if (f.query_exists()){
 
112
 
 
113
                                /*
 
114
                                        NAME="Ubuntu"
 
115
                                        VERSION="13.04, Raring Ringtail"
 
116
                                        ID=ubuntu
 
117
                                        ID_LIKE=debian
 
118
                                        PRETTY_NAME="Ubuntu 13.04"
 
119
                                        VERSION_ID="13.04"
 
120
                                        HOME_URL="http://www.ubuntu.com/"
 
121
                                        SUPPORT_URL="http://help.ubuntu.com/"
 
122
                                        BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
 
123
                                */
 
124
 
 
125
                                foreach(string line in file_read(dist_file).split("\n")){
 
126
 
 
127
                                        if (line.split("=").length != 2){ continue; }
 
128
 
 
129
                                        string key = line.split("=")[0].strip();
 
130
                                        string val = line.split("=")[1].strip();
 
131
 
 
132
                                        switch (key){
 
133
                                                case "ID":
 
134
                                                        info.dist_id = val;
 
135
                                                        break;
 
136
                                                case "VERSION_ID":
 
137
                                                        info.release = val;
 
138
                                                        break;
 
139
                                                //case "DISTRIB_CODENAME":
 
140
                                                        //info.codename = val;
 
141
                                                        //break;
 
142
                                                case "PRETTY_NAME":
 
143
                                                        info.description = val;
 
144
                                                        break;
 
145
                                        }
 
146
                                }
 
147
                        }
 
148
                }
 
149
 
 
150
                return info;
 
151
        }
 
152
 
 
153
        public static string get_running_desktop_name(){
 
154
 
 
155
                /* Return the names of the current Desktop environment */
 
156
 
 
157
                int pid = -1;
 
158
 
 
159
                pid = get_pid_by_name("cinnamon");
 
160
                if (pid > 0){
 
161
                        return "Cinnamon";
 
162
                }
 
163
 
 
164
                pid = get_pid_by_name("xfdesktop");
 
165
                if (pid > 0){
 
166
                        return "Xfce";
 
167
                }
 
168
 
 
169
                pid = get_pid_by_name("lxsession");
 
170
                if (pid > 0){
 
171
                        return "LXDE";
 
172
                }
 
173
 
 
174
                pid = get_pid_by_name("gnome-shell");
 
175
                if (pid > 0){
 
176
                        return "Gnome";
 
177
                }
 
178
 
 
179
                pid = get_pid_by_name("wingpanel");
 
180
                if (pid > 0){
 
181
                        return "Elementary";
 
182
                }
 
183
 
 
184
                pid = get_pid_by_name("unity-panel-service");
 
185
                if (pid > 0){
 
186
                        return "Unity";
 
187
                }
 
188
 
 
189
                pid = get_pid_by_name("plasma-desktop");
 
190
                if (pid > 0){
 
191
                        return "KDE";
 
192
                }
 
193
 
 
194
                return "Unknown";
 
195
        }
 
196
 
 
197
}
 
198
 
 
199