00001 #include <qdir.h>
00002
00003 #include <qpe/config.h>
00004
00005 #include <stdlib.h>
00006
00007 #include "configfile.h"
00008
00009 ConfigFile::ConfigFile() : QObject()
00010 {
00011 checkDirectory();
00012
00013 QDir dir((QString) getenv("HOME") + "/Applications/mail/accounts");
00014 QStringList entries = dir.entryList("account-*");
00015
00016 QStringList::Iterator it;
00017 for (it = entries.begin(); it != entries.end(); it++) {
00018 Account account;
00019
00020 Config *config = new Config((QString) getenv("HOME") + "/Applications/mail/accounts/" + *it, Config::File);
00021 config->setGroup("Account");
00022 account.setAccountName((*it).replace(0, 8, ""));
00023 account.setRealName(config->readEntry("RealName"));
00024 account.setEmail(config->readEntry("Email"));
00025 account.setOrg(config->readEntry("Organization"));
00026 account.setImapServer(config->readEntry("ImapServer"));
00027 account.setImapPort(config->readEntry("ImapPort"));
00028 account.setSmtpServer(config->readEntry("SmtpServer"));
00029 account.setSmtpPort(config->readEntry("SmtpPort"));
00030 account.setUser(config->readEntry("User"));
00031 account.setPass(rot13(config->readEntryCrypt("Pass")));
00032 account.setSmtpSsl(config->readBoolEntry("SmtpSsl"));
00033 account.setSmtpSslPort(config->readEntry("SmtpSslPort"));
00034 account.setImapSsl(config->readBoolEntry("ImapSsl"));
00035 account.setImapSslPort(config->readEntry("ImapSslPort"));
00036 account.setDefaultCc(config->readBoolEntry("DefaultCc"));
00037 account.setDefaultBcc(config->readBoolEntry("DefaultBcc"));
00038 account.setDefaultReplyTo(config->readBoolEntry("DefaultReplyTo"));
00039 account.setCc(config->readEntry("Cc"));
00040 account.setBcc(config->readEntry("Bcc"));
00041 account.setReplyTo(config->readEntry("ReplyTo"));
00042 account.setSignature(config->readEntry("Signature").replace(QRegExp("<br>"), "\n"));
00043
00044 _accounts.append(account);
00045 }
00046 }
00047
00048 QValueList<Account> ConfigFile::getAccounts()
00049 {
00050 ConfigFile *configFile = new ConfigFile();
00051 return configFile->_accounts;
00052 }
00053
00054 void ConfigFile::updateAccount(Account account)
00055 {
00056 checkDirectory();
00057 Config *config = new Config((QString) getenv("HOME") + "/Applications/mail/accounts/account-" + account.accountName(), Config::File);
00058
00059 config->setGroup("Account");
00060 config->writeEntry("RealName", account.realName());
00061 config->writeEntry("Email", account.email());
00062 config->writeEntry("Organization", account.org());
00063 config->writeEntry("ImapServer", account.imapServer());
00064 config->writeEntry("ImapPort", account.imapPort());
00065 config->writeEntry("SmtpServer", account.smtpServer());
00066 config->writeEntry("SmtpPort", account.smtpPort());
00067 config->writeEntry("User", account.user());
00068 config->writeEntryCrypt("Pass", rot13(account.pass()));
00069 config->writeEntry("SmtpSsl", account.smtpSsl());
00070 config->writeEntry("SmtpSslPort", account.smtpSslPort());
00071 config->writeEntry("ImapSsl", account.imapSsl());
00072 config->writeEntry("ImapSslPort", account.imapSslPort());
00073 config->writeEntry("DefaultCc", account.defaultCc());
00074 config->writeEntry("DefaultBcc", account.defaultBcc());
00075 config->writeEntry("DefaultReplyTo", account.defaultReplyTo());
00076 config->writeEntry("Cc", account.cc());
00077 config->writeEntry("Bcc", account.bcc());
00078 config->writeEntry("ReplyTo", account.replyTo());
00079 config->writeEntry("Signature", account.signature().replace(QRegExp("\\n"), "<br>"));
00080
00081 config->write();
00082 }
00083
00084 void ConfigFile::deleteAccount(Account account)
00085 {
00086 QFile f((QString) getenv("HOME") + "/Applications/mail/accounts/account-" + account.accountName());
00087 f.remove();
00088 }
00089
00090 void ConfigFile::checkDirectory()
00091 {
00092 if (!QDir((QString) getenv("HOME") + "/Applications/mail/accounts").exists()) {
00093 system("mkdir -p $HOME/Applications/mail/accounts");
00094 qWarning("mail: $HOME/Applications/mail/accounts created");
00095 }
00096 }
00097
00098 QString ConfigFile::rot13(const QString &input)
00099 {
00100 QString i = input;
00101 int l = i.length();
00102 while(l--) {
00103 if (i[l] >= QChar('A') && i[l] <= QChar('M') ||
00104 i[l] >= QChar('a') && i[l] <= QChar('m'))
00105 i[l] = QChar(i[l].unicode()+13);
00106 else if (i[l] >= QChar('N') && i[l] <= QChar('Z') ||
00107 i[l] >= QChar('n') && i[l] <= QChar('z'))
00108 i[l] = QChar(i[l].unicode()-13);
00109 }
00110 return i;
00111 }
00112