1/*
2 This file is part of libkresources.
3
4 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
6 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22*/
23
24#include "resource.h"
25
26#include <kdebug.h>
27#include <krandom.h>
28#include <kconfig.h>
29#include <klocalizedstring.h>
30#include <kconfiggroup.h>
31
32using namespace KRES;
33
34class Resource::ResourcePrivate
35{
36 public:
37#ifdef QT_THREAD_SUPPORT
38 QMutex mMutex;
39#endif
40 int mOpenCount;
41 QString mType;
42 QString mIdentifier;
43 bool mReadOnly;
44 QString mName;
45 bool mActive;
46 bool mIsOpen;
47};
48
49/*
50Resource::Resource( const KConfig* config )
51 : QObject( 0 ), d( new ResourcePrivate )
52{
53 d->mOpenCount = 0;
54 d->mIsOpen = false;
55
56 if ( config ) {
57 d->mType = config->readEntry( "ResourceType" );
58 d->mName = config->readEntry( "ResourceName" );
59 d->mReadOnly = config->readEntry("ResourceIsReadOnly", false);
60 d->mActive = config->readEntry("ResourceIsActive", true);
61 d->mIdentifier = config->readEntry( "ResourceIdentifier" );
62 } else {
63 d->mType = "type";
64 d->mName = i18n("resource");
65 d->mReadOnly = false;
66 d->mActive = true;
67 d->mIdentifier = KRandom::randomString( 10 );
68 }
69}
70*/
71
72Resource::Resource()
73 : QObject( 0 ), d( new ResourcePrivate )
74{
75 d->mOpenCount = 0;
76 d->mIsOpen = false;
77
78 d->mType = QLatin1String("type");
79 d->mName = i18n( "resource" );
80 d->mReadOnly = false;
81 d->mActive = true;
82 d->mIdentifier = KRandom::randomString( 10 );
83}
84
85Resource::Resource( const KConfigGroup &group )
86 : QObject( 0 ), d( new ResourcePrivate )
87{
88 d->mOpenCount = 0;
89 d->mIsOpen = false;
90
91 d->mType = group.readEntry( "ResourceType" );
92 d->mName = group.readEntry( "ResourceName" );
93 d->mReadOnly = group.readEntry( "ResourceIsReadOnly", false );
94 d->mActive = group.readEntry( "ResourceIsActive", true );
95 d->mIdentifier = group.readEntry( "ResourceIdentifier" );
96}
97
98Resource::~Resource()
99{
100 delete d;
101}
102
103void Resource::writeConfig( KConfigGroup &group )
104{
105 kDebug();
106
107 group.writeEntry( "ResourceType", d->mType );
108 group.writeEntry( "ResourceName", d->mName );
109 group.writeEntry( "ResourceIsReadOnly", d->mReadOnly );
110 group.writeEntry( "ResourceIsActive", d->mActive );
111 group.writeEntry( "ResourceIdentifier", d->mIdentifier );
112}
113
114bool Resource::open()
115{
116 d->mIsOpen = true;
117#ifdef QT_THREAD_SUPPORT
118 QMutexLocker guard( &( d->mMutex ) );
119#endif
120 if ( !d->mOpenCount ) {
121 kDebug() << "Opening resource" << resourceName();
122 d->mIsOpen = doOpen();
123 }
124 d->mOpenCount++;
125 return d->mIsOpen;
126}
127
128void Resource::close()
129{
130#ifdef QT_THREAD_SUPPORT
131 QMutexLocker guard( &( d->mMutex ) );
132#endif
133 if ( !d->mOpenCount ) {
134 kDebug() << "ERROR: Resource" << resourceName()
135 << " closed more times than previously opened";
136 return;
137 }
138 d->mOpenCount--;
139 if ( !d->mOpenCount ) {
140 kDebug() << "Closing resource" << resourceName();
141 doClose();
142 d->mIsOpen = false;
143 } else {
144 kDebug() << "Not yet closing resource" << resourceName()
145 << ", open count =" << d->mOpenCount;
146 }
147}
148
149bool Resource::isOpen() const
150{
151 return d->mIsOpen;
152}
153
154void Resource::setIdentifier( const QString &identifier )
155{
156 d->mIdentifier = identifier;
157}
158
159QString Resource::identifier() const
160{
161 return d->mIdentifier;
162}
163
164void Resource::setType( const QString &type )
165{
166 d->mType = type;
167}
168
169QString Resource::type() const
170{
171 return d->mType;
172}
173
174void Resource::setReadOnly( bool value )
175{
176 d->mReadOnly = value;
177}
178
179bool Resource::readOnly() const
180{
181 return d->mReadOnly;
182}
183
184void Resource::setResourceName( const QString &name )
185{
186 d->mName = name;
187}
188
189QString Resource::resourceName() const
190{
191 return d->mName;
192}
193
194void Resource::setActive( bool value )
195{
196 d->mActive = value;
197}
198
199bool Resource::isActive() const
200{
201 return d->mActive;
202}
203
204void Resource::dump() const
205{
206 kDebug() << "Resource:";
207 kDebug() << " Name:" << d->mName;
208 kDebug() << " Identifier:" << d->mIdentifier;
209 kDebug() << " Type:" << d->mType;
210 kDebug() << " OpenCount:" << d->mOpenCount;
211 kDebug() << " ReadOnly:" << ( d->mReadOnly ? "yes" : "no" );
212 kDebug() << " Active:" << ( d->mActive ? "yes" : "no" );
213 kDebug() << " IsOpen:" << ( d->mIsOpen ? "yes" : "no" );
214}
215
216bool Resource::doOpen()
217{
218 return true;
219}
220
221void Resource::doClose()
222{
223}
224
225QObject *PluginFactoryBase::createObject( QObject *parent,
226 const char *className,
227 const QStringList &args )
228{
229 Q_UNUSED( parent );
230 Q_UNUSED( className );
231 Q_UNUSED( args );
232 return 0;
233}
234
235