~menulibre-dev/menulibre/import

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#!/usr/bin/python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#   MenuLibre - Advanced fd.o Compliant Menu Editor
#   Copyright (C) 2012-2019 Sean Davis <smd.seandavis@gmail.com>
#   Copyright (C) 2017-2018 OmegaPhil <OmegaPhil@startmail.com>
#
#   This program is free software: you can redistribute it and/or modify it
#   under the terms of the GNU General Public License version 3, as published
#   by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranties of
#   MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.

import os
import re
import subprocess

import getpass
import psutil

from locale import gettext as _

from gi.repository import GLib, Gdk

import logging
logger = logging.getLogger('menulibre')

old_psutil_format = isinstance(psutil.Process.username, property)


def enum(**enums):
    """Add enumarations to Python."""
    return type('Enum', (), enums)


MenuItemTypes = enum(
    SEPARATOR=-1,
    APPLICATION=0,
    LINK=1,
    DIRECTORY=2
)


MenuItemKeys = (
    # Key, Type, Required, Types (MenuItemType)
    ("Version", str, False, (0, 1, 2)),
    ("Type", str, True, (0, 1, 2)),
    ("Name", str, True, (0, 1, 2)),
    ("GenericName", str, False, (0, 1, 2)),
    ("NoDisplay", bool, False, (0, 1, 2)),
    ("Comment", str, False, (0, 1, 2)),
    ("Icon", str, False, (0, 1, 2)),
    ("Hidden", bool, False, (0, 1, 2)),
    ("OnlyShowIn", list, False, (0, 1, 2)),
    ("NotShowIn", list, False, (0, 1, 2)),
    ("DBusActivatable", bool, False, (0,)),
    ("TryExec", str, False, (0,)),
    ("Exec", str, True, (0,)),
    ("Path", str, False, (0,)),
    ("Terminal", bool, False, (0,)),
    ("Actions", list, False, (0,)),
    ("MimeType", list, False, (0,)),
    ("Categories", list, False, (0,)),
    ("Implements", list, False, (0,)),
    ("Keywords", list, False, (0,)),
    ("StartupNotify", bool, False, (0,)),
    ("StartupWMClass", str, False, (0,)),
    ("URL", str, True, (1,))
)


def getRelatedKeys(menu_item_type, key_only=False):
    if isinstance(menu_item_type, str):
        if menu_item_type == "Application":
            menu_item_type = MenuItemTypes.APPLICATION
        elif menu_item_type == "Link":
            menu_item_type = MenuItemTypes.LINK
        elif menu_item_type == "Directory":
            menu_item_type = MenuItemTypes.DIRECTORY

    results = []
    for tup in MenuItemKeys:
        if menu_item_type in tup[3]:
            if key_only:
                results.append(tup[0])
            else:
                results.append((tup[0], tup[1], tup[2]))
    return results


def escapeText(text):
    if text is None:
        return ""
    return GLib.markup_escape_text(text)


def getProcessUsername(process):
    """Get the username of the process owner. Return None if fail."""
    username = None

    try:
        if old_psutil_format:
            username = process.username
        else:
            username = process.username()
    except:  # noqa
        pass

    return username


def getProcessName(process):
    """Get the process name. Return None if fail."""
    p_name = None

    try:
        if old_psutil_format:
            p_name = process.name
        else:
            p_name = process.name()
    except:  # noqa
        pass

    return p_name


def getProcessList():
    """Return a list of unique process names for the current user."""
    username = getpass.getuser()
    try:
        pids = psutil.get_pid_list()
    except AttributeError:
        pids = psutil.pids()
    processes = []
    for pid in pids:
        try:
            process = psutil.Process(pid)
            p_user = getProcessUsername(process)
            if p_user == username:
                p_name = getProcessName(process)
                if p_name is not None and p_name not in processes:
                    processes.append(p_name)
        except:  # noqa
            pass
    processes.sort()
    return processes


def getBasename(filename):
    if filename.endswith('.desktop'):
        basename = filename.split('/applications/', 1)[1]
    elif filename.endswith('.directory'):
        basename = filename.split('/desktop-directories/', 1)[1]
    return basename


def getCurrentDesktop():
    current_desktop = os.environ.get("XDG_CURRENT_DESKTOP", "")
    current_desktop = current_desktop.lower()
    for desktop in ["budgie", "pantheon", "gnome"]:
        if desktop in current_desktop:
            return desktop
    if "kde" in current_desktop:
        kde_version = int(os.environ.get("KDE_SESSION_VERSION", "4"))
        if kde_version >= 5:
            return "plasma"
        return "kde"
    return current_desktop


def getDefaultMenuPrefix(): # noqa
    """Return the default menu prefix."""
    prefix = os.environ.get('XDG_MENU_PREFIX', '')

    # Cinnamon and MATE don't set this variable
    if prefix == "":
        if 'cinnamon' in os.environ.get('DESKTOP_SESSION', ''):
            prefix = 'cinnamon-'
        elif 'mate' in os.environ.get('DESKTOP_SESSION', ''):
            prefix = 'mate-'

    if prefix == "":
        desktop = getCurrentDesktop()
        if desktop == 'plasma':
            prefix = 'kf5-'
        elif desktop == 'kde':
            prefix = 'kde4-'

    if prefix == "":
        processes = getProcessList()
        if 'xfce4-panel' in processes:
            prefix = 'xfce-'
        elif 'mate-panel' in processes:
            prefix = 'mate-'

    if len(prefix) == 0:
        logger.warning("No menu prefix found, MenuLibre will not function "
                       "properly.")

    return prefix


def getItemPath(file_id):
    """Return the path to the system-installed .desktop file."""
    for path in GLib.get_system_data_dirs():
        file_path = os.path.join(path, 'applications', file_id)
        if os.path.isfile(file_path):
            return file_path
    return None


def getUserItemPath():
    """Return the path to the user applications directory."""
    item_dir = os.path.join(GLib.get_user_data_dir(), 'applications')
    if not os.path.isdir(item_dir):
        os.makedirs(item_dir)
    return item_dir


def getDirectoryPath(file_id):
    """Return the path to the system-installed .directory file."""
    for path in GLib.get_system_data_dirs():
        file_path = os.path.join(path, 'desktop-directories', file_id)
        if os.path.isfile(file_path):
            return file_path
    return None


def getUserDirectoryPath():
    """Return the path to the user desktop-directories directory."""
    menu_dir = os.path.join(GLib.get_user_data_dir(), 'desktop-directories')
    if not os.path.isdir(menu_dir):
        os.makedirs(menu_dir)
    return menu_dir


def getUserMenuPath():
    """Return the path to the user menus directory."""
    menu_dir = os.path.join(GLib.get_user_config_dir(), 'menus')
    if not os.path.isdir(menu_dir):
        os.makedirs(menu_dir)
    return menu_dir


def getUserLauncherPath(basename):
    """Return the user-installed path to a .desktop or .directory file."""
    if basename.endswith('.desktop'):
        check_dir = "applications"
    else:
        check_dir = "desktop-directories"
    path = os.path.join(GLib.get_user_data_dir(), check_dir)
    filename = os.path.join(path, basename)
    if os.path.isfile(filename):
        return filename
    return None


def getSystemMenuPath(file_id):
    """Return the path to the system-installed menu file."""
    for path in GLib.get_system_config_dirs():
        file_path = os.path.join(path, 'menus', file_id)
        if os.path.isfile(file_path):
            return file_path
    return None


def getSystemLauncherPath(basename):
    """Return the system-installed path to a .desktop or .directory file."""
    if basename.endswith('.desktop'):
        check_dir = "applications"
    else:
        check_dir = "desktop-directories"
    for path in GLib.get_system_data_dirs():
        path = os.path.join(path, check_dir)
        filename = os.path.join(path, basename)
        if os.path.isfile(filename):
            return filename
    return None


