OSDN Git Service

Evenoteの関連ノート取得を別スレッドで実行するよう試作。まだまだ問題あり。
authoryuki <kimaira7@gmail.com>
Sun, 30 Jun 2013 08:37:24 +0000 (17:37 +0900)
committeryuki <kimaira7@gmail.com>
Sun, 30 Jun 2013 08:37:24 +0000 (17:37 +0900)
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/gui/RensoNoteList.java
src/cx/fbn/nevernote/signals/ENRelatedNotesSignal.java [new file with mode: 0644]
src/cx/fbn/nevernote/threads/ENRelatedNotesRunner.java [new file with mode: 0644]

index bd100e3..3d30b1f 100644 (file)
@@ -1251,6 +1251,9 @@ public class NeverNote extends QMainWindow{
                saveNote();
                listManager.stop();
                saveWindowState();
                saveNote();
                listManager.stop();
                saveWindowState();
+               
+               // 連想ノートリストのEvernote関連ノート取得スレッドを終了
+               rensoNoteList.stopThread();
 
                if (tempFiles != null)
                        tempFiles.clear();
 
                if (tempFiles != null)
                        tempFiles.clear();
index 747a4f6..c48d3b9 100644 (file)
@@ -26,15 +26,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
 import java.util.List;
 import java.util.Set;
 
-import com.evernote.edam.error.EDAMNotFoundException;
-import com.evernote.edam.error.EDAMSystemException;
-import com.evernote.edam.error.EDAMUserException;
-import com.evernote.edam.limits.Constants;
-import com.evernote.edam.notestore.RelatedQuery;
-import com.evernote.edam.notestore.RelatedResult;
-import com.evernote.edam.notestore.RelatedResultSpec;
 import com.evernote.edam.type.Note;
 import com.evernote.edam.type.Note;
-import com.evernote.thrift.TException;
+import com.trolltech.qt.QThread;
 import com.trolltech.qt.core.QSize;
 import com.trolltech.qt.core.Qt.MouseButton;
 import com.trolltech.qt.gui.QAction;
 import com.trolltech.qt.core.QSize;
 import com.trolltech.qt.core.Qt.MouseButton;
 import com.trolltech.qt.gui.QAction;
@@ -47,6 +40,7 @@ import com.trolltech.qt.gui.QMenu;
 import cx.fbn.nevernote.Global;
 import cx.fbn.nevernote.NeverNote;
 import cx.fbn.nevernote.sql.DatabaseConnection;
 import cx.fbn.nevernote.Global;
 import cx.fbn.nevernote.NeverNote;
 import cx.fbn.nevernote.sql.DatabaseConnection;
+import cx.fbn.nevernote.threads.ENRelatedNotesRunner;
 import cx.fbn.nevernote.threads.SyncRunner;
 import cx.fbn.nevernote.utilities.ApplicationLogger;
 
 import cx.fbn.nevernote.threads.SyncRunner;
 import cx.fbn.nevernote.utilities.ApplicationLogger;
 
@@ -56,7 +50,6 @@ public class RensoNoteList extends QListWidget {
        private final HashMap<QListWidgetItem, String> rensoNoteListItems;
        private final List<RensoNoteListItem> rensoNoteListTrueItems;
        private String rensoNotePressedItemGuid;
        private final HashMap<QListWidgetItem, String> rensoNoteListItems;
        private final List<RensoNoteListItem> rensoNoteListTrueItems;
        private String rensoNotePressedItemGuid;
-       
        private final QAction openNewTabAction;
        private final QAction starAction;
        private final QAction unstarAction;
        private final QAction openNewTabAction;
        private final QAction starAction;
        private final QAction unstarAction;
@@ -64,6 +57,8 @@ public class RensoNoteList extends QListWidget {
        private final NeverNote parent;
        private final QMenu menu;
        private final SyncRunner syncRunner;
        private final NeverNote parent;
        private final QMenu menu;
        private final SyncRunner syncRunner;
+       private final ENRelatedNotesRunner ENRelatedNotesRunner;
+       private final QThread ENRelatedNotesThread;
        private int allPointSum;
 
        public RensoNoteList(DatabaseConnection c, NeverNote p, SyncRunner syncRunner) {
        private int allPointSum;
 
        public RensoNoteList(DatabaseConnection c, NeverNote p, SyncRunner syncRunner) {
@@ -74,6 +69,12 @@ public class RensoNoteList extends QListWidget {
                this.conn = c;
                this.parent = p;
                this.syncRunner = syncRunner;
                this.conn = c;
                this.parent = p;
                this.syncRunner = syncRunner;
+               
+               this.ENRelatedNotesRunner = new ENRelatedNotesRunner(this.syncRunner);
+               this.ENRelatedNotesRunner.ENRelatedNotesSignal.getENRelatedNotesFinished.connect(this, "ENRelatedNotesComplete()");
+               this.ENRelatedNotesThread = new QThread(ENRelatedNotesRunner, "ENRelatedNotes Thread");
+               this.ENRelatedNotesThread.start();
+               
                rensoNoteListItems = new HashMap<QListWidgetItem, String>();
                rensoNoteListTrueItems = new ArrayList<RensoNoteListItem>();
                
                rensoNoteListItems = new HashMap<QListWidgetItem, String>();
                rensoNoteListTrueItems = new ArrayList<RensoNoteListItem>();
                
@@ -121,6 +122,27 @@ public class RensoNoteList extends QListWidget {
 
                HashMap<String, Integer> mergedHistory = new HashMap<String, Integer>();
                
 
                HashMap<String, Integer> mergedHistory = new HashMap<String, Integer>();
                
+               // Evernoteの関連ノートを別スレッドで取得
+               ENRelatedNotesRunner.addGuid(guid);
+//             ENRelatedNotesRunner.setGuid(guid);
+//             ENRelatedNotesThread.finished.connect(this, "ENRelatedNotesComplete()");
+               
+//             RelatedResult result = getENRelatedNotes(guid);
+//             List<Note> relatedNotes = new ArrayList<Note>();
+//             if (result != null) {
+//                     relatedNotes = result.getNotes();
+//             }
+//             if (relatedNotes != null && !relatedNotes.isEmpty()) {
+//                     HashMap<String, Integer> ENRelatedNotes = new HashMap<String, Integer>();
+//                     
+//                     for (Note relatedNote : relatedNotes) {
+//                             String relatedGuid = relatedNote.getGuid();
+//                             ENRelatedNotes.put(relatedGuid, 1);
+//                     }
+//                     addWeight(ENRelatedNotes, 10);
+//                     mergedHistory = mergeHistory(ENRelatedNotes, mergedHistory);
+//             }
+               
                // browseHistory<guid, 回数(ポイント)>
                HashMap<String, Integer> browseHistory = conn.getHistoryTable().getBehaviorHistory("browse", guid);
                addWeight(browseHistory, Global.getBrowseWeight());
                // browseHistory<guid, 回数(ポイント)>
                HashMap<String, Integer> browseHistory = conn.getHistoryTable().getBehaviorHistory("browse", guid);
                addWeight(browseHistory, Global.getBrowseWeight());
@@ -151,23 +173,6 @@ public class RensoNoteList extends QListWidget {
                addWeight(sameNotebookHistory, Global.getSameNotebookWeight());
                mergedHistory = mergeHistory(sameNotebookHistory, mergedHistory);
                
                addWeight(sameNotebookHistory, Global.getSameNotebookWeight());
                mergedHistory = mergeHistory(sameNotebookHistory, mergedHistory);
                
-               // Evernoteの関連ノートを取得
-               RelatedResult result = getENRelatedNotes(guid);
-               List<Note> relatedNotes = new ArrayList<Note>();
-               if (result != null) {
-                       relatedNotes = result.getNotes();
-               }
-               if (relatedNotes != null && !relatedNotes.isEmpty()) {
-                       HashMap<String, Integer> ENRelatedNotes = new HashMap<String, Integer>();
-                       
-                       for (Note relatedNote : relatedNotes) {
-                               String relatedGuid = relatedNote.getGuid();
-                               ENRelatedNotes.put(relatedGuid, 1);
-                       }
-                       addWeight(ENRelatedNotes, 10);
-                       mergedHistory = mergeHistory(ENRelatedNotes, mergedHistory);
-               }
-               
                // すべての関連ポイントの合計を取得(関連度のパーセント算出に利用)
                allPointSum = 0;
                for (int p : mergedHistory.values()) {
                // すべての関連ポイントの合計を取得(関連度のパーセント算出に利用)
                allPointSum = 0;
                for (int p : mergedHistory.values()) {
@@ -179,31 +184,31 @@ public class RensoNoteList extends QListWidget {
                logger.log(logger.HIGH, "Leaving RensoNoteList.refreshRensoNoteList");
        }
        
                logger.log(logger.HIGH, "Leaving RensoNoteList.refreshRensoNoteList");
        }
        
-       private RelatedResult getENRelatedNotes(String guid) {
-               RelatedQuery rquery = new RelatedQuery();
-               rquery.setNoteGuid(guid);
-               RelatedResultSpec resultSpec = new RelatedResultSpec();
-               resultSpec.setMaxNotes(Constants.EDAM_RELATED_MAX_NOTES);
-               if (syncRunner != null && syncRunner.localNoteStore != null) {
-                       try {
-                               RelatedResult result = syncRunner.localNoteStore.findRelated(syncRunner.authToken, rquery, resultSpec);
-                               return result;
-                       } catch (EDAMUserException e) {
-                               // TODO 自動生成された catch ブロック
-                               e.printStackTrace();
-                       } catch (EDAMSystemException e) {
-                               // TODO 自動生成された catch ブロック
-                               e.printStackTrace();
-                       } catch (EDAMNotFoundException e) {
-                               // TODO 自動生成された catch ブロック
-                               e.printStackTrace();
-                       } catch (TException e) {
-                               // TODO 自動生成された catch ブロック
-                               e.printStackTrace();
-                       }
-               }
-               return null;
-       }
+//     private RelatedResult getENRelatedNotes(String guid) {
+//             RelatedQuery rquery = new RelatedQuery();
+//             rquery.setNoteGuid(guid);
+//             RelatedResultSpec resultSpec = new RelatedResultSpec();
+//             resultSpec.setMaxNotes(Constants.EDAM_RELATED_MAX_NOTES);
+//             if (syncRunner != null && syncRunner.localNoteStore != null) {
+//                     try {
+//                             RelatedResult result = syncRunner.localNoteStore.findRelated(syncRunner.authToken, rquery, resultSpec);
+//                             return result;
+//                     } catch (EDAMUserException e) {
+//                             // TODO 自動生成された catch ブロック
+//                             e.printStackTrace();
+//                     } catch (EDAMSystemException e) {
+//                             // TODO 自動生成された catch ブロック
+//                             e.printStackTrace();
+//                     } catch (EDAMNotFoundException e) {
+//                             // TODO 自動生成された catch ブロック
+//                             e.printStackTrace();
+//                     } catch (TException e) {
+//                             // TODO 自動生成された catch ブロック
+//                             e.printStackTrace();
+//                     }
+//             }
+//             return null;
+//     }
 
        // 操作回数に重み付けする
        private void addWeight(HashMap<String, Integer> history, int weight){
 
        // 操作回数に重み付けする
        private void addWeight(HashMap<String, Integer> history, int weight){
@@ -365,4 +370,18 @@ public class RensoNoteList extends QListWidget {
                        rensoNotePressedItemGuid = getNoteGuid(current);
                }
        }
                        rensoNotePressedItemGuid = getNoteGuid(current);
                }
        }
+       
+       // Evernoteの関連ノートの取得が完了
+       @SuppressWarnings("unused")
+       private void ENRelatedNotesComplete() {
+//             List<String> relatedNoteGuids = ENRelatedNotesRunner.getRelatedGuids();
+               List<String> relatedNoteGuids = ENRelatedNotesRunner.getENRelatedNoteGuids();
+               System.out.println(relatedNoteGuids);
+       }
+       
+       // Evernoteの関連ノート取得スレッドを終了させる
+       public void stopThread() {
+               ENRelatedNotesRunner.setKeepRunning(false);
+               // TODO keepRunningをfalseにしても、guidQueueが次のキューが来るまで待機するので終了できない。guidQueueに終了命令を処理できる仕組みを要追加。
+       }
 }
 }
diff --git a/src/cx/fbn/nevernote/signals/ENRelatedNotesSignal.java b/src/cx/fbn/nevernote/signals/ENRelatedNotesSignal.java
new file mode 100644 (file)
index 0000000..0982842
--- /dev/null
@@ -0,0 +1,7 @@
+package cx.fbn.nevernote.signals;
+
+import com.trolltech.qt.QSignalEmitter;
+
+public class ENRelatedNotesSignal extends QSignalEmitter {
+       public Signal0 getENRelatedNotesFinished = new Signal0();
+}
diff --git a/src/cx/fbn/nevernote/threads/ENRelatedNotesRunner.java b/src/cx/fbn/nevernote/threads/ENRelatedNotesRunner.java
new file mode 100644 (file)
index 0000000..0240b45
--- /dev/null
@@ -0,0 +1,132 @@
+package cx.fbn.nevernote.threads;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import com.evernote.edam.error.EDAMNotFoundException;
+import com.evernote.edam.error.EDAMSystemException;
+import com.evernote.edam.error.EDAMUserException;
+import com.evernote.edam.limits.Constants;
+import com.evernote.edam.notestore.RelatedQuery;
+import com.evernote.edam.notestore.RelatedResult;
+import com.evernote.edam.notestore.RelatedResultSpec;
+import com.evernote.edam.type.Note;
+import com.evernote.thrift.TException;
+import com.trolltech.qt.core.QMutex;
+import com.trolltech.qt.core.QObject;
+
+import cx.fbn.nevernote.signals.ENRelatedNotesSignal;
+
+public class ENRelatedNotesRunner extends QObject implements Runnable{
+       private final SyncRunner syncRunner;
+//     public volatile Signal0 getENRelatedNotesFinished;
+       public volatile ENRelatedNotesSignal ENRelatedNotesSignal;
+//     private List<String> relatedNoteGuids;
+       public QMutex mutex;
+       private volatile boolean keepRunning;
+       private volatile LinkedBlockingQueue<String> guidQueue;
+       private volatile LinkedBlockingQueue<List<String>> resultQueue;
+       
+       public ENRelatedNotesRunner(SyncRunner syncRunner) {
+               this.syncRunner = syncRunner;
+//             this.relatedNoteGuids = new ArrayList<String>();
+//             this.getENRelatedNotesFinished = new Signal0();
+               this.ENRelatedNotesSignal = new ENRelatedNotesSignal();
+               this.mutex = new QMutex();
+               this.keepRunning = true;
+               this.guidQueue = new LinkedBlockingQueue<String>();
+               this.resultQueue = new LinkedBlockingQueue<List<String>>();
+       }
+
+       @Override
+       public void run() {
+               thread().setPriority(Thread.MIN_PRIORITY);
+               
+               while (keepRunning) {
+                       try {
+                               String guid = guidQueue.take();
+                               List<String> relatedNoteGuids = new ArrayList<String>();
+                               mutex.lock();
+                       
+                               List<Note> relatedNotes = getENRelatedNotes(guid);
+                               if (relatedNotes != null && !relatedNotes.isEmpty()) {
+                                       for (Note relatedNote : relatedNotes) {
+                                               relatedNoteGuids.add(relatedNote.getGuid());
+                                       }
+                               }
+                               
+                               resultQueue.offer(relatedNoteGuids);
+                               ENRelatedNotesSignal.getENRelatedNotesFinished.emit();
+                               mutex.unlock();
+                       } catch (InterruptedException e) {
+                               // TODO 自動生成された catch ブロック
+                               e.printStackTrace();
+                       }
+               }
+       }
+       
+       private List<Note> getENRelatedNotes(String guid) {
+               RelatedResult result = getENRelatedResult(guid);
+               List<Note> relatedNotes = new ArrayList<Note>();
+               
+               if (result != null) {
+                       relatedNotes = result.getNotes();
+               }
+               
+               return relatedNotes;
+       }
+       
+       private RelatedResult getENRelatedResult(String guid) {
+               RelatedQuery rquery = new RelatedQuery();
+               rquery.setNoteGuid(guid);
+               RelatedResultSpec resultSpec = new RelatedResultSpec();
+               resultSpec.setMaxNotes(Constants.EDAM_RELATED_MAX_NOTES);
+               if (syncRunner != null && syncRunner.localNoteStore != null) {
+                       try {
+                               RelatedResult result = syncRunner.localNoteStore.findRelated(syncRunner.authToken, rquery, resultSpec);
+                               return result;
+                       } catch (EDAMUserException e) {
+                               // TODO 自動生成された catch ブロック
+                               e.printStackTrace();
+                       } catch (EDAMSystemException e) {
+                               // TODO 自動生成された catch ブロック
+                               e.printStackTrace();
+                       } catch (EDAMNotFoundException e) {
+                               // TODO 自動生成された catch ブロック
+                               e.printStackTrace();
+                       } catch (TException e) {
+                               // TODO 自動生成された catch ブロック
+                               e.printStackTrace();
+                       }
+               }
+               return null;
+       }
+
+       public boolean isKeepRunning() {
+               return keepRunning;
+       }
+
+       public void setKeepRunning(boolean keepRunning) {
+               this.keepRunning = keepRunning;
+       }
+       
+       public synchronized boolean addGuid(String guid) {
+               if (guidQueue.offer(guid)) {
+                       return true;
+               }
+               
+               return false;
+       }
+       
+       public synchronized List<String> getENRelatedNoteGuids() {
+               try {
+                       return resultQueue.take();
+               } catch (InterruptedException e) {
+                       // TODO 自動生成された catch ブロック
+                       e.printStackTrace();
+               }
+               
+               return null;
+       }
+}