summaryrefslogtreecommitdiff
path: root/src/Application.vala
blob: d9edcaf4090f3658cc83a0a670b9bba75a890734 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

[DBus(name = "org.gnome.Shotwell.Authenticate")]
public interface AuthenticationReceiver : Object {
    public abstract void callback(string url) throws DBusError, IOError;
}

[DBus(name = "org.gnome.Shotwell.Authenticate")]
internal class AuthenticatorReceiverApp : Gtk.Application, AuthenticationReceiver {
    private Gee.HashMap<string, Spit.Publishing.AuthenticatedCallback>
        pending_auth_requests = new Gee.HashMap<string, Spit.Publishing.AuthenticatedCallback>();

    public AuthenticatorReceiverApp() {
        Object(application_id: "org.gnome.Shotwell", flags: GLib.ApplicationFlags.HANDLES_OPEN |
            GLib.ApplicationFlags.HANDLES_COMMAND_LINE);
    }
    public override bool dbus_register(DBusConnection connection, string object_path) throws Error {
        try {
            connection.register_object(object_path, this);
        } catch (IOError e) {
            warning("Failed to register authentication helper on session connection: %s", e.message);
        }
        return true;
    }


    internal void register_auth_callback(string cookie, Spit.Publishing.AuthenticatedCallback cb) {
        pending_auth_requests[cookie] = cb;
    }

    internal void unregister_auth_callback(string cookie) {
        pending_auth_requests.unset(cookie);
    }

    public void callback(string callback_url) throws DBusError, IOError {
        try {
            var uri = Uri.parse(callback_url, UriFlags.NONE);
            debug("Got authentication callback uri: %s", callback_url);
            // See if something is waiting for a pending authentication
            var query = uri.get_query();
            if (query == null || query == "") {
                debug("Callback does not have parameters. Not accepting");

                return;
            }
            var uri_params = Uri.parse_params(uri.get_query());
            if ("sw_auth_cookie" in uri_params) {
                var cookie = uri_params["sw_auth_cookie"];
                if (pending_auth_requests.has_key(cookie)) {
                    pending_auth_requests[cookie].authenticated(uri_params);
                    LibraryWindow.get_app().present();
                } else {
                    debug("No call-back registered for cookie %s, probably user cancelled", cookie);
                }
            } else if (uri.get_scheme().has_prefix("com.googleusercontent.apps")) {
                if (pending_auth_requests.has_key(uri.get_scheme())) {
                    pending_auth_requests[uri.get_scheme()].authenticated(uri_params);
                } else {
                    debug("No call-back registered for cookie %s, probably user cancelled", uri.get_scheme());
                }
            }
        } catch (Error error) {
            warning("Got invalid authentication call-back: %s", callback_url);
        }
    }
}

public class Application {
    public interface AuthCallback : Object {
        public abstract void authenticated(HashTable<string, string> params);
    }
    private static Application instance = null;
    private Gtk.Application system_app = null;
    private int system_app_run_retval = 0;
    private bool direct;

    public virtual signal void starting() {
    }

    public virtual signal void exiting(bool panicked) {
    }
    
    public virtual signal void init_done() {
    }

    private bool fixup_raw_thumbs = false;
    
    public void set_raw_thumbs_fix_required(bool should_fixup) {
        fixup_raw_thumbs = should_fixup;
    }

    public bool get_raw_thumbs_fix_required() {
        return fixup_raw_thumbs;
    }

    public Gtk.Application get_system_app () {
        return system_app;
    }

    private bool running = false;
    private bool exiting_fired = false;

    Gee.HashMap<string, AuthCallback> pending_auth_requests = new Gee.HashMap<string, AuthCallback>();

    private Application(bool is_direct) {
        if (is_direct) {
            // we allow multiple instances of ourself in direct mode, so DON'T
            // attempt to be unique.  We don't request any command-line handling
            // here because this is processed elsewhere, and we don't need to handle
            // command lines from remote instances, since we don't care about them.
            system_app = new Gtk.Application("org.gnome.Shotwell-Viewer", GLib.ApplicationFlags.HANDLES_OPEN |
                GLib.ApplicationFlags.NON_UNIQUE);
        } else {
            // we've been invoked in library mode; set up for uniqueness and handling
            // of incoming command lines from remote instances (needed for getting
            // storage device and camera mounts).
            system_app = new AuthenticatorReceiverApp();
        }

        // GLib will assert if we don't do this...
        try {
            system_app.register();
        } catch (Error e) {
            panic();
        }
        
        direct = is_direct;

        if (!direct) {
            system_app.command_line.connect(on_command_line);
            var action = new SimpleAction("authenticated", VariantType.STRING);
            system_app.add_action(action);
            action.activate.connect((a, p) => {
                try {
                    var uri = Uri.parse(p.get_string(), UriFlags.NONE);
                    debug("Got authentication callback uri: %s", p.get_string());
                    // See if something is waiting for a pending authentication
                    var uri_params = Uri.parse_params(uri.get_query());
                    if ("sw_auth_cookie" in uri_params) {
                        var cookie = uri_params["sw_auth_cookie"];
                        if (pending_auth_requests.has_key(cookie)) {
                            pending_auth_requests[cookie].authenticated(uri_params);
                        } else {
                            debug("No call-back registered for cookie %s, probably user cancelled", cookie);
                        }
                    }
                } catch (Error error) {
                    warning("Got invalid authentication call-back: %s", p.get_string());
                }
            });
        }

        system_app.activate.connect(on_activated);
        system_app.startup.connect(on_activated);
    }

