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>
36 #include <QPushButton>
37 #include <QSpacerItem>
38 #include <QVBoxLayout>
43 #include <KEditListWidget>
45 #include <KMessageBox>
46 #include <KSqueezedTextLabel>
48 using namespace MailCommon;
52 CollectionPropertiesPage( parent ), mLastItem(0), changed(false)
54 setObjectName( QLatin1String(
"KMail::CollectionMailingListPage" ) );
55 setPageTitle( i18nc(
"@title:tab Mailing list settings for a folder.",
"Mailing List" ) );
62 void CollectionMailingListPage::slotConfigChanged()
70 return ( !CommonKernel->isSystemFolderCollection( col ) &&
71 !fd->isStructural() &&
72 !MailCommon::Util::isVirtualCollection( col ) );
77 mCurrentCollection = col;
78 mFolder = FolderCollection::forCollection( col,
false );
80 QVBoxLayout *topLayout =
new QVBoxLayout(
this );
81 topLayout->setSpacing( KDialog::spacingHint() );
83 mHoldsMailingList =
new QCheckBox( i18n(
"Folder holds a mailing list"),
this );
84 connect( mHoldsMailingList, SIGNAL(toggled(
bool)),
85 SLOT(slotHoldsML(
bool)) );
86 connect( mHoldsMailingList, SIGNAL(toggled(
bool)), SLOT(slotConfigChanged()) );
87 topLayout->addWidget( mHoldsMailingList );
89 mGroupWidget =
new QWidget(
this );
90 QGridLayout *groupLayout =
new QGridLayout( mGroupWidget );
91 groupLayout->setSpacing( KDialog::spacingHint() );
93 mDetectButton =
new QPushButton( i18n(
"Detect Automatically"), mGroupWidget );
94 connect( mDetectButton, SIGNAL(pressed()),
95 SLOT(slotDetectMailingList()) );
96 groupLayout->addWidget( mDetectButton, 2, 1 );
98 groupLayout->addItem(
new QSpacerItem( 0, 10 ), 3, 0 );
100 QLabel *label =
new QLabel( i18n(
"Mailing list description:"), mGroupWidget );
101 groupLayout->addWidget( label, 4, 0 );
102 mMLId =
new KSqueezedTextLabel( QString(), mGroupWidget );
103 mMLId->setTextElideMode( Qt::ElideRight );
104 groupLayout->addWidget( mMLId, 4, 1, 1, 2 );
107 label =
new QLabel( i18n(
"Preferred handler:"), mGroupWidget );
108 groupLayout->addWidget( label, 5, 0 );
109 mMLHandlerCombo =
new KComboBox( mGroupWidget );
110 mMLHandlerCombo->addItem( i18n(
"KMail"), MailingList::KMail );
111 mMLHandlerCombo->addItem( i18n(
"Browser"), MailingList::Browser );
112 groupLayout->addWidget( mMLHandlerCombo, 5, 1, 1, 2 );
113 connect( mMLHandlerCombo, SIGNAL(activated(
int)),
114 SLOT(slotMLHandling(
int)) );
115 label->setBuddy( mMLHandlerCombo );
117 label =
new QLabel( i18n(
"Address type:"), mGroupWidget );
118 groupLayout->addWidget( label, 6, 0 );
119 mAddressCombo =
new KComboBox( mGroupWidget );
120 label->setBuddy( mAddressCombo );
121 groupLayout->addWidget( mAddressCombo, 6, 1 );
126 QPushButton *handleButton =
new QPushButton( i18n(
"Invoke Handler" ), mGroupWidget );
128 connect( handleButton, SIGNAL(clicked()),
129 SLOT(slotInvokeHandler()) );
131 handleButton->setEnabled(
false );
134 groupLayout->addWidget( handleButton, 6, 2 );
136 mEditList =
new KEditListWidget( mGroupWidget );
137 mEditList->lineEdit()->setClearButtonShown(
true);
138 connect(mEditList, SIGNAL(changed()),SLOT(slotConfigChanged()));
139 groupLayout->addWidget( mEditList, 7, 0, 1, 4 );
145 el << i18n(
"Post to List" )
146 << i18n(
"Subscribe to List" )
147 << i18n(
"Unsubscribe From List" )
148 << i18n(
"List Archives" )
149 << i18n(
"List Help" );
150 mAddressCombo->addItems( el );
151 connect( mAddressCombo, SIGNAL(activated(
int)),
152 SLOT(slotAddressChanged(
int)) );
154 topLayout->addWidget( mGroupWidget );
155 mGroupWidget->setEnabled(
false );
163 mMailingList = mFolder->mailingList();
165 mMLId->setText( (mMailingList.id().isEmpty() ? i18n(
"Not available") : mMailingList.id()) );
166 mMLHandlerCombo->setCurrentIndex( mMailingList.handler() );
167 mEditList->insertStringList( mMailingList.postUrls().toStringList() );
169 mAddressCombo->setCurrentIndex( mLastItem );
170 mHoldsMailingList->setChecked( mFolder && mFolder->isMailingListEnabled() );
171 slotHoldsML( mHoldsMailingList->isChecked() );
181 mFolder->setMailingListEnabled( mHoldsMailingList && mHoldsMailingList->isChecked() );
183 mFolder->setMailingList( mMailingList );
189 void CollectionMailingListPage::slotHoldsML(
bool holdsML )
191 mGroupWidget->setEnabled( holdsML );
192 mDetectButton->setEnabled( mFolder && mFolder->count()!=0 );
196 void CollectionMailingListPage::slotDetectMailingList()
201 kDebug()<<
"Detecting mailing list";
204 if ( !( mMailingList.features() & MailingList::Post ) ) {
206 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( mFolder->collection(), this );
207 job->fetchScope().fetchPayloadPart( Akonadi::MessagePart::Header );
210 mDetectButton->setEnabled(
false );
212 mMLId->setText( (mMailingList.id().isEmpty() ? i18n(
"Not available.") : mMailingList.id() ) );
220 mDetectButton->setEnabled(
true );
221 if ( MailCommon::Util::showJobErrorMessage(job) ) {
224 Akonadi::ItemFetchJob *fjob =
dynamic_cast<Akonadi::ItemFetchJob*
>( job );
226 Akonadi::Item::List items = fjob->items();
227 const int maxchecks = 5;
228 int num = items.size();
229 for (
int i = --num ; ( i > num - maxchecks ) && ( i >= 0 ); --i ) {
230 Akonadi::Item item = items[i];
231 if ( item.hasPayload<KMime::Message::Ptr>() ) {
232 KMime::Message::Ptr message = item.payload<KMime::Message::Ptr>();
233 mMailingList = MessageCore::MailingList::detect( message );
234 if ( mMailingList.features() & MailingList::Post )
238 if ( !(mMailingList.features() & MailingList::Post) ) {
239 if ( mMailingList.features() == MailingList::None ) {
240 KMessageBox::error(
this,
241 i18n(
"KMail was unable to detect any mailing list in this folder.") );
243 KMessageBox::error(
this,
244 i18n(
"KMail was unable to fully detect a mailing list in this folder. "
245 "Please fill in the addresses by hand.") );
248 mMLId->setText( (mMailingList.id().isEmpty() ? i18n(
"Not available.") : mMailingList.id() ) );
255 void CollectionMailingListPage::slotMLHandling(
int element )
257 mMailingList.setHandler( static_cast<MailingList::Handler>( element ) );
262 void CollectionMailingListPage::slotAddressChanged(
int i )
271 void CollectionMailingListPage::fillMLFromWidgets()
273 if ( !mHoldsMailingList->isChecked() )
277 bool listChanged =
false;
278 const QStringList oldList = mEditList->items();
280 QStringList::ConstIterator end = oldList.constEnd();
281 for ( QStringList::ConstIterator it = oldList.constBegin(); it != end; ++it ) {
282 if ( !(*it).startsWith(QLatin1String(
"http:")) && !(*it).startsWith(QLatin1String(
"https:")) &&
283 !(*it).startsWith(QLatin1String(
"mailto:")) && ( (*it).contains(QLatin1Char(
'@')) ) ) {
285 newList << QLatin1String(
"mailto:") + *it;
293 mEditList->insertStringList( newList );
297 switch ( mLastItem ) {
299 mMailingList.setPostUrls( mEditList->items() );
302 mMailingList.setSubscribeUrls( mEditList->items() );
305 mMailingList.setUnsubscribeUrls( mEditList->items() );
308 mMailingList.setArchiveUrls( mEditList->items() );
311 mMailingList.setHelpUrls( mEditList->items() );
314 kWarning()<<
"Wrong entry in the mailing list entry combo!";
318 void CollectionMailingListPage::fillEditBox()
321 switch ( mAddressCombo->currentIndex() ) {
323 mEditList->insertStringList( mMailingList.postUrls().toStringList() );
326 mEditList->insertStringList( mMailingList.subscribeUrls().toStringList() );
329 mEditList->insertStringList( mMailingList.unsubscribeUrls().toStringList() );
332 mEditList->insertStringList( mMailingList.archiveUrls().toStringList() );
335 mEditList->insertStringList( mMailingList.helpUrls().toStringList() );
338 kWarning()<<
"Wrong entry in the mailing list entry combo!";
342 void CollectionMailingListPage::slotInvokeHandler()
344 save( mCurrentCollection );
345 switch ( mAddressCombo->currentIndex() ) {
362 kWarning()<<
"Wrong entry in the mailing list entry combo!";
366 #include "collectionmailinglistpage.moc"
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)
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)
~CollectionMailingListPage()
void save(Akonadi::Collection &col)
void load(const Akonadi::Collection &col)