|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
This file is part of the KDE project |
| 3 |
|
| 4 |
Copyright (C) 2008 Tobias Koenig <tokoe@kde.org> |
| 5 |
Copyright (C) 2016 Emanoil Kotsev <deloptes@yahoo.com> |
| 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., 51 Franklin Street, Fifth Floor, |
| 20 |
Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "ktrashpropsdlgplugin.h" |
| 24 |
#include "discspaceutil.h" |
| 25 |
#include "trashimpl.h" |
| 26 |
|
| 27 |
#include <tqcheckbox.h> |
| 28 |
#include <tqcombobox.h> |
| 29 |
#include <tqlabel.h> |
| 30 |
#include <tqlayout.h> |
| 31 |
#include <tqlistbox.h> |
| 32 |
#include <tqspinbox.h> |
| 33 |
|
| 34 |
#include <kdesktopfile.h> |
| 35 |
#include <kgenericfactory.h> |
| 36 |
#include <tdeglobal.h> |
| 37 |
#include <kiconloader.h> |
| 38 |
#include <tdelocale.h> |
| 39 |
#include <knuminput.h> |
| 40 |
|
| 41 |
#include <kdebug.h> |
| 42 |
|
| 43 |
typedef KGenericFactory<KTrashPropsDlgPlugin, KPropertiesDialog> Factory; |
| 44 |
K_EXPORT_COMPONENT_FACTORY( ktrashpropsdlgplugin, Factory( "ktrashpropsdlgplugin" ) ) |
| 45 |
|
| 46 |
KTrashPropsDlgPlugin::KTrashPropsDlgPlugin( KPropertiesDialog *dialog, const char*, const TQStringList& ) |
| 47 |
: KPropsDlgPlugin( dialog ) |
| 48 |
{ |
| 49 |
kdDebug() << "KTrashPropsDlgPlugin::KTrashPropsDlgPlugin called" << endl; |
| 50 |
if ( dialog->items().count() != 1 ) |
| 51 |
return; |
| 52 |
|
| 53 |
KFileItem *item = dialog->items().first(); |
| 54 |
|
| 55 |
if ( !KPropsDlgPlugin::isDesktopFile( item ) ) |
| 56 |
return; |
| 57 |
|
| 58 |
kdDebug() << "KPropsDlgPlugin::isDesktopFile( item ) passed" << endl; |
| 59 |
KDesktopFile deskFile( item->url().path(), true /* readonly */ ); |
| 60 |
|
| 61 |
if ( deskFile.readURL() != "trash:/" ) |
| 62 |
return; |
| 63 |
|
| 64 |
kdDebug() << "deskFile.readURL() != \"trash:/\") passed" << endl; |
| 65 |
TDEGlobal::locale()->insertCatalogue( "tdeio_trash" ); |
| 66 |
|
| 67 |
mTrashImpl = new TrashImpl(); |
| 68 |
mTrashImpl->init(); |
| 69 |
|
| 70 |
readConfig(); |
| 71 |
|
| 72 |
TQFrame *frame = dialog->addPage( i18n( "Size Limits" ) ); |
| 73 |
setupGui( frame ); |
| 74 |
|
| 75 |
|
| 76 |
mUseTimeLimit->setChecked( mConfigMap[ mCurrentTrash ].useTimeLimit ); |
| 77 |
mUseSizeLimit->setChecked( mConfigMap[ mCurrentTrash ].useSizeLimit ); |
| 78 |
mDays->setValue( mConfigMap[ mCurrentTrash ].days ); |
| 79 |
mPercent->setValue( mConfigMap[ mCurrentTrash ].percent ); |
| 80 |
mPercent->setPrecision(3); |
| 81 |
mLimitReachedAction->setCurrentItem( mConfigMap[ mCurrentTrash ].actionType ); |
| 82 |
percentChanged(mPercent->value()); |
| 83 |
|
| 84 |
useTypeChanged(); |
| 85 |
|
| 86 |
connect( mUseTimeLimit, SIGNAL( toggled( bool ) ), |
| 87 |
this, SLOT( setDirty() ) ); |
| 88 |
connect( mUseTimeLimit, SIGNAL( toggled( bool ) ), |
| 89 |
this, SLOT( useTypeChanged() ) ); |
| 90 |
connect( mDays, SIGNAL( valueChanged( int ) ), |
| 91 |
this, SLOT( setDirty() ) ); |
| 92 |
connect( mUseSizeLimit, SIGNAL( toggled( bool ) ), |
| 93 |
this, SLOT( setDirty() ) ); |
| 94 |
connect( mUseSizeLimit, SIGNAL( toggled( bool ) ), |
| 95 |
this, SLOT( useTypeChanged() ) ); |
| 96 |
connect( mPercent, SIGNAL( valueChanged( double ) ), |
| 97 |
this, SLOT( percentChanged( double ) ) ); |
| 98 |
connect( mPercent, SIGNAL( valueChanged( double ) ), |
| 99 |
this, SLOT( setDirty() ) ); |
| 100 |
connect( mLimitReachedAction, SIGNAL( activated( int ) ), |
| 101 |
this, SLOT( setDirty() ) ); |
| 102 |
|
| 103 |
trashChanged( 0 ); |
| 104 |
} |
| 105 |
|
| 106 |
KTrashPropsDlgPlugin::~KTrashPropsDlgPlugin() |
| 107 |
{ |
| 108 |
} |
| 109 |
|
| 110 |
void KTrashPropsDlgPlugin::applyChanges() |
| 111 |
{ |
| 112 |
if ( !mCurrentTrash.isEmpty() ) { |
| 113 |
ConfigEntry entry; |
| 114 |
entry.useTimeLimit = mUseTimeLimit->isChecked(); |
| 115 |
entry.days = mDays->value(); |
| 116 |
entry.useSizeLimit = mUseSizeLimit->isChecked(); |
| 117 |
entry.percent = mPercent->value(), |
| 118 |
entry.actionType = mLimitReachedAction->currentItem(); |
| 119 |
mConfigMap.insert( mCurrentTrash, entry ); |
| 120 |
} |
| 121 |
|
| 122 |
writeConfig(); |
| 123 |
} |
| 124 |
|
| 125 |
void KTrashPropsDlgPlugin::percentChanged( double percent ) |
| 126 |
{ |
| 127 |
DiscSpaceUtil util( mCurrentTrash ); |
| 128 |
|
| 129 |
double partitionSize = util.size() * 1024.0; // convert to byte |
| 130 |
|
| 131 |
double size = partitionSize*(percent/100.0); |
| 132 |
|
| 133 |
// Step should be depending on size |
| 134 |
// https://api.kde.org/3.5-api/kdelibs-apidocs/kdeui/html/classKDoubleSpinBox.html |
| 135 |
// https://api.kde.org/3.5-api/kdelibs-apidocs/kdeui/html/knuminput_8cpp_source.html |
| 136 |
mPercent->setPrecision(3); |
| 137 |
|
| 138 |
TQString unit = i18n( "Byte" ); |
| 139 |
if ( size > 1024 ) { |
| 140 |
unit = i18n( "KByte" ); |
| 141 |
size = size/1024.0; |
| 142 |
mPercent->setLineStep(0.001); |
| 143 |
} |
| 144 |
if ( size > 1024 ) { |
| 145 |
unit = i18n( "MByte" ); |
| 146 |
size = size/1024.0; |
| 147 |
mPercent->setLineStep(0.01); |
| 148 |
} |
| 149 |
if ( size > 1024 ) { |
| 150 |
unit = i18n( "GByte" ); |
| 151 |
size = size/1024.0; |
| 152 |
mPercent->setLineStep(0.1); |
| 153 |
} |
| 154 |
if ( size > 1024 ) { |
| 155 |
unit = i18n( "TByte" ); |
| 156 |
size = size/1024.0; |
| 157 |
mPercent->setLineStep(1); |
| 158 |
} |
| 159 |
|
| 160 |
mSizeLabel->setText( i18n( "(%1 %2)" ).arg( TQString::number( size, 'f', 2 ) ).arg( unit ) ); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
void KTrashPropsDlgPlugin::trashChanged( int value ) |
| 165 |
{ |
| 166 |
const TrashImpl::TrashDirMap map = mTrashImpl->trashDirectories(); |
| 167 |
|
| 168 |
if ( !mCurrentTrash.isEmpty() ) { |
| 169 |
ConfigEntry entry; |
| 170 |
entry.useTimeLimit = mUseTimeLimit->isChecked(); |
| 171 |
entry.days = mDays->value(); |
| 172 |
entry.useSizeLimit = mUseSizeLimit->isChecked(); |
| 173 |
entry.percent = mPercent->value(), |
| 174 |
entry.actionType = mLimitReachedAction->currentItem(); |
| 175 |
mConfigMap.insert( mCurrentTrash, entry ); |
| 176 |
} |
| 177 |
|
| 178 |
mCurrentTrash = map[ value ]; |
| 179 |
|
| 180 |
if ( mConfigMap.contains( mCurrentTrash ) ) { |
| 181 |
const ConfigEntry entry = mConfigMap[ mCurrentTrash ]; |
| 182 |
mUseTimeLimit->setChecked( entry.useTimeLimit ); |
| 183 |
mDays->setValue( entry.days ); |
| 184 |
mUseSizeLimit->setChecked( entry.useSizeLimit ); |
| 185 |
mPercent->setValue( entry.percent ); |
| 186 |
mLimitReachedAction->setCurrentItem( entry.actionType ); |
| 187 |
} else { |
| 188 |
mUseTimeLimit->setChecked( false ); |
| 189 |
mDays->setValue( 7 ); |
| 190 |
mUseSizeLimit->setChecked( true ); |
| 191 |
mPercent->setValue( 10.0 ); |
| 192 |
mLimitReachedAction->setCurrentItem( 0 ); |
| 193 |
} |
| 194 |
|
| 195 |
percentChanged( mPercent->value() ); |
| 196 |
} |
| 197 |
|
| 198 |
void KTrashPropsDlgPlugin::useTypeChanged() |
| 199 |
{ |
| 200 |
mDays->setEnabled( mUseTimeLimit->isChecked() ); |
| 201 |
mSizeWidget->setEnabled( mUseSizeLimit->isChecked() ); |
| 202 |
} |
| 203 |
|
| 204 |
void KTrashPropsDlgPlugin::readConfig() |
| 205 |
{ |
| 206 |
kdDebug() << "KTrashPropsDlgPlugin::readConfig() called" << endl; |
| 207 |
TDEConfig config( "trashrc" ); |
| 208 |
mConfigMap.clear(); |
| 209 |
|
| 210 |
const TQStringList groups = config.groupList(); |
| 211 |
for ( uint i = 0; i < groups.count(); ++i ) { |
| 212 |
if ( groups[ i ].startsWith( "/" ) ) { |
| 213 |
config.setGroup( groups[ i ] ); |
| 214 |
ConfigEntry entry; |
| 215 |
entry.useTimeLimit = config.readBoolEntry( "UseTimeLimit", false ); |
| 216 |
entry.days = config.readNumEntry( "Days", 7 ); |
| 217 |
entry.useSizeLimit = config.readBoolEntry( "UseSizeLimit", true ); |
| 218 |
entry.percent = config.readDoubleNumEntry( "Percent", 10 ); |
| 219 |
entry.actionType = config.readNumEntry( "LimitReachedAction", 0 ); |
| 220 |
mConfigMap.insert( groups[ i ], entry ); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void KTrashPropsDlgPlugin::writeConfig() |
| 226 |
{ |
| 227 |
kdDebug() << "KTrashPropsDlgPlugin::writeConfig() called" << endl; |
| 228 |
TDEConfig config( "trashrc" ); |
| 229 |
|
| 230 |
// first delete all existing groups |
| 231 |
const TQStringList groups = config.groupList(); |
| 232 |
for ( uint i = 0; i < groups.count(); ++i ) |
| 233 |
if ( groups[ i ].startsWith( "/" ) ) |
| 234 |
config.deleteGroup( groups[ i ] ); |
| 235 |
|
| 236 |
TQMapConstIterator<TQString, ConfigEntry> it; |
| 237 |
for ( it = mConfigMap.begin(); it != mConfigMap.end(); ++it ) { |
| 238 |
config.setGroup( it.key() ); |
| 239 |
config.writeEntry( "UseTimeLimit", it.data().useTimeLimit ); |
| 240 |
config.writeEntry( "Days", it.data().days ); |
| 241 |
config.writeEntry( "UseSizeLimit", it.data().useSizeLimit ); |
| 242 |
config.writeEntry( "Percent", it.data().percent ); |
| 243 |
config.writeEntry( "LimitReachedAction", it.data().actionType ); |
| 244 |
} |
| 245 |
|
| 246 |
config.sync(); |
| 247 |
} |
| 248 |
|
| 249 |
void KTrashPropsDlgPlugin::setupGui( TQFrame *frame ) |
| 250 |
{ |
| 251 |
kdDebug() << "KTrashPropsDlgPlugin::setupGui( TQFrame *frame )" << endl; |
| 252 |
TQVBoxLayout *layout = new TQVBoxLayout( frame, 11, 6 ); |
| 253 |
|
| 254 |
TrashImpl::TrashDirMap map = mTrashImpl->trashDirectories(); |
| 255 |
if ( map.count() != 1 ) { |
| 256 |
// If we have multiple trashes, we setup a widget to choose |
| 257 |
// which trash to configure |
| 258 |
TQListBox *mountPoints = new TQListBox( frame ); |
| 259 |
layout->addWidget( mountPoints ); |
| 260 |
|
| 261 |
const TQPixmap folderPixmap = TDEGlobal::iconLoader()->loadIcon( "folder", TDEIcon::Small ); |
| 262 |
TQMapConstIterator<int, TQString> it; |
| 263 |
for ( it = map.begin(); it != map.end(); ++it ) { |
| 264 |
DiscSpaceUtil util( it.data() ); |
| 265 |
mountPoints->insertItem( folderPixmap, util.mountPoint(), it.key() ); |
| 266 |
} |
| 267 |
|
| 268 |
mountPoints->setCurrentItem( 0 ); |
| 269 |
|
| 270 |
connect( mountPoints, SIGNAL( highlighted( int ) ), SLOT( trashChanged( int ) ) ); |
| 271 |
} else { |
| 272 |
mCurrentTrash = map[0]; |
| 273 |
} |
| 274 |
|
| 275 |
TQHBoxLayout *daysLayout = new TQHBoxLayout( layout ); |
| 276 |
|
| 277 |
mUseTimeLimit = new TQCheckBox( i18n( "Delete files older than:" ), frame ); |
| 278 |
daysLayout->addWidget( mUseTimeLimit ); |
| 279 |
mDays = new TQSpinBox( 1, 365, 1, frame ); |
| 280 |
mDays->setSuffix( " days" ); |
| 281 |
daysLayout->addWidget( mDays ); |
| 282 |
|
| 283 |
TQGridLayout *sizeLayout = new TQGridLayout( layout, 2, 2 ); |
| 284 |
mUseSizeLimit = new TQCheckBox( i18n( "Limit to maximum size" ), frame ); |
| 285 |
sizeLayout->addMultiCellWidget( mUseSizeLimit, 0, 0, 0, 1 ); |
| 286 |
|
| 287 |
mSizeWidget = new TQWidget( frame ); |
| 288 |
sizeLayout->setColSpacing( 0, 30 ); |
| 289 |
sizeLayout->addWidget( mSizeWidget, 1, 1 ); |
| 290 |
|
| 291 |
TQGridLayout *sizeWidgetLayout = new TQGridLayout( mSizeWidget, 2, 3, 11, 6 ); |
| 292 |
|
| 293 |
TQLabel *label = new TQLabel( i18n( "Maximum Size:" ), mSizeWidget ); |
| 294 |
sizeWidgetLayout->addWidget( label, 0, 0 ); |
| 295 |
|
| 296 |
mPercent = new KDoubleSpinBox( 0.001, 100, 1, 10, 3, mSizeWidget ); |
| 297 |
mPercent->setSuffix( " %" ); |
| 298 |
sizeWidgetLayout->addWidget( mPercent, 0, 1 ); |
| 299 |
|
| 300 |
mSizeLabel = new TQLabel( mSizeWidget ); |
| 301 |
sizeWidgetLayout->addWidget( mSizeLabel, 0, 2 ); |
| 302 |
|
| 303 |
label = new TQLabel( i18n( "When limit reached:" ), mSizeWidget ); |
| 304 |
sizeWidgetLayout->addWidget( label, 1, 0 ); |
| 305 |
|
| 306 |
mLimitReachedAction = new TQComboBox( mSizeWidget ); |
| 307 |
mLimitReachedAction->insertItem( i18n( "Warn me" ) ); |
| 308 |
mLimitReachedAction->insertItem( i18n( "Delete oldest files from trash" ) ); |
| 309 |
mLimitReachedAction->insertItem( i18n( "Delete biggest files from trash" ) ); |
| 310 |
sizeWidgetLayout->addMultiCellWidget( mLimitReachedAction, 1, 1, 1, 2 ); |
| 311 |
|
| 312 |
layout->addStretch(); |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
#include "ktrashpropsdlgplugin.moc" |