kitchensync Library API Documentation

ksharedfile.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2001, 2002 Holger Freyther <freyher@kde.org>
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004           
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2 as published by the Free Software Foundation.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 
00019 */
00020 #include <sys/stat.h>
00021 #include <unistd.h>
00022 
00023 #include <dcopclient.h>
00024 #include <kapplication.h>
00025 #include <kdebug.h>
00026 
00027 
00028 #include <qfile.h>
00029 #include <qtimer.h>
00030 #include <qtextstream.h>
00031 #include "ksharedfile.h"
00032 
00033 
00034 #include "ksharedfile.moc"
00035 
00036 KSharedFile::KSharedFile( const QString &filename ) : QObject( 0, "KSharedFile" ),  DCOPObject("KSharedFileManager" )
00037 {
00038   // dcop bits
00039   readLock = false;
00040   writeLock = false;
00041   setFileName( filename);
00042   connectDCOPSignal( "kded", "ksharedfile", // makes it even load
00043              "fileLocked(QString)",
00044              "slotFileLocked(QString)", false );
00045   connectDCOPSignal( "kded", "ksharedfile",
00046              "fileUnlocked(QString)",
00047              "slotFileUnlocked(QString)", false );
00048   connectDCOPSignal( "kded", "ksharedfile",
00049              "fileChanged(QString)",
00050              "slotFileChanged(QString)",  false );
00051 }
00052 KSharedFile::KSharedFile( const QFile &file ) : QObject( 0, "KSharedFile" )
00053 {
00054   // dcop bits
00055   readLock = false;
00056   writeLock = false;
00057   setFile( file);
00058   connectDCOPSignal( "kded", "ksharedfile",
00059              "fileLocked(QString)",
00060              "slotFileLocked(QString)", false );
00061   connectDCOPSignal( "kded", "ksharedfile",
00062              "fileUnlocked(QString)",
00063              "slotFileUnlocked(QString)", false );
00064   connectDCOPSignal( "kded", "ksharedfile",
00065              "fileChanged(QString)",
00066              "slotFileChanged(QString)",  false );
00067   kdDebug(5910) << file.name() << endl;
00068 }
00069 KSharedFile::~KSharedFile( )
00070 { // remove all locks and interests
00071   updateLocks();
00072   kdDebug(5910) << "~KSharedFile" << endl;
00073 }
00074 
00075 void KSharedFile::setFileName ( const QString &filename )
00076 {
00077   updateLocks();
00079    m_fileName = filename;
00080   QByteArray data;
00081   QDataStream stream2(data, IO_WriteOnly );
00082   stream2 << m_fileName;
00083   kapp->dcopClient()->send("kded", "ksharedfile","interestedIn(QString)", data  );
00084 }
00085 void KSharedFile::setFile( const QFile &file )
00086 {
00087   setFileName( file.name() );
00088 }
00089 QString KSharedFile::fileName( )const 
00090 {
00091   return m_fileName;
00092 }
00093 
00094 
00095 KSharedFile::Ticket *KSharedFile::requestWriteTicket( )
00096 { 
00097   kdDebug(5910) << "KSharedFile::requestSaveTicket " << m_fileName << endl; 
00098   Ticket *ticket=0l;
00099   if(writeLock)
00100     return 0l; 
00101   // Create lock file
00102   // dcop bits
00103   QByteArray data;
00104   QByteArray replyData;
00105   QCString replyType;
00106   QDataStream arg(data, IO_WriteOnly );
00107   arg << m_fileName;
00108   if(kapp->dcopClient()->call( "kded", "ksharedfile",
00109                    "writeLockFile(QString)",
00110                    data,
00111                    replyType, replyData ) )
00112 {
00113     qWarning("If" );
00114     if( replyType == "bool"){
00115       kdDebug(5910) << "reply" << endl;
00116       QDataStream result(replyData, IO_ReadOnly ); 
00117       bool ok;
00118       result >> ok;
00119       if( ok ){
00120     kdDebug(5910 ) << "request Worked" << endl;
00121     ticket = new Ticket(m_fileName );
00122     writeLock = true;
00123       }else{
00124     kdDebug(5910) << "failed to lock" << endl;
00125       }
00126     }
00127   }
00128   return ticket;
00129 }
00130 
00131 KSharedFile::Ticket *KSharedFile::requestReadTicket( )
00132 {
00133   Ticket *ticket = 0l;
00134   kdDebug(5910) << "requestReadTicket" << endl;
00135   if(readLock)
00136     return ticket;
00137   kdDebug(5910) << "DCOP" << endl;
00138   QByteArray data;
00139   QByteArray replyData;
00140   QCString replyType;
00141   QDataStream arg(data, IO_WriteOnly );
00142   arg << m_fileName;
00143   if(kapp->dcopClient()->call( "kded", "ksharedfile",
00144                    "readShareFile(QString)",
00145                    data,
00146                    replyType, replyData ) )
00147     {
00148     qWarning("If" );
00149     if( replyType == "bool"){
00150       kdDebug(5910) << "bool" << endl;
00151       QDataStream result(replyData, IO_ReadOnly ); 
00152       bool ok;
00153       result >> ok;
00154       if( ok ){
00155     ticket = new Ticket(m_fileName );
00156     readLock = true;
00157     kdDebug(5910) <<"readLockFileOk" <<endl;
00158       }
00159       kdDebug(5910) << "bool end" << endl;
00160     }
00161   }
00162   return ticket;
00163 }
00164 bool KSharedFile::unlockWriteFile( Ticket* ticket )
00165 {
00166   if( writeLock && ticket->m_fileName==m_fileName){
00167     QByteArray data;
00168     QByteArray replyData;
00169     QCString replyType;
00170     QDataStream arg(data, IO_WriteOnly );
00171     arg << m_fileName;
00172     if(kapp->dcopClient()->call( "kded", "ksharedfile",
00173                  "writeUnlockFile(QString)",
00174                  data,
00175                  replyType, replyData ) ){
00176       qWarning("If" );
00177       if( replyType == "bool"){
00178     QDataStream result(replyData, IO_ReadOnly );
00179     bool ok;
00180     result >> ok;
00181     if( ok ){
00182       delete ticket;
00183       writeLock = false;
00184       if(m_file != 0 ){
00185         delete m_file;
00186       }
00187       return true;
00188     }
00189       }
00190     }
00191   }
00192   return false;
00193 }
00194 bool KSharedFile::unlockReadFile( Ticket *ticket )
00195 {
00196   kdDebug(5910) << "unlockReadFile" << endl; 
00197   if(ticket == 0l)
00198     return false;
00199   if (readLock && ticket->m_fileName == m_fileName ) {
00200     kdDebug(5910) << "going to unlock" << endl;
00201     readLock = false;
00202     QByteArray data;
00203     QByteArray replyData;
00204     QCString replyType;
00205     QDataStream arg(data, IO_WriteOnly );
00206     arg << m_fileName;
00207     if(kapp->dcopClient()->call( "kded", "ksharedfile",
00208                  "readUnshareFile(QString)",
00209                  data,
00210                  replyType, replyData ) ){
00211       qWarning("If" );
00212       if( replyType == "bool"){
00213         kdDebug(5910) << "unlock bool type" << endl;
00214     QDataStream result(replyData, IO_ReadOnly );
00215     bool ok;
00216     result >> ok;
00217     if( ok ){
00218       delete ticket;
00219       readLock = false;
00220       kdDebug(5910) << "bool" <<endl;
00221       return true;
00222     }
00223       }
00224     }
00225   }
00226   return false;
00227 }
00228 bool KSharedFile::canReadLock( )
00229 {
00230   //return m_locked; dcop bits
00231   return true;
00232 }
00233 bool KSharedFile::canWriteLock()
00234 {
00235   return true;
00236 }
00237 //QString KSharedFile::whoHoldsLock( ) const
00238 //{
00239 //  return QString();
00240 //}
00241 bool KSharedFile::save( Ticket *ticket, const QString &string )
00242 {
00243   if ( writeLock && ticket->m_fileName == m_fileName ){
00244     QFile file( m_fileName ); // handles unicode names inernal
00245     file.open(IO_WriteOnly);
00246     QTextStream stream( &file );
00247     //file.writeBlock( string ); //doesn't work
00248     stream << string;
00249     file.close();
00250     return true;
00251   }
00252   return false;
00253 }
00254 bool KSharedFile::save( Ticket *ticket, const QByteArray &array )
00255 {
00256   if ( writeLock && ticket->m_fileName == m_fileName ){
00257     QFile file( m_fileName ); // handles unicode names inernal
00258     file.open(IO_WriteOnly);
00259     file.writeBlock(array );
00260     file.close();
00261     return true;
00262   }
00263   return false;
00264 
00265 }
00266 QFile* KSharedFile::save( Ticket *ticket )
00267 {
00268   if ( writeLock && ticket->m_fileName == m_fileName ){
00269     m_file = new QFile(ticket->m_fileName );
00270     m_file->open(IO_WriteOnly );
00271     return m_file;
00272   }
00273   return 0l;
00274 }
00275 QString KSharedFile::readAsString( bool &ok, Ticket *ticket )
00276 {
00277   if( readLock && ticket->m_fileName == m_fileName ){
00278     QString dummy;
00279     QFile file( m_fileName );
00280     file.open(IO_ReadOnly);
00281     dummy = QString(file.readAll() );
00282     file.close();
00283     ok=true;
00284     return dummy;
00285   }else {
00286     ok=false;
00287     return QString();
00288   }
00289 }
00290 QByteArray KSharedFile::readAsByteArray(bool &ok, Ticket *ticket )
00291 {
00292   if( readLock && ticket->m_fileName == m_fileName ){
00293     QByteArray dummy;
00294     QFile file( m_fileName );
00295     file.open(IO_ReadOnly);
00296     dummy = file.readAll() ;
00297     file.close();
00298     ok=true;
00299     return dummy;
00300   }else {
00301     ok=false;
00302     return QByteArray();
00303   }
00304 }
00305 QFile* KSharedFile::readAsFile(Ticket *ticket)
00306 {
00307   if ( readLock && ticket->m_fileName == m_fileName ){
00308     m_file = new QFile(ticket->m_fileName );
00309     m_file->open(IO_ReadOnly );
00310     return m_file;
00311   }
00312   return 0l;
00313 }
00314 bool KSharedFile::didIReadLock() 
00315 {
00316   return readLock;
00317 }
00318 bool KSharedFile::didIWriteLock()
00319 {
00320   return writeLock;
00321 }
00322 void KSharedFile::slotFileChanged(QString) {
00323 
00324 }
00325 void KSharedFile::updateLocks()
00326 {
00327   if(m_fileName.isEmpty() )
00328     return;
00329   if( readLock ){
00330     QByteArray data;
00331     QByteArray replyData;
00332     QCString replyType;
00333     QDataStream arg(data, IO_WriteOnly );
00334     arg << m_fileName;
00335     kapp->dcopClient()->call( "kded", "ksharedfile",
00336                  "readUnlockFile(QString)",
00337                  data,
00338                  replyType, replyData );
00339     readLock=false;
00340   }else if(writeLock ){
00341     QByteArray data;
00342     QByteArray replyData;
00343     QCString replyType;
00344     QDataStream arg(data, IO_WriteOnly );
00345     arg << m_fileName;
00346     kapp->dcopClient()->call( "kded", "ksharedfile",
00347                   "readUnlockFile(QString)",
00348                   data,
00349                   replyType, replyData );
00350     writeLock=false; 
00351   }
00352   QByteArray data;
00353   QDataStream stream(data, IO_WriteOnly );
00354   stream << m_fileName;
00355   kapp->dcopClient()->send("kded", "ksharedfile","removeInterestIn(QString)", data  );
00356   if(m_file != 0 )
00357     delete m_file;
00358 }
00359 
00360 
00361 
00362 
00363 
00364 
00365 
00366 
00367 
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:41:41 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003