OSDN Git Service

Allow customizing of notebook icons.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / NotebookTable.java
index fafd2eb..7cd6e0e 100644 (file)
@@ -24,9 +24,16 @@ import java.text.DateFormat;
 import java.text.ParseException;\r
 import java.text.SimpleDateFormat;\r
 import java.util.ArrayList;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 \r
 import com.evernote.edam.type.Notebook;\r
+import com.trolltech.qt.core.QBuffer;\r
+import com.trolltech.qt.core.QByteArray;\r
+import com.trolltech.qt.core.QIODevice;\r
+import com.trolltech.qt.gui.QIcon;\r
+import com.trolltech.qt.gui.QImage;\r
+import com.trolltech.qt.gui.QPixmap;\r
 \r
 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
@@ -403,7 +410,68 @@ public class NotebookTable {
                        logger.log(logger.EXTREME, "Error setting default notebook.");\r
        }\r
        \r
+       // Get a list of all icons\r
+       public HashMap<String, QIcon> getAllIcons() {\r
+               HashMap<String, QIcon> values = new HashMap<String, QIcon>();\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
        \r
+               if (!query.exec("SELECT guid, icon from notebook where ARCHIVED  != true"))\r
+                       logger.log(logger.EXTREME, "Error executing notebook getAllIcons select.");\r
+               while (query.next()) {\r
+                       if (query.getBlob(1) != null) {\r
+                               String guid = query.valueString(0);\r
+                               QByteArray blob = new QByteArray(query.getBlob(1));\r
+                               QIcon icon = new QIcon(QPixmap.fromImage(QImage.fromData(blob)));\r
+                               values.put(guid, icon);\r
+                       }\r
+               }\r
+               return values;\r
+       }\r
+       \r
+       // Get the notebooks custom icon\r
+       public QIcon getIcon(String guid) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               if (!query.prepare("Select icon from notebook where guid=:guid"))\r
+                       logger.log(logger.EXTREME, "Error preparing notebook icon select.");\r
+               query.bindValue(":guid", guid);\r
+               if (!query.exec())\r
+                       logger.log(logger.EXTREME, "Error finding notebook icon.");\r
+               if (!query.next() || query.getBlob(0) == null)\r
+                       return null;\r
+               \r
+               QByteArray blob = new QByteArray(query.getBlob(0));\r
+               QIcon icon = new QIcon(QPixmap.fromImage(QImage.fromData(blob)));\r
+               return icon;\r
+       }\r
+       // Set the notebooks custom icon\r
+       public void setIcon(String guid, QIcon icon, String type) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               if (icon == null) {\r
+                       if (!query.prepare("update notebook set icon=null where guid=:guid"))\r
+                               logger.log(logger.EXTREME, "Error preparing notebook icon select.");\r
+               } else {\r
+                       if (!query.prepare("update notebook set icon=:icon where guid=:guid"))\r
+                               logger.log(logger.EXTREME, "Error preparing notebook icon select.");\r
+                       QBuffer buffer = new QBuffer();\r
+               if (!buffer.open(QIODevice.OpenModeFlag.ReadWrite)) {\r
+                       logger.log(logger.EXTREME, "Failure to open buffer.  Aborting.");\r
+                       return;\r
+               }\r
+               QPixmap p = icon.pixmap(32, 32);\r
+               QImage i = p.toImage();\r
+               i.save(buffer, type.toUpperCase());\r
+               buffer.close();\r
+               QByteArray b = new QByteArray(buffer.buffer());\r
+               if (!b.isNull() && !b.isEmpty())\r
+                       query.bindValue(":icon", b.toByteArray());\r
+               else\r
+                       return;\r
+               }\r
+               query.bindValue(":guid", guid);\r
+               if (!query.exec()) \r
+                       logger.log(logger.LOW, "Error setting notebook icon. " +query.lastError());\r
+       }\r
 \r
        // does a record exist?\r
        public String findNotebookByName(String newname) {\r