You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

272 lines
6.8 KiB

// KMail startup and initialize code
// Author: Stefan Taferner <taferner@alpin.or.at>
#include <qstring.h>
#include <qdir.h>
#include "kmglobal.h"
#include "kmmainwin.h"
#include "kmacctmgr.h"
#include "kmfoldermgr.h"
#include "kmfilteraction.h"
#include "kmfolder.h"
#include "kmsender.h"
#include "kbusyptr.h"
#include "kmfiltermgr.h"
#include "kmversion.h"
#include "kmmessage.h"
#include "kmcomposewin.h"
#include "kmaddrbook.h"
#include "kcharsets.h"
#include <kapp.h>
#include <stdio.h>
#include <stdlib.h>
#include <kmsgbox.h>
#include <klocale.h>
#include <kstdaccel.h>
#include <kmidentity.h>
#include <dirent.h>
#include <sys/stat.h>
KBusyPtr* kbp = NULL;
KApplication* app = NULL;
KMAcctMgr* acctMgr = NULL;
KMFolderMgr* folderMgr = NULL;
KMFilterMgr* filterMgr = NULL;
KMSender* msgSender = NULL;
KLocale* nls = NULL;
KMFolder* inboxFolder = NULL;
KMFolder* outboxFolder = NULL;
KMFolder* queuedFolder = NULL;
KMFolder* sentFolder = NULL;
KMFolder* trashFolder = NULL;
KStdAccel* keys = NULL;
KMIdentity* identity = NULL;
KMFilterActionDict* filterActionDict = NULL;
KMAddrBook* addrBook = NULL;
bool shuttingDown = FALSE;
const char* aboutText =
"KMail [" KMAIL_VERSION "] by\n\n"
"Stefan Taferner <taferner@kde.org>,\n"
"Markus Wübben <markus.wuebben@kde.org>\n\n"
"based on the work of:\n"
"Lynx <lynx@topaz.hknet.com>,\n"
"Stephan Meyer <Stephan.Meyer@pobox.com>,\n"
"and the above authors.\n\n"
"This program is covered by the GPL.";
static msg_handler oldMsgHandler = NULL;
//-----------------------------------------------------------------------------
// Message handler
static void kmailMsgHandler(QtMsgType aType, const char* aMsg)
{
QString appName = app->appName();
switch (aType)
{
case QtDebugMsg:
fprintf(stderr, "%s: %s\n", (const char*)app->appName(), aMsg);
break;
case QtWarningMsg:
fprintf(stderr, "%s: %s\n", (const char*)app->appName(), aMsg);
if (strncmp(aMsg,"KCharset:",9) != 0 &&
strncmp(aMsg,"QGManager:",10) != 0)
KMsgBox::message(NULL, appName+" "+nls->translate("warning"), aMsg,
KMsgBox::EXCLAMATION);
break;
case QtFatalMsg:
fprintf(stderr, appName+" "+nls->translate("fatal error")+": %s\n", aMsg);
KMsgBox::message(NULL, appName+" "+nls->translate("fatal error"),
aMsg, KMsgBox::STOP);
abort();
}
}
//-----------------------------------------------------------------------------
void testDir( const char *_name )
{
DIR *dp;
QString c = getenv("HOME");
c += _name;
dp = opendir(c.data());
if (dp == NULL)
::mkdir(c.data(), S_IRWXU);
else
closedir(dp);
}
//-----------------------------------------------------------------------------
static void transferMail(void)
{
QDir dir = QDir::home();
int rc;
// This function is for all the whiners who think that KMail is
// broken because they cannot read mail with pine and do not
// know how to fix this problem with a simple symbolic link =;-)
if (!dir.cd("KMail")) return;
rc = KMsgBox::yesNo(NULL, app->appName()+" "+nls->translate("warning"),
nls->translate(
"The directory ~/KMail exists. From now on, KMail uses the\n"
"directory ~/Mail for it's messages.\n"
"KMail can move the contents of the directory ~/KMail into\n"
"~/Mail, but this will replace existing files with the same\n"
"name in the directory ~/Mail (e.g. inbox).\n\n"
"Shall KMail move the mail folders now ?"),
KMsgBox::QUESTION);
if (rc != 1) return;
dir.cd("/"); // otherwise we lock the directory
testDir("/Mail");
system("mv -f ~/KMail/* ~/Mail");
system("mv -f ~/KMail/.??* ~/Mail");
system("rmdir ~/KMail");
}
//-----------------------------------------------------------------------------
static void initFolders(KConfig* cfg)
{
QString name;
name = cfg->readEntry("inboxFolder");
// Currently the folder manager cannot manage folders which are not
// in the base folder directory.
//if (name.isEmpty()) name = getenv("MAIL");
if (name.isEmpty()) name = "inbox";
inboxFolder = (KMFolder*)folderMgr->findOrCreate(name);
// inboxFolder->open();
outboxFolder = folderMgr->findOrCreate(cfg->readEntry("outboxFolder", "outbox"));
outboxFolder->setType("Out");
outboxFolder->open();
sentFolder = folderMgr->findOrCreate(cfg->readEntry("sentFolder", "sent-mail"));
sentFolder->setType("St");
sentFolder->open();
trashFolder = folderMgr->findOrCreate(cfg->readEntry("trashFolder", "trash"));
trashFolder->setType("Tr");
trashFolder->open();
}
//-----------------------------------------------------------------------------
static void init(int argc, char *argv[])
{
QString acctPath, foldersPath;
KConfig* cfg;
app = new KApplication(argc, argv, "kmail");
nls = app->getLocale();
kbp = new KBusyPtr;
cfg = app->getConfig();
keys = new KStdAccel(cfg);
oldMsgHandler = qInstallMsgHandler(kmailMsgHandler);
testDir("/.kde");
testDir("/.kde/share");
testDir("/.kde/share/config");
testDir("/.kde/share/apps");
testDir("/.kde/share/apps/kmail");
identity = new KMIdentity;
cfg->setGroup("General");
foldersPath = cfg->readEntry("folders", "");
acctPath = cfg->readEntry("accounts", foldersPath + "/.kmail-accounts");
if (foldersPath.isEmpty())
{
foldersPath = QDir::homeDirPath() + QString("/Mail");
transferMail();
}
folderMgr = new KMFolderMgr(foldersPath);
acctMgr = new KMAcctMgr(acctPath);
filterMgr = new KMFilterMgr;
filterActionDict = new KMFilterActionDict;
addrBook = new KMAddrBook;
initFolders(cfg);
acctMgr->readConfig();
filterMgr->readConfig();
addrBook->readConfig();
addrBook->load();
KMMessage::readConfig();
msgSender = new KMSender;
}
//-----------------------------------------------------------------------------
static void cleanup(void)
{
KConfig* config = kapp->getConfig();
shuttingDown = TRUE;
if (trashFolder)
{
trashFolder->close(TRUE);
config->setGroup("General");
if (config->readNumEntry("empty-trash-on-exit", 0))
trashFolder->expunge();
}
if (inboxFolder) inboxFolder->close(TRUE);
if (outboxFolder) outboxFolder->close(TRUE);
if (sentFolder) sentFolder->close(TRUE);
if (msgSender) delete msgSender;
if (addrBook) delete addrBook;
if (filterMgr) delete filterMgr;
if (acctMgr) delete acctMgr;
if (folderMgr) delete folderMgr;
if (kbp) delete kbp;
qInstallMsgHandler(oldMsgHandler);
app->getConfig()->sync();
}
//-----------------------------------------------------------------------------
main(int argc, char *argv[])
{
KMMainWin* mainWin;
init(argc, argv);
mainWin = new KMMainWin;
mainWin->show();
if (argc > 1 && argv[1][0]!='-')
{
KMComposeWin *win;
KMMessage* msg = new KMMessage;
msg->initHeader();
msg->setTo(argv[1]);
win = new KMComposeWin(msg);
win->show();
}
app->exec();
cleanup();
}