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

mailcommon

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

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