File indexing completed on 2025-01-19 04:51:55

0001 /*
0002  *  Copyright (C) 2017 Michael Bohlender, <michael.bohlender@kdemail.net>
0003  *
0004  *  This program is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation; either version 2 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License along
0015  *  with this program; if not, write to the Free Software Foundation, Inc.,
0016  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  */
0018 
0019 #include "gmailcontroller.h"
0020 
0021 #include <sink/store.h>
0022 
0023 using namespace Sink;
0024 using namespace Sink::ApplicationDomain;
0025 
0026 GmailController::GmailController() : Kube::Controller(),
0027 action_create{new Kube::ControllerAction{this, &GmailController::create}},
0028 action_modify{new Kube::ControllerAction{this, &GmailController::modify}},
0029 action_remove{new Kube::ControllerAction{this, &GmailController::remove}}
0030 {
0031 
0032 }
0033 
0034 void GmailController::create() {
0035 
0036     //account
0037     auto account = ApplicationDomainType::createEntity<SinkAccount>();
0038     account.setProperty("type", "imap");
0039     account.setProperty("name", getName());
0040     Store::create(account).exec().waitForFinished();
0041 
0042     //imap
0043     auto resource =  ApplicationDomainType::createEntity<SinkResource>();
0044     resource.setResourceType("sink.imap");
0045     resource.setAccount(account);
0046     resource.setProperty("server","imaps://imap.gmail.com:993");
0047     resource.setProperty("username", getEmailAddress());
0048     resource.setProperty("password", getPassword());
0049     Store::create(resource).exec().waitForFinished();
0050 
0051     //smtp
0052     resource = ApplicationDomainType::createEntity<SinkResource>();
0053     resource.setResourceType("sink.mailtransport");
0054     resource.setAccount(account);
0055     resource.setProperty("server", "smtps://smtp.gmail.com:465");
0056     resource.setProperty("username", getEmailAddress());
0057     resource.setProperty("password", getPassword());
0058     Store::create(resource).exec().waitForFinished();
0059 
0060     //identity
0061     auto identity = ApplicationDomainType::createEntity<Identity>();
0062     m_identityId = identity.identifier();
0063     identity.setAccount(account);
0064     identity.setName(getIdentityName());
0065     identity.setAddress(getEmailAddress());
0066     Store::create(identity).exec();
0067 }
0068 
0069 void GmailController::modify() {
0070     //TODO
0071 }
0072 
0073 void GmailController::remove() {
0074     SinkAccount account(m_accountId);
0075     Store::remove(account).exec();
0076 }
0077 
0078 void GmailController::load(const QByteArray &id) {
0079 
0080     m_accountId = id;
0081 
0082     Store::fetchOne<SinkAccount>(Query().filter(m_accountId))
0083     .then([this](const SinkAccount &account) {
0084         setName(account.getName());
0085     }).exec();
0086 
0087     //TODO
0088 }
0089