OSDN Git Service

Add automatic upgrade checking.
authorRandy Baumgarte <randy@fbn.cx>
Tue, 28 Dec 2010 15:35:03 +0000 (10:35 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Sat, 1 Jan 2011 23:40:29 +0000 (18:40 -0500)
src/cx/fbn/nevernote/Global.java
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/dialog/ConfigAppearancePage.java
src/cx/fbn/nevernote/dialog/ConfigDialog.java
src/cx/fbn/nevernote/dialog/UpgradeAvailableDialog.java [new file with mode: 0644]
src/cx/fbn/nevernote/gui/MainMenuBar.java

index 829bdea..0ed7d20 100644 (file)
@@ -1337,6 +1337,35 @@ public class Global {
                settings.endGroup();    \r
     }\r
 \r
+    //*******************\r
+    // Check version information\r
+    //*******************\r
+    public static boolean checkVersionUpgrade() {\r
+               settings.beginGroup("Upgrade");\r
+               String text = (String)settings.value("checkForUpdates", "true");\r
+               settings.endGroup();\r
+               if (text.equalsIgnoreCase("true"))\r
+                       return true;\r
+               else\r
+                       return false;   \r
+    }\r
+    public static void setCheckVersionUpgrade(boolean value) {\r
+               settings.beginGroup("Upgrade");\r
+               settings.setValue("checkForUpdates", value);\r
+               settings.endGroup();    \r
+    }\r
+    public static String getUpdatesAvailableUrl() {\r
+               settings.beginGroup("Upgrade");\r
+               String text = (String)settings.value("avialableUrl", "http://nevernote.sourceforge.net/versions.txt");\r
+               settings.endGroup();    \r
+               return text;\r
+    }\r
+    public static String getUpdateAnnounceUrl() {\r
+               settings.beginGroup("Upgrade");\r
+               String text = (String)settings.value("announceUrl", "http://nevernote.sourceforge.net/upgrade.html");\r
+               settings.endGroup();    \r
+               return text;\r
+    }\r
     \r
     //*******************\r
     // Index attachments\r
index 861d9fe..3ab1cfb 100644 (file)
@@ -128,6 +128,9 @@ import com.trolltech.qt.gui.QTableWidgetItem;
 import com.trolltech.qt.gui.QTextEdit;
 import com.trolltech.qt.gui.QToolBar;
 import com.trolltech.qt.gui.QTreeWidgetItem;
+import com.trolltech.qt.network.QNetworkAccessManager;
+import com.trolltech.qt.network.QNetworkReply;
+import com.trolltech.qt.network.QNetworkRequest;
 import com.trolltech.qt.webkit.QWebPage.WebAction;
 import com.trolltech.qt.webkit.QWebSettings;
 
@@ -151,6 +154,7 @@ import cx.fbn.nevernote.dialog.ShareNotebook;
 import cx.fbn.nevernote.dialog.StackNotebook;
 import cx.fbn.nevernote.dialog.TagEdit;
 import cx.fbn.nevernote.dialog.ThumbnailViewer;
+import cx.fbn.nevernote.dialog.UpgradeAvailableDialog;
 import cx.fbn.nevernote.dialog.WatchFolder;
 import cx.fbn.nevernote.filters.FilterEditorNotebooks;
 import cx.fbn.nevernote.filters.FilterEditorTags;
@@ -196,6 +200,7 @@ public class NeverNote extends QMainWindow{
        QAction                                 trayExitAction;                         // Exit the application
        QAction                                 trayShowAction;                         // toggle the show/hide action          
        QAction                                 trayAddNoteAction;                      // Add a note from the system tray
+       QNetworkAccessManager   versionChecker;                         // Used when checking for new versions
        
     NotebookTreeWidget                 notebookTree;                           // List of notebooks
     AttributeTreeWidget                attributeTree;                          // List of note attributes
@@ -691,7 +696,76 @@ public class NeverNote extends QMainWindow{
        int sortCol = Global.getSortColumn();
                int sortOrder = Global.getSortOrder();
                noteTableView.sortByColumn(sortCol, SortOrder.resolve(sortOrder));
-
+               if (Global.checkVersionUpgrade())
+                       checkForUpdates();
+       }
+       
+       public void checkForUpdates() {
+               // Send off thread to check for a new version
+               versionChecker = new QNetworkAccessManager(this);
+               versionChecker.finished.connect(this, "upgradeFileRead(QNetworkReply)");
+               QNetworkRequest request = new QNetworkRequest();
+               request.setUrl(new QUrl(Global.getUpdatesAvailableUrl()));
+               versionChecker.get(request);
+       }
+       private void upgradeFileRead(QNetworkReply reply) {
+               if (!reply.isReadable())
+                       return;
+               
+               String winVersion = Global.version;
+               String osxVersion = Global.version;
+               String linuxVersion = Global.version;
+               String linux64Version = Global.version;
+               String version = Global.version;
+               
+               // Determine the versions available
+               QByteArray data = reply.readLine();
+               while (data != null && !reply.atEnd()) {
+                       String line = data.toString();
+                       String lineVersion;
+                       if (line.contains(":")) 
+                               lineVersion = line.substring(line.indexOf(":")+1).replace(" ", "").replace("\n", "");
+                       else
+                               lineVersion = "";
+                       if (line.toLowerCase().contains("windows")) 
+                               winVersion = lineVersion;
+                       else if (line.toLowerCase().contains("os-x")) 
+                               osxVersion = lineVersion;
+                       else if (line.toLowerCase().contains("linux amd64")) 
+                               linux64Version = lineVersion;
+                       else if (line.toLowerCase().contains("linux i386")) 
+                               linuxVersion = lineVersion;
+                       else if (line.toLowerCase().contains("default")) 
+                               version = lineVersion;
+                       
+                       // Read the next line
+                       data = reply.readLine();
+               }
+               
+               // Now we need to determine what system we are on.
+               if (System.getProperty("os.name").toLowerCase().contains("windows"))
+                       version = winVersion;
+               if (System.getProperty("os.name").toLowerCase().contains("mac os"))
+                       version = osxVersion;
+               if (System.getProperty("os.name").toLowerCase().contains("Linux")) {
+                       if (System.getProperty("os.arch").contains("amd64") ||
+                               System.getProperty("os.arch").contains("x86_64") ||
+                               System.getProperty("os.arch").contains("i686"))
+                                       version = linux64Version;
+                       else
+                               version = linuxVersion;
+               }
+               
+               
+               if (Global.version.equals(version))
+                       return;
+               
+               UpgradeAvailableDialog dialog = new UpgradeAvailableDialog();
+               dialog.exec();
+               if (dialog.remindMe())
+                       Global.setCheckVersionUpgrade(true);
+               else
+                       Global.setCheckVersionUpgrade(false);
        }
 
        
index 4e95242..c2f8d49 100644 (file)
@@ -46,6 +46,7 @@ public class ConfigAppearancePage extends QWidget {
        private final QCheckBox showTrayIcon;\r
        private final QCheckBox verifyDelete;\r
        private final QCheckBox pdfPreview;\r
+       private final QCheckBox checkForUpdates;\r
        private final QCheckBox newNoteWithTags;\r
        private final QCheckBox mimicEvernote;\r
        private final QCheckBox startMinimized;\r
@@ -138,6 +139,7 @@ public class ConfigAppearancePage extends QWidget {
                verifyDelete = new QCheckBox(tr("Verify Deletes"));\r
                startMinimized = new QCheckBox(tr("Start Minimized"));\r
                pdfPreview = new QCheckBox(tr("Display PDF Documents Inline"));\r
+               checkForUpdates = new QCheckBox(tr("Check For Updates At Startup"));\r
                newNoteWithTags = new QCheckBox(tr("Create New Notes With Selected Tags"));\r
                \r
                QHBoxLayout autoSaveLayout = new QHBoxLayout();\r
@@ -160,6 +162,7 @@ public class ConfigAppearancePage extends QWidget {
                mainLayout.addWidget(verifyDelete);\r
                mainLayout.addWidget(pdfPreview);\r
                mainLayout.addWidget(newNoteWithTags);\r
+               mainLayout.addWidget(checkForUpdates);\r
                mainLayout.addStretch(1);\r
                setLayout(mainLayout);\r
                \r
@@ -293,6 +296,16 @@ public class ConfigAppearancePage extends QWidget {
        public boolean getPdfPreview() {\r
                return pdfPreview.isChecked();\r
        }\r
+       \r
+       //*******************************************\r
+       //* check for updates get/set\r
+       //*******************************************\r
+       public void setCheckForUpdates(boolean val) {\r
+               checkForUpdates.setChecked(val);        \r
+       }\r
+       public boolean getCheckForUpdates() {\r
+               return checkForUpdates.isChecked();\r
+       }\r
 \r
        \r
        //********************************************\r
index 917a91c..472ad73 100644 (file)
@@ -126,6 +126,12 @@ public class ConfigDialog extends QDialog {
                        Global.setPdfPreview(true);\r
                else\r
                        Global.setPdfPreview(false);\r
+\r
+               if (appearancePage.getCheckForUpdates())\r
+                       Global.setCheckVersionUpgrade(true);\r
+               else\r
+                       Global.setCheckVersionUpgrade(false);\r
+\r
                \r
                if (appearancePage.getNewNoteWithTags())\r
                        Global.setNewNoteWithSelectedTags(true);\r
@@ -298,6 +304,7 @@ public class ConfigDialog extends QDialog {
                appearancePage.setVerifyDelete(Global.verifyDelete());\r
                appearancePage.setStartMinimized(Global.startMinimized());\r
                appearancePage.setPdfPreview(Global.pdfPreview());\r
+               appearancePage.setCheckForUpdates(Global.checkVersionUpgrade());\r
                appearancePage.setNewNoteWithTags(Global.newNoteWithSelectedTags());\r
                appearancePage.setShowSplashScreen(Global.isWindowVisible("SplashScreen"));\r
                appearancePage.setTagBehavior(Global.tagBehavior());\r
diff --git a/src/cx/fbn/nevernote/dialog/UpgradeAvailableDialog.java b/src/cx/fbn/nevernote/dialog/UpgradeAvailableDialog.java
new file mode 100644 (file)
index 0000000..8e9493c
--- /dev/null
@@ -0,0 +1,92 @@
+/*\r
+ * This file is part of NeverNote \r
+ * Copyright 2009 Randy Baumgarte\r
+ * \r
+ * This file may be licensed under the terms of of the\r
+ * GNU General Public License Version 2 (the ``GPL'').\r
+ *\r
+ * Software distributed under the License is distributed\r
+ * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
+ * express or implied. See the GPL for the specific language\r
+ * governing rights and limitations.\r
+ *\r
+ * You should have received a copy of the GPL along with this\r
+ * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
+ * or write to the Free Software Foundation, Inc.,\r
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
+ *\r
+*/\r
+\r
+package cx.fbn.nevernote.dialog;\r
+\r
+\r
+import com.trolltech.qt.core.QUrl;\r
+import com.trolltech.qt.gui.QCheckBox;\r
+import com.trolltech.qt.gui.QDialog;\r
+import com.trolltech.qt.gui.QGridLayout;\r
+import com.trolltech.qt.gui.QHBoxLayout;\r
+import com.trolltech.qt.gui.QIcon;\r
+import com.trolltech.qt.gui.QPushButton;\r
+import com.trolltech.qt.gui.QVBoxLayout;\r
+import com.trolltech.qt.webkit.QWebView;\r
+\r
+import cx.fbn.nevernote.Global;\r
+\r
+public class UpgradeAvailableDialog extends QDialog {\r
+\r
+       private boolean         okPressed;\r
+       private final QPushButton ok;\r
+       private final QCheckBox doNotRemindeMe;\r
+       private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
+       \r
+       // Constructor\r
+       public UpgradeAvailableDialog() {\r
+               okPressed = false;\r
+               setWindowTitle(tr("Upgrade Available"));\r
+               setWindowIcon(new QIcon(iconPath+"nevernote.png"));\r
+               QVBoxLayout grid = new QVBoxLayout();\r
+               QGridLayout input = new QGridLayout();\r
+               QHBoxLayout button = new QHBoxLayout();\r
+               setLayout(grid);                \r
+                       \r
+               QWebView page = new QWebView(this);\r
+               page.setUrl(new QUrl(Global.getUpdateAnnounceUrl()));\r
+               \r
+               doNotRemindeMe = new QCheckBox();\r
+               doNotRemindeMe.setText(tr("Automatically check for updates at startup"));\r
+               input.addWidget(page,1,1);\r
+               doNotRemindeMe.setChecked(true);\r
+               input.addWidget(doNotRemindeMe,2,1);\r
+               \r
+               grid.addLayout(input);\r
+               \r
+               ok = new QPushButton("OK");\r
+               ok.clicked.connect(this, "okButtonPressed()");\r
+\r
+               button.addStretch();\r
+               button.addWidget(ok);\r
+               button.addStretch();\r
+               grid.addLayout(button);         \r
+       }\r
+\r
+       // The OK button was pressed\r
+       @SuppressWarnings("unused")\r
+       private void okButtonPressed() {\r
+               okPressed = true;\r
+               close();\r
+       }\r
+       // The CANCEL button was pressed\r
+       @SuppressWarnings("unused")\r
+       private void cancelButtonPressed() {\r
+               okPressed = false;\r
+               close();\r
+       }\r
+       // Check if the OK button was pressed\r
+       public boolean okPressed() {\r
+               return okPressed;\r
+       }\r
+       \r
+       public boolean remindMe() {\r
+               return doNotRemindeMe.isChecked();\r
+       }\r
+}\r
index 72e7311..3857ffe 100644 (file)
@@ -40,6 +40,7 @@ public class MainMenuBar extends QMenuBar {
        public QAction                  emptyTrashAction;                       // Action when a user wants to clear the trash file\r
        public QAction                  exitAction;                                     // Action when user selects "exit"\r
        public QAction                  aboutAction;                            // Action when a user selects "About"\r
+       public QAction                  checkForUpdates;                        // Check for newer versions\r
        public QAction                  loggerAction;                           // Action when a user selects "Log"\r
        public QAction                  releaseAction;                          // Release notes\r
 \r
@@ -598,6 +599,11 @@ public class MainMenuBar extends QMenuBar {
                releaseAction.triggered.connect(parent, "releaseNotes()");      \r
                setupShortcut(releaseAction, "About_Release_Notes");\r
                \r
+               checkForUpdates = new QAction(tr("Check For Updates"), this);\r
+               checkForUpdates.setToolTip("Check for newer versions");\r
+               checkForUpdates.triggered.connect(parent, "checkForUpdates()"); \r
+               setupShortcut(checkForUpdates, "Help_Check_For_Updates");\r
+               \r
                aboutAction = new QAction(tr("About"), this);\r
                aboutAction.setToolTip("About NeverNote");\r
                aboutAction.triggered.connect(parent, "about()"); \r
@@ -735,6 +741,7 @@ public class MainMenuBar extends QMenuBar {
 \r
                helpMenu = addMenu(tr("&Help"));\r
                helpMenu.addAction(releaseAction);\r
+               helpMenu.addAction(checkForUpdates);\r
                helpMenu.addAction(loggerAction);\r
                helpMenu.addSeparator();\r
                helpMenu.addAction(aboutAction);\r