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