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

tdecore

  • tdecore
knotifyclient.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Charles Samuels <charles@altair.dhs.org>
3  2000 Malte Starostik <starosti@zedat.fu-berlin.de>
4  2000,2003 Carsten Pfeiffer <pfeiffer@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "knotifyclient.h"
22 
23 #include <tqdatastream.h>
24 #include <tqptrstack.h>
25 
26 #include <tdeapplication.h>
27 #include <kstandarddirs.h>
28 #include <tdeapplication.h>
29 #include <tdeconfig.h>
30 #include <dcopclient.h>
31 #include <kdebug.h>
32 #include <kstaticdeleter.h>
33 
34 #ifdef Q_WS_X11
35 #include <X11/X.h>
36 #include <X11/Xlib.h>
37 #include <X11/XKBlib.h>
38 #include <X11/keysym.h>
39 #include <fixx11h.h>
40 #endif
41 
42 static const char daemonName[] = "knotify";
43 
44 static bool canAvoidStartupEvent( const TQString& event, const TQString& appname, int present )
45 {
46  static bool checkAvoid = true;
47  if( !checkAvoid )
48  return false;
49  if(( appname != "twin" && appname != "ksmserver" ) || present > 0 ) {
50  checkAvoid = false;
51  return false;
52  }
53  // starttde event is in global events file
54  static TDEConfig* configfile = appname != "ksmserver"
55  ? new TDEConfig( appname + ".eventsrc", true, false )
56  : new TDEConfig( "knotify.eventsrc", true, false );
57  static TDEConfig* eventsfile = appname != "ksmserver"
58  ? new TDEConfig( appname + "/eventsrc", true, false, "data" )
59  : new TDEConfig( "knotify/eventsrc", true, false, "data" );
60  configfile->setGroup( event );
61  eventsfile->setGroup( event );
62  int ev1 = configfile->readNumEntry( "presentation", -2 );
63  int ev2 = eventsfile->readNumEntry( "default_presentation", -2 );
64  if(( ev1 == -2 && ev2 == -2 ) // unknown
65  || ev1 > 0 // configured to have presentation
66  || ( ev1 == -2 && ev2 > 0 )) { // not configured, has default presentation
67  checkAvoid = false;
68  return false;
69  }
70  return true;
71 }
72 
73 static int sendNotifyEvent(const TQString &message, const TQString &text,
74  int present, int level, const TQString &sound,
75  const TQString &file, int winId )
76 {
77  if (!kapp) return 0;
78 
79  // ensure tray icon is shown and positioned before sending event to notification daemon
80 #ifdef Q_WS_X11
81  XFlush(tqt_xdisplay());
82 #endif
83 
84  DCOPClient *client=kapp->dcopClient();
85  if (!client->isAttached())
86  {
87  client->attach();
88  if (!client->isAttached())
89  return 0;
90  }
91 
92  TQString appname = KNotifyClient::instance()->instanceName();
93 
94  if( canAvoidStartupEvent( message, appname, present ))
95  return -1; // done "successfully" - there will be no event presentation
96 
97  int uniqueId = kMax( 1, kapp->random() ); // must not be 0 -- means failure!
98 
99  // knotify daemon needs toplevel window
100  TQWidget* widget = TQT_TQWIDGET(TQWidget::find( (WId)winId ));
101  if( widget )
102  winId = (int)widget->topLevelWidget()->winId();
103 
104  TQByteArray data;
105  TQDataStream ds(data, IO_WriteOnly);
106  ds << message << appname << text << sound << file << present << level
107  << winId << uniqueId;
108 
109  if ( !KNotifyClient::startDaemon() )
110  return 0;
111 
112  if ( client->send(daemonName, "Notify", "notify(TQString,TQString,TQString,TQString,TQString,int,int,int,int)", data) )
113  {
114  return uniqueId;
115  }
116 
117  return 0;
118 }
119 
120 int KNotifyClient::event( StandardEvent type, const TQString& text )
121 {
122  return event( 0, type, text );
123 }
124 
125 int KNotifyClient::event(const TQString &message, const TQString &text)
126 {
127  return event(0, message, text);
128 }
129 
130 int KNotifyClient::userEvent(const TQString &text, int present, int level,
131  const TQString &sound, const TQString &file)
132 {
133  return userEvent( 0, text, present, level, sound, file );
134 }
135 
136 
137 int KNotifyClient::event( int winId, StandardEvent type, const TQString& text )
138 {
139  TQString message;
140  switch ( type ) {
141  case cannotOpenFile:
142  message = TQString::fromLatin1("cannotopenfile");
143  break;
144  case warning:
145  message = TQString::fromLatin1("warning");
146  break;
147  case fatalError:
148  message = TQString::fromLatin1("fatalerror");
149  break;
150  case catastrophe:
151  message = TQString::fromLatin1("catastrophe");
152  break;
153  case notification: // fall through
154  default:
155  message = TQString::fromLatin1("notification");
156  break;
157  }
158 
159  return sendNotifyEvent( message, text, Default, Default,
160  TQString::null, TQString::null, winId );
161 }
162 
163 int KNotifyClient::event(int winId, const TQString &message,
164  const TQString &text)
165 {
166  return sendNotifyEvent(message, text, Default, Default, TQString::null, TQString::null, winId);
167 }
168 
169 int KNotifyClient::userEvent(int winId, const TQString &text, int present,
170  int level,
171  const TQString &sound, const TQString &file)
172 {
173  return sendNotifyEvent(TQString::null, text, present, level, sound, file, winId);
174 }
175 
176 int KNotifyClient::getPresentation(const TQString &eventname)
177 {
178  int present;
179  if (eventname.isEmpty()) return Default;
180 
181  TDEConfig eventsfile( KNotifyClient::instance()->instanceName()+".eventsrc", true, false);
182  eventsfile.setGroup(eventname);
183 
184  present=eventsfile.readNumEntry("presentation", -1);
185 
186  return present;
187 }
188 
189 TQString KNotifyClient::getFile(const TQString &eventname, int present)
190 {
191  if (eventname.isEmpty()) return TQString::null;
192 
193  TDEConfig eventsfile( KNotifyClient::instance()->instanceName()+".eventsrc", true, false);
194  eventsfile.setGroup(eventname);
195 
196  switch (present)
197  {
198  case (Sound):
199  return eventsfile.readPathEntry("soundfile");
200  case (Logfile):
201  return eventsfile.readPathEntry("logfile");
202  }
203 
204  return TQString::null;
205 }
206 
207 int KNotifyClient::getDefaultPresentation(const TQString &eventname)
208 {
209  int present;
210  if (eventname.isEmpty()) return Default;
211 
212  TDEConfig eventsfile( KNotifyClient::instance()->instanceName()+"/eventsrc", true, false, "data");
213  eventsfile.setGroup(eventname);
214 
215  present=eventsfile.readNumEntry("default_presentation", -1);
216 
217  return present;
218 }
219 
220 TQString KNotifyClient::getDefaultFile(const TQString &eventname, int present)
221 {
222  if (eventname.isEmpty()) return TQString::null;
223 
224  TDEConfig eventsfile( KNotifyClient::instance()->instanceName()+"/eventsrc", true, false, "data");
225  eventsfile.setGroup(eventname);
226 
227  switch (present)
228  {
229  case (Sound):
230  return eventsfile.readPathEntry("default_sound");
231  case (Logfile):
232  return eventsfile.readPathEntry("default_logfile");
233  }
234 
235  return TQString::null;
236 }
237 
238 bool KNotifyClient::startDaemon()
239 {
240  static bool firstTry = true;
241  if (!kapp->dcopClient()->isApplicationRegistered(daemonName)) {
242  if( firstTry ) {
243  firstTry = false;
244  return TDEApplication::startServiceByDesktopName(daemonName) == 0;
245  }
246  return false;
247  }
248  return true;
249 }
250 
251 
252 void KNotifyClient::beep(const TQString& reason)
253 {
254  if ( !kapp || KNotifyClient::Instance::currentInstance()->useSystemBell() ) {
255  TQApplication::beep();
256  return;
257  }
258 
259  DCOPClient *client=kapp->dcopClient();
260  if (!client->isAttached())
261  {
262  client->attach();
263  if (!client->isAttached() || !client->isApplicationRegistered(daemonName))
264  {
265  TQApplication::beep();
266  return;
267  }
268  }
269  // The kaccess daemon handles visual and other audible beeps
270  if ( client->isApplicationRegistered( "kaccess" ) )
271  {
272  TQApplication::beep();
273  return;
274  }
275 
276  KNotifyClient::event(KNotifyClient::notification, reason);
277 }
278 
279 
280 TDEInstance * KNotifyClient::instance() {
281  return KNotifyClient::Instance::current();
282 }
283 
284 
285 class KNotifyClient::InstanceStack
286 {
287 public:
288  InstanceStack() { m_defaultInstance = 0; }
289  virtual ~InstanceStack() { delete m_defaultInstance; }
290  void push(Instance *instance) { m_instances.push(instance); }
291 
292  void pop(Instance *instance)
293  {
294  if (m_instances.top() == instance)
295  m_instances.pop();
296  else if (!m_instances.isEmpty())
297  {
298  kdWarning(160) << "Tried to remove an Instance that is not the current," << endl;
299  kdWarning(160) << "Resetting to the main TDEApplication." << endl;
300  m_instances.clear();
301  }
302  else
303  kdWarning(160) << "Tried to remove an Instance, but the stack was empty." << endl;
304  }
305 
306  Instance *currentInstance()
307  {
308  if (m_instances.isEmpty())
309  {
310  m_defaultInstance = new Instance(kapp);
311  }
312  return m_instances.top();
313  }
314 
315 private:
316  TQPtrStack<Instance> m_instances;
317  Instance *m_defaultInstance;
318 };
319 
320 KNotifyClient::InstanceStack * KNotifyClient::Instance::s_instances = 0L;
321 static KStaticDeleter<KNotifyClient::InstanceStack > instancesDeleter;
322 
323 struct KNotifyClient::InstancePrivate
324 {
325  TDEInstance *instance;
326  bool useSystemBell;
327 };
328 
329 KNotifyClient::Instance::Instance(TDEInstance *instance)
330 {
331  d = new InstancePrivate;
332  d->instance = instance;
333  instances()->push(this);
334 
335  TDEConfig *config = instance->config();
336  TDEConfigGroupSaver cs( config, "General" );
337  d->useSystemBell = config->readBoolEntry( "UseSystemBell", false );
338 }
339 
340 KNotifyClient::Instance::~Instance()
341 {
342  if (s_instances)
343  s_instances->pop(this);
344  delete d;
345 }
346 
347 KNotifyClient::InstanceStack *KNotifyClient::Instance::instances()
348 {
349  if (!s_instances)
350  instancesDeleter.setObject(s_instances, new InstanceStack);
351  return s_instances;
352 }
353 
354 bool KNotifyClient::Instance::useSystemBell() const
355 {
356  return d->useSystemBell;
357 }
358 
359 
360 // static methods
361 
362 // We always return a valid KNotifyClient::Instance here. If no special one
363 // is available, we have a default-instance with kapp as TDEInstance.
364 // We make sure to always have that default-instance in the stack, because
365 // the stack might have gotten cleared in the destructor.
366 // We can't use QPtrStack::setAutoDelete( true ), because no instance besides
367 // our default instance is owned by us.
368 KNotifyClient::Instance * KNotifyClient::Instance::currentInstance()
369 {
370  return instances()->currentInstance();
371 }
372 
373 TDEInstance *KNotifyClient::Instance::current()
374 {
375  return currentInstance()->d->instance;
376 }
TDEConfig
Access KDE Configuration entries.
Definition: tdeconfig.h:43
KNotifyClient::event
int event(const TQString &message, const TQString &text=TQString::null) KDE_DEPRECATED
Definition: knotifyclient.cpp:125
KNotifyClient::Instance::current
static TDEInstance * current()
Returns the currently active TDEInstance.
Definition: knotifyclient.cpp:373
KNotifyClient::startDaemon
bool startDaemon()
This starts the KNotify Daemon, if it's not already started.
Definition: knotifyclient.cpp:238
DCOPClient::isApplicationRegistered
bool isApplicationRegistered(const TQCString &remApp)
KNotifyClient::beep
void beep(const TQString &reason=TQString::null)
This is a simple substitution for TQApplication::beep().
Definition: knotifyclient.cpp:252
KNotifyClient::Instance::Instance
Instance(TDEInstance *instance)
Constructs a KNotifyClient::Instance to make KNotifyClient use the specified TDEInstance for the even...
Definition: knotifyclient.cpp:329
TDEConfigGroupSaver
Helper class to facilitate working with TDEConfig / KSimpleConfig groups.
Definition: tdeconfigbase.h:2082
KNotifyClient::Instance::~Instance
~Instance()
Destructs the KNotifyClient::Instance and resets KNotifyClient to the previously used TDEInstance...
Definition: knotifyclient.cpp:340
TDEConfigBase::setGroup
void setGroup(const TQString &group)
Specifies the group in which keys will be read and written.
Definition: tdeconfigbase.cpp:79
DCOPClient::attach
bool attach()
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
KNotifyClient::userEvent
int userEvent(const TQString &text=TQString::null, int present=Default, int level=Default, const TQString &sound=TQString::null, const TQString &file=TQString::null) KDE_DEPRECATED
Definition: knotifyclient.cpp:130
DCOPClient::send
bool send(const TQCString &remApp, const TQCString &remObj, const TQCString &remFun, const TQByteArray &data)
DCOPClient
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to TDEGlobal.
Definition: kstaticdeleter.h:85
TDEApplication::startServiceByDesktopName
static int startServiceByDesktopName(const TQString &_name, const TQString &URL, TQString *error=0, TQCString *dcopService=0, int *pid=0, const TQCString &startup_id="", bool noWait=false)
Starts a service based on the desktop name of the service.
Definition: tdeapplication.cpp:3186
KNotifyClient::instance
TDEInstance * instance()
Shortcut to KNotifyClient::Instance::current() :)
Definition: knotifyclient.cpp:280
KNotifyClient::getDefaultPresentation
int getDefaultPresentation(const TQString &eventname)
Gets the default presentation for the event of this program.
Definition: knotifyclient.cpp:207
KNotifyClient::Instance
Makes it possible to use KNotifyClient with a TDEInstance that is not the application.
Definition: knotifyclient.h:96
KNotifyClient::getFile
TQString getFile(const TQString &eventname, int present)
Gets the default file associated with a certain event name The control panel module will list all the...
Definition: knotifyclient.cpp:189
TDEConfigBase::readBoolEntry
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
Reads a boolean entry.
Definition: tdeconfigbase.cpp:772
KNotifyClient::getPresentation
int getPresentation(const TQString &eventname)
Gets the presentation associated with a certain event name Remeber that they may be ORed: ...
Definition: knotifyclient.cpp:176
KNotifyClient::StandardEvent
StandardEvent
default events you can use
Definition: knotifyclient.h:163
TDEInstance::instanceName
TQCString instanceName() const
Returns the name of the instance.
Definition: kinstance.cpp:320
KNotifyClient::getDefaultFile
TQString getDefaultFile(const TQString &eventname, int present)
Gets the default File for the event of this program.
Definition: knotifyclient.cpp:220
KNotifyClient::Instance::useSystemBell
bool useSystemBell() const
Checks whether the system bell should be used.
Definition: knotifyclient.cpp:354
TDEInstance::config
TDEConfig * config() const
Returns the general config object ("appnamerc").
Definition: kinstance.cpp:210
DCOPClient::isAttached
bool isAttached() const
TDEConfigBase::readPathEntry
TQString readPathEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
Reads a path.
Definition: tdeconfigbase.cpp:609
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583
KNotifyClient::Instance::currentInstance
static Instance * currentInstance()
Returns the current KNotifyClient::Instance (not the TDEInstance).
Definition: knotifyclient.cpp:368
TDEInstance
Access to KDE global objects for use in shared libraries.
Definition: kinstance.h:47
TDEConfigBase::readNumEntry
int readNumEntry(const TQString &pKey, int nDefault=0) const
Reads a numerical value.
Definition: tdeconfigbase.cpp:637

tdecore

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

tdecore

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