OSDN Git Service

複数タブを開いた状態で終了したら、次回起動時にそれらのタブを復元する。
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / NeverNote.java
index 7a3e172..32fe99e 100644 (file)
@@ -1,6 +1,7 @@
 /*
-  * This file is part of NixNote 
+  * This file is part of NixNote/NeighborNote 
  * Copyright 2009 Randy Baumgarte
+ * Copyright 2013 Yuki Takahashi
  * 
  * This file may be licensed under the terms of of the
  * GNU General Public License Version 2 (the ``GPL'').
@@ -81,6 +82,7 @@ import com.trolltech.qt.core.QModelIndex;
 import com.trolltech.qt.core.QSize;
 import com.trolltech.qt.core.QTemporaryFile;
 import com.trolltech.qt.core.QTextCodec;
+import com.trolltech.qt.core.QTextStream;
 import com.trolltech.qt.core.QThreadPool;
 import com.trolltech.qt.core.QTimer;
 import com.trolltech.qt.core.QTranslator;
@@ -412,7 +414,7 @@ public class NeverNote extends QMainWindow{
                logger.log(logger.EXTREME, "Starting GUI build");
 
                QTranslator nevernoteTranslator = new QTranslator();
-               nevernoteTranslator.load(Global.getFileManager().getTranslateFilePath("nevernote_" + QLocale.system().name() + ".qm"));
+               nevernoteTranslator.load(Global.getFileManager().getTranslateFilePath("neighbornote_" + QLocale.system().name() + ".qm"));
                QApplication.instance().installTranslator(nevernoteTranslator);
 
                Global.originalPalette = QApplication.palette();
@@ -597,25 +599,13 @@ public class NeverNote extends QMainWindow{
                tabWindows = new HashMap<Integer, TabBrowse>();
                tabBrowser = new TabBrowserWidget(this);
                tabBrowser.setStyleSheet("QTabBar::tab{width:150px;}");
-               TabBrowse tab = new TabBrowse(conn, tabBrowser, cbObserver);
-               browserWindow = tab.getBrowserWindow();
-               int index = tabBrowser.addNewTab(tab, "");
-               tabWindows.put(index, tab);
+               tabBrowser.setMovable(true);
                tabBrowser.setTabsClosable(true);
                tabBrowser.currentChanged.connect(this, "tabWindowChanged(int)");
                tabBrowser.tabCloseRequested.connect(this, "tabWindowClosing(int)");
                
-               noteDirty = new HashMap<Integer, Boolean>();
-               noteDirty.put(index, false);
-               
                inkNote = new HashMap<Integer, Boolean>();
                readOnly = new HashMap<Integer, Boolean>();
-
-               // ICHANGED
-               // 履歴記録のハッシュマップを初期化
-               historyGuids.put(index, new ArrayList<String>());
-               historyPosition.put(index, 0);
-               fromHistory.put(index, false);
                
         mainLeftRightSplitter.addWidget(leftSplitter1);
         mainLeftRightSplitter.addWidget(browserIndexSplitter);
@@ -735,13 +725,21 @@ public class NeverNote extends QMainWindow{
                noteTableView.setVisible(Global.isWindowVisible("noteList"));
                menuBar.hideNoteList.setChecked(Global.isWindowVisible("noteList"));
                
-               if (!Global.isWindowVisible("editorButtonBar"))
+               // ICHANGED
+               if (!Global.isWindowVisible("editorButtonBar")) {
+                       menuBar.showEditorBar.setChecked(false);
                        toggleEditorButtonBar();
+               }
                
                if (!Global.isWindowVisible("leftPanel"))
                        menuBar.hideLeftSide.setChecked(true);
-               if (Global.isWindowVisible("noteInformation"))
+               
+               // ICHANGED
+               if (Global.isWindowVisible("noteInformation")) {
+                       menuBar.noteAttributes.setChecked(true);
                        toggleNoteInformation();
+               }
+               
                quotaBar.setVisible(Global.isWindowVisible("quota"));
                // IFIXED quotaBar.isVisible() → Global.isWindowVisible("quota")
                // なぜかquotaBar.isVisible()が常にfalseを返すようなので修正
@@ -781,23 +779,75 @@ public class NeverNote extends QMainWindow{
                trayIcon.setToolTip(tr("NeighborNote"));
                trayIcon.setContextMenu(trayMenu);
                trayIcon.activated.connect(this, "trayActivated(com.trolltech.qt.gui.QSystemTrayIcon$ActivationReason)");
-
-               currentNoteGuid="";
-               currentNoteGuid = Global.getLastViewedNoteGuid();
-               if (currentNoteGuid.equals(""))
-                       currentNote = new Note();
                
-               // ICHANGED
-               /* 上に移動したので要らない
-               historyGuids = new ArrayList<String>();
-               historyPosition = 0;
-               fromHistory = false;
-               */
+               // 前回のタブを復元
+               noteTableView.load(true);
+               HashMap<Integer, String> tabs = new HashMap<Integer, String>();
+               if (Global.getLastViewedTabs() != null) {
+                       tabs.putAll(Global.getLastViewedTabs());
+               }
                
