-rw-r--r-- | microkde/kresources/configdialog.cpp | 137 | ||||
-rw-r--r-- | microkde/kresources/configdialog.h | 60 | ||||
-rw-r--r-- | microkde/kresources/configpage.cpp | 507 | ||||
-rw-r--r-- | microkde/kresources/configpage.h | 103 | ||||
-rw-r--r-- | microkde/kresources/configwidget.cpp | 45 | ||||
-rw-r--r-- | microkde/kresources/configwidget.h | 59 | ||||
-rw-r--r-- | microkde/kresources/factory.cpp | 216 | ||||
-rw-r--r-- | microkde/kresources/factory.h | 113 | ||||
-rw-r--r-- | microkde/kresources/kcmkresources.cpp | 89 | ||||
-rw-r--r-- | microkde/kresources/kcmkresources.h | 54 | ||||
-rw-r--r-- | microkde/kresources/manager.h | 332 | ||||
-rw-r--r-- | microkde/kresources/managerimpl.cpp | 353 | ||||
-rw-r--r-- | microkde/kresources/managerimpl.h | 113 | ||||
-rw-r--r-- | microkde/kresources/resource.cpp | 185 | ||||
-rw-r--r-- | microkde/kresources/resource.h | 401 | ||||
-rw-r--r-- | microkde/kresources/resourceselectdialog.h | 12 | ||||
-rw-r--r-- | microkde/kresources/selectdialog.cpp | 154 | ||||
-rw-r--r-- | microkde/kresources/selectdialog.h | 92 |
18 files changed, 3025 insertions, 0 deletions
diff --git a/microkde/kresources/configdialog.cpp b/microkde/kresources/configdialog.cpp new file mode 100644 index 0000000..48d9137 --- a/dev/null +++ b/microkde/kresources/configdialog.cpp | |||
@@ -0,0 +1,137 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 <klocale.h> | ||
25 | #include <kglobal.h> | ||
26 | #include <kmessagebox.h> | ||
27 | |||
28 | #include <qgroupbox.h> | ||
29 | #include <qlabel.h> | ||
30 | #include <qlayout.h> | ||
31 | #include <qpushbutton.h> | ||
32 | |||
33 | #include <qcheckbox.h> | ||
34 | |||
35 | #include <kbuttonbox.h> | ||
36 | #include <kdialog.h> | ||
37 | #include <klineedit.h> | ||
38 | |||
39 | #include "factory.h" | ||
40 | #include "configwidget.h" | ||
41 | #include "configdialog.h" | ||
42 | |||
43 | using namespace KRES; | ||
44 | |||
45 | ConfigDialog::ConfigDialog( QWidget *parent, const QString& resourceFamily, | ||
46 | /*const QString& type,*/ Resource* resource, /*KConfig *config, */const char *name ) | ||
47 | : KDialogBase( parent, name, true, i18n( "Resource Configuration" ), | ||
48 | Ok|Cancel, Ok, true )/*, mConfig( config )*/, mResource( resource ) | ||
49 | { | ||
50 | Factory *factory = Factory::self( resourceFamily ); | ||
51 | |||
52 | //US resize( 250, 240 ); | ||
53 | resize( KMIN(KGlobal::getDesktopWidth(), 250), KMIN(KGlobal::getDesktopHeight(), 240)); | ||
54 | |||
55 | //US QFrame *main = makeMainWidget(); | ||
56 | QFrame *main = plainPage(); | ||
57 | |||
58 | QVBoxLayout *mainLayout = new QVBoxLayout( main, 0, spacingHint() ); | ||
59 | |||
60 | QGroupBox *generalGroupBox = new QGroupBox( 2, Qt::Horizontal, main ); | ||
61 | generalGroupBox->layout()->setSpacing( spacingHint() ); | ||
62 | generalGroupBox->setTitle( i18n( "General Settings" ) ); | ||
63 | |||
64 | new QLabel( i18n( "Name:" ), generalGroupBox ); | ||
65 | |||
66 | mName = new KLineEdit( generalGroupBox ); | ||
67 | |||
68 | mReadOnly = new QCheckBox( i18n( "Read-only" ), generalGroupBox ); | ||
69 | |||
70 | mName->setText( mResource->resourceName() ); | ||
71 | mReadOnly->setChecked( mResource->readOnly() ); | ||
72 | |||
73 | mainLayout->addWidget( generalGroupBox ); | ||
74 | |||
75 | QGroupBox *resourceGroupBox = new QGroupBox( 2, Qt::Horizontal, main ); | ||
76 | resourceGroupBox->layout()->setSpacing( spacingHint()); | ||
77 | resourceGroupBox->setTitle( i18n( "%1 Resource Settings" ) | ||
78 | .arg( factory->typeName( resource->type() ) ) ); | ||
79 | mainLayout->addWidget( resourceGroupBox ); | ||
80 | |||
81 | mainLayout->addStretch(); | ||
82 | |||
83 | mConfigWidget = factory->configWidget( resource->type(), resourceGroupBox ); | ||
84 | if ( mConfigWidget ) { | ||
85 | mConfigWidget->setInEditMode( false ); | ||
86 | mConfigWidget->loadSettings( mResource ); | ||
87 | mConfigWidget->show(); | ||
88 | connect( mConfigWidget, SIGNAL( setReadOnly( bool ) ), | ||
89 | SLOT( setReadOnly( bool ) ) ); | ||
90 | } | ||
91 | |||
92 | connect( mName, SIGNAL( textChanged(const QString &)), | ||
93 | SLOT( slotNameChanged(const QString &))); | ||
94 | |||
95 | slotNameChanged( mName->text() ); | ||
96 | |||
97 | //US setMinimumSize( 400, 250 ); | ||
98 | setMinimumSize( KMIN(KGlobal::getDesktopWidth(), 400), KMIN(KGlobal::getDesktopHeight(), 250)); | ||
99 | |||
100 | } | ||
101 | |||
102 | void ConfigDialog::setInEditMode( bool value ) | ||
103 | { | ||
104 | if ( mConfigWidget ) | ||
105 | mConfigWidget->setInEditMode( value ); | ||
106 | } | ||
107 | |||
108 | void ConfigDialog::slotNameChanged( const QString &text) | ||
109 | { | ||
110 | enableButtonOK( !text.isEmpty() ); | ||
111 | } | ||
112 | |||
113 | void ConfigDialog::setReadOnly( bool value ) | ||
114 | { | ||
115 | mReadOnly->setChecked( value ); | ||
116 | } | ||
117 | |||
118 | void ConfigDialog::accept() | ||
119 | { | ||
120 | if ( mName->text().isEmpty() ) { | ||
121 | KMessageBox::sorry( this, i18n( "Please enter a resource name" ) ); | ||
122 | return; | ||
123 | } | ||
124 | |||
125 | mResource->setResourceName( mName->text() ); | ||
126 | mResource->setReadOnly( mReadOnly->isChecked() ); | ||
127 | |||
128 | if ( mConfigWidget ) { | ||
129 | // First save generic information | ||
130 | // Also save setting of specific resource type | ||
131 | mConfigWidget->saveSettings( mResource ); | ||
132 | } | ||
133 | |||
134 | KDialog::accept(); | ||
135 | } | ||
136 | |||
137 | //US #include "configdialog.moc" | ||
diff --git a/microkde/kresources/configdialog.h b/microkde/kresources/configdialog.h new file mode 100644 index 0000000..6acc5d9 --- a/dev/null +++ b/microkde/kresources/configdialog.h | |||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> | ||
4 | Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> | ||
5 | |||
6 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | ||
10 | |||
11 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
19 | Boston, MA 02111-1307, USA. | ||
20 | */ | ||
21 | |||
22 | #ifndef KRESOURCES_CONFIGDIALOG_H | ||
23 | #define KRESOURCES_CONFIGDIALOG_H | ||
24 | |||
25 | #include <kdialogbase.h> | ||
26 | |||
27 | class KLineEdit; | ||
28 | class QCheckBox; | ||
29 | class KButtonBox; | ||
30 | |||
31 | namespace KRES { | ||
32 | class Resource; | ||
33 | class ConfigWidget; | ||
34 | |||
35 | class ConfigDialog : public KDialogBase | ||
36 | { | ||
37 | Q_OBJECT | ||
38 | public: | ||
39 | // Resource=0: create new resource | ||
40 | ConfigDialog( QWidget *parent, const QString& resourceFamily, | ||
41 | Resource* resource, const char *name = 0); | ||
42 | |||
43 | void setInEditMode( bool value ); | ||
44 | |||
45 | protected slots: | ||
46 | void accept(); | ||
47 | void setReadOnly( bool value ); | ||
48 | void slotNameChanged( const QString &text); | ||
49 | |||
50 | private: | ||
51 | ConfigWidget *mConfigWidget; | ||
52 | Resource* mResource; | ||
53 | |||
54 | KLineEdit *mName; | ||
55 | QCheckBox *mReadOnly; | ||
56 | }; | ||
57 | |||
58 | } | ||
59 | |||
60 | #endif | ||
diff --git a/microkde/kresources/configpage.cpp b/microkde/kresources/configpage.cpp new file mode 100644 index 0000000..0f1469d --- a/dev/null +++ b/microkde/kresources/configpage.cpp | |||
@@ -0,0 +1,507 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 <qgroupbox.h> | ||
25 | #include <qinputdialog.h> | ||
26 | #include <qlabel.h> | ||
27 | #include <qlayout.h> | ||
28 | |||
29 | #include <kapplication.h> | ||
30 | #include <kcombobox.h> | ||
31 | #include <kdebug.h> | ||
32 | #include <klocale.h> | ||
33 | #include <kmessagebox.h> | ||
34 | #include <ksimpleconfig.h> | ||
35 | #include <kstandarddirs.h> | ||
36 | #include <kurlrequester.h> | ||
37 | #include <klistview.h> | ||
38 | #include <kbuttonbox.h> | ||
39 | //US #include <ktrader.h> | ||
40 | |||
41 | #include "resource.h" | ||
42 | #include "configdialog.h" | ||
43 | |||
44 | #include "configpage.h" | ||
45 | |||
46 | //US | ||
47 | #include <qpushbutton.h> | ||
48 | #include <qfile.h> | ||
49 | #include <kglobal.h> | ||
50 | |||
51 | using namespace KRES; | ||
52 | |||
53 | class ConfigViewItem : public QCheckListItem | ||
54 | { | ||
55 | public: | ||
56 | ConfigViewItem( QListView *parent, Resource* resource ) : | ||
57 | QCheckListItem( parent, resource->resourceName(), CheckBox ), | ||
58 | mResource( resource ), | ||
59 | mIsStandard( false ) | ||
60 | { | ||
61 | setText( 1, mResource->type() ); | ||
62 | setOn( mResource->isActive() ); | ||
63 | } | ||
64 | |||
65 | void setStandard( bool value ) | ||
66 | { | ||
67 | setText( 2, ( value ? i18n( "Yes" ) : QString::null ) ); | ||
68 | mIsStandard = value; | ||
69 | } | ||
70 | |||
71 | bool standard() const { return mIsStandard; } | ||
72 | bool readOnly() const { return mResource->readOnly(); } | ||
73 | |||
74 | Resource *resource() { return mResource; } | ||
75 | |||
76 | private: | ||
77 | Resource* mResource; | ||
78 | |||
79 | bool mIsStandard; | ||
80 | }; | ||
81 | |||
82 | ConfigPage::ConfigPage( QWidget *parent, const char *name ) | ||
83 | : QWidget( parent, name ), | ||
84 | mCurrentManager( 0 ), | ||
85 | mCurrentConfig( 0 ) | ||
86 | { | ||
87 | setCaption( i18n( "Resource Configuration" ) ); | ||
88 | |||
89 | QVBoxLayout *mainLayout = new QVBoxLayout( this ); | ||
90 | |||
91 | QGroupBox *groupBox = new QGroupBox( i18n( "Resources" ), this ); | ||
92 | groupBox->setColumnLayout(0, Qt::Vertical ); | ||
93 | groupBox->layout()->setSpacing( 6 ); | ||
94 | groupBox->layout()->setMargin( 11 ); | ||
95 | QGridLayout *groupBoxLayout = new QGridLayout( groupBox->layout(), 2, 2 ); | ||
96 | |||
97 | //US mFamilyCombo = new KComboBox( false, groupBox ); | ||
98 | mFamilyCombo = new KComboBox( groupBox ); | ||
99 | groupBoxLayout->addMultiCellWidget( mFamilyCombo, 0, 0, 0, 1 ); | ||
100 | |||
101 | mListView = new KListView( groupBox ); | ||
102 | mListView->setAllColumnsShowFocus( true ); | ||
103 | mListView->addColumn( i18n( "Name" ) ); | ||
104 | mListView->addColumn( i18n( "Type" ) ); | ||
105 | mListView->addColumn( i18n( "Standard" ) ); | ||
106 | |||
107 | groupBoxLayout->addWidget( mListView, 1, 0 ); | ||
108 | |||
109 | KButtonBox *buttonBox = new KButtonBox( groupBox, Vertical ); | ||
110 | mAddButton = buttonBox->addButton( i18n( "&Add..." ), this, SLOT(slotAdd()) ); | ||
111 | mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT(slotRemove()) ); | ||
112 | mRemoveButton->setEnabled( false ); | ||
113 | mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT(slotEdit()) ); | ||
114 | mEditButton->setEnabled( false ); | ||
115 | mStandardButton = buttonBox->addButton( i18n( "&Use as Standard" ), this, SLOT(slotStandard()) ); | ||
116 | mStandardButton->setEnabled( false ); | ||
117 | buttonBox->layout(); | ||
118 | |||
119 | groupBoxLayout->addWidget( buttonBox, 1, 1 ); | ||
120 | |||
121 | mainLayout->addWidget( groupBox ); | ||
122 | |||
123 | connect( mFamilyCombo, SIGNAL( activated( int ) ), | ||
124 | SLOT( slotFamilyChanged( int ) ) ); | ||
125 | connect( mListView, SIGNAL( selectionChanged() ), | ||
126 | SLOT( slotSelectionChanged() ) ); | ||
127 | connect( mListView, SIGNAL( clicked( QListViewItem * ) ), | ||
128 | SLOT( slotItemClicked( QListViewItem * ) ) ); | ||
129 | |||
130 | mLastItem = 0; | ||
131 | |||
132 | //US mConfig = new KConfig( "kcmkresourcesrc" ); | ||
133 | mConfig = new KConfig( locateLocal( "config", "kcmkresourcesrc") ); | ||
134 | mConfig->setGroup( "General" ); | ||
135 | |||
136 | load(); | ||
137 | } | ||
138 | |||
139 | ConfigPage::~ConfigPage() | ||
140 | { | ||
141 | QValueList<ResourcePageInfo>::Iterator it; | ||
142 | for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) { | ||
143 | (*it).mManager->removeListener( this ); | ||
144 | delete (*it).mManager; | ||
145 | delete (*it).mConfig; | ||
146 | } | ||
147 | |||
148 | mConfig->writeEntry( "CurrentFamily", mFamilyCombo->currentItem() ); | ||
149 | delete mConfig; | ||
150 | mConfig = 0; | ||
151 | } | ||
152 | |||
153 | void ConfigPage::load() | ||
154 | { | ||
155 | kdDebug(5650) << "ConfigPage::load()" << endl; | ||
156 | |||
157 | mListView->clear(); | ||
158 | |||
159 | //US we remove the dynamic pluginloader, and set the one family we need (contact) manually. | ||
160 | |||
161 | //US KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin" ); | ||
162 | //US KTrader::OfferList::ConstIterator it; | ||
163 | //US for ( it = plugins.begin(); it != plugins.end(); ++it ) { | ||
164 | //US QVariant tmp = (*it)->property( "X-KDE-ResourceFamily" ); | ||
165 | //US QString family = tmp.toString(); | ||
166 | |||
167 | QString family = "contact"; | ||
168 | if ( !family.isEmpty() ) { | ||
169 | if ( !mFamilyMap.contains( family ) ) { | ||
170 | mCurrentManager = new Manager<Resource>( family ); | ||
171 | if ( mCurrentManager ) { | ||
172 | mFamilyMap.append( family ); | ||
173 | mCurrentManager->addListener( this ); | ||
174 | |||
175 | ResourcePageInfo info; | ||
176 | info.mManager = mCurrentManager; | ||
177 | QString configDir = KGlobal::dirs()->saveLocation( "config" ); | ||
178 | //QString configDir = KStandardDirs::appDir() + "/config"; | ||
179 | if ( family == "contact" && QFile::exists( configDir + "/kabcrc" ) ) { | ||
180 | info.mConfig = new KConfig( locateLocal( "config", "kabcrc" ) ); | ||
181 | } else if ( family == "calendar" && QFile::exists( configDir + "/kcalrc" ) ) { | ||
182 | info.mConfig = new KConfig( locateLocal( "config", "kcalrc" ) ); | ||
183 | } else { | ||
184 | QString configFile = locateLocal( "config", QString( "kresources/%1/stdrc" ).arg( family ) ); | ||
185 | info.mConfig = new KConfig( configFile ); | ||
186 | } | ||
187 | info.mManager->readConfig( info.mConfig ); | ||
188 | |||
189 | mInfoMap.append( info ); | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | //US } | ||
194 | mCurrentManager = 0; | ||
195 | |||
196 | mFamilyCombo->insertStringList( mFamilyMap ); | ||
197 | |||
198 | int currentFamily = mConfig->readNumEntry( "CurrentFamily", 0 ); | ||
199 | mFamilyCombo->setCurrentItem( currentFamily ); | ||
200 | slotFamilyChanged( currentFamily ); | ||
201 | } | ||
202 | |||
203 | void ConfigPage::save() | ||
204 | { | ||
205 | saveResourceSettings(); | ||
206 | |||
207 | QValueList<ResourcePageInfo>::Iterator it; | ||
208 | for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) | ||
209 | (*it).mManager->writeConfig( (*it).mConfig ); | ||
210 | |||
211 | emit changed( false ); | ||
212 | } | ||
213 | |||
214 | void ConfigPage::defaults() | ||
215 | { | ||
216 | } | ||
217 | |||
218 | void ConfigPage::slotFamilyChanged( int pos ) | ||
219 | { | ||
220 | if ( pos < 0 || pos >= (int)mFamilyMap.count() ) | ||
221 | return; | ||
222 | |||
223 | saveResourceSettings(); | ||
224 | |||
225 | mFamily = mFamilyMap[ pos ]; | ||
226 | |||
227 | //US qDebug("ConfigPage::slotFamilyChanged 4 family=%s", mFamily.latin1()); | ||
228 | |||
229 | mCurrentManager = mInfoMap[ pos ].mManager; | ||
230 | mCurrentConfig = mInfoMap[ pos ].mConfig; | ||
231 | |||
232 | if ( !mCurrentManager ) | ||
233 | kdDebug(5650) << "ERROR: cannot create ResourceManager<Resource>( mFamily )" << endl; | ||
234 | |||
235 | mListView->clear(); | ||
236 | |||
237 | if ( mCurrentManager->isEmpty() ) { | ||
238 | //US qDebug("ConfigPage::slotFamilyChanged 4.1 mCurrentManager=%ul", mCurrentManager ); | ||
239 | |||
240 | defaults(); | ||
241 | } | ||
242 | |||
243 | Resource *standardResource = mCurrentManager->standardResource(); | ||
244 | |||
245 | //US qDebug("ConfigPage::slotFamilyChanged 4.4 resourcename=%s", standardResource->resourceName().latin1()); | ||
246 | |||
247 | |||
248 | Manager<Resource>::Iterator it; | ||
249 | for ( it = mCurrentManager->begin(); it != mCurrentManager->end(); ++it ) { | ||
250 | ConfigViewItem *item = new ConfigViewItem( mListView, *it ); | ||
251 | if ( *it == standardResource ) | ||
252 | item->setStandard( true ); | ||
253 | } | ||
254 | |||
255 | if ( mListView->childCount() == 0 ) { | ||
256 | //US qDebug("ConfigPage::slotFamilyChanged 4.5 "); | ||
257 | |||
258 | defaults(); | ||
259 | emit changed( true ); | ||
260 | mCurrentManager->writeConfig( mCurrentConfig ); | ||
261 | } else { | ||
262 | //US qDebug("ConfigPage::slotFamilyChanged 4.6 "); | ||
263 | |||
264 | if ( !standardResource ) { | ||
265 | KMessageBox::sorry( this, i18n( "There is no standard resource! Please select one." ) ); | ||
266 | |||
267 | //US qDebug("ConfigPage::slotFamilyChanged 4.7" ); | ||
268 | |||
269 | } | ||
270 | |||
271 | emit changed( false ); | ||
272 | } | ||
273 | } | ||
274 | |||
275 | void ConfigPage::slotAdd() | ||
276 | { | ||
277 | if ( !mCurrentManager ) | ||
278 | return; | ||
279 | |||
280 | QStringList types = mCurrentManager->resourceTypeNames(); | ||
281 | QStringList descs = mCurrentManager->resourceTypeDescriptions(); | ||
282 | bool ok = false; | ||
283 | QString desc = QInputDialog::getItem( i18n( "Resource Configuration" ), | ||
284 | i18n( "Please select type of the new resource:" ), descs, 0, | ||
285 | false, &ok, this ); | ||
286 | if ( !ok ) | ||
287 | return; | ||
288 | |||
289 | QString type = types[ descs.findIndex( desc ) ]; | ||
290 | |||
291 | // Create new resource | ||
292 | Resource *resource = mCurrentManager->createResource( type ); | ||
293 | if ( !resource ) { | ||
294 | KMessageBox::error( this, i18n("Unable to create resource of type '%1'.") | ||
295 | .arg( type ) ); | ||
296 | return; | ||
297 | } | ||
298 | |||
299 | resource->setResourceName( type + "-resource" ); | ||
300 | |||
301 | ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" ); | ||
302 | |||
303 | if ( dlg.exec() ) { | ||
304 | mCurrentManager->add( resource ); | ||
305 | |||
306 | ConfigViewItem *item = new ConfigViewItem( mListView, resource ); | ||
307 | |||
308 | mLastItem = item; | ||
309 | |||
310 | // if there are only read-only resources we'll set this resource | ||
311 | // as standard resource | ||
312 | if ( !resource->readOnly() ) { | ||
313 | bool onlyReadOnly = true; | ||
314 | QListViewItem *it = mListView->firstChild(); | ||
315 | while ( it != 0 ) { | ||
316 | ConfigViewItem *confIt = static_cast<ConfigViewItem*>( it ); | ||
317 | if ( !confIt->readOnly() && confIt != item ) | ||
318 | onlyReadOnly = false; | ||
319 | |||
320 | it = it->itemBelow(); | ||
321 | } | ||
322 | |||
323 | if ( onlyReadOnly ) | ||
324 | item->setStandard( true ); | ||
325 | } | ||
326 | |||
327 | emit changed( true ); | ||
328 | } else { | ||
329 | delete resource; | ||
330 | resource = 0; | ||
331 | } | ||
332 | } | ||
333 | |||
334 | void ConfigPage::slotRemove() | ||
335 | { | ||
336 | if ( !mCurrentManager ) | ||
337 | return; | ||
338 | |||
339 | QListViewItem *item = mListView->currentItem(); | ||
340 | ConfigViewItem *confItem = static_cast<ConfigViewItem*>( item ); | ||
341 | |||
342 | if ( !confItem ) | ||
343 | return; | ||
344 | |||
345 | if ( confItem->standard() ) { | ||
346 | KMessageBox::sorry( this, i18n( "You cannot remove your standard resource!\n Please select a new standard resource first." ) ); | ||
347 | return; | ||
348 | } | ||
349 | |||
350 | mCurrentManager->remove( confItem->resource() ); | ||
351 | |||
352 | if ( item == mLastItem ) | ||
353 | mLastItem = 0; | ||
354 | |||
355 | mListView->takeItem( item ); | ||
356 | delete item; | ||
357 | |||
358 | emit changed( true ); | ||
359 | } | ||
360 | |||
361 | void ConfigPage::slotEdit() | ||
362 | { | ||
363 | if ( !mCurrentManager ) | ||
364 | return; | ||
365 | |||
366 | QListViewItem *item = mListView->currentItem(); | ||
367 | ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item ); | ||
368 | if ( !configItem ) | ||
369 | return; | ||
370 | |||
371 | Resource *resource = configItem->resource(); | ||
372 | |||
373 | ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" ); | ||
374 | |||
375 | if ( dlg.exec() ) { | ||
376 | configItem->setText( 0, resource->resourceName() ); | ||
377 | configItem->setText( 1, resource->type() ); | ||
378 | |||
379 | if ( configItem->standard() && configItem->readOnly() ) { | ||
380 | KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) ); | ||
381 | configItem->setStandard( false ); | ||
382 | } | ||
383 | |||
384 | mCurrentManager->resourceChanged( resource ); | ||
385 | emit changed( true ); | ||
386 | } | ||
387 | } | ||
388 | |||
389 | void ConfigPage::slotStandard() | ||
390 | { | ||
391 | if ( !mCurrentManager ) | ||
392 | return; | ||
393 | |||
394 | ConfigViewItem *item = static_cast<ConfigViewItem*>( mListView->currentItem() ); | ||
395 | if ( !item ) | ||
396 | return; | ||
397 | |||
398 | if ( item->readOnly() ) { | ||
399 | KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) ); | ||
400 | return; | ||
401 | } | ||
402 | |||
403 | if ( !item->isOn() ) { | ||
404 | KMessageBox::sorry( this, i18n( "You cannot use an inactive resource as standard!" ) ); | ||
405 | return; | ||
406 | } | ||
407 | |||
408 | QListViewItem *it = mListView->firstChild(); | ||
409 | while ( it != 0 ) { | ||
410 | ConfigViewItem *configItem = static_cast<ConfigViewItem*>( it ); | ||
411 | if ( configItem->standard() ) | ||
412 | configItem->setStandard( false ); | ||
413 | it = it->itemBelow(); | ||
414 | } | ||
415 | |||
416 | item->setStandard( true ); | ||
417 | mCurrentManager->setStandardResource( item->resource() ); | ||
418 | emit changed( true ); | ||
419 | |||
420 | } | ||
421 | |||
422 | void ConfigPage::slotSelectionChanged() | ||
423 | { | ||
424 | bool state = ( mListView->currentItem() != 0 ); | ||
425 | |||
426 | mRemoveButton->setEnabled( state ); | ||
427 | mEditButton->setEnabled( state ); | ||
428 | mStandardButton->setEnabled( state ); | ||
429 | } | ||
430 | |||
431 | void ConfigPage::resourceAdded( Resource* resource ) | ||
432 | { | ||
433 | qDebug("ConfigPage::resourceAdded : %s", resource->resourceName().latin1()); | ||
434 | kdDebug(5650) << "ConfigPage::resourceAdded( " << resource->resourceName() << " )" << endl; | ||
435 | ConfigViewItem *item = new ConfigViewItem( mListView, resource ); | ||
436 | |||
437 | // FIXME: this sucks. This should be in the config file, | ||
438 | // or application-dependent, in which case it's always Off | ||
439 | item->setOn( false ); | ||
440 | |||
441 | mLastItem = item; | ||
442 | |||
443 | emit changed( true ); | ||
444 | } | ||
445 | |||
446 | void ConfigPage::resourceModified( Resource* resource ) | ||
447 | { | ||
448 | qDebug("ConfigPage::resourceModified : %s", resource->resourceName().latin1()); | ||
449 | kdDebug(5650) << "ConfigPage::resourceModified( " << resource->resourceName() << " )" << endl; | ||
450 | } | ||
451 | |||
452 | void ConfigPage::resourceDeleted( Resource* resource ) | ||
453 | { | ||
454 | qDebug("ConfigPage::resourceDeleted : %s", resource->resourceName().latin1()); | ||
455 | kdDebug(5650) << "ConfigPage::resourceDeleted( " << resource->resourceName() << " )" << endl; | ||
456 | } | ||
457 | |||
458 | void ConfigPage::slotItemClicked( QListViewItem *item ) | ||
459 | { | ||
460 | ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item ); | ||
461 | if ( !configItem ) return; | ||
462 | |||
463 | if ( configItem->standard() && !configItem->isOn() ) { | ||
464 | KMessageBox::sorry( this, i18n( "You cannot deactivate the standard resource. Choose another standard resource first." ) ); | ||
465 | configItem->setOn( true ); | ||
466 | return; | ||
467 | } | ||
468 | |||
469 | if ( configItem->isOn() != configItem->resource()->isActive() ) { | ||
470 | emit changed( true ); | ||
471 | } | ||
472 | } | ||
473 | |||
474 | void ConfigPage::saveResourceSettings() | ||
475 | { | ||
476 | qDebug("ConfigPage::saveResourceSettings() begin"); | ||
477 | |||
478 | if ( mCurrentManager ) { | ||
479 | |||
480 | QListViewItem *item = mListView->firstChild(); | ||
481 | while ( item ) { | ||
482 | ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item ); | ||
483 | |||
484 | // check if standard resource | ||
485 | if ( configItem->standard() && !configItem->readOnly() && | ||
486 | configItem->isOn() ) { | ||
487 | |||
488 | mCurrentManager->setStandardResource( configItem->resource() ); | ||
489 | } | ||
490 | |||
491 | // check if active or passive resource | ||
492 | configItem->resource()->setActive( configItem->isOn() ); | ||
493 | |||
494 | item = item->nextSibling(); | ||
495 | } | ||
496 | mCurrentManager->writeConfig( mCurrentConfig ); | ||
497 | |||
498 | if ( !mCurrentManager->standardResource() ) | ||
499 | KMessageBox::sorry( this, i18n( "There is no valid standard resource! Please select one which is neither read-only nor inactive." ) ); | ||
500 | } | ||
501 | |||
502 | qDebug("ConfigPage::saveResourceSettings() end"); | ||
503 | |||
504 | } | ||
505 | |||
506 | //US #include "configpage.moc" | ||
507 | |||
diff --git a/microkde/kresources/configpage.h b/microkde/kresources/configpage.h new file mode 100644 index 0000000..492ea54 --- a/dev/null +++ b/microkde/kresources/configpage.h | |||
@@ -0,0 +1,103 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> | ||
4 | Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> | ||
5 | |||
6 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | ||
10 | |||
11 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
19 | Boston, MA 02111-1307, USA. | ||
20 | */ | ||
21 | |||
22 | |||
23 | #ifndef KRESOURCES_CONFIGPAGE_H | ||
24 | #define KRESOURCES_CONFIGPAGE_H | ||
25 | |||
26 | #include <qstringlist.h> | ||
27 | #include <qwidget.h> | ||
28 | |||
29 | #include "manager.h" | ||
30 | |||
31 | class KComboBox; | ||
32 | class KListView; | ||
33 | |||
34 | class QListViewItem; | ||
35 | class QPushButton; | ||
36 | |||
37 | |||
38 | namespace KRES { | ||
39 | |||
40 | class ResourcePageInfo | ||
41 | { | ||
42 | public: | ||
43 | Manager<Resource> *mManager; | ||
44 | KConfig *mConfig; | ||
45 | }; | ||
46 | |||
47 | class Resource; | ||
48 | |||
49 | class ConfigPage : public QWidget, public ManagerListener<Resource> | ||
50 | { | ||
51 | Q_OBJECT | ||
52 | |||
53 | public: | ||
54 | ConfigPage( QWidget *parent = 0, const char *name = 0 ); | ||
55 | virtual ~ConfigPage(); | ||
56 | |||
57 | void load(); | ||
58 | void save(); | ||
59 | virtual void defaults(); | ||
60 | |||
61 | public slots: | ||
62 | void slotFamilyChanged( int ); | ||
63 | void slotAdd(); | ||
64 | void slotRemove(); | ||
65 | void slotEdit(); | ||
66 | void slotStandard(); | ||
67 | void slotSelectionChanged(); | ||
68 | |||
69 | // From ManagerListener<Resource> | ||
70 | public: | ||
71 | virtual void resourceAdded( Resource* resource ); | ||
72 | virtual void resourceModified( Resource* resource ); | ||
73 | virtual void resourceDeleted( Resource* resource ); | ||
74 | |||
75 | protected slots: | ||
76 | void slotItemClicked( QListViewItem * ); | ||
77 | |||
78 | signals: | ||
79 | void changed( bool ); | ||
80 | |||
81 | private: | ||
82 | void saveResourceSettings(); | ||
83 | |||
84 | Manager<Resource>* mCurrentManager; | ||
85 | KConfig* mCurrentConfig; | ||
86 | KConfig* mConfig; | ||
87 | QString mFamily; | ||
88 | QStringList mFamilyMap; | ||
89 | QValueList<ResourcePageInfo> mInfoMap; | ||
90 | |||
91 | KComboBox* mFamilyCombo; | ||
92 | KListView* mListView; | ||
93 | QPushButton* mAddButton; | ||
94 | QPushButton* mRemoveButton; | ||
95 | QPushButton* mEditButton; | ||
96 | QPushButton* mStandardButton; | ||
97 | |||
98 | QListViewItem* mLastItem; | ||
99 | }; | ||
100 | |||
101 | } | ||
102 | |||
103 | #endif | ||
diff --git a/microkde/kresources/configwidget.cpp b/microkde/kresources/configwidget.cpp new file mode 100644 index 0000000..c42cbd4 --- a/dev/null +++ b/microkde/kresources/configwidget.cpp | |||
@@ -0,0 +1,45 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 "configwidget.h" | ||
25 | |||
26 | using namespace KRES; | ||
27 | |||
28 | ConfigWidget::ConfigWidget( QWidget *parent, const char *name ) | ||
29 | : QWidget( parent, name ) | ||
30 | { | ||
31 | } | ||
32 | |||
33 | void ConfigWidget::setInEditMode( bool ) | ||
34 | { | ||
35 | } | ||
36 | |||
37 | void ConfigWidget::loadSettings( Resource * ) | ||
38 | { | ||
39 | } | ||
40 | |||
41 | void ConfigWidget::saveSettings( Resource * ) | ||
42 | { | ||
43 | } | ||
44 | |||
45 | //US #include "configwidget.moc" | ||
diff --git a/microkde/kresources/configwidget.h b/microkde/kresources/configwidget.h new file mode 100644 index 0000000..04dd696 --- a/dev/null +++ b/microkde/kresources/configwidget.h | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> | ||
4 | Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> | ||
5 | |||
6 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | ||
10 | |||
11 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
19 | Boston, MA 02111-1307, USA. | ||
20 | */ | ||
21 | |||
22 | #ifndef KRESOURCES_CONFIGWIDGET_H | ||
23 | #define KRESOURCES_CONFIGWIDGET_H | ||
24 | |||
25 | #include <qwidget.h> | ||
26 | |||
27 | #include <kconfig.h> | ||
28 | |||
29 | #include "resource.h" | ||
30 | |||
31 | namespace KRES { | ||
32 | |||
33 | class ConfigWidget : public QWidget | ||
34 | { | ||
35 | Q_OBJECT | ||
36 | |||
37 | public: | ||
38 | ConfigWidget( QWidget *parent = 0, const char *name = 0 ); | ||
39 | |||
40 | /** | ||
41 | Sets the widget to 'edit' mode. Reimplement this method if you are | ||
42 | interested in the mode change (to disable some GUI element for | ||
43 | example). By default the widget is in 'create new' mode. | ||
44 | */ | ||
45 | virtual void setInEditMode( bool value ); | ||
46 | |||
47 | public slots: | ||
48 | virtual void loadSettings( Resource *resource ); | ||
49 | virtual void saveSettings( Resource *resource ); | ||
50 | |||
51 | signals: | ||
52 | void setReadOnly( bool value ); | ||
53 | |||
54 | protected: | ||
55 | Resource* mResource; | ||
56 | }; | ||
57 | |||
58 | } | ||
59 | #endif | ||
diff --git a/microkde/kresources/factory.cpp b/microkde/kresources/factory.cpp new file mode 100644 index 0000000..709cd4a --- a/dev/null +++ b/microkde/kresources/factory.cpp | |||
@@ -0,0 +1,216 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 <klocale.h> | ||
26 | #include <ksimpleconfig.h> | ||
27 | #include <kstandarddirs.h> | ||
28 | #include <kstaticdeleter.h> | ||
29 | |||
30 | #include <qfile.h> | ||
31 | |||
32 | #include <plugins/file/resourcefile.h> | ||
33 | #include <plugins/file/resourcefileconfig.h> | ||
34 | #include <plugins/dir/resourcedir.h> | ||
35 | #include <plugins/dir/resourcedirconfig.h> | ||
36 | //#include <plugins/ldap/resourceldap.h> | ||
37 | //#include <plugins/ldap/resourceldapconfig.h> | ||
38 | |||
39 | |||
40 | #include "resource.h" | ||
41 | #include "factory.h" | ||
42 | |||
43 | using namespace KRES; | ||
44 | |||
45 | QDict<Factory> *Factory::mSelves = 0; | ||
46 | static KStaticDeleter< QDict<Factory> > staticDeleter; | ||
47 | |||
48 | Factory *Factory::self( const QString& resourceFamily ) | ||
49 | { | ||
50 | kdDebug(5650) << "Factory::self()" << endl; | ||
51 | |||
52 | Factory *factory = 0; | ||
53 | if ( !mSelves ) | ||
54 | { | ||
55 | mSelves = staticDeleter.setObject( new QDict<Factory> ); | ||
56 | } | ||
57 | |||
58 | factory = mSelves->find( resourceFamily ); | ||
59 | |||
60 | if ( !factory ) { | ||
61 | factory = new Factory( resourceFamily ); | ||
62 | mSelves->insert( resourceFamily, factory ); | ||
63 | } | ||
64 | |||
65 | return factory; | ||
66 | } | ||
67 | |||
68 | Factory::Factory( const QString& resourceFamily ) : | ||
69 | mResourceFamily( resourceFamily ) | ||
70 | { | ||
71 | //US so far we have three types available for resourceFamily "contact" | ||
72 | // and that are "file", "dir", "ldap" | ||
73 | /*US | ||
74 | |||
75 | KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin", QString( "[X-KDE-ResourceFamily] == '%1'" ) | ||
76 | .arg( resourceFamily ) ); | ||
77 | KTrader::OfferList::ConstIterator it; | ||
78 | for ( it = plugins.begin(); it != plugins.end(); ++it ) { | ||
79 | QVariant type = (*it)->property( "X-KDE-ResourceType" ); | ||
80 | if ( !type.toString().isEmpty() ) | ||
81 | mTypeMap.insert( type.toString(), *it ); | ||
82 | } | ||
83 | */ | ||
84 | |||
85 | //US !!!!!!!!!!!!!!! | ||
86 | KRES::PluginFactoryBase* pf = (KRES::PluginFactoryBase*)new KRES::PluginFactory<KABC::ResourceFile,KABC::ResourceFileConfig>(); | ||
87 | mTypeMap.insert( "file", pf ); | ||
88 | |||
89 | pf = (KRES::PluginFactoryBase*)new KRES::PluginFactory<KABC::ResourceDir,KABC::ResourceDirConfig>(); | ||
90 | mTypeMap.insert( "dir", pf ); | ||
91 | /* | ||
92 | pf = (KRES::PluginFactoryBase*)new KRES::PluginFactory<KABC::ResourceLDAP,KABC::ResourceLDAPConfig>(); | ||
93 | mTypeMap.insert( "ldap", pf ); | ||
94 | */ | ||
95 | } | ||
96 | |||
97 | Factory::~Factory() | ||
98 | { | ||
99 | } | ||
100 | |||
101 | QStringList Factory::typeNames() const | ||
102 | { | ||
103 | //US method QMap::keys() not available yet. SO collect the data manually | ||
104 | //US return mTypeMap.keys(); | ||
105 | |||
106 | QStringList result; | ||
107 | |||
108 | QMap<QString, PluginFactoryBase*>::ConstIterator it; | ||
109 | for( it = mTypeMap.begin(); it != mTypeMap.end(); ++it ) { | ||
110 | result << it.key().latin1(); | ||
111 | // qDebug("Factory::typeNames() : %s ", it.key().latin1()); | ||
112 | |||
113 | } | ||
114 | return result; | ||
115 | } | ||
116 | |||
117 | ConfigWidget *Factory::configWidget( const QString& type, QWidget *parent ) | ||
118 | { | ||
119 | if ( type.isEmpty() || !mTypeMap.contains( type ) ) | ||
120 | return 0; | ||
121 | |||
122 | /*US load the lib not dynamically. !! | ||
123 | KService::Ptr ptr = mTypeMap[ type ]; | ||
124 | KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() ); | ||
125 | if ( !factory ) { | ||
126 | kdDebug() << "KRES::Factory::configWidget(): Factory creation failed" << endl; | ||
127 | return 0; | ||
128 | } | ||
129 | */ | ||
130 | PluginFactoryBase *factory = mTypeMap[ type ]; | ||
131 | if ( !factory ) { | ||
132 | kdDebug() << "KRES::Factory::configWidget(): Factory creation failed" << endl; | ||
133 | return 0; | ||
134 | } | ||
135 | |||
136 | |||
137 | PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory ); | ||
138 | |||
139 | if ( !pluginFactory ) { | ||
140 | kdDebug() << "KRES::Factory::configWidget(): no plugin factory." << endl; | ||
141 | return 0; | ||
142 | } | ||
143 | |||
144 | ConfigWidget *wdg = pluginFactory->configWidget( parent ); | ||
145 | if ( !wdg ) { | ||
146 | //US kdDebug() << "'" << ptr->library() << "' is not a " + mResourceFamily + " plugin." << endl; | ||
147 | kdDebug() << " is not a " + mResourceFamily + " plugin." << endl; | ||
148 | return 0; | ||
149 | } | ||
150 | return wdg; | ||
151 | |||
152 | } | ||
153 | |||
154 | QString Factory::typeName( const QString &type ) const | ||
155 | { | ||
156 | if ( type.isEmpty() || !mTypeMap.contains( type ) ) | ||
157 | return QString(); | ||
158 | |||
159 | //US KService::Ptr ptr = mTypeMap[ type ]; | ||
160 | //US return ptr->name(); | ||
161 | //US I guess this is correct since we loaded the factory staticly. | ||
162 | return type; | ||
163 | |||
164 | } | ||
165 | |||
166 | QString Factory::typeDescription( const QString &type ) const | ||
167 | { | ||
168 | if ( type.isEmpty() || !mTypeMap.contains( type ) ) | ||
169 | return QString(); | ||
170 | |||
171 | //US KService::Ptr ptr = mTypeMap[ type ]; | ||
172 | //US return ptr->comment(); | ||
173 | //US I guess this is correct since we loaded the factory staticly. | ||
174 | return type; | ||
175 | |||
176 | } | ||
177 | |||
178 | Resource *Factory::resource( const QString& type, const KConfig *config ) | ||
179 | { | ||
180 | kdDebug() << "Factory::resource( " << type << ", config)" << endl; | ||
181 | |||
182 | if ( type.isEmpty() || !mTypeMap.contains( type ) ) | ||
183 | return 0; | ||
184 | |||
185 | /*US load the lib not dynamicly. !! | ||
186 | KService::Ptr ptr = mTypeMap[ type ]; | ||
187 | KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() ); | ||
188 | if ( !factory ) { | ||
189 | kdDebug() << "KRES::Factory::resource(): Factory creation failed" << endl; | ||
190 | return 0; | ||
191 | } | ||
192 | */ | ||
193 | PluginFactoryBase *factory = mTypeMap[ type ]; | ||
194 | if ( !factory ) { | ||
195 | kdDebug() << "KRES::Factory::resource(): Factory creation failed" << endl; | ||
196 | return 0; | ||
197 | } | ||
198 | |||
199 | PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory ); | ||
200 | |||
201 | if ( !pluginFactory ) { | ||
202 | kdDebug() << "KRES::Factory::resource(): no plugin factory." << endl; | ||
203 | return 0; | ||
204 | } | ||
205 | |||
206 | Resource *resource = pluginFactory->resource( config ); | ||
207 | if ( !resource ) { | ||
208 | //US kdDebug() << "'" << ptr->library() << "' is not a " + mResourceFamily + " plugin." << endl; | ||
209 | kdDebug() << " is not a " + mResourceFamily + " plugin." << endl; | ||
210 | return 0; | ||
211 | } | ||
212 | |||
213 | resource->setType( type ); | ||
214 | |||
215 | return resource; | ||
216 | } | ||
diff --git a/microkde/kresources/factory.h b/microkde/kresources/factory.h new file mode 100644 index 0000000..f391bb3 --- a/dev/null +++ b/microkde/kresources/factory.h | |||
@@ -0,0 +1,113 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 | #ifndef KRESOURCES_FACTORY_H | ||
25 | #define KRESOURCES_FACTORY_H | ||
26 | |||
27 | #include <qdict.h> | ||
28 | #include <qstring.h> | ||
29 | |||
30 | #include <kconfig.h> | ||
31 | |||
32 | |||
33 | #include "resource.h" | ||
34 | |||
35 | namespace KRES { | ||
36 | |||
37 | /** | ||
38 | * Class for loading resource plugins. | ||
39 | * Do not use this class directly. Use ResourceManager instead | ||
40 | * | ||
41 | * Example: | ||
42 | * | ||
43 | * <pre> | ||
44 | * KABC::Factory<Calendar> *factory = KABC::Factory<Calendar>::self(); | ||
45 | * | ||
46 | * QStringList list = factory->resources(); | ||
47 | * QStringList::Iterator it; | ||
48 | * for ( it = list.begin(); it != list.end(); ++it ) { | ||
49 | * Resource<Calendar> *resource = factory->resource( (*it), | ||
50 | * KABC::StdAddressBook::self(), 0 ); | ||
51 | * // do something with resource | ||
52 | * } | ||
53 | * </pre> | ||
54 | */ | ||
55 | class Factory | ||
56 | { | ||
57 | public: | ||
58 | |||
59 | /** | ||
60 | * Returns the global resource factory. | ||
61 | */ | ||
62 | static Factory *self( const QString& resourceFamily ); | ||
63 | |||
64 | ~Factory(); | ||
65 | |||
66 | /** | ||
67 | * Returns the config widget for the given resource type, | ||
68 | * or a null pointer if resource type doesn't exist. | ||
69 | * | ||
70 | * @param type The type of the resource, returned by @ref resources() | ||
71 | * @param resource The resource to be editted. | ||
72 | * @param parent The parent widget | ||
73 | */ | ||
74 | ConfigWidget *configWidget( const QString& type, QWidget *parent = 0 ); | ||
75 | |||
76 | /** | ||
77 | * Returns a pointer to a resource object or a null pointer | ||
78 | * if resource type doesn't exist. | ||
79 | * | ||
80 | * @param type The type of the resource, returned by @ref resources() | ||
81 | * @param ab The address book, the resource should belong to | ||
82 | * @param config The config object where the resource get it settings from, or 0 if a new resource should be created. | ||
83 | */ | ||
84 | Resource *resource( const QString& type, const KConfig *config ); | ||
85 | |||
86 | /** | ||
87 | * Returns a list of all available resource types. | ||
88 | */ | ||
89 | QStringList typeNames() const; | ||
90 | |||
91 | /** | ||
92 | * Returns the name for a special type. | ||
93 | */ | ||
94 | QString typeName( const QString &type ) const; | ||
95 | |||
96 | /** | ||
97 | * Returns the description for a special type. | ||
98 | */ | ||
99 | QString typeDescription( const QString &type ) const; | ||
100 | |||
101 | protected: | ||
102 | Factory( const QString& resourceFamily ); | ||
103 | |||
104 | private: | ||
105 | static QDict<Factory> *mSelves; | ||
106 | |||
107 | QString mResourceFamily; | ||
108 | //US QMap<QString, KService::Ptr> mTypeMap; | ||
109 | QMap<QString, PluginFactoryBase*> mTypeMap; | ||
110 | }; | ||
111 | |||
112 | } | ||
113 | #endif | ||
diff --git a/microkde/kresources/kcmkresources.cpp b/microkde/kresources/kcmkresources.cpp new file mode 100644 index 0000000..d600a31 --- a/dev/null +++ b/microkde/kresources/kcmkresources.cpp | |||
@@ -0,0 +1,89 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> | ||
5 | |||
6 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | ||
10 | |||
11 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
19 | Boston, MA 02111-1307, USA. | ||
20 | */ | ||
21 | |||
22 | #include <qlayout.h> | ||
23 | |||
24 | //US #include <kaboutdata.h> | ||
25 | //US #include <kgenericfactory.h> | ||
26 | #include <klocale.h> | ||
27 | |||
28 | #include "configpage.h" | ||
29 | |||
30 | #include "kcmkresources.h" | ||
31 | |||
32 | using namespace KRES; | ||
33 | |||
34 | //US typedef KGenericFactory<KCMKResources, QWidget> ResourcesFactory; | ||
35 | //US K_EXPORT_COMPONENT_FACTORY( kcm_kresources, ResourcesFactory( "kcmkresources" ) ); | ||
36 | |||
37 | //US KCMKResources::KCMKResources( QWidget *parent, const char *name, const QStringList& ) | ||
38 | //US : KCModule( ResourcesFactory::instance(), parent, name ) | ||
39 | KCMKResources::KCMKResources( QWidget *parent, const char *name, const QStringList& ) | ||
40 | : KDialogBase( parent, name, true, i18n( "Configure Resources" ), | ||
41 | Ok|Cancel, Ok, true ) | ||
42 | { | ||
43 | QFrame *main = plainPage(); | ||
44 | |||
45 | QVBoxLayout *layout = new QVBoxLayout( main ); | ||
46 | mConfigPage = new KRES::ConfigPage( main ); | ||
47 | layout->addWidget( mConfigPage ); | ||
48 | |||
49 | |||
50 | connect( mConfigPage, SIGNAL( changed( bool ) ), SLOT( changed( bool ) ) ); | ||
51 | #ifndef DESKTOP_VERSION | ||
52 | showMaximized(); | ||
53 | #endif | ||
54 | } | ||
55 | |||
56 | void KCMKResources::changed( bool changed) | ||
57 | { | ||
58 | modified = changed; | ||
59 | } | ||
60 | |||
61 | void KCMKResources::slotOk() | ||
62 | { | ||
63 | if (modified) { | ||
64 | mConfigPage->save(); | ||
65 | modified = false; | ||
66 | } | ||
67 | |||
68 | KDialogBase::slotOk(); | ||
69 | } | ||
70 | |||
71 | void KCMKResources::load() | ||
72 | { | ||
73 | qDebug("KCMKResources::load" ); | ||
74 | mConfigPage->load(); | ||
75 | } | ||
76 | |||
77 | void KCMKResources::save() | ||
78 | { | ||
79 | qDebug("KCMKResources::save" ); | ||
80 | mConfigPage->save(); | ||
81 | } | ||
82 | |||
83 | void KCMKResources::defaults() | ||
84 | { | ||
85 | qDebug("KCMKResources::defaults" ); | ||
86 | mConfigPage->defaults(); | ||
87 | } | ||
88 | |||
89 | //US #include "kcmkresources.moc" | ||
diff --git a/microkde/kresources/kcmkresources.h b/microkde/kresources/kcmkresources.h new file mode 100644 index 0000000..a83bb33 --- a/dev/null +++ b/microkde/kresources/kcmkresources.h | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> | ||
5 | |||
6 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | ||
10 | |||
11 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
19 | Boston, MA 02111-1307, USA. | ||
20 | */ | ||
21 | |||
22 | #ifndef KRESOURCES_KCMKRESOURCES_H | ||
23 | #define KRESOURCES_KCMKRESOURCES_H | ||
24 | |||
25 | #include <kdialogbase.h> | ||
26 | |||
27 | namespace KRES { | ||
28 | |||
29 | class ConfigPage; | ||
30 | |||
31 | |||
32 | //US class KCMKResources : public KCModule | ||
33 | class KCMKResources : public KDialogBase | ||
34 | { | ||
35 | Q_OBJECT | ||
36 | |||
37 | public: | ||
38 | KCMKResources( QWidget *parent, const char *name, const QStringList& ); | ||
39 | |||
40 | void load(); | ||
41 | void save(); | ||
42 | void defaults(); | ||
43 | |||
44 | protected slots: | ||
45 | virtual void slotOk(); | ||
46 | void changed( bool ); | ||
47 | |||
48 | private: | ||
49 | KRES::ConfigPage *mConfigPage; | ||
50 | bool modified; | ||
51 | }; | ||
52 | |||
53 | } | ||
54 | #endif | ||
diff --git a/microkde/kresources/manager.h b/microkde/kresources/manager.h new file mode 100644 index 0000000..b5e97fc --- a/dev/null +++ b/microkde/kresources/manager.h | |||
@@ -0,0 +1,332 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 | #ifndef KRESOURCES_MANAGER_H | ||
25 | #define KRESOURCES_MANAGER_H | ||
26 | |||
27 | #include <qdict.h> | ||
28 | #include <qstringlist.h> | ||
29 | |||
30 | #include "factory.h" | ||
31 | #include "managerimpl.h" | ||
32 | |||
33 | namespace KRES { | ||
34 | |||
35 | class Resource; | ||
36 | |||
37 | template<class T> | ||
38 | class ManagerListener | ||
39 | { | ||
40 | public: | ||
41 | virtual void resourceAdded( T *resource ) = 0; | ||
42 | virtual void resourceModified( T *resource ) = 0; | ||
43 | virtual void resourceDeleted( T *resource ) = 0; | ||
44 | }; | ||
45 | |||
46 | // TODO: | ||
47 | // The resource manager should provide some signals | ||
48 | // to warn applications that resources have been added, | ||
49 | // removed or modified. | ||
50 | // | ||
51 | // The manager should also keep track of which (or at least | ||
52 | // how many) applications hve opened a resource, so that it | ||
53 | // is only closed if none of them is using it any more | ||
54 | |||
55 | template<class T> | ||
56 | class Manager : private ManagerImplListener | ||
57 | { | ||
58 | public: | ||
59 | class Iterator | ||
60 | { | ||
61 | friend class Manager; | ||
62 | public: | ||
63 | Iterator() {}; | ||
64 | Iterator( const Iterator &it ) { mIt = it.mIt; } | ||
65 | |||
66 | T *operator*() { return static_cast<T *>( *mIt ); } | ||
67 | Iterator &operator++() { mIt++; return *this; } | ||
68 | Iterator &operator++(int) { mIt++; return *this; } | ||
69 | Iterator &operator--() { mIt--; return *this; } | ||
70 | Iterator &operator--(int) { mIt--; return *this; } | ||
71 | bool operator==( const Iterator &it ) { return mIt == it.mIt; } | ||
72 | bool operator!=( const Iterator &it ) { return mIt != it.mIt; } | ||
73 | |||
74 | private: | ||
75 | Resource::List::Iterator mIt; | ||
76 | }; | ||
77 | |||
78 | Iterator begin() | ||
79 | { | ||
80 | Iterator it; | ||
81 | it.mIt = mImpl->resourceList()->begin(); | ||
82 | return it; | ||
83 | } | ||
84 | |||
85 | Iterator end() | ||
86 | { | ||
87 | Iterator it; | ||
88 | it.mIt = mImpl->resourceList()->end(); | ||
89 | return it; | ||
90 | } | ||
91 | |||
92 | class ActiveIterator | ||
93 | { | ||
94 | friend class Manager; | ||
95 | public: | ||
96 | ActiveIterator() : mList( 0 ) {}; | ||
97 | ActiveIterator( const ActiveIterator &it ) | ||
98 | { | ||
99 | mIt = it.mIt; | ||
100 | mList = it.mList; | ||
101 | } | ||
102 | |||
103 | T *operator*() { return static_cast<T *>( *mIt ); } | ||
104 | ActiveIterator &operator++() | ||
105 | { | ||
106 | do { mIt++; } while ( checkActive() ); | ||
107 | return *this; | ||
108 | } | ||
109 | ActiveIterator &operator++(int) | ||
110 | { | ||
111 | do { mIt++; } while ( checkActive() ); | ||
112 | return *this; | ||
113 | } | ||
114 | ActiveIterator &operator--() | ||
115 | { | ||
116 | do { mIt--; } while ( checkActive() ); | ||
117 | return *this; | ||
118 | } | ||
119 | ActiveIterator &operator--(int) | ||
120 | { | ||
121 | do { mIt--; } while ( checkActive() ); | ||
122 | return *this; | ||
123 | } | ||
124 | bool operator==( const ActiveIterator &it ) { return mIt == it.mIt; } | ||
125 | bool operator!=( const ActiveIterator &it ) { return mIt != it.mIt; } | ||
126 | |||
127 | private: | ||
128 | /** | ||
129 | Check if iterator needs to be advanced once more. | ||
130 | */ | ||
131 | bool checkActive() | ||
132 | { | ||
133 | if ( !mList || mIt == mList->end() ) return false; | ||
134 | return !(*mIt)->isActive(); | ||
135 | } | ||
136 | |||
137 | Resource::List::Iterator mIt; | ||
138 | Resource::List *mList; | ||
139 | }; | ||
140 | |||
141 | ActiveIterator activeBegin() | ||
142 | { | ||
143 | ActiveIterator it; | ||
144 | it.mIt = mImpl->resourceList()->begin(); | ||
145 | it.mList = mImpl->resourceList(); | ||
146 | if ( it.mIt != mImpl->resourceList()->end() ) { | ||
147 | if ( !(*it)->isActive() ) it++; | ||
148 | } | ||
149 | return it; | ||
150 | } | ||
151 | |||
152 | ActiveIterator activeEnd() | ||
153 | { | ||
154 | ActiveIterator it; | ||
155 | it.mIt = mImpl->resourceList()->end(); | ||
156 | it.mList = mImpl->resourceList(); | ||
157 | return it; | ||
158 | } | ||
159 | |||
160 | bool isEmpty() const { return mImpl->resourceList()->isEmpty(); } | ||
161 | |||
162 | Manager( const QString &family ) | ||
163 | { | ||
164 | mFactory = Factory::self( family ); | ||
165 | // The managerimpl will use the same Factory object as the manager | ||
166 | // because of the Factory::self() pattern | ||
167 | mImpl = new ManagerImpl( family ); | ||
168 | mImpl->setListener( this ); | ||
169 | |||
170 | mListeners = new QPtrList<ManagerListener<T> >; | ||
171 | } | ||
172 | |||
173 | virtual ~Manager() | ||
174 | { | ||
175 | mImpl->setListener( 0 ); | ||
176 | delete mListeners; | ||
177 | delete mImpl; | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | Recreate Resource objects from configuration file. If cfg is 0, read standard | ||
182 | configuration file. | ||
183 | */ | ||
184 | void readConfig( KConfig *cfg = 0 ) | ||
185 | { | ||
186 | mImpl->readConfig( cfg ); | ||
187 | } | ||
188 | |||
189 | /** | ||
190 | Write configuration of Resource objects to configuration file. If cfg is 0, write | ||
191 | to standard configuration file. | ||
192 | */ | ||
193 | void writeConfig( KConfig *cfg = 0 ) | ||
194 | { | ||
195 | mImpl->writeConfig( cfg ); | ||
196 | } | ||
197 | |||
198 | /** | ||
199 | Add resource to manager. This passes ownership of the Resource object | ||
200 | to the manager. | ||
201 | */ | ||
202 | void add( Resource *resource ) | ||
203 | { | ||
204 | if ( resource ) mImpl->add( resource ); | ||
205 | } | ||
206 | |||
207 | void remove( Resource *resource ) | ||
208 | { | ||
209 | if ( resource ) mImpl->remove( resource ); | ||
210 | } | ||
211 | |||
212 | T* standardResource() | ||
213 | { | ||
214 | return static_cast<T *>( mImpl->standardResource() ); | ||
215 | } | ||
216 | |||
217 | void setStandardResource( T *resource ) | ||
218 | { | ||
219 | if ( resource ) mImpl->setStandardResource( resource ); | ||
220 | } | ||
221 | |||
222 | void setActive( Resource *resource, bool active ) | ||
223 | { | ||
224 | if ( resource ) mImpl->setActive( resource, active ); | ||
225 | } | ||
226 | |||
227 | /** | ||
228 | Returns a list of the names of the reources managed by the | ||
229 | Manager for this family. | ||
230 | */ | ||
231 | QStringList resourceNames() const | ||
232 | { | ||
233 | return mImpl->resourceNames(); | ||
234 | } | ||
235 | |||
236 | ConfigWidget *configWidget( const QString& type, QWidget *parent = 0 ) | ||
237 | { | ||
238 | return mFactory->resourceConfigWidget( type, parent ); | ||
239 | } | ||
240 | |||
241 | /** | ||
242 | Creates a new resource of type @param type, with default | ||
243 | settings. The resource is | ||
244 | not added to the manager, the application has to do that. | ||
245 | Returns a pointer to a resource object or a null pointer | ||
246 | if resource type doesn't exist. | ||
247 | |||
248 | @param type The type of the resource, one of those returned | ||
249 | by @ref resourceTypeNames() | ||
250 | */ | ||
251 | T *createResource( const QString& type ) | ||
252 | { | ||
253 | return (T *)( mFactory->resource( type, 0 ) ); | ||
254 | } | ||
255 | |||
256 | /** | ||
257 | Returns a list of the names of all available resource types. | ||
258 | */ | ||
259 | QStringList resourceTypeNames() const | ||
260 | { | ||
261 | return mFactory->typeNames(); | ||
262 | } | ||
263 | |||
264 | QStringList resourceTypeDescriptions() const | ||
265 | { | ||
266 | QStringList typeDescs; | ||
267 | QStringList types = mFactory->typeNames(); | ||
268 | |||
269 | for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) { | ||
270 | QString desc = mFactory->typeName( *it ); | ||
271 | if ( !mFactory->typeDescription( *it ).isEmpty() ) | ||
272 | desc += " (" + mFactory->typeDescription( *it ) + ")"; | ||
273 | |||
274 | typeDescs.append( desc ); | ||
275 | } | ||
276 | |||
277 | return typeDescs; | ||
278 | } | ||
279 | |||
280 | void resourceChanged( T *resource ) | ||
281 | { | ||
282 | mImpl->resourceChanged( resource ); | ||
283 | } | ||
284 | |||
285 | void addListener( ManagerListener<T> *listener ) | ||
286 | { | ||
287 | mListeners->append( listener ); | ||
288 | } | ||
289 | |||
290 | void removeListener( ManagerListener<T> *listener ) | ||
291 | { | ||
292 | mListeners->remove( listener ); | ||
293 | } | ||
294 | |||
295 | virtual void resourceAdded( Resource *res ) | ||
296 | { | ||
297 | kdDebug(5650) << "Manager::resourceAdded " << res->resourceName() << endl; | ||
298 | T* resource = (T *)( res ); | ||
299 | ManagerListener<T> *listener; | ||
300 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) | ||
301 | listener->resourceAdded( resource ); | ||
302 | } | ||
303 | |||
304 | virtual void resourceModified( Resource *res ) | ||
305 | { | ||
306 | kdDebug(5650) << "Manager::resourceModified " << res->resourceName() << endl; | ||
307 | T* resource = (T *)( res ); | ||
308 | ManagerListener<T> *listener; | ||
309 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) | ||
310 | listener->resourceModified( resource ); | ||
311 | } | ||
312 | |||
313 | virtual void resourceDeleted( Resource *res ) | ||
314 | { | ||
315 | kdDebug(5650) << "Manager::resourceDeleted " << res->resourceName() << endl; | ||
316 | T* resource = (T *)( res ); | ||
317 | ManagerListener<T> *listener; | ||
318 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) { | ||
319 | kdDebug(5650) << "Notifying a listener to Manager..." << endl; | ||
320 | listener->resourceDeleted( resource ); | ||
321 | } | ||
322 | } | ||
323 | |||
324 | private: | ||
325 | ManagerImpl *mImpl; | ||
326 | Factory *mFactory; | ||
327 | QPtrList<ManagerListener<T> > *mListeners; | ||
328 | }; | ||
329 | |||
330 | } | ||
331 | |||
332 | #endif | ||
diff --git a/microkde/kresources/managerimpl.cpp b/microkde/kresources/managerimpl.cpp new file mode 100644 index 0000000..1baa6be --- a/dev/null +++ b/microkde/kresources/managerimpl.cpp | |||
@@ -0,0 +1,353 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 <kglobal.h> | ||
25 | |||
26 | #include <kapplication.h> | ||
27 | #include <kdebug.h> | ||
28 | #include <kconfig.h> | ||
29 | #include <kstandarddirs.h> | ||
30 | |||
31 | #include "resource.h" | ||
32 | #include "factory.h" | ||
33 | #include "managerimpl.h" | ||
34 | |||
35 | using namespace KRES; | ||
36 | |||
37 | ManagerImpl::ManagerImpl( const QString &family ) | ||
38 | : mFamily( family ), mConfig( 0 ), mStdConfig( 0 ), mStandard( 0 ), | ||
39 | mFactory( 0 ) | ||
40 | |||
41 | { | ||
42 | kdDebug(5650) << "ManagerImpl::ManagerImpl()" << endl; | ||
43 | |||
44 | |||
45 | } | ||
46 | |||
47 | ManagerImpl::~ManagerImpl() | ||
48 | { | ||
49 | kdDebug(5650) << "ManagerImpl::~ManagerImpl()" << endl; | ||
50 | |||
51 | Resource::List::ConstIterator it; | ||
52 | for ( it = mResources.begin(); it != mResources.end(); ++it ) { | ||
53 | delete *it; | ||
54 | } | ||
55 | |||
56 | delete mStdConfig; | ||
57 | } | ||
58 | |||
59 | void ManagerImpl::createStandardConfig() | ||
60 | { | ||
61 | if ( !mStdConfig ) { | ||
62 | QString file = locateLocal( "data", KGlobal::getAppName() | ||
63 | + "/kresources/" + mFamily + "rc" ); | ||
64 | mStdConfig = new KConfig( file ); | ||
65 | } | ||
66 | |||
67 | mConfig = mStdConfig; | ||
68 | } | ||
69 | |||
70 | void ManagerImpl::readConfig( KConfig *cfg ) | ||
71 | { | ||
72 | kdDebug(5650) << "ManagerImpl::readConfig()" << endl; | ||
73 | |||
74 | delete mFactory; | ||
75 | mFactory = Factory::self( mFamily ); | ||
76 | |||
77 | if ( !cfg ) { | ||
78 | createStandardConfig(); | ||
79 | } else { | ||
80 | mConfig = cfg; | ||
81 | } | ||
82 | |||
83 | mStandard = 0; | ||
84 | |||
85 | mConfig->setGroup( "General" ); | ||
86 | |||
87 | QStringList keys = mConfig->readListEntry( "ResourceKeys" ); | ||
88 | keys += mConfig->readListEntry( "PassiveResourceKeys" ); | ||
89 | |||
90 | QString standardKey = mConfig->readEntry( "Standard" ); | ||
91 | |||
92 | for ( QStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) { | ||
93 | readResourceConfig( *it, false ); | ||
94 | } | ||
95 | |||
96 | } | ||
97 | |||
98 | void ManagerImpl::writeConfig( KConfig *cfg ) | ||
99 | { | ||
100 | //USqDebug("ManagerImpl::writeConfig begin this= %ul cfg=%ul", this, cfg); | ||
101 | |||
102 | |||
103 | kdDebug(5650) << "ManagerImpl::writeConfig()" << endl; | ||
104 | |||
105 | if ( !cfg ) { | ||
106 | createStandardConfig(); | ||
107 | } else { | ||
108 | mConfig = cfg; | ||
109 | } | ||
110 | |||
111 | QStringList activeKeys; | ||
112 | QStringList passiveKeys; | ||
113 | |||
114 | // First write all keys, collect active and passive keys on the way | ||
115 | Resource::List::Iterator it; | ||
116 | for ( it = mResources.begin(); it != mResources.end(); ++it ) { | ||
117 | writeResourceConfig( *it, false ); | ||
118 | |||
119 | QString key = (*it)->identifier(); | ||
120 | if( (*it)->isActive() ) | ||
121 | activeKeys.append( key ); | ||
122 | else | ||
123 | passiveKeys.append( key ); | ||
124 | } | ||
125 | |||
126 | // And then the general group | ||
127 | |||
128 | kdDebug(5650) << "Saving general info" << endl; | ||
129 | mConfig->setGroup( "General" ); | ||
130 | mConfig->writeEntry( "ResourceKeys", activeKeys ); | ||
131 | mConfig->writeEntry( "PassiveResourceKeys", passiveKeys ); | ||
132 | if ( mStandard ) | ||
133 | mConfig->writeEntry( "Standard", mStandard->identifier() ); | ||
134 | else | ||
135 | mConfig->writeEntry( "Standard", "" ); | ||
136 | |||
137 | mConfig->sync(); | ||
138 | kdDebug(5650) << "ManagerImpl::save() finished" << endl; | ||
139 | |||
140 | //US qDebug("ManagerImpl::writeConfig end this= %ul cfg=%ul", this, cfg); | ||
141 | |||
142 | } | ||
143 | |||
144 | void ManagerImpl::add( Resource *resource, bool useDCOP ) | ||
145 | { | ||
146 | qDebug("ManagerImpl::add begin this= %ul resource=%ul", this, resource); | ||
147 | |||
148 | resource->setActive( true ); | ||
149 | |||
150 | if ( mResources.isEmpty() ) { | ||
151 | mStandard = resource; | ||
152 | } | ||
153 | |||
154 | mResources.append( resource ); | ||
155 | |||
156 | writeResourceConfig( resource, true ); | ||
157 | |||
158 | qDebug("ManagerImpl::add end this= %ul resource=%ul", this, resource); | ||
159 | |||
160 | } | ||
161 | |||
162 | void ManagerImpl::remove( Resource *resource, bool useDCOP ) | ||
163 | { | ||
164 | if ( mStandard == resource ) mStandard = 0; | ||
165 | removeResource( resource ); | ||
166 | |||
167 | mResources.remove( resource ); | ||
168 | |||
169 | delete resource; | ||
170 | |||
171 | kdDebug(5650) << "Finished ManagerImpl::remove()" << endl; | ||
172 | } | ||
173 | |||
174 | void ManagerImpl::setActive( Resource *resource, bool active ) | ||
175 | { | ||
176 | if ( resource && resource->isActive() != active ) { | ||
177 | resource->setActive( active ); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | Resource *ManagerImpl::standardResource() | ||
182 | { | ||
183 | return mStandard; | ||
184 | } | ||
185 | |||
186 | void ManagerImpl::setStandardResource( Resource *resource ) | ||
187 | { | ||
188 | mStandard = resource; | ||
189 | } | ||
190 | |||
191 | void ManagerImpl::resourceChanged( Resource *resource ) | ||
192 | { | ||
193 | writeResourceConfig( resource, true ); | ||
194 | |||
195 | |||
196 | // ManagerIface_stub allManagers( "*", "ManagerIface_" + mFamily.utf8() ); | ||
197 | // allManagers.dcopResourceModified( resource->identifier() ); | ||
198 | } | ||
199 | |||
200 | // DCOP asynchronous functions | ||
201 | //US since we work from inside the application, we call the methods directly. | ||
202 | |||
203 | QStringList ManagerImpl::resourceNames() | ||
204 | { | ||
205 | QStringList result; | ||
206 | |||
207 | Resource::List::ConstIterator it; | ||
208 | for ( it = mResources.begin(); it != mResources.end(); ++it ) { | ||
209 | result.append( (*it)->resourceName() ); | ||
210 | } | ||
211 | return result; | ||
212 | } | ||
213 | |||
214 | Resource::List *ManagerImpl::resourceList() | ||
215 | { | ||
216 | return &mResources; | ||
217 | } | ||
218 | |||
219 | QPtrList<Resource> ManagerImpl::resources() | ||
220 | { | ||
221 | QPtrList<Resource> result; | ||
222 | |||
223 | Resource::List::ConstIterator it; | ||
224 | for ( it = mResources.begin(); it != mResources.end(); ++it ) { | ||
225 | result.append( *it ); | ||
226 | } | ||
227 | return result; | ||
228 | } | ||
229 | |||
230 | QPtrList<Resource> ManagerImpl::resources( bool active ) | ||
231 | { | ||
232 | QPtrList<Resource> result; | ||
233 | |||
234 | Resource::List::ConstIterator it; | ||
235 | for ( it = mResources.begin(); it != mResources.end(); ++it ) { | ||
236 | if ( (*it)->isActive() == active ) { | ||
237 | result.append( *it ); | ||
238 | } | ||
239 | } | ||
240 | return result; | ||
241 | } | ||
242 | |||
243 | void ManagerImpl::setListener( ManagerImplListener *listener ) | ||
244 | { | ||
245 | mListener = listener; | ||
246 | } | ||
247 | |||
248 | Resource* ManagerImpl::readResourceConfig( const QString& identifier, | ||
249 | bool checkActive ) | ||
250 | { | ||
251 | kdDebug() << "ManagerImpl::readResourceConfig() " << identifier << endl; | ||
252 | |||
253 | // qDebug("ManagerImpl::readResourceConfig() %s", identifier.latin1()); | ||
254 | |||
255 | mConfig->setGroup( "Resource_" + identifier ); | ||
256 | |||
257 | QString type = mConfig->readEntry( "ResourceType" ); | ||
258 | QString name = mConfig->readEntry( "ResourceName" ); | ||
259 | Resource *resource = mFactory->resource( type, mConfig ); | ||
260 | if ( !resource ) { | ||
261 | kdDebug(5650) << "Failed to create resource with id " << identifier << endl; | ||
262 | return 0; | ||
263 | } | ||
264 | |||
265 | if ( resource->identifier().isEmpty() ) | ||
266 | resource->setIdentifier( identifier ); | ||
267 | |||
268 | mConfig->setGroup( "General" ); | ||
269 | |||
270 | QString standardKey = mConfig->readEntry( "Standard" ); | ||
271 | if ( standardKey == identifier ) { | ||
272 | mStandard = resource; | ||
273 | } | ||
274 | |||
275 | if ( checkActive ) { | ||
276 | QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" ); | ||
277 | resource->setActive( activeKeys.contains( identifier ) ); | ||
278 | } | ||
279 | mResources.append( resource ); | ||
280 | |||
281 | return resource; | ||
282 | } | ||
283 | |||
284 | void ManagerImpl::writeResourceConfig( Resource *resource, | ||
285 | bool checkActive ) | ||
286 | { | ||
287 | QString key = resource->identifier(); | ||
288 | |||
289 | kdDebug(5650) << "Saving resource " << key << endl; | ||
290 | |||
291 | if ( !mConfig ) createStandardConfig(); | ||
292 | |||
293 | mConfig->setGroup( "Resource_" + key ); | ||
294 | resource->writeConfig( mConfig ); | ||
295 | |||
296 | mConfig->setGroup( "General" ); | ||
297 | QString standardKey = mConfig->readEntry( "Standard" ); | ||
298 | |||
299 | if ( resource == mStandard && standardKey != key ) | ||
300 | mConfig->writeEntry( "Standard", resource->identifier() ); | ||
301 | else if ( resource != mStandard && standardKey == key ) | ||
302 | mConfig->writeEntry( "Standard", "" ); | ||
303 | |||
304 | if ( checkActive ) { | ||
305 | QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" ); | ||
306 | if ( resource->isActive() && !activeKeys.contains( key ) ) { | ||
307 | activeKeys.append( resource->identifier() ); | ||
308 | mConfig->writeEntry( "ResourceKeys", activeKeys ); | ||
309 | } else if ( !resource->isActive() && activeKeys.contains( key ) ) { | ||
310 | activeKeys.remove( key ); | ||
311 | mConfig->writeEntry( "ResourceKeys", activeKeys ); | ||
312 | } | ||
313 | } | ||
314 | |||
315 | mConfig->sync(); | ||
316 | } | ||
317 | |||
318 | void ManagerImpl::removeResource( Resource *resource ) | ||
319 | { | ||
320 | QString key = resource->identifier(); | ||
321 | |||
322 | if ( !mConfig ) createStandardConfig(); | ||
323 | |||
324 | mConfig->setGroup( "General" ); | ||
325 | QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" ); | ||
326 | if ( activeKeys.contains( key ) ) { | ||
327 | activeKeys.remove( key ); | ||
328 | mConfig->writeEntry( "ResourceKeys", activeKeys ); | ||
329 | } else { | ||
330 | QStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" ); | ||
331 | passiveKeys.remove( key ); | ||
332 | mConfig->writeEntry( "PassiveResourceKeys", passiveKeys ); | ||
333 | } | ||
334 | |||
335 | QString standardKey = mConfig->readEntry( "Standard" ); | ||
336 | if ( standardKey == key ) { | ||
337 | mConfig->writeEntry( "Standard", "" ); | ||
338 | } | ||
339 | |||
340 | mConfig->deleteGroup( "Resource_" + resource->identifier() ); | ||
341 | |||
342 | mConfig->sync(); | ||
343 | } | ||
344 | |||
345 | Resource* ManagerImpl::getResource( const QString& identifier ) | ||
346 | { | ||
347 | Resource::List::ConstIterator it; | ||
348 | for ( it = mResources.begin(); it != mResources.end(); ++it ) { | ||
349 | if ( (*it)->identifier() == identifier ) | ||
350 | return *it; | ||
351 | } | ||
352 | return 0; | ||
353 | } | ||
diff --git a/microkde/kresources/managerimpl.h b/microkde/kresources/managerimpl.h new file mode 100644 index 0000000..a049bcc --- a/dev/null +++ b/microkde/kresources/managerimpl.h | |||
@@ -0,0 +1,113 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 | #ifndef KRESOURCES_MANAGERIMPL_H | ||
25 | #define KRESOURCES_MANAGERIMPL_H | ||
26 | |||
27 | #include <qstring.h> | ||
28 | #include <qptrlist.h> | ||
29 | #include <qdict.h> | ||
30 | //US | ||
31 | #include <qobject.h> | ||
32 | |||
33 | #include "resource.h" | ||
34 | |||
35 | |||
36 | class KConfig; | ||
37 | |||
38 | namespace KRES { | ||
39 | |||
40 | class Resource; | ||
41 | class Factory; | ||
42 | |||
43 | class ManagerImplListener | ||
44 | { | ||
45 | public: | ||
46 | virtual void resourceAdded( Resource *resource ) = 0; | ||
47 | virtual void resourceModified( Resource *resource ) = 0; | ||
48 | virtual void resourceDeleted( Resource *resource ) = 0; | ||
49 | }; | ||
50 | |||
51 | |||
52 | /** | ||
53 | @internal | ||
54 | |||
55 | Do not use this class directly. Use ResourceManager instead | ||
56 | */ | ||
57 | class ManagerImpl : public QObject | ||
58 | { | ||
59 | Q_OBJECT | ||
60 | public: | ||
61 | ManagerImpl( const QString &family ); | ||
62 | ~ManagerImpl(); | ||
63 | |||
64 | void readConfig( KConfig * ); | ||
65 | void writeConfig( KConfig * ); | ||
66 | |||
67 | void add( Resource *resource, bool useDCOP = true ); | ||
68 | void remove( Resource *resource, bool useDCOP = true ); | ||
69 | |||
70 | Resource *standardResource(); | ||
71 | void setStandardResource( Resource *resource ); | ||
72 | |||
73 | void setActive( Resource *resource, bool active ); | ||
74 | |||
75 | Resource::List *resourceList(); | ||
76 | |||
77 | QPtrList<Resource> resources(); | ||
78 | |||
79 | // Get only active or passive resources | ||
80 | QPtrList<Resource> resources( bool active ); | ||
81 | |||
82 | QStringList resourceNames(); | ||
83 | |||
84 | void setListener( ManagerImplListener *listener ); | ||
85 | |||
86 | public slots: | ||
87 | void resourceChanged( Resource *resource ); | ||
88 | |||
89 | private: | ||
90 | // dcop calls | ||
91 | |||
92 | private: | ||
93 | void createStandardConfig(); | ||
94 | |||
95 | Resource *readResourceConfig( const QString& identifier, bool checkActive ); | ||
96 | void writeResourceConfig( Resource *resource, bool checkActive ); | ||
97 | |||
98 | void removeResource( Resource *resource ); | ||
99 | Resource *getResource( Resource *resource ); | ||
100 | Resource *getResource( const QString& identifier ); | ||
101 | |||
102 | QString mFamily; | ||
103 | KConfig *mConfig; | ||
104 | KConfig *mStdConfig; | ||
105 | Resource *mStandard; | ||
106 | Factory *mFactory; | ||
107 | Resource::List mResources; | ||
108 | ManagerImplListener *mListener; | ||
109 | }; | ||
110 | |||
111 | } | ||
112 | |||
113 | #endif | ||
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 | } | ||
diff --git a/microkde/kresources/resource.h b/microkde/kresources/resource.h new file mode 100644 index 0000000..7ff4f23 --- a/dev/null +++ b/microkde/kresources/resource.h | |||
@@ -0,0 +1,401 @@ | |||
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 | |||
7 | This library is free software; you can redistribute it and/or | ||
8 | modify it under the terms of the GNU Library General Public | ||
9 | License as published by the Free Software Foundation; either | ||
10 | version 2 of the License, or (at your option) any later version. | ||
11 | |||
12 | This library is distributed in the hope that it will be useful, | ||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | Library General Public License for more details. | ||
16 | |||
17 | You should have received a copy of the GNU Library General Public License | ||
18 | along with this library; see the file COPYING.LIB. If not, write to | ||
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
20 | Boston, MA 02111-1307, USA. | ||
21 | */ | ||
22 | |||
23 | #ifndef KRESOURCES_RESOURCE_H | ||
24 | #define KRESOURCES_RESOURCE_H | ||
25 | |||
26 | //US | ||
27 | #ifdef QT_THREAD_SUPPORT | ||
28 | #include <qmutex.h> | ||
29 | #endif //QT_THREAD_SUPPORT | ||
30 | |||
31 | #include <qvaluelist.h> | ||
32 | #include <qwidget.h> | ||
33 | |||
34 | #include <qobject.h> | ||
35 | |||
36 | class KConfig; | ||
37 | |||
38 | namespace KRES { | ||
39 | |||
40 | class KLibFactory; | ||
41 | class ConfigWidget; | ||
42 | |||
43 | /** | ||
44 | * @internal | ||
45 | * @libdoc The KDE Resource library | ||
46 | * | ||
47 | * NOTE: this library is NOT (YET?) PUBLIC. Do not publish this | ||
48 | * interface, it is in constant flux. | ||
49 | * | ||
50 | * The KDE Resource framework can be used to manage resources of | ||
51 | * different types, organized in families. The Resource framework | ||
52 | * is currently used for addressbook resources in libkabc and for | ||
53 | * calendar resources in libkcal. | ||
54 | * | ||
55 | * When you want to use the framework for a new family, you need to | ||
56 | * <ul><li>Define a name for your resource family</li> | ||
57 | * <li>subclass Resource and add the fields and method that are needed | ||
58 | * in your application</li> | ||
59 | * <li>If needed, override the doOpen() and doClose() methods. | ||
60 | * <li> Provide a configuration possibility for resources in your | ||
61 | * new family. You can use @ref ResourcesConfigPage to easily create a | ||
62 | * KControl applet</li> | ||
63 | * <li>In your application, you can use @ref ResourceManager to keep track | ||
64 | * of the resources in your family, and you can use @ref ResourceSelectDialog | ||
65 | * to let the user select a single resource.</li> | ||
66 | * </ul> | ||
67 | * | ||
68 | * When you want to add a new resource type to an existing resource family, | ||
69 | * you need to | ||
70 | * <ul><li>Further subclass the family-specific Resource to implement | ||
71 | * resource type-specific operation</li> | ||
72 | * <li>Subclass ResourceConfigWidget to provide a configuration widget | ||
73 | * for your new resource type</li> | ||
74 | * <li>Provide a .desktop file so that the new resource type can be found | ||
75 | * automatically by the ResourceManager</li> | ||
76 | * </ul> | ||
77 | * | ||
78 | * Example: | ||
79 | * | ||
80 | <B>resourceexample.h</B>: | ||
81 | <pre> | ||
82 | #include <kconfig.h> | ||
83 | #include <kresources/resource.h> | ||
84 | |||
85 | class ResourceExample : public KRES::ResourceExample | ||
86 | { | ||
87 | public: | ||
88 | ResourceExample( const KConfig * ); | ||
89 | ~ResourceCalendarExchange(); | ||
90 | void writeConfig( KConfig *config ); | ||
91 | private: | ||
92 | QString mLocation; | ||
93 | QString mPassword; | ||
94 | } | ||
95 | </pre> | ||
96 | <B>resourceexample.cpp</B>: | ||
97 | <pre> | ||
98 | #include <kconfig.h> | ||
99 | |||
100 | #include "resourceexample.h" | ||
101 | |||
102 | ResourceExample::ResourceExample( const KConfig *config ) | ||
103 | : Resource( config ) | ||
104 | { | ||
105 | if ( config ) { | ||
106 | mLocation = config->readEntry( "Location" ); | ||
107 | mPassword = KStringHandler::obscure( config->readEntry( "Password" ) ); | ||
108 | } else { | ||
109 | mLocation = ""; // Or some sensible default | ||
110 | mPassword = ""; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | void ResourceExample::writeConfig( KConfig *config ) | ||
115 | { | ||
116 | KRES::Resource::writeConfig( config ); | ||
117 | config->writeEntry( "Location", mLocation ); | ||
118 | config->writeEntry( "Password", KStringHandler::obscure( mPassword ) ); | ||
119 | } | ||
120 | |||
121 | extern "C" | ||
122 | { | ||
123 | KRES::ResourceExample *config_widget( QWidget *parent ) { | ||
124 | return new ResourceExampleConfig( parent, "Configure Example Resource" ); | ||
125 | } | ||
126 | |||
127 | KRES::Resource *resource( const KConfig *config ) { | ||
128 | return new ResourceExample( config ); | ||
129 | } | ||
130 | } | ||
131 | </pre> | ||
132 | * <B>resourceexampleconfig.h</B>: | ||
133 | <pre> | ||
134 | #include <klineedit.h> | ||
135 | #include <kresources/resourceconfigwidget.h> | ||
136 | |||
137 | #include "resourceexample.h" | ||
138 | |||
139 | class ResourceExampleConfig : public KRES::ResourceConfigWidget | ||
140 | { | ||
141 | Q_OBJECT | ||
142 | |||
143 | public: | ||
144 | ResourceExampleConfig( QWidget* parent = 0, const char* name = 0 ); | ||
145 | |||
146 | public slots: | ||
147 | virtual void loadSettings( KRES::Resource *resource); | ||
148 | virtual void saveSettings( KRES::Resource *resource ); | ||
149 | |||
150 | private: | ||
151 | KLineEdit* mLocationEdit; | ||
152 | KLineEdit* mPasswordEdit; | ||
153 | }; | ||
154 | </pre> | ||
155 | * <B>resourceexampleconfig.cpp</B>: | ||
156 | <pre> | ||
157 | #include <qlayout.h> | ||
158 | #include <qlabel.h" | ||
159 | #include <kresources/resourceconfigwidget.h> | ||
160 | #include "resourceexample.h" | ||
161 | #include "resourceexampleconfig.h" | ||
162 | |||
163 | ResourceExampleConfig::ResourceExampleConfig( QWidget* parent, const char* name ) | ||
164 | : KRES::ResourceConfigWidget( parent, name ) | ||
165 | { | ||
166 | resize( 245, 115 ); | ||
167 | QGridLayout *mainLayout = new QGridLayout( this, 2, 2 ); | ||
168 | |||
169 | QLabel *label = new QLabel( i18n( "Location:" ), this ); | ||
170 | mHostEdit = new KLineEdit( this ); | ||
171 | mainLayout->addWidget( label, 1, 0 ); | ||
172 | mainLayout->addWidget( mHostEdit, 1, 1 ); | ||
173 | |||
174 | label = new QLabel( i18n( "Password:" ), this ); | ||
175 | mPasswordEdit = new KLineEdit( this ); | ||
176 | mPasswordEdit->setEchoMode( QLineEdit::Password ); | ||
177 | mainLayout->addWidget( label, 2, 0 ); | ||
178 | mainLayout->addWidget( mPasswordEdit, 2, 1 ); | ||
179 | } | ||
180 | |||
181 | void ResourceExampleConfig::loadSettings( KRES::Resource *resource ) | ||
182 | { | ||
183 | ResourceExample* res = dynamic_cast<ResourceExample *>( resource ); | ||
184 | if (res) { | ||
185 | mHostEdit->setText( res->host() ); | ||
186 | mPasswordEdit->setText( res->password() ); | ||
187 | } else | ||
188 | kdDebug(5700) << "ERROR: ResourceExampleConfig::loadSettings(): no ResourceExample, cast failed" << endl; | ||
189 | } | ||
190 | |||
191 | void ResourceExampleConfig::saveSettings( KRES::Resource *resource ) | ||
192 | { | ||
193 | ResourceExample* res = dynamic_cast<ResourceExample *>( resource ); | ||
194 | if (res) { | ||
195 | res->setHost(mHostEdit->text()); | ||
196 | res->setPassword(mPasswordEdit->text()); | ||
197 | } else | ||
198 | kdDebug(5700) << "ERROR: ResourceExampleConfig::saveSettings(): no ResourceExample, cast failed" << endl; | ||
199 | } | ||
200 | </pre> | ||
201 | * <B>resourceexample.desktop</B>: | ||
202 | <pre> | ||
203 | [Desktop Entry] | ||
204 | Type=Service | ||
205 | |||
206 | [Misc] | ||
207 | Encoding=UTF-8 | ||
208 | Name=Example Resource | ||
209 | |||
210 | [Plugin] | ||
211 | Type=exchange | ||
212 | X-KDE-Library=resourceexample | ||
213 | </pre> | ||
214 | * <B>Makefile.am</B> | ||
215 | <pre> | ||
216 | kde_module_LTLIBRARIES = resourceexample.la | ||
217 | |||
218 | resourceexample_la_SOURCES = resourceexample.cpp resourceexampleconfig.cpp | ||
219 | resourceexample_la_LDFLAGS= $(all_libraries) -module $(KDE_PLUGIN) | ||
220 | resourceexample_la_LIBADD= -lkderesources | ||
221 | |||
222 | linkdir= $(kde_datadir)/resources/family | ||
223 | link_DATA= resourceexample.desktop | ||
224 | </pre> | ||
225 | * | ||
226 | * | ||
227 | */ | ||
228 | |||
229 | /** | ||
230 | * A @ref Resource is a ... | ||
231 | * | ||
232 | * A subclass should reimplement at least the constructor and the | ||
233 | * @ref writeConfig method. | ||
234 | * | ||
235 | */ | ||
236 | class Resource : public QObject | ||
237 | { | ||
238 | Q_OBJECT | ||
239 | |||
240 | public: | ||
241 | typedef QValueList<Resource *> List; | ||
242 | |||
243 | /** | ||
244 | * Constructor. Construct resource from config. | ||
245 | * @param config Configuration to read persistence information from. | ||
246 | * If config==0, create object using default settings. | ||
247 | */ | ||
248 | Resource( const KConfig* config ); | ||
249 | |||
250 | /** | ||
251 | * Destructor. | ||
252 | */ | ||
253 | virtual ~Resource(); | ||
254 | |||
255 | /** | ||
256 | * Write configuration information for this resource to a configuration | ||
257 | * file. If you override this method, remember to call Resource::writeConfig | ||
258 | * or Terrible Things(TM) will happen. | ||
259 | * @param config Configuration to write persistence information to. | ||
260 | */ | ||
261 | virtual void writeConfig( KConfig* config ); | ||
262 | |||
263 | /** | ||
264 | * Open this resource, if it not already open. Increase the open | ||
265 | * count of this object, and open the resource by calling @ref doOpen(). | ||
266 | * This method may block while another thread is concurrently opening | ||
267 | * or closing the resource. | ||
268 | * | ||
269 | * Returns true if the resource was already opened or if it was opened | ||
270 | * successfully; returns false if the resource was not opened successfully. | ||
271 | */ | ||
272 | bool open(); | ||
273 | |||
274 | /** | ||
275 | * Decrease the open count of this object, and if the count reaches | ||
276 | * zero, close this resource by calling @ref doClose(). | ||
277 | * This method may block while another thread is concurrently closing | ||
278 | * or opening the resource. | ||
279 | */ | ||
280 | void close(); | ||
281 | |||
282 | /** | ||
283 | * Returns whether the resource is open or not. | ||
284 | */ | ||
285 | bool isOpen() const; | ||
286 | |||
287 | /** | ||
288 | * Returns a unique identifier. The identifier is unique for this resource. | ||
289 | * It is created when the resource is first created, and it is retained | ||
290 | * in the resource family configuration file for this resource. | ||
291 | * @return This resource's identifier | ||
292 | */ | ||
293 | QString identifier() const; | ||
294 | |||
295 | /** | ||
296 | * Returns the type of this resource. | ||
297 | */ | ||
298 | QString type() const; | ||
299 | |||
300 | /** | ||
301 | * Mark the resource as read-only. You can override this method, | ||
302 | * but also remember to call Resource::setReadOnly(). | ||
303 | */ | ||
304 | virtual void setReadOnly( bool value ); | ||
305 | |||
306 | /** | ||
307 | * Returns, if the resource is read-only. | ||
308 | */ | ||
309 | virtual bool readOnly() const; | ||
310 | |||
311 | /** | ||
312 | * Set the name of resource.You can override this method, | ||
313 | * but also remember to call Resource::setResourceName(). | ||
314 | */ | ||
315 | virtual void setResourceName( const QString &name ); | ||
316 | |||
317 | /** | ||
318 | * Returns the name of resource. | ||
319 | */ | ||
320 | virtual QString resourceName() const; | ||
321 | |||
322 | /** | ||
323 | Sets, if the resource is active. | ||
324 | */ | ||
325 | void setActive( bool active ); | ||
326 | |||
327 | /** | ||
328 | Return true, if the resource is active. | ||
329 | */ | ||
330 | bool isActive() const; | ||
331 | |||
332 | friend class Factory; | ||
333 | friend class ManagerImpl; | ||
334 | |||
335 | /** | ||
336 | Print resource information as debug output. | ||
337 | */ | ||
338 | virtual void dump() const; | ||
339 | |||
340 | protected: | ||
341 | /** | ||
342 | * Open this resource. When called, the resource must be in | ||
343 | * a closed state. | ||
344 | * | ||
345 | * Returns true if the resource was opened successfully; | ||
346 | * returns false if the resource was not opened successfully. | ||
347 | * | ||
348 | * The result of this call can be accessed later by @ref isOpen() | ||
349 | */ | ||
350 | virtual bool doOpen() { return true; } | ||
351 | |||
352 | /** | ||
353 | * Close this resource. Pre-condition: resource is open. | ||
354 | * Post-condition: resource is closed. | ||
355 | */ | ||
356 | virtual void doClose() {} | ||
357 | |||
358 | void setIdentifier( const QString& identifier ); | ||
359 | void setType( const QString& type ); | ||
360 | |||
361 | private: | ||
362 | class ResourcePrivate; | ||
363 | ResourcePrivate *d; | ||
364 | }; | ||
365 | |||
366 | //US class PluginFactoryBase : public KLibFactory | ||
367 | class PluginFactoryBase | ||
368 | { | ||
369 | public: | ||
370 | virtual Resource *resource( const KConfig *config ) = 0; | ||
371 | |||
372 | virtual ConfigWidget *configWidget( QWidget *parent ) = 0; | ||
373 | |||
374 | protected: | ||
375 | virtual QObject* createObject( QObject*, const char*, const char*, | ||
376 | const QStringList & ) | ||
377 | { | ||
378 | return 0; | ||
379 | } | ||
380 | }; | ||
381 | |||
382 | template<class TR,class TC> | ||
383 | class PluginFactory : public PluginFactoryBase | ||
384 | { | ||
385 | public: | ||
386 | Resource *resource( const KConfig *config ) | ||
387 | { | ||
388 | return new TR( config ); | ||
389 | } | ||
390 | |||
391 | ConfigWidget *configWidget( QWidget *parent ) | ||
392 | { | ||
393 | return new TC( parent ); | ||
394 | } | ||
395 | }; | ||
396 | |||
397 | |||
398 | |||
399 | } | ||
400 | |||
401 | #endif | ||
diff --git a/microkde/kresources/resourceselectdialog.h b/microkde/kresources/resourceselectdialog.h new file mode 100644 index 0000000..fef689d --- a/dev/null +++ b/microkde/kresources/resourceselectdialog.h | |||
@@ -0,0 +1,12 @@ | |||
1 | #ifndef MICRO_KRES_RESOURCESELECTDIALOG_H | ||
2 | #define MICRO_KRES_RESOURCESELECTDIALOG_H | ||
3 | |||
4 | namespace KRES { | ||
5 | |||
6 | class ResourceSelectDialog | ||
7 | { | ||
8 | }; | ||
9 | |||
10 | } | ||
11 | |||
12 | #endif | ||
diff --git a/microkde/kresources/selectdialog.cpp b/microkde/kresources/selectdialog.cpp new file mode 100644 index 0000000..fba8648 --- a/dev/null +++ b/microkde/kresources/selectdialog.cpp | |||
@@ -0,0 +1,154 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 | /*US | ||
25 | #include <kbuttonbox.h> | ||
26 | #include <klistbox.h> | ||
27 | #include <klocale.h> | ||
28 | #include <kmessagebox.h> | ||
29 | |||
30 | */ | ||
31 | #include <klocale.h> | ||
32 | #include <kmessagebox.h> | ||
33 | |||
34 | //US | ||
35 | #include <kglobal.h> | ||
36 | |||
37 | #include <qlistbox.h> | ||
38 | #include <qlayout.h> | ||
39 | #include <qgroupbox.h> | ||
40 | |||
41 | #include "resource.h" | ||
42 | |||
43 | #include "selectdialog.h" | ||
44 | |||
45 | using namespace KRES; | ||
46 | |||
47 | //US I am using KBaseDialog instead of KDialog | ||
48 | //US : KDialog( parent, name, true ) | ||
49 | SelectDialog::SelectDialog( QPtrList<Resource> list, QWidget *parent, | ||
50 | const char *name ) | ||
51 | : KDialogBase( parent, name, true, i18n( "Resource Selection" ), Help | Ok | Cancel, | ||
52 | Ok, true) | ||
53 | |||
54 | { | ||
55 | //US setCaption( i18n( "Resource Selection" ) ); | ||
56 | //US resize( 300, 200 ); | ||
57 | resize( KMIN(KGlobal::getDesktopWidth(), 300), KMIN(KGlobal::getDesktopHeight(), 200) ); | ||
58 | |||
59 | //US | ||
60 | QFrame *main = plainPage(); | ||
61 | /*US | ||
62 | QVBoxLayout *layout = new QVBoxLayout( main ); | ||
63 | mConfigPage = new KRES::ConfigPage( main ); | ||
64 | layout->addWidget( mConfigPage ); | ||
65 | */ | ||
66 | |||
67 | //US QVBoxLayout *mainLayout = new QVBoxLayout( this ); | ||
68 | QVBoxLayout *mainLayout = new QVBoxLayout( main ); | ||
69 | mainLayout->setMargin( marginHint() ); | ||
70 | |||
71 | //US QGroupBox *groupBox = new QGroupBox( 2, Qt::Horizontal, this ); | ||
72 | QGroupBox *groupBox = new QGroupBox( 2, Qt::Horizontal, main ); | ||
73 | groupBox->setTitle( i18n( "Resources" ) ); | ||
74 | |||
75 | //US mResourceId = new KListBox( groupBox ); | ||
76 | mResourceId = new QListBox( groupBox ); | ||
77 | |||
78 | mainLayout->addWidget( groupBox ); | ||
79 | |||
80 | mainLayout->addSpacing( 40 ); | ||
81 | |||
82 | /*US | ||
83 | KButtonBox *buttonBox = new KButtonBox( this ); | ||
84 | |||
85 | buttonBox->addStretch(); | ||
86 | buttonBox->addButton( i18n( "&OK" ), this, SLOT( accept() ) ); | ||
87 | buttonBox->addButton( i18n( "&Cancel" ), this, SLOT( reject() ) ); | ||
88 | buttonBox->layout(); | ||
89 | |||
90 | mainLayout->addWidget( buttonBox ); | ||
91 | */ | ||
92 | // setup listbox | ||
93 | uint counter = 0; | ||
94 | for ( uint i = 0; i < list.count(); ++i ) { | ||
95 | Resource *resource = list.at( i ); | ||
96 | if ( resource && !resource->readOnly() ) { | ||
97 | mResourceMap.insert( counter, resource ); | ||
98 | mResourceId->insertItem( resource->resourceName() ); | ||
99 | counter++; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | mResourceId->setCurrentItem( 0 ); | ||
104 | connect( mResourceId, SIGNAL(returnPressed(QListBoxItem*)), | ||
105 | SLOT(accept()) ); | ||
106 | } | ||
107 | |||
108 | Resource *SelectDialog::resource() | ||
109 | { | ||
110 | if ( mResourceId->currentItem() != -1 ) | ||
111 | return mResourceMap[ mResourceId->currentItem() ]; | ||
112 | else | ||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | Resource *SelectDialog::getResource( QPtrList<Resource> list, QWidget *parent ) | ||
117 | { | ||
118 | if ( list.count() == 0 ) { | ||
119 | KMessageBox::error( parent, i18n( "There is no resource available!" ) ); | ||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | if ( list.count() == 1 ) return list.first(); | ||
124 | |||
125 | // the following lines will return a writeable resource if only _one_ writeable | ||
126 | // resource exists | ||
127 | Resource *found = 0; | ||
128 | Resource *it = list.first(); | ||
129 | while ( it ) { | ||
130 | if ( !it->readOnly() ) { | ||
131 | if ( found ) { | ||
132 | found = 0; | ||
133 | break; | ||
134 | } else | ||
135 | found = it; | ||
136 | } | ||
137 | it = list.next(); | ||
138 | } | ||
139 | |||
140 | if ( found ) | ||
141 | return found; | ||
142 | |||
143 | SelectDialog dlg( list, parent); | ||
144 | //US if ( dlg.exec() == KDialog::Accepted ) | ||
145 | if ( dlg.exec() ) | ||
146 | return dlg.resource(); | ||
147 | else | ||
148 | return 0; | ||
149 | } | ||
150 | |||
151 | /*US | ||
152 | #include "selectdialog.moc" | ||
153 | */ | ||
154 | |||
diff --git a/microkde/kresources/selectdialog.h b/microkde/kresources/selectdialog.h new file mode 100644 index 0000000..7026212 --- a/dev/null +++ b/microkde/kresources/selectdialog.h | |||
@@ -0,0 +1,92 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
3 | |||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@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 | #ifndef KRESOURCES_SELECTDIALOG_H | ||
25 | #define KRESOURCES_SELECTDIALOG_H | ||
26 | |||
27 | #include <qobject.h> | ||
28 | #include <qptrlist.h> | ||
29 | #include <qmap.h> | ||
30 | |||
31 | #include <kdialogbase.h> | ||
32 | |||
33 | //US class KListBox; | ||
34 | class QListBox; | ||
35 | |||
36 | namespace KRES { | ||
37 | |||
38 | class Resource; | ||
39 | |||
40 | /** | ||
41 | * Dialog for selecting a resource. | ||
42 | * | ||
43 | * Example: | ||
44 | * | ||
45 | * <pre> | ||
46 | * KABC::Resource *res = KABC::SelectDialog::getResource(); | ||
47 | * if ( !( res ) ) { | ||
48 | * // no resource selected | ||
49 | * } else { | ||
50 | * // do something with resource | ||
51 | * } | ||
52 | * </pre> | ||
53 | */ | ||
54 | //US class SelectDialog : KDialog | ||
55 | class SelectDialog : KDialogBase | ||
56 | { | ||
57 | // Q_OBJECT | ||
58 | public: | ||
59 | /** | ||
60 | * Constructor. | ||
61 | * @param ab The address book you want to select the resource from | ||
62 | * @param parent The parent widget | ||
63 | * @param name The name of the dialog | ||
64 | */ | ||
65 | SelectDialog( QPtrList<Resource> list, QWidget *parent = 0, | ||
66 | const char *name = 0); | ||
67 | |||
68 | // ~SelectDialog(); | ||
69 | |||
70 | /** | ||
71 | * Return selected resource. | ||
72 | */ | ||
73 | Resource *resource(); | ||
74 | |||
75 | /** | ||
76 | * Open a dialog showing the available resources and return the resource the | ||
77 | * user has selected. Returns 0, if the dialog was canceled. | ||
78 | */ | ||
79 | static Resource *getResource( QPtrList<Resource> list, QWidget *parent = 0 ); | ||
80 | |||
81 | private: | ||
82 | //US KListBox *mResourceId; | ||
83 | QListBox *mResourceId; | ||
84 | |||
85 | QMap<int, Resource*> mResourceMap; | ||
86 | }; | ||
87 | |||
88 | |||
89 | |||
90 | } | ||
91 | |||
92 | #endif | ||