author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /microkde/kresources/resource.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
Diffstat (limited to 'microkde/kresources/resource.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | microkde/kresources/resource.cpp | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/microkde/kresources/resource.cpp b/microkde/kresources/resource.cpp new file mode 100644 index 0000000..169eaa4 --- a/dev/null +++ b/microkde/kresources/resource.cpp | |||
@@ -0,0 +1,185 @@ | |||
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., 59 Temple Place - Suite 330, | ||
21 | Boston, MA 02111-1307, USA. | ||
22 | */ | ||
23 | |||
24 | #include <kdebug.h> | ||
25 | #include <kapplication.h> | ||
26 | #include <kconfig.h> | ||
27 | |||
28 | #include "resource.h" | ||
29 | |||
30 | using namespace KRES; | ||
31 | |||
32 | class Resource::ResourcePrivate | ||
33 | { | ||
34 | public: | ||
35 | #ifdef QT_THREAD_SUPPORT | ||
36 | QMutex mMutex; | ||
37 | #endif | ||
38 | int mOpenCount; | ||
39 | QString mType; | ||
40 | QString mIdentifier; | ||
41 | bool mReadOnly; | ||
42 | QString mName; | ||
43 | bool mActive; | ||
44 | bool mIsOpen; | ||
45 | }; | ||
46 | |||
47 | Resource::Resource( const KConfig* config ) | ||
48 | : QObject( 0, "" ), d( new ResourcePrivate ) | ||
49 | { | ||
50 | d->mOpenCount = 0; | ||
51 | d->mIsOpen = false; | ||
52 | |||
53 | //US compiler claimed that const discards qualifier | ||
54 | KConfig* cfg = (KConfig*)config; | ||
55 | if ( cfg ) { | ||
56 | d->mType = cfg->readEntry( "ResourceType" ); | ||
57 | d->mName = cfg->readEntry( "ResourceName" ); | ||
58 | d->mReadOnly = cfg->readBoolEntry( "ResourceIsReadOnly", false ); | ||
59 | d->mActive = cfg->readBoolEntry( "ResourceIsActive", true ); | ||
60 | d->mIdentifier = cfg->readEntry( "ResourceIdentifier" ); | ||
61 | } else { | ||
62 | d->mType = "type"; | ||
63 | d->mName = "resource-name"; | ||
64 | d->mReadOnly = false; | ||
65 | d->mActive = true; | ||
66 | d->mIdentifier = KApplication::randomString( 10 ); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | Resource::~Resource() | ||
71 | { | ||
72 | delete d; | ||
73 | d = 0; | ||
74 | } | ||
75 | |||
76 | void Resource::writeConfig( KConfig* config ) | ||
77 | { | ||
78 | kdDebug(5650) << "Resource::writeConfig()" << endl; | ||
79 | |||
80 | config->writeEntry( "ResourceType", d->mType ); | ||
81 | config->writeEntry( "ResourceName", d->mName ); | ||
82 | config->writeEntry( "ResourceIsReadOnly", d->mReadOnly ); | ||
83 | config->writeEntry( "ResourceIsActive", d->mActive ); | ||
84 | config->writeEntry( "ResourceIdentifier", d->mIdentifier ); | ||
85 | } | ||
86 | |||
87 | bool Resource::open() | ||
88 | { | ||
89 | d->mIsOpen = true; | ||
90 | #ifdef QT_THREAD_SUPPORT | ||
91 | QMutexLocker guard( &(d->mMutex) ); | ||
92 | #endif | ||
93 | if ( !d->mOpenCount ) { | ||
94 | kdDebug(5650) << "Opening resource " << resourceName() << endl; | ||
95 | d->mIsOpen = doOpen(); | ||
96 | } | ||
97 | d->mOpenCount++; | ||
98 | return d->mIsOpen; | ||
99 | } | ||
100 | |||
101 | void Resource::close() | ||
102 | { | ||
103 | #ifdef QT_THREAD_SUPPORT | ||
104 | QMutexLocker guard( &(d->mMutex) ); | ||
105 | #endif | ||
106 | if ( !d->mOpenCount ) { | ||
107 | kdDebug(5650) << "ERROR: Resource " << resourceName() << " closed more times than previously opened" << endl; | ||
108 | return; | ||
109 | } | ||
110 | d->mOpenCount--; | ||
111 | if ( !d->mOpenCount ) { | ||
112 | kdDebug(5650) << "Closing resource " << resourceName() << endl; | ||
113 | doClose(); | ||
114 | d->mIsOpen = false; | ||
115 | } else { | ||
116 | kdDebug(5650) << "Not yet closing resource " << resourceName() << ", open count = " << d->mOpenCount << endl; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | bool Resource::isOpen() const | ||
121 | { | ||
122 | return d->mIsOpen; | ||
123 | } | ||
124 | |||
125 | void Resource::setIdentifier( const QString& identifier ) | ||
126 | { | ||
127 | d->mIdentifier = identifier; | ||
128 | } | ||
129 | |||
130 | QString Resource::identifier() const | ||
131 | { | ||
132 | return d->mIdentifier; | ||
133 | } | ||
134 | |||
135 | void Resource::setType( const QString& type ) | ||
136 | { | ||
137 | d->mType = type; | ||
138 | } | ||
139 | |||
140 | QString Resource::type() const | ||
141 | { | ||
142 | return d->mType; | ||
143 | } | ||
144 | |||
145 | void Resource::setReadOnly( bool value ) | ||
146 | { | ||
147 | d->mReadOnly = value; | ||
148 | } | ||
149 | |||
150 | bool Resource::readOnly() const | ||
151 | { | ||
152 | return d->mReadOnly; | ||
153 | } | ||
154 | |||
155 | void Resource::setResourceName( const QString &name ) | ||
156 | { | ||
157 | d->mName = name; | ||
158 | } | ||
159 | |||
160 | QString Resource::resourceName() const | ||
161 | { | ||
162 | return d->mName; | ||
163 | } | ||
164 | |||
165 | void Resource::setActive( bool value ) | ||
166 | { | ||
167 | d->mActive = value; | ||
168 | } | ||
169 | |||
170 | bool Resource::isActive() const | ||
171 | { | ||
172 | return d->mActive; | ||
173 | } | ||
174 | |||
175 | void Resource::dump() const | ||
176 | { | ||
177 | kdDebug(5650) << "Resource:" << endl; | ||
178 | kdDebug(5650) << " Name: " << d->mName << endl; | ||
179 | kdDebug(5650) << " Identifier: " << d->mIdentifier << endl; | ||
180 | kdDebug(5650) << " Type: " << d->mType << endl; | ||
181 | kdDebug(5650) << " OpenCount: " << d->mOpenCount << endl; | ||
182 | kdDebug(5650) << " ReadOnly: " << ( d->mReadOnly ? "yes" : "no" ) << endl; | ||
183 | kdDebug(5650) << " Active: " << ( d->mActive ? "yes" : "no" ) << endl; | ||
184 | kdDebug(5650) << " IsOpen: " << ( d->mIsOpen ? "yes" : "no" ) << endl; | ||
185 | } | ||