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

kdecore

  • kdecore
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 <kapplication.h>
27 #include <kstandarddirs.h>
28 #include <kapplication.h>
29 #include <kconfig.h>
30 #include <dcopclient.h>
31 #include <kdebug.h>
32 #include <kstaticdeleter.h>
33 
34 static const char daemonName[] = "knotify";
35 
36 static bool canAvoidStartupEvent( const TQString& event, const TQString& appname, int present )
37 {
38  static bool checkAvoid = true;
39  if( !checkAvoid )
40  return false;
41  if(( appname != "kwin" && appname != "ksmserver" ) || present > 0 ) {
42  checkAvoid = false;
43  return false;
44  }
45  // startkde event is in global events file
46  static KConfig* configfile = appname != "ksmserver"
47  ? new KConfig( appname + ".eventsrc", true, false )
48  : new KConfig( "knotify.eventsrc", true, false );
49  static KConfig* eventsfile = appname != "ksmserver"
50  ? new KConfig( appname + "/eventsrc", true, false, "data" )
51  : new KConfig( "knotify/eventsrc", true, false, "data" );
52  configfile->setGroup( event );
53  eventsfile->setGroup( event );
54  int ev1 = configfile->readNumEntry( "presentation", -2 );
55  int ev2 = eventsfile->readNumEntry( "default_presentation", -2 );
56  if(( ev1 == -2 && ev2 == -2 ) // unknown
57  || ev1 > 0 // configured to have presentation
58  || ( ev1 == -2 && ev2 > 0 )) { // not configured, has default presentation
59  checkAvoid = false;
60  return false;
61  }
62  return true;
63 }
64 
65 static int sendNotifyEvent(const TQString &message, const TQString &text,
66  int present, int level, const TQString &sound,
67  const TQString &file, int winId )
68 {
69  if (!kapp) return 0;
70 
71  DCOPClient *client=kapp->dcopClient();
72  if (!client->isAttached())
73  {
74  client->attach();
75  if (!client->isAttached())
76  return 0;
77  }
78 
79  TQString appname = KNotifyClient::instance()->instanceName();
80 
81  if( canAvoidStartupEvent( message, appname, present ))
82  return -1; // done "successfully" - there will be no event presentation
83 
84  int uniqueId = kMax( 1, kapp->random() ); // must not be 0 -- means failure!
85 
86  // knotify daemon needs toplevel window
87  TQWidget* widget = TQT_TQWIDGET(TQWidget::find( (WId)winId ));
88  if( widget )
89  winId = (int)widget->topLevelWidget()->winId();
90 
91  TQByteArray data;
92  TQDataStream ds(data, IO_WriteOnly);
93  ds << message << appname << text << sound << file << present << level
94  << winId << uniqueId;
95 
96  if ( !KNotifyClient::startDaemon() )
97  return 0;
98 
99  if ( client->send(daemonName, "Notify", "notify(TQString,TQString,TQString,TQString,TQString,int,int,int,int)", data) )
100  {
101  return uniqueId;
102  }
103 
104  return 0;
105 }
106 
107 int KNotifyClient::event( StandardEvent type, const TQString& text )
108 {
109  return event( 0, type, text );
110 }
111 
112 int KNotifyClient::event(const TQString &message, const TQString &text)
113 {
114  return event(0, message, text);
115 }
116 
117 int KNotifyClient::userEvent(const TQString &text, int present, int level,
118  const TQString &sound, const TQString &file)
119 {
120  return userEvent( 0, text, present, level, sound, file );
121 }
122 
123 
124 int KNotifyClient::event( int winId, StandardEvent type, const TQString& text )
125 {
126  TQString message;
127  switch ( type ) {
128  case cannotOpenFile:
129  message = TQString::fromLatin1("cannotopenfile");
130  break;
131  case warning:
132  message = TQString::fromLatin1("warning");
133  break;
134  case fatalError:
135  message = TQString::fromLatin1("fatalerror");
136  break;
137  case catastrophe:
138  message = TQString::fromLatin1("catastrophe");
139  break;
140  case notification: // fall through
141  default:
142  message = TQString::fromLatin1("notification");
143  break;
144  }
145 
146  return sendNotifyEvent( message, text, Default, Default,
147  TQString::null, TQString::null, winId );
148 }
149 
150 int KNotifyClient::event(int winId, const TQString &message,
151  const TQString &text)
152 {
153  return sendNotifyEvent(message, text, Default, Default, TQString::null, TQString::null, winId);
154 }
155 
156 int KNotifyClient::userEvent(int winId, const TQString &text, int present,
157  int level,
158  const TQString &sound, const TQString &file)
159 {
160  return sendNotifyEvent(TQString::null, text, present, level, sound, file, winId);
161 }
162 
163 int KNotifyClient::getPresentation(const TQString &eventname)
164 {
165  int present;
166  if (eventname.isEmpty()) return Default;
167 
168  KConfig eventsfile( KNotifyClient::instance()->instanceName()+".eventsrc", true, false);
169  eventsfile.setGroup(eventname);
170 
171  present=eventsfile.readNumEntry("presentation", -1);
172 
173  return present;
174 }
175 
176 TQString KNotifyClient::getFile(const TQString &eventname, int present)
177 {
178  if (eventname.isEmpty()) return TQString::null;
179 
180  KConfig eventsfile( KNotifyClient::instance()->instanceName()+".eventsrc", true, false);
181  eventsfile.setGroup(eventname);
182 
183  switch (present)
184  {
185  case (Sound):
186  return eventsfile.readPathEntry("soundfile");
187  case (Logfile):
188  return eventsfile.readPathEntry("logfile");
189  }
190 
191  return TQString::null;
192 }
193 
194 int KNotifyClient::getDefaultPresentation(const TQString &eventname)
195 {
196  int present;
197  if (eventname.isEmpty()) return Default;
198 
199  KConfig eventsfile( KNotifyClient::instance()->instanceName()+"/eventsrc", true, false, "data");
200  eventsfile.setGroup(eventname);
201 
202  present=eventsfile.readNumEntry("default_presentation", -1);
203 
204  return present;
205 }
206 
207 TQString KNotifyClient::getDefaultFile(const TQString &eventname, int present)
208 {
209  if (eventname.isEmpty()) return TQString::null;
210 
211  KConfig eventsfile( KNotifyClient::instance()->instanceName()+"/eventsrc", true, false, "data");
212  eventsfile.setGroup(eventname);
213 
214  switch (present)
215  {
216  case (Sound):
217  return eventsfile.readPathEntry("default_sound");
218  case (Logfile):
219  return eventsfile.readPathEntry("default_logfile");
220  }
221 
222  return TQString::null;
223 }
224 
225 bool KNotifyClient::startDaemon()
226 {
227  static bool firstTry = true;
228  if (!kapp->dcopClient()->isApplicationRegistered(daemonName)) {
229  if( firstTry ) {
230  firstTry = false;
231  return KApplication::startServiceByDesktopName(daemonName) == 0;
232  }
233  return false;
234  }
235  return true;
236 }
237 
238 
239 void KNotifyClient::beep(const TQString& reason)
240 {
241  if ( !kapp || KNotifyClient::Instance::currentInstance()->useSystemBell() ) {
242  TQApplication::beep();
243  return;
244  }
245 
246  DCOPClient *client=kapp->dcopClient();
247  if (!client->isAttached())
248  {
249  client->attach();
250  if (!client->isAttached() || !client->isApplicationRegistered(daemonName))
251  {
252  TQApplication::beep();
253  return;
254  }
255  }
256  // The kaccess daemon handles visual and other audible beeps
257  if ( client->isApplicationRegistered( "kaccess" ) )
258  {
259  TQApplication::beep();
260  return;
261  }
262 
263  KNotifyClient::event(KNotifyClient::notification, reason);
264 }
265 
266 
267 KInstance * KNotifyClient::instance() {
268  return KNotifyClient::Instance::current();
269 }
270 
271 
272 class KNotifyClient::InstanceStack
273 {
274 public:
275  InstanceStack() { m_defaultInstance = 0; }
276  virtual ~InstanceStack() { delete m_defaultInstance; }
277  void push(Instance *instance) { m_instances.push(instance); }
278 
279  void pop(Instance *instance)
280  {
281  if (m_instances.top() == instance)
282  m_instances.pop();
283  else if (!m_instances.isEmpty())
284  {
285  kdWarning(160) << "Tried to remove an Instance that is not the current," << endl;
286  kdWarning(160) << "Resetting to the main KApplication." << endl;
287  m_instances.clear();
288  }
289  else
290  kdWarning(160) << "Tried to remove an Instance, but the stack was empty." << endl;
291  }
292 
293  Instance *currentInstance()
294  {
295  if (m_instances.isEmpty())
296  {
297  m_defaultInstance = new Instance(kapp);
298  }
299  return m_instances.top();
300  }
301 
302 private:
303  TQPtrStack<Instance> m_instances;
304  Instance *m_defaultInstance;
305 };
306 
307 KNotifyClient::InstanceStack * KNotifyClient::Instance::s_instances = 0L;
308 static KStaticDeleter<KNotifyClient::InstanceStack > instancesDeleter;
309 
310 struct KNotifyClient::InstancePrivate
311 {
312  KInstance *instance;
313  bool useSystemBell;
314 };
315 
316 KNotifyClient::Instance::Instance(KInstance *instance)
317 {
318  d = new InstancePrivate;
319  d->instance = instance;
320  instances()->push(this);
321 
322  KConfig *config = instance->config();
323  KConfigGroupSaver cs( config, "General" );
324  d->useSystemBell = config->readBoolEntry( "UseSystemBell", false );
325 }
326 
327 KNotifyClient::Instance::~Instance()
328 {
329  if (s_instances)
330  s_instances->pop(this);
331  delete d;
332 }
333 
334 KNotifyClient::InstanceStack *KNotifyClient::Instance::instances()
335 {
336  if (!s_instances)
337  instancesDeleter.setObject(s_instances, new InstanceStack);
338  return s_instances;
339 }
340 
341 bool KNotifyClient::Instance::useSystemBell() const
342 {
343  return d->useSystemBell;
344 }
345 
346 
347 // static methods
348 
349 // We always return a valid KNotifyClient::Instance here. If no special one
350 // is available, we have a default-instance with kapp as KInstance.
351 // We make sure to always have that default-instance in the stack, because
352 // the stack might have gotten cleared in the destructor.
353 // We can't use QStack::setAutoDelete( true ), because no instance besides
354 // our default instance is owned by us.
355 KNotifyClient::Instance * KNotifyClient::Instance::currentInstance()
356 {
357  return instances()->currentInstance();
358 }
359 
360 KInstance *KNotifyClient::Instance::current()
361 {
362  return currentInstance()->d->instance;
363 }
DCOPClient
DCOPClient::isApplicationRegistered
bool isApplicationRegistered(const TQCString &remApp)
DCOPClient::attach
bool attach()
DCOPClient::send
bool send(const TQCString &remApp, const TQCString &remObj, const TQCString &remFun, const TQByteArray &data)
DCOPClient::isAttached
bool isAttached() const
KApplication::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: kapplication.cpp:3122
KConfigBase::readPathEntry
TQString readPathEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
Reads a path.
Definition: kconfigbase.cpp:608
KConfigBase::setGroup
void setGroup(const TQString &group)
Specifies the group in which keys will be read and written.
Definition: kconfigbase.cpp:80
KConfigBase::readNumEntry
int readNumEntry(const TQString &pKey, int nDefault=0) const
Reads a numerical value.
Definition: kconfigbase.cpp:636
KConfigBase::readBoolEntry
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
Reads a boolean entry.
Definition: kconfigbase.cpp:771
KConfigGroupSaver
Helper class to facilitate working with KConfig / KSimpleConfig groups.
Definition: kconfigbase.h:2060
KConfig
Access KDE Configuration entries.
Definition: kconfig.h:44
KInstance
Access to KDE global objects for use in shared libraries.
Definition: kinstance.h:44
KInstance::config
KConfig * config() const
Returns the general config object ("appnamerc").
Definition: kinstance.cpp:177
KInstance::instanceName
TQCString instanceName() const
Returns the name of the instance.
Definition: kinstance.cpp:263
KNotifyClient::Instance
Makes it possible to use KNotifyClient with a KInstance that is not the application.
Definition: knotifyclient.h:97
KNotifyClient::Instance::useSystemBell
bool useSystemBell() const
Checks whether the system bell should be used.
Definition: knotifyclient.cpp:341
KNotifyClient::Instance::currentInstance
static Instance * currentInstance()
Returns the current KNotifyClient::Instance (not the KInstance).
Definition: knotifyclient.cpp:355
KNotifyClient::Instance::current
static KInstance * current()
Definition: knotifyclient.cpp:360
KNotifyClient::Instance::Instance
Instance(KInstance *instance)
Definition: knotifyclient.cpp:316
KNotifyClient::Instance::~Instance
~Instance()
Destructs the KNotifyClient::Instance and resets KNotifyClient to the previously used KInstance.
Definition: knotifyclient.cpp:327
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583
KNotifyClient::getDefaultFile
TQString getDefaultFile(const TQString &eventname, int present)
Gets the default File for the event of this program.
Definition: knotifyclient.cpp:207
KNotifyClient::instance
KInstance * instance()
Shortcut to KNotifyClient::Instance::current() :)
Definition: knotifyclient.cpp:267
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:117
KNotifyClient::getDefaultPresentation
int getDefaultPresentation(const TQString &eventname)
Gets the default presentation for the event of this program.
Definition: knotifyclient.cpp:194
KNotifyClient::startDaemon
bool startDaemon()
This starts the KNotify Daemon, if it's not already started.
Definition: knotifyclient.cpp:225
KNotifyClient::event
int event(const TQString &message, const TQString &text=TQString::null) KDE_DEPRECATED
Definition: knotifyclient.cpp:112
KNotifyClient::beep
void beep(const TQString &reason=TQString::null)
This is a simple substitution for TQApplication::beep().
Definition: knotifyclient.cpp:239
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:163
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:176
KNotifyClient::StandardEvent
StandardEvent
default events you can use
Definition: knotifyclient.h:163

kdecore

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

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |