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

mailcommon

  • sources
  • kde-4.14
  • kdepim
  • mailcommon
  • kernel
mailkernel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
3  Copyright (c) 2010 Andras Mantia <andras@kdab.com>
4 
5  This program is free software; you can redistribute it and/or modify it
6  under the terms of the GNU General Public License, version 2, as
7  published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "mailkernel.h"
20 #include "util/mailutil.h"
21 #include "imapresourcesettings.h"
22 #include "pop3settings.h"
23 
24 #include "pimcommon/util/pimutil.h"
25 #include "pimcommon/util/imapresourcecapabilitiesmanager.h"
26 
27 #include <Akonadi/AgentInstance>
28 #include <Akonadi/AgentManager>
29 #include <Akonadi/EntityMimeTypeFilterModel>
30 #include <Akonadi/KMime/SpecialMailCollections>
31 #include <Akonadi/KMime/SpecialMailCollectionsRequestJob>
32 #include <Akonadi/KMime/SpecialMailCollectionsDiscoveryJob>
33 
34 #include <KPIMIdentities/Identity>
35 #include <KPIMIdentities/IdentityManager>
36 
37 #include <KDebug>
38 #include <KLocale>
39 #include <KMessageBox>
40 
41 namespace MailCommon {
42 
43 class KernelPrivate
44 {
45 public:
46  KernelPrivate() : kernel( new Kernel )
47  {
48  }
49 
50  ~KernelPrivate()
51  {
52  kDebug();
53  delete kernel;
54  }
55  Kernel *kernel;
56 };
57 
58 K_GLOBAL_STATIC( KernelPrivate, sInstance )
59 
60 Kernel::Kernel( QObject *parent ) : QObject( parent )
61 {
62  mKernelIf = 0;
63  mSettingsIf = 0;
64  mFilterIf = 0;
65  mImapResourceManager = new PimCommon::ImapResourceCapabilitiesManager(this);
66 }
67 
68 Kernel::~Kernel()
69 {
70  kDebug();
71 }
72 
73 Kernel *Kernel::self()
74 {
75  return sInstance->kernel; //will create it
76 }
77 
78 PimCommon::ImapResourceCapabilitiesManager *Kernel::imapResourceManager() const
79 {
80  return mImapResourceManager;
81 }
82 
83 Akonadi::Collection Kernel::collectionFromId( const Akonadi::Collection::Id &id ) const
84 {
85  const QModelIndex idx =
86  Akonadi::EntityTreeModel::modelIndexForCollection(
87  kernelIf()->collectionModel(), Akonadi::Collection( id ) );
88 
89  return idx.data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
90 }
91 
92 Akonadi::Collection Kernel::trashCollectionFolder()
93 {
94  return
95  Akonadi::SpecialMailCollections::self()->defaultCollection(
96  Akonadi::SpecialMailCollections::Trash );
97 }
98 
99 Akonadi::Collection Kernel::inboxCollectionFolder()
100 {
101  return
102  Akonadi::SpecialMailCollections::self()->defaultCollection(
103  Akonadi::SpecialMailCollections::Inbox );
104 }
105 
106 Akonadi::Collection Kernel::outboxCollectionFolder()
107 {
108  return
109  Akonadi::SpecialMailCollections::self()->defaultCollection(
110  Akonadi::SpecialMailCollections::Outbox );
111 }
112 
113 Akonadi::Collection Kernel::sentCollectionFolder()
114 {
115  return
116  Akonadi::SpecialMailCollections::self()->defaultCollection(
117  Akonadi::SpecialMailCollections::SentMail );
118 }
119 
120 Akonadi::Collection Kernel::draftsCollectionFolder()
121 {
122  return
123  Akonadi::SpecialMailCollections::self()->defaultCollection(
124  Akonadi::SpecialMailCollections::Drafts );
125 }
126 
127 Akonadi::Collection Kernel::templatesCollectionFolder()
128 {
129  return
130  Akonadi::SpecialMailCollections::self()->defaultCollection(
131  Akonadi::SpecialMailCollections::Templates );
132 }
133 
134 bool Kernel::isSystemFolderCollection( const Akonadi::Collection &col )
135 {
136  return ( col == inboxCollectionFolder() ||
137  col == outboxCollectionFolder() ||
138  col == sentCollectionFolder() ||
139  col == trashCollectionFolder() ||
140  col == draftsCollectionFolder() ||
141  col == templatesCollectionFolder() );
142 }
143 
144 bool Kernel::isMainFolderCollection( const Akonadi::Collection &col )
145 {
146  return col == inboxCollectionFolder();
147 }
148 
149 //-----------------------------------------------------------------------------
150 void Kernel::initFolders()
151 {
152  kDebug() << "Initialized and looking for specialcollection folders.";
153  findCreateDefaultCollection( Akonadi::SpecialMailCollections::Inbox );
154  findCreateDefaultCollection( Akonadi::SpecialMailCollections::Outbox );
155  findCreateDefaultCollection( Akonadi::SpecialMailCollections::SentMail );
156  findCreateDefaultCollection( Akonadi::SpecialMailCollections::Drafts );
157  findCreateDefaultCollection( Akonadi::SpecialMailCollections::Trash );
158  findCreateDefaultCollection( Akonadi::SpecialMailCollections::Templates );
159 
160  Akonadi::SpecialMailCollectionsDiscoveryJob *job =
161  new Akonadi::SpecialMailCollectionsDiscoveryJob( this );
162  job->start();
163  // we don't connect to the job directly: it will register stuff into SpecialMailCollections, which will emit signals.
164 }
165 
166 void Kernel::findCreateDefaultCollection( Akonadi::SpecialMailCollections::Type type )
167 {
168  if ( Akonadi::SpecialMailCollections::self()->hasDefaultCollection( type ) ) {
169  const Akonadi::Collection col =
170  Akonadi::SpecialMailCollections::self()->defaultCollection( type );
171 
172  if ( !( col.rights() & Akonadi::Collection::AllRights ) ) {
173  emergencyExit( i18n( "You do not have read/write permission to your inbox folder." ) );
174  }
175  } else {
176  Akonadi::SpecialMailCollectionsRequestJob *job =
177  new Akonadi::SpecialMailCollectionsRequestJob( this );
178 
179  connect( job, SIGNAL(result(KJob*)),
180  this, SLOT(createDefaultCollectionDone(KJob*)) );
181 
182  job->requestDefaultCollection( type );
183  }
184 }
185 
186 void Kernel::createDefaultCollectionDone( KJob *job )
187 {
188  if ( job->error() ) {
189  emergencyExit( job->errorText() );
190  return;
191  }
192 
193  Akonadi::SpecialMailCollectionsRequestJob *requestJob =
194  qobject_cast<Akonadi::SpecialMailCollectionsRequestJob*>( job );
195 
196  const Akonadi::Collection col = requestJob->collection();
197  if ( !( col.rights() & Akonadi::Collection::AllRights ) ) {
198  emergencyExit( i18n( "You do not have read/write permission to your inbox folder." ) );
199  }
200  Akonadi::SpecialMailCollections::self()->verifyI18nDefaultCollection( Akonadi::SpecialMailCollections::Inbox );
201  Akonadi::SpecialMailCollections::self()->verifyI18nDefaultCollection( Akonadi::SpecialMailCollections::Outbox );
202  Akonadi::SpecialMailCollections::self()->verifyI18nDefaultCollection( Akonadi::SpecialMailCollections::SentMail );
203  Akonadi::SpecialMailCollections::self()->verifyI18nDefaultCollection( Akonadi::SpecialMailCollections::Drafts );
204  Akonadi::SpecialMailCollections::self()->verifyI18nDefaultCollection( Akonadi::SpecialMailCollections::Trash );
205  Akonadi::SpecialMailCollections::self()->verifyI18nDefaultCollection( Akonadi::SpecialMailCollections::Templates );
206 
207  connect( Akonadi::SpecialMailCollections::self(), SIGNAL(defaultCollectionsChanged()),
208  this, SLOT(slotDefaultCollectionsChanged()), Qt::UniqueConnection );
209 }
210 
211 void Kernel::slotDefaultCollectionsChanged()
212 {
213  disconnect( Akonadi::SpecialMailCollections::self(), SIGNAL(defaultCollectionsChanged()),
214  this, SLOT(slotDefaultCollectionsChanged()));
215  initFolders();
216 }
217 
218 void Kernel::emergencyExit( const QString &reason )
219 {
220  QString mesg;
221  if ( reason.isEmpty() ) {
222  mesg = i18n( "The Email program encountered a fatal error and will terminate now" );
223  } else {
224  mesg = i18n( "The Email program encountered a fatal error and will terminate now.\n"
225  "The error was:\n%1", reason );
226  }
227 
228  kWarning() << mesg;
229 
230  // Show error box for the first error that caused emergencyExit.
231  static bool s_showingErrorBox = false;
232  if ( !s_showingErrorBox ) {
233  s_showingErrorBox = true;
234  if ( qApp ) { //see bug 313104
235  KMessageBox::error( 0, mesg );
236  }
237  ::exit(1);
238  }
239 }
240 
241 bool Kernel::folderIsDraftOrOutbox( const Akonadi::Collection &col )
242 {
243  if ( col == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Outbox ) ) {
244  return true;
245  }
246 
247  return folderIsDrafts( col );
248 }
249 
250 bool Kernel::folderIsDrafts( const Akonadi::Collection &col )
251 {
252  if ( col == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Drafts ) ) {
253  return true;
254  }
255 
256  const QString idString = QString::number( col.id() );
257  if ( idString.isEmpty() ) {
258  return false;
259  }
260 
261  // search the identities if the folder matches the drafts-folder
262  const KPIMIdentities::IdentityManager * im = KernelIf->identityManager();
263  KPIMIdentities::IdentityManager::ConstIterator end( im->end() );
264  for ( KPIMIdentities::IdentityManager::ConstIterator it = im->begin(); it != end; ++it ) {
265  if ( (*it).drafts() == idString ) {
266  return true;
267  }
268  }
269  return false;
270 }
271 
272 bool Kernel::folderIsTemplates( const Akonadi::Collection &col )
273 {
274  if ( col == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Templates ) ) {
275  return true;
276  }
277 
278  const QString idString = QString::number( col.id() );
279  if ( idString.isEmpty() ) {
280  return false;
281  }
282 
283  // search the identities if the folder matches the templates-folder
284  const KPIMIdentities::IdentityManager * im = KernelIf->identityManager();
285  KPIMIdentities::IdentityManager::ConstIterator end( im->end() );
286  for ( KPIMIdentities::IdentityManager::ConstIterator it = im->begin(); it != end; ++it ) {
287  if ( (*it).templates() == idString ) {
288  return true;
289  }
290  }
291  return false;
292 }
293 
294 Akonadi::Collection Kernel::trashCollectionFromResource( const Akonadi::Collection &col )
295 {
296  Akonadi::Collection trashCol;
297  if ( col.isValid() ) {
298  const Akonadi::AgentInstance agent = Akonadi::AgentManager::self()->instance(col.resource());
299  trashCol = Akonadi::SpecialMailCollections::self()->collection(Akonadi::SpecialMailCollections::Trash, agent);
300  }
301  return trashCol;
302 }
303 
304 bool Kernel::folderIsTrash( const Akonadi::Collection &col )
305 {
306  if ( col == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ) ) {
307  return true;
308  }
309 
310  const Akonadi::AgentInstance::List lst = MailCommon::Util::agentInstances();
311  foreach ( const Akonadi::AgentInstance &agent, lst ) {
312  const Akonadi::Collection trash = Akonadi::SpecialMailCollections::self()->collection(Akonadi::SpecialMailCollections::Trash, agent);
313  if (col == trash) {
314  return true;
315  }
316  }
317  return false;
318 }
319 
320 bool Kernel::folderIsSentMailFolder( const Akonadi::Collection &col )
321 {
322  if ( col == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::SentMail ) ) {
323  return true;
324  }
325 
326  const QString idString = QString::number( col.id() );
327  if ( idString.isEmpty() ) {
328  return false;
329  }
330 
331  // search the identities if the folder matches the sent-folder
332  const KPIMIdentities::IdentityManager * im = KernelIf->identityManager();
333  KPIMIdentities::IdentityManager::ConstIterator end( im->end() );
334  for ( KPIMIdentities::IdentityManager::ConstIterator it = im->begin(); it != end; ++it ) {
335  if ( (*it).fcc() == idString ) {
336  return true;
337  }
338  }
339  return false;
340 }
341 
342 bool Kernel::folderIsInbox( const Akonadi::Collection &collection, bool withoutPop3InboxSetting )
343 {
344  if ( collection.remoteId().toLower() == QLatin1String( "inbox" ) ||
345  collection.remoteId().toLower() == QLatin1String( "/inbox" ) ||
346  collection.remoteId().toLower() == QLatin1String( ".inbox" ) ) {
347  return true;
348  }
349  //Fix order. Remoteid is not "inbox" when translated
350  if ( collection == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Inbox ) ) {
351  return true;
352  }
353 
354  //MBOX is one folder only, treat as inbox
355  if ( collection.resource().contains(MBOX_RESOURCE_IDENTIFIER) ) {
356  return true;
357  }
358 
359  if ( !withoutPop3InboxSetting ) {
360  const Akonadi::AgentInstance::List lst = MailCommon::Util::agentInstances();
361  foreach ( const Akonadi::AgentInstance &type, lst ) {
362  if ( type.status() == Akonadi::AgentInstance::Broken ) {
363  continue;
364  }
365  if ( type.identifier().contains( POP3_RESOURCE_IDENTIFIER ) ) {
366  OrgKdeAkonadiPOP3SettingsInterface *iface =
367  MailCommon::Util::createPop3SettingsInterface( type.identifier() );
368 
369  if ( iface->isValid() ) {
370  if ( iface->targetCollection() == collection.id() ) {
371  delete iface;
372  return true;
373  }
374  }
375  delete iface;
376  }
377  }
378  }
379  return false;
380 }
381 
382 }
383 
384 
MailCommon::Kernel::folderIsTrash
bool folderIsTrash(const Akonadi::Collection &collection)
Returns true if the folder is a trash folder.
Definition: mailkernel.cpp:304
QModelIndex
MailCommon::Util::agentInstances
MAILCOMMON_EXPORT Akonadi::AgentInstance::List agentInstances(bool excludeMailTransport=true)
Definition: mailutil.cpp:147
MailCommon::Kernel::isMainFolderCollection
bool isMainFolderCollection(const Akonadi::Collection &col)
Returns true if this folder is the inbox on the local disk.
Definition: mailkernel.cpp:144
MailCommon::Kernel::folderIsTemplates
bool folderIsTemplates(const Akonadi::Collection &collection)
Definition: mailkernel.cpp:272
KernelIf
#define KernelIf
Definition: mailkernel.h:191
MailCommon::Kernel::folderIsSentMailFolder
bool folderIsSentMailFolder(const Akonadi::Collection &)
Returns true if the folder is one of the sent-mail folders.
Definition: mailkernel.cpp:320
MailCommon::Kernel::sentCollectionFolder
Akonadi::Collection sentCollectionFolder()
Definition: mailkernel.cpp:113
MailCommon::Kernel::emergencyExit
void emergencyExit(const QString &reason)
Definition: mailkernel.cpp:218
MailCommon::Kernel::trashCollectionFolder
Akonadi::Collection trashCollectionFolder()
Definition: mailkernel.cpp:92
MailCommon::Kernel::inboxCollectionFolder
Akonadi::Collection inboxCollectionFolder()
Definition: mailkernel.cpp:99
QVariant::value
T value() const
MailCommon::Kernel::folderIsInbox
static bool folderIsInbox(const Akonadi::Collection &, bool withoutPop3InboxSetting=false)
Definition: mailkernel.cpp:342
MailCommon::Kernel::templatesCollectionFolder
Akonadi::Collection templatesCollectionFolder()
Definition: mailkernel.cpp:127
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
MailCommon::Kernel::~Kernel
virtual ~Kernel()
Definition: mailkernel.cpp:68
QString::number
QString number(int n, int base)
MailCommon::Kernel::trashCollectionFromResource
Akonadi::Collection trashCollectionFromResource(const Akonadi::Collection &col)
Returns the trash folder for the resource which col belongs to.
Definition: mailkernel.cpp:294
QObject
QString::isEmpty
bool isEmpty() const
QString
MailCommon::Kernel::folderIsDrafts
bool folderIsDrafts(const Akonadi::Collection &)
Definition: mailkernel.cpp:250
MailCommon::Kernel
Deals with common mail application related operations.
Definition: mailkernel.h:44
MailCommon::Kernel::collectionFromId
Akonadi::Collection collectionFromId(const Akonadi::Collection::Id &id) const
Returns the collection associated with the given id, or an invalid collection if not found...
Definition: mailkernel.cpp:83
MailCommon::Kernel::folderIsDraftOrOutbox
bool folderIsDraftOrOutbox(const Akonadi::Collection &collection)
Returns true if the folder is either the outbox or one of the drafts-folders.
Definition: mailkernel.cpp:241
QModelIndex::data
QVariant data(int role) const
QLatin1String
mailkernel.h
MailCommon::Kernel::isSystemFolderCollection
bool isSystemFolderCollection(const Akonadi::Collection &col)
Definition: mailkernel.cpp:134
MailCommon::Kernel::outboxCollectionFolder
Akonadi::Collection outboxCollectionFolder()
Definition: mailkernel.cpp:106
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
MailCommon::Kernel::self
static Kernel * self()
Definition: mailkernel.cpp:73
mailutil.h
MailCommon::Kernel::draftsCollectionFolder
Akonadi::Collection draftsCollectionFolder()
Definition: mailkernel.cpp:120
MailCommon::Kernel::initFolders
void initFolders()
Definition: mailkernel.cpp:150
MailCommon::Kernel::imapResourceManager
PimCommon::ImapResourceCapabilitiesManager * imapResourceManager() const
Definition: mailkernel.cpp:78
MailCommon::Kernel::kernelIf
IKernel * kernelIf() const
Definition: mailkernel.h:68
MailCommon::Util::createPop3SettingsInterface
MAILCOMMON_EXPORT OrgKdeAkonadiPOP3SettingsInterface * createPop3SettingsInterface(const QString &ident)
Definition: mailutil.cpp:89
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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
  • pimprint

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