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 void ArchiveFolderDialog::slotFolderChanged( KMFolder *folder )
00127 {
00128   mDeleteCheckBox->setEnabled( folder->canDeleteMessages() );
00129 }
00130 
00131 void ArchiveFolderDialog::setFolder( KMFolder *defaultFolder )
00132 {
00133   mFolderRequester->setFolder( defaultFolder );
00134   // TODO: what if the file already exists?
00135   mUrlRequester->setURL( standardArchivePath( defaultFolder->name() ) );
00136   mDeleteCheckBox->setEnabled( defaultFolder->canDeleteMessages() );
00137 }
00138 
00139 void ArchiveFolderDialog::slotOk()
00140 {
00141   if ( !Util::checkOverwrite( mUrlRequester->url(), this ) ) {
00142     return;
00143   }
00144 
00145   if ( !mFolderRequester->folder() ) {
00146     KMessageBox::information( this, i18n( "Please select the folder that should be archived." ),
00147                               i18n( "No folder selected" ) );
00148     return;
00149   }
00150 
00151   // TODO: check if url is empty. or better yet, disable ok button until file is chosen
00152   KMail::BackupJob *backupJob = new KMail::BackupJob( mParentWidget );
00153   backupJob->setRootFolder( mFolderRequester->folder() );
00154   backupJob->setSaveLocation( mUrlRequester->url() );
00155   backupJob->setArchiveType( static_cast<BackupJob::ArchiveType>( mFormatComboBox->currentItem() ) );
00156   backupJob->setDeleteFoldersAfterCompletion( mDeleteCheckBox->isChecked() &&
00157                                               mFolderRequester->folder()->canDeleteMessages() );
00158   backupJob->start();
00159   accept();
00160 }
00161 
00162 void ArchiveFolderDialog::slotFixFileExtension()
00163 {
00164   // KDE4: use KMimeType::extractKnownExtension() here
00165   const int numExtensions = 4;
00166 
00167   // These extensions are sorted differently, .tar has to come last, or it will match before giving
00168   // the more specific ones time to match.
00169   const char *sortedExtensions[numExtensions] = { ".zip", ".tar.bz2", ".tar.gz", ".tar" };
00170 
00171   // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
00172   const char *extensions[numExtensions] = { ".zip", ".tar", ".tar.bz2", ".tar.gz" };
00173 
00174   QString fileName = mUrlRequester->url();
00175 
00176   // First, try to find the extension of the file name and remove it
00177   for( int i = 0; i < numExtensions; i++ ) {
00178     int index = fileName.lower().findRev( sortedExtensions[i] );
00179     if ( index != -1 ) {
00180       fileName = fileName.left( fileName.length() - QString( sortedExtensions[i] ).length() );
00181       break;
00182     }
00183   }
00184 
00185   // Now, we've got a filename without an extension, simply append the correct one
00186   fileName += extensions[mFormatComboBox->currentItem()];
00187   mUrlRequester->setURL( fileName );
00188 }
00189 
00190 #include "archivefolderdialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys