1 /* Copyright 2009 Klarälvdalens Datakonsult AB
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License as
5 published by the Free Software Foundation; either version 2 of
6 the License or (at your option) version 3 or any later version
7 accepted by the membership of KDE e.V. (or its successor approved
8 by the membership of KDE e.V.), which shall act as a proxy
9 defined in Section 14 of version 3 of the license.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "archivefolderdialog.h"
21 #include "backupjob.h"
23 #include "kmmainwidget.h"
24 #include "folderrequester.h"
25 #include "messageviewer/util.h"
27 #include <Akonadi/Collection>
30 #include <kcombobox.h>
31 #include <kurlrequester.h>
32 #include <kmessagebox.h>
35 #include <qcheckbox.h>
38 using namespace KMail
;
39 using namespace MailCommon
;
41 static QString
standardArchivePath( const QString
&folderName
)
43 QString currentPath
= KGlobalSettings::documentPath();
44 QDir
dir( currentPath
);
46 currentPath
= QDir::homePath();
47 return currentPath
+ QLatin1Char( '/' ) +
48 i18nc( "Start of the filename for a mail archive file" , "Archive" ) + QLatin1Char( '_' ) + folderName
+ QLatin1Char( '_' ) + QDate::currentDate().toString( Qt::ISODate
) + QLatin1String( ".tar.bz2" );
51 ArchiveFolderDialog::ArchiveFolderDialog( QWidget
*parent
)
52 : KDialog( parent
), mParentWidget( parent
)
54 setObjectName( "archive_folder_dialog" );
55 setCaption( i18n( "Archive Folder" ) );
56 setButtons( Ok
|Cancel
);
57 setDefaultButton( Ok
);
59 QWidget
*mainWidget
= new QWidget( this );
60 QGridLayout
*mainLayout
= new QGridLayout( mainWidget
);
61 mainLayout
->setSpacing( KDialog::spacingHint() );
62 mainLayout
->setMargin( KDialog::marginHint() );
63 setMainWidget( mainWidget
);
67 // TODO: better label for "Ok" button
68 // TODO: Explaination label
69 // TODO: Use QFormLayout in KDE4
71 QLabel
*folderLabel
= new QLabel( i18n( "&Folder:" ), mainWidget
);
72 mainLayout
->addWidget( folderLabel
, row
, 0 );
73 mFolderRequester
= new FolderRequester( mainWidget
);
74 mFolderRequester
->setMustBeReadWrite( false );
75 mFolderRequester
->setNotAllowToCreateNewFolder( true );
76 connect( mFolderRequester
, SIGNAL(folderChanged(Akonadi::Collection
)), SLOT(slotFolderChanged(Akonadi::Collection
)) );
77 folderLabel
->setBuddy( mFolderRequester
);
78 mainLayout
->addWidget( mFolderRequester
, row
, 1 );
81 QLabel
*formatLabel
= new QLabel( i18n( "F&ormat:" ), mainWidget
);
82 mainLayout
->addWidget( formatLabel
, row
, 0 );
83 mFormatComboBox
= new KComboBox( mainWidget
);
84 formatLabel
->setBuddy( mFormatComboBox
);
86 // These combobox values have to stay in sync with the ArchiveType enum from BackupJob!
87 mFormatComboBox
->addItem( i18n( "Compressed Zip Archive (.zip)" ) );
88 mFormatComboBox
->addItem( i18n( "Uncompressed Archive (.tar)" ) );
89 mFormatComboBox
->addItem( i18n( "BZ2-Compressed Tar Archive (.tar.bz2)" ) );
90 mFormatComboBox
->addItem( i18n( "GZ-Compressed Tar Archive (.tar.gz)" ) );
91 mFormatComboBox
->setCurrentIndex( 2 );
92 connect( mFormatComboBox
, SIGNAL(activated(int)),
93 this, SLOT(slotFixFileExtension()) );
94 mainLayout
->addWidget( mFormatComboBox
, row
, 1 );
97 QLabel
*fileNameLabel
= new QLabel( i18n( "&Archive File:" ), mainWidget
);
98 mainLayout
->addWidget( fileNameLabel
, row
, 0 );
99 mUrlRequester
= new KUrlRequester( mainWidget
);
100 mUrlRequester
->setMode( KFile::LocalOnly
| KFile::File
);
101 mUrlRequester
->setFilter( "*.tar *.zip *.tar.gz *.tar.bz2" );
102 fileNameLabel
->setBuddy( mUrlRequester
);
103 connect( mUrlRequester
, SIGNAL(urlSelected(KUrl
)),
104 this, SLOT(slotFixFileExtension()) );
105 mainLayout
->addWidget( mUrlRequester
, row
, 1 );
108 // TODO: Make this appear more dangerous!
109 mDeleteCheckBox
= new QCheckBox( i18n( "&Delete folders after completion" ), mainWidget
);
110 mainLayout
->addWidget( mDeleteCheckBox
, row
, 0, 1, 2, Qt::AlignLeft
);
113 // TODO: what's this, tooltips
115 // TODO: Warn that user should do mail check for online IMAP and possibly cached IMAP as well
117 mainLayout
->setColumnStretch( 1, 1 );
118 mainLayout
->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding
, QSizePolicy::Expanding
), row
, 0 );
120 // Make it a bit bigger, else the folder requester cuts off the text too early
121 resize( 500, minimumSize().height() );
124 bool canRemoveFolder( const Akonadi::Collection
& col
)
126 const QSharedPointer
<FolderCollection
> folder
= FolderCollection::forCollection( col
,false );
127 return folder
&& col
.isValid() && col
.rights() & Akonadi::Collection::CanDeleteCollection
&& !folder
->isStructural() && !folder
->isSystemFolder() && col
.resource() != QLatin1String( "akonadi_nepomuktag_resource" );
130 void ArchiveFolderDialog::slotFolderChanged( const Akonadi::Collection
&folder
)
132 mDeleteCheckBox
->setEnabled( canRemoveFolder( folder
) );
135 void ArchiveFolderDialog::setFolder( const Akonadi::Collection
&defaultCollection
)
137 mFolderRequester
->setFolder( defaultCollection
);
138 // TODO: what if the file already exists?
139 mUrlRequester
->setUrl( standardArchivePath( defaultCollection
.name() ) );
140 const QSharedPointer
<FolderCollection
> folder
= FolderCollection::forCollection( defaultCollection
, false );
141 mDeleteCheckBox
->setEnabled( canRemoveFolder( defaultCollection
) );
142 enableButtonOk( defaultCollection
.isValid() && folder
&& !folder
->isStructural() );
145 void ArchiveFolderDialog::slotButtonClicked( int button
)
147 if ( button
== KDialog::Cancel
) {
151 Q_ASSERT( button
== KDialog::Ok
);
153 if ( !MessageViewer::Util::checkOverwrite( mUrlRequester
->url(), this ) ) {
157 if ( !mFolderRequester
->folderCollection().isValid() ) {
158 KMessageBox::information( this, i18n( "Please select the folder that should be archived." ),
159 i18n( "No folder selected" ) );
163 KMail::BackupJob
*backupJob
= new KMail::BackupJob( mParentWidget
);
164 backupJob
->setRootFolder( mFolderRequester
->folderCollection() );
165 backupJob
->setSaveLocation( mUrlRequester
->url() );
166 backupJob
->setArchiveType( static_cast<BackupJob::ArchiveType
>( mFormatComboBox
->currentIndex() ) );
167 backupJob
->setDeleteFoldersAfterCompletion( mDeleteCheckBox
->isEnabled() && mDeleteCheckBox
->isChecked());
172 void ArchiveFolderDialog::slotFixFileExtension()
174 // KDE4: use KMimeType::extractKnownExtension() here
175 const int numExtensions
= 4;
177 // These extensions are sorted differently, .tar has to come last, or it will match before giving
178 // the more specific ones time to match.
179 const char *sortedExtensions
[numExtensions
] = { ".zip", ".tar.bz2", ".tar.gz", ".tar" };
181 // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
182 const char *extensions
[numExtensions
] = { ".zip", ".tar", ".tar.bz2", ".tar.gz" };
184 QString fileName
= mUrlRequester
->url().path();
185 if ( fileName
.isEmpty() )
186 fileName
= standardArchivePath( mFolderRequester
->folderCollection().isValid() ?
187 mFolderRequester
->folderCollection().name() : QString() );
189 // First, try to find the extension of the file name and remove it
190 for( int i
= 0; i
< numExtensions
; ++i
) {
191 const int index
= fileName
.toLower().lastIndexOf( sortedExtensions
[i
] );
193 fileName
= fileName
.left( fileName
.length() - QString( sortedExtensions
[i
] ).length() );
198 // Now, we've got a filename without an extension, simply append the correct one
199 fileName
+= extensions
[mFormatComboBox
->currentIndex()];
200 mUrlRequester
->setUrl( fileName
);
203 #include "archivefolderdialog.moc"