cinnamon-menus-6.0.0/ 0000775 0001750 0001750 00000000000 14526401433 013372 5 ustar fabio fabio cinnamon-menus-6.0.0/debian/ 0000775 0001750 0001750 00000000000 14526401433 014614 5 ustar fabio fabio cinnamon-menus-6.0.0/meson.build 0000664 0001750 0001750 00000002350 14526401433 015534 0 ustar fabio fabio project('cinnamon-menus', 'c', version : '6.0.0', meson_version: '>=0.56.0')
gnome = import('gnome')
version = meson.project_version()
binary_version = '0.0.1'
binary_major_version = binary_version.split('.')[0]
cmenu_conf = configuration_data()
cmenu_conf.set_quoted('PACKAGE', meson.project_name())
# directories
prefix = get_option('prefix')
datadir = get_option('datadir')
libdir = get_option('libdir')
includedir = get_option('includedir')
# generate config.h
config_h_file = configure_file(
output : 'config.h',
configuration : cmenu_conf
)
config_h = declare_dependency(
sources: config_h_file
)
include_root = include_directories('.')
c_args = [
'-DGMENU_I_KNOW_THIS_IS_UNSTABLE',
]
if get_option('enable_debug')
c_args += '-DG_ENABLE_DEBUG'
else
c_args += '-DG_DISABLE_ASSERT'
c_args += '-DG_DISABLE_CHECKS'
c_args += '-DG_DISABLE_CAST_CHECKS'
endif
if not get_option('deprecated_warnings')
c_args += '-Wno-deprecated-declarations'
c_args += '-Wno-deprecated'
c_args += '-Wno-declaration-after-statement'
endif
add_global_arguments(c_args, language: 'c')
gio = dependency('gio-unix-2.0', version: '>= 2.29.15')
subdir('libmenu')
if get_option('enable_docs')
subdir('docs/reference')
endif
cinnamon-menus-6.0.0/docs/ 0000775 0001750 0001750 00000000000 14526401433 014322 5 ustar fabio fabio cinnamon-menus-6.0.0/docs/reference/ 0000775 0001750 0001750 00000000000 14526401433 016260 5 ustar fabio fabio cinnamon-menus-6.0.0/docs/reference/meson.build 0000664 0001750 0001750 00000000665 14526401433 020431 0 ustar fabio fabio version_conf = configuration_data()
version_conf.set('VERSION', version)
configure_file(
input: 'version.xml.in',
output: 'version.xml',
configuration: version_conf,
)
gnome.gtkdoc(
'cmenu',
src_dir: join_paths(meson.project_source_root(), 'libmenu'),
main_xml: 'cmenu-docs.xml',
scan_args: ['--rebuild-types'],
ignore_headers: libmenu_private_headers,
dependencies: cmenu_dep,
install: true,
)
cinnamon-menus-6.0.0/docs/reference/version.xml.in 0000664 0001750 0001750 00000000012 14526401433 021065 0 ustar fabio fabio @VERSION@
cinnamon-menus-6.0.0/docs/reference/cmenu-docs.xml 0000664 0001750 0001750 00000001644 14526401433 021044 0 ustar fabio fabio
]>
Cinnamon Menus Reference Manual
For Cinnamon Menus &version;
API Reference
Classes
API Index
Index
cinnamon-menus-6.0.0/.gitignore 0000664 0001750 0001750 00000000342 14526401433 015361 0 ustar fabio fabio *.debhelper.log
debian/*.debhelper
debian/*.substvars
debian/debhelper-build-stamp
debian/files
debian/gir1.2-cmenu-3.0/
debian/libcinnamon-menu-3-0-dbg/
debian/libcinnamon-menu-3-0/
debian/libcinnamon-menu-3-dev/
debian/tmp/
cinnamon-menus-6.0.0/README 0000664 0001750 0001750 00000002305 14526401433 014252 0 ustar fabio fabio cinnamon-menus
===========
cinnamon-menus contains the libcinnamon-menu library, the layout configuration
files for the Cinnamon menu, as well as a simple menu editor.
The libcinnamon-menu library implements the "Desktop Menu Specification"
from freedesktop.org:
http://freedesktop.org/wiki/Specifications/menu-spec
http://specifications.freedesktop.org/menu-spec/menu-spec-latest.html
You may download updates to the package from:
https://github.com/linuxmint/cinnamon-menus/releases
Installation
============
1) Run meson with options you like. The following configuration installs
all binaries, libs, and shared files into /usr/local, and enables all
available options:
meson debian/build \
--prefix=/usr/local \
--buildtype=plain \
-D deprecated_warnings=false
2) Compile and install (sudo is needed for install)
ninja -C debian/build
ninja -C debian/build install
3) You can uninstall the installed files with
ninja -C debian/build uninstall
How to report bugs
==================
Bugs should be reported to the Cinnamon bug tracking system:
https://github.com/linuxmint/cinnamon-menus/issues
You will need to create an account for yourself.
cinnamon-menus-6.0.0/AUTHORS 0000664 0001750 0001750 00000000141 14526401433 014436 0 ustar fabio fabio Mark McLoughlin
Havoc Pennington
Vincent Untz
cinnamon-menus-6.0.0/COPYING 0000664 0001750 0001750 00000043254 14526401433 014435 0 ustar fabio fabio GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
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.
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.
cinnamon-menus-6.0.0/.github/ 0000775 0001750 0001750 00000000000 14526401433 014732 5 ustar fabio fabio cinnamon-menus-6.0.0/.github/workflows/ 0000775 0001750 0001750 00000000000 14526401433 016767 5 ustar fabio fabio cinnamon-menus-6.0.0/.github/workflows/build.yml 0000664 0001750 0001750 00000001071 14526401433 020610 0 ustar fabio fabio name: Build
on:
push:
branches:
- master
pull_request:
branches:
- master
workflow_dispatch:
inputs:
debug_enabled:
type: boolean
description: 'Start an SSH server on failure.'
required: false
default: false
jobs:
build:
uses: linuxmint/github-actions/.github/workflows/do-builds.yml@master
with:
commit_id: master
############################## Comma separated list - like 'linuxmint/xapp, linuxmint/cinnamon-desktop'
dependencies:
##############################
cinnamon-menus-6.0.0/libmenu/ 0000775 0001750 0001750 00000000000 14526401433 015025 5 ustar fabio fabio cinnamon-menus-6.0.0/libmenu/menu-monitor.h 0000664 0001750 0001750 00000003456 14526401433 017637 0 ustar fabio fabio /*
* Copyright (C) 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __MENU_MONITOR_H__
#define __MENU_MONITOR_H__
#include
G_BEGIN_DECLS
typedef struct MenuMonitor MenuMonitor;
typedef enum
{
MENU_MONITOR_EVENT_INVALID = 0,
MENU_MONITOR_EVENT_CREATED = 1,
MENU_MONITOR_EVENT_DELETED = 2,
MENU_MONITOR_EVENT_CHANGED = 3
} MenuMonitorEvent;
typedef void (*MenuMonitorNotifyFunc) (MenuMonitor *monitor,
MenuMonitorEvent event,
const char *path,
gpointer user_data);
MenuMonitor *menu_get_file_monitor (const char *path);
MenuMonitor *menu_get_directory_monitor (const char *path);
MenuMonitor *menu_monitor_ref (MenuMonitor *monitor);
void menu_monitor_unref (MenuMonitor *monitor);
void menu_monitor_add_notify (MenuMonitor *monitor,
MenuMonitorNotifyFunc notify_func,
gpointer user_data);
void menu_monitor_remove_notify (MenuMonitor *monitor,
MenuMonitorNotifyFunc notify_func,
gpointer user_data);
G_END_DECLS
#endif /* __MENU_MONITOR_H__ */
cinnamon-menus-6.0.0/libmenu/desktop-entries.c 0000664 0001750 0001750 00000060537 14526401433 020324 0 ustar fabio fabio /*
* Copyright (C) 2002 - 2004 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "desktop-entries.h"
#include "gmenu-desktopappinfo.h"
#include
#include "menu-util.h"
#define DESKTOP_ENTRY_GROUP "Desktop Entry"
struct DesktopEntry
{
guint refcount;
char *path;
const char *basename;
guint type : 2;
guint reserved : 30;
};
typedef struct
{
DesktopEntry base;
GMenuDesktopAppInfo *appinfo;
GQuark *categories;
guint showin : 1;
} DesktopEntryDesktop;
typedef struct
{
DesktopEntry base;
char *name;
char *generic_name;
char *comment;
GIcon *icon;
guint nodisplay : 1;
guint hidden : 1;
guint showin : 1;
} DesktopEntryDirectory;
struct DesktopEntrySet
{
int refcount;
GHashTable *hash;
};
/*
* Desktop entries
*/
/**
* unix_basename_from_path:
* @path: Path string
*
* Returns: A constant pointer into the basename of @path
*/
static const char *
unix_basename_from_path (const char *path)
{
const char *basename = g_strrstr (path, "/");
if (basename)
return basename + 1;
else
return path;
}
static const char *
get_current_desktop (void)
{
static char *current_desktop = NULL;
/* Support XDG_CURRENT_DESKTOP environment variable; this can be used
* to abuse gnome-menus in non-GNOME desktops. */
if (!current_desktop)
{
const char *desktop;
desktop = g_getenv ("XDG_CURRENT_DESKTOP");
/* Note: if XDG_CURRENT_DESKTOP is set but empty, do as if it
* was not set */
if (!desktop || desktop[0] == '\0')
current_desktop = g_strdup ("GNOME");
else
current_desktop = g_strdup (desktop);
}
/* Using "*" means skipping desktop-related checks */
if (g_strcmp0 (current_desktop, "*") == 0)
return NULL;
return current_desktop;
}
static GIcon *
key_file_get_icon (GKeyFile *key_file)
{
GIcon *icon = NULL;
gchar *icon_name;
icon_name = g_key_file_get_locale_string (key_file, DESKTOP_ENTRY_GROUP,
"Icon", NULL, NULL);
if (!icon_name)
return NULL;
if (g_path_is_absolute (icon_name)) {
GFile *file;
file = g_file_new_for_path (icon_name);
icon = g_file_icon_new (file);
g_object_unref (file);
} else {
char *p;
/* Work around a common mistake in desktop files */
if ((p = strrchr (icon_name, '.')) != NULL &&
(strcmp (p, ".png") == 0 ||
strcmp (p, ".xpm") == 0 ||
strcmp (p, ".svg") == 0))
*p = 0;
icon = g_themed_icon_new (icon_name);
}
g_free (icon_name);
return icon;
}
static gboolean
key_file_get_show_in (GKeyFile *key_file)
{
const gchar *current_desktop;
gchar **strv;
gboolean show_in = TRUE;
int i;
gchar *exec;
current_desktop = get_current_desktop ();
if (!current_desktop)
return TRUE;
exec = g_key_file_get_string (key_file,
DESKTOP_ENTRY_GROUP,
"Exec",
NULL);
if (exec) {
if (g_str_has_prefix (exec, "gnome-control-center")) {
g_free (exec);
return FALSE;
}
g_free (exec);
}
strv = g_key_file_get_string_list (key_file,
DESKTOP_ENTRY_GROUP,
"OnlyShowIn",
NULL,
NULL);
if (strv)
{
show_in = FALSE;
for (i = 0; strv[i]; i++)
{
if (!strcmp (strv[i], "GNOME") || !strcmp (strv[i], "X-Cinnamon"))
{
show_in = TRUE;
break;
}
}
}
else
{
strv = g_key_file_get_string_list (key_file,
DESKTOP_ENTRY_GROUP,
"NotShowIn",
NULL,
NULL);
if (strv)
{
show_in = TRUE;
for (i = 0; strv[i]; i++)
{
if (!strcmp (strv[i], current_desktop))
{
show_in = FALSE;
}
}
}
}
g_strfreev (strv);
return show_in;
}
static gboolean
desktop_entry_load_directory (DesktopEntry *entry,
GKeyFile *key_file,
GError **error)
{
DesktopEntryDirectory *entry_directory = (DesktopEntryDirectory*)entry;
char *type_str;
type_str = g_key_file_get_string (key_file, DESKTOP_ENTRY_GROUP, "Type", error);
if (!type_str)
return FALSE;
if (strcmp (type_str, "Directory") != 0)
{
g_set_error (error,
G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE,
"\"%s\" does not contain the correct \"Type\" value\n", entry->path);
g_free (type_str);
return FALSE;
}
g_free (type_str);
entry_directory->name = g_key_file_get_locale_string (key_file, DESKTOP_ENTRY_GROUP, "Name", NULL, error);
if (entry_directory->name == NULL)
return FALSE;
entry_directory->generic_name = g_key_file_get_locale_string (key_file, DESKTOP_ENTRY_GROUP, "GenericName", NULL, NULL);
entry_directory->comment = g_key_file_get_locale_string (key_file, DESKTOP_ENTRY_GROUP, "Comment", NULL, NULL);
entry_directory->icon = key_file_get_icon (key_file);
entry_directory->nodisplay = g_key_file_get_boolean (key_file,
DESKTOP_ENTRY_GROUP,
"NoDisplay",
NULL);
entry_directory->hidden = g_key_file_get_boolean (key_file,
DESKTOP_ENTRY_GROUP,
"Hidden",
NULL);
entry_directory->showin = key_file_get_show_in (key_file);
return TRUE;
}
static DesktopEntryResultCode
desktop_entry_load (DesktopEntry *entry)
{
if (strstr (entry->path, "/menu-xdg/"))
return DESKTOP_ENTRY_LOAD_FAIL_OTHER;
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
GKeyFile *key_file = NULL;
DesktopEntryDesktop *entry_desktop = (DesktopEntryDesktop*)entry;
const char *categories_str;
entry_desktop->appinfo = gmenu_desktopappinfo_new_from_filename (entry->path);
if (!GMENU_IS_DESKTOPAPPINFO (((DesktopEntryDesktop*)entry)->appinfo))
{
menu_verbose ("Failed to load \"%s\"\n", entry->path);
return DESKTOP_ENTRY_LOAD_FAIL_APPINFO;
}
categories_str = gmenu_desktopappinfo_get_categories (entry_desktop->appinfo);
if (categories_str)
{
char **categories;
int i;
categories = g_strsplit (categories_str, ";", -1);
entry_desktop->categories = g_new0 (GQuark, g_strv_length (categories) + 1);
for (i = 0; categories[i]; i++)
entry_desktop->categories[i] = g_quark_from_string (categories[i]);
g_strfreev (categories);
}
key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, entry->path, 0, NULL))
entry_desktop->showin = TRUE;
else
entry_desktop->showin = key_file_get_show_in (key_file);
g_key_file_free (key_file);
return DESKTOP_ENTRY_LOAD_SUCCESS;
}
else if (entry->type == DESKTOP_ENTRY_DIRECTORY)
{
GKeyFile *key_file = NULL;
GError *error = NULL;
DesktopEntryResultCode rescode = DESKTOP_ENTRY_LOAD_SUCCESS;
key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, entry->path, 0, &error))
{
rescode = DESKTOP_ENTRY_LOAD_FAIL_OTHER;
goto out;
}
if (!desktop_entry_load_directory (entry, key_file, &error))
{
rescode = DESKTOP_ENTRY_LOAD_FAIL_OTHER;
goto out;
}
rescode = DESKTOP_ENTRY_LOAD_SUCCESS;
out:
g_key_file_free (key_file);
if (rescode == DESKTOP_ENTRY_LOAD_FAIL_OTHER)
{
if (error)
{
menu_verbose ("Failed to load \"%s\": %s\n", entry->path, error->message);
g_error_free (error);
}
else
menu_verbose ("Failed to load \"%s\"\n", entry->path);
}
return rescode;
}
else
g_assert_not_reached ();
return DESKTOP_ENTRY_LOAD_FAIL_OTHER;
}
DesktopEntry *
desktop_entry_new (const char *path,
DesktopEntryResultCode *res_code)
{
DesktopEntryType type;
DesktopEntry *retval;
DesktopEntryResultCode code;
menu_verbose ("Loading desktop entry \"%s\"\n", path);
if (g_str_has_suffix (path, ".desktop"))
{
type = DESKTOP_ENTRY_DESKTOP;
retval = (DesktopEntry*)g_new0 (DesktopEntryDesktop, 1);
}
else if (g_str_has_suffix (path, ".directory"))
{
type = DESKTOP_ENTRY_DIRECTORY;
retval = (DesktopEntry*)g_new0 (DesktopEntryDirectory, 1);
}
else
{
menu_verbose ("Unknown desktop entry suffix in \"%s\"\n",
path);
*res_code = DESKTOP_ENTRY_LOAD_FAIL_OTHER;
return NULL;
}
retval->refcount = 1;
retval->type = type;
retval->path = g_strdup (path);
retval->basename = unix_basename_from_path (retval->path);
code = desktop_entry_load (retval);
*res_code = code;
if (code < DESKTOP_ENTRY_LOAD_SUCCESS)
{
desktop_entry_unref (retval);
return NULL;
}
return retval;
}
DesktopEntry *
desktop_entry_reload (DesktopEntry *entry)
{
g_return_val_if_fail (entry != NULL, NULL);
menu_verbose ("Re-loading desktop entry \"%s\"\n", entry->path);
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
DesktopEntryDesktop *entry_desktop = (DesktopEntryDesktop *) entry;
g_object_unref (entry_desktop->appinfo);
entry_desktop->appinfo = NULL;
g_free (entry_desktop->categories);
entry_desktop->categories = NULL;
}
else if (entry->type == DESKTOP_ENTRY_DIRECTORY)
{
DesktopEntryDirectory *entry_directory = (DesktopEntryDirectory*) entry;
g_free (entry_directory->name);
entry_directory->name = NULL;
g_free (entry_directory->comment);
entry_directory->comment = NULL;
g_object_unref (entry_directory->icon);
entry_directory->icon = NULL;
}
else
g_assert_not_reached ();
if (desktop_entry_load (entry) < DESKTOP_ENTRY_LOAD_SUCCESS)
{
desktop_entry_unref (entry);
return NULL;
}
return entry;
}
DesktopEntry *
desktop_entry_ref (DesktopEntry *entry)
{
g_return_val_if_fail (entry != NULL, NULL);
g_return_val_if_fail (entry->refcount > 0, NULL);
entry->refcount += 1;
return entry;
}
DesktopEntry *
desktop_entry_copy (DesktopEntry *entry)
{
DesktopEntry *retval;
menu_verbose ("Copying desktop entry \"%s\"\n",
entry->basename);
if (entry->type == DESKTOP_ENTRY_DESKTOP)
retval = (DesktopEntry*)g_new0 (DesktopEntryDesktop, 1);
else if (entry->type == DESKTOP_ENTRY_DIRECTORY)
retval = (DesktopEntry*)g_new0 (DesktopEntryDirectory, 1);
else
g_assert_not_reached ();
retval->refcount = 1;
retval->type = entry->type;
retval->path = g_strdup (entry->path);
retval->basename = unix_basename_from_path (retval->path);
if (retval->type == DESKTOP_ENTRY_DESKTOP)
{
DesktopEntryDesktop *desktop_entry = (DesktopEntryDesktop*) entry;
DesktopEntryDesktop *retval_desktop_entry = (DesktopEntryDesktop*) retval;
int i;
retval_desktop_entry->appinfo = g_object_ref (desktop_entry->appinfo);
if (desktop_entry->categories != NULL)
{
i = 0;
for (; desktop_entry->categories[i]; i++);
retval_desktop_entry->categories = g_new0 (GQuark, i + 1);
i = 0;
for (; desktop_entry->categories[i]; i++)
retval_desktop_entry->categories[i] = desktop_entry->categories[i];
}
else
retval_desktop_entry->categories = NULL;
}
else if (entry->type == DESKTOP_ENTRY_DIRECTORY)
{
DesktopEntryDirectory *entry_directory = (DesktopEntryDirectory*)entry;
DesktopEntryDirectory *retval_directory = (DesktopEntryDirectory*)retval;
retval_directory->name = g_strdup (entry_directory->name);
retval_directory->comment = g_strdup (entry_directory->comment);
retval_directory->icon = g_object_ref (entry_directory->icon);
retval_directory->nodisplay = entry_directory->nodisplay;
retval_directory->hidden = entry_directory->hidden;
retval_directory->showin = entry_directory->showin;
}
return retval;
}
void
desktop_entry_unref (DesktopEntry *entry)
{
g_return_if_fail (entry != NULL);
g_return_if_fail (entry->refcount > 0);
entry->refcount -= 1;
if (entry->refcount != 0)
return;
g_free (entry->path);
entry->path = NULL;
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
DesktopEntryDesktop *desktop_entry = (DesktopEntryDesktop*) entry;
g_free (desktop_entry->categories);
if (desktop_entry->appinfo)
{
g_object_unref (desktop_entry->appinfo);
desktop_entry->appinfo = NULL;
}
}
else if (entry->type == DESKTOP_ENTRY_DIRECTORY)
{
DesktopEntryDirectory *entry_directory = (DesktopEntryDirectory*) entry;
g_free (entry_directory->name);
entry_directory->name = NULL;
g_free (entry_directory->comment);
entry_directory->comment = NULL;
if (entry_directory->icon != NULL)
{
g_object_unref (entry_directory->icon);
entry_directory->icon = NULL;
}
}
else
g_assert_not_reached ();
g_free (entry);
}
DesktopEntryType
desktop_entry_get_type (DesktopEntry *entry)
{
return entry->type;
}
const char *
desktop_entry_get_path (DesktopEntry *entry)
{
return entry->path;
}
const char *
desktop_entry_get_basename (DesktopEntry *entry)
{
return entry->basename;
}
const char *
desktop_entry_get_name (DesktopEntry *entry)
{
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
g_return_val_if_fail (GMENU_IS_DESKTOPAPPINFO (((DesktopEntryDesktop*)entry)->appinfo), NULL);
return g_app_info_get_name (G_APP_INFO (((DesktopEntryDesktop*)entry)->appinfo));
}
return ((DesktopEntryDirectory*)entry)->name;
}
const char *
desktop_entry_get_generic_name (DesktopEntry *entry)
{
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
g_return_val_if_fail (GMENU_IS_DESKTOPAPPINFO (((DesktopEntryDesktop*)entry)->appinfo), NULL);
return gmenu_desktopappinfo_get_generic_name (((DesktopEntryDesktop*)entry)->appinfo);
}
return ((DesktopEntryDirectory*)entry)->generic_name;
}
const char *
desktop_entry_get_comment (DesktopEntry *entry)
{
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
g_return_val_if_fail (GMENU_IS_DESKTOPAPPINFO (((DesktopEntryDesktop*)entry)->appinfo), NULL);
return g_app_info_get_description (G_APP_INFO (((DesktopEntryDesktop*)entry)->appinfo));
}
return ((DesktopEntryDirectory*)entry)->comment;
}
GIcon *
desktop_entry_get_icon (DesktopEntry *entry)
{
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
g_return_val_if_fail (GMENU_IS_DESKTOPAPPINFO (((DesktopEntryDesktop*)entry)->appinfo), NULL);
return g_app_info_get_icon (G_APP_INFO (((DesktopEntryDesktop*)entry)->appinfo));
}
return ((DesktopEntryDirectory*)entry)->icon;
}
gboolean
desktop_entry_get_no_display (DesktopEntry *entry)
{
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
g_return_val_if_fail (GMENU_IS_DESKTOPAPPINFO (((DesktopEntryDesktop*)entry)->appinfo), FALSE);
return gmenu_desktopappinfo_get_nodisplay (((DesktopEntryDesktop*)entry)->appinfo);
}
return ((DesktopEntryDirectory*)entry)->nodisplay;
}
gboolean
desktop_entry_get_hidden (DesktopEntry *entry)
{
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
g_return_val_if_fail (GMENU_IS_DESKTOPAPPINFO (((DesktopEntryDesktop*)entry)->appinfo), FALSE);
return gmenu_desktopappinfo_get_is_hidden (((DesktopEntryDesktop*)entry)->appinfo);
}
return ((DesktopEntryDirectory*)entry)->hidden;
}
gboolean
desktop_entry_get_show_in (DesktopEntry *entry)
{
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
const char *current_desktop = get_current_desktop ();
if (current_desktop == NULL)
return TRUE;
else {
return ((DesktopEntryDesktop *)entry)->showin;
}
}
return ((DesktopEntryDirectory*)entry)->showin;
}
const char *
desktop_entry_get_id (DesktopEntry *entry)
{
if (entry->type == DESKTOP_ENTRY_DESKTOP)
{
g_return_val_if_fail (GMENU_IS_DESKTOPAPPINFO (((DesktopEntryDesktop*)entry)->appinfo), NULL);
return g_app_info_get_id (G_APP_INFO (((DesktopEntryDesktop*)entry)->appinfo));
}
// this only applies to non-desktop entries
return entry->basename;
}
GMenuDesktopAppInfo *
desktop_entry_get_app_info (DesktopEntry *entry)
{
g_return_val_if_fail (entry->type == DESKTOP_ENTRY_DESKTOP, NULL);
return ((DesktopEntryDesktop*)entry)->appinfo;
}
gboolean
desktop_entry_has_categories (DesktopEntry *entry)
{
DesktopEntryDesktop *desktop_entry;
if (entry->type != DESKTOP_ENTRY_DESKTOP)
return FALSE;
desktop_entry = (DesktopEntryDesktop*) entry;
return (desktop_entry->categories != NULL && desktop_entry->categories[0] != 0);
}
gboolean
desktop_entry_has_category (DesktopEntry *entry,
const char *category)
{
GQuark quark;
int i;
DesktopEntryDesktop *desktop_entry;
if (entry->type != DESKTOP_ENTRY_DESKTOP)
return FALSE;
desktop_entry = (DesktopEntryDesktop*) entry;
if (desktop_entry->categories == NULL)
return FALSE;
if (!(quark = g_quark_try_string (category)))
return FALSE;
for (i = 0; desktop_entry->categories[i]; i++)
{
if (quark == desktop_entry->categories[i])
return TRUE;
}
return FALSE;
}
/*
* Entry sets
*/
DesktopEntrySet *
desktop_entry_set_new (void)
{
DesktopEntrySet *set;
set = g_new0 (DesktopEntrySet, 1);
set->refcount = 1;
menu_verbose (" New entry set %p\n", set);
return set;
}
DesktopEntrySet *
desktop_entry_set_ref (DesktopEntrySet *set)
{
g_return_val_if_fail (set != NULL, NULL);
g_return_val_if_fail (set->refcount > 0, NULL);
set->refcount += 1;
return set;
}
void
desktop_entry_set_unref (DesktopEntrySet *set)
{
g_return_if_fail (set != NULL);
g_return_if_fail (set->refcount > 0);
set->refcount -= 1;
if (set->refcount == 0)
{
menu_verbose (" Deleting entry set %p\n", set);
if (set->hash)
g_hash_table_destroy (set->hash);
set->hash = NULL;
g_free (set);
}
}
void
desktop_entry_set_add_entry (DesktopEntrySet *set,
DesktopEntry *entry,
const char *file_id)
{
menu_verbose (" Adding to set %p entry %s\n", set, file_id);
if (set->hash == NULL)
{
set->hash = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify) desktop_entry_unref);
}
g_hash_table_replace (set->hash,
g_strdup (file_id),
desktop_entry_ref (entry));
}
DesktopEntry *
desktop_entry_set_lookup (DesktopEntrySet *set,
const char *file_id)
{
if (set->hash == NULL)
return NULL;
return g_hash_table_lookup (set->hash, file_id);
}
typedef struct
{
DesktopEntrySetForeachFunc func;
gpointer user_data;
} EntryHashForeachData;
static void
entry_hash_foreach (const char *file_id,
DesktopEntry *entry,
EntryHashForeachData *fd)
{
fd->func (file_id, entry, fd->user_data);
}
void
desktop_entry_set_foreach (DesktopEntrySet *set,
DesktopEntrySetForeachFunc func,
gpointer user_data)
{
g_return_if_fail (set != NULL);
g_return_if_fail (func != NULL);
if (set->hash != NULL)
{
EntryHashForeachData fd;
fd.func = func;
fd.user_data = user_data;
g_hash_table_foreach (set->hash,
(GHFunc) entry_hash_foreach,
&fd);
}
}
static void
desktop_entry_set_clear (DesktopEntrySet *set)
{
menu_verbose (" Clearing set %p\n", set);
if (set->hash != NULL)
{
g_hash_table_destroy (set->hash);
set->hash = NULL;
}
}
int
desktop_entry_set_get_count (DesktopEntrySet *set)
{
if (set->hash == NULL)
return 0;
return g_hash_table_size (set->hash);
}
static void
union_foreach (const char *file_id,
DesktopEntry *entry,
DesktopEntrySet *set)
{
/* we are iterating over "with" adding anything not
* already in "set". We unconditionally overwrite
* the stuff in "set" because we can assume
* two entries with the same name are equivalent.
*/
desktop_entry_set_add_entry (set, entry, file_id);
}
void
desktop_entry_set_union (DesktopEntrySet *set,
DesktopEntrySet *with)
{
menu_verbose (" Union of %p and %p\n", set, with);
if (desktop_entry_set_get_count (with) == 0)
return; /* A fast simple case */
g_hash_table_foreach (with->hash,
(GHFunc) union_foreach,
set);
}
typedef struct
{
DesktopEntrySet *set;
DesktopEntrySet *with;
} IntersectData;
static gboolean
intersect_foreach_remove (const char *file_id,
DesktopEntry *entry,
IntersectData *id)
{
/* Remove everything in "set" which is not in "with" */
if (g_hash_table_lookup (id->with->hash, file_id) != NULL)
return FALSE;
menu_verbose (" Removing from %p entry %s\n", id->set, file_id);
return TRUE; /* return TRUE to remove */
}
void
desktop_entry_set_intersection (DesktopEntrySet *set,
DesktopEntrySet *with)
{
IntersectData id;
menu_verbose (" Intersection of %p and %p\n", set, with);
if (desktop_entry_set_get_count (set) == 0 ||
desktop_entry_set_get_count (with) == 0)
{
/* A fast simple case */
desktop_entry_set_clear (set);
return;
}
id.set = set;
id.with = with;
g_hash_table_foreach_remove (set->hash,
(GHRFunc) intersect_foreach_remove,
&id);
}
typedef struct
{
DesktopEntrySet *set;
DesktopEntrySet *other;
} SubtractData;
static gboolean
subtract_foreach_remove (const char *file_id,
DesktopEntry *entry,
SubtractData *sd)
{
/* Remove everything in "set" which is not in "other" */
if (g_hash_table_lookup (sd->other->hash, file_id) == NULL)
return FALSE;
menu_verbose (" Removing from %p entry %s\n", sd->set, file_id);
return TRUE; /* return TRUE to remove */
}
void
desktop_entry_set_subtract (DesktopEntrySet *set,
DesktopEntrySet *other)
{
SubtractData sd;
menu_verbose (" Subtract from %p set %p\n", set, other);
if (desktop_entry_set_get_count (set) == 0 ||
desktop_entry_set_get_count (other) == 0)
return; /* A fast simple case */
sd.set = set;
sd.other = other;
g_hash_table_foreach_remove (set->hash,
(GHRFunc) subtract_foreach_remove,
&sd);
}
void
desktop_entry_set_swap_contents (DesktopEntrySet *a,
DesktopEntrySet *b)
{
GHashTable *tmp;
menu_verbose (" Swap contents of %p and %p\n", a, b);
tmp = a->hash;
a->hash = b->hash;
b->hash = tmp;
}
cinnamon-menus-6.0.0/libmenu/entry-directories.c 0000664 0001750 0001750 00000076605 14526401433 020662 0 ustar fabio fabio /*
* Copyright (C) 2002 - 2004 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "entry-directories.h"
#include
#include
#include
#include
#include
#include "menu-util.h"
#include "menu-monitor.h"
typedef struct CachedDir CachedDir;
typedef struct CachedDirMonitor CachedDirMonitor;
struct EntryDirectory
{
CachedDir *dir;
guint entry_type : 2;
guint refcount : 24;
};
struct EntryDirectoryList
{
int refcount;
int length;
GList *dirs;
};
struct CachedDir
{
CachedDir *parent;
char *name;
GSList *entries;
GSList *subdirs;
GSList *retry_later_desktop_entries;
MenuMonitor *dir_monitor;
GSList *monitors;
guint have_read_entries : 1;
guint deleted : 1;
guint references;
GFunc notify;
gpointer notify_data;
};
struct CachedDirMonitor
{
EntryDirectory *ed;
EntryDirectoryChangedFunc callback;
gpointer user_data;
};
static void cached_dir_add_reference (CachedDir *dir);
static void cached_dir_remove_reference (CachedDir *dir);
static void cached_dir_free (CachedDir *dir);
static gboolean cached_dir_load_entries_recursive (CachedDir *dir,
const char *dirname);
static void cached_dir_unref (CachedDir *dir);
static void cached_dir_unref_noparent (CachedDir *dir);
static CachedDir * cached_dir_add_subdir (CachedDir *dir,
const char *basename,
const char *path);
static gboolean cached_dir_remove_subdir (CachedDir *dir,
const char *basename);
static void handle_cached_dir_changed (MenuMonitor *monitor,
MenuMonitorEvent event,
const char *path,
CachedDir *dir);
/*
* Entry directory cache
*/
static CachedDir *dir_cache = NULL;
static void
clear_cache (CachedDir *dir,
gpointer *cache)
{
*cache = NULL;
}
static CachedDir *
cached_dir_new (const char *name)
{
CachedDir *dir;
dir = g_new0 (CachedDir, 1);
dir->name = g_strdup (name);
return dir;
}
static CachedDir *
cached_dir_new_full (const char *name,
GFunc notify,
gpointer notify_data)
{
CachedDir *dir;
dir = cached_dir_new (name);
dir->notify = notify;
dir->notify_data = notify_data;
return dir;
}
static void
cached_dir_free (CachedDir *dir)
{
if (dir->dir_monitor)
{
menu_monitor_remove_notify (dir->dir_monitor,
(MenuMonitorNotifyFunc) handle_cached_dir_changed,
dir);
menu_monitor_unref (dir->dir_monitor);
dir->dir_monitor = NULL;
}
g_slist_foreach (dir->monitors, (GFunc) g_free, NULL);
g_slist_free (dir->monitors);
dir->monitors = NULL;
g_slist_foreach (dir->entries,
(GFunc) desktop_entry_unref,
NULL);
g_slist_free (dir->entries);
dir->entries = NULL;
g_slist_foreach (dir->subdirs,
(GFunc) cached_dir_unref_noparent,
NULL);
g_slist_free (dir->subdirs);
dir->subdirs = NULL;
g_slist_free_full (dir->retry_later_desktop_entries, g_free);
dir->retry_later_desktop_entries = NULL;
g_free (dir->name);
g_free (dir);
}
static CachedDir *
cached_dir_ref (CachedDir *dir)
{
dir->references++;
return dir;
}
static void
cached_dir_unref (CachedDir *dir)
{
if (--dir->references == 0)
{
CachedDir *parent;
parent = dir->parent;
if (parent != NULL)
cached_dir_remove_subdir (parent, dir->name);
if (dir->notify)
dir->notify (dir, dir->notify_data);
cached_dir_free (dir);
}
}
static void
cached_dir_unref_noparent (CachedDir *dir)
{
if (--dir->references == 0)
{
if (dir->notify)
dir->notify (dir, dir->notify_data);
cached_dir_free (dir);
}
}
static inline CachedDir *
find_subdir (CachedDir *dir,
const char *subdir)
{
GSList *tmp;
tmp = dir->subdirs;
while (tmp != NULL)
{
CachedDir *sub = tmp->data;
if (strcmp (sub->name, subdir) == 0)
return sub;
tmp = tmp->next;
}
return NULL;
}
static DesktopEntry *
find_entry (CachedDir *dir,
const char *basename)
{
GSList *tmp;
tmp = dir->entries;
while (tmp != NULL)
{
if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
return tmp->data;
tmp = tmp->next;
}
return NULL;
}
static DesktopEntry *
cached_dir_find_relative_path (CachedDir *dir,
const char *relative_path)
{
DesktopEntry *retval = NULL;
char **split;
int i;
split = g_strsplit (relative_path, "/", -1);
i = 0;
while (split[i] != NULL)
{
if (split[i + 1] != NULL)
{
if ((dir = find_subdir (dir, split[i])) == NULL)
break;
}
else
{
retval = find_entry (dir, split[i]);
break;
}
++i;
}
g_strfreev (split);
return retval;
}
static CachedDir *
cached_dir_lookup (const char *canonical)
{
CachedDir *dir;
char **split;
int i;
if (dir_cache == NULL)
dir_cache = cached_dir_new_full ("/",
(GFunc) clear_cache,
&dir_cache);
dir = dir_cache;
g_assert (canonical != NULL && canonical[0] == G_DIR_SEPARATOR);
menu_verbose ("Looking up cached dir \"%s\"\n", canonical);
split = g_strsplit (canonical + 1, "/", -1);
i = 0;
while (split[i] != NULL)
{
CachedDir *subdir;
subdir = cached_dir_add_subdir (dir, split[i], NULL);
dir = subdir;
++i;
}
g_strfreev (split);
g_assert (dir != NULL);
return dir;
}
static gboolean
cached_dir_add_entry (CachedDir *dir,
const char *basename,
const char *path)
{
DesktopEntry *entry;
DesktopEntryResultCode code;
entry = desktop_entry_new (path, &code);
if (entry == NULL)
{
if (code == DESKTOP_ENTRY_LOAD_FAIL_APPINFO)
{
menu_verbose ("Adding %s to the retry list (mimeinfo.cache maybe isn't done getting updated yet\n", path);
dir->retry_later_desktop_entries = g_slist_prepend (dir->retry_later_desktop_entries, g_strdup (path));
}
return FALSE;
}
dir->entries = g_slist_prepend (dir->entries, entry);
return TRUE;
}
static gboolean
cached_dir_update_entry (CachedDir *dir,
const char *basename,
const char *path)
{
GSList *tmp;
tmp = dir->entries;
while (tmp != NULL)
{
if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
{
if (!desktop_entry_reload (tmp->data))
{
dir->entries = g_slist_delete_link (dir->entries, tmp);
}
return TRUE;
}
tmp = tmp->next;
}
return cached_dir_add_entry (dir, basename, path);
}
static gboolean
cached_dir_remove_entry (CachedDir *dir,
const char *basename)
{
GSList *tmp;
tmp = dir->entries;
while (tmp != NULL)
{
if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
{
desktop_entry_unref (tmp->data);
dir->entries = g_slist_delete_link (dir->entries, tmp);
return TRUE;
}
tmp = tmp->next;
}
return FALSE;
}
static CachedDir *
cached_dir_add_subdir (CachedDir *dir,
const char *basename,
const char *path)
{
CachedDir *subdir;
subdir = find_subdir (dir, basename);
if (subdir != NULL)
{
subdir->deleted = FALSE;
return subdir;
}
subdir = cached_dir_new (basename);
if (path != NULL && !cached_dir_load_entries_recursive (subdir, path))
{
cached_dir_free (subdir);
return NULL;
}
menu_verbose ("Caching dir \"%s\"\n", basename);
subdir->parent = dir;
dir->subdirs = g_slist_prepend (dir->subdirs, cached_dir_ref (subdir));
return subdir;
}
static gboolean
cached_dir_remove_subdir (CachedDir *dir,
const char *basename)
{
CachedDir *subdir;
subdir = find_subdir (dir, basename);
if (subdir != NULL)
{
subdir->deleted = TRUE;
cached_dir_unref (subdir);
dir->subdirs = g_slist_remove (dir->subdirs, subdir);
return TRUE;
}
return FALSE;
}
static guint monitors_idle_handler = 0;
static GSList *pending_monitors_dirs = NULL;
static void
cached_dir_invoke_monitors (CachedDir *dir)
{
GSList *tmp;
tmp = dir->monitors;
while (tmp != NULL)
{
CachedDirMonitor *monitor = tmp->data;
GSList *next = tmp->next;
monitor->callback (monitor->ed, monitor->user_data);
tmp = next;
}
/* we explicitly don't invoke monitors of the parent since an
* event has been queued for it too */
}
static gboolean
emit_monitors_in_idle (void)
{
GSList *monitors_to_emit;
GSList *tmp;
monitors_to_emit = pending_monitors_dirs;
pending_monitors_dirs = NULL;
monitors_idle_handler = 0;
tmp = monitors_to_emit;
while (tmp != NULL)
{
CachedDir *dir = tmp->data;
cached_dir_invoke_monitors (dir);
cached_dir_remove_reference (dir);
tmp = tmp->next;
}
g_slist_free (monitors_to_emit);
return FALSE;
}
static void
cached_dir_queue_monitor_event (CachedDir *dir)
{
GSList *tmp;
tmp = pending_monitors_dirs;
while (tmp != NULL)
{
CachedDir *d = tmp->data;
GSList *next = tmp->next;
if (dir->parent == d->parent &&
g_strcmp0 (dir->name, d->name) == 0)
break;
tmp = next;
}
/* not found, so let's queue it */
if (tmp == NULL)
{
cached_dir_add_reference (dir);
pending_monitors_dirs = g_slist_append (pending_monitors_dirs, dir);
}
if (dir->parent)
{
cached_dir_queue_monitor_event (dir->parent);
}
if (monitors_idle_handler > 0)
{
g_source_remove (monitors_idle_handler);
}
monitors_idle_handler = g_timeout_add (100, (GSourceFunc) emit_monitors_in_idle, NULL);
}
static void
handle_cached_dir_changed (MenuMonitor *monitor,
MenuMonitorEvent event,
const char *path,
CachedDir *dir)
{
gboolean handled = FALSE;
gboolean retry_changes = FALSE;
char *basename;
char *dirname;
dirname = g_path_get_dirname (path);
basename = g_path_get_basename (path);
dir = cached_dir_lookup (dirname);
cached_dir_add_reference (dir);
if (g_str_has_suffix (basename, ".desktop") ||
g_str_has_suffix (basename, ".directory"))
{
switch (event)
{
case MENU_MONITOR_EVENT_CREATED:
case MENU_MONITOR_EVENT_CHANGED:
handled = cached_dir_update_entry (dir, basename, path);
break;
case MENU_MONITOR_EVENT_DELETED:
handled = cached_dir_remove_entry (dir, basename);
break;
default:
g_assert_not_reached ();
break;
}
}
else if (g_strcmp0 (basename, "mimeinfo.cache") == 0)
{
/* The observed file notifies when a new desktop file is added
* (but fails to load) go something like:
*
* NOTIFY: foo.desktop
* NOTIFY: mimeinfo.cache.tempfile
* NOTIFY: mimeinfo.cache.tempfile
* NOTIFY: mimeinfo.cache
*
* Additionally, the failure is not upon trying to read the file,
* but attempting to get its GAppInfo (gmenu_desktopappinfo_new_from_filename()
* in desktop-entries.c ln 277). If you jigger desktop_entry_load() around
* and read the file as a keyfile *first*, it succeeds. If you then try
* to run gmenu_desktopappinfo_new_from_keyfile(), *then* it fails.
*
* The theory here is there is a race condition where app info (which includes
* mimetype stuff) is unavailable because mimeinfo.cache is updated immediately
* after the app is installed.
*
* What we do here is, when a desktop fails to load, we add it to a temporary
* list. We wait until mimeinfo.cache changes, then retry that desktop file,
* which succeeds this second time.
*
* Note: An alternative fix (presented more as a proof than a suggestion) is to
* change line 151 in menu-monitor.c to use g_timeout_add_seconds, and delay
* for one second. This also avoids the issue (but it remains a race condition).
*/
GSList *iter;
menu_verbose ("mimeinfo changed, checking for failed entries\n");
for (iter = dir->retry_later_desktop_entries; iter != NULL; iter = iter->next)
{
const gchar *retry_path = iter->data;
menu_verbose ("retrying %s\n", retry_path);
char *retry_basename = g_path_get_basename (retry_path);
if (cached_dir_update_entry (dir, retry_basename, retry_path))
retry_changes = TRUE;
g_free (retry_basename);
}
g_slist_free_full (dir->retry_later_desktop_entries, g_free);
dir->retry_later_desktop_entries = NULL;
handled = retry_changes;
}
else /* Try recursing */
{
switch (event)
{
case MENU_MONITOR_EVENT_CREATED:
handled = cached_dir_add_subdir (dir, basename, path) != NULL;
break;
case MENU_MONITOR_EVENT_CHANGED:
break;
case MENU_MONITOR_EVENT_DELETED:
handled = cached_dir_remove_subdir (dir, basename);
break;
default:
g_assert_not_reached ();
break;
}
}
g_free (basename);
g_free (dirname);
if (handled)
{
menu_verbose ("'%s' notified of '%s' %s - invalidating cache\n",
dir->name,
path,
event == MENU_MONITOR_EVENT_CREATED ? ("created") :
event == MENU_MONITOR_EVENT_DELETED ? ("deleted") : ("changed"));
/* CHANGED events don't change the set of desktop entries, unless it's the mimeinfo.cache file changing */
if (retry_changes || (event == MENU_MONITOR_EVENT_CREATED || event == MENU_MONITOR_EVENT_DELETED))
{
_entry_directory_list_empty_desktop_cache ();
}
cached_dir_queue_monitor_event (dir);
}
cached_dir_remove_reference (dir);
}
static void
cached_dir_ensure_monitor (CachedDir *dir,
const char *dirname)
{
if (dir->dir_monitor == NULL)
{
dir->dir_monitor = menu_get_directory_monitor (dirname);
menu_monitor_add_notify (dir->dir_monitor,
(MenuMonitorNotifyFunc) handle_cached_dir_changed,
dir);
}
}
static gboolean
cached_dir_load_entries_recursive (CachedDir *dir,
const char *dirname)
{
DIR *dp;
struct dirent *dent;
GString *fullpath;
gsize fullpath_len;
g_assert (dir != NULL);
if (dir->have_read_entries)
return TRUE;
menu_verbose ("Attempting to read entries from %s (full path %s)\n",
dir->name, dirname);
dp = opendir (dirname);
if (dp == NULL)
{
menu_verbose ("Unable to list directory \"%s\"\n",
dirname);
return FALSE;
}
cached_dir_ensure_monitor (dir, dirname);
fullpath = g_string_new (dirname);
if (fullpath->str[fullpath->len - 1] != G_DIR_SEPARATOR)
g_string_append_c (fullpath, G_DIR_SEPARATOR);
fullpath_len = fullpath->len;
while ((dent = readdir (dp)) != NULL)
{
/* ignore . and .. */
if (dent->d_name[0] == '.' &&
(dent->d_name[1] == '\0' ||
(dent->d_name[1] == '.' &&
dent->d_name[2] == '\0')))
continue;
g_string_append (fullpath, dent->d_name);
if (g_str_has_suffix (dent->d_name, ".desktop") ||
g_str_has_suffix (dent->d_name, ".directory"))
{
cached_dir_add_entry (dir, dent->d_name, fullpath->str);
}
else /* Try recursing */
{
cached_dir_add_subdir (dir, dent->d_name, fullpath->str);
}
g_string_truncate (fullpath, fullpath_len);
}
closedir (dp);
g_string_free (fullpath, TRUE);
dir->have_read_entries = TRUE;
return TRUE;
}
static void
cached_dir_add_monitor (CachedDir *dir,
EntryDirectory *ed,
EntryDirectoryChangedFunc callback,
gpointer user_data)
{
CachedDirMonitor *monitor;
GSList *tmp;
tmp = dir->monitors;
while (tmp != NULL)
{
monitor = tmp->data;
if (monitor->ed == ed &&
monitor->callback == callback &&
monitor->user_data == user_data)
break;
tmp = tmp->next;
}
if (tmp == NULL)
{
monitor = g_new0 (CachedDirMonitor, 1);
monitor->ed = ed;
monitor->callback = callback;
monitor->user_data = user_data;
dir->monitors = g_slist_append (dir->monitors, monitor);
}
}
static void
cached_dir_remove_monitor (CachedDir *dir,
EntryDirectory *ed,
EntryDirectoryChangedFunc callback,
gpointer user_data)
{
GSList *tmp;
tmp = dir->monitors;
while (tmp != NULL)
{
CachedDirMonitor *monitor = tmp->data;
GSList *next = tmp->next;
if (monitor->ed == ed &&
monitor->callback == callback &&
monitor->user_data == user_data)
{
dir->monitors = g_slist_delete_link (dir->monitors, tmp);
g_free (monitor);
}
tmp = next;
}
}
static void
cached_dir_add_reference (CachedDir *dir)
{
cached_dir_ref (dir);
if (dir->parent != NULL)
{
cached_dir_add_reference (dir->parent);
}
}
static void
cached_dir_remove_reference (CachedDir *dir)
{
CachedDir *parent;
parent = dir->parent;
cached_dir_unref (dir);
if (parent != NULL)
{
cached_dir_remove_reference (parent);
}
}
/*
* Entry directories
*/
EntryDirectory *
entry_directory_new (DesktopEntryType entry_type,
const char *path)
{
EntryDirectory *ed;
char *canonical;
menu_verbose ("Loading entry directory \"%s\"\n", path);
canonical = realpath (path, NULL);
if (canonical == NULL)
{
menu_verbose ("Failed to canonicalize \"%s\": %s\n",
path, g_strerror (errno));
return NULL;
}
ed = g_new0 (EntryDirectory, 1);
ed->dir = cached_dir_lookup (canonical);
g_assert (ed->dir != NULL);
cached_dir_add_reference (ed->dir);
cached_dir_load_entries_recursive (ed->dir, canonical);
ed->entry_type = entry_type;
ed->refcount = 1;
g_free (canonical);
return ed;
}
EntryDirectory *
entry_directory_ref (EntryDirectory *ed)
{
g_return_val_if_fail (ed != NULL, NULL);
g_return_val_if_fail (ed->refcount > 0, NULL);
ed->refcount++;
return ed;
}
void
entry_directory_unref (EntryDirectory *ed)
{
g_return_if_fail (ed != NULL);
g_return_if_fail (ed->refcount > 0);
if (--ed->refcount == 0)
{
cached_dir_remove_reference (ed->dir);
ed->dir = NULL;
ed->entry_type = DESKTOP_ENTRY_INVALID;
g_free (ed);
}
}
static void
entry_directory_add_monitor (EntryDirectory *ed,
EntryDirectoryChangedFunc callback,
gpointer user_data)
{
cached_dir_add_monitor (ed->dir, ed, callback, user_data);
}
static void
entry_directory_remove_monitor (EntryDirectory *ed,
EntryDirectoryChangedFunc callback,
gpointer user_data)
{
cached_dir_remove_monitor (ed->dir, ed, callback, user_data);
}
static DesktopEntry *
entry_directory_get_directory (EntryDirectory *ed,
const char *relative_path)
{
DesktopEntry *entry;
if (ed->entry_type != DESKTOP_ENTRY_DIRECTORY)
return NULL;
entry = cached_dir_find_relative_path (ed->dir, relative_path);
if (entry == NULL || desktop_entry_get_type (entry) != DESKTOP_ENTRY_DIRECTORY)
return NULL;
return desktop_entry_ref (entry);
}
static char *
get_desktop_file_id_from_path (EntryDirectory *ed,
DesktopEntryType entry_type,
const char *relative_path,
DesktopEntry *entry)
{
char *retval;
retval = NULL;
if (entry_type == DESKTOP_ENTRY_DESKTOP)
{
GMenuDesktopAppInfo *appinfo;
appinfo = desktop_entry_get_app_info (entry);
retval = g_strdelimit (g_strdup (relative_path), "/", '-');
if (gmenu_desktopappinfo_get_is_flatpak (appinfo))
{
char* tmp;
tmp = retval;
retval = g_strconcat (retval, GMENU_DESKTOPAPPINFO_FLATPAK_SUFFIX, NULL);
g_free (tmp);
}
}
else
{
retval = g_strdup (relative_path);
}
return retval;
}
typedef gboolean (* EntryDirectoryForeachFunc) (EntryDirectory *ed,
DesktopEntry *entry,
const char *file_id,
DesktopEntrySet *set,
gpointer user_data);
static gboolean
entry_directory_foreach_recursive (EntryDirectory *ed,
CachedDir *cd,
GString *relative_path,
EntryDirectoryForeachFunc func,
DesktopEntrySet *set,
gpointer user_data)
{
GSList *tmp;
int relative_path_len;
if (cd->deleted)
return TRUE;
relative_path_len = relative_path->len;
tmp = cd->entries;
while (tmp != NULL)
{
DesktopEntry *entry = tmp->data;
if (desktop_entry_get_type (entry) == ed->entry_type)
{
gboolean ret;
char *file_id;
g_string_append (relative_path,
desktop_entry_get_basename (entry));
file_id = get_desktop_file_id_from_path (ed,
ed->entry_type,
relative_path->str,
entry);
ret = func (ed, entry, file_id, set, user_data);
g_free (file_id);
g_string_truncate (relative_path, relative_path_len);
if (!ret)
return FALSE;
}
tmp = tmp->next;
}
tmp = cd->subdirs;
while (tmp != NULL)
{
CachedDir *subdir = tmp->data;
g_string_append (relative_path, subdir->name);
g_string_append_c (relative_path, G_DIR_SEPARATOR);
if (!entry_directory_foreach_recursive (ed,
subdir,
relative_path,
func,
set,
user_data))
return FALSE;
g_string_truncate (relative_path, relative_path_len);
tmp = tmp->next;
}
return TRUE;
}
static void
entry_directory_foreach (EntryDirectory *ed,
EntryDirectoryForeachFunc func,
DesktopEntrySet *set,
gpointer user_data)
{
GString *path;
path = g_string_new (NULL);
entry_directory_foreach_recursive (ed,
ed->dir,
path,
func,
set,
user_data);
g_string_free (path, TRUE);
}
void
entry_directory_get_flat_contents (EntryDirectory *ed,
DesktopEntrySet *desktop_entries,
DesktopEntrySet *directory_entries,
GSList **subdirs)
{
GSList *tmp;
if (subdirs)
*subdirs = NULL;
tmp = ed->dir->entries;
while (tmp != NULL)
{
DesktopEntry *entry = tmp->data;
const char *basename;
basename = desktop_entry_get_basename (entry);
if (desktop_entries &&
desktop_entry_get_type (entry) == DESKTOP_ENTRY_DESKTOP)
{
desktop_entry_set_add_entry (desktop_entries,
entry,
NULL);
}
if (directory_entries &&
desktop_entry_get_type (entry) == DESKTOP_ENTRY_DIRECTORY)
{
desktop_entry_set_add_entry (directory_entries,
entry,
basename);
}
tmp = tmp->next;
}
if (subdirs)
{
tmp = ed->dir->subdirs;
while (tmp != NULL)
{
CachedDir *cd = tmp->data;
if (!cd->deleted)
{
*subdirs = g_slist_prepend (*subdirs, g_strdup (cd->name));
}
tmp = tmp->next;
}
}
if (subdirs)
*subdirs = g_slist_reverse (*subdirs);
}
/*
* Entry directory lists
*/
EntryDirectoryList *
entry_directory_list_new (void)
{
EntryDirectoryList *list;
list = g_new0 (EntryDirectoryList, 1);
list->refcount = 1;
list->dirs = NULL;
list->length = 0;
return list;
}
EntryDirectoryList *
entry_directory_list_ref (EntryDirectoryList *list)
{
g_return_val_if_fail (list != NULL, NULL);
g_return_val_if_fail (list->refcount > 0, NULL);
list->refcount += 1;
return list;
}
void
entry_directory_list_unref (EntryDirectoryList *list)
{
g_return_if_fail (list != NULL);
g_return_if_fail (list->refcount > 0);
list->refcount -= 1;
if (list->refcount == 0)
{
g_list_foreach (list->dirs, (GFunc) entry_directory_unref, NULL);
g_list_free (list->dirs);
list->dirs = NULL;
list->length = 0;
g_free (list);
}
}
void
entry_directory_list_prepend (EntryDirectoryList *list,
EntryDirectory *ed)
{
list->length += 1;
list->dirs = g_list_prepend (list->dirs,
entry_directory_ref (ed));
}
int
entry_directory_list_get_length (EntryDirectoryList *list)
{
return list->length;
}
void
entry_directory_list_append_list (EntryDirectoryList *list,
EntryDirectoryList *to_append)
{
GList *tmp;
GList *new_dirs = NULL;
if (to_append->length == 0)
return;
tmp = to_append->dirs;
while (tmp != NULL)
{
list->length += 1;
new_dirs = g_list_prepend (new_dirs,
entry_directory_ref (tmp->data));
tmp = tmp->next;
}
new_dirs = g_list_reverse (new_dirs);
list->dirs = g_list_concat (list->dirs, new_dirs);
}
DesktopEntry *
entry_directory_list_get_directory (EntryDirectoryList *list,
const char *relative_path)
{
DesktopEntry *retval = NULL;
GList *tmp;
tmp = list->dirs;
while (tmp != NULL)
{
if ((retval = entry_directory_get_directory (tmp->data, relative_path)) != NULL)
break;
tmp = tmp->next;
}
return retval;
}
gboolean
_entry_directory_list_compare (const EntryDirectoryList *a,
const EntryDirectoryList *b)
{
GList *al, *bl;
if (a == NULL && b == NULL)
return TRUE;
if ((a == NULL || b == NULL))
return FALSE;
if (a->length != b->length)
return FALSE;
al = a->dirs; bl = b->dirs;
while (al && bl && al->data == bl->data)
{
al = al->next;
bl = bl->next;
}
return (al == NULL && bl == NULL);
}
static gboolean
get_all_func (EntryDirectory *ed,
DesktopEntry *entry,
const char *file_id,
DesktopEntrySet *set,
gpointer user_data)
{
entry = desktop_entry_ref (entry);
desktop_entry_set_add_entry (set, entry, file_id);
desktop_entry_unref (entry);
return TRUE;
}
static DesktopEntrySet *entry_directory_last_set = NULL;
static EntryDirectoryList *entry_directory_last_list = NULL;
void
_entry_directory_list_empty_desktop_cache (void)
{
if (entry_directory_last_set != NULL)
desktop_entry_set_unref (entry_directory_last_set);
entry_directory_last_set = NULL;
if (entry_directory_last_list != NULL)
entry_directory_list_unref (entry_directory_last_list);
entry_directory_last_list = NULL;
}
DesktopEntrySet *
_entry_directory_list_get_all_desktops (EntryDirectoryList *list)
{
GList *tmp;
DesktopEntrySet *set;
/* The only tricky thing here is that desktop files later
* in the search list with the same relative path
* are "hidden" by desktop files earlier in the path,
* so we have to do the earlier files first causing
* the later files to replace the earlier files
* in the DesktopEntrySet
*
* We go from the end of the list so we can just
* g_hash_table_replace and not have to do two
* hash lookups (check for existing entry, then insert new
* entry)
*/
/* This method is -extremely- slow, so we have a simple
one-entry cache here */
if (_entry_directory_list_compare (list, entry_directory_last_list))
{
menu_verbose (" Hit desktop list (%p) cache\n", list);
return desktop_entry_set_ref (entry_directory_last_set);
}
if (entry_directory_last_set != NULL)
desktop_entry_set_unref (entry_directory_last_set);
if (entry_directory_last_list != NULL)
entry_directory_list_unref (entry_directory_last_list);
set = desktop_entry_set_new ();
menu_verbose (" Storing all of list %p in set %p\n",
list, set);
tmp = g_list_last (list->dirs);
while (tmp != NULL)
{
entry_directory_foreach (tmp->data, get_all_func, set, NULL);
tmp = tmp->prev;
}
entry_directory_last_list = entry_directory_list_ref (list);
entry_directory_last_set = desktop_entry_set_ref (set);
return set;
}
void
entry_directory_list_add_monitors (EntryDirectoryList *list,
EntryDirectoryChangedFunc callback,
gpointer user_data)
{
GList *tmp;
tmp = list->dirs;
while (tmp != NULL)
{
entry_directory_add_monitor (tmp->data, callback, user_data);
tmp = tmp->next;
}
}
void
entry_directory_list_remove_monitors (EntryDirectoryList *list,
EntryDirectoryChangedFunc callback,
gpointer user_data)
{
GList *tmp;
tmp = list->dirs;
while (tmp != NULL)
{
entry_directory_remove_monitor (tmp->data, callback, user_data);
tmp = tmp->next;
}
}
cinnamon-menus-6.0.0/libmenu/meson.build 0000664 0001750 0001750 00000003311 14526401433 017165 0 ustar fabio fabio public_headers = [
'gmenu-tree.h',
'gmenu-desktopappinfo.h'
]
public_sources = [
'gmenu-tree.c',
'gmenu-desktopappinfo.c',
public_headers,
]
libmenu_private_headers = [
'desktop-entries.h',
'entry-directories.h',
'menu-layout.h',
'menu-monitor.h',
'menu-util.h',
]
libmenu_sources = [
'desktop-entries.c',
'entry-directories.c',
'menu-layout.c',
'menu-monitor.c',
'menu-util.c',
public_sources,
libmenu_private_headers,
]
libmenu_deps = [
gio,
config_h,
]
libcinnamon_menus = library(
'cinnamon-menu-3',
libmenu_sources,
soversion: binary_major_version,
version: binary_version,
include_directories: include_root,
dependencies: libmenu_deps,
install: true,
build_by_default: false,
)
cmenu_dep = declare_dependency(
include_directories: include_directories('.'),
link_with: libcinnamon_menus,
dependencies: libmenu_deps,
link_args: ['-Wl,-Bsymbolic', '-Wl,-z,relro', '-Wl,-z,now'],
)
install_headers(
public_headers,
subdir: 'cinnamon-menus-3.0'
)
pkgconfig = import('pkgconfig')
# meson 0.46.0 can drop the version keyword and move libraries to a
# positional argument
pkgconfig.generate(
name: 'libcinnamon-menu-3.0',
description: 'Desktop Menu Specification Implementation',
version: version,
libraries: libcinnamon_menus,
subdirs: 'cinnamon-menus-3.0'
)
gnome.generate_gir(
libcinnamon_menus,
namespace: 'CMenu',
nsversion: '3.0',
sources: public_sources,
identifier_prefix: 'GMenu',
symbol_prefix: 'gmenu',
includes: 'Gio-2.0',
install: true,
install_dir_gir: join_paths(datadir, 'gir-1.0'),
export_packages: 'libcinnamon-menu-3.0',
)
cinnamon-menus-6.0.0/libmenu/gmenu-desktopappinfo.h 0000664 0001750 0001750 00000011173 14526401433 021340 0 ustar fabio fabio /*
* gmenu-desktopappinfo.h
* Copyright (C) 2020 Lars Mueller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110, USA.
*/
#ifndef _GMENU_DESKTOPAPPINFO_H_
#define _GMENU_DESKTOPAPPINFO_H_
#ifndef GMENU_I_KNOW_THIS_IS_UNSTABLE
#error "libgnome-menu should only be used if you understand that it's subject to frequent change, and is not supported as a fixed API/ABI or as part of the platform"
#endif
#include
G_BEGIN_DECLS
#define GMENU_TYPE_DESKTOPAPPINFO (gmenu_desktopappinfo_get_type ())
#define GMENU_DESKTOPAPPINFO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMENU_TYPE_DESKTOPAPPINFO, GMenuDesktopAppInfo))
#define GMENU_DESKTOPAPPINFO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMENU_TYPE_DESKTOPAPPINFO, GMenuDesktopAppInfoClass))
#define GMENU_IS_DESKTOPAPPINFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GMENU_TYPE_DESKTOPAPPINFO))
#define GMENU_IS_DESKTOPAPPINFO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GMENU_TYPE_DESKTOPAPPINFO))
#define GMENU_DESKTOPAPPINFO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GMENU_TYPE_DESKTOPAPPINFO, GMenuDesktopAppInfoClass))
typedef struct _GMenuDesktopAppInfoClass GMenuDesktopAppInfoClass;
typedef struct _GMenuDesktopAppInfo GMenuDesktopAppInfo;
struct _GMenuDesktopAppInfoClass
{
GObjectClass parent_class;
};
#define GMENU_DESKTOPAPPINFO_FLATPAK_SUFFIX ":flatpak"
GType gmenu_desktopappinfo_get_type (void) G_GNUC_CONST;
GMenuDesktopAppInfo* gmenu_desktopappinfo_new_from_filename (gchar *filename);
GMenuDesktopAppInfo *gmenu_desktopappinfo_new_from_keyfile (GKeyFile *key_file);
const char * gmenu_desktopappinfo_get_filename (GMenuDesktopAppInfo *appinfo);
const char * gmenu_desktopappinfo_get_generic_name (GMenuDesktopAppInfo *appinfo);
const char * gmenu_desktopappinfo_get_categories (GMenuDesktopAppInfo *appinfo);
const char * const *gmenu_desktopappinfo_get_keywords (GMenuDesktopAppInfo *appinfo);
gboolean gmenu_desktopappinfo_get_nodisplay (GMenuDesktopAppInfo *appinfo);
gboolean gmenu_desktopappinfo_get_show_in (GMenuDesktopAppInfo *appinfo, const gchar *desktop_env);
const char * gmenu_desktopappinfo_get_startup_wm_class (GMenuDesktopAppInfo *appinfo);
GMenuDesktopAppInfo *gmenu_desktopappinfo_new (const char *desktop_id);
gboolean gmenu_desktopappinfo_get_is_hidden (GMenuDesktopAppInfo *appinfo);
gboolean gmenu_desktopappinfo_has_key (GMenuDesktopAppInfo *appinfo, const char *key);
char * gmenu_desktopappinfo_get_string (GMenuDesktopAppInfo *appinfo, const char *key);
char * gmenu_desktopappinfo_get_locale_string (GMenuDesktopAppInfo *appinfo, const char *key);
gboolean gmenu_desktopappinfo_get_boolean (GMenuDesktopAppInfo *appinfo, const char *key);
const gchar * const * gmenu_desktopappinfo_list_actions (GMenuDesktopAppInfo *appinfo);
void gmenu_desktopappinfo_launch_action (GMenuDesktopAppInfo *appinfo, const gchar *action_name, GAppLaunchContext *launch_context);
gchar * gmenu_desktopappinfo_get_action_name (GMenuDesktopAppInfo *appinfo, const gchar *action_name);
gboolean gmenu_desktopappinfo_launch_uris_as_manager (GMenuDesktopAppInfo *appinfo,
GList *uris,
GAppLaunchContext *launch_context,
GSpawnFlags spawn_flags,
GSpawnChildSetupFunc user_setup,
gpointer user_setup_data,
GDesktopAppLaunchCallback pid_callback,
gpointer pid_callback_data,
GError **error);
gboolean gmenu_desktopappinfo_get_is_flatpak (GMenuDesktopAppInfo *appinfo);
const char * gmenu_desktopappinfo_get_flatpak_app_id (GMenuDesktopAppInfo *appinfo);
G_END_DECLS
#endif /* _GMENU_DESKTOPAPPINFO_H_ */
cinnamon-menus-6.0.0/libmenu/menu-layout.c 0000664 0001750 0001750 00000172055 14526401433 017462 0 ustar fabio fabio /* Menu layout in-memory data structure (a custom "DOM tree") */
/*
* Copyright (C) 2002 - 2004 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "menu-layout.h"
#include
#include
#include
#include
#include
#include "entry-directories.h"
#include "menu-util.h"
typedef struct MenuLayoutNodeMenu MenuLayoutNodeMenu;
typedef struct MenuLayoutNodeRoot MenuLayoutNodeRoot;
typedef struct MenuLayoutNodeLegacyDir MenuLayoutNodeLegacyDir;
typedef struct MenuLayoutNodeMergeFile MenuLayoutNodeMergeFile;
typedef struct MenuLayoutNodeDefaultLayout MenuLayoutNodeDefaultLayout;
typedef struct MenuLayoutNodeMenuname MenuLayoutNodeMenuname;
typedef struct MenuLayoutNodeMerge MenuLayoutNodeMerge;
struct MenuLayoutNode
{
/* Node lists are circular, for length-one lists
* prev/next point back to the node itself.
*/
MenuLayoutNode *prev;
MenuLayoutNode *next;
MenuLayoutNode *parent;
MenuLayoutNode *children;
char *content;
guint refcount : 20;
guint type : 7;
};
struct MenuLayoutNodeRoot
{
MenuLayoutNode node;
char *basedir;
char *name;
GMainContext *main_context;
GSList *monitors;
GSource *monitors_idle_handler;
};
struct MenuLayoutNodeMenu
{
MenuLayoutNode node;
MenuLayoutNode *name_node; /* cache of the node */
EntryDirectoryList *app_dirs;
EntryDirectoryList *dir_dirs;
};
struct MenuLayoutNodeLegacyDir
{
MenuLayoutNode node;
char *prefix;
};
struct MenuLayoutNodeMergeFile
{
MenuLayoutNode node;
MenuMergeFileType type;
};
struct MenuLayoutNodeDefaultLayout
{
MenuLayoutNode node;
MenuLayoutValues layout_values;
};
struct MenuLayoutNodeMenuname
{
MenuLayoutNode node;
MenuLayoutValues layout_values;
};
struct MenuLayoutNodeMerge
{
MenuLayoutNode node;
MenuLayoutMergeType merge_type;
};
typedef struct
{
MenuLayoutNodeEntriesChangedFunc callback;
gpointer user_data;
} MenuLayoutNodeEntriesMonitor;
static inline MenuLayoutNode *
node_next (MenuLayoutNode *node)
{
/* root nodes (no parent) never have siblings */
if (node->parent == NULL)
return NULL;
/* circular list */
if (node->next == node->parent->children)
return NULL;
return node->next;
}
static gboolean
menu_layout_invoke_monitors (MenuLayoutNodeRoot *nr)
{
GSList *tmp;
g_assert (nr->node.type == MENU_LAYOUT_NODE_ROOT);
nr->monitors_idle_handler = NULL;
tmp = nr->monitors;
while (tmp != NULL)
{
MenuLayoutNodeEntriesMonitor *monitor = tmp->data;
GSList *next = tmp->next;
monitor->callback ((MenuLayoutNode *) nr, monitor->user_data);
tmp = next;
}
return FALSE;
}
static void
handle_entry_directory_changed (EntryDirectory *dir,
MenuLayoutNode *node)
{
MenuLayoutNodeRoot *nr;
g_assert (node->type == MENU_LAYOUT_NODE_MENU);
nr = (MenuLayoutNodeRoot *) menu_layout_node_get_root (node);
if (nr->monitors_idle_handler == NULL)
{
nr->monitors_idle_handler = g_idle_source_new ();
g_source_set_callback (nr->monitors_idle_handler,
(GSourceFunc) menu_layout_invoke_monitors, nr, NULL);
g_source_attach (nr->monitors_idle_handler, nr->main_context);
g_source_unref (nr->monitors_idle_handler);
}
}
static void
remove_entry_directory_list (MenuLayoutNodeMenu *nm,
EntryDirectoryList **dirs)
{
if (*dirs)
{
entry_directory_list_remove_monitors (*dirs,
(EntryDirectoryChangedFunc) handle_entry_directory_changed,
nm);
entry_directory_list_unref (*dirs);
*dirs = NULL;
}
}
MenuLayoutNode *
menu_layout_node_ref (MenuLayoutNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
node->refcount += 1;
return node;
}
void
menu_layout_node_unref (MenuLayoutNode *node)
{
g_return_if_fail (node != NULL);
g_return_if_fail (node->refcount > 0);
node->refcount -= 1;
if (node->refcount == 0)
{
MenuLayoutNode *iter;
iter = node->children;
while (iter != NULL)
{
MenuLayoutNode *next = node_next (iter);
menu_layout_node_unref (iter);
iter = next;
}
if (node->type == MENU_LAYOUT_NODE_MENU)
{
MenuLayoutNodeMenu *nm = (MenuLayoutNodeMenu *) node;
if (nm->name_node)
menu_layout_node_unref (nm->name_node);
remove_entry_directory_list (nm, &nm->app_dirs);
remove_entry_directory_list (nm, &nm->dir_dirs);
}
else if (node->type == MENU_LAYOUT_NODE_LEGACY_DIR)
{
MenuLayoutNodeLegacyDir *legacy = (MenuLayoutNodeLegacyDir *) node;
g_free (legacy->prefix);
}
else if (node->type == MENU_LAYOUT_NODE_ROOT)
{
MenuLayoutNodeRoot *nr = (MenuLayoutNodeRoot*) node;
g_slist_foreach (nr->monitors, (GFunc) g_free, NULL);
g_slist_free (nr->monitors);
if (nr->monitors_idle_handler != NULL)
g_source_destroy (nr->monitors_idle_handler);
nr->monitors_idle_handler = NULL;
if (nr->main_context != NULL)
g_main_context_unref (nr->main_context);
nr->main_context = NULL;
g_free (nr->basedir);
g_free (nr->name);
}
g_free (node->content);
g_free (node);
}
}
MenuLayoutNode *
menu_layout_node_new (MenuLayoutNodeType type)
{
MenuLayoutNode *node;
switch (type)
{
case MENU_LAYOUT_NODE_MENU:
node = (MenuLayoutNode *) g_new0 (MenuLayoutNodeMenu, 1);
break;
case MENU_LAYOUT_NODE_LEGACY_DIR:
node = (MenuLayoutNode *) g_new0 (MenuLayoutNodeLegacyDir, 1);
break;
case MENU_LAYOUT_NODE_ROOT:
node = (MenuLayoutNode*) g_new0 (MenuLayoutNodeRoot, 1);
break;
case MENU_LAYOUT_NODE_MERGE_FILE:
node = (MenuLayoutNode *) g_new0 (MenuLayoutNodeMergeFile, 1);
break;
case MENU_LAYOUT_NODE_DEFAULT_LAYOUT:
node = (MenuLayoutNode *) g_new0 (MenuLayoutNodeDefaultLayout, 1);
break;
case MENU_LAYOUT_NODE_MENUNAME:
node = (MenuLayoutNode *) g_new0 (MenuLayoutNodeMenuname, 1);
break;
case MENU_LAYOUT_NODE_MERGE:
node = (MenuLayoutNode *) g_new0 (MenuLayoutNodeMerge, 1);
break;
default:
node = g_new0 (MenuLayoutNode, 1);
break;
}
node->type = type;
node->refcount = 1;
/* we're in a list of one node */
node->next = node;
node->prev = node;
return node;
}
MenuLayoutNode *
menu_layout_node_get_next (MenuLayoutNode *node)
{
return node_next (node);
}
MenuLayoutNode *
menu_layout_node_get_parent (MenuLayoutNode *node)
{
return node->parent;
}
MenuLayoutNode *
menu_layout_node_get_children (MenuLayoutNode *node)
{
return node->children;
}
MenuLayoutNode *
menu_layout_node_get_root (MenuLayoutNode *node)
{
MenuLayoutNode *parent;
parent = node;
while (parent->parent != NULL)
parent = parent->parent;
g_assert (parent->type == MENU_LAYOUT_NODE_ROOT);
return parent;
}
char *
menu_layout_node_get_content_as_path (MenuLayoutNode *node)
{
if (node->content == NULL)
{
menu_verbose (" (node has no content to get as a path)\n");
return NULL;
}
if (g_path_is_absolute (node->content))
{
return g_strdup (node->content);
}
else
{
MenuLayoutNodeRoot *root;
root = (MenuLayoutNodeRoot *) menu_layout_node_get_root (node);
if (root->basedir == NULL)
{
menu_verbose ("No basedir available, using \"%s\" as-is\n",
node->content);
return g_strdup (node->content);
}
else
{
menu_verbose ("Using basedir \"%s\" filename \"%s\"\n",
root->basedir, node->content);
return g_build_filename (root->basedir, node->content, NULL);
}
}
}
#define RETURN_IF_NO_PARENT(node) G_STMT_START { \
if ((node)->parent == NULL) \
{ \
g_warning ("To add siblings to a menu node, " \
"it must not be the root node, " \
"and must be linked in below some root node\n" \
"node parent = %p and type = %d", \
(node)->parent, (node)->type); \
return; \
} \
} G_STMT_END
#define RETURN_IF_HAS_ENTRY_DIRS(node) G_STMT_START { \
if ((node)->type == MENU_LAYOUT_NODE_MENU && \
(((MenuLayoutNodeMenu*)(node))->app_dirs != NULL || \
((MenuLayoutNodeMenu*)(node))->dir_dirs != NULL)) \
{ \
g_warning ("node acquired ->app_dirs or ->dir_dirs " \
"while not rooted in a tree\n"); \
return; \
} \
} G_STMT_END \
void
menu_layout_node_insert_before (MenuLayoutNode *node,
MenuLayoutNode *new_sibling)
{
g_return_if_fail (new_sibling != NULL);
g_return_if_fail (new_sibling->parent == NULL);
RETURN_IF_NO_PARENT (node);
RETURN_IF_HAS_ENTRY_DIRS (new_sibling);
new_sibling->next = node;
new_sibling->prev = node->prev;
node->prev = new_sibling;
new_sibling->prev->next = new_sibling;
new_sibling->parent = node->parent;
if (node == node->parent->children)
node->parent->children = new_sibling;
menu_layout_node_ref (new_sibling);
}
void
menu_layout_node_insert_after (MenuLayoutNode *node,
MenuLayoutNode *new_sibling)
{
g_return_if_fail (new_sibling != NULL);
g_return_if_fail (new_sibling->parent == NULL);
RETURN_IF_NO_PARENT (node);
RETURN_IF_HAS_ENTRY_DIRS (new_sibling);
new_sibling->prev = node;
new_sibling->next = node->next;
node->next = new_sibling;
new_sibling->next->prev = new_sibling;
new_sibling->parent = node->parent;
menu_layout_node_ref (new_sibling);
}
void
menu_layout_node_prepend_child (MenuLayoutNode *parent,
MenuLayoutNode *new_child)
{
RETURN_IF_HAS_ENTRY_DIRS (new_child);
if (parent->children)
{
menu_layout_node_insert_before (parent->children, new_child);
}
else
{
parent->children = menu_layout_node_ref (new_child);
new_child->parent = parent;
}
}
void
menu_layout_node_append_child (MenuLayoutNode *parent,
MenuLayoutNode *new_child)
{
RETURN_IF_HAS_ENTRY_DIRS (new_child);
if (parent->children)
{
menu_layout_node_insert_after (parent->children->prev, new_child);
}
else
{
parent->children = menu_layout_node_ref (new_child);
new_child->parent = parent;
}
}
void
menu_layout_node_unlink (MenuLayoutNode *node)
{
g_return_if_fail (node != NULL);
g_return_if_fail (node->parent != NULL);
menu_layout_node_steal (node);
menu_layout_node_unref (node);
}
static void
recursive_clean_entry_directory_lists (MenuLayoutNode *node,
gboolean apps)
{
EntryDirectoryList **dirs;
MenuLayoutNodeMenu *nm;
MenuLayoutNode *iter;
if (node->type != MENU_LAYOUT_NODE_MENU)
return;
nm = (MenuLayoutNodeMenu *) node;
dirs = apps ? &nm->app_dirs : &nm->dir_dirs;
if (*dirs == NULL || entry_directory_list_get_length (*dirs) == 0)
return; /* child menus continue to have valid lists */
remove_entry_directory_list (nm, dirs);
iter = node->children;
while (iter != NULL)
{
if (iter->type == MENU_LAYOUT_NODE_MENU)
recursive_clean_entry_directory_lists (iter, apps);
iter = node_next (iter);
}
}
void
menu_layout_node_steal (MenuLayoutNode *node)
{
g_return_if_fail (node != NULL);
g_return_if_fail (node->parent != NULL);
switch (node->type)
{
case MENU_LAYOUT_NODE_NAME:
{
MenuLayoutNodeMenu *nm = (MenuLayoutNodeMenu *) node->parent;
if (nm->name_node == node)
{
menu_layout_node_unref (nm->name_node);
nm->name_node = NULL;
}
}
break;
case MENU_LAYOUT_NODE_APP_DIR:
recursive_clean_entry_directory_lists (node->parent, TRUE);
break;
case MENU_LAYOUT_NODE_DIRECTORY_DIR:
recursive_clean_entry_directory_lists (node->parent, FALSE);
break;
default:
break;
}
if (node->parent && node->parent->children == node)
{
if (node->next != node)
node->parent->children = node->next;
else
node->parent->children = NULL;
}
/* these are no-ops for length-one node lists */
node->prev->next = node->next;
node->next->prev = node->prev;
node->parent = NULL;
/* point to ourselves, now we're length one */
node->next = node;
node->prev = node;
}
MenuLayoutNodeType
menu_layout_node_get_type (MenuLayoutNode *node)
{
return node->type;
}
const char *
menu_layout_node_get_content (MenuLayoutNode *node)
{
return node->content;
}
void
menu_layout_node_set_content (MenuLayoutNode *node,
const char *content)
{
if (node->content == content)
return;
g_free (node->content);
node->content = g_strdup (content);
}
const char *
menu_layout_node_root_get_name (MenuLayoutNode *node)
{
MenuLayoutNodeRoot *nr;
g_return_val_if_fail (node->type == MENU_LAYOUT_NODE_ROOT, NULL);
nr = (MenuLayoutNodeRoot*) node;
return nr->name;
}
const char *
menu_layout_node_root_get_basedir (MenuLayoutNode *node)
{
MenuLayoutNodeRoot *nr;
g_return_val_if_fail (node->type == MENU_LAYOUT_NODE_ROOT, NULL);
nr = (MenuLayoutNodeRoot*) node;
return nr->basedir;
}
const char *
menu_layout_node_menu_get_name (MenuLayoutNode *node)
{
MenuLayoutNodeMenu *nm;
g_return_val_if_fail (node->type == MENU_LAYOUT_NODE_MENU, NULL);
nm = (MenuLayoutNodeMenu*) node;
if (nm->name_node == NULL)
{
MenuLayoutNode *iter;
iter = node->children;
while (iter != NULL)
{
if (iter->type == MENU_LAYOUT_NODE_NAME)
{
nm->name_node = menu_layout_node_ref (iter);
break;
}
iter = node_next (iter);
}
}
if (nm->name_node == NULL)
return NULL;
return menu_layout_node_get_content (nm->name_node);
}
static void
ensure_dir_lists (MenuLayoutNodeMenu *nm)
{
MenuLayoutNode *node;
MenuLayoutNode *iter;
EntryDirectoryList *app_dirs;
EntryDirectoryList *dir_dirs;
node = (MenuLayoutNode *) nm;
if (nm->app_dirs && nm->dir_dirs)
return;
app_dirs = NULL;
dir_dirs = NULL;
if (nm->app_dirs == NULL)
{
app_dirs = entry_directory_list_new ();
if (node->parent && node->parent->type == MENU_LAYOUT_NODE_MENU)
{
EntryDirectoryList *dirs;
if ((dirs = menu_layout_node_menu_get_app_dirs (node->parent)))
entry_directory_list_append_list (app_dirs, dirs);
}
}
if (nm->dir_dirs == NULL)
{
dir_dirs = entry_directory_list_new ();
if (node->parent && node->parent->type == MENU_LAYOUT_NODE_MENU)
{
EntryDirectoryList *dirs;
if ((dirs = menu_layout_node_menu_get_directory_dirs (node->parent)))
entry_directory_list_append_list (dir_dirs, dirs);
}
}
iter = node->children;
while (iter != NULL)
{
EntryDirectory *ed;
if (app_dirs != NULL && iter->type == MENU_LAYOUT_NODE_APP_DIR)
{
char *path;
path = menu_layout_node_get_content_as_path (iter);
ed = entry_directory_new (DESKTOP_ENTRY_DESKTOP, path);
if (ed != NULL)
{
entry_directory_list_prepend (app_dirs, ed);
entry_directory_unref (ed);
}
g_free (path);
}
if (dir_dirs != NULL && iter->type == MENU_LAYOUT_NODE_DIRECTORY_DIR)
{
char *path;
path = menu_layout_node_get_content_as_path (iter);
ed = entry_directory_new (DESKTOP_ENTRY_DIRECTORY, path);
if (ed != NULL)
{
entry_directory_list_prepend (dir_dirs, ed);
entry_directory_unref (ed);
}
g_free (path);
}
iter = node_next (iter);
}
if (app_dirs)
{
g_assert (nm->app_dirs == NULL);
nm->app_dirs = app_dirs;
entry_directory_list_add_monitors (nm->app_dirs,
(EntryDirectoryChangedFunc) handle_entry_directory_changed,
nm);
}
if (dir_dirs)
{
g_assert (nm->dir_dirs == NULL);
nm->dir_dirs = dir_dirs;
entry_directory_list_add_monitors (nm->dir_dirs,
(EntryDirectoryChangedFunc) handle_entry_directory_changed,
nm);
}
}
EntryDirectoryList *
menu_layout_node_menu_get_app_dirs (MenuLayoutNode *node)
{
MenuLayoutNodeMenu *nm;
g_return_val_if_fail (node->type == MENU_LAYOUT_NODE_MENU, NULL);
nm = (MenuLayoutNodeMenu *) node;
ensure_dir_lists (nm);
return nm->app_dirs;
}
EntryDirectoryList *
menu_layout_node_menu_get_directory_dirs (MenuLayoutNode *node)
{
MenuLayoutNodeMenu *nm;
g_return_val_if_fail (node->type == MENU_LAYOUT_NODE_MENU, NULL);
nm = (MenuLayoutNodeMenu *) node;
ensure_dir_lists (nm);
return nm->dir_dirs;
}
const char *
menu_layout_node_move_get_old (MenuLayoutNode *node)
{
MenuLayoutNode *iter;
iter = node->children;
while (iter != NULL)
{
if (iter->type == MENU_LAYOUT_NODE_OLD)
return iter->content;
iter = node_next (iter);
}
return NULL;
}
const char *
menu_layout_node_move_get_new (MenuLayoutNode *node)
{
MenuLayoutNode *iter;
iter = node->children;
while (iter != NULL)
{
if (iter->type == MENU_LAYOUT_NODE_NEW)
return iter->content;
iter = node_next (iter);
}
return NULL;
}
const char *
menu_layout_node_legacy_dir_get_prefix (MenuLayoutNode *node)
{
MenuLayoutNodeLegacyDir *legacy;
g_return_val_if_fail (node->type == MENU_LAYOUT_NODE_LEGACY_DIR, NULL);
legacy = (MenuLayoutNodeLegacyDir *) node;
return legacy->prefix;
}
void
menu_layout_node_legacy_dir_set_prefix (MenuLayoutNode *node,
const char *prefix)
{
MenuLayoutNodeLegacyDir *legacy;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_LEGACY_DIR);
legacy = (MenuLayoutNodeLegacyDir *) node;
g_free (legacy->prefix);
legacy->prefix = g_strdup (prefix);
}
MenuMergeFileType
menu_layout_node_merge_file_get_type (MenuLayoutNode *node)
{
MenuLayoutNodeMergeFile *merge_file;
g_return_val_if_fail (node->type == MENU_LAYOUT_NODE_MERGE_FILE, FALSE);
merge_file = (MenuLayoutNodeMergeFile *) node;
return merge_file->type;
}
void
menu_layout_node_merge_file_set_type (MenuLayoutNode *node,
MenuMergeFileType type)
{
MenuLayoutNodeMergeFile *merge_file;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_MERGE_FILE);
merge_file = (MenuLayoutNodeMergeFile *) node;
merge_file->type = type;
}
MenuLayoutMergeType
menu_layout_node_merge_get_type (MenuLayoutNode *node)
{
MenuLayoutNodeMerge *merge;
g_return_val_if_fail (node->type == MENU_LAYOUT_NODE_MERGE, 0);
merge = (MenuLayoutNodeMerge *) node;
return merge->merge_type;
}
static void
menu_layout_node_merge_set_type (MenuLayoutNode *node,
const char *merge_type)
{
MenuLayoutNodeMerge *merge;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_MERGE);
merge = (MenuLayoutNodeMerge *) node;
merge->merge_type = MENU_LAYOUT_MERGE_NONE;
if (strcmp (merge_type, "menus") == 0)
{
merge->merge_type = MENU_LAYOUT_MERGE_MENUS;
}
else if (strcmp (merge_type, "files") == 0)
{
merge->merge_type = MENU_LAYOUT_MERGE_FILES;
}
else if (strcmp (merge_type, "all") == 0)
{
merge->merge_type = MENU_LAYOUT_MERGE_ALL;
}
}
void
menu_layout_node_default_layout_get_values (MenuLayoutNode *node,
MenuLayoutValues *values)
{
MenuLayoutNodeDefaultLayout *default_layout;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_DEFAULT_LAYOUT);
g_return_if_fail (values != NULL);
default_layout = (MenuLayoutNodeDefaultLayout *) node;
*values = default_layout->layout_values;
}
void
menu_layout_node_menuname_get_values (MenuLayoutNode *node,
MenuLayoutValues *values)
{
MenuLayoutNodeMenuname *menuname;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_MENUNAME);
g_return_if_fail (values != NULL);
menuname = (MenuLayoutNodeMenuname *) node;
*values = menuname->layout_values;
}
static void
menu_layout_values_set (MenuLayoutValues *values,
const char *show_empty,
const char *inline_menus,
const char *inline_limit,
const char *inline_header,
const char *inline_alias)
{
values->mask = MENU_LAYOUT_VALUES_NONE;
values->show_empty = FALSE;
values->inline_menus = FALSE;
values->inline_limit = 4;
values->inline_header = FALSE;
values->inline_alias = FALSE;
if (show_empty != NULL)
{
values->show_empty = strcmp (show_empty, "true") == 0;
values->mask |= MENU_LAYOUT_VALUES_SHOW_EMPTY;
}
if (inline_menus != NULL)
{
values->inline_menus = strcmp (inline_menus, "true") == 0;
values->mask |= MENU_LAYOUT_VALUES_INLINE_MENUS;
}
if (inline_limit != NULL)
{
char *end;
int limit;
limit = strtol (inline_limit, &end, 10);
if (*end == '\0')
{
values->inline_limit = limit;
values->mask |= MENU_LAYOUT_VALUES_INLINE_LIMIT;
}
}
if (inline_header != NULL)
{
values->inline_header = strcmp (inline_header, "true") == 0;
values->mask |= MENU_LAYOUT_VALUES_INLINE_HEADER;
}
if (inline_alias != NULL)
{
values->inline_alias = strcmp (inline_alias, "true") == 0;
values->mask |= MENU_LAYOUT_VALUES_INLINE_ALIAS;
}
}
static void
menu_layout_node_default_layout_set_values (MenuLayoutNode *node,
const char *show_empty,
const char *inline_menus,
const char *inline_limit,
const char *inline_header,
const char *inline_alias)
{
MenuLayoutNodeDefaultLayout *default_layout;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_DEFAULT_LAYOUT);
default_layout = (MenuLayoutNodeDefaultLayout *) node;
menu_layout_values_set (&default_layout->layout_values,
show_empty,
inline_menus,
inline_limit,
inline_header,
inline_alias);
}
static void
menu_layout_node_menuname_set_values (MenuLayoutNode *node,
const char *show_empty,
const char *inline_menus,
const char *inline_limit,
const char *inline_header,
const char *inline_alias)
{
MenuLayoutNodeMenuname *menuname;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_MENUNAME);
menuname = (MenuLayoutNodeMenuname *) node;
menu_layout_values_set (&menuname->layout_values,
show_empty,
inline_menus,
inline_limit,
inline_header,
inline_alias);
}
void
menu_layout_node_root_add_entries_monitor (MenuLayoutNode *node,
MenuLayoutNodeEntriesChangedFunc callback,
gpointer user_data)
{
MenuLayoutNodeEntriesMonitor *monitor;
MenuLayoutNodeRoot *nr;
GSList *tmp;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_ROOT);
nr = (MenuLayoutNodeRoot *) node;
tmp = nr->monitors;
while (tmp != NULL)
{
monitor = tmp->data;
if (monitor->callback == callback &&
monitor->user_data == user_data)
break;
tmp = tmp->next;
}
if (tmp == NULL)
{
monitor = g_new0 (MenuLayoutNodeEntriesMonitor, 1);
monitor->callback = callback;
monitor->user_data = user_data;
nr->monitors = g_slist_append (nr->monitors, monitor);
}
}
void
menu_layout_node_root_remove_entries_monitor (MenuLayoutNode *node,
MenuLayoutNodeEntriesChangedFunc callback,
gpointer user_data)
{
MenuLayoutNodeRoot *nr;
GSList *tmp;
g_return_if_fail (node->type == MENU_LAYOUT_NODE_ROOT);
nr = (MenuLayoutNodeRoot *) node;
tmp = nr->monitors;
while (tmp != NULL)
{
MenuLayoutNodeEntriesMonitor *monitor = tmp->data;
GSList *next = tmp->next;
if (monitor->callback == callback &&
monitor->user_data == user_data)
{
nr->monitors = g_slist_delete_link (nr->monitors, tmp);
g_free (monitor);
}
tmp = next;
}
}
/*
* Menu file parsing
*/
typedef struct
{
MenuLayoutNode *root;
MenuLayoutNode *stack_top;
} MenuParser;
static void set_error (GError **err,
GMarkupParseContext *context,
int error_domain,
int error_code,
const char *format,
...) G_GNUC_PRINTF (5, 6);
static void add_context_to_error (GError **err,
GMarkupParseContext *context);
static void start_element_handler (GMarkupParseContext *context,
const char *element_name,
const char **attribute_names,
const char **attribute_values,
gpointer user_data,
GError **error);
static void end_element_handler (GMarkupParseContext *context,
const char *element_name,
gpointer user_data,
GError **error);
static void text_handler (GMarkupParseContext *context,
const char *text,
gsize text_len,
gpointer user_data,
GError **error);
static void passthrough_handler (GMarkupParseContext *context,
const char *passthrough_text,
gsize text_len,
gpointer user_data,
GError **error);
static GMarkupParser menu_funcs = {
start_element_handler,
end_element_handler,
text_handler,
passthrough_handler,
NULL
};
static void
set_error (GError **err,
GMarkupParseContext *context,
int error_domain,
int error_code,
const char *format,
...)
{
int line, ch;
va_list args;
char *str;
g_markup_parse_context_get_position (context, &line, &ch);
va_start (args, format);
str = g_strdup_vprintf (format, args);
va_end (args);
g_set_error (err, error_domain, error_code,
"Line %d character %d: %s",
line, ch, str);
g_free (str);
}
static void
add_context_to_error (GError **err,
GMarkupParseContext *context)
{
int line, ch;
char *str;
if (err == NULL || *err == NULL)
return;
g_markup_parse_context_get_position (context, &line, &ch);
str = g_strdup_printf ("Line %d character %d: %s",
line, ch, (*err)->message);
g_free ((*err)->message);
(*err)->message = str;
}
#define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
typedef struct
{
const char *name;
const char **retloc;
} LocateAttr;
static gboolean
locate_attributes (GMarkupParseContext *context,
const char *element_name,
const char **attribute_names,
const char **attribute_values,
GError **error,
const char *first_attribute_name,
const char **first_attribute_retloc,
...)
{
#define MAX_ATTRS 24
LocateAttr attrs[MAX_ATTRS];
int n_attrs;
va_list args;
const char *name;
const char **retloc;
gboolean retval;
int i;
g_return_val_if_fail (first_attribute_name != NULL, FALSE);
g_return_val_if_fail (first_attribute_retloc != NULL, FALSE);
retval = TRUE;
n_attrs = 1;
attrs[0].name = first_attribute_name;
attrs[0].retloc = first_attribute_retloc;
*first_attribute_retloc = NULL;
va_start (args, first_attribute_retloc);
name = va_arg (args, const char *);
retloc = va_arg (args, const char **);
while (name != NULL)
{
g_return_val_if_fail (retloc != NULL, FALSE);
g_assert (n_attrs < MAX_ATTRS);
attrs[n_attrs].name = name;
attrs[n_attrs].retloc = retloc;
n_attrs += 1;
*retloc = NULL;
name = va_arg (args, const char *);
retloc = va_arg (args, const char **);
}
va_end (args);
i = 0;
while (attribute_names[i])
{
int j;
j = 0;
while (j < n_attrs)
{
if (strcmp (attrs[j].name, attribute_names[i]) == 0)
{
retloc = attrs[j].retloc;
if (*retloc != NULL)
{
set_error (error, context,
G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
"Attribute \"%s\" repeated twice on the same <%s> element",
attrs[j].name, element_name);
retval = FALSE;
goto out;
}
*retloc = attribute_values[i];
break;
}
++j;
}
if (j == n_attrs)
{
set_error (error, context,
G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
"Attribute \"%s\" is invalid on <%s> element in this context",
attribute_names[i], element_name);
retval = FALSE;
goto out;
}
++i;
}
out:
return retval;
#undef MAX_ATTRS
}
static gboolean
check_no_attributes (GMarkupParseContext *context,
const char *element_name,
const char **attribute_names,
const char **attribute_values,
GError **error)
{
if (attribute_names[0] != NULL)
{
set_error (error, context,
G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
"Attribute \"%s\" is invalid on <%s> element in this context",
attribute_names[0], element_name);
return FALSE;
}
return TRUE;
}
static int
has_child_of_type (MenuLayoutNode *node,
MenuLayoutNodeType type)
{
MenuLayoutNode *iter;
iter = node->children;
while (iter)
{
if (iter->type == type)
return TRUE;
iter = node_next (iter);
}
return FALSE;
}
static void
push_node (MenuParser *parser,
MenuLayoutNodeType type)
{
MenuLayoutNode *node;
node = menu_layout_node_new (type);
menu_layout_node_append_child (parser->stack_top, node);
menu_layout_node_unref (node);
parser->stack_top = node;
}
static void
start_menu_element (MenuParser *parser,
GMarkupParseContext *context,
const char *element_name,
const char **attribute_names,
const char **attribute_values,
GError **error)
{
if (!check_no_attributes (context, element_name,
attribute_names, attribute_values,
error))
return;
if (!(parser->stack_top->type == MENU_LAYOUT_NODE_ROOT ||
parser->stack_top->type == MENU_LAYOUT_NODE_MENU))
{
set_error (error, context,
G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
"