-               if (!currentNoteGuid.trim().equals("")) {
+               Collection<String>              guids = tabs.values();
+               Iterator<String>                guidIterator = guids.iterator();
+               Collection<Integer>             indexes = tabs.keySet();
+               Iterator<Integer>               indexIterator = indexes.iterator();
+               noteDirty = new HashMap<Integer, Boolean>();
+               
+               while (guidIterator.hasNext()) {
+                       String guid = guidIterator.next();
+                       int tabIndex = indexIterator.next();
+                       
+                       currentNoteGuid = guid;
+                       noteDirty.put(tabIndex, false);
+                       if (!currentNoteGuid.trim().equals("")) {
+                               currentNote = conn.getNoteTable().getNote(currentNoteGuid, true, true, false, false, true);
+                               if (currentNote != null) {
+                                       openTabEditor(guid, tabIndex);
+                               }
+                       }
+               }
+               
+               // タブが0個だったら作る
+               if (tabBrowser.count() <= 0) {
+                       TabBrowse tab = new TabBrowse(conn, tabBrowser, cbObserver);
+                       browserWindow = tab.getBrowserWindow();
+                       tabBrowser.currentChanged.disconnect();
+                       int index = tabBrowser.addNewTab(tab, "");
+                       tabBrowser.currentChanged.connect(this, "tabWindowChanged(int)");
+                       tabWindows.put(index, tab);
+                       noteDirty.put(index, false);
+                       
+                       // 履歴記録のハッシュマップを初期化
+                       historyGuids.put(index, new ArrayList<String>());
+                       historyPosition.put(index, 0);
+                       fromHistory.put(index, false);
+               }
+               
+               currentNoteGuid = Global.getLastViewedNoteGuid();
+               if (currentNoteGuid.trim().equals("")) {
+                       currentNote = new Note();
+               } else {
                        currentNote = conn.getNoteTable().getNote(currentNoteGuid, true,true,false,false,true);
                }
                
+               // タブのフォーカスをcurrentNoteGuidのノートに移す
+               if (tabBrowser.count() >= 2) {
+                       Collection<TabBrowse>   tabBrowsers = tabWindows.values();
+                       Iterator<TabBrowse>             tabBrowserIterator = tabBrowsers.iterator();
+                       Collection<Integer>             tabIndexes = tabWindows.keySet();
+                       Iterator<Integer>               tabIndexIterator = tabIndexes.iterator();
+                       
+                       while (tabBrowserIterator.hasNext()) {
+                               TabBrowse tab = tabBrowserIterator.next();
+                               int i = tabIndexIterator.next();
+                               String guid = tab.getBrowserWindow().getNote().getGuid();
+                               if (currentNoteGuid.equals(guid)) {
+                                       tabBrowser.setCurrentIndex(i);
+                                       break;
+                               }
+                       }
+               }
+               
                noteIndexUpdated(true);
                showColumns();
                menuBar.showEditorBar.setChecked(Global.isWindowVisible("editorButtonBar"));
@@ -908,6 +958,11 @@ public class NeverNote extends QMainWindow{
                        // ICHANGED TODO とりあえず封印
                        // checkForUpdates();
                }
+               
+               // ICHANGED
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               }
        }
        
        
