summaryrefslogtreecommitdiffstats
path: root/kded/tde-menu.cpp
blob: 4cf7a843e2d1a91d9d46c5163fe44fe06b4a2b48 (plain)
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
/*  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 <tqfile.h>

#include <dcopclient.h>
#include <dcopref.h>

#include "tdeaboutdata.h"
#include "tdeapplication.h"
#include "tdecmdlineargs.h"
#include "tdeglobal.h"
#include "tdelocale.h"
#include "kservice.h"
#include "kservicegroup.h"
#include "kstandarddirs.h"

static TDECmdLineOptions options[] = {
   { "utf8", I18N_NOOP("Output data in UTF-8 instead of local encoding"), 0 },
   { "print-menu-id", I18N_NOOP("Print menu-id of the menu that contains\nthe application"), 0 },
   { "print-menu-name", I18N_NOOP("Print menu name (caption) of the menu that\ncontains the application"), 0 },
   { "highlight", I18N_NOOP("Highlight the entry in the menu"), 0 },
   { "nocache-update", I18N_NOOP("Do not check if sycoca database is up to date"), 0 },
   { "+<application-id>", I18N_NOOP("The id of the menu entry to locate"), 0 },
   TDECmdLineLastOption
};

static const char appName[] = "tde-menu";
static const char appVersion[] = "1.0";
static bool utf8;

static bool bPrintMenuId;
static bool bPrintMenuName;
static bool bHighlight;

static void result(const TQString &txt)
{
   if (utf8)
      puts( txt.utf8() );
   else
      puts( txt.local8Bit() );
}

static void error(int exitCode, const TQString &txt)
{
   tqWarning("tde-menu: %s", txt.local8Bit().data());
   exit(exitCode);
}

static void findMenuEntry(KServiceGroup::Ptr parent, const TQString &name, const TQString &menuId)
{
   KServiceGroup::List list = parent->entries(true, true, false);
   KServiceGroup::List::ConstIterator it = list.begin();
   for (; it != list.end(); ++it)
   {
      KSycocaEntry * e = *it;

      if (e->isType(KST_KServiceGroup))
      {
         KServiceGroup::Ptr g(static_cast<KServiceGroup *>(e));
         
         findMenuEntry(g, name.isEmpty() ? g->caption() : name+"/"+g->caption(), menuId);
      }
      else if (e->isType(KST_KService))
      {
         KService::Ptr s(static_cast<KService *>(e));
         if (s->menuId() == menuId)
         {
            if (bPrintMenuId)
            {
               result(parent->relPath());
            }
            if (bPrintMenuName)
            {
               result(name);
            }
            if (bHighlight)
            {
               DCOPRef kicker( "kicker", "kicker" );
               bool result = kicker.call( "highlightMenuItem", menuId );
               if (!result)
                  error(3, TQString(i18n("Menu item '%1' could not be highlighted.").arg(menuId)).local8Bit());
            }
            exit(0);
         }
      }
   }
}


int main(int argc, char **argv)
{
   TDELocale::setMainCatalogue("tdelibs");
   const char *description = I18N_NOOP("TDE 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 TDE menu a specific application is located.");
   
   TDEAboutData d(appName, I18N_NOOP("tde-menu"), appVersion,
                description,
                TDEAboutData::License_GPL, "(c) 2003 Waldo Bastian");
   d.addAuthor("Waldo Bastian", I18N_NOOP("Author"), "bastian@kde.org");

   TDECmdLineArgs::init(argc, argv, &d);
   TDECmdLineArgs::addCmdLineOptions(options);

//   TDEApplication k(false, false);
   TDEApplication k(false);
   k.disableSessionManagement();

   // this program is in tdelibs so it uses tdelibs as catalog
   TDELocale::setMainCatalogue("tdelibs");

   TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
   if (args->count() != 1)
      TDECmdLineArgs::usage(i18n("You must specify an application-id such as 'tde-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)
      TDECmdLineArgs::usage(i18n("You must specify at least one of --print-menu-id, --print-menu-name or --highlight"));

   if (args->isSet("cache-update"))
   {
      TQStringList args;
      args.append("--incremental");
      args.append("--checkstamps");
      TQString command = "tdebuildsycoca";
      TQCString _launcher = TDEApplication::launcher();
      if (!DCOPRef(_launcher, _launcher).call("tdeinit_exec_wait", command, args).isValid())
      {
         tqWarning("Can't talk to tdelauncher!");
         command = TDEGlobal::dirs()->findExe(command);
         command += " " + args.join(" ");
         system(command.local8Bit());
      }
   }

   TQString menuId = TQFile::decodeName(args->arg(0));
   KService::Ptr s = KService::serviceByMenuId(menuId);
   
   if (!s)
      error(1, i18n("No menu item '%1'.").arg(menuId));

   findMenuEntry(KServiceGroup::root(), "", menuId);

   error(2, i18n("Menu item '%1' not found in menu.").arg(menuId));
   return 2;
}