kabc Library: testlock.cpp Source File (kabc Library)

| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# testlock.cpp

    00001 /*
    00002     This file is part of libkabc.
    00003
    00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
    00005
    00006     This library is free software; you can redistribute it and/or
    00007     modify it under the terms of the GNU Library General Public
    00008     License as published by the Free Software Foundation; either
    00009     version 2 of the License, or (at your option) any later version.
    00010
    00011     This library is distributed in the hope that it will be useful,
    00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014     Library General Public License for more details.
    00015
    00016     You should have received a copy of the GNU Library General Public License
    00017     along with this library; see the file COPYING.LIB.  If not, write to
    00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019     Boston, MA 02111-1307, USA.
    00020 */
    00021
    00022 #include "testlock.h"
    00023
    00024 #include "stdaddressbook.h"
    00025
    00026 #include <kaboutdata.h>
    00027 #include <kapplication.h>
    00028 #include <kdebug.h>
    00029 #include <klocale.h>
    00030 #include <kcmdlineargs.h>
    00031 #include <kdirwatch.h>
    00032
    00033 #include <kmessagebox.h>
    00034 #include <kdialog.h>
    00035
    00036 #include <qwidget.h>
    00037 #include <qlabel.h>
    00038 #include <qlayout.h>
    00039 #include <qpushbutton.h>
    00040 #include <qlistview.h>
    00041 #include <qdir.h>
    00042
    00043 #include <iostream>
    00044
    00045 #include <sys/types.h>
    00046 #include <unistd.h>
    00047
    00048 using namespace KABC;
    00049
    00050 LockWidget::LockWidget( const QString &identifier )
    00051 {
    00052   QVBoxLayout *topLayout = new QVBoxLayout( this );
    00053   topLayout->setMargin( KDialog::marginHint() );
    00054   topLayout->setSpacing( KDialog::spacingHint() );
    00055
    00056   if ( identifier.isEmpty() ) {
    00057     mLock = 0;
    00058   } else {
    00059     mLock = new Lock( identifier );
    00060
    00061     int pid = getpid();
    00062
    00063     QLabel *pidLabel = new QLabel( "Process ID: " + QString::number( pid ),
    00064                                    this );
    00065     topLayout->addWidget( pidLabel );
    00066
    00067     QHBoxLayout *identifierLayout = new QHBoxLayout( topLayout );
    00068
    00069     QLabel *resourceLabel = new QLabel( "Identifier:", this );
    00070     identifierLayout->addWidget( resourceLabel );
    00071
    00072     QLabel *resourceIdentifier = new QLabel( identifier, this );
    00073     identifierLayout->addWidget( resourceIdentifier );
    00074
    00075     mStatus = new QLabel( "Status: Unlocked", this );
    00076     topLayout->addWidget( mStatus );
    00077
    00078     QPushButton *button = new QPushButton( "Lock", this );
    00079     topLayout->addWidget( button );
    00080     connect( button, SIGNAL( clicked() ), SLOT( lock() ) );
    00081
    00082     button = new QPushButton( "Unlock", this );
    00083     topLayout->addWidget( button );
    00084     connect( button, SIGNAL( clicked() ), SLOT( unlock() ) );
    00085   }
    00086
    00087   mLockView = new QListView( this );
    00088   topLayout->addWidget( mLockView );
    00089   mLockView->addColumn( "Lock File" );
    00090   mLockView->addColumn( "PID" );
    00091   mLockView->addColumn( "Locking App" );
    00092
    00093   updateLockView();
    00094
    00095   QPushButton *quitButton = new QPushButton( "Quit", this );
    00096   topLayout->addWidget( quitButton );
    00097   connect( quitButton, SIGNAL( clicked() ), SLOT( close() ) );
    00098
    00099   KDirWatch *watch = KDirWatch::self();
    00100   connect( watch, SIGNAL( dirty( const QString & ) ),
    00101            SLOT( updateLockView() ) );
    00102   connect( watch, SIGNAL( created( const QString & ) ),
    00103            SLOT( updateLockView() ) );
    00104   connect( watch, SIGNAL( deleted( const QString & ) ),
    00105            SLOT( updateLockView() ) );
    00106   watch->addDir( Lock::locksDir() );
    00107   watch->startScan();
    00108 }
    00109
    00110 LockWidget::~LockWidget()
    00111 {
    00112   delete mLock;
    00113 }
    00114
    00115 void LockWidget::updateLockView()
    00116 {
    00117   mLockView->clear();
    00118
    00119   QDir dir( Lock::locksDir() );
    00120
    00121   QStringList files = dir.entryList( "*.lock" );
    00122
    00123   QStringList::ConstIterator it;
    00124   for( it = files.begin(); it != files.end(); ++it ) {
    00125     if ( *it == "." || *it == ".." ) continue;
    00126
    00127     QString app;
    00128     int pid;
    00129     if ( !Lock::readLockFile( dir.filePath( *it ), pid, app ) ) {
    00130       kdWarning() << "Unable to open lock file '" << *it << "'" << endl;
    00131     } else {
    00132       new QListViewItem( mLockView, *it, QString::number( pid ), app );
    00133     }
    00134   }
    00135 }
    00136
    00137 void LockWidget::lock()
    00138 {
    00139   if ( !mLock->lock() ) {
    00140     KMessageBox::sorry( this, mLock->error() );
    00141   } else {
    00142     mStatus->setText( "Status: Locked" );
    00143   }
    00144 }
    00145
    00146 void LockWidget::unlock()
    00147 {
    00148   if ( !mLock->unlock() ) {
    00149     KMessageBox::sorry( this, mLock->error() );
    00150   } else {
    00151     mStatus->setText( "Status: Unlocked" );
    00152   }
    00153 }
    00154
    00155
    00156 static const KCmdLineOptions options[] =
    00157 {
    00158   { "a", 0, 0 },
    00159   { "addressbook", "Standard address book", 0 },
    00160   { "d", 0, 0 },
    00161   { "diraddressbook", "Standard address book directory resource", 0 },
    00162   { "+identifier", "Identifier of resource to be locked, e.g. filename", 0 },
    00163   KCmdLineLastOption
    00164 };
    00165
    00166 int main(int argc,char **argv)
    00167 {
    00168   KAboutData aboutData("testlock",I18N_NOOP("Test libkabc Lock"),"0.1");
    00169   KCmdLineArgs::init(argc,argv,&aboutData);
    00170   KCmdLineArgs::addCmdLineOptions( options );
    00171
    00172   KApplication app;
    00173
    00174   QString identifier;
    00175
    00176   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    00177   if ( args->count() == 1 ) {
    00178     identifier = args->arg( 0 );
    00179   } else if ( args->count() != 0 ) {
    00180     std::cerr << "Usage: testlock <identifier>" << std::endl;
    00181     return 1;
    00182   }
    00183
    00184   if ( args->isSet( "addressbook" ) ) {
    00185     if ( args->count() == 1 ) {
    00186       std::cerr << "Ignoring resource identifier" << std::endl;
    00187     }
    00188     identifier = StdAddressBook::fileName();
    00189   }
    00190
    00191   if ( args->isSet( "diraddressbook" ) ) {
    00192     if ( args->count() == 1 ) {
    00193       std::cerr << "Ignoring resource identifier" << std::endl;
    00194     }
    00195     identifier = StdAddressBook::directoryName();
    00196   }
    00197
    00198   LockWidget mainWidget( identifier );
    00199
    00200   kapp->setMainWidget( &mainWidget );
    00201   mainWidget.show();
    00202
    00203   return app.exec();
    00204 }
    00205
    00206 #include "testlock.moc"

This file is part of the documentation for kabc Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:31 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003