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
|
// -*- c-basic-offset: 3 -*-
/* This file is part of the KDE libraries
* Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation;
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
**/
#include <stdlib.h>
#include <QtCore/QFile>
#include <QtDBus/QtDBus>
#include <kaboutdata.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kglobal.h>
#include <klocale.h>
#include <kservice.h>
#include <kservicegroup.h>
#include <kstandarddirs.h>
#include <ktoolinvocation.h>
#include "klauncher_iface.h"
#include <ksycoca.h>
static const char appName[] = "kde-menu";
static const char appVersion[] = "1.0";
static bool utf8;
static bool bPrintMenuId;
static bool bPrintMenuName;
static bool bHighlight;
static void result(const QString &txt)
{
if (utf8)
puts( txt.toUtf8() );
else
puts( txt.toLocal8Bit() );
}
static void error(int exitCode, const QString &txt)
{
qWarning("kde-menu: %s", txt.toLocal8Bit().data());
exit(exitCode);
}
static void findMenuEntry(KServiceGroup::Ptr parent, const QString &name, const QString &menuId)
{
const KServiceGroup::List list = parent->entries(true, true, false);
KServiceGroup::List::ConstIterator it = list.constBegin();
for (; it != list.constEnd(); ++it)
{
KSycocaEntry::Ptr e = (*it);
if (e->isType(KST_KServiceGroup))
{
KServiceGroup::Ptr g = KServiceGroup::Ptr::staticCast( e );
findMenuEntry(g, name.isEmpty() ? g->caption() : name+'/'+g->caption(), menuId);
}
else if (e->isType(KST_KService))
{
KService::Ptr s = KService::Ptr::staticCast( e );
if (s->menuId() == menuId)
{
if (bPrintMenuId)
{
result(parent->relPath());
}
if (bPrintMenuName)
{
result(name);
}
#if 0
#ifdef Q_WS_X11
if (bHighlight)
{
QDBusInterface kicker( "org.kde.kicker", "/kicker", "org.kde.Kicker" );
QDBusReply<void> result = kicker.call( "highlightMenuItem", menuId );
if (!result.isValid())
error(3, i18n("Menu item '%1' could not be highlighted.", menuId).toLocal8Bit());
}
#endif
#endif
exit(0);
}
}
}
}
int main(int argc, char **argv)
{
const char *description = I18N_NOOP("KDE Menu query tool.\n"
"This tool can be used to find in which menu a specific application is shown.\n"
"The --highlight option can be used to visually indicate to the user where\n"
"in the KDE menu a specific application is located.");
KAboutData d(appName, "kde-menu", ki18n("kde-menu"), appVersion,
ki18n(description),
KAboutData::License_GPL, ki18n("(c) 2003 Waldo Bastian"));
d.addAuthor(ki18n("Waldo Bastian"), ki18n("Author"), "bastian@kde.org");
KCmdLineArgs::init(argc, argv, &d);
KCmdLineOptions options;
options.add("utf8", ki18n("Output data in UTF-8 instead of local encoding"));
options.add("print-menu-id", ki18n("Print menu-id of the menu that contains\nthe application"));
options.add("print-menu-name", ki18n("Print menu name (caption) of the menu that\ncontains the application"));
options.add("highlight", ki18n("Highlight the entry in the menu"));
options.add("nocache-update", ki18n("Do not check if sycoca database is up to date"));
options.add("+<application-id>", ki18n("The id of the menu entry to locate"));
KCmdLineArgs::addCmdLineOptions(options);
// KApplication k(false, false);
KApplication k(false);
k.disableSessionManagement();
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
if (args->count() != 1)
KCmdLineArgs::usageError(i18n("You must specify an application-id such as 'kde4-konsole.desktop'"));
utf8 = args->isSet("utf8");
bPrintMenuId = args->isSet("print-menu-id");
bPrintMenuName = args->isSet("print-menu-name");
bHighlight = args->isSet("highlight");
if (!bPrintMenuId && !bPrintMenuName && !bHighlight)
KCmdLineArgs::usageError(i18n("You must specify at least one of --print-menu-id, --print-menu-name or --highlight"));
if (args->isSet("cache-update"))
{
QStringList args;
args.append("--incremental");
args.append("--checkstamps");
QString command = KStandardDirs::findExe(KBUILDSYCOCA_EXENAME);
QDBusMessage reply = KToolInvocation::klauncher()->call("kdeinit_exec_wait", command, args, QStringList(), QString());
if (reply.type() != QDBusMessage::ReplyMessage)
{
qWarning("Can not talk to klauncher!");
command = KGlobal::dirs()->findExe(command);
command += ' ' + args.join(" ");
system(command.toLocal8Bit());
}
}
QString menuId = args->arg(0);
KService::Ptr s = KService::serviceByMenuId(menuId);
if (!s)
error(1, i18n("No menu item '%1'.", menuId));
findMenuEntry(KServiceGroup::root(), "", menuId);
error(2, i18n("Menu item '%1' not found in menu.", menuId));
return 2;
}
|