OSDN Git Service

Updated comments in some of the code.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / DBEncryptDialog.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.dialog;\r
21 \r
22 //**********************************************\r
23 //**********************************************\r
24 //* Set the database to be encrypted.\r
25 //**********************************************\r
26 //**********************************************\r
27 \r
28 import com.trolltech.qt.gui.QComboBox;\r
29 import com.trolltech.qt.gui.QDialog;\r
30 import com.trolltech.qt.gui.QGridLayout;\r
31 import com.trolltech.qt.gui.QIcon;\r
32 import com.trolltech.qt.gui.QLabel;\r
33 import com.trolltech.qt.gui.QLineEdit;\r
34 import com.trolltech.qt.gui.QPushButton;\r
35 \r
36 public class DBEncryptDialog extends QDialog {\r
37 \r
38         private boolean         okPressed;\r
39         private final QLineEdit password1;\r
40         private final QLineEdit password2;\r
41         private final QPushButton ok;\r
42         private final QComboBox encryptionType;\r
43         private final QLabel encryptionLabel;\r
44         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
45         \r
46         \r
47         // Constructor\r
48         public DBEncryptDialog() {\r
49                 okPressed = false;\r
50                 setWindowTitle(tr("Database Encryption"));\r
51                 setWindowIcon(new QIcon(iconPath+"password.png"));\r
52                 QGridLayout grid = new QGridLayout();\r
53                 setLayout(grid);\r
54                 QGridLayout passwordGrid = new QGridLayout();\r
55                 QGridLayout buttonGrid = new QGridLayout();\r
56                                 \r
57                 password1 = new QLineEdit();\r
58                 password1.setEchoMode(QLineEdit.EchoMode.Password);\r
59                 password2 = new QLineEdit();\r
60                 password2.setEchoMode(QLineEdit.EchoMode.Password);\r
61                 \r
62                 password1.textChanged.connect(this, "validateInput()");\r
63                 password2.textChanged.connect(this, "validateInput()");\r
64                 \r
65                 encryptionLabel = new QLabel(tr("Encryption Method"));\r
66                 encryptionType = new QComboBox();\r
67                 encryptionType.addItem(tr("AES"), "AES");\r
68                 encryptionType.addItem(tr("XTEA"), "XTEA");\r
69                 \r
70                 passwordGrid.addWidget(new QLabel(tr("Password")), 1,1);\r
71                 passwordGrid.addWidget(password1, 1, 2);\r
72                 passwordGrid.addWidget(new QLabel(tr("Verify Password")), 2,1);\r
73                 passwordGrid.addWidget(password2, 2, 2);\r
74                 passwordGrid.addWidget(encryptionLabel, 3,1);\r
75                 passwordGrid.addWidget(encryptionType, 3,2);\r
76                 passwordGrid.setContentsMargins(10, 10,  -10, -10);\r
77                 grid.addLayout(passwordGrid,1,1);\r
78                 \r
79                 \r
80                 \r
81                 ok = new QPushButton(tr("OK"));\r
82                 ok.setEnabled(false);\r
83                 ok.clicked.connect(this, "okButtonPressed()");\r
84                 QPushButton cancel = new QPushButton(tr("Cancel"));\r
85                 cancel.clicked.connect(this, "cancelButtonPressed()");\r
86                 buttonGrid.addWidget(ok, 1, 1);\r
87                 buttonGrid.addWidget(cancel, 1,2);\r
88                 grid.addLayout(buttonGrid,2,1);\r
89         }\r
90         \r
91         public void hideEncryption() {\r
92                 encryptionType.setVisible(false);\r
93                 encryptionLabel.setVisible(false);\r
94         }\r
95         \r
96         public String getEncryptionMethod() {\r
97                 int i = encryptionType.currentIndex();\r
98                 return encryptionType.itemData(i).toString();\r
99         }\r
100         \r
101         // The OK button was pressed\r
102         @SuppressWarnings("unused")\r
103         private void okButtonPressed() {\r
104                 okPressed = true;\r
105                 close();\r
106         }\r
107         \r
108         // The CANCEL button was pressed\r
109         @SuppressWarnings("unused")\r
110         private void cancelButtonPressed() {\r
111                 okPressed = false;\r
112                 close();\r
113         }\r
114         \r
115         // Get the userid from the field\r
116         public String getPassword() {\r
117                 return password1.text();\r
118         }\r
119         \r
120         // Check if the OK button was pressed\r
121         public boolean okPressed() {\r
122                 return okPressed;\r
123         }\r
124         \r
125         // Validate user input\r
126         public void validateInput() {\r
127                 ok.setEnabled(true);\r
128                 if (password1.text().trim().equals("")) {\r
129                         ok.setEnabled(false);\r
130                         return;\r
131                 }       \r
132                 if (password1.text().length() < 4) {\r
133                         ok.setEnabled(false);\r
134                         return;\r
135                 }\r
136                 if (!password1.text().equals(password2.text())) {\r
137                         ok.setEnabled(false);\r
138                         return;\r
139                 }\r
140                 if (password1.text().indexOf(" ") > -1) {\r
141                         ok.setEnabled(false);\r
142                         return;\r
143                 }\r
144         }\r
145 }\r