def getDirectoryName(directory_str):  # noqa
    """Return the directory name to be used in the XML file."""

    # Note: When adding new logic here, please see if
    # getDirectoryNameFromCategory should also be updated

    # Get the menu prefix
    prefix = getDefaultMenuPrefix()
    has_prefix = False

    basename = getBasename(directory_str)
    name, ext = os.path.splitext(basename)

    # Handle directories like xfce-development
    if name.startswith(prefix):
        name = name[len(prefix):]
        name = name.title()
        has_prefix = True

    # Handle X-GNOME, X-XFCE
    if name.startswith("X-"):
        # Handle X-GNOME, X-XFCE
        condensed = name.split('-', 2)[-1]
        non_camel = re.sub('(?!^)([A-Z]+)', r' \1', condensed)
        return non_camel

    # Cleanup ArcadeGames and others as per the norm.
    if name.endswith('Games') and name != 'Games':
        condensed = name[:-5]
        non_camel = re.sub('(?!^)([A-Z]+)', r' \1', condensed)
        return non_camel

    # GNOME...
    if name == 'AudioVideo' or name == 'Audio-Video':
        return 'Multimedia'

    if name == 'Game':
        return 'Games'

    if name == 'Network' and prefix != 'xfce-':
        return 'Internet'

    if name == 'Utility':
        return 'Accessories'

    if name == 'System-Tools':
        if prefix == 'lxde-':
            return 'Administration'
        else:
            return 'System'

    if name == 'Settings':
        if prefix == 'lxde-':
            return 'DesktopSettings'
        elif has_prefix and prefix == 'xfce-':
            return name
        else:
            return 'Preferences'

    if name == 'Settings-System':
        return 'Administration'

    if name == 'GnomeScience':
        return 'Science'

    if name == 'Utility-Accessibility':
        return 'Universal Access'

    # We tried, just return the name.
    return name


def getDirectoryNameFromCategory(name):  # noqa
    """Guess at the directory name a category should cause its launcher to
    appear in. This is used to add launchers to or remove from the right
    directories after category addition without having to restart menulibre."""

    # Note: When adding new logic here, please see if
    # getDirectoryName should also be updated

    # I don't want to overload the use of getDirectoryName, so have spun out
    # this similar function

    # Only interested in generic categories here, so no need to handle
    # categories named after desktop environments
    prefix = getDefaultMenuPrefix()

    # Cleanup ArcadeGames and others as per the norm.
    if name.endswith('Games') and name != 'Games':
        condensed = name[:-5]
        non_camel = re.sub('(?!^)([A-Z]+)', r' \1', condensed)
        return non_camel

    # GNOME...
    if name == 'AudioVideo' or name == 'Audio-Video':
        return 'Multimedia'

    if name == 'Game':
        return 'Games'

    if name == 'Network':
        return 'Internet'

    if name == 'Utility':
        return 'Accessories'

    if name == 'System-Tools':
        if prefix == 'lxde-':
            return 'Administration'
        else:
            return 'System'

    if name == 'Settings':
        if prefix == 'lxde-':
            return 'DesktopSettings'
        elif prefix == 'xfce-':
            return name
        else:
            return 'Preferences'

    if name == 'Settings-System':
        return 'Administration'

    if name == 'GnomeScience':
        return 'Science'

    if name == 'Utility-Accessibility':
        return 'Universal Access'

    # We tried, just return the name.
    return name


def getRequiredCategories(directory):
    """Return the list of required categories for a directory string."""
    prefix = getDefaultMenuPrefix()
    if directory is not None:
        basename = getBasename(directory)
        name, ext = os.path.splitext(basename)

        # Handle directories like xfce-development
        if name.startswith(prefix):
            name = name[len(prefix):]
            name = name.title()

        if name == 'Accessories':
            return ['Utility']

        if name == 'Games':
            return ['Game']

        if name == 'Multimedia':
            return ['AudioVideo']

        else:
            return [name]
    else:
        # Get The Toplevel item if necessary...
        if prefix == 'xfce-':
            return ['X-XFCE', 'X-Xfce-Toplevel']
    return []


