summaryrefslogtreecommitdiffabout
path: root/microkde/kresources/configpage.cpp
blob: 1a9d063cfb19d5a84274da34c5c1dcbd20e78e31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
    This file is part of libkresources.

    Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
    Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
    Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

/*
Enhanced Version of the file for platform independent KDE tools.
Copyright (c) 2004 Ulf Schenk

$Id$
*/

#include <q3groupbox.h>

#ifdef DESKTOP_VERSION
#include <qinputdialog.h>
#else
#include <qtcompat/qinputdialog.h>
#endif
#include <qlabel.h>
#include <qlayout.h>
//Added by qt3to4:
#include <Q3GridLayout>
#include <Q3ValueList>
#include <Q3VBoxLayout>
#include <kapplication.h>
#include <kcombobox.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>
#include <kurlrequester.h>
#include <klistview.h>
#include <kbuttonbox.h>
//US #include <ktrader.h>

#include "resource.h"
#include "configdialog.h"

#include "configpage.h"

//US
#include <qpushbutton.h>
#include <qfile.h>
#include <kglobal.h>

using namespace KRES;

class ConfigViewItem : public Q3CheckListItem
{
  public:
    ConfigViewItem( Q3ListView *parent, Resource* resource ) :
      Q3CheckListItem( parent, resource->resourceName(), CheckBox ),
      mResource( resource ),
      mIsStandard( false )
    {
      setText( 1, mResource->type() );
      setOn( mResource->isActive() );
    }

    void setStandard( bool value )
    {
      setText( 2, ( value ? i18n( "Yes" ) : QString::null ) );
      mIsStandard = value;
    }

    bool standard() const { return mIsStandard; }
    bool readOnly() const { return mResource->readOnly(); }

    Resource *resource() { return mResource; }

  private:
    Resource* mResource;

    bool mIsStandard;
};

ConfigPage::ConfigPage( QWidget *parent, const char *name )
  : QWidget( parent, name ),
    mCurrentManager( 0 ),
    mCurrentConfig( 0 )
{
  setCaption( i18n( "Resource Configuration" ) );

  Q3VBoxLayout *mainLayout = new Q3VBoxLayout( this );

  Q3GroupBox *groupBox = new Q3GroupBox( i18n( "Resources" ), this );
  groupBox->setColumnLayout(0, Qt::Vertical );
  groupBox->layout()->setSpacing( 3 );
  groupBox->layout()->setMargin( 5 );
  Q3GridLayout *groupBoxLayout = new Q3GridLayout( groupBox->layout(), 4, 2 );

//US  mFamilyCombo = new KComboBox( false, groupBox );
  mFamilyCombo = new KComboBox( groupBox );
  groupBoxLayout->addMultiCellWidget( mFamilyCombo, 0, 0, 0, 1 );

  mListView = new KListView( groupBox );
  mListView->setAllColumnsShowFocus( true );
  mListView->addColumn( i18n( "Name" ) );
  mListView->addColumn( i18n( "Type" ) );
  mListView->addColumn( i18n( "Standard" ) ); 

//US  groupBoxLayout->addWidget( mListView, 1, 0 );
  groupBoxLayout->addMultiCellWidget( mListView, 1, 1, 0, 1 );


  mAddButton = new QPushButton( i18n( "&Add..." ), groupBox );
  groupBoxLayout->addWidget( mAddButton, 2, 0 );
  mRemoveButton = new QPushButton( i18n( "&Remove" ), groupBox );
  groupBoxLayout->addWidget( mRemoveButton, 2, 1 );
  mEditButton = new QPushButton( i18n( "&Edit..." ), groupBox );
  groupBoxLayout->addWidget( mEditButton, 3, 0 );
  mStandardButton = new QPushButton( i18n( "&Use as Standard" ), groupBox );
  groupBoxLayout->addWidget( mStandardButton, 3, 1 );

  mRemoveButton->setEnabled( false );
  mEditButton->setEnabled( false );
  mStandardButton->setEnabled( false );


  connect( mAddButton, SIGNAL( clicked() ), SLOT(slotAdd()) );
  connect( mRemoveButton, SIGNAL( clicked() ), SLOT(slotRemove()) );
  connect( mEditButton, SIGNAL( clicked() ), SLOT(slotEdit()) );
  connect( mStandardButton, SIGNAL( clicked() ), SLOT(slotStandard()) );


  mainLayout->addWidget( groupBox );

  connect( mFamilyCombo, SIGNAL( activated( int ) ),
           SLOT( slotFamilyChanged( int ) ) );
  connect( mListView, SIGNAL( selectionChanged() ),
           SLOT( slotSelectionChanged() ) );
  connect( mListView, SIGNAL( clicked( Q3ListViewItem * ) ),
           SLOT( slotItemClicked( Q3ListViewItem * ) ) );

  mLastItem = 0;

//US  mConfig = new KConfig( "kcmkresourcesrc" );
  mConfig = new KConfig( locateLocal( "config", "kcmkresourcesrc")   );
  mConfig->setGroup( "General" );

  load();
}

