backport 1145339: no error box spam
[kdepim.git] / kmail / kmkernel.h
blob9c673cc35689321616e121298c6680d1b38ead35
1 // -*- mode: C++; c-file-style: "gnu" -*-
3 #ifndef _KMKERNEL_H
4 #define _KMKERNEL_H
6 #include <QByteArray>
7 #include <QList>
8 #include <QObject>
9 #include <QString>
10 #include <QDBusObjectPath>
12 #include <kconfig.h>
13 #include <kurl.h>
15 #include "kmail_export.h"
16 #include "globalsettings.h"
17 #include <kcomponentdata.h>
18 #include <akonadi/kmime/specialmailcollections.h>
19 #include <akonadi/servermanager.h>
21 #define kmkernel KMKernel::self()
22 #define kmconfig KMKernel::config()
24 namespace Akonadi {
25 class Collection;
26 class ChangeRecorder;
27 class EntityTreeModel;
28 class EntityMimeTypeFilterModel;
31 namespace KIO {
32 class Job;
35 namespace KPIM {
36 class ProgressItem;
39 class MessageSender;
41 class KJob;
42 /** The KMail namespace contains classes used for KMail.
43 * This is to keep them out of the way from all the other
44 * un-namespaced classes in libs and the rest of PIM.
46 namespace KMail {
47 class MailServiceImpl;
48 class UndoStack;
49 class JobScheduler;
51 namespace KPIM { class ProgressDialog; }
52 using KMail::MailServiceImpl;
53 using KMail::UndoStack;
54 using KMail::JobScheduler;
55 using KPIM::ProgressDialog;
56 class KMFilterMgr;
57 class KMFilterActionDict;
58 class AkonadiSender;
59 class FolderCollection;
60 namespace KPIMIdentities {
61 class Identity;
62 class IdentityManager;
64 class KMKernel;
65 class KComponentData;
66 class QTimer;
67 class KMMainWin;
68 class KMainWindow;
69 class KMMainWidget;
70 class ConfigureDialog;
71 class FolderCollectionMonitor;
72 class KMSystemTray;
74 /**
75 * @short Central point of coordination in KMail
77 * The KMKernel class represents the core of KMail, where the different parts
78 * come together and are coordinated. It is currently also the class which exports
79 * KMail's main D-BUS interfaces.
81 * The kernel is responsible for creating various
82 * (singleton) objects such as the identity manager and the message sender.
84 * The kernel also creates an Akonadi Session, Monitor and EntityTreeModel. These
85 * are shared so that other objects in KMail have access to it. Having only one EntityTreeModel
86 * instead of many reduces the overall communication with the Akonadi server.
88 * The kernel also manages some stuff that should be factored out:
89 * - default collection handling, like inboxCollectionFolder()
90 * - job handling, like jobScheduler()
91 * - handling of some config settings, like wrapCol()
92 * - various other stuff
94 class KMAIL_EXPORT KMKernel : public QObject
96 Q_OBJECT
97 Q_CLASSINFO("D-Bus Interface", "org.kde.kmail.kmail")
99 public:
100 explicit KMKernel (QObject *parent=0, const char *name=0);
101 ~KMKernel ();
104 * Start of D-Bus callable stuff. The D-Bus methods need to be public slots,
105 * otherwise they can't be accessed.
107 public Q_SLOTS:
109 Q_SCRIPTABLE void checkMail();
110 Q_SCRIPTABLE void openReader() { openReader( false ); }
113 * Pauses all background jobs and does not
114 * allow new background jobs to be started.
116 Q_SCRIPTABLE void pauseBackgroundJobs();
119 * Resumes all background jobs and allows
120 * new jobs to be started.
122 Q_SCRIPTABLE void resumeBackgroundJobs();
125 * Stops all network related jobs and enter offline mode
126 * New network jobs cannot be started.
128 Q_SCRIPTABLE void stopNetworkJobs();
131 * Resumes all network related jobs and enter online mode
132 * New network jobs can be started.
134 Q_SCRIPTABLE void resumeNetworkJobs();
136 Q_SCRIPTABLE QStringList accounts();
139 * Checks the account with the specified name for new mail.
140 * If the account name is empty, all accounts not excluded from manual
141 * mail check will be checked.
143 Q_SCRIPTABLE void checkAccount( const QString & account );
145 Q_SCRIPTABLE void selectFolder( const QString & folder );
147 Q_SCRIPTABLE bool canQueryClose();
149 Q_SCRIPTABLE bool handleCommandLine( bool noArgsOpensReader );
152 * returns id of composer if more are opened
154 Q_SCRIPTABLE int openComposer( const QString & to,
155 const QString & cc,
156 const QString & bcc,
157 const QString & subject,
158 const QString & body,
159 bool hidden,
160 const QString & messageFile,
161 const QStringList & attachmentPaths,
162 const QStringList & customHeaders );
164 Q_SCRIPTABLE int openComposer( const QString & to,
165 const QString & cc,
166 const QString & bcc,
167 const QString & subject,
168 const QString & body,
169 bool hidden,
170 const QString & attachName,
171 const QByteArray & attachCte,
172 const QByteArray &attachData,
173 const QByteArray & attachType,
174 const QByteArray & attachSubType,
175 const QByteArray & attachParamAttr,
176 const QString & attachParamValue,
177 const QByteArray & attachContDisp,
178 const QByteArray & attachCharset,
179 unsigned int identity );
181 Q_SCRIPTABLE QDBusObjectPath openComposer( const QString & to,
182 const QString & cc,
183 const QString & bcc,
184 const QString & subject,
185 const QString & body,
186 bool hidden );
189 * D-Bus call used by the Kontact plugin to create a new message.
191 Q_SCRIPTABLE QDBusObjectPath newMessage( const QString & to,
192 const QString & cc,
193 const QString & bcc,
194 bool hidden,
195 bool useFolderId,
196 const QString & messageFile,
197 const QString & attachURL );
199 Q_SCRIPTABLE bool showMail( quint32 serialNumber, const QString & messageId );
201 Q_SCRIPTABLE int viewMessage( const KUrl & messageFile );
204 * End of D-Bus callable stuff
208 public:
209 void checkMailOnStartup();
211 /** A static helper function that asks the user
212 * if they want to go online.
213 * @return true if the user wants to go online
214 * @return false if the user wants to stay offline
216 static bool askToGoOnline();
218 /** Checks if the current network state is online or offline
219 * @return true if the network state is offline
220 * @return false if the network state is online
222 static bool isOffline();
224 /** normal control stuff */
226 static KMKernel *self();
227 static KSharedConfig::Ptr config();
229 void init();
230 void setupDBus();
231 void readConfig();
233 Akonadi::ChangeRecorder *monitor() const;
236 * Returns the main model, which contains all folders and the items of recently opened folders.
238 Akonadi::EntityTreeModel *entityTreeModel() const;
241 * Returns a model of all folders in KMail. This is basically the same as entityTreeModel(),
242 * but with items filtered out, the model contains only collections.
244 Akonadi::EntityMimeTypeFilterModel *collectionModel() const;
246 void recoverDeadLetters();
247 void closeAllKMailWindows();
248 void cleanup(void);
249 void quit();
250 bool doSessionManagement();
251 bool firstInstance() const { return the_firstInstance; }
252 void setFirstInstance(bool value) { the_firstInstance = value; }
253 void action( bool mailto, bool check, const QString &to, const QString &cc,
254 const QString &bcc, const QString &subj, const QString &body,
255 const KUrl &messageFile, const KUrl::List &attach,
256 const QStringList &customHeaders );
257 bool folderIsDraftOrOutbox(const Akonadi::Collection &);
258 bool folderIsDrafts(const Akonadi::Collection&);
260 bool folderIsTemplates(const Akonadi::Collection &);
261 bool folderIsTrash( const Akonadi::Collection & );
263 Akonadi::Collection trashCollectionFromResource( const Akonadi::Collection & col );
266 * Returns true if the folder is one of the sent-mail folders.
268 bool folderIsSentMailFolder( const Akonadi::Collection& );
270 bool isImapFolder( const Akonadi::Collection& );
272 const KComponentData &xmlGuiInstance() { return mXmlGuiInstance; }
273 void setXmlGuiInstance( const KComponentData &instance ) { mXmlGuiInstance = instance; }
276 Akonadi::Collection inboxCollectionFolder();
277 Akonadi::Collection outboxCollectionFolder();
278 Akonadi::Collection sentCollectionFolder();
279 Akonadi::Collection trashCollectionFolder();
280 Akonadi::Collection draftsCollectionFolder();
281 Akonadi::Collection templatesCollectionFolder();
283 bool isSystemFolderCollection( const Akonadi::Collection &col);
285 /** Returns true if this folder is the inbox on the local disk */
286 bool isMainFolderCollection( const Akonadi::Collection &col );
288 UndoStack *undoStack() { return the_undoStack; }
289 KMFilterMgr *filterMgr() { return the_filterMgr; }
290 KMFilterMgr *popFilterMgr() { return the_popFilterMgr; }
291 KMFilterActionDict *filterActionDict() { return the_filterActionDict; }
292 MessageSender *msgSender();
294 /** return the pointer to the identity manager */
295 KPIMIdentities::IdentityManager *identityManager();
297 JobScheduler* jobScheduler() { return mJobScheduler; }
299 /** Expire all folders, used for the gui action */
300 void expireAllFoldersNow();
302 int wrapCol() const { return mWrapCol;}
304 bool firstStart() const { return the_firstStart; }
305 /** Mark first start as done */
306 void firstStartDone() { the_firstStart = false; }
307 QString previousVersion() const { return the_previousVersion; }
308 bool startingUp() const { return the_startingUp; }
309 void setStartingUp (bool flag) { the_startingUp = flag; }
310 bool shuttingDown() const { return the_shuttingDown; }
311 void setShuttingDown(bool flag) { the_shuttingDown = flag; }
313 /** Returns the full path of the user's local data directory for KMail.
314 The path ends with '/'.
316 static QString localDataPath();
318 /** Returns true if we have a system tray applet. This is needed in order
319 * to know whether the application should be allowed to exit in case the
320 * last visible composer or separate message window is closed.
322 bool haveSystemTrayApplet();
324 bool registerSystemTrayApplet( KMSystemTray* );
325 bool unregisterSystemTrayApplet( KMSystemTray* );
327 /// Reimplemented from KMailIface
328 void emergencyExit( const QString& reason );
330 QTextCodec *networkCodec() { return netCodec; }
332 /** returns a reference to the first Mainwin or a temporary Mainwin */
333 KMainWindow* mainWin();
335 /** Get first mainwidget */
336 KMMainWidget *getKMMainWidget();
339 * Returns a list of all currently loaded folders. Since folders are loaded async, this
340 * is empty at startup.
342 Akonadi::Collection::List allFolders() const;
344 void selectCollectionFromId( const Akonadi::Collection::Id id);
348 * Returns the collection associated with the given @p id, or an invalid collection if not found.
349 * The EntityTreeModel of the kernel is searched for the collection. Since the ETM is loaded
350 * async, this method will not find the collection right after startup, when the ETM is not yet
351 * fully loaded.
353 Akonadi::Collection collectionFromId( const Akonadi::Collection::Id& id ) const;
356 * Converts @p idString into a number and returns the collection for it.
357 * @see collectionFromId( qint64 )
359 Akonadi::Collection collectionFromId( const QString &idString ) const;
362 * @deprecated this uses exec(), don't use. Use collectionFromId() but be aware of the
363 * restrictions.
365 Akonadi::Collection KDE_DEPRECATED findFolderCollectionById( const QString& id );
367 void raise();
370 void findCreateDefaultCollection( Akonadi::SpecialMailCollections::Type );
372 void stopAgentInstance();
374 void updateSystemTray();
376 public slots:
378 /** Custom templates have changed, so all windows using them need
379 to regenerate their menus */
380 void updatedTemplates();
382 /// Save contents of all open composer widnows to ~/dead.letter
383 void dumpDeadLetters();
385 /** Call this slot instead of directly KConfig::sync() to
386 minimize the overall config writes. Calling this slot will
387 schedule a sync of the application config file using a timer, so
388 that many consecutive calls can be condensed into a single
389 sync, which is more efficient. */
390 void slotRequestConfigSync();
393 * Sync the config immediatley
395 void slotSyncConfig();
397 /** empty all the trash bins */
398 void slotEmptyTrash();
400 void slotShowConfigurationDialog();
401 void slotRunBackgroundTasks();
403 void slotConfigChanged();
404 void slotDefaultCollectionsChanged();
406 signals:
407 void configChanged();
408 void onlineStatusChanged( GlobalSettings::EnumNetworkState::type );
409 void customTemplatesChanged();
411 void startCheckMail();
412 void endCheckMail();
415 private slots:
416 /** Updates identities when a transport has been deleted. */
417 void transportRemoved( int id, const QString &name );
418 /** Updates identities when a transport has been renamed. */
419 void transportRenamed( int id, const QString &oldName, const QString &newName );
420 void itemDispatchStarted();
421 void instanceProgressChanged( Akonadi::AgentInstance );
422 void createDefaultCollectionDone( KJob * job);
424 void initFolders();
425 void akonadiStateChanged( Akonadi::ServerManager::State );
426 void slotProgressItemCompletedOrCanceled( KPIM::ProgressItem * item);
427 private:
428 void openReader( bool onlyCheck );
429 QSharedPointer<FolderCollection> currentFolderCollection();
433 Akonadi::Collection::Id the_inboxCollectionFolder;
434 Akonadi::Collection::Id the_outboxCollectionFolder;
435 Akonadi::Collection::Id the_sentCollectionFolder;
436 Akonadi::Collection::Id the_trashCollectionFolder;
437 Akonadi::Collection::Id the_draftsCollectionFolder;
438 Akonadi::Collection::Id the_templatesCollectionFolder;
440 UndoStack *the_undoStack;
441 KMFilterMgr *the_filterMgr;
442 KMFilterMgr *the_popFilterMgr;
443 KMFilterActionDict *the_filterActionDict;
444 mutable KPIMIdentities::IdentityManager *mIdentityManager;
445 AkonadiSender *the_msgSender;
446 /** previous KMail version. If different from current,
447 the user has just updated. read from config */
448 QString the_previousVersion;
449 /** is this the first start? read from config */
450 bool the_firstStart;
451 /** are we starting up? set in main.cpp directly before kapp->exec() */
452 bool the_startingUp;
453 /** are we going down? set from here */
454 bool the_shuttingDown;
455 /** true unles kmail is closed by session management */
456 bool closed_by_user;
457 bool the_firstInstance;
459 KSharedConfig::Ptr mConfig;
460 QTextCodec *netCodec;
461 KComponentData mXmlGuiInstance;
462 ConfigureDialog *mConfigureDialog;
464 QTimer *mBackgroundTasksTimer;
465 JobScheduler* mJobScheduler;
466 // temporary mainwin
467 KMMainWin *mWin;
468 MailServiceImpl *mMailService;
470 QList<KMSystemTray*> systemTrayApplets;
472 FolderCollectionMonitor *mFolderCollectionMonitor;
473 Akonadi::EntityTreeModel *mEntityTreeModel;
474 Akonadi::EntityMimeTypeFilterModel *mCollectionModel;
476 QList<KPIM::ProgressItem *>mListProgressItem;
478 int mWrapCol;
481 #endif // _KMKERNEL_H