File indexing completed on 2024-10-06 04:26:03
0001 /* 0002 SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl> 0003 SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 0009 #include "k3btempdirselectionwidget.h" 0010 #include "k3bcore.h" 0011 #include "k3bglobalsettings.h" 0012 0013 #include <KLineEdit> 0014 #include <KColorScheme> 0015 #include <KConfig> 0016 #include <KLocalizedString> 0017 #include <KIconLoader> 0018 #include <KIO/Global> 0019 #include <KUrlRequester> 0020 0021 #include <QFileInfo> 0022 #include <QTimer> 0023 #include <QGroupBox> 0024 #include <QLabel> 0025 #include <QLayout> 0026 #include <QToolTip> 0027 #include <QStorageInfo> 0028 0029 0030 K3b::TempDirSelectionWidget::TempDirSelectionWidget( QWidget *parent ) 0031 : QGroupBox( parent ), 0032 m_labelCdSize(0), 0033 m_requestedSize(0), 0034 m_defaultImageFileName( "k3b_image.iso" ) 0035 { 0036 QGridLayout* layout = new QGridLayout( this ); 0037 0038 m_imageFileLabel = new QLabel( this ); 0039 m_editDirectory = new KUrlRequester( this ); 0040 0041 m_imageFileLabel->setBuddy( m_editDirectory ); 0042 0043 QLabel* freeSpaceLabel = new QLabel( i18n( "Free space in temporary folder:" ), this ); 0044 m_labelFreeSpace = new QLabel( this ); 0045 m_labelFreeSpace->setAlignment( Qt::AlignVCenter | Qt::AlignRight ); 0046 0047 layout->addWidget( m_imageFileLabel, 0, 0, 1, 2 ); 0048 layout->addWidget( m_editDirectory, 1, 0, 1, 2 ); 0049 layout->addWidget( freeSpaceLabel, 2, 0 ); 0050 layout->addWidget( m_labelFreeSpace, 2, 1 ); 0051 0052 // do not use row 3 here since that could be used in setNeededSize below 0053 layout->setRowStretch( 4, 1 ); 0054 0055 connect( m_editDirectory, SIGNAL(textChanged(QString)), 0056 this, SLOT(slotUpdateFreeTempSpace()) ); 0057 connect( m_editDirectory->lineEdit(), SIGNAL(lostFocus()), 0058 this, SLOT(slotFixTempPath()) ); 0059 0060 // choose a default 0061 setSelectionMode( DIR ); 0062 0063 m_editDirectory->setAcceptMode(QFileDialog::AcceptSave); 0064 m_editDirectory->setUrl( QUrl::fromLocalFile( k3bcore->globalSettings()->defaultTempPath() ) ); 0065 slotUpdateFreeTempSpace(); 0066 0067 // ToolTips 0068 // -------------------------------------------------------------------------------- 0069 m_editDirectory->setToolTip( i18n("The folder in which to save the image files") ); 0070 0071 // What's This info 0072 // -------------------------------------------------------------------------------- 0073 m_editDirectory->setWhatsThis( i18n("<p>This is the folder in which K3b will save the <em>image files</em>." 0074 "<p>Please make sure that it resides on a partition that has enough free space.") ); 0075 } 0076 0077 0078 K3b::TempDirSelectionWidget::~TempDirSelectionWidget() 0079 { 0080 } 0081 0082 0083 KIO::filesize_t K3b::TempDirSelectionWidget::freeTempSpace() const 0084 { 0085 QString path = m_editDirectory->url().toLocalFile(); 0086 0087 if( !QFile::exists( path ) ) 0088 path.truncate( path.lastIndexOf('/') ); 0089 0090 const QStorageInfo diskFreeSpaceInfo( path ); 0091 return diskFreeSpaceInfo.bytesFree(); 0092 } 0093 0094 0095 void K3b::TempDirSelectionWidget::slotUpdateFreeTempSpace() 0096 { 0097 // update the temp space 0098 KIO::filesize_t tempFreeSpace = freeTempSpace(); 0099 0100 KColorScheme::ForegroundRole role; 0101 if( tempFreeSpace < m_requestedSize ) 0102 role = KColorScheme::NegativeText; 0103 else 0104 role = KColorScheme::NormalText; 0105 0106 QPalette pal( m_labelFreeSpace->palette() ); 0107 pal.setBrush( QPalette::Disabled, QPalette::WindowText, KColorScheme( QPalette::Disabled, KColorScheme::Window ).foreground( role ) ); 0108 pal.setBrush( QPalette::Active, QPalette::WindowText, KColorScheme( QPalette::Active, KColorScheme::Window ).foreground( role ) ); 0109 pal.setBrush( QPalette::Inactive, QPalette::WindowText, KColorScheme( QPalette::Inactive, KColorScheme::Window ).foreground( role ) ); 0110 pal.setBrush( QPalette::Normal, QPalette::WindowText, KColorScheme( QPalette::Normal, KColorScheme::Window ).foreground( role ) ); 0111 0112 m_labelFreeSpace->setPalette( pal ); 0113 m_labelFreeSpace->setText( KIO::convertSize(tempFreeSpace) ); 0114 0115 QTimer::singleShot( 1000, this, SLOT(slotUpdateFreeTempSpace()) ); 0116 } 0117 0118 0119 void K3b::TempDirSelectionWidget::setTempPath( const QString& dir ) 0120 { 0121 m_editDirectory->setUrl( QUrl::fromLocalFile( dir ) ); 0122 slotUpdateFreeTempSpace(); 0123 } 0124 0125 0126 QString K3b::TempDirSelectionWidget::tempPath() const 0127 { 0128 QFileInfo fi( m_editDirectory->url().toLocalFile() ); 0129 0130 if( fi.exists() ) { 0131 if( m_mode == DIR ) { 0132 if( fi.isDir() ) 0133 return fi.absoluteFilePath(); 0134 else 0135 return fi.absolutePath(); 0136 } 0137 else { 0138 if( fi.isFile() ) 0139 return fi.absoluteFilePath(); 0140 else 0141 return fi.absoluteFilePath() + "/k3b_image.iso"; 0142 } 0143 } 0144 else { 0145 return fi.absoluteFilePath(); 0146 } 0147 } 0148 0149 0150 QString K3b::TempDirSelectionWidget::plainTempPath() const 0151 { 0152 return m_editDirectory->url().toLocalFile(); 0153 } 0154 0155 0156 QString K3b::TempDirSelectionWidget::tempDirectory() const 0157 { 0158 QString td( m_editDirectory->url().toLocalFile() ); 0159 0160 // remove a trailing slash 0161 while( !td.isEmpty() && td[td.length()-1] == '/' ) 0162 td.truncate( td.length()-1 ); 0163 0164 QFileInfo fi( td ); 0165 if( fi.exists() && fi.isDir() ) 0166 return td + '/'; 0167 0168 // now we treat the last section as a filename and return the path 0169 // in front of it 0170 td.truncate( td.lastIndexOf( '/' ) + 1 ); 0171 return td; 0172 } 0173 0174 0175 void K3b::TempDirSelectionWidget::setSelectionMode( int mode ) 0176 { 0177 m_mode = mode; 0178 0179 if( m_mode == DIR ) { 0180 m_editDirectory->setWindowTitle( i18n("Select Temporary Folder") ); 0181 m_editDirectory->setMode( KFile::Directory | KFile::ExistingOnly | KFile::LocalOnly ); 0182 m_imageFileLabel->setText( i18n( "Wri&te image files to:" ) ); 0183 setTitle( i18n("Temporary Folder") ); 0184 } 0185 else { 0186 m_editDirectory->setWindowTitle( i18n("Select Temporary File") ); 0187 m_editDirectory->setMode( KFile::File | KFile::LocalOnly ); 0188 m_imageFileLabel->setText( i18n( "Wri&te image file to:" ) ); 0189 setTitle( i18n("Temporary File") ); 0190 } 0191 } 0192 0193 0194 void K3b::TempDirSelectionWidget::setNeededSize( KIO::filesize_t bytes ) 0195 { 0196 m_requestedSize = bytes; 0197 if( !m_labelCdSize ) { 0198 QGridLayout* grid = static_cast<QGridLayout*>( layout() ); 0199 grid->addWidget( new QLabel( i18n( "Size of project:" ), this ), 3, 0 ); 0200 m_labelCdSize = new QLabel( KIO::convertSize(m_requestedSize), this ); 0201 m_labelCdSize->setAlignment( Qt::AlignVCenter | Qt::AlignRight ); 0202 grid->addWidget( m_labelCdSize, 3, 1 ); 0203 } 0204 m_labelCdSize->setText( KIO::convertSize(m_requestedSize) ); 0205 } 0206 0207 0208 void K3b::TempDirSelectionWidget::saveConfig() 0209 { 0210 k3bcore->globalSettings()->setDefaultTempPath( tempDirectory() ); 0211 } 0212 0213 0214 void K3b::TempDirSelectionWidget::readConfig( const KConfigGroup& c ) 0215 { 0216 setTempPath( c.readPathEntry( "image path", k3bcore->globalSettings()->defaultTempPath() ) ); 0217 } 0218 0219 0220 void K3b::TempDirSelectionWidget::saveConfig( KConfigGroup c ) 0221 { 0222 c.writePathEntry( "image path", tempPath() ); 0223 } 0224 0225 0226 void K3b::TempDirSelectionWidget::setDefaultImageFileName( const QString& name, bool changeImageName ) 0227 { 0228 if ( !name.isEmpty() ) { 0229 if ( selectionMode() == FILE ) { 0230 if ( plainTempPath().section( '/', -1 ) == m_defaultImageFileName ) { 0231 changeImageName = true; 0232 } 0233 } 0234 0235 m_defaultImageFileName = name; 0236 if ( !m_defaultImageFileName.contains( '.' ) ) { 0237 m_defaultImageFileName += ".iso"; 0238 } 0239 fixTempPath( changeImageName ); 0240 } 0241 } 0242 0243 0244 void K3b::TempDirSelectionWidget::slotFixTempPath() 0245 { 0246 fixTempPath( false ); 0247 } 0248 0249 0250 void K3b::TempDirSelectionWidget::fixTempPath( bool forceNewImageName ) 0251 { 0252 // if in file selection mode and no image file is specified or 0253 // forceNewImageName is true set the default image file name 0254 if ( selectionMode() == FILE ) { 0255 if ( forceNewImageName || 0256 QFileInfo( plainTempPath() ).isDir() ) { 0257 setTempPath( tempDirectory() + m_defaultImageFileName ); 0258 } 0259 } 0260 } 0261 0262 0263 void K3b::TempDirSelectionWidget::setImageFileLabel(const QString &label) 0264 { 0265 m_imageFileLabel->setText(label); 0266 } 0267 0268 #include "moc_k3btempdirselectionwidget.cpp"