TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tde-menu.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* This file is part of the KDE libraries
  2. * Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License version 2 as published by the Free Software Foundation;
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public License
  14. * along with this library; see the file COPYING.LIB. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301, USA.
  17. **/
  18. #include <stdlib.h>
  19. #include <tqfile.h>
  20. #include <dcopclient.h>
  21. #include <dcopref.h>
  22. #include "tdeaboutdata.h"
  23. #include "tdeapplication.h"
  24. #include "tdecmdlineargs.h"
  25. #include "tdeglobal.h"
  26. #include "tdelocale.h"
  27. #include "kservice.h"
  28. #include "kservicegroup.h"
  29. #include "kstandarddirs.h"
  30. static TDECmdLineOptions options[] = {
  31. { "utf8", I18N_NOOP("Output data in UTF-8 instead of local encoding"), 0 },
  32. { "print-menu-id", I18N_NOOP("Print menu-id of the menu that contains\nthe application"), 0 },
  33. { "print-menu-name", I18N_NOOP("Print menu name (caption) of the menu that\ncontains the application"), 0 },
  34. { "highlight", I18N_NOOP("Highlight the entry in the menu"), 0 },
  35. { "nocache-update", I18N_NOOP("Do not check if sycoca database is up to date"), 0 },
  36. { "+<application-id>", I18N_NOOP("The id of the menu entry to locate"), 0 },
  37. TDECmdLineLastOption
  38. };
  39. static const char appName[] = "tde-menu";
  40. static const char appVersion[] = "1.0";
  41. static bool utf8;
  42. static bool bPrintMenuId;
  43. static bool bPrintMenuName;
  44. static bool bHighlight;
  45. static void result(const TQString &txt)
  46. {
  47. if (utf8)
  48. puts( txt.utf8() );
  49. else
  50. puts( txt.local8Bit() );
  51. }
  52. static void error(int exitCode, const TQString &txt)
  53. {
  54. tqWarning("tde-menu: %s", txt.local8Bit().data());
  55. exit(exitCode);
  56. }
  57. static void findMenuEntry(KServiceGroup::Ptr parent, const TQString &name, const TQString &menuId)
  58. {
  59. KServiceGroup::List list = parent->entries(true, true, false);
  60. KServiceGroup::List::ConstIterator it = list.begin();
  61. for (; it != list.end(); ++it)
  62. {
  63. KSycocaEntry * e = *it;
  64. if (e->isType(KST_KServiceGroup))
  65. {
  66. KServiceGroup::Ptr g(static_cast<KServiceGroup *>(e));
  67. findMenuEntry(g, name.isEmpty() ? g->caption() : name+"/"+g->caption(), menuId);
  68. }
  69. else if (e->isType(KST_KService))
  70. {
  71. KService::Ptr s(static_cast<KService *>(e));
  72. if (s->menuId() == menuId)
  73. {
  74. if (bPrintMenuId)
  75. {
  76. result(parent->relPath());
  77. }
  78. if (bPrintMenuName)
  79. {
  80. result(name);
  81. }
  82. if (bHighlight)
  83. {
  84. DCOPRef kicker( "kicker", "kicker" );
  85. bool result = kicker.call( "highlightMenuItem", menuId );
  86. if (!result)
  87. error(3, TQString(i18n("Menu item '%1' could not be highlighted.").arg(menuId)).local8Bit());
  88. }
  89. exit(0);
  90. }
  91. }
  92. }
  93. }
  94. int main(int argc, char **argv)
  95. {
  96. TDELocale::setMainCatalogue("tdelibs");
  97. const char *description = I18N_NOOP("TDE Menu query tool.\n"
  98. "This tool can be used to find in which menu a specific application is shown.\n"
  99. "The --highlight option can be used to visually indicate to the user where\n"
  100. "in the TDE menu a specific application is located.");
  101. TDEAboutData d(appName, I18N_NOOP("tde-menu"), appVersion,
  102. description,
  103. TDEAboutData::License_GPL, "(c) 2003 Waldo Bastian");
  104. d.addAuthor("Waldo Bastian", I18N_NOOP("Author"), "bastian@kde.org");
  105. TDECmdLineArgs::init(argc, argv, &d);
  106. TDECmdLineArgs::addCmdLineOptions(options);
  107. // TDEApplication k(false, false);
  108. TDEApplication k(false);
  109. k.disableSessionManagement();
  110. // this program is in tdelibs so it uses tdelibs as catalog
  111. TDELocale::setMainCatalogue("tdelibs");
  112. TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
  113. if (args->count() != 1)
  114. TDECmdLineArgs::usage(i18n("You must specify an application-id such as 'tde-konsole.desktop'"));
  115. utf8 = args->isSet("utf8");
  116. bPrintMenuId = args->isSet("print-menu-id");
  117. bPrintMenuName = args->isSet("print-menu-name");
  118. bHighlight = args->isSet("highlight");
  119. if (!bPrintMenuId && !bPrintMenuName && !bHighlight)
  120. TDECmdLineArgs::usage(i18n("You must specify at least one of --print-menu-id, --print-menu-name or --highlight"));
  121. if (args->isSet("cache-update"))
  122. {
  123. TQStringList args;
  124. args.append("--incremental");
  125. args.append("--checkstamps");
  126. TQString command = "tdebuildsycoca";
  127. TQCString _launcher = TDEApplication::launcher();
  128. if (!DCOPRef(_launcher, _launcher).call("tdeinit_exec_wait", command, args).isValid())
  129. {
  130. tqWarning("Can't talk to tdelauncher!");
  131. command = TDEGlobal::dirs()->findExe(command);
  132. command += " " + args.join(" ");
  133. system(command.local8Bit());
  134. }
  135. }
  136. TQString menuId = TQFile::decodeName(args->arg(0));
  137. KService::Ptr s = KService::serviceByMenuId(menuId);
  138. if (!s)
  139. error(1, i18n("No menu item '%1'.").arg(menuId));
  140. findMenuEntry(KServiceGroup::root(), "", menuId);
  141. error(2, i18n("Menu item '%1' not found in menu.").arg(menuId));
  142. return 2;
  143. }