OSDN Git Service

Add the ability to assign custom icons to the "All Notebooks" & stack icons.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / SystemIconTable.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 \r
21 package cx.fbn.nevernote.sql;\r
22 \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
29 \r
30 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
31 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
32 \r
33 public class SystemIconTable {\r
34         \r
35         private final ApplicationLogger                 logger;\r
36         DatabaseConnection                                                      db;\r
37         \r
38         // Constructor\r
39         public SystemIconTable(ApplicationLogger l, DatabaseConnection d) {\r
40                 logger = l;\r
41                 db = d;\r
42         }\r
43         // Create the table\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
48                         "type varchar, " +\r
49                         "primary key(name, type), " +\r
50                         "icon blob)"))                          \r
51                 logger.log(logger.HIGH, "Table SystemIcon creation FAILED!!!");   \r
52         }\r
53         // Drop the table\r
54         public void dropTable() {\r
55                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
56                 query.exec("Drop table SystemIcon");\r
57         }\r
58 \r
59         \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
63                 \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
68                 if (!query.exec())\r
69                         logger.log(logger.EXTREME, "Error finding system icon.");\r
70                 if (!query.next() || query.getBlob(0) == null)\r
71                         return null;\r
72                 \r
73                 QByteArray blob = new QByteArray(query.getBlob(0));\r
74                 QIcon icon = new QIcon(QPixmap.fromImage(QImage.fromData(blob)));\r
75                 return icon;\r
76         }\r
77         \r
78         \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
82                 \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
87                 if (!query.exec())\r
88                         logger.log(logger.EXTREME, "Error finding system icon.");\r
89                 if (!query.next() || query.getBlob(0) == null)\r
90                         return false;\r
91                 return true;\r
92         }\r
93         \r
94         \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
99                 else\r
100                         addIcon(name, rectype, icon, filetype);\r
101         }\r
102         \r
103         \r
104         \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
109                         return;\r
110                 } else {\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
116                         return;\r
117                 }\r
118                 QPixmap p = icon.pixmap(32, 32);\r
119                 QImage i = p.toImage();\r
120                 i.save(buffer, filetype.toUpperCase());\r
121                 buffer.close();\r
122                 QByteArray b = new QByteArray(buffer.buffer());\r
123                 if (!b.isNull() && !b.isEmpty())\r
124                         query.bindValue(":icon", b.toByteArray());\r
125                 else\r
126                         return;\r
127                 }\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
132         }\r
133         \r
134         \r
135         \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
142                 } else {\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
148                         return;\r
149                 }\r
150                 QPixmap p = icon.pixmap(32, 32);\r
151                 QImage i = p.toImage();\r
152                 i.save(buffer, filetype.toUpperCase());\r
153                 buffer.close();\r
154                 QByteArray b = new QByteArray(buffer.buffer());\r
155                 if (!b.isNull() && !b.isEmpty())\r
156                         query.bindValue(":icon", b.toByteArray());\r
157                 else\r
158                         return;\r
159                 }\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
164         }\r
165 \r
166 }\r
167 \r