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 <tdelocale.h>
00029 #include <kcombobox.h>
00030 #include <kurlrequester.h>
00031 #include <tdemessagebox.h>
00032 #include <tdefiledialog.h>
00033 
00034 #include <tqlabel.h>
00035 #include <tqcheckbox.h>
00036 #include <tqlayout.h>
00037 
00038 using namespace KMail;
00039 
00040 static TQString standardArchivePath( const TQString &folderName )
00041 {
00042   TQString currentPath = TDEGlobalSettings::documentPath();
00043   TQDir dir( currentPath );
00044   if( !dir.exists() ) {
00045     currentPath = TQDir::homeDirPath() + '/';
00046   }
00047   return currentPath +
00048     i18n( "Start of the filename for a mail archive file" , "Archive" ) + "_" + folderName +
00049     "_" + TQDate::currentDate().toString( Qt::ISODate ) + ".tar.bz2";
00050 }
00051 
00052 ArchiveFolderDialog::ArchiveFolderDialog( TQWidget *parent )
00053   : KDialogBase( parent, "archive_folder_dialog", false, i18n( "Archive Folder" ),
00054                  KDialogBase::Ok | KDialogBase::Cancel,
00055                  KDialogBase::Ok, true ),
00056     mParentWidget( parent )
00057 {
00058   TQWidget *mainWidget = new TQWidget( this );
00059   TQGridLayout *mainLayout = new TQGridLayout( mainWidget );
00060   mainLayout->setSpacing( KDialog::spacingHint() );
00061   mainLayout->setMargin( KDialog::marginHint() );
00062   setMainWidget( mainWidget );
00063 
00064   int row = 0;
00065 
00066   // TODO: better label for "Ok" button
00067   // TODO: Explaination label
00068   // TODO: Use TQFormLayout in KDE4
00069 
00070   TQLabel *folderLabel = new TQLabel( i18n( "&Folder:" ), mainWidget );
00071   mainLayout->addWidget( folderLabel, row, 0 );
00072   mFolderRequester = new FolderRequester( mainWidget, kmkernel->getKMMainWidget()->folderTree() );
00073   mFolderRequester->setMustBeReadWrite( false );
00074   connect( mFolderRequester, TQT_SIGNAL(folderChanged(KMFolder *)),
00075            TQT_SLOT(slotFolderChanged(KMFolder *)) );
00076   folderLabel->setBuddy( mFolderRequester );
00077   mainLayout->addWidget( mFolderRequester, row, 1 );
00078   row++;
00079 
00080   TQLabel *formatLabel = new TQLabel( i18n( "F&ormat:" ), mainWidget );
00081   mainLayout->addWidget( formatLabel, row, 0 );
00082   mFormatComboBox = new KComboBox( mainWidget );
00083   formatLabel->setBuddy( mFormatComboBox );
00084 
00085   // These combobox values have to stay in sync with the ArchiveType enum from BackupJob!
00086   mFormatComboBox->insertItem( i18n( "Compressed Zip Archive (.zip)" ) );
00087   mFormatComboBox->insertItem( i18n( "Uncompressed Archive (.tar)" ) );
00088   mFormatComboBox->insertItem( i18n( "BZ2-Compressed Tar Archive (.tar.bz2)" ) );
00089   mFormatComboBox->insertItem( i18n( "GZ-Compressed Tar Archive (.tar.gz)" ) );
00090   mFormatComboBox->setCurrentItem( 2 );
00091   connect( mFormatComboBox, TQT_SIGNAL(activated(int)),
00092            this, TQT_SLOT(slotFixFileExtension()) );
00093   mainLayout->addWidget( mFormatComboBox, row, 1 );
00094   row++;
00095 
00096   TQLabel *fileNameLabel = new TQLabel( i18n( "&Archive File:" ), mainWidget );
00097   mainLayout->addWidget( fileNameLabel, row, 0 );
00098   mUrlRequester = new KURLRequester( mainWidget );
00099   mUrlRequester->setMode( KFile::LocalOnly );
00100   mUrlRequester->setFilter( "*.tar *.zip *.tar.gz *.tar.bz2" );
00101   mUrlRequester->fileDialog()->setKeepLocation( true );
00102   fileNameLabel->setBuddy( mUrlRequester );
00103   connect( mUrlRequester->lineEdit(), TQT_SIGNAL(textChanged(const TQString &)),
00104            TQT_SLOT(slotUrlChanged(const TQString &)) );
00105   connect( mUrlRequester, TQT_SIGNAL(urlSelected(const TQString&)),
00106            this, TQT_SLOT(slotFixFileExtension()) );
00107   mainLayout->addWidget( mUrlRequester, row, 1 );
00108   row++;
00109 
00110   // TODO: Make this appear more dangerous!
00111   mDeleteCheckBox = new TQCheckBox( i18n( "&Delete folders after completion" ), mainWidget );
00112   mainLayout->addMultiCellWidget( mDeleteCheckBox, row, row, 0, 1, TQt::AlignLeft );
00113   row++;
00114 
00115   // TODO: what's this, tooltips
00116 
00117   // TODO: Warn that user should do mail check for online IMAP and possibly cached IMAP as well
00118 
00119   mainLayout->setColStretch( 1, 1 );
00120   mainLayout->addItem( new TQSpacerItem( 1, 1, TQSizePolicy::Expanding, TQSizePolicy::Expanding ), row, 0 );
00121 
00122   // Make it a bit bigger, else the folder requester cuts off the text too early
00123   resize( 500, minimumSize().height() );
00124 }
00125 
00126 void ArchiveFolderDialog::slotUrlChanged( const TQString &text )
00127 {
00128   enableButton( Ok, !text.isEmpty() );
00129 }
00130 
00131 bool canRemoveFolder( KMFolder *folder )
00132 {
00133   return
00134     folder &&
00135     folder->canDeleteMessages() &&
00136     !folder->noContent() &&
00137     !folder->isSystemFolder();
00138 }
00139 
00140 void ArchiveFolderDialog::slotFolderChanged( KMFolder *folder )
00141 {
00142   mDeleteCheckBox->setEnabled( canRemoveFolder( folder ) );
00143   enableButton( Ok, folder && !folder->noContent());
00144 }
00145 
00146 void ArchiveFolderDialog::setFolder( KMFolder *defaultFolder )
00147 {
00148   mFolderRequester->setFolder( defaultFolder );
00149   // TODO: what if the file already exists?
00150   mUrlRequester->setURL( standardArchivePath( defaultFolder->name() ) );
00151   mDeleteCheckBox->setEnabled( canRemoveFolder( defaultFolder ) );
00152 }
00153 
00154 void ArchiveFolderDialog::slotOk()
00155 {
00156   if ( !Util::checkOverwrite( mUrlRequester->url(), this ) ) {
00157     return;
00158   }
00159 
00160   if ( !mFolderRequester->folder() ) {
00161     KMessageBox::information( this, i18n( "Please select the folder that should be archived." ),
00162                               i18n( "No folder selected" ) );
00163     return;
00164   }
00165 
00166   // TODO: check if url is empty. or better yet, disable ok button until file is chosen
00167   KMail::BackupJob *backupJob = new KMail::BackupJob( mParentWidget );
00168   backupJob->setRootFolder( mFolderRequester->folder() );
00169   backupJob->setSaveLocation( mUrlRequester->url() );
00170   backupJob->setArchiveType( static_cast<BackupJob::ArchiveType>( mFormatComboBox->currentItem() ) );
00171   backupJob->setDeleteFoldersAfterCompletion( mDeleteCheckBox->isEnabled() &&
00172                                               mDeleteCheckBox->isChecked() );
00173   backupJob->start();
00174   accept();
00175 }
00176 
00177 void ArchiveFolderDialog::slotFixFileExtension()
00178 {
00179   // KDE4: use KMimeType::extractKnownExtension() here
00180   const int numExtensions = 4;
00181 
00182   // These extensions are sorted differently, .tar has to come last, or it will match before giving
00183   // the more specific ones time to match.
00184   const char *sortedExtensions[numExtensions] = { ".zip", ".tar.bz2", ".tar.gz", ".tar" };
00185 
00186   // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
00187   const char *extensions[numExtensions] = { ".zip", ".tar", ".tar.bz2", ".tar.gz" };
00188 
00189   TQString fileName = mUrlRequester->url();
00190   if ( fileName.isEmpty() ) {
00191     fileName = standardArchivePath( mFolderRequester->folder() ?
00192                                     mFolderRequester->folder()->name() : "" );
00193   }
00194 
00195   // First, try to find the extension of the file name and remove it
00196   for( int i = 0; i < numExtensions; i++ ) {
00197     int index = fileName.lower().findRev( sortedExtensions[i] );
00198     if ( index != -1 ) {
00199       fileName = fileName.left( fileName.length() - TQString( sortedExtensions[i] ).length() );
00200       break;
00201     }
00202   }
00203 
00204   // Now, we've got a filename without an extension, simply append the correct one
00205   fileName += extensions[mFormatComboBox->currentItem()];
00206   mUrlRequester->setURL( fileName );
00207 }
00208 
00209 #include "archivefolderdialog.moc"