/loggerouter/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerouter/trunk

« back to all changes in this revision

Viewing changes to src/tools.vala

  • Committer: Gustav Hartvigsson
  • Date: 2025-06-03 20:51:10 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20250603205110-zfozhyqh8werenbv
* Inital release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 ) {
 
34
    return str;
 
35
  }
 
36
 
 
37
 
 
38
  // Fake old style gtk_main () from old GTK.
 
39
  void fake_gtk_main () {
 
40
    if ( Gtk.Window.get_toplevels () != null ){
 
41
      while (((GLib.ListStore)Gtk.Window.get_toplevels ()).get_n_items () > 0) {
 
42
        GLib.MainContext.default ().iteration (true);
 
43
      }
 
44
    }
 
45
  }
 
46
 
 
47
  void __dialog_response (int id) {
 
48
    dia.destroy ();
 
49
    app.can_quit = true;
 
50
  }
 
51
 
 
52
#if GTK_4_10
 
53
  Gtk.AlertDialog dia;
 
54
#else
 
55
  Gtk.MessageDialog dia;
 
56
#endif
 
57
 
 
58
  // Easier presentation of dialog...
 
59
  void prensent_dialog (string? format, ...) {
 
60
    bool use_fake_main_loop = false;
 
61
    Gtk.Window? parent = null;
 
62
    if (!Gtk.is_initialized ()) {
 
63
      Gtk.init ();
 
64
      use_fake_main_loop = true;
 
65
    } else {
 
66
      parent = app.window;
 
67
      app.can_quit = false;
 
68
      app.hold ();
 
69
    }
 
70
    string message = format.vprintf (va_list ());
 
71
    GLib.stderr.printf (message);
 
72
#if GTK_4_10
 
73
    dia = new Gtk.AlertDialog (message, null);
 
74
    dia.response.connect (__dialog_responce );
 
75
    dia.show (parent);
 
76
#else
 
77
    dia = new Gtk.MessageDialog (parent,
 
78
                                     Gtk.DialogFlags.USE_HEADER_BAR,
 
79
                                     Gtk.MessageType.ERROR,
 
80
                                     Gtk.ButtonsType.CANCEL, message, null);
 
81
    dia.response.connect (__dialog_response );
 
82
    dia.present ();
 
83
#endif
 
84
    if (use_fake_main_loop) {
 
85
      fake_gtk_main ();
 
86
    } else {
 
87
      app.release ();
 
88
    }
 
89
  }
 
90
 
 
91
  bool str_starts_with (string stack, string needle) {
 
92
    if (stack.length < needle.length ) {
 
93
      return false;
 
94
    }
 
95
    for (long i = 0; i < needle.length; i++) {
 
96
      if (stack[i] != needle[i]) {
 
97
        return false;
 
98
      }
 
99
    }
 
100
    return true;
 
101
  }
 
102
 
 
103
 
 
104
  string resolve_path (string path) {
 
105
    string base_dir;
 
106
    string tmp_file;
 
107
    string out_path;
 
108
 
 
109
    if (path[0] == '/') {
 
110
      // Absolute Path
 
111
      out_path = path;
 
112
    } else {
 
113
      if (str_starts_with (path, "~/")) {
 
114
        base_dir = GLib.Environment.get_home_dir ();
 
115
        long index = path.index_of_char ('/');
 
116
 
 
117
       out_path = base_dir +  path.substring (index);
 
118
 
 
119
      } else {
 
120
        base_dir = GLib.Environment.get_current_dir () + "/";
 
121
        // either "./" or nothing at start.
 
122
        if (str_starts_with (path, "./")) {
 
123
 
 
124
          long index = path.index_of_char ('/') + 1;
 
125
          tmp_file = path.substring (index);
 
126
 
 
127
        } else {
 
128
          tmp_file = path;
 
129
        }
 
130
 
 
131
        out_path= base_dir + tmp_file;
 
132
      }
 
133
    }
 
134
    return out_path;
 
135
  }
 
136
}