OSDN Git Service

ff0f5e001330f60e0b6ba2b866d713fae7132411
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / ExternalBrowse.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.gui;\r
21 \r
22 import java.awt.Desktop;\r
23 import java.util.List;\r
24 \r
25 import com.trolltech.qt.core.QUrl;\r
26 import com.trolltech.qt.core.Qt.WidgetAttribute;\r
27 import com.trolltech.qt.gui.QAction;\r
28 import com.trolltech.qt.gui.QCloseEvent;\r
29 import com.trolltech.qt.gui.QDesktopServices;\r
30 import com.trolltech.qt.gui.QDialog;\r
31 import com.trolltech.qt.gui.QMdiSubWindow;\r
32 import com.trolltech.qt.gui.QPrintDialog;\r
33 import com.trolltech.qt.gui.QPrinter;\r
34 \r
35 import cx.fbn.nevernote.Global;\r
36 import cx.fbn.nevernote.dialog.FindDialog;\r
37 import cx.fbn.nevernote.sql.DatabaseConnection;\r
38 \r
39 public class ExternalBrowse extends QMdiSubWindow {\r
40         private final DatabaseConnection  conn;\r
41         private final BrowserWindow     browser;\r
42         public Signal4<String, String, Boolean, BrowserWindow> contentsChanged;\r
43         public Signal1<String>  windowClosing;\r
44         boolean noteDirty;\r
45         private QAction editFind;\r
46         private final FindDialog        find;                                           // Text search in note dialog\r
47 //      ExternalBrowserMenuBar          menu;\r
48         ExternalBrowserMenuBar  menu;\r
49         \r
50         // Constructor\r
51         public ExternalBrowse(DatabaseConnection c) {\r
52                 setAttribute(WidgetAttribute.WA_QuitOnClose, false);\r
53                 setWindowTitle(tr("NeverNote"));\r
54                 conn = c;\r
55                 contentsChanged = new Signal4<String, String, Boolean, BrowserWindow>();\r
56                 windowClosing = new Signal1<String>();\r
57                 browser = new BrowserWindow(conn);\r
58                 menu = new ExternalBrowserMenuBar(this);\r
59                 for (int i=0; i<menu.actions().size(); i++) {\r
60                         addAction(menu.actions().get(i));\r
61                 }\r
62                 \r
63                 setWidget(browser);\r
64                 noteDirty = false;\r
65                 browser.titleLabel.textChanged.connect(this, "titleChanged(String)");\r
66                 browser.getBrowser().page().contentsChanged.connect(this, "contentChanged()");\r
67                 find = new FindDialog();\r
68                 find.getOkButton().clicked.connect(this, "doFindText()");\r
69         }\r
70         \r
71         @SuppressWarnings("unused")\r
72         private void contentChanged() {\r
73                 noteDirty = true;\r
74                 contentsChanged.emit(getBrowserWindow().getNote().getGuid(), getBrowserWindow().getContent(), false, getBrowserWindow());\r
75         }\r
76 \r
77         \r
78         @Override\r
79         public void closeEvent(QCloseEvent event) {\r
80                 if (noteDirty) \r
81                         contentsChanged.emit(getBrowserWindow().getNote().getGuid(), getBrowserWindow().getContent(), true, getBrowserWindow());\r
82                 windowClosing.emit(getBrowserWindow().getNote().getGuid());\r
83         }\r
84         \r
85     public BrowserWindow getBrowserWindow() {\r
86         return browser;\r
87     }\r
88     \r
89     @SuppressWarnings("unused")\r
90         private void titleChanged(String value) {\r
91         setWindowTitle(tr("NeverNote - ") +value);\r
92     }\r
93     \r
94         @SuppressWarnings("unused")\r
95         private void updateTitle(String guid, String title) {\r
96                 if (guid.equals(getBrowserWindow().getNote().getGuid())) {\r
97                         getBrowserWindow().loadingData(true);\r
98                         getBrowserWindow().setTitle(title);\r
99                         getBrowserWindow().getNote().setTitle(title);\r
100                         getBrowserWindow().loadingData(false);\r
101                 }\r
102         }\r
103         @SuppressWarnings("unused")\r
104         private void updateNotebook(String guid, String notebook) {\r
105                 if (guid.equals(getBrowserWindow().getNote().getGuid())) {\r
106                         getBrowserWindow().loadingData(true);\r
107                         getBrowserWindow().setNotebook(notebook);\r
108                         getBrowserWindow().loadingData(false);\r
109                 }\r
110         }\r
111         \r
112         @SuppressWarnings("unused")\r
113         private void updateTags(String guid, List<String> tags) {\r
114                 if (guid.equals(getBrowserWindow().getNote().getGuid())) {\r
115                         StringBuffer tagLine = new StringBuffer(100);\r
116                         for (int i=0; i<tags.size(); i++) {\r
117                                 if (i>0)\r
118                                         tagLine.append(Global.tagDelimeter+" ");\r
119                                 tagLine.append(tags.get(i));\r
120                                 \r
121                         }\r
122                         getBrowserWindow().loadingData(true);\r
123                         getBrowserWindow().getTagLine().setText(tagLine.toString());\r
124                         getBrowserWindow().loadingData(false);\r
125                 }\r
126         }\r
127 \r
128         \r
129     @SuppressWarnings("unused")\r
130         private void findText() {\r
131         find.show();\r
132         find.setFocusOnTextField();\r
133     }\r
134     @SuppressWarnings("unused")\r
135         private void doFindText() {\r
136         browser.getBrowser().page().findText(find.getText(), find.getFlags());\r
137         find.setFocus();\r
138     }\r
139 \r
140         \r
141     @SuppressWarnings("unused")\r
142         private void printNote() {\r
143 \r
144         QPrintDialog dialog = new QPrintDialog();\r
145         if (dialog.exec() == QDialog.DialogCode.Accepted.value()) {\r
146                 QPrinter printer = dialog.printer();\r
147                 browser.getBrowser().print(printer);\r
148         }\r
149     }\r
150     \r
151     // Listener triggered when the email button is pressed\r
152     @SuppressWarnings("unused")\r
153         private void emailNote() {\r
154         if (Desktop.isDesktopSupported()) {\r
155             Desktop desktop = Desktop.getDesktop();\r
156             \r
157             String text2 = browser.getContentsToEmail();\r
158             QUrl url = new QUrl("mailto:");\r
159             url.addQueryItem("subject", browser.getTitle());\r
160             url.addQueryItem("body", text2);\r
161             QDesktopServices.openUrl(url);\r
162         }\r
163     }\r
164 }\r