1 | // -*- c-basic-offset: 3 -*- |
2 | /* This file is part of the KDE libraries |
3 | * Copyright (C) 2003 Waldo Bastian <bastian@kde.org> |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License version 2 as published by the Free Software Foundation; |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | **/ |
19 | |
20 | #include <stdlib.h> |
21 | |
22 | #include <QtCore/QFile> |
23 | #include <QtDBus/QtDBus> |
24 | |
25 | #include <kaboutdata.h> |
26 | #include <kapplication.h> |
27 | #include <kcmdlineargs.h> |
28 | #include <kglobal.h> |
29 | #include <klocale.h> |
30 | #include <kservice.h> |
31 | #include <kservicegroup.h> |
32 | #include <kstandarddirs.h> |
33 | #include <ktoolinvocation.h> |
34 | #include "klauncher_iface.h" |
35 | #include <ksycoca.h> |
36 | |
37 | static const char appName[] = "kde-menu" ; |
38 | static const char appVersion[] = "1.0" ; |
39 | static bool utf8; |
40 | |
41 | static bool ; |
42 | static bool ; |
43 | static bool bHighlight; |
44 | |
45 | static void result(const QString &txt) |
46 | { |
47 | if (utf8) |
48 | puts( txt.toUtf8() ); |
49 | else |
50 | puts( txt.toLocal8Bit() ); |
51 | } |
52 | |
53 | static void error(int exitCode, const QString &txt) |
54 | { |
55 | qWarning("kde-menu: %s" , txt.toLocal8Bit().data()); |
56 | exit(exitCode); |
57 | } |
58 | |
59 | static void (KServiceGroup::Ptr parent, const QString &name, const QString &) |
60 | { |
61 | const KServiceGroup::List list = parent->entries(true, true, false); |
62 | KServiceGroup::List::ConstIterator it = list.constBegin(); |
63 | for (; it != list.constEnd(); ++it) |
64 | { |
65 | KSycocaEntry::Ptr e = (*it); |
66 | |
67 | if (e->isType(KST_KServiceGroup)) |
68 | { |
69 | KServiceGroup::Ptr g = KServiceGroup::Ptr::staticCast( e ); |
70 | |
71 | findMenuEntry(g, name.isEmpty() ? g->caption() : name+'/'+g->caption(), menuId); |
72 | } |
73 | else if (e->isType(KST_KService)) |
74 | { |
75 | KService::Ptr s = KService::Ptr::staticCast( e ); |
76 | if (s->menuId() == menuId) |
77 | { |
78 | if (bPrintMenuId) |
79 | { |
80 | result(parent->relPath()); |
81 | } |
82 | if (bPrintMenuName) |
83 | { |
84 | result(name); |
85 | } |
86 | #if 0 |
87 | #ifdef Q_WS_X11 |
88 | if (bHighlight) |
89 | { |
90 | QDBusInterface kicker( "org.kde.kicker" , "/kicker" , "org.kde.Kicker" ); |
91 | QDBusReply<void> result = kicker.call( "highlightMenuItem" , menuId ); |
92 | if (!result.isValid()) |
93 | error(3, i18n("Menu item '%1' could not be highlighted." , menuId).toLocal8Bit()); |
94 | } |
95 | #endif |
96 | #endif |
97 | exit(0); |
98 | } |
99 | } |
100 | } |
101 | } |
102 | |
103 | |
104 | int main(int argc, char **argv) |
105 | { |
106 | const char *description = I18N_NOOP("KDE Menu query tool.\n" |
107 | "This tool can be used to find in which menu a specific application is shown.\n" |
108 | "The --highlight option can be used to visually indicate to the user where\n" |
109 | "in the KDE menu a specific application is located." ); |
110 | |
111 | KAboutData d(appName, "kde-menu" , ki18n("kde-menu" ), appVersion, |
112 | ki18n(description), |
113 | KAboutData::License_GPL, ki18n("(c) 2003 Waldo Bastian" )); |
114 | d.addAuthor(ki18n("Waldo Bastian" ), ki18n("Author" ), "bastian@kde.org" ); |
115 | |
116 | KCmdLineArgs::init(argc, argv, &d); |
117 | |
118 | KCmdLineOptions options; |
119 | options.add("utf8" , ki18n("Output data in UTF-8 instead of local encoding" )); |
120 | options.add("print-menu-id" , ki18n("Print menu-id of the menu that contains\nthe application" )); |
121 | options.add("print-menu-name" , ki18n("Print menu name (caption) of the menu that\ncontains the application" )); |
122 | options.add("highlight" , ki18n("Highlight the entry in the menu" )); |
123 | options.add("nocache-update" , ki18n("Do not check if sycoca database is up to date" )); |
124 | options.add("+<application-id>" , ki18n("The id of the menu entry to locate" )); |
125 | KCmdLineArgs::addCmdLineOptions(options); |
126 | |
127 | // KApplication k(false, false); |
128 | KApplication k(false); |
129 | k.disableSessionManagement(); |
130 | |
131 | KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); |
132 | if (args->count() != 1) |
133 | KCmdLineArgs::usageError(i18n("You must specify an application-id such as 'kde4-konsole.desktop'" )); |
134 | |
135 | utf8 = args->isSet("utf8" ); |
136 | |
137 | bPrintMenuId = args->isSet("print-menu-id" ); |
138 | bPrintMenuName = args->isSet("print-menu-name" ); |
139 | bHighlight = args->isSet("highlight" ); |
140 | |
141 | if (!bPrintMenuId && !bPrintMenuName && !bHighlight) |
142 | KCmdLineArgs::usageError(i18n("You must specify at least one of --print-menu-id, --print-menu-name or --highlight" )); |
143 | |
144 | if (args->isSet("cache-update" )) |
145 | { |
146 | QStringList args; |
147 | args.append("--incremental" ); |
148 | args.append("--checkstamps" ); |
149 | QString command = KStandardDirs::findExe(KBUILDSYCOCA_EXENAME); |
150 | QDBusMessage reply = KToolInvocation::klauncher()->call("kdeinit_exec_wait" , command, args, QStringList(), QString()); |
151 | if (reply.type() != QDBusMessage::ReplyMessage) |
152 | { |
153 | qWarning("Can not talk to klauncher!" ); |
154 | command = KGlobal::dirs()->findExe(command); |
155 | command += ' ' + args.join(" " ); |
156 | system(command.toLocal8Bit()); |
157 | } |
158 | } |
159 | |
160 | QString = args->arg(0); |
161 | KService::Ptr s = KService::serviceByMenuId(menuId); |
162 | |
163 | if (!s) |
164 | error(1, i18n("No menu item '%1'." , menuId)); |
165 | |
166 | findMenuEntry(KServiceGroup::root(), "" , menuId); |
167 | |
168 | error(2, i18n("Menu item '%1' not found in menu." , menuId)); |
169 | return 2; |
170 | } |
171 | |
172 | |