@ -19,17 +19,14 @@
// $Id: traylabelmgr.cpp,v 1.10 2005/02/09 03:38:43 cs19713 Exp $
// $Id: traylabelmgr.cpp,v 1.10 2005/02/09 03:38:43 cs19713 Exp $
# include <tqdir.h>
# include <tqapplication.h>
# include <tqmessagebox.h>
# include <tqtimer.h>
# include <tqfile.h>
# include <tqfile.h>
# include <tqaction.h>
# include <tqpopupmenu.h>
# include <tqtextstream.h>
# include <tqtextstream.h>
# include <tqfiledialog.h>
# include <tdeapplication.h>
# include <tdecmdlineargs.h>
# include <tdeconfig.h>
# include <tdelocale.h>
# include <tdelocale.h>
# include <tdemessagebox.h>
# include "trace.h"
# include "trace.h"
# include "traylabelmgr.h"
# include "traylabelmgr.h"
@ -40,9 +37,9 @@
# include <stdlib.h>
# include <stdlib.h>
TrayLabelMgr * TrayLabelMgr : : gTrayLabelMgr = NULL ;
TrayLabelMgr * TrayLabelMgr : : gTrayLabelMgr = NULL ;
const char * TrayLabelMgr : : mOptionString = " + a bdefi:lmop:qtw:" ;
const char * TrayLabelMgr : : mOptionString = " + bdefi:lmop:qtw:" ;
TrayLabelMgr * TrayLabelMgr : : instance ( void )
TrayLabelMgr * TrayLabelMgr : : instance ( )
{
{
if ( gTrayLabelMgr ) return gTrayLabelMgr ;
if ( gTrayLabelMgr ) return gTrayLabelMgr ;
TRACE ( " Creating new instance " ) ;
TRACE ( " Creating new instance " ) ;
@ -51,6 +48,8 @@ TrayLabelMgr* TrayLabelMgr::instance(void)
TrayLabelMgr : : TrayLabelMgr ( ) : mReady ( false ) , mHiddenLabelsCount ( 0 )
TrayLabelMgr : : TrayLabelMgr ( ) : mReady ( false ) , mHiddenLabelsCount ( 0 )
{
{
connect ( & restoreSessionTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( doRestoreSession ( ) ) ) ;
// Set ourselves up to be called from the application loop
// Set ourselves up to be called from the application loop
TQTimer : : singleShot ( 0 , this , SLOT ( startup ( ) ) ) ;
TQTimer : : singleShot ( 0 , this , SLOT ( startup ( ) ) ) ;
}
}
@ -60,15 +59,6 @@ TrayLabelMgr::~TrayLabelMgr()
undockAll ( ) ;
undockAll ( ) ;
}
}
void TrayLabelMgr : : about ( void )
{
if ( TQMessageBox : : information ( NULL , i18n ( " About TDEDocker " ) ,
i18n ( " Bugs/wishes to Girish Ramakrishnan (gramakri@uiuc.edu) \n "
" English translation by Girish (gramakri@uiuc.edu) \n \n "
" http://tdedocker.sourceforge.net for updates " ) ,
TQString : : null , SHOW_TRACE_TEXT ) = = 1 ) SHOW_TRACE ( ) ;
}
void TrayLabelMgr : : startup ( void )
void TrayLabelMgr : : startup ( void )
{
{
const int WAIT_TIME = 10 ;
const int WAIT_TIME = 10 ;
@ -78,7 +68,7 @@ void TrayLabelMgr::startup(void)
* stdout is a tty ) OR if we are getting restored , wait for WAIT_TIME until
* stdout is a tty ) OR if we are getting restored , wait for WAIT_TIME until
* the system tray shows up ( before informing the user )
* the system tray shows up ( before informing the user )
*/
*/
static bool do_wait = ! isatty ( fileno ( stdout ) ) | | tqApp- > isSession Restored( ) ;
static bool do_wait = ! isatty ( fileno ( stdout ) ) | | TDEApplication: : kApplication ( ) - > is Restored( ) ;
SysTrayState state = sysTrayStatus ( TQPaintDevice : : x11AppDisplay ( ) ) ;
SysTrayState state = sysTrayStatus ( TQPaintDevice : : x11AppDisplay ( ) ) ;
@ -91,10 +81,9 @@ void TrayLabelMgr::startup(void)
return ;
return ;
}
}
if ( TQMessageBox : : warning ( NULL , i18n ( " TDEDocker " ) ,
if ( KMessageBox : : warningContinueCancel ( NULL ,
i18n ( state = = SysTrayAbsent ? " No system tray found "
state = = SysTrayAbsent ? i18n ( " No system tray found " ) : i18n ( " System tray appears to be hidden " ) ,
: " System tray appears to be hidden " ) ,
i18n ( " TDEDocker " ) ) = = KMessageBox : : Cancel )
TQMessageBox : : Abort , TQMessageBox : : Ignore ) = = TQMessageBox : : Abort )
{
{
tqApp - > quit ( ) ;
tqApp - > quit ( ) ;
return ;
return ;
@ -104,10 +93,17 @@ void TrayLabelMgr::startup(void)
// Things are fine or user with OK with the state of system tray
// Things are fine or user with OK with the state of system tray
mReady = true ;
mReady = true ;
bool ok = false ;
bool ok = false ;
if ( tqApp - > isSessionRestored ( ) ) ok = restoreSession ( tqApp - > sessionId ( ) ) ;
if ( TDEApplication : : kApplication ( ) - > isRestored ( ) )
else ok = processCommand ( tqApp - > argc ( ) , tqApp - > argv ( ) ) ;
{
// Process the request Q from previous instances
restoreSession ( ) ;
ok = true ;
}
else
{
ok = processCommand ( TDECmdLineArgs : : parsedArgs ( ) ) ;
}
// Process the request Q from previous instances
TRACE ( " Request queue has %i requests " , mRequestQ . count ( ) ) ;
TRACE ( " Request queue has %i requests " , mRequestQ . count ( ) ) ;
for ( unsigned i = 0 ; i < mRequestQ . count ( ) ; i + + )
for ( unsigned i = 0 ; i < mRequestQ . count ( ) ; i + + )
ok | = processCommand ( mRequestQ [ i ] ) ;
ok | = processCommand ( mRequestQ [ i ] ) ;
@ -117,8 +113,7 @@ void TrayLabelMgr::startup(void)
// Initialize a TQTrayLabel after its creation
// Initialize a TQTrayLabel after its creation
void TrayLabelMgr : : manageTrayLabel ( TQTrayLabel * t )
void TrayLabelMgr : : manageTrayLabel ( TQTrayLabel * t )
{
{
connect ( t , SIGNAL ( destroyed ( TQObject * ) ) ,
connect ( t , SIGNAL ( destroyed ( TQObject * ) ) , this , SLOT ( trayLabelDestroyed ( TQObject * ) ) ) ;
this , SLOT ( trayLabelDestroyed ( TQObject * ) ) ) ;
connect ( t , SIGNAL ( undocked ( TQTrayLabel * ) ) , t , SLOT ( deleteLater ( ) ) ) ;
connect ( t , SIGNAL ( undocked ( TQTrayLabel * ) ) , t , SLOT ( deleteLater ( ) ) ) ;
// All TQTrayLabels will emit this signal. We just need one of them
// All TQTrayLabels will emit this signal. We just need one of them
@ -152,7 +147,7 @@ void TrayLabelMgr::undockAll()
}
}
// Process the command line
// Process the command line
bool TrayLabelMgr : : processCommand ( const TQStringList & args )
bool TrayLabelMgr : : processCommand ( const TQStringList & args )
{
{
if ( ! mReady )
if ( ! mReady )
{
{
@ -161,19 +156,83 @@ bool TrayLabelMgr::processCommand(const TQStringList& args)
return true ;
return true ;
}
}
const int MAX_ARGS = 2 0;
const int MAX_ARGS = 5 0;
const char * argv [ MAX_ARGS ] ;
const char * argv [ MAX_ARGS ] ;
int argc = args . count ( ) ;
int argc = args . count ( ) ;
if ( argc > = MAX_ARGS ) argc = MAX_ARGS - 1 ;
if ( argc > = MAX_ARGS ) argc = MAX_ARGS - 1 ;
for ( int i = 0 ; i < argc ; i + + )
for ( int i = 0 ; i < argc ; i + + )
argv [ i ] = args [ i ] . latin1 ( ) ;
{
argv [ i ] = args [ i ] . local8Bit ( ) ;
}
argv [ argc ] = NULL ; // null terminate the array
argv [ argc ] = NULL ; // null terminate the array
return processCommand ( argc , const_cast < char * * > ( argv ) ) ;
return processCommand ( argc , const_cast < char * * > ( argv ) ) ;
}
}
// Process the command line
bool TrayLabelMgr : : processCommand ( TDECmdLineArgs * args )
{
TQStringList argl ;
argl . append ( TDECmdLineArgs : : appName ( ) ) ;
if ( args - > isSet ( " b " ) )
{
argl . append ( " -b " ) ;
}
if ( args - > isSet ( " d " ) )
{
argl . append ( " -d " ) ;
}
if ( args - > isSet ( " e " ) )
{
argl . append ( " -e " ) ;
}
if ( args - > isSet ( " f " ) )
{
argl . append ( " -f " ) ;
}
if ( args - > isSet ( " i " ) )
{
argl . append ( " -i " ) ;
argl . append ( args - > getOption ( " i " ) ) ;
}
if ( args - > isSet ( " m " ) )
{
argl . append ( " -m " ) ;
}
if ( args - > isSet ( " o " ) )
{
argl . append ( " -o " ) ;
}
if ( args - > isSet ( " p " ) )
{
argl . append ( " -p " ) ;
argl . append ( args - > getOption ( " p " ) ) ;
}
if ( args - > isSet ( " q " ) )
{
argl . append ( " -q " ) ;
}
if ( args - > isSet ( " t " ) )
{
argl . append ( " -t " ) ;
}
if ( args - > isSet ( " w " ) )
{
argl . append ( " -w " ) ;
argl . append ( args - > getOption ( " w " ) ) ;
}
for ( int i = 0 ; i < args - > count ( ) ; + + i )
{
argl . append ( args - > arg ( i ) ) ;
}
return processCommand ( argl ) ;
}
// Process the command line
// Process the command line
bool TrayLabelMgr : : processCommand ( int argc , char * * argv )
bool TrayLabelMgr : : processCommand ( int argc , char * * argv )
{
{
@ -182,11 +241,12 @@ bool TrayLabelMgr::processCommand(int argc, char** argv)
if ( argc < 1 ) return false ;
if ( argc < 1 ) return false ;
// Restore session ( S ee the comments in TDEDocker::notifyPreviousInstance()
// Restore session ( s ee the comments in TDEDocker::notifyPreviousInstance() )
if ( qstrcmp ( argv [ 1 ] , " - session " ) = = 0 )
if ( qstrcmp ( argv [ 1 ] , " - -restore-internal " ) = = 0 )
{
{
TRACE ( " Restoring session %s (new instance request) " , argv [ 2 ] ) ;
TRACE ( " Restoring session (new instance request) " ) ;
return restoreSession ( TQString ( argv [ 2 ] ) ) ;
restoreSession ( ) ;
return true ;
}
}
int option ;
int option ;
@ -194,8 +254,7 @@ bool TrayLabelMgr::processCommand(int argc, char** argv)
const char * icon = NULL ;
const char * icon = NULL ;
int balloon_timeout = 4000 ;
int balloon_timeout = 4000 ;
bool withdraw = true , skip_taskbar = false ,
bool withdraw = true , skip_taskbar = false ,
auto_launch = false , dock_obscure = false , check_normality = true ,
dock_obscure = false , check_normality = true , enable_sm = true ;
enable_sm = true ;
optind = 0 ; // initialise the getopt static
optind = 0 ; // initialise the getopt static
@ -205,9 +264,6 @@ bool TrayLabelMgr::processCommand(int argc, char** argv)
{
{
case ' ? ' :
case ' ? ' :
return false ;
return false ;
case ' a ' :
tqDebug ( " %s " , i18n ( " Girish Ramakrishnan (gramakri@uiuc.edu) " ) . local8Bit ( ) . data ( ) ) ;
return false ;
case ' b ' :
case ' b ' :
check_normality = false ;
check_normality = false ;
break ;
break ;
@ -224,9 +280,6 @@ bool TrayLabelMgr::processCommand(int argc, char** argv)
case ' i ' :
case ' i ' :
icon = optarg ;
icon = optarg ;
break ;
break ;
case ' l ' :
auto_launch = true ;
break ;
case ' m ' :
case ' m ' :
withdraw = false ;
withdraw = false ;
break ;
break ;
@ -270,7 +323,6 @@ bool TrayLabelMgr::processCommand(int argc, char** argv)
if ( withdraw ) t - > withdraw ( ) ; else t - > map ( ) ;
if ( withdraw ) t - > withdraw ( ) ; else t - > map ( ) ;
t - > enableSessionManagement ( enable_sm ) ;
t - > enableSessionManagement ( enable_sm ) ;
t - > dock ( ) ;
t - > dock ( ) ;
t - > setLaunchOnStartup ( auto_launch ) ;
return true ;
return true ;
}
}
@ -288,7 +340,10 @@ TQTrayLabel *TrayLabelMgr::selectAndDock(Window w, bool checkNormality)
if ( ( w = selectWindow ( TQPaintDevice : : x11AppDisplay ( ) , & err ) ) = = None )
if ( ( w = selectWindow ( TQPaintDevice : : x11AppDisplay ( ) , & err ) ) = = None )
{
{
if ( err ) TQMessageBox : : critical ( NULL , i18n ( " TDEDocker " ) , i18n ( err ) ) ;
if ( err )
{
KMessageBox : : error ( NULL , err , i18n ( " TDEDocker " ) ) ;
}
return NULL ;
return NULL ;
}
}
}
}
@ -299,20 +354,20 @@ TQTrayLabel *TrayLabelMgr::selectAndDock(Window w, bool checkNormality)
* Abort should be the only option here really . " Ignore " is provided here
* Abort should be the only option here really . " Ignore " is provided here
* for the curious user who wants to screw himself very badly
* for the curious user who wants to screw himself very badly
*/
*/
if ( TQMessageBox: : warning ( NULL , i18n ( " TDEDocker " ) ,
if ( KMessageBox: : warningContinueCancel ( NULL ,
i18n ( " The window you are attempting to dock does not seem to be a "
i18n ( " The window you are attempting to dock does not seem to be a normal window. " ) ,
" normal window. " ) , TQMessageBox : : Abort ,
i18n ( " TDEDocker " ) ) = = KMessageBox : : Cancel )
TQMessageBox : : Ignore ) = = TQMessageBox : : Abort )
{
return NULL ;
return NULL ;
}
}
}
if ( ! isWindowDocked ( w ) ) return new CustomTrayLabel ( w ) ;
if ( ! isWindowDocked ( w ) ) return new CustomTrayLabel ( w ) ;
TRACE ( " 0x%x is not docked " , ( unsigned ) w ) ;
TRACE ( " 0x%x is not docked " , ( unsigned ) w ) ;
TQMessageBox : : message ( i18n ( " TDEDocker " ) ,
KMessageBox : : error ( NULL , i18n ( " This window is already docked. \n "
i18n ( " This window is already docked. \n "
" Click on system tray icon to toggle docking. " ) , i18n ( " TDEDocker " ) ) ;
" Click on system tray icon to toggle docking. " ) ) ;
return NULL ;
return NULL ;
}
}
@ -349,7 +404,6 @@ TQTrayLabel *TrayLabelMgr::dockApplication(char *argv[])
close ( filedes [ 1 ] ) ;
close ( filedes [ 1 ] ) ;
read ( filedes [ 0 ] , buf , sizeof ( buf ) ) ;
read ( filedes [ 0 ] , buf , sizeof ( buf ) ) ;
close ( filedes [ 0 ] ) ;
close ( filedes [ 0 ] ) ;
if ( execvp ( argv [ 0 ] , argv ) = = - 1 )
if ( execvp ( argv [ 0 ] , argv ) = = - 1 )
{
{
tqDebug ( " %s " , i18n ( " Failed to exec [%1]: %2 " ) . arg ( argv [ 0 ] ) . arg ( strerror ( errno ) ) . local8Bit ( ) . data ( ) ) ;
tqDebug ( " %s " , i18n ( " Failed to exec [%1]: %2 " ) . arg ( argv [ 0 ] ) . arg ( strerror ( errno ) ) . local8Bit ( ) . data ( ) ) ;
@ -360,8 +414,7 @@ TQTrayLabel *TrayLabelMgr::dockApplication(char *argv[])
if ( pid = = - 1 )
if ( pid = = - 1 )
{
{
TQMessageBox : : critical ( NULL , " TDEDocker " ,
KMessageBox : : error ( NULL , i18n ( " Failed to fork: %1 " ) . arg ( strerror ( errno ) ) , i18n ( " Ignore " ) ) ;
i18n ( " Failed to fork: %1 " ) . arg ( strerror ( errno ) ) ) ;
return NULL ;
return NULL ;
}
}
@ -427,72 +480,60 @@ void TrayLabelMgr::notifySysTrayAbsence()
if ( state = = SysTrayPresent )
if ( state = = SysTrayPresent )
return ; // So sweet of the systray to come back so soon
return ; // So sweet of the systray to come back so soon
if ( TQMessageBox : : warning ( NULL , i18n ( " TDEDocker " ) ,
KMessageBox : : error ( NULL , i18n ( " The System tray was hidden or removed. All applications "
i18n ( " The System tray was hidden or removed " ) ,
" will be undocked. " ) , i18n ( " TDEDocker " ) ) ;
i18n ( " Undock All " ) , i18n ( " Ignore " ) ) = = 0 )
undockAll ( ) ;
undockAll ( ) ;
}
}
/*
void TrayLabelMgr : : restoreSession ( )
* Session Management . Always return " true " . Atleast , for now
*/
bool TrayLabelMgr : : restoreSession ( const TQString & sessionId )
{
{
TQString session_file = " tdedocker_ " + sessionId ;
// After restoring a session, the TDE session manager will relaunch the applications
// that were previously docked before terminating the previous session. To avoid
// launching the same apps twice, wait for a while before restoring the tdedocker
// session, so that we give tdedocker a chance to simply docks the applications
// already launched by TDE session manager.
restoreSessionTimer . start ( 5000 , true ) ;
}
TQSettings settings ;
void TrayLabelMgr : : doRestoreSession ( )
settings . beginGroup ( TQString ( " / " + session_file ) ) ;
{
TRACE ( " Restoring session " ) ;
for ( int i = 1 ; ; i + + )
TDEConfig * config = TDEApplication : : kApplication ( ) - > sessionConfig ( ) ;
for ( int i = 1 ; ; + + i )
{
{
settings . beginGroup ( TQString ( " /Instance " ) + TQString ( " " ) . setNum ( i ) ) ;
if ( ! config - > hasGroup ( " Instance " + TQString : : number ( i ) ) )
TQString pname = settings . readEntry ( " /Application " ) ;
TRACE ( " Restoring Application[%s] " , pname . latin1 ( ) ) ;
if ( pname . isEmpty ( ) ) break ;
if ( settings . readBoolEntry ( " /LaunchOnStartup " ) )
{
{
TQStringList args ( " tdedocker " ) ;
return ;
args + = TQStringList : : split ( " " , pname ) ;
TRACE ( " Triggering AutoLaunch " ) ;
if ( ! processCommand ( args ) ) continue ;
}
}
else
config - > setGroup ( " Instance " + TQString : : number ( i ) ) ;
TQString pname = config - > readEntry ( " Application " , TQString : : null ) ;
if ( ! pname . isEmpty ( ) )
{
TRACE ( " Restoring Application[%s] " , pname . ascii ( ) ) ;
manageTrayLabel ( new CustomTrayLabel ( TQStringList : : split ( " " , pname ) , 0 ) ) ;
manageTrayLabel ( new CustomTrayLabel ( TQStringList : : split ( " " , pname ) , 0 ) ) ;
mTrayLabels . getFirst ( ) - > restoreState ( config ) ;
TQTrayLabel * tl = mTrayLabels . getFirst ( ) ; // the one that was created above
}
tl - > restoreState ( settings ) ;
settings . endGroup ( ) ;
}
}
return true ;
}
}
TQString TrayLabelMgr : : saveSession ( void )
bool TrayLabelMgr : : saveState ( TQSessionManager & sm )
{
{
TQString session_file = " tdedocker_ " + tqApp - > sessionId ( ) ;
TQSettings settings ;
settings . beginGroup ( TQString ( " / " + session_file ) ) ;
TRACE ( " Saving session " ) ;
TRACE ( " Saving session " ) ;
TQPtrListIterator < TQTrayLabel > it ( mTrayLabels ) ;
TQTrayLabel * t ;
int i = 1 ;
int i = 1 ;
while ( ( t = it . current ( ) ) ! = 0 )
TQTrayLabel * t ;
TDEConfig * config = TDEApplication : : kApplication ( ) - > sessionConfig ( ) ;
TQPtrListIterator < TQTrayLabel > it ( mTrayLabels ) ;
for ( it . toFirst ( ) ; it . current ( ) ; + + it )
{
{
+ + it ;
t = it . current ( ) ;
TRACE ( " Saving instance %i " , i ) ;
TRACE ( " Saving instance %i " , i ) ;
settings . beginGroup ( TQString ( " /Instance " ) + TQString ( " " ) . setNum ( i ) ) ;
config - > setGroup ( " Instance " + TQString : : number ( i ) ) ;
bool ok = t - > saveState ( settings ) ;
t - > saveState ( config ) ;
settings . endGroup ( ) ;
+ + i ;
if ( ok ) + + i ; else TRACE ( " Saving of instance %i was skipped " , i ) ;
}
}
return true ;
// Aaaaaaaaaaaaaa.........
settings . removeEntry ( TQString ( " /Instance " ) + TQString ( " " ) . setNum ( i ) ) ;
return TQDir : : homeDirPath ( ) + " /.qt/ " + session_file + " rc " ;
}
}
/*
/*