    public static void register_auth_callback(string cookie, Spit.Publishing.AuthenticatedCallback cb) {
        var instance = get_instance();
        if (!instance.direct) {
            ((AuthenticatorReceiverApp)instance.system_app).register_auth_callback(cookie, cb);
        }
    }

    public static void unregister_auth_callback(string cookie) {
        var instance = get_instance();
        if (!instance.direct) {
            ((AuthenticatorReceiverApp)instance.system_app).unregister_auth_callback(cookie);
        }
    }

    public static double get_scale() {
        var instance = get_instance().system_app;
        unowned GLib.List<Gtk.Window> windows = instance.get_windows();

        if (windows == null)
            return 1.0;

        return windows.data.get_scale_factor();
    }

    /**
     * @brief This is a helper for library mode that should only be
     * called if we've gotten a camera mount and are _not_ the primary
     * instance.
     */
    public static void send_to_primary_instance(string[]? argv) {
        get_instance().system_app.run(argv);
    }
    
    /**
     * @brief A helper for library mode that tells the primary
     * instance to bring its window to the foreground.  This
     * should only be called if we are _not_ the primary instance.
     */
    public static void present_primary_instance() {
        get_instance().system_app.activate();
    }

    public static bool get_is_remote() {
        return get_instance().system_app.get_is_remote();
    }
    
    public static bool get_is_direct() {
        return get_instance().direct;
    }

    public static void set_accels_for_action (string action, string[] accel) {
        get_instance().system_app.set_accels_for_action (action, accel);
    }

    public static void set_menubar (GLib.MenuModel? model) {
        get_instance().system_app.set_menubar (model);
    }

    /**
     * @brief Signal handler for GApplication's 'activate' signal.
     *
     * The most likely scenario for this to be fired is if the user
     * either tried to run us twice in library mode, or we've just gotten
     * a camera/removable-storage mount; in either case, the remote instance
     * will trigger this and exit, and we'll need to bring the window back up...
     */
    public static void on_activated() {
        get_instance();

        LibraryWindow lw = AppWindow.get_instance() as LibraryWindow;
        if ((lw != null) && (!get_is_direct())) {
            LibraryWindow.get_app().present();
        }
    }

    /**
     * @brief Signal handler for GApplication's 'command-line' signal.
     *
     * Gets fired whenever a remote instance tries to run, usually
     * with an incoming camera connection.
     *
     * @note This does _not_ get called in direct-edit mode.
     */
    public static int on_command_line(ApplicationCommandLine acl) {
        string[]? argv = acl.get_arguments();

        if (argv != null) {
            foreach (string s in argv) {
                LibraryWindow lw = AppWindow.get_instance() as LibraryWindow;
                if (lw != null) {
                    lw.mounted_camera_shell_notification(s, false);
                }
            }
        }
        on_activated();
        return 0;
    }

    /**
     * @brief Initializes the Shotwell application object and prepares
     * it for use.
     *
     * @param is_direct Whether the application was invoked in direct
     * or in library mode; defaults to FALSE, that is, library mode.
     *
     * @note This MUST be called prior to calling get_instance(), as the
     * application needs to know what mode it was brought up in; failure to
     * call this first will lead to an assertion.
     */
    public static void init(bool is_direct = false) {
        if (instance == null)
            instance = new Application(is_direct);
    }

    public static void terminate() {
        get_instance().exit();
    }

    public static Application get_instance() {
        assert (instance != null);

        return instance;
    }

    public void start(string[]? argv = null) {
        if (running)
            return;

        running = true;

        starting();

        assert(AppWindow.get_instance() != null);
        system_app.add_window(AppWindow.get_instance());
        system_app_run_retval = system_app.run(argv);

        if (!direct) {
            system_app.command_line.disconnect(on_command_line);
        }

        system_app.activate.disconnect(on_activated);
        system_app.startup.disconnect(on_activated);

        running = false;
    }

    public void exit() {
        // only fire this once, but thanks to terminate(), it will be fired at least once (even
        // if start() is not called and "starting" is not fired)
        if (exiting_fired || !running)
            return;

        exiting_fired = true;

        exiting(false);

        system_app.release();
    }

    // This will fire the exiting signal with panicked set to true, but only if exit() hasn't
    // already been called.  This call will immediately halt the application.
    public void panic() {
        if (!exiting_fired) {
            exiting_fired = true;
            exiting(true);
        }
        Posix.exit(1);
    }

    /**
     * @brief Allows the caller to ask for some part of the desktop session's functionality to
     * be prevented from running; wrapper for Gtk.Application.inhibit().
     *
     * @note The return value is a 'cookie' that needs to be passed to 'uninhibit' to turn
     * off a requested inhibition and should be saved by the caller.
     */ 
    public uint inhibit(Gtk.ApplicationInhibitFlags what, string? reason="none given") {
        return system_app.inhibit(AppWindow.get_instance(), what, reason);
    }

    /**
     * @brief Turns off a previously-requested inhibition. Wrapper for
     * Gtk.Application.uninhibit().
     */
    public void uninhibit(uint cookie) {
        system_app.uninhibit(cookie);
    }

    public int get_run_return_value() {
        return system_app_run_retval;
    }
}