23 #include <tdeapplication.h>
26 #include <kstandarddirs.h>
27 #include <tdetempfile.h>
32 #include <sys/types.h>
39 : mIdentifier( identifier )
41 mIdentifier.replace(
"/",
"_" );
49 TQString Lock::locksDir()
54 bool Lock::readLockFile(
const TQString &filename,
int &pid, TQString &app )
56 TQFile file( filename );
57 if ( !file.open( IO_ReadOnly ) )
return false;
59 TQTextStream t( &file );
60 pid = t.readLine().toInt();
66 bool Lock::writeLockFile(
const TQString &filename )
68 TQFile file( filename );
69 if ( !file.open( IO_WriteOnly ) )
return false;
70 TQTextStream t( &file );
76 TQString Lock::lockFileName()
const
78 return locksDir() + mIdentifier +
".lock";
85 TQString lockName = lockFileName();
86 kdDebug(5700) <<
"-- lock name: " << lockName <<
endl;
88 if ( TQFile::exists( lockName ) ) {
92 if ( !readLockFile( lockFileName(), pid, app ) ) {
93 mError = i18n(
"Unable to open lock file.");
97 int retval = ::kill( pid, 0 );
98 if ( retval == -1 && errno == ESRCH ) {
99 TQFile::remove( lockName );
100 kdWarning(5700) <<
"Removed stale lock file from process '" << app <<
"'"
103 TQString identifier( mIdentifier );
104 identifier.replace(
'_',
'/' );
106 mError = i18n(
"The address book '%1' is locked by application '%2'.\nIf you believe this is incorrect, just remove the lock file from '%3'")
107 .arg( identifier ).arg( app ).arg(
locateLocal(
"data",
"tdeabc/lock/*.lock" ) );
112 TQString lockUniqueName;
113 lockUniqueName = mIdentifier + kapp->randomString( 8 );
114 mLockUniqueName =
locateLocal(
"data",
"tdeabc/lock/" + lockUniqueName );
115 kdDebug(5700) <<
"-- lock unique name: " << mLockUniqueName <<
endl;
118 writeLockFile( mLockUniqueName );
121 int result = ::link( TQFile::encodeName( mLockUniqueName ),
122 TQFile::encodeName( lockName ) );
132 mError = i18n(
"Error");
140 if ( readLockFile( lockFileName(), pid, app ) ) {
141 if ( pid == getpid() ) {
142 TQFile::remove( lockFileName() );
143 TQFile::remove( mLockUniqueName );
146 mError = i18n(
"Unlock failed. Lock file is owned by other process: %1 (%2)")
147 .arg( app ).arg( TQString::number( pid ) );
157 TQString Lock::error()
const
Lock(const TQString &identifier)
Constructor.
~Lock()
Destruct lock object.
virtual bool unlock()
Unlock resource.
virtual bool lock()
Lock resource.
static TDEInstance * instance()
kndbgstream & endl(kndbgstream &s)
kdbgstream kdWarning(int area=0)
kdbgstream kdDebug(int area=0)
TQString locateLocal(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
static data, shared by ALL addressee objects