def getSaveFilename(name, filename, item_type, force_update=False):  # noqa
    """Determime the filename to be used to store the launcher.

    Return the filename to be used."""
    # Check if the filename is writeable. If not, generate a new one.
    unique = filename is None or len(filename) == 0

    if unique or not os.access(filename, os.W_OK):
        # No filename, make one from the launcher name.
        if unique:
            basename = "menulibre-" + name.lower().replace(' ', '-')

        # Use the current filename as a base.
        else:
            basename = getBasename(filename)

        # Split the basename into filename and extension.
        name, ext = os.path.splitext(basename)

        # Get the save location of the launcher base on type.
        if item_type == 'Application':
            path = getUserItemPath()
            ext = '.desktop'
        elif item_type == 'Directory':
            path = getUserDirectoryPath()
            ext = '.directory'

        basedir = os.path.dirname(os.path.join(path, basename))
        if not os.path.exists(basedir):
            os.makedirs(basedir)

        # Index for unique filenames.
        count = 1

        # Be sure to not overwrite system launchers if new.
        if unique:
            # Check for the system version of the launcher.
            if getSystemLauncherPath("%s%s" % (name, ext)) is not None:
                # If found, check for any additional ones.
                while getSystemLauncherPath("%s%i%s" % (name, count, ext)) \
                        is not None:
                    count += 1

                # Now be sure to not overwrite locally installed ones.
                filename = os.path.join(path, name)
                filename = "%s%i%s" % (filename, count, ext)

                # Append numbers as necessary to make the filename unique.
                while os.path.exists(filename):
                    new_basename = "%s%i%s" % (name, count, ext)
                    filename = os.path.join(path, new_basename)
                    count += 1

            else:
                # Create the new base filename.
                filename = os.path.join(path, name)
                filename = "%s%s" % (filename, ext)

                # Append numbers as necessary to make the filename unique.
                while os.path.exists(filename):
                    new_basename = "%s%i%s" % (name, count, ext)
                    filename = os.path.join(path, new_basename)
                    count += 1

        else:
            # Create the new base filename.
            filename = os.path.join(path, basename)

            if force_update:
                return filename

            # Append numbers as necessary to make the filename unique.
            while os.path.exists(filename):
                new_basename = "%s%i%s" % (name, count, ext)
                filename = os.path.join(path, new_basename)
                count += 1

    return filename


def check_keypress(event, keys):  # noqa
    """Compare keypress events with desired keys and return True if matched."""
    if 'Control' in keys:
        if not bool(event.get_state() & Gdk.ModifierType.CONTROL_MASK):
            return False
    if 'Alt' in keys:
        if not bool(event.get_state() & Gdk.ModifierType.MOD1_MASK):
            return False
    if 'Shift' in keys:
        if not bool(event.get_state() & Gdk.ModifierType.SHIFT_MASK):
            return False
    if 'Super' in keys:
        if not bool(event.get_state() & Gdk.ModifierType.SUPER_MASK):
            return False
    if 'Escape' in keys:
        keys[keys.index('Escape')] = 'escape'
    if Gdk.keyval_name(event.get_keyval()[1]).lower() not in keys:
        return False

    return True


def determine_bad_desktop_files():
    """Run the gmenu-invalid-desktop-files script to get at the GMenu library's
    debug output, which lists files that failed to load, and return these as a
    sorted list."""

    # Run the helper script with normal binary lookup via the shell, capturing
    # stderr, sensitive to errors
    try:
        result = subprocess.run(['menulibre-menu-validate'],
                                stderr=subprocess.PIPE, shell=True, check=True)
    except subprocess.CalledProcessError:
        return []

    # stderr is returned as bytes, so converting it to the line-buffered output
    # I actually want
    bad_desktop_files = []
    for line in result.stderr.decode('UTF-8').split('\n'):
        matches = re.match(r'^Failed to load "(.+\.desktop)"$', line)
        if matches:
            desktop_file = matches.groups()[0]
            if validate_desktop_file(desktop_file):
                bad_desktop_files.append(matches.groups()[0])

    # Alphabetical sort on bad desktop file paths
    bad_desktop_files.sort()

    return bad_desktop_files


