kmail

archivefolderdialog.cpp

00001 /* Copyright 2009 Klarälvdalens Datakonsult AB
00002 
00003    This program is free software; you can redistribute it and/or
00004    modify it under the terms of the GNU General Public License as
00005    published by the Free Software Foundation; either version 2 of
00006    the License or (at your option) version 3 or any later version
00007    accepted by the membership of KDE e.V. (or its successor approved
00008    by the membership of KDE e.V.), which shall act as a proxy
00009    defined in Section 14 of version 3 of the license.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 #include "archivefolderdialog.h"
00020 
00021 #include "backupjob.h"
00022 #include "kmkernel.h"
00023 #include "kmfolder.h"
00024 #include "kmmainwidget.h"
00025 #include "folderrequester.h"
00026 #include "util.h"
00027 
00028 #include <klocale.h>
00029 #include <kcombobox.h>
00030 #include <kurlrequester.h>
00031 #include <kmessagebox.h>
00032 #include <kfiledialog.h>
00033 
00034 #include <qlabel.h>
00035 #include <qcheckbox.h>
00036 #include <qlayout.h>
00037 
00038 using namespace KMail;
00039 
00040 static QString standardArchivePath( const QString &folderName )
00041 {
00042   return KGlobalSettings::documentPath() +
00043           i18n( "Start of the filename for a mail archive file" , "Archive" ) + "_" + folderName +
00044            "_" + QDate::currentDate().toString( Qt::ISODate ) + ".tar.bz2";
00045 }
00046 
00047 ArchiveFolderDialog::ArchiveFolderDialog( QWidget *parent )
00048   : KDialogBase( parent, "archive_folder_dialog", false, i18n( "Archive Folder" ),
00049                  KDialogBase::Ok | KDialogBase::Cancel,
00050                  KDialogBase::Ok, true ),
00051     mParentWidget( parent )
00052 {
00053   QWidget *mainWidget = new QWidget( this );
00054   QGridLayout *mainLayout = new QGridLayout( mainWidget );
00055   mainLayout->setSpacing( KDialog::spacingHint() );
00056   mainLayout->setMargin( KDialog::marginHint() );
00057   setMainWidget( mainWidget );
00058 
00059   int row = 0;
00060 
00061   // TODO: better label for "Ok" button
00062   // TODO: Explaination label
00063   // TODO: Use QFormLayout in KDE4
00064 
00065   QLabel *folderLabel = new QLabel( i18n( "&Folder:" ), mainWidget );
00066   mainLayout->addWidget( folderLabel, row, 0 );
00067   mFolderRequester = new FolderRequester( mainWidget, kmkernel->getKMMainWidget()->folderTree() );
00068   mFolderRequester->setMustBeReadWrite( false );
00069   connect( mFolderRequester, SIGNAL(folderChanged(KMFolder *)),
00070            SLOT(slotFolderChanged(KMFolder *)) );
00071   folderLabel->setBuddy( mFolderRequester );
00072   mainLayout->addWidget( mFolderRequester, row, 1 );
00073   row++;
00074 
00075   QLabel *formatLabel = new QLabel( i18n( "F&ormat:" ), mainWidget );
00076   mainLayout->addWidget( formatLabel, row, 0 );
00077   mFormatComboBox = new KComboBox( mainWidget );
00078   formatLabel->setBuddy( mFormatComboBox );
00079 
00080   // These combobox values have to stay in sync with the ArchiveType enum from BackupJob!
00081   mFormatComboBox->insertItem( i18n( "Compressed Zip Archive (.zip)" ) );
00082   mFormatComboBox->insertItem( i18n( "Uncompressed Archive (.tar)" ) );
00083   mFormatComboBox->insertItem( i18n( "BZ2-Compressed Tar Archive (.tar.bz2)" ) );
00084   mFormatComboBox->insertItem( i18n( "GZ-Compressed Tar Archive (.tar.gz)" ) );
00085   mFormatComboBox->setCurrentItem( 2 );
00086   connect( mFormatComboBox, SIGNAL(activated(int)),
00087            this, SLOT(slotFixFileExtension()) );
00088   mainLayout->addWidget( mFormatComboBox, row, 1 );
00089   row++;
00090 
00091   QLabel *fileNameLabel = new QLabel( i18n( "&Archive File:" ), mainWidget );
00092   mainLayout->addWidget( fileNameLabel, row, 0 );
00093   mUrlRequester = new KURLRequester( mainWidget );
00094   mUrlRequester->setMode( KFile::LocalOnly );
00095   mUrlRequester->setFilter( "*.tar *.zip *.tar.gz *.tar.bz2" );
00096   mUrlRequester->fileDialog()->setKeepLocation( true );
00097   fileNameLabel->setBuddy( mUrlRequester );
00098   connect( mUrlRequester->lineEdit(), SIGNAL(textChanged(const QString &)),
00099            SLOT(slotUrlChanged(const QString &)) );
00100   connect( mUrlRequester, SIGNAL(urlSelected(const QString&)),
00101            this, SLOT(slotFixFileExtension()) );
00102   mainLayout->addWidget( mUrlRequester, row, 1 );
00103   row++;
00104 
00105   // TODO: Make this appear more dangerous!
00106   mDeleteCheckBox = new QCheckBox( i18n( "&Delete folders after completion" ), mainWidget );
00107   mainLayout->addMultiCellWidget( mDeleteCheckBox, row, row, 0, 1, Qt::AlignLeft );
00108   row++;
00109 
00110   // TODO: what's this, tooltips
00111 
00112   // TODO: Warn that user should do mail check for online IMAP and possibly cached IMAP as well
00113 
00114   mainLayout->setColStretch( 1, 1 );
00115   mainLayout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding ), row, 0 );
00116 
00117   // Make it a bit bigger, else the folder requester cuts off the text too early
00118   resize( 500, minimumSize().height() );
00119 }
00120 
00121 void ArchiveFolderDialog::slotUrlChanged( const QString &text )
00122 {
00123   enableButton( Ok, !text.isEmpty() );
00124 }
00125 
00126 bool canRemoveFolder( KMFolder *folder )
00127 {
00128   return
00129     folder &&
00130     folder->canDeleteMessages() &&
00131     !folder->noContent() &&
00132     !folder->isSystemFolder();
00133 }
00134 
00135 void ArchiveFolderDialog::slotFolderChanged( KMFolder *folder )
00136 {
00137   mDeleteCheckBox->setEnabled( canRemoveFolder( folder ) );
00138   enableButton( Ok, folder && !folder->noContent());
00139 }
00140 
00141 void ArchiveFolderDialog::setFolder( KMFolder *defaultFolder )
00142 {
00143   mFolderRequester->setFolder( defaultFolder );
00144   // TODO: what if the file already exists?
00145   mUrlRequester->setURL( standardArchivePath( defaultFolder->name() ) );
00146   mDeleteCheckBox->setEnabled( canRemoveFolder( defaultFolder ) );
00147 }
00148 
00149 void ArchiveFolderDialog::slotOk()
00150 {
00151   if ( !Util::checkOverwrite( mUrlRequester->url(), this ) ) {
00152     return;
00153   }
00154 
00155   if ( !mFolderRequester->folder() ) {
00156     KMessageBox::information( this, i18n( "Please select the folder that should be archived." ),
00157                               i18n( "No folder selected" ) );
00158     return;
00159   }
00160 
00161   // TODO: check if url is empty. or better yet, disable ok button until file is chosen
00162   KMail::BackupJob *backupJob = new KMail::BackupJob( mParentWidget );
00163   backupJob->setRootFolder( mFolderRequester->folder() );
00164   backupJob->setSaveLocation( mUrlRequester->url() );
00165   backupJob->setArchiveType( static_cast<BackupJob::ArchiveType>( mFormatComboBox->currentItem() ) );
00166   backupJob->setDeleteFoldersAfterCompletion( mDeleteCheckBox->isEnabled() &&
00167                                               mDeleteCheckBox->isChecked() );
00168   backupJob->start();
00169   accept();
00170 }
00171 
00172 void ArchiveFolderDialog::slotFixFileExtension()
00173 {
00174   // KDE4: use KMimeType::extractKnownExtension() here
00175   const int numExtensions = 4;
00176 
00177   // These extensions are sorted differently, .tar has to come last, or it will match before giving
00178   // the more specific ones time to match.
00179   const char *sortedExtensions[numExtensions] = { ".zip", ".tar.bz2", ".tar.gz", ".tar" };
00180 
00181   // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
00182   const char *extensions[numExtensions] = { ".zip", ".tar", ".tar.bz2", ".tar.gz" };
00183 
00184   QString fileName = mUrlRequester->url();
00185   if ( fileName.isEmpty() ) {
00186     fileName = standardArchivePath( mFolderRequester->folder() ?
00187                                     mFolderRequester->folder()->name() : "" );
00188   }
00189 
00190   // First, try to find the extension of the file name and remove it
00191   for( int i = 0; i < numExtensions; i++ ) {
00192     int index = fileName.lower().findRev( sortedExtensions[i] );
00193     if ( index != -1 ) {
00194       fileName = fileName.left( fileName.length() - QString( sortedExtensions[i] ).length() );
00195       break;
00196     }
00197   }
00198 
00199   // Now, we've got a filename without an extension, simply append the correct one
00200   fileName += extensions[mFormatComboBox->currentItem()];
00201   mUrlRequester->setURL( fileName );
00202 }
00203 
00204 #include "archivefolderdialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys