1/****************************************************************************
2
3 Copyright (C) 2001-2003 Lubos Lunak <l.lunak@kde.org>
4
5Permission is hereby granted, free of charge, to any person obtaining a
6copy of this software and associated documentation files (the "Software"),
7to deal in the Software without restriction, including without limitation
8the rights to use, copy, modify, merge, publish, distribute, sublicense,
9and/or sell copies of the Software, and to permit persons to whom the
10Software is furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21DEALINGS IN THE SOFTWARE.
22
23****************************************************************************/
24
25#ifndef KSTARTUPINFO_H
26#define KSTARTUPINFO_H
27
28#include <kdeui_export.h>
29
30#include <QtCore/QObject>
31#include <QtCore/QString>
32#include <QtCore/QChildEvent>
33#include <QtGui/QWidgetList>
34
35#include <sys/types.h>
36
37#if !defined(Q_WS_X11)
38typedef void Display;
39#endif
40
41class KStartupInfoId;
42class KStartupInfoData;
43
44/**
45 * Class for manipulating the application startup notification.
46 *
47 * This class can be used to send information about started application,
48 * change the information and receive this information. For detailed
49 * description, see kdelibs/kdecore/README.kstartupinfo.
50 *
51 * You usually don't need to use this class for sending the notification
52 * information, as KDE libraries should do this when an application is
53 * started (e.g. KRun class).
54 *
55 * For receiving the startup notification info, create an instance and connect
56 * to its slots. It will automatically detect started applications and when
57 * they are ready.
58 *
59 * @see KStartupInfoId
60 * @see KStartupInfoData
61 *
62 * @author Lubos Lunak <l.lunak@kde.org>
63 */
64class KDEUI_EXPORT KStartupInfo
65 : public QObject
66 {
67 Q_OBJECT
68 public:
69 /**
70 * By default, the startup notification is ended for the application
71 * after it shows its first toplevel window. If you app e.g. has
72 * several toplevel windows after its startup, you can disable
73 * the automatic handling, and call appStarted() manually after
74 * all toplevel windows have been shown.
75 */
76 static void disableAutoAppStartedSending( bool disable = true );
77
78 /**
79 * Manual notification that the application has started.
80 * If you do not map a (toplevel) window, then startup
81 * notification will not disappear for the application
82 * until a timeout. You can use this as an alternative
83 * method in this case.
84 */
85 static void appStarted();
86
87 /**
88 * Sends explicit notification that the startup notification
89 * with id startup_id should end.
90 */
91 static void appStarted( const QByteArray& startup_id );
92
93 /**
94 * Use this function if the application got a request with startup
95 * notification from outside (for example, when KUniqueApplication::newInstance()
96 * is called, or e.g. when khelpcenter opens new URL in its window).
97 * The window can be either an already existing and visible window,
98 * or a new one, before being shown. Note that this function is usually
99 * needed only when a window is reused.
100 */
101 static void setNewStartupId( QWidget* window, const QByteArray& startup_id );
102
103 /**
104 * If your application shows temporarily some window during its startup,
105 * for example a dialog, and only after closing it shows the main window,
106 * startup notification would normally be shown while the dialog is visible.
107 * To temporarily suspend and resume the notification, use this function.
108 * Note that this is cumulative, i.e. after suspending twice, you have to
109 * resume twice.
110 */
111 static void silenceStartup( bool silence );
112
113 /**
114 * Creates and returns new startup id. The id includes properly setup
115 * user timestamp.
116 */
117 static QByteArray createNewStartupId();
118 /**
119 *
120 */
121 enum {
122 CleanOnCantDetect = 1 << 0,
123 DisableKWinModule = 1 << 1,
124 AnnounceSilenceChanges = 1 << 2
125 };
126
127 /**
128 * Creates an instance that will receive the startup notifications.
129 * The various flags passed may be
130 * @li CleanOnCantDetect - when a new unknown window appears, all startup
131 * notifications for applications that are not compliant with
132 * the startup protocol are removed
133 * @li DisableKWinModule - KWinModule, which is normally used to detect
134 * new windows, is disabled. With this flag, checkStartup() must be
135 * called in order to check newly mapped windows.
136 * @li AnnounceSilenceChanges - normally, startup notifications are
137 * "removed" when they're silenced, and "recreated" when they're resumed.
138 * With this flag, the change is normally announced with gotStartupChange().
139 *
140 * @param flags OR-ed combination of flags
141 * @param parent the parent of this QObject (can be 0 for no parent)
142 *
143 */
144 explicit KStartupInfo( int flags, QObject* parent = 0 );
145 /**
146 * Creates an instance that will receive the startup notifications.
147 *
148 * @param clean_on_cantdetect if true, and a new unknown window appears,
149 * removes all notification for applications that are not compliant
150 * with the app startup protocol
151 * @param parent the parent of this QObject (can be 0 for no parent)
152 *
153 * @obsolete
154 */
155 explicit KStartupInfo( bool clean_on_cantdetect, QObject* parent = 0 );
156 virtual ~KStartupInfo();
157 /**
158 * Sends given notification data about started application
159 * with the given startup identification. If no notification for this identification
160 * exists yet, it is created, otherwise it's updated. Note that the name field
161 * in data is required.
162 *
163 * @param id the id of the application
164 * @param data the application's data
165 * @return true if successful, false otherwise
166 * @see KStartupInfoId
167 * @see KStartupInfoData
168 */
169 static bool sendStartup( const KStartupInfoId& id, const KStartupInfoData& data );
170
171 /**
172 * Like sendStartup , uses dpy instead of qt_x11display() for sending the info.
173 * @param dpy the display of the application. Note that the name field
174 * in data is required.
175 * @param id the id of the application
176 * @param data the application's data
177 * @return true if successful, false otherwise
178 */
179 static bool sendStartupX( Display* dpy, const KStartupInfoId& id,
180 const KStartupInfoData& data );
181
182 /**
183 * Sends given notification data about started application
184 * with the given startup identification. This is used for updating the notification
185 * info, if no notification for this identification exists, it's ignored.
186 * @param id the id of the application
187 * @param data the application's data
188 * @return true if successful, false otherwise
189 * @see KStartupInfoId
190 * @see KStartupInfoData
191 */
192 static bool sendChange( const KStartupInfoId& id, const KStartupInfoData& data );
193
194 /**
195 * Like sendChange , uses dpy instead of qt_x11display() for sending the info.
196 * @param dpy the display of the application.
197 * @param id the id of the application
198 * @param data the application's data
199 * @return true if successful, false otherwise
200 */
201 static bool sendChangeX( Display* dpy, const KStartupInfoId& id,
202 const KStartupInfoData& data );
203
204 /**
205 * Ends startup notification with the given identification.
206 * @param id the id of the application
207 * @return true if successful, false otherwise
208 */
209 static bool sendFinish( const KStartupInfoId& id );
210
211 /**
212 * Like sendFinish , uses dpy instead of qt_x11display() for sending the info.
213 * @param dpy the display of the application.
214 * @param id the id of the application
215 * @return true if successful, false otherwise
216 */
217 static bool sendFinishX( Display* dpy, const KStartupInfoId& id );
218
219 /**
220 * Ends startup notification with the given identification and the given data ( e.g.
221 * PIDs of processes for this startup notification that exited ).
222 * @param id the id of the application
223 * @param data the application's data
224 * @return true if successful, false otherwise
225 */
226 static bool sendFinish( const KStartupInfoId& id, const KStartupInfoData& data );
227
228 /**
229 * Like sendFinish , uses dpy instead of qt_x11display() for sending the info.
230 * @param dpy the display of the application.
231 * @param id the id of the application
232 * @param data the application's data
233 * @return true if successful, false otherwise
234 */
235 static bool sendFinishX( Display* dpy, const KStartupInfoId& id,
236 const KStartupInfoData& data );
237
238 /**
239 * Returns the current startup notification identification for the current
240 * startup notification environment variable. Note that KApplication constructor
241 * unsets the variable and you have to use KApplication::startupId .
242 * @return the current startup notification identification
243 */
244 static KStartupInfoId currentStartupIdEnv();
245 /**
246 * Unsets the startup notification environment variable.
247 */
248 static void resetStartupEnv();
249 /**
250 * @li NoMatch - the window doesn't match any existing startup notification
251 * @li Match - the window matches an existing startup notification
252 * @li CantDetect - unable to detect if the window matches any existing
253 * startup notification
254 */
255 enum startup_t { NoMatch, Match, CantDetect };
256 /**
257 * Checks if the given windows matches any existing startup notification.
258 * @param w the window id to check
259 * @return the result of the operation
260 */
261 startup_t checkStartup( WId w );
262 /**
263 * Checks if the given windows matches any existing startup notification, and
264 * if yes, returns the identification in id.
265 * @param w the window id to check
266 * @param id if found, the id of the startup notification will be written here
267 * @return the result of the operation
268 */
269 startup_t checkStartup( WId w, KStartupInfoId& id );
270 /**
271 * Checks if the given windows matches any existing startup notification, and
272 * if yes, returns the notification data in data.
273 * @param w the window id to check
274 * @param data if found, the data of the startup notification will be written here
275 * @return the result of the operation
276 */
277 startup_t checkStartup( WId w, KStartupInfoData& data );
278 /**
279 * Checks if the given windows matches any existing startup notification, and
280 * if yes, returns the identification in id and notification data in data.
281 * @param w the window id to check
282 * @param id if found, the id of the startup notification will be written here
283 * @param data if found, the data of the startup notification will be written here
284 * @return the result of the operation
285 */
286 startup_t checkStartup( WId w, KStartupInfoId& id, KStartupInfoData& data );
287 /**
288 * Sets the timeout for notifications, after this timeout a notification is removed.
289 * @param secs the new timeout in seconds
290 */
291 void setTimeout( unsigned int secs );
292 /**
293 * Sets the startup notification window property on the given window.
294 * @param window the id of the window
295 * @param id the startup notification id
296 */
297 static void setWindowStartupId( WId window, const QByteArray& id );
298 /**
299 * Returns startup notification identification of the given window.
300 * @param w the id of the window
301 * @return the startup notification id. Can be null if not found.
302 */
303 static QByteArray windowStartupId( WId w );
304 /**
305 * @internal
306 */
307 static void handleAutoAppStartedSending();
308 /**
309 * @internal
310 */
311 class Data;
312
313 /**
314 * @internal
315 */
316 class Private;
317 Q_SIGNALS:
318 /**
319 * Emitted when a new startup notification is created (i.e. a new application is
320 * being started).
321 * @param id the notification identification
322 * @param data the notification data
323 */
324 void gotNewStartup( const KStartupInfoId& id, const KStartupInfoData& data );
325 /**
326 * Emitted when a startup notification changes.
327 * @param id the notification identification
328 * @param data the notification data
329 */
330 void gotStartupChange( const KStartupInfoId& id, const KStartupInfoData& data );
331 /**
332 * Emitted when a startup notification is removed (either because it was detected
333 * that the application is ready or because of a timeout).
334 * @param id the notification identification
335 * @param data the notification data
336 */
337 void gotRemoveStartup( const KStartupInfoId& id, const KStartupInfoData& data );
338
339 protected:
340 /**
341 *
342 */
343 virtual void customEvent( QEvent* e_P );
344
345 private:
346 Q_PRIVATE_SLOT(d, void startups_cleanup())
347 Q_PRIVATE_SLOT(d, void startups_cleanup_no_age())
348 Q_PRIVATE_SLOT(d, void got_message( const QString& msg ))
349 Q_PRIVATE_SLOT(d, void window_added( WId w ))
350 Q_PRIVATE_SLOT(d, void slot_window_added( WId w ))
351
352 Private* const d;
353
354 Q_DISABLE_COPY(KStartupInfo)
355 };
356
357/**
358 * Class representing an identification of application startup notification.
359 *
360 * Every existing notification about a starting application has its own unique
361 * identification, that's used to identify and manipulate the notification.
362 *
363 * @see KStartupInfo
364 * @see KStartupInfoData
365 *
366 * @author Lubos Lunak <l.lunak@kde.org>
367 */
368class KDEUI_EXPORT KStartupInfoId
369 {
370 public:
371 /**
372 * Overloaded operator.
373 * @return true if the notification identifications are the same
374 */
375 bool operator==( const KStartupInfoId& id ) const;
376 /**
377 * Overloaded operator.
378 * @return true if the notification identifications are different
379 */
380 bool operator!=( const KStartupInfoId& id ) const;
381 /**
382 * Checks whether the identifier is valid.
383 * @return true if this object doesn't represent a valid notification identification
384 */
385 bool none() const;
386 /**
387 * Initializes this object with the given identification ( which may be also "0"
388 * for no notification ), or if "" is given, tries to read it from the startup
389 * notification environment variable, and if it's not set, creates a new one.
390 * @param id the new identification, "0" for no notification or "" to read
391 * the environment variable
392 */
393 void initId( const QByteArray& id = "" );
394 /**
395 * Returns the notification identifier as string.
396 * @return the identification string for the notification
397 */
398 const QByteArray& id() const;
399 /**
400 * Return the user timestamp for the startup notification, or 0 if no timestamp
401 * is set.
402 */
403 unsigned long timestamp() const;
404 /**
405 * Sets the startup notification environment variable to this identification.
406 * @return true if successful, false otherwise
407 */
408 bool setupStartupEnv() const;
409 /**
410 * Creates an empty identification
411 */
412 KStartupInfoId();
413 /**
414 * Copy constructor.
415 */
416 KStartupInfoId( const KStartupInfoId& data );
417 ~KStartupInfoId();
418 KStartupInfoId& operator=( const KStartupInfoId& data );
419 bool operator<( const KStartupInfoId& id ) const;
420 private:
421 explicit KStartupInfoId( const QString& txt );
422 friend class KStartupInfo;
423 friend class KStartupInfo::Private;
424 struct Private;
425 Private *const d;
426 };
427
428/**
429 * Class representing data about an application startup notification.
430 *
431 * Such data include the icon of the starting application, the desktop on which
432 * the application should start, the binary name of the application, etc.
433 *
434 * @see KStartupInfo
435 * @see KStartupInfoId
436 *
437 * @author Lubos Lunak <l.lunak@kde.org>
438 */
439class KDEUI_EXPORT KStartupInfoData
440 {
441 public:
442 /**
443 * Sets the binary name of the application ( e.g. 'kcontrol' ).
444 * @param bin the new binary name of the application
445 */
446 void setBin( const QString& bin );
447 /**
448 * Returns the binary name of the starting application
449 * @return the new binary name of the application
450 */
451 const QString& bin() const;
452 /**
453 * Sets the name for the notification (e.g. 'Control Center')
454 */
455 void setName( const QString& name );
456 /**
457 * Returns the name of the startup notification. If it's not available,
458 * it tries to use other information (binary name).
459 * @return the name of the startup notification
460 */
461 const QString& findName() const;
462 /**
463 * Returns the name of the startup notification, or empty if not available.
464 * @return the name of the startup notification, or an empty string
465 * if not set.
466 */
467 const QString& name() const;
468 /**
469 * Sets the description for the notification (e.g. 'Launching Control Center').
470 * I.e. name() describes what is being started, while description() is
471 * the actual action performed by the starting.
472 */
473 void setDescription( const QString& descr );
474 /**
475 * Returns the description of the startup notification. If it's not available,
476 * it returns name().
477 * @return the description of the startup notification
478 */
479 const QString& findDescription() const;
480 /**
481 * Returns the name of the startup notification, or empty if not available.
482 * @return the name of the startup notificaiton, or an empty string
483 * if not set.
484 */
485 const QString& description() const;
486 /**
487 * Sets the icon for the startup notification ( e.g. 'kcontrol' )
488 * @param icon the name of the icon
489 */
490 void setIcon( const QString& icon );
491 /**
492 * Returns the icon of the startup notification, and if it's not available,
493 * tries to get it from the binary name.
494 * @return the name of the startup notification's icon, or the name of
495 * the binary if not set
496 */
497 const QString& findIcon() const;
498 /**
499 * Returns the icon of the startup notification, or empty if not available.
500 * @return the name of the icon, or an empty string if not set.
501 */
502 const QString& icon() const;
503 /**
504 * Sets the desktop for the startup notification ( i.e. the desktop on which
505 * the starting application should appear ).
506 * @param desktop the desktop for the startup notification
507 */
508 void setDesktop( int desktop );
509 /**
510 * Returns the desktop for the startup notification.
511 * @return the desktop for the startup notification
512 */
513 int desktop() const;
514 /**
515 * Sets a WM_CLASS value for the startup notification, it may be used for increasing
516 * the chance that the windows created by the starting application will be
517 * detected correctly.
518 * @param wmclass the WM_CLASS value for the startup notification
519 */
520 void setWMClass( const QByteArray& wmclass );
521 /**
522 * Returns the WM_CLASS value for the startup notification, or binary name if not
523 * available.
524 * @return the WM_CLASS value for the startup notification, or the binary name
525 * if not set
526 */
527 const QByteArray findWMClass() const;
528 /**
529 * Returns the WM_CLASS value for the startup notification, or empty if not available.
530 * @return the WM_CLASS value for the startup notification, or empty
531 * if not set
532 */
533 QByteArray WMClass() const;
534 /**
535 * Adds a PID to the list of processes that belong to the startup notification. It
536 * may be used to increase the chance that the windows created by the starting
537 * application will be detected correctly, and also for detecting if the application
538 * has quit without creating any window.
539 * @param pid the PID to add
540 */
541 void addPid( pid_t pid );
542 /**
543 * Returns all PIDs for the startup notification.
544 * @return the list of all PIDs
545 */
546 QList< pid_t > pids() const;
547 /**
548 * Checks whether the given @p pid is in the list of PIDs for starup
549 * notification.
550 * @return true if the given @p pid is in the list of PIDs for the startup notification
551 */
552 bool is_pid( pid_t pid ) const;
553 /**
554 * Sets the hostname on which the application is starting. It's necessary to set
555 * it if PIDs are set.
556 * @param hostname the application's hostname. If it's a null string, the current hostname is used
557 */
558 void setHostname( const QByteArray& hostname = QByteArray());
559 /**
560 * Returns the hostname for the startup notification.
561 * @return the hostname
562 */
563 QByteArray hostname() const;
564
565 /**
566 *
567 */
568 enum TriState { Yes, No, Unknown };
569
570 /**
571 * Sets whether the visual feedback for this startup notification
572 * should be silenced (temporarily suspended).
573 */
574 void setSilent( TriState state );
575
576 /**
577 * Return the silence status for the startup notification.
578 * @return KStartupInfoData::Yes if visual feedback is silenced
579 */
580 TriState silent() const;
581
582 /**
583 * @obsolete Timestamp is already assigned in KStartupInfoId::initId().
584 * Sets timestamp for the startup notification. The timestamp is expressed
585 * as XServer time, and is used to prevent activation of the matching
586 * window if user interaction took place after this timestamp.
587 * Value -1 means no timestamp set, value 0 means that the window should
588 * not be activated.
589 */
590 void setTimestamp( unsigned long time );
591
592 /**
593 * @obsolete Use KStartupInfoId::timestamp().
594 * Return the timestamp for the startup notification, or -1 if no timestamp
595 * is set.
596 */
597 unsigned long timestamp() const;
598
599 /**
600 * The X11 screen on which the startup notification is happening, -1 if unknown.
601 */
602 int screen() const;
603
604 /**
605 * Sets the X11 screen on which the startup notification should happen.
606 * This is usually not necessary to set, as it's set by default to QX11Info::screen().
607 */
608 void setScreen( int screen );
609
610 /**
611 * The Xinerama screen for the startup notification, -1 if unknown.
612 */
613 int xinerama() const;
614
615 /**
616 * Sets the Xinerama screen for the startup notification ( i.e. the screeen on which
617 * the starting application should appear ).
618 * @param xinerama the Xinerama screen for the startup notification
619 */
620 void setXinerama( int xinerama );
621
622 /**
623 * The toplevel window of the application that caused this startup notification,
624 * 0 if unknown.
625 */
626 WId launchedBy() const;
627
628 /**
629 * Sets the toplevel window of the application that caused this startup notification.
630 * @param window window ID of the toplevel window that is responsible for this startup
631 */
632 void setLaunchedBy( WId window );
633
634 /**
635 * The .desktop file used to initiate this startup notification, or empty. This information
636 * should be used only to identify the application, not to read any additional information.
637 * @since 4.5
638 **/
639 QString applicationId() const;
640
641 /**
642 * Sets the .desktop file that was used to initiate the startup notification.
643 * @since 4.5
644 */
645 void setApplicationId( const QString& desktop );
646
647 /**
648 * Updates the notification data from the given data. Some data, such as the desktop
649 * or the name, won't be rewritten if already set.
650 * @param data the data to update
651 */
652 void update( const KStartupInfoData& data );
653
654 /**
655 * Constructor. Initializes all the data to their default empty values.
656 */
657 KStartupInfoData();
658
659 /**
660 * Copy constructor.
661 */
662 KStartupInfoData( const KStartupInfoData& data );
663 ~KStartupInfoData();
664 KStartupInfoData& operator=( const KStartupInfoData& data );
665 private:
666 explicit KStartupInfoData( const QString& txt );
667 friend class KStartupInfo;
668 friend class KStartupInfo::Data;
669 friend class KStartupInfo::Private;
670 struct Private;
671 Private *const d;
672 };
673
674#endif
675
676