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

kmail

  • sources
  • kde-4.12
  • 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 static QString 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( i18n( "Archive Folder" ) );
58  setButtons( Ok|Cancel );
59  setDefaultButton( Ok );
60  setButtonText(KDialog::Ok,i18n("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  && ( col.resource() != QLatin1String( "akonadi_nepomuktag_resource" ) );
145 }
146 
147 void ArchiveFolderDialog::slotRecursiveCheckboxClicked()
148 {
149  slotFolderChanged( mFolderRequester->collection() );
150 }
151 
152 void ArchiveFolderDialog::slotFolderChanged( const Akonadi::Collection &folder )
153 {
154  mDeleteCheckBox->setEnabled( allowToDeleteFolders( folder ) );
155 }
156 
157 bool ArchiveFolderDialog::allowToDeleteFolders( const Akonadi::Collection &folder) const
158 {
159  return canRemoveFolder( folder ) && mRecursiveCheckBox->isChecked();
160 }
161 
162 void ArchiveFolderDialog::setFolder( const Akonadi::Collection &defaultCollection )
163 {
164  mFolderRequester->setCollection( defaultCollection );
165  // TODO: what if the file already exists?
166  mUrlRequester->setUrl( standardArchivePath( defaultCollection.name() ) );
167  const QSharedPointer<FolderCollection> folder = FolderCollection::forCollection( defaultCollection, false );
168  mDeleteCheckBox->setEnabled( allowToDeleteFolders( defaultCollection ) );
169  enableButtonOk( defaultCollection.isValid() && folder && !folder->isStructural() );
170 }
171 
172 void ArchiveFolderDialog::slotButtonClicked( int button )
173 {
174  if ( button == KDialog::Cancel ) {
175  reject();
176  return;
177  }
178  Q_ASSERT( button == KDialog::Ok );
179 
180  if ( !MessageViewer::Util::checkOverwrite( mUrlRequester->url(), this ) ) {
181  return;
182  }
183 
184  if ( !mFolderRequester->hasCollection() ) {
185  KMessageBox::information( this, i18n( "Please select the folder that should be archived." ),
186  i18n( "No folder selected" ) );
187  return;
188  }
189 
190  MailCommon::BackupJob *backupJob = new MailCommon::BackupJob( mParentWidget );
191  backupJob->setRootFolder( mFolderRequester->collection() );
192  backupJob->setSaveLocation( mUrlRequester->url() );
193  backupJob->setArchiveType( static_cast<BackupJob::ArchiveType>( mFormatComboBox->currentIndex() ) );
194  backupJob->setDeleteFoldersAfterCompletion( mDeleteCheckBox->isEnabled() && mDeleteCheckBox->isChecked());
195  backupJob->setRecursive( mRecursiveCheckBox->isChecked() );
196  backupJob->start();
197  accept();
198 }
199 
200 void ArchiveFolderDialog::slotFixFileExtension()
201 {
202  const int numExtensions = 4;
203  // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
204  const char *extensions[numExtensions] = { ".zip", ".tar", ".tar.bz2", ".tar.gz" };
205 
206  QString fileName = mUrlRequester->url().path();
207  if ( fileName.isEmpty() )
208  fileName = standardArchivePath( mFolderRequester->hasCollection() ?
209  mFolderRequester->collection().name() : QString() );
210 
211  const QString extension = KMimeType::extractKnownExtension(fileName);
212  if(!extension.isEmpty()) {
213  fileName = fileName.left( fileName.length() - extension.length() - 1 );
214  }
215 
216  // Now, we've got a filename without an extension, simply append the correct one
217  fileName += QLatin1String(extensions[mFormatComboBox->currentIndex()]);
218  mUrlRequester->setUrl( fileName );
219 }
220 
221 void ArchiveFolderDialog::slotUrlChanged(const QString &url)
222 {
223  enableButtonOk(!url.isEmpty());
224 }
225 
226 #include "archivefolderdialog.moc"
QSharedPointer
Definition: collectionmailinglistpage.h:34
kmmainwidget.h
KMail::ArchiveFolderDialog::ArchiveFolderDialog
ArchiveFolderDialog(QWidget *parent=0)
Definition: archivefolderdialog.cpp:53
QWidget
standardArchivePath
static QString standardArchivePath(const QString &folderName)
Definition: archivefolderdialog.cpp:43
KDialog
KMail::ArchiveFolderDialog::setFolder
void setFolder(const Akonadi::Collection &defaultCollection)
Definition: archivefolderdialog.cpp:162
canRemoveFolder
bool canRemoveFolder(const Akonadi::Collection &col)
Definition: archivefolderdialog.cpp:135
archivefolderdialog.h
kmkernel.h
QLabel
KComboBox
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:51 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

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