/loggerouter/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerouter/trunk
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
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
   This file is part of LoggerOuter.

 LoggerOuter is free software: you can redistribute it and/or modify it under the
 terms of the GNU Lesser General Public License as published by the Free Software
 Foundation, either version 3 of the License, or (at your option) any later
 version.

 LoggerOuter 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 Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lessel General Public License
 along with LoggerOuter. If not, see <https://www.gnu.org/licenses/>.
 */

namespace LO {


  public  static string strip_prelude_of_path (string str) {
    string tmp_str;
    if (str_starts_with (str, "file://")) {
      print ("Icon is path!\n");
      tmp_str = str.substring (7).escape (null);
      print (@"$(tmp_str)\n");
    } else{
      tmp_str = str;
    }
    return tmp_str;
  }

  public static string str_escape_enviorment_variables (string str ) {
    StringBuilder builder = new StringBuilder ();
    var enviorment = GLib.Environ.@get ();

    if (str_starts_with (str, "/")) {
      builder.append_c ('/');
    }

    var tokens = str.split ("/");
    builder.append (tokens[0]);

    for (size_t i = 1; i <= tokens.length; i++) {
      var t = tokens[i];

      if (str_starts_with (str, "$")) {
        builder.append (GLib.Environ.get_variable (enviorment, t));
      } else {
        builder.append (t);
      }
    }

    if (str_ends_with (str, "/")) {
      builder.append_c ('/');
    }

    return builder.str;
  }


  // Fake old style gtk_main () from old GTK.
  void fake_gtk_main () {
    if ( Gtk.Window.get_toplevels () != null ){
      while (((GLib.ListStore)Gtk.Window.get_toplevels ()).get_n_items () > 0) {
        GLib.MainContext.default ().iteration (true);
      }
    }
  }

  void __dialog_response (int id) {
    dia.destroy ();
    app.can_quit = true;
  }

#if GTK_4_10
  Gtk.AlertDialog dia;
#else
  Gtk.MessageDialog dia;
#endif

  // Easier presentation of dialog...
  void present_dialog (string? format, ...) {
    bool use_fake_main_loop = false;
    Gtk.Window? parent = null;
    if (!Gtk.is_initialized ()) {
      Gtk.init ();
      use_fake_main_loop = true;
    } else {
      parent = app.window;
      app.can_quit = false;
      app.hold ();
    }
    string message = format.vprintf (va_list ());
    GLib.stderr.printf (message);
#if GTK_4_10
    dia = new Gtk.AlertDialog (message, null);
    dia.response.connect (__dialog_responce );
    dia.show (parent);
#else
    dia = new Gtk.MessageDialog (parent,
                                     Gtk.DialogFlags.USE_HEADER_BAR,
                                     Gtk.MessageType.ERROR,
                                     Gtk.ButtonsType.CANCEL, message, null);
    dia.response.connect (__dialog_response );
    dia.present ();
#endif
    if (use_fake_main_loop) {
      fake_gtk_main ();
    } else {
      app.release ();
    }
  }

  bool str_starts_with (string stack, string needle) {
    if (stack.length < needle.length ) {
      return false;
    }
    for (long i = 0; i < needle.length; i++) {
      if (stack[i] != needle[i]) {
        return false;
      }
    }
    return true;
  }

  bool str_ends_with (string stack, string needle) {
    if (stack.length < needle.length) {
      return false;
    }
    long stack_len = stack.length;
    for (long i = needle.length; i > 0; i--) {
      if (stack[i] != needle[stack_len]) {
        return false;
      }
      stack_len--;
    }
    return true;
  }


  string resolve_path (string path) {
    string base_dir;
    string tmp_file;
    string out_path;

    if (path[0] == '/') {
      // Absolute Path
      out_path = path;
    } else {
      if (str_starts_with (path, "~/")) {
        base_dir = GLib.Environment.get_home_dir ();
        long index = path.index_of_char ('/');

       out_path = base_dir +  path.substring (index);

      } else {
        base_dir = GLib.Environment.get_current_dir () + "/";
        // either "./" or nothing at start.
        if (str_starts_with (path, "./")) {

          long index = path.index_of_char ('/') + 1;
          tmp_file = path.substring (index);

        } else {
          tmp_file = path;
        }

        out_path= base_dir + tmp_file;
      }
    }
    return out_path;
  }
}