Fix Bug 361605 - kmail crash on double click email
[kdepim.git] / kmail / kmkernel.h
blob0a4879f301d229354d3a7baed517700f16aff703
1 //
3 #ifndef _KMKERNEL_H
4 #define _KMKERNEL_H
6 #include "mailcommon/mailinterfaces.h"
8 #include <QList>
9 #include <QObject>
10 #include <QPointer>
11 #include <QDBusObjectPath>
13 #include <qurl.h>
15 #include "kmail_export.h"
16 #include "settings/kmailsettings.h"
17 #include <AkonadiCore/servermanager.h>
18 #include "messageviewer/viewer.h"
19 #include "libkdepim/progressmanager.h"
21 #define kmkernel KMKernel::self()
22 #define kmconfig KMKernel::config()
24 class QAbstractItemModel;
25 namespace Akonadi
27 class Collection;
28 class ChangeRecorder;
29 class EntityTreeModel;
30 class EntityMimeTypeFilterModel;
33 namespace KIO
35 class Job;
38 namespace MessageComposer
40 class MessageSender;
42 namespace PimCommon
44 class AutoCorrection;
45 class StorageServiceManager;
48 /** The KMail namespace contains classes used for KMail.
49 * This is to keep them out of the way from all the other
50 * un-namespaced classes in libs and the rest of PIM.
52 namespace KMail
54 class MailServiceImpl;
55 class UndoStack;
56 class KMSystemTray;
58 using KMail::MailServiceImpl;
59 using KMail::UndoStack;
60 namespace MessageComposer
62 class AkonadiSender;
65 namespace KIdentityManagement
67 class Identity;
68 class IdentityManager;
71 namespace MailCommon
73 class Kernel;
74 class FolderCollection;
75 class FolderCollectionMonitor;
76 class JobScheduler;
77 class KMFilterDialog;
80 class QTimer;
81 class KMMainWin;
82 class KMainWindow;
83 class KMMainWidget;
84 class ConfigureDialog;
85 class FolderArchiveManager;
87 /**
88 * @short Central point of coordination in KMail
90 * The KMKernel class represents the core of KMail, where the different parts
91 * come together and are coordinated. It is currently also the class which exports
92 * KMail's main D-BUS interfaces.
94 * The kernel is responsible for creating various
95 * (singleton) objects such as the identity manager and the message sender.
97 * The kernel also creates an Akonadi Session, Monitor and EntityTreeModel. These
98 * are shared so that other objects in KMail have access to it. Having only one EntityTreeModel
99 * instead of many reduces the overall communication with the Akonadi server.
101 * The kernel also manages some stuff that should be factored out:
102 * - default collection handling, like inboxCollectionFolder()
103 * - job handling, like jobScheduler()
104 * - handling of some config settings, like wrapCol()
105 * - various other stuff
107 class KMAIL_EXPORT KMKernel : public QObject, public MailCommon::IKernel, public MailCommon::ISettings, public MailCommon::IFilter
109 Q_OBJECT
110 Q_CLASSINFO("D-Bus Interface", "org.kde.kmail.kmail")
112 public:
113 explicit KMKernel(QObject *parent = Q_NULLPTR);
114 ~KMKernel();
117 * Start of D-Bus callable stuff. The D-Bus methods need to be public slots,
118 * otherwise they can't be accessed.
120 public Q_SLOTS:
122 Q_SCRIPTABLE void checkMail();
123 Q_SCRIPTABLE void openReader();
126 * Enables/disables systray icon changing when mail arrives.
127 * With this disabled the systray icon will always be the same.
129 Q_SCRIPTABLE void setSystrayIconNotificationsEnabled(bool enabled);
132 * Pauses all background jobs and does not
133 * allow new background jobs to be started.
135 Q_SCRIPTABLE void pauseBackgroundJobs();
138 * Resumes all background jobs and allows
139 * new jobs to be started.
141 Q_SCRIPTABLE void resumeBackgroundJobs();
144 * Stops all network related jobs and enter offline mode
145 * New network jobs cannot be started.
147 Q_SCRIPTABLE void stopNetworkJobs();
150 * Resumes all network related jobs and enter online mode
151 * New network jobs can be started.
153 Q_SCRIPTABLE void resumeNetworkJobs();
155 Q_SCRIPTABLE QStringList accounts();
157 Q_SCRIPTABLE void makeResourceOnline(MessageViewer::Viewer::ResourceOnlineMode mode);
160 * Checks the account with the specified name for new mail.
161 * If the account name is empty, all accounts not excluded from manual
162 * mail check will be checked.
164 Q_SCRIPTABLE void checkAccount(const QString &account);
166 Q_SCRIPTABLE bool selectFolder(const QString &folder);
168 Q_SCRIPTABLE bool canQueryClose();
170 Q_SCRIPTABLE bool handleCommandLine(bool noArgsOpensReader, const QStringList &args, const QString &workingDir);
173 * Opens a composer window and prefills it with different
174 * message parts.
176 * @returns The id of composer if more are opened.
178 * @param to A comma separated list of To addresses.
179 * @param cc A comma separated list of CC addresses.
180 * @param bcc A comma separated list of BCC addresses.
181 * @param subject The message subject.
182 * @param body The message body.
183 * @param hidden Whether the composer window shall initially be hidden.
184 * @param messageFile A message file that will be used as message body.
185 * @param attachmentPaths A list of files that will be attached to the message.
186 * @param customHeaders A list of custom headers.
188 Q_SCRIPTABLE int openComposer(const QString &to,
189 const QString &cc,
190 const QString &bcc,
191 const QString &subject,
192 const QString &body,
193 bool hidden,
194 const QString &messageFile,
195 const QStringList &attachmentPaths,
196 const QStringList &customHeaders, const QString &replyTo = QString(), const QString &inReplyTo = QString());
199 * Opens a composer window and prefills it with different
200 * message parts.
202 * @returns The id of composer if more are opened.
204 * @param to A comma separated list of To addresses.
205 * @param cc A comma separated list of CC addresses.
206 * @param bcc A comma separated list of BCC addresses.
207 * @param subject The message subject.
208 * @param body The message body.
209 * @param hidden Whether the composer window shall initially be hidden.
210 * @param attachName The name of the attachment.
211 * @param attachCte The content transfer encoding of the attachment.
212 * @param attachData The raw data of the attachment.
213 * @param attachType The mime type of the attachment.
214 * @param attachSubType The sub mime type of the attachment.
215 * @param attachParamAttr The parameter attribute of the attachment.
216 * @param attachParamValue The parameter value of the attachment.
217 * @param attachContDisp The content display type of the attachment.
218 * @param attachCharset The charset of the attachment.
219 * @param identity The identity identifier which will be used as sender identity.
221 Q_SCRIPTABLE int openComposer(const QString &to,
222 const QString &cc,
223 const QString &bcc,
224 const QString &subject,
225 const QString &body,
226 bool hidden,
227 const QString &attachName,
228 const QByteArray &attachCte,
229 const QByteArray &attachData,
230 const QByteArray &attachType,
231 const QByteArray &attachSubType,
232 const QByteArray &attachParamAttr,
233 const QString &attachParamValue,
234 const QByteArray &attachContDisp,
235 const QByteArray &attachCharset,
236 unsigned int identity);
239 * Opens a composer window and prefills it with different
240 * message parts.
241 * @since 5.0
243 * @returns The id of composer if more are opened.
245 * @param to A comma separated list of To addresses.
246 * @param cc A comma separated list of CC addresses.
247 * @param bcc A comma separated list of BCC addresses.
248 * @param subject The message subject.
249 * @param body The message body.
250 * @param attachName The name of the attachment.
251 * @param attachCte The content transfer encoding of the attachment.
252 * @param attachData The raw data of the attachment.
253 * @param attachType The mime type of the attachment.
254 * @param attachSubType The sub mime type of the attachment.
255 * @param attachParamAttr The parameter attribute of the attachment.
256 * @param attachParamValue The parameter value of the attachment.
257 * @param attachContDisp The content display type of the attachment.
258 * @param attachCharset The charset of the attachment.
259 * @param identity The identity identifier which will be used as sender identity.
261 Q_SCRIPTABLE int openComposer(const QString &to,
262 const QString &cc,
263 const QString &bcc,
264 const QString &subject,
265 const QString &body,
266 const QString &attachName,
267 const QByteArray &attachCte,
268 const QByteArray &attachData,
269 const QByteArray &attachType,
270 const QByteArray &attachSubType,
271 const QByteArray &attachParamAttr,
272 const QString &attachParamValue,
273 const QByteArray &attachContDisp,
274 const QByteArray &attachCharset,
275 unsigned int identity);
278 * Opens a composer window and prefills it with different
279 * message parts.
281 * @returns The DBus object path for the composer.
283 * @param to A comma separated list of To addresses.
284 * @param cc A comma separated list of CC addresses.
285 * @param bcc A comma separated list of BCC addresses.
286 * @param subject The message subject.
287 * @param body The message body.
288 * @param hidden Whether the composer window shall initially be hidden.
290 Q_SCRIPTABLE QDBusObjectPath openComposer(const QString &to,
291 const QString &cc,
292 const QString &bcc,
293 const QString &subject,
294 const QString &body,
295 bool hidden);
298 * Opens a composer window and prefills it with different
299 * message parts.
301 * @returns The DBus object path for the composer.
303 * @param to A comma separated list of To addresses.
304 * @param cc A comma separated list of CC addresses.
305 * @param bcc A comma separated list of BCC addresses.
306 * @param subject The message subject.
307 * @param body The message body.
308 * @param hidden Whether the composer window shall initially be hidden.
309 * @param useFolderId The id of the folder whose associated identity will be used.
310 * @param messageFile A message file that will be used as message body.
311 * @param attachURL The URL to the file that will be attached to the message.
313 Q_SCRIPTABLE QDBusObjectPath newMessage(const QString &to,
314 const QString &cc,
315 const QString &bcc,
316 bool hidden,
317 bool useFolderId,
318 const QString &messageFile,
319 const QString &attachURL);
321 Q_SCRIPTABLE bool showMail(qint64 serialNumber);
323 Q_SCRIPTABLE int viewMessage(const QString &messageFile);
325 Q_SCRIPTABLE void updateConfig();
327 Q_SCRIPTABLE void showFolder(const QString &collectionId);
329 Q_SCRIPTABLE void reloadFolderArchiveConfig();
332 * End of D-Bus callable stuff
335 public:
336 void checkMailOnStartup();
338 /** A static helper function that asks the user
339 * if they want to go online.
340 * @return true if the user wants to go online
341 * @return false if the user wants to stay offline
343 static bool askToGoOnline();
345 /** Checks if the current network state is online or offline
346 * @return true if the network state is offline
347 * @return false if the network state is online
349 static bool isOffline();
351 /** normal control stuff */
353 static KMKernel *self();
354 KSharedConfig::Ptr config() Q_DECL_OVERRIDE;
355 void syncConfig() Q_DECL_OVERRIDE;
357 void init();
358 void setupDBus();
360 Akonadi::ChangeRecorder *folderCollectionMonitor() const Q_DECL_OVERRIDE;
363 * Returns the main model, which contains all folders and the items of recently opened folders.
365 Akonadi::EntityTreeModel *entityTreeModel() const;
368 * Returns a model of all folders in KMail. This is basically the same as entityTreeModel(),
369 * but with items filtered out, the model contains only collections.
371 Akonadi::EntityMimeTypeFilterModel *collectionModel() const Q_DECL_OVERRIDE;
373 void recoverDeadLetters();
374 void closeAllKMailWindows();
375 void cleanup(void);
376 void quit();
377 bool doSessionManagement();
378 bool firstInstance() const;
379 void setFirstInstance(bool value);
380 void action(bool mailto, bool check, const QString &to, const QString &cc,
381 const QString &bcc, const QString &subj, const QString &body,
382 const QUrl &messageFile, const QList<QUrl> &attach,
383 const QStringList &customHeaders, const QString &replyTo, const QString &inReplyTo);
385 //sets online status for akonadi accounts. true for online, false for offline
386 void setAccountStatus(bool);
388 const QString xmlGuiInstanceName() const;
389 void setXmlGuiInstanceName(const QString &instance);
391 UndoStack *undoStack() const;
392 MessageComposer::MessageSender *msgSender() Q_DECL_OVERRIDE;
394 void openFilterDialog(bool createDummyFilter = true) Q_DECL_OVERRIDE;
395 void createFilter(const QByteArray &field, const QString &value) Q_DECL_OVERRIDE;
397 /** return the pointer to the identity manager */
398 KIdentityManagement::IdentityManager *identityManager() Q_DECL_OVERRIDE;
400 MailCommon::JobScheduler *jobScheduler() const Q_DECL_OVERRIDE
402 return mJobScheduler;
405 /** Expire all folders, used for the gui action */
406 void expireAllFoldersNow();
408 bool firstStart() const;
409 QString previousVersion() const;
410 bool shuttingDown() const;
411 void setShuttingDown(bool flag);
413 /** Returns the full path of the user's local data directory for KMail.
414 The path ends with '/'.
416 static QString localDataPath();
418 /** Returns true if we have a system tray applet. This is needed in order
419 * to know whether the application should be allowed to exit in case the
420 * last visible composer or separate message window is closed.
422 bool haveSystemTrayApplet() const;
424 QTextCodec *networkCodec() const
426 return mNetCodec;
429 /** returns a reference to the first Mainwin or a temporary Mainwin */
430 KMainWindow *mainWin();
432 /** Get first mainwidget */
433 KMMainWidget *getKMMainWidget();
436 * Returns a list of all currently loaded folders. Since folders are loaded async, this
437 * is empty at startup.
439 Akonadi::Collection::List allFolders() const;
441 void selectCollectionFromId(Akonadi::Collection::Id id);
443 void raise();
445 void stopAgentInstance();
447 //ISettings
448 bool showPopupAfterDnD() Q_DECL_OVERRIDE;
450 bool excludeImportantMailFromExpiry() Q_DECL_OVERRIDE;
452 qreal closeToQuotaThreshold() Q_DECL_OVERRIDE;
454 Akonadi::Collection::Id lastSelectedFolder() Q_DECL_OVERRIDE;
455 void setLastSelectedFolder(Akonadi::Collection::Id col) Q_DECL_OVERRIDE;
457 QStringList customTemplates() Q_DECL_OVERRIDE;
459 void checkFolderFromResources(const Akonadi::Collection::List &collectionList);
461 const QAbstractItemModel *treeviewModelSelection();
463 void savePaneSelection();
465 void updatePaneTagComboBox();
467 PimCommon::AutoCorrection *composerAutoCorrection();
469 void toggleSystemTray();
470 FolderArchiveManager *folderArchiveManager() const;
471 PimCommon::StorageServiceManager *storageServiceManager() const;
473 bool allowToDebugBalooSupport() const;
475 protected:
476 void agentInstanceBroken(const Akonadi::AgentInstance &instance);
478 public Q_SLOTS:
480 void updateSystemTray() Q_DECL_OVERRIDE;
482 /** Custom templates have changed, so all windows using them need
483 to regenerate their menus */
484 void updatedTemplates();
486 /// Save contents of all open composer widnows to ~/dead.letter
487 void dumpDeadLetters();
489 /** Call this slot instead of directly KConfig::sync() to
490 minimize the overall config writes. Calling this slot will
491 schedule a sync of the application config file using a timer, so
492 that many consecutive calls can be condensed into a single
493 sync, which is more efficient. */
494 void slotRequestConfigSync();
497 * Sync the config immediatley
499 void slotSyncConfig();
501 void slotShowConfigurationDialog();
502 void slotRunBackgroundTasks();
504 void slotConfigChanged();
506 Q_SIGNALS:
507 void configChanged();
508 void onlineStatusChanged(KMailSettings::EnumNetworkState::type);
509 void customTemplatesChanged();
511 void startCheckMail();
512 void endCheckMail();
514 private Q_SLOTS:
515 /** Updates identities when a transport has been deleted. */
516 void transportRemoved(int id, const QString &name);
517 /** Updates identities when a transport has been renamed. */
518 void transportRenamed(int id, const QString &oldName, const QString &newName);
519 void itemDispatchStarted();
520 void instanceStatusChanged(const Akonadi::AgentInstance &);
522 void akonadiStateChanged(Akonadi::ServerManager::State);
523 void slotProgressItemCompletedOrCanceled(KPIM::ProgressItem *item);
524 void slotInstanceError(const Akonadi::AgentInstance &instance, const QString &message);
525 void slotInstanceWarning(const Akonadi::AgentInstance &instance, const QString &message);
526 void slotCollectionRemoved(const Akonadi::Collection &col);
527 void slotDeleteIdentity(uint identity);
528 void slotInstanceRemoved(const Akonadi::AgentInstance &);
529 void slotSystemNetworkStatusChanged(bool isOnline);
530 void slotCollectionChanged(const Akonadi::Collection &, const QSet<QByteArray> &set);
532 void slotCheckAccount(Akonadi::ServerManager::State state);
533 private:
534 void viewMessage(const QUrl &url);
536 * Fills a composer cWin
538 * @returns true if attached message is a valid iCal message
540 bool fillComposer(KMail::Composer *&cWin,
541 const QString &to, const QString &cc,
542 const QString &bcc, const QString &subject,
543 const QString &body,
544 const QString &attachName,
545 const QByteArray &attachCte,
546 const QByteArray &attachData,
547 const QByteArray &attachType,
548 const QByteArray &attachSubType,
549 const QByteArray &attachParamAttr,
550 const QString &attachParamValue,
551 const QByteArray &attachContDisp,
552 const QByteArray &attachCharset,
553 unsigned int identity);
555 void verifyAccount();
556 void resourceGoOnLine();
557 void openReader(bool onlyCheck);
558 QSharedPointer<MailCommon::FolderCollection> currentFolderCollection();
560 UndoStack *the_undoStack;
561 mutable KIdentityManagement::IdentityManager *mIdentityManager;
562 MessageComposer::AkonadiSender *the_msgSender;
563 /** previous KMail version. If different from current,
564 the user has just updated. read from config */
565 QString the_previousVersion;
566 /** is this the first start? read from config */
567 bool the_firstStart;
568 /** are we going down? set from here */
569 bool the_shuttingDown;
570 /** true unles kmail is closed by session management */
571 bool the_firstInstance;
573 KSharedConfig::Ptr mConfig;
574 QTextCodec *mNetCodec;
575 QString mXmlGuiInstance;
576 ConfigureDialog *mConfigureDialog;
578 QTimer *mBackgroundTasksTimer;
579 MailCommon::JobScheduler *mJobScheduler;
580 // temporary mainwin
581 KMMainWin *mWin;
582 MailServiceImpl *mMailService;
584 bool mSystemNetworkStatus;
586 KMail::KMSystemTray *mSystemTray;
587 QHash<QString, KPIM::ProgressItem::CryptoStatus> mResourceCryptoSettingCache;
588 MailCommon::FolderCollectionMonitor *mFolderCollectionMonitor;
589 Akonadi::EntityTreeModel *mEntityTreeModel;
590 Akonadi::EntityMimeTypeFilterModel *mCollectionModel;
592 /// List of Akonadi resources that are currently being checked.
593 QList<QString> mResourcesBeingChecked;
595 QPointer<MailCommon::KMFilterDialog> mFilterEditDialog;
596 PimCommon::AutoCorrection *mAutoCorrection;
597 FolderArchiveManager *mFolderArchiveManager;
598 PimCommon::StorageServiceManager *mStorageManager;
599 bool mDebugBaloo;
602 #endif // _KMKERNEL_H