| 1 | /* ---------------------------------- emailDialog.cpp --------------------------------------------------------------------------- |
| 2 | // Class to echedule an email report after a profile execution |
| 3 | |
| 4 | =============================================================================================================================== |
| 5 | =============================================================================================================================== |
| 6 | This file is part of "luckyBackup" project |
| 7 | Copyright, Loukas Avgeriou |
| 8 | luckyBackup is distributed under the terms of the GNU General Public License |
| 9 | luckyBackup is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | luckyBackup is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with luckyBackup. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | |
| 23 | project version : Please see "main.cpp" for project version |
| 24 | |
| 25 | developer : luckyb |
| 26 | last modified : 22 May 2016 |
| 27 | =============================================================================================================================== |
| 28 | =============================================================================================================================== |
| 29 | */ |
| 30 | |
| 31 | #include "emailDialog.h" |
| 32 | |
| 33 | #include <QSignalMapper> |
| 34 | |
| 35 | #include "textDialog.h" |
| 36 | #include "global.h" |
| 37 | |
| 38 | // class emailDialog Constructor=============================================================================================== |
| 39 | // Displays a dialog to schedule email reports |
| 40 | emailDialog::emailDialog (QDialog *parent) : QDialog (parent) |
| 41 | { |
| 42 | uiE.setupUi(this); |
| 43 | |
| 44 | fillWindow(); |
| 45 | |
| 46 | //connect okay & cancel pushButton SLOTs ---------------- |
| 47 | connect ( uiE.pushButton_ok, SIGNAL( clicked() ), this, SLOT( okay() ) ); |
| 48 | connect ( uiE.pushButton_cancel, SIGNAL( clicked() ), this, SLOT( cancel() ) ); |
| 49 | connect ( uiE.pushButton_test, SIGNAL( clicked() ), this, SLOT( emailTest() ) ); |
| 50 | connect ( uiE.lineEdit_command, SIGNAL( textEdited(QString) ), this, SLOT( enableTLS() ) ); |
| 51 | connect ( uiE.checkBox_disable_tls, SIGNAL( stateChanged(int) ), this, SLOT( enableTLS() ) ); |
| 52 | |
| 53 | //Connect all "revert to default" toolButtons with the revertDefault SLOT |
| 54 | //Map all the toolButton signals and transmit a different integer depending on the toolbutton |
| 55 | QSignalMapper *signalMapper1 = new QSignalMapper(this); |
| 56 | signalMapper1 -> setMapping ( uiE.toolButton_default_command, 0 ); |
| 57 | connect (uiE.toolButton_default_command, SIGNAL (clicked() ), signalMapper1 , SLOT (map())); |
| 58 | signalMapper1 -> setMapping ( uiE.toolButton_default_subject, 1 ); |
| 59 | connect (uiE.toolButton_default_subject, SIGNAL (clicked() ), signalMapper1 , SLOT (map())); |
| 60 | signalMapper1 -> setMapping ( uiE.toolButton_default_body, 2 ); |
| 61 | connect (uiE.toolButton_default_body, SIGNAL (clicked() ), signalMapper1 , SLOT (map())); |
| 62 | connect(signalMapper1, SIGNAL(mapped( const int)), this, SLOT( revertDefault( const int) ) ); |
| 63 | |
| 64 | GoOn = 0; |
| 65 | } |
| 66 | |
| 67 | // SLOTS------------------------------------------------------------------------------------- |
| 68 | // --------------------------------okay pressed------------------------------------------------ |
| 69 | void emailDialog::okay() |
| 70 | { |
| 71 | GoOn = 0; // ok pressed -> Everything good |
| 72 | fillVariables(); |
| 73 | |
| 74 | if (!checkFields()) |
| 75 | { |
| 76 | textDialog textdialogW ("QtWarning", warningMessage, this); |
| 77 | textdialogW.exec(); |
| 78 | return; // do not proceed if something is wrong with the fields |
| 79 | } |
| 80 | |
| 81 | |
| 82 | close(); //emmit a QcloseEvent |
| 83 | } |
| 84 | |
| 85 | // --------------------------------cancel pressed------------------------------------------------ |
| 86 | void emailDialog::cancel() |
| 87 | { |
| 88 | GoOn = 1; // cancel pressed |
| 89 | |
| 90 | close(); //emmit a QcloseEvent |
| 91 | } |
| 92 | |
| 93 | // -------------------------------- closeEvent emitted -------------------------------- |
| 94 | void emailDialog::closeEvent(QCloseEvent *event) |
| 95 | { |
| 96 | event -> accept(); |
| 97 | } |
| 98 | |
| 99 | // revertDefault ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 | //SLOT to revert a field to the default value |
| 101 | void emailDialog::revertDefault(const int field) |
| 102 | { |
| 103 | switch (field) |
| 104 | { |
| 105 | //command |
| 106 | case 0: |
| 107 | if (WINrunning) |
| 108 | { |
| 109 | uiE.lineEdit_command -> setText(emailDefaultWinCommand); |
| 110 | uiE.lineEdit_arguments -> setText(emailDefaultWinArguments); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | uiE.lineEdit_command -> setText(emailDefaultCommand); |
| 115 | uiE.lineEdit_arguments -> setText(emailDefaultArguments); |
| 116 | } |
| 117 | break; |
| 118 | //subject |
| 119 | case 1: |
| 120 | uiE.lineEdit_arg_subject -> setText(emailDefaultSubject); |
| 121 | break; |
| 122 | //body text |
| 123 | case 2: |
| 124 | uiE.textBrowser_arg_body -> setText(emailDefaultBody); |
| 125 | break; |
| 126 | } |
| 127 | enableTLS(); |
| 128 | } |
| 129 | |
| 130 | // Check if all fields are ok |
| 131 | bool emailDialog::checkFields() |
| 132 | { |
| 133 | bool returnVal = true; |
| 134 | if (uiE.checkBox_conditionNever->isChecked()) // if the never condition is checked, just return. No email will be sent anyway |
| 135 | return returnVal; |
| 136 | |
| 137 | warningMessage = ""; |
| 138 | |
| 139 | if (uiE.lineEdit_command -> text() == "") |
| 140 | { |
| 141 | warningMessage.append(tr("The command field is empty") + "!! \n"); |
| 142 | returnVal = false; |
| 143 | } |
| 144 | |
| 145 | /* This is commented because a command with no arguments (eg a script) is possible to be used |
| 146 | if (uiE.lineEdit_arguments -> text() == "") |
| 147 | { |
| 148 | warningMessage.append(tr("The command arguments field is empty") + "!! \n"); |
| 149 | returnVal = false; |
| 150 | }*/ |
| 151 | |
| 152 | return returnVal ; |
| 153 | } |
| 154 | |
| 155 | // fillWindow |
| 156 | // Fill gui fields from variables |
| 157 | void emailDialog::fillWindow() |
| 158 | { |
| 159 | uiE.lineEdit_command -> setText(emailCommand); |
| 160 | uiE.lineEdit_arguments -> setText(emailArguments); |
| 161 | uiE.lineEdit_arg_from -> setText(emailFrom); |
| 162 | uiE.lineEdit_arg_to -> setText(emailTo); |
| 163 | uiE.lineEdit_arg_subject -> setText(emailSubject); |
| 164 | uiE.lineEdit_arg_smtp -> setText(emailSMTP); |
| 165 | uiE.textBrowser_arg_body -> setText(emailBody); |
| 166 | uiE.checkBox_conditionNever -> setChecked(emailNever); |
| 167 | uiE.checkBox_conditionError -> setChecked(emailError); |
| 168 | uiE.checkBox_conditionSchedule -> setChecked(emailSchedule); |
| 169 | uiE.checkBox_disable_tls -> setChecked(emailTLS); |
| 170 | enableTLS(); |
| 171 | } |
| 172 | |
| 173 | // fillVariables |
| 174 | // Fill variables from gui fields |
| 175 | void emailDialog::fillVariables() |
| 176 | { |
| 177 | emailCommand = uiE.lineEdit_command -> text(); |
| 178 | emailArguments = uiE.lineEdit_arguments -> text(); |
| 179 | emailFrom = uiE.lineEdit_arg_from -> text(); |
| 180 | emailTo = uiE.lineEdit_arg_to -> text(); |
| 181 | emailSubject = uiE.lineEdit_arg_subject -> text(); |
| 182 | if (WINrunning) |
| 183 | { |
| 184 | QStringList smtpSrv = (uiE.lineEdit_arg_smtp -> text()).split(":"); |
| 185 | emailSMTP = smtpSrv.first(); |
| 186 | QString smtpPort = "25"; |
| 187 | if (smtpSrv.length()>1) |
| 188 | smtpPort=smtpSrv.at(1); |
| 189 | //QString smtpUser = uiE.lineEdit_arg_smtp_user->text(); // security issues ? |
| 190 | //QString smtpPass = uiE.lineEdit_arg_smtp_pass->text(); // security issues ? |
| 191 | } |
| 192 | else |
| 193 | emailSMTP = uiE.lineEdit_arg_smtp -> text(); |
| 194 | emailBody = uiE.textBrowser_arg_body -> toPlainText(); |
| 195 | emailNever = uiE.checkBox_conditionNever -> isChecked(); |
| 196 | emailError = uiE.checkBox_conditionError -> isChecked(); |
| 197 | emailSchedule = uiE.checkBox_conditionSchedule -> isChecked(); |
| 198 | emailTLS = uiE.checkBox_disable_tls -> isChecked(); |
| 199 | |
| 200 | // Windows TESTING purposes for Juan TESTING~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 201 | /* if (WINrunning) |
| 202 | { |
| 203 | // Execute the email command |
| 204 | QProcess *emailProcess; emailProcess = new QProcess; |
| 205 | emailProcess -> setProcessChannelMode(QProcess::MergedChannels); |
| 206 | QDir::setCurrent(luckyBackupDir); |
| 207 | emailProcess -> setWorkingDirectory(luckyBackupDir); |
| 208 | QStringList args; |
| 209 | if (!smtpUser.isEmpty() && !smtpPass.isEmpty()) |
| 210 | { |
| 211 | args << "-install" << emailSMTP << emailFrom <<"2" << smtpPort <<"-" << smtpUser << smtpPass; |
| 212 | emailProcess -> start ("blat.exe",args); |
| 213 | emailProcess -> waitForFinished(10000); |
| 214 | } |
| 215 | |
| 216 | // Build the return string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 217 | //QString errorOccured = emailProcess -> errorString(); |
| 218 | }*/ |
| 219 | // WINDOWS TESTING END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 220 | } |
| 221 | |
| 222 | // emailTest |
| 223 | // Send an email now, using current gui field values |
| 224 | void emailDialog::emailTest() |
| 225 | { |
| 226 | fillVariables(); |
| 227 | |
| 228 | if (!checkFields()) |
| 229 | { |
| 230 | textDialog textdialogW ("QtWarning", warningMessage, this); |
| 231 | textdialogW.exec(); |
| 232 | return; // do not proceed if something is wrong with the fields (eg command is empty) |
| 233 | } |
| 234 | |
| 235 | warningMessage = sendEmailNow(true); |
| 236 | |
| 237 | textDialog textdialogW ("emailOutput",warningMessage, this); |
| 238 | textdialogW.exec(); |
| 239 | } |
| 240 | |
| 241 | // enableTLS |
| 242 | // enable/disable the TLS option |
| 243 | void emailDialog::enableTLS() |
| 244 | { |
| 245 | QString tlsArgument= "-o tls=no"; |
| 246 | QString tempEmailArguments = uiE.lineEdit_arguments -> text(); |
| 247 | tempEmailArguments.remove(tlsArgument +" "); |
| 248 | tempEmailArguments.remove(" " + tlsArgument); |
| 249 | |
| 250 | //hide or show the "disable TLS" option if "sendemail" is used as command |
| 251 | if (uiE.lineEdit_command -> text() == emailDefaultCommand) |
| 252 | { |
| 253 | uiE.checkBox_disable_tls -> setEnabled(true); |
| 254 | if (uiE.checkBox_disable_tls -> isChecked()) |
| 255 | tempEmailArguments.prepend(tlsArgument + " "); |
| 256 | } |
| 257 | else |
| 258 | uiE.checkBox_disable_tls -> setEnabled(false); |
| 259 | |
| 260 | uiE.lineEdit_arguments -> setText(tempEmailArguments); |
| 261 | } |
| 262 | // --------------------------------accessors----------------------------------- |
| 263 | // Return an integer according to different status |
| 264 | int emailDialog::getGoOn() |
| 265 | { |
| 266 | return GoOn ; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | // end of emailDialog.cpp --------------------------------------------------------------------------- |
| 271 |