@@ -1262,6 +1317,20 @@ public class NeverNote extends QMainWindow{
                
                Global.saveWindowMaximized(isMaximized());
                Global.saveCurrentNoteGuid(currentNoteGuid);
+               
+               // 開いていたタブ群をsettingsに保存しておく
+               Collection<TabBrowse>           tabBrowsers = tabWindows.values();
+               Iterator<TabBrowse>                     tabIterator = tabBrowsers.iterator();
+               Collection<Integer>                     tabIndexes = tabWindows.keySet();
+               Iterator<Integer>                       indexIterator = tabIndexes.iterator();
+               HashMap<Integer, String>        tabs = new HashMap<Integer, String>();
+               while (tabIterator.hasNext()) {
+                       TabBrowse tab = tabIterator.next();
+                       int index = indexIterator.next();
+                       String guid = tab.getBrowserWindow().getNote().getGuid();
+                       tabs.put(index, guid);
+               }
+               Global.setLastViewedTabs(tabs);
                        
                int sortCol = noteTableView.proxyModel.sortColumn();
                int sortOrder = noteTableView.proxyModel.sortOrder().value();
@@ -2988,7 +3057,12 @@ public class NeverNote extends QMainWindow{
                if (!file.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly,
                 QIODevice.OpenModeFlag.Text)))
                        return;
-               textBox.setText(file.readAll().toString());
+               // ICHANGED 日本語文字化け対策
+               QTextCodec codec = QTextCodec.codecForName("UTF-8");
+               QTextStream textStream = new QTextStream(file);
+               textStream.setCodec(codec);
+               textBox.setText(textStream.readAll().toString());
+               
                file.close();
                dialog.setWindowTitle(tr("Release Notes"));
                dialog.setLayout(layout);
@@ -3569,6 +3643,12 @@ public class NeverNote extends QMainWindow{
                                        
                Global.showDeleted = true;
        }
+       
+       menuBar.noteAddNewTab.setEnabled(newButton.isEnabled());
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               }
+       
        listManager.loadNotesIndex();
        noteIndexUpdated(false);
 ////           browserWindow.setEnabled(newButton.isEnabled());
@@ -3588,6 +3668,10 @@ public class NeverNote extends QMainWindow{
                listManager.setSelectedSavedSearch("");
                newButton.setEnabled(!newButton.isEnabled());
                menuBar.noteAdd.setEnabled(newButton.isEnabled());
+               menuBar.noteAddNewTab.setEnabled(newButton.isEnabled());
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               }
                menuBar.noteAdd.setVisible(true);
                browserWindow.clear();
                
@@ -3626,6 +3710,11 @@ public class NeverNote extends QMainWindow{
                Global.showDeleted = false;
        newButton.setEnabled(true);
        menuBar.noteAdd.setEnabled(true);
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               } else {
+                       menuBar.noteAddNewTab.setEnabled(true);
+               }
        menuBar.noteAdd.setVisible(true);
                trashTree.blockSignals(true);
                trashTree.clearSelection();
@@ -3950,6 +4039,9 @@ public class NeverNote extends QMainWindow{
                menuBar.noteOnlineHistoryAction.setEnabled(true);
                menuBar.noteMergeAction.setEnabled(true);
                selectedNoteGUIDs.clear();
+               if (currentNoteGuid != null && !currentNoteGuid.equals("") && !Global.showDeleted) {
+                       menuBar.noteAddNewTab.setEnabled(true);
+               }
                if (selections.size() != 1 || Global.showDeleted) {
                        menuBar.noteDuplicateAction.setEnabled(false);
                }
@@ -4750,17 +4842,36 @@ public class NeverNote extends QMainWindow{
                        conn.getHistoryTable().addHistory("rensoItemClick", prevCurrentNoteGuid, rensoNotePressedItemGuid);
                }
        }
