delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kde-menu / kde-menu.cpp
blobbb692a343580f2a4a7e9447c1ecccd69e38eed80
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.
18 **/
20 #include <stdlib.h>
22 #include <QtCore/QFile>
23 #include <QtDBus/QtDBus>
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>
37 static const char appName[] = "kde-menu";
38 static const char appVersion[] = "1.0";
39 static bool utf8;
41 static bool bPrintMenuId;
42 static bool bPrintMenuName;
43 static bool bHighlight;
45 static void result(const QString &txt)
47 if (utf8)
48 puts( txt.toUtf8() );
49 else
50 puts( txt.toLocal8Bit() );
53 static void error(int exitCode, const QString &txt)
55 qWarning("kde-menu: %s", txt.toLocal8Bit().data());
56 exit(exitCode);
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)
78 if (bPrintMenuId)
80 result(parent->relPath());
82 if (bPrintMenuName)
84 result(name);
86 #if 0
87 #ifdef Q_WS_X11
88 if (bHighlight)
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());
95 #endif
96 #endif
97 exit(0);
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,
112 ki18n(description),
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"))
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)
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);
163 if (!s)
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));
169 return 2;