~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/TeeJee.Misc.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
 * TeeJee.Misc.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
namespace TeeJee.Misc {
 
26
 
 
27
        /* Various utility functions */
 
28
 
 
29
        using Gtk;
 
30
        using TeeJee.Logging;
 
31
        using TeeJee.FileSystem;
 
32
        using TeeJee.ProcessHelper;
 
33
 
 
34
        // color format -------------------
 
35
        
 
36
        public static Gdk.RGBA hex_to_rgba (string hex_color){
 
37
 
 
38
                /* Converts the color in hex to RGBA */
 
39
 
 
40
                string hex = hex_color.strip().down();
 
41
                if (hex.has_prefix("#") == false){
 
42
                        hex = "#" + hex;
 
43
                }
 
44
 
 
45
                Gdk.RGBA color = Gdk.RGBA();
 
46
                if(color.parse(hex) == false){
 
47
                        color.parse("#000000");
 
48
                }
 
49
                color.alpha = 255;
 
50
 
 
51
                return color;
 
52
        }
 
53
 
 
54
        public static string rgba_to_hex (Gdk.RGBA color, bool alpha = false, bool prefix_hash = true){
 
55
 
 
56
                /* Converts the color in RGBA to hex */
 
57
 
 
58
                string hex = "";
 
59
 
 
60
                if (alpha){
 
61
                        hex = "%02x%02x%02x%02x".printf((uint)(Math.round(color.red*255)),
 
62
                                                                        (uint)(Math.round(color.green*255)),
 
63
                                                                        (uint)(Math.round(color.blue*255)),
 
64
                                                                        (uint)(Math.round(color.alpha*255)))
 
65
                                                                        .up();
 
66
                }
 
67
                else {
 
68
                        hex = "%02x%02x%02x".printf((uint)(Math.round(color.red*255)),
 
69
                                                                        (uint)(Math.round(color.green*255)),
 
70
                                                                        (uint)(Math.round(color.blue*255)))
 
71
                                                                        .up();
 
72
                }
 
73
 
 
74
                if (prefix_hash){
 
75
                        hex = "#" + hex;
 
76
                }
 
77
 
 
78
                return hex;
 
79
        }
 
80
 
 
81
        // localization --------------------
 
82
 
 
83
        public void set_numeric_locale(string type){
 
84
                Intl.setlocale(GLib.LocaleCategory.NUMERIC, type);
 
85
            Intl.setlocale(GLib.LocaleCategory.COLLATE, type);
 
86
            Intl.setlocale(GLib.LocaleCategory.TIME, type);
 
87
        }
 
88
        
 
89
        // timestamp ----------------
 
90
        
 
91
        public string timestamp (){
 
92
 
 
93
                /* Returns a formatted timestamp string */
 
94
 
 
95
                Time t = Time.local (time_t ());
 
96
                return t.format ("%H:%M:%S");
 
97
        }
 
98
 
 
99
        public string timestamp_numeric (){
 
100
 
 
101
                /* Returns a numeric timestamp string */
 
102
 
 
103
                return "%ld".printf((long) time_t ());
 
104
        }
 
105
 
 
106
        public string timestamp_for_path (){
 
107
 
 
108
                /* Returns a formatted timestamp string */
 
109
 
 
110
                Time t = Time.local (time_t ());
 
111
                return t.format ("%Y-%d-%m_%H-%M-%S");
 
112
        }
 
113
 
 
114
        // string formatting -------------------------------------------------
 
115
        
 
116
        public string format_duration (long millis){
 
117
 
 
118
                /* Converts time in milliseconds to format '00:00:00.0' */
 
119
 
 
120
            double time = millis / 1000.0; // time in seconds
 
121
 
 
122
            double hr = Math.floor(time / (60.0 * 60));
 
123
            time = time - (hr * 60 * 60);
 
124
            double min = Math.floor(time / 60.0);
 
125
            time = time - (min * 60);
 
126
            double sec = Math.floor(time);
 
127
 
 
128
        return "%02.0lf:%02.0lf:%02.0lf".printf (hr, min, sec);
 
129
        }
 
130
 
 
131
        public double parse_time (string time){
 
132
 
 
133
                /* Converts time in format '00:00:00.0' to milliseconds */
 
134
 
 
135
                string[] arr = time.split (":");
 
136
                double millis = 0;
 
137
                if (arr.length >= 3){
 
138
                        millis += double.parse(arr[0]) * 60 * 60;
 
139
                        millis += double.parse(arr[1]) * 60;
 
140
                        millis += double.parse(arr[2]);
 
141
                }
 
142
                return millis;
 
143
        }
 
144
 
 
145
        public string string_replace(string str, string search, string replacement, int count = -1){
 
146
                string[] arr = str.split(search);
 
147
                string new_txt = "";
 
148
                bool first = true;
 
149
                
 
150
                foreach(string part in arr){
 
151
                        if (first){
 
152
                                new_txt += part;
 
153
                        }
 
154
                        else{
 
155
                                if (count == 0){
 
156
                                        new_txt += search;
 
157
                                        new_txt += part;
 
158
                                }
 
159
                                else{
 
160
                                        new_txt += replacement;
 
161
                                        new_txt += part;
 
162
                                        count--;
 
163
                                }
 
164
                        }
 
165
                        first = false;
 
166
                }
 
167
 
 
168
                return new_txt;
 
169
        }
 
170
        
 
171
        public string escape_html(string html){
 
172
                return html
 
173
                .replace("&","&amp;")
 
174
                .replace("\"","&quot;")
 
175
                //.replace(" ","&nbsp;") //pango markup throws an error with &nbsp;
 
176
                .replace("<","&lt;")
 
177
                .replace(">","&gt;")
 
178
                ;
 
179
        }
 
180
 
 
181
        public string unescape_html(string html){
 
182
                return html
 
183
                .replace("&amp;","&")
 
184
                .replace("&quot;","\"")
 
185
                //.replace("&nbsp;"," ") //pango markup throws an error with &nbsp;
 
186
                .replace("&lt;","<")
 
187
                .replace("&gt;",">")
 
188
                ;
 
189
        }
 
190
 
 
191
        public string uri_encode(string path, bool encode_forward_slash){
 
192
                string uri = Uri.escape_string(path);
 
193
                if (!encode_forward_slash){
 
194
                        uri = uri.replace("%2F","/");
 
195
                }
 
196
                return uri;
 
197
        }
 
198
 
 
199
        public string uri_decode(string path){
 
200
                return Uri.unescape_string(path);
 
201
        }
 
202
 
 
203
        public DateTime datetime_from_string (string date_time_string){
 
204
 
 
205
                /* Converts date time string to DateTime
 
206
                 * 
 
207
                 * Supported inputs:
 
208
                 * 'yyyy-MM-dd'
 
209
                 * 'yyyy-MM-dd HH'
 
210
                 * 'yyyy-MM-dd HH:mm'
 
211
                 * 'yyyy-MM-dd HH:mm:ss'
 
212
                 * */
 
213
 
 
214
                string[] arr = date_time_string.replace(":"," ").replace("-"," ").strip().split(" ");
 
215
 
 
216
                int year  = (arr.length >= 3) ? int.parse(arr[0]) : 0;
 
217
                int month = (arr.length >= 3) ? int.parse(arr[1]) : 0;
 
218
                int day   = (arr.length >= 3) ? int.parse(arr[2]) : 0;
 
219
                int hour  = (arr.length >= 4) ? int.parse(arr[3]) : 0;
 
220
                int min   = (arr.length >= 5) ? int.parse(arr[4]) : 0;
 
221
                int sec   = (arr.length >= 6) ? int.parse(arr[5]) : 0;
 
222
 
 
223
                return new DateTime.utc(year,month,day,hour,min,sec);
 
224
        }
 
225
 
 
226
        public string break_string_by_word(string input_text){
 
227
                string text = "";
 
228
                string line = "";
 
229
                foreach(string part in input_text.split(" ")){
 
230
                        line += part + " ";
 
231
                        if (line.length > 50){
 
232
                                text += line.strip() + "\n";
 
233
                                line = "";
 
234
                        }
 
235
                }
 
236
                if (line.length > 0){
 
237
                        text += line;
 
238
                }
 
239
                if (text.has_suffix("\n")){
 
240
                        text = text[0:text.length-1].strip();
 
241
                }
 
242
                return text;
 
243
        }
 
244
 
 
245
        public string[] array_concat(string[] a, string[] b){
 
246
                string[] c = {};
 
247
                foreach(string str in a){ c += str; }
 
248
                foreach(string str in b){ c += str; }
 
249
                return c;
 
250
        }
 
251
 
 
252
        public string random_string(int length = 8, string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"){
 
253
                string random = "";
 
254
 
 
255
                for(int i=0;i<length;i++){
 
256
                        int random_index = Random.int_range(0,charset.length);
 
257
                        string ch = charset.get_char(charset.index_of_nth_char(random_index)).to_string();
 
258
                        random += ch;
 
259
                }
 
260
 
 
261
                return random;
 
262
        }
 
263
 
 
264
        private string pad_numbers_in_string(
 
265
                string input, int max_length = 3, char pad_char = '0'){
 
266
                        
 
267
                string sequence = "";
 
268
                string output = "";
 
269
                bool seq_started = false;
 
270
 
 
271
                unichar c;
 
272
                string character;
 
273
                for (int i = 0; input.get_next_char(ref i, out c);) {
 
274
                        character = c.to_string();
 
275
 
 
276
                        if (c.isdigit()){
 
277
                                sequence += character;
 
278
                                seq_started = true;
 
279
                        }
 
280
                        else{
 
281
                                if (seq_started){
 
282
                                        if ((max_length - sequence.length) > 0){
 
283
                                                sequence = string.nfill(max_length - sequence.length, pad_char) + sequence;
 
284
                                        }
 
285
                                        output += sequence;
 
286
                                        sequence = "";
 
287
                                        seq_started = false;
 
288
                                }
 
289
 
 
290
                                output += character;
 
291
                        }
 
292
                }
 
293
 
 
294
                //append remaining characters in sequence
 
295
                if (sequence.length > 0){
 
296
                        if ((max_length - sequence.length) > 0){
 
297
                                sequence = string.nfill(max_length - sequence.length, pad_char) + sequence;
 
298
                        }
 
299
                        output += sequence;
 
300
                        sequence = "";
 
301
                }
 
302
                                        
 
303
                return output;
 
304
        }
 
305
 
 
306
 
 
307
}