1 // -*- c-basic-offset: 3 -*-
2 /* This file is part of the KDE libraries
3 * Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
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;
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.
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.
22 #include <QtCore/QFile>
23 #include <QtDBus/QtDBus>
25 #include <kaboutdata.h>
26 #include <kapplication.h>
27 #include <kcmdlineargs.h>
31 #include <kservicegroup.h>
32 #include <kstandarddirs.h>
33 #include <ktoolinvocation.h>
34 #include "klauncher_iface.h"
37 static const char appName
[] = "kde-menu";
38 static const char appVersion
[] = "1.0";
41 static bool bPrintMenuId
;
42 static bool bPrintMenuName
;
43 static bool bHighlight
;
45 static void result(const QString
&txt
)
50 puts( txt
.toLocal8Bit() );
53 static void error(int exitCode
, const QString
&txt
)
55 qWarning("kde-menu: %s", txt
.toLocal8Bit().data());
59 static void findMenuEntry(KServiceGroup::Ptr parent
, const QString
&name
, const QString
&menuId
)
61 const KServiceGroup::List list
= parent
->entries(true, true, false);
62 KServiceGroup::List::ConstIterator it
= list
.constBegin();
63 for (; it
!= list
.constEnd(); ++it
)
65 KSycocaEntry::Ptr e
= (*it
);
67 if (e
->isType(KST_KServiceGroup
))
69 KServiceGroup::Ptr g
= KServiceGroup::Ptr::staticCast( e
);
71 findMenuEntry(g
, name
.isEmpty() ? g
->caption() : name
+'/'+g
->caption(), menuId
);
73 else if (e
->isType(KST_KService
))
75 KService::Ptr s
= KService::Ptr::staticCast( e
);
76 if (s
->menuId() == menuId
)
80 result(parent
->relPath());
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());
104 int main(int argc
, char **argv
)
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.");
111 KAboutData
d(appName
, "kde-menu", ki18n("kde-menu"), appVersion
,
113 KAboutData::License_GPL
, ki18n("(c) 2003 Waldo Bastian"));
114 d
.addAuthor(ki18n("Waldo Bastian"), ki18n("Author"), "bastian@kde.org");
116 KCmdLineArgs::init(argc
, argv
, &d
);
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
);
127 // KApplication k(false, false);
128 KApplication
k(false);
129 k
.disableSessionManagement();
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'"));
135 utf8
= args
->isSet("utf8");
137 bPrintMenuId
= args
->isSet("print-menu-id");
138 bPrintMenuName
= args
->isSet("print-menu-name");
139 bHighlight
= args
->isSet("highlight");
141 if (!bPrintMenuId
&& !bPrintMenuName
&& !bHighlight
)
142 KCmdLineArgs::usageError(i18n("You must specify at least one of --print-menu-id, --print-menu-name or --highlight"));
144 if (args
->isSet("cache-update"))
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
)
153 qWarning("Can not talk to klauncher!");
154 command
= KGlobal::dirs()->findExe(command
);
155 command
+= ' ' + args
.join(" ");
156 system(command
.toLocal8Bit());
160 QString menuId
= args
->arg(0);
161 KService::Ptr s
= KService::serviceByMenuId(menuId
);
164 error(1, i18n("No menu item '%1'.", menuId
));
166 findMenuEntry(KServiceGroup::root(), "", menuId
);
168 error(2, i18n("Menu item '%1' not found in menu.", menuId
));