ConfigPage::~ConfigPage()
{
  Q3ValueList<ResourcePageInfo>::Iterator it;
  for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) {
    (*it).mManager->removeListener( this );
    delete (*it).mManager;
    delete (*it).mConfig;
  }

  mConfig->writeEntry( "CurrentFamily", mFamilyCombo->currentItem() );
  delete mConfig;
  mConfig = 0;
}

void ConfigPage::load()
{
  kdDebug(5650) << "ConfigPage::load()" << endl;

  mListView->clear();

//US we remove the dynamic pluginloader, and set the one family we need (contact) manually.

//US  KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin" );
//US  KTrader::OfferList::ConstIterator it;
//US  for ( it = plugins.begin(); it != plugins.end(); ++it ) {
//US    QVariant tmp = (*it)->property( "X-KDE-ResourceFamily" );
//US    QString family = tmp.toString();
  QStringList families;
  families << "contact";


  for ( QStringList::Iterator it = families.begin(); it != families.end(); ++it )
  {
    QString family = (*it);
    if ( !family.isEmpty() ) {
      if ( !mFamilyMap.contains( family ) ) {
        mCurrentManager = new Manager<Resource>( family );
        if ( mCurrentManager ) {
          mFamilyMap.append( family );
          mCurrentManager->addListener( this );

          ResourcePageInfo info;
          info.mManager = mCurrentManager;
          QString configDir = KGlobal::dirs()->saveLocation( "config" );
          //QString configDir = KStandardDirs::appDir() + "/config";
          if ( family == "contact" && QFile::exists( configDir + "/kabcrc" ) ) {
            info.mConfig = new KConfig( locateLocal( "config", "kabcrc" ) );
          } else if ( family == "calendar" && QFile::exists( configDir + "/kcalrc" ) ) {
            info.mConfig = new KConfig( locateLocal( "config", "kcalrc" ) );
          } else {
            QString configFile = locateLocal( "config", QString( "kresources/%1/stdrc" ).arg( family ) );
            info.mConfig = new KConfig( configFile );
          }
          info.mManager->readConfig( info.mConfig );

          mInfoMap.append( info );
        }
      }
    }
  }
  mCurrentManager = 0;

  mFamilyCombo->insertStringList( mFamilyMap );

  int currentFamily = mConfig->readNumEntry( "CurrentFamily", 0 );
  mFamilyCombo->setCurrentItem( currentFamily );
  slotFamilyChanged( currentFamily );
}

void ConfigPage::save()
{
  saveResourceSettings();

  Q3ValueList<ResourcePageInfo>::Iterator it;
  for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it )
    (*it).mManager->writeConfig( (*it).mConfig );

  emit changed( false );
}

void ConfigPage::defaults()
{
}

