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
|
/*
* Copyright (C) 2007 OpenedHand Ltd
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include <config.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <ctype.h>
#include "taku-menu.h"
#include "taku-launcher-tile.h"
#include "launcher-util.h"
#if WITH_INOTIFY
#include <sys/inotify.h>
#include "inotify/inotify-path.h"
static gboolean with_inotify;
G_LOCK_DEFINE (inotify_lock);
#endif
G_DEFINE_TYPE (TakuMenu, taku_menu, G_TYPE_OBJECT)
#define TAKU_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
TAKU_TYPE_MENU, TakuMenuPrivate))
struct _TakuMenuPrivate
{
GList *categories;
GList *items;
GHashTable *path_items_hash;
TakuLauncherCategory *fallback_category;
};
struct _TakuMenuItem
{
gchar *path;
gchar *name;
gchar *description;
gchar *icon_name;
gchar **cats;
GList *categories;
gchar **argv;
gboolean use_sn;
gboolean single_instance;
};
enum
{
ITEM_ADDED,
ITEM_REMOVED,
LAST_SIGNAL
};
static guint _menu_signals[LAST_SIGNAL] = {0};
/*
* Public functions
*/
/*
* Returns a list of TakuLauncherCategorys
*/
GList*
taku_menu_get_categories (TakuMenu *menu)
{
g_return_val_if_fail (TAKU_IS_MENU (menu), NULL);
return menu->priv->categories;
}
/*
* Returns a list of TakuMenuItems
*/
GList*
taku_menu_get_items (TakuMenu *menu)
{
g_return_val_if_fail (TAKU_IS_MENU (menu), NULL);
return menu->priv->items;
}
/*
* < MenuItem functions />
*/
const gchar*
taku_menu_item_get_name (TakuMenuItem *item)
{
g_return_val_if_fail (item, NULL);
return item->name;
}
const gchar*
taku_menu_item_get_description (TakuMenuItem *item)
{
g_return_val_if_fail (item, NULL);
return item->description;
}
GdkPixbuf*
taku_menu_item_get_icon (TakuMenuItem *item, int size)
{
g_return_val_if_fail (item, NULL);
return get_icon (item->icon_name, size);
}
GList*
taku_menu_item_get_categories (TakuMenuItem *item)
{
g_return_val_if_fail (item, NULL);
return item->categories;
}
gboolean
taku_menu_item_launch (TakuMenuItem *item, GtkWidget *widget)
{
g_return_val_if_fail (item, FALSE);
launcher_start (widget,
item,
item->argv,
item->use_sn,
item->single_instance);
return TRUE;
}
const gchar *
taku_menu_desktop_get_executable (TakuMenuItem *item)
{
g_return_val_if_fail (item, NULL);
return item->argv[0];
}
/*
* private functions
*/
/*
* Load all .directory files from @vfolderdir, and add them as tables.
*/
static void
load_vfolder_dir (TakuMenu *menu, const char *vfolderdir)
{
TakuMenuPrivate *priv;
GError *error = NULL;
FILE *fp;
char name[NAME_MAX], *filename;
TakuLauncherCategory *category;
g_return_if_fail (TAKU_IS_MENU (menu));
priv = menu->priv;
priv->categories = NULL;
filename = g_build_filename (vfolderdir, "Root.order", NULL);
fp = fopen (filename, "r");
if (fp == NULL) {
g_warning ("Cannot read %s", filename);
g_free (filename);
return;
}
g_free (filename);
while (fgets (name, NAME_MAX - 9, fp) != NULL) {
char **matches = NULL, *local_name = NULL;
char **l;
GKeyFile *key_file;
if (name[0] == '#' || isspace (name[0]))
continue;
strcpy (name + strlen (name) - 1, ".directory");
filename = g_build_filename (vfolderdir, name, NULL);
key_file = g_key_file_new ();
g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &error);
if (error) {
g_warning ("Cannot read %s: %s", filename, error->message);
g_error_free (error);
error = NULL;
goto done;
}
matches = g_key_file_get_string_list (key_file, "Desktop Entry",
"Match", NULL, NULL);
if (matches == NULL)
goto done;
local_name = g_key_file_get_locale_string (key_file, "Desktop Entry",
"Name", NULL, NULL);
if (local_name == NULL) {
g_warning ("Directory file %s does not contain a \"Name\" field",
filename);
g_strfreev (matches);
goto done;
}
category = taku_launcher_category_new ();
category->matches = matches;
category->name = local_name;
priv->categories = g_list_append (priv->categories, category);
/* Find a fallback category */
if (priv->fallback_category == NULL)
for (l = matches; *l; l++)
if (strcmp (*l, "meta-fallback") == 0)
priv->fallback_category = category;
done:
g_key_file_free (key_file);
g_free (filename);
}
fclose (fp);
}
static void
match_category (TakuLauncherCategory *category,
TakuMenuItem *item,
gboolean *placed)
{
char **groups;
char **match;
for (match = category->matches; *match; match++) {
/* Add all tiles to the all group */
if (strcmp (*match, "meta-all") == 0) {
item->categories = g_list_append (item->categories, category);
return;
}
for (groups = item->cats; *groups; groups++) {
if (strcmp (*match, *groups) == 0) {
item->categories = g_list_append (item->categories, category);
*placed = TRUE;
return;
}
}
}
}
static void
set_groups (TakuMenu *menu, TakuMenuItem *item)
{
gboolean placed = FALSE;
GList *l;
for (l = menu->priv->categories; l ; l = l->next) {
match_category (l->data, item, &placed);
}
if (!placed && menu->priv->fallback_category)
item->categories = g_list_append (item->categories,
menu->priv->fallback_category);
}
/*
* Get the boolean for the key @key from @key_file, and if it cannot be parsed
* or does not exist return @def.
*/
static gboolean
get_desktop_boolean (GKeyFile *key_file, const char *key, gboolean def)
{
GError *error = NULL;
gboolean b;
g_assert (key_file);
g_assert (key);
b = g_key_file_get_boolean (key_file, DESKTOP, key, &error);
if (error) {
g_error_free (error);
b = def;
}
return b;
}
static char *
get_desktop_string (GKeyFile *key_file, const char *key)
{
char *s;
g_assert (key_file);
g_assert (key);
/* Get the key */
s = g_key_file_get_locale_string (key_file, DESKTOP, key, NULL, NULL);
/* Strip any whitespace */
s = s ? g_strstrip (s) : NULL;
if (s && s[0] != '\0') {
return s;
} else {
if (s) g_free (s);
return NULL;
}
}
/*
* Load the desktop file @filename, and add it to the table.
*/
static TakuMenuItem*
load_desktop_file (TakuMenu *menu, const char *filename)
{
TakuMenuPrivate *priv;
TakuMenuItem *item = NULL;
GKeyFile *key_file;
GError *err = NULL;
gchar *exec, *cats;
g_assert (filename);
g_return_val_if_fail (TAKU_IS_MENU (menu), NULL);
priv = menu->priv;
/* Check for duplicate desktop files based on path name */
if (g_hash_table_lookup (priv->path_items_hash, filename))
return NULL;
key_file = g_key_file_new ();
/* Do the checks to make sure the .desktop file is valid */
if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &err)) {
g_key_file_free (key_file);
g_error_free (err);
return NULL;
}
if (get_desktop_boolean (key_file, "NoDisplay", FALSE)) {
g_key_file_free (key_file);
return NULL;
}
/* This is important, so read it first to simplyfy cleanup */
exec = get_desktop_string (key_file, "Exec");
if (exec == NULL) {
g_free (exec);
g_key_file_free (key_file);
return NULL;
}
/* Okay, were good to go */
item = g_slice_new0 (TakuMenuItem);
item->path = g_strdup (filename);
item->name = get_desktop_string (key_file, "Name");
item->description = get_desktop_string (key_file, "Comment");
item->icon_name = get_desktop_string (key_file, "Icon");
if (GDK_IS_X11_DISPLAY (gdk_display_get_default()))
item->use_sn = get_desktop_boolean (key_file, "StartupNotify", FALSE);
else
item->use_sn = FALSE;
item->single_instance = get_desktop_boolean (key_file,
"X-MB-SingleInstance", FALSE)
|| get_desktop_boolean (key_file,
"SingleInstance", FALSE);
item->argv = exec_to_argv (exec);
g_free (exec);
cats = get_desktop_string (key_file, "Categories");
if (cats == NULL)
cats = g_strdup ("");
item->cats = g_strsplit (cats, ";", -1);
g_free (cats);
set_groups (menu, item);
priv->items = g_list_append (priv->items, item);
g_hash_table_insert (priv->path_items_hash, item->path, item);
return item;
}
#if WITH_INOTIFY
/*
* Monitor @directory with inotify, if available.
*/
static void
monitor (const char *directory)
{
inotify_sub *sub;
if (!with_inotify)
return;
sub = _ih_sub_new (directory, NULL, NULL);
_ip_start_watching (sub);
}
/*
* Used to delete tiles when they are removed from disk. @a is the tile, @b is
* the desktop filename to look for.
*/
/*
static void
find_and_destroy (GtkWidget *widget, gpointer data)
{
TakuLauncherTile *tile;
const char *removed, *filename;
tile = TAKU_LAUNCHER_TILE (widget);
if (!tile)
return;
removed = data;
filename = taku_launcher_tile_get_filename (tile);
if (strcmp (removed, filename) == 0)
gtk_widget_destroy (widget);
}
*/
static TakuMenuItem *
_find_item (TakuMenu *menu, const gchar *path)
{
TakuMenuPrivate *priv;
GList *l;
g_return_val_if_fail (TAKU_IS_MENU (menu), NULL);
priv = menu->priv;
for (l = priv->items; l; l = l->next)
{
TakuMenuItem *item = l->data;
if (strcmp (item->path, path) == 0)
return item;
}
return NULL;
}
static void
_remove_item (TakuMenu *menu, TakuMenuItem *item)
{
TakuMenuPrivate *priv;
g_return_if_fail (TAKU_IS_MENU (menu));
priv = menu->priv;
priv->items = g_list_remove (priv->items, item);
g_hash_table_remove (priv->path_items_hash, item->path);
/* TODO: Lots of leaks here */
g_slice_free (TakuMenuItem, item);
}
static void
inotify_event (ik_event_t *event, inotify_sub *sub)
{
char *path;
TakuMenu *menu = taku_menu_get_default ();
TakuMenuItem *item = NULL;
if (event->mask & IN_MOVED_TO || event->mask & IN_CREATE) {
if (g_str_has_suffix (event->name, ".desktop")) {
path = g_build_filename (sub->dirname, event->name, NULL);
item = load_desktop_file (taku_menu_get_default (), path);
if (item)
g_signal_emit (menu, _menu_signals[ITEM_ADDED], 0, item);
g_free (path);
}
} else if (event->mask & IN_MOVED_FROM || event->mask & IN_DELETE) {
path = g_build_filename (sub->dirname, event->name, NULL);
item = _find_item (menu, path);
if (item) {
g_signal_emit (menu, _menu_signals[ITEM_REMOVED], 0, item);
_remove_item (menu, item);
}
g_free (path);
}
}
#endif
/*
* Recursively load all desktop files in @directory
*/
static void
load_desktop_files (TakuMenu *menu, const char *directory)
{
GError *error = NULL;
GDir *dir;
const char *name;
g_assert (menu);
g_assert (directory);
/* Check if the directory exists */
if (! g_file_test (directory, G_FILE_TEST_IS_DIR)) {
return;
}
#if WITH_INOTIFY
monitor (directory);
#endif
dir = g_dir_open (directory, 0, &error);
if (error) {
g_warning ("Cannot read %s: %s", directory, error->message);
g_error_free (error);
return;
}
while ((name = g_dir_read_name (dir)) != NULL) {
char *filename;
filename = g_build_filename (directory, name, NULL);
if (g_file_test (filename, G_FILE_TEST_IS_DIR)) {
load_desktop_files (menu, filename);
} else if (g_file_test (filename, G_FILE_TEST_IS_REGULAR) &&
g_str_has_suffix (name, ".desktop")) {
load_desktop_file (menu, filename);
}
g_free (filename);
}
g_dir_close (dir);
}
/*
* Load all .desktop files in @datadir/applications/.
*/
static void
load_data_dir (TakuMenu *menu, const char *datadir)
{
char *directory;
g_return_if_fail (TAKU_IS_MENU (menu));
g_return_if_fail (datadir);
directory = g_build_filename (datadir, "applications", NULL);
load_desktop_files (menu, directory);
g_free (directory);
}
/* GObject stuff */
static void
taku_menu_finalize (GObject *menu)
{
/* TODO */
G_OBJECT_CLASS (taku_menu_parent_class)->finalize (menu);
}
static void
taku_menu_class_init (TakuMenuClass *klass)
{
GObjectClass *obj_class = G_OBJECT_CLASS (klass);
obj_class->finalize = taku_menu_finalize;
/* Class signals */
_menu_signals[ITEM_ADDED] =
g_signal_new ("item-added",
G_OBJECT_CLASS_TYPE (obj_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TakuMenuClass, item_added),
NULL, NULL,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
_menu_signals[ITEM_REMOVED] =
g_signal_new ("item-removed",
G_OBJECT_CLASS_TYPE (obj_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TakuMenuClass, item_removed),
NULL, NULL,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
g_type_class_add_private (obj_class, sizeof(TakuMenuPrivate));
}
static void
taku_menu_init (TakuMenu *menu)
{
TakuMenuPrivate *priv;
gchar *vfolder_dir = NULL;
const gchar * const *dirs;
priv = menu->priv = TAKU_MENU_GET_PRIVATE (menu);
priv->path_items_hash = g_hash_table_new_full (g_str_hash,
g_str_equal,
NULL,
NULL);
#if WITH_INOTIFY
with_inotify = _ip_startup (inotify_event);
#endif
/* Create the categories from matchbox vfolders*/
vfolder_dir = g_build_filename (g_get_home_dir (),
".matchbox", "vfolders", NULL);
if (g_file_test (vfolder_dir, G_FILE_TEST_EXISTS))
load_vfolder_dir (menu, vfolder_dir);
else
load_vfolder_dir (menu, PKGDATADIR "/vfolders");
g_free (vfolder_dir);
/*
* Load all desktop files in the system data directories, and the user data
* directory. TODO: would it be best to do this in an idle handler and
* populate the desktop incrementally?
*/
for (dirs = g_get_system_data_dirs (); *dirs; dirs++) {
load_data_dir (menu, *dirs);
}
load_data_dir (menu, g_get_user_data_dir ());
}
/*
* Expected to create a list of TakuLauncherCategorys and TakuMenuItems
*/
TakuMenu*
taku_menu_get_default (void)
{
static TakuMenu *menu = NULL;
if (menu == NULL)
menu = g_object_new (TAKU_TYPE_MENU, NULL);
return menu;
}
|