1 /***************************************************************************
2 * Copyright (C) 2008 by Tobias Koenig <tokoe@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
21 #include "discspaceutil.h"
22 #include "trashimpl.h"
24 #include <QtGui/QCheckBox>
25 #include <QtGui/QComboBox>
26 #include <QtGui/QDoubleSpinBox>
27 #include <QtGui/QLabel>
28 #include <QtGui/QLayout>
29 #include <QtGui/QListWidget>
30 #include <QtGui/QListWidgetItem>
31 #include <QtGui/QSpinBox>
34 #include <kconfiggroup.h>
38 #include <kpluginfactory.h>
39 #include <kpluginloader.h>
42 K_PLUGIN_FACTORY( KCMTrashConfigFactory
, registerPlugin
<TrashConfigModule
>( "trash" ); )
43 K_EXPORT_PLUGIN( KCMTrashConfigFactory( "kcmtrash" ) )
45 TrashConfigModule::TrashConfigModule( QWidget
* parent
, const QVariantList
& )
46 : KCModule( KCMTrashConfigFactory::componentData(), parent
), trashInitialize( false )
48 KGlobal::locale()->insertCatalog( "kio_trash" );
50 mTrashImpl
= new TrashImpl();
59 connect( mUseTimeLimit
, SIGNAL( toggled( bool ) ),
60 this, SLOT( changed() ) );
61 connect( mUseTimeLimit
, SIGNAL( toggled( bool ) ),
62 this, SLOT( useTypeChanged() ) );
63 connect( mDays
, SIGNAL( valueChanged( int ) ),
64 this, SLOT( changed() ) );
65 connect( mUseSizeLimit
, SIGNAL( toggled( bool ) ),
66 this, SLOT( changed() ) );
67 connect( mUseSizeLimit
, SIGNAL( toggled( bool ) ),
68 this, SLOT( useTypeChanged() ) );
69 connect( mPercent
, SIGNAL( valueChanged( double ) ),
70 this, SLOT( percentChanged( double ) ) );
71 connect( mPercent
, SIGNAL( valueChanged( double ) ),
72 this, SLOT( changed() ) );
73 connect( mLimitReachedAction
, SIGNAL( activated( int ) ),
74 this, SLOT( changed() ) );
77 trashInitialize
= true;
80 TrashConfigModule::~TrashConfigModule()
84 void TrashConfigModule::save()
86 if ( !mCurrentTrash
.isEmpty() ) {
88 entry
.useTimeLimit
= mUseTimeLimit
->isChecked();
89 entry
.days
= mDays
->value();
90 entry
.useSizeLimit
= mUseSizeLimit
->isChecked();
91 entry
.percent
= mPercent
->value(),
92 entry
.actionType
= mLimitReachedAction
->currentIndex();
93 mConfigMap
.insert( mCurrentTrash
, entry
);
99 void TrashConfigModule::defaults()
102 entry
.useTimeLimit
= false;
104 entry
.useSizeLimit
= true;
105 entry
.percent
= 10.0;
106 entry
.actionType
= 0;
107 mConfigMap
.insert( mCurrentTrash
, entry
);
108 trashInitialize
= false;
112 void TrashConfigModule::percentChanged( double percent
)
114 DiscSpaceUtil
util( mCurrentTrash
);
116 qulonglong partitionSize
= util
.size();
117 double size
= ((double)(partitionSize
/100))*percent
;
119 QString unit
= i18n( "Byte" );
121 unit
= i18n( "KByte" );
125 unit
= i18n( "MByte" );
129 unit
= i18n( "GByte" );
133 unit
= i18n( "TByte" );
137 mSizeLabel
->setText( i18n( "(%1 %2)", QString::number( size
, 'f', 2 ), unit
) );
140 void TrashConfigModule::trashChanged( QListWidgetItem
*item
)
142 trashChanged( item
->data( Qt::UserRole
).toInt() );
145 void TrashConfigModule::trashChanged( int value
)
147 const TrashImpl::TrashDirMap map
= mTrashImpl
->trashDirectories();
149 if ( !mCurrentTrash
.isEmpty() && trashInitialize
) {
151 entry
.useTimeLimit
= mUseTimeLimit
->isChecked();
152 entry
.days
= mDays
->value();
153 entry
.useSizeLimit
= mUseSizeLimit
->isChecked();
154 entry
.percent
= mPercent
->value(),
155 entry
.actionType
= mLimitReachedAction
->currentIndex();
156 mConfigMap
.insert( mCurrentTrash
, entry
);
159 mCurrentTrash
= map
[ value
];
160 if ( mConfigMap
.contains( mCurrentTrash
) ) {
161 const ConfigEntry entry
= mConfigMap
[ mCurrentTrash
];
162 mUseTimeLimit
->setChecked( entry
.useTimeLimit
);
163 mDays
->setValue( entry
.days
);
164 mUseSizeLimit
->setChecked( entry
.useSizeLimit
);
165 mPercent
->setValue( entry
.percent
);
166 mLimitReachedAction
->setCurrentIndex( entry
.actionType
);
168 mUseTimeLimit
->setChecked( false );
169 mDays
->setValue( 7 );
170 mUseSizeLimit
->setChecked( true );
171 mPercent
->setValue( 10.0 );
172 mLimitReachedAction
->setCurrentIndex( 0 );
175 percentChanged( mPercent
->value() );
179 void TrashConfigModule::useTypeChanged()
181 mDays
->setEnabled( mUseTimeLimit
->isChecked() );
182 mSizeWidget
->setEnabled( mUseSizeLimit
->isChecked() );
185 void TrashConfigModule::readConfig()
187 KConfig
config( "ktrashrc" );
190 const QStringList groups
= config
.groupList();
191 for ( int i
= 0; i
< groups
.count(); ++i
) {
192 if ( groups
[ i
].startsWith( '/' ) ) {
193 const KConfigGroup group
= config
.group( groups
[ i
] );
196 entry
.useTimeLimit
= group
.readEntry( "UseTimeLimit", false );
197 entry
.days
= group
.readEntry( "Days", 7 );
198 entry
.useSizeLimit
= group
.readEntry( "UseSizeLimit", true );
199 entry
.percent
= group
.readEntry( "Percent", 10.0 );
200 entry
.actionType
= group
.readEntry( "LimitReachedAction", 0 );
201 mConfigMap
.insert( groups
[ i
], entry
);
206 void TrashConfigModule::writeConfig()
208 KConfig
config( "ktrashrc" );
210 // first delete all existing groups
211 const QStringList groups
= config
.groupList();
212 for ( int i
= 0; i
< groups
.count(); ++i
)
213 if ( groups
[ i
].startsWith( '/' ) )
214 config
.deleteGroup( groups
[ i
] );
216 QMapIterator
<QString
, ConfigEntry
> it( mConfigMap
);
217 while ( it
.hasNext() ) {
219 KConfigGroup group
= config
.group( it
.key() );
221 group
.writeEntry( "UseTimeLimit", it
.value().useTimeLimit
);
222 group
.writeEntry( "Days", it
.value().days
);
223 group
.writeEntry( "UseSizeLimit", it
.value().useSizeLimit
);
224 group
.writeEntry( "Percent", it
.value().percent
);
225 group
.writeEntry( "LimitReachedAction", it
.value().actionType
);
230 void TrashConfigModule::setupGui()
232 QVBoxLayout
*layout
= new QVBoxLayout( this );
233 layout
->setMargin( 11 );
234 layout
->setSpacing( 6 );
236 TrashImpl::TrashDirMap map
= mTrashImpl
->trashDirectories();
237 if ( map
.count() != 1 ) {
238 // If we have multiple trashes, we setup a widget to choose
239 // which trash to configure
240 QListWidget
*mountPoints
= new QListWidget( this );
241 layout
->addWidget( mountPoints
);
243 QMapIterator
<int, QString
> it( map
);
244 while ( it
.hasNext() ) {
246 DiscSpaceUtil
util( it
.value() );
247 QListWidgetItem
*item
= new QListWidgetItem( KIcon( "folder" ), util
.mountPoint() );
248 item
->setData( Qt::UserRole
, it
.key() );
250 mountPoints
->addItem( item
);
253 mountPoints
->setCurrentRow( 0 );
255 connect( mountPoints
, SIGNAL( currentItemChanged( QListWidgetItem
*, QListWidgetItem
* ) ),
256 this, SLOT( trashChanged( QListWidgetItem
* ) ) );
258 mCurrentTrash
= map
.value( 0 );
261 QHBoxLayout
*daysLayout
= new QHBoxLayout();
262 layout
->addLayout( daysLayout
);
264 mUseTimeLimit
= new QCheckBox( i18n( "Delete files older than:" ), this );
265 daysLayout
->addWidget( mUseTimeLimit
);
266 mDays
= new QSpinBox( this );
267 mDays
->setRange( 1, 365 );
268 mDays
->setSingleStep( 1 );
269 mDays
->setSuffix( " days" );
270 daysLayout
->addWidget( mDays
);
272 QGridLayout
*sizeLayout
= new QGridLayout();
273 layout
->addLayout( sizeLayout
);
275 mUseSizeLimit
= new QCheckBox( i18n( "Limit to maximum size" ), this );
276 sizeLayout
->addWidget( mUseSizeLimit
, 0, 0, 1, 2 );
278 mSizeWidget
= new QWidget( this );
279 sizeLayout
->setColumnMinimumWidth( 0, 30 );
280 sizeLayout
->addWidget( mSizeWidget
, 1, 1 );
282 QGridLayout
*sizeWidgetLayout
= new QGridLayout( mSizeWidget
);
283 sizeWidgetLayout
->setMargin( 11 );
284 sizeWidgetLayout
->setSpacing( 6 );
286 QLabel
*label
= new QLabel( i18n( "Maximum size:" ), mSizeWidget
);
287 sizeWidgetLayout
->addWidget( label
, 0, 0 );
289 mPercent
= new QDoubleSpinBox( mSizeWidget
);
290 mPercent
->setRange( 0.001, 100 );
291 mPercent
->setDecimals( 3 );
292 mPercent
->setSingleStep( 1 );
293 mPercent
->setSuffix( " %" );
294 sizeWidgetLayout
->addWidget( mPercent
, 0, 1 );
296 mSizeLabel
= new QLabel( mSizeWidget
);
297 sizeWidgetLayout
->addWidget( mSizeLabel
, 0, 2 );
299 label
= new QLabel( i18n( "When limit reached:" ), mSizeWidget
);
300 sizeWidgetLayout
->addWidget( label
, 1, 0 );
302 mLimitReachedAction
= new QComboBox( mSizeWidget
);
303 mLimitReachedAction
->addItem( i18n( "Warn Me" ) );
304 mLimitReachedAction
->addItem( i18n( "Delete Oldest Files From Trash" ) );
305 mLimitReachedAction
->addItem( i18n( "Delete Biggest Files From Trash" ) );
306 sizeWidgetLayout
->addWidget( mLimitReachedAction
, 1, 1, 1, 2 );
308 layout
->addStretch();
311 #include "kcmtrash.moc"