void ConfigPage::slotFamilyChanged( int pos )
{
  if ( pos < 0 || pos >= (int)mFamilyMap.count() )
    return;

  saveResourceSettings();

  mFamily = mFamilyMap[ pos ];

//US  qDebug("ConfigPage::slotFamilyChanged 4 family=%s", mFamily.latin1());

  mCurrentManager = mInfoMap[ pos ].mManager;
  mCurrentConfig = mInfoMap[ pos ].mConfig;

  if ( !mCurrentManager )
    kdDebug(5650) << "ERROR: cannot create ResourceManager<Resource>( mFamily )" << endl;

  mListView->clear();

  if ( mCurrentManager->isEmpty() ) {
//US    qDebug("ConfigPage::slotFamilyChanged 4.1 mCurrentManager=%ul", mCurrentManager );

    defaults();
  }

  Resource *standardResource = mCurrentManager->standardResource();

//US    qDebug("ConfigPage::slotFamilyChanged 4.4 resourcename=%s", standardResource->resourceName().latin1());


  Manager<Resource>::Iterator it;
  for ( it = mCurrentManager->begin(); it != mCurrentManager->end(); ++it ) {
    ConfigViewItem *item = new ConfigViewItem( mListView, *it );
    if ( *it == standardResource )
      item->setStandard( true );
  }

  if ( mListView->childCount() == 0 ) {
//US    qDebug("ConfigPage::slotFamilyChanged 4.5 ");

    defaults();
    emit changed( true );
    mCurrentManager->writeConfig( mCurrentConfig );
  } else {
//US    qDebug("ConfigPage::slotFamilyChanged 4.6 ");

    if ( !standardResource ) {
      KMessageBox::sorry( this, i18n( "There is no standard resource!<br> Please select one." ) );

//US    qDebug("ConfigPage::slotFamilyChanged 4.7" );

    }

    emit changed( false );
  }
}

void ConfigPage::slotAdd()
{
  if ( !mCurrentManager )
    return;

  QStringList types = mCurrentManager->resourceTypeNames();
  QStringList descs = mCurrentManager->resourceTypeDescriptions();
  bool ok = false;

  QString desc;

  desc = QInputDialog::getItem( i18n( "Resource Configuration" ),
                    i18n( "Select type of the new resource:" ), descs, 0,
                    false, &ok, this );

  if ( !ok )
    return;

  QString type = types[ descs.findIndex( desc ) ];

  // Create new resource
  Resource *resource = mCurrentManager->createResource( type );
  if ( !resource ) {
    KMessageBox::error( this, i18n("Unable to create resource of type '%1'.")
                              .arg( type ) );
    return;
  }

  resource->setResourceName( type + "-resource" );

  ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" );

  if ( dlg.exec() ) {
    mCurrentManager->add( resource );

    ConfigViewItem *item = new ConfigViewItem( mListView, resource );

    mLastItem = item;

    // if there are only read-only resources we'll set this resource
    // as standard resource
    if ( !resource->readOnly() ) {
      bool onlyReadOnly = true;
      Q3ListViewItem *it = mListView->firstChild();
      while ( it != 0 ) {
        ConfigViewItem *confIt = static_cast<ConfigViewItem*>( it );
        if ( !confIt->readOnly() && confIt != item )
          onlyReadOnly = false;

        it = it->itemBelow();
      }

      if ( onlyReadOnly )
        item->setStandard( true );
    }

    emit changed( true );
  } else {
    delete resource;
    resource = 0;
  }
}

void ConfigPage::slotRemove()
{
  if ( !mCurrentManager )
    return;

  Q3ListViewItem *item = mListView->currentItem();
  ConfigViewItem *confItem = static_cast<ConfigViewItem*>( item );

  if ( !confItem )
    return;

  if ( confItem->standard() ) {
    KMessageBox::sorry( this, i18n( "You cannot remove your standard resource!\n Please select a new standard resource first." ) );
    return;
  }

  mCurrentManager->remove( confItem->resource() );

  if ( item == mLastItem )
    mLastItem = 0;

  mListView->takeItem( item );
  delete item;

  emit changed( true );
}

