OSDN Git Service

Change generation of files/paths in xml dir to use FileManager
[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 ConfigShowColumnsPage             columnsPage;\r
50         private final ConfigIndexPage                   indexPage;\r
51         private final ConfigShowEditorButtonsPage               editorButtonsPage;\r
52         \r
53         public ConfigDialog(QWidget parent) {\r
54                 \r
55                 contentsWidget = new QListWidget(this);\r
56                 contentsWidget.setViewMode(QListView.ViewMode.IconMode);\r
57                 contentsWidget.setIconSize(new QSize(96, 84));\r
58                 contentsWidget.setMovement(QListView.Movement.Static);\r
59                 contentsWidget.setMaximumWidth(128);\r
60                 contentsWidget.setSpacing(12);\r
61                 \r
62                 pagesWidget = new QStackedWidget(this);\r
63                 connectionPage = new ConfigConnectionPage(this);\r
64                 appearancePage = new ConfigAppearancePage(this);\r
65                 indexPage = new ConfigIndexPage(this);\r
66                 debugPage = new ConfigDebugPage(this);\r
67                 columnsPage = new ConfigShowColumnsPage(this);\r
68                 editorButtonsPage = new ConfigShowEditorButtonsPage(this);\r
69                 pagesWidget.addWidget(appearancePage);\r
70                 pagesWidget.addWidget(indexPage);\r
71                 pagesWidget.addWidget(connectionPage);\r
72                 pagesWidget.addWidget(columnsPage);\r
73                 pagesWidget.addWidget(editorButtonsPage);\r
74                 pagesWidget.addWidget(debugPage);\r
75                 \r
76                 QPushButton cancelButton = new QPushButton(tr("Cancel"));\r
77                 QPushButton okButton = new QPushButton(tr("OK"));\r
78                 okButton.clicked.connect(this, "okPushed()");\r
79                 cancelButton.clicked.connect(this, "close()");\r
80                 \r
81                 createIcons();\r
82                 contentsWidget.setCurrentRow(0);\r
83                 \r
84                 QHBoxLayout horizontalLayout = new QHBoxLayout();\r
85                 horizontalLayout.addWidget(contentsWidget);\r
86                 horizontalLayout.addWidget(pagesWidget,1);\r
87                 \r
88                 QHBoxLayout buttonLayout = new QHBoxLayout();\r
89                 buttonLayout.addStretch(1);\r
90                 buttonLayout.addWidget(okButton);\r
91                 buttonLayout.addWidget(cancelButton);\r
92                 setWindowTitle(tr("Settings")); \r
93                 \r
94                 QVBoxLayout mainLayout = new QVBoxLayout();\r
95                 mainLayout.addLayout(horizontalLayout);\r
96                 mainLayout.addSpacing(1);\r
97                 mainLayout.addLayout(buttonLayout);\r
98                 setLayout(mainLayout);\r
99                 \r
100                 loadSettings();\r
101         }\r
102         public void okPushed() {\r
103                 Global.setServer(debugPage.getServer());\r
104                 AESEncrypter aes = new AESEncrypter();\r
105                 aes.setUserid(connectionPage.getUserid().trim());\r
106                 \r
107                 if (debugPage.getDisableUploads())\r
108                         Global.disableUploads = true;\r
109                 else\r
110                         Global.disableUploads = false;\r
111                 Global.setDisableUploads(Global.disableUploads);\r
112                 Global.setMimicEvernoteInterface(appearancePage.getMimicEvernote());\r
113                 \r
114                 if (appearancePage.getShowSplashScreen())\r
115                         Global.saveWindowVisible("SplashScreen", true);\r
116                 else\r
117                         Global.saveWindowVisible("SplashScreen", false);\r
118                         \r
119                 \r
120                 if (appearancePage.getPdfPreview())\r
121                         Global.setPdfPreview(true);\r
122                 else\r
123                         Global.setPdfPreview(false);\r
124                 \r
125                 Global.setAutoSaveInterval(appearancePage.getAutoSaveInterval());\r
126                                                 \r
127                 Global.setAutomaticLogin(connectionPage.getAutomaticLogin());\r
128                 Global.setRememberPassword(connectionPage.getRememberPassword());\r
129                 if (connectionPage.getRememberPassword()) {     \r
130                         aes.setPassword(connectionPage.getPassword());\r
131                 }\r
132                 Global.setShowTrayIcon(appearancePage.getShowTrayIcon());\r
133                 Global.setVerifyDelete(appearancePage.getVerifyDelete());\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.getDirectoryPath()+"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.setMessageLevel( debugPage.getDebugLevel());\r
152                 Global.saveCarriageReturnFix(debugPage.getCarriageReturnFix());\r
153                 Global.enableCarriageReturnFix = debugPage.getCarriageReturnFix();\r
154                 \r
155                 String guiFormat = appearancePage.getStyle();\r
156                 QApplication.setStyle(guiFormat);\r
157                 QApplication.style().standardPalette();\r
158                 Global.setStyle(guiFormat);\r
159                 Global.setStandardPalette(appearancePage.getStandardPalette());\r
160                 if (Global.useStandardPalette())\r
161                         QApplication.setPalette(QApplication.style().standardPalette());\r
162                 else\r
163                         QApplication.setPalette(Global.originalPalette);\r
164                 \r
165                 String dateFmt = appearancePage.getDateFormat();\r
166                 String timeFmt = appearancePage.getTimeFormat();\r
167                 int dash = dateFmt.indexOf("-");\r
168                 dateFmt = dateFmt.substring(0,dash-1);\r
169                 dash = timeFmt.indexOf("-");\r
170                 timeFmt = timeFmt.substring(0,dash-1);\r
171                 \r
172                 Global.setDateFormat(dateFmt);\r
173                 Global.setTimeFormat(timeFmt);\r
174                 \r
175                 Global.setSyncInterval(connectionPage.getSyncInterval());\r
176                 \r
177                 Global.saveColumnVisible("dateCreated", columnsPage.showDateCreated());\r
178                 Global.saveColumnVisible("dateChanged", columnsPage.showDateChanged());\r
179                 Global.saveColumnVisible("dateSubject", columnsPage.showDateSubject());\r
180                 Global.saveColumnVisible("author", columnsPage.showAuthor());\r
181                 Global.saveColumnVisible("sourceUrl", columnsPage.showSourceUrl());\r
182                 Global.saveColumnVisible("synchronized", columnsPage.showSynchronized());\r
183                 Global.saveColumnVisible("notebook", columnsPage.showNotebook());\r
184                 Global.saveColumnVisible("tags", columnsPage.showTags());\r
185                 \r
186                 Global.saveEditorButtonsVisible("undo", editorButtonsPage.showUndo());\r
187                 Global.saveEditorButtonsVisible("redo", editorButtonsPage.showRedo());\r
188                 Global.saveEditorButtonsVisible("cut", editorButtonsPage.showCut());\r
189                 Global.saveEditorButtonsVisible("copy", editorButtonsPage.showCopy());\r
190                 Global.saveEditorButtonsVisible("paste", editorButtonsPage.showPaste());\r
191                 Global.saveEditorButtonsVisible("underline", editorButtonsPage.showUnderline());\r
192                 Global.saveEditorButtonsVisible("strikethrough", editorButtonsPage.showStrikethrough());\r
193                 Global.saveEditorButtonsVisible("italic", editorButtonsPage.showItalic());\r
194                 Global.saveEditorButtonsVisible("bold", editorButtonsPage.showBold());\r
195                 Global.saveEditorButtonsVisible("font", editorButtonsPage.showFont());\r
196                 Global.saveEditorButtonsVisible("fontSize", editorButtonsPage.showFontSize());\r
197                 Global.saveEditorButtonsVisible("fontColor", editorButtonsPage.showFontColor());\r
198                 Global.saveEditorButtonsVisible("fontHilight", editorButtonsPage.showFontHilight());\r
199                 Global.saveEditorButtonsVisible("indent", editorButtonsPage.showIndent());\r
200                 Global.saveEditorButtonsVisible("outdent", editorButtonsPage.showOutdent());\r
201                 Global.saveEditorButtonsVisible("numberList", editorButtonsPage.showNumberList());\r
202                 Global.saveEditorButtonsVisible("bulletList", editorButtonsPage.showBulletList());\r
203                 Global.saveEditorButtonsVisible("alignCenter", editorButtonsPage.showAlignCenter());\r
204                 Global.saveEditorButtonsVisible("alignLeft", editorButtonsPage.showAlignLeft());\r
205                 Global.saveEditorButtonsVisible("alignRight", editorButtonsPage.showAlignRight());\r
206                 close();\r
207         }\r
208         @Override\r
209         public void reject() {\r
210                 QApplication.setStyle(Global.getStyle());\r
211                 super.reject();\r
212         }\r
213         \r
214         public ConfigDebugPage getDebugPage() {\r
215                 return debugPage;\r
216         }\r
217         \r
218         \r
219         public ConfigConnectionPage getConfigPage() {\r
220                 return connectionPage;\r
221         }\r
222         \r
223         public void createIcons() {\r
224                 String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
225 \r
226                 \r
227                 QListWidgetItem formatsButton = new QListWidgetItem(contentsWidget);\r
228                 formatsButton.setText(tr("Appearance"));\r
229                 formatsButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
230                 formatsButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
231                 formatsButton.setIcon(new QIcon(iconPath+"appearance.jpg"));\r
232                 \r
233                 QListWidgetItem indexButton = new QListWidgetItem(contentsWidget);\r
234                 indexButton.setText(tr("Indexing"));\r
235                 indexButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
236                 indexButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
237                 indexButton.setIcon(new QIcon(iconPath+"search_config.jpg"));\r
238 \r
239                 QListWidgetItem configButton = new QListWidgetItem(contentsWidget);\r
240                 configButton.setText(tr("Connection"));\r
241                 configButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
242                 configButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
243                 configButton.setIcon(new QIcon(iconPath+"synchronize.png"));\r
244                 \r
245                 QListWidgetItem columnsButton = new QListWidgetItem(contentsWidget);\r
246                 columnsButton.setText(tr("Hide Columns"));\r
247                 columnsButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
248                 columnsButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
249                 columnsButton.setIcon(new QIcon(iconPath+"show-columns.png"));\r
250 \r
251                 QListWidgetItem editorButton = new QListWidgetItem(contentsWidget);\r
252                 editorButton.setText(tr("Hide Edit Buttons"));\r
253                 editorButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
254                 editorButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
255                 editorButton.setIcon(new QIcon(iconPath+"scissors.jpg"));\r
256 \r
257                 QListWidgetItem debugButton = new QListWidgetItem(contentsWidget);\r
258                 debugButton.setText(tr("Debugging"));\r
259                 debugButton.setTextAlignment(AlignmentFlag.AlignHCenter.value());\r
260                 debugButton.setFlags(ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEnabled);\r
261                 debugButton.setIcon(new QIcon(iconPath+"debug.jpg"));\r
262                 \r
263                 contentsWidget.currentItemChanged.connect(this, "changePage(QListWidgetItem, QListWidgetItem)");\r
264         }\r
265         \r
266         protected void changePage(QListWidgetItem current, QListWidgetItem previous) {\r
267                 pagesWidget.setCurrentIndex(contentsWidget.row(current));\r
268         }\r
269         \r
270         private void loadSettings() {\r
271                 Global.originalPalette = QApplication.palette();\r
272                 \r
273                 debugPage.setServer(Global.getServer());\r
274                 debugPage.setDisableUploads(Global.disableUploads);\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.getDirectoryPath()+"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.setPdfPreview(Global.pdfPreview());\r
303                 appearancePage.setShowSplashScreen(Global.isWindowVisible("SplashScreen"));\r
304                 appearancePage.setTagBehavior(Global.tagBehavior());\r
305                 \r
306                 indexPage.setRegex(Global.getWordRegex());\r
307                 indexPage.setWordLength(Global.getMinimumWordLength());\r
308                 indexPage.setIndexThreads(Global.getIndexThreads());\r
309                 connectionPage.setSyncInterval(Global.getSyncInterval());\r
310                 \r
311                 appearancePage.setDateFormat(Global.getDateFormat());\r
312                 appearancePage.setTimeFormat(Global.getTimeFormat());\r
313                 appearancePage.setStyle(Global.getStyle());\r
314                 appearancePage.setStandardPalette(Global.useStandardPalette());\r
315                 \r
316                 columnsPage.setDateCreated(Global.isColumnVisible("dateCreated"));\r
317                 columnsPage.setDateSubject(Global.isColumnVisible("dateSubject"));\r
318                 columnsPage.setDateChanged(Global.isColumnVisible("dateChanged"));\r
319                 columnsPage.setAuthor(Global.isColumnVisible("author"));\r
320                 columnsPage.setSourceUrl(Global.isColumnVisible("sourceUrl"));\r
321                 columnsPage.setTags(Global.isColumnVisible("tags"));\r
322                 columnsPage.setSynchronized(Global.isColumnVisible("synchronized"));\r
323                 columnsPage.setNotebook(Global.isColumnVisible("notebook"));\r
324                 \r
325                 editorButtonsPage.setUndo(Global.isEditorButtonVisible("undo"));\r
326                 editorButtonsPage.setRedo(Global.isEditorButtonVisible("redo"));\r
327                 editorButtonsPage.setCut(Global.isEditorButtonVisible("cut"));\r
328                 editorButtonsPage.setCopy(Global.isEditorButtonVisible("copy"));\r
329                 editorButtonsPage.setPaste(Global.isEditorButtonVisible("paste"));\r
330                 editorButtonsPage.setBold(Global.isEditorButtonVisible("bold"));\r
331                 editorButtonsPage.setItalic(Global.isEditorButtonVisible("italic"));\r
332                 editorButtonsPage.setUnderline(Global.isEditorButtonVisible("underline"));\r
333                 editorButtonsPage.setStrikethrough(Global.isEditorButtonVisible("strikethrough"));\r
334                 editorButtonsPage.setIndent(Global.isEditorButtonVisible("indent"));\r
335                 editorButtonsPage.setHline(Global.isEditorButtonVisible("hline"));\r
336                 editorButtonsPage.setOutdent(Global.isEditorButtonVisible("outdent"));\r
337                 editorButtonsPage.setBulletList(Global.isEditorButtonVisible("bulletList"));\r
338                 editorButtonsPage.setNumberList(Global.isEditorButtonVisible("numberList"));\r
339                 editorButtonsPage.setFont(Global.isEditorButtonVisible("font"));\r
340                 editorButtonsPage.setFontSize(Global.isEditorButtonVisible("fontSize"));\r
341                 editorButtonsPage.setFontColor(Global.isEditorButtonVisible("fontColor"));\r
342                 editorButtonsPage.setFontHighlight(Global.isEditorButtonVisible("fontHilight"));\r
343                 editorButtonsPage.setAlignLeft(Global.isEditorButtonVisible("alignLeft"));\r
344                 editorButtonsPage.setAlignCenter(Global.isEditorButtonVisible("alignCenter"));\r
345                 editorButtonsPage.setAlignRight(Global.isEditorButtonVisible("alignRight"));\r
346                 \r
347                 debugPage.setDebugLevel(Global.getMessageLevel());\r
348                 debugPage.setCarriageReturnFix(Global.enableCarriageReturnFix());\r
349                 \r
350         }\r
351         \r
352 }\r