2 * This file is part of NixNote
\r
3 * Copyright 2009 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
21 package cx.fbn.nevernote.sql;
\r
23 import com.trolltech.qt.core.QBuffer;
\r
24 import com.trolltech.qt.core.QByteArray;
\r
25 import com.trolltech.qt.core.QIODevice;
\r
26 import com.trolltech.qt.gui.QIcon;
\r
27 import com.trolltech.qt.gui.QImage;
\r
28 import com.trolltech.qt.gui.QPixmap;
\r
30 import cx.fbn.nevernote.sql.driver.NSqlQuery;
\r
31 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
33 public class SystemIconTable {
\r
35 private final ApplicationLogger logger;
\r
36 DatabaseConnection db;
\r
39 public SystemIconTable(ApplicationLogger l, DatabaseConnection d) {
\r
44 public void createTable() {
\r
45 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
46 logger.log(logger.HIGH, "Creating table SystemIcon...");
\r
47 if (!query.exec("Create table SystemIcon (name varchar, " +
\r
49 "primary key(name, type), " +
\r
51 logger.log(logger.HIGH, "Table SystemIcon creation FAILED!!!");
\r
54 public void dropTable() {
\r
55 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
56 query.exec("Drop table SystemIcon");
\r
60 // Get the notebooks custom icon
\r
61 public QIcon getIcon(String name, String type) {
\r
62 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
64 if (!query.prepare("Select icon from SystemIcon where name=:name and type=:type"))
\r
65 logger.log(logger.EXTREME, "Error preparing system icon select.");
\r
66 query.bindValue(":name", name);
\r
67 query.bindValue(":type", type);
\r
69 logger.log(logger.EXTREME, "Error finding system icon.");
\r
70 if (!query.next() || query.getBlob(0) == null)
\r
73 QByteArray blob = new QByteArray(query.getBlob(0));
\r
74 QIcon icon = new QIcon(QPixmap.fromImage(QImage.fromData(blob)));
\r
79 // Get the notebooks custom icon
\r
80 public boolean exists(String name, String type) {
\r
81 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
83 if (!query.prepare("Select icon from SystemIcon where name=:name and type=:type"))
\r
84 logger.log(logger.EXTREME, "Error preparing system icon select.");
\r
85 query.bindValue(":name", name);
\r
86 query.bindValue(":type", type);
\r
88 logger.log(logger.EXTREME, "Error finding system icon.");
\r
89 if (!query.next() || query.getBlob(0) == null)
\r
95 // Set the notebooks custom icon
\r
96 public void setIcon(String name, String rectype, QIcon icon, String filetype) {
\r
97 if (exists(name, rectype))
\r
98 updateIcon(name, rectype, icon, filetype);
\r
100 addIcon(name, rectype, icon, filetype);
\r
105 // Set the notebooks custom icon
\r
106 public void addIcon(String name, String rectype, QIcon icon, String filetype) {
\r
107 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
108 if (icon == null) {
\r
111 if (!query.prepare("Insert into SystemIcon (icon, name, type) values (:icon, :name, :type)"))
\r
112 logger.log(logger.EXTREME, "Error preparing notebook icon select.");
\r
113 QBuffer buffer = new QBuffer();
\r
114 if (!buffer.open(QIODevice.OpenModeFlag.ReadWrite)) {
\r
115 logger.log(logger.EXTREME, "Failure to open buffer. Aborting.");
\r
118 QPixmap p = icon.pixmap(32, 32);
\r
119 QImage i = p.toImage();
\r
120 i.save(buffer, filetype.toUpperCase());
\r
122 QByteArray b = new QByteArray(buffer.buffer());
\r
123 if (!b.isNull() && !b.isEmpty())
\r
124 query.bindValue(":icon", b.toByteArray());
\r
128 query.bindValue(":name", name);
\r
129 query.bindValue(":type", rectype);
\r
130 if (!query.exec())
\r
131 logger.log(logger.LOW, "Error setting system icon. " +query.lastError());
\r
136 // Set the notebooks custom icon
\r
137 public void updateIcon(String name, String rectype, QIcon icon, String filetype) {
\r
138 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
139 if (icon == null) {
\r
140 if (!query.prepare("delete from SystemIcon where name=:name and type=:type"))
\r
141 logger.log(logger.EXTREME, "Error preparing notebook icon select.");
\r
143 if (!query.prepare("update SystemIcon set icon=:icon where name=:name and type=:type"))
\r
144 logger.log(logger.EXTREME, "Error preparing notebook icon select.");
\r
145 QBuffer buffer = new QBuffer();
\r
146 if (!buffer.open(QIODevice.OpenModeFlag.ReadWrite)) {
\r
147 logger.log(logger.EXTREME, "Failure to open buffer. Aborting.");
\r
150 QPixmap p = icon.pixmap(32, 32);
\r
151 QImage i = p.toImage();
\r
152 i.save(buffer, filetype.toUpperCase());
\r
154 QByteArray b = new QByteArray(buffer.buffer());
\r
155 if (!b.isNull() && !b.isEmpty())
\r
156 query.bindValue(":icon", b.toByteArray());
\r
160 query.bindValue(":name", name);
\r
161 query.bindValue(":type", rectype);
\r
162 if (!query.exec())
\r
163 logger.log(logger.LOW, "Error setting system icon. " +query.lastError());
\r