void ConfigPage::slotEdit()
{
  if ( !mCurrentManager )
    return;

  Q3ListViewItem *item = mListView->currentItem();
  ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item );
  if ( !configItem )
    return;

  Resource *resource = configItem->resource();

  ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" );

  if ( dlg.exec() ) {
    configItem->setText( 0, resource->resourceName() );
    configItem->setText( 1, resource->type() );

    if ( configItem->standard() && configItem->readOnly() ) {
      KMessageBox::sorry( this, i18n( "You cannot use a read-only<br> resource as standard!" ) );
      configItem->setStandard( false );
    }

    mCurrentManager->resourceChanged( resource );
    emit changed( true );
  }
}

void ConfigPage::slotStandard()
{
  if ( !mCurrentManager )
    return;

  ConfigViewItem *item = static_cast<ConfigViewItem*>( mListView->currentItem() );
  if ( !item )
    return;

  if ( item->readOnly() ) {
    KMessageBox::sorry( this, i18n( "You cannot use a read-only<br>resource as standard!" ) );
    return;
  }

  if ( !item->isOn() ) {
    KMessageBox::sorry( this, i18n( "You cannot use an inactive<br>resource as standard!" ) );
    return;
  }

  Q3ListViewItem *it = mListView->firstChild();
  while ( it != 0 ) {
    ConfigViewItem *configItem = static_cast<ConfigViewItem*>( it );
    if ( configItem->standard() )
      configItem->setStandard( false );
    it = it->itemBelow();
  }

  item->setStandard( true );
  mCurrentManager->setStandardResource( item->resource() );
   emit changed( true );

}

void ConfigPage::slotSelectionChanged()
{
  bool state = ( mListView->currentItem() != 0 );

  mRemoveButton->setEnabled( state );
  mEditButton->setEnabled( state );
  mStandardButton->setEnabled( state );
}

void ConfigPage::resourceAdded( Resource* resource )
{
  qDebug("ConfigPage::resourceAdded  : %s", resource->resourceName().latin1());
  ConfigViewItem *item = new ConfigViewItem( mListView, resource );

  // FIXME: this sucks. This should be in the config file,
  // or application-dependent, in which case it's always Off
  item->setOn( false );

  mLastItem = item;

  emit changed( true );
}

void ConfigPage::resourceModified( Resource* resource )
{
  qDebug("ConfigPage::resourceModified  : %s", resource->resourceName().latin1());
}

void ConfigPage::resourceDeleted( Resource* resource )
{
  qDebug("ConfigPage::resourceDeleted  : %s", resource->resourceName().latin1());
}

void ConfigPage::slotItemClicked( Q3ListViewItem *item )
{
  ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item );
  if ( !configItem ) return;

  if ( configItem->standard() && !configItem->isOn() ) {
    KMessageBox::sorry( this, i18n( "You cannot deactivate the<br>standard resource. Choose<br>another standard resource first." ) );
    configItem->setOn( true );
    return;
  }

  if ( configItem->isOn() != configItem->resource()->isActive() ) {
    emit changed( true );
  }
}

void ConfigPage::saveResourceSettings()
{
    //qDebug("ConfigPage::saveResourceSettings() begin");

  if ( mCurrentManager ) {

    Q3ListViewItem *item = mListView->firstChild();
    while ( item ) {
      ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item );

      // check if standard resource
      if ( configItem->standard() && !configItem->readOnly() &&
           configItem->isOn() ) {

        mCurrentManager->setStandardResource( configItem->resource() );
      }

      // check if active or passive resource
      configItem->resource()->setActive( configItem->isOn() );

      item = item->nextSibling();
    }
    mCurrentManager->writeConfig( mCurrentConfig );

    if ( !mCurrentManager->standardResource() )
      KMessageBox::sorry( this, i18n( "There is no valid standard resource!<br>Please select one which is neither read-only nor inactive." ) );
  }

  //qDebug("ConfigPage::saveResourceSettings() end");

}

//US #include "configpage.moc"