-
-       // ICHANGED
+       
        private void openTabEditor(String guid) {
-               
+               openTabEditor(guid, -1);
+       }
+
+       // オーバーロード insertIndex < 0 ならば普通に追加
+       private void openTabEditor(String guid, int insertIndex) {
                Note note = conn.getNoteTable().getNote(guid, true, true, false, true, true);
                // 新しいタブエディタを作成
                TabBrowse newBrowser = new TabBrowse(conn, tabBrowser, cbObserver);
                showEditorButtons(newBrowser.getBrowserWindow());
                
                String noteTitle = note.getTitle();
-               int index = tabBrowser.addNewTab(newBrowser, noteTitle);
+               int index;
+               // 最初のタブだけ、追加後に自動的にフォーカスが移ってしまうので、一度切断する
+               if (tabBrowser.count() == 0) {
+                       tabBrowser.currentChanged.disconnect();
+                       if (insertIndex < 0) {
+                               index = tabBrowser.addNewTab(newBrowser, noteTitle);
+                       } else {
+                               index = tabBrowser.insertNewTab(insertIndex, newBrowser, noteTitle);
+                       }
+                       tabBrowser.currentChanged.connect(this, "tabWindowChanged(int)");
+               } else {
+                       if (insertIndex < 0) {
+                               index = tabBrowser.addNewTab(newBrowser, noteTitle);
+                       } else {
+                               index = tabBrowser.insertNewTab(insertIndex, newBrowser, noteTitle);
+                       }
+               }
                tabWindows.put(index, newBrowser);
                noteDirty.put(index, false);
                
@@ -4796,6 +4907,8 @@ public class NeverNote extends QMainWindow{
                                addBrowseHistory();
                        }
                }
+               
+               rensoNoteList.refreshRensoNoteList(currentNoteGuid);
        }
 
        // ICHANGED タブが閉じられた
@@ -4826,6 +4939,8 @@ public class NeverNote extends QMainWindow{
                tabWindows.remove(index);
                tabBrowser.removeTab(index);
                noteDirty.remove(index);
+               inkNote.remove(index);
+               readOnly.remove(index);
 
                // 履歴記録のハッシュマップを削除
                historyGuids.remove(index);
@@ -4842,6 +4957,14 @@ public class NeverNote extends QMainWindow{
                        boolean isNoteDirty = noteDirty.get(i + 1);
                        noteDirty.put(i, isNoteDirty);
                        noteDirty.remove(i + 1);
+                       // inkNote
+                       boolean isInkNote = inkNote.get(i + 1);
+                       inkNote.put(i, isInkNote);
+                       inkNote.remove(i + 1);
+                       // readOnly
+                       boolean isReadOnly = readOnly.get(i + 1);
+                       readOnly.put(i, isReadOnly);
+                       readOnly.remove(i + 1);
                        // historyGuids
                        ArrayList<String> histGuids = historyGuids.get(i + 1);
                        historyGuids.put(i, histGuids);
@@ -4866,15 +4989,18 @@ public class NeverNote extends QMainWindow{
                
                // ノート追加前に開いていたノートとの関連性を記録するためにguidをとっておく
                TabBrowse prevTab = (TabBrowse)tabBrowser.currentWidget();
-               String prevTabGuid = prevTab.getBrowserWindow().getNote().getGuid();
+               String prevTabGuid = null;
+               if (prevTab.getBrowserWindow() != null && prevTab.getBrowserWindow().getNote() != null) {
+                       prevTabGuid = prevTab.getBrowserWindow().getNote().getGuid();
+               }
                
                openEmptyTabEditor();
                addNote();
                
                // 追加されたノートのguidを取得し、ノート追加操作履歴としてデータベースに登録
-               TabBrowse addedTab = (TabBrowse)tabBrowser.currentWidget();
-               String addedTabGuid = addedTab.getBrowserWindow().getNote().getGuid();
                if (prevTabGuid != null && !prevTabGuid.equals("")) {
+                       TabBrowse addedTab = (TabBrowse)tabBrowser.currentWidget();
+                       String addedTabGuid = addedTab.getBrowserWindow().getNote().getGuid();
                        if (addedTabGuid != null && !addedTabGuid.equals("")) {
                                if (!prevTabGuid.equals(addedTabGuid)) {
                                        conn.getHistoryTable().addHistory("addNewNote", prevTabGuid, addedTabGuid);
@@ -5399,6 +5525,7 @@ public class NeverNote extends QMainWindow{
                }
                if (selectedNoteGUIDs.size() == 0 && !currentNoteGuid.equals("")) 
                        selectedNoteGUIDs.add(currentNoteGuid);
+               closeTabs(selectedNoteGUIDs);
                for (int i=0; i<selectedNoteGUIDs.size(); i++) {
                        listManager.deleteNote(selectedNoteGUIDs.get(i));
                }
@@ -5434,6 +5561,7 @@ public class NeverNote extends QMainWindow{
                                }
                        }
                }
+                       closeTabs(selectedNoteGUIDs);
                        listManager.expungeNote(selectedNoteGUIDs.get(i));
                        
                        // ICHANGED
@@ -5444,33 +5572,20 @@ public class NeverNote extends QMainWindow{
                }
        }
        currentNoteGuid = "";
-       
-       // ICHANGED ↓↓↓ここから↓↓↓
-               // 削除したノートを外部ウィンドウで開いていたら、閉じる
-               Collection<ExternalBrowse>      windows = externalWindows.values();
-               Iterator<ExternalBrowse>        windowIterator = windows.iterator();
-               Collection<String>                      guids = externalWindows.keySet();
-               Iterator<String>                        guidIterator = guids.iterator();
-               List<ExternalBrowse>            closeWindows = new ArrayList<ExternalBrowse>(); // イテレータ操作中に中身をいじっちゃダメなので
-               
-               while (windowIterator.hasNext()) {
-                       ExternalBrowse browser = windowIterator.next();
-                       String guid = guidIterator.next();
-                       
-                       for (int i = 0; i < selectedNoteGUIDs.size(); i++) {
-                               if (guid.equals(selectedNoteGUIDs.get(i))) {
-                                       closeWindows.add(browser);
-                               }
-                       }
+       closeExternalWindows(selectedNoteGUIDs);
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
                }
                
-               for (int i = closeWindows.size() - 1; i >= 0; i--) {
-                       closeWindows.get(i).close();
-               }
-               // ICHANGED ↑↑↑ここまで↑↑↑
-               
-       // ICHANGED ↓↓↓ここから↓↓↓
-       // 削除したノートをタブで開いていたら、閉じる
+       listManager.loadNotesIndex();
+       noteIndexUpdated(false);
+       refreshEvernoteNote(true);
+       scrollToGuid(currentNoteGuid);
+       logger.log(logger.HIGH, "Leaving NeverNote.deleteNote");
+    }
+    
+    // 対象ノートをタブで開いていたら閉じる
+    private void closeTabs(List<String> noteGUIDs) {
                Collection<TabBrowse> tabBrowsers = tabWindows.values();
                Iterator<TabBrowse> tabIterator = tabBrowsers.iterator();
                Collection<Integer> tabIndexes = tabWindows.keySet();
@@ -5482,8 +5597,8 @@ public class NeverNote extends QMainWindow{
                        int index = indexIterator.next();
                        String guid = tab.getBrowserWindow().getNote().getGuid();
                        
-                       for(int i = 0; i < selectedNoteGUIDs.size(); i++){
-                               if(guid.equals(selectedNoteGUIDs.get(i))){
+                       for(int i = 0; i < noteGUIDs.size(); i++){
+                               if(guid.equals(noteGUIDs.get(i))){
                                        closeIndexes.add(index);
                                }
                        }
@@ -5492,16 +5607,33 @@ public class NeverNote extends QMainWindow{
                for(int i = closeIndexes.size() - 1; i >= 0; i--){
                        tabWindowClosing(closeIndexes.get(i));
                }
-               // ICHANGED ↑↑↑ここまで↑↑↑              
-       
-       listManager.loadNotesIndex();
-       noteIndexUpdated(false);
-       refreshEvernoteNote(true);
-       scrollToGuid(currentNoteGuid);
-       logger.log(logger.HIGH, "Leaving NeverNote.deleteNote");
     }
+    
+    // 対象ノートを外部ウィンドウで開いていたら閉じる
+    private void closeExternalWindows(List<String> noteGUIDs) {
+               Collection<ExternalBrowse>      windows = externalWindows.values();
+               Iterator<ExternalBrowse>        windowIterator = windows.iterator();
+               Collection<String>                      guids = externalWindows.keySet();
+               Iterator<String>                        guidIterator = guids.iterator();
+               List<ExternalBrowse>            closeWindows = new ArrayList<ExternalBrowse>(); // イテレータ操作中に中身をいじっちゃダメなので
+               
+               while (windowIterator.hasNext()) {
+                       ExternalBrowse browser = windowIterator.next();
+                       String guid = guidIterator.next();
+                       
+                       for (int i = 0; i < noteGUIDs.size(); i++) {
+                               if (guid.equals(noteGUIDs.get(i))) {
+                                       closeWindows.add(browser);
+                               }
+                       }
+               }
+               
+               for (int i = closeWindows.size() - 1; i >= 0; i--) {
+                       closeWindows.get(i).close();
+               }
+    }
+    
     // Add a new note
-    // ICHANGED @SuppressWarnings("unused") を削除
        private void addNote() {
        logger.log(logger.HIGH, "Inside NeverNote.addNote");
 //     browserWindow.setEnabled(true);
@@ -5706,7 +5838,9 @@ public class NeverNote extends QMainWindow{
                // ICHANGED
                for(int i = 0; i < tabBrowser.count(); i++){
                        TabBrowse b = (TabBrowse)tabBrowser.widget(i);
-                       b.getBrowserWindow().getNote().setGuid(newGuid);
+                       if (b.getBrowserWindow().getNote().getGuid().equals(oldGuid)) {
+                               b.getBrowserWindow().getNote().setGuid(newGuid);
+                       }
                }
 
        for (int i=0; i<listManager.getNoteIndex().size(); i++) {
@@ -7508,6 +7642,10 @@ public class NeverNote extends QMainWindow{
        // toggle the add buttons
        newButton.setEnabled(!newButton.isEnabled());
        menuBar.noteAdd.setEnabled(newButton.isEnabled());
+       menuBar.noteAddNewTab.setEnabled(newButton.isEnabled());
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               }
        menuBar.noteAdd.setVisible(true);
        
        if (!toDeleted) {       // 生存ノートテーブルへ
@@ -7659,4 +7797,37 @@ public class NeverNote extends QMainWindow{
        public String getCurrentNoteGuid() {
                return currentNoteGuid;
        }
+       
+       @SuppressWarnings("unused")
+       // タブ入れ替えによってタブインデックスが変わったので、インデックスで管理しているハッシュマップ達も入れ替える
+       private void tabIndexChanged(int from, int to) {
+               // tabWindows
+               TabBrowse tab = tabWindows.get(from);
+               tabWindows.put(from, tabWindows.get(to));
+               tabWindows.put(to, tab);
+               // noteDirty
+               boolean isNoteDirty = noteDirty.get(from);
+               noteDirty.put(from, noteDirty.get(to));
+               noteDirty.put(to, isNoteDirty);
+               // inkNote
+               boolean isInkNote = inkNote.get(from);
+               inkNote.put(from, inkNote.get(to));
+               inkNote.put(to, isInkNote);
+               // readOnly
+               boolean isReadOnly = readOnly.get(from);
+               readOnly.put(from, readOnly.get(to));
+               readOnly.put(to, isReadOnly);
+               // historyGuids
+               ArrayList<String> histGuids = historyGuids.get(from);
+               historyGuids.put(from, historyGuids.get(to));
+               historyGuids.put(to, histGuids);
+               // historyPosition
+               int histPosition = historyPosition.get(from);
+               historyPosition.put(from, historyPosition.get(to));
+               historyPosition.put(to, histPosition);
+               // fromHistory
+               boolean fromHist = fromHistory.get(from);
+               fromHistory.put(from,  fromHistory.get(to));
+               fromHistory.put(to, fromHist);
+       }
 }