kresources
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 "resource.h" 00025 00026 #include <kdebug.h> 00027 #include <krandom.h> 00028 #include <kconfig.h> 00029 #include <klocale.h> 00030 #include <kconfiggroup.h> 00031 00032 using namespace KRES; 00033 00034 class Resource::ResourcePrivate 00035 { 00036 public: 00037 #ifdef QT_THREAD_SUPPORT 00038 QMutex mMutex; 00039 #endif 00040 int mOpenCount; 00041 QString mType; 00042 QString mIdentifier; 00043 bool mReadOnly; 00044 QString mName; 00045 bool mActive; 00046 bool mIsOpen; 00047 }; 00048 00049 /* 00050 Resource::Resource( const KConfig* config ) 00051 : QObject( 0 ), d( new ResourcePrivate ) 00052 { 00053 d->mOpenCount = 0; 00054 d->mIsOpen = false; 00055 00056 if ( config ) { 00057 d->mType = config->readEntry( "ResourceType" ); 00058 d->mName = config->readEntry( "ResourceName" ); 00059 d->mReadOnly = config->readEntry("ResourceIsReadOnly", false); 00060 d->mActive = config->readEntry("ResourceIsActive", true); 00061 d->mIdentifier = config->readEntry( "ResourceIdentifier" ); 00062 } else { 00063 d->mType = "type"; 00064 d->mName = i18n("resource"); 00065 d->mReadOnly = false; 00066 d->mActive = true; 00067 d->mIdentifier = KRandom::randomString( 10 ); 00068 } 00069 } 00070 */ 00071 00072 Resource::Resource() 00073 : QObject( 0 ), d( new ResourcePrivate ) 00074 { 00075 d->mOpenCount = 0; 00076 d->mIsOpen = false; 00077 00078 d->mType = "type"; 00079 d->mName = i18n( "resource" ); 00080 d->mReadOnly = false; 00081 d->mActive = true; 00082 d->mIdentifier = KRandom::randomString( 10 ); 00083 } 00084 00085 Resource::Resource( const KConfigGroup &group ) 00086 : QObject( 0 ), d( new ResourcePrivate ) 00087 { 00088 d->mOpenCount = 0; 00089 d->mIsOpen = false; 00090 00091 d->mType = group.readEntry( "ResourceType" ); 00092 d->mName = group.readEntry( "ResourceName" ); 00093 d->mReadOnly = group.readEntry( "ResourceIsReadOnly", false ); 00094 d->mActive = group.readEntry( "ResourceIsActive", true ); 00095 d->mIdentifier = group.readEntry( "ResourceIdentifier" ); 00096 } 00097 00098 Resource::~Resource() 00099 { 00100 delete d; 00101 } 00102 00103 void Resource::writeConfig( KConfigGroup &group ) 00104 { 00105 kDebug(); 00106 00107 group.writeEntry( "ResourceType", d->mType ); 00108 group.writeEntry( "ResourceName", d->mName ); 00109 group.writeEntry( "ResourceIsReadOnly", d->mReadOnly ); 00110 group.writeEntry( "ResourceIsActive", d->mActive ); 00111 group.writeEntry( "ResourceIdentifier", d->mIdentifier ); 00112 } 00113 00114 bool Resource::open() 00115 { 00116 d->mIsOpen = true; 00117 #ifdef QT_THREAD_SUPPORT 00118 QMutexLocker guard( &(d->mMutex) ); 00119 #endif 00120 if ( !d->mOpenCount ) { 00121 kDebug() << "Opening resource" << resourceName(); 00122 d->mIsOpen = doOpen(); 00123 } 00124 d->mOpenCount++; 00125 return d->mIsOpen; 00126 } 00127 00128 void Resource::close() 00129 { 00130 #ifdef QT_THREAD_SUPPORT 00131 QMutexLocker guard( &(d->mMutex) ); 00132 #endif 00133 if ( !d->mOpenCount ) { 00134 kDebug() << "ERROR: Resource" << resourceName() 00135 << " closed more times than previously opened"; 00136 return; 00137 } 00138 d->mOpenCount--; 00139 if ( !d->mOpenCount ) { 00140 kDebug() << "Closing resource" << resourceName(); 00141 doClose(); 00142 d->mIsOpen = false; 00143 } else { 00144 kDebug() << "Not yet closing resource" << resourceName() 00145 << ", open count =" << d->mOpenCount; 00146 } 00147 } 00148 00149 bool Resource::isOpen() const 00150 { 00151 return d->mIsOpen; 00152 } 00153 00154 void Resource::setIdentifier( const QString &identifier ) 00155 { 00156 d->mIdentifier = identifier; 00157 } 00158 00159 QString Resource::identifier() const 00160 { 00161 return d->mIdentifier; 00162 } 00163 00164 void Resource::setType( const QString &type ) 00165 { 00166 d->mType = type; 00167 } 00168 00169 QString Resource::type() const 00170 { 00171 return d->mType; 00172 } 00173 00174 void Resource::setReadOnly( bool value ) 00175 { 00176 d->mReadOnly = value; 00177 } 00178 00179 bool Resource::readOnly() const 00180 { 00181 return d->mReadOnly; 00182 } 00183 00184 void Resource::setResourceName( const QString &name ) 00185 { 00186 d->mName = name; 00187 } 00188 00189 QString Resource::resourceName() const 00190 { 00191 return d->mName; 00192 } 00193 00194 void Resource::setActive( bool value ) 00195 { 00196 d->mActive = value; 00197 } 00198 00199 bool Resource::isActive() const 00200 { 00201 return d->mActive; 00202 } 00203 00204 void Resource::dump() const 00205 { 00206 kDebug() << "Resource:"; 00207 kDebug() << " Name:" << d->mName; 00208 kDebug() << " Identifier:" << d->mIdentifier; 00209 kDebug() << " Type:" << d->mType; 00210 kDebug() << " OpenCount:" << d->mOpenCount; 00211 kDebug() << " ReadOnly:" << ( d->mReadOnly ? "yes" : "no" ); 00212 kDebug() << " Active:" << ( d->mActive ? "yes" : "no" ); 00213 kDebug() << " IsOpen:" << ( d->mIsOpen ? "yes" : "no" ); 00214 } 00215 00216 bool Resource::doOpen() 00217 { 00218 return true; 00219 } 00220 00221 void Resource::doClose() 00222 { 00223 } 00224 00225 QObject *PluginFactoryBase::createObject( QObject *parent, 00226 const char *className, 00227 const QStringList &args ) 00228 { 00229 Q_UNUSED( parent ); 00230 Q_UNUSED( className ); 00231 Q_UNUSED( args ); 00232 return 0; 00233 } 00234 00235 #include "resource.moc"