21 #include "mailcommon/job/backupjob.h"
24 #include "folderrequester.h"
25 #include "messageviewer/utils/util.h"
27 #include <Akonadi/Collection>
30 #include <kcombobox.h>
31 #include <kurlrequester.h>
32 #include <kmessagebox.h>
37 #include <qcheckbox.h>
38 #include <QGridLayout>
40 using namespace KMail;
41 using namespace MailCommon;
43 QString ArchiveFolderDialog::standardArchivePath(
const QString &folderName )
45 QString currentPath = KGlobalSettings::documentPath();
46 QDir dir( currentPath );
54 :
KDialog( parent ), mParentWidget( parent )
57 setCaption( i18nc(
"@title:window for archiving a folder",
"Archive Folder" ) );
58 setButtons( Ok|Cancel );
59 setDefaultButton( Ok );
60 setButtonText( KDialog::Ok, i18nc(
"@action",
"Archive" ) );
64 mainLayout->
setSpacing( KDialog::spacingHint() );
65 mainLayout->
setMargin( KDialog::marginHint() );
66 setMainWidget( mainWidget );
72 QLabel *folderLabel =
new QLabel( i18n(
"&Folder:" ), mainWidget );
73 mainLayout->
addWidget( folderLabel, row, 0 );
74 mFolderRequester =
new FolderRequester( mainWidget );
75 mFolderRequester->setMustBeReadWrite(
false );
76 mFolderRequester->setNotAllowToCreateNewFolder(
true );
77 connect( mFolderRequester, SIGNAL(folderChanged(Akonadi::Collection)), SLOT(slotFolderChanged(Akonadi::Collection)) );
78 folderLabel->
setBuddy( mFolderRequester );
79 mainLayout->
addWidget( mFolderRequester, row, 1 );
82 QLabel *formatLabel =
new QLabel( i18n(
"F&ormat:" ), mainWidget );
83 mainLayout->
addWidget( formatLabel, row, 0 );
84 mFormatComboBox =
new KComboBox( mainWidget );
85 formatLabel->
setBuddy( mFormatComboBox );
88 mFormatComboBox->addItem( i18n(
"Compressed Zip Archive (.zip)" ) );
89 mFormatComboBox->addItem( i18n(
"Uncompressed Archive (.tar)" ) );
90 mFormatComboBox->addItem( i18n(
"BZ2-Compressed Tar Archive (.tar.bz2)" ) );
91 mFormatComboBox->addItem( i18n(
"GZ-Compressed Tar Archive (.tar.gz)" ) );
92 mFormatComboBox->setCurrentIndex( 2 );
93 connect( mFormatComboBox, SIGNAL(activated(
int)),
94 this, SLOT(slotFixFileExtension()) );
95 mainLayout->
addWidget( mFormatComboBox, row, 1 );
98 QLabel *fileNameLabel =
new QLabel( i18n(
"&Archive File:" ), mainWidget );
99 mainLayout->
addWidget( fileNameLabel, row, 0 );
100 mUrlRequester =
new KUrlRequester( mainWidget );
101 mUrlRequester->setMode( KFile::LocalOnly | KFile::File );
102 mUrlRequester->setFilter(
QLatin1String(
"*.tar *.zip *.tar.gz *.tar.bz2") );
103 fileNameLabel->
setBuddy( mUrlRequester );
104 connect( mUrlRequester, SIGNAL(urlSelected(KUrl)),
105 this, SLOT(slotFixFileExtension()) );
106 connect( mUrlRequester, SIGNAL(textChanged(
QString)),
107 this, SLOT(slotUrlChanged(
QString)) );
108 mainLayout->
addWidget( mUrlRequester, row, 1 );
112 mDeleteCheckBox =
new QCheckBox( i18n(
"&Delete folder and subfolders after completion" ), mainWidget );
113 mainLayout->
addWidget( mDeleteCheckBox, row, 0, 1, 2, Qt::AlignLeft );
116 mRecursiveCheckBox =
new QCheckBox( i18n(
"Archive all subfolders" ), mainWidget );
117 connect( mRecursiveCheckBox, SIGNAL(clicked()),
this, SLOT(slotRecursiveCheckboxClicked()) );
118 mainLayout->
addWidget( mRecursiveCheckBox, row, 0, 1, 2, Qt::AlignLeft );
126 mainLayout->
addWidget(
new KSeparator(), row, 0, 1, 2);
129 mainLayout->
addItem(
new QSpacerItem( 1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding ), row, 0 );
132 resize( 500, minimumSize().height() );
141 && ( col.rights() & Akonadi::Collection::CanDeleteCollection )
142 && !folder->isStructural()
143 && !folder->isSystemFolder();
146 void ArchiveFolderDialog::slotRecursiveCheckboxClicked()
148 slotFolderChanged( mFolderRequester->collection() );
151 void ArchiveFolderDialog::slotFolderChanged(
const Akonadi::Collection &folder )
153 mDeleteCheckBox->
setEnabled( allowToDeleteFolders( folder ) );
156 bool ArchiveFolderDialog::allowToDeleteFolders(
const Akonadi::Collection &folder)
const
163 mFolderRequester->setCollection( defaultCollection );
165 mUrlRequester->setUrl( standardArchivePath( defaultCollection.name() ) );
167 mDeleteCheckBox->
setEnabled( allowToDeleteFolders( defaultCollection ) );
168 enableButtonOk( defaultCollection.isValid() && folder && !folder->isStructural() );
171 void ArchiveFolderDialog::slotButtonClicked(
int button )
173 if ( button == KDialog::Cancel ) {
177 Q_ASSERT( button == KDialog::Ok );
179 if ( !MessageViewer::Util::checkOverwrite( mUrlRequester->url(), this ) ) {
183 if ( !mFolderRequester->hasCollection() ) {
184 KMessageBox::information(
this, i18n(
"Please select the folder that should be archived." ),
185 i18n(
"No folder selected" ) );
189 MailCommon::BackupJob *backupJob =
new MailCommon::BackupJob( mParentWidget );
190 backupJob->setRootFolder( mFolderRequester->collection() );
191 backupJob->setSaveLocation( mUrlRequester->url() );
192 backupJob->setArchiveType( static_cast<BackupJob::ArchiveType>( mFormatComboBox->currentIndex() ) );
193 backupJob->setDeleteFoldersAfterCompletion( mDeleteCheckBox->
isEnabled() && mDeleteCheckBox->
isChecked());
194 backupJob->setRecursive( mRecursiveCheckBox->
isChecked() );
199 void ArchiveFolderDialog::slotFixFileExtension()
201 const int numExtensions = 4;
203 const char *extensions[numExtensions] = {
".zip",
".tar",
".tar.bz2",
".tar.gz" };
205 QString fileName = mUrlRequester->url().path();
207 fileName = standardArchivePath( mFolderRequester->hasCollection() ?
208 mFolderRequester->collection().name() :
QString() );
210 const QString extension = KMimeType::extractKnownExtension(fileName);
216 fileName +=
QLatin1String(extensions[mFormatComboBox->currentIndex()]);
217 mUrlRequester->setUrl( fileName );
220 void ArchiveFolderDialog::slotUrlChanged(
const QString &url)
222 enableButtonOk(!url.
isEmpty());
QString toString(Qt::DateFormat format) const
ArchiveFolderDialog(QWidget *parent=0)
void setSpacing(int spacing)
void setBuddy(QWidget *buddy)
void setFolder(const Akonadi::Collection &defaultCollection)
void setMargin(int margin)
bool canRemoveFolder(const Akonadi::Collection &col)
void setColumnStretch(int column, int stretch)
void addItem(QLayoutItem *item, int row, int column, int rowSpan, int columnSpan, QFlags< Qt::AlignmentFlag > alignment)
QString left(int n) const