• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.14
  • kdepim
  • kmail
  • dialog
archivefolderdialog.cpp
Go to the documentation of this file.
1 /* Copyright 2009 Klarälvdalens Datakonsult AB
2 
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.
10 
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.
15 
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/>.
18 */
19 #include "archivefolderdialog.h"
20 
21 #include "mailcommon/job/backupjob.h"
22 #include "kmkernel.h"
23 #include "kmmainwidget.h"
24 #include "folderrequester.h"
25 #include "messageviewer/utils/util.h"
26 
27 #include <Akonadi/Collection>
28 
29 #include <klocale.h>
30 #include <kcombobox.h>
31 #include <kurlrequester.h>
32 #include <kmessagebox.h>
33 #include <KMimeType>
34 #include <KSeparator>
35 
36 #include <qlabel.h>
37 #include <qcheckbox.h>
38 #include <QGridLayout>
39 
40 using namespace KMail;
41 using namespace MailCommon;
42 
43 QString ArchiveFolderDialog::standardArchivePath( const QString &folderName )
44 {
45  QString currentPath = KGlobalSettings::documentPath();
46  QDir dir( currentPath );
47  if( !dir.exists() )
48  currentPath = QDir::homePath();
49  return currentPath + QLatin1Char( '/' ) +
50  i18nc( "Start of the filename for a mail archive file" , "Archive" ) + QLatin1Char( '_' ) + folderName + QLatin1Char( '_' ) + QDate::currentDate().toString( Qt::ISODate ) + QLatin1String( ".tar.bz2" );
51 }
52 
53 ArchiveFolderDialog::ArchiveFolderDialog( QWidget *parent )
54  : KDialog( parent ), mParentWidget( parent )
55 {
56  setObjectName( QLatin1String("archive_folder_dialog") );
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" ) );
61  setModal( true );
62  QWidget *mainWidget = new QWidget( this );
63  QGridLayout *mainLayout = new QGridLayout( mainWidget );
64  mainLayout->setSpacing( KDialog::spacingHint() );
65  mainLayout->setMargin( KDialog::marginHint() );
66  setMainWidget( mainWidget );
67 
68  int row = 0;
69 
70  // TODO: Explaination label
71 
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 );
80  row++;
81 
82  QLabel *formatLabel = new QLabel( i18n( "F&ormat:" ), mainWidget );
83  mainLayout->addWidget( formatLabel, row, 0 );
84  mFormatComboBox = new KComboBox( mainWidget );
85  formatLabel->setBuddy( mFormatComboBox );
86 
87  // These combobox values have to stay in sync with the ArchiveType enum from BackupJob!
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 );
96  row++;
97 
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 );
109  row++;
110 
111  // TODO: Make this appear more dangerous!
112  mDeleteCheckBox = new QCheckBox( i18n( "&Delete folder and subfolders after completion" ), mainWidget );
113  mainLayout->addWidget( mDeleteCheckBox, row, 0, 1, 2, Qt::AlignLeft );
114  row++;
115 
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 );
119  mRecursiveCheckBox->setChecked( true );
120  row++;
121 
122  // TODO: what's this, tooltips
123 
124  // TODO: Warn that user should do mail check for online IMAP and possibly cached IMAP as well
125 
126  mainLayout->addWidget( new KSeparator(), row, 0, 1, 2);
127  row++;
128  mainLayout->setColumnStretch( 1, 1 );
129  mainLayout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding ), row, 0 );
130 
131  // Make it a bit bigger, else the folder requester cuts off the text too early
132  resize( 500, minimumSize().height() );
133 }
134 
135 bool canRemoveFolder( const Akonadi::Collection& col )
136 {
137  const QSharedPointer<FolderCollection> folder = FolderCollection::forCollection( col,false );
138  return folder
139  && col.isValid()
140  && !col.isVirtual()
141  && ( col.rights() & Akonadi::Collection::CanDeleteCollection )
142  && !folder->isStructural()
143  && !folder->isSystemFolder();
144 }
145 
146 void ArchiveFolderDialog::slotRecursiveCheckboxClicked()
147 {
148  slotFolderChanged( mFolderRequester->collection() );
149 }
150 
151 void ArchiveFolderDialog::slotFolderChanged( const Akonadi::Collection &folder )
152 {
153  mDeleteCheckBox->setEnabled( allowToDeleteFolders( folder ) );
154 }
155 
156 bool ArchiveFolderDialog::allowToDeleteFolders( const Akonadi::Collection &folder) const
157 {
158  return canRemoveFolder( folder ) && mRecursiveCheckBox->isChecked();
159 }
160 
161 void ArchiveFolderDialog::setFolder( const Akonadi::Collection &defaultCollection )
162 {
163  mFolderRequester->setCollection( defaultCollection );
164  // TODO: what if the file already exists?
165  mUrlRequester->setUrl( standardArchivePath( defaultCollection.name() ) );
166  const QSharedPointer<FolderCollection> folder = FolderCollection::forCollection( defaultCollection, false );
167  mDeleteCheckBox->setEnabled( allowToDeleteFolders( defaultCollection ) );
168  enableButtonOk( defaultCollection.isValid() && folder && !folder->isStructural() );
169 }
170 
171 void ArchiveFolderDialog::slotButtonClicked( int button )
172 {
173  if ( button == KDialog::Cancel ) {
174  reject();
175  return;
176  }
177  Q_ASSERT( button == KDialog::Ok );
178 
179  if ( !MessageViewer::Util::checkOverwrite( mUrlRequester->url(), this ) ) {
180  return;
181  }
182 
183  if ( !mFolderRequester->hasCollection() ) {
184  KMessageBox::information( this, i18n( "Please select the folder that should be archived." ),
185  i18n( "No folder selected" ) );
186  return;
187  }
188 
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() );
195  backupJob->start();
196  accept();
197 }
198 
199 void ArchiveFolderDialog::slotFixFileExtension()
200 {
201  const int numExtensions = 4;
202  // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
203  const char *extensions[numExtensions] = { ".zip", ".tar", ".tar.bz2", ".tar.gz" };
204 
205  QString fileName = mUrlRequester->url().path();
206  if ( fileName.isEmpty() )
207  fileName = standardArchivePath( mFolderRequester->hasCollection() ?
208  mFolderRequester->collection().name() : QString() );
209 
210  const QString extension = KMimeType::extractKnownExtension(fileName);
211  if(!extension.isEmpty()) {
212  fileName = fileName.left( fileName.length() - extension.length() - 1 );
213  }
214 
215  // Now, we've got a filename without an extension, simply append the correct one
216  fileName += QLatin1String(extensions[mFormatComboBox->currentIndex()]);
217  mUrlRequester->setUrl( fileName );
218 }
219 
220 void ArchiveFolderDialog::slotUrlChanged(const QString &url)
221 {
222  enableButtonOk(!url.isEmpty());
223 }
224 
QSpacerItem
QWidget
kmmainwidget.h
QDate::toString
QString toString(Qt::DateFormat format) const
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
KMail::ArchiveFolderDialog::ArchiveFolderDialog
ArchiveFolderDialog(QWidget *parent=0)
Definition: archivefolderdialog.cpp:53
QGridLayout
KDialog
QDir::homePath
QString homePath()
QGridLayout::setSpacing
void setSpacing(int spacing)
QLabel::setBuddy
void setBuddy(QWidget *buddy)
QWidget::setEnabled
void setEnabled(bool)
QSharedPointer
Definition: collectionmailinglistpage.h:34
QCheckBox
QString::isEmpty
bool isEmpty() const
QString
KMail::ArchiveFolderDialog::setFolder
void setFolder(const Akonadi::Collection &defaultCollection)
Definition: archivefolderdialog.cpp:161
QLayout::setMargin
void setMargin(int margin)
canRemoveFolder
bool canRemoveFolder(const Akonadi::Collection &col)
Definition: archivefolderdialog.cpp:135
QLatin1Char
QDir
archivefolderdialog.h
QAbstractButton::setChecked
void setChecked(bool)
kmkernel.h
QLatin1String
QGridLayout::setColumnStretch
void setColumnStretch(int column, int stretch)
QGridLayout::addItem
void addItem(QLayoutItem *item, int row, int column, int rowSpan, int columnSpan, QFlags< Qt::AlignmentFlag > alignment)
QDate::currentDate
QDate currentDate()
KComboBox
QString::length
int length() const
QString::left
QString left(int n) const
QLabel
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:32 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal