diff options
Diffstat (limited to 'accounts/maildir/maildirsettings.cpp')
-rw-r--r-- | accounts/maildir/maildirsettings.cpp | 85 |
1 files changed, 83 insertions, 2 deletions
diff --git a/accounts/maildir/maildirsettings.cpp b/accounts/maildir/maildirsettings.cpp index 847ce92e..fa30851c 100644 --- a/accounts/maildir/maildirsettings.cpp +++ b/accounts/maildir/maildirsettings.cpp | |||
@@ -38,6 +38,18 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) | |||
38 | return; | 38 | return; |
39 | } | 39 | } |
40 | mAccountIdentifier = id; | 40 | mAccountIdentifier = id; |
41 | |||
42 | //Clear | ||
43 | mIcon = QString(); | ||
44 | mName = QString(); | ||
45 | mPath = QString(); | ||
46 | mSmtpServer = QString(); | ||
47 | mSmtpUsername = QString(); | ||
48 | mSmtpPassword = QString(); | ||
49 | emit changed(); | ||
50 | emit pathChanged(); | ||
51 | emit smtpResourceChanged(); | ||
52 | |||
41 | Q_ASSERT(!id.isEmpty()); | 53 | Q_ASSERT(!id.isEmpty()); |
42 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkAccount>(Sink::Query::IdentityFilter(id)) | 54 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkAccount>(Sink::Query::IdentityFilter(id)) |
43 | .then<void, Sink::ApplicationDomain::SinkAccount>([this](const Sink::ApplicationDomain::SinkAccount &account) { | 55 | .then<void, Sink::ApplicationDomain::SinkAccount>([this](const Sink::ApplicationDomain::SinkAccount &account) { |
@@ -46,7 +58,7 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) | |||
46 | emit changed(); | 58 | emit changed(); |
47 | }).exec(); | 59 | }).exec(); |
48 | 60 | ||
49 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::PropertyFilter("account", QVariant::fromValue(id))) | 61 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::PropertyFilter("account", QVariant::fromValue(id)) + Sink::Query::PropertyFilter("type", QString("org.kde.maildir"))) |
50 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { | 62 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { |
51 | mIdentifier = resource.identifier(); | 63 | mIdentifier = resource.identifier(); |
52 | auto path = resource.getProperty("path").toString(); | 64 | auto path = resource.getProperty("path").toString(); |
@@ -54,6 +66,21 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) | |||
54 | mPath = path; | 66 | mPath = path; |
55 | emit pathChanged(); | 67 | emit pathChanged(); |
56 | } | 68 | } |
69 | }, | ||
70 | [](int errorCode, const QString &errorMessage) { | ||
71 | qWarning() << "Failed to find the maildir resource: " << errorMessage; | ||
72 | }).exec(); | ||
73 | |||
74 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::PropertyFilter("account", QVariant::fromValue(id)) + Sink::Query::PropertyFilter("type", QString("org.kde.mailtransport"))) | ||
75 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { | ||
76 | mMailtransportIdentifier = resource.identifier(); | ||
77 | mSmtpServer = resource.getProperty("server").toString(); | ||
78 | mSmtpUsername = resource.getProperty("username").toString(); | ||
79 | mSmtpPassword = resource.getProperty("password").toString(); | ||
80 | emit smtpResourceChanged(); | ||
81 | }, | ||
82 | [](int errorCode, const QString &errorMessage) { | ||
83 | qWarning() << "Failed to find the maildir resource: " << errorMessage; | ||
57 | }).exec(); | 84 | }).exec(); |
58 | } | 85 | } |
59 | 86 | ||
@@ -92,13 +119,32 @@ QValidator *MaildirSettings::pathValidator() const | |||
92 | return pathValidator; | 119 | return pathValidator; |
93 | } | 120 | } |
94 | 121 | ||
122 | QValidator *MaildirSettings::smtpServerValidator() const | ||
123 | { | ||
124 | class SmtpServerValidator : public QValidator { | ||
125 | State validate(QString &input, int &pos) const { | ||
126 | Q_UNUSED(pos); | ||
127 | // smtps://mainserver.example.net:475 | ||
128 | const QUrl url(input); | ||
129 | static QSet<QString> validProtocols = QSet<QString>() << "smtp" << "smtps"; | ||
130 | if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { | ||
131 | return Acceptable; | ||
132 | } else { | ||
133 | return Intermediate; | ||
134 | } | ||
135 | } | ||
136 | }; | ||
137 | static SmtpServerValidator *validator = new SmtpServerValidator; | ||
138 | return validator; | ||
139 | } | ||
140 | |||
95 | void MaildirSettings::save() | 141 | void MaildirSettings::save() |
96 | { | 142 | { |
97 | if (!QDir(mPath).exists()) { | 143 | if (!QDir(mPath).exists()) { |
98 | qWarning() << "The path doesn't exist: " << mPath; | 144 | qWarning() << "The path doesn't exist: " << mPath; |
99 | return; | 145 | return; |
100 | } | 146 | } |
101 | qDebug() << "Saving account " << mAccountIdentifier << mIdentifier; | 147 | qDebug() << "Saving account " << mAccountIdentifier << mIdentifier << mMailtransportIdentifier; |
102 | Q_ASSERT(!mAccountIdentifier.isEmpty()); | 148 | Q_ASSERT(!mAccountIdentifier.isEmpty()); |
103 | Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); | 149 | Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); |
104 | account.setProperty("type", "maildir"); | 150 | account.setProperty("type", "maildir"); |
@@ -133,6 +179,34 @@ void MaildirSettings::save() | |||
133 | }) | 179 | }) |
134 | .exec(); | 180 | .exec(); |
135 | } | 181 | } |
182 | |||
183 | if (!mMailtransportIdentifier.isEmpty()) { | ||
184 | Sink::ApplicationDomain::SinkResource resource(mMailtransportIdentifier); | ||
185 | resource.setProperty("server", mSmtpServer); | ||
186 | resource.setProperty("username", mSmtpUsername); | ||
187 | resource.setProperty("password", mSmtpPassword); | ||
188 | Sink::Store::modify(resource).then<void>([](){}, [](int errorCode, const QString &errorMessage) { | ||
189 | qWarning() << "Error while modifying resource: " << errorMessage; | ||
190 | }) | ||
191 | .exec(); | ||
192 | } else { | ||
193 | //FIXME we shouldn't have to do this magic | ||
194 | const auto resourceIdentifier = "org.kde.mailtransport." + QUuid::createUuid().toByteArray(); | ||
195 | mMailtransportIdentifier = resourceIdentifier; | ||
196 | |||
197 | Sink::ApplicationDomain::SinkResource resource; | ||
198 | resource.setProperty("identifier", resourceIdentifier); | ||
199 | resource.setProperty("type", "org.kde.mailtransport"); | ||
200 | resource.setProperty("account", mAccountIdentifier); | ||
201 | resource.setProperty("server", mSmtpServer); | ||
202 | resource.setProperty("username", mSmtpUsername); | ||
203 | resource.setProperty("password", mSmtpPassword); | ||
204 | Sink::Store::create(resource).then<void>([]() {}, | ||
205 | [](int errorCode, const QString &errorMessage) { | ||
206 | qWarning() << "Error while creating resource: " << errorMessage; | ||
207 | }) | ||
208 | .exec(); | ||
209 | } | ||
136 | } | 210 | } |
137 | 211 | ||
138 | void MaildirSettings::remove() | 212 | void MaildirSettings::remove() |
@@ -140,6 +214,13 @@ void MaildirSettings::remove() | |||
140 | if (mIdentifier.isEmpty()) { | 214 | if (mIdentifier.isEmpty()) { |
141 | qWarning() << "We're missing an identifier"; | 215 | qWarning() << "We're missing an identifier"; |
142 | } else { | 216 | } else { |
217 | Sink::ApplicationDomain::SinkResource mailTransportResource("", mMailtransportIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
218 | Sink::Store::remove(mailTransportResource).then<void>([]() {}, | ||
219 | [](int errorCode, const QString &errorMessage) { | ||
220 | qWarning() << "Error while removing resource: " << errorMessage; | ||
221 | }) | ||
222 | .exec(); | ||
223 | |||
143 | Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | 224 | Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); |
144 | Sink::Store::remove(resource).then<void>([]() {}, | 225 | Sink::Store::remove(resource).then<void>([]() {}, |
145 | [](int errorCode, const QString &errorMessage) { | 226 | [](int errorCode, const QString &errorMessage) { |