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 
00033 #include <qlabel.h>
00034 #include <qcheckbox.h>
00035 #include <qlayout.h>
00036 
00037 using namespace KMail;
00038 
00039 static QString standardArchivePath( const QString &folderName )
00040 {
00041   return KGlobalSettings::documentPath() +
00042           i18n( "Start of the filename for a mail archive file" , "Archive" ) + "_" + folderName +
00043            "_" + QDate::currentDate().toString( Qt::ISODate ) + ".zip";
00044 }
00045 
00046 ArchiveFolderDialog::ArchiveFolderDialog( QWidget *parent )
00047   : KDialogBase( parent, "archive_folder_dialog", false, i18n( "Archive Folder" ),
00048                  KDialogBase::Ok | KDialogBase::Cancel,
00049                  KDialogBase::Ok, true ),
00050     mParentWidget( parent )
00051 {
00052   QWidget *mainWidget = new QWidget( this );
00053   QGridLayout *mainLayout = new QGridLayout( mainWidget );
00054   mainLayout->setSpacing( KDialog::spacingHint() );
00055   mainLayout->setMargin( KDialog::marginHint() );
00056   setMainWidget( mainWidget );
00057 
00058   int row = 0;
00059 
00060   // TODO: better label for "Ok" button
00061   // TODO: Explaination label
00062   // TODO: Use QFormLayout in KDE4
00063 
00064   QLabel *folderLabel = new QLabel( i18n( "&Folder:" ), mainWidget );
00065   mainLayout->addWidget( folderLabel, row, 0 );
00066   mFolderRequester = new FolderRequester( mainWidget, kmkernel->getKMMainWidget()->folderTree() );
00067   folderLabel->setBuddy( mFolderRequester );
00068   mainLayout->addWidget( mFolderRequester, row, 1 );
00069   row++;
00070 
00071   QLabel *formatLabel = new QLabel( i18n( "F&ormat:" ), mainWidget );
00072   mainLayout->addWidget( formatLabel, row, 0 );
00073   mFormatComboBox = new KComboBox( mainWidget );
00074   formatLabel->setBuddy( mFormatComboBox );
00075 
00076   // These combobox values have to stay in sync with the ArchiveType enum from BackupJob!
00077   mFormatComboBox->insertItem( i18n( "Compressed Zip Archive (.zip)" ) );
00078   mFormatComboBox->insertItem( i18n( "Uncompressed Archive (.tar)" ) );
00079   mFormatComboBox->insertItem( i18n( "BZ2-Compressed Tar Archive (.tar.bz2)" ) );
00080   mFormatComboBox->insertItem( i18n( "GZ-Compressed Tar Archive (.tar.gz)" ) );
00081   mFormatComboBox->setCurrentItem( 0 );
00082   connect( mFormatComboBox, SIGNAL(activated(int)),
00083            this, SLOT(slotFixFileExtension()) );
00084   mainLayout->addWidget( mFormatComboBox, row, 1 );
00085   row++;
00086 
00087   QLabel *fileNameLabel = new QLabel( i18n( "&Archive File:" ), mainWidget );
00088   mainLayout->addWidget( fileNameLabel, row, 0 );
00089   mUrlRequester = new KURLRequester( mainWidget );
00090   mUrlRequester->setMode( KFile::LocalOnly );
00091   mUrlRequester->setFilter( "*.tar *.zip *.tar.gz *.tar.bz2" );
00092   fileNameLabel->setBuddy( mUrlRequester );
00093   connect( mUrlRequester, SIGNAL(urlSelected(const QString&)),
00094            this, SLOT(slotFixFileExtension()) );
00095   mainLayout->addWidget( mUrlRequester, row, 1 );
00096   row++;
00097 
00098   // TODO: Make this appear more dangerous!
00099   mDeleteCheckBox = new QCheckBox( i18n( "&Delete folders after completion" ), mainWidget );
00100   mainLayout->addMultiCellWidget( mDeleteCheckBox, row, row, 0, 1, Qt::AlignLeft );
00101   row++;
00102 
00103   // TODO: what's this, tooltips
00104 
00105   // TODO: Warn that user should do mail check for online IMAP and possibly cached IMAP as well
00106 
00107   mainLayout->setColStretch( 1, 1 );
00108   mainLayout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding ), row, 0 );
00109 
00110   // Make it a bit bigger, else the folder requester cuts off the text too early
00111   resize( 500, minimumSize().height() );
00112 }
00113 
00114 void ArchiveFolderDialog::setFolder( KMFolder *defaultFolder )
00115 {
00116   mFolderRequester->setFolder( defaultFolder );
00117   // TODO: what if the file already exists?
00118   mUrlRequester->setURL( standardArchivePath( defaultFolder->name() ) );
00119 }
00120 
00121 void ArchiveFolderDialog::slotOk()
00122 {
00123   if ( !Util::checkOverwrite( mUrlRequester->url(), this ) ) {
00124     return;
00125   }
00126 
00127   if ( !mFolderRequester->folder() ) {
00128     KMessageBox::information( this, i18n( "Please select the folder that should be archived." ),
00129                               i18n( "No folder selected" ) );
00130     return;
00131   }
00132 
00133   // TODO: check if url is empty. or better yet, disable ok button until file is chosen
00134   KMail::BackupJob *backupJob = new KMail::BackupJob( mParentWidget );
00135   backupJob->setRootFolder( mFolderRequester->folder() );
00136   backupJob->setSaveLocation( mUrlRequester->url() );
00137   backupJob->setArchiveType( static_cast<BackupJob::ArchiveType>( mFormatComboBox->currentItem() ) );
00138   backupJob->setDeleteFoldersAfterCompletion( mDeleteCheckBox->isChecked() );
00139   backupJob->start();
00140   accept();
00141 }
00142 
00143 void ArchiveFolderDialog::slotFixFileExtension()
00144 {
00145   // KDE4: use KMimeType::extractKnownExtension() here
00146   const int numExtensions = 4;
00147 
00148   // These extensions are sorted differently, .tar has to come last, or it will match before giving
00149   // the more specific ones time to match.
00150   const char *sortedExtensions[numExtensions] = { ".zip", ".tar.bz2", ".tar.gz", ".tar" };
00151 
00152   // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
00153   const char *extensions[numExtensions] = { ".zip", ".tar", ".tar.bz2", ".tar.gz" };
00154 
00155   QString fileName = mUrlRequester->url();
00156 
00157   // First, try to find the extension of the file name and remove it
00158   for( int i = 0; i < numExtensions; i++ ) {
00159     int index = fileName.lower().findRev( sortedExtensions[i] );
00160     if ( index != -1 ) {
00161       fileName = fileName.left( fileName.length() - QString( sortedExtensions[i] ).length() );
00162       break;
00163     }
00164   }
00165 
00166   // Now, we've got a filename without an extension, simply append the correct one
00167   fileName += extensions[mFormatComboBox->currentItem()];
00168   mUrlRequester->setURL( fileName );
00169 }
00170 
00171 #include "archivefolderdialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys