OSDN Git Service

Evernoteサーバから取得したサムネイルをDBのNoteテーブルenThumbnailカラムに保存するようにした
authoryuki <kimaira7@gmail.com>
Fri, 1 Nov 2013 10:34:23 +0000 (19:34 +0900)
committeryuki <kimaira7@gmail.com>
Tue, 5 Nov 2013 08:54:14 +0000 (17:54 +0900)
src/cx/fbn/nevernote/gui/RensoNoteList.java
src/cx/fbn/nevernote/gui/RensoNoteListItem.java
src/cx/fbn/nevernote/sql/DatabaseConnection.java
src/cx/fbn/nevernote/sql/NoteTable.java
src/cx/fbn/nevernote/threads/ENThumbnailRunner.java

index 5307edb..bdb5eeb 100644 (file)
@@ -28,6 +28,7 @@ import java.util.Set;
 
 import com.evernote.edam.type.Note;
 import com.trolltech.qt.QThread;
+import com.trolltech.qt.core.QByteArray;
 import com.trolltech.qt.core.QFile;
 import com.trolltech.qt.core.QSize;
 import com.trolltech.qt.core.Qt.MouseButton;
@@ -87,7 +88,7 @@ public class RensoNoteList extends QListWidget {
                this.enRelatedNotesThread = new QThread(enRelatedNotesRunner, "ENRelatedNotes Thread");
                this.getEnRelatedNotesThread().start();
                
-               this.enThumbnailRunner = new ENThumbnailRunner(this.logger);
+               this.enThumbnailRunner = new ENThumbnailRunner(this.logger, this.conn);
                this.enThumbnailRunner.enThumbnailSignal.getENThumbnailFinished.connect(this, "enThumbnailComplete(String)");
                this.enThumbnailRunner.limitSignal.rateLimitReached.connect(parent, "informRateLimit(Integer)");
                this.enThumbnailThread = new QThread(enThumbnailRunner, "ENThumbnail Thread");
@@ -322,12 +323,14 @@ public class RensoNoteList extends QListWidget {
                                // 存在していて、かつ関連度0でなければノート情報を取得して連想ノートリストに追加
                                if (isNoteActive && maxNum > 0) {
                                        // Evernoteサムネイルが取得済みか確認。未取得ならサムネイル取得スレッドにキュー
-                                       // TODO 今はとりあえずresディレクトリを確認。あとでデータベースにカラムを追加する
                                        if (Global.isConnected) {
                                                String thumbnailName = Global.getFileManager().getResDirPath("enThumbnail-" + maxGuid + ".png");
                                                QFile thumbnail = new QFile(thumbnailName);
-                                               if (!thumbnail.exists()) {
-                                                       enThumbnailRunner.addGuid(maxGuid);
+                                               if (!thumbnail.exists()) {      // Evernoteサムネイルがファイルとして存在しない
+                                                       QByteArray data = conn.getNoteTable().getENThumbnail(maxGuid);
+                                                       if (data == null) {     // Evernoteサムネイル未取得
+                                                               enThumbnailRunner.addGuid(maxGuid);
+                                                       }
                                                }
                                        }
                                        
index fbbbf25..6a10b06 100644 (file)
@@ -22,6 +22,7 @@ package cx.fbn.nevernote.gui;
 import java.text.SimpleDateFormat;
 
 import com.evernote.edam.type.Note;
+import com.trolltech.qt.core.QByteArray;
 import com.trolltech.qt.core.QEvent;
 import com.trolltech.qt.core.QFile;
 import com.trolltech.qt.core.QRectF;
@@ -149,14 +150,26 @@ public class RensoNoteListItem extends QWidget{
                painter.drawText(70, size().height() - 33, size().width() - 70, 33, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");             
                painter.setPen(tmpPen);
                // サムネイル
-               QImage img;
-               String thumbnailName = Global.getFileManager().getResDirPath("enThumbnail-" + noteGuid + ".png");
-               QFile thumbnail = new QFile(thumbnailName);
-               if (!thumbnail.exists()) {
-                       img = new QImage();
-                       img.loadFromData(conn.getNoteTable().getThumbnail(noteGuid));
+               // 優先順位   1.Evernoteサムネイル(ファイル), 2.Evernoteサムネイル(DB), 3.生成サムネイル(ファイル), 4.生成サムネイル(DB)
+               QImage img = new QImage();
+               String enThumbnailName = Global.getFileManager().getResDirPath("enThumbnail-" + noteGuid + ".png");
+               QFile enThumbnail = new QFile(enThumbnailName);
+               if (!enThumbnail.exists()) {    // Evernoteサムネイルがファイルとして存在しない
+                       QByteArray b =conn.getNoteTable().getENThumbnail(noteGuid);
+                       if (b == null) {        // Evernoteサムネイルがデータベースにも存在しない
+                               String thumbnailName = Global.getFileManager().getResDirPath("thumbnail-" + noteGuid + ".png");
+                               QFile thumbnail = new QFile(thumbnailName);
+                               if (!thumbnail.exists()) {      // 生成サムネイルがファイルとして存在しない
+                                       img = new QImage();
+                                       img.loadFromData(conn.getNoteTable().getThumbnail(noteGuid));
+                               }
+                       } else {
+                               img = new QImage();
+                               img.loadFromData(b);
+                               saveImage(img, noteGuid);
+                       }
                } else {
-                       img = new QImage(thumbnailName);
+                       img = new QImage(enThumbnailName);
                }
                painter.drawImage(2, 4, img, 0, 0, 80, rect().height() - 10);
                painter.setPen(QColor.lightGray);
@@ -200,4 +213,10 @@ public class RensoNoteListItem extends QWidget{
                palette.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
                this.setPalette(palette);
        }
+       
+       // サムネイルをpng形式のファイルとしてresディレクトリに保存
+       private void saveImage(QImage thumbnail, String guid) {
+               String thumbnailName = Global.getFileManager().getResDirPath("enThumbnail-" + guid + ".png");
+               thumbnail.save(thumbnailName, "PNG");
+       }
 }
index 95ea023..d048680 100644 (file)
@@ -306,6 +306,11 @@ public class DatabaseConnection {
                        Global.rebuildFullTextNoteTarget(this);
                }
                
+               // Evernoteサムネイルカラムを追加
+               if (!dbTableColumnExists("NOTE", "ENTHUMBNAIL")) {
+                       executeSql("alter table note add column enThumbNail Blob");
+               }
+               
                // Apache Luceneを使った日本語検索のためのプレーンテキストノートリソースカラムを準備
                NSqlQuery rQuery = new NSqlQuery(resourceConn);
                rQuery.exec("select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='NOTERESOURCES' and COLUMN_NAME='RESOURCETEXT'");
index 0fa6c97..c1a4d41 100644 (file)
@@ -1783,6 +1783,44 @@ public class NoteTable {
 
                return noteContentText;
        }
+       
+       // Evernoteサムネイルを取得
+       public QByteArray getENThumbnail(String guid) {
+               boolean check;                  
+        NSqlQuery query = new NSqlQuery(db.getConnection());
+                                       
+               check = query.prepare("Select enThumbnail from note where guid=:guid");
+               query.bindValue(":guid", guid);
+               check = query.exec();
+               if (!check) {
+                       logger.log(logger.EXTREME, "Note SQL get enThumbail failed: " +query.lastError().toString());
+               }
+               
+               if (query.next())  {
+                       try {
+                               if (query.getBlob(0) != null) {
+                                       return new QByteArray(query.getBlob(0)); 
+                               }
+                       } catch (java.lang.IllegalArgumentException e) {
+                               return null;
+                       }
+               }
+               return null;
+       }
+       
+       // Evernoteサムネイルを登録
+       public void setENThumbnail(String guid, QByteArray thumbnail) {
+               boolean check;
+        NSqlQuery query = new NSqlQuery(db.getConnection());
+                                       
+               check = query.prepare("Update note set enThumbnail = :enThumbnail where guid=:guid");
+               query.bindValue(":guid", guid);
+               query.bindValue(":enThumbnail", thumbnail.toByteArray());
+               check = query.exec();
+               if (!check) {
+                       logger.log(logger.EXTREME, "Note SQL set enThumbail failed: " +query.lastError().toString());
+               }
+       }
 }      
 
 
index be84c90..e9d434d 100644 (file)
@@ -47,12 +47,14 @@ import cx.fbn.nevernote.Global;
 import cx.fbn.nevernote.oauth.OAuthTokenizer;
 import cx.fbn.nevernote.signals.ENThumbnailSignal;
 import cx.fbn.nevernote.signals.LimitSignal;
+import cx.fbn.nevernote.sql.DatabaseConnection;
 import cx.fbn.nevernote.utilities.AESEncrypter;
 import cx.fbn.nevernote.utilities.ApplicationLogger;
 
 public class ENThumbnailRunner extends QObject implements Runnable{
        
        private final ApplicationLogger                                 logger;
+       private final DatabaseConnection                                conn;
        public volatile ENThumbnailSignal                               enThumbnailSignal;
        public QMutex                                                                   mutex;
        private volatile boolean                                                keepRunning;
@@ -61,8 +63,9 @@ public class ENThumbnailRunner extends QObject implements Runnable{
        private volatile User                                                   user;
        private volatile String                                                 serverUrl;
        
-       public ENThumbnailRunner(ApplicationLogger logger) {
+       public ENThumbnailRunner(ApplicationLogger logger, DatabaseConnection conn) {
                this.logger = logger;
+               this.conn = conn;
                this.enThumbnailSignal = new ENThumbnailSignal();
                this.mutex = new QMutex();
                this.keepRunning = true;
@@ -85,12 +88,15 @@ public class ENThumbnailRunner extends QObject implements Runnable{
                                        String guid = work.replace("GET ", "");
                                        logger.log(logger.EXTREME, "Evernoteサムネイル取得開始 guid = " + guid);
                                        
-                                       QPixmap thumbnail_p = getENThumbnail(guid);
-                                       if (thumbnail_p == null) {                              // 取得に失敗
+                                       QByteArray thumbnailData = getENThumbnailData(guid);
+                                       if (thumbnailData == null) {                            // 取得に失敗
                                                logger.log(logger.EXTREME, "Evernoteサムネイルの取得に失敗");
                                        } else {
+                                               QPixmap thumbnail_p = new QPixmap();
+                                               thumbnail_p.loadFromData(thumbnailData);
                                                logger.log(logger.EXTREME, "Evernoteサムネイルの取得に成功");
                                                saveImage(thumbnail_p, guid);
+                                               registImage(thumbnailData, guid);
                                        }
                                        
                                        enThumbnailSignal.getENThumbnailFinished.emit(guid);
@@ -108,7 +114,7 @@ public class ENThumbnailRunner extends QObject implements Runnable{
        }
        
        // Evernoteサーバからサムネイルを取得
-       private synchronized QPixmap getENThumbnail(String guid) {
+       private synchronized QByteArray getENThumbnailData(String guid) {
                // サムネイルをEvernoteサーバから取得
                String shardId = user.getShardId();
                if (shardId == null || shardId.equals("")) {
@@ -139,18 +145,14 @@ public class ENThumbnailRunner extends QObject implements Runnable{
                nameValuePairs.add(new BasicNameValuePair("auth", oauthToken));
                nameValuePairs.add(new BasicNameValuePair("size", "80"));
                
-               QPixmap p = new QPixmap();
+               QByteArray data = new QByteArray();
                try {
                        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        // Webサーバからのレスポンスを処理
                        HttpResponse response = null;
                        response = httpClient.execute(httpPost);
                        byte[] bytes = EntityUtils.toByteArray(response.getEntity());
-                       QByteArray data = new QByteArray(bytes);
-                       // データベースにEvernoteサーバから取得したサムネイルを保存。例↓
-                       // conn.getNoteTable().setThumbnail(guid, data);
-                       
-                       p.loadFromData(data);
+                       data = new QByteArray(bytes);
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        return null;
@@ -164,13 +166,19 @@ public class ENThumbnailRunner extends QObject implements Runnable{
                        httpClient.getConnectionManager().shutdown();
                }
 
-               return p;
+               return data;
        }
        
+       // サムネイルをpng形式のファイルとしてresディレクトリに保存
        private synchronized void saveImage(QPixmap thumbnail, String guid) {
                String thumbnailName = Global.getFileManager().getResDirPath("enThumbnail-" + guid + ".png");
                thumbnail.save(thumbnailName, "PNG");
        }
+       
+       // サムネイルのバイナリデータをデータベースに登録
+       private synchronized void registImage(QByteArray data, String guid) {
+               conn.getNoteTable().setENThumbnail(guid, data);
+       }
 
        public boolean isKeepRunning() {
                return keepRunning;