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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2012 Jonathan Matthew <jonathan@d14n.org>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <config.h>
#include <glib/gi18n.h>
#include <libpeas/peas.h>
#include <libpeas-gtk/peas-gtk.h>
#include <shell/rb-application.h>
#include <shell/rb-shell.h>
#include <lib/rb-debug.h>
#include <lib/rb-file-helpers.h>
#include <lib/rb-builder-helpers.h>
#include <lib/rb-stock-icons.h>
#include <widgets/rb-dialog.h>
/**
* SECTION:rb-application
* @short_description: the rhythmbox subclass of GtkApplication
*
* RBApplication contains some interactions with the desktop
* environment, such as the app menu and processing of files specified
* on the command line.
*/
static void rb_application_class_init (RBApplicationClass *klass);
static void rb_application_init (RBApplication *app);
struct _RBApplicationPrivate
{
RBShell *shell;
GtkWidget *plugins;
GHashTable *shared_menus;
GHashTable *plugin_menus;
gboolean autostarted;
gboolean no_update;
gboolean no_registration;
gboolean dry_run;
gboolean disable_plugins;
char *rhythmdb_file;
char *playlists_file;
};
G_DEFINE_TYPE (RBApplication, rb_application, GTK_TYPE_APPLICATION);
enum {
PROP_0,
PROP_SHELL
};
static void
load_uri_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
{
RBApplication *rb = RB_APPLICATION (user_data);
const char *uri;
gboolean play;
g_variant_get (parameters, "(&sb)", &uri, &play);
rb_shell_load_uri (RB_SHELL (rb->priv->shell), uri, play, NULL);
}
static void
activate_source_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
{
RBApplication *rb = RB_APPLICATION (user_data);
const char *source;
guint play;
g_variant_get (parameters, "(&su)", &source, &play);
rb_shell_activate_source_by_uri (RB_SHELL (rb->priv->shell), source, play, NULL);
}
static void
quit_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
{
RBApplication *rb = RB_APPLICATION (user_data);
rb_shell_quit (RB_SHELL (rb->priv->shell), NULL);
}
static gboolean
plugins_window_delete_cb (GtkWidget *window,
GdkEventAny *event,
gpointer data)
{
gtk_widget_hide (window);
return TRUE;
}
static void
plugins_response_cb (GtkDialog *dialog,
int response_id,
gpointer data)
{
if (response_id == GTK_RESPONSE_CLOSE)
gtk_widget_hide (GTK_WIDGET (dialog));
}
static void
plugins_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
{
RBApplication *app = RB_APPLICATION (user_data);
if (app->priv->plugins == NULL) {
GtkWidget *content_area;
GtkWidget *manager;
GtkWindow *window;
g_object_get (app->priv->shell, "window", &window, NULL);
app->priv->plugins = gtk_dialog_new_with_buttons (_("Configure Plugins"),
window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_CLOSE,
GTK_RESPONSE_CLOSE,
NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (app->priv->plugins));
gtk_container_set_border_width (GTK_CONTAINER (app->priv->plugins), 5);
gtk_box_set_spacing (GTK_BOX (content_area), 2);
g_signal_connect_object (G_OBJECT (app->priv->plugins),
"delete_event",
G_CALLBACK (plugins_window_delete_cb),
NULL, 0);
g_signal_connect_object (G_OBJECT (app->priv->plugins),
"response",
G_CALLBACK (plugins_response_cb),
NULL, 0);
manager = peas_gtk_plugin_manager_new (NULL);
gtk_widget_show_all (GTK_WIDGET (manager));
gtk_box_pack_start (GTK_BOX (content_area), manager, TRUE, TRUE, 0);
gtk_window_set_default_size (GTK_WINDOW (app->priv->plugins), 600, 400);
g_object_unref (window);
}
gtk_window_present (GTK_WINDOW (app->priv->plugins));
}
static void
preferences_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
{
RBApplication *app = RB_APPLICATION (user_data);
RBShellPreferences *prefs;
g_object_get (app->priv->shell, "prefs", &prefs, NULL);
gtk_window_present (GTK_WINDOW (prefs));
g_object_unref (prefs);
}
static void
about_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
{
RBApplication *app = RB_APPLICATION (user_data);
GtkWindow *window;
const char **tem;
GString *comment;
const char *authors[] = {
"",
#include "MAINTAINERS.tab"
"",
NULL,
#include "MAINTAINERS.old.tab"
"",
NULL,
#include "AUTHORS.tab"
NULL
};
const char *documenters[] = {
#include "DOCUMENTERS.tab"
NULL
};
const char *translator_credits = _("translator-credits");
const char *license[] = {
N_("Rhythmbox is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version.\n"),
N_("Rhythmbox is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"),
N_("You should have received a copy of the GNU General Public License\n"
"along with Rhythmbox; if not, write to the Free Software Foundation, Inc.,\n"
"51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n")
};
char *license_trans;
authors[0] = _("Maintainers:");
for (tem = authors; *tem != NULL; tem++)
;
*tem = _("Former Maintainers:");
for (; *tem != NULL; tem++)
;
*tem = _("Contributors:");
comment = g_string_new (_("Music management and playback software for GNOME."));
license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
_(license[2]), "\n", NULL);
g_object_get (app->priv->shell, "window", &window, NULL);
gtk_show_about_dialog (GTK_WINDOW (window),
"version", VERSION,
"copyright", "Copyright \xc2\xa9 2005 - 2012 The Rhythmbox authors\nCopyright \xc2\xa9 2003 - 2005 Colin Walters\nCopyright \xc2\xa9 2002, 2003 Jorn Baayen",
"license", license_trans,
"website-label", _("Rhythmbox Website"),
"website", "https://wiki.gnome.org/Apps/Rhythmbox",
"comments", comment->str,
"authors", (const char **) authors,
"documenters", (const char **) documenters,
"translator-credits", strcmp (translator_credits, "translator-credits") != 0 ? translator_credits : NULL,
"logo-icon-name", "rhythmbox",
NULL);
g_string_free (comment, TRUE);
g_free (license_trans);
g_object_unref (window);
}
static void
help_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
{
RBApplication *app = RB_APPLICATION (user_data);
GError *error = NULL;
GtkWindow *window;
g_object_get (app->priv->shell, "window", &window, NULL);
gtk_show_uri (gtk_widget_get_screen (GTK_WIDGET (window)),
"help:rhythmbox",
gtk_get_current_event_time (),
&error);
if (error != NULL) {
rb_error_dialog (NULL, _("Couldn't display help"),
"%s", error->message);
g_error_free (error);
}
g_object_unref (window);
}
static void
impl_activate (GApplication *app)
{
RBApplication *rb = RB_APPLICATION (app);
rb_shell_present (rb->priv->shell, gtk_get_current_event_time (), NULL);
}
static void
impl_open (GApplication *app, GFile **files, int n_files, const char *hint)
{
RBApplication *rb = RB_APPLICATION (app);
int i;
for (i = 0; i < n_files; i++) {
char *uri;
uri = g_file_get_uri (files[i]);
/*
* rb_uri_exists won't work if the location isn't mounted.
* however, things that are interesting to mount are generally
* non-local, so we'll process them anyway.
*/
if (rb_uri_is_local (uri) == FALSE || rb_uri_exists (uri)) {
rb_shell_load_uri (rb->priv->shell, uri, TRUE, NULL);
}
g_free (uri);
}
}
static void
load_state_changed_cb (GActionGroup *action_group, const char *action_name, GVariant *state, GPtrArray *files)
{
gboolean loaded;
gboolean scanned;
if (g_strcmp0 (action_name, "load-uri") != 0) {
return;
}
g_variant_get (state, "(bb)", &loaded, &scanned);
if (loaded) {
rb_debug ("opening files now");
g_signal_handlers_disconnect_by_func (action_group, load_state_changed_cb, files);
g_application_open (G_APPLICATION (action_group), (GFile **)files->pdata, files->len, "");
g_ptr_array_free (files, TRUE);
}
}
static void
impl_startup (GApplication *app)
{
RBApplication *rb = RB_APPLICATION (app);
gboolean shell_shows_app_menu;
GtkBuilder *builder;
GMenuModel *menu;
GtkCssProvider *provider;
GActionEntry app_actions[] = {
/* rhythmbox-client actions */
{ "load-uri", load_uri_action_cb, "(sb)", "(false, false)" },
{ "activate-source", activate_source_action_cb, "(su)" },
/* app menu actions */
{ "plugins", plugins_action_cb },
{ "preferences", preferences_action_cb },
{ "help", help_action_cb },
{ "about", about_action_cb },
{ "quit", quit_action_cb },
};
(* G_APPLICATION_CLASS (rb_application_parent_class)->startup) (app);
rb_stock_icons_init ();
g_action_map_add_action_entries (G_ACTION_MAP (app),
app_actions,
G_N_ELEMENTS (app_actions),
app);
g_object_get (gtk_settings_get_default (),
"gtk-shell-shows-app-menu", &shell_shows_app_menu,
NULL);
builder = rb_builder_load ("app-menu.ui", NULL);
menu = G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"));
rb_application_link_shared_menus (rb, G_MENU (menu));
rb_application_add_shared_menu (rb, "app-menu", menu);
/* only set the app menu if the shell shows it; otherwise, we'll
* stick a menu button in the toolbar.
*/
if (shell_shows_app_menu) {
gtk_application_set_app_menu (GTK_APPLICATION (app), menu);
}
g_object_unref (builder);
/* Use our own css provider */
provider = gtk_css_provider_new ();
if (gtk_css_provider_load_from_path (provider, rb_file ("style.css"), NULL)) {
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER (provider),
600);
}
rb->priv->shell = RB_SHELL (g_object_new (RB_TYPE_SHELL,
"application", rb,
"autostarted", rb->priv->autostarted,
"no-registration", rb->priv->no_registration,
"no-update", rb->priv->no_update,
"dry-run", rb->priv->dry_run,
"rhythmdb-file", rb->priv->rhythmdb_file,
"playlists-file", rb->priv->playlists_file,
"disable-plugins", rb->priv->disable_plugins,
NULL));
}
static gboolean
impl_local_command_line (GApplication *app, gchar ***args, int *exit_status)
{
RBApplication *rb = RB_APPLICATION (app);
GError *error = NULL;
gboolean scanned;
gboolean loaded;
GPtrArray *files;
int n_files;
int i;
n_files = g_strv_length (*args) - 1;
if (rb->priv->no_registration) {
if (n_files > 0) {
g_warning ("Unable to open files on the commandline with --no-registration");
}
impl_startup (app);
return TRUE;
}
if (!g_application_register (app, NULL, &error)) {
g_critical ("%s", error->message);
g_error_free (error);
*exit_status = 1;
return TRUE;
}
if (n_files <= 0) {
g_application_activate (app);
*exit_status = 0;
return TRUE;
}
files = g_ptr_array_new_with_free_func (g_object_unref);
for (i = 0; i < n_files; i++) {
g_ptr_array_add (files, g_file_new_for_commandline_arg ((*args)[i + 1]));
}
g_variant_get (g_action_group_get_action_state (G_ACTION_GROUP (app), "load-uri"), "(bb)", &loaded, &scanned);
if (loaded) {
rb_debug ("opening files immediately");
g_application_open (app, (GFile **)files->pdata, files->len, "");
g_ptr_array_free (files, TRUE);
} else {
rb_debug ("opening files once db is loaded");
g_signal_connect (app, "action-state-changed::load-uri", G_CALLBACK (load_state_changed_cb), files);
}
return TRUE;
}
static void
impl_shutdown (GApplication *app)
{
RBApplication *rb = RB_APPLICATION (app);
if (rb->priv->shell != NULL) {
rb_shell_quit (rb->priv->shell, NULL);
g_object_unref (rb->priv->shell);
rb->priv->shell = NULL;
}
(* G_APPLICATION_CLASS (rb_application_parent_class)->shutdown) (app);
}
static void
impl_finalize (GObject *object)
{
RBApplication *app = RB_APPLICATION (object);
g_hash_table_destroy (app->priv->shared_menus);
g_hash_table_destroy (app->priv->plugin_menus);
rb_file_helpers_shutdown ();
rb_stock_icons_shutdown ();
rb_refstring_system_shutdown ();
G_OBJECT_CLASS (rb_application_parent_class)->finalize (object);
}
static void
impl_dispose (GObject *object)
{
RBApplication *app = RB_APPLICATION (object);
g_clear_object (&app->priv->shell);
G_OBJECT_CLASS (rb_application_parent_class)->dispose (object);
}
static void
impl_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
/* RBApplication *app = RB_APPLICATION (object); */
switch (prop_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void
impl_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
RBApplication *app = RB_APPLICATION (object);
switch (prop_id) {
case PROP_SHELL:
g_value_set_object (value, app->priv->shell);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void
rb_application_init (RBApplication *app)
{
app->priv = G_TYPE_INSTANCE_GET_PRIVATE (app,
RB_TYPE_APPLICATION,
RBApplicationPrivate);
rb_user_data_dir ();
rb_refstring_system_init ();
#ifdef USE_UNINSTALLED_DIRS
rb_file_helpers_init (TRUE);
#else
rb_file_helpers_init (FALSE);
#endif
app->priv->shared_menus = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) g_object_unref);
app->priv->plugin_menus = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) g_object_unref);
g_setenv ("PULSE_PROP_media.role", "music", TRUE);
}
static void
rb_application_class_init (RBApplicationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GApplicationClass *app_class = G_APPLICATION_CLASS (klass);
object_class->finalize = impl_finalize;
object_class->dispose = impl_dispose;
object_class->set_property = impl_set_property;
object_class->get_property = impl_get_property;
app_class->open = impl_open;
app_class->activate = impl_activate;
app_class->local_command_line = impl_local_command_line;
app_class->startup = impl_startup;
app_class->shutdown = impl_shutdown;
g_object_class_install_property (object_class,
PROP_SHELL,
g_param_spec_object ("shell",
"shell",
"RBShell instance",
RB_TYPE_SHELL,
G_PARAM_READABLE));
g_type_class_add_private (klass, sizeof (RBApplicationPrivate));
}
/**
* rb_application_new:
*
* Creates the application instance.
*
* Return value: application instance
*/
GApplication *
rb_application_new (void)
{
return G_APPLICATION (g_object_new (RB_TYPE_APPLICATION,
"application-id", "org.gnome.Rhythmbox3",
"flags", G_APPLICATION_HANDLES_OPEN,
NULL));
}
/**
* rb_application_run:
* @app: the application instance
* @argc: arg count
* @argv: arg values
*
* Runs the application
*
* Return value: exit code
*/
int
rb_application_run (RBApplication *app, int argc, char **argv)
{
GOptionContext *context;
gboolean debug = FALSE;
char *debug_match = NULL;
int nargc;
char **nargv;
GError *error = NULL;
g_application_set_default (G_APPLICATION (app));
app->priv->autostarted = (g_getenv ("DESKTOP_AUTOSTART_ID") != NULL);
const GOptionEntry options [] = {
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, N_("Enable debug output"), NULL },
{ "debug-match", 'D', 0, G_OPTION_ARG_STRING, &debug_match, N_("Enable debug output matching a specified string"), NULL },
{ "no-update", 0, 0, G_OPTION_ARG_NONE, &app->priv->no_update, N_("Do not update the library with file changes"), NULL },
{ "no-registration", 'n', 0, G_OPTION_ARG_NONE, &app->priv->no_registration, N_("Do not register the shell"), NULL },
{ "dry-run", 0, 0, G_OPTION_ARG_NONE, &app->priv->dry_run, N_("Don't save any data permanently (implies --no-registration)"), NULL },
{ "disable-plugins", 0, 0, G_OPTION_ARG_NONE, &app->priv->disable_plugins, N_("Disable loading of plugins"), NULL },
{ "rhythmdb-file", 0, 0, G_OPTION_ARG_STRING, &app->priv->rhythmdb_file, N_("Path for database file to use"), NULL },
{ "playlists-file", 0, 0, G_OPTION_ARG_STRING, &app->priv->playlists_file, N_("Path for playlists file to use"), NULL },
{ NULL }
};
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
g_option_context_add_group (context, gst_init_get_option_group ());
g_option_context_add_group (context, gtk_get_option_group (TRUE));
nargc = argc;
nargv = argv;
if (g_option_context_parse (context, &nargc, &nargv, &error) == FALSE) {
g_print (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
error->message, argv[0]);
g_error_free (error);
g_option_context_free (context);
return 1;
}
g_option_context_free (context);
if (!debug && debug_match)
rb_debug_init_match (debug_match);
else
rb_debug_init (debug);
g_object_set (app, "register-session", !app->priv->no_registration, NULL);
return g_application_run (G_APPLICATION (app), nargc, nargv);
}
/**
* rb_application_add_shared_menu:
* @app: the application instance
* @name: a name for the menu
* @menu: #GMenuModel instance
*
* Adds a menu model to the set of shared menus
* available for linking into other menus.
*/
void
rb_application_add_shared_menu (RBApplication *app, const char *name, GMenuModel *menu)
{
g_assert (menu != NULL);
g_hash_table_insert (app->priv->shared_menus, g_strdup (name), g_object_ref (menu));
}
/**
* rb_application_get_shared_menu:
* @app: the application instance
* @name: name of menu to return
*
* Returns a shared menu instance added with @rb_application_add_shared_menu
*
* Return value: (transfer none): menu model instance, or NULL if not found
*/
GMenuModel *
rb_application_get_shared_menu (RBApplication *app, const char *name)
{
return g_hash_table_lookup (app->priv->shared_menus, name);
}
/**
* rb_application_get_plugin_menu:
* @app: the application instance
* @menu: name of plugin menu to return
*
* Returns a plugin menu instance. Plugin menus are like shared menus except
* they are created empty on first access, and they consist solely of entries
* added through @rb_application_add_plugin_item.
*
* Return value: (transfer none): plugin menu instance.
*/
GMenuModel *
rb_application_get_plugin_menu (RBApplication *app, const char *menu)
{
GMenuModel *pmenu;
pmenu = g_hash_table_lookup (app->priv->plugin_menus, menu);
if (pmenu == NULL) {
pmenu = G_MENU_MODEL (g_menu_new ());
g_object_ref_sink (pmenu);
g_hash_table_insert (app->priv->plugin_menus, g_strdup (menu), pmenu);
}
return pmenu;
}
/**
* rb_application_add_plugin_menu_item:
* @app: the application instance
* @menu: name of the menu to add to
* @id: id of the item to add (used to remove it, must be unique within the menu)
* @item: menu item to add
*
* Adds an item to a plugin menu. The id can be used to remove the item.
*/
void
rb_application_add_plugin_menu_item (RBApplication *app, const char *menu, const char *id, GMenuItem *item)
{
GMenuModel *pmenu;
pmenu = rb_application_get_plugin_menu (app, menu);
g_assert (pmenu != NULL);
g_menu_item_set_attribute (item, "rb-plugin-item-id", "s", id);
g_menu_append_item (G_MENU (pmenu), item);
}
/**
* rb_application_remove_plugin_item:
* @app: the application instance
* @menu: plugin menu to remove the item from
* @id: id of the item to remove
*
* Removes an item from a plugin menu.
*/
void
rb_application_remove_plugin_menu_item (RBApplication *app, const char *menu, const char *id)
{
GMenuModel *pmenu;
int i;
pmenu = rb_application_get_plugin_menu (app, menu);
g_assert (pmenu != NULL);
for (i = 0; i < g_menu_model_get_n_items (pmenu); i++) {
char *item_id;
item_id = NULL;
g_menu_model_get_item_attribute (pmenu, i, "rb-plugin-item-id", "s", &item_id);
if (g_strcmp0 (item_id, id) == 0) {
g_menu_remove (G_MENU (pmenu), i);
g_free (item_id);
return;
}
g_free (item_id);
}
}
/**
* rb_application_link_shared_menus:
* @app: the #RBApplication
* @menu: a #GMenu to process
*
* Processes shared menu links in the given menu. Menu links take the
* form of items with "rb-menu-link" or "rb-plugin-menu-link" and "rb-menu-link-type" attributes.
* "rb-menu-link" specifies the name of a shared menu to link in,
* "rb-plugin-menu-link" specifies the name of a plugin menu to link in,
* "rb-menu-link-type" specifies the link type, either "section" or
* "submenu". A link item must have "rb-menu-link-type" and one of
* "rb-menu-link" or "rb-plugin-menu-link".
*/
void
rb_application_link_shared_menus (RBApplication *app, GMenu *menu)
{
int i;
for (i = 0; i < g_menu_model_get_n_items (G_MENU_MODEL (menu)); i++) {
GMenuModel *symlink_menu;
GMenuLinkIter *iter;
GMenuModel *link;
const char *name;
const char *symlink;
symlink_menu = NULL;
symlink = NULL;
g_menu_model_get_item_attribute (G_MENU_MODEL (menu), i, "rb-menu-link", "s", &symlink);
if (symlink != NULL) {
symlink_menu = rb_application_get_shared_menu (app, symlink);
if (symlink_menu == NULL) {
g_warning ("can't find target menu for link %s", symlink);
continue;
}
} else {
g_menu_model_get_item_attribute (G_MENU_MODEL (menu), i, "rb-plugin-menu-link", "s", &symlink);
if (symlink != NULL) {
symlink_menu = rb_application_get_plugin_menu (app, symlink);
}
}
iter = g_menu_model_iterate_item_links (G_MENU_MODEL (menu), i);
if (symlink_menu != NULL) {
GMenuAttributeIter *attrs;
const char *attr;
GVariant *value;
GMenuItem *item;
if (g_menu_link_iter_get_next (iter, &name, &link)) {
/* replace the existing item, since we can't modify it */
item = g_menu_item_new (NULL, NULL);
attrs = g_menu_model_iterate_item_attributes (G_MENU_MODEL (menu), i);
while (g_menu_attribute_iter_get_next (attrs, &attr, &value)) {
g_menu_item_set_attribute_value (item, attr, value);
g_variant_unref (value);
}
g_menu_item_set_link (item, name, symlink_menu);
g_menu_remove (menu, i);
g_menu_insert_item (menu, i, item);
g_object_unref (link);
}
} else {
/* recurse into submenus and sections */
while (g_menu_link_iter_get_next (iter, &name, &link)) {
if (G_IS_MENU (link)) {
rb_application_link_shared_menus (app, G_MENU (link));
}
g_object_unref (link);
}
}
g_object_unref (iter);
}
}
static void
set_accelerator (RBApplication *app, GMenuModel *model, int item, gboolean enable)
{
GMenuAttributeIter *iter;
GVariant *value;
GVariant *target = NULL;
const char *key;
const char *accel = NULL;
const char *action = NULL;
iter = g_menu_model_iterate_item_attributes (model, item);
while (g_menu_attribute_iter_get_next (iter, &key, &value)) {
if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
action = g_variant_get_string (value, NULL);
else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
accel = g_variant_get_string (value, NULL);
else if (g_str_equal (key, "target"))
target = g_variant_ref (value);
g_variant_unref (value);
}
g_object_unref (iter);
if (accel && action) {
if (enable)
gtk_application_add_accelerator (GTK_APPLICATION (app), accel, action, target);
else
gtk_application_remove_accelerator (GTK_APPLICATION (app), action, target);
}
if (target)
g_variant_unref (target);
}
/**
* rb_application_set_menu_accelerators:
* @app: the #RBApplication
* @menu: a #GMenuModel for which to enable or disable accelerators
* @enable: %TRUE to enable accelerators, %FALSE to disable
*
* Enables or disables accelerators for items in @menu.
*/
void
rb_application_set_menu_accelerators (RBApplication *app, GMenuModel *menu, gboolean enable)
{
int i;
for (i = 0; i < g_menu_model_get_n_items (menu); i++) {
GMenuLinkIter *iter;
GMenuModel *more;
const char *key;
set_accelerator (app, menu, i, enable);
iter = g_menu_model_iterate_item_links (menu, i);
while (g_menu_link_iter_get_next (iter, &key, &more)) {
rb_application_set_menu_accelerators (app, more, enable);
g_object_unref (more);
}
g_object_unref (iter);
}
}
|