24 #include "kernel/mailkernel.h"
25 #include "util/mailutil.h"
28 #include <akonadi/itemfetchjob.h>
29 #include <akonadi/itemfetchscope.h>
30 #include <akonadi/kmime/messageparts.h>
32 #include <QGridLayout>
35 #include <QPushButton>
36 #include <QSpacerItem>
37 #include <QVBoxLayout>
42 #include <KEditListWidget>
43 #include <KLocalizedString>
44 #include <KMessageBox>
45 #include <KSqueezedTextLabel>
47 using namespace MailCommon;
51 CollectionPropertiesPage( parent ), mGroupWidget(0), mLastItem(0), changed(false)
53 setObjectName(
QLatin1String(
"KMail::CollectionMailingListPage" ) );
54 setPageTitle( i18nc(
"@title:tab Mailing list settings for a folder.",
"Mailing List" ) );
61 void CollectionMailingListPage::slotConfigChanged()
69 return ( !CommonKernel->isSystemFolderCollection( col ) &&
70 !fd->isStructural() &&
71 !MailCommon::Util::isVirtualCollection( col ) );
76 mCurrentCollection = col;
77 mFolder = FolderCollection::forCollection( col,
false );
80 topLayout->
setSpacing( KDialog::spacingHint() );
82 mHoldsMailingList =
new QCheckBox( i18n(
"Folder holds a mailing list"),
this );
83 connect( mHoldsMailingList, SIGNAL(toggled(
bool)),
84 SLOT(slotHoldsML(
bool)) );
85 connect( mHoldsMailingList, SIGNAL(toggled(
bool)), SLOT(slotConfigChanged()) );
86 topLayout->
addWidget( mHoldsMailingList );
88 mGroupWidget =
new QWidget(
this );
90 groupLayout->
setSpacing( KDialog::spacingHint() );
92 mDetectButton =
new QPushButton( i18n(
"Detect Automatically"), mGroupWidget );
93 connect( mDetectButton, SIGNAL(pressed()),
94 SLOT(slotDetectMailingList()) );
95 groupLayout->
addWidget( mDetectButton, 2, 1 );
99 QLabel *label =
new QLabel( i18n(
"Mailing list description:"), mGroupWidget );
101 mMLId =
new KSqueezedTextLabel(
QString(), mGroupWidget );
102 mMLId->setTextElideMode( Qt::ElideRight );
103 groupLayout->
addWidget( mMLId, 4, 1, 1, 2 );
106 label =
new QLabel( i18n(
"Preferred handler:"), mGroupWidget );
108 mMLHandlerCombo =
new KComboBox( mGroupWidget );
109 mMLHandlerCombo->addItem( i18n(
"KMail"), MailingList::KMail );
110 mMLHandlerCombo->addItem( i18n(
"Browser"), MailingList::Browser );
111 groupLayout->
addWidget( mMLHandlerCombo, 5, 1, 1, 2 );
112 connect( mMLHandlerCombo, SIGNAL(activated(
int)),
113 SLOT(slotMLHandling(
int)) );
116 label =
new QLabel( i18n(
"Address type:"), mGroupWidget );
118 mAddressCombo =
new KComboBox( mGroupWidget );
120 groupLayout->
addWidget( mAddressCombo, 6, 1 );
127 connect( handleButton, SIGNAL(clicked()),
128 SLOT(slotInvokeHandler()) );
133 groupLayout->
addWidget( handleButton, 6, 2 );
135 mEditList =
new KEditListWidget( mGroupWidget );
136 mEditList->lineEdit()->setClearButtonShown(
true);
137 connect(mEditList, SIGNAL(changed()),SLOT(slotConfigChanged()));
138 groupLayout->
addWidget( mEditList, 7, 0, 1, 4 );
144 el << i18n(
"Post to List" )
145 << i18n(
"Subscribe to List" )
146 << i18n(
"Unsubscribe From List" )
147 << i18n(
"List Archives" )
148 << i18n(
"List Help" );
149 mAddressCombo->addItems( el );
150 connect( mAddressCombo, SIGNAL(activated(
int)),
151 SLOT(slotAddressChanged(
int)) );
162 mMailingList = mFolder->mailingList();
164 mMLId->setText( (mMailingList.id().isEmpty() ? i18n(
"Not available") : mMailingList.id()) );
165 mMLHandlerCombo->setCurrentIndex( mMailingList.handler() );
166 mEditList->insertStringList( mMailingList.postUrls().toStringList() );
168 mAddressCombo->setCurrentIndex( mLastItem );
169 mHoldsMailingList->
setChecked( mFolder && mFolder->isMailingListEnabled() );
170 slotHoldsML( mHoldsMailingList->
isChecked() );
180 mFolder->setMailingListEnabled( mHoldsMailingList && mHoldsMailingList->
isChecked() );
182 mFolder->setMailingList( mMailingList );
188 void CollectionMailingListPage::slotHoldsML(
bool holdsML )
191 mDetectButton->
setEnabled( mFolder && mFolder->count()!=0 );
195 void CollectionMailingListPage::slotDetectMailingList()
200 kDebug()<<
"Detecting mailing list";
203 if ( !( mMailingList.features() & MailingList::Post ) ) {
205 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( mFolder->collection(), this );
206 job->fetchScope().fetchPayloadPart( Akonadi::MessagePart::Header );
211 mMLId->setText( (mMailingList.id().isEmpty() ? i18n(
"Not available.") : mMailingList.id() ) );
220 if ( MailCommon::Util::showJobErrorMessage(job) ) {
223 Akonadi::ItemFetchJob *fjob =
dynamic_cast<Akonadi::ItemFetchJob*
>( job );
225 Akonadi::Item::List items = fjob->items();
226 const int maxchecks = 5;
227 int num = items.size();
228 for (
int i = --num ; ( i > num - maxchecks ) && ( i >= 0 ); --i ) {
229 Akonadi::Item item = items[i];
230 if ( item.hasPayload<KMime::Message::Ptr>() ) {
231 KMime::Message::Ptr message = item.payload<KMime::Message::Ptr>();
232 mMailingList = MessageCore::MailingList::detect( message );
233 if ( mMailingList.features() & MailingList::Post )
237 if ( !(mMailingList.features() & MailingList::Post) ) {
238 if ( mMailingList.features() == MailingList::None ) {
239 KMessageBox::error(
this,
240 i18n(
"KMail was unable to detect any mailing list in this folder.") );
242 KMessageBox::error(
this,
243 i18n(
"KMail was unable to fully detect a mailing list in this folder. "
244 "Please fill in the addresses by hand.") );
247 mMLId->setText( (mMailingList.id().isEmpty() ? i18n(
"Not available.") : mMailingList.id() ) );
254 void CollectionMailingListPage::slotMLHandling(
int element )
256 mMailingList.setHandler( static_cast<MailingList::Handler>( element ) );
261 void CollectionMailingListPage::slotAddressChanged(
int i )
270 void CollectionMailingListPage::fillMLFromWidgets()
276 bool listChanged =
false;
292 mEditList->insertStringList( newList );
296 switch ( mLastItem ) {
298 mMailingList.setPostUrls( mEditList->items() );
301 mMailingList.setSubscribeUrls( mEditList->items() );
304 mMailingList.setUnsubscribeUrls( mEditList->items() );
307 mMailingList.setArchiveUrls( mEditList->items() );
310 mMailingList.setHelpUrls( mEditList->items() );
313 kWarning()<<
"Wrong entry in the mailing list entry combo!";
317 void CollectionMailingListPage::fillEditBox()
320 switch ( mAddressCombo->currentIndex() ) {
322 mEditList->insertStringList( mMailingList.postUrls().toStringList() );
325 mEditList->insertStringList( mMailingList.subscribeUrls().toStringList() );
328 mEditList->insertStringList( mMailingList.unsubscribeUrls().toStringList() );
331 mEditList->insertStringList( mMailingList.archiveUrls().toStringList() );
334 mEditList->insertStringList( mMailingList.helpUrls().toStringList() );
337 kWarning()<<
"Wrong entry in the mailing list entry combo!";
341 void CollectionMailingListPage::slotInvokeHandler()
343 save( mCurrentCollection );
344 switch ( mAddressCombo->currentIndex() ) {
361 kWarning()<<
"Wrong entry in the mailing list entry combo!";
bool mailingListSubscribe(const QSharedPointer< MailCommon::FolderCollection > &fd)
bool mailingListPost(const QSharedPointer< MailCommon::FolderCollection > &fd)
void init(const Akonadi::Collection &)
bool mailingListUnsubscribe(const QSharedPointer< MailCommon::FolderCollection > &fd)
CollectionMailingListPage(QWidget *parent=0)
void setSpacing(int spacing)
void setBuddy(QWidget *buddy)
bool canHandle(const Akonadi::Collection &col) const
bool mailingListArchives(const QSharedPointer< MailCommon::FolderCollection > &fd)
void slotFetchDone(KJob *job)
bool mailingListHelp(const QSharedPointer< MailCommon::FolderCollection > &fd)
void addItem(QLayoutItem *item, int row, int column, int rowSpan, int columnSpan, QFlags< Qt::AlignmentFlag > alignment)
~CollectionMailingListPage()
const_iterator constEnd() const
const_iterator constBegin() const
void save(Akonadi::Collection &col)
void load(const Akonadi::Collection &col)
void setSpacing(int spacing)