OSDN Git Service

91093b52abd80d487189ec687e827e513c61311e
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / ConfigDialog.java
1 /*\r
2  * This file is part of NixNote \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                 Global.setIndexNoteBody(indexPage.getIndexNoteBody());\r
177                 Global.setIndexNoteTitle(indexPage.getIndexNoteTitle());\r
178                 Global.setIndexImageRecognition(indexPage.getIndexImageRecognition());\r
179                 Global.setSpecialIndexCharacters(indexPage.getSpecialCharacters());\r
180                 Global.setIncludeTagChildren(appearancePage.getIncludeTagChildren());\r
181                 \r
182         FileOutputStream out = null;\r
183                 try {\r
184                         out = new FileOutputStream(Global.getFileManager().getHomeDirFile("secure.txt"));\r
185                 } catch (FileNotFoundException e) {\r
186                         // if it isn't found we'll write it.\r
187                 }\r
188                 if (out != null)\r
189                         aes.encrypt(out);\r
190                 Global.userStoreUrl = "https://"+debugPage.getServer()+"/edam/user";\r
191                 Global.setWordRegex(indexPage.getRegex());\r
192                 Global.setRecognitionWeight(indexPage.getRecognitionWeight());\r
193                 Global.setIndexThreadSleepInterval(indexPage.getSleepInterval());\r
194                 Global.setMessageLevel( debugPage.getDebugLevel());\r
195                 Global.saveCarriageReturnFix(debugPage.getCarriageReturnFix());\r
196                 Global.enableCarriageReturnFix = debugPage.getCarriageReturnFix();\r
197                 Global.saveHtmlEntitiesFix(debugPage.getHtmlEntitiesFix());\r
198                 Global.enableHTMLEntitiesFix = debugPage.getHtmlEntitiesFix();\r
199                 \r
200                 Global.setSpellSetting(Configuration.SPELL_IGNOREDIGITWORDS, spellPage.getIgnoreDigitWords());\r
201                 Global.setSpellSetting(Configuration.SPELL_IGNOREINTERNETADDRESSES, spellPage.getIgnoreInternetAddresses());\r
202                 Global.setSpellSetting(Configuration.SPELL_IGNOREMIXEDCASE, spellPage.getIgnoreMixedCase());\r
203                 Global.setSpellSetting(Configuration.SPELL_IGNOREUPPERCASE, spellPage.getIgnoreUpperCase());\r
204                 Global.setSpellSetting(Configuration.SPELL_IGNORESENTENCECAPITALIZATION, spellPage.getIgnoreSentenceCapitalization());\r
205                 \r
206                 String guiFormat = appearancePage.getStyle();\r
207                 QApplication.setStyle(guiFormat);\r
208                 QApplication.style().standardPalette();\r
209                 Global.setStyle(guiFormat);\r
210                 Global.setStandardPalette(appearancePage.getStandardPalette());\r
211                 if (Global.useStandardPalette())\r
212                         QApplication.setPalette(QApplication.style().standardPalette());\r
213                 else\r
214                         QApplication.setPalette(Global.originalPalette);\r
215                 \r
216                 String dateFmt = appearancePage.getDateFormat();\r
217                 String timeFmt = appearancePage.getTimeFormat();\r
218                 int dash = dateFmt.indexOf("-");\r
219                 dateFmt = dateFmt.substring(0,dash-1);\r
220                 dash = timeFmt.indexOf("-");\r
221                 timeFmt = timeFmt.substring(0,dash-1);\r
222                 \r
223                 Global.setDateFormat(dateFmt);\r
224                 Global.setTimeFormat(timeFmt);\r
225                 \r
226                 Global.setSyncInterval(connectionPage.getSyncInterval());\r
227                 \r
228                 Global.setOverrideDefaultFont(fontPage.overrideFont());\r
229                 Global.setDefaultFont(fontPage.getFont());\r
230                 Global.setDefaultFontSize(fontPage.getFontSize());\r
231                 Global.setDatabaseCache(debugPage.getDatabaseCacheSize());\r
232                                 \r
233                 close();\r
234         }\r
235         \r
236         \r
237         // Reject the current style\r
238         @Override\r
239         public void reject() {\r
240                 QApplication.setStyle(Global.getStyle());\r
241                 super.reject();\r
242         }\r
243         \r
244         //* return the debugging information page\r
245         public ConfigDebugPage getDebugPage() {\r
246                 return debugPage;\r
247         }\r
248         \r
249         // Get the Evernote connection page\r
250         public ConfigConnectionPage getConfigPage() {\r
251                 return connectionPage;\r
252         }\r
253         \r
254         // Create icons used for navigating the page\r
255         public void createIcons() {\r
256                 String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
257 \r
258                 \r
259                 QListWidgetItem formatsButton = new QListWidgetItem(contentsWidget);\r
260                 formatsButton.setText(tr("Appearance"));\r
261                 formatsButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
262                 formatsButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
263                 formatsButton.setIcon(new QIcon(iconPath+"appearance.jpg"));\r
264                 \r
265                 QListWidgetItem fontButton = new QListWidgetItem(contentsWidget);\r
266                 fontButton.setText(tr("Fonts"));\r
267                 fontButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
268                 fontButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
269                 fontButton.setIcon(new QIcon(iconPath+"fontConfig.png"));\r
270                 \r
271                 QListWidgetItem indexButton = new QListWidgetItem(contentsWidget);\r
272                 indexButton.setText(tr("Indexing"));\r
273                 indexButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
274                 indexButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
275                 indexButton.setIcon(new QIcon(iconPath+"search_config.jpg"));\r
276 \r
277                 QListWidgetItem spellButton = new QListWidgetItem(contentsWidget);\r
278                 spellButton.setText(tr("Spell Check"));\r
279                 spellButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
280                 spellButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
281                 spellButton.setIcon(new QIcon(iconPath+"dictionary.png"));\r
282 \r
283                 QListWidgetItem configButton = new QListWidgetItem(contentsWidget);\r
284                 configButton.setText(tr("Connection"));\r
285                 configButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
286                 configButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
287                 configButton.setIcon(new QIcon(iconPath+"synchronize.png"));\r
288 \r
289                 QListWidgetItem debugButton = new QListWidgetItem(contentsWidget);\r
290                 debugButton.setText(tr("Debugging"));\r
291                 debugButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
292                 debugButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
293                 debugButton.setIcon(new QIcon(iconPath+"debug.jpg"));\r
294                 \r
295                 contentsWidget.currentItemChanged.connect(this, "changePage(QListWidgetItem, QListWidgetItem)");\r
296         }\r
297         \r
298         // this is called when the user switches config pages\r
299         protected void changePage(QListWidgetItem current, QListWidgetItem previous) {\r
300                 pagesWidget.setCurrentIndex(contentsWidget.row(current));\r
301         }\r
302         \r
303         // Load initial settings\r
304         private void loadSettings() {\r
305                 Global.originalPalette = QApplication.palette();\r
306                 \r
307                 debugPage.setServer(Global.getServer());\r
308                 debugPage.setDisableUploads(Global.disableUploads);\r
309                 debugPage.setEnableThumbnails(Global.enableThumbnails());\r
310 //              if (Global.getUpdateSequenceNumber() > 0)\r
311                         debugPage.serverCombo.setEnabled(false);\r
312                 \r
313                 if (Global.username.equalsIgnoreCase("") || Global.password.equalsIgnoreCase("")) {\r
314                 AESEncrypter aes = new AESEncrypter();\r
315                 try {\r
316                                 aes.decrypt(new FileInputStream(Global.getFileManager().getHomeDirFile("secure.txt")));\r
317                         } catch (FileNotFoundException e) {\r
318                                 // File not found, so we'll just get empty strings anyway. \r
319                         }\r
320                         String userid = aes.getUserid();\r
321                         String password = aes.getPassword();\r
322                         if (!userid.equals("") && !password.equals("")) {\r
323                         Global.username = userid;\r
324                         Global.password = password;\r
325                         }                                       \r
326                 }\r
327                 appearancePage.setAutoSaveInterval(Global.getAutoSaveInterval());\r
328                 connectionPage.setUserid(Global.username);\r
329                 connectionPage.setPassword(Global.password);\r
330                 connectionPage.setAutomaticLogin(Global.automaticLogin());\r
331                 connectionPage.setRememberPassword(Global.rememberPassword());\r
332                 appearancePage.setMimicEvernote(Global.getMimicEvernoteInterface());\r
333                 appearancePage.setShowTrayIcon(Global.showTrayIcon());\r
334                 connectionPage.setSynchronizeOnClose(Global.synchronizeOnClose());\r
335                 connectionPage.setSyncronizeDeletedContent(Global.synchronizeDeletedContent());\r
336                 appearancePage.setVerifyDelete(Global.verifyDelete());\r
337                 appearancePage.setStartMinimized(Global.startMinimized());\r
338                 appearancePage.setPdfPreview(Global.pdfPreview());\r
339                 appearancePage.setCheckForUpdates(Global.checkVersionUpgrade());\r
340                 appearancePage.setNewNoteWithTags(Global.newNoteWithSelectedTags());\r
341                 appearancePage.setAnyTagSelection(Global.anyTagSelectionMatch());\r
342                 appearancePage.setShowSplashScreen(Global.isWindowVisible("SplashScreen"));\r
343                 appearancePage.setTagBehavior(Global.tagBehavior());\r
344                 appearancePage.setMinimizeOnClose(Global.minimizeOnClose());\r
345                 appearancePage.setIncludeTagChildren(Global.includeTagChildren());\r
346                 \r
347                 indexPage.setRegex(Global.getWordRegex());\r
348                 indexPage.setSleepInterval(Global.getIndexThreadSleepInterval());\r
349                 connectionPage.setSyncInterval(Global.getSyncInterval());\r
350                 \r
351                 appearancePage.setDateFormat(Global.getDateFormat());\r
352                 appearancePage.setTimeFormat(Global.getTimeFormat());\r
353                 appearancePage.setStyle(Global.getStyle());\r
354                 appearancePage.setStandardPalette(Global.useStandardPalette());\r
355                                                 \r
356                 debugPage.setDebugLevel(Global.getMessageLevel());\r
357                 debugPage.setCarriageReturnFix(Global.enableCarriageReturnFix());\r
358                 debugPage.setHtmlEntitiesFix(Global.enableHTMLEntitiesFix);\r
359                 \r
360         }\r
361         \r
362 }\r