1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 /* 25 * Conversion parameters: 26 * inFile = GApplication.html 27 * outPack = gio 28 * outFile = Application 29 * strct = GApplication 30 * realStrct= 31 * ctorStrct= 32 * clss = Application 33 * interf = 34 * class Code: Yes 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * - ActionGroupIF 40 * prefixes: 41 * - g_application_ 42 * omit structs: 43 * omit prefixes: 44 * omit code: 45 * omit signals: 46 * imports: 47 * - glib.Str 48 * - glib.ErrorG 49 * - glib.GException 50 * - gio.Cancellable 51 * - gio.File 52 * - gio.ActionGroupIF 53 * - gio.ActionGroupT 54 * structWrap: 55 * - GCancellable* -> Cancellable 56 * - GFile* -> File 57 * module aliases: 58 * local aliases: 59 * overrides: 60 */ 61 62 module gio.Application; 63 64 public import gtkc.giotypes; 65 66 private import gtkc.gio; 67 private import glib.ConstructionException; 68 private import gobject.ObjectG; 69 70 private import gobject.Signals; 71 public import gtkc.gdktypes; 72 73 private import glib.Str; 74 private import glib.ErrorG; 75 private import glib.GException; 76 private import gio.Cancellable; 77 private import gio.File; 78 private import gio.ActionGroupIF; 79 private import gio.ActionGroupT; 80 81 82 83 private import gobject.ObjectG; 84 85 /** 86 * Description 87 * A GApplication is the foundation of an application, unique for a 88 * given application identifier. The GApplication class wraps some 89 * low-level platform-specific services and is intended to act as the 90 * foundation for higher-level application classes such as 91 * GtkApplication or MxApplication. In general, you should not use 92 * this class outside of a higher level framework. 93 * One of the core features that GApplication provides is process 94 * uniqueness, in the context of a "session". The session concept is 95 * platform-dependent, but corresponds roughly to a graphical desktop 96 * login. When your application is launched again, its arguments 97 * are passed through platform communication to the already running 98 * program. The already running instance of the program is called the 99 * primary instance. 100 * Before using GApplication, you must choose an "application identifier". 101 * The expected form of an application identifier is very close to that of 102 * of a DBus bus name. 103 * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator". 104 * For details on valid application identifiers, see 105 * g_application_id_is_valid(). 106 * The application identifier is claimed by the application as a 107 * well-known bus name on the user's session bus. This means that the 108 * uniqueness of your application is scoped to the current session. It 109 * also means that your application may provide additional services 110 * (through registration of other object paths) at that bus name. 111 * The registration of these object paths should be done with the shared 112 * GDBus session bus. Note that due to the internal architecture of 113 * GDBus, method calls can be dispatched at any time (even if a main 114 * loop is not running). For this reason, you must ensure that any 115 * object paths that you wish to register are registered before 116 * GApplication attempts to acquire the bus name of your application 117 * (which happens in g_application_register()). Unfortunately, this 118 * means that you can not use g_application_get_is_remote() to decide if 119 * you want to register object paths. 120 * GApplication provides convenient life cycle management by maintaining 121 * a use count for the primary application instance. 122 * The use count can be changed using g_application_hold() and 123 * g_application_release(). If it drops to zero, the application exits. 124 * GApplication also implements the GActionGroup interface and lets you 125 * easily export actions by adding them with g_application_set_action_group(). 126 * When invoking an action by calling g_action_group_activate_action() on 127 * the application, it is always invoked in the primary instance. 128 * There is a number of different entry points into a GApplication: 129 * via 'Activate' (i.e. just starting the application) 130 * via 'Open' (i.e. opening some files) 131 * by handling a command-line 132 * via activating an action 133 * The "startup" signal lets you handle the application 134 * initialization for all of these in a single place. 135 * Regardless of which of these entry points is used to start the application, 136 * GApplication passes some platform 137 * data from the launching instance to the primary instance, 138 * in the form of a GVariant dictionary mapping strings to variants. 139 * To use platform data, override the before_emit or after_emit virtual 140 * functions in your GApplication subclass. When dealing with 141 * GApplicationCommandline objects, the platform data is directly 142 * available via g_application_command_line_get_cwd(), 143 * g_application_command_line_get_environ() and 144 * g_application_command_line_get_platform_data(). 145 * As the name indicates, the platform data may vary depending on the 146 * operating system, but it always includes the current directory (key 147 * "cwd"), and optionally the environment (ie the set of environment 148 * variables and their values) of the calling process (key "environ"). 149 * The environment is only added to the platform data if the 150 * G_APPLICATION_SEND_ENVIONMENT flag is set. GApplication subclasses 151 * can add their own platform data by overriding the add_platform_data 152 * virtual function. For instance, GtkApplication adds startup notification 153 * data in this way. 154 * To parse commandline arguments you may handle the 155 * "command-line" signal or override the local_command_line() 156 * vfunc, to parse them in either the primary instance or the local instance, 157 * respectively. 158 * $(DDOC_COMMENT example) 159 * $(DDOC_COMMENT example) 160 */ 161 public class Application : ObjectG, ActionGroupIF 162 { 163 164 /** the main Gtk struct */ 165 protected GApplication* gApplication; 166 167 168 public GApplication* getApplicationStruct() 169 { 170 return gApplication; 171 } 172 173 174 /** the main Gtk struct as a void* */ 175 protected override void* getStruct() 176 { 177 return cast(void*)gApplication; 178 } 179 180 /** 181 * Sets our main struct and passes it to the parent class 182 */ 183 public this (GApplication* gApplication) 184 { 185 super(cast(GObject*)gApplication); 186 this.gApplication = gApplication; 187 } 188 189 protected override void setStruct(GObject* obj) 190 { 191 super.setStruct(obj); 192 gApplication = cast(GApplication*)obj; 193 } 194 195 // add the ActionGroup capabilities 196 mixin ActionGroupT!(GApplication); 197 198 /** 199 */ 200 int[string] connectedSignals; 201 202 void delegate(Application)[] onActivateListeners; 203 /** 204 * The ::activate signal is emitted on the primary instance when an 205 * activation occurs. See g_application_activate(). 206 */ 207 void addOnActivate(void delegate(Application) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 208 { 209 if ( !("activate" in connectedSignals) ) 210 { 211 Signals.connectData( 212 getStruct(), 213 "activate", 214 cast(GCallback)&callBackActivate, 215 cast(void*)this, 216 null, 217 connectFlags); 218 connectedSignals["activate"] = 1; 219 } 220 onActivateListeners ~= dlg; 221 } 222 extern(C) static void callBackActivate(GApplication* applicationStruct, Application _application) 223 { 224 foreach ( void delegate(Application) dlg ; _application.onActivateListeners ) 225 { 226 dlg(_application); 227 } 228 } 229 230 gint delegate(GApplicationCommandLine*, Application)[] onCommandLineListeners; 231 /** 232 * The ::command-line signal is emitted on the primary instance when 233 * a commandline is not handled locally. See g_application_run() and 234 * the GApplicationCommandline documentation for more information. 235 */ 236 void addOnCommandLine(gint delegate(GApplicationCommandLine*, Application) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 237 { 238 if ( !("command-line" in connectedSignals) ) 239 { 240 Signals.connectData( 241 getStruct(), 242 "command-line", 243 cast(GCallback)&callBackCommandLine, 244 cast(void*)this, 245 null, 246 connectFlags); 247 connectedSignals["command-line"] = 1; 248 } 249 onCommandLineListeners ~= dlg; 250 } 251 extern(C) static void callBackCommandLine(GApplication* applicationStruct, GApplicationCommandLine* commandLine, Application _application) 252 { 253 foreach ( gint delegate(GApplicationCommandLine*, Application) dlg ; _application.onCommandLineListeners ) 254 { 255 dlg(commandLine, _application); 256 } 257 } 258 259 void delegate(void*, gint, string, Application)[] onOpenListeners; 260 /** 261 * The ::open signal is emitted on the primary instance when there are 262 * files to open. See g_application_open() for more information. 263 */ 264 void addOnOpen(void delegate(void*, gint, string, Application) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 265 { 266 if ( !("open" in connectedSignals) ) 267 { 268 Signals.connectData( 269 getStruct(), 270 "open", 271 cast(GCallback)&callBackOpen, 272 cast(void*)this, 273 null, 274 connectFlags); 275 connectedSignals["open"] = 1; 276 } 277 onOpenListeners ~= dlg; 278 } 279 extern(C) static void callBackOpen(GApplication* applicationStruct, void* files, gint nFiles, gchar* hint, Application _application) 280 { 281 foreach ( void delegate(void*, gint, string, Application) dlg ; _application.onOpenListeners ) 282 { 283 dlg(files, nFiles, Str.toString(hint), _application); 284 } 285 } 286 287 void delegate(Application)[] onStartupListeners; 288 /** 289 * The ::startup signal is emitted on the primary instance immediately 290 * after registration. See g_application_register(). 291 */ 292 void addOnStartup(void delegate(Application) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 293 { 294 if ( !("startup" in connectedSignals) ) 295 { 296 Signals.connectData( 297 getStruct(), 298 "startup", 299 cast(GCallback)&callBackStartup, 300 cast(void*)this, 301 null, 302 connectFlags); 303 connectedSignals["startup"] = 1; 304 } 305 onStartupListeners ~= dlg; 306 } 307 extern(C) static void callBackStartup(GApplication* applicationStruct, Application _application) 308 { 309 foreach ( void delegate(Application) dlg ; _application.onStartupListeners ) 310 { 311 dlg(_application); 312 } 313 } 314 315 316 /** 317 * Checks if application_id is a valid application identifier. 318 * A valid ID is required for calls to g_application_new() and 319 * g_application_set_application_id(). 320 * For convenience, the restrictions on application identifiers are 321 * Params: 322 * applicationId = a potential application identifier 323 * Returns: TRUE if application_id is valid 324 */ 325 public static int idIsValid(string applicationId) 326 { 327 // gboolean g_application_id_is_valid (const gchar *application_id); 328 return g_application_id_is_valid(Str.toStringz(applicationId)); 329 } 330 331 /** 332 * Creates a new GApplication instance. 333 * This function calls g_type_init() for you. 334 * The application id must be valid. See g_application_id_is_valid(). 335 * Params: 336 * applicationId = the application id 337 * flags = the application flags 338 * Throws: ConstructionException GTK+ fails to create the object. 339 */ 340 public this (string applicationId, GApplicationFlags flags) 341 { 342 // GApplication * g_application_new (const gchar *application_id, GApplicationFlags flags); 343 auto p = g_application_new(Str.toStringz(applicationId), flags); 344 if(p is null) 345 { 346 throw new ConstructionException("null returned by g_application_new(Str.toStringz(applicationId), flags)"); 347 } 348 this(cast(GApplication*) p); 349 } 350 351 /** 352 * Gets the unique identifier for application. 353 * Since 2.28 354 * Returns: the identifier for application, owned by application 355 */ 356 public string getApplicationId() 357 { 358 // const gchar * g_application_get_application_id (GApplication *application); 359 return Str.toString(g_application_get_application_id(gApplication)); 360 } 361 362 /** 363 * Sets the unique identifier for application. 364 * The application id can only be modified if application has not yet 365 * been registered. 366 * The application id must be valid. See g_application_id_is_valid(). 367 * Since 2.28 368 * Params: 369 * application = a GApplication 370 * applicationId = the identifier for application 371 */ 372 public void setApplicationId(string applicationId) 373 { 374 // void g_application_set_application_id (GApplication *application, const gchar *application_id); 375 g_application_set_application_id(gApplication, Str.toStringz(applicationId)); 376 } 377 378 /** 379 * Gets the current inactivity timeout for the application. 380 * This is the amount of time (in milliseconds) after the last call to 381 * g_application_release() before the application stops running. 382 * Since 2.28 383 * Returns: the timeout, in milliseconds 384 */ 385 public uint getInactivityTimeout() 386 { 387 // guint g_application_get_inactivity_timeout (GApplication *application); 388 return g_application_get_inactivity_timeout(gApplication); 389 } 390 391 /** 392 * Sets the current inactivity timeout for the application. 393 * This is the amount of time (in milliseconds) after the last call to 394 * g_application_release() before the application stops running. 395 * This call has no side effects of its own. The value set here is only 396 * used for next time g_application_release() drops the use count to 397 * zero. Any timeouts currently in progress are not impacted. 398 * Since 2.28 399 * Params: 400 * inactivityTimeout = the timeout, in milliseconds 401 */ 402 public void setInactivityTimeout(uint inactivityTimeout) 403 { 404 // void g_application_set_inactivity_timeout (GApplication *application, guint inactivity_timeout); 405 g_application_set_inactivity_timeout(gApplication, inactivityTimeout); 406 } 407 408 /** 409 * Gets the flags for application. 410 * See GApplicationFlags. 411 * Since 2.28 412 * Returns: the flags for application 413 */ 414 public GApplicationFlags getFlags() 415 { 416 // GApplicationFlags g_application_get_flags (GApplication *application); 417 return g_application_get_flags(gApplication); 418 } 419 420 /** 421 * Sets the flags for application. 422 * The flags can only be modified if application has not yet been 423 * registered. 424 * See GApplicationFlags. 425 * Since 2.28 426 * Params: 427 * flags = the flags for application 428 */ 429 public void setFlags(GApplicationFlags flags) 430 { 431 // void g_application_set_flags (GApplication *application, GApplicationFlags flags); 432 g_application_set_flags(gApplication, flags); 433 } 434 435 /** 436 * Sets or unsets the group of actions associated with the application. 437 * These actions are the actions that can be remotely invoked. 438 * It is an error to call this function after the application has been 439 * registered. 440 * Since 2.28 441 * Params: 442 * actionGroup = a GActionGroup, or NULL. [allow-none] 443 */ 444 public void setActionGroup(GActionGroup* actionGroup) 445 { 446 // void g_application_set_action_group (GApplication *application, GActionGroup *action_group); 447 g_application_set_action_group(gApplication, actionGroup); 448 } 449 450 /** 451 * Checks if application is registered. 452 * An application is registered if g_application_register() has been 453 * successfully called. 454 * Since 2.28 455 * Returns: TRUE if application is registered 456 */ 457 public int getIsRegistered() 458 { 459 // gboolean g_application_get_is_registered (GApplication *application); 460 return g_application_get_is_registered(gApplication); 461 } 462 463 /** 464 * Checks if application is remote. 465 * If application is remote then it means that another instance of 466 * application already exists (the 'primary' instance). Calls to 467 * perform actions on application will result in the actions being 468 * performed by the primary instance. 469 * The value of this property can not be accessed before 470 * g_application_register() has been called. See 471 * g_application_get_is_registered(). 472 * Since 2.28 473 * Returns: TRUE if application is remote 474 */ 475 public int getIsRemote() 476 { 477 // gboolean g_application_get_is_remote (GApplication *application); 478 return g_application_get_is_remote(gApplication); 479 } 480 481 /** 482 * Attempts registration of the application. 483 * This is the point at which the application discovers if it is the 484 * primary instance or merely acting as a remote for an already-existing 485 * primary instance. This is implemented by attempting to acquire the 486 * application identifier as a unique bus name on the session bus using 487 * GDBus. 488 * Due to the internal architecture of GDBus, method calls can be 489 * dispatched at any time (even if a main loop is not running). For 490 * this reason, you must ensure that any object paths that you wish to 491 * register are registered before calling this function. 492 * If the application has already been registered then TRUE is 493 * returned with no work performed. 494 * The "startup" signal is emitted if registration succeeds 495 * and application is the primary instance. 496 * In the event of an error (such as cancellable being cancelled, or a 497 * failure to connect to the session bus), FALSE is returned and error 498 * is set appropriately. 499 * Note: the return value of this function is not an indicator that this 500 * instance is or is not the primary instance of the application. See 501 * g_application_get_is_remote() for that. 502 * Since 2.28 503 * Params: 504 * cancellable = a GCancellable, or NULL 505 * Returns: TRUE if registration succeeded 506 * Throws: GException on failure. 507 */ 508 public int register(Cancellable cancellable) 509 { 510 // gboolean g_application_register (GApplication *application, GCancellable *cancellable, GError **error); 511 GError* err = null; 512 513 auto p = g_application_register(gApplication, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 514 515 if (err !is null) 516 { 517 throw new GException( new ErrorG(err) ); 518 } 519 520 return p; 521 } 522 523 /** 524 * Increases the use count of application. 525 * Use this function to indicate that the application has a reason to 526 * continue to run. For example, g_application_hold() is called by GTK+ 527 * when a toplevel window is on the screen. 528 * To cancel the hold, call g_application_release(). 529 */ 530 public void hold() 531 { 532 // void g_application_hold (GApplication *application); 533 g_application_hold(gApplication); 534 } 535 536 /** 537 * Decrease the use count of application. 538 * When the use count reaches zero, the application will stop running. 539 * Never call this function except to cancel the effect of a previous 540 * call to g_application_hold(). 541 */ 542 public void release() 543 { 544 // void g_application_release (GApplication *application); 545 g_application_release(gApplication); 546 } 547 548 /** 549 * Activates the application. 550 * In essence, this results in the GApplication::activate() signal being 551 * emitted in the primary instance. 552 * The application must be registered before calling this function. 553 * Since 2.28 554 */ 555 public void activate() 556 { 557 // void g_application_activate (GApplication *application); 558 g_application_activate(gApplication); 559 } 560 561 /** 562 * Opens the given files. 563 * In essence, this results in the "open" signal being emitted 564 * in the primary instance. 565 * n_files must be greater than zero. 566 * hint is simply passed through to the ::open signal. It is 567 * intended to be used by applications that have multiple modes for 568 * opening files (eg: "view" vs "edit", etc). Unless you have a need 569 * for this functionality, you should use "". 570 * The application must be registered before calling this function 571 * and it must have the G_APPLICATION_HANDLES_OPEN flag set. 572 * Since 2.28 573 * Params: 574 * files = an array of GFiles to open. [array length=n_files] 575 * hint = a hint (or ""), but never NULL 576 */ 577 public void open(File[] files, string hint) 578 { 579 // void g_application_open (GApplication *application, GFile **files, gint n_files, const gchar *hint); 580 581 GFile*[] filesArray = new GFile*[files.length]; 582 for ( int i = 0; i < files.length ; i++ ) 583 { 584 filesArray[i] = files[i].getFileStruct(); 585 } 586 587 g_application_open(gApplication, filesArray.ptr, cast(int) files.length, Str.toStringz(hint)); 588 } 589 590 /** 591 * Runs the application. 592 * This function is intended to be run from main() and its return value 593 * is intended to be returned by main(). Although you are expected to pass 594 * the argc, argv parameters from main() to this function, it is possible 595 * to pass NULL if argv is not available or commandline handling is not 596 * required. 597 * First, the local_command_line() virtual function is invoked. 598 * This function always runs on the local instance. It gets passed a pointer 599 * to a NULL-terminated copy of argv and is expected to remove the arguments 600 * that it handled (shifting up remaining arguments). See 601 * Example 16, “Split commandline handling” for an example of 602 * parsing argv manually. Alternatively, you may use the GOptionContext API, 603 * after setting argc = g_strv_length (argv);. 604 * The last argument to local_command_line() is a pointer to the status 605 * variable which can used to set the exit status that is returned from 606 * g_application_run(). 607 * If local_command_line() returns TRUE, the command line is expected 608 * to be completely handled, including possibly registering as the primary 609 * instance, calling g_application_activate() or g_application_open(), etc. 610 * If local_command_line() returns FALSE then the application is registered 611 * and the "command-line" signal is emitted in the primary 612 * instance (which may or may not be this instance). The signal handler 613 * gets passed a GApplicationCommandline object that (among other things) 614 * contains the remaining commandline arguments that have not been handled 615 * by local_command_line(). 616 * If the application has the G_APPLICATION_HANDLES_COMMAND_LINE 617 * flag set then the default implementation of local_command_line() 618 * always returns FALSE immediately, resulting in the commandline 619 * always being handled in the primary instance. 620 * Otherwise, the default implementation of local_command_line() tries 621 * to do a couple of things that are probably reasonable for most 622 * applications. First, g_application_register() is called to attempt 623 * to register the application. If that works, then the command line 624 * arguments are inspected. If no commandline arguments are given, then 625 * g_application_activate() is called. If commandline arguments are 626 * given and the G_APPLICATION_HANDLES_OPEN flag is set then they 627 * are assumed to be filenames and g_application_open() is called. 628 * If you need to handle commandline arguments that are not filenames, 629 * and you don't mind commandline handling to happen in the primary 630 * instance, you should set G_APPLICATION_HANDLED_COMMAND_LINE and 631 * process the commandline arguments in your "command-line" 632 * signal handler, either manually or using the GOptionContext API. 633 * If you are interested in doing more complicated local handling of the 634 * commandline then you should implement your own GApplication subclass 635 * and override local_command_line(). In this case, you most likely want 636 * to return TRUE from your local_command_line() implementation to 637 * suppress the default handling. See 638 * Example 16, “Split commandline handling” for an example. 639 * If, after the above is done, the use count of the application is zero 640 * then the exit status is returned immediately. If the use count is 641 * non-zero then the mainloop is run until the use count falls to zero, 642 * at which point 0 is returned. 643 * If the G_APPLICATION_IS_SERVICE flag is set, then the exiting at 644 * use count of zero is delayed for a while (ie: the instance stays 645 * around to provide its service to others). 646 * Since 2.28 647 * Params: 648 * argv = the argv from main(), or NULL. [array length=argc] 649 * Returns: the exit status 650 */ 651 public int run(string[] argv) 652 { 653 // int g_application_run (GApplication *application, int argc, char **argv); 654 return g_application_run(gApplication, cast(int) argv.length, Str.toStringzArray(argv)); 655 } 656 }