~ztefn/haguichi/1.4

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * This file is part of Haguichi, a graphical frontend for Hamachi.
 * Copyright (C) 2007-2021 Stephen Brandt <stephen@stephenbrandt.com>
 *
 * Haguichi is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 */

using Gtk;
using Config;

class Haguichi : Gtk.Application
{
    public static Gtk.Application app;
#if ENABLE_APPINDICATOR
    public static HaguichiIndicator indicator;
#endif
    public static HaguichiWindow window;
    public static Dialogs.Preferences preferences_dialog;
    
    public static Object modal_dialog;
    
    public static Connection connection;
    public static AppSession session;
    public static Inhibitor inhibitor;
    
    public static string current_desktop;
    
    public static bool running_in_flatpak;
    public static bool window_use_header_bar;
    public static bool dialog_use_header_bar;
    
    public static bool hidden;
    public static bool debugging;
    public static bool demo_mode;
    public static string demo_list_path;
    
    public static ThreadPool<Member>  member_threads;
    public static ThreadPool<Network> network_threads;
    
    private static int activate_count;
    private static uint registration_id;
    private static int64 startup_moment;
    
    public Haguichi ()
    {
        Object (application_id: "com.github.ztefn.haguichi", flags: ApplicationFlags.FLAGS_NONE);
    }
    
    public override void activate ()
    {
        activate_count ++;
        
        if (activate_count > 1)
        {
            Debug.log (Debug.domain.ENVIRONMENT, "Haguichi.activate", "Received activate signal, presenting window");
            Haguichi.window.present();
        }
    }
    
    public override bool dbus_register (DBusConnection connection, string object_path) throws Error
    {
        base.dbus_register (connection, object_path);
        
        session = new AppSession();
        registration_id = connection.register_object ("/com/github/ztefn/haguichi", session);
        
        return true;
    }
    
    public override void dbus_unregister (DBusConnection connection, string object_path)
    {
        connection.unregister_object (registration_id);
        
        base.dbus_unregister (connection, object_path);
    }
    
    public override void startup ()
    {
        base.startup();
        
        startup_moment = get_real_time();
        
        Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
        Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        Intl.textdomain (GETTEXT_PACKAGE);
        
        Debug.log (Debug.domain.INFO, "Haguichi.startup", "Greetings, I am " + Text.app_name + " " + Text.app_version);
        
        running_in_flatpak = FileUtils.test ("/.flatpak-info", FileTest.EXISTS);
        Debug.log (Debug.domain.ENVIRONMENT, "Haguichi.startup", "Running inside Flatpak sandbox: " + running_in_flatpak.to_string());
        Command.spawn_wrap = running_in_flatpak ? "flatpak-spawn --host " : "";
        
#if USE_LIBHANDY
        Hdy.init();
#endif
        Text.init();
        Settings.init();
        
        current_desktop = Environment.get_variable ("XDG_CURRENT_DESKTOP");
        
#if FOR_ELEMENTARY
        window_use_header_bar = true;
        dialog_use_header_bar = false;
        Gtk.Settings.get_default().set ("gtk-application-prefer-dark-theme", true);
        Gtk.Settings.get_default().set ("gtk-theme-name", "io.elementary.stylesheet.blueberry");
        Gtk.Settings.get_default().set ("gtk-icon-theme-name", "elementary");
#else
        // Only on specific desktops we use header bars and possibly dark theme
        if ((current_desktop.contains ("GNOME")) ||
            (current_desktop == "Deepin") ||
            (current_desktop == "KDE") ||
            (current_desktop == "LXQt") ||
            (current_desktop == "MATE") ||
            (current_desktop == "Pantheon") ||
            (current_desktop == "XFCE") ||
            (current_desktop == "X-Cinnamon"))
        {
            window_use_header_bar = true;
            Gtk.Settings.get_default().get ("gtk-dialogs-use-header", ref dialog_use_header_bar);
            Gtk.Settings.get_default().set ("gtk-application-prefer-dark-theme", (bool) Settings.prefer_dark_theme.val);
            
            // Add 52 pixels offset for all GTK+ versions before 3.20
            if (Gtk.check_version (3, 20, 0) != null)
            {
            	Settings.decorator_offset = 52;
            }
        }
#endif
        
        GlobalActions.init (app);
        
        window = new HaguichiWindow();
        add_window (window);
        
        preferences_dialog = new Dialogs.Preferences();
        
#if ENABLE_APPINDICATOR
        indicator = new HaguichiIndicator();
        indicator.active = (bool) Settings.show_indicator.val;
#endif
        
        connection = new Connection();
        inhibitor = new Inhibitor();
        
        try
        {
            member_threads  = new ThreadPool<Member>.with_owned_data  ((member)  => { member.get_long_nick_thread();        }, 2, false);
            network_threads = new ThreadPool<Network>.with_owned_data ((network) => { network.determine_ownership_thread(); }, 2, false);
        }
        catch (ThreadError e)
        {
            Debug.log (Debug.domain.ERROR, "Haguichi.startup", e.message);
        }
        
        Controller.init();
        
        Debug.log (Debug.domain.INFO, "Haguichi.startup", "Completed startup in " + (get_real_time() - startup_moment).to_string() + " microseconds");
    }
    
    public static int main (string[] args)
    {
        hidden = false;
        debugging = false;
        demo_mode = false;
        demo_list_path = null;
        
        foreach (string s in args)
        {
            if ((s == "-h") || (s == "--help"))
            {
                stdout.printf ("%s\n", Text.app_help);
                return 0;
            }
            if ((s == "-v") || (s == "--version"))
            {
                stdout.printf ("%s %s\n", Text.app_name, Text.app_version);
                return 0;
            }
            if (s == "--license")
            {
                stdout.printf ("\n%s\n\n%s\n\n", Text.app_info, Text.app_license);
                return 0;
            }
            
            if (s == "--hidden")
            {
                hidden = true;
            }
            else if ((s == "-d") || (s == "--debug"))
            {
                debugging = true;
            }
            else if (s == "--demo")
            {
                demo_mode = true;
            }
            else if (s.has_prefix ("--list="))
            {
                demo_list_path = s.replace ("--list=", "");
            }
            else if (s.has_prefix ("-"))
            {
                stdout.printf ("Unknown option %s\n\n%s\n", s, Text.app_help);
                return 0;
            }
        }
        
        app = new Haguichi();
        return app.run();
    }
}