1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * plug-in-menu-path.c
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "config.h"
21 
22 #include <string.h>
23 
24 #include <gio/gio.h>
25 
26 #include "libgimpbase/gimpbase.h"
27 
28 #include "plug-in-types.h"
29 
30 #include "plug-in-menu-path.h"
31 
32 
33 typedef struct _MenuPathMapping MenuPathMapping;
34 
35 struct _MenuPathMapping
36 {
37   const gchar *orig_path;
38   const gchar *label;
39   const gchar *mapped_path;
40 };
41 
42 
43 static const MenuPathMapping menu_path_mappings[] =
44 {
45   { "<Toolbox>/Xtns/Languages",       NULL, "<Image>/Filters/Languages"           },
46   { "<Toolbox>/Xtns/Extensions",      NULL, "<Image>/Filters/Extensions"          },
47 
48   { "<Toolbox>/Xtns/Buttons",         NULL, "<Image>/File/Create/Buttons"         },
49   { "<Toolbox>/Xtns/Logos",           NULL, "<Image>/File/Create/Logos"           },
50   { "<Toolbox>/Xtns/Misc",            NULL, "<Image>/File/Create/Misc"            },
51   { "<Toolbox>/Xtns/Patterns",        NULL, "<Image>/File/Create/Patterns"        },
52   { "<Toolbox>/Xtns/Web Page Themes", NULL, "<Image>/File/Create/Web Page Themes" },
53 
54   { "<Toolbox>/Xtns", "Buttons",            "<Image>/File/Create"                 },
55   { "<Toolbox>/Xtns", "Logos",              "<Image>/File/Create"                 },
56   { "<Toolbox>/Xtns", "Misc",               "<Image>/File/Create"                 },
57   { "<Toolbox>/Xtns", "Patterns",           "<Image>/File/Create"                 },
58   { "<Toolbox>/Xtns", "Web Page Themes",    "<Image>/File/Create"                 },
59 
60   { "<Toolbox>/Xtns",                 NULL, "<Image>/Filters/Extensions"          },
61   { "<Toolbox>/Help",                 NULL, "<Image>/Help"                        },
62 
63   { "<Toolbox>/File/Acquire",           NULL, "<Image>/File/Create/Acquire"       },
64   { "<Toolbox>",                        NULL, "<Image>"                           },
65   { "<Image>/File/Acquire",             NULL, "<Image>/File/Create/Acquire"       },
66   { "<Image>/File/New",                 NULL, "<Image>/File/Create"               },
67   { "<Image>/Image/Mode/Color Profile", NULL, "<Image>/Image/Color Management"    },
68   { NULL, NULL, NULL }
69 };
70 
71 
72 gchar *
plug_in_menu_path_map(const gchar * menu_path,const gchar * menu_label)73 plug_in_menu_path_map (const gchar *menu_path,
74                        const gchar *menu_label)
75 {
76   const MenuPathMapping *mapping;
77   gchar                 *stripped_label = NULL;
78 
79   g_return_val_if_fail (menu_path != NULL, NULL);
80 
81   if (menu_label)
82     stripped_label = gimp_strip_uline (menu_label);
83 
84   for (mapping = menu_path_mappings; mapping->orig_path; mapping++)
85     {
86       if (g_str_has_prefix (menu_path, mapping->orig_path))
87         {
88           gint   orig_len = strlen (mapping->orig_path);
89           gchar *mapped_path;
90 
91           /*  if the mapping has a label, only map if the passed label
92            *  is identical and the paths' lengths match exactly.
93            */
94           if (mapping->label &&
95               (! stripped_label               ||
96                strlen (menu_path) != orig_len ||
97                strcmp (mapping->label, stripped_label)))
98             {
99               continue;
100             }
101 
102           if (strlen (menu_path) > orig_len)
103             mapped_path = g_strconcat (mapping->mapped_path,
104                                        menu_path + orig_len,
105                                        NULL);
106           else
107             mapped_path = g_strdup (mapping->mapped_path);
108 
109 #if GIMP_UNSTABLE
110           {
111             gchar *orig;
112             gchar *mapped;
113 
114             if (menu_label)
115               {
116                 orig   = g_strdup_printf ("%s/%s", menu_path,   stripped_label);
117                 mapped = g_strdup_printf ("%s/%s", mapped_path, stripped_label);
118               }
119             else
120               {
121                 orig   = g_strdup (menu_path);
122                 mapped = g_strdup (mapped_path);
123               }
124 
125             g_printerr (" mapped '%s' to '%s'\n", orig, mapped);
126 
127             g_free (orig);
128             g_free (mapped);
129           }
130 #endif
131 
132           g_free (stripped_label);
133 
134           return mapped_path;
135         }
136     }
137 
138   g_free (stripped_label);
139 
140   return g_strdup (menu_path);
141 }
142