OSDN Git Service

Add option to start minimized.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / ConfigDialog.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 import java.io.FileInputStream;\r
23 import java.io.FileNotFoundException;\r
24 import java.io.FileOutputStream;\r
25 \r
26 import com.trolltech.qt.core.QSize;\r
27 import com.trolltech.qt.core.Qt.AlignmentFlag;\r
28 import com.trolltech.qt.core.Qt.ItemFlag;\r
29 import com.trolltech.qt.gui.QApplication;\r
30 import com.trolltech.qt.gui.QDialog;\r
31 import com.trolltech.qt.gui.QHBoxLayout;\r
32 import com.trolltech.qt.gui.QIcon;\r
33 import com.trolltech.qt.gui.QListView;\r
34 import com.trolltech.qt.gui.QListWidget;\r
35 import com.trolltech.qt.gui.QListWidgetItem;\r
36 import com.trolltech.qt.gui.QPushButton;\r
37 import com.trolltech.qt.gui.QStackedWidget;\r
38 import com.trolltech.qt.gui.QVBoxLayout;\r
39 import com.trolltech.qt.gui.QWidget;\r
40 \r
41 import cx.fbn.nevernote.Global;\r
42 import cx.fbn.nevernote.utilities.AESEncrypter;\r
43 public class ConfigDialog extends QDialog {\r
44         private final QListWidget                               contentsWidget;\r
45         private final QStackedWidget                    pagesWidget;\r
46         private final ConfigConnectionPage              connectionPage;\r
47         private final ConfigDebugPage                   debugPage;\r
48         private final ConfigAppearancePage              appearancePage;\r
49         private final ConfigIndexPage                   indexPage;\r
50         \r
51         public ConfigDialog(QWidget parent) {\r
52                 \r
53                 contentsWidget = new QListWidget(this);\r
54                 contentsWidget.setViewMode(QListView.ViewMode.IconMode);\r
55                 contentsWidget.setIconSize(new QSize(96, 84));\r
56                 contentsWidget.setMovement(QListView.Movement.Static);\r
57                 contentsWidget.setMaximumWidth(128);\r
58                 contentsWidget.setSpacing(12);\r
59                 \r
60                 pagesWidget = new QStackedWidget(this);\r
61                 connectionPage = new ConfigConnectionPage(this);\r
62                 appearancePage = new ConfigAppearancePage(this);\r
63                 indexPage = new ConfigIndexPage(this);\r
64                 debugPage = new ConfigDebugPage(this);\r
65                 pagesWidget.addWidget(appearancePage);\r
66                 pagesWidget.addWidget(indexPage);\r
67                 pagesWidget.addWidget(connectionPage);\r
68                 pagesWidget.addWidget(debugPage);\r
69                 \r
70                 QPushButton cancelButton = new QPushButton(tr("Cancel"));\r
71                 QPushButton okButton = new QPushButton(tr("OK"));\r
72                 okButton.clicked.connect(this, "okPushed()");\r
73                 cancelButton.clicked.connect(this, "close()");\r
74                 \r
75                 createIcons();\r
76                 contentsWidget.setCurrentRow(0);\r
77                 \r
78                 QHBoxLayout horizontalLayout = new QHBoxLayout();\r
79                 horizontalLayout.addWidget(contentsWidget);\r
80                 horizontalLayout.addWidget(pagesWidget,1);\r
81                 \r
82                 QHBoxLayout buttonLayout = new QHBoxLayout();\r
83                 buttonLayout.addStretch(1);\r
84                 buttonLayout.addWidget(okButton);\r
85                 buttonLayout.addWidget(cancelButton);\r
86                 setWindowTitle(tr("Settings")); \r
87                 \r
88                 QVBoxLayout mainLayout = new QVBoxLayout();\r
89                 mainLayout.addLayout(horizontalLayout);\r
90                 mainLayout.addSpacing(1);\r
91                 mainLayout.addLayout(buttonLayout);\r
92                 setLayout(mainLayout);\r
93                 \r
94                 loadSettings();\r
95         }\r
96         public void okPushed() {\r
97                 Global.setServer(debugPage.getServer());\r
98                 AESEncrypter aes = new AESEncrypter();\r
99                 aes.setUserid(connectionPage.getUserid().trim());\r
100                 \r
101                 if (debugPage.getDisableUploads())\r
102                         Global.disableUploads = true;\r
103                 else\r
104                         Global.disableUploads = false;\r
105                 Global.setDisableUploads(Global.disableUploads);\r
106                 Global.setMimicEvernoteInterface(appearancePage.getMimicEvernote());\r
107                 \r
108                 if (appearancePage.getShowSplashScreen())\r
109                         Global.saveWindowVisible("SplashScreen", true);\r
110                 else\r
111                         Global.saveWindowVisible("SplashScreen", false);\r
112                         \r
113                 \r
114                 if (appearancePage.getPdfPreview())\r
115                         Global.setPdfPreview(true);\r
116                 else\r
117                         Global.setPdfPreview(false);\r
118                 \r
119                 if (appearancePage.getNewNoteWithTags())\r
120                         Global.setNewNoteWithSelectedTags(true);\r
121                 else\r
122                         Global.setNewNoteWithSelectedTags(false);\r
123                 \r
124                 Global.setAutoSaveInterval(appearancePage.getAutoSaveInterval());\r
125                                                 \r
126                 Global.setAutomaticLogin(connectionPage.getAutomaticLogin());\r
127                 Global.setRememberPassword(connectionPage.getRememberPassword());\r
128                 if (connectionPage.getRememberPassword()) {     \r
129                         aes.setPassword(connectionPage.getPassword());\r
130                 }\r
131                 Global.setShowTrayIcon(appearancePage.getShowTrayIcon());\r
132                 Global.setVerifyDelete(appearancePage.getVerifyDelete());\r
133                 Global.setStartMinimized(appearancePage.getStartMinimized());\r
134                 Global.setSynchronizeOnClose(connectionPage.getSynchronizeOnClose());\r
135                 Global.setSynchronizeDeletedContent(connectionPage.getSynchronizeDeletedContent());\r
136                 Global.setTagBehavior(appearancePage.getTagBehavior());\r
137         FileOutputStream out = null;\r
138                 try {\r
139                         out = new FileOutputStream(Global.getFileManager().getHomeDirFile("secure.txt"));\r
140                 } catch (FileNotFoundException e) {\r
141                         // if it isn't found we'll write it.\r
142                 }\r
143                 if (out != null)\r
144                         aes.encrypt(out);\r
145                 Global.userStoreUrl = "https://"+debugPage.getServer()+"/edam/user";\r
146                 Global.setWordRegex(indexPage.getRegex());\r
147                 Global.setRecognitionWeight(indexPage.getRecognitionWeight());\r
148                 Global.setMinimumWordLength(indexPage.getWordLength());\r
149                 Global.minimumWordCount = indexPage.getWordLength();    \r
150                 Global.setIndexThreads(indexPage.getIndexThreads());\r
151                 Global.setIndexThreadSleepInterval(indexPage.getSleepInterval());\r
152                 Global.setMessageLevel( debugPage.getDebugLevel());\r
153                 Global.saveCarriageReturnFix(debugPage.getCarriageReturnFix());\r
154                 Global.enableCarriageReturnFix = debugPage.getCarriageReturnFix();\r
155                 \r
156                 String guiFormat = appearancePage.getStyle();\r
157                 QApplication.setStyle(guiFormat);\r
158                 QApplication.style().standardPalette();\r
159                 Global.setStyle(guiFormat);\r
160                 Global.setStandardPalette(appearancePage.getStandardPalette());\r
161                 if (Global.useStandardPalette())\r
162                         QApplication.setPalette(QApplication.style().standardPalette());\r
163                 else\r
164                         QApplication.setPalette(Global.originalPalette);\r
165                 \r
166                 String dateFmt = appearancePage.getDateFormat();\r
167                 String timeFmt = appearancePage.getTimeFormat();\r
168                 int dash = dateFmt.indexOf("-");\r
169                 dateFmt = dateFmt.substring(0,dash-1);\r
170                 dash = timeFmt.indexOf("-");\r
171                 timeFmt = timeFmt.substring(0,dash-1);\r
172                 \r
173                 Global.setDateFormat(dateFmt);\r
174                 Global.setTimeFormat(timeFmt);\r
175                 \r
176                 Global.setSyncInterval(connectionPage.getSyncInterval());\r
177                                 \r
178                 close();\r
179         }\r
180         @Override\r
181         public void reject() {\r
182                 QApplication.setStyle(Global.getStyle());\r
183                 super.reject();\r
184         }\r
185         \r
186         public ConfigDebugPage getDebugPage() {\r
187                 return debugPage;\r
188         }\r
189         \r
190         \r
191         public ConfigConnectionPage getConfigPage() {\r
192                 return connectionPage;\r
193         }\r
194         \r
195         public void createIcons() {\r
196                 String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
197 \r
198                 \r
199                 QListWidgetItem formatsButton = new QListWidgetItem(contentsWidget);\r
200                 formatsButton.setText(tr("Appearance"));\r
201                 formatsButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
202                 formatsButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
203                 formatsButton.setIcon(new QIcon(iconPath+"appearance.jpg"));\r
204                 \r
205                 QListWidgetItem indexButton = new QListWidgetItem(contentsWidget);\r
206                 indexButton.setText(tr("Indexing"));\r
207                 indexButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
208                 indexButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
209                 indexButton.setIcon(new QIcon(iconPath+"search_config.jpg"));\r
210 \r
211                 QListWidgetItem configButton = new QListWidgetItem(contentsWidget);\r
212                 configButton.setText(tr("Connection"));\r
213                 configButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
214                 configButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
215                 configButton.setIcon(new QIcon(iconPath+"synchronize.png"));\r
216 \r
217                 QListWidgetItem debugButton = new QListWidgetItem(contentsWidget);\r
218                 debugButton.setText(tr("Debugging"));\r
219                 debugButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
220                 debugButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
221                 debugButton.setIcon(new QIcon(iconPath+"debug.jpg"));\r
222                 \r
223                 contentsWidget.currentItemChanged.connect(this, "changePage(QListWidgetItem, QListWidgetItem)");\r
224         }\r
225         \r
226         protected void changePage(QListWidgetItem current, QListWidgetItem previous) {\r
227                 pagesWidget.setCurrentIndex(contentsWidget.row(current));\r
228         }\r
229         \r
230         private void loadSettings() {\r
231                 Global.originalPalette = QApplication.palette();\r
232                 \r
233                 debugPage.setServer(Global.getServer());\r
234                 debugPage.setDisableUploads(Global.disableUploads);\r
235 //              if (Global.getUpdateSequenceNumber() > 0)\r
236                         debugPage.serverCombo.setEnabled(false);\r
237                 \r
238                 if (Global.username.equalsIgnoreCase("") || Global.password.equalsIgnoreCase("")) {\r
239                 AESEncrypter aes = new AESEncrypter();\r
240                 try {\r
241                                 aes.decrypt(new FileInputStream(Global.getFileManager().getHomeDirFile("secure.txt")));\r
242                         } catch (FileNotFoundException e) {\r
243                                 // File not found, so we'll just get empty strings anyway. \r
244                         }\r
245                         String userid = aes.getUserid();\r
246                         String password = aes.getPassword();\r
247                         if (!userid.equals("") && !password.equals("")) {\r
248                         Global.username = userid;\r
249                         Global.password = password;\r
250                         }                                       \r
251                 }\r
252                 appearancePage.setAutoSaveInterval(Global.getAutoSaveInterval());\r
253                 connectionPage.setUserid(Global.username);\r
254                 connectionPage.setPassword(Global.password);\r
255                 connectionPage.setAutomaticLogin(Global.automaticLogin());\r
256                 connectionPage.setRememberPassword(Global.rememberPassword());\r
257                 appearancePage.setMimicEvernote(Global.getMimicEvernoteInterface());\r
258                 appearancePage.setShowTrayIcon(Global.showTrayIcon());\r
259                 connectionPage.setSynchronizeOnClose(Global.synchronizeOnClose());\r
260                 connectionPage.setSyncronizeDeletedContent(Global.synchronizeDeletedContent());\r
261                 appearancePage.setVerifyDelete(Global.verifyDelete());\r
262                 appearancePage.setStartMinimized(Global.startMinimized());\r
263                 appearancePage.setPdfPreview(Global.pdfPreview());\r
264                 appearancePage.setNewNoteWithTags(Global.newNoteWithSelectedTags());\r
265                 appearancePage.setShowSplashScreen(Global.isWindowVisible("SplashScreen"));\r
266                 appearancePage.setTagBehavior(Global.tagBehavior());\r
267                 \r
268                 indexPage.setRegex(Global.getWordRegex());\r
269                 indexPage.setWordLength(Global.getMinimumWordLength());\r
270                 indexPage.setIndexThreads(Global.getIndexThreads());\r
271                 indexPage.setSleepInterval(Global.getIndexThreadSleepInterval());\r
272                 connectionPage.setSyncInterval(Global.getSyncInterval());\r
273                 \r
274                 appearancePage.setDateFormat(Global.getDateFormat());\r
275                 appearancePage.setTimeFormat(Global.getTimeFormat());\r
276                 appearancePage.setStyle(Global.getStyle());\r
277                 appearancePage.setStandardPalette(Global.useStandardPalette());\r
278                                                 \r
279                 debugPage.setDebugLevel(Global.getMessageLevel());\r
280                 debugPage.setCarriageReturnFix(Global.enableCarriageReturnFix());\r
281                 \r
282         }\r
283         \r
284 }\r