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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::AppInfo;
use crate::File;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    /// Integrating the launch with the launching application. This is used to
    /// handle for instance startup notification and launching the new application
    /// on the same screen as the launching window.
    ///
    /// # Implements
    ///
    /// [`AppLaunchContextExt`][trait@crate::prelude::AppLaunchContextExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GAppLaunchContext")]
    pub struct AppLaunchContext(Object<ffi::GAppLaunchContext, ffi::GAppLaunchContextClass>);

    match fn {
        type_ => || ffi::g_app_launch_context_get_type(),
    }
}

impl AppLaunchContext {
    /// Creates a new application launch context. This is not normally used,
    /// instead you instantiate a subclass of this, such as `GdkAppLaunchContext`.
    ///
    /// # Returns
    ///
    /// a [`AppLaunchContext`][crate::AppLaunchContext].
    #[doc(alias = "g_app_launch_context_new")]
    pub fn new() -> AppLaunchContext {
        unsafe { from_glib_full(ffi::g_app_launch_context_new()) }
    }
}

impl Default for AppLaunchContext {
    fn default() -> Self {
        Self::new()
    }
}

pub const NONE_APP_LAUNCH_CONTEXT: Option<&AppLaunchContext> = None;

/// Trait containing all [`struct@AppLaunchContext`] methods.
///
/// # Implementors
///
/// [`AppLaunchContext`][struct@crate::AppLaunchContext]
pub trait AppLaunchContextExt: 'static {
    /// Gets the display string for the `self`. This is used to ensure new
    /// applications are started on the same display as the launching
    /// application, by setting the `DISPLAY` environment variable.
    /// ## `info`
    /// a [`AppInfo`][crate::AppInfo]
    /// ## `files`
    /// a `GList` of [`File`][crate::File] objects
    ///
    /// # Returns
    ///
    /// a display string for the display.
    #[doc(alias = "g_app_launch_context_get_display")]
    #[doc(alias = "get_display")]
    fn display<P: IsA<AppInfo>>(&self, info: &P, files: &[File]) -> Option<glib::GString>;

    /// Gets the complete environment variable list to be passed to
    /// the child process when `self` is used to launch an application.
    /// This is a [`None`]-terminated array of strings, where each string has
    /// the form `KEY=VALUE`.
    ///
    /// # Returns
    ///
    ///
    ///  the child's environment
    #[doc(alias = "g_app_launch_context_get_environment")]
    #[doc(alias = "get_environment")]
    fn environment(&self) -> Vec<std::ffi::OsString>;

    /// Initiates startup notification for the application and returns the
    /// `DESKTOP_STARTUP_ID` for the launched operation, if supported.
    ///
    /// Startup notification IDs are defined in the
    /// [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt).
    /// ## `info`
    /// a [`AppInfo`][crate::AppInfo]
    /// ## `files`
    /// a `GList` of of [`File`][crate::File] objects
    ///
    /// # Returns
    ///
    /// a startup notification ID for the application, or [`None`] if
    ///  not supported.
    #[doc(alias = "g_app_launch_context_get_startup_notify_id")]
    #[doc(alias = "get_startup_notify_id")]
    fn startup_notify_id<P: IsA<AppInfo>>(&self, info: &P, files: &[File])
        -> Option<glib::GString>;

    /// Called when an application has failed to launch, so that it can cancel
    /// the application startup notification started in [`startup_notify_id()`][Self::startup_notify_id()].
    /// ## `startup_notify_id`
    /// the startup notification id that was returned by [`startup_notify_id()`][Self::startup_notify_id()].
    #[doc(alias = "g_app_launch_context_launch_failed")]
    fn launch_failed(&self, startup_notify_id: &str);

    /// Arranges for `variable` to be set to `value` in the child's
    /// environment when `self` is used to launch an application.
    /// ## `variable`
    /// the environment variable to set
    /// ## `value`
    /// the value for to set the variable to.
    #[doc(alias = "g_app_launch_context_setenv")]
    fn setenv<P: AsRef<std::ffi::OsStr>, Q: AsRef<std::ffi::OsStr>>(&self, variable: P, value: Q);

    /// Arranges for `variable` to be unset in the child's environment
    /// when `self` is used to launch an application.
    /// ## `variable`
    /// the environment variable to remove
    #[doc(alias = "g_app_launch_context_unsetenv")]
    fn unsetenv<P: AsRef<std::ffi::OsStr>>(&self, variable: P);

    /// The ::launch-failed signal is emitted when a [`AppInfo`][crate::AppInfo] launch
    /// fails. The startup notification id is provided, so that the launcher
    /// can cancel the startup notification.
    /// ## `startup_notify_id`
    /// the startup notification id for the failed launch
    #[doc(alias = "launch-failed")]
    fn connect_launch_failed<F: Fn(&Self, &str) + 'static>(&self, f: F) -> SignalHandlerId;

    /// The ::launched signal is emitted when a [`AppInfo`][crate::AppInfo] is successfully
    /// launched. The `platform_data` is an GVariant dictionary mapping
    /// strings to variants (ie a{sv}), which contains additional,
    /// platform-specific data about this launch. On UNIX, at least the
    /// "pid" and "startup-notification-id" keys will be present.
    /// ## `info`
    /// the [`AppInfo`][crate::AppInfo] that was just launched
    /// ## `platform_data`
    /// additional platform-specific data for this launch
    #[doc(alias = "launched")]
    fn connect_launched<F: Fn(&Self, &AppInfo, &glib::Variant) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<AppLaunchContext>> AppLaunchContextExt for O {
    fn display<P: IsA<AppInfo>>(&self, info: &P, files: &[File]) -> Option<glib::GString> {
        unsafe {
            from_glib_full(ffi::g_app_launch_context_get_display(
                self.as_ref().to_glib_none().0,
                info.as_ref().to_glib_none().0,
                files.to_glib_none().0,
            ))
        }
    }

    fn environment(&self) -> Vec<std::ffi::OsString> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::g_app_launch_context_get_environment(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn startup_notify_id<P: IsA<AppInfo>>(
        &self,
        info: &P,
        files: &[File],
    ) -> Option<glib::GString> {
        unsafe {
            from_glib_full(ffi::g_app_launch_context_get_startup_notify_id(
                self.as_ref().to_glib_none().0,
                info.as_ref().to_glib_none().0,
                files.to_glib_none().0,
            ))
        }
    }

    fn launch_failed(&self, startup_notify_id: &str) {
        unsafe {
            ffi::g_app_launch_context_launch_failed(
                self.as_ref().to_glib_none().0,
                startup_notify_id.to_glib_none().0,
            );
        }
    }

    fn setenv<P: AsRef<std::ffi::OsStr>, Q: AsRef<std::ffi::OsStr>>(&self, variable: P, value: Q) {
        unsafe {
            ffi::g_app_launch_context_setenv(
                self.as_ref().to_glib_none().0,
                variable.as_ref().to_glib_none().0,
                value.as_ref().to_glib_none().0,
            );
        }
    }

    fn unsetenv<P: AsRef<std::ffi::OsStr>>(&self, variable: P) {
        unsafe {
            ffi::g_app_launch_context_unsetenv(
                self.as_ref().to_glib_none().0,
                variable.as_ref().to_glib_none().0,
            );
        }
    }

    fn connect_launch_failed<F: Fn(&Self, &str) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn launch_failed_trampoline<
            P: IsA<AppLaunchContext>,
            F: Fn(&P, &str) + 'static,
        >(
            this: *mut ffi::GAppLaunchContext,
            startup_notify_id: *mut libc::c_char,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                AppLaunchContext::from_glib_borrow(this).unsafe_cast_ref(),
                &glib::GString::from_glib_borrow(startup_notify_id),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"launch-failed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    launch_failed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_launched<F: Fn(&Self, &AppInfo, &glib::Variant) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn launched_trampoline<
            P: IsA<AppLaunchContext>,
            F: Fn(&P, &AppInfo, &glib::Variant) + 'static,
        >(
            this: *mut ffi::GAppLaunchContext,
            info: *mut ffi::GAppInfo,
            platform_data: *mut glib::ffi::GVariant,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                AppLaunchContext::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(info),
                &from_glib_borrow(platform_data),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"launched\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    launched_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for AppLaunchContext {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("AppLaunchContext")
    }
}