OSDN Git Service

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