kitchensync Library API Documentation

kdedsharedfile.cpp

00001 /* This file is part of the KDE Project
00002    Copyright (c) 2002 Holger Freyther <freyther@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 #include <qmutex.h>
00019 #include <qmap.h>
00020 #include "kdedsharedfile.h"
00021 
00022 #include <kmessagebox.h>
00023 
00024 class FileLockObject {
00025  public:
00026   FileLockObject(){
00027     readShares = 0;
00028     writeLock = false;
00029   };
00030   FileLockObject(const QString &fileName ){
00031     readShares = 0;
00032     writeLock = false;
00033     this->fileName = fileName;
00034   }
00035   ~FileLockObject(){};
00036   FileLockObject(const FileLockObject &obj  ){
00037     readShares = obj.readShares;
00038     writeLock = obj.writeLock;
00039     fileName = obj.fileName;
00040   }
00041   uint readShares;
00042   bool writeLock:1;
00043   QString fileName;
00044 };
00045 
00046 class KShareFileModule::KShareFileModulePrivate {
00047 public:
00048   KShareFileModulePrivate(){
00049   };
00050   QMutex mutex;
00051   QMap<QString, FileLockObject> m_objects;
00052   QMap<QString, FileLockObject>::Iterator m_it;
00053 };
00054 
00055 KShareFileModule::KShareFileModule(const QCString &obj ) : KDEDModule(obj )
00056 {
00057   d = new KShareFileModulePrivate();
00058 }
00059 KShareFileModule::~KShareFileModule()
00060 {
00061   delete d;
00062 }
00063 /*
00064  * We want to manage a new file
00065  * Check if already have it our cache
00066  * and if not insert it
00067  */
00068 void KShareFileModule::interestedIn(const QString &fileName )
00069 {
00070   KMessageBox::error(0, "", "ksharedfile-kded" );
00071   d->mutex.lock();
00072   if( !d->m_objects.contains(fileName) ){
00073     d->m_objects.insert(fileName, FileLockObject(fileName) );
00074   }
00075   d->mutex.unlock();
00076 }
00077 void KShareFileModule::removeInterestIn(const QString &fileName )
00078 {
00079   KMessageBox::error(0, "", "ksharedfile-kded" );
00080   d->mutex.lock();
00081   if( d->m_objects.contains(fileName) ){
00082     d->m_it = d->m_objects.find(fileName);
00083     if( !d->m_it.data().writeLock && d->m_it.data().readShares == 0 ){
00084       d->m_objects.remove(d->m_it );
00085     }
00086   }
00087   d->mutex.unlock();
00088 }
00089 /*
00090  Look if its inside the module
00091 
00092  */
00093 bool KShareFileModule::readShareFile(const QString &fileName )
00094 {
00095   KMessageBox::error(0, "", "ksharedfile-kded" );
00096   static bool val=false;
00097   d->mutex.lock();
00098   if(!d->m_objects.contains(fileName ) ){
00099     d->m_objects.insert(fileName, FileLockObject(fileName ) );
00100   }
00101   d->m_it = d->m_objects.find(fileName );
00102   if( !d->m_it.data().writeLock ){
00103     val = true;
00104     d->m_it.data().readShares += 1;
00105   }else{
00106     val = false;
00107   }
00108   d->mutex.unlock();
00109   return val;
00110 }
00111 bool KShareFileModule::readUnshareFile(const QString &fileName )
00112 {
00113     bool val=false;
00114     d->mutex.lock();
00115     if(d->m_objects.contains(fileName ) ){
00116         d->m_it = d->m_objects.find(fileName );
00117         if(!d->m_it.data().writeLock ){
00118             if ( d->m_it.data().readShares > 0 )
00119                 d->m_it.data().readShares--;
00120             val = true;
00121         }
00122     }
00123     d->mutex.unlock();
00124 
00125     return val;
00126 }
00127 bool KShareFileModule::writeLockFile(const QString &fileName )
00128 {
00129   static bool val=false;
00130   d->mutex.lock();
00131   if(!d->m_objects.contains(fileName ) ){
00132     d->m_objects.insert(fileName, FileLockObject(fileName ) );
00133   }
00134   d->m_it = d->m_objects.find(fileName );
00135   if( !d->m_it.data().writeLock && d->m_it.data().readShares==0 ){
00136     val = true;
00137     d->m_it.data().writeLock = true;
00138   }else {
00139     val = false;
00140   }
00141   d->mutex.unlock();
00142   return val;
00143 }
00144 bool KShareFileModule::writeUnlockFile(const QString &fileName )
00145 {
00146   bool val=false;
00147   d->mutex.lock();
00148   if(d->m_objects.contains(fileName ) ){
00149     d->m_it = d->m_objects.find(fileName );
00150     if(  d->m_it.data().writeLock ){
00151       //d->m_objects.remove(d->m_it );
00152       d->m_it.data().writeLock = false;
00153       val = true;
00154     }
00155   }
00156   d->mutex.unlock();
00157   return val;
00158 }
00159 
00160 
00161 
00162 extern "C" {
00163   KDEDModule *create_ksharedfile(const QCString &obj)
00164   {
00165     return new KShareFileModule(obj);
00166   }
00167 }
00168 #include "kdedsharedfile.moc"
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 Jan 31 15:53:45 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003