/loggerouter/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerouter/trunk
1 by Gustav Hartvigsson
* Inital release
1
/*
2
   This file is part of LoggerOuter.
3
4
 LoggerOuter is free software: you can redistribute it and/or modify it under the
5
 terms of the GNU Lesser General Public License as published by the Free Software
6
 Foundation, either version 3 of the License, or (at your option) any later
7
 version.
8
9
 LoggerOuter is distributed in the hope that it will be useful, but WITHOUT
10
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12
 License for more details.
13
14
 You should have received a copy of the GNU Lessel General Public License
15
 along with LoggerOuter. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
namespace LO {
19
20
21
  public  static string strip_prelude_of_path (string str) {
22
    string tmp_str;
23
    if (str_starts_with (str, "file://")) {
24
      print ("Icon is path!\n");
25
      tmp_str = str.substring (7).escape (null);
26
      print (@"$(tmp_str)\n");
27
    } else{
28
      tmp_str = str;
29
    }
30
    return tmp_str;
31
  }
32
33
  public static string str_escape_enviorment_variables (string str ) {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
34
    StringBuilder builder = new StringBuilder ();
35
    var enviorment = GLib.Environ.@get ();
36
37
    if (str_starts_with (str, "/")) {
38
      builder.append_c ('/');
39
    }
40
41
    var tokens = str.split ("/");
42
    builder.append (tokens[0]);
43
44
    for (size_t i = 1; i <= tokens.length; i++) {
45
      var t = tokens[i];
46
47
      if (str_starts_with (str, "$")) {
48
        builder.append (GLib.Environ.get_variable (enviorment, t));
49
      } else {
50
        builder.append (t);
51
      }
52
    }
53
54
    if (str_ends_with (str, "/")) {
55
      builder.append_c ('/');
56
    }
57
58
    return builder.str;
1 by Gustav Hartvigsson
* Inital release
59
  }
60
61
62
  // Fake old style gtk_main () from old GTK.
63
  void fake_gtk_main () {
64
    if ( Gtk.Window.get_toplevels () != null ){
65
      while (((GLib.ListStore)Gtk.Window.get_toplevels ()).get_n_items () > 0) {
66
        GLib.MainContext.default ().iteration (true);
67
      }
68
    }
69
  }
70
71
  void __dialog_response (int id) {
72
    dia.destroy ();
73
    app.can_quit = true;
74
  }
75
76
#if GTK_4_10
77
  Gtk.AlertDialog dia;
78
#else
79
  Gtk.MessageDialog dia;
80
#endif
81
82
  // Easier presentation of dialog...
5 by Gustav Hartvigsson
[timer.vala] Fixed the timeout being triggered even is user has pressed concle.
83
  void present_dialog (string? format, ...) {
1 by Gustav Hartvigsson
* Inital release
84
    bool use_fake_main_loop = false;
85
    Gtk.Window? parent = null;
86
    if (!Gtk.is_initialized ()) {
87
      Gtk.init ();
88
      use_fake_main_loop = true;
89
    } else {
90
      parent = app.window;
91
      app.can_quit = false;
92
      app.hold ();
93
    }
94
    string message = format.vprintf (va_list ());
95
    GLib.stderr.printf (message);
96
#if GTK_4_10
97
    dia = new Gtk.AlertDialog (message, null);
98
    dia.response.connect (__dialog_responce );
99
    dia.show (parent);
100
#else
101
    dia = new Gtk.MessageDialog (parent,
102
                                     Gtk.DialogFlags.USE_HEADER_BAR,
103
                                     Gtk.MessageType.ERROR,
104
                                     Gtk.ButtonsType.CANCEL, message, null);
105
    dia.response.connect (__dialog_response );
106
    dia.present ();
107
#endif
108
    if (use_fake_main_loop) {
109
      fake_gtk_main ();
110
    } else {
111
      app.release ();
112
    }
113
  }
114
115
  bool str_starts_with (string stack, string needle) {
116
    if (stack.length < needle.length ) {
117
      return false;
118
    }
119
    for (long i = 0; i < needle.length; i++) {
120
      if (stack[i] != needle[i]) {
121
        return false;
122
      }
123
    }
124
    return true;
125
  }
126
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
127
  bool str_ends_with (string stack, string needle) {
128
    if (stack.length < needle.length) {
129
      return false;
130
    }
131
    long stack_len = stack.length;
132
    for (long i = needle.length; i > 0; i--) {
133
      if (stack[i] != needle[stack_len]) {
134
        return false;
135
      }
136
      stack_len--;
137
    }
138
    return true;
139
  }
140
1 by Gustav Hartvigsson
* Inital release
141
142
  string resolve_path (string path) {
143
    string base_dir;
144
    string tmp_file;
145
    string out_path;
146
147
    if (path[0] == '/') {
148
      // Absolute Path
149
      out_path = path;
150
    } else {
151
      if (str_starts_with (path, "~/")) {
152
        base_dir = GLib.Environment.get_home_dir ();
153
        long index = path.index_of_char ('/');
154
155
       out_path = base_dir +  path.substring (index);
156
157
      } else {
158
        base_dir = GLib.Environment.get_current_dir () + "/";
159
        // either "./" or nothing at start.
160
        if (str_starts_with (path, "./")) {
161
162
          long index = path.index_of_char ('/') + 1;
163
          tmp_file = path.substring (index);
164
165
        } else {
166
          tmp_file = path;
167
        }
168
169
        out_path= base_dir + tmp_file;
170
      }
171
    }
172
    return out_path;
173
  }
174
}