OSDN Git Service

帯域制限超過時のメッセージを更新した
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / NeverNote.java
index 022276a..0a1a9a2 100644 (file)
@@ -42,6 +42,7 @@ import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.SortedMap;
 import java.util.Vector;
 
@@ -599,7 +600,7 @@ public class NeverNote extends QMainWindow{
                int index = tabBrowser.addNewTab(tab, "");
                tabWindows.put(index, tab);
                tabBrowser.currentChanged.connect(this, "tabWindowChanged(int)");
-               tabBrowser.tabCloseRequested.connect(this, "tabWindowClosing(int)");
+               tabBrowser.tabCloseRequested.connect(this, "tabCloseRequested(int)");
                
                noteDirty = new HashMap<Integer, Boolean>();
                noteDirty.put(index, false);
@@ -2999,7 +3000,7 @@ public class NeverNote extends QMainWindow{
                QMessageBox.about(this, 
                                                tr("About NeighborNote"),
                                                tr("<h4><center><b>NeighborNote</b></center></h4><hr><center>Version ")
-                                               +Global.version + "(based on NixNote 1.5)"
+                                               +Global.version + "(based on NixNote 1.6)"
                                                //+"1.2.120724"
                                                +tr("<hr>"
                                                                +"Open Source Evernote Client.<br><br>" 
@@ -3201,14 +3202,14 @@ public class NeverNote extends QMainWindow{
        QIcon prevIcon = new QIcon(iconPath+"back.png");
        prevButton.setIcon(prevIcon);
        prevButton.triggered.connect(this, "previousViewedAction()");   
-       togglePrevArrowButton(Global.isToolbarButtonVisible("prevArrow"));
+       togglePrevArrowButton(Global.isToolbarButtonVisible("prevArrow", true));
        
        nextButton = toolBar.addAction(tr(""));
        nextButton.setToolTip(tr("Next"));
        QIcon nextIcon = new QIcon(iconPath+"forward.png");
        nextButton.setIcon(nextIcon);
        nextButton.triggered.connect(this, "nextViewedAction()");       
-       toggleNextArrowButton(Global.isToolbarButtonVisible("nextArrow"));
+       toggleNextArrowButton(Global.isToolbarButtonVisible("nextArrow", true));
        
        toolBar.addSeparator();
        
@@ -3216,62 +3217,67 @@ public class NeverNote extends QMainWindow{
        QIcon upIcon = new QIcon(iconPath+"up.png");
        upButton.setIcon(upIcon);
        upButton.triggered.connect(this, "upAction()");         
-       toggleUpArrowButton(Global.isToolbarButtonVisible("upArrow"));
+       toggleUpArrowButton(Global.isToolbarButtonVisible("upArrow", false));
 
        
        downButton = toolBar.addAction(tr("Down"));
        QIcon downIcon = new QIcon(iconPath+"down.png");
        downButton.setIcon(downIcon);
        downButton.triggered.connect(this, "downAction()");
-       toggleDownArrowButton(Global.isToolbarButtonVisible("downArrow"));
+       toggleDownArrowButton(Global.isToolbarButtonVisible("downArrow", false));
        
        synchronizeButton = toolBar.addAction(tr("Synchronize"));
        synchronizeButton.setIcon(new QIcon(iconPath+"synchronize.png"));
        synchronizeIconAngle = 0;
        synchronizeButton.triggered.connect(this, "evernoteSync()");
-       toggleSynchronizeButton(Global.isToolbarButtonVisible("synchronize"));
+       toggleSynchronizeButton(Global.isToolbarButtonVisible("synchronize", true));
        
        printButton = toolBar.addAction(tr("Print"));
        QIcon printIcon = new QIcon(iconPath+"print.png");
        printButton.setIcon(printIcon);
        printButton.triggered.connect(this, "printNote()");
-       togglePrintButton(Global.isToolbarButtonVisible("print"));
+       togglePrintButton(Global.isToolbarButtonVisible("print", false));
 
        tagButton = toolBar.addAction(tr("Tag")); 
        QIcon tagIcon = new QIcon(iconPath+"tag.png");
        tagButton.setIcon(tagIcon);
        tagButton.triggered.connect(browserWindow, "modifyTags()");
-       toggleTagButton(Global.isToolbarButtonVisible("tag"));
+       toggleTagButton(Global.isToolbarButtonVisible("tag", false));
 
        attributeButton = toolBar.addAction(tr("Attributes")); 
        QIcon attributeIcon = new QIcon(iconPath+"attribute.png");
        attributeButton.setIcon(attributeIcon);
        attributeButton.triggered.connect(this, "toggleNoteAttributes()");
-       toggleAttributeButton(Global.isToolbarButtonVisible("attribute"));
+       toggleAttributeButton(Global.isToolbarButtonVisible("attribute", true));
                
        emailButton = toolBar.addAction(tr("Email"));
        QIcon emailIcon = new QIcon(iconPath+"email.png");
        emailButton.setIcon(emailIcon);
        emailButton.triggered.connect(this, "emailNote()");
-       toggleEmailButton(Global.isToolbarButtonVisible("email"));
+       toggleEmailButton(Global.isToolbarButtonVisible("email", false));
 
        deleteButton = toolBar.addAction(tr("Delete"));         
        QIcon deleteIcon = new QIcon(iconPath+"delete.png");
        deleteButton.setIcon(deleteIcon);
        deleteButton.triggered.connect(this, "deleteNote()");
-       toggleDeleteButton(Global.isToolbarButtonVisible("delete"));
+       toggleDeleteButton(Global.isToolbarButtonVisible("delete", true));
 
        newButton = toolBar.addAction(tr("New"));
        QIcon newIcon = new QIcon(iconPath+"new.png");
-       newButton.triggered.connect(this, "addNote()");
+       if (Global.toolBarNewAction()) {
+               newButton.triggered.connect(this, "noteAddNewTab()");
+       } else {
+               newButton.triggered.connect(this, "addNote()");
+       }
+
        newButton.setIcon(newIcon);
-       toggleNewButton(Global.isToolbarButtonVisible("new"));
+       toggleNewButton(Global.isToolbarButtonVisible("new", true));
        
        allNotesButton = toolBar.addAction(tr("All Notes"));
        QIcon allIcon = new QIcon(iconPath+"books.png");
        allNotesButton.triggered.connect(this, "allNotes()");
        allNotesButton.setIcon(allIcon);
-       toggleAllNotesButton(Global.isToolbarButtonVisible("allNotes"));
+       toggleAllNotesButton(Global.isToolbarButtonVisible("allNotes", true));
        
        //toolBar.addSeparator();
        //toolBar.addWidget(new QLabel(tr("Quota:")));
@@ -3356,9 +3362,9 @@ public class NeverNote extends QMainWindow{
        contextMenu.addAction(allNotesAction);
        allNotesAction.triggered.connect(this, "toggleAllNotesButton(Boolean)");
        
-       QAction searchClearAction = addContextAction("searchClear", tr("Search Clear"));
-       contextMenu.addAction(searchClearAction);
-       searchClearAction.triggered.connect(this, "toggleSearchClearButton(Boolean)");
+//     QAction searchClearAction = addContextAction("searchClear", tr("Search Clear"));
+//     contextMenu.addAction(searchClearAction);
+//     searchClearAction.triggered.connect(this, "toggleSearchClearButton(Boolean)");
        
        return contextMenu;
        
@@ -3367,7 +3373,7 @@ public class NeverNote extends QMainWindow{
        QAction newAction = new QAction(this);
                newAction.setText(name);
                newAction.setCheckable(true);
-               newAction.setChecked(Global.isToolbarButtonVisible(config));
+               newAction.setChecked(Global.isToolbarButtonVisible(config, true));
                return newAction;
     }
     private void togglePrevArrowButton(Boolean toggle) {
@@ -3418,11 +3424,11 @@ public class NeverNote extends QMainWindow{
                allNotesButton.setVisible(toggle);
                Global.saveToolbarButtonsVisible("allNotes", toggle);
     }
-    @SuppressWarnings("unused")
-       private void toggleSearchClearButton(Boolean toggle) {
-               searchClearButton.setVisible(toggle);
-               Global.saveToolbarButtonsVisible("searchClear", toggle);
-    }
+//    @SuppressWarnings("unused")
+//     private void toggleSearchClearButton(Boolean toggle) {
+//             searchClearButton.setVisible(toggle);
+//             Global.saveToolbarButtonsVisible("searchClear", toggle);
+//    }
 
 
 
@@ -3674,7 +3680,7 @@ public class NeverNote extends QMainWindow{
        OAuthTokenizer tokenizer = new OAuthTokenizer();
        AESEncrypter aes = new AESEncrypter();
        try {
-                       aes.decrypt(new FileInputStream(Global.getFileManager().getHomeDirFile("oauth.txt")));
+                       aes.decrypt(new FileInputStream(Global.getFileManager().getHomeDirFile("oauthkey.txt")));
                } catch (FileNotFoundException e) {
                        // File not found, so we'll just get empty strings anyway. 
                }
@@ -3737,7 +3743,7 @@ public class NeverNote extends QMainWindow{
                }
                aes.setString(window.response);
                try {
-                               aes.encrypt(new FileOutputStream(Global.getFileManager().getHomeDirFile("oauth.txt")));
+                               aes.encrypt(new FileOutputStream(Global.getFileManager().getHomeDirFile("oauthkey.txt")));
                        } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
@@ -4802,22 +4808,28 @@ public class NeverNote extends QMainWindow{
                        }
                }
        }
+       
+       // タブが閉じられた
+       @SuppressWarnings("unused")
+       private void tabCloseRequested(int index) {
+               tabWindowClosing((TabBrowse)tabBrowser.widget(index));
+       }
 
        // タブが閉じられた
-       private void tabWindowClosing(int index) {
+       private void tabWindowClosing(TabBrowse tab) {
                // タブが1つしかなかったら閉じない
                if (tabBrowser.count() <= 1) {
                        return;
                }
 
-               TabBrowse t = (TabBrowse) tabBrowser.widget(index);
-               String guid = t.getBrowserWindow().getNote().getGuid();
-               String content = t.getBrowserWindow().getContent();
-               BrowserWindow browser = t.getBrowserWindow();
-               // ノートが変更されていたら保存
-               if (t.getNoteDirty()) {
-                       saveNoteTabBrowser(guid, content, true, browser);
-               }
+               int index = tabBrowser.indexOf(tab);
+//             String guid = tab.getBrowserWindow().getNote().getGuid();
+//             String content = tab.getBrowserWindow().getContent();
+               BrowserWindow browser = tab.getBrowserWindow();
+//             // ノートが変更されていたら保存
+//             if (tab.getNoteDirty()) {
+//                     saveNoteTabBrowser(guid, content, true, browser);
+//             }
 
                // シグナル切断
                browser.noteSignal.tagsChanged.disconnect();
@@ -4842,8 +4854,8 @@ public class NeverNote extends QMainWindow{
                // タブのインデックスを更新(削除によって空いた部分を詰める)
                for(int i = index ; tabWindows.containsKey(i + 1) ; i++){
                        // tabWindows
-                       TabBrowse tab = tabWindows.get(i + 1);
-                       tabWindows.put(i, tab);
+                       TabBrowse nextTab = tabWindows.get(i + 1);
+                       tabWindows.put(i, nextTab);
                        tabWindows.remove(i + 1);
                        // noteDirty
                        boolean isNoteDirty = noteDirty.get(i + 1);
@@ -4884,6 +4896,12 @@ public class NeverNote extends QMainWindow{
        private void noteAddNewTab() {
                saveNote();
                
+               // ノートを何も開いていないときは現在のタブにノート追加
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       addNote();
+                       return;
+               }
+               
                // ノート追加前に開いていたノートとの関連性を記録するためにguidをとっておく
                TabBrowse prevTab = (TabBrowse)tabBrowser.currentWidget();
                String prevTabGuid = null;
@@ -5035,17 +5053,17 @@ public class NeverNote extends QMainWindow{
        
     }
     
-       private void saveNoteTabBrowser(String guid, String content, Boolean save,
-                       BrowserWindow browser) {
-               QTextCodec codec = QTextCodec.codecForName("UTF-8");
-               QByteArray unicode = codec.fromUnicode(content);
-               noteCache.remove(guid);
-               noteCache.put(guid, unicode.toString());
-               if (save) {
-                       thumbnailRunner.addWork("GENERATE " + guid);
-                       saveNote(guid, browser);
-               }
-       }
+//     private void saveNoteTabBrowser(String guid, String content, Boolean save,
+//                     BrowserWindow browser) {
+//             QTextCodec codec = QTextCodec.codecForName("UTF-8");
+//             QByteArray unicode = codec.fromUnicode(content);
+//             noteCache.remove(guid);
+//             noteCache.put(guid, unicode.toString());
+//             if (save) {
+//                     thumbnailRunner.addWork("GENERATE " + guid);
+//                     saveNote(guid, browser);
+//             }
+//     }
        
     private void saveNote() {
        // すべてのタブに対して、Dirtyを確認し、trueならセーブする
@@ -5386,6 +5404,7 @@ public class NeverNote extends QMainWindow{
                return;
        if (currentNoteGuid.equals(""))
                return;
+       
        String title = null;
        if (selectedNoteGUIDs.size() == 1)
                title = conn.getNoteTable().getNote(selectedNoteGUIDs.get(0),false,false,false,false,false).getTitle();
@@ -5408,12 +5427,17 @@ public class NeverNote extends QMainWindow{
                                        return;
                        }
                }
-               if (selectedNoteGUIDs.size() == 0 && !currentNoteGuid.equals("")) 
+               if (selectedNoteGUIDs.size() == 0 && !currentNoteGuid.equals("")) {
                        selectedNoteGUIDs.add(currentNoteGuid);
+               }
+               
+               List<String> deleteNoteGUIDs = new ArrayList<String>(selectedNoteGUIDs);        // タブを閉じるとselectedNoteGUIDsが変わってしまうのでその前にコピー
                closeTabs(selectedNoteGUIDs);
-               for (int i=0; i<selectedNoteGUIDs.size(); i++) {
-                       listManager.deleteNote(selectedNoteGUIDs.get(i));
+               for (String guid : deleteNoteGUIDs) {
+                       listManager.deleteNote(guid);
                }
+               
+               closeExternalWindows(deleteNoteGUIDs);
        } else { 
                // If we are deleting from the trash.
                if (Global.verifyDelete()) {
@@ -5434,29 +5458,31 @@ public class NeverNote extends QMainWindow{
                }
                if (selectedNoteGUIDs.size() == 0 && !currentNoteGuid.equals("")) 
                        selectedNoteGUIDs.add(currentNoteGuid);
-               for (int i=selectedNoteGUIDs.size()-1; i>=0; i--) {
+               
+               List<String> deleteNoteGUIDs = new ArrayList<String>(selectedNoteGUIDs);        // タブを閉じるとselectedNoteGUIDsが変わってしまうのでその前にコピー
+               for (int i=deleteNoteGUIDs.size()-1; i>=0; i--) {
                        for (int j=listManager.getNoteTableModel().rowCount()-1; j>=0; j--) {
                        QModelIndex modelIndex =  listManager.getNoteTableModel().index(j, Global.noteTableGuidPosition);
                        if (modelIndex != null) {
                                SortedMap<Integer, Object> ix = listManager.getNoteTableModel().itemData(modelIndex);
                                String tableGuid =  (String)ix.values().toArray()[0];
-                               if (tableGuid.equals(selectedNoteGUIDs.get(i))) {
+                               if (tableGuid.equals(deleteNoteGUIDs.get(i))) {
                                        listManager.getNoteTableModel().removeRow(j);
                                        j=-1;
                                }
                        }
                }
-                       closeTabs(selectedNoteGUIDs);
-                       listManager.expungeNote(selectedNoteGUIDs.get(i));
-                       
-                       conn.getHistoryTable().expungeHistory(selectedNoteGUIDs.get(i));
-                       conn.getExcludedTable().expungeExcludedNote(selectedNoteGUIDs.get(i));
-                       conn.getStaredTable().expungeStaredNote(selectedNoteGUIDs.get(i));
+                       closeTab(deleteNoteGUIDs.get(i));
+                       listManager.expungeNote(deleteNoteGUIDs.get(i));
                        
+                       conn.getHistoryTable().expungeHistory(deleteNoteGUIDs.get(i));
+                       conn.getExcludedTable().expungeExcludedNote(deleteNoteGUIDs.get(i));
+                       conn.getStaredTable().expungeStaredNote(deleteNoteGUIDs.get(i));
                }
+               
+               closeExternalWindows(deleteNoteGUIDs);
        }
-       currentNoteGuid = "";
-       closeExternalWindows(selectedNoteGUIDs);
+       
                if (currentNoteGuid == null || currentNoteGuid.equals("")) {
                        menuBar.noteAddNewTab.setEnabled(false);
                }
@@ -5470,50 +5496,42 @@ public class NeverNote extends QMainWindow{
     
     // 対象ノートをタブで開いていたら閉じる
     private void closeTabs(List<String> noteGUIDs) {
-               Collection<TabBrowse> tabBrowsers = tabWindows.values();
-               Iterator<TabBrowse> tabIterator = tabBrowsers.iterator();
-               Collection<Integer> tabIndexes = tabWindows.keySet();
-               Iterator<Integer>       indexIterator = tabIndexes.iterator();
-               List<Integer> closeIndexes = new ArrayList<Integer>();  //イテレータ操作中に中身をいじっちゃダメなので
-
-               while (tabIterator.hasNext()) {
-                       TabBrowse tab = tabIterator.next();
-                       int index = indexIterator.next();
-                       String guid = tab.getBrowserWindow().getNote().getGuid();
-                       
-                       for(int i = 0; i < noteGUIDs.size(); i++){
-                               if(guid.equals(noteGUIDs.get(i))){
-                                       closeIndexes.add(index);
-                               }
-                       }
-               }
-               
-               for(int i = closeIndexes.size() - 1; i >= 0; i--){
-                       tabWindowClosing(closeIndexes.get(i));
-               }
+       for (String guid : noteGUIDs) {
+               closeTab(guid);
+       }
+    }
+    
+    // 対象ノートをタブで開いていたら閉じる
+    private void closeTab(String noteGUID) {
+       List<TabBrowse> closeTabs = new ArrayList<TabBrowse>();
+       
+       for (TabBrowse tab : tabWindows.values()) {
+               String guid = tab.getBrowserWindow().getNote().getGuid();
+               
+               if (guid.equals(noteGUID)) {
+                       closeTabs.add(tab);
+               }
+       }
+       
+       for (TabBrowse tab : closeTabs) {
+               tabWindowClosing(tab);
+       }
     }
     
     // 対象ノートを外部ウィンドウで開いていたら閉じる
     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>(); // イテレータ操作中に中身をいじっちゃダメなので
+               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 (Map.Entry<String, ExternalBrowse> e : externalWindows.entrySet()) {
+                       for (String guid : noteGUIDs) {
+                               if (guid.equals(e.getKey())) {
+                                       closeWindows.add(e.getValue());
                                }
                        }
                }
                
-               for (int i = closeWindows.size() - 1; i >= 0; i--) {
-                       closeWindows.get(i).close();
+               for (ExternalBrowse externalBrowse : closeWindows) {
+                       externalBrowse.close();
                }
     }
     
@@ -5962,26 +5980,18 @@ public class NeverNote extends QMainWindow{
                }
                
        // マージしたノート(child)をタブで開いていたら、閉じる
-               Collection<TabBrowse> tabBrowsers = tabWindows.values();
-               Iterator<TabBrowse> tabIterator = tabBrowsers.iterator();
-               Collection<Integer> tabIndexes = tabWindows.keySet();
-               Iterator<Integer>       indexIterator = tabIndexes.iterator();
-               List<Integer> closeIndexes = new ArrayList<Integer>();
-
-               while (tabIterator.hasNext()) {
-                       TabBrowse tab = tabIterator.next();
-                       int tabIndex = indexIterator.next();
+               List<TabBrowse> closeTabs = new ArrayList<TabBrowse>();
+               for (TabBrowse tab : tabWindows.values()) {
                        String guid = tab.getBrowserWindow().getNote().getGuid();
                        
-                       for(int i = 0; i < sources.size(); i++){
-                               if(guid.equals(sources.get(i))){
-                                       closeIndexes.add(tabIndex);
+                       for (String source : sources) {
+                               if (guid.equals(source)) {
+                                       closeTabs.add(tab);
                                }
                        }
                }
-               
-               for(int i = closeIndexes.size() - 1; i >= 0; i--){
-                       tabWindowClosing(closeIndexes.get(i));
+               for (TabBrowse tab : closeTabs) {
+                       tabWindowClosing(tab);
                }
                
                noteIndexUpdated(false);
@@ -6154,7 +6164,7 @@ public class NeverNote extends QMainWindow{
                        return;
                } catch (EDAMSystemException e) {
                        if (e.getErrorCode() == EDAMErrorCode.RATE_LIMIT_REACHED) {
-                               QMessageBox.warning(this, tr("Rate limit reached"), tr("Rate limit reached.\nRetry your request in " + e.getRateLimitDuration() + " seconds."));
+                               QMessageBox.warning(this, tr("Rate limit reached"), tr("Evernote usage has been temporarily exceeded. Please try again in ") +  + e.getRateLimitDuration() + tr(" seconds."));
                        }
                        setMessage("EDAMSystemException: " +e.getMessage());
                        return;
@@ -6216,7 +6226,7 @@ public class NeverNote extends QMainWindow{
                                return null;
                        } catch (EDAMSystemException e) {
                                if (e.getErrorCode() == EDAMErrorCode.RATE_LIMIT_REACHED) {
-                                       QMessageBox.warning(this, tr("Rate limit reached"), tr("Rate limit reached.\nRetry your request in " + e.getRateLimitDuration() + " seconds."));
+                                       QMessageBox.warning(this, tr("Rate limit reached"), tr("Evernote usage has been temporarily exceeded. Please try again in ") +  + e.getRateLimitDuration() + tr(" seconds."));
                                }
                                setMessage("EDAMSystemException: " +e.getMessage());
                                waitCursor(false);
@@ -6283,7 +6293,7 @@ public class NeverNote extends QMainWindow{
                        return;
                } catch (EDAMSystemException e) {
                        if (e.getErrorCode() == EDAMErrorCode.RATE_LIMIT_REACHED) {
-                               QMessageBox.warning(this, tr("Rate limit reached"), tr("Rate limit reached.\nRetry your request in " + e.getRateLimitDuration() + " seconds."));
+                               QMessageBox.warning(this, tr("Rate limit reached"), tr("Evernote usage has been temporarily exceeded. Please try again in ") +  + e.getRateLimitDuration() + tr(" seconds."));
                        }
                        setMessage("EDAMSystemException: " +e.getMessage());
                        return;
@@ -7720,6 +7730,12 @@ public class NeverNote extends QMainWindow{
        // 帯域制限の超過をユーザに通知
        @SuppressWarnings("unused")
        private void informRateLimit(Integer rateLimitDuration) {
-               QMessageBox.warning(this, tr("Rate limit reached"), tr("Rate limit reached.\nRetry your request in " + rateLimitDuration + " seconds."));
+               QMessageBox.warning(this, tr("Rate limit reached"), tr("Evernote usage has been temporarily exceeded. Please try again in ") +  + rateLimitDuration + tr(" seconds."));
+       }
+       
+       // ツールバーの「新規」ボタンの接続スロットを設定
+       public void connectNewButtonSlot(String slot) {
+               newButton.triggered.disconnect();
+               newButton.triggered.connect(this, slot);
        }
 }