2 * This file is part of NixNote
\r
3 * Copyright 2011 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\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
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
20 package cx.fbn.nevernote.dialog;
\r
22 //**********************************************
\r
23 //**********************************************
\r
24 //* This is the dialog that shows a user
\r
25 //* a quick popup of a note based upon its title.
\r
26 //* It is used in the Quick Link function.
\r
27 //**********************************************
\r
28 //**********************************************
\r
30 import java.util.List;
\r
32 import com.evernote.edam.type.Note;
\r
33 import com.trolltech.qt.core.QByteArray;
\r
34 import com.trolltech.qt.core.QTemporaryFile;
\r
35 import com.trolltech.qt.core.Qt.ContextMenuPolicy;
\r
36 import com.trolltech.qt.gui.QComboBox;
\r
37 import com.trolltech.qt.gui.QDialog;
\r
38 import com.trolltech.qt.gui.QHBoxLayout;
\r
39 import com.trolltech.qt.gui.QIcon;
\r
40 import com.trolltech.qt.gui.QLabel;
\r
41 import com.trolltech.qt.gui.QPushButton;
\r
42 import com.trolltech.qt.gui.QVBoxLayout;
\r
44 import cx.fbn.nevernote.gui.BrowserWindow;
\r
45 import cx.fbn.nevernote.sql.DatabaseConnection;
\r
46 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
47 import cx.fbn.nevernote.utilities.Pair;
\r
48 import cx.fbn.nevernote.xml.NoteFormatter;
\r
50 public class NoteQuickLinkDialog extends QDialog {
\r
51 public final QPushButton ok;
\r
52 public final QPushButton cancel;
\r
53 private final DatabaseConnection conn;
\r
54 public final QComboBox titleCombo;
\r
55 private final BrowserWindow browser;
\r
56 private final ApplicationLogger logger;
\r
57 List<Pair<String,String>> results;
\r
58 public boolean okPressed;
\r
59 private List<QTemporaryFile> tempFiles;
\r
60 private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
\r
63 public NoteQuickLinkDialog(ApplicationLogger l, DatabaseConnection c, String text) {
\r
65 setWindowTitle(tr("Quick Link Notes"));
\r
66 setWindowIcon(new QIcon(iconPath+"notebook-green.png"));
\r
67 QVBoxLayout main = new QVBoxLayout();
\r
69 titleCombo = new QComboBox(this);
\r
71 QHBoxLayout comboLayout = new QHBoxLayout();
\r
72 comboLayout.addWidget(new QLabel(tr("Matching Notes:")));
\r
73 comboLayout.addWidget(titleCombo);
\r
74 comboLayout.addStretch(100);
\r
76 main.addLayout(comboLayout);
\r
79 browser = new BrowserWindow(conn);
\r
80 main.addWidget(browser);
\r
81 browser.titleLabel.setVisible(false);
\r
82 browser.notebookBox.setVisible(false);
\r
83 browser.hideButtons();
\r
84 browser.tagEdit.setVisible(false);
\r
85 browser.tagLabel.setVisible(false);
\r
87 QHBoxLayout buttonLayout = new QHBoxLayout();
\r
88 buttonLayout.addStretch(100);
\r
89 ok = new QPushButton(tr("OK"));
\r
90 ok.clicked.connect(this, "okPressed()");
\r
92 cancel = new QPushButton(tr("Cancel"));
\r
93 cancel.clicked.connect(this, "cancelPressed()");
\r
95 buttonLayout.addWidget(ok);
\r
96 buttonLayout.addWidget(cancel);
\r
97 main.addLayout(buttonLayout);
\r
99 browser.getBrowser().setContextMenuPolicy(ContextMenuPolicy.NoContextMenu);
\r
102 // Search for matching notes
\r
103 results = conn.getNoteTable().findNotesByTitle(text);
\r
105 // Add the results to the combo box
\r
106 for (int i=0; i<results.size(); i++) {
\r
107 titleCombo.addItem(results.get(i).getSecond(), results.get(i).getFirst());
\r
109 titleCombo.activated.connect(this, "selectionChanged(String)");
\r
111 // Load the results into the combo box
\r
112 if (results.size() > 0) {
\r
113 Note currentNote = conn.getNoteTable().getNote(results.get(0).getFirst(), true, true, false, true, true);
\r
114 setContent(currentNote);
\r
118 // Cancel button pressed
\r
119 @SuppressWarnings("unused")
\r
120 private void cancelPressed() {
\r
124 // OK button pressed
\r
125 @SuppressWarnings("unused")
\r
126 private void okPressed() {
\r
131 // When the selection changes, we refresh the browser window with the new content
\r
132 @SuppressWarnings("unused")
\r
133 private void selectionChanged(String text) {
\r
134 int pos = titleCombo.currentIndex();
\r
135 String guid = results.get(pos).getFirst();
\r
136 Note note = conn.getNoteTable().getNote(guid, true, true, false, true, true);
\r
140 // Return the note the user is currently viewing
\r
141 public String getSelectedNote() {
\r
142 int pos = titleCombo.currentIndex();
\r
143 return results.get(pos).getFirst();
\r
147 // Load the content of the note into the viewing window.
\r
148 public void setContent(Note currentNote) {
\r
149 NoteFormatter formatter = new NoteFormatter(logger, conn, tempFiles);
\r
150 formatter.setNote(currentNote, false);
\r
151 formatter.setHighlight(null);
\r
152 formatter.setNoteHistory(true);
\r
154 StringBuffer js = new StringBuffer();
\r
156 // We need to prepend the note with <HEAD></HEAD> or encoded characters are ugly
\r
157 js.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
\r
158 js.append("<style type=\"text/css\">en-crypt-temp { border-style:solid; border-color:blue; padding:1mm 1mm 1mm 1mm; }</style>");
\r
159 js.append("</head>");
\r
160 js.append(formatter.rebuildNoteHTML());
\r
161 js.append("</HTML>");
\r
163 browser.setNote(currentNote);
\r
164 browser.setContent(new QByteArray(js.toString()));
\r
167 // give the results from the DB search back to the caller.
\r
168 public List<Pair<String,String>> getResults() {
\r