kde-menu.cpp
00001 /* This file is part of the KDE libraries 00002 * Copyright (C) 2003 Waldo Bastian <bastian@kde.org> 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License version 2 as published by the Free Software Foundation; 00007 * 00008 * This library is distributed in the hope that it will be useful, 00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Library General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Library General Public License 00014 * along with this library; see the file COPYING.LIB. If not, write to 00015 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 * Boston, MA 02110-1301, USA. 00017 **/ 00018 00019 #include <stdlib.h> 00020 00021 #include <tqfile.h> 00022 00023 #include <dcopclient.h> 00024 #include <dcopref.h> 00025 00026 #include "kaboutdata.h" 00027 #include "kapplication.h" 00028 #include "kcmdlineargs.h" 00029 #include "kglobal.h" 00030 #include "klocale.h" 00031 #include "kservice.h" 00032 #include "kservicegroup.h" 00033 #include "kstandarddirs.h" 00034 00035 static KCmdLineOptions options[] = { 00036 { "utf8", I18N_NOOP("Output data in UTF-8 instead of local encoding"), 0 }, 00037 { "print-menu-id", I18N_NOOP("Print menu-id of the menu that contains\nthe application"), 0 }, 00038 { "print-menu-name", I18N_NOOP("Print menu name (caption) of the menu that\ncontains the application"), 0 }, 00039 { "highlight", I18N_NOOP("Highlight the entry in the menu"), 0 }, 00040 { "nocache-update", I18N_NOOP("Do not check if sycoca database is up to date"), 0 }, 00041 { "+<application-id>", I18N_NOOP("The id of the menu entry to locate"), 0 }, 00042 KCmdLineLastOption 00043 }; 00044 00045 static const char appName[] = "kde-menu"; 00046 static const char appVersion[] = "1.0"; 00047 static bool utf8; 00048 00049 static bool bPrintMenuId; 00050 static bool bPrintMenuName; 00051 static bool bHighlight; 00052 00053 static void result(const TQString &txt) 00054 { 00055 if (utf8) 00056 puts( txt.utf8() ); 00057 else 00058 puts( txt.local8Bit() ); 00059 } 00060 00061 static void error(int exitCode, const TQString &txt) 00062 { 00063 qWarning("kde-menu: %s", txt.local8Bit().data()); 00064 exit(exitCode); 00065 } 00066 00067 static void findMenuEntry(KServiceGroup::Ptr parent, const TQString &name, const TQString &menuId) 00068 { 00069 KServiceGroup::List list = parent->entries(true, true, false); 00070 KServiceGroup::List::ConstIterator it = list.begin(); 00071 for (; it != list.end(); ++it) 00072 { 00073 KSycocaEntry * e = *it; 00074 00075 if (e->isType(KST_KServiceGroup)) 00076 { 00077 KServiceGroup::Ptr g(static_cast<KServiceGroup *>(e)); 00078 00079 findMenuEntry(g, name.isEmpty() ? g->caption() : name+"/"+g->caption(), menuId); 00080 } 00081 else if (e->isType(KST_KService)) 00082 { 00083 KService::Ptr s(static_cast<KService *>(e)); 00084 if (s->menuId() == menuId) 00085 { 00086 if (bPrintMenuId) 00087 { 00088 result(parent->relPath()); 00089 } 00090 if (bPrintMenuName) 00091 { 00092 result(name); 00093 } 00094 if (bHighlight) 00095 { 00096 DCOPRef kicker( "kicker", "kicker" ); 00097 bool result = kicker.call( "highlightMenuItem", menuId ); 00098 if (!result) 00099 error(3, TQString(i18n("Menu item '%1' could not be highlighted.").arg(menuId)).local8Bit()); 00100 } 00101 exit(0); 00102 } 00103 } 00104 } 00105 } 00106 00107 00108 int main(int argc, char **argv) 00109 { 00110 KLocale::setMainCatalogue("kdelibs"); 00111 const char *description = I18N_NOOP("KDE Menu query tool.\n" 00112 "This tool can be used to find in which menu a specific application is shown.\n" 00113 "The --highlight option can be used to visually indicate to the user where\n" 00114 "in the KDE menu a specific application is located."); 00115 00116 KAboutData d(appName, I18N_NOOP("kde-menu"), appVersion, 00117 description, 00118 KAboutData::License_GPL, "(c) 2003 Waldo Bastian"); 00119 d.addAuthor("Waldo Bastian", I18N_NOOP("Author"), "bastian@kde.org"); 00120 00121 KCmdLineArgs::init(argc, argv, &d); 00122 KCmdLineArgs::addCmdLineOptions(options); 00123 00124 // KApplication k(false, false); 00125 KApplication k(false); 00126 k.disableSessionManagement(); 00127 00128 // this program is in kdelibs so it uses kdelibs as catalog 00129 KLocale::setMainCatalogue("kdelibs"); 00130 00131 KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); 00132 if (args->count() != 1) 00133 KCmdLineArgs::usage(i18n("You must specify an application-id such as 'kde-konsole.desktop'")); 00134 00135 utf8 = args->isSet("utf8"); 00136 00137 bPrintMenuId = args->isSet("print-menu-id"); 00138 bPrintMenuName = args->isSet("print-menu-name"); 00139 bHighlight = args->isSet("highlight"); 00140 00141 if (!bPrintMenuId && !bPrintMenuName && !bHighlight) 00142 KCmdLineArgs::usage(i18n("You must specify at least one of --print-menu-id, --print-menu-name or --highlight")); 00143 00144 if (args->isSet("cache-update")) 00145 { 00146 TQStringList args; 00147 args.append("--incremental"); 00148 args.append("--checkstamps"); 00149 TQString command = "kbuildsycoca"; 00150 TQCString _launcher = KApplication::launcher(); 00151 if (!DCOPRef(_launcher, _launcher).call("kdeinit_exec_wait", command, args).isValid()) 00152 { 00153 qWarning("Can't talk to klauncher!"); 00154 command = KGlobal::dirs()->findExe(command); 00155 command += " " + args.join(" "); 00156 system(command.local8Bit()); 00157 } 00158 } 00159 00160 TQString menuId = TQFile::decodeName(args->arg(0)); 00161 KService::Ptr s = KService::serviceByMenuId(menuId); 00162 00163 if (!s) 00164 error(1, i18n("No menu item '%1'.").arg(menuId)); 00165 00166 findMenuEntry(KServiceGroup::root(), "", menuId); 00167 00168 error(2, i18n("Menu item '%1' not found in menu.").arg(menuId)); 00169 return 2; 00170 } 00171