OSDN Git Service

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