2 * This file is part of NeighborNote
3 * Copyright 2013 Yuki Takahashi
5 * This file may be licensed under the terms of of the
6 * GNU General Public License Version 2 (the ``GPL'').
8 * Software distributed under the License is distributed
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10 * express or implied. See the GPL for the specific language
11 * governing rights and limitations.
13 * You should have received a copy of the GPL along with this
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
15 * or write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 package cx.fbn.nevernote.gui;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
29 import com.evernote.edam.type.Note;
30 import com.trolltech.qt.QThread;
31 import com.trolltech.qt.core.QSize;
32 import com.trolltech.qt.core.Qt.MouseButton;
33 import com.trolltech.qt.gui.QAction;
34 import com.trolltech.qt.gui.QApplication;
35 import com.trolltech.qt.gui.QContextMenuEvent;
36 import com.trolltech.qt.gui.QListWidget;
37 import com.trolltech.qt.gui.QListWidgetItem;
38 import com.trolltech.qt.gui.QMenu;
40 import cx.fbn.nevernote.Global;
41 import cx.fbn.nevernote.NeverNote;
42 import cx.fbn.nevernote.sql.DatabaseConnection;
43 import cx.fbn.nevernote.threads.ENRelatedNotesRunner;
44 import cx.fbn.nevernote.threads.SyncRunner;
45 import cx.fbn.nevernote.utilities.ApplicationLogger;
46 import cx.fbn.nevernote.utilities.Pair;
48 public class RensoNoteList extends QListWidget {
49 private final DatabaseConnection conn;
50 private final ApplicationLogger logger;
51 private final HashMap<QListWidgetItem, String> rensoNoteListItems;
52 private final List<RensoNoteListItem> rensoNoteListTrueItems;
53 private String rensoNotePressedItemGuid;
54 private final QAction openNewTabAction;
55 private final QAction starAction;
56 private final QAction unstarAction;
57 private final QAction excludeNoteAction;
58 private final NeverNote parent;
59 private final QMenu menu;
60 private HashMap<String, Integer> mergedHistory; // マージされた操作履歴
61 private final SyncRunner syncRunner;
62 private final ENRelatedNotesRunner enRelatedNotesRunner;
63 private final QThread enRelatedNotesThread;
64 private final HashMap<String, List<String>> enRelatedNotesCache; // Evernote関連ノートのキャッシュ<guid, 関連ノートリスト>
66 private int allPointSum;
68 public RensoNoteList(DatabaseConnection c, NeverNote p, SyncRunner syncRunner, ApplicationLogger logger) {
70 this.logger.log(this.logger.HIGH, "Setting up rensoNoteList");
75 this.syncRunner = syncRunner;
77 this.guid = new String();
78 mergedHistory = new HashMap<String, Integer>();
79 enRelatedNotesCache = new HashMap<String, List<String>>();
80 this.enRelatedNotesRunner = new ENRelatedNotesRunner(this.syncRunner, this.logger);
81 this.enRelatedNotesRunner.enRelatedNotesSignal.getENRelatedNotesFinished.connect(this, "enRelatedNotesComplete()");
82 this.enRelatedNotesThread = new QThread(enRelatedNotesRunner, "ENRelatedNotes Thread");
83 this.getEnRelatedNotesThread().start();
85 rensoNoteListItems = new HashMap<QListWidgetItem, String>();
86 rensoNoteListTrueItems = new ArrayList<RensoNoteListItem>();
88 this.itemPressed.connect(this, "rensoNoteItemPressed(QListWidgetItem)");
91 menu = new QMenu(this);
93 openNewTabAction = new QAction(tr("Open in New Tab"), this);
94 openNewTabAction.setToolTip(tr("Open this note in new tab"));
95 openNewTabAction.triggered.connect(parent, "openNewTabFromRNL()");
97 starAction = new QAction(tr("Add Star"), this);
98 starAction.setToolTip(tr("Add Star to this item"));
99 starAction.triggered.connect(parent, "starNote()");
101 unstarAction = new QAction(tr("Remove Star"), this);
102 unstarAction.setToolTip(tr("Remove Star from this item"));
103 unstarAction.triggered.connect(parent, "unstarNote()");
105 excludeNoteAction = new QAction(tr("Exclude"), this);
106 excludeNoteAction.setToolTip(tr("Exclude this note from RensoNoteList"));
107 excludeNoteAction.triggered.connect(parent, "excludeNote()");
109 menu.addAction(openNewTabAction);
110 menu.addAction(excludeNoteAction);
111 menu.aboutToHide.connect(this, "contextMenuHidden()");
113 this.logger.log(this.logger.HIGH, "rensoNoteList setup complete");
117 // 現在開いているノートの連想ノートリストをリフレッシュ
118 public void refreshRensoNoteList() {
119 refreshRensoNoteList(guid);
123 public void refreshRensoNoteList(String guid) {
124 logger.log(logger.HIGH, "Entering RensoNoteList.refreshRensoNoteList guid = " + guid);
127 rensoNoteListItems.clear();
128 rensoNoteListTrueItems.clear();
129 mergedHistory = new HashMap<String, Integer>();
131 if (!this.isEnabled()) {
134 if (guid == null || guid.equals("")) {
139 // すでにEvernote関連ノートがキャッシュされているか確認
141 isCached = enRelatedNotesCache.containsKey(guid);
142 if (!isCached) { // キャッシュ無し
143 // Evernoteの関連ノートを別スレッドで取得させる
144 enRelatedNotesRunner.addGuid(guid);
146 List<String> relatedNoteGuids = enRelatedNotesCache.get(guid);
147 addENRelatedNotes(relatedNoteGuids);
150 calculateHistory(guid);
151 repaintRensoNoteList(false);
153 logger.log(logger.HIGH, "Leaving RensoNoteList.refreshRensoNoteList");
156 // 操作履歴をデータベースから取得してノートごとの関連度を算出、その後mergedHistoryに追加
157 private void calculateHistory(String guid) {
158 logger.log(logger.EXTREME, "Entering RensoNoteList.calculateHistory guid = " + guid);
160 // browseHistory<guid, 回数(ポイント)>
161 HashMap<String, Integer> browseHistory = conn.getHistoryTable().getBehaviorHistory("browse", guid);
162 addWeight(browseHistory, Global.getBrowseWeight());
163 mergedHistory = mergeHistory(browseHistory, mergedHistory);
165 // copy&pasteHistory<guid, 回数(ポイント)>
166 HashMap<String, Integer> copyAndPasteHistory = conn.getHistoryTable().getBehaviorHistory("copy & paste", guid);
167 addWeight(copyAndPasteHistory, Global.getCopyPasteWeight());
168 mergedHistory = mergeHistory(copyAndPasteHistory, mergedHistory);
170 // addNewNoteHistory<guid, 回数(ポイント)>
171 HashMap<String, Integer> addNewNoteHistory = conn.getHistoryTable().getBehaviorHistory("addNewNote", guid);
172 addWeight(addNewNoteHistory, Global.getAddNewNoteWeight());
173 mergedHistory = mergeHistory(addNewNoteHistory, mergedHistory);
175 // rensoItemClickHistory<guid, 回数(ポイント)>
176 HashMap<String, Integer> rensoItemClickHistory = conn.getHistoryTable().getBehaviorHistory("rensoItemClick", guid);
177 addWeight(rensoItemClickHistory, Global.getRensoItemClickWeight());
178 mergedHistory = mergeHistory(rensoItemClickHistory, mergedHistory);
180 // sameTagHistory<guid, 回数(ポイント)>
181 HashMap<String, Integer> sameTagHistory = conn.getHistoryTable().getBehaviorHistory("sameTag", guid);
182 addWeight(sameTagHistory, Global.getSameTagWeight());
183 mergedHistory = mergeHistory(sameTagHistory, mergedHistory);
185 // sameNotebookNoteHistory<guid, 回数(ポイント)>
186 HashMap<String, Integer> sameNotebookHistory = conn.getHistoryTable().getBehaviorHistory("sameNotebook", guid);
187 addWeight(sameNotebookHistory, Global.getSameNotebookWeight());
188 mergedHistory = mergeHistory(sameNotebookHistory, mergedHistory);
189 logger.log(logger.EXTREME, "Leaving RensoNoteList.calculateHistory");
193 private void addWeight(HashMap<String, Integer> history, int weight){
194 logger.log(logger.EXTREME, "Entering RensoNoteList.addWeight");
196 Set<String> keySet = history.keySet();
197 Iterator<String> hist_iterator = keySet.iterator();
198 while(hist_iterator.hasNext()){
199 String key = hist_iterator.next();
200 history.put(key, history.get(key) * weight);
203 logger.log(logger.EXTREME, "Leaving RensoNoteList.addWeight");
207 private void repaintRensoNoteList(boolean needClear) {
208 logger.log(logger.EXTREME, "Entering RensoNoteList.repaintRensoNoteList");
212 rensoNoteListItems.clear();
213 rensoNoteListTrueItems.clear();
216 if (!this.isEnabled()) {
220 // すべての関連ポイントの合計を取得(関連度のパーセント算出に利用)
222 for (int p : mergedHistory.values()) {
226 addRensoNoteList(mergedHistory);
228 logger.log(logger.EXTREME, "Leaving RensoNoteList.repaintRensoNoteList");
231 // 引数1と引数2をマージしたハッシュマップを返す
232 private HashMap<String, Integer> mergeHistory(HashMap<String, Integer> History1, HashMap<String, Integer> History2){
233 logger.log(logger.EXTREME, "Entering RensoNoteList.mergeHistory");
235 HashMap<String, Integer> mergedHistory = new HashMap<String, Integer>();
237 mergedHistory.putAll(History1);
239 Set<String> keySet = History2.keySet();
240 Iterator<String> hist2_iterator = keySet.iterator();
241 while(hist2_iterator.hasNext()){
242 String key = hist2_iterator.next();
243 if(mergedHistory.containsKey(key)){
244 mergedHistory.put(key, mergedHistory.get(key) + History2.get(key));
246 mergedHistory.put(key, History2.get(key));
250 logger.log(logger.EXTREME, "Leaving RensoNoteList.mergeHistory");
251 return mergedHistory;
254 // 連想ノートリストにハッシュマップのデータを追加
255 private void addRensoNoteList(HashMap<String, Integer> History){
256 logger.log(logger.EXTREME, "Entering RensoNoteList.addRensoNoteList");
258 String currentNoteGuid = new String(parent.getCurrentNoteGuid());
260 // スター付きノートとスター無しノートを分ける
261 HashMap<String, Integer> staredNotes = new HashMap<String, Integer>(); // スター付きノートのマップ
262 HashMap<String, Integer> normalNotes = new HashMap<String, Integer>(); // スター無しノートのマップ
263 for (String nextGuid: History.keySet()) {
264 int relationPoint = History.get(nextGuid);
265 boolean isStared = conn.getStaredTable().existNote(currentNoteGuid, nextGuid);
267 staredNotes.put(nextGuid, relationPoint);
269 normalNotes.put(nextGuid, relationPoint);
273 // 連想ノートリストアイテムの最大表示数まで繰り返す
274 for (int i = 0; i < Global.getRensoListItemMaximum(); i++) {
275 // スター付きノートがあれば先に処理する
276 HashMap<String, Integer> tmpMap = new HashMap<String, Integer>();
277 if (!staredNotes.isEmpty()) {
278 tmpMap = staredNotes;
279 }else if (!normalNotes.isEmpty()) {
280 tmpMap = normalNotes;
283 // 操作回数が多い順に取り出して連想ノートリストに追加する
284 if (!tmpMap.isEmpty()) {
286 String maxGuid = new String();
288 for (String nextGuid: tmpMap.keySet()) {
289 int relationPoint = tmpMap.get(nextGuid);
292 if (relationPoint > maxNum) {
293 maxNum = relationPoint;
298 // 次の最大値探索で邪魔なので最大値をHashMapから削除
299 tmpMap.remove(maxGuid);
301 // 関連度最大のノートがアクティブか確認
302 Note maxNote = conn.getNoteTable().getNote(maxGuid, true, false, false, false, true);
303 boolean isNoteActive = false;
304 if(maxNote != null) {
305 isNoteActive = maxNote.isActive();
308 // 存在していて、かつ関連度0でなければノート情報を取得して連想ノートリストに追加
309 if (isNoteActive && maxNum > 0) {
312 isStared = conn.getStaredTable().existNote(currentNoteGuid, maxGuid);
314 QListWidgetItem item = new QListWidgetItem();
315 RensoNoteListItem myItem = new RensoNoteListItem(maxNote, maxNum, isStared, allPointSum, conn, this);
316 item.setSizeHint(new QSize(0, 90));
318 this.setItemWidget(item, myItem);
319 rensoNoteListItems.put(item, maxGuid);
320 rensoNoteListTrueItems.add(myItem);
326 logger.log(logger.EXTREME, "Leaving RensoNoteList.addRensoNoteList");
329 // リストのアイテムから対象ノートのguidを取得
330 public String getNoteGuid(QListWidgetItem item) {
331 return rensoNoteListItems.get(item);
334 // 関連ノートリストの右クリックメニュー
336 public void contextMenuEvent(QContextMenuEvent event){
337 logger.log(logger.EXTREME, "Entering RensoNoteList.contextMenuEvent");
339 if (rensoNotePressedItemGuid == null || rensoNotePressedItemGuid.equals("")) {
343 // STAR, UNSTARがあれば、一度消す
344 List<QAction> menuActions = new ArrayList<QAction>(menu.actions());
345 if (menuActions.contains(starAction)) {
346 menu.removeAction(starAction);
348 if (menuActions.contains(unstarAction)) {
349 menu.removeAction(unstarAction);
352 // 対象アイテムがスター付きなら「UNSTAR」、スター無しなら「STAR」を追加
353 String currentNoteGuid = parent.getCurrentNoteGuid();
354 boolean isExist = conn.getStaredTable().existNote(currentNoteGuid, rensoNotePressedItemGuid);
356 menu.insertAction(excludeNoteAction, unstarAction);
358 menu.insertAction(excludeNoteAction, starAction);
362 menu.exec(event.globalPos());
364 rensoNotePressedItemGuid = null;
366 logger.log(logger.EXTREME, "Leaving RensoNoteList.contextMenuEvent");
369 // コンテキストメニューが表示されているかどうか
370 public boolean isContextMenuVisible() {
371 return menu.isVisible();
375 @SuppressWarnings("unused")
376 private void contextMenuHidden() {
377 for (int i = 0; i < rensoNoteListTrueItems.size(); i++) {
378 RensoNoteListItem item = rensoNoteListTrueItems.get(i);
379 item.setDefaultBackground();
383 // ユーザが連想ノートリストのアイテムを選択した時の処理
384 @SuppressWarnings("unused")
385 private void rensoNoteItemPressed(QListWidgetItem current) {
386 logger.log(logger.HIGH, "Entering RensoNoteList.rensoNoteItemPressed");
388 rensoNotePressedItemGuid = null;
390 if (QApplication.mouseButtons().isSet(MouseButton.RightButton)) {
391 rensoNotePressedItemGuid = getNoteGuid(current);
394 logger.log(logger.HIGH, "Leaving RensoNoteList.rensoNoteItemPressed");
397 // Evernoteの関連ノートの取得が完了
398 @SuppressWarnings("unused")
399 private void enRelatedNotesComplete() {
400 logger.log(logger.HIGH, "Entering RensoNoteList.enRelatedNotesComplete");
402 Pair<String, List<String>> enRelatedNoteGuidPair = enRelatedNotesRunner.getENRelatedNoteGuids(); // <元ノートguid, 関連ノートguidリスト>
404 if (enRelatedNoteGuidPair == null) {
408 String sourceGuid = enRelatedNoteGuidPair.getFirst();
409 List<String> enRelatedNoteGuids = enRelatedNoteGuidPair.getSecond();
412 if (sourceGuid != null && !sourceGuid.equals("") && enRelatedNoteGuids != null) { // Evernote関連ノートがnullでなければ
414 enRelatedNotesCache.put(sourceGuid, enRelatedNoteGuids);
416 if (!enRelatedNoteGuids.isEmpty()) { // Evernote関連ノートが存在していて
417 if (sourceGuid.equals(this.guid)) { // 取得したデータが今開いているノートの関連ノートなら
418 // mergedHistoryにEvernote関連ノートを追加してから再描画
419 addENRelatedNotes(enRelatedNoteGuids);
420 repaintRensoNoteList(true);
425 logger.log(logger.HIGH, "Leaving RensoNoteList.enRelatedNotesComplete");
428 // Evernote関連ノートの関連度情報をmergedHistoryに追加
429 private void addENRelatedNotes(List<String> relatedNoteGuids) {
430 logger.log(logger.EXTREME, "Entering RensoNoteList.addENRelatedNotes");
432 // Evernote関連ノート<guid, 関連ポイント>
433 HashMap<String, Integer> enRelatedNotes = new HashMap<String, Integer>();
435 for (String relatedGuid : relatedNoteGuids) {
436 enRelatedNotes.put(relatedGuid, Global.getENRelatedNotesWeight());
439 mergedHistory = mergeHistory(enRelatedNotes, mergedHistory);
441 logger.log(logger.EXTREME, "Leaving RensoNoteList.addENRelatedNotes");
444 // Evernoteの関連ノート取得スレッドを終了させる
445 public boolean stopThread() {
446 logger.log(logger.HIGH, "Entering RensoNoteList.stopThread");
448 if (enRelatedNotesRunner.addStop()) {
449 logger.log(logger.HIGH, "RensoNoteList.stopThread succeeded");
452 logger.log(logger.HIGH, "RensoNoteList.stopThread failed");
456 public QThread getEnRelatedNotesThread() {
457 return enRelatedNotesThread;