• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • dcop
 

dcop

  • dcop
  • client
dcopfind.cpp
1 /*****************************************************************
2 Copyright (c) 2000 Matthias Ettrich <ettrich@kde.org>
3 Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 ******************************************************************/
23 
24 #include <tqbuffer.h>
25 #include <tqvariant.h>
26 #include <tqcolor.h>
27 #include <tqimage.h>
28 #include "../kdatastream.h"
29 #include "../dcopclient.h"
30 #include "../dcopref.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 
35 #include "marshall.cpp"
36 
37 static DCOPClient* dcop = 0;
38 static bool bAppIdOnly = 0;
39 static bool bLaunchApp = 0;
40 
41 bool findObject( const char* app, const char* obj, const char* func, QCStringList args )
42 {
43  TQString f = func; // Qt is better with unicode strings, so use one.
44  int left = f.find( '(' );
45  int right = f.find( ')' );
46 
47  if ( right < left )
48  {
49  tqWarning( "parentheses do not match" );
50  exit(1);
51  }
52 
53  if ( !f.isEmpty() && (left < 0) )
54  f += "()";
55 
56  // This may seem expensive but is done only once per invocation
57  // of dcop, so it should be OK.
58  //
59  //
60  TQStringList intTypes;
61  intTypes << "int" << "unsigned" << "long" << "bool" ;
62 
63  TQStringList types;
64  if ( left >0 && left + 1 < right - 1) {
65  types = TQStringList::split( ',', f.mid( left + 1, right - left - 1) );
66  for ( TQStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
67  TQString lt = (*it).simplifyWhiteSpace();
68 
69  int s = lt.find(' ');
70 
71  // If there are spaces in the name, there may be two
72  // reasons: the parameter name is still there, ie.
73  // "TQString URL" or it's a complicated int type, ie.
74  // "unsigned long long int bool".
75  //
76  //
77  if ( s > 0 )
78  {
79  TQStringList partl = TQStringList::split(' ' , lt);
80 
81  // The zero'th part is -- at the very least -- a
82  // type part. Any trailing parts *might* be extra
83  // int-type keywords, or at most one may be the
84  // parameter name.
85  //
86  //
87  s=1;
88 
89  while (s < (int)partl.count() && intTypes.contains(partl[s]))
90  {
91  s++;
92  }
93 
94  if (s<(int)partl.count()-1)
95  {
96  tqWarning("The argument `%s' seems syntactically wrong.",
97  lt.latin1());
98  }
99  if (s==(int)partl.count()-1)
100  {
101  partl.remove(partl.at(s));
102  }
103 
104  lt = partl.join(" ");
105  lt = lt.simplifyWhiteSpace();
106  }
107 
108  (*it) = lt;
109  }
110  TQString fc = f.left( left );
111  fc += '(';
112  bool first = true;
113  for ( TQStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
114  if ( !first )
115  fc +=",";
116  first = false;
117  fc += *it;
118  }
119  fc += ')';
120  f = fc;
121  }
122 
123  if ( types.count() != args.count() ) {
124  tqWarning( "arguments do not match" );
125  exit(1);
126  }
127 
128  TQByteArray data;
129  TQDataStream arg(data, IO_WriteOnly);
130 
131  uint i = 0;
132  for ( TQStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
133  marshall(arg, args, i, *it);
134  }
135  if ( (uint) i != args.count() ) {
136  tqWarning( "arguments do not match" );
137  exit(1);
138  }
139 
140  TQCString foundApp;
141  TQCString foundObj;
142  if ( dcop->findObject( app, obj, f.latin1(), data, foundApp, foundObj) )
143  {
144  if (bAppIdOnly)
145  puts(foundApp.data());
146  else
147  printf("DCOPRef(%s,%s)\n", qStringToC(foundApp), qStringToC(foundObj));
148  return true;
149  }
150  return false;
151 }
152 
153 bool launchApp(TQString app)
154 {
155  int l = app.length();
156  if (l && (app[l-1] == '*'))
157  l--;
158  if (l && (app[l-1] == '-'))
159  l--;
160  if (!l) return false;
161  app.truncate(l);
162 
163  TQStringList URLs;
164  TQByteArray data, replyData;
165  TQCString replyType;
166  TQDataStream arg(data, IO_WriteOnly);
167  arg << app << URLs;
168 
169  if ( !dcop->call( "tdelauncher", "tdelauncher", "start_service_by_desktop_name(TQString,TQStringList)",
170  data, replyType, replyData) ) {
171  tqWarning( "call to tdelauncher failed.");
172  return false;
173  }
174  TQDataStream reply(replyData, IO_ReadOnly);
175 
176  if ( replyType != "serviceResult" )
177  {
178  tqWarning( "unexpected result '%s' from tdelauncher.", replyType.data());
179  return false;
180  }
181  int result;
182  TQCString dcopName;
183  TQString error;
184  reply >> result >> dcopName >> error;
185  if (result != 0)
186  {
187  tqWarning("Error starting '%s': %s", app.local8Bit().data(), error.local8Bit().data());
188  return false;
189  }
190  return true;
191 }
192 
193 void usage()
194 {
195  fprintf( stderr, "[dcopfind] Usage: dcopfind [-l] [-a] application [object [function [arg1] [arg2] [arg3] ... ] ] ] \n" );
196  exit(0);
197 }
198 
199 #ifdef Q_OS_WIN
200 # define main kdemain
201 #endif
202 
203 int main( int argc, char** argv )
204 {
205  int argi = 1;
206 
207  while ((argi < argc) && (argv[argi][0] == '-'))
208  {
209  switch ( argv[argi][1] ) {
210  case 'l':
211  bLaunchApp = true;
212  break;
213  case 'a':
214  bAppIdOnly = true;
215  break;
216  default:
217  usage();
218  }
219  argi++;
220  }
221 
222  if (argc <= argi)
223  usage();
224 
225  DCOPClient client;
226  client.attach();
227  dcop = &client;
228 
229  TQCString app;
230  TQCString objid;
231  TQCString function;
232  char **args = 0;
233  if ((argc > argi) && (strncmp(argv[argi], "DCOPRef(", 8)) == 0)
234  {
235  char *delim = strchr(argv[argi], ',');
236  if (!delim)
237  {
238  fprintf(stderr, "Error: '%s' is not a valid DCOP reference.\n", argv[argi]);
239  return 1;
240  }
241  *delim = 0;
242  app = argv[argi++] + 8;
243  delim++;
244  delim[strlen(delim)-1] = 0;
245  objid = delim;
246  }
247  else
248  {
249  if (argc > argi)
250  app = argv[argi++];
251  if (argc > argi)
252  objid = argv[argi++];
253  }
254  if (argc > argi)
255  function = argv[argi++];
256 
257  if (argc > argi)
258  {
259  args = &argv[argi];
260  argc = argc-argi;
261  }
262  else
263  {
264  argc = 0;
265  }
266 
267  QCStringList params;
268  for( int i = 0; i < argc; i++ )
269  params.append( args[ i ] );
270  bool ok = findObject( app, objid, function, params );
271  if (ok)
272  return 0;
273  if (bLaunchApp)
274  {
275  ok = launchApp(app);
276  if (!ok)
277  return 2;
278  ok = findObject( app, objid, function, params );
279  }
280 
281  return 1;
282 }
DCOPClient
Inter-process communication and remote procedure calls for KDE applications.
Definition: dcopclient.h:69
DCOPClient::attach
bool attach()
Attaches to the DCOP server.
Definition: dcopclient.cpp:679
DCOPClient::call
bool call(const TQCString &remApp, const TQCString &remObj, const TQCString &remFun, const TQByteArray &data, TQCString &replyType, TQByteArray &replyData, bool useEventLoop, int timeout, bool forceRemote)
Performs a synchronous send and receive.
Definition: dcopclient.cpp:1786
DCOPClient::findObject
bool findObject(const TQCString &remApp, const TQCString &remObj, const TQCString &remFun, const TQByteArray &data, TQCString &foundApp, TQCString &foundObj, bool useEventLoop, int timeout)
Searches for an object which matches a criteria.
Definition: dcopclient.cpp:1160

dcop

Skip menu "dcop"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

dcop

Skip menu "dcop"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for dcop by doxygen 1.9.1
This website is maintained by Timothy Pearson.