def find_program(program):
    program = program.strip()
    if len(program) == 0:
        return None

    params = list(GLib.shell_parse_argv(program)[1])
    executable = params[0]

    if os.path.exists(executable):
        return executable

    path = GLib.find_program_in_path(executable)
    if path is not None:
        return path

    return None

def validate_desktop_file(desktop_file):  # noqa
    """Validate a known-bad desktop file in the same way GMenu/glib does, to
    give a user real information about why certain files are broken."""

    # This is a reimplementation of the validation logic in glib2's
    # gio/gdesktopappinfo.c:g_desktop_app_info_load_from_keyfile.
    # gnome-menus appears also to try to do its own validation in
    # libmenu/desktop-entries.c:desktop_entry_load, however
    # g_desktop_app_info_new_from_filename will not return a valid
    # GDesktopAppInfo in the first place if something is wrong with the
    # desktop file

    try:

        # Looks like load_from_file is not a class method??
        keyfile = GLib.KeyFile()
        keyfile.load_from_file(desktop_file, GLib.KeyFileFlags.NONE)

    except Exception as e:
        # Translators: This error is displayed when a desktop file cannot
        # be correctly read by MenuLibre. A (possibly untranslated) error
        # code is displayed.
        return _('Unable to load desktop file due to the following error:'
                 ' %s') % e

    # File is at least a valid keyfile, so can start the real desktop
    # validation
    # Start group validation
    try:
        start_group = keyfile.get_start_group()
    except GLib.Error:
        start_group = None

    if start_group != GLib.KEY_FILE_DESKTOP_GROUP:
        # Translators: This error is displayed when the first group in a
        # failing desktop file is incorrect. "Start group" can be safely
        # translated.
        return (_('Start group is invalid - currently \'%s\', should be '
                  '\'%s\'') % (start_group, GLib.KEY_FILE_DESKTOP_GROUP))

    # Type validation
    try:
        type_key = keyfile.get_string(start_group,
                                      GLib.KEY_FILE_DESKTOP_KEY_TYPE)
    except GLib.Error:
        # Translators: This error is displayed when a required key is
        # missing in a failing desktop file.
        return _('%s key not found') % 'Type'

    if type_key != GLib.KEY_FILE_DESKTOP_TYPE_APPLICATION:
        # Translators: This error is displayed when a failing desktop file
        # has an invalid value for the provided key.
        return (_('%s value is invalid - currently \'%s\', should be \'%s\'')
                % ('Type', type_key, GLib.KEY_FILE_DESKTOP_TYPE_APPLICATION))

    # Validating 'try exec' if its present
    # Invalid TryExec is a valid state. "If the file is not present or if it
    # is not executable, the entry may be ignored (not be used in menus, for
    # example)."
    try:
        try_exec = keyfile.get_string(start_group,
                                      GLib.KEY_FILE_DESKTOP_KEY_TRY_EXEC)
    except GLib.Error:
        pass

    else:
        try:
            if len(try_exec) > 0 and find_program(try_exec) is None:
                return False
        except Exception as e:
            return False

    # Validating executable
    try:
        exec_key = keyfile.get_string(start_group,
                                      GLib.KEY_FILE_DESKTOP_KEY_EXEC)
    except GLib.Error:
        return False # LP: #1788814, Exec key is not required

    try:
        if find_program(exec_key) is None:
            return (_('%s program \'%s\' has not been found in the PATH')
                    % ('Exec', exec_key))
    except Exception as e:
        return (_('%s program \'%s\' is not a valid shell command '
                  'according to GLib.shell_parse_argv, error: %s')
                % ('Exec', exec_key, e))

    # Translators: This error is displayed for a failing desktop file where
    # errors were detected but the file seems otherwise valid.
    return _('Unknown error. Desktop file appears to be valid.')