1 /*******************************************************************************
4 ** Created on : 03 April, 2005
5 ** Copyright : (c) 2005 Till Adam
6 ** Email : <adam@kde.org>
8 *******************************************************************************/
10 /*******************************************************************************
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
17 ** It is distributed in the hope that it will be useful, but
18 ** WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 ** In addition, as a special exception, the copyright holders give
27 ** permission to link the code of this program with any edition of
28 ** the Qt library by Trolltech AS, Norway (or with modified versions
29 ** of Qt that use the same license as Qt), and distribute linked
30 ** combinations including the two. You must obey the GNU General
31 ** Public License in all respects for all of the code used other than
32 ** Qt. If you modify this file, you may extend this exception to
33 ** your version of the file, but you are not obligated to do so. If
34 ** you do not wish to do so, delete this exception statement from
37 *******************************************************************************/
41 #include "imapsettings.h"
43 #include "messagecore/stringutil.h"
44 #include "messagecomposer/messagehelper.h"
46 #include <kmime/kmime_message.h>
47 #include <kpimutils/email.h>
48 #include <kimap/loginjob.h>
49 #include <Akonadi/AgentManager>
50 #include <Akonadi/EntityTreeModel>
51 #include <akonadi/entitymimetypefiltermodel.h>
53 #include <KStandardDirs>
62 #include "foldercollection.h"
64 uint
KMail::Util::folderIdentity(const Akonadi::Item
& item
)
67 if( item
.isValid() && item
.parentCollection().isValid() ) {
68 QSharedPointer
<FolderCollection
> fd
= FolderCollection::forCollection( item
.parentCollection() );
74 QStringList
KMail::Util::mailingListsFromMessage( const Akonadi::Item
& item
)
76 QStringList addresses
;
77 // determine the mailing list posting address
78 Akonadi::Collection parentCollection
= item
.parentCollection();
79 QSharedPointer
<FolderCollection
> fd
;
80 if ( parentCollection
.isValid() ) {
81 fd
= FolderCollection::forCollection( parentCollection
);
82 if ( fd
->isMailingListEnabled() && !fd
->mailingListPostAddress().isEmpty() ) {
83 addresses
<< fd
->mailingListPostAddress();
89 Akonadi::Item::Id
KMail::Util::putRepliesInSameFolder( const Akonadi::Item
& item
)
91 Akonadi::Collection parentCollection
= item
.parentCollection();
92 QSharedPointer
<FolderCollection
> fd
;
93 if ( parentCollection
.isValid() ) {
94 fd
= FolderCollection::forCollection( parentCollection
);
95 if( fd
->putRepliesInSameFolder() ) {
96 return parentCollection
.id();
104 KUrl
KMail::Util::findSieveUrlForAccount( OrgKdeAkonadiImapSettingsInterface
*a
, const QString
& ident
) {
106 if ( !a
->sieveSupport() )
108 if ( a
->sieveReuseConfig() ) {
109 // assemble Sieve url from the settings of the account:
111 u
.setProtocol( "sieve" );
113 QDBusReply
<QString
> reply
= a
->imapServer();
114 if ( reply
.isValid() ) {
116 server
= server
.section( ':', 0, 0 );
121 u
.setUser( a
->userName() );
123 QDBusInterface
resourceSettings( QString("org.freedesktop.Akonadi.Resource.")+ident
,"/Settings", "org.kde.Akonadi.Imap.Wallet" );
126 QDBusReply
<QString
> replyPass
= resourceSettings
.call( "password" );
127 if ( replyPass
.isValid() ) {
131 u
.setPort( a
->sievePort() );
133 switch( a
->authentication() ) {
134 case KIMAP::LoginJob::ClearText
:
137 case KIMAP::LoginJob::Login
:
140 case KIMAP::LoginJob::Plain
:
143 case KIMAP::LoginJob::CramMD5
:
144 authStr
= "CRAM-MD5";
146 case KIMAP::LoginJob::DigestMD5
:
147 authStr
= "DIGEST-MD5";
149 case KIMAP::LoginJob::GSSAPI
:
152 case KIMAP::LoginJob::Anonymous
:
153 authStr
= "ANONYMOUS";
159 u
.addQueryItem( "x-mech", authStr
);
160 if ( a
->safety() == ( int )( KIMAP::LoginJob::Unencrypted
))
161 u
.addQueryItem( "x-allow-unencrypted", "true" );
162 u
.setFileName( a
->sieveVacationFilename() );
165 KUrl
u( a
->sieveAlternateUrl() );
166 if ( u
.protocol().toLower() == "sieve" && ( a
->safety() == ( int )( KIMAP::LoginJob::Unencrypted
) ) && u
.queryItem("x-allow-unencrypted").isEmpty() )
167 u
.addQueryItem( "x-allow-unencrypted", "true" );
168 u
.setFileName( a
->sieveVacationFilename() );
173 OrgKdeAkonadiImapSettingsInterface
*KMail::Util::createImapSettingsInterface( const QString
&ident
)
175 return new OrgKdeAkonadiImapSettingsInterface("org.freedesktop.Akonadi.Resource." + ident
, "/Settings", QDBusConnection::sessionBus() );
179 void KMail::Util::launchAccountWizard( QWidget
*w
)
182 lst
.append( "--type" );
183 lst
.append( "message/rfc822" );
185 const QString path
= KStandardDirs::findExe( QLatin1String("accountwizard" ) );
186 if( !QProcess::startDetached( path
, lst
) )
187 KMessageBox::error( w
, i18n( "Could not start the account wizard. "
188 "Please check your installation." ),
189 i18n( "Unable to start account wizard" ) );
193 Akonadi::AgentInstance::List
KMail::Util::agentInstances()
195 Akonadi::AgentInstance::List relevantInstances
;
196 foreach ( const Akonadi::AgentInstance
&instance
, Akonadi::AgentManager::self()->instances() ) {
197 if ( instance
.type().mimeTypes().contains( KMime::Message::mimeType() ) &&
198 instance
.type().capabilities().contains( "Resource" ) ) {
199 relevantInstances
<< instance
;
202 return relevantInstances
;
205 void KMail::Util::handleClickedURL( const KUrl
&url
, uint identity
)
207 if ( url
.protocol() == "mailto" )
209 KMime::Message::Ptr
msg ( new KMime::Message
);
210 MessageHelper::initHeader( msg
, KMKernel::self()->identityManager(), identity
);
211 msg
->contentType()->setCharset("utf-8");
213 QMap
<QString
, QString
> fields
= MessageCore::StringUtil::parseMailtoUrl( url
);
215 msg
->to()->fromUnicodeString( fields
.value( "to" ),"utf-8" );
216 if ( !fields
.value( "subject" ).isEmpty() )
217 msg
->subject()->fromUnicodeString( fields
.value( "subject" ),"utf-8" );
218 if ( !fields
.value( "body" ).isEmpty() )
219 msg
->setBody( fields
.value( "body" ).toUtf8() );
220 if ( !fields
.value( "cc" ).isEmpty() )
221 msg
->cc()->fromUnicodeString( fields
.value( "cc" ),"utf-8" );
223 KMail::Composer
* win
= KMail::makeComposer( msg
, KMail::Composer::New
, identity
);
224 win
->setFocusToSubject();
227 kWarning() << "Can't handle URL:" << url
;
231 bool KMail::Util::isVirtualCollection(const Akonadi::Collection
& collection
)
233 return ( collection
.resource() == QLatin1String( "akonadi_nepomuktag_resource" ) || collection
.resource() == QLatin1String( "akonadi_search_resource" ) );
237 QString
KMail::Util::fullCollectionPath( const Akonadi::Collection
& collection
)
240 QModelIndex idx
= Akonadi::EntityTreeModel::modelIndexForCollection( KMKernel::self()->collectionModel(), collection
);
241 if ( !idx
.isValid() )
243 fullPath
= idx
.data().toString();
245 while ( idx
!= QModelIndex() ) {
246 fullPath
= idx
.data().toString() + '/' + fullPath
;