resource.cpp

00001 /*
00002     This file is part of libkresources.
00003 
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
00006     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include <kdebug.h>
00025 #include <kapplication.h>
00026 #include <kconfig.h>
00027 #include <klocale.h>
00028 #include "resource.h"
00029 
00030 using namespace KRES;
00031 
00032 class Resource::ResourcePrivate
00033 {
00034   public:
00035 #ifdef QT_THREAD_SUPPORT
00036     QMutex mMutex;
00037 #endif
00038     int mOpenCount;
00039     QString mType;
00040     QString mIdentifier;
00041     bool mReadOnly;
00042     QString mName;
00043     bool mActive;
00044     bool mIsOpen;
00045 };
00046 
00047 Resource::Resource( const KConfig* config )
00048   : QObject( 0, "" ), d( new ResourcePrivate )
00049 {
00050   d->mOpenCount = 0;
00051   d->mIsOpen = false;
00052 
00053   if ( config ) {
00054     d->mType = config->readEntry( "ResourceType" );
00055     d->mName = config->readEntry( "ResourceName" );
00056     d->mReadOnly = config->readBoolEntry( "ResourceIsReadOnly", false );
00057     d->mActive = config->readBoolEntry( "ResourceIsActive", true );
00058     d->mIdentifier = config->readEntry( "ResourceIdentifier" );
00059   } else {
00060     d->mType = "type";
00061     d->mName = i18n("resource");
00062     d->mReadOnly = false;
00063     d->mActive = true;
00064     d->mIdentifier = KApplication::randomString( 10 );
00065   }
00066 }
00067 
00068 Resource::~Resource()
00069 {
00070   delete d;
00071   d = 0;
00072 }
00073 
00074 void Resource::writeConfig( KConfig* config )
00075 {
00076   kdDebug(5650) << "Resource::writeConfig()" << endl;
00077 
00078   config->writeEntry( "ResourceType", d->mType );
00079   config->writeEntry( "ResourceName", d->mName );
00080   config->writeEntry( "ResourceIsReadOnly", d->mReadOnly );
00081   config->writeEntry( "ResourceIsActive", d->mActive );
00082   config->writeEntry( "ResourceIdentifier", d->mIdentifier );
00083 }
00084 
00085 bool Resource::open()
00086 {
00087   d->mIsOpen = true;
00088 #ifdef QT_THREAD_SUPPORT
00089   QMutexLocker guard( &(d->mMutex) );
00090 #endif
00091   if ( !d->mOpenCount ) {
00092     kdDebug(5650) << "Opening resource " << resourceName() << endl;
00093     d->mIsOpen = doOpen();
00094   }
00095   d->mOpenCount++;
00096   return d->mIsOpen;
00097 }
00098 
00099 void Resource::close()
00100 {
00101 #ifdef QT_THREAD_SUPPORT
00102   QMutexLocker guard( &(d->mMutex) );
00103 #endif
00104   if ( !d->mOpenCount ) {
00105     kdDebug(5650) << "ERROR: Resource " << resourceName() << " closed more times than previously opened" << endl;
00106     return;
00107   }
00108   d->mOpenCount--;
00109   if ( !d->mOpenCount ) {
00110     kdDebug(5650) << "Closing resource " << resourceName() << endl;
00111     doClose();
00112     d->mIsOpen = false;
00113   } else {
00114     kdDebug(5650) << "Not yet closing resource " << resourceName() << ", open count = " << d->mOpenCount << endl;
00115   }
00116 }
00117 
00118 bool Resource::isOpen() const
00119 {
00120   return d->mIsOpen;
00121 }
00122 
00123 void Resource::setIdentifier( const QString& identifier )
00124 {
00125   d->mIdentifier = identifier;
00126 }
00127 
00128 QString Resource::identifier() const
00129 {
00130   return d->mIdentifier;
00131 }
00132 
00133 void Resource::setType( const QString& type )
00134 {
00135   d->mType = type;
00136 }
00137 
00138 QString Resource::type() const
00139 {
00140   return d->mType;
00141 }
00142 
00143 void Resource::setReadOnly( bool value )
00144 {
00145   d->mReadOnly = value;
00146 }
00147 
00148 bool Resource::readOnly() const
00149 {
00150   return d->mReadOnly;
00151 }
00152 
00153 void Resource::setResourceName( const QString &name )
00154 {
00155   d->mName = name;
00156 }
00157 
00158 QString Resource::resourceName() const
00159 {
00160   return d->mName;
00161 }
00162 
00163 void Resource::setActive( bool value )
00164 {
00165   d->mActive = value;
00166 }
00167 
00168 bool Resource::isActive() const
00169 {
00170   return d->mActive;
00171 }
00172 
00173 void Resource::dump() const
00174 {
00175   kdDebug(5650) << "Resource:" << endl;
00176   kdDebug(5650) << "  Name: " << d->mName << endl;
00177   kdDebug(5650) << "  Identifier: " << d->mIdentifier << endl;
00178   kdDebug(5650) << "  Type: " << d->mType << endl;
00179   kdDebug(5650) << "  OpenCount: " << d->mOpenCount << endl;
00180   kdDebug(5650) << "  ReadOnly: " << ( d->mReadOnly ? "yes" : "no" ) << endl;
00181   kdDebug(5650) << "  Active: " << ( d->mActive ? "yes" : "no" ) << endl;
00182   kdDebug(5650) << "  IsOpen: " << ( d->mIsOpen ? "yes" : "no" ) << endl;
00183 }
00184 
00185 #include "resource.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys