backport 1145339: no error box spam
[kdepim.git] / kmail / util.cpp
blobc14c3834e2fdf8c04ce52f77d777e0f268bc3d9b
1 /*******************************************************************************
2 **
3 ** Filename : util
4 ** Created on : 03 April, 2005
5 ** Copyright : (c) 2005 Till Adam
6 ** Email : <adam@kde.org>
7 **
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
35 ** your version.
37 *******************************************************************************/
40 #include "util.h"
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>
54 #include <kascii.h>
55 #include <KCharsets>
58 #include <stdlib.h>
60 #ifdef Q_WS_MACX
61 #endif
62 #include "foldercollection.h"
64 uint KMail::Util::folderIdentity(const Akonadi::Item& item)
66 uint id = 0;
67 if( item.isValid() && item.parentCollection().isValid() ) {
68 QSharedPointer<FolderCollection> fd = FolderCollection::forCollection( item.parentCollection() );
69 id = fd->identity();
71 return id;
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();
86 return addresses;
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();
99 return -1;
104 KUrl KMail::Util::findSieveUrlForAccount( OrgKdeAkonadiImapSettingsInterface *a, const QString& ident) {
105 assert( a );
106 if ( !a->sieveSupport() )
107 return KUrl();
108 if ( a->sieveReuseConfig() ) {
109 // assemble Sieve url from the settings of the account:
110 KUrl u;
111 u.setProtocol( "sieve" );
112 QString server;
113 QDBusReply<QString> reply = a->imapServer();
114 if ( reply.isValid() ) {
115 server = reply;
116 server = server.section( ':', 0, 0 );
117 } else {
118 return KUrl();
120 u.setHost( server );
121 u.setUser( a->userName() );
123 QDBusInterface resourceSettings( QString("org.freedesktop.Akonadi.Resource.")+ident,"/Settings", "org.kde.Akonadi.Imap.Wallet" );
125 QString pwd;
126 QDBusReply<QString> replyPass = resourceSettings.call( "password" );
127 if ( replyPass.isValid() ) {
128 pwd = replyPass;
130 u.setPass( pwd );
131 u.setPort( a->sievePort() );
132 QString authStr;
133 switch( a->authentication() ) {
134 case KIMAP::LoginJob::ClearText:
135 authStr = "PLAIN";
136 break;
137 case KIMAP::LoginJob::Login:
138 authStr = "LOGIN";
139 break;
140 case KIMAP::LoginJob::Plain:
141 authStr = "PLAIN";
142 break;
143 case KIMAP::LoginJob::CramMD5:
144 authStr = "CRAM-MD5";
145 break;
146 case KIMAP::LoginJob::DigestMD5:
147 authStr = "DIGEST-MD5";
148 break;
149 case KIMAP::LoginJob::GSSAPI:
150 authStr = "GSSAPI";
151 break;
152 case KIMAP::LoginJob::Anonymous:
153 authStr = "ANONYMOUS";
154 break;
155 default:
156 authStr = "PLAIN";
157 break;
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() );
163 return u;
164 } else {
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() );
169 return u;
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 )
181 QStringList lst;
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();
225 win->show();
226 } else {
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 )
239 QString fullPath;
240 QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection( KMKernel::self()->collectionModel(), collection );
241 if ( !idx.isValid() )
242 return fullPath;
243 fullPath = idx.data().toString();
244 idx = idx.parent();
245 while ( idx != QModelIndex() ) {
246 fullPath = idx.data().toString() + '/' + fullPath;
247 idx = idx.parent();
249 return fullPath;