File: emaildialog.cpp

package info (click to toggle)
nixnote2 2.0~beta11-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,448 kB
  • ctags: 7,058
  • sloc: cpp: 68,338; java: 1,096; sh: 834; makefile: 27
file content (195 lines) | stat: -rw-r--r-- 6,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2015 Randy Baumgarte

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
***********************************************************************************/


#include "emaildialog.h"
#include <QGridLayout>
#include <QLineEdit>
#include <QListWidget>
#include <QRegExp>
#include "global.h"

extern Global global;

EmailDialog::EmailDialog(QWidget *parent) :
    QDialog(parent)
{
    setWindowIcon(global.getIconResource(":emailIcon"));
    sendPressed = false;
    cancelPressed = true;
    setWindowTitle(tr("Send Email"));

    sendButton = new QPushButton(tr("Send"));
    cancelButton = new QPushButton(tr("Cancel"));

    QGridLayout *grid = new QGridLayout(this);
    setLayout(grid);
    QGridLayout *addressGrid = new QGridLayout();
    QGridLayout *noteGrid = new QGridLayout();
    QGridLayout *buttonGrid = new QGridLayout();
    grid->addLayout(addressGrid, 0,0);
    grid->addLayout(noteGrid, 1,0);
    grid->addLayout(buttonGrid,2,0);

    toLabel = new QLabel(this);
    toLabel->setText(tr("To:"));
    bccLabel = new QLabel(this);
    bccLabel->setText(tr("BCC:"));
    ccLabel = new QLabel(this);
    ccLabel->setText(tr("CC:"));
    subjectLabel = new QLabel(this);
    subjectLabel->setText(tr("Subject:"));
    noteLabel = new QLabel(this);
    noteLabel->setText(tr("Note:"));

    ccSelf = new QCheckBox(this);

    toAddress = new QLineEdit(this);
    ccAddress = new QLineEdit(this);
    bccAddress = new QLineEdit(this);
    subject = new QLineEdit(this);
    note = new QPlainTextEdit(this);

    int row = 0;
    addressGrid->addWidget(toLabel, row, 0);
    addressGrid->addWidget(toAddress, row++, 1);
    addressGrid->addWidget(ccLabel, row, 0);
    addressGrid->addWidget(ccAddress, row++, 1);
    addressGrid->addWidget(bccLabel, row, 0);
    addressGrid->addWidget(bccAddress, row++, 1);

    ccSelf->setText(tr("CC me on this email"));
    noteGrid->addWidget(ccSelf, 0,0);

    addressGrid->addWidget(subjectLabel, row, 0);
    addressGrid->addWidget(subject, row++,1);

    noteGrid->addWidget(noteLabel, 1, 0);
    noteGrid->addWidget(note, 2,0);

    buttonGrid->addWidget(cancelButton, 0,0);
    buttonGrid->addWidget(sendButton, 0,1);

    connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonPressed()));
    connect(sendButton, SIGNAL(clicked()), this, SLOT(sendButtonPressed()));

    connect(toAddress, SIGNAL(textChanged(QString)), this, SLOT(toAddressChanged()));
    connect(ccAddress, SIGNAL(textChanged(QString)), this, SLOT(toAddressChanged()));
    connect(bccAddress, SIGNAL(textChanged(QString)), this, SLOT(toAddressChanged()));
    connect(ccSelf, SIGNAL(toggled(bool)), this, SLOT(toAddressChanged()));
    cancelButton->setAutoDefault(false);
    sendButton->setEnabled(false);
    sendButton->setAutoDefault(true);
    sendButton->setAutoExclusive(true);
    toAddress->setFocus();
}




void EmailDialog::sendButtonPressed() {
    sendPressed = true;
    cancelPressed = false;
    close();
}



void EmailDialog::cancelButtonPressed() {
    cancelPressed = true;
    sendPressed = false;
    close();
}


void EmailDialog::toAddressChanged() {
    QString to, bcc, cc;
    to = toAddress->text().trimmed();
    bcc = bccAddress->text().trimmed();
    cc = ccAddress->text().trimmed();

    // Setup regular expression to test email addresses
    QRegExp mailREX("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
    mailREX.setCaseSensitivity(Qt::CaseInsensitive);
    mailREX.setPatternSyntax(QRegExp::RegExp);

    // Check if "to" address is valid
    bool validTo = true;
    QStringList toList = tokenizeString(to);
    for (int i=0; i<toList.size(); i++) {
        if (!mailREX.exactMatch(toList[i]))
            validTo = false;
    }

    // check if "cc" address is valid
    bool validCc = true;
    QStringList ccList = tokenizeString(cc);
    for (int i=0; i<ccList.size(); i++) {
        if (!mailREX.exactMatch(ccList[i]))
            validCc = false;
    }

    // Check if "bcc" address is valid
    bool validBcc = true;
    QStringList bccList = tokenizeString(bcc);
    for (int i=0; i<bccList.size(); i++) {
        if (!mailREX.exactMatch(bccList[i]))
            validBcc = false;
    }

    // If we have all vaild email addresses or ccSelf is checked, then
    // we can send an email.
    if ((validTo && validCc && validBcc) || ccSelf->isChecked())
        sendButton->setEnabled(true);
    else
        sendButton->setEnabled(false);

    // If no emails are specified and we are not going to cc oursel,
    // then we can't send.
    if (to == "" && cc == "" && bcc == "" && !ccSelf->isChecked())
        sendButton->setEnabled(false);
}



QStringList EmailDialog::tokenizeString(QString value) {
    QStringList values =  value.split(QRegExp(",|;|\\s+"), QString::SkipEmptyParts);

    // There is probably an easier way to do this with regular expressions, but
    // I am horrible at regular expressions.
    for (int i=0; i<values.size(); i++) {
        values[i] = values[i].trimmed();
    }
    return values;
}


QStringList EmailDialog::getToAddresses() {
    return tokenizeString(toAddress->text().trimmed());
}


QStringList EmailDialog::getCcAddresses() {
    return tokenizeString(ccAddress->text().trimmed());
}


QStringList EmailDialog::getBccAddresses() {
    return tokenizeString(bccAddress